blob: ac0db1679f098ee4ec08c6770fe1f1374c8bf431 [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) {
674 if (dir == HDA_OUTPUT || path->idx[i] == idx)
675 return true;
676 break;
677 }
678 }
679 }
680 return false;
681}
682
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200683/* check whether the NID is referred by any active paths */
684#define is_active_nid_for_any(codec, nid) \
685 is_active_nid(codec, nid, HDA_OUTPUT, 0)
686
Takashi Iwai352f7f92012-12-19 12:52:06 +0100687/* get the default amp value for the target state */
688static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100689 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100690{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100691 unsigned int val = 0;
692
Takashi Iwai352f7f92012-12-19 12:52:06 +0100693 if (caps & AC_AMPCAP_NUM_STEPS) {
694 /* set to 0dB */
695 if (enable)
696 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
697 }
Takashi Iwaif69910d2013-08-08 09:32:37 +0200698 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100699 if (!enable)
700 val |= HDA_AMP_MUTE;
701 }
702 return val;
703}
704
Takashi Iwaicc261732015-03-16 10:18:08 +0100705/* is this a stereo widget or a stereo-to-mono mix? */
706static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
707{
708 unsigned int wcaps = get_wcaps(codec, nid);
709 hda_nid_t conn;
710
711 if (wcaps & AC_WCAP_STEREO)
712 return true;
713 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
714 return false;
715 if (snd_hda_get_num_conns(codec, nid) != 1)
716 return false;
717 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
718 return false;
719 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
720}
721
Takashi Iwai352f7f92012-12-19 12:52:06 +0100722/* initialize the amp value (only at the first time) */
723static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
724{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100725 unsigned int caps = query_amp_caps(codec, nid, dir);
726 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwaief403ed2015-03-12 08:30:11 +0100727
Takashi Iwaicc261732015-03-16 10:18:08 +0100728 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100729 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
730 else
731 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
732}
733
734/* update the amp, doing in stereo or mono depending on NID */
735static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
736 unsigned int mask, unsigned int val)
737{
Takashi Iwaicc261732015-03-16 10:18:08 +0100738 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100739 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
740 mask, val);
741 else
742 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
743 mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100744}
745
Takashi Iwai8999bf02013-01-18 11:01:33 +0100746/* calculate amp value mask we can modify;
747 * if the given amp is controlled by mixers, don't touch it
748 */
749static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
750 hda_nid_t nid, int dir, int idx,
751 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100752{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100753 unsigned int mask = 0xff;
754
Takashi Iwaif69910d2013-08-08 09:32:37 +0200755 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100756 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
757 mask &= ~0x80;
758 }
759 if (caps & AC_AMPCAP_NUM_STEPS) {
760 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
761 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
762 mask &= ~0x7f;
763 }
764 return mask;
765}
766
767static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
768 int idx, int idx_to_check, bool enable)
769{
770 unsigned int caps;
771 unsigned int mask, val;
772
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100773 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100774 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100775
776 caps = query_amp_caps(codec, nid, dir);
777 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
778 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
779 if (!mask)
780 return;
781
782 val &= mask;
Takashi Iwaief403ed2015-03-12 08:30:11 +0100783 update_amp(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100784}
785
786static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
787 int i, bool enable)
788{
789 hda_nid_t nid = path->path[i];
790 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100791 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100792}
793
794static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
795 int i, bool enable, bool add_aamix)
796{
797 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100798 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100799 int n, nums, idx;
800 int type;
801 hda_nid_t nid = path->path[i];
802
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100803 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100804 type = get_wcaps_type(get_wcaps(codec, nid));
805 if (type == AC_WID_PIN ||
806 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
807 nums = 1;
808 idx = 0;
809 } else
810 idx = path->idx[i];
811
812 for (n = 0; n < nums; n++)
813 init_amp(codec, nid, HDA_INPUT, n);
814
Takashi Iwai352f7f92012-12-19 12:52:06 +0100815 /* here is a little bit tricky in comparison with activate_amp_out();
816 * when aa-mixer is available, we need to enable the path as well
817 */
818 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100819 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100820 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100821 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823}
824
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100825/* sync power of each widget in the the given path */
826static hda_nid_t path_power_update(struct hda_codec *codec,
827 struct nid_path *path,
828 bool allow_powerdown)
829{
830 hda_nid_t nid, changed = 0;
831 int i, state;
832
833 for (i = 0; i < path->depth; i++) {
834 nid = path->path[i];
Takashi Iwai2206dc92015-04-09 10:18:31 +0200835 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
836 continue;
Takashi Iwai7639a062015-03-03 10:07:24 +0100837 if (nid == codec->core.afg)
Takashi Iwai5ccf8352015-03-18 09:23:10 +0100838 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100839 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
840 state = AC_PWRST_D0;
841 else
842 state = AC_PWRST_D3;
843 if (!snd_hda_check_power_state(codec, nid, state)) {
844 snd_hda_codec_write(codec, nid, 0,
845 AC_VERB_SET_POWER_STATE, state);
846 changed = nid;
Takashi Iwai48f4b3a2015-05-20 06:49:37 +0200847 /* all known codecs seem to be capable to handl
848 * widgets state even in D3, so far.
849 * if any new codecs need to restore the widget
850 * states after D0 transition, call the function
851 * below.
852 */
853#if 0 /* disabled */
Takashi Iwaid545a572015-04-04 12:17:28 +0200854 if (state == AC_PWRST_D0)
855 snd_hdac_regmap_sync_node(&codec->core, nid);
Takashi Iwai48f4b3a2015-05-20 06:49:37 +0200856#endif
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100857 }
858 }
859 return changed;
860}
861
862/* do sync with the last power state change */
863static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
864{
865 if (nid) {
866 msleep(10);
867 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
868 }
869}
870
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100871/**
872 * snd_hda_activate_path - activate or deactivate the given path
873 * @codec: the HDA codec
874 * @path: the path to activate/deactivate
875 * @enable: flag to activate or not
876 * @add_aamix: enable the input from aamix NID
877 *
878 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100880void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
881 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100883 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100884 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Takashi Iwai352f7f92012-12-19 12:52:06 +0100886 if (!enable)
887 path->active = false;
888
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100889 /* make sure the widget is powered up */
Takashi Iwai967b1302015-03-20 18:21:03 +0100890 if (enable && (spec->power_down_unused || codec->power_save_node))
891 path_power_update(codec, path, codec->power_save_node);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100892
Takashi Iwai352f7f92012-12-19 12:52:06 +0100893 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100894 hda_nid_t nid = path->path[i];
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100895
Takashi Iwai352f7f92012-12-19 12:52:06 +0100896 if (enable && path->multi[i])
Takashi Iwai8f0972d2014-01-27 16:26:16 +0100897 snd_hda_codec_update_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100898 AC_VERB_SET_CONNECT_SEL,
899 path->idx[i]);
900 if (has_amp_in(codec, path, i))
901 activate_amp_in(codec, path, i, enable, add_aamix);
902 if (has_amp_out(codec, path, i))
903 activate_amp_out(codec, path, i, enable);
904 }
905
906 if (enable)
907 path->active = true;
908}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100909EXPORT_SYMBOL_GPL(snd_hda_activate_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100910
Takashi Iwai55196ff2013-01-24 17:32:56 +0100911/* if the given path is inactive, put widgets into D3 (only if suitable) */
912static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
913{
914 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100915
Takashi Iwai967b1302015-03-20 18:21:03 +0100916 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
Takashi Iwai55196ff2013-01-24 17:32:56 +0100917 return;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100918 sync_power_state_change(codec, path_power_update(codec, path, true));
Takashi Iwai55196ff2013-01-24 17:32:56 +0100919}
920
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100921/* turn on/off EAPD on the given pin */
922static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
923{
924 struct hda_gen_spec *spec = codec->spec;
925 if (spec->own_eapd_ctl ||
926 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
927 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200928 if (spec->keep_eapd_on && !enable)
929 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100930 if (codec->inv_eapd)
931 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100932 snd_hda_codec_update_cache(codec, pin, 0,
933 AC_VERB_SET_EAPD_BTLENABLE,
934 enable ? 0x02 : 0x00);
935}
936
Takashi Iwai3e367f12013-01-23 17:07:23 +0100937/* re-initialize the path specified by the given path index */
938static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
939{
940 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
941 if (path)
942 snd_hda_activate_path(codec, path, path->active, false);
943}
944
Takashi Iwai352f7f92012-12-19 12:52:06 +0100945
946/*
947 * Helper functions for creating mixer ctl elements
948 */
949
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200950static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
951 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200952static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
953 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200954
Takashi Iwai352f7f92012-12-19 12:52:06 +0100955enum {
956 HDA_CTL_WIDGET_VOL,
957 HDA_CTL_WIDGET_MUTE,
958 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100959};
960static const struct snd_kcontrol_new control_templates[] = {
961 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200962 /* only the put callback is replaced for handling the special mute */
963 {
964 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
965 .subdevice = HDA_SUBDEV_AMP_FLAG,
966 .info = snd_hda_mixer_amp_switch_info,
967 .get = snd_hda_mixer_amp_switch_get,
968 .put = hda_gen_mixer_mute_put, /* replaced */
969 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
970 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200971 {
972 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
973 .info = snd_hda_mixer_amp_switch_info,
974 .get = snd_hda_mixer_bind_switch_get,
975 .put = hda_gen_bind_mute_put, /* replaced */
976 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
977 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100978};
979
980/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100981static struct snd_kcontrol_new *
982add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100983 int cidx, unsigned long val)
984{
985 struct snd_kcontrol_new *knew;
986
Takashi Iwai12c93df2012-12-19 14:38:33 +0100987 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100988 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100989 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100990 knew->index = cidx;
991 if (get_amp_nid_(val))
992 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
993 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100994 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100995}
996
997static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
998 const char *pfx, const char *dir,
999 const char *sfx, int cidx, unsigned long val)
1000{
Takashi Iwai975cc022013-06-28 11:56:49 +02001001 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01001002 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01001003 if (!add_control(spec, type, name, cidx, val))
1004 return -ENOMEM;
1005 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001006}
1007
1008#define add_pb_vol_ctrl(spec, type, pfx, val) \
1009 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1010#define add_pb_sw_ctrl(spec, type, pfx, val) \
1011 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1012#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1013 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1014#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1015 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1016
1017static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1018 unsigned int chs, struct nid_path *path)
1019{
1020 unsigned int val;
1021 if (!path)
1022 return 0;
1023 val = path->ctls[NID_PATH_VOL_CTL];
1024 if (!val)
1025 return 0;
1026 val = amp_val_replace_channels(val, chs);
1027 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1028}
1029
1030/* return the channel bits suitable for the given path->ctls[] */
1031static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1032 int type)
1033{
1034 int chs = 1; /* mono (left only) */
1035 if (path) {
1036 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1037 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1038 chs = 3; /* stereo */
1039 }
1040 return chs;
1041}
1042
1043static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1044 struct nid_path *path)
1045{
1046 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1047 return add_vol_ctl(codec, pfx, cidx, chs, path);
1048}
1049
1050/* create a mute-switch for the given mixer widget;
1051 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1052 */
1053static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1054 unsigned int chs, struct nid_path *path)
1055{
1056 unsigned int val;
1057 int type = HDA_CTL_WIDGET_MUTE;
1058
1059 if (!path)
1060 return 0;
1061 val = path->ctls[NID_PATH_MUTE_CTL];
1062 if (!val)
1063 return 0;
1064 val = amp_val_replace_channels(val, chs);
1065 if (get_amp_direction_(val) == HDA_INPUT) {
1066 hda_nid_t nid = get_amp_nid_(val);
1067 int nums = snd_hda_get_num_conns(codec, nid);
1068 if (nums > 1) {
1069 type = HDA_CTL_BIND_MUTE;
1070 val |= nums << 19;
1071 }
1072 }
1073 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1074}
1075
1076static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1077 int cidx, struct nid_path *path)
1078{
1079 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1080 return add_sw_ctl(codec, pfx, cidx, chs, path);
1081}
1082
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001083/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001084static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1085 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001086{
1087 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1088 struct hda_gen_spec *spec = codec->spec;
1089
1090 if (spec->auto_mute_via_amp) {
1091 hda_nid_t nid = get_amp_nid(kcontrol);
1092 bool enabled = !((spec->mute_bits >> nid) & 1);
1093 ucontrol->value.integer.value[0] &= enabled;
1094 ucontrol->value.integer.value[1] &= enabled;
1095 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001096}
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001097
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001098static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1099 struct snd_ctl_elem_value *ucontrol)
1100{
1101 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001102 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1103}
1104
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001105static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1106 struct snd_ctl_elem_value *ucontrol)
1107{
1108 sync_auto_mute_bits(kcontrol, ucontrol);
1109 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1110}
1111
Takashi Iwai247d85e2013-01-17 16:18:11 +01001112/* any ctl assigned to the path with the given index? */
1113static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1114{
1115 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1116 return path && path->ctls[ctl_type];
1117}
1118
Takashi Iwai352f7f92012-12-19 12:52:06 +01001119static const char * const channel_name[4] = {
1120 "Front", "Surround", "CLFE", "Side"
1121};
1122
1123/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +01001124static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1125 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001126{
Takashi Iwai247d85e2013-01-17 16:18:11 +01001127 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001128 struct auto_pin_cfg *cfg = &spec->autocfg;
1129
1130 *index = 0;
1131 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001132 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001133 return spec->vmaster_mute.hook ? "PCM" : "Master";
1134
1135 /* if there is really a single DAC used in the whole output paths,
1136 * use it master (or "PCM" if a vmaster hook is present)
1137 */
1138 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1139 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1140 return spec->vmaster_mute.hook ? "PCM" : "Master";
1141
Takashi Iwai247d85e2013-01-17 16:18:11 +01001142 /* multi-io channels */
1143 if (ch >= cfg->line_outs)
1144 return channel_name[ch];
1145
Takashi Iwai352f7f92012-12-19 12:52:06 +01001146 switch (cfg->line_out_type) {
1147 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001148 /* if the primary channel vol/mute is shared with HP volume,
1149 * don't name it as Speaker
1150 */
1151 if (!ch && cfg->hp_outs &&
1152 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1153 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001154 if (cfg->line_outs == 1)
1155 return "Speaker";
1156 if (cfg->line_outs == 2)
1157 return ch ? "Bass Speaker" : "Speaker";
1158 break;
1159 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001160 /* if the primary channel vol/mute is shared with spk volume,
1161 * don't name it as Headphone
1162 */
1163 if (!ch && cfg->speaker_outs &&
1164 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1165 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001166 /* for multi-io case, only the primary out */
1167 if (ch && spec->multi_ios)
1168 break;
1169 *index = ch;
1170 return "Headphone";
David Henningsson03ad6a82014-10-16 15:33:45 +02001171 case AUTO_PIN_LINE_OUT:
1172 /* This deals with the case where we have two DACs and
1173 * one LO, one HP and one Speaker */
1174 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1175 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1176 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1177 if (hp_lo_shared && spk_lo_shared)
1178 return spec->vmaster_mute.hook ? "PCM" : "Master";
1179 if (hp_lo_shared)
1180 return "Headphone+LO";
1181 if (spk_lo_shared)
1182 return "Speaker+LO";
1183 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001184 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001185
1186 /* for a single channel output, we don't have to name the channel */
1187 if (cfg->line_outs == 1 && !spec->multi_ios)
David Henningsson3abb4f42014-10-16 15:33:46 +02001188 return "Line Out";
Takashi Iwai247d85e2013-01-17 16:18:11 +01001189
Takashi Iwai352f7f92012-12-19 12:52:06 +01001190 if (ch >= ARRAY_SIZE(channel_name)) {
1191 snd_BUG();
1192 return "PCM";
1193 }
1194
1195 return channel_name[ch];
1196}
1197
1198/*
1199 * Parse output paths
1200 */
1201
1202/* badness definition */
1203enum {
1204 /* No primary DAC is found for the main output */
1205 BAD_NO_PRIMARY_DAC = 0x10000,
1206 /* No DAC is found for the extra output */
1207 BAD_NO_DAC = 0x4000,
1208 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001209 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001210 /* No individual DAC for extra output */
1211 BAD_NO_EXTRA_DAC = 0x102,
1212 /* No individual DAC for extra surrounds */
1213 BAD_NO_EXTRA_SURR_DAC = 0x101,
1214 /* Primary DAC shared with main surrounds */
1215 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001216 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001217 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001218 /* Primary DAC shared with main CLFE */
1219 BAD_SHARED_CLFE = 0x10,
1220 /* Primary DAC shared with extra surrounds */
1221 BAD_SHARED_EXTRA_SURROUND = 0x10,
1222 /* Volume widget is shared */
1223 BAD_SHARED_VOL = 0x10,
1224};
1225
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001226/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001227 * volume and mute controls, and assign the values to ctls[].
1228 *
1229 * When no appropriate widget is found in the path, the badness value
1230 * is incremented depending on the situation. The function returns the
1231 * total badness for both volume and mute controls.
1232 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001233static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001234{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001235 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001236 hda_nid_t nid;
1237 unsigned int val;
1238 int badness = 0;
1239
1240 if (!path)
1241 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001242
1243 if (path->ctls[NID_PATH_VOL_CTL] ||
1244 path->ctls[NID_PATH_MUTE_CTL])
1245 return 0; /* already evaluated */
1246
Takashi Iwai352f7f92012-12-19 12:52:06 +01001247 nid = look_for_out_vol_nid(codec, path);
1248 if (nid) {
1249 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001250 if (spec->dac_min_mute)
1251 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001252 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1253 badness += BAD_SHARED_VOL;
1254 else
1255 path->ctls[NID_PATH_VOL_CTL] = val;
1256 } else
1257 badness += BAD_SHARED_VOL;
1258 nid = look_for_out_mute_nid(codec, path);
1259 if (nid) {
1260 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1261 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1262 nid_has_mute(codec, nid, HDA_OUTPUT))
1263 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1264 else
1265 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1266 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1267 badness += BAD_SHARED_VOL;
1268 else
1269 path->ctls[NID_PATH_MUTE_CTL] = val;
1270 } else
1271 badness += BAD_SHARED_VOL;
1272 return badness;
1273}
1274
Takashi Iwai98bd1112013-03-22 14:53:50 +01001275const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001276 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1277 .no_dac = BAD_NO_DAC,
1278 .shared_primary = BAD_NO_PRIMARY_DAC,
1279 .shared_surr = BAD_SHARED_SURROUND,
1280 .shared_clfe = BAD_SHARED_CLFE,
1281 .shared_surr_main = BAD_SHARED_SURROUND,
1282};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001283EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001284
Takashi Iwai98bd1112013-03-22 14:53:50 +01001285const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001286 .no_primary_dac = BAD_NO_DAC,
1287 .no_dac = BAD_NO_DAC,
1288 .shared_primary = BAD_NO_EXTRA_DAC,
1289 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1290 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1291 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1292};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001293EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001294
Takashi Iwai7385df62013-01-07 09:50:52 +01001295/* get the DAC of the primary output corresponding to the given array index */
1296static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1297{
1298 struct hda_gen_spec *spec = codec->spec;
1299 struct auto_pin_cfg *cfg = &spec->autocfg;
1300
1301 if (cfg->line_outs > idx)
1302 return spec->private_dac_nids[idx];
1303 idx -= cfg->line_outs;
1304 if (spec->multi_ios > idx)
1305 return spec->multi_io[idx].dac;
1306 return 0;
1307}
1308
1309/* return the DAC if it's reachable, otherwise zero */
1310static inline hda_nid_t try_dac(struct hda_codec *codec,
1311 hda_nid_t dac, hda_nid_t pin)
1312{
1313 return is_reachable_path(codec, dac, pin) ? dac : 0;
1314}
1315
Takashi Iwai352f7f92012-12-19 12:52:06 +01001316/* try to assign DACs to pins and return the resultant badness */
1317static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1318 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001319 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001320 const struct badness_table *bad)
1321{
1322 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001323 int i, j;
1324 int badness = 0;
1325 hda_nid_t dac;
1326
1327 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return 0;
1329
Takashi Iwai352f7f92012-12-19 12:52:06 +01001330 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001331 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001332 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001333
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001334 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1335 if (path) {
1336 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001337 continue;
1338 }
1339
Takashi Iwai36907392013-12-10 17:29:26 +01001340 dacs[i] = get_preferred_dac(codec, pin);
1341 if (dacs[i]) {
1342 if (is_dac_already_used(codec, dacs[i]))
1343 badness += bad->shared_primary;
1344 }
1345
1346 if (!dacs[i])
1347 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001348 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001349 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001350 for (j = 1; j < num_outs; j++) {
1351 if (is_reachable_path(codec, dacs[j], pin)) {
1352 dacs[0] = dacs[j];
1353 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001354 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001355 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001356 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
1358 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001359 }
1360 dac = dacs[i];
1361 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001362 if (num_outs > 2)
1363 dac = try_dac(codec, get_primary_out(codec, i), pin);
1364 if (!dac)
1365 dac = try_dac(codec, dacs[0], pin);
1366 if (!dac)
1367 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001368 if (dac) {
1369 if (!i)
1370 badness += bad->shared_primary;
1371 else if (i == 1)
1372 badness += bad->shared_surr;
1373 else
1374 badness += bad->shared_clfe;
1375 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1376 dac = spec->private_dac_nids[0];
1377 badness += bad->shared_surr_main;
1378 } else if (!i)
1379 badness += bad->no_primary_dac;
1380 else
1381 badness += bad->no_dac;
1382 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001383 if (!dac)
1384 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001385 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001386 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001387 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001388 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001389 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001390 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001391 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001392 badness += bad->no_dac;
1393 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001394 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001395 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001396 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001397 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001398 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001399 }
1400
1401 return badness;
1402}
1403
1404/* return NID if the given pin has only a single connection to a certain DAC */
1405static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1406{
1407 struct hda_gen_spec *spec = codec->spec;
1408 int i;
1409 hda_nid_t nid_found = 0;
1410
1411 for (i = 0; i < spec->num_all_dacs; i++) {
1412 hda_nid_t nid = spec->all_dacs[i];
1413 if (!nid || is_dac_already_used(codec, nid))
1414 continue;
1415 if (is_reachable_path(codec, nid, pin)) {
1416 if (nid_found)
1417 return 0;
1418 nid_found = nid;
1419 }
1420 }
1421 return nid_found;
1422}
1423
1424/* check whether the given pin can be a multi-io pin */
1425static bool can_be_multiio_pin(struct hda_codec *codec,
1426 unsigned int location, hda_nid_t nid)
1427{
1428 unsigned int defcfg, caps;
1429
1430 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1431 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1432 return false;
1433 if (location && get_defcfg_location(defcfg) != location)
1434 return false;
1435 caps = snd_hda_query_pin_caps(codec, nid);
1436 if (!(caps & AC_PINCAP_OUT))
1437 return false;
1438 return true;
1439}
1440
Takashi Iwaie22aab72013-01-04 14:50:04 +01001441/* count the number of input pins that are capable to be multi-io */
1442static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1443{
1444 struct hda_gen_spec *spec = codec->spec;
1445 struct auto_pin_cfg *cfg = &spec->autocfg;
1446 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1447 unsigned int location = get_defcfg_location(defcfg);
1448 int type, i;
1449 int num_pins = 0;
1450
1451 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1452 for (i = 0; i < cfg->num_inputs; i++) {
1453 if (cfg->inputs[i].type != type)
1454 continue;
1455 if (can_be_multiio_pin(codec, location,
1456 cfg->inputs[i].pin))
1457 num_pins++;
1458 }
1459 }
1460 return num_pins;
1461}
1462
Takashi Iwai352f7f92012-12-19 12:52:06 +01001463/*
1464 * multi-io helper
1465 *
1466 * When hardwired is set, try to fill ony hardwired pins, and returns
1467 * zero if any pins are filled, non-zero if nothing found.
1468 * When hardwired is off, try to fill possible input pins, and returns
1469 * the badness value.
1470 */
1471static int fill_multi_ios(struct hda_codec *codec,
1472 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001473 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001474{
1475 struct hda_gen_spec *spec = codec->spec;
1476 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001477 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001478 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1479 unsigned int location = get_defcfg_location(defcfg);
1480 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001481 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001482
1483 old_pins = spec->multi_ios;
1484 if (old_pins >= 2)
1485 goto end_fill;
1486
Takashi Iwaie22aab72013-01-04 14:50:04 +01001487 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001488 if (num_pins < 2)
1489 goto end_fill;
1490
Takashi Iwai352f7f92012-12-19 12:52:06 +01001491 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1492 for (i = 0; i < cfg->num_inputs; i++) {
1493 hda_nid_t nid = cfg->inputs[i].pin;
1494 hda_nid_t dac = 0;
1495
1496 if (cfg->inputs[i].type != type)
1497 continue;
1498 if (!can_be_multiio_pin(codec, location, nid))
1499 continue;
1500 for (j = 0; j < spec->multi_ios; j++) {
1501 if (nid == spec->multi_io[j].pin)
1502 break;
1503 }
1504 if (j < spec->multi_ios)
1505 continue;
1506
Takashi Iwai352f7f92012-12-19 12:52:06 +01001507 if (hardwired)
1508 dac = get_dac_if_single(codec, nid);
1509 else if (!dac)
1510 dac = look_for_dac(codec, nid, false);
1511 if (!dac) {
1512 badness++;
1513 continue;
1514 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001515 path = snd_hda_add_new_path(codec, dac, nid,
1516 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001517 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001518 badness++;
1519 continue;
1520 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001521 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001522 spec->multi_io[spec->multi_ios].pin = nid;
1523 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001524 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1525 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001526 spec->multi_ios++;
1527 if (spec->multi_ios >= 2)
1528 break;
1529 }
1530 }
1531 end_fill:
1532 if (badness)
1533 badness = BAD_MULTI_IO;
1534 if (old_pins == spec->multi_ios) {
1535 if (hardwired)
1536 return 1; /* nothing found */
1537 else
1538 return badness; /* no badness if nothing found */
1539 }
1540 if (!hardwired && spec->multi_ios < 2) {
1541 /* cancel newly assigned paths */
1542 spec->paths.used -= spec->multi_ios - old_pins;
1543 spec->multi_ios = old_pins;
1544 return badness;
1545 }
1546
1547 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001548 for (i = old_pins; i < spec->multi_ios; i++) {
1549 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1550 badness += assign_out_path_ctls(codec, path);
1551 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001552
1553 return badness;
1554}
1555
1556/* map DACs for all pins in the list if they are single connections */
1557static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001558 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001559{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001560 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001561 int i;
1562 bool found = false;
1563 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001564 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001565 hda_nid_t dac;
1566 if (dacs[i])
1567 continue;
1568 dac = get_dac_if_single(codec, pins[i]);
1569 if (!dac)
1570 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001571 path = snd_hda_add_new_path(codec, dac, pins[i],
1572 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001573 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001574 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001575 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001576 dacs[i] = dac;
1577 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001578 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001579 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001580 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001581 }
1582 }
1583 return found;
1584}
1585
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001586/* create a new path including aamix if available, and return its index */
1587static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1588{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001589 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001590 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001591 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001592
1593 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001594 if (!path || !path->depth ||
1595 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001596 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001597 path_dac = path->path[0];
1598 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001599 pin = path->path[path->depth - 1];
1600 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1601 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001602 if (dac != path_dac)
1603 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001604 else if (spec->multiout.hp_out_nid[0])
1605 dac = spec->multiout.hp_out_nid[0];
1606 else if (spec->multiout.extra_out_nid[0])
1607 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001608 else
1609 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001610 if (dac)
1611 path = snd_hda_add_new_path(codec, dac, pin,
1612 spec->mixer_nid);
1613 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001614 if (!path)
1615 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001616 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001617 path->active = false; /* unused as default */
Takashi Iwai6b275b12015-03-20 18:11:05 +01001618 path->pin_fixed = true; /* static route */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001619 return snd_hda_get_path_idx(codec, path);
1620}
1621
Takashi Iwai55a63d42013-03-21 17:20:12 +01001622/* check whether the independent HP is available with the current config */
1623static bool indep_hp_possible(struct hda_codec *codec)
1624{
1625 struct hda_gen_spec *spec = codec->spec;
1626 struct auto_pin_cfg *cfg = &spec->autocfg;
1627 struct nid_path *path;
1628 int i, idx;
1629
1630 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1631 idx = spec->out_paths[0];
1632 else
1633 idx = spec->hp_paths[0];
1634 path = snd_hda_get_path_from_idx(codec, idx);
1635 if (!path)
1636 return false;
1637
1638 /* assume no path conflicts unless aamix is involved */
1639 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1640 return true;
1641
1642 /* check whether output paths contain aamix */
1643 for (i = 0; i < cfg->line_outs; i++) {
1644 if (spec->out_paths[i] == idx)
1645 break;
1646 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1647 if (path && is_nid_contained(path, spec->mixer_nid))
1648 return false;
1649 }
1650 for (i = 0; i < cfg->speaker_outs; i++) {
1651 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1652 if (path && is_nid_contained(path, spec->mixer_nid))
1653 return false;
1654 }
1655
1656 return true;
1657}
1658
Takashi Iwaia07a9492013-01-07 16:44:06 +01001659/* fill the empty entries in the dac array for speaker/hp with the
1660 * shared dac pointed by the paths
1661 */
1662static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1663 hda_nid_t *dacs, int *path_idx)
1664{
1665 struct nid_path *path;
1666 int i;
1667
1668 for (i = 0; i < num_outs; i++) {
1669 if (dacs[i])
1670 continue;
1671 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1672 if (!path)
1673 continue;
1674 dacs[i] = path->path[0];
1675 }
1676}
1677
Takashi Iwai352f7f92012-12-19 12:52:06 +01001678/* fill in the dac_nids table from the parsed pin configuration */
1679static int fill_and_eval_dacs(struct hda_codec *codec,
1680 bool fill_hardwired,
1681 bool fill_mio_first)
1682{
1683 struct hda_gen_spec *spec = codec->spec;
1684 struct auto_pin_cfg *cfg = &spec->autocfg;
1685 int i, err, badness;
1686
1687 /* set num_dacs once to full for look_for_dac() */
1688 spec->multiout.num_dacs = cfg->line_outs;
1689 spec->multiout.dac_nids = spec->private_dac_nids;
1690 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1691 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1692 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1693 spec->multi_ios = 0;
1694 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001695
1696 /* clear path indices */
1697 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1698 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1699 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1700 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1701 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001702 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001703 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1704 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1705
Takashi Iwai352f7f92012-12-19 12:52:06 +01001706 badness = 0;
1707
1708 /* fill hard-wired DACs first */
1709 if (fill_hardwired) {
1710 bool mapped;
1711 do {
1712 mapped = map_singles(codec, cfg->line_outs,
1713 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001714 spec->private_dac_nids,
1715 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001716 mapped |= map_singles(codec, cfg->hp_outs,
1717 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001718 spec->multiout.hp_out_nid,
1719 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001720 mapped |= map_singles(codec, cfg->speaker_outs,
1721 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001722 spec->multiout.extra_out_nid,
1723 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001724 if (!spec->no_multi_io &&
1725 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001726 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001727 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001728 if (!err)
1729 mapped = true;
1730 }
1731 } while (mapped);
1732 }
1733
1734 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001735 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001736 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001737
Takashi Iwaida96fb52013-07-29 16:54:36 +02001738 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001739 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1740 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001741 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001742 if (err < 0)
1743 return err;
1744 /* we don't count badness at this stage yet */
1745 }
1746
1747 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1748 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1749 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001750 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001751 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001752 if (err < 0)
1753 return err;
1754 badness += err;
1755 }
1756 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1757 err = try_assign_dacs(codec, cfg->speaker_outs,
1758 cfg->speaker_pins,
1759 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001760 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001761 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001762 if (err < 0)
1763 return err;
1764 badness += err;
1765 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001766 if (!spec->no_multi_io &&
1767 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001768 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001769 if (err < 0)
1770 return err;
1771 badness += err;
1772 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001773
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001774 if (spec->mixer_nid) {
1775 spec->aamix_out_paths[0] =
1776 check_aamix_out_path(codec, spec->out_paths[0]);
1777 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1778 spec->aamix_out_paths[1] =
1779 check_aamix_out_path(codec, spec->hp_paths[0]);
1780 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1781 spec->aamix_out_paths[2] =
1782 check_aamix_out_path(codec, spec->speaker_paths[0]);
1783 }
1784
Takashi Iwaida96fb52013-07-29 16:54:36 +02001785 if (!spec->no_multi_io &&
1786 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001787 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1788 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001789
Takashi Iwaia07a9492013-01-07 16:44:06 +01001790 /* re-count num_dacs and squash invalid entries */
1791 spec->multiout.num_dacs = 0;
1792 for (i = 0; i < cfg->line_outs; i++) {
1793 if (spec->private_dac_nids[i])
1794 spec->multiout.num_dacs++;
1795 else {
1796 memmove(spec->private_dac_nids + i,
1797 spec->private_dac_nids + i + 1,
1798 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1799 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1800 }
1801 }
1802
1803 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001804 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001805
Takashi Iwai352f7f92012-12-19 12:52:06 +01001806 if (spec->multi_ios == 2) {
1807 for (i = 0; i < 2; i++)
1808 spec->private_dac_nids[spec->multiout.num_dacs++] =
1809 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001810 } else if (spec->multi_ios) {
1811 spec->multi_ios = 0;
1812 badness += BAD_MULTI_IO;
1813 }
1814
Takashi Iwai55a63d42013-03-21 17:20:12 +01001815 if (spec->indep_hp && !indep_hp_possible(codec))
1816 badness += BAD_NO_INDEP_HP;
1817
Takashi Iwaia07a9492013-01-07 16:44:06 +01001818 /* re-fill the shared DAC for speaker / headphone */
1819 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1820 refill_shared_dacs(codec, cfg->hp_outs,
1821 spec->multiout.hp_out_nid,
1822 spec->hp_paths);
1823 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1824 refill_shared_dacs(codec, cfg->speaker_outs,
1825 spec->multiout.extra_out_nid,
1826 spec->speaker_paths);
1827
Takashi Iwai352f7f92012-12-19 12:52:06 +01001828 return badness;
1829}
1830
1831#define DEBUG_BADNESS
1832
1833#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001834#define debug_badness(fmt, ...) \
1835 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001836#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001837#define debug_badness(fmt, ...) \
1838 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001839#endif
1840
Takashi Iwaia7694092013-01-21 10:43:18 +01001841#ifdef DEBUG_BADNESS
1842static inline void print_nid_path_idx(struct hda_codec *codec,
1843 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001844{
Takashi Iwaia7694092013-01-21 10:43:18 +01001845 struct nid_path *path;
1846
1847 path = snd_hda_get_path_from_idx(codec, idx);
1848 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001849 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001850}
1851
1852static void debug_show_configs(struct hda_codec *codec,
1853 struct auto_pin_cfg *cfg)
1854{
1855 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001856 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001857 int i;
1858
1859 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001860 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001861 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001862 spec->multiout.dac_nids[0],
1863 spec->multiout.dac_nids[1],
1864 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001865 spec->multiout.dac_nids[3],
1866 lo_type[cfg->line_out_type]);
1867 for (i = 0; i < cfg->line_outs; i++)
1868 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001869 if (spec->multi_ios > 0)
1870 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1871 spec->multi_ios,
1872 spec->multi_io[0].pin, spec->multi_io[1].pin,
1873 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001874 for (i = 0; i < spec->multi_ios; i++)
1875 print_nid_path_idx(codec, " mio",
1876 spec->out_paths[cfg->line_outs + i]);
1877 if (cfg->hp_outs)
1878 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001879 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001880 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001881 spec->multiout.hp_out_nid[0],
1882 spec->multiout.hp_out_nid[1],
1883 spec->multiout.hp_out_nid[2],
1884 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001885 for (i = 0; i < cfg->hp_outs; i++)
1886 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1887 if (cfg->speaker_outs)
1888 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001889 cfg->speaker_pins[0], cfg->speaker_pins[1],
1890 cfg->speaker_pins[2], cfg->speaker_pins[3],
1891 spec->multiout.extra_out_nid[0],
1892 spec->multiout.extra_out_nid[1],
1893 spec->multiout.extra_out_nid[2],
1894 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001895 for (i = 0; i < cfg->speaker_outs; i++)
1896 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1897 for (i = 0; i < 3; i++)
1898 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001899}
Takashi Iwaia7694092013-01-21 10:43:18 +01001900#else
1901#define debug_show_configs(codec, cfg) /* NOP */
1902#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001903
1904/* find all available DACs of the codec */
1905static void fill_all_dac_nids(struct hda_codec *codec)
1906{
1907 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001908 hda_nid_t nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001909
1910 spec->num_all_dacs = 0;
1911 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
Takashi Iwai7639a062015-03-03 10:07:24 +01001912 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001913 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1914 continue;
1915 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001916 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001917 break;
1918 }
1919 spec->all_dacs[spec->num_all_dacs++] = nid;
1920 }
1921}
1922
1923static int parse_output_paths(struct hda_codec *codec)
1924{
1925 struct hda_gen_spec *spec = codec->spec;
1926 struct auto_pin_cfg *cfg = &spec->autocfg;
1927 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001928 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001929 int best_badness = INT_MAX;
1930 int badness;
1931 bool fill_hardwired = true, fill_mio_first = true;
1932 bool best_wired = true, best_mio = true;
1933 bool hp_spk_swapped = false;
1934
Takashi Iwai352f7f92012-12-19 12:52:06 +01001935 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1936 if (!best_cfg)
1937 return -ENOMEM;
1938 *best_cfg = *cfg;
1939
1940 for (;;) {
1941 badness = fill_and_eval_dacs(codec, fill_hardwired,
1942 fill_mio_first);
1943 if (badness < 0) {
1944 kfree(best_cfg);
1945 return badness;
1946 }
1947 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1948 cfg->line_out_type, fill_hardwired, fill_mio_first,
1949 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001950 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001951 if (badness < best_badness) {
1952 best_badness = badness;
1953 *best_cfg = *cfg;
1954 best_wired = fill_hardwired;
1955 best_mio = fill_mio_first;
1956 }
1957 if (!badness)
1958 break;
1959 fill_mio_first = !fill_mio_first;
1960 if (!fill_mio_first)
1961 continue;
1962 fill_hardwired = !fill_hardwired;
1963 if (!fill_hardwired)
1964 continue;
1965 if (hp_spk_swapped)
1966 break;
1967 hp_spk_swapped = true;
1968 if (cfg->speaker_outs > 0 &&
1969 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1970 cfg->hp_outs = cfg->line_outs;
1971 memcpy(cfg->hp_pins, cfg->line_out_pins,
1972 sizeof(cfg->hp_pins));
1973 cfg->line_outs = cfg->speaker_outs;
1974 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1975 sizeof(cfg->speaker_pins));
1976 cfg->speaker_outs = 0;
1977 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1978 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1979 fill_hardwired = true;
1980 continue;
1981 }
1982 if (cfg->hp_outs > 0 &&
1983 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1984 cfg->speaker_outs = cfg->line_outs;
1985 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1986 sizeof(cfg->speaker_pins));
1987 cfg->line_outs = cfg->hp_outs;
1988 memcpy(cfg->line_out_pins, cfg->hp_pins,
1989 sizeof(cfg->hp_pins));
1990 cfg->hp_outs = 0;
1991 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1992 cfg->line_out_type = AUTO_PIN_HP_OUT;
1993 fill_hardwired = true;
1994 continue;
1995 }
1996 break;
1997 }
1998
1999 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002000 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01002001 *cfg = *best_cfg;
2002 fill_and_eval_dacs(codec, best_wired, best_mio);
2003 }
2004 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2005 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01002006 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002007
2008 if (cfg->line_out_pins[0]) {
2009 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01002010 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002011 if (path)
2012 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002013 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01002014 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2015 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002016 if (spec->dac_min_mute)
2017 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2018 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002019 }
2020
Takashi Iwai9314a582013-01-21 10:49:05 +01002021 /* set initial pinctl targets */
2022 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2023 val = PIN_HP;
2024 else
2025 val = PIN_OUT;
2026 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2027 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2028 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2029 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2030 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2031 set_pin_targets(codec, cfg->speaker_outs,
2032 cfg->speaker_pins, val);
2033 }
2034
Takashi Iwai55a63d42013-03-21 17:20:12 +01002035 /* clear indep_hp flag if not available */
2036 if (spec->indep_hp && !indep_hp_possible(codec))
2037 spec->indep_hp = 0;
2038
Takashi Iwai352f7f92012-12-19 12:52:06 +01002039 kfree(best_cfg);
2040 return 0;
2041}
2042
2043/* add playback controls from the parsed DAC table */
2044static int create_multi_out_ctls(struct hda_codec *codec,
2045 const struct auto_pin_cfg *cfg)
2046{
2047 struct hda_gen_spec *spec = codec->spec;
2048 int i, err, noutputs;
2049
2050 noutputs = cfg->line_outs;
2051 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2052 noutputs += spec->multi_ios;
2053
2054 for (i = 0; i < noutputs; i++) {
2055 const char *name;
2056 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002057 struct nid_path *path;
2058
Takashi Iwai196c17662013-01-04 15:01:40 +01002059 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002060 if (!path)
2061 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002062
2063 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002064 if (!name || !strcmp(name, "CLFE")) {
2065 /* Center/LFE */
2066 err = add_vol_ctl(codec, "Center", 0, 1, path);
2067 if (err < 0)
2068 return err;
2069 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2070 if (err < 0)
2071 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002072 } else {
2073 err = add_stereo_vol(codec, name, index, path);
2074 if (err < 0)
2075 return err;
2076 }
2077
2078 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2079 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002080 err = add_sw_ctl(codec, "Center", 0, 1, path);
2081 if (err < 0)
2082 return err;
2083 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2084 if (err < 0)
2085 return err;
2086 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002087 err = add_stereo_sw(codec, name, index, path);
2088 if (err < 0)
2089 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
2091 }
2092 return 0;
2093}
2094
Takashi Iwaic2c80382013-01-07 10:33:57 +01002095static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01002096 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002098 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 int err;
2100
Takashi Iwai196c17662013-01-04 15:01:40 +01002101 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002102 if (!path)
2103 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01002104 err = add_stereo_vol(codec, pfx, cidx, path);
2105 if (err < 0)
2106 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002107 err = add_stereo_sw(codec, pfx, cidx, path);
2108 if (err < 0)
2109 return err;
2110 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
Takashi Iwai352f7f92012-12-19 12:52:06 +01002113/* add playback controls for speaker and HP outputs */
2114static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01002115 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Takashi Iwaic2c80382013-01-07 10:33:57 +01002117 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002118
2119 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002120 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02002121 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01002122 int err, idx = 0;
2123
2124 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2125 name = "Bass Speaker";
2126 else if (num_pins >= 3) {
2127 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01002128 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01002129 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002130 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002131 name = pfx;
2132 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01002134 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002135 if (err < 0)
2136 return err;
2137 }
2138 return 0;
2139}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002140
Takashi Iwai352f7f92012-12-19 12:52:06 +01002141static int create_hp_out_ctls(struct hda_codec *codec)
2142{
2143 struct hda_gen_spec *spec = codec->spec;
2144 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002145 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002146 "Headphone");
2147}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Takashi Iwai352f7f92012-12-19 12:52:06 +01002149static int create_speaker_out_ctls(struct hda_codec *codec)
2150{
2151 struct hda_gen_spec *spec = codec->spec;
2152 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002153 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002154 "Speaker");
2155}
2156
2157/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002158 * independent HP controls
2159 */
2160
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02002161static void call_hp_automute(struct hda_codec *codec,
2162 struct hda_jack_callback *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002163static int indep_hp_info(struct snd_kcontrol *kcontrol,
2164 struct snd_ctl_elem_info *uinfo)
2165{
2166 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2167}
2168
2169static int indep_hp_get(struct snd_kcontrol *kcontrol,
2170 struct snd_ctl_elem_value *ucontrol)
2171{
2172 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2173 struct hda_gen_spec *spec = codec->spec;
2174 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2175 return 0;
2176}
2177
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002178static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2179 int nomix_path_idx, int mix_path_idx,
2180 int out_type);
2181
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002182static int indep_hp_put(struct snd_kcontrol *kcontrol,
2183 struct snd_ctl_elem_value *ucontrol)
2184{
2185 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2186 struct hda_gen_spec *spec = codec->spec;
2187 unsigned int select = ucontrol->value.enumerated.item[0];
2188 int ret = 0;
2189
2190 mutex_lock(&spec->pcm_mutex);
2191 if (spec->active_streams) {
2192 ret = -EBUSY;
2193 goto unlock;
2194 }
2195
2196 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002197 hda_nid_t *dacp;
2198 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2199 dacp = &spec->private_dac_nids[0];
2200 else
2201 dacp = &spec->multiout.hp_out_nid[0];
2202
2203 /* update HP aamix paths in case it conflicts with indep HP */
2204 if (spec->have_aamix_ctl) {
2205 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2206 update_aamix_paths(codec, spec->aamix_mode,
2207 spec->out_paths[0],
2208 spec->aamix_out_paths[0],
2209 spec->autocfg.line_out_type);
2210 else
2211 update_aamix_paths(codec, spec->aamix_mode,
2212 spec->hp_paths[0],
2213 spec->aamix_out_paths[1],
2214 AUTO_PIN_HP_OUT);
2215 }
2216
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002217 spec->indep_hp_enabled = select;
2218 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002219 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002220 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002221 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002222
Takashi Iwai963afde2013-05-31 15:20:31 +02002223 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002224 ret = 1;
2225 }
2226 unlock:
2227 mutex_unlock(&spec->pcm_mutex);
2228 return ret;
2229}
2230
2231static const struct snd_kcontrol_new indep_hp_ctl = {
2232 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2233 .name = "Independent HP",
2234 .info = indep_hp_info,
2235 .get = indep_hp_get,
2236 .put = indep_hp_put,
2237};
2238
2239
2240static int create_indep_hp_ctls(struct hda_codec *codec)
2241{
2242 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002243 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002244
2245 if (!spec->indep_hp)
2246 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002247 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2248 dac = spec->multiout.dac_nids[0];
2249 else
2250 dac = spec->multiout.hp_out_nid[0];
2251 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002252 spec->indep_hp = 0;
2253 return 0;
2254 }
2255
2256 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002257 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002258 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2259 return -ENOMEM;
2260 return 0;
2261}
2262
2263/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002264 * channel mode enum control
2265 */
2266
2267static int ch_mode_info(struct snd_kcontrol *kcontrol,
2268 struct snd_ctl_elem_info *uinfo)
2269{
2270 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2271 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002272 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002273
2274 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2275 uinfo->count = 1;
2276 uinfo->value.enumerated.items = spec->multi_ios + 1;
2277 if (uinfo->value.enumerated.item > spec->multi_ios)
2278 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002279 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2280 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002281 return 0;
2282}
2283
2284static int ch_mode_get(struct snd_kcontrol *kcontrol,
2285 struct snd_ctl_elem_value *ucontrol)
2286{
2287 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2288 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002289 ucontrol->value.enumerated.item[0] =
2290 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002291 return 0;
2292}
2293
Takashi Iwai196c17662013-01-04 15:01:40 +01002294static inline struct nid_path *
2295get_multiio_path(struct hda_codec *codec, int idx)
2296{
2297 struct hda_gen_spec *spec = codec->spec;
2298 return snd_hda_get_path_from_idx(codec,
2299 spec->out_paths[spec->autocfg.line_outs + idx]);
2300}
2301
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002302static void update_automute_all(struct hda_codec *codec);
2303
Takashi Iwai65033cc2013-04-16 12:31:05 +02002304/* Default value to be passed as aamix argument for snd_hda_activate_path();
2305 * used for output paths
2306 */
2307static bool aamix_default(struct hda_gen_spec *spec)
2308{
2309 return !spec->have_aamix_ctl || spec->aamix_mode;
2310}
2311
Takashi Iwai352f7f92012-12-19 12:52:06 +01002312static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2313{
2314 struct hda_gen_spec *spec = codec->spec;
2315 hda_nid_t nid = spec->multi_io[idx].pin;
2316 struct nid_path *path;
2317
Takashi Iwai196c17662013-01-04 15:01:40 +01002318 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002319 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002321
2322 if (path->active == output)
2323 return 0;
2324
2325 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002326 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002327 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002328 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002329 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002330 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002331 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002332 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002333 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002335
2336 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002337 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002338
Takashi Iwai352f7f92012-12-19 12:52:06 +01002339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340}
2341
Takashi Iwai352f7f92012-12-19 12:52:06 +01002342static int ch_mode_put(struct snd_kcontrol *kcontrol,
2343 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002345 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2346 struct hda_gen_spec *spec = codec->spec;
2347 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Takashi Iwai352f7f92012-12-19 12:52:06 +01002349 ch = ucontrol->value.enumerated.item[0];
2350 if (ch < 0 || ch > spec->multi_ios)
2351 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002352 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002353 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002354 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002355 for (i = 0; i < spec->multi_ios; i++)
2356 set_multi_io(codec, i, i < ch);
2357 spec->multiout.max_channels = max(spec->ext_channel_count,
2358 spec->const_channel_count);
2359 if (spec->need_dac_fix)
2360 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return 1;
2362}
2363
Takashi Iwai352f7f92012-12-19 12:52:06 +01002364static const struct snd_kcontrol_new channel_mode_enum = {
2365 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2366 .name = "Channel Mode",
2367 .info = ch_mode_info,
2368 .get = ch_mode_get,
2369 .put = ch_mode_put,
2370};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Takashi Iwai352f7f92012-12-19 12:52:06 +01002372static int create_multi_channel_mode(struct hda_codec *codec)
2373{
2374 struct hda_gen_spec *spec = codec->spec;
2375
2376 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002377 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002378 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 return 0;
2381}
2382
Takashi Iwai352f7f92012-12-19 12:52:06 +01002383/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002384 * aamix loopback enable/disable switch
2385 */
2386
2387#define loopback_mixing_info indep_hp_info
2388
2389static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2390 struct snd_ctl_elem_value *ucontrol)
2391{
2392 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2393 struct hda_gen_spec *spec = codec->spec;
2394 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2395 return 0;
2396}
2397
2398static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002399 int nomix_path_idx, int mix_path_idx,
2400 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002401{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002402 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002403 struct nid_path *nomix_path, *mix_path;
2404
2405 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2406 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2407 if (!nomix_path || !mix_path)
2408 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002409
2410 /* if HP aamix path is driven from a different DAC and the
2411 * independent HP mode is ON, can't turn on aamix path
2412 */
2413 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2414 mix_path->path[0] != spec->alt_dac_nid)
2415 do_mix = false;
2416
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002417 if (do_mix) {
2418 snd_hda_activate_path(codec, nomix_path, false, true);
2419 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002420 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002421 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002422 snd_hda_activate_path(codec, mix_path, false, false);
2423 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002424 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002425 }
2426}
2427
2428static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2429 struct snd_ctl_elem_value *ucontrol)
2430{
2431 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2432 struct hda_gen_spec *spec = codec->spec;
2433 unsigned int val = ucontrol->value.enumerated.item[0];
2434
2435 if (val == spec->aamix_mode)
2436 return 0;
2437 spec->aamix_mode = val;
2438 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002439 spec->aamix_out_paths[0],
2440 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002441 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002442 spec->aamix_out_paths[1],
2443 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002444 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002445 spec->aamix_out_paths[2],
2446 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002447 return 1;
2448}
2449
2450static const struct snd_kcontrol_new loopback_mixing_enum = {
2451 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2452 .name = "Loopback Mixing",
2453 .info = loopback_mixing_info,
2454 .get = loopback_mixing_get,
2455 .put = loopback_mixing_put,
2456};
2457
2458static int create_loopback_mixing_ctl(struct hda_codec *codec)
2459{
2460 struct hda_gen_spec *spec = codec->spec;
2461
2462 if (!spec->mixer_nid)
2463 return 0;
2464 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2465 spec->aamix_out_paths[2]))
2466 return 0;
2467 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2468 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002469 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002470 return 0;
2471}
2472
2473/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002474 * shared headphone/mic handling
2475 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002476
Takashi Iwai352f7f92012-12-19 12:52:06 +01002477static void call_update_outputs(struct hda_codec *codec);
2478
2479/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002480static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002481{
2482 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002483 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002484 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002485 hda_nid_t pin;
2486
2487 pin = spec->hp_mic_pin;
2488 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2489
2490 if (!force) {
2491 val = snd_hda_codec_get_pin_target(codec, pin);
2492 if (as_mic) {
2493 if (val & PIN_IN)
2494 return;
2495 } else {
2496 if (val & PIN_OUT)
2497 return;
2498 }
2499 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002500
2501 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002502 /* if the HP pin doesn't support VREF and the codec driver gives an
2503 * alternative pin, set up the VREF on that pin instead
2504 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002505 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2506 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2507 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2508 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002509 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002510 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002511 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002512
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002513 if (!spec->hp_mic_jack_modes) {
2514 if (as_mic)
2515 val |= PIN_IN;
2516 else
2517 val = PIN_HP;
2518 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002519 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002520 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002521}
2522
2523/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002524static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002525{
2526 struct hda_gen_spec *spec = codec->spec;
2527 struct auto_pin_cfg *cfg = &spec->autocfg;
2528 unsigned int defcfg;
2529 hda_nid_t nid;
2530
Takashi Iwai967303d2013-02-19 17:12:42 +01002531 if (!spec->hp_mic) {
2532 if (spec->suppress_hp_mic_detect)
2533 return 0;
2534 /* automatic detection: only if no input or a single internal
2535 * input pin is found, try to detect the shared hp/mic
2536 */
2537 if (cfg->num_inputs > 1)
2538 return 0;
2539 else if (cfg->num_inputs == 1) {
2540 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2541 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2542 return 0;
2543 }
2544 }
2545
2546 spec->hp_mic = 0; /* clear once */
2547 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002548 return 0;
2549
Takashi Iwai967303d2013-02-19 17:12:42 +01002550 nid = 0;
2551 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2552 nid = cfg->line_out_pins[0];
2553 else if (cfg->hp_outs > 0)
2554 nid = cfg->hp_pins[0];
2555 if (!nid)
2556 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002557
2558 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2559 return 0; /* no input */
2560
Takashi Iwai967303d2013-02-19 17:12:42 +01002561 cfg->inputs[cfg->num_inputs].pin = nid;
2562 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002563 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002564 cfg->num_inputs++;
2565 spec->hp_mic = 1;
2566 spec->hp_mic_pin = nid;
2567 /* we can't handle auto-mic together with HP-mic */
2568 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002569 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002570 return 0;
2571}
2572
Takashi Iwai978e77e2013-01-10 16:57:58 +01002573/*
2574 * output jack mode
2575 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002576
2577static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2578
2579static const char * const out_jack_texts[] = {
2580 "Line Out", "Headphone Out",
2581};
2582
Takashi Iwai978e77e2013-01-10 16:57:58 +01002583static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2584 struct snd_ctl_elem_info *uinfo)
2585{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002586 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002587}
2588
2589static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2590 struct snd_ctl_elem_value *ucontrol)
2591{
2592 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2593 hda_nid_t nid = kcontrol->private_value;
2594 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2595 ucontrol->value.enumerated.item[0] = 1;
2596 else
2597 ucontrol->value.enumerated.item[0] = 0;
2598 return 0;
2599}
2600
2601static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2602 struct snd_ctl_elem_value *ucontrol)
2603{
2604 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2605 hda_nid_t nid = kcontrol->private_value;
2606 unsigned int val;
2607
2608 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2609 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2610 return 0;
2611 snd_hda_set_pin_ctl_cache(codec, nid, val);
2612 return 1;
2613}
2614
2615static const struct snd_kcontrol_new out_jack_mode_enum = {
2616 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2617 .info = out_jack_mode_info,
2618 .get = out_jack_mode_get,
2619 .put = out_jack_mode_put,
2620};
2621
2622static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2623{
2624 struct hda_gen_spec *spec = codec->spec;
2625 int i;
2626
2627 for (i = 0; i < spec->kctls.used; i++) {
2628 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2629 if (!strcmp(kctl->name, name) && kctl->index == idx)
2630 return true;
2631 }
2632 return false;
2633}
2634
2635static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2636 char *name, size_t name_len)
2637{
2638 struct hda_gen_spec *spec = codec->spec;
2639 int idx = 0;
2640
2641 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2642 strlcat(name, " Jack Mode", name_len);
2643
2644 for (; find_kctl_name(codec, name, idx); idx++)
2645 ;
2646}
2647
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002648static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2649{
2650 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002651 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002652 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2653 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2654 return 2;
2655 }
2656 return 1;
2657}
2658
Takashi Iwai978e77e2013-01-10 16:57:58 +01002659static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2660 hda_nid_t *pins)
2661{
2662 struct hda_gen_spec *spec = codec->spec;
2663 int i;
2664
2665 for (i = 0; i < num_pins; i++) {
2666 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002667 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002668 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002669 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002670 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002671 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002672 get_jack_mode_name(codec, pin, name, sizeof(name));
2673 knew = snd_hda_gen_add_kctl(spec, name,
2674 &out_jack_mode_enum);
2675 if (!knew)
2676 return -ENOMEM;
2677 knew->private_value = pin;
2678 }
2679 }
2680
2681 return 0;
2682}
2683
Takashi Iwai294765582013-01-17 09:52:11 +01002684/*
2685 * input jack mode
2686 */
2687
2688/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2689#define NUM_VREFS 6
2690
2691static const char * const vref_texts[NUM_VREFS] = {
2692 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2693 "", "Mic 80pc Bias", "Mic 100pc Bias"
2694};
2695
2696static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2697{
2698 unsigned int pincap;
2699
2700 pincap = snd_hda_query_pin_caps(codec, pin);
2701 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2702 /* filter out unusual vrefs */
2703 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2704 return pincap;
2705}
2706
2707/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2708static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2709{
2710 unsigned int i, n = 0;
2711
2712 for (i = 0; i < NUM_VREFS; i++) {
2713 if (vref_caps & (1 << i)) {
2714 if (n == item_idx)
2715 return i;
2716 n++;
2717 }
2718 }
2719 return 0;
2720}
2721
2722/* convert back from the vref ctl index to the enum item index */
2723static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2724{
2725 unsigned int i, n = 0;
2726
2727 for (i = 0; i < NUM_VREFS; i++) {
2728 if (i == idx)
2729 return n;
2730 if (vref_caps & (1 << i))
2731 n++;
2732 }
2733 return 0;
2734}
2735
2736static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2737 struct snd_ctl_elem_info *uinfo)
2738{
2739 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2740 hda_nid_t nid = kcontrol->private_value;
2741 unsigned int vref_caps = get_vref_caps(codec, nid);
2742
2743 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2744 vref_texts);
2745 /* set the right text */
2746 strcpy(uinfo->value.enumerated.name,
2747 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2748 return 0;
2749}
2750
2751static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2752 struct snd_ctl_elem_value *ucontrol)
2753{
2754 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2755 hda_nid_t nid = kcontrol->private_value;
2756 unsigned int vref_caps = get_vref_caps(codec, nid);
2757 unsigned int idx;
2758
2759 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2760 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2761 return 0;
2762}
2763
2764static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2765 struct snd_ctl_elem_value *ucontrol)
2766{
2767 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2768 hda_nid_t nid = kcontrol->private_value;
2769 unsigned int vref_caps = get_vref_caps(codec, nid);
2770 unsigned int val, idx;
2771
2772 val = snd_hda_codec_get_pin_target(codec, nid);
2773 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2774 if (idx == ucontrol->value.enumerated.item[0])
2775 return 0;
2776
2777 val &= ~AC_PINCTL_VREFEN;
2778 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2779 snd_hda_set_pin_ctl_cache(codec, nid, val);
2780 return 1;
2781}
2782
2783static const struct snd_kcontrol_new in_jack_mode_enum = {
2784 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2785 .info = in_jack_mode_info,
2786 .get = in_jack_mode_get,
2787 .put = in_jack_mode_put,
2788};
2789
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002790static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2791{
2792 struct hda_gen_spec *spec = codec->spec;
2793 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002794 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002795 nitems = hweight32(get_vref_caps(codec, pin));
2796 return nitems ? nitems : 1;
2797}
2798
Takashi Iwai294765582013-01-17 09:52:11 +01002799static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2800{
2801 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002802 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002803 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002804 unsigned int defcfg;
2805
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002806 if (pin == spec->hp_mic_pin)
2807 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002808
2809 /* no jack mode for fixed pins */
2810 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2811 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2812 return 0;
2813
2814 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002815 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002816 return 0;
2817
2818 get_jack_mode_name(codec, pin, name, sizeof(name));
2819 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2820 if (!knew)
2821 return -ENOMEM;
2822 knew->private_value = pin;
2823 return 0;
2824}
2825
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002826/*
2827 * HP/mic shared jack mode
2828 */
2829static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2830 struct snd_ctl_elem_info *uinfo)
2831{
2832 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2833 hda_nid_t nid = kcontrol->private_value;
2834 int out_jacks = get_out_jack_num_items(codec, nid);
2835 int in_jacks = get_in_jack_num_items(codec, nid);
2836 const char *text = NULL;
2837 int idx;
2838
2839 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2840 uinfo->count = 1;
2841 uinfo->value.enumerated.items = out_jacks + in_jacks;
2842 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2843 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2844 idx = uinfo->value.enumerated.item;
2845 if (idx < out_jacks) {
2846 if (out_jacks > 1)
2847 text = out_jack_texts[idx];
2848 else
2849 text = "Headphone Out";
2850 } else {
2851 idx -= out_jacks;
2852 if (in_jacks > 1) {
2853 unsigned int vref_caps = get_vref_caps(codec, nid);
2854 text = vref_texts[get_vref_idx(vref_caps, idx)];
2855 } else
2856 text = "Mic In";
2857 }
2858
2859 strcpy(uinfo->value.enumerated.name, text);
2860 return 0;
2861}
2862
2863static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2864{
2865 int out_jacks = get_out_jack_num_items(codec, nid);
2866 int in_jacks = get_in_jack_num_items(codec, nid);
2867 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2868 int idx = 0;
2869
2870 if (val & PIN_OUT) {
2871 if (out_jacks > 1 && val == PIN_HP)
2872 idx = 1;
2873 } else if (val & PIN_IN) {
2874 idx = out_jacks;
2875 if (in_jacks > 1) {
2876 unsigned int vref_caps = get_vref_caps(codec, nid);
2877 val &= AC_PINCTL_VREFEN;
2878 idx += cvt_from_vref_idx(vref_caps, val);
2879 }
2880 }
2881 return idx;
2882}
2883
2884static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2885 struct snd_ctl_elem_value *ucontrol)
2886{
2887 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2888 hda_nid_t nid = kcontrol->private_value;
2889 ucontrol->value.enumerated.item[0] =
2890 get_cur_hp_mic_jack_mode(codec, nid);
2891 return 0;
2892}
2893
2894static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2895 struct snd_ctl_elem_value *ucontrol)
2896{
2897 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2898 hda_nid_t nid = kcontrol->private_value;
2899 int out_jacks = get_out_jack_num_items(codec, nid);
2900 int in_jacks = get_in_jack_num_items(codec, nid);
2901 unsigned int val, oldval, idx;
2902
2903 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2904 idx = ucontrol->value.enumerated.item[0];
2905 if (oldval == idx)
2906 return 0;
2907
2908 if (idx < out_jacks) {
2909 if (out_jacks > 1)
2910 val = idx ? PIN_HP : PIN_OUT;
2911 else
2912 val = PIN_HP;
2913 } else {
2914 idx -= out_jacks;
2915 if (in_jacks > 1) {
2916 unsigned int vref_caps = get_vref_caps(codec, nid);
2917 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002918 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2919 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002920 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002921 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002922 }
2923 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002924 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002925
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002926 return 1;
2927}
2928
2929static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2930 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2931 .info = hp_mic_jack_mode_info,
2932 .get = hp_mic_jack_mode_get,
2933 .put = hp_mic_jack_mode_put,
2934};
2935
2936static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2937{
2938 struct hda_gen_spec *spec = codec->spec;
2939 struct snd_kcontrol_new *knew;
2940
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002941 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2942 &hp_mic_jack_mode_enum);
2943 if (!knew)
2944 return -ENOMEM;
2945 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002946 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002947 return 0;
2948}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002949
2950/*
2951 * Parse input paths
2952 */
2953
Takashi Iwai352f7f92012-12-19 12:52:06 +01002954/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002955static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002956{
2957 struct hda_amp_list *list;
2958
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002959 list = snd_array_new(&spec->loopback_list);
2960 if (!list)
2961 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002962 list->nid = mix;
2963 list->dir = HDA_INPUT;
2964 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002965 spec->loopback.amplist = spec->loopback_list.list;
2966 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002967}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002968
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002969/* return true if either a volume or a mute amp is found for the given
2970 * aamix path; the amp has to be either in the mixer node or its direct leaf
2971 */
2972static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2973 hda_nid_t pin, unsigned int *mix_val,
2974 unsigned int *mute_val)
2975{
2976 int idx, num_conns;
2977 const hda_nid_t *list;
2978 hda_nid_t nid;
2979
2980 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2981 if (idx < 0)
2982 return false;
2983
2984 *mix_val = *mute_val = 0;
2985 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2986 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2987 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2988 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2989 if (*mix_val && *mute_val)
2990 return true;
2991
2992 /* check leaf node */
2993 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2994 if (num_conns < idx)
2995 return false;
2996 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01002997 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2998 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002999 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01003000 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3001 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003002 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3003
3004 return *mix_val || *mute_val;
3005}
3006
Takashi Iwai352f7f92012-12-19 12:52:06 +01003007/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01003008static int new_analog_input(struct hda_codec *codec, int input_idx,
3009 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003010 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003012 struct hda_gen_spec *spec = codec->spec;
3013 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003014 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003015 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003017 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3018 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003019
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003020 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003021 if (!path)
3022 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003023 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01003024 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003025
3026 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003027 if (mix_val) {
3028 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003029 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003031 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 }
3033
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003034 if (mute_val) {
3035 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003036 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003038 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 }
3040
Takashi Iwai352f7f92012-12-19 12:52:06 +01003041 path->active = true;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003042 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003043 err = add_loopback_list(spec, mix_nid, idx);
3044 if (err < 0)
3045 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003046
3047 if (spec->mixer_nid != spec->mixer_merge_nid &&
3048 !spec->loopback_merge_path) {
3049 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3050 spec->mixer_merge_nid, 0);
3051 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003052 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003053 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003054 path->pin_fixed = true; /* static route */
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003055 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003056 spec->loopback_merge_path =
3057 snd_hda_get_path_idx(codec, path);
3058 }
3059 }
3060
Takashi Iwai352f7f92012-12-19 12:52:06 +01003061 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062}
3063
Takashi Iwai352f7f92012-12-19 12:52:06 +01003064static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003066 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3067 return (pincap & AC_PINCAP_IN) != 0;
3068}
3069
3070/* Parse the codec tree and retrieve ADCs */
3071static int fill_adc_nids(struct hda_codec *codec)
3072{
3073 struct hda_gen_spec *spec = codec->spec;
3074 hda_nid_t nid;
3075 hda_nid_t *adc_nids = spec->adc_nids;
3076 int max_nums = ARRAY_SIZE(spec->adc_nids);
Takashi Iwai7639a062015-03-03 10:07:24 +01003077 int nums = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003078
Takashi Iwai7639a062015-03-03 10:07:24 +01003079 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003080 unsigned int caps = get_wcaps(codec, nid);
3081 int type = get_wcaps_type(caps);
3082
3083 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3084 continue;
3085 adc_nids[nums] = nid;
3086 if (++nums >= max_nums)
3087 break;
3088 }
3089 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01003090
3091 /* copy the detected ADCs to all_adcs[] */
3092 spec->num_all_adcs = nums;
3093 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3094
Takashi Iwai352f7f92012-12-19 12:52:06 +01003095 return nums;
3096}
3097
3098/* filter out invalid adc_nids that don't give all active input pins;
3099 * if needed, check whether dynamic ADC-switching is available
3100 */
3101static int check_dyn_adc_switch(struct hda_codec *codec)
3102{
3103 struct hda_gen_spec *spec = codec->spec;
3104 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003105 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003106 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003107
Takashi Iwai352f7f92012-12-19 12:52:06 +01003108 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003109 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003110 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003111 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003112 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003113 break;
3114 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003115 if (i >= imux->num_items) {
3116 ok_bits |= (1 << n);
3117 nums++;
3118 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003119 }
3120
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003121 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003122 /* check whether ADC-switch is possible */
3123 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003124 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003125 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003126 spec->dyn_adc_idx[i] = n;
3127 break;
3128 }
3129 }
3130 }
3131
Takashi Iwai4e76a882014-02-25 12:21:03 +01003132 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003133 spec->dyn_adc_switch = 1;
3134 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003135 /* shrink the invalid adcs and input paths */
3136 nums = 0;
3137 for (n = 0; n < spec->num_adc_nids; n++) {
3138 if (!(ok_bits & (1 << n)))
3139 continue;
3140 if (n != nums) {
3141 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003142 for (i = 0; i < imux->num_items; i++) {
3143 invalidate_nid_path(codec,
3144 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003145 spec->input_paths[i][nums] =
3146 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003147 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003148 }
3149 nums++;
3150 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003151 spec->num_adc_nids = nums;
3152 }
3153
Takashi Iwai967303d2013-02-19 17:12:42 +01003154 if (imux->num_items == 1 ||
3155 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003156 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003157 spec->num_adc_nids = 1; /* reduce to a single ADC */
3158 }
3159
3160 /* single index for individual volumes ctls */
3161 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3162 spec->num_adc_nids = 1;
3163
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 return 0;
3165}
3166
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003167/* parse capture source paths from the given pin and create imux items */
3168static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003169 int cfg_idx, int num_adcs,
3170 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003171{
3172 struct hda_gen_spec *spec = codec->spec;
3173 struct hda_input_mux *imux = &spec->input_mux;
3174 int imux_idx = imux->num_items;
3175 bool imux_added = false;
3176 int c;
3177
3178 for (c = 0; c < num_adcs; c++) {
3179 struct nid_path *path;
3180 hda_nid_t adc = spec->adc_nids[c];
3181
3182 if (!is_reachable_path(codec, pin, adc))
3183 continue;
3184 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3185 if (!path)
3186 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003187 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003188 spec->input_paths[imux_idx][c] =
3189 snd_hda_get_path_idx(codec, path);
3190
3191 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003192 if (spec->hp_mic_pin == pin)
3193 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003194 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003195 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003196 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003197 if (spec->dyn_adc_switch)
3198 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003199 }
3200 }
3201
3202 return 0;
3203}
3204
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003206 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003208
Takashi Iwaic9700422013-01-18 10:17:30 +01003209/* fill the label for each input at first */
3210static int fill_input_pin_labels(struct hda_codec *codec)
3211{
3212 struct hda_gen_spec *spec = codec->spec;
3213 const struct auto_pin_cfg *cfg = &spec->autocfg;
3214 int i;
3215
3216 for (i = 0; i < cfg->num_inputs; i++) {
3217 hda_nid_t pin = cfg->inputs[i].pin;
3218 const char *label;
3219 int j, idx;
3220
3221 if (!is_input_pin(codec, pin))
3222 continue;
3223
3224 label = hda_get_autocfg_input_label(codec, cfg, i);
3225 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003226 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003227 if (spec->input_labels[j] &&
3228 !strcmp(spec->input_labels[j], label)) {
3229 idx = spec->input_label_idxs[j] + 1;
3230 break;
3231 }
3232 }
3233
3234 spec->input_labels[i] = label;
3235 spec->input_label_idxs[i] = idx;
3236 }
3237
3238 return 0;
3239}
3240
Takashi Iwai9dba2052013-01-18 10:01:15 +01003241#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3242
Takashi Iwai352f7f92012-12-19 12:52:06 +01003243static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003244{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003245 struct hda_gen_spec *spec = codec->spec;
3246 const struct auto_pin_cfg *cfg = &spec->autocfg;
3247 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003248 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003249 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003250 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003251
Takashi Iwai352f7f92012-12-19 12:52:06 +01003252 num_adcs = fill_adc_nids(codec);
3253 if (num_adcs < 0)
3254 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003255
Takashi Iwaic9700422013-01-18 10:17:30 +01003256 err = fill_input_pin_labels(codec);
3257 if (err < 0)
3258 return err;
3259
Takashi Iwai352f7f92012-12-19 12:52:06 +01003260 for (i = 0; i < cfg->num_inputs; i++) {
3261 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
Takashi Iwai352f7f92012-12-19 12:52:06 +01003263 pin = cfg->inputs[i].pin;
3264 if (!is_input_pin(codec, pin))
3265 continue;
3266
Takashi Iwai2c12c302013-01-10 09:33:29 +01003267 val = PIN_IN;
3268 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3269 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai3e1b0c42015-04-27 10:43:22 +02003270 if (pin != spec->hp_mic_pin &&
3271 !snd_hda_codec_get_pin_target(codec, pin))
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003272 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003273
Takashi Iwai352f7f92012-12-19 12:52:06 +01003274 if (mixer) {
3275 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003276 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003277 spec->input_labels[i],
3278 spec->input_label_idxs[i],
3279 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003280 if (err < 0)
3281 return err;
3282 }
3283 }
3284
Takashi Iwaic9700422013-01-18 10:17:30 +01003285 err = parse_capture_source(codec, pin, i, num_adcs,
3286 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003287 if (err < 0)
3288 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003289
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003290 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003291 err = create_in_jack_mode(codec, pin);
3292 if (err < 0)
3293 return err;
3294 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003295 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003296
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003297 /* add stereo mix when explicitly enabled via hint */
Takashi Iwai74f14b32014-12-15 13:43:59 +01003298 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003299 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003300 "Stereo Mix", 0);
3301 if (err < 0)
3302 return err;
Takashi Iwai82d04e12014-12-15 13:40:42 +01003303 else
3304 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003305 }
3306
3307 return 0;
3308}
3309
3310
3311/*
3312 * input source mux
3313 */
3314
Takashi Iwaic697b712013-01-07 17:09:26 +01003315/* get the input path specified by the given adc and imux indices */
3316static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003317{
3318 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003319 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3320 snd_BUG();
3321 return NULL;
3322 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003323 if (spec->dyn_adc_switch)
3324 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003325 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003326 snd_BUG();
3327 return NULL;
3328 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003329 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003330}
3331
3332static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3333 unsigned int idx);
3334
3335static int mux_enum_info(struct snd_kcontrol *kcontrol,
3336 struct snd_ctl_elem_info *uinfo)
3337{
3338 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3339 struct hda_gen_spec *spec = codec->spec;
3340 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3341}
3342
3343static int mux_enum_get(struct snd_kcontrol *kcontrol,
3344 struct snd_ctl_elem_value *ucontrol)
3345{
3346 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3347 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003348 /* the ctls are created at once with multiple counts */
3349 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003350
3351 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3352 return 0;
3353}
3354
3355static int mux_enum_put(struct snd_kcontrol *kcontrol,
3356 struct snd_ctl_elem_value *ucontrol)
3357{
3358 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003359 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003360 return mux_select(codec, adc_idx,
3361 ucontrol->value.enumerated.item[0]);
3362}
3363
Takashi Iwai352f7f92012-12-19 12:52:06 +01003364static const struct snd_kcontrol_new cap_src_temp = {
3365 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3366 .name = "Input Source",
3367 .info = mux_enum_info,
3368 .get = mux_enum_get,
3369 .put = mux_enum_put,
3370};
3371
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003372/*
3373 * capture volume and capture switch ctls
3374 */
3375
Takashi Iwai352f7f92012-12-19 12:52:06 +01003376typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3377 struct snd_ctl_elem_value *ucontrol);
3378
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003379/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003380static int cap_put_caller(struct snd_kcontrol *kcontrol,
3381 struct snd_ctl_elem_value *ucontrol,
3382 put_call_t func, int type)
3383{
3384 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3385 struct hda_gen_spec *spec = codec->spec;
3386 const struct hda_input_mux *imux;
3387 struct nid_path *path;
3388 int i, adc_idx, err = 0;
3389
3390 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003391 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003392 mutex_lock(&codec->control_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003393 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003394 path = get_input_path(codec, adc_idx, i);
3395 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003396 continue;
3397 kcontrol->private_value = path->ctls[type];
3398 err = func(kcontrol, ucontrol);
3399 if (err < 0)
Takashi Iwaia551d912015-02-26 12:34:49 +01003400 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003401 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003402 mutex_unlock(&codec->control_mutex);
3403 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003404 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003405 return err;
3406}
3407
3408/* capture volume ctl callbacks */
3409#define cap_vol_info snd_hda_mixer_amp_volume_info
3410#define cap_vol_get snd_hda_mixer_amp_volume_get
3411#define cap_vol_tlv snd_hda_mixer_amp_tlv
3412
3413static int cap_vol_put(struct snd_kcontrol *kcontrol,
3414 struct snd_ctl_elem_value *ucontrol)
3415{
3416 return cap_put_caller(kcontrol, ucontrol,
3417 snd_hda_mixer_amp_volume_put,
3418 NID_PATH_VOL_CTL);
3419}
3420
3421static const struct snd_kcontrol_new cap_vol_temp = {
3422 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3423 .name = "Capture Volume",
3424 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3425 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3426 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3427 .info = cap_vol_info,
3428 .get = cap_vol_get,
3429 .put = cap_vol_put,
3430 .tlv = { .c = cap_vol_tlv },
3431};
3432
3433/* capture switch ctl callbacks */
3434#define cap_sw_info snd_ctl_boolean_stereo_info
3435#define cap_sw_get snd_hda_mixer_amp_switch_get
3436
3437static int cap_sw_put(struct snd_kcontrol *kcontrol,
3438 struct snd_ctl_elem_value *ucontrol)
3439{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003440 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003441 snd_hda_mixer_amp_switch_put,
3442 NID_PATH_MUTE_CTL);
3443}
3444
3445static const struct snd_kcontrol_new cap_sw_temp = {
3446 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3447 .name = "Capture Switch",
3448 .info = cap_sw_info,
3449 .get = cap_sw_get,
3450 .put = cap_sw_put,
3451};
3452
3453static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3454{
3455 hda_nid_t nid;
3456 int i, depth;
3457
3458 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3459 for (depth = 0; depth < 3; depth++) {
3460 if (depth >= path->depth)
3461 return -EINVAL;
3462 i = path->depth - depth - 1;
3463 nid = path->path[i];
3464 if (!path->ctls[NID_PATH_VOL_CTL]) {
3465 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3466 path->ctls[NID_PATH_VOL_CTL] =
3467 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3468 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3469 int idx = path->idx[i];
3470 if (!depth && codec->single_adc_amp)
3471 idx = 0;
3472 path->ctls[NID_PATH_VOL_CTL] =
3473 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3474 }
3475 }
3476 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3477 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3478 path->ctls[NID_PATH_MUTE_CTL] =
3479 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3480 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3481 int idx = path->idx[i];
3482 if (!depth && codec->single_adc_amp)
3483 idx = 0;
3484 path->ctls[NID_PATH_MUTE_CTL] =
3485 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3486 }
3487 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 return 0;
3490}
3491
Takashi Iwai352f7f92012-12-19 12:52:06 +01003492static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003494 struct hda_gen_spec *spec = codec->spec;
3495 struct auto_pin_cfg *cfg = &spec->autocfg;
3496 unsigned int val;
3497 int i;
3498
3499 if (!spec->inv_dmic_split)
3500 return false;
3501 for (i = 0; i < cfg->num_inputs; i++) {
3502 if (cfg->inputs[i].pin != nid)
3503 continue;
3504 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3505 return false;
3506 val = snd_hda_codec_get_pincfg(codec, nid);
3507 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3508 }
3509 return false;
3510}
3511
Takashi Iwaia90229e2013-01-18 14:10:00 +01003512/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003513static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3514 struct snd_ctl_elem_value *ucontrol)
3515{
3516 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3517 struct hda_gen_spec *spec = codec->spec;
3518 int ret;
3519
3520 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3521 if (ret < 0)
3522 return ret;
3523
Takashi Iwaia90229e2013-01-18 14:10:00 +01003524 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003525 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003526
3527 return ret;
3528}
3529
Takashi Iwai352f7f92012-12-19 12:52:06 +01003530static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3531 int idx, bool is_switch, unsigned int ctl,
3532 bool inv_dmic)
3533{
3534 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003535 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003536 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3537 const char *sfx = is_switch ? "Switch" : "Volume";
3538 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003539 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003540
3541 if (!ctl)
3542 return 0;
3543
3544 if (label)
3545 snprintf(tmpname, sizeof(tmpname),
3546 "%s Capture %s", label, sfx);
3547 else
3548 snprintf(tmpname, sizeof(tmpname),
3549 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003550 knew = add_control(spec, type, tmpname, idx,
3551 amp_val_replace_channels(ctl, chs));
3552 if (!knew)
3553 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003554 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003555 knew->put = cap_single_sw_put;
3556 if (!inv_dmic)
3557 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003558
3559 /* Make independent right kcontrol */
3560 if (label)
3561 snprintf(tmpname, sizeof(tmpname),
3562 "Inverted %s Capture %s", label, sfx);
3563 else
3564 snprintf(tmpname, sizeof(tmpname),
3565 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003566 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003567 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003568 if (!knew)
3569 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003570 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003571 knew->put = cap_single_sw_put;
3572 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003573}
3574
3575/* create single (and simple) capture volume and switch controls */
3576static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3577 unsigned int vol_ctl, unsigned int sw_ctl,
3578 bool inv_dmic)
3579{
3580 int err;
3581 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3582 if (err < 0)
3583 return err;
3584 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3585 if (err < 0)
3586 return err;
3587 return 0;
3588}
3589
3590/* create bound capture volume and switch controls */
3591static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3592 unsigned int vol_ctl, unsigned int sw_ctl)
3593{
3594 struct hda_gen_spec *spec = codec->spec;
3595 struct snd_kcontrol_new *knew;
3596
3597 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003598 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003599 if (!knew)
3600 return -ENOMEM;
3601 knew->index = idx;
3602 knew->private_value = vol_ctl;
3603 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3604 }
3605 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003606 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003607 if (!knew)
3608 return -ENOMEM;
3609 knew->index = idx;
3610 knew->private_value = sw_ctl;
3611 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3612 }
3613 return 0;
3614}
3615
3616/* return the vol ctl when used first in the imux list */
3617static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3618{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003619 struct nid_path *path;
3620 unsigned int ctl;
3621 int i;
3622
Takashi Iwaic697b712013-01-07 17:09:26 +01003623 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003624 if (!path)
3625 return 0;
3626 ctl = path->ctls[type];
3627 if (!ctl)
3628 return 0;
3629 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003630 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003631 if (path && path->ctls[type] == ctl)
3632 return 0;
3633 }
3634 return ctl;
3635}
3636
3637/* create individual capture volume and switch controls per input */
3638static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3639{
3640 struct hda_gen_spec *spec = codec->spec;
3641 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003642 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003643
3644 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003645 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003646 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003647
Takashi Iwaic9700422013-01-18 10:17:30 +01003648 idx = imux->items[i].index;
3649 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003650 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003651 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3652
3653 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003654 err = add_single_cap_ctl(codec,
3655 spec->input_labels[idx],
3656 spec->input_label_idxs[idx],
3657 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003658 get_first_cap_ctl(codec, i, type),
3659 inv_dmic);
3660 if (err < 0)
3661 return err;
3662 }
3663 }
3664 return 0;
3665}
3666
3667static int create_capture_mixers(struct hda_codec *codec)
3668{
3669 struct hda_gen_spec *spec = codec->spec;
3670 struct hda_input_mux *imux = &spec->input_mux;
3671 int i, n, nums, err;
3672
3673 if (spec->dyn_adc_switch)
3674 nums = 1;
3675 else
3676 nums = spec->num_adc_nids;
3677
3678 if (!spec->auto_mic && imux->num_items > 1) {
3679 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003680 const char *name;
3681 name = nums > 1 ? "Input Source" : "Capture Source";
3682 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003683 if (!knew)
3684 return -ENOMEM;
3685 knew->count = nums;
3686 }
3687
3688 for (n = 0; n < nums; n++) {
3689 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003690 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003691 bool inv_dmic = false;
3692 int vol, sw;
3693
3694 vol = sw = 0;
3695 for (i = 0; i < imux->num_items; i++) {
3696 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003697 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003698 if (!path)
3699 continue;
3700 parse_capvol_in_path(codec, path);
3701 if (!vol)
3702 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003703 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003704 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003705 if (!same_amp_caps(codec, vol,
3706 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3707 multi_cap_vol = true;
3708 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003709 if (!sw)
3710 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003711 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003712 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003713 if (!same_amp_caps(codec, sw,
3714 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3715 multi_cap_vol = true;
3716 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003717 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3718 inv_dmic = true;
3719 }
3720
3721 if (!multi)
3722 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3723 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003724 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003725 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3726 else
3727 err = create_multi_cap_vol_ctl(codec);
3728 if (err < 0)
3729 return err;
3730 }
3731
3732 return 0;
3733}
3734
3735/*
3736 * add mic boosts if needed
3737 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003738
3739/* check whether the given amp is feasible as a boost volume */
3740static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3741 int dir, int idx)
3742{
3743 unsigned int step;
3744
3745 if (!nid_has_volume(codec, nid, dir) ||
3746 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3747 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3748 return false;
3749
3750 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3751 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3752 if (step < 0x20)
3753 return false;
3754 return true;
3755}
3756
3757/* look for a boost amp in a widget close to the pin */
3758static unsigned int look_for_boost_amp(struct hda_codec *codec,
3759 struct nid_path *path)
3760{
3761 unsigned int val = 0;
3762 hda_nid_t nid;
3763 int depth;
3764
3765 for (depth = 0; depth < 3; depth++) {
3766 if (depth >= path->depth - 1)
3767 break;
3768 nid = path->path[depth];
3769 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3770 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3771 break;
3772 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3773 path->idx[depth])) {
3774 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3775 HDA_INPUT);
3776 break;
3777 }
3778 }
3779
3780 return val;
3781}
3782
Takashi Iwai352f7f92012-12-19 12:52:06 +01003783static int parse_mic_boost(struct hda_codec *codec)
3784{
3785 struct hda_gen_spec *spec = codec->spec;
3786 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003787 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003788 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003789
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003790 if (!spec->num_adc_nids)
3791 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003792
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003793 for (i = 0; i < imux->num_items; i++) {
3794 struct nid_path *path;
3795 unsigned int val;
3796 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003797 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003798
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003799 idx = imux->items[i].index;
3800 if (idx >= imux->num_items)
3801 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003802
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003803 /* check only line-in and mic pins */
Takashi Iwai1799cdd2013-01-18 14:37:16 +01003804 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003805 continue;
3806
3807 path = get_input_path(codec, 0, i);
3808 if (!path)
3809 continue;
3810
3811 val = look_for_boost_amp(codec, path);
3812 if (!val)
3813 continue;
3814
3815 /* create a boost control */
3816 snprintf(boost_label, sizeof(boost_label),
3817 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003818 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3819 spec->input_label_idxs[idx], val))
3820 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003821
3822 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003823 }
3824 return 0;
3825}
3826
3827/*
3828 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3829 */
3830static void parse_digital(struct hda_codec *codec)
3831{
3832 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003833 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003834 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003835 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003836
3837 /* support multiple SPDIFs; the secondary is set up as a slave */
3838 nums = 0;
3839 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003840 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003841 dig_nid = look_for_dac(codec, pin, true);
3842 if (!dig_nid)
3843 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003844 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003845 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003846 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003847 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003848 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003849 path->pin_fixed = true; /* no jack detection */
Takashi Iwai196c17662013-01-04 15:01:40 +01003850 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003851 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003852 if (!nums) {
3853 spec->multiout.dig_out_nid = dig_nid;
3854 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3855 } else {
3856 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3857 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
Dan Carpenterd5764222014-05-14 17:18:31 +03003858 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003859 spec->slave_dig_outs[nums - 1] = dig_nid;
3860 }
3861 nums++;
3862 }
3863
3864 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003865 pin = spec->autocfg.dig_in_pin;
Takashi Iwai7639a062015-03-03 10:07:24 +01003866 for_each_hda_codec_node(dig_nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003867 unsigned int wcaps = get_wcaps(codec, dig_nid);
3868 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3869 continue;
3870 if (!(wcaps & AC_WCAP_DIGITAL))
3871 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003872 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003873 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003874 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003875 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003876 path->pin_fixed = true; /* no jack */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003877 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003878 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003879 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003880 break;
3881 }
3882 }
3883 }
3884}
3885
3886
3887/*
3888 * input MUX handling
3889 */
3890
3891static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3892
3893/* select the given imux item; either unmute exclusively or select the route */
3894static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3895 unsigned int idx)
3896{
3897 struct hda_gen_spec *spec = codec->spec;
3898 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003899 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003900
3901 imux = &spec->input_mux;
3902 if (!imux->num_items)
3903 return 0;
3904
3905 if (idx >= imux->num_items)
3906 idx = imux->num_items - 1;
3907 if (spec->cur_mux[adc_idx] == idx)
3908 return 0;
3909
Takashi Iwai55196ff2013-01-24 17:32:56 +01003910 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3911 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003912 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003913 if (old_path->active)
3914 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003915
3916 spec->cur_mux[adc_idx] = idx;
3917
Takashi Iwai967303d2013-02-19 17:12:42 +01003918 if (spec->hp_mic)
3919 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003920
3921 if (spec->dyn_adc_switch)
3922 dyn_adc_pcm_resetup(codec, idx);
3923
Takashi Iwaic697b712013-01-07 17:09:26 +01003924 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003925 if (!path)
3926 return 0;
3927 if (path->active)
3928 return 0;
3929 snd_hda_activate_path(codec, path, true, false);
3930 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003931 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003932 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003933 return 1;
3934}
3935
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003936/* power up/down widgets in the all paths that match with the given NID
3937 * as terminals (either start- or endpoint)
3938 *
3939 * returns the last changed NID, or zero if unchanged.
3940 */
3941static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
3942 int pin_state, int stream_state)
3943{
3944 struct hda_gen_spec *spec = codec->spec;
3945 hda_nid_t last, changed = 0;
3946 struct nid_path *path;
3947 int n;
3948
3949 for (n = 0; n < spec->paths.used; n++) {
3950 path = snd_array_elem(&spec->paths, n);
3951 if (path->path[0] == nid ||
3952 path->path[path->depth - 1] == nid) {
3953 bool pin_old = path->pin_enabled;
3954 bool stream_old = path->stream_enabled;
3955
3956 if (pin_state >= 0)
3957 path->pin_enabled = pin_state;
3958 if (stream_state >= 0)
3959 path->stream_enabled = stream_state;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003960 if ((!path->pin_fixed && path->pin_enabled != pin_old)
3961 || path->stream_enabled != stream_old) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003962 last = path_power_update(codec, path, true);
3963 if (last)
3964 changed = last;
3965 }
3966 }
3967 }
3968 return changed;
3969}
3970
Takashi Iwaid5ac0102015-04-09 10:21:30 +02003971/* check the jack status for power control */
3972static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
3973{
3974 if (!is_jack_detectable(codec, pin))
3975 return true;
3976 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
3977}
3978
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003979/* power up/down the paths of the given pin according to the jack state;
3980 * power = 0/1 : only power up/down if it matches with the jack state,
3981 * < 0 : force power up/down to follow the jack sate
3982 *
3983 * returns the last changed NID, or zero if unchanged.
3984 */
3985static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
3986 int power)
3987{
3988 bool on;
3989
Takashi Iwai967b1302015-03-20 18:21:03 +01003990 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003991 return 0;
3992
Takashi Iwaid5ac0102015-04-09 10:21:30 +02003993 on = detect_pin_state(codec, pin);
3994
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003995 if (power >= 0 && on != power)
3996 return 0;
3997 return set_path_power(codec, pin, on, -1);
3998}
3999
4000static void pin_power_callback(struct hda_codec *codec,
4001 struct hda_jack_callback *jack,
4002 bool on)
4003{
4004 if (jack && jack->tbl->nid)
4005 sync_power_state_change(codec,
4006 set_pin_power_jack(codec, jack->tbl->nid, on));
4007}
4008
4009/* callback only doing power up -- called at first */
4010static void pin_power_up_callback(struct hda_codec *codec,
4011 struct hda_jack_callback *jack)
4012{
4013 pin_power_callback(codec, jack, true);
4014}
4015
4016/* callback only doing power down -- called at last */
4017static void pin_power_down_callback(struct hda_codec *codec,
4018 struct hda_jack_callback *jack)
4019{
4020 pin_power_callback(codec, jack, false);
4021}
4022
4023/* set up the power up/down callbacks */
4024static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4025 const hda_nid_t *pins, bool on)
4026{
4027 int i;
4028 hda_jack_callback_fn cb =
4029 on ? pin_power_up_callback : pin_power_down_callback;
4030
4031 for (i = 0; i < num_pins && pins[i]; i++) {
4032 if (is_jack_detectable(codec, pins[i]))
4033 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4034 else
4035 set_path_power(codec, pins[i], true, -1);
4036 }
4037}
4038
4039/* enabled power callback to each available I/O pin with jack detections;
4040 * the digital I/O pins are excluded because of the unreliable detectsion
4041 */
4042static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4043{
4044 struct hda_gen_spec *spec = codec->spec;
4045 struct auto_pin_cfg *cfg = &spec->autocfg;
4046 int i;
4047
Takashi Iwai967b1302015-03-20 18:21:03 +01004048 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004049 return;
4050 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4051 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4052 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4053 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4054 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4055 for (i = 0; i < cfg->num_inputs; i++)
4056 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4057}
4058
4059/* sync path power up/down with the jack states of given pins */
4060static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4061 const hda_nid_t *pins)
4062{
4063 int i;
4064
4065 for (i = 0; i < num_pins && pins[i]; i++)
4066 if (is_jack_detectable(codec, pins[i]))
4067 set_pin_power_jack(codec, pins[i], -1);
4068}
4069
4070/* sync path power up/down with pins; called at init and resume */
4071static void sync_all_pin_power_ctls(struct hda_codec *codec)
4072{
4073 struct hda_gen_spec *spec = codec->spec;
4074 struct auto_pin_cfg *cfg = &spec->autocfg;
4075 int i;
4076
Takashi Iwai967b1302015-03-20 18:21:03 +01004077 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004078 return;
4079 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4080 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4081 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4082 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4083 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4084 for (i = 0; i < cfg->num_inputs; i++)
4085 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4086}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004087
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004088/* add fake paths if not present yet */
4089static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4090 int num_pins, const hda_nid_t *pins)
4091{
4092 struct hda_gen_spec *spec = codec->spec;
4093 struct nid_path *path;
4094 int i;
4095
4096 for (i = 0; i < num_pins; i++) {
4097 if (!pins[i])
4098 break;
4099 if (get_nid_path(codec, nid, pins[i], 0))
4100 continue;
4101 path = snd_array_new(&spec->paths);
4102 if (!path)
4103 return -ENOMEM;
4104 memset(path, 0, sizeof(*path));
4105 path->depth = 2;
4106 path->path[0] = nid;
4107 path->path[1] = pins[i];
4108 path->active = true;
4109 }
4110 return 0;
4111}
4112
4113/* create fake paths to all outputs from beep */
4114static int add_fake_beep_paths(struct hda_codec *codec)
4115{
4116 struct hda_gen_spec *spec = codec->spec;
4117 struct auto_pin_cfg *cfg = &spec->autocfg;
4118 hda_nid_t nid = spec->beep_nid;
4119 int err;
4120
Takashi Iwai967b1302015-03-20 18:21:03 +01004121 if (!codec->power_save_node || !nid)
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004122 return 0;
4123 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4124 if (err < 0)
4125 return err;
4126 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4127 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4128 if (err < 0)
4129 return err;
4130 }
4131 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4132 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4133 cfg->speaker_pins);
4134 if (err < 0)
4135 return err;
4136 }
4137 return 0;
4138}
4139
4140/* power up/down beep widget and its output paths */
4141static void beep_power_hook(struct hda_beep *beep, bool on)
4142{
4143 set_path_power(beep->codec, beep->nid, -1, on);
4144}
4145
Takashi Iwai6b275b12015-03-20 18:11:05 +01004146/**
4147 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4148 * @codec: the HDA codec
4149 * @pin: NID of pin to fix
4150 */
4151int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4152{
4153 struct hda_gen_spec *spec = codec->spec;
4154 struct nid_path *path;
4155
4156 path = snd_array_new(&spec->paths);
4157 if (!path)
4158 return -ENOMEM;
4159 memset(path, 0, sizeof(*path));
4160 path->depth = 1;
4161 path->path[0] = pin;
4162 path->active = true;
4163 path->pin_fixed = true;
4164 path->stream_enabled = true;
4165 return 0;
4166}
4167EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4168
Takashi Iwai352f7f92012-12-19 12:52:06 +01004169/*
4170 * Jack detections for HP auto-mute and mic-switch
4171 */
4172
4173/* check each pin in the given array; returns true if any of them is plugged */
4174static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4175{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004176 int i;
4177 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004178
4179 for (i = 0; i < num_pins; i++) {
4180 hda_nid_t nid = pins[i];
4181 if (!nid)
4182 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01004183 /* don't detect pins retasked as inputs */
4184 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4185 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004186 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4187 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004188 }
4189 return present;
4190}
4191
4192/* standard HP/line-out auto-mute helper */
4193static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004194 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004195{
4196 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004197 int i;
4198
4199 for (i = 0; i < num_pins; i++) {
4200 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01004201 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004202 if (!nid)
4203 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004204
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004205 oldval = snd_hda_codec_get_pin_target(codec, nid);
4206 if (oldval & PIN_IN)
4207 continue; /* no mute for inputs */
4208
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004209 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004210 struct nid_path *path;
4211 hda_nid_t mute_nid;
4212
4213 path = snd_hda_get_path_from_idx(codec, paths[i]);
4214 if (!path)
4215 continue;
4216 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4217 if (!mute_nid)
4218 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004219 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004220 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004221 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004222 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004223 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004224 } else {
4225 /* don't reset VREF value in case it's controlling
4226 * the amp (see alc861_fixup_asus_amp_vref_0f())
4227 */
4228 if (spec->keep_vref_in_automute)
4229 val = oldval & ~PIN_HP;
4230 else
4231 val = 0;
4232 if (!mute)
4233 val |= oldval;
4234 /* here we call update_pin_ctl() so that the pinctl is
4235 * changed without changing the pinctl target value;
4236 * the original target value will be still referred at
4237 * the init / resume again
4238 */
4239 update_pin_ctl(codec, nid, val);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004240 }
4241
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01004242 set_pin_eapd(codec, nid, !mute);
Takashi Iwai967b1302015-03-20 18:21:03 +01004243 if (codec->power_save_node) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004244 bool on = !mute;
4245 if (on)
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004246 on = detect_pin_state(codec, nid);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004247 set_path_power(codec, nid, on, -1);
4248 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004249 }
4250}
4251
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004252/**
4253 * snd_hda_gen_update_outputs - Toggle outputs muting
4254 * @codec: the HDA codec
4255 *
4256 * Update the mute status of all outputs based on the current jack states.
4257 */
Takashi Iwai5d550e12012-12-19 15:16:44 +01004258void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004259{
4260 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004261 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004262 int on;
4263
4264 /* Control HP pins/amps depending on master_mute state;
4265 * in general, HP pins/amps control should be enabled in all cases,
4266 * but currently set only for master_mute, just to be safe
4267 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004268 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4269 paths = spec->out_paths;
4270 else
4271 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01004272 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004273 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004274
4275 if (!spec->automute_speaker)
4276 on = 0;
4277 else
4278 on = spec->hp_jack_present | spec->line_jack_present;
4279 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01004280 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004281 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4282 paths = spec->out_paths;
4283 else
4284 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004285 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004286 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004287
4288 /* toggle line-out mutes if needed, too */
4289 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4290 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4291 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4292 return;
4293 if (!spec->automute_lo)
4294 on = 0;
4295 else
4296 on = spec->hp_jack_present;
4297 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01004298 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004299 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004300 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004301 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004302}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004303EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004304
4305static void call_update_outputs(struct hda_codec *codec)
4306{
4307 struct hda_gen_spec *spec = codec->spec;
4308 if (spec->automute_hook)
4309 spec->automute_hook(codec);
4310 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01004311 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004312
4313 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4314 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4315 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004316}
4317
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004318/**
4319 * snd_hda_gen_hp_automute - standard HP-automute helper
4320 * @codec: the HDA codec
4321 * @jack: jack object, NULL for the whole
4322 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004323void snd_hda_gen_hp_automute(struct hda_codec *codec,
4324 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004325{
4326 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01004327 hda_nid_t *pins = spec->autocfg.hp_pins;
4328 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004329
Takashi Iwai92603c52013-01-22 07:46:31 +01004330 /* No detection for the first HP jack during indep-HP mode */
4331 if (spec->indep_hp_enabled) {
4332 pins++;
4333 num_pins--;
4334 }
4335
4336 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004337 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4338 return;
4339 call_update_outputs(codec);
4340}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004341EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004342
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004343/**
4344 * snd_hda_gen_line_automute - standard line-out-automute helper
4345 * @codec: the HDA codec
4346 * @jack: jack object, NULL for the whole
4347 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004348void snd_hda_gen_line_automute(struct hda_codec *codec,
4349 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004350{
4351 struct hda_gen_spec *spec = codec->spec;
4352
4353 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4354 return;
4355 /* check LO jack only when it's different from HP */
4356 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4357 return;
4358
4359 spec->line_jack_present =
4360 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4361 spec->autocfg.line_out_pins);
4362 if (!spec->automute_speaker || !spec->detect_lo)
4363 return;
4364 call_update_outputs(codec);
4365}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004366EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004367
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004368/**
4369 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4370 * @codec: the HDA codec
4371 * @jack: jack object, NULL for the whole
4372 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004373void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4374 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004375{
4376 struct hda_gen_spec *spec = codec->spec;
4377 int i;
4378
4379 if (!spec->auto_mic)
4380 return;
4381
4382 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01004383 hda_nid_t pin = spec->am_entry[i].pin;
4384 /* don't detect pins retasked as outputs */
4385 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4386 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004387 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004388 mux_select(codec, 0, spec->am_entry[i].idx);
4389 return;
4390 }
4391 }
4392 mux_select(codec, 0, spec->am_entry[0].idx);
4393}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004394EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004395
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004396/* call appropriate hooks */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004397static void call_hp_automute(struct hda_codec *codec,
4398 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004399{
4400 struct hda_gen_spec *spec = codec->spec;
4401 if (spec->hp_automute_hook)
4402 spec->hp_automute_hook(codec, jack);
4403 else
4404 snd_hda_gen_hp_automute(codec, jack);
4405}
4406
4407static void call_line_automute(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004408 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004409{
4410 struct hda_gen_spec *spec = codec->spec;
4411 if (spec->line_automute_hook)
4412 spec->line_automute_hook(codec, jack);
4413 else
4414 snd_hda_gen_line_automute(codec, jack);
4415}
4416
4417static void call_mic_autoswitch(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004418 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004419{
4420 struct hda_gen_spec *spec = codec->spec;
4421 if (spec->mic_autoswitch_hook)
4422 spec->mic_autoswitch_hook(codec, jack);
4423 else
4424 snd_hda_gen_mic_autoswitch(codec, jack);
4425}
4426
Takashi Iwai963afde2013-05-31 15:20:31 +02004427/* update jack retasking */
4428static void update_automute_all(struct hda_codec *codec)
4429{
4430 call_hp_automute(codec, NULL);
4431 call_line_automute(codec, NULL);
4432 call_mic_autoswitch(codec, NULL);
4433}
4434
Takashi Iwai352f7f92012-12-19 12:52:06 +01004435/*
4436 * Auto-Mute mode mixer enum support
4437 */
4438static int automute_mode_info(struct snd_kcontrol *kcontrol,
4439 struct snd_ctl_elem_info *uinfo)
4440{
4441 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4442 struct hda_gen_spec *spec = codec->spec;
4443 static const char * const texts3[] = {
4444 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004445 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
Takashi Iwai352f7f92012-12-19 12:52:06 +01004447 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4448 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4449 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4450}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451
Takashi Iwai352f7f92012-12-19 12:52:06 +01004452static int automute_mode_get(struct snd_kcontrol *kcontrol,
4453 struct snd_ctl_elem_value *ucontrol)
4454{
4455 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4456 struct hda_gen_spec *spec = codec->spec;
4457 unsigned int val = 0;
4458 if (spec->automute_speaker)
4459 val++;
4460 if (spec->automute_lo)
4461 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004462
Takashi Iwai352f7f92012-12-19 12:52:06 +01004463 ucontrol->value.enumerated.item[0] = val;
4464 return 0;
4465}
4466
4467static int automute_mode_put(struct snd_kcontrol *kcontrol,
4468 struct snd_ctl_elem_value *ucontrol)
4469{
4470 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4471 struct hda_gen_spec *spec = codec->spec;
4472
4473 switch (ucontrol->value.enumerated.item[0]) {
4474 case 0:
4475 if (!spec->automute_speaker && !spec->automute_lo)
4476 return 0;
4477 spec->automute_speaker = 0;
4478 spec->automute_lo = 0;
4479 break;
4480 case 1:
4481 if (spec->automute_speaker_possible) {
4482 if (!spec->automute_lo && spec->automute_speaker)
4483 return 0;
4484 spec->automute_speaker = 1;
4485 spec->automute_lo = 0;
4486 } else if (spec->automute_lo_possible) {
4487 if (spec->automute_lo)
4488 return 0;
4489 spec->automute_lo = 1;
4490 } else
4491 return -EINVAL;
4492 break;
4493 case 2:
4494 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4495 return -EINVAL;
4496 if (spec->automute_speaker && spec->automute_lo)
4497 return 0;
4498 spec->automute_speaker = 1;
4499 spec->automute_lo = 1;
4500 break;
4501 default:
4502 return -EINVAL;
4503 }
4504 call_update_outputs(codec);
4505 return 1;
4506}
4507
4508static const struct snd_kcontrol_new automute_mode_enum = {
4509 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4510 .name = "Auto-Mute Mode",
4511 .info = automute_mode_info,
4512 .get = automute_mode_get,
4513 .put = automute_mode_put,
4514};
4515
4516static int add_automute_mode_enum(struct hda_codec *codec)
4517{
4518 struct hda_gen_spec *spec = codec->spec;
4519
Takashi Iwai12c93df2012-12-19 14:38:33 +01004520 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004521 return -ENOMEM;
4522 return 0;
4523}
4524
4525/*
4526 * Check the availability of HP/line-out auto-mute;
4527 * Set up appropriately if really supported
4528 */
4529static int check_auto_mute_availability(struct hda_codec *codec)
4530{
4531 struct hda_gen_spec *spec = codec->spec;
4532 struct auto_pin_cfg *cfg = &spec->autocfg;
4533 int present = 0;
4534 int i, err;
4535
Takashi Iwaif72706b2013-01-16 18:20:07 +01004536 if (spec->suppress_auto_mute)
4537 return 0;
4538
Takashi Iwai352f7f92012-12-19 12:52:06 +01004539 if (cfg->hp_pins[0])
4540 present++;
4541 if (cfg->line_out_pins[0])
4542 present++;
4543 if (cfg->speaker_pins[0])
4544 present++;
4545 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004546 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004547
4548 if (!cfg->speaker_pins[0] &&
4549 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4550 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4551 sizeof(cfg->speaker_pins));
4552 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
Takashi Iwai352f7f92012-12-19 12:52:06 +01004555 if (!cfg->hp_pins[0] &&
4556 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4557 memcpy(cfg->hp_pins, cfg->line_out_pins,
4558 sizeof(cfg->hp_pins));
4559 cfg->hp_outs = cfg->line_outs;
4560 }
4561
4562 for (i = 0; i < cfg->hp_outs; i++) {
4563 hda_nid_t nid = cfg->hp_pins[i];
4564 if (!is_jack_detectable(codec, nid))
4565 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004566 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai62f949b2014-09-11 14:06:53 +02004567 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004568 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004569 spec->detect_hp = 1;
4570 }
4571
4572 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4573 if (cfg->speaker_outs)
4574 for (i = 0; i < cfg->line_outs; i++) {
4575 hda_nid_t nid = cfg->line_out_pins[i];
4576 if (!is_jack_detectable(codec, nid))
4577 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004578 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004579 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004580 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004581 spec->detect_lo = 1;
4582 }
4583 spec->automute_lo_possible = spec->detect_hp;
4584 }
4585
4586 spec->automute_speaker_possible = cfg->speaker_outs &&
4587 (spec->detect_hp || spec->detect_lo);
4588
4589 spec->automute_lo = spec->automute_lo_possible;
4590 spec->automute_speaker = spec->automute_speaker_possible;
4591
4592 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4593 /* create a control for automute mode */
4594 err = add_automute_mode_enum(codec);
4595 if (err < 0)
4596 return err;
4597 }
4598 return 0;
4599}
4600
Takashi Iwai352f7f92012-12-19 12:52:06 +01004601/* check whether all auto-mic pins are valid; setup indices if OK */
4602static bool auto_mic_check_imux(struct hda_codec *codec)
4603{
4604 struct hda_gen_spec *spec = codec->spec;
4605 const struct hda_input_mux *imux;
4606 int i;
4607
4608 imux = &spec->input_mux;
4609 for (i = 0; i < spec->am_num_entries; i++) {
4610 spec->am_entry[i].idx =
4611 find_idx_in_nid_list(spec->am_entry[i].pin,
4612 spec->imux_pins, imux->num_items);
4613 if (spec->am_entry[i].idx < 0)
4614 return false; /* no corresponding imux */
4615 }
4616
4617 /* we don't need the jack detection for the first pin */
4618 for (i = 1; i < spec->am_num_entries; i++)
4619 snd_hda_jack_detect_enable_callback(codec,
4620 spec->am_entry[i].pin,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004621 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004622 return true;
4623}
4624
4625static int compare_attr(const void *ap, const void *bp)
4626{
4627 const struct automic_entry *a = ap;
4628 const struct automic_entry *b = bp;
4629 return (int)(a->attr - b->attr);
4630}
4631
4632/*
4633 * Check the availability of auto-mic switch;
4634 * Set up if really supported
4635 */
4636static int check_auto_mic_availability(struct hda_codec *codec)
4637{
4638 struct hda_gen_spec *spec = codec->spec;
4639 struct auto_pin_cfg *cfg = &spec->autocfg;
4640 unsigned int types;
4641 int i, num_pins;
4642
Takashi Iwaid12daf62013-01-07 16:32:11 +01004643 if (spec->suppress_auto_mic)
4644 return 0;
4645
Takashi Iwai352f7f92012-12-19 12:52:06 +01004646 types = 0;
4647 num_pins = 0;
4648 for (i = 0; i < cfg->num_inputs; i++) {
4649 hda_nid_t nid = cfg->inputs[i].pin;
4650 unsigned int attr;
4651 attr = snd_hda_codec_get_pincfg(codec, nid);
4652 attr = snd_hda_get_input_pin_attr(attr);
4653 if (types & (1 << attr))
4654 return 0; /* already occupied */
4655 switch (attr) {
4656 case INPUT_PIN_ATTR_INT:
4657 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4658 return 0; /* invalid type */
4659 break;
4660 case INPUT_PIN_ATTR_UNUSED:
4661 return 0; /* invalid entry */
4662 default:
4663 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4664 return 0; /* invalid type */
4665 if (!spec->line_in_auto_switch &&
4666 cfg->inputs[i].type != AUTO_PIN_MIC)
4667 return 0; /* only mic is allowed */
4668 if (!is_jack_detectable(codec, nid))
4669 return 0; /* no unsol support */
4670 break;
4671 }
4672 if (num_pins >= MAX_AUTO_MIC_PINS)
4673 return 0;
4674 types |= (1 << attr);
4675 spec->am_entry[num_pins].pin = nid;
4676 spec->am_entry[num_pins].attr = attr;
4677 num_pins++;
4678 }
4679
4680 if (num_pins < 2)
4681 return 0;
4682
4683 spec->am_num_entries = num_pins;
4684 /* sort the am_entry in the order of attr so that the pin with a
4685 * higher attr will be selected when the jack is plugged.
4686 */
4687 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4688 compare_attr, NULL);
4689
4690 if (!auto_mic_check_imux(codec))
4691 return 0;
4692
4693 spec->auto_mic = 1;
4694 spec->num_adc_nids = 1;
4695 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004696 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004697 spec->am_entry[0].pin,
4698 spec->am_entry[1].pin,
4699 spec->am_entry[2].pin);
4700
4701 return 0;
4702}
4703
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004704/**
4705 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4706 * into power down
4707 * @codec: the HDA codec
4708 * @nid: NID to evalute
4709 * @power_state: target power state
4710 */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004711unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004712 hda_nid_t nid,
4713 unsigned int power_state)
4714{
Takashi Iwaib6c09b32015-04-09 10:25:03 +02004715 struct hda_gen_spec *spec = codec->spec;
4716
4717 if (!spec->power_down_unused && !codec->power_save_node)
4718 return power_state;
Takashi Iwai7639a062015-03-03 10:07:24 +01004719 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004720 return power_state;
4721 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4722 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004723 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004724 return power_state;
4725 return AC_PWRST_D3;
4726}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004727EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004728
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004729/* mute all aamix inputs initially; parse up to the first leaves */
4730static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4731{
4732 int i, nums;
4733 const hda_nid_t *conn;
4734 bool has_amp;
4735
4736 nums = snd_hda_get_conn_list(codec, mix, &conn);
4737 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4738 for (i = 0; i < nums; i++) {
4739 if (has_amp)
Takashi Iwaief403ed2015-03-12 08:30:11 +01004740 update_amp(codec, mix, HDA_INPUT, i,
4741 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004742 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
Takashi Iwaief403ed2015-03-12 08:30:11 +01004743 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4744 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004745 }
4746}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004747
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004748/**
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004749 * snd_hda_gen_stream_pm - Stream power management callback
4750 * @codec: the HDA codec
4751 * @nid: audio widget
4752 * @on: power on/off flag
4753 *
Takashi Iwai967b1302015-03-20 18:21:03 +01004754 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004755 */
4756void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4757{
Takashi Iwai967b1302015-03-20 18:21:03 +01004758 if (codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004759 set_path_power(codec, nid, -1, on);
4760}
4761EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4762
4763/**
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004764 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4765 * set up the hda_gen_spec
4766 * @codec: the HDA codec
4767 * @cfg: Parsed pin configuration
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004768 *
4769 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004770 * or a negative error code
4771 */
4772int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004773 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004774{
4775 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004776 int err;
4777
Takashi Iwai1c70a582013-01-11 17:48:22 +01004778 parse_user_hints(codec);
4779
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004780 if (spec->mixer_nid && !spec->mixer_merge_nid)
4781 spec->mixer_merge_nid = spec->mixer_nid;
4782
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004783 if (cfg != &spec->autocfg) {
4784 spec->autocfg = *cfg;
4785 cfg = &spec->autocfg;
4786 }
4787
Takashi Iwai98bd1112013-03-22 14:53:50 +01004788 if (!spec->main_out_badness)
4789 spec->main_out_badness = &hda_main_out_badness;
4790 if (!spec->extra_out_badness)
4791 spec->extra_out_badness = &hda_extra_out_badness;
4792
David Henningsson6fc4cb92013-01-16 15:58:45 +01004793 fill_all_dac_nids(codec);
4794
Takashi Iwai352f7f92012-12-19 12:52:06 +01004795 if (!cfg->line_outs) {
4796 if (cfg->dig_outs || cfg->dig_in_pin) {
4797 spec->multiout.max_channels = 2;
4798 spec->no_analog = 1;
4799 goto dig_only;
4800 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004801 if (!cfg->num_inputs && !cfg->dig_in_pin)
4802 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004803 }
4804
4805 if (!spec->no_primary_hp &&
4806 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4807 cfg->line_outs <= cfg->hp_outs) {
4808 /* use HP as primary out */
4809 cfg->speaker_outs = cfg->line_outs;
4810 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4811 sizeof(cfg->speaker_pins));
4812 cfg->line_outs = cfg->hp_outs;
4813 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4814 cfg->hp_outs = 0;
4815 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4816 cfg->line_out_type = AUTO_PIN_HP_OUT;
4817 }
4818
4819 err = parse_output_paths(codec);
4820 if (err < 0)
4821 return err;
4822 err = create_multi_channel_mode(codec);
4823 if (err < 0)
4824 return err;
4825 err = create_multi_out_ctls(codec, cfg);
4826 if (err < 0)
4827 return err;
4828 err = create_hp_out_ctls(codec);
4829 if (err < 0)
4830 return err;
4831 err = create_speaker_out_ctls(codec);
4832 if (err < 0)
4833 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004834 err = create_indep_hp_ctls(codec);
4835 if (err < 0)
4836 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004837 err = create_loopback_mixing_ctl(codec);
4838 if (err < 0)
4839 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004840 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004841 if (err < 0)
4842 return err;
4843 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004844 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004845 return err;
4846
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004847 /* add power-down pin callbacks at first */
4848 add_all_pin_power_ctls(codec, false);
4849
Takashi Iwaia07a9492013-01-07 16:44:06 +01004850 spec->const_channel_count = spec->ext_channel_count;
4851 /* check the multiple speaker and headphone pins */
4852 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4853 spec->const_channel_count = max(spec->const_channel_count,
4854 cfg->speaker_outs * 2);
4855 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4856 spec->const_channel_count = max(spec->const_channel_count,
4857 cfg->hp_outs * 2);
4858 spec->multiout.max_channels = max(spec->ext_channel_count,
4859 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004860
4861 err = check_auto_mute_availability(codec);
4862 if (err < 0)
4863 return err;
4864
4865 err = check_dyn_adc_switch(codec);
4866 if (err < 0)
4867 return err;
4868
Takashi Iwai967303d2013-02-19 17:12:42 +01004869 err = check_auto_mic_availability(codec);
4870 if (err < 0)
4871 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004872
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004873 /* add stereo mix if available and not enabled yet */
4874 if (!spec->auto_mic && spec->mixer_nid &&
Takashi Iwai74f14b32014-12-15 13:43:59 +01004875 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4876 spec->input_mux.num_items > 1) {
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004877 err = parse_capture_source(codec, spec->mixer_nid,
4878 CFG_IDX_MIX, spec->num_all_adcs,
4879 "Stereo Mix", 0);
4880 if (err < 0)
4881 return err;
4882 }
4883
4884
Takashi Iwai352f7f92012-12-19 12:52:06 +01004885 err = create_capture_mixers(codec);
4886 if (err < 0)
4887 return err;
4888
4889 err = parse_mic_boost(codec);
4890 if (err < 0)
4891 return err;
4892
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004893 /* create "Headphone Mic Jack Mode" if no input selection is
4894 * available (or user specifies add_jack_modes hint)
4895 */
4896 if (spec->hp_mic_pin &&
4897 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4898 spec->add_jack_modes)) {
4899 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4900 if (err < 0)
4901 return err;
4902 }
4903
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004904 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004905 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4906 err = create_out_jack_modes(codec, cfg->line_outs,
4907 cfg->line_out_pins);
4908 if (err < 0)
4909 return err;
4910 }
4911 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4912 err = create_out_jack_modes(codec, cfg->hp_outs,
4913 cfg->hp_pins);
4914 if (err < 0)
4915 return err;
4916 }
4917 }
4918
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004919 /* add power-up pin callbacks at last */
4920 add_all_pin_power_ctls(codec, true);
4921
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004922 /* mute all aamix input initially */
4923 if (spec->mixer_nid)
4924 mute_all_mixer_nid(codec, spec->mixer_nid);
4925
Takashi Iwai352f7f92012-12-19 12:52:06 +01004926 dig_only:
4927 parse_digital(codec);
4928
Takashi Iwai49fb1892015-05-27 08:37:19 +02004929 if (spec->power_down_unused || codec->power_save_node) {
Takashi Iwai24fef902015-04-09 10:28:15 +02004930 if (!codec->power_filter)
4931 codec->power_filter = snd_hda_gen_path_power_filter;
Takashi Iwai49fb1892015-05-27 08:37:19 +02004932 if (!codec->patch_ops.stream_pm)
4933 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
4934 }
Takashi Iwai55196ff2013-01-24 17:32:56 +01004935
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004936 if (!spec->no_analog && spec->beep_nid) {
4937 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4938 if (err < 0)
4939 return err;
Takashi Iwai967b1302015-03-20 18:21:03 +01004940 if (codec->beep && codec->power_save_node) {
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004941 err = add_fake_beep_paths(codec);
4942 if (err < 0)
4943 return err;
4944 codec->beep->power_hook = beep_power_hook;
4945 }
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004946 }
4947
Takashi Iwai352f7f92012-12-19 12:52:06 +01004948 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004950EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
4952
4953/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004954 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004956
4957/* slave controls for virtual master */
4958static const char * const slave_pfxs[] = {
4959 "Front", "Surround", "Center", "LFE", "Side",
4960 "Headphone", "Speaker", "Mono", "Line Out",
4961 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004962 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4963 "Headphone Front", "Headphone Surround", "Headphone CLFE",
David Henningsson03ad6a82014-10-16 15:33:45 +02004964 "Headphone Side", "Headphone+LO", "Speaker+LO",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004965 NULL,
4966};
4967
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004968/**
4969 * snd_hda_gen_build_controls - Build controls from the parsed results
4970 * @codec: the HDA codec
4971 *
4972 * Pass this to build_controls patch_ops.
4973 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004974int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004976 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
Takashi Iwai36502d02012-12-19 15:15:10 +01004979 if (spec->kctls.used) {
4980 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4981 if (err < 0)
4982 return err;
4983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984
Takashi Iwai352f7f92012-12-19 12:52:06 +01004985 if (spec->multiout.dig_out_nid) {
4986 err = snd_hda_create_dig_out_ctls(codec,
4987 spec->multiout.dig_out_nid,
4988 spec->multiout.dig_out_nid,
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004989 spec->pcm_rec[1]->pcm_type);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004990 if (err < 0)
4991 return err;
4992 if (!spec->no_analog) {
4993 err = snd_hda_create_spdif_share_sw(codec,
4994 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 if (err < 0)
4996 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004997 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 }
4999 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005000 if (spec->dig_in_nid) {
5001 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5002 if (err < 0)
5003 return err;
5004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
Takashi Iwai352f7f92012-12-19 12:52:06 +01005006 /* if we have no master control, let's create it */
5007 if (!spec->no_analog &&
5008 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01005009 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01005010 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005011 "Playback Volume");
5012 if (err < 0)
5013 return err;
5014 }
5015 if (!spec->no_analog &&
5016 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5017 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5018 NULL, slave_pfxs,
5019 "Playback Switch",
5020 true, &spec->vmaster_mute.sw_kctl);
5021 if (err < 0)
5022 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02005023 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01005024 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5025 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02005026 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5027 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
Takashi Iwai352f7f92012-12-19 12:52:06 +01005030 free_kctls(spec); /* no longer needed */
5031
Takashi Iwai352f7f92012-12-19 12:52:06 +01005032 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5033 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 return err;
5035
5036 return 0;
5037}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005038EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005039
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040
5041/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01005042 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005045static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5046 struct hda_codec *codec,
5047 struct snd_pcm_substream *substream,
5048 int action)
5049{
5050 struct hda_gen_spec *spec = codec->spec;
5051 if (spec->pcm_playback_hook)
5052 spec->pcm_playback_hook(hinfo, codec, substream, action);
5053}
5054
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005055static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5056 struct hda_codec *codec,
5057 struct snd_pcm_substream *substream,
5058 int action)
5059{
5060 struct hda_gen_spec *spec = codec->spec;
5061 if (spec->pcm_capture_hook)
5062 spec->pcm_capture_hook(hinfo, codec, substream, action);
5063}
5064
Takashi Iwai352f7f92012-12-19 12:52:06 +01005065/*
5066 * Analog playback callbacks
5067 */
5068static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5069 struct hda_codec *codec,
5070 struct snd_pcm_substream *substream)
5071{
5072 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005073 int err;
5074
5075 mutex_lock(&spec->pcm_mutex);
5076 err = snd_hda_multi_out_analog_open(codec,
5077 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005078 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005079 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005080 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005081 call_pcm_playback_hook(hinfo, codec, substream,
5082 HDA_GEN_PCM_ACT_OPEN);
5083 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005084 mutex_unlock(&spec->pcm_mutex);
5085 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005086}
5087
5088static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01005089 struct hda_codec *codec,
5090 unsigned int stream_tag,
5091 unsigned int format,
5092 struct snd_pcm_substream *substream)
5093{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005094 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005095 int err;
5096
5097 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5098 stream_tag, format, substream);
5099 if (!err)
5100 call_pcm_playback_hook(hinfo, codec, substream,
5101 HDA_GEN_PCM_ACT_PREPARE);
5102 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005103}
Takashi Iwai97ec5582006-03-21 11:29:07 +01005104
Takashi Iwai352f7f92012-12-19 12:52:06 +01005105static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5106 struct hda_codec *codec,
5107 struct snd_pcm_substream *substream)
5108{
5109 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005110 int err;
5111
5112 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5113 if (!err)
5114 call_pcm_playback_hook(hinfo, codec, substream,
5115 HDA_GEN_PCM_ACT_CLEANUP);
5116 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005117}
5118
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005119static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5120 struct hda_codec *codec,
5121 struct snd_pcm_substream *substream)
5122{
5123 struct hda_gen_spec *spec = codec->spec;
5124 mutex_lock(&spec->pcm_mutex);
5125 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005126 call_pcm_playback_hook(hinfo, codec, substream,
5127 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005128 mutex_unlock(&spec->pcm_mutex);
5129 return 0;
5130}
5131
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005132static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5133 struct hda_codec *codec,
5134 struct snd_pcm_substream *substream)
5135{
5136 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5137 return 0;
5138}
5139
5140static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5141 struct hda_codec *codec,
5142 unsigned int stream_tag,
5143 unsigned int format,
5144 struct snd_pcm_substream *substream)
5145{
5146 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5147 call_pcm_capture_hook(hinfo, codec, substream,
5148 HDA_GEN_PCM_ACT_PREPARE);
5149 return 0;
5150}
5151
5152static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5153 struct hda_codec *codec,
5154 struct snd_pcm_substream *substream)
5155{
5156 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5157 call_pcm_capture_hook(hinfo, codec, substream,
5158 HDA_GEN_PCM_ACT_CLEANUP);
5159 return 0;
5160}
5161
5162static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5163 struct hda_codec *codec,
5164 struct snd_pcm_substream *substream)
5165{
5166 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5167 return 0;
5168}
5169
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005170static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5171 struct hda_codec *codec,
5172 struct snd_pcm_substream *substream)
5173{
5174 struct hda_gen_spec *spec = codec->spec;
5175 int err = 0;
5176
5177 mutex_lock(&spec->pcm_mutex);
5178 if (!spec->indep_hp_enabled)
5179 err = -EBUSY;
5180 else
5181 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005182 call_pcm_playback_hook(hinfo, codec, substream,
5183 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005184 mutex_unlock(&spec->pcm_mutex);
5185 return err;
5186}
5187
5188static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5189 struct hda_codec *codec,
5190 struct snd_pcm_substream *substream)
5191{
5192 struct hda_gen_spec *spec = codec->spec;
5193 mutex_lock(&spec->pcm_mutex);
5194 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005195 call_pcm_playback_hook(hinfo, codec, substream,
5196 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005197 mutex_unlock(&spec->pcm_mutex);
5198 return 0;
5199}
5200
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005201static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5202 struct hda_codec *codec,
5203 unsigned int stream_tag,
5204 unsigned int format,
5205 struct snd_pcm_substream *substream)
5206{
5207 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5208 call_pcm_playback_hook(hinfo, codec, substream,
5209 HDA_GEN_PCM_ACT_PREPARE);
5210 return 0;
5211}
5212
5213static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5214 struct hda_codec *codec,
5215 struct snd_pcm_substream *substream)
5216{
5217 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5218 call_pcm_playback_hook(hinfo, codec, substream,
5219 HDA_GEN_PCM_ACT_CLEANUP);
5220 return 0;
5221}
5222
Takashi Iwai352f7f92012-12-19 12:52:06 +01005223/*
5224 * Digital out
5225 */
5226static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5227 struct hda_codec *codec,
5228 struct snd_pcm_substream *substream)
5229{
5230 struct hda_gen_spec *spec = codec->spec;
5231 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5232}
5233
5234static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5235 struct hda_codec *codec,
5236 unsigned int stream_tag,
5237 unsigned int format,
5238 struct snd_pcm_substream *substream)
5239{
5240 struct hda_gen_spec *spec = codec->spec;
5241 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5242 stream_tag, format, substream);
5243}
5244
5245static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5246 struct hda_codec *codec,
5247 struct snd_pcm_substream *substream)
5248{
5249 struct hda_gen_spec *spec = codec->spec;
5250 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5251}
5252
5253static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5254 struct hda_codec *codec,
5255 struct snd_pcm_substream *substream)
5256{
5257 struct hda_gen_spec *spec = codec->spec;
5258 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5259}
5260
5261/*
5262 * Analog capture
5263 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005264#define alt_capture_pcm_open capture_pcm_open
5265#define alt_capture_pcm_close capture_pcm_close
5266
Takashi Iwai352f7f92012-12-19 12:52:06 +01005267static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5268 struct hda_codec *codec,
5269 unsigned int stream_tag,
5270 unsigned int format,
5271 struct snd_pcm_substream *substream)
5272{
5273 struct hda_gen_spec *spec = codec->spec;
5274
5275 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01005276 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005277 call_pcm_capture_hook(hinfo, codec, substream,
5278 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005279 return 0;
5280}
5281
Takashi Iwai352f7f92012-12-19 12:52:06 +01005282static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5283 struct hda_codec *codec,
5284 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01005285{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005286 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01005287
Takashi Iwai352f7f92012-12-19 12:52:06 +01005288 snd_hda_codec_cleanup_stream(codec,
5289 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005290 call_pcm_capture_hook(hinfo, codec, substream,
5291 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005292 return 0;
5293}
5294
Takashi Iwai352f7f92012-12-19 12:52:06 +01005295/*
5296 */
5297static const struct hda_pcm_stream pcm_analog_playback = {
5298 .substreams = 1,
5299 .channels_min = 2,
5300 .channels_max = 8,
5301 /* NID is set in build_pcms */
5302 .ops = {
5303 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005304 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005305 .prepare = playback_pcm_prepare,
5306 .cleanup = playback_pcm_cleanup
5307 },
5308};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309
Takashi Iwai352f7f92012-12-19 12:52:06 +01005310static const struct hda_pcm_stream pcm_analog_capture = {
5311 .substreams = 1,
5312 .channels_min = 2,
5313 .channels_max = 2,
5314 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005315 .ops = {
5316 .open = capture_pcm_open,
5317 .close = capture_pcm_close,
5318 .prepare = capture_pcm_prepare,
5319 .cleanup = capture_pcm_cleanup
5320 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005321};
5322
5323static const struct hda_pcm_stream pcm_analog_alt_playback = {
5324 .substreams = 1,
5325 .channels_min = 2,
5326 .channels_max = 2,
5327 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005328 .ops = {
5329 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005330 .close = alt_playback_pcm_close,
5331 .prepare = alt_playback_pcm_prepare,
5332 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005333 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005334};
5335
5336static const struct hda_pcm_stream pcm_analog_alt_capture = {
5337 .substreams = 2, /* can be overridden */
5338 .channels_min = 2,
5339 .channels_max = 2,
5340 /* NID is set in build_pcms */
5341 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005342 .open = alt_capture_pcm_open,
5343 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005344 .prepare = alt_capture_pcm_prepare,
5345 .cleanup = alt_capture_pcm_cleanup
5346 },
5347};
5348
5349static const struct hda_pcm_stream pcm_digital_playback = {
5350 .substreams = 1,
5351 .channels_min = 2,
5352 .channels_max = 2,
5353 /* NID is set in build_pcms */
5354 .ops = {
5355 .open = dig_playback_pcm_open,
5356 .close = dig_playback_pcm_close,
5357 .prepare = dig_playback_pcm_prepare,
5358 .cleanup = dig_playback_pcm_cleanup
5359 },
5360};
5361
5362static const struct hda_pcm_stream pcm_digital_capture = {
5363 .substreams = 1,
5364 .channels_min = 2,
5365 .channels_max = 2,
5366 /* NID is set in build_pcms */
5367};
5368
5369/* Used by build_pcms to flag that a PCM has no playback stream */
5370static const struct hda_pcm_stream pcm_null_stream = {
5371 .substreams = 0,
5372 .channels_min = 0,
5373 .channels_max = 0,
5374};
5375
5376/*
5377 * dynamic changing ADC PCM streams
5378 */
5379static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5380{
5381 struct hda_gen_spec *spec = codec->spec;
5382 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5383
5384 if (spec->cur_adc && spec->cur_adc != new_adc) {
5385 /* stream is running, let's swap the current ADC */
5386 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5387 spec->cur_adc = new_adc;
5388 snd_hda_codec_setup_stream(codec, new_adc,
5389 spec->cur_adc_stream_tag, 0,
5390 spec->cur_adc_format);
5391 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005393 return false;
5394}
5395
5396/* analog capture with dynamic dual-adc changes */
5397static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5398 struct hda_codec *codec,
5399 unsigned int stream_tag,
5400 unsigned int format,
5401 struct snd_pcm_substream *substream)
5402{
5403 struct hda_gen_spec *spec = codec->spec;
5404 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5405 spec->cur_adc_stream_tag = stream_tag;
5406 spec->cur_adc_format = format;
5407 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5408 return 0;
5409}
5410
5411static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5412 struct hda_codec *codec,
5413 struct snd_pcm_substream *substream)
5414{
5415 struct hda_gen_spec *spec = codec->spec;
5416 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5417 spec->cur_adc = 0;
5418 return 0;
5419}
5420
5421static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5422 .substreams = 1,
5423 .channels_min = 2,
5424 .channels_max = 2,
5425 .nid = 0, /* fill later */
5426 .ops = {
5427 .prepare = dyn_adc_capture_pcm_prepare,
5428 .cleanup = dyn_adc_capture_pcm_cleanup
5429 },
5430};
5431
Takashi Iwaif873e532012-12-20 16:58:39 +01005432static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5433 const char *chip_name)
5434{
5435 char *p;
5436
5437 if (*str)
5438 return;
5439 strlcpy(str, chip_name, len);
5440
5441 /* drop non-alnum chars after a space */
5442 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5443 if (!isalnum(p[1])) {
5444 *p = 0;
5445 break;
5446 }
5447 }
5448 strlcat(str, sfx, len);
5449}
5450
Takashi Iwaifb83b632015-03-16 23:34:34 +01005451/* copy PCM stream info from @default_str, and override non-NULL entries
5452 * from @spec_str and @nid
5453 */
5454static void setup_pcm_stream(struct hda_pcm_stream *str,
5455 const struct hda_pcm_stream *default_str,
5456 const struct hda_pcm_stream *spec_str,
5457 hda_nid_t nid)
5458{
5459 *str = *default_str;
5460 if (nid)
5461 str->nid = nid;
5462 if (spec_str) {
5463 if (spec_str->substreams)
5464 str->substreams = spec_str->substreams;
5465 if (spec_str->channels_min)
5466 str->channels_min = spec_str->channels_min;
5467 if (spec_str->channels_max)
5468 str->channels_max = spec_str->channels_max;
5469 if (spec_str->rates)
5470 str->rates = spec_str->rates;
5471 if (spec_str->formats)
5472 str->formats = spec_str->formats;
5473 if (spec_str->maxbps)
5474 str->maxbps = spec_str->maxbps;
5475 }
5476}
5477
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005478/**
5479 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5480 * @codec: the HDA codec
5481 *
5482 * Pass this to build_pcms patch_ops.
5483 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005484int snd_hda_gen_build_pcms(struct hda_codec *codec)
5485{
5486 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005487 struct hda_pcm *info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005488 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
Takashi Iwai352f7f92012-12-19 12:52:06 +01005490 if (spec->no_analog)
5491 goto skip_analog;
5492
Takashi Iwaif873e532012-12-20 16:58:39 +01005493 fill_pcm_stream_name(spec->stream_name_analog,
5494 sizeof(spec->stream_name_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005495 " Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005496 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5497 if (!info)
5498 return -ENOMEM;
5499 spec->pcm_rec[0] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005500
5501 if (spec->multiout.num_dacs > 0) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005502 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5503 &pcm_analog_playback,
5504 spec->stream_analog_playback,
5505 spec->multiout.dac_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005506 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5507 spec->multiout.max_channels;
5508 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5509 spec->autocfg.line_outs == 2)
5510 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5511 snd_pcm_2_1_chmaps;
5512 }
5513 if (spec->num_adc_nids) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005514 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5515 (spec->dyn_adc_switch ?
5516 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5517 spec->stream_analog_capture,
5518 spec->adc_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005519 }
5520
Takashi Iwai352f7f92012-12-19 12:52:06 +01005521 skip_analog:
5522 /* SPDIF for stream index #1 */
5523 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005524 fill_pcm_stream_name(spec->stream_name_digital,
5525 sizeof(spec->stream_name_digital),
Takashi Iwai7639a062015-03-03 10:07:24 +01005526 " Digital", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005527 info = snd_hda_codec_pcm_new(codec, "%s",
5528 spec->stream_name_digital);
5529 if (!info)
5530 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005531 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005532 spec->pcm_rec[1] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005533 if (spec->dig_out_type)
5534 info->pcm_type = spec->dig_out_type;
5535 else
5536 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005537 if (spec->multiout.dig_out_nid)
5538 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5539 &pcm_digital_playback,
5540 spec->stream_digital_playback,
5541 spec->multiout.dig_out_nid);
5542 if (spec->dig_in_nid)
5543 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5544 &pcm_digital_capture,
5545 spec->stream_digital_capture,
5546 spec->dig_in_nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005547 }
5548
5549 if (spec->no_analog)
5550 return 0;
5551
5552 /* If the use of more than one ADC is requested for the current
5553 * model, configure a second analog capture-only PCM.
5554 */
5555 have_multi_adcs = (spec->num_adc_nids > 1) &&
5556 !spec->dyn_adc_switch && !spec->auto_mic;
5557 /* Additional Analaog capture for index #2 */
5558 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005559 fill_pcm_stream_name(spec->stream_name_alt_analog,
5560 sizeof(spec->stream_name_alt_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005561 " Alt Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005562 info = snd_hda_codec_pcm_new(codec, "%s",
5563 spec->stream_name_alt_analog);
5564 if (!info)
5565 return -ENOMEM;
5566 spec->pcm_rec[2] = info;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005567 if (spec->alt_dac_nid)
5568 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5569 &pcm_analog_alt_playback,
5570 spec->stream_analog_alt_playback,
5571 spec->alt_dac_nid);
5572 else
5573 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5574 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005575 if (have_multi_adcs) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005576 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5577 &pcm_analog_alt_capture,
5578 spec->stream_analog_alt_capture,
5579 spec->adc_nids[1]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005580 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5581 spec->num_adc_nids - 1;
5582 } else {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005583 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5584 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 }
5587
5588 return 0;
5589}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005590EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005591
5592
5593/*
5594 * Standard auto-parser initializations
5595 */
5596
Takashi Iwaid4156932013-01-07 10:08:02 +01005597/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005598static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005599{
5600 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005601 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005602
Takashi Iwai196c17662013-01-04 15:01:40 +01005603 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005604 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005605 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005606 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005607 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005608 snd_hda_activate_path(codec, path, path->active,
5609 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005610 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005611}
5612
5613/* initialize primary output paths */
5614static void init_multi_out(struct hda_codec *codec)
5615{
5616 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005617 int i;
5618
Takashi Iwaid4156932013-01-07 10:08:02 +01005619 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005620 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005621}
5622
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005623
Takashi Iwai2c12c302013-01-10 09:33:29 +01005624static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005625{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005626 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005627
Takashi Iwaid4156932013-01-07 10:08:02 +01005628 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005629 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005630}
5631
5632/* initialize hp and speaker paths */
5633static void init_extra_out(struct hda_codec *codec)
5634{
5635 struct hda_gen_spec *spec = codec->spec;
5636
5637 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005638 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005639 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5640 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005641 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005642}
5643
5644/* initialize multi-io paths */
5645static void init_multi_io(struct hda_codec *codec)
5646{
5647 struct hda_gen_spec *spec = codec->spec;
5648 int i;
5649
5650 for (i = 0; i < spec->multi_ios; i++) {
5651 hda_nid_t pin = spec->multi_io[i].pin;
5652 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005653 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005654 if (!path)
5655 continue;
5656 if (!spec->multi_io[i].ctl_in)
5657 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005658 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005659 snd_hda_activate_path(codec, path, path->active,
5660 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005661 }
5662}
5663
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005664static void init_aamix_paths(struct hda_codec *codec)
5665{
5666 struct hda_gen_spec *spec = codec->spec;
5667
5668 if (!spec->have_aamix_ctl)
5669 return;
5670 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5671 spec->aamix_out_paths[0],
5672 spec->autocfg.line_out_type);
5673 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5674 spec->aamix_out_paths[1],
5675 AUTO_PIN_HP_OUT);
5676 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5677 spec->aamix_out_paths[2],
5678 AUTO_PIN_SPEAKER_OUT);
5679}
5680
Takashi Iwai352f7f92012-12-19 12:52:06 +01005681/* set up input pins and loopback paths */
5682static void init_analog_input(struct hda_codec *codec)
5683{
5684 struct hda_gen_spec *spec = codec->spec;
5685 struct auto_pin_cfg *cfg = &spec->autocfg;
5686 int i;
5687
5688 for (i = 0; i < cfg->num_inputs; i++) {
5689 hda_nid_t nid = cfg->inputs[i].pin;
5690 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005691 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005692
5693 /* init loopback inputs */
5694 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005695 resume_path_from_idx(codec, spec->loopback_paths[i]);
5696 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005697 }
5698 }
5699}
5700
5701/* initialize ADC paths */
5702static void init_input_src(struct hda_codec *codec)
5703{
5704 struct hda_gen_spec *spec = codec->spec;
5705 struct hda_input_mux *imux = &spec->input_mux;
5706 struct nid_path *path;
5707 int i, c, nums;
5708
5709 if (spec->dyn_adc_switch)
5710 nums = 1;
5711 else
5712 nums = spec->num_adc_nids;
5713
5714 for (c = 0; c < nums; c++) {
5715 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005716 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005717 if (path) {
5718 bool active = path->active;
5719 if (i == spec->cur_mux[c])
5720 active = true;
5721 snd_hda_activate_path(codec, path, active, false);
5722 }
5723 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005724 if (spec->hp_mic)
5725 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005726 }
5727
Takashi Iwai352f7f92012-12-19 12:52:06 +01005728 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005729 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005730}
5731
5732/* set right pin controls for digital I/O */
5733static void init_digital(struct hda_codec *codec)
5734{
5735 struct hda_gen_spec *spec = codec->spec;
5736 int i;
5737 hda_nid_t pin;
5738
Takashi Iwaid4156932013-01-07 10:08:02 +01005739 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005740 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005741 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005742 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005743 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005744 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005745 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005746}
5747
Takashi Iwai973e4972012-12-20 15:16:09 +01005748/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5749 * invalid unsol tags by some reason
5750 */
5751static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5752{
5753 int i;
5754
5755 for (i = 0; i < codec->init_pins.used; i++) {
5756 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5757 hda_nid_t nid = pin->nid;
5758 if (is_jack_detectable(codec, nid) &&
5759 !snd_hda_jack_tbl_get(codec, nid))
5760 snd_hda_codec_update_cache(codec, nid, 0,
5761 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5762 }
5763}
5764
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005765/**
5766 * snd_hda_gen_init - initialize the generic spec
5767 * @codec: the HDA codec
5768 *
5769 * This can be put as patch_ops init function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005770 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005771int snd_hda_gen_init(struct hda_codec *codec)
5772{
5773 struct hda_gen_spec *spec = codec->spec;
5774
5775 if (spec->init_hook)
5776 spec->init_hook(codec);
5777
5778 snd_hda_apply_verbs(codec);
5779
5780 init_multi_out(codec);
5781 init_extra_out(codec);
5782 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005783 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005784 init_analog_input(codec);
5785 init_input_src(codec);
5786 init_digital(codec);
5787
Takashi Iwai973e4972012-12-20 15:16:09 +01005788 clear_unsol_on_unused_pins(codec);
5789
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01005790 sync_all_pin_power_ctls(codec);
5791
Takashi Iwai352f7f92012-12-19 12:52:06 +01005792 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005793 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005794
Takashi Iwaia551d912015-02-26 12:34:49 +01005795 regcache_sync(codec->core.regmap);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005796
Takashi Iwai352f7f92012-12-19 12:52:06 +01005797 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5798 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5799
5800 hda_call_check_power_status(codec, 0x01);
5801 return 0;
5802}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005803EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005804
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005805/**
5806 * snd_hda_gen_free - free the generic spec
5807 * @codec: the HDA codec
5808 *
5809 * This can be put as patch_ops free function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005810 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005811void snd_hda_gen_free(struct hda_codec *codec)
5812{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01005813 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005814 snd_hda_gen_spec_free(codec->spec);
5815 kfree(codec->spec);
5816 codec->spec = NULL;
5817}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005818EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005819
5820#ifdef CONFIG_PM
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005821/**
5822 * snd_hda_gen_check_power_status - check the loopback power save state
5823 * @codec: the HDA codec
5824 * @nid: NID to inspect
5825 *
5826 * This can be put as patch_ops check_power_status function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005827 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005828int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5829{
5830 struct hda_gen_spec *spec = codec->spec;
5831 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5832}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005833EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005834#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005835
5836
5837/*
5838 * the generic codec support
5839 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840
Takashi Iwai352f7f92012-12-19 12:52:06 +01005841static const struct hda_codec_ops generic_patch_ops = {
5842 .build_controls = snd_hda_gen_build_controls,
5843 .build_pcms = snd_hda_gen_build_pcms,
5844 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005845 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005846 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005847#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005848 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005849#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850};
5851
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005852/*
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005853 * snd_hda_parse_generic_codec - Generic codec parser
5854 * @codec: the HDA codec
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005855 */
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005856static int snd_hda_parse_generic_codec(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005858 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 int err;
5860
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005861 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005862 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005864 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005867 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5868 if (err < 0)
5869 return err;
5870
5871 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005872 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 goto error;
5874
5875 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 return 0;
5877
Takashi Iwai352f7f92012-12-19 12:52:06 +01005878error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005879 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 return err;
5881}
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005882
5883static const struct hda_codec_preset snd_hda_preset_generic[] = {
5884 { .id = HDA_CODEC_ID_GENERIC, .patch = snd_hda_parse_generic_codec },
5885 {} /* terminator */
5886};
5887
5888static struct hda_codec_driver generic_driver = {
5889 .preset = snd_hda_preset_generic,
5890};
5891
5892module_hda_codec_driver(generic_driver);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01005893
5894MODULE_LICENSE("GPL");
5895MODULE_DESCRIPTION("Generic HD-audio codec parser");