blob: 368181d8b0ce35d7838ee0e4f48c200c087aed1c [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 Iwai352f7f92012-12-19 12:52:06 +0100887 if (!enable)
888 path->active = false;
889
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100890 /* make sure the widget is powered up */
Takashi Iwai967b1302015-03-20 18:21:03 +0100891 if (enable && (spec->power_down_unused || codec->power_save_node))
892 path_power_update(codec, path, codec->power_save_node);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100893
Takashi Iwai352f7f92012-12-19 12:52:06 +0100894 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100895 hda_nid_t nid = path->path[i];
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100896
Takashi Iwai352f7f92012-12-19 12:52:06 +0100897 if (enable && path->multi[i])
Takashi Iwai8f0972d2014-01-27 16:26:16 +0100898 snd_hda_codec_update_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100899 AC_VERB_SET_CONNECT_SEL,
900 path->idx[i]);
901 if (has_amp_in(codec, path, i))
902 activate_amp_in(codec, path, i, enable, add_aamix);
903 if (has_amp_out(codec, path, i))
904 activate_amp_out(codec, path, i, enable);
905 }
906
907 if (enable)
908 path->active = true;
909}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100910EXPORT_SYMBOL_GPL(snd_hda_activate_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100911
Takashi Iwai55196ff2013-01-24 17:32:56 +0100912/* if the given path is inactive, put widgets into D3 (only if suitable) */
913static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
914{
915 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100916
Takashi Iwai967b1302015-03-20 18:21:03 +0100917 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
Takashi Iwai55196ff2013-01-24 17:32:56 +0100918 return;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100919 sync_power_state_change(codec, path_power_update(codec, path, true));
Takashi Iwai55196ff2013-01-24 17:32:56 +0100920}
921
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100922/* turn on/off EAPD on the given pin */
923static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
924{
925 struct hda_gen_spec *spec = codec->spec;
926 if (spec->own_eapd_ctl ||
927 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
928 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200929 if (spec->keep_eapd_on && !enable)
930 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100931 if (codec->inv_eapd)
932 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100933 snd_hda_codec_update_cache(codec, pin, 0,
934 AC_VERB_SET_EAPD_BTLENABLE,
935 enable ? 0x02 : 0x00);
936}
937
Takashi Iwai3e367f12013-01-23 17:07:23 +0100938/* re-initialize the path specified by the given path index */
939static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
940{
941 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
942 if (path)
943 snd_hda_activate_path(codec, path, path->active, false);
944}
945
Takashi Iwai352f7f92012-12-19 12:52:06 +0100946
947/*
948 * Helper functions for creating mixer ctl elements
949 */
950
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200951static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
952 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200953static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
954 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200955
Takashi Iwai352f7f92012-12-19 12:52:06 +0100956enum {
957 HDA_CTL_WIDGET_VOL,
958 HDA_CTL_WIDGET_MUTE,
959 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100960};
961static const struct snd_kcontrol_new control_templates[] = {
962 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200963 /* only the put callback is replaced for handling the special mute */
964 {
965 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
966 .subdevice = HDA_SUBDEV_AMP_FLAG,
967 .info = snd_hda_mixer_amp_switch_info,
968 .get = snd_hda_mixer_amp_switch_get,
969 .put = hda_gen_mixer_mute_put, /* replaced */
970 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
971 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200972 {
973 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
974 .info = snd_hda_mixer_amp_switch_info,
975 .get = snd_hda_mixer_bind_switch_get,
976 .put = hda_gen_bind_mute_put, /* replaced */
977 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
978 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100979};
980
981/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100982static struct snd_kcontrol_new *
983add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100984 int cidx, unsigned long val)
985{
986 struct snd_kcontrol_new *knew;
987
Takashi Iwai12c93df2012-12-19 14:38:33 +0100988 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100989 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100990 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100991 knew->index = cidx;
992 if (get_amp_nid_(val))
993 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
994 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100995 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100996}
997
998static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
999 const char *pfx, const char *dir,
1000 const char *sfx, int cidx, unsigned long val)
1001{
Takashi Iwai975cc022013-06-28 11:56:49 +02001002 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01001003 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01001004 if (!add_control(spec, type, name, cidx, val))
1005 return -ENOMEM;
1006 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001007}
1008
1009#define add_pb_vol_ctrl(spec, type, pfx, val) \
1010 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1011#define add_pb_sw_ctrl(spec, type, pfx, val) \
1012 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1013#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1014 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1015#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1016 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1017
1018static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1019 unsigned int chs, struct nid_path *path)
1020{
1021 unsigned int val;
1022 if (!path)
1023 return 0;
1024 val = path->ctls[NID_PATH_VOL_CTL];
1025 if (!val)
1026 return 0;
1027 val = amp_val_replace_channels(val, chs);
1028 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1029}
1030
1031/* return the channel bits suitable for the given path->ctls[] */
1032static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1033 int type)
1034{
1035 int chs = 1; /* mono (left only) */
1036 if (path) {
1037 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1038 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1039 chs = 3; /* stereo */
1040 }
1041 return chs;
1042}
1043
1044static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1045 struct nid_path *path)
1046{
1047 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1048 return add_vol_ctl(codec, pfx, cidx, chs, path);
1049}
1050
1051/* create a mute-switch for the given mixer widget;
1052 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1053 */
1054static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1055 unsigned int chs, struct nid_path *path)
1056{
1057 unsigned int val;
1058 int type = HDA_CTL_WIDGET_MUTE;
1059
1060 if (!path)
1061 return 0;
1062 val = path->ctls[NID_PATH_MUTE_CTL];
1063 if (!val)
1064 return 0;
1065 val = amp_val_replace_channels(val, chs);
1066 if (get_amp_direction_(val) == HDA_INPUT) {
1067 hda_nid_t nid = get_amp_nid_(val);
1068 int nums = snd_hda_get_num_conns(codec, nid);
1069 if (nums > 1) {
1070 type = HDA_CTL_BIND_MUTE;
1071 val |= nums << 19;
1072 }
1073 }
1074 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1075}
1076
1077static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1078 int cidx, struct nid_path *path)
1079{
1080 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1081 return add_sw_ctl(codec, pfx, cidx, chs, path);
1082}
1083
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001084/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001085static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1086 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001087{
1088 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1089 struct hda_gen_spec *spec = codec->spec;
1090
1091 if (spec->auto_mute_via_amp) {
1092 hda_nid_t nid = get_amp_nid(kcontrol);
1093 bool enabled = !((spec->mute_bits >> nid) & 1);
1094 ucontrol->value.integer.value[0] &= enabled;
1095 ucontrol->value.integer.value[1] &= enabled;
1096 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001097}
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001098
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001099static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1100 struct snd_ctl_elem_value *ucontrol)
1101{
1102 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001103 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1104}
1105
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001106static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1107 struct snd_ctl_elem_value *ucontrol)
1108{
1109 sync_auto_mute_bits(kcontrol, ucontrol);
1110 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1111}
1112
Takashi Iwai247d85e2013-01-17 16:18:11 +01001113/* any ctl assigned to the path with the given index? */
1114static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1115{
1116 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1117 return path && path->ctls[ctl_type];
1118}
1119
Takashi Iwai352f7f92012-12-19 12:52:06 +01001120static const char * const channel_name[4] = {
1121 "Front", "Surround", "CLFE", "Side"
1122};
1123
1124/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +01001125static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1126 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001127{
Takashi Iwai247d85e2013-01-17 16:18:11 +01001128 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001129 struct auto_pin_cfg *cfg = &spec->autocfg;
1130
1131 *index = 0;
1132 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001133 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001134 return spec->vmaster_mute.hook ? "PCM" : "Master";
1135
1136 /* if there is really a single DAC used in the whole output paths,
1137 * use it master (or "PCM" if a vmaster hook is present)
1138 */
1139 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1140 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1141 return spec->vmaster_mute.hook ? "PCM" : "Master";
1142
Takashi Iwai247d85e2013-01-17 16:18:11 +01001143 /* multi-io channels */
1144 if (ch >= cfg->line_outs)
1145 return channel_name[ch];
1146
Takashi Iwai352f7f92012-12-19 12:52:06 +01001147 switch (cfg->line_out_type) {
1148 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001149 /* if the primary channel vol/mute is shared with HP volume,
1150 * don't name it as Speaker
1151 */
1152 if (!ch && cfg->hp_outs &&
1153 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1154 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001155 if (cfg->line_outs == 1)
1156 return "Speaker";
1157 if (cfg->line_outs == 2)
1158 return ch ? "Bass Speaker" : "Speaker";
1159 break;
1160 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001161 /* if the primary channel vol/mute is shared with spk volume,
1162 * don't name it as Headphone
1163 */
1164 if (!ch && cfg->speaker_outs &&
1165 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1166 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001167 /* for multi-io case, only the primary out */
1168 if (ch && spec->multi_ios)
1169 break;
1170 *index = ch;
1171 return "Headphone";
David Henningsson03ad6a82014-10-16 15:33:45 +02001172 case AUTO_PIN_LINE_OUT:
1173 /* This deals with the case where we have two DACs and
1174 * one LO, one HP and one Speaker */
1175 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1176 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1177 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1178 if (hp_lo_shared && spk_lo_shared)
1179 return spec->vmaster_mute.hook ? "PCM" : "Master";
1180 if (hp_lo_shared)
1181 return "Headphone+LO";
1182 if (spk_lo_shared)
1183 return "Speaker+LO";
1184 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001185 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001186
1187 /* for a single channel output, we don't have to name the channel */
1188 if (cfg->line_outs == 1 && !spec->multi_ios)
David Henningsson3abb4f42014-10-16 15:33:46 +02001189 return "Line Out";
Takashi Iwai247d85e2013-01-17 16:18:11 +01001190
Takashi Iwai352f7f92012-12-19 12:52:06 +01001191 if (ch >= ARRAY_SIZE(channel_name)) {
1192 snd_BUG();
1193 return "PCM";
1194 }
1195
1196 return channel_name[ch];
1197}
1198
1199/*
1200 * Parse output paths
1201 */
1202
1203/* badness definition */
1204enum {
1205 /* No primary DAC is found for the main output */
1206 BAD_NO_PRIMARY_DAC = 0x10000,
1207 /* No DAC is found for the extra output */
1208 BAD_NO_DAC = 0x4000,
1209 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001210 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001211 /* No individual DAC for extra output */
1212 BAD_NO_EXTRA_DAC = 0x102,
1213 /* No individual DAC for extra surrounds */
1214 BAD_NO_EXTRA_SURR_DAC = 0x101,
1215 /* Primary DAC shared with main surrounds */
1216 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001217 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001218 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001219 /* Primary DAC shared with main CLFE */
1220 BAD_SHARED_CLFE = 0x10,
1221 /* Primary DAC shared with extra surrounds */
1222 BAD_SHARED_EXTRA_SURROUND = 0x10,
1223 /* Volume widget is shared */
1224 BAD_SHARED_VOL = 0x10,
1225};
1226
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001227/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001228 * volume and mute controls, and assign the values to ctls[].
1229 *
1230 * When no appropriate widget is found in the path, the badness value
1231 * is incremented depending on the situation. The function returns the
1232 * total badness for both volume and mute controls.
1233 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001234static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001235{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001236 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001237 hda_nid_t nid;
1238 unsigned int val;
1239 int badness = 0;
1240
1241 if (!path)
1242 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001243
1244 if (path->ctls[NID_PATH_VOL_CTL] ||
1245 path->ctls[NID_PATH_MUTE_CTL])
1246 return 0; /* already evaluated */
1247
Takashi Iwai352f7f92012-12-19 12:52:06 +01001248 nid = look_for_out_vol_nid(codec, path);
1249 if (nid) {
1250 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001251 if (spec->dac_min_mute)
1252 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001253 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1254 badness += BAD_SHARED_VOL;
1255 else
1256 path->ctls[NID_PATH_VOL_CTL] = val;
1257 } else
1258 badness += BAD_SHARED_VOL;
1259 nid = look_for_out_mute_nid(codec, path);
1260 if (nid) {
1261 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1262 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1263 nid_has_mute(codec, nid, HDA_OUTPUT))
1264 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1265 else
1266 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1267 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1268 badness += BAD_SHARED_VOL;
1269 else
1270 path->ctls[NID_PATH_MUTE_CTL] = val;
1271 } else
1272 badness += BAD_SHARED_VOL;
1273 return badness;
1274}
1275
Takashi Iwai98bd1112013-03-22 14:53:50 +01001276const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001277 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1278 .no_dac = BAD_NO_DAC,
1279 .shared_primary = BAD_NO_PRIMARY_DAC,
1280 .shared_surr = BAD_SHARED_SURROUND,
1281 .shared_clfe = BAD_SHARED_CLFE,
1282 .shared_surr_main = BAD_SHARED_SURROUND,
1283};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001284EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001285
Takashi Iwai98bd1112013-03-22 14:53:50 +01001286const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001287 .no_primary_dac = BAD_NO_DAC,
1288 .no_dac = BAD_NO_DAC,
1289 .shared_primary = BAD_NO_EXTRA_DAC,
1290 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1291 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1292 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1293};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001294EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001295
Takashi Iwai7385df62013-01-07 09:50:52 +01001296/* get the DAC of the primary output corresponding to the given array index */
1297static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1298{
1299 struct hda_gen_spec *spec = codec->spec;
1300 struct auto_pin_cfg *cfg = &spec->autocfg;
1301
1302 if (cfg->line_outs > idx)
1303 return spec->private_dac_nids[idx];
1304 idx -= cfg->line_outs;
1305 if (spec->multi_ios > idx)
1306 return spec->multi_io[idx].dac;
1307 return 0;
1308}
1309
1310/* return the DAC if it's reachable, otherwise zero */
1311static inline hda_nid_t try_dac(struct hda_codec *codec,
1312 hda_nid_t dac, hda_nid_t pin)
1313{
1314 return is_reachable_path(codec, dac, pin) ? dac : 0;
1315}
1316
Takashi Iwai352f7f92012-12-19 12:52:06 +01001317/* try to assign DACs to pins and return the resultant badness */
1318static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1319 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001320 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001321 const struct badness_table *bad)
1322{
1323 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001324 int i, j;
1325 int badness = 0;
1326 hda_nid_t dac;
1327
1328 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return 0;
1330
Takashi Iwai352f7f92012-12-19 12:52:06 +01001331 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001332 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001333 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001334
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001335 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1336 if (path) {
1337 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001338 continue;
1339 }
1340
Takashi Iwai36907392013-12-10 17:29:26 +01001341 dacs[i] = get_preferred_dac(codec, pin);
1342 if (dacs[i]) {
1343 if (is_dac_already_used(codec, dacs[i]))
1344 badness += bad->shared_primary;
1345 }
1346
1347 if (!dacs[i])
1348 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001349 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001350 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001351 for (j = 1; j < num_outs; j++) {
1352 if (is_reachable_path(codec, dacs[j], pin)) {
1353 dacs[0] = dacs[j];
1354 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001355 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001356 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001357 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001360 }
1361 dac = dacs[i];
1362 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001363 if (num_outs > 2)
1364 dac = try_dac(codec, get_primary_out(codec, i), pin);
1365 if (!dac)
1366 dac = try_dac(codec, dacs[0], pin);
1367 if (!dac)
1368 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001369 if (dac) {
1370 if (!i)
1371 badness += bad->shared_primary;
1372 else if (i == 1)
1373 badness += bad->shared_surr;
1374 else
1375 badness += bad->shared_clfe;
1376 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1377 dac = spec->private_dac_nids[0];
1378 badness += bad->shared_surr_main;
1379 } else if (!i)
1380 badness += bad->no_primary_dac;
1381 else
1382 badness += bad->no_dac;
1383 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001384 if (!dac)
1385 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001386 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001387 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001388 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001389 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001390 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001391 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001392 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001393 badness += bad->no_dac;
1394 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001395 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001396 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001397 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001398 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001399 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001400 }
1401
1402 return badness;
1403}
1404
1405/* return NID if the given pin has only a single connection to a certain DAC */
1406static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1407{
1408 struct hda_gen_spec *spec = codec->spec;
1409 int i;
1410 hda_nid_t nid_found = 0;
1411
1412 for (i = 0; i < spec->num_all_dacs; i++) {
1413 hda_nid_t nid = spec->all_dacs[i];
1414 if (!nid || is_dac_already_used(codec, nid))
1415 continue;
1416 if (is_reachable_path(codec, nid, pin)) {
1417 if (nid_found)
1418 return 0;
1419 nid_found = nid;
1420 }
1421 }
1422 return nid_found;
1423}
1424
1425/* check whether the given pin can be a multi-io pin */
1426static bool can_be_multiio_pin(struct hda_codec *codec,
1427 unsigned int location, hda_nid_t nid)
1428{
1429 unsigned int defcfg, caps;
1430
1431 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1432 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1433 return false;
1434 if (location && get_defcfg_location(defcfg) != location)
1435 return false;
1436 caps = snd_hda_query_pin_caps(codec, nid);
1437 if (!(caps & AC_PINCAP_OUT))
1438 return false;
1439 return true;
1440}
1441
Takashi Iwaie22aab72013-01-04 14:50:04 +01001442/* count the number of input pins that are capable to be multi-io */
1443static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1444{
1445 struct hda_gen_spec *spec = codec->spec;
1446 struct auto_pin_cfg *cfg = &spec->autocfg;
1447 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1448 unsigned int location = get_defcfg_location(defcfg);
1449 int type, i;
1450 int num_pins = 0;
1451
1452 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1453 for (i = 0; i < cfg->num_inputs; i++) {
1454 if (cfg->inputs[i].type != type)
1455 continue;
1456 if (can_be_multiio_pin(codec, location,
1457 cfg->inputs[i].pin))
1458 num_pins++;
1459 }
1460 }
1461 return num_pins;
1462}
1463
Takashi Iwai352f7f92012-12-19 12:52:06 +01001464/*
1465 * multi-io helper
1466 *
1467 * When hardwired is set, try to fill ony hardwired pins, and returns
1468 * zero if any pins are filled, non-zero if nothing found.
1469 * When hardwired is off, try to fill possible input pins, and returns
1470 * the badness value.
1471 */
1472static int fill_multi_ios(struct hda_codec *codec,
1473 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001474 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001475{
1476 struct hda_gen_spec *spec = codec->spec;
1477 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001478 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001479 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1480 unsigned int location = get_defcfg_location(defcfg);
1481 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001482 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001483
1484 old_pins = spec->multi_ios;
1485 if (old_pins >= 2)
1486 goto end_fill;
1487
Takashi Iwaie22aab72013-01-04 14:50:04 +01001488 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001489 if (num_pins < 2)
1490 goto end_fill;
1491
Takashi Iwai352f7f92012-12-19 12:52:06 +01001492 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1493 for (i = 0; i < cfg->num_inputs; i++) {
1494 hda_nid_t nid = cfg->inputs[i].pin;
1495 hda_nid_t dac = 0;
1496
1497 if (cfg->inputs[i].type != type)
1498 continue;
1499 if (!can_be_multiio_pin(codec, location, nid))
1500 continue;
1501 for (j = 0; j < spec->multi_ios; j++) {
1502 if (nid == spec->multi_io[j].pin)
1503 break;
1504 }
1505 if (j < spec->multi_ios)
1506 continue;
1507
Takashi Iwai352f7f92012-12-19 12:52:06 +01001508 if (hardwired)
1509 dac = get_dac_if_single(codec, nid);
1510 else if (!dac)
1511 dac = look_for_dac(codec, nid, false);
1512 if (!dac) {
1513 badness++;
1514 continue;
1515 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001516 path = snd_hda_add_new_path(codec, dac, nid,
1517 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001518 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001519 badness++;
1520 continue;
1521 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001522 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001523 spec->multi_io[spec->multi_ios].pin = nid;
1524 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001525 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1526 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001527 spec->multi_ios++;
1528 if (spec->multi_ios >= 2)
1529 break;
1530 }
1531 }
1532 end_fill:
1533 if (badness)
1534 badness = BAD_MULTI_IO;
1535 if (old_pins == spec->multi_ios) {
1536 if (hardwired)
1537 return 1; /* nothing found */
1538 else
1539 return badness; /* no badness if nothing found */
1540 }
1541 if (!hardwired && spec->multi_ios < 2) {
1542 /* cancel newly assigned paths */
1543 spec->paths.used -= spec->multi_ios - old_pins;
1544 spec->multi_ios = old_pins;
1545 return badness;
1546 }
1547
1548 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001549 for (i = old_pins; i < spec->multi_ios; i++) {
1550 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1551 badness += assign_out_path_ctls(codec, path);
1552 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001553
1554 return badness;
1555}
1556
1557/* map DACs for all pins in the list if they are single connections */
1558static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001559 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001560{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001561 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001562 int i;
1563 bool found = false;
1564 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001565 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001566 hda_nid_t dac;
1567 if (dacs[i])
1568 continue;
1569 dac = get_dac_if_single(codec, pins[i]);
1570 if (!dac)
1571 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001572 path = snd_hda_add_new_path(codec, dac, pins[i],
1573 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001574 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001575 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001576 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001577 dacs[i] = dac;
1578 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001579 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001580 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001581 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001582 }
1583 }
1584 return found;
1585}
1586
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001587/* create a new path including aamix if available, and return its index */
1588static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1589{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001590 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001591 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001592 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001593
1594 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001595 if (!path || !path->depth ||
1596 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001597 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001598 path_dac = path->path[0];
1599 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001600 pin = path->path[path->depth - 1];
1601 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1602 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001603 if (dac != path_dac)
1604 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001605 else if (spec->multiout.hp_out_nid[0])
1606 dac = spec->multiout.hp_out_nid[0];
1607 else if (spec->multiout.extra_out_nid[0])
1608 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001609 else
1610 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001611 if (dac)
1612 path = snd_hda_add_new_path(codec, dac, pin,
1613 spec->mixer_nid);
1614 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001615 if (!path)
1616 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001617 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001618 path->active = false; /* unused as default */
Takashi Iwai6b275b12015-03-20 18:11:05 +01001619 path->pin_fixed = true; /* static route */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001620 return snd_hda_get_path_idx(codec, path);
1621}
1622
Takashi Iwai55a63d42013-03-21 17:20:12 +01001623/* check whether the independent HP is available with the current config */
1624static bool indep_hp_possible(struct hda_codec *codec)
1625{
1626 struct hda_gen_spec *spec = codec->spec;
1627 struct auto_pin_cfg *cfg = &spec->autocfg;
1628 struct nid_path *path;
1629 int i, idx;
1630
1631 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1632 idx = spec->out_paths[0];
1633 else
1634 idx = spec->hp_paths[0];
1635 path = snd_hda_get_path_from_idx(codec, idx);
1636 if (!path)
1637 return false;
1638
1639 /* assume no path conflicts unless aamix is involved */
1640 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1641 return true;
1642
1643 /* check whether output paths contain aamix */
1644 for (i = 0; i < cfg->line_outs; i++) {
1645 if (spec->out_paths[i] == idx)
1646 break;
1647 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1648 if (path && is_nid_contained(path, spec->mixer_nid))
1649 return false;
1650 }
1651 for (i = 0; i < cfg->speaker_outs; i++) {
1652 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1653 if (path && is_nid_contained(path, spec->mixer_nid))
1654 return false;
1655 }
1656
1657 return true;
1658}
1659
Takashi Iwaia07a9492013-01-07 16:44:06 +01001660/* fill the empty entries in the dac array for speaker/hp with the
1661 * shared dac pointed by the paths
1662 */
1663static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1664 hda_nid_t *dacs, int *path_idx)
1665{
1666 struct nid_path *path;
1667 int i;
1668
1669 for (i = 0; i < num_outs; i++) {
1670 if (dacs[i])
1671 continue;
1672 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1673 if (!path)
1674 continue;
1675 dacs[i] = path->path[0];
1676 }
1677}
1678
Takashi Iwai352f7f92012-12-19 12:52:06 +01001679/* fill in the dac_nids table from the parsed pin configuration */
1680static int fill_and_eval_dacs(struct hda_codec *codec,
1681 bool fill_hardwired,
1682 bool fill_mio_first)
1683{
1684 struct hda_gen_spec *spec = codec->spec;
1685 struct auto_pin_cfg *cfg = &spec->autocfg;
1686 int i, err, badness;
1687
1688 /* set num_dacs once to full for look_for_dac() */
1689 spec->multiout.num_dacs = cfg->line_outs;
1690 spec->multiout.dac_nids = spec->private_dac_nids;
1691 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1692 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1693 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1694 spec->multi_ios = 0;
1695 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001696
1697 /* clear path indices */
1698 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1699 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1700 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1701 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1702 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001703 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001704 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1705 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1706
Takashi Iwai352f7f92012-12-19 12:52:06 +01001707 badness = 0;
1708
1709 /* fill hard-wired DACs first */
1710 if (fill_hardwired) {
1711 bool mapped;
1712 do {
1713 mapped = map_singles(codec, cfg->line_outs,
1714 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001715 spec->private_dac_nids,
1716 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001717 mapped |= map_singles(codec, cfg->hp_outs,
1718 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001719 spec->multiout.hp_out_nid,
1720 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001721 mapped |= map_singles(codec, cfg->speaker_outs,
1722 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001723 spec->multiout.extra_out_nid,
1724 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001725 if (!spec->no_multi_io &&
1726 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001727 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001728 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001729 if (!err)
1730 mapped = true;
1731 }
1732 } while (mapped);
1733 }
1734
1735 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001736 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001737 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001738
Takashi Iwaida96fb52013-07-29 16:54:36 +02001739 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001740 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1741 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001742 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001743 if (err < 0)
1744 return err;
1745 /* we don't count badness at this stage yet */
1746 }
1747
1748 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1749 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1750 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001751 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001752 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001753 if (err < 0)
1754 return err;
1755 badness += err;
1756 }
1757 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1758 err = try_assign_dacs(codec, cfg->speaker_outs,
1759 cfg->speaker_pins,
1760 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001761 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001762 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001763 if (err < 0)
1764 return err;
1765 badness += err;
1766 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001767 if (!spec->no_multi_io &&
1768 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001769 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001770 if (err < 0)
1771 return err;
1772 badness += err;
1773 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001774
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001775 if (spec->mixer_nid) {
1776 spec->aamix_out_paths[0] =
1777 check_aamix_out_path(codec, spec->out_paths[0]);
1778 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1779 spec->aamix_out_paths[1] =
1780 check_aamix_out_path(codec, spec->hp_paths[0]);
1781 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1782 spec->aamix_out_paths[2] =
1783 check_aamix_out_path(codec, spec->speaker_paths[0]);
1784 }
1785
Takashi Iwaida96fb52013-07-29 16:54:36 +02001786 if (!spec->no_multi_io &&
1787 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001788 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1789 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001790
Takashi Iwaia07a9492013-01-07 16:44:06 +01001791 /* re-count num_dacs and squash invalid entries */
1792 spec->multiout.num_dacs = 0;
1793 for (i = 0; i < cfg->line_outs; i++) {
1794 if (spec->private_dac_nids[i])
1795 spec->multiout.num_dacs++;
1796 else {
1797 memmove(spec->private_dac_nids + i,
1798 spec->private_dac_nids + i + 1,
1799 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1800 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1801 }
1802 }
1803
1804 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001805 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001806
Takashi Iwai352f7f92012-12-19 12:52:06 +01001807 if (spec->multi_ios == 2) {
1808 for (i = 0; i < 2; i++)
1809 spec->private_dac_nids[spec->multiout.num_dacs++] =
1810 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001811 } else if (spec->multi_ios) {
1812 spec->multi_ios = 0;
1813 badness += BAD_MULTI_IO;
1814 }
1815
Takashi Iwai55a63d42013-03-21 17:20:12 +01001816 if (spec->indep_hp && !indep_hp_possible(codec))
1817 badness += BAD_NO_INDEP_HP;
1818
Takashi Iwaia07a9492013-01-07 16:44:06 +01001819 /* re-fill the shared DAC for speaker / headphone */
1820 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1821 refill_shared_dacs(codec, cfg->hp_outs,
1822 spec->multiout.hp_out_nid,
1823 spec->hp_paths);
1824 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1825 refill_shared_dacs(codec, cfg->speaker_outs,
1826 spec->multiout.extra_out_nid,
1827 spec->speaker_paths);
1828
Takashi Iwai352f7f92012-12-19 12:52:06 +01001829 return badness;
1830}
1831
1832#define DEBUG_BADNESS
1833
1834#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001835#define debug_badness(fmt, ...) \
1836 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001837#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001838#define debug_badness(fmt, ...) \
1839 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001840#endif
1841
Takashi Iwaia7694092013-01-21 10:43:18 +01001842#ifdef DEBUG_BADNESS
1843static inline void print_nid_path_idx(struct hda_codec *codec,
1844 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001845{
Takashi Iwaia7694092013-01-21 10:43:18 +01001846 struct nid_path *path;
1847
1848 path = snd_hda_get_path_from_idx(codec, idx);
1849 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001850 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001851}
1852
1853static void debug_show_configs(struct hda_codec *codec,
1854 struct auto_pin_cfg *cfg)
1855{
1856 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001857 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001858 int i;
1859
1860 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001861 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001862 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001863 spec->multiout.dac_nids[0],
1864 spec->multiout.dac_nids[1],
1865 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001866 spec->multiout.dac_nids[3],
1867 lo_type[cfg->line_out_type]);
1868 for (i = 0; i < cfg->line_outs; i++)
1869 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001870 if (spec->multi_ios > 0)
1871 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1872 spec->multi_ios,
1873 spec->multi_io[0].pin, spec->multi_io[1].pin,
1874 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001875 for (i = 0; i < spec->multi_ios; i++)
1876 print_nid_path_idx(codec, " mio",
1877 spec->out_paths[cfg->line_outs + i]);
1878 if (cfg->hp_outs)
1879 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001880 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001881 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001882 spec->multiout.hp_out_nid[0],
1883 spec->multiout.hp_out_nid[1],
1884 spec->multiout.hp_out_nid[2],
1885 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001886 for (i = 0; i < cfg->hp_outs; i++)
1887 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1888 if (cfg->speaker_outs)
1889 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001890 cfg->speaker_pins[0], cfg->speaker_pins[1],
1891 cfg->speaker_pins[2], cfg->speaker_pins[3],
1892 spec->multiout.extra_out_nid[0],
1893 spec->multiout.extra_out_nid[1],
1894 spec->multiout.extra_out_nid[2],
1895 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001896 for (i = 0; i < cfg->speaker_outs; i++)
1897 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1898 for (i = 0; i < 3; i++)
1899 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001900}
Takashi Iwaia7694092013-01-21 10:43:18 +01001901#else
1902#define debug_show_configs(codec, cfg) /* NOP */
1903#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001904
1905/* find all available DACs of the codec */
1906static void fill_all_dac_nids(struct hda_codec *codec)
1907{
1908 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001909 hda_nid_t nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001910
1911 spec->num_all_dacs = 0;
1912 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
Takashi Iwai7639a062015-03-03 10:07:24 +01001913 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001914 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1915 continue;
1916 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001917 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001918 break;
1919 }
1920 spec->all_dacs[spec->num_all_dacs++] = nid;
1921 }
1922}
1923
1924static int parse_output_paths(struct hda_codec *codec)
1925{
1926 struct hda_gen_spec *spec = codec->spec;
1927 struct auto_pin_cfg *cfg = &spec->autocfg;
1928 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001929 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001930 int best_badness = INT_MAX;
1931 int badness;
1932 bool fill_hardwired = true, fill_mio_first = true;
1933 bool best_wired = true, best_mio = true;
1934 bool hp_spk_swapped = false;
1935
Takashi Iwai352f7f92012-12-19 12:52:06 +01001936 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1937 if (!best_cfg)
1938 return -ENOMEM;
1939 *best_cfg = *cfg;
1940
1941 for (;;) {
1942 badness = fill_and_eval_dacs(codec, fill_hardwired,
1943 fill_mio_first);
1944 if (badness < 0) {
1945 kfree(best_cfg);
1946 return badness;
1947 }
1948 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1949 cfg->line_out_type, fill_hardwired, fill_mio_first,
1950 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001951 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001952 if (badness < best_badness) {
1953 best_badness = badness;
1954 *best_cfg = *cfg;
1955 best_wired = fill_hardwired;
1956 best_mio = fill_mio_first;
1957 }
1958 if (!badness)
1959 break;
1960 fill_mio_first = !fill_mio_first;
1961 if (!fill_mio_first)
1962 continue;
1963 fill_hardwired = !fill_hardwired;
1964 if (!fill_hardwired)
1965 continue;
1966 if (hp_spk_swapped)
1967 break;
1968 hp_spk_swapped = true;
1969 if (cfg->speaker_outs > 0 &&
1970 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1971 cfg->hp_outs = cfg->line_outs;
1972 memcpy(cfg->hp_pins, cfg->line_out_pins,
1973 sizeof(cfg->hp_pins));
1974 cfg->line_outs = cfg->speaker_outs;
1975 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1976 sizeof(cfg->speaker_pins));
1977 cfg->speaker_outs = 0;
1978 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1979 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1980 fill_hardwired = true;
1981 continue;
1982 }
1983 if (cfg->hp_outs > 0 &&
1984 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1985 cfg->speaker_outs = cfg->line_outs;
1986 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1987 sizeof(cfg->speaker_pins));
1988 cfg->line_outs = cfg->hp_outs;
1989 memcpy(cfg->line_out_pins, cfg->hp_pins,
1990 sizeof(cfg->hp_pins));
1991 cfg->hp_outs = 0;
1992 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1993 cfg->line_out_type = AUTO_PIN_HP_OUT;
1994 fill_hardwired = true;
1995 continue;
1996 }
1997 break;
1998 }
1999
2000 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002001 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01002002 *cfg = *best_cfg;
2003 fill_and_eval_dacs(codec, best_wired, best_mio);
2004 }
2005 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2006 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01002007 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002008
2009 if (cfg->line_out_pins[0]) {
2010 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01002011 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002012 if (path)
2013 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002014 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01002015 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2016 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002017 if (spec->dac_min_mute)
2018 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2019 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002020 }
2021
Takashi Iwai9314a582013-01-21 10:49:05 +01002022 /* set initial pinctl targets */
2023 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2024 val = PIN_HP;
2025 else
2026 val = PIN_OUT;
2027 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2028 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2029 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2030 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2031 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2032 set_pin_targets(codec, cfg->speaker_outs,
2033 cfg->speaker_pins, val);
2034 }
2035
Takashi Iwai55a63d42013-03-21 17:20:12 +01002036 /* clear indep_hp flag if not available */
2037 if (spec->indep_hp && !indep_hp_possible(codec))
2038 spec->indep_hp = 0;
2039
Takashi Iwai352f7f92012-12-19 12:52:06 +01002040 kfree(best_cfg);
2041 return 0;
2042}
2043
2044/* add playback controls from the parsed DAC table */
2045static int create_multi_out_ctls(struct hda_codec *codec,
2046 const struct auto_pin_cfg *cfg)
2047{
2048 struct hda_gen_spec *spec = codec->spec;
2049 int i, err, noutputs;
2050
2051 noutputs = cfg->line_outs;
2052 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2053 noutputs += spec->multi_ios;
2054
2055 for (i = 0; i < noutputs; i++) {
2056 const char *name;
2057 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002058 struct nid_path *path;
2059
Takashi Iwai196c17662013-01-04 15:01:40 +01002060 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002061 if (!path)
2062 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002063
2064 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002065 if (!name || !strcmp(name, "CLFE")) {
2066 /* Center/LFE */
2067 err = add_vol_ctl(codec, "Center", 0, 1, path);
2068 if (err < 0)
2069 return err;
2070 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2071 if (err < 0)
2072 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002073 } else {
2074 err = add_stereo_vol(codec, name, index, path);
2075 if (err < 0)
2076 return err;
2077 }
2078
2079 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2080 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002081 err = add_sw_ctl(codec, "Center", 0, 1, path);
2082 if (err < 0)
2083 return err;
2084 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2085 if (err < 0)
2086 return err;
2087 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002088 err = add_stereo_sw(codec, name, index, path);
2089 if (err < 0)
2090 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
2092 }
2093 return 0;
2094}
2095
Takashi Iwaic2c80382013-01-07 10:33:57 +01002096static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01002097 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002099 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 int err;
2101
Takashi Iwai196c17662013-01-04 15:01:40 +01002102 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002103 if (!path)
2104 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01002105 err = add_stereo_vol(codec, pfx, cidx, path);
2106 if (err < 0)
2107 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002108 err = add_stereo_sw(codec, pfx, cidx, path);
2109 if (err < 0)
2110 return err;
2111 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
Takashi Iwai352f7f92012-12-19 12:52:06 +01002114/* add playback controls for speaker and HP outputs */
2115static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01002116 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Takashi Iwaic2c80382013-01-07 10:33:57 +01002118 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002119
2120 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002121 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02002122 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01002123 int err, idx = 0;
2124
2125 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2126 name = "Bass Speaker";
2127 else if (num_pins >= 3) {
2128 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01002129 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01002130 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002131 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002132 name = pfx;
2133 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01002135 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002136 if (err < 0)
2137 return err;
2138 }
2139 return 0;
2140}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002141
Takashi Iwai352f7f92012-12-19 12:52:06 +01002142static int create_hp_out_ctls(struct hda_codec *codec)
2143{
2144 struct hda_gen_spec *spec = codec->spec;
2145 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002146 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002147 "Headphone");
2148}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Takashi Iwai352f7f92012-12-19 12:52:06 +01002150static int create_speaker_out_ctls(struct hda_codec *codec)
2151{
2152 struct hda_gen_spec *spec = codec->spec;
2153 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002154 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002155 "Speaker");
2156}
2157
2158/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002159 * independent HP controls
2160 */
2161
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02002162static void call_hp_automute(struct hda_codec *codec,
2163 struct hda_jack_callback *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002164static int indep_hp_info(struct snd_kcontrol *kcontrol,
2165 struct snd_ctl_elem_info *uinfo)
2166{
2167 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2168}
2169
2170static int indep_hp_get(struct snd_kcontrol *kcontrol,
2171 struct snd_ctl_elem_value *ucontrol)
2172{
2173 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2174 struct hda_gen_spec *spec = codec->spec;
2175 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2176 return 0;
2177}
2178
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002179static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2180 int nomix_path_idx, int mix_path_idx,
2181 int out_type);
2182
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002183static int indep_hp_put(struct snd_kcontrol *kcontrol,
2184 struct snd_ctl_elem_value *ucontrol)
2185{
2186 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2187 struct hda_gen_spec *spec = codec->spec;
2188 unsigned int select = ucontrol->value.enumerated.item[0];
2189 int ret = 0;
2190
2191 mutex_lock(&spec->pcm_mutex);
2192 if (spec->active_streams) {
2193 ret = -EBUSY;
2194 goto unlock;
2195 }
2196
2197 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002198 hda_nid_t *dacp;
2199 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2200 dacp = &spec->private_dac_nids[0];
2201 else
2202 dacp = &spec->multiout.hp_out_nid[0];
2203
2204 /* update HP aamix paths in case it conflicts with indep HP */
2205 if (spec->have_aamix_ctl) {
2206 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2207 update_aamix_paths(codec, spec->aamix_mode,
2208 spec->out_paths[0],
2209 spec->aamix_out_paths[0],
2210 spec->autocfg.line_out_type);
2211 else
2212 update_aamix_paths(codec, spec->aamix_mode,
2213 spec->hp_paths[0],
2214 spec->aamix_out_paths[1],
2215 AUTO_PIN_HP_OUT);
2216 }
2217
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002218 spec->indep_hp_enabled = select;
2219 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002220 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002221 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002222 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002223
Takashi Iwai963afde2013-05-31 15:20:31 +02002224 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002225 ret = 1;
2226 }
2227 unlock:
2228 mutex_unlock(&spec->pcm_mutex);
2229 return ret;
2230}
2231
2232static const struct snd_kcontrol_new indep_hp_ctl = {
2233 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2234 .name = "Independent HP",
2235 .info = indep_hp_info,
2236 .get = indep_hp_get,
2237 .put = indep_hp_put,
2238};
2239
2240
2241static int create_indep_hp_ctls(struct hda_codec *codec)
2242{
2243 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002244 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002245
2246 if (!spec->indep_hp)
2247 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002248 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2249 dac = spec->multiout.dac_nids[0];
2250 else
2251 dac = spec->multiout.hp_out_nid[0];
2252 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002253 spec->indep_hp = 0;
2254 return 0;
2255 }
2256
2257 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002258 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002259 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2260 return -ENOMEM;
2261 return 0;
2262}
2263
2264/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002265 * channel mode enum control
2266 */
2267
2268static int ch_mode_info(struct snd_kcontrol *kcontrol,
2269 struct snd_ctl_elem_info *uinfo)
2270{
2271 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2272 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002273 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002274
2275 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2276 uinfo->count = 1;
2277 uinfo->value.enumerated.items = spec->multi_ios + 1;
2278 if (uinfo->value.enumerated.item > spec->multi_ios)
2279 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002280 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2281 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002282 return 0;
2283}
2284
2285static int ch_mode_get(struct snd_kcontrol *kcontrol,
2286 struct snd_ctl_elem_value *ucontrol)
2287{
2288 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2289 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002290 ucontrol->value.enumerated.item[0] =
2291 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002292 return 0;
2293}
2294
Takashi Iwai196c17662013-01-04 15:01:40 +01002295static inline struct nid_path *
2296get_multiio_path(struct hda_codec *codec, int idx)
2297{
2298 struct hda_gen_spec *spec = codec->spec;
2299 return snd_hda_get_path_from_idx(codec,
2300 spec->out_paths[spec->autocfg.line_outs + idx]);
2301}
2302
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002303static void update_automute_all(struct hda_codec *codec);
2304
Takashi Iwai65033cc2013-04-16 12:31:05 +02002305/* Default value to be passed as aamix argument for snd_hda_activate_path();
2306 * used for output paths
2307 */
2308static bool aamix_default(struct hda_gen_spec *spec)
2309{
2310 return !spec->have_aamix_ctl || spec->aamix_mode;
2311}
2312
Takashi Iwai352f7f92012-12-19 12:52:06 +01002313static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2314{
2315 struct hda_gen_spec *spec = codec->spec;
2316 hda_nid_t nid = spec->multi_io[idx].pin;
2317 struct nid_path *path;
2318
Takashi Iwai196c17662013-01-04 15:01:40 +01002319 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002320 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002322
2323 if (path->active == output)
2324 return 0;
2325
2326 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002327 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002328 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002329 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002330 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002331 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002332 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002333 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002334 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002336
2337 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002338 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002339
Takashi Iwai352f7f92012-12-19 12:52:06 +01002340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341}
2342
Takashi Iwai352f7f92012-12-19 12:52:06 +01002343static int ch_mode_put(struct snd_kcontrol *kcontrol,
2344 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002346 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2347 struct hda_gen_spec *spec = codec->spec;
2348 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Takashi Iwai352f7f92012-12-19 12:52:06 +01002350 ch = ucontrol->value.enumerated.item[0];
2351 if (ch < 0 || ch > spec->multi_ios)
2352 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002353 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002354 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002355 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002356 for (i = 0; i < spec->multi_ios; i++)
2357 set_multi_io(codec, i, i < ch);
2358 spec->multiout.max_channels = max(spec->ext_channel_count,
2359 spec->const_channel_count);
2360 if (spec->need_dac_fix)
2361 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return 1;
2363}
2364
Takashi Iwai352f7f92012-12-19 12:52:06 +01002365static const struct snd_kcontrol_new channel_mode_enum = {
2366 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2367 .name = "Channel Mode",
2368 .info = ch_mode_info,
2369 .get = ch_mode_get,
2370 .put = ch_mode_put,
2371};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Takashi Iwai352f7f92012-12-19 12:52:06 +01002373static int create_multi_channel_mode(struct hda_codec *codec)
2374{
2375 struct hda_gen_spec *spec = codec->spec;
2376
2377 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002378 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002379 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return 0;
2382}
2383
Takashi Iwai352f7f92012-12-19 12:52:06 +01002384/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002385 * aamix loopback enable/disable switch
2386 */
2387
2388#define loopback_mixing_info indep_hp_info
2389
2390static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2391 struct snd_ctl_elem_value *ucontrol)
2392{
2393 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2394 struct hda_gen_spec *spec = codec->spec;
2395 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2396 return 0;
2397}
2398
2399static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002400 int nomix_path_idx, int mix_path_idx,
2401 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002402{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002403 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002404 struct nid_path *nomix_path, *mix_path;
2405
2406 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2407 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2408 if (!nomix_path || !mix_path)
2409 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002410
2411 /* if HP aamix path is driven from a different DAC and the
2412 * independent HP mode is ON, can't turn on aamix path
2413 */
2414 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2415 mix_path->path[0] != spec->alt_dac_nid)
2416 do_mix = false;
2417
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002418 if (do_mix) {
2419 snd_hda_activate_path(codec, nomix_path, false, true);
2420 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002421 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002422 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002423 snd_hda_activate_path(codec, mix_path, false, false);
2424 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002425 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002426 }
2427}
2428
2429static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2430 struct snd_ctl_elem_value *ucontrol)
2431{
2432 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2433 struct hda_gen_spec *spec = codec->spec;
2434 unsigned int val = ucontrol->value.enumerated.item[0];
2435
2436 if (val == spec->aamix_mode)
2437 return 0;
2438 spec->aamix_mode = val;
2439 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002440 spec->aamix_out_paths[0],
2441 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002442 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002443 spec->aamix_out_paths[1],
2444 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002445 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002446 spec->aamix_out_paths[2],
2447 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002448 return 1;
2449}
2450
2451static const struct snd_kcontrol_new loopback_mixing_enum = {
2452 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2453 .name = "Loopback Mixing",
2454 .info = loopback_mixing_info,
2455 .get = loopback_mixing_get,
2456 .put = loopback_mixing_put,
2457};
2458
2459static int create_loopback_mixing_ctl(struct hda_codec *codec)
2460{
2461 struct hda_gen_spec *spec = codec->spec;
2462
2463 if (!spec->mixer_nid)
2464 return 0;
2465 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2466 spec->aamix_out_paths[2]))
2467 return 0;
2468 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2469 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002470 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002471 return 0;
2472}
2473
2474/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002475 * shared headphone/mic handling
2476 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002477
Takashi Iwai352f7f92012-12-19 12:52:06 +01002478static void call_update_outputs(struct hda_codec *codec);
2479
2480/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002481static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002482{
2483 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002484 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002485 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002486 hda_nid_t pin;
2487
2488 pin = spec->hp_mic_pin;
2489 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2490
2491 if (!force) {
2492 val = snd_hda_codec_get_pin_target(codec, pin);
2493 if (as_mic) {
2494 if (val & PIN_IN)
2495 return;
2496 } else {
2497 if (val & PIN_OUT)
2498 return;
2499 }
2500 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002501
2502 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002503 /* if the HP pin doesn't support VREF and the codec driver gives an
2504 * alternative pin, set up the VREF on that pin instead
2505 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002506 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2507 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2508 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2509 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002510 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002511 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002512 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002513
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002514 if (!spec->hp_mic_jack_modes) {
2515 if (as_mic)
2516 val |= PIN_IN;
2517 else
2518 val = PIN_HP;
2519 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002520 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002521 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002522}
2523
2524/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002525static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002526{
2527 struct hda_gen_spec *spec = codec->spec;
2528 struct auto_pin_cfg *cfg = &spec->autocfg;
2529 unsigned int defcfg;
2530 hda_nid_t nid;
2531
Takashi Iwai967303d2013-02-19 17:12:42 +01002532 if (!spec->hp_mic) {
2533 if (spec->suppress_hp_mic_detect)
2534 return 0;
2535 /* automatic detection: only if no input or a single internal
2536 * input pin is found, try to detect the shared hp/mic
2537 */
2538 if (cfg->num_inputs > 1)
2539 return 0;
2540 else if (cfg->num_inputs == 1) {
2541 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2542 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2543 return 0;
2544 }
2545 }
2546
2547 spec->hp_mic = 0; /* clear once */
2548 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002549 return 0;
2550
Takashi Iwai967303d2013-02-19 17:12:42 +01002551 nid = 0;
2552 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2553 nid = cfg->line_out_pins[0];
2554 else if (cfg->hp_outs > 0)
2555 nid = cfg->hp_pins[0];
2556 if (!nid)
2557 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002558
2559 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2560 return 0; /* no input */
2561
Takashi Iwai967303d2013-02-19 17:12:42 +01002562 cfg->inputs[cfg->num_inputs].pin = nid;
2563 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002564 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002565 cfg->num_inputs++;
2566 spec->hp_mic = 1;
2567 spec->hp_mic_pin = nid;
2568 /* we can't handle auto-mic together with HP-mic */
2569 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002570 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002571 return 0;
2572}
2573
Takashi Iwai978e77e2013-01-10 16:57:58 +01002574/*
2575 * output jack mode
2576 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002577
2578static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2579
2580static const char * const out_jack_texts[] = {
2581 "Line Out", "Headphone Out",
2582};
2583
Takashi Iwai978e77e2013-01-10 16:57:58 +01002584static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2585 struct snd_ctl_elem_info *uinfo)
2586{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002587 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002588}
2589
2590static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2591 struct snd_ctl_elem_value *ucontrol)
2592{
2593 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2594 hda_nid_t nid = kcontrol->private_value;
2595 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2596 ucontrol->value.enumerated.item[0] = 1;
2597 else
2598 ucontrol->value.enumerated.item[0] = 0;
2599 return 0;
2600}
2601
2602static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2603 struct snd_ctl_elem_value *ucontrol)
2604{
2605 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2606 hda_nid_t nid = kcontrol->private_value;
2607 unsigned int val;
2608
2609 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2610 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2611 return 0;
2612 snd_hda_set_pin_ctl_cache(codec, nid, val);
2613 return 1;
2614}
2615
2616static const struct snd_kcontrol_new out_jack_mode_enum = {
2617 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2618 .info = out_jack_mode_info,
2619 .get = out_jack_mode_get,
2620 .put = out_jack_mode_put,
2621};
2622
2623static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2624{
2625 struct hda_gen_spec *spec = codec->spec;
2626 int i;
2627
2628 for (i = 0; i < spec->kctls.used; i++) {
2629 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2630 if (!strcmp(kctl->name, name) && kctl->index == idx)
2631 return true;
2632 }
2633 return false;
2634}
2635
2636static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2637 char *name, size_t name_len)
2638{
2639 struct hda_gen_spec *spec = codec->spec;
2640 int idx = 0;
2641
2642 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2643 strlcat(name, " Jack Mode", name_len);
2644
2645 for (; find_kctl_name(codec, name, idx); idx++)
2646 ;
2647}
2648
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002649static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2650{
2651 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002652 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002653 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2654 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2655 return 2;
2656 }
2657 return 1;
2658}
2659
Takashi Iwai978e77e2013-01-10 16:57:58 +01002660static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2661 hda_nid_t *pins)
2662{
2663 struct hda_gen_spec *spec = codec->spec;
2664 int i;
2665
2666 for (i = 0; i < num_pins; i++) {
2667 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002668 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002669 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002670 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002671 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002672 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002673 get_jack_mode_name(codec, pin, name, sizeof(name));
2674 knew = snd_hda_gen_add_kctl(spec, name,
2675 &out_jack_mode_enum);
2676 if (!knew)
2677 return -ENOMEM;
2678 knew->private_value = pin;
2679 }
2680 }
2681
2682 return 0;
2683}
2684
Takashi Iwai294765582013-01-17 09:52:11 +01002685/*
2686 * input jack mode
2687 */
2688
2689/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2690#define NUM_VREFS 6
2691
2692static const char * const vref_texts[NUM_VREFS] = {
2693 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2694 "", "Mic 80pc Bias", "Mic 100pc Bias"
2695};
2696
2697static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2698{
2699 unsigned int pincap;
2700
2701 pincap = snd_hda_query_pin_caps(codec, pin);
2702 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2703 /* filter out unusual vrefs */
2704 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2705 return pincap;
2706}
2707
2708/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2709static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2710{
2711 unsigned int i, n = 0;
2712
2713 for (i = 0; i < NUM_VREFS; i++) {
2714 if (vref_caps & (1 << i)) {
2715 if (n == item_idx)
2716 return i;
2717 n++;
2718 }
2719 }
2720 return 0;
2721}
2722
2723/* convert back from the vref ctl index to the enum item index */
2724static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2725{
2726 unsigned int i, n = 0;
2727
2728 for (i = 0; i < NUM_VREFS; i++) {
2729 if (i == idx)
2730 return n;
2731 if (vref_caps & (1 << i))
2732 n++;
2733 }
2734 return 0;
2735}
2736
2737static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2738 struct snd_ctl_elem_info *uinfo)
2739{
2740 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2741 hda_nid_t nid = kcontrol->private_value;
2742 unsigned int vref_caps = get_vref_caps(codec, nid);
2743
2744 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2745 vref_texts);
2746 /* set the right text */
2747 strcpy(uinfo->value.enumerated.name,
2748 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2749 return 0;
2750}
2751
2752static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2753 struct snd_ctl_elem_value *ucontrol)
2754{
2755 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2756 hda_nid_t nid = kcontrol->private_value;
2757 unsigned int vref_caps = get_vref_caps(codec, nid);
2758 unsigned int idx;
2759
2760 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2761 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2762 return 0;
2763}
2764
2765static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2766 struct snd_ctl_elem_value *ucontrol)
2767{
2768 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2769 hda_nid_t nid = kcontrol->private_value;
2770 unsigned int vref_caps = get_vref_caps(codec, nid);
2771 unsigned int val, idx;
2772
2773 val = snd_hda_codec_get_pin_target(codec, nid);
2774 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2775 if (idx == ucontrol->value.enumerated.item[0])
2776 return 0;
2777
2778 val &= ~AC_PINCTL_VREFEN;
2779 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2780 snd_hda_set_pin_ctl_cache(codec, nid, val);
2781 return 1;
2782}
2783
2784static const struct snd_kcontrol_new in_jack_mode_enum = {
2785 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2786 .info = in_jack_mode_info,
2787 .get = in_jack_mode_get,
2788 .put = in_jack_mode_put,
2789};
2790
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002791static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2792{
2793 struct hda_gen_spec *spec = codec->spec;
2794 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002795 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002796 nitems = hweight32(get_vref_caps(codec, pin));
2797 return nitems ? nitems : 1;
2798}
2799
Takashi Iwai294765582013-01-17 09:52:11 +01002800static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2801{
2802 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002803 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002804 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002805 unsigned int defcfg;
2806
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002807 if (pin == spec->hp_mic_pin)
2808 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002809
2810 /* no jack mode for fixed pins */
2811 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2812 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2813 return 0;
2814
2815 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002816 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002817 return 0;
2818
2819 get_jack_mode_name(codec, pin, name, sizeof(name));
2820 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2821 if (!knew)
2822 return -ENOMEM;
2823 knew->private_value = pin;
2824 return 0;
2825}
2826
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002827/*
2828 * HP/mic shared jack mode
2829 */
2830static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2831 struct snd_ctl_elem_info *uinfo)
2832{
2833 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2834 hda_nid_t nid = kcontrol->private_value;
2835 int out_jacks = get_out_jack_num_items(codec, nid);
2836 int in_jacks = get_in_jack_num_items(codec, nid);
2837 const char *text = NULL;
2838 int idx;
2839
2840 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2841 uinfo->count = 1;
2842 uinfo->value.enumerated.items = out_jacks + in_jacks;
2843 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2844 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2845 idx = uinfo->value.enumerated.item;
2846 if (idx < out_jacks) {
2847 if (out_jacks > 1)
2848 text = out_jack_texts[idx];
2849 else
2850 text = "Headphone Out";
2851 } else {
2852 idx -= out_jacks;
2853 if (in_jacks > 1) {
2854 unsigned int vref_caps = get_vref_caps(codec, nid);
2855 text = vref_texts[get_vref_idx(vref_caps, idx)];
2856 } else
2857 text = "Mic In";
2858 }
2859
2860 strcpy(uinfo->value.enumerated.name, text);
2861 return 0;
2862}
2863
2864static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2865{
2866 int out_jacks = get_out_jack_num_items(codec, nid);
2867 int in_jacks = get_in_jack_num_items(codec, nid);
2868 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2869 int idx = 0;
2870
2871 if (val & PIN_OUT) {
2872 if (out_jacks > 1 && val == PIN_HP)
2873 idx = 1;
2874 } else if (val & PIN_IN) {
2875 idx = out_jacks;
2876 if (in_jacks > 1) {
2877 unsigned int vref_caps = get_vref_caps(codec, nid);
2878 val &= AC_PINCTL_VREFEN;
2879 idx += cvt_from_vref_idx(vref_caps, val);
2880 }
2881 }
2882 return idx;
2883}
2884
2885static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2886 struct snd_ctl_elem_value *ucontrol)
2887{
2888 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2889 hda_nid_t nid = kcontrol->private_value;
2890 ucontrol->value.enumerated.item[0] =
2891 get_cur_hp_mic_jack_mode(codec, nid);
2892 return 0;
2893}
2894
2895static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2896 struct snd_ctl_elem_value *ucontrol)
2897{
2898 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2899 hda_nid_t nid = kcontrol->private_value;
2900 int out_jacks = get_out_jack_num_items(codec, nid);
2901 int in_jacks = get_in_jack_num_items(codec, nid);
2902 unsigned int val, oldval, idx;
2903
2904 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2905 idx = ucontrol->value.enumerated.item[0];
2906 if (oldval == idx)
2907 return 0;
2908
2909 if (idx < out_jacks) {
2910 if (out_jacks > 1)
2911 val = idx ? PIN_HP : PIN_OUT;
2912 else
2913 val = PIN_HP;
2914 } else {
2915 idx -= out_jacks;
2916 if (in_jacks > 1) {
2917 unsigned int vref_caps = get_vref_caps(codec, nid);
2918 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e322013-03-07 18:30:27 +01002919 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2920 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002921 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002922 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002923 }
2924 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002925 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002926
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002927 return 1;
2928}
2929
2930static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2931 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2932 .info = hp_mic_jack_mode_info,
2933 .get = hp_mic_jack_mode_get,
2934 .put = hp_mic_jack_mode_put,
2935};
2936
2937static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2938{
2939 struct hda_gen_spec *spec = codec->spec;
2940 struct snd_kcontrol_new *knew;
2941
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002942 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2943 &hp_mic_jack_mode_enum);
2944 if (!knew)
2945 return -ENOMEM;
2946 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002947 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002948 return 0;
2949}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002950
2951/*
2952 * Parse input paths
2953 */
2954
Takashi Iwai352f7f92012-12-19 12:52:06 +01002955/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002956static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002957{
2958 struct hda_amp_list *list;
2959
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002960 list = snd_array_new(&spec->loopback_list);
2961 if (!list)
2962 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002963 list->nid = mix;
2964 list->dir = HDA_INPUT;
2965 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002966 spec->loopback.amplist = spec->loopback_list.list;
2967 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002968}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002969
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002970/* return true if either a volume or a mute amp is found for the given
2971 * aamix path; the amp has to be either in the mixer node or its direct leaf
2972 */
2973static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2974 hda_nid_t pin, unsigned int *mix_val,
2975 unsigned int *mute_val)
2976{
2977 int idx, num_conns;
2978 const hda_nid_t *list;
2979 hda_nid_t nid;
2980
2981 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2982 if (idx < 0)
2983 return false;
2984
2985 *mix_val = *mute_val = 0;
2986 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2987 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2988 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2989 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2990 if (*mix_val && *mute_val)
2991 return true;
2992
2993 /* check leaf node */
2994 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2995 if (num_conns < idx)
2996 return false;
2997 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01002998 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2999 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003000 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01003001 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3002 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003003 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3004
3005 return *mix_val || *mute_val;
3006}
3007
Takashi Iwai352f7f92012-12-19 12:52:06 +01003008/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01003009static int new_analog_input(struct hda_codec *codec, int input_idx,
3010 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003011 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003013 struct hda_gen_spec *spec = codec->spec;
3014 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003015 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003016 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003018 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3019 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003020
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003021 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003022 if (!path)
3023 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003024 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01003025 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003026
3027 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003028 if (mix_val) {
3029 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003030 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003032 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 }
3034
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003035 if (mute_val) {
3036 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003037 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003039 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 }
3041
Takashi Iwai352f7f92012-12-19 12:52:06 +01003042 path->active = true;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003043 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003044 err = add_loopback_list(spec, mix_nid, idx);
3045 if (err < 0)
3046 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003047
3048 if (spec->mixer_nid != spec->mixer_merge_nid &&
3049 !spec->loopback_merge_path) {
3050 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3051 spec->mixer_merge_nid, 0);
3052 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003053 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003054 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003055 path->pin_fixed = true; /* static route */
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003056 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003057 spec->loopback_merge_path =
3058 snd_hda_get_path_idx(codec, path);
3059 }
3060 }
3061
Takashi Iwai352f7f92012-12-19 12:52:06 +01003062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063}
3064
Takashi Iwai352f7f92012-12-19 12:52:06 +01003065static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003067 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3068 return (pincap & AC_PINCAP_IN) != 0;
3069}
3070
3071/* Parse the codec tree and retrieve ADCs */
3072static int fill_adc_nids(struct hda_codec *codec)
3073{
3074 struct hda_gen_spec *spec = codec->spec;
3075 hda_nid_t nid;
3076 hda_nid_t *adc_nids = spec->adc_nids;
3077 int max_nums = ARRAY_SIZE(spec->adc_nids);
Takashi Iwai7639a062015-03-03 10:07:24 +01003078 int nums = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003079
Takashi Iwai7639a062015-03-03 10:07:24 +01003080 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003081 unsigned int caps = get_wcaps(codec, nid);
3082 int type = get_wcaps_type(caps);
3083
3084 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3085 continue;
3086 adc_nids[nums] = nid;
3087 if (++nums >= max_nums)
3088 break;
3089 }
3090 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01003091
3092 /* copy the detected ADCs to all_adcs[] */
3093 spec->num_all_adcs = nums;
3094 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3095
Takashi Iwai352f7f92012-12-19 12:52:06 +01003096 return nums;
3097}
3098
3099/* filter out invalid adc_nids that don't give all active input pins;
3100 * if needed, check whether dynamic ADC-switching is available
3101 */
3102static int check_dyn_adc_switch(struct hda_codec *codec)
3103{
3104 struct hda_gen_spec *spec = codec->spec;
3105 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003106 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003107 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003108
Takashi Iwai352f7f92012-12-19 12:52:06 +01003109 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003110 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003111 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003112 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003113 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003114 break;
3115 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003116 if (i >= imux->num_items) {
3117 ok_bits |= (1 << n);
3118 nums++;
3119 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003120 }
3121
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003122 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003123 /* check whether ADC-switch is possible */
3124 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003125 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003126 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003127 spec->dyn_adc_idx[i] = n;
3128 break;
3129 }
3130 }
3131 }
3132
Takashi Iwai4e76a882014-02-25 12:21:03 +01003133 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003134 spec->dyn_adc_switch = 1;
3135 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003136 /* shrink the invalid adcs and input paths */
3137 nums = 0;
3138 for (n = 0; n < spec->num_adc_nids; n++) {
3139 if (!(ok_bits & (1 << n)))
3140 continue;
3141 if (n != nums) {
3142 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003143 for (i = 0; i < imux->num_items; i++) {
3144 invalidate_nid_path(codec,
3145 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003146 spec->input_paths[i][nums] =
3147 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003148 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003149 }
3150 nums++;
3151 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003152 spec->num_adc_nids = nums;
3153 }
3154
Takashi Iwai967303d2013-02-19 17:12:42 +01003155 if (imux->num_items == 1 ||
3156 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003157 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003158 spec->num_adc_nids = 1; /* reduce to a single ADC */
3159 }
3160
3161 /* single index for individual volumes ctls */
3162 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3163 spec->num_adc_nids = 1;
3164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 return 0;
3166}
3167
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003168/* parse capture source paths from the given pin and create imux items */
3169static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003170 int cfg_idx, int num_adcs,
3171 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003172{
3173 struct hda_gen_spec *spec = codec->spec;
3174 struct hda_input_mux *imux = &spec->input_mux;
3175 int imux_idx = imux->num_items;
3176 bool imux_added = false;
3177 int c;
3178
3179 for (c = 0; c < num_adcs; c++) {
3180 struct nid_path *path;
3181 hda_nid_t adc = spec->adc_nids[c];
3182
3183 if (!is_reachable_path(codec, pin, adc))
3184 continue;
3185 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3186 if (!path)
3187 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003188 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003189 spec->input_paths[imux_idx][c] =
3190 snd_hda_get_path_idx(codec, path);
3191
3192 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003193 if (spec->hp_mic_pin == pin)
3194 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003195 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003196 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003197 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003198 if (spec->dyn_adc_switch)
3199 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003200 }
3201 }
3202
3203 return 0;
3204}
3205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003207 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003209
Takashi Iwaic9700422013-01-18 10:17:30 +01003210/* fill the label for each input at first */
3211static int fill_input_pin_labels(struct hda_codec *codec)
3212{
3213 struct hda_gen_spec *spec = codec->spec;
3214 const struct auto_pin_cfg *cfg = &spec->autocfg;
3215 int i;
3216
3217 for (i = 0; i < cfg->num_inputs; i++) {
3218 hda_nid_t pin = cfg->inputs[i].pin;
3219 const char *label;
3220 int j, idx;
3221
3222 if (!is_input_pin(codec, pin))
3223 continue;
3224
3225 label = hda_get_autocfg_input_label(codec, cfg, i);
3226 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003227 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003228 if (spec->input_labels[j] &&
3229 !strcmp(spec->input_labels[j], label)) {
3230 idx = spec->input_label_idxs[j] + 1;
3231 break;
3232 }
3233 }
3234
3235 spec->input_labels[i] = label;
3236 spec->input_label_idxs[i] = idx;
3237 }
3238
3239 return 0;
3240}
3241
Takashi Iwai9dba2052013-01-18 10:01:15 +01003242#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3243
Takashi Iwai352f7f92012-12-19 12:52:06 +01003244static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003245{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003246 struct hda_gen_spec *spec = codec->spec;
3247 const struct auto_pin_cfg *cfg = &spec->autocfg;
3248 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003249 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003250 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003251 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003252
Takashi Iwai352f7f92012-12-19 12:52:06 +01003253 num_adcs = fill_adc_nids(codec);
3254 if (num_adcs < 0)
3255 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003256
Takashi Iwaic9700422013-01-18 10:17:30 +01003257 err = fill_input_pin_labels(codec);
3258 if (err < 0)
3259 return err;
3260
Takashi Iwai352f7f92012-12-19 12:52:06 +01003261 for (i = 0; i < cfg->num_inputs; i++) {
3262 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Takashi Iwai352f7f92012-12-19 12:52:06 +01003264 pin = cfg->inputs[i].pin;
3265 if (!is_input_pin(codec, pin))
3266 continue;
3267
Takashi Iwai2c12c302013-01-10 09:33:29 +01003268 val = PIN_IN;
3269 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3270 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai3e1b0c42015-04-27 10:43:22 +02003271 if (pin != spec->hp_mic_pin &&
3272 !snd_hda_codec_get_pin_target(codec, pin))
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003273 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003274
Takashi Iwai352f7f92012-12-19 12:52:06 +01003275 if (mixer) {
3276 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003277 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003278 spec->input_labels[i],
3279 spec->input_label_idxs[i],
3280 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003281 if (err < 0)
3282 return err;
3283 }
3284 }
3285
Takashi Iwaic9700422013-01-18 10:17:30 +01003286 err = parse_capture_source(codec, pin, i, num_adcs,
3287 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003288 if (err < 0)
3289 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003290
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003291 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003292 err = create_in_jack_mode(codec, pin);
3293 if (err < 0)
3294 return err;
3295 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003296 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003297
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003298 /* add stereo mix when explicitly enabled via hint */
Takashi Iwai74f14b32014-12-15 13:43:59 +01003299 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003300 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003301 "Stereo Mix", 0);
3302 if (err < 0)
3303 return err;
Takashi Iwai82d04e12014-12-15 13:40:42 +01003304 else
3305 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003306 }
3307
3308 return 0;
3309}
3310
3311
3312/*
3313 * input source mux
3314 */
3315
Takashi Iwaic697b712013-01-07 17:09:26 +01003316/* get the input path specified by the given adc and imux indices */
3317static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003318{
3319 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003320 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3321 snd_BUG();
3322 return NULL;
3323 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003324 if (spec->dyn_adc_switch)
3325 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003326 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003327 snd_BUG();
3328 return NULL;
3329 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003330 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003331}
3332
3333static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3334 unsigned int idx);
3335
3336static int mux_enum_info(struct snd_kcontrol *kcontrol,
3337 struct snd_ctl_elem_info *uinfo)
3338{
3339 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3340 struct hda_gen_spec *spec = codec->spec;
3341 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3342}
3343
3344static int mux_enum_get(struct snd_kcontrol *kcontrol,
3345 struct snd_ctl_elem_value *ucontrol)
3346{
3347 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3348 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003349 /* the ctls are created at once with multiple counts */
3350 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003351
3352 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3353 return 0;
3354}
3355
3356static int mux_enum_put(struct snd_kcontrol *kcontrol,
3357 struct snd_ctl_elem_value *ucontrol)
3358{
3359 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003360 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003361 return mux_select(codec, adc_idx,
3362 ucontrol->value.enumerated.item[0]);
3363}
3364
Takashi Iwai352f7f92012-12-19 12:52:06 +01003365static const struct snd_kcontrol_new cap_src_temp = {
3366 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3367 .name = "Input Source",
3368 .info = mux_enum_info,
3369 .get = mux_enum_get,
3370 .put = mux_enum_put,
3371};
3372
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003373/*
3374 * capture volume and capture switch ctls
3375 */
3376
Takashi Iwai352f7f92012-12-19 12:52:06 +01003377typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3378 struct snd_ctl_elem_value *ucontrol);
3379
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003380/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003381static int cap_put_caller(struct snd_kcontrol *kcontrol,
3382 struct snd_ctl_elem_value *ucontrol,
3383 put_call_t func, int type)
3384{
3385 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3386 struct hda_gen_spec *spec = codec->spec;
3387 const struct hda_input_mux *imux;
3388 struct nid_path *path;
3389 int i, adc_idx, err = 0;
3390
3391 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003392 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003393 mutex_lock(&codec->control_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003394 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003395 path = get_input_path(codec, adc_idx, i);
3396 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003397 continue;
3398 kcontrol->private_value = path->ctls[type];
3399 err = func(kcontrol, ucontrol);
3400 if (err < 0)
Takashi Iwaia551d912015-02-26 12:34:49 +01003401 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003402 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003403 mutex_unlock(&codec->control_mutex);
3404 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003405 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003406 return err;
3407}
3408
3409/* capture volume ctl callbacks */
3410#define cap_vol_info snd_hda_mixer_amp_volume_info
3411#define cap_vol_get snd_hda_mixer_amp_volume_get
3412#define cap_vol_tlv snd_hda_mixer_amp_tlv
3413
3414static int cap_vol_put(struct snd_kcontrol *kcontrol,
3415 struct snd_ctl_elem_value *ucontrol)
3416{
3417 return cap_put_caller(kcontrol, ucontrol,
3418 snd_hda_mixer_amp_volume_put,
3419 NID_PATH_VOL_CTL);
3420}
3421
3422static const struct snd_kcontrol_new cap_vol_temp = {
3423 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3424 .name = "Capture Volume",
3425 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3426 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3427 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3428 .info = cap_vol_info,
3429 .get = cap_vol_get,
3430 .put = cap_vol_put,
3431 .tlv = { .c = cap_vol_tlv },
3432};
3433
3434/* capture switch ctl callbacks */
3435#define cap_sw_info snd_ctl_boolean_stereo_info
3436#define cap_sw_get snd_hda_mixer_amp_switch_get
3437
3438static int cap_sw_put(struct snd_kcontrol *kcontrol,
3439 struct snd_ctl_elem_value *ucontrol)
3440{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003441 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003442 snd_hda_mixer_amp_switch_put,
3443 NID_PATH_MUTE_CTL);
3444}
3445
3446static const struct snd_kcontrol_new cap_sw_temp = {
3447 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3448 .name = "Capture Switch",
3449 .info = cap_sw_info,
3450 .get = cap_sw_get,
3451 .put = cap_sw_put,
3452};
3453
3454static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3455{
3456 hda_nid_t nid;
3457 int i, depth;
3458
3459 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3460 for (depth = 0; depth < 3; depth++) {
3461 if (depth >= path->depth)
3462 return -EINVAL;
3463 i = path->depth - depth - 1;
3464 nid = path->path[i];
3465 if (!path->ctls[NID_PATH_VOL_CTL]) {
3466 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3467 path->ctls[NID_PATH_VOL_CTL] =
3468 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3469 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3470 int idx = path->idx[i];
3471 if (!depth && codec->single_adc_amp)
3472 idx = 0;
3473 path->ctls[NID_PATH_VOL_CTL] =
3474 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3475 }
3476 }
3477 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3478 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3479 path->ctls[NID_PATH_MUTE_CTL] =
3480 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3481 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3482 int idx = path->idx[i];
3483 if (!depth && codec->single_adc_amp)
3484 idx = 0;
3485 path->ctls[NID_PATH_MUTE_CTL] =
3486 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3487 }
3488 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 return 0;
3491}
3492
Takashi Iwai352f7f92012-12-19 12:52:06 +01003493static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003495 struct hda_gen_spec *spec = codec->spec;
3496 struct auto_pin_cfg *cfg = &spec->autocfg;
3497 unsigned int val;
3498 int i;
3499
3500 if (!spec->inv_dmic_split)
3501 return false;
3502 for (i = 0; i < cfg->num_inputs; i++) {
3503 if (cfg->inputs[i].pin != nid)
3504 continue;
3505 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3506 return false;
3507 val = snd_hda_codec_get_pincfg(codec, nid);
3508 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3509 }
3510 return false;
3511}
3512
Takashi Iwaia90229e2013-01-18 14:10:00 +01003513/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003514static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3515 struct snd_ctl_elem_value *ucontrol)
3516{
3517 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3518 struct hda_gen_spec *spec = codec->spec;
3519 int ret;
3520
3521 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3522 if (ret < 0)
3523 return ret;
3524
Takashi Iwaia90229e2013-01-18 14:10:00 +01003525 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003526 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003527
3528 return ret;
3529}
3530
Takashi Iwai352f7f92012-12-19 12:52:06 +01003531static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3532 int idx, bool is_switch, unsigned int ctl,
3533 bool inv_dmic)
3534{
3535 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003536 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003537 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3538 const char *sfx = is_switch ? "Switch" : "Volume";
3539 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003540 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003541
3542 if (!ctl)
3543 return 0;
3544
3545 if (label)
3546 snprintf(tmpname, sizeof(tmpname),
3547 "%s Capture %s", label, sfx);
3548 else
3549 snprintf(tmpname, sizeof(tmpname),
3550 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003551 knew = add_control(spec, type, tmpname, idx,
3552 amp_val_replace_channels(ctl, chs));
3553 if (!knew)
3554 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003555 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003556 knew->put = cap_single_sw_put;
3557 if (!inv_dmic)
3558 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003559
3560 /* Make independent right kcontrol */
3561 if (label)
3562 snprintf(tmpname, sizeof(tmpname),
3563 "Inverted %s Capture %s", label, sfx);
3564 else
3565 snprintf(tmpname, sizeof(tmpname),
3566 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003567 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003568 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003569 if (!knew)
3570 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003571 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003572 knew->put = cap_single_sw_put;
3573 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003574}
3575
3576/* create single (and simple) capture volume and switch controls */
3577static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3578 unsigned int vol_ctl, unsigned int sw_ctl,
3579 bool inv_dmic)
3580{
3581 int err;
3582 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3583 if (err < 0)
3584 return err;
3585 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3586 if (err < 0)
3587 return err;
3588 return 0;
3589}
3590
3591/* create bound capture volume and switch controls */
3592static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3593 unsigned int vol_ctl, unsigned int sw_ctl)
3594{
3595 struct hda_gen_spec *spec = codec->spec;
3596 struct snd_kcontrol_new *knew;
3597
3598 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003599 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003600 if (!knew)
3601 return -ENOMEM;
3602 knew->index = idx;
3603 knew->private_value = vol_ctl;
3604 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3605 }
3606 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003607 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003608 if (!knew)
3609 return -ENOMEM;
3610 knew->index = idx;
3611 knew->private_value = sw_ctl;
3612 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3613 }
3614 return 0;
3615}
3616
3617/* return the vol ctl when used first in the imux list */
3618static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3619{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003620 struct nid_path *path;
3621 unsigned int ctl;
3622 int i;
3623
Takashi Iwaic697b712013-01-07 17:09:26 +01003624 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003625 if (!path)
3626 return 0;
3627 ctl = path->ctls[type];
3628 if (!ctl)
3629 return 0;
3630 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003631 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003632 if (path && path->ctls[type] == ctl)
3633 return 0;
3634 }
3635 return ctl;
3636}
3637
3638/* create individual capture volume and switch controls per input */
3639static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3640{
3641 struct hda_gen_spec *spec = codec->spec;
3642 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003643 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003644
3645 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003646 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003647 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003648
Takashi Iwaic9700422013-01-18 10:17:30 +01003649 idx = imux->items[i].index;
3650 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003651 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003652 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3653
3654 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003655 err = add_single_cap_ctl(codec,
3656 spec->input_labels[idx],
3657 spec->input_label_idxs[idx],
3658 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003659 get_first_cap_ctl(codec, i, type),
3660 inv_dmic);
3661 if (err < 0)
3662 return err;
3663 }
3664 }
3665 return 0;
3666}
3667
3668static int create_capture_mixers(struct hda_codec *codec)
3669{
3670 struct hda_gen_spec *spec = codec->spec;
3671 struct hda_input_mux *imux = &spec->input_mux;
3672 int i, n, nums, err;
3673
3674 if (spec->dyn_adc_switch)
3675 nums = 1;
3676 else
3677 nums = spec->num_adc_nids;
3678
3679 if (!spec->auto_mic && imux->num_items > 1) {
3680 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003681 const char *name;
3682 name = nums > 1 ? "Input Source" : "Capture Source";
3683 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003684 if (!knew)
3685 return -ENOMEM;
3686 knew->count = nums;
3687 }
3688
3689 for (n = 0; n < nums; n++) {
3690 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003691 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003692 bool inv_dmic = false;
3693 int vol, sw;
3694
3695 vol = sw = 0;
3696 for (i = 0; i < imux->num_items; i++) {
3697 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003698 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003699 if (!path)
3700 continue;
3701 parse_capvol_in_path(codec, path);
3702 if (!vol)
3703 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003704 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003705 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003706 if (!same_amp_caps(codec, vol,
3707 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3708 multi_cap_vol = true;
3709 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003710 if (!sw)
3711 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003712 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003713 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003714 if (!same_amp_caps(codec, sw,
3715 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3716 multi_cap_vol = true;
3717 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003718 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3719 inv_dmic = true;
3720 }
3721
3722 if (!multi)
3723 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3724 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003725 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003726 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3727 else
3728 err = create_multi_cap_vol_ctl(codec);
3729 if (err < 0)
3730 return err;
3731 }
3732
3733 return 0;
3734}
3735
3736/*
3737 * add mic boosts if needed
3738 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003739
3740/* check whether the given amp is feasible as a boost volume */
3741static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3742 int dir, int idx)
3743{
3744 unsigned int step;
3745
3746 if (!nid_has_volume(codec, nid, dir) ||
3747 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3748 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3749 return false;
3750
3751 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3752 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3753 if (step < 0x20)
3754 return false;
3755 return true;
3756}
3757
3758/* look for a boost amp in a widget close to the pin */
3759static unsigned int look_for_boost_amp(struct hda_codec *codec,
3760 struct nid_path *path)
3761{
3762 unsigned int val = 0;
3763 hda_nid_t nid;
3764 int depth;
3765
3766 for (depth = 0; depth < 3; depth++) {
3767 if (depth >= path->depth - 1)
3768 break;
3769 nid = path->path[depth];
3770 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3771 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3772 break;
3773 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3774 path->idx[depth])) {
3775 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3776 HDA_INPUT);
3777 break;
3778 }
3779 }
3780
3781 return val;
3782}
3783
Takashi Iwai352f7f92012-12-19 12:52:06 +01003784static int parse_mic_boost(struct hda_codec *codec)
3785{
3786 struct hda_gen_spec *spec = codec->spec;
3787 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003788 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003789 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003790
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003791 if (!spec->num_adc_nids)
3792 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003793
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003794 for (i = 0; i < imux->num_items; i++) {
3795 struct nid_path *path;
3796 unsigned int val;
3797 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003798 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003799
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003800 idx = imux->items[i].index;
3801 if (idx >= imux->num_items)
3802 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003803
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003804 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003805 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003806 continue;
3807
3808 path = get_input_path(codec, 0, i);
3809 if (!path)
3810 continue;
3811
3812 val = look_for_boost_amp(codec, path);
3813 if (!val)
3814 continue;
3815
3816 /* create a boost control */
3817 snprintf(boost_label, sizeof(boost_label),
3818 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003819 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3820 spec->input_label_idxs[idx], val))
3821 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003822
3823 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003824 }
3825 return 0;
3826}
3827
3828/*
3829 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3830 */
3831static void parse_digital(struct hda_codec *codec)
3832{
3833 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003834 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003835 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003836 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003837
3838 /* support multiple SPDIFs; the secondary is set up as a slave */
3839 nums = 0;
3840 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003841 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003842 dig_nid = look_for_dac(codec, pin, true);
3843 if (!dig_nid)
3844 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003845 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003846 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003847 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003848 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003849 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003850 path->pin_fixed = true; /* no jack detection */
Takashi Iwai196c17662013-01-04 15:01:40 +01003851 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003852 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003853 if (!nums) {
3854 spec->multiout.dig_out_nid = dig_nid;
3855 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3856 } else {
3857 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3858 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
Dan Carpenterd5764222014-05-14 17:18:31 +03003859 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003860 spec->slave_dig_outs[nums - 1] = dig_nid;
3861 }
3862 nums++;
3863 }
3864
3865 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003866 pin = spec->autocfg.dig_in_pin;
Takashi Iwai7639a062015-03-03 10:07:24 +01003867 for_each_hda_codec_node(dig_nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003868 unsigned int wcaps = get_wcaps(codec, dig_nid);
3869 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3870 continue;
3871 if (!(wcaps & AC_WCAP_DIGITAL))
3872 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003873 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003874 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003875 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003876 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003877 path->pin_fixed = true; /* no jack */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003878 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003879 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003880 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003881 break;
3882 }
3883 }
3884 }
3885}
3886
3887
3888/*
3889 * input MUX handling
3890 */
3891
3892static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3893
3894/* select the given imux item; either unmute exclusively or select the route */
3895static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3896 unsigned int idx)
3897{
3898 struct hda_gen_spec *spec = codec->spec;
3899 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003900 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003901
3902 imux = &spec->input_mux;
3903 if (!imux->num_items)
3904 return 0;
3905
3906 if (idx >= imux->num_items)
3907 idx = imux->num_items - 1;
3908 if (spec->cur_mux[adc_idx] == idx)
3909 return 0;
3910
Takashi Iwai55196ff2013-01-24 17:32:56 +01003911 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3912 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003913 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003914 if (old_path->active)
3915 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003916
3917 spec->cur_mux[adc_idx] = idx;
3918
Takashi Iwai967303d2013-02-19 17:12:42 +01003919 if (spec->hp_mic)
3920 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003921
3922 if (spec->dyn_adc_switch)
3923 dyn_adc_pcm_resetup(codec, idx);
3924
Takashi Iwaic697b712013-01-07 17:09:26 +01003925 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003926 if (!path)
3927 return 0;
3928 if (path->active)
3929 return 0;
3930 snd_hda_activate_path(codec, path, true, false);
3931 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003932 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003933 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003934 return 1;
3935}
3936
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003937/* power up/down widgets in the all paths that match with the given NID
3938 * as terminals (either start- or endpoint)
3939 *
3940 * returns the last changed NID, or zero if unchanged.
3941 */
3942static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
3943 int pin_state, int stream_state)
3944{
3945 struct hda_gen_spec *spec = codec->spec;
3946 hda_nid_t last, changed = 0;
3947 struct nid_path *path;
3948 int n;
3949
3950 for (n = 0; n < spec->paths.used; n++) {
3951 path = snd_array_elem(&spec->paths, n);
3952 if (path->path[0] == nid ||
3953 path->path[path->depth - 1] == nid) {
3954 bool pin_old = path->pin_enabled;
3955 bool stream_old = path->stream_enabled;
3956
3957 if (pin_state >= 0)
3958 path->pin_enabled = pin_state;
3959 if (stream_state >= 0)
3960 path->stream_enabled = stream_state;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003961 if ((!path->pin_fixed && path->pin_enabled != pin_old)
3962 || path->stream_enabled != stream_old) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003963 last = path_power_update(codec, path, true);
3964 if (last)
3965 changed = last;
3966 }
3967 }
3968 }
3969 return changed;
3970}
3971
Takashi Iwaid5ac0102015-04-09 10:21:30 +02003972/* check the jack status for power control */
3973static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
3974{
3975 if (!is_jack_detectable(codec, pin))
3976 return true;
3977 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
3978}
3979
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003980/* power up/down the paths of the given pin according to the jack state;
3981 * power = 0/1 : only power up/down if it matches with the jack state,
3982 * < 0 : force power up/down to follow the jack sate
3983 *
3984 * returns the last changed NID, or zero if unchanged.
3985 */
3986static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
3987 int power)
3988{
3989 bool on;
3990
Takashi Iwai967b1302015-03-20 18:21:03 +01003991 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003992 return 0;
3993
Takashi Iwaid5ac0102015-04-09 10:21:30 +02003994 on = detect_pin_state(codec, pin);
3995
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003996 if (power >= 0 && on != power)
3997 return 0;
3998 return set_path_power(codec, pin, on, -1);
3999}
4000
4001static void pin_power_callback(struct hda_codec *codec,
4002 struct hda_jack_callback *jack,
4003 bool on)
4004{
4005 if (jack && jack->tbl->nid)
4006 sync_power_state_change(codec,
4007 set_pin_power_jack(codec, jack->tbl->nid, on));
4008}
4009
4010/* callback only doing power up -- called at first */
4011static void pin_power_up_callback(struct hda_codec *codec,
4012 struct hda_jack_callback *jack)
4013{
4014 pin_power_callback(codec, jack, true);
4015}
4016
4017/* callback only doing power down -- called at last */
4018static void pin_power_down_callback(struct hda_codec *codec,
4019 struct hda_jack_callback *jack)
4020{
4021 pin_power_callback(codec, jack, false);
4022}
4023
4024/* set up the power up/down callbacks */
4025static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4026 const hda_nid_t *pins, bool on)
4027{
4028 int i;
4029 hda_jack_callback_fn cb =
4030 on ? pin_power_up_callback : pin_power_down_callback;
4031
4032 for (i = 0; i < num_pins && pins[i]; i++) {
4033 if (is_jack_detectable(codec, pins[i]))
4034 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4035 else
4036 set_path_power(codec, pins[i], true, -1);
4037 }
4038}
4039
4040/* enabled power callback to each available I/O pin with jack detections;
4041 * the digital I/O pins are excluded because of the unreliable detectsion
4042 */
4043static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4044{
4045 struct hda_gen_spec *spec = codec->spec;
4046 struct auto_pin_cfg *cfg = &spec->autocfg;
4047 int i;
4048
Takashi Iwai967b1302015-03-20 18:21:03 +01004049 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004050 return;
4051 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4052 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4053 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4054 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4055 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4056 for (i = 0; i < cfg->num_inputs; i++)
4057 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4058}
4059
4060/* sync path power up/down with the jack states of given pins */
4061static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4062 const hda_nid_t *pins)
4063{
4064 int i;
4065
4066 for (i = 0; i < num_pins && pins[i]; i++)
4067 if (is_jack_detectable(codec, pins[i]))
4068 set_pin_power_jack(codec, pins[i], -1);
4069}
4070
4071/* sync path power up/down with pins; called at init and resume */
4072static void sync_all_pin_power_ctls(struct hda_codec *codec)
4073{
4074 struct hda_gen_spec *spec = codec->spec;
4075 struct auto_pin_cfg *cfg = &spec->autocfg;
4076 int i;
4077
Takashi Iwai967b1302015-03-20 18:21:03 +01004078 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004079 return;
4080 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4081 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4082 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4083 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4084 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4085 for (i = 0; i < cfg->num_inputs; i++)
4086 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4087}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004088
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004089/* add fake paths if not present yet */
4090static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4091 int num_pins, const hda_nid_t *pins)
4092{
4093 struct hda_gen_spec *spec = codec->spec;
4094 struct nid_path *path;
4095 int i;
4096
4097 for (i = 0; i < num_pins; i++) {
4098 if (!pins[i])
4099 break;
4100 if (get_nid_path(codec, nid, pins[i], 0))
4101 continue;
4102 path = snd_array_new(&spec->paths);
4103 if (!path)
4104 return -ENOMEM;
4105 memset(path, 0, sizeof(*path));
4106 path->depth = 2;
4107 path->path[0] = nid;
4108 path->path[1] = pins[i];
4109 path->active = true;
4110 }
4111 return 0;
4112}
4113
4114/* create fake paths to all outputs from beep */
4115static int add_fake_beep_paths(struct hda_codec *codec)
4116{
4117 struct hda_gen_spec *spec = codec->spec;
4118 struct auto_pin_cfg *cfg = &spec->autocfg;
4119 hda_nid_t nid = spec->beep_nid;
4120 int err;
4121
Takashi Iwai967b1302015-03-20 18:21:03 +01004122 if (!codec->power_save_node || !nid)
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004123 return 0;
4124 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4125 if (err < 0)
4126 return err;
4127 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4128 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4129 if (err < 0)
4130 return err;
4131 }
4132 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4133 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4134 cfg->speaker_pins);
4135 if (err < 0)
4136 return err;
4137 }
4138 return 0;
4139}
4140
4141/* power up/down beep widget and its output paths */
4142static void beep_power_hook(struct hda_beep *beep, bool on)
4143{
4144 set_path_power(beep->codec, beep->nid, -1, on);
4145}
4146
Takashi Iwai6b275b12015-03-20 18:11:05 +01004147/**
4148 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4149 * @codec: the HDA codec
4150 * @pin: NID of pin to fix
4151 */
4152int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4153{
4154 struct hda_gen_spec *spec = codec->spec;
4155 struct nid_path *path;
4156
4157 path = snd_array_new(&spec->paths);
4158 if (!path)
4159 return -ENOMEM;
4160 memset(path, 0, sizeof(*path));
4161 path->depth = 1;
4162 path->path[0] = pin;
4163 path->active = true;
4164 path->pin_fixed = true;
4165 path->stream_enabled = true;
4166 return 0;
4167}
4168EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4169
Takashi Iwai352f7f92012-12-19 12:52:06 +01004170/*
4171 * Jack detections for HP auto-mute and mic-switch
4172 */
4173
4174/* check each pin in the given array; returns true if any of them is plugged */
4175static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4176{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004177 int i;
4178 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004179
4180 for (i = 0; i < num_pins; i++) {
4181 hda_nid_t nid = pins[i];
4182 if (!nid)
4183 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01004184 /* don't detect pins retasked as inputs */
4185 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4186 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004187 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4188 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004189 }
4190 return present;
4191}
4192
4193/* standard HP/line-out auto-mute helper */
4194static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004195 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004196{
4197 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004198 int i;
4199
4200 for (i = 0; i < num_pins; i++) {
4201 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01004202 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004203 if (!nid)
4204 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004205
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004206 oldval = snd_hda_codec_get_pin_target(codec, nid);
4207 if (oldval & PIN_IN)
4208 continue; /* no mute for inputs */
4209
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004210 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004211 struct nid_path *path;
4212 hda_nid_t mute_nid;
4213
4214 path = snd_hda_get_path_from_idx(codec, paths[i]);
4215 if (!path)
4216 continue;
4217 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4218 if (!mute_nid)
4219 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004220 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004221 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004222 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004223 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004224 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004225 } else {
4226 /* don't reset VREF value in case it's controlling
4227 * the amp (see alc861_fixup_asus_amp_vref_0f())
4228 */
4229 if (spec->keep_vref_in_automute)
4230 val = oldval & ~PIN_HP;
4231 else
4232 val = 0;
4233 if (!mute)
4234 val |= oldval;
4235 /* here we call update_pin_ctl() so that the pinctl is
4236 * changed without changing the pinctl target value;
4237 * the original target value will be still referred at
4238 * the init / resume again
4239 */
4240 update_pin_ctl(codec, nid, val);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004241 }
4242
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01004243 set_pin_eapd(codec, nid, !mute);
Takashi Iwai967b1302015-03-20 18:21:03 +01004244 if (codec->power_save_node) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004245 bool on = !mute;
4246 if (on)
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004247 on = detect_pin_state(codec, nid);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004248 set_path_power(codec, nid, on, -1);
4249 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004250 }
4251}
4252
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004253/**
4254 * snd_hda_gen_update_outputs - Toggle outputs muting
4255 * @codec: the HDA codec
4256 *
4257 * Update the mute status of all outputs based on the current jack states.
4258 */
Takashi Iwai5d550e12012-12-19 15:16:44 +01004259void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004260{
4261 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004262 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004263 int on;
4264
4265 /* Control HP pins/amps depending on master_mute state;
4266 * in general, HP pins/amps control should be enabled in all cases,
4267 * but currently set only for master_mute, just to be safe
4268 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004269 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4270 paths = spec->out_paths;
4271 else
4272 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01004273 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004274 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004275
4276 if (!spec->automute_speaker)
4277 on = 0;
4278 else
4279 on = spec->hp_jack_present | spec->line_jack_present;
4280 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01004281 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004282 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4283 paths = spec->out_paths;
4284 else
4285 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004286 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004287 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004288
4289 /* toggle line-out mutes if needed, too */
4290 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4291 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4292 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4293 return;
4294 if (!spec->automute_lo)
4295 on = 0;
4296 else
4297 on = spec->hp_jack_present;
4298 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01004299 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004300 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004301 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004302 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004303}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004304EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004305
4306static void call_update_outputs(struct hda_codec *codec)
4307{
4308 struct hda_gen_spec *spec = codec->spec;
4309 if (spec->automute_hook)
4310 spec->automute_hook(codec);
4311 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01004312 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004313
4314 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4315 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4316 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004317}
4318
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004319/**
4320 * snd_hda_gen_hp_automute - standard HP-automute helper
4321 * @codec: the HDA codec
4322 * @jack: jack object, NULL for the whole
4323 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004324void snd_hda_gen_hp_automute(struct hda_codec *codec,
4325 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004326{
4327 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01004328 hda_nid_t *pins = spec->autocfg.hp_pins;
4329 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004330
Takashi Iwai92603c52013-01-22 07:46:31 +01004331 /* No detection for the first HP jack during indep-HP mode */
4332 if (spec->indep_hp_enabled) {
4333 pins++;
4334 num_pins--;
4335 }
4336
4337 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004338 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4339 return;
4340 call_update_outputs(codec);
4341}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004342EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004343
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004344/**
4345 * snd_hda_gen_line_automute - standard line-out-automute helper
4346 * @codec: the HDA codec
4347 * @jack: jack object, NULL for the whole
4348 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004349void snd_hda_gen_line_automute(struct hda_codec *codec,
4350 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004351{
4352 struct hda_gen_spec *spec = codec->spec;
4353
4354 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4355 return;
4356 /* check LO jack only when it's different from HP */
4357 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4358 return;
4359
4360 spec->line_jack_present =
4361 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4362 spec->autocfg.line_out_pins);
4363 if (!spec->automute_speaker || !spec->detect_lo)
4364 return;
4365 call_update_outputs(codec);
4366}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004367EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004368
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004369/**
4370 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4371 * @codec: the HDA codec
4372 * @jack: jack object, NULL for the whole
4373 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004374void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4375 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004376{
4377 struct hda_gen_spec *spec = codec->spec;
4378 int i;
4379
4380 if (!spec->auto_mic)
4381 return;
4382
4383 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01004384 hda_nid_t pin = spec->am_entry[i].pin;
4385 /* don't detect pins retasked as outputs */
4386 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4387 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004388 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004389 mux_select(codec, 0, spec->am_entry[i].idx);
4390 return;
4391 }
4392 }
4393 mux_select(codec, 0, spec->am_entry[0].idx);
4394}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004395EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004396
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004397/* call appropriate hooks */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004398static void call_hp_automute(struct hda_codec *codec,
4399 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004400{
4401 struct hda_gen_spec *spec = codec->spec;
4402 if (spec->hp_automute_hook)
4403 spec->hp_automute_hook(codec, jack);
4404 else
4405 snd_hda_gen_hp_automute(codec, jack);
4406}
4407
4408static void call_line_automute(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004409 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004410{
4411 struct hda_gen_spec *spec = codec->spec;
4412 if (spec->line_automute_hook)
4413 spec->line_automute_hook(codec, jack);
4414 else
4415 snd_hda_gen_line_automute(codec, jack);
4416}
4417
4418static void call_mic_autoswitch(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004419 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004420{
4421 struct hda_gen_spec *spec = codec->spec;
4422 if (spec->mic_autoswitch_hook)
4423 spec->mic_autoswitch_hook(codec, jack);
4424 else
4425 snd_hda_gen_mic_autoswitch(codec, jack);
4426}
4427
Takashi Iwai963afde2013-05-31 15:20:31 +02004428/* update jack retasking */
4429static void update_automute_all(struct hda_codec *codec)
4430{
4431 call_hp_automute(codec, NULL);
4432 call_line_automute(codec, NULL);
4433 call_mic_autoswitch(codec, NULL);
4434}
4435
Takashi Iwai352f7f92012-12-19 12:52:06 +01004436/*
4437 * Auto-Mute mode mixer enum support
4438 */
4439static int automute_mode_info(struct snd_kcontrol *kcontrol,
4440 struct snd_ctl_elem_info *uinfo)
4441{
4442 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4443 struct hda_gen_spec *spec = codec->spec;
4444 static const char * const texts3[] = {
4445 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004446 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Takashi Iwai352f7f92012-12-19 12:52:06 +01004448 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4449 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4450 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4451}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452
Takashi Iwai352f7f92012-12-19 12:52:06 +01004453static int automute_mode_get(struct snd_kcontrol *kcontrol,
4454 struct snd_ctl_elem_value *ucontrol)
4455{
4456 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4457 struct hda_gen_spec *spec = codec->spec;
4458 unsigned int val = 0;
4459 if (spec->automute_speaker)
4460 val++;
4461 if (spec->automute_lo)
4462 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004463
Takashi Iwai352f7f92012-12-19 12:52:06 +01004464 ucontrol->value.enumerated.item[0] = val;
4465 return 0;
4466}
4467
4468static int automute_mode_put(struct snd_kcontrol *kcontrol,
4469 struct snd_ctl_elem_value *ucontrol)
4470{
4471 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4472 struct hda_gen_spec *spec = codec->spec;
4473
4474 switch (ucontrol->value.enumerated.item[0]) {
4475 case 0:
4476 if (!spec->automute_speaker && !spec->automute_lo)
4477 return 0;
4478 spec->automute_speaker = 0;
4479 spec->automute_lo = 0;
4480 break;
4481 case 1:
4482 if (spec->automute_speaker_possible) {
4483 if (!spec->automute_lo && spec->automute_speaker)
4484 return 0;
4485 spec->automute_speaker = 1;
4486 spec->automute_lo = 0;
4487 } else if (spec->automute_lo_possible) {
4488 if (spec->automute_lo)
4489 return 0;
4490 spec->automute_lo = 1;
4491 } else
4492 return -EINVAL;
4493 break;
4494 case 2:
4495 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4496 return -EINVAL;
4497 if (spec->automute_speaker && spec->automute_lo)
4498 return 0;
4499 spec->automute_speaker = 1;
4500 spec->automute_lo = 1;
4501 break;
4502 default:
4503 return -EINVAL;
4504 }
4505 call_update_outputs(codec);
4506 return 1;
4507}
4508
4509static const struct snd_kcontrol_new automute_mode_enum = {
4510 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4511 .name = "Auto-Mute Mode",
4512 .info = automute_mode_info,
4513 .get = automute_mode_get,
4514 .put = automute_mode_put,
4515};
4516
4517static int add_automute_mode_enum(struct hda_codec *codec)
4518{
4519 struct hda_gen_spec *spec = codec->spec;
4520
Takashi Iwai12c93df2012-12-19 14:38:33 +01004521 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004522 return -ENOMEM;
4523 return 0;
4524}
4525
4526/*
4527 * Check the availability of HP/line-out auto-mute;
4528 * Set up appropriately if really supported
4529 */
4530static int check_auto_mute_availability(struct hda_codec *codec)
4531{
4532 struct hda_gen_spec *spec = codec->spec;
4533 struct auto_pin_cfg *cfg = &spec->autocfg;
4534 int present = 0;
4535 int i, err;
4536
Takashi Iwaif72706b2013-01-16 18:20:07 +01004537 if (spec->suppress_auto_mute)
4538 return 0;
4539
Takashi Iwai352f7f92012-12-19 12:52:06 +01004540 if (cfg->hp_pins[0])
4541 present++;
4542 if (cfg->line_out_pins[0])
4543 present++;
4544 if (cfg->speaker_pins[0])
4545 present++;
4546 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004547 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004548
4549 if (!cfg->speaker_pins[0] &&
4550 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4551 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4552 sizeof(cfg->speaker_pins));
4553 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
Takashi Iwai352f7f92012-12-19 12:52:06 +01004556 if (!cfg->hp_pins[0] &&
4557 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4558 memcpy(cfg->hp_pins, cfg->line_out_pins,
4559 sizeof(cfg->hp_pins));
4560 cfg->hp_outs = cfg->line_outs;
4561 }
4562
4563 for (i = 0; i < cfg->hp_outs; i++) {
4564 hda_nid_t nid = cfg->hp_pins[i];
4565 if (!is_jack_detectable(codec, nid))
4566 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004567 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai62f949b2014-09-11 14:06:53 +02004568 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004569 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004570 spec->detect_hp = 1;
4571 }
4572
4573 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4574 if (cfg->speaker_outs)
4575 for (i = 0; i < cfg->line_outs; i++) {
4576 hda_nid_t nid = cfg->line_out_pins[i];
4577 if (!is_jack_detectable(codec, nid))
4578 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004579 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004580 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004581 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004582 spec->detect_lo = 1;
4583 }
4584 spec->automute_lo_possible = spec->detect_hp;
4585 }
4586
4587 spec->automute_speaker_possible = cfg->speaker_outs &&
4588 (spec->detect_hp || spec->detect_lo);
4589
4590 spec->automute_lo = spec->automute_lo_possible;
4591 spec->automute_speaker = spec->automute_speaker_possible;
4592
4593 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4594 /* create a control for automute mode */
4595 err = add_automute_mode_enum(codec);
4596 if (err < 0)
4597 return err;
4598 }
4599 return 0;
4600}
4601
Takashi Iwai352f7f92012-12-19 12:52:06 +01004602/* check whether all auto-mic pins are valid; setup indices if OK */
4603static bool auto_mic_check_imux(struct hda_codec *codec)
4604{
4605 struct hda_gen_spec *spec = codec->spec;
4606 const struct hda_input_mux *imux;
4607 int i;
4608
4609 imux = &spec->input_mux;
4610 for (i = 0; i < spec->am_num_entries; i++) {
4611 spec->am_entry[i].idx =
4612 find_idx_in_nid_list(spec->am_entry[i].pin,
4613 spec->imux_pins, imux->num_items);
4614 if (spec->am_entry[i].idx < 0)
4615 return false; /* no corresponding imux */
4616 }
4617
4618 /* we don't need the jack detection for the first pin */
4619 for (i = 1; i < spec->am_num_entries; i++)
4620 snd_hda_jack_detect_enable_callback(codec,
4621 spec->am_entry[i].pin,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004622 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004623 return true;
4624}
4625
4626static int compare_attr(const void *ap, const void *bp)
4627{
4628 const struct automic_entry *a = ap;
4629 const struct automic_entry *b = bp;
4630 return (int)(a->attr - b->attr);
4631}
4632
4633/*
4634 * Check the availability of auto-mic switch;
4635 * Set up if really supported
4636 */
4637static int check_auto_mic_availability(struct hda_codec *codec)
4638{
4639 struct hda_gen_spec *spec = codec->spec;
4640 struct auto_pin_cfg *cfg = &spec->autocfg;
4641 unsigned int types;
4642 int i, num_pins;
4643
Takashi Iwaid12daf62013-01-07 16:32:11 +01004644 if (spec->suppress_auto_mic)
4645 return 0;
4646
Takashi Iwai352f7f92012-12-19 12:52:06 +01004647 types = 0;
4648 num_pins = 0;
4649 for (i = 0; i < cfg->num_inputs; i++) {
4650 hda_nid_t nid = cfg->inputs[i].pin;
4651 unsigned int attr;
4652 attr = snd_hda_codec_get_pincfg(codec, nid);
4653 attr = snd_hda_get_input_pin_attr(attr);
4654 if (types & (1 << attr))
4655 return 0; /* already occupied */
4656 switch (attr) {
4657 case INPUT_PIN_ATTR_INT:
4658 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4659 return 0; /* invalid type */
4660 break;
4661 case INPUT_PIN_ATTR_UNUSED:
4662 return 0; /* invalid entry */
4663 default:
4664 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4665 return 0; /* invalid type */
4666 if (!spec->line_in_auto_switch &&
4667 cfg->inputs[i].type != AUTO_PIN_MIC)
4668 return 0; /* only mic is allowed */
4669 if (!is_jack_detectable(codec, nid))
4670 return 0; /* no unsol support */
4671 break;
4672 }
4673 if (num_pins >= MAX_AUTO_MIC_PINS)
4674 return 0;
4675 types |= (1 << attr);
4676 spec->am_entry[num_pins].pin = nid;
4677 spec->am_entry[num_pins].attr = attr;
4678 num_pins++;
4679 }
4680
4681 if (num_pins < 2)
4682 return 0;
4683
4684 spec->am_num_entries = num_pins;
4685 /* sort the am_entry in the order of attr so that the pin with a
4686 * higher attr will be selected when the jack is plugged.
4687 */
4688 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4689 compare_attr, NULL);
4690
4691 if (!auto_mic_check_imux(codec))
4692 return 0;
4693
4694 spec->auto_mic = 1;
4695 spec->num_adc_nids = 1;
4696 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004697 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004698 spec->am_entry[0].pin,
4699 spec->am_entry[1].pin,
4700 spec->am_entry[2].pin);
4701
4702 return 0;
4703}
4704
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004705/**
4706 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4707 * into power down
4708 * @codec: the HDA codec
4709 * @nid: NID to evalute
4710 * @power_state: target power state
4711 */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004712unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004713 hda_nid_t nid,
4714 unsigned int power_state)
4715{
Takashi Iwaib6c09b32015-04-09 10:25:03 +02004716 struct hda_gen_spec *spec = codec->spec;
4717
4718 if (!spec->power_down_unused && !codec->power_save_node)
4719 return power_state;
Takashi Iwai7639a062015-03-03 10:07:24 +01004720 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004721 return power_state;
4722 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4723 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004724 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004725 return power_state;
4726 return AC_PWRST_D3;
4727}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004728EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004729
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004730/* mute all aamix inputs initially; parse up to the first leaves */
4731static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4732{
4733 int i, nums;
4734 const hda_nid_t *conn;
4735 bool has_amp;
4736
4737 nums = snd_hda_get_conn_list(codec, mix, &conn);
4738 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4739 for (i = 0; i < nums; i++) {
4740 if (has_amp)
Takashi Iwaief403ed2015-03-12 08:30:11 +01004741 update_amp(codec, mix, HDA_INPUT, i,
4742 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004743 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
Takashi Iwaief403ed2015-03-12 08:30:11 +01004744 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4745 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004746 }
4747}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004748
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004749/**
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004750 * snd_hda_gen_stream_pm - Stream power management callback
4751 * @codec: the HDA codec
4752 * @nid: audio widget
4753 * @on: power on/off flag
4754 *
Takashi Iwai967b1302015-03-20 18:21:03 +01004755 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004756 */
4757void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4758{
Takashi Iwai967b1302015-03-20 18:21:03 +01004759 if (codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004760 set_path_power(codec, nid, -1, on);
4761}
4762EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4763
4764/**
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004765 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4766 * set up the hda_gen_spec
4767 * @codec: the HDA codec
4768 * @cfg: Parsed pin configuration
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004769 *
4770 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004771 * or a negative error code
4772 */
4773int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004774 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004775{
4776 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004777 int err;
4778
Takashi Iwai1c70a582013-01-11 17:48:22 +01004779 parse_user_hints(codec);
4780
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004781 if (spec->mixer_nid && !spec->mixer_merge_nid)
4782 spec->mixer_merge_nid = spec->mixer_nid;
4783
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004784 if (cfg != &spec->autocfg) {
4785 spec->autocfg = *cfg;
4786 cfg = &spec->autocfg;
4787 }
4788
Takashi Iwai98bd1112013-03-22 14:53:50 +01004789 if (!spec->main_out_badness)
4790 spec->main_out_badness = &hda_main_out_badness;
4791 if (!spec->extra_out_badness)
4792 spec->extra_out_badness = &hda_extra_out_badness;
4793
David Henningsson6fc4cb92013-01-16 15:58:45 +01004794 fill_all_dac_nids(codec);
4795
Takashi Iwai352f7f92012-12-19 12:52:06 +01004796 if (!cfg->line_outs) {
4797 if (cfg->dig_outs || cfg->dig_in_pin) {
4798 spec->multiout.max_channels = 2;
4799 spec->no_analog = 1;
4800 goto dig_only;
4801 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004802 if (!cfg->num_inputs && !cfg->dig_in_pin)
4803 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004804 }
4805
4806 if (!spec->no_primary_hp &&
4807 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4808 cfg->line_outs <= cfg->hp_outs) {
4809 /* use HP as primary out */
4810 cfg->speaker_outs = cfg->line_outs;
4811 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4812 sizeof(cfg->speaker_pins));
4813 cfg->line_outs = cfg->hp_outs;
4814 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4815 cfg->hp_outs = 0;
4816 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4817 cfg->line_out_type = AUTO_PIN_HP_OUT;
4818 }
4819
4820 err = parse_output_paths(codec);
4821 if (err < 0)
4822 return err;
4823 err = create_multi_channel_mode(codec);
4824 if (err < 0)
4825 return err;
4826 err = create_multi_out_ctls(codec, cfg);
4827 if (err < 0)
4828 return err;
4829 err = create_hp_out_ctls(codec);
4830 if (err < 0)
4831 return err;
4832 err = create_speaker_out_ctls(codec);
4833 if (err < 0)
4834 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004835 err = create_indep_hp_ctls(codec);
4836 if (err < 0)
4837 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004838 err = create_loopback_mixing_ctl(codec);
4839 if (err < 0)
4840 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004841 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004842 if (err < 0)
4843 return err;
4844 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004845 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004846 return err;
4847
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004848 /* add power-down pin callbacks at first */
4849 add_all_pin_power_ctls(codec, false);
4850
Takashi Iwaia07a9492013-01-07 16:44:06 +01004851 spec->const_channel_count = spec->ext_channel_count;
4852 /* check the multiple speaker and headphone pins */
4853 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4854 spec->const_channel_count = max(spec->const_channel_count,
4855 cfg->speaker_outs * 2);
4856 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4857 spec->const_channel_count = max(spec->const_channel_count,
4858 cfg->hp_outs * 2);
4859 spec->multiout.max_channels = max(spec->ext_channel_count,
4860 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004861
4862 err = check_auto_mute_availability(codec);
4863 if (err < 0)
4864 return err;
4865
4866 err = check_dyn_adc_switch(codec);
4867 if (err < 0)
4868 return err;
4869
Takashi Iwai967303d2013-02-19 17:12:42 +01004870 err = check_auto_mic_availability(codec);
4871 if (err < 0)
4872 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004873
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004874 /* add stereo mix if available and not enabled yet */
4875 if (!spec->auto_mic && spec->mixer_nid &&
Takashi Iwai74f14b32014-12-15 13:43:59 +01004876 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4877 spec->input_mux.num_items > 1) {
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004878 err = parse_capture_source(codec, spec->mixer_nid,
4879 CFG_IDX_MIX, spec->num_all_adcs,
4880 "Stereo Mix", 0);
4881 if (err < 0)
4882 return err;
4883 }
4884
4885
Takashi Iwai352f7f92012-12-19 12:52:06 +01004886 err = create_capture_mixers(codec);
4887 if (err < 0)
4888 return err;
4889
4890 err = parse_mic_boost(codec);
4891 if (err < 0)
4892 return err;
4893
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004894 /* create "Headphone Mic Jack Mode" if no input selection is
4895 * available (or user specifies add_jack_modes hint)
4896 */
4897 if (spec->hp_mic_pin &&
4898 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4899 spec->add_jack_modes)) {
4900 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4901 if (err < 0)
4902 return err;
4903 }
4904
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004905 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004906 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4907 err = create_out_jack_modes(codec, cfg->line_outs,
4908 cfg->line_out_pins);
4909 if (err < 0)
4910 return err;
4911 }
4912 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4913 err = create_out_jack_modes(codec, cfg->hp_outs,
4914 cfg->hp_pins);
4915 if (err < 0)
4916 return err;
4917 }
4918 }
4919
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004920 /* add power-up pin callbacks at last */
4921 add_all_pin_power_ctls(codec, true);
4922
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004923 /* mute all aamix input initially */
4924 if (spec->mixer_nid)
4925 mute_all_mixer_nid(codec, spec->mixer_nid);
4926
Takashi Iwai352f7f92012-12-19 12:52:06 +01004927 dig_only:
4928 parse_digital(codec);
4929
Takashi Iwai49fb1892015-05-27 08:37:19 +02004930 if (spec->power_down_unused || codec->power_save_node) {
Takashi Iwai24fef902015-04-09 10:28:15 +02004931 if (!codec->power_filter)
4932 codec->power_filter = snd_hda_gen_path_power_filter;
Takashi Iwai49fb1892015-05-27 08:37:19 +02004933 if (!codec->patch_ops.stream_pm)
4934 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
4935 }
Takashi Iwai55196ff2013-01-24 17:32:56 +01004936
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004937 if (!spec->no_analog && spec->beep_nid) {
4938 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4939 if (err < 0)
4940 return err;
Takashi Iwai967b1302015-03-20 18:21:03 +01004941 if (codec->beep && codec->power_save_node) {
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004942 err = add_fake_beep_paths(codec);
4943 if (err < 0)
4944 return err;
4945 codec->beep->power_hook = beep_power_hook;
4946 }
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004947 }
4948
Takashi Iwai352f7f92012-12-19 12:52:06 +01004949 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004951EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
4953
4954/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004955 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004957
4958/* slave controls for virtual master */
4959static const char * const slave_pfxs[] = {
4960 "Front", "Surround", "Center", "LFE", "Side",
4961 "Headphone", "Speaker", "Mono", "Line Out",
4962 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004963 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4964 "Headphone Front", "Headphone Surround", "Headphone CLFE",
David Henningsson03ad6a82014-10-16 15:33:45 +02004965 "Headphone Side", "Headphone+LO", "Speaker+LO",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004966 NULL,
4967};
4968
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004969/**
4970 * snd_hda_gen_build_controls - Build controls from the parsed results
4971 * @codec: the HDA codec
4972 *
4973 * Pass this to build_controls patch_ops.
4974 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004975int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004977 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979
Takashi Iwai36502d02012-12-19 15:15:10 +01004980 if (spec->kctls.used) {
4981 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4982 if (err < 0)
4983 return err;
4984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
Takashi Iwai352f7f92012-12-19 12:52:06 +01004986 if (spec->multiout.dig_out_nid) {
4987 err = snd_hda_create_dig_out_ctls(codec,
4988 spec->multiout.dig_out_nid,
4989 spec->multiout.dig_out_nid,
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004990 spec->pcm_rec[1]->pcm_type);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004991 if (err < 0)
4992 return err;
4993 if (!spec->no_analog) {
4994 err = snd_hda_create_spdif_share_sw(codec,
4995 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 if (err < 0)
4997 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004998 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 }
5000 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005001 if (spec->dig_in_nid) {
5002 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5003 if (err < 0)
5004 return err;
5005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006
Takashi Iwai352f7f92012-12-19 12:52:06 +01005007 /* if we have no master control, let's create it */
5008 if (!spec->no_analog &&
5009 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01005010 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01005011 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005012 "Playback Volume");
5013 if (err < 0)
5014 return err;
5015 }
5016 if (!spec->no_analog &&
5017 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5018 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5019 NULL, slave_pfxs,
5020 "Playback Switch",
5021 true, &spec->vmaster_mute.sw_kctl);
5022 if (err < 0)
5023 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02005024 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01005025 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5026 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02005027 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5028 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030
Takashi Iwai352f7f92012-12-19 12:52:06 +01005031 free_kctls(spec); /* no longer needed */
5032
Takashi Iwai352f7f92012-12-19 12:52:06 +01005033 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5034 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 return err;
5036
5037 return 0;
5038}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005039EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005040
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
5042/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01005043 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005046static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5047 struct hda_codec *codec,
5048 struct snd_pcm_substream *substream,
5049 int action)
5050{
5051 struct hda_gen_spec *spec = codec->spec;
5052 if (spec->pcm_playback_hook)
5053 spec->pcm_playback_hook(hinfo, codec, substream, action);
5054}
5055
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005056static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5057 struct hda_codec *codec,
5058 struct snd_pcm_substream *substream,
5059 int action)
5060{
5061 struct hda_gen_spec *spec = codec->spec;
5062 if (spec->pcm_capture_hook)
5063 spec->pcm_capture_hook(hinfo, codec, substream, action);
5064}
5065
Takashi Iwai352f7f92012-12-19 12:52:06 +01005066/*
5067 * Analog playback callbacks
5068 */
5069static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5070 struct hda_codec *codec,
5071 struct snd_pcm_substream *substream)
5072{
5073 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005074 int err;
5075
5076 mutex_lock(&spec->pcm_mutex);
5077 err = snd_hda_multi_out_analog_open(codec,
5078 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005079 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005080 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005081 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005082 call_pcm_playback_hook(hinfo, codec, substream,
5083 HDA_GEN_PCM_ACT_OPEN);
5084 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005085 mutex_unlock(&spec->pcm_mutex);
5086 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005087}
5088
5089static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01005090 struct hda_codec *codec,
5091 unsigned int stream_tag,
5092 unsigned int format,
5093 struct snd_pcm_substream *substream)
5094{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005095 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005096 int err;
5097
5098 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5099 stream_tag, format, substream);
5100 if (!err)
5101 call_pcm_playback_hook(hinfo, codec, substream,
5102 HDA_GEN_PCM_ACT_PREPARE);
5103 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005104}
Takashi Iwai97ec5582006-03-21 11:29:07 +01005105
Takashi Iwai352f7f92012-12-19 12:52:06 +01005106static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5107 struct hda_codec *codec,
5108 struct snd_pcm_substream *substream)
5109{
5110 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005111 int err;
5112
5113 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5114 if (!err)
5115 call_pcm_playback_hook(hinfo, codec, substream,
5116 HDA_GEN_PCM_ACT_CLEANUP);
5117 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005118}
5119
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005120static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5121 struct hda_codec *codec,
5122 struct snd_pcm_substream *substream)
5123{
5124 struct hda_gen_spec *spec = codec->spec;
5125 mutex_lock(&spec->pcm_mutex);
5126 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005127 call_pcm_playback_hook(hinfo, codec, substream,
5128 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005129 mutex_unlock(&spec->pcm_mutex);
5130 return 0;
5131}
5132
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005133static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5134 struct hda_codec *codec,
5135 struct snd_pcm_substream *substream)
5136{
5137 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5138 return 0;
5139}
5140
5141static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5142 struct hda_codec *codec,
5143 unsigned int stream_tag,
5144 unsigned int format,
5145 struct snd_pcm_substream *substream)
5146{
5147 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5148 call_pcm_capture_hook(hinfo, codec, substream,
5149 HDA_GEN_PCM_ACT_PREPARE);
5150 return 0;
5151}
5152
5153static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5154 struct hda_codec *codec,
5155 struct snd_pcm_substream *substream)
5156{
5157 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5158 call_pcm_capture_hook(hinfo, codec, substream,
5159 HDA_GEN_PCM_ACT_CLEANUP);
5160 return 0;
5161}
5162
5163static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5164 struct hda_codec *codec,
5165 struct snd_pcm_substream *substream)
5166{
5167 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5168 return 0;
5169}
5170
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005171static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5172 struct hda_codec *codec,
5173 struct snd_pcm_substream *substream)
5174{
5175 struct hda_gen_spec *spec = codec->spec;
5176 int err = 0;
5177
5178 mutex_lock(&spec->pcm_mutex);
Takashi Iwaid1f15e02015-07-08 09:22:10 +02005179 if (spec->indep_hp && !spec->indep_hp_enabled)
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005180 err = -EBUSY;
5181 else
5182 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005183 call_pcm_playback_hook(hinfo, codec, substream,
5184 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005185 mutex_unlock(&spec->pcm_mutex);
5186 return err;
5187}
5188
5189static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5190 struct hda_codec *codec,
5191 struct snd_pcm_substream *substream)
5192{
5193 struct hda_gen_spec *spec = codec->spec;
5194 mutex_lock(&spec->pcm_mutex);
5195 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005196 call_pcm_playback_hook(hinfo, codec, substream,
5197 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005198 mutex_unlock(&spec->pcm_mutex);
5199 return 0;
5200}
5201
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005202static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5203 struct hda_codec *codec,
5204 unsigned int stream_tag,
5205 unsigned int format,
5206 struct snd_pcm_substream *substream)
5207{
5208 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5209 call_pcm_playback_hook(hinfo, codec, substream,
5210 HDA_GEN_PCM_ACT_PREPARE);
5211 return 0;
5212}
5213
5214static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5215 struct hda_codec *codec,
5216 struct snd_pcm_substream *substream)
5217{
5218 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5219 call_pcm_playback_hook(hinfo, codec, substream,
5220 HDA_GEN_PCM_ACT_CLEANUP);
5221 return 0;
5222}
5223
Takashi Iwai352f7f92012-12-19 12:52:06 +01005224/*
5225 * Digital out
5226 */
5227static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5228 struct hda_codec *codec,
5229 struct snd_pcm_substream *substream)
5230{
5231 struct hda_gen_spec *spec = codec->spec;
5232 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5233}
5234
5235static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5236 struct hda_codec *codec,
5237 unsigned int stream_tag,
5238 unsigned int format,
5239 struct snd_pcm_substream *substream)
5240{
5241 struct hda_gen_spec *spec = codec->spec;
5242 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5243 stream_tag, format, substream);
5244}
5245
5246static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5247 struct hda_codec *codec,
5248 struct snd_pcm_substream *substream)
5249{
5250 struct hda_gen_spec *spec = codec->spec;
5251 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5252}
5253
5254static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5255 struct hda_codec *codec,
5256 struct snd_pcm_substream *substream)
5257{
5258 struct hda_gen_spec *spec = codec->spec;
5259 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5260}
5261
5262/*
5263 * Analog capture
5264 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005265#define alt_capture_pcm_open capture_pcm_open
5266#define alt_capture_pcm_close capture_pcm_close
5267
Takashi Iwai352f7f92012-12-19 12:52:06 +01005268static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5269 struct hda_codec *codec,
5270 unsigned int stream_tag,
5271 unsigned int format,
5272 struct snd_pcm_substream *substream)
5273{
5274 struct hda_gen_spec *spec = codec->spec;
5275
5276 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01005277 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005278 call_pcm_capture_hook(hinfo, codec, substream,
5279 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005280 return 0;
5281}
5282
Takashi Iwai352f7f92012-12-19 12:52:06 +01005283static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5284 struct hda_codec *codec,
5285 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01005286{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005287 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01005288
Takashi Iwai352f7f92012-12-19 12:52:06 +01005289 snd_hda_codec_cleanup_stream(codec,
5290 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005291 call_pcm_capture_hook(hinfo, codec, substream,
5292 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005293 return 0;
5294}
5295
Takashi Iwai352f7f92012-12-19 12:52:06 +01005296/*
5297 */
5298static const struct hda_pcm_stream pcm_analog_playback = {
5299 .substreams = 1,
5300 .channels_min = 2,
5301 .channels_max = 8,
5302 /* NID is set in build_pcms */
5303 .ops = {
5304 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005305 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005306 .prepare = playback_pcm_prepare,
5307 .cleanup = playback_pcm_cleanup
5308 },
5309};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310
Takashi Iwai352f7f92012-12-19 12:52:06 +01005311static const struct hda_pcm_stream pcm_analog_capture = {
5312 .substreams = 1,
5313 .channels_min = 2,
5314 .channels_max = 2,
5315 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005316 .ops = {
5317 .open = capture_pcm_open,
5318 .close = capture_pcm_close,
5319 .prepare = capture_pcm_prepare,
5320 .cleanup = capture_pcm_cleanup
5321 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005322};
5323
5324static const struct hda_pcm_stream pcm_analog_alt_playback = {
5325 .substreams = 1,
5326 .channels_min = 2,
5327 .channels_max = 2,
5328 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005329 .ops = {
5330 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005331 .close = alt_playback_pcm_close,
5332 .prepare = alt_playback_pcm_prepare,
5333 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005334 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005335};
5336
5337static const struct hda_pcm_stream pcm_analog_alt_capture = {
5338 .substreams = 2, /* can be overridden */
5339 .channels_min = 2,
5340 .channels_max = 2,
5341 /* NID is set in build_pcms */
5342 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005343 .open = alt_capture_pcm_open,
5344 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005345 .prepare = alt_capture_pcm_prepare,
5346 .cleanup = alt_capture_pcm_cleanup
5347 },
5348};
5349
5350static const struct hda_pcm_stream pcm_digital_playback = {
5351 .substreams = 1,
5352 .channels_min = 2,
5353 .channels_max = 2,
5354 /* NID is set in build_pcms */
5355 .ops = {
5356 .open = dig_playback_pcm_open,
5357 .close = dig_playback_pcm_close,
5358 .prepare = dig_playback_pcm_prepare,
5359 .cleanup = dig_playback_pcm_cleanup
5360 },
5361};
5362
5363static const struct hda_pcm_stream pcm_digital_capture = {
5364 .substreams = 1,
5365 .channels_min = 2,
5366 .channels_max = 2,
5367 /* NID is set in build_pcms */
5368};
5369
5370/* Used by build_pcms to flag that a PCM has no playback stream */
5371static const struct hda_pcm_stream pcm_null_stream = {
5372 .substreams = 0,
5373 .channels_min = 0,
5374 .channels_max = 0,
5375};
5376
5377/*
5378 * dynamic changing ADC PCM streams
5379 */
5380static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5381{
5382 struct hda_gen_spec *spec = codec->spec;
5383 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5384
5385 if (spec->cur_adc && spec->cur_adc != new_adc) {
5386 /* stream is running, let's swap the current ADC */
5387 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5388 spec->cur_adc = new_adc;
5389 snd_hda_codec_setup_stream(codec, new_adc,
5390 spec->cur_adc_stream_tag, 0,
5391 spec->cur_adc_format);
5392 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005394 return false;
5395}
5396
5397/* analog capture with dynamic dual-adc changes */
5398static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5399 struct hda_codec *codec,
5400 unsigned int stream_tag,
5401 unsigned int format,
5402 struct snd_pcm_substream *substream)
5403{
5404 struct hda_gen_spec *spec = codec->spec;
5405 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5406 spec->cur_adc_stream_tag = stream_tag;
5407 spec->cur_adc_format = format;
5408 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5409 return 0;
5410}
5411
5412static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5413 struct hda_codec *codec,
5414 struct snd_pcm_substream *substream)
5415{
5416 struct hda_gen_spec *spec = codec->spec;
5417 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5418 spec->cur_adc = 0;
5419 return 0;
5420}
5421
5422static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5423 .substreams = 1,
5424 .channels_min = 2,
5425 .channels_max = 2,
5426 .nid = 0, /* fill later */
5427 .ops = {
5428 .prepare = dyn_adc_capture_pcm_prepare,
5429 .cleanup = dyn_adc_capture_pcm_cleanup
5430 },
5431};
5432
Takashi Iwaif873e532012-12-20 16:58:39 +01005433static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5434 const char *chip_name)
5435{
5436 char *p;
5437
5438 if (*str)
5439 return;
5440 strlcpy(str, chip_name, len);
5441
5442 /* drop non-alnum chars after a space */
5443 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5444 if (!isalnum(p[1])) {
5445 *p = 0;
5446 break;
5447 }
5448 }
5449 strlcat(str, sfx, len);
5450}
5451
Takashi Iwaifb83b632015-03-16 23:34:34 +01005452/* copy PCM stream info from @default_str, and override non-NULL entries
5453 * from @spec_str and @nid
5454 */
5455static void setup_pcm_stream(struct hda_pcm_stream *str,
5456 const struct hda_pcm_stream *default_str,
5457 const struct hda_pcm_stream *spec_str,
5458 hda_nid_t nid)
5459{
5460 *str = *default_str;
5461 if (nid)
5462 str->nid = nid;
5463 if (spec_str) {
5464 if (spec_str->substreams)
5465 str->substreams = spec_str->substreams;
5466 if (spec_str->channels_min)
5467 str->channels_min = spec_str->channels_min;
5468 if (spec_str->channels_max)
5469 str->channels_max = spec_str->channels_max;
5470 if (spec_str->rates)
5471 str->rates = spec_str->rates;
5472 if (spec_str->formats)
5473 str->formats = spec_str->formats;
5474 if (spec_str->maxbps)
5475 str->maxbps = spec_str->maxbps;
5476 }
5477}
5478
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005479/**
5480 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5481 * @codec: the HDA codec
5482 *
5483 * Pass this to build_pcms patch_ops.
5484 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005485int snd_hda_gen_build_pcms(struct hda_codec *codec)
5486{
5487 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005488 struct hda_pcm *info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005489 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490
Takashi Iwai352f7f92012-12-19 12:52:06 +01005491 if (spec->no_analog)
5492 goto skip_analog;
5493
Takashi Iwaif873e532012-12-20 16:58:39 +01005494 fill_pcm_stream_name(spec->stream_name_analog,
5495 sizeof(spec->stream_name_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005496 " Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005497 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5498 if (!info)
5499 return -ENOMEM;
5500 spec->pcm_rec[0] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005501
5502 if (spec->multiout.num_dacs > 0) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005503 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5504 &pcm_analog_playback,
5505 spec->stream_analog_playback,
5506 spec->multiout.dac_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005507 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5508 spec->multiout.max_channels;
5509 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5510 spec->autocfg.line_outs == 2)
5511 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5512 snd_pcm_2_1_chmaps;
5513 }
5514 if (spec->num_adc_nids) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005515 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5516 (spec->dyn_adc_switch ?
5517 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5518 spec->stream_analog_capture,
5519 spec->adc_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005520 }
5521
Takashi Iwai352f7f92012-12-19 12:52:06 +01005522 skip_analog:
5523 /* SPDIF for stream index #1 */
5524 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005525 fill_pcm_stream_name(spec->stream_name_digital,
5526 sizeof(spec->stream_name_digital),
Takashi Iwai7639a062015-03-03 10:07:24 +01005527 " Digital", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005528 info = snd_hda_codec_pcm_new(codec, "%s",
5529 spec->stream_name_digital);
5530 if (!info)
5531 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005532 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005533 spec->pcm_rec[1] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005534 if (spec->dig_out_type)
5535 info->pcm_type = spec->dig_out_type;
5536 else
5537 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005538 if (spec->multiout.dig_out_nid)
5539 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5540 &pcm_digital_playback,
5541 spec->stream_digital_playback,
5542 spec->multiout.dig_out_nid);
5543 if (spec->dig_in_nid)
5544 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5545 &pcm_digital_capture,
5546 spec->stream_digital_capture,
5547 spec->dig_in_nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005548 }
5549
5550 if (spec->no_analog)
5551 return 0;
5552
5553 /* If the use of more than one ADC is requested for the current
5554 * model, configure a second analog capture-only PCM.
5555 */
5556 have_multi_adcs = (spec->num_adc_nids > 1) &&
5557 !spec->dyn_adc_switch && !spec->auto_mic;
5558 /* Additional Analaog capture for index #2 */
5559 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005560 fill_pcm_stream_name(spec->stream_name_alt_analog,
5561 sizeof(spec->stream_name_alt_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005562 " Alt Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005563 info = snd_hda_codec_pcm_new(codec, "%s",
5564 spec->stream_name_alt_analog);
5565 if (!info)
5566 return -ENOMEM;
5567 spec->pcm_rec[2] = info;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005568 if (spec->alt_dac_nid)
5569 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5570 &pcm_analog_alt_playback,
5571 spec->stream_analog_alt_playback,
5572 spec->alt_dac_nid);
5573 else
5574 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5575 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005576 if (have_multi_adcs) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005577 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5578 &pcm_analog_alt_capture,
5579 spec->stream_analog_alt_capture,
5580 spec->adc_nids[1]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005581 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5582 spec->num_adc_nids - 1;
5583 } else {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005584 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5585 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 }
5588
5589 return 0;
5590}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005591EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005592
5593
5594/*
5595 * Standard auto-parser initializations
5596 */
5597
Takashi Iwaid4156932013-01-07 10:08:02 +01005598/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005599static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005600{
5601 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005602 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005603
Takashi Iwai196c17662013-01-04 15:01:40 +01005604 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005605 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005606 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005607 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005608 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005609 snd_hda_activate_path(codec, path, path->active,
5610 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005611 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005612}
5613
5614/* initialize primary output paths */
5615static void init_multi_out(struct hda_codec *codec)
5616{
5617 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005618 int i;
5619
Takashi Iwaid4156932013-01-07 10:08:02 +01005620 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005621 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005622}
5623
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005624
Takashi Iwai2c12c302013-01-10 09:33:29 +01005625static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005626{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005627 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005628
Takashi Iwaid4156932013-01-07 10:08:02 +01005629 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005630 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005631}
5632
5633/* initialize hp and speaker paths */
5634static void init_extra_out(struct hda_codec *codec)
5635{
5636 struct hda_gen_spec *spec = codec->spec;
5637
5638 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005639 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005640 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5641 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005642 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005643}
5644
5645/* initialize multi-io paths */
5646static void init_multi_io(struct hda_codec *codec)
5647{
5648 struct hda_gen_spec *spec = codec->spec;
5649 int i;
5650
5651 for (i = 0; i < spec->multi_ios; i++) {
5652 hda_nid_t pin = spec->multi_io[i].pin;
5653 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005654 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005655 if (!path)
5656 continue;
5657 if (!spec->multi_io[i].ctl_in)
5658 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005659 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005660 snd_hda_activate_path(codec, path, path->active,
5661 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005662 }
5663}
5664
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005665static void init_aamix_paths(struct hda_codec *codec)
5666{
5667 struct hda_gen_spec *spec = codec->spec;
5668
5669 if (!spec->have_aamix_ctl)
5670 return;
5671 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5672 spec->aamix_out_paths[0],
5673 spec->autocfg.line_out_type);
5674 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5675 spec->aamix_out_paths[1],
5676 AUTO_PIN_HP_OUT);
5677 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5678 spec->aamix_out_paths[2],
5679 AUTO_PIN_SPEAKER_OUT);
5680}
5681
Takashi Iwai352f7f92012-12-19 12:52:06 +01005682/* set up input pins and loopback paths */
5683static void init_analog_input(struct hda_codec *codec)
5684{
5685 struct hda_gen_spec *spec = codec->spec;
5686 struct auto_pin_cfg *cfg = &spec->autocfg;
5687 int i;
5688
5689 for (i = 0; i < cfg->num_inputs; i++) {
5690 hda_nid_t nid = cfg->inputs[i].pin;
5691 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005692 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005693
5694 /* init loopback inputs */
5695 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005696 resume_path_from_idx(codec, spec->loopback_paths[i]);
5697 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005698 }
5699 }
5700}
5701
5702/* initialize ADC paths */
5703static void init_input_src(struct hda_codec *codec)
5704{
5705 struct hda_gen_spec *spec = codec->spec;
5706 struct hda_input_mux *imux = &spec->input_mux;
5707 struct nid_path *path;
5708 int i, c, nums;
5709
5710 if (spec->dyn_adc_switch)
5711 nums = 1;
5712 else
5713 nums = spec->num_adc_nids;
5714
5715 for (c = 0; c < nums; c++) {
5716 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005717 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005718 if (path) {
5719 bool active = path->active;
5720 if (i == spec->cur_mux[c])
5721 active = true;
5722 snd_hda_activate_path(codec, path, active, false);
5723 }
5724 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005725 if (spec->hp_mic)
5726 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005727 }
5728
Takashi Iwai352f7f92012-12-19 12:52:06 +01005729 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005730 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005731}
5732
5733/* set right pin controls for digital I/O */
5734static void init_digital(struct hda_codec *codec)
5735{
5736 struct hda_gen_spec *spec = codec->spec;
5737 int i;
5738 hda_nid_t pin;
5739
Takashi Iwaid4156932013-01-07 10:08:02 +01005740 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005741 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005742 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005743 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005744 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005745 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005746 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005747}
5748
Takashi Iwai973e4972012-12-20 15:16:09 +01005749/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5750 * invalid unsol tags by some reason
5751 */
5752static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5753{
5754 int i;
5755
5756 for (i = 0; i < codec->init_pins.used; i++) {
5757 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5758 hda_nid_t nid = pin->nid;
5759 if (is_jack_detectable(codec, nid) &&
5760 !snd_hda_jack_tbl_get(codec, nid))
5761 snd_hda_codec_update_cache(codec, nid, 0,
5762 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5763 }
5764}
5765
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005766/**
5767 * snd_hda_gen_init - initialize the generic spec
5768 * @codec: the HDA codec
5769 *
5770 * This can be put as patch_ops init function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005771 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005772int snd_hda_gen_init(struct hda_codec *codec)
5773{
5774 struct hda_gen_spec *spec = codec->spec;
5775
5776 if (spec->init_hook)
5777 spec->init_hook(codec);
5778
5779 snd_hda_apply_verbs(codec);
5780
5781 init_multi_out(codec);
5782 init_extra_out(codec);
5783 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005784 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005785 init_analog_input(codec);
5786 init_input_src(codec);
5787 init_digital(codec);
5788
Takashi Iwai973e4972012-12-20 15:16:09 +01005789 clear_unsol_on_unused_pins(codec);
5790
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01005791 sync_all_pin_power_ctls(codec);
5792
Takashi Iwai352f7f92012-12-19 12:52:06 +01005793 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005794 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005795
Takashi Iwaia551d912015-02-26 12:34:49 +01005796 regcache_sync(codec->core.regmap);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005797
Takashi Iwai352f7f92012-12-19 12:52:06 +01005798 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5799 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5800
5801 hda_call_check_power_status(codec, 0x01);
5802 return 0;
5803}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005804EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005805
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005806/**
5807 * snd_hda_gen_free - free the generic spec
5808 * @codec: the HDA codec
5809 *
5810 * This can be put as patch_ops free function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005811 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005812void snd_hda_gen_free(struct hda_codec *codec)
5813{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01005814 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005815 snd_hda_gen_spec_free(codec->spec);
5816 kfree(codec->spec);
5817 codec->spec = NULL;
5818}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005819EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005820
5821#ifdef CONFIG_PM
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005822/**
5823 * snd_hda_gen_check_power_status - check the loopback power save state
5824 * @codec: the HDA codec
5825 * @nid: NID to inspect
5826 *
5827 * This can be put as patch_ops check_power_status function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005828 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005829int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5830{
5831 struct hda_gen_spec *spec = codec->spec;
5832 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5833}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005834EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005835#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005836
5837
5838/*
5839 * the generic codec support
5840 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841
Takashi Iwai352f7f92012-12-19 12:52:06 +01005842static const struct hda_codec_ops generic_patch_ops = {
5843 .build_controls = snd_hda_gen_build_controls,
5844 .build_pcms = snd_hda_gen_build_pcms,
5845 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005846 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005847 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005848#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005849 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005850#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851};
5852
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005853/*
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005854 * snd_hda_parse_generic_codec - Generic codec parser
5855 * @codec: the HDA codec
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005856 */
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005857static int snd_hda_parse_generic_codec(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005859 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 int err;
5861
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005862 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005863 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005865 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005868 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5869 if (err < 0)
5870 return err;
5871
5872 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005873 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 goto error;
5875
5876 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 return 0;
5878
Takashi Iwai352f7f92012-12-19 12:52:06 +01005879error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005880 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 return err;
5882}
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005883
5884static const struct hda_codec_preset snd_hda_preset_generic[] = {
5885 { .id = HDA_CODEC_ID_GENERIC, .patch = snd_hda_parse_generic_codec },
5886 {} /* terminator */
5887};
5888
5889static struct hda_codec_driver generic_driver = {
5890 .preset = snd_hda_preset_generic,
5891};
5892
5893module_hda_codec_driver(generic_driver);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01005894
5895MODULE_LICENSE("GPL");
5896MODULE_DESCRIPTION("Generic HD-audio codec parser");