blob: 1f2ca7be146860d015608ad6dea1af8938d3a8a1 [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 Iwai7639a062015-03-03 10:07:24 +0100835 if (nid == codec->core.afg)
Takashi Iwai5ccf8352015-03-18 09:23:10 +0100836 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100837 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
838 state = AC_PWRST_D0;
839 else
840 state = AC_PWRST_D3;
841 if (!snd_hda_check_power_state(codec, nid, state)) {
842 snd_hda_codec_write(codec, nid, 0,
843 AC_VERB_SET_POWER_STATE, state);
844 changed = nid;
Takashi Iwaid545a572015-04-04 12:17:28 +0200845 if (state == AC_PWRST_D0)
846 snd_hdac_regmap_sync_node(&codec->core, nid);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100847 }
848 }
849 return changed;
850}
851
852/* do sync with the last power state change */
853static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
854{
855 if (nid) {
856 msleep(10);
857 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
858 }
859}
860
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100861/**
862 * snd_hda_activate_path - activate or deactivate the given path
863 * @codec: the HDA codec
864 * @path: the path to activate/deactivate
865 * @enable: flag to activate or not
866 * @add_aamix: enable the input from aamix NID
867 *
868 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100870void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
871 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100873 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100874 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Takashi Iwai352f7f92012-12-19 12:52:06 +0100876 if (!enable)
877 path->active = false;
878
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100879 /* make sure the widget is powered up */
Takashi Iwai967b1302015-03-20 18:21:03 +0100880 if (enable && (spec->power_down_unused || codec->power_save_node))
881 path_power_update(codec, path, codec->power_save_node);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100882
Takashi Iwai352f7f92012-12-19 12:52:06 +0100883 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100884 hda_nid_t nid = path->path[i];
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100885
Takashi Iwai352f7f92012-12-19 12:52:06 +0100886 if (enable && path->multi[i])
Takashi Iwai8f0972d2014-01-27 16:26:16 +0100887 snd_hda_codec_update_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100888 AC_VERB_SET_CONNECT_SEL,
889 path->idx[i]);
890 if (has_amp_in(codec, path, i))
891 activate_amp_in(codec, path, i, enable, add_aamix);
892 if (has_amp_out(codec, path, i))
893 activate_amp_out(codec, path, i, enable);
894 }
895
896 if (enable)
897 path->active = true;
898}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100899EXPORT_SYMBOL_GPL(snd_hda_activate_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100900
Takashi Iwai55196ff2013-01-24 17:32:56 +0100901/* if the given path is inactive, put widgets into D3 (only if suitable) */
902static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
903{
904 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100905
Takashi Iwai967b1302015-03-20 18:21:03 +0100906 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
Takashi Iwai55196ff2013-01-24 17:32:56 +0100907 return;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100908 sync_power_state_change(codec, path_power_update(codec, path, true));
Takashi Iwai55196ff2013-01-24 17:32:56 +0100909}
910
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100911/* turn on/off EAPD on the given pin */
912static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
913{
914 struct hda_gen_spec *spec = codec->spec;
915 if (spec->own_eapd_ctl ||
916 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
917 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200918 if (spec->keep_eapd_on && !enable)
919 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100920 if (codec->inv_eapd)
921 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100922 snd_hda_codec_update_cache(codec, pin, 0,
923 AC_VERB_SET_EAPD_BTLENABLE,
924 enable ? 0x02 : 0x00);
925}
926
Takashi Iwai3e367f12013-01-23 17:07:23 +0100927/* re-initialize the path specified by the given path index */
928static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
929{
930 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
931 if (path)
932 snd_hda_activate_path(codec, path, path->active, false);
933}
934
Takashi Iwai352f7f92012-12-19 12:52:06 +0100935
936/*
937 * Helper functions for creating mixer ctl elements
938 */
939
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200940static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
941 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200942static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
943 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200944
Takashi Iwai352f7f92012-12-19 12:52:06 +0100945enum {
946 HDA_CTL_WIDGET_VOL,
947 HDA_CTL_WIDGET_MUTE,
948 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100949};
950static const struct snd_kcontrol_new control_templates[] = {
951 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200952 /* only the put callback is replaced for handling the special mute */
953 {
954 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
955 .subdevice = HDA_SUBDEV_AMP_FLAG,
956 .info = snd_hda_mixer_amp_switch_info,
957 .get = snd_hda_mixer_amp_switch_get,
958 .put = hda_gen_mixer_mute_put, /* replaced */
959 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
960 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200961 {
962 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
963 .info = snd_hda_mixer_amp_switch_info,
964 .get = snd_hda_mixer_bind_switch_get,
965 .put = hda_gen_bind_mute_put, /* replaced */
966 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
967 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100968};
969
970/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100971static struct snd_kcontrol_new *
972add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100973 int cidx, unsigned long val)
974{
975 struct snd_kcontrol_new *knew;
976
Takashi Iwai12c93df2012-12-19 14:38:33 +0100977 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100978 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100979 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100980 knew->index = cidx;
981 if (get_amp_nid_(val))
982 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
983 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100984 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100985}
986
987static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
988 const char *pfx, const char *dir,
989 const char *sfx, int cidx, unsigned long val)
990{
Takashi Iwai975cc022013-06-28 11:56:49 +0200991 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100992 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100993 if (!add_control(spec, type, name, cidx, val))
994 return -ENOMEM;
995 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100996}
997
998#define add_pb_vol_ctrl(spec, type, pfx, val) \
999 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1000#define add_pb_sw_ctrl(spec, type, pfx, val) \
1001 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1002#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1003 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1004#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1005 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1006
1007static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1008 unsigned int chs, struct nid_path *path)
1009{
1010 unsigned int val;
1011 if (!path)
1012 return 0;
1013 val = path->ctls[NID_PATH_VOL_CTL];
1014 if (!val)
1015 return 0;
1016 val = amp_val_replace_channels(val, chs);
1017 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1018}
1019
1020/* return the channel bits suitable for the given path->ctls[] */
1021static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1022 int type)
1023{
1024 int chs = 1; /* mono (left only) */
1025 if (path) {
1026 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1027 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1028 chs = 3; /* stereo */
1029 }
1030 return chs;
1031}
1032
1033static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1034 struct nid_path *path)
1035{
1036 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1037 return add_vol_ctl(codec, pfx, cidx, chs, path);
1038}
1039
1040/* create a mute-switch for the given mixer widget;
1041 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1042 */
1043static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1044 unsigned int chs, struct nid_path *path)
1045{
1046 unsigned int val;
1047 int type = HDA_CTL_WIDGET_MUTE;
1048
1049 if (!path)
1050 return 0;
1051 val = path->ctls[NID_PATH_MUTE_CTL];
1052 if (!val)
1053 return 0;
1054 val = amp_val_replace_channels(val, chs);
1055 if (get_amp_direction_(val) == HDA_INPUT) {
1056 hda_nid_t nid = get_amp_nid_(val);
1057 int nums = snd_hda_get_num_conns(codec, nid);
1058 if (nums > 1) {
1059 type = HDA_CTL_BIND_MUTE;
1060 val |= nums << 19;
1061 }
1062 }
1063 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1064}
1065
1066static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1067 int cidx, struct nid_path *path)
1068{
1069 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1070 return add_sw_ctl(codec, pfx, cidx, chs, path);
1071}
1072
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001073/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001074static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1075 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001076{
1077 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1078 struct hda_gen_spec *spec = codec->spec;
1079
1080 if (spec->auto_mute_via_amp) {
1081 hda_nid_t nid = get_amp_nid(kcontrol);
1082 bool enabled = !((spec->mute_bits >> nid) & 1);
1083 ucontrol->value.integer.value[0] &= enabled;
1084 ucontrol->value.integer.value[1] &= enabled;
1085 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001086}
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001087
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001088static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1089 struct snd_ctl_elem_value *ucontrol)
1090{
1091 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001092 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1093}
1094
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001095static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1096 struct snd_ctl_elem_value *ucontrol)
1097{
1098 sync_auto_mute_bits(kcontrol, ucontrol);
1099 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1100}
1101
Takashi Iwai247d85e2013-01-17 16:18:11 +01001102/* any ctl assigned to the path with the given index? */
1103static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1104{
1105 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1106 return path && path->ctls[ctl_type];
1107}
1108
Takashi Iwai352f7f92012-12-19 12:52:06 +01001109static const char * const channel_name[4] = {
1110 "Front", "Surround", "CLFE", "Side"
1111};
1112
1113/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +01001114static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1115 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001116{
Takashi Iwai247d85e2013-01-17 16:18:11 +01001117 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001118 struct auto_pin_cfg *cfg = &spec->autocfg;
1119
1120 *index = 0;
1121 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001122 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001123 return spec->vmaster_mute.hook ? "PCM" : "Master";
1124
1125 /* if there is really a single DAC used in the whole output paths,
1126 * use it master (or "PCM" if a vmaster hook is present)
1127 */
1128 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1129 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1130 return spec->vmaster_mute.hook ? "PCM" : "Master";
1131
Takashi Iwai247d85e2013-01-17 16:18:11 +01001132 /* multi-io channels */
1133 if (ch >= cfg->line_outs)
1134 return channel_name[ch];
1135
Takashi Iwai352f7f92012-12-19 12:52:06 +01001136 switch (cfg->line_out_type) {
1137 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001138 /* if the primary channel vol/mute is shared with HP volume,
1139 * don't name it as Speaker
1140 */
1141 if (!ch && cfg->hp_outs &&
1142 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1143 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001144 if (cfg->line_outs == 1)
1145 return "Speaker";
1146 if (cfg->line_outs == 2)
1147 return ch ? "Bass Speaker" : "Speaker";
1148 break;
1149 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001150 /* if the primary channel vol/mute is shared with spk volume,
1151 * don't name it as Headphone
1152 */
1153 if (!ch && cfg->speaker_outs &&
1154 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1155 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001156 /* for multi-io case, only the primary out */
1157 if (ch && spec->multi_ios)
1158 break;
1159 *index = ch;
1160 return "Headphone";
David Henningsson03ad6a82014-10-16 15:33:45 +02001161 case AUTO_PIN_LINE_OUT:
1162 /* This deals with the case where we have two DACs and
1163 * one LO, one HP and one Speaker */
1164 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1165 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1166 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1167 if (hp_lo_shared && spk_lo_shared)
1168 return spec->vmaster_mute.hook ? "PCM" : "Master";
1169 if (hp_lo_shared)
1170 return "Headphone+LO";
1171 if (spk_lo_shared)
1172 return "Speaker+LO";
1173 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001174 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001175
1176 /* for a single channel output, we don't have to name the channel */
1177 if (cfg->line_outs == 1 && !spec->multi_ios)
David Henningsson3abb4f42014-10-16 15:33:46 +02001178 return "Line Out";
Takashi Iwai247d85e2013-01-17 16:18:11 +01001179
Takashi Iwai352f7f92012-12-19 12:52:06 +01001180 if (ch >= ARRAY_SIZE(channel_name)) {
1181 snd_BUG();
1182 return "PCM";
1183 }
1184
1185 return channel_name[ch];
1186}
1187
1188/*
1189 * Parse output paths
1190 */
1191
1192/* badness definition */
1193enum {
1194 /* No primary DAC is found for the main output */
1195 BAD_NO_PRIMARY_DAC = 0x10000,
1196 /* No DAC is found for the extra output */
1197 BAD_NO_DAC = 0x4000,
1198 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001199 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001200 /* No individual DAC for extra output */
1201 BAD_NO_EXTRA_DAC = 0x102,
1202 /* No individual DAC for extra surrounds */
1203 BAD_NO_EXTRA_SURR_DAC = 0x101,
1204 /* Primary DAC shared with main surrounds */
1205 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001206 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001207 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001208 /* Primary DAC shared with main CLFE */
1209 BAD_SHARED_CLFE = 0x10,
1210 /* Primary DAC shared with extra surrounds */
1211 BAD_SHARED_EXTRA_SURROUND = 0x10,
1212 /* Volume widget is shared */
1213 BAD_SHARED_VOL = 0x10,
1214};
1215
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001216/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001217 * volume and mute controls, and assign the values to ctls[].
1218 *
1219 * When no appropriate widget is found in the path, the badness value
1220 * is incremented depending on the situation. The function returns the
1221 * total badness for both volume and mute controls.
1222 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001223static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001224{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001225 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001226 hda_nid_t nid;
1227 unsigned int val;
1228 int badness = 0;
1229
1230 if (!path)
1231 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001232
1233 if (path->ctls[NID_PATH_VOL_CTL] ||
1234 path->ctls[NID_PATH_MUTE_CTL])
1235 return 0; /* already evaluated */
1236
Takashi Iwai352f7f92012-12-19 12:52:06 +01001237 nid = look_for_out_vol_nid(codec, path);
1238 if (nid) {
1239 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001240 if (spec->dac_min_mute)
1241 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001242 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1243 badness += BAD_SHARED_VOL;
1244 else
1245 path->ctls[NID_PATH_VOL_CTL] = val;
1246 } else
1247 badness += BAD_SHARED_VOL;
1248 nid = look_for_out_mute_nid(codec, path);
1249 if (nid) {
1250 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1251 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1252 nid_has_mute(codec, nid, HDA_OUTPUT))
1253 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1254 else
1255 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1256 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1257 badness += BAD_SHARED_VOL;
1258 else
1259 path->ctls[NID_PATH_MUTE_CTL] = val;
1260 } else
1261 badness += BAD_SHARED_VOL;
1262 return badness;
1263}
1264
Takashi Iwai98bd1112013-03-22 14:53:50 +01001265const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001266 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1267 .no_dac = BAD_NO_DAC,
1268 .shared_primary = BAD_NO_PRIMARY_DAC,
1269 .shared_surr = BAD_SHARED_SURROUND,
1270 .shared_clfe = BAD_SHARED_CLFE,
1271 .shared_surr_main = BAD_SHARED_SURROUND,
1272};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001273EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001274
Takashi Iwai98bd1112013-03-22 14:53:50 +01001275const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001276 .no_primary_dac = BAD_NO_DAC,
1277 .no_dac = BAD_NO_DAC,
1278 .shared_primary = BAD_NO_EXTRA_DAC,
1279 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1280 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1281 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1282};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001283EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001284
Takashi Iwai7385df62013-01-07 09:50:52 +01001285/* get the DAC of the primary output corresponding to the given array index */
1286static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1287{
1288 struct hda_gen_spec *spec = codec->spec;
1289 struct auto_pin_cfg *cfg = &spec->autocfg;
1290
1291 if (cfg->line_outs > idx)
1292 return spec->private_dac_nids[idx];
1293 idx -= cfg->line_outs;
1294 if (spec->multi_ios > idx)
1295 return spec->multi_io[idx].dac;
1296 return 0;
1297}
1298
1299/* return the DAC if it's reachable, otherwise zero */
1300static inline hda_nid_t try_dac(struct hda_codec *codec,
1301 hda_nid_t dac, hda_nid_t pin)
1302{
1303 return is_reachable_path(codec, dac, pin) ? dac : 0;
1304}
1305
Takashi Iwai352f7f92012-12-19 12:52:06 +01001306/* try to assign DACs to pins and return the resultant badness */
1307static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1308 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001309 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001310 const struct badness_table *bad)
1311{
1312 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001313 int i, j;
1314 int badness = 0;
1315 hda_nid_t dac;
1316
1317 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return 0;
1319
Takashi Iwai352f7f92012-12-19 12:52:06 +01001320 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001321 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001322 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001323
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001324 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1325 if (path) {
1326 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001327 continue;
1328 }
1329
Takashi Iwai36907392013-12-10 17:29:26 +01001330 dacs[i] = get_preferred_dac(codec, pin);
1331 if (dacs[i]) {
1332 if (is_dac_already_used(codec, dacs[i]))
1333 badness += bad->shared_primary;
1334 }
1335
1336 if (!dacs[i])
1337 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001338 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001339 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001340 for (j = 1; j < num_outs; j++) {
1341 if (is_reachable_path(codec, dacs[j], pin)) {
1342 dacs[0] = dacs[j];
1343 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001344 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001345 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001346 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001349 }
1350 dac = dacs[i];
1351 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001352 if (num_outs > 2)
1353 dac = try_dac(codec, get_primary_out(codec, i), pin);
1354 if (!dac)
1355 dac = try_dac(codec, dacs[0], pin);
1356 if (!dac)
1357 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001358 if (dac) {
1359 if (!i)
1360 badness += bad->shared_primary;
1361 else if (i == 1)
1362 badness += bad->shared_surr;
1363 else
1364 badness += bad->shared_clfe;
1365 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1366 dac = spec->private_dac_nids[0];
1367 badness += bad->shared_surr_main;
1368 } else if (!i)
1369 badness += bad->no_primary_dac;
1370 else
1371 badness += bad->no_dac;
1372 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001373 if (!dac)
1374 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001375 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001376 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001377 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001378 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001379 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001380 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001381 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001382 badness += bad->no_dac;
1383 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001384 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001385 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001386 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001387 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001388 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001389 }
1390
1391 return badness;
1392}
1393
1394/* return NID if the given pin has only a single connection to a certain DAC */
1395static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1396{
1397 struct hda_gen_spec *spec = codec->spec;
1398 int i;
1399 hda_nid_t nid_found = 0;
1400
1401 for (i = 0; i < spec->num_all_dacs; i++) {
1402 hda_nid_t nid = spec->all_dacs[i];
1403 if (!nid || is_dac_already_used(codec, nid))
1404 continue;
1405 if (is_reachable_path(codec, nid, pin)) {
1406 if (nid_found)
1407 return 0;
1408 nid_found = nid;
1409 }
1410 }
1411 return nid_found;
1412}
1413
1414/* check whether the given pin can be a multi-io pin */
1415static bool can_be_multiio_pin(struct hda_codec *codec,
1416 unsigned int location, hda_nid_t nid)
1417{
1418 unsigned int defcfg, caps;
1419
1420 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1421 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1422 return false;
1423 if (location && get_defcfg_location(defcfg) != location)
1424 return false;
1425 caps = snd_hda_query_pin_caps(codec, nid);
1426 if (!(caps & AC_PINCAP_OUT))
1427 return false;
1428 return true;
1429}
1430
Takashi Iwaie22aab72013-01-04 14:50:04 +01001431/* count the number of input pins that are capable to be multi-io */
1432static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1433{
1434 struct hda_gen_spec *spec = codec->spec;
1435 struct auto_pin_cfg *cfg = &spec->autocfg;
1436 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1437 unsigned int location = get_defcfg_location(defcfg);
1438 int type, i;
1439 int num_pins = 0;
1440
1441 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1442 for (i = 0; i < cfg->num_inputs; i++) {
1443 if (cfg->inputs[i].type != type)
1444 continue;
1445 if (can_be_multiio_pin(codec, location,
1446 cfg->inputs[i].pin))
1447 num_pins++;
1448 }
1449 }
1450 return num_pins;
1451}
1452
Takashi Iwai352f7f92012-12-19 12:52:06 +01001453/*
1454 * multi-io helper
1455 *
1456 * When hardwired is set, try to fill ony hardwired pins, and returns
1457 * zero if any pins are filled, non-zero if nothing found.
1458 * When hardwired is off, try to fill possible input pins, and returns
1459 * the badness value.
1460 */
1461static int fill_multi_ios(struct hda_codec *codec,
1462 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001463 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001464{
1465 struct hda_gen_spec *spec = codec->spec;
1466 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001467 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001468 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1469 unsigned int location = get_defcfg_location(defcfg);
1470 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001471 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001472
1473 old_pins = spec->multi_ios;
1474 if (old_pins >= 2)
1475 goto end_fill;
1476
Takashi Iwaie22aab72013-01-04 14:50:04 +01001477 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001478 if (num_pins < 2)
1479 goto end_fill;
1480
Takashi Iwai352f7f92012-12-19 12:52:06 +01001481 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1482 for (i = 0; i < cfg->num_inputs; i++) {
1483 hda_nid_t nid = cfg->inputs[i].pin;
1484 hda_nid_t dac = 0;
1485
1486 if (cfg->inputs[i].type != type)
1487 continue;
1488 if (!can_be_multiio_pin(codec, location, nid))
1489 continue;
1490 for (j = 0; j < spec->multi_ios; j++) {
1491 if (nid == spec->multi_io[j].pin)
1492 break;
1493 }
1494 if (j < spec->multi_ios)
1495 continue;
1496
Takashi Iwai352f7f92012-12-19 12:52:06 +01001497 if (hardwired)
1498 dac = get_dac_if_single(codec, nid);
1499 else if (!dac)
1500 dac = look_for_dac(codec, nid, false);
1501 if (!dac) {
1502 badness++;
1503 continue;
1504 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001505 path = snd_hda_add_new_path(codec, dac, nid,
1506 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001507 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001508 badness++;
1509 continue;
1510 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001511 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001512 spec->multi_io[spec->multi_ios].pin = nid;
1513 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001514 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1515 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001516 spec->multi_ios++;
1517 if (spec->multi_ios >= 2)
1518 break;
1519 }
1520 }
1521 end_fill:
1522 if (badness)
1523 badness = BAD_MULTI_IO;
1524 if (old_pins == spec->multi_ios) {
1525 if (hardwired)
1526 return 1; /* nothing found */
1527 else
1528 return badness; /* no badness if nothing found */
1529 }
1530 if (!hardwired && spec->multi_ios < 2) {
1531 /* cancel newly assigned paths */
1532 spec->paths.used -= spec->multi_ios - old_pins;
1533 spec->multi_ios = old_pins;
1534 return badness;
1535 }
1536
1537 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001538 for (i = old_pins; i < spec->multi_ios; i++) {
1539 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1540 badness += assign_out_path_ctls(codec, path);
1541 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001542
1543 return badness;
1544}
1545
1546/* map DACs for all pins in the list if they are single connections */
1547static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001548 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001549{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001550 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001551 int i;
1552 bool found = false;
1553 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001554 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001555 hda_nid_t dac;
1556 if (dacs[i])
1557 continue;
1558 dac = get_dac_if_single(codec, pins[i]);
1559 if (!dac)
1560 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001561 path = snd_hda_add_new_path(codec, dac, pins[i],
1562 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001563 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001564 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001565 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001566 dacs[i] = dac;
1567 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001568 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001569 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001570 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001571 }
1572 }
1573 return found;
1574}
1575
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001576/* create a new path including aamix if available, and return its index */
1577static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1578{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001579 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001580 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001581 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001582
1583 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001584 if (!path || !path->depth ||
1585 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001586 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001587 path_dac = path->path[0];
1588 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001589 pin = path->path[path->depth - 1];
1590 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1591 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001592 if (dac != path_dac)
1593 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001594 else if (spec->multiout.hp_out_nid[0])
1595 dac = spec->multiout.hp_out_nid[0];
1596 else if (spec->multiout.extra_out_nid[0])
1597 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001598 else
1599 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001600 if (dac)
1601 path = snd_hda_add_new_path(codec, dac, pin,
1602 spec->mixer_nid);
1603 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001604 if (!path)
1605 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001606 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001607 path->active = false; /* unused as default */
Takashi Iwai6b275b12015-03-20 18:11:05 +01001608 path->pin_fixed = true; /* static route */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001609 return snd_hda_get_path_idx(codec, path);
1610}
1611
Takashi Iwai55a63d42013-03-21 17:20:12 +01001612/* check whether the independent HP is available with the current config */
1613static bool indep_hp_possible(struct hda_codec *codec)
1614{
1615 struct hda_gen_spec *spec = codec->spec;
1616 struct auto_pin_cfg *cfg = &spec->autocfg;
1617 struct nid_path *path;
1618 int i, idx;
1619
1620 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1621 idx = spec->out_paths[0];
1622 else
1623 idx = spec->hp_paths[0];
1624 path = snd_hda_get_path_from_idx(codec, idx);
1625 if (!path)
1626 return false;
1627
1628 /* assume no path conflicts unless aamix is involved */
1629 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1630 return true;
1631
1632 /* check whether output paths contain aamix */
1633 for (i = 0; i < cfg->line_outs; i++) {
1634 if (spec->out_paths[i] == idx)
1635 break;
1636 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1637 if (path && is_nid_contained(path, spec->mixer_nid))
1638 return false;
1639 }
1640 for (i = 0; i < cfg->speaker_outs; i++) {
1641 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1642 if (path && is_nid_contained(path, spec->mixer_nid))
1643 return false;
1644 }
1645
1646 return true;
1647}
1648
Takashi Iwaia07a9492013-01-07 16:44:06 +01001649/* fill the empty entries in the dac array for speaker/hp with the
1650 * shared dac pointed by the paths
1651 */
1652static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1653 hda_nid_t *dacs, int *path_idx)
1654{
1655 struct nid_path *path;
1656 int i;
1657
1658 for (i = 0; i < num_outs; i++) {
1659 if (dacs[i])
1660 continue;
1661 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1662 if (!path)
1663 continue;
1664 dacs[i] = path->path[0];
1665 }
1666}
1667
Takashi Iwai352f7f92012-12-19 12:52:06 +01001668/* fill in the dac_nids table from the parsed pin configuration */
1669static int fill_and_eval_dacs(struct hda_codec *codec,
1670 bool fill_hardwired,
1671 bool fill_mio_first)
1672{
1673 struct hda_gen_spec *spec = codec->spec;
1674 struct auto_pin_cfg *cfg = &spec->autocfg;
1675 int i, err, badness;
1676
1677 /* set num_dacs once to full for look_for_dac() */
1678 spec->multiout.num_dacs = cfg->line_outs;
1679 spec->multiout.dac_nids = spec->private_dac_nids;
1680 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1681 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1682 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1683 spec->multi_ios = 0;
1684 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001685
1686 /* clear path indices */
1687 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1688 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1689 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1690 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1691 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001692 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001693 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1694 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1695
Takashi Iwai352f7f92012-12-19 12:52:06 +01001696 badness = 0;
1697
1698 /* fill hard-wired DACs first */
1699 if (fill_hardwired) {
1700 bool mapped;
1701 do {
1702 mapped = map_singles(codec, cfg->line_outs,
1703 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001704 spec->private_dac_nids,
1705 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001706 mapped |= map_singles(codec, cfg->hp_outs,
1707 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001708 spec->multiout.hp_out_nid,
1709 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001710 mapped |= map_singles(codec, cfg->speaker_outs,
1711 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001712 spec->multiout.extra_out_nid,
1713 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001714 if (!spec->no_multi_io &&
1715 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001716 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001717 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001718 if (!err)
1719 mapped = true;
1720 }
1721 } while (mapped);
1722 }
1723
1724 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001725 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001726 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001727
Takashi Iwaida96fb52013-07-29 16:54:36 +02001728 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001729 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1730 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001731 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001732 if (err < 0)
1733 return err;
1734 /* we don't count badness at this stage yet */
1735 }
1736
1737 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1738 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1739 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001740 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001741 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001742 if (err < 0)
1743 return err;
1744 badness += err;
1745 }
1746 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1747 err = try_assign_dacs(codec, cfg->speaker_outs,
1748 cfg->speaker_pins,
1749 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001750 spec->speaker_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 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001756 if (!spec->no_multi_io &&
1757 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001758 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001759 if (err < 0)
1760 return err;
1761 badness += err;
1762 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001763
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001764 if (spec->mixer_nid) {
1765 spec->aamix_out_paths[0] =
1766 check_aamix_out_path(codec, spec->out_paths[0]);
1767 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1768 spec->aamix_out_paths[1] =
1769 check_aamix_out_path(codec, spec->hp_paths[0]);
1770 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1771 spec->aamix_out_paths[2] =
1772 check_aamix_out_path(codec, spec->speaker_paths[0]);
1773 }
1774
Takashi Iwaida96fb52013-07-29 16:54:36 +02001775 if (!spec->no_multi_io &&
1776 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001777 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1778 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001779
Takashi Iwaia07a9492013-01-07 16:44:06 +01001780 /* re-count num_dacs and squash invalid entries */
1781 spec->multiout.num_dacs = 0;
1782 for (i = 0; i < cfg->line_outs; i++) {
1783 if (spec->private_dac_nids[i])
1784 spec->multiout.num_dacs++;
1785 else {
1786 memmove(spec->private_dac_nids + i,
1787 spec->private_dac_nids + i + 1,
1788 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1789 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1790 }
1791 }
1792
1793 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001794 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001795
Takashi Iwai352f7f92012-12-19 12:52:06 +01001796 if (spec->multi_ios == 2) {
1797 for (i = 0; i < 2; i++)
1798 spec->private_dac_nids[spec->multiout.num_dacs++] =
1799 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001800 } else if (spec->multi_ios) {
1801 spec->multi_ios = 0;
1802 badness += BAD_MULTI_IO;
1803 }
1804
Takashi Iwai55a63d42013-03-21 17:20:12 +01001805 if (spec->indep_hp && !indep_hp_possible(codec))
1806 badness += BAD_NO_INDEP_HP;
1807
Takashi Iwaia07a9492013-01-07 16:44:06 +01001808 /* re-fill the shared DAC for speaker / headphone */
1809 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1810 refill_shared_dacs(codec, cfg->hp_outs,
1811 spec->multiout.hp_out_nid,
1812 spec->hp_paths);
1813 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1814 refill_shared_dacs(codec, cfg->speaker_outs,
1815 spec->multiout.extra_out_nid,
1816 spec->speaker_paths);
1817
Takashi Iwai352f7f92012-12-19 12:52:06 +01001818 return badness;
1819}
1820
1821#define DEBUG_BADNESS
1822
1823#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001824#define debug_badness(fmt, ...) \
1825 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001826#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001827#define debug_badness(fmt, ...) \
1828 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001829#endif
1830
Takashi Iwaia7694092013-01-21 10:43:18 +01001831#ifdef DEBUG_BADNESS
1832static inline void print_nid_path_idx(struct hda_codec *codec,
1833 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001834{
Takashi Iwaia7694092013-01-21 10:43:18 +01001835 struct nid_path *path;
1836
1837 path = snd_hda_get_path_from_idx(codec, idx);
1838 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001839 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001840}
1841
1842static void debug_show_configs(struct hda_codec *codec,
1843 struct auto_pin_cfg *cfg)
1844{
1845 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001846 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001847 int i;
1848
1849 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001850 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001851 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001852 spec->multiout.dac_nids[0],
1853 spec->multiout.dac_nids[1],
1854 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001855 spec->multiout.dac_nids[3],
1856 lo_type[cfg->line_out_type]);
1857 for (i = 0; i < cfg->line_outs; i++)
1858 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001859 if (spec->multi_ios > 0)
1860 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1861 spec->multi_ios,
1862 spec->multi_io[0].pin, spec->multi_io[1].pin,
1863 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001864 for (i = 0; i < spec->multi_ios; i++)
1865 print_nid_path_idx(codec, " mio",
1866 spec->out_paths[cfg->line_outs + i]);
1867 if (cfg->hp_outs)
1868 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001869 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001870 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001871 spec->multiout.hp_out_nid[0],
1872 spec->multiout.hp_out_nid[1],
1873 spec->multiout.hp_out_nid[2],
1874 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001875 for (i = 0; i < cfg->hp_outs; i++)
1876 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1877 if (cfg->speaker_outs)
1878 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001879 cfg->speaker_pins[0], cfg->speaker_pins[1],
1880 cfg->speaker_pins[2], cfg->speaker_pins[3],
1881 spec->multiout.extra_out_nid[0],
1882 spec->multiout.extra_out_nid[1],
1883 spec->multiout.extra_out_nid[2],
1884 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001885 for (i = 0; i < cfg->speaker_outs; i++)
1886 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1887 for (i = 0; i < 3; i++)
1888 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001889}
Takashi Iwaia7694092013-01-21 10:43:18 +01001890#else
1891#define debug_show_configs(codec, cfg) /* NOP */
1892#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001893
1894/* find all available DACs of the codec */
1895static void fill_all_dac_nids(struct hda_codec *codec)
1896{
1897 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001898 hda_nid_t nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001899
1900 spec->num_all_dacs = 0;
1901 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
Takashi Iwai7639a062015-03-03 10:07:24 +01001902 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001903 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1904 continue;
1905 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001906 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001907 break;
1908 }
1909 spec->all_dacs[spec->num_all_dacs++] = nid;
1910 }
1911}
1912
1913static int parse_output_paths(struct hda_codec *codec)
1914{
1915 struct hda_gen_spec *spec = codec->spec;
1916 struct auto_pin_cfg *cfg = &spec->autocfg;
1917 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001918 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001919 int best_badness = INT_MAX;
1920 int badness;
1921 bool fill_hardwired = true, fill_mio_first = true;
1922 bool best_wired = true, best_mio = true;
1923 bool hp_spk_swapped = false;
1924
Takashi Iwai352f7f92012-12-19 12:52:06 +01001925 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1926 if (!best_cfg)
1927 return -ENOMEM;
1928 *best_cfg = *cfg;
1929
1930 for (;;) {
1931 badness = fill_and_eval_dacs(codec, fill_hardwired,
1932 fill_mio_first);
1933 if (badness < 0) {
1934 kfree(best_cfg);
1935 return badness;
1936 }
1937 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1938 cfg->line_out_type, fill_hardwired, fill_mio_first,
1939 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001940 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001941 if (badness < best_badness) {
1942 best_badness = badness;
1943 *best_cfg = *cfg;
1944 best_wired = fill_hardwired;
1945 best_mio = fill_mio_first;
1946 }
1947 if (!badness)
1948 break;
1949 fill_mio_first = !fill_mio_first;
1950 if (!fill_mio_first)
1951 continue;
1952 fill_hardwired = !fill_hardwired;
1953 if (!fill_hardwired)
1954 continue;
1955 if (hp_spk_swapped)
1956 break;
1957 hp_spk_swapped = true;
1958 if (cfg->speaker_outs > 0 &&
1959 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1960 cfg->hp_outs = cfg->line_outs;
1961 memcpy(cfg->hp_pins, cfg->line_out_pins,
1962 sizeof(cfg->hp_pins));
1963 cfg->line_outs = cfg->speaker_outs;
1964 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1965 sizeof(cfg->speaker_pins));
1966 cfg->speaker_outs = 0;
1967 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1968 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1969 fill_hardwired = true;
1970 continue;
1971 }
1972 if (cfg->hp_outs > 0 &&
1973 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1974 cfg->speaker_outs = cfg->line_outs;
1975 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1976 sizeof(cfg->speaker_pins));
1977 cfg->line_outs = cfg->hp_outs;
1978 memcpy(cfg->line_out_pins, cfg->hp_pins,
1979 sizeof(cfg->hp_pins));
1980 cfg->hp_outs = 0;
1981 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1982 cfg->line_out_type = AUTO_PIN_HP_OUT;
1983 fill_hardwired = true;
1984 continue;
1985 }
1986 break;
1987 }
1988
1989 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001990 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001991 *cfg = *best_cfg;
1992 fill_and_eval_dacs(codec, best_wired, best_mio);
1993 }
1994 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1995 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001996 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001997
1998 if (cfg->line_out_pins[0]) {
1999 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01002000 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002001 if (path)
2002 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002003 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01002004 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2005 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002006 if (spec->dac_min_mute)
2007 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2008 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002009 }
2010
Takashi Iwai9314a582013-01-21 10:49:05 +01002011 /* set initial pinctl targets */
2012 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2013 val = PIN_HP;
2014 else
2015 val = PIN_OUT;
2016 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2017 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2018 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2019 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2020 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2021 set_pin_targets(codec, cfg->speaker_outs,
2022 cfg->speaker_pins, val);
2023 }
2024
Takashi Iwai55a63d42013-03-21 17:20:12 +01002025 /* clear indep_hp flag if not available */
2026 if (spec->indep_hp && !indep_hp_possible(codec))
2027 spec->indep_hp = 0;
2028
Takashi Iwai352f7f92012-12-19 12:52:06 +01002029 kfree(best_cfg);
2030 return 0;
2031}
2032
2033/* add playback controls from the parsed DAC table */
2034static int create_multi_out_ctls(struct hda_codec *codec,
2035 const struct auto_pin_cfg *cfg)
2036{
2037 struct hda_gen_spec *spec = codec->spec;
2038 int i, err, noutputs;
2039
2040 noutputs = cfg->line_outs;
2041 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2042 noutputs += spec->multi_ios;
2043
2044 for (i = 0; i < noutputs; i++) {
2045 const char *name;
2046 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002047 struct nid_path *path;
2048
Takashi Iwai196c17662013-01-04 15:01:40 +01002049 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002050 if (!path)
2051 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002052
2053 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002054 if (!name || !strcmp(name, "CLFE")) {
2055 /* Center/LFE */
2056 err = add_vol_ctl(codec, "Center", 0, 1, path);
2057 if (err < 0)
2058 return err;
2059 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2060 if (err < 0)
2061 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002062 } else {
2063 err = add_stereo_vol(codec, name, index, path);
2064 if (err < 0)
2065 return err;
2066 }
2067
2068 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2069 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002070 err = add_sw_ctl(codec, "Center", 0, 1, path);
2071 if (err < 0)
2072 return err;
2073 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2074 if (err < 0)
2075 return err;
2076 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002077 err = add_stereo_sw(codec, name, index, path);
2078 if (err < 0)
2079 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 }
2081 }
2082 return 0;
2083}
2084
Takashi Iwaic2c80382013-01-07 10:33:57 +01002085static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01002086 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002088 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 int err;
2090
Takashi Iwai196c17662013-01-04 15:01:40 +01002091 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002092 if (!path)
2093 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01002094 err = add_stereo_vol(codec, pfx, cidx, path);
2095 if (err < 0)
2096 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002097 err = add_stereo_sw(codec, pfx, cidx, path);
2098 if (err < 0)
2099 return err;
2100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102
Takashi Iwai352f7f92012-12-19 12:52:06 +01002103/* add playback controls for speaker and HP outputs */
2104static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01002105 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
Takashi Iwaic2c80382013-01-07 10:33:57 +01002107 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002108
2109 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002110 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02002111 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01002112 int err, idx = 0;
2113
2114 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2115 name = "Bass Speaker";
2116 else if (num_pins >= 3) {
2117 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01002118 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01002119 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002120 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002121 name = pfx;
2122 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01002124 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002125 if (err < 0)
2126 return err;
2127 }
2128 return 0;
2129}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002130
Takashi Iwai352f7f92012-12-19 12:52:06 +01002131static int create_hp_out_ctls(struct hda_codec *codec)
2132{
2133 struct hda_gen_spec *spec = codec->spec;
2134 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002135 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002136 "Headphone");
2137}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Takashi Iwai352f7f92012-12-19 12:52:06 +01002139static int create_speaker_out_ctls(struct hda_codec *codec)
2140{
2141 struct hda_gen_spec *spec = codec->spec;
2142 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002143 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002144 "Speaker");
2145}
2146
2147/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002148 * independent HP controls
2149 */
2150
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02002151static void call_hp_automute(struct hda_codec *codec,
2152 struct hda_jack_callback *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002153static int indep_hp_info(struct snd_kcontrol *kcontrol,
2154 struct snd_ctl_elem_info *uinfo)
2155{
2156 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2157}
2158
2159static int indep_hp_get(struct snd_kcontrol *kcontrol,
2160 struct snd_ctl_elem_value *ucontrol)
2161{
2162 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2163 struct hda_gen_spec *spec = codec->spec;
2164 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2165 return 0;
2166}
2167
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002168static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2169 int nomix_path_idx, int mix_path_idx,
2170 int out_type);
2171
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002172static int indep_hp_put(struct snd_kcontrol *kcontrol,
2173 struct snd_ctl_elem_value *ucontrol)
2174{
2175 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2176 struct hda_gen_spec *spec = codec->spec;
2177 unsigned int select = ucontrol->value.enumerated.item[0];
2178 int ret = 0;
2179
2180 mutex_lock(&spec->pcm_mutex);
2181 if (spec->active_streams) {
2182 ret = -EBUSY;
2183 goto unlock;
2184 }
2185
2186 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002187 hda_nid_t *dacp;
2188 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2189 dacp = &spec->private_dac_nids[0];
2190 else
2191 dacp = &spec->multiout.hp_out_nid[0];
2192
2193 /* update HP aamix paths in case it conflicts with indep HP */
2194 if (spec->have_aamix_ctl) {
2195 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2196 update_aamix_paths(codec, spec->aamix_mode,
2197 spec->out_paths[0],
2198 spec->aamix_out_paths[0],
2199 spec->autocfg.line_out_type);
2200 else
2201 update_aamix_paths(codec, spec->aamix_mode,
2202 spec->hp_paths[0],
2203 spec->aamix_out_paths[1],
2204 AUTO_PIN_HP_OUT);
2205 }
2206
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002207 spec->indep_hp_enabled = select;
2208 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002209 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002210 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002211 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002212
Takashi Iwai963afde2013-05-31 15:20:31 +02002213 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002214 ret = 1;
2215 }
2216 unlock:
2217 mutex_unlock(&spec->pcm_mutex);
2218 return ret;
2219}
2220
2221static const struct snd_kcontrol_new indep_hp_ctl = {
2222 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2223 .name = "Independent HP",
2224 .info = indep_hp_info,
2225 .get = indep_hp_get,
2226 .put = indep_hp_put,
2227};
2228
2229
2230static int create_indep_hp_ctls(struct hda_codec *codec)
2231{
2232 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002233 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002234
2235 if (!spec->indep_hp)
2236 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002237 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2238 dac = spec->multiout.dac_nids[0];
2239 else
2240 dac = spec->multiout.hp_out_nid[0];
2241 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002242 spec->indep_hp = 0;
2243 return 0;
2244 }
2245
2246 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002247 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002248 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2249 return -ENOMEM;
2250 return 0;
2251}
2252
2253/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002254 * channel mode enum control
2255 */
2256
2257static int ch_mode_info(struct snd_kcontrol *kcontrol,
2258 struct snd_ctl_elem_info *uinfo)
2259{
2260 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2261 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002262 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002263
2264 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2265 uinfo->count = 1;
2266 uinfo->value.enumerated.items = spec->multi_ios + 1;
2267 if (uinfo->value.enumerated.item > spec->multi_ios)
2268 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002269 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2270 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002271 return 0;
2272}
2273
2274static int ch_mode_get(struct snd_kcontrol *kcontrol,
2275 struct snd_ctl_elem_value *ucontrol)
2276{
2277 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2278 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002279 ucontrol->value.enumerated.item[0] =
2280 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002281 return 0;
2282}
2283
Takashi Iwai196c17662013-01-04 15:01:40 +01002284static inline struct nid_path *
2285get_multiio_path(struct hda_codec *codec, int idx)
2286{
2287 struct hda_gen_spec *spec = codec->spec;
2288 return snd_hda_get_path_from_idx(codec,
2289 spec->out_paths[spec->autocfg.line_outs + idx]);
2290}
2291
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002292static void update_automute_all(struct hda_codec *codec);
2293
Takashi Iwai65033cc2013-04-16 12:31:05 +02002294/* Default value to be passed as aamix argument for snd_hda_activate_path();
2295 * used for output paths
2296 */
2297static bool aamix_default(struct hda_gen_spec *spec)
2298{
2299 return !spec->have_aamix_ctl || spec->aamix_mode;
2300}
2301
Takashi Iwai352f7f92012-12-19 12:52:06 +01002302static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2303{
2304 struct hda_gen_spec *spec = codec->spec;
2305 hda_nid_t nid = spec->multi_io[idx].pin;
2306 struct nid_path *path;
2307
Takashi Iwai196c17662013-01-04 15:01:40 +01002308 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002309 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002311
2312 if (path->active == output)
2313 return 0;
2314
2315 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002316 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002317 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002318 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002319 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002320 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002321 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002322 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002323 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002325
2326 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002327 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002328
Takashi Iwai352f7f92012-12-19 12:52:06 +01002329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330}
2331
Takashi Iwai352f7f92012-12-19 12:52:06 +01002332static int ch_mode_put(struct snd_kcontrol *kcontrol,
2333 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002335 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2336 struct hda_gen_spec *spec = codec->spec;
2337 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Takashi Iwai352f7f92012-12-19 12:52:06 +01002339 ch = ucontrol->value.enumerated.item[0];
2340 if (ch < 0 || ch > spec->multi_ios)
2341 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002342 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002343 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002344 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002345 for (i = 0; i < spec->multi_ios; i++)
2346 set_multi_io(codec, i, i < ch);
2347 spec->multiout.max_channels = max(spec->ext_channel_count,
2348 spec->const_channel_count);
2349 if (spec->need_dac_fix)
2350 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 return 1;
2352}
2353
Takashi Iwai352f7f92012-12-19 12:52:06 +01002354static const struct snd_kcontrol_new channel_mode_enum = {
2355 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2356 .name = "Channel Mode",
2357 .info = ch_mode_info,
2358 .get = ch_mode_get,
2359 .put = ch_mode_put,
2360};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Takashi Iwai352f7f92012-12-19 12:52:06 +01002362static int create_multi_channel_mode(struct hda_codec *codec)
2363{
2364 struct hda_gen_spec *spec = codec->spec;
2365
2366 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002367 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002368 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 return 0;
2371}
2372
Takashi Iwai352f7f92012-12-19 12:52:06 +01002373/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002374 * aamix loopback enable/disable switch
2375 */
2376
2377#define loopback_mixing_info indep_hp_info
2378
2379static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2380 struct snd_ctl_elem_value *ucontrol)
2381{
2382 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2383 struct hda_gen_spec *spec = codec->spec;
2384 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2385 return 0;
2386}
2387
2388static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002389 int nomix_path_idx, int mix_path_idx,
2390 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002391{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002392 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002393 struct nid_path *nomix_path, *mix_path;
2394
2395 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2396 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2397 if (!nomix_path || !mix_path)
2398 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002399
2400 /* if HP aamix path is driven from a different DAC and the
2401 * independent HP mode is ON, can't turn on aamix path
2402 */
2403 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2404 mix_path->path[0] != spec->alt_dac_nid)
2405 do_mix = false;
2406
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002407 if (do_mix) {
2408 snd_hda_activate_path(codec, nomix_path, false, true);
2409 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002410 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002411 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002412 snd_hda_activate_path(codec, mix_path, false, false);
2413 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002414 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002415 }
2416}
2417
2418static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2419 struct snd_ctl_elem_value *ucontrol)
2420{
2421 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2422 struct hda_gen_spec *spec = codec->spec;
2423 unsigned int val = ucontrol->value.enumerated.item[0];
2424
2425 if (val == spec->aamix_mode)
2426 return 0;
2427 spec->aamix_mode = val;
2428 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002429 spec->aamix_out_paths[0],
2430 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002431 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002432 spec->aamix_out_paths[1],
2433 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002434 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002435 spec->aamix_out_paths[2],
2436 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002437 return 1;
2438}
2439
2440static const struct snd_kcontrol_new loopback_mixing_enum = {
2441 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2442 .name = "Loopback Mixing",
2443 .info = loopback_mixing_info,
2444 .get = loopback_mixing_get,
2445 .put = loopback_mixing_put,
2446};
2447
2448static int create_loopback_mixing_ctl(struct hda_codec *codec)
2449{
2450 struct hda_gen_spec *spec = codec->spec;
2451
2452 if (!spec->mixer_nid)
2453 return 0;
2454 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2455 spec->aamix_out_paths[2]))
2456 return 0;
2457 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2458 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002459 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002460 return 0;
2461}
2462
2463/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002464 * shared headphone/mic handling
2465 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002466
Takashi Iwai352f7f92012-12-19 12:52:06 +01002467static void call_update_outputs(struct hda_codec *codec);
2468
2469/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002470static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002471{
2472 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002473 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002474 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002475 hda_nid_t pin;
2476
2477 pin = spec->hp_mic_pin;
2478 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2479
2480 if (!force) {
2481 val = snd_hda_codec_get_pin_target(codec, pin);
2482 if (as_mic) {
2483 if (val & PIN_IN)
2484 return;
2485 } else {
2486 if (val & PIN_OUT)
2487 return;
2488 }
2489 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002490
2491 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002492 /* if the HP pin doesn't support VREF and the codec driver gives an
2493 * alternative pin, set up the VREF on that pin instead
2494 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002495 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2496 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2497 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2498 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002499 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002500 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002501 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002502
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002503 if (!spec->hp_mic_jack_modes) {
2504 if (as_mic)
2505 val |= PIN_IN;
2506 else
2507 val = PIN_HP;
2508 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002509 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002510 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002511}
2512
2513/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002514static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002515{
2516 struct hda_gen_spec *spec = codec->spec;
2517 struct auto_pin_cfg *cfg = &spec->autocfg;
2518 unsigned int defcfg;
2519 hda_nid_t nid;
2520
Takashi Iwai967303d2013-02-19 17:12:42 +01002521 if (!spec->hp_mic) {
2522 if (spec->suppress_hp_mic_detect)
2523 return 0;
2524 /* automatic detection: only if no input or a single internal
2525 * input pin is found, try to detect the shared hp/mic
2526 */
2527 if (cfg->num_inputs > 1)
2528 return 0;
2529 else if (cfg->num_inputs == 1) {
2530 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2531 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2532 return 0;
2533 }
2534 }
2535
2536 spec->hp_mic = 0; /* clear once */
2537 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002538 return 0;
2539
Takashi Iwai967303d2013-02-19 17:12:42 +01002540 nid = 0;
2541 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2542 nid = cfg->line_out_pins[0];
2543 else if (cfg->hp_outs > 0)
2544 nid = cfg->hp_pins[0];
2545 if (!nid)
2546 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002547
2548 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2549 return 0; /* no input */
2550
Takashi Iwai967303d2013-02-19 17:12:42 +01002551 cfg->inputs[cfg->num_inputs].pin = nid;
2552 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002553 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002554 cfg->num_inputs++;
2555 spec->hp_mic = 1;
2556 spec->hp_mic_pin = nid;
2557 /* we can't handle auto-mic together with HP-mic */
2558 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002559 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002560 return 0;
2561}
2562
Takashi Iwai978e77e2013-01-10 16:57:58 +01002563/*
2564 * output jack mode
2565 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002566
2567static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2568
2569static const char * const out_jack_texts[] = {
2570 "Line Out", "Headphone Out",
2571};
2572
Takashi Iwai978e77e2013-01-10 16:57:58 +01002573static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2574 struct snd_ctl_elem_info *uinfo)
2575{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002576 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002577}
2578
2579static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2580 struct snd_ctl_elem_value *ucontrol)
2581{
2582 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2583 hda_nid_t nid = kcontrol->private_value;
2584 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2585 ucontrol->value.enumerated.item[0] = 1;
2586 else
2587 ucontrol->value.enumerated.item[0] = 0;
2588 return 0;
2589}
2590
2591static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2592 struct snd_ctl_elem_value *ucontrol)
2593{
2594 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2595 hda_nid_t nid = kcontrol->private_value;
2596 unsigned int val;
2597
2598 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2599 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2600 return 0;
2601 snd_hda_set_pin_ctl_cache(codec, nid, val);
2602 return 1;
2603}
2604
2605static const struct snd_kcontrol_new out_jack_mode_enum = {
2606 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2607 .info = out_jack_mode_info,
2608 .get = out_jack_mode_get,
2609 .put = out_jack_mode_put,
2610};
2611
2612static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2613{
2614 struct hda_gen_spec *spec = codec->spec;
2615 int i;
2616
2617 for (i = 0; i < spec->kctls.used; i++) {
2618 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2619 if (!strcmp(kctl->name, name) && kctl->index == idx)
2620 return true;
2621 }
2622 return false;
2623}
2624
2625static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2626 char *name, size_t name_len)
2627{
2628 struct hda_gen_spec *spec = codec->spec;
2629 int idx = 0;
2630
2631 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2632 strlcat(name, " Jack Mode", name_len);
2633
2634 for (; find_kctl_name(codec, name, idx); idx++)
2635 ;
2636}
2637
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002638static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2639{
2640 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002641 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002642 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2643 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2644 return 2;
2645 }
2646 return 1;
2647}
2648
Takashi Iwai978e77e2013-01-10 16:57:58 +01002649static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2650 hda_nid_t *pins)
2651{
2652 struct hda_gen_spec *spec = codec->spec;
2653 int i;
2654
2655 for (i = 0; i < num_pins; i++) {
2656 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002657 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002658 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002659 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002660 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002661 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002662 get_jack_mode_name(codec, pin, name, sizeof(name));
2663 knew = snd_hda_gen_add_kctl(spec, name,
2664 &out_jack_mode_enum);
2665 if (!knew)
2666 return -ENOMEM;
2667 knew->private_value = pin;
2668 }
2669 }
2670
2671 return 0;
2672}
2673
Takashi Iwai294765582013-01-17 09:52:11 +01002674/*
2675 * input jack mode
2676 */
2677
2678/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2679#define NUM_VREFS 6
2680
2681static const char * const vref_texts[NUM_VREFS] = {
2682 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2683 "", "Mic 80pc Bias", "Mic 100pc Bias"
2684};
2685
2686static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2687{
2688 unsigned int pincap;
2689
2690 pincap = snd_hda_query_pin_caps(codec, pin);
2691 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2692 /* filter out unusual vrefs */
2693 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2694 return pincap;
2695}
2696
2697/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2698static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2699{
2700 unsigned int i, n = 0;
2701
2702 for (i = 0; i < NUM_VREFS; i++) {
2703 if (vref_caps & (1 << i)) {
2704 if (n == item_idx)
2705 return i;
2706 n++;
2707 }
2708 }
2709 return 0;
2710}
2711
2712/* convert back from the vref ctl index to the enum item index */
2713static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2714{
2715 unsigned int i, n = 0;
2716
2717 for (i = 0; i < NUM_VREFS; i++) {
2718 if (i == idx)
2719 return n;
2720 if (vref_caps & (1 << i))
2721 n++;
2722 }
2723 return 0;
2724}
2725
2726static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2727 struct snd_ctl_elem_info *uinfo)
2728{
2729 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2730 hda_nid_t nid = kcontrol->private_value;
2731 unsigned int vref_caps = get_vref_caps(codec, nid);
2732
2733 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2734 vref_texts);
2735 /* set the right text */
2736 strcpy(uinfo->value.enumerated.name,
2737 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2738 return 0;
2739}
2740
2741static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2742 struct snd_ctl_elem_value *ucontrol)
2743{
2744 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2745 hda_nid_t nid = kcontrol->private_value;
2746 unsigned int vref_caps = get_vref_caps(codec, nid);
2747 unsigned int idx;
2748
2749 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2750 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2751 return 0;
2752}
2753
2754static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2755 struct snd_ctl_elem_value *ucontrol)
2756{
2757 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2758 hda_nid_t nid = kcontrol->private_value;
2759 unsigned int vref_caps = get_vref_caps(codec, nid);
2760 unsigned int val, idx;
2761
2762 val = snd_hda_codec_get_pin_target(codec, nid);
2763 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2764 if (idx == ucontrol->value.enumerated.item[0])
2765 return 0;
2766
2767 val &= ~AC_PINCTL_VREFEN;
2768 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2769 snd_hda_set_pin_ctl_cache(codec, nid, val);
2770 return 1;
2771}
2772
2773static const struct snd_kcontrol_new in_jack_mode_enum = {
2774 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2775 .info = in_jack_mode_info,
2776 .get = in_jack_mode_get,
2777 .put = in_jack_mode_put,
2778};
2779
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002780static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2781{
2782 struct hda_gen_spec *spec = codec->spec;
2783 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002784 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002785 nitems = hweight32(get_vref_caps(codec, pin));
2786 return nitems ? nitems : 1;
2787}
2788
Takashi Iwai294765582013-01-17 09:52:11 +01002789static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2790{
2791 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002792 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002793 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002794 unsigned int defcfg;
2795
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002796 if (pin == spec->hp_mic_pin)
2797 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002798
2799 /* no jack mode for fixed pins */
2800 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2801 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2802 return 0;
2803
2804 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002805 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002806 return 0;
2807
2808 get_jack_mode_name(codec, pin, name, sizeof(name));
2809 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2810 if (!knew)
2811 return -ENOMEM;
2812 knew->private_value = pin;
2813 return 0;
2814}
2815
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002816/*
2817 * HP/mic shared jack mode
2818 */
2819static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2820 struct snd_ctl_elem_info *uinfo)
2821{
2822 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2823 hda_nid_t nid = kcontrol->private_value;
2824 int out_jacks = get_out_jack_num_items(codec, nid);
2825 int in_jacks = get_in_jack_num_items(codec, nid);
2826 const char *text = NULL;
2827 int idx;
2828
2829 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2830 uinfo->count = 1;
2831 uinfo->value.enumerated.items = out_jacks + in_jacks;
2832 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2833 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2834 idx = uinfo->value.enumerated.item;
2835 if (idx < out_jacks) {
2836 if (out_jacks > 1)
2837 text = out_jack_texts[idx];
2838 else
2839 text = "Headphone Out";
2840 } else {
2841 idx -= out_jacks;
2842 if (in_jacks > 1) {
2843 unsigned int vref_caps = get_vref_caps(codec, nid);
2844 text = vref_texts[get_vref_idx(vref_caps, idx)];
2845 } else
2846 text = "Mic In";
2847 }
2848
2849 strcpy(uinfo->value.enumerated.name, text);
2850 return 0;
2851}
2852
2853static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2854{
2855 int out_jacks = get_out_jack_num_items(codec, nid);
2856 int in_jacks = get_in_jack_num_items(codec, nid);
2857 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2858 int idx = 0;
2859
2860 if (val & PIN_OUT) {
2861 if (out_jacks > 1 && val == PIN_HP)
2862 idx = 1;
2863 } else if (val & PIN_IN) {
2864 idx = out_jacks;
2865 if (in_jacks > 1) {
2866 unsigned int vref_caps = get_vref_caps(codec, nid);
2867 val &= AC_PINCTL_VREFEN;
2868 idx += cvt_from_vref_idx(vref_caps, val);
2869 }
2870 }
2871 return idx;
2872}
2873
2874static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2875 struct snd_ctl_elem_value *ucontrol)
2876{
2877 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2878 hda_nid_t nid = kcontrol->private_value;
2879 ucontrol->value.enumerated.item[0] =
2880 get_cur_hp_mic_jack_mode(codec, nid);
2881 return 0;
2882}
2883
2884static int hp_mic_jack_mode_put(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 int out_jacks = get_out_jack_num_items(codec, nid);
2890 int in_jacks = get_in_jack_num_items(codec, nid);
2891 unsigned int val, oldval, idx;
2892
2893 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2894 idx = ucontrol->value.enumerated.item[0];
2895 if (oldval == idx)
2896 return 0;
2897
2898 if (idx < out_jacks) {
2899 if (out_jacks > 1)
2900 val = idx ? PIN_HP : PIN_OUT;
2901 else
2902 val = PIN_HP;
2903 } else {
2904 idx -= out_jacks;
2905 if (in_jacks > 1) {
2906 unsigned int vref_caps = get_vref_caps(codec, nid);
2907 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002908 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2909 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002910 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002911 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002912 }
2913 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002914 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002915
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002916 return 1;
2917}
2918
2919static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2920 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2921 .info = hp_mic_jack_mode_info,
2922 .get = hp_mic_jack_mode_get,
2923 .put = hp_mic_jack_mode_put,
2924};
2925
2926static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2927{
2928 struct hda_gen_spec *spec = codec->spec;
2929 struct snd_kcontrol_new *knew;
2930
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002931 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2932 &hp_mic_jack_mode_enum);
2933 if (!knew)
2934 return -ENOMEM;
2935 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002936 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002937 return 0;
2938}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002939
2940/*
2941 * Parse input paths
2942 */
2943
Takashi Iwai352f7f92012-12-19 12:52:06 +01002944/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002945static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002946{
2947 struct hda_amp_list *list;
2948
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002949 list = snd_array_new(&spec->loopback_list);
2950 if (!list)
2951 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002952 list->nid = mix;
2953 list->dir = HDA_INPUT;
2954 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002955 spec->loopback.amplist = spec->loopback_list.list;
2956 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002957}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002958
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002959/* return true if either a volume or a mute amp is found for the given
2960 * aamix path; the amp has to be either in the mixer node or its direct leaf
2961 */
2962static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2963 hda_nid_t pin, unsigned int *mix_val,
2964 unsigned int *mute_val)
2965{
2966 int idx, num_conns;
2967 const hda_nid_t *list;
2968 hda_nid_t nid;
2969
2970 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2971 if (idx < 0)
2972 return false;
2973
2974 *mix_val = *mute_val = 0;
2975 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2976 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2977 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2978 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2979 if (*mix_val && *mute_val)
2980 return true;
2981
2982 /* check leaf node */
2983 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2984 if (num_conns < idx)
2985 return false;
2986 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01002987 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2988 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002989 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01002990 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
2991 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002992 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2993
2994 return *mix_val || *mute_val;
2995}
2996
Takashi Iwai352f7f92012-12-19 12:52:06 +01002997/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002998static int new_analog_input(struct hda_codec *codec, int input_idx,
2999 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003000 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003002 struct hda_gen_spec *spec = codec->spec;
3003 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003004 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003005 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003007 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3008 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003009
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003010 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003011 if (!path)
3012 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003013 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01003014 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003015
3016 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003017 if (mix_val) {
3018 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003019 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003021 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 }
3023
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003024 if (mute_val) {
3025 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003026 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003028 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 }
3030
Takashi Iwai352f7f92012-12-19 12:52:06 +01003031 path->active = true;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003032 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003033 err = add_loopback_list(spec, mix_nid, idx);
3034 if (err < 0)
3035 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003036
3037 if (spec->mixer_nid != spec->mixer_merge_nid &&
3038 !spec->loopback_merge_path) {
3039 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3040 spec->mixer_merge_nid, 0);
3041 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003042 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003043 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003044 path->pin_fixed = true; /* static route */
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003045 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003046 spec->loopback_merge_path =
3047 snd_hda_get_path_idx(codec, path);
3048 }
3049 }
3050
Takashi Iwai352f7f92012-12-19 12:52:06 +01003051 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052}
3053
Takashi Iwai352f7f92012-12-19 12:52:06 +01003054static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003056 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3057 return (pincap & AC_PINCAP_IN) != 0;
3058}
3059
3060/* Parse the codec tree and retrieve ADCs */
3061static int fill_adc_nids(struct hda_codec *codec)
3062{
3063 struct hda_gen_spec *spec = codec->spec;
3064 hda_nid_t nid;
3065 hda_nid_t *adc_nids = spec->adc_nids;
3066 int max_nums = ARRAY_SIZE(spec->adc_nids);
Takashi Iwai7639a062015-03-03 10:07:24 +01003067 int nums = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003068
Takashi Iwai7639a062015-03-03 10:07:24 +01003069 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003070 unsigned int caps = get_wcaps(codec, nid);
3071 int type = get_wcaps_type(caps);
3072
3073 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3074 continue;
3075 adc_nids[nums] = nid;
3076 if (++nums >= max_nums)
3077 break;
3078 }
3079 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01003080
3081 /* copy the detected ADCs to all_adcs[] */
3082 spec->num_all_adcs = nums;
3083 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3084
Takashi Iwai352f7f92012-12-19 12:52:06 +01003085 return nums;
3086}
3087
3088/* filter out invalid adc_nids that don't give all active input pins;
3089 * if needed, check whether dynamic ADC-switching is available
3090 */
3091static int check_dyn_adc_switch(struct hda_codec *codec)
3092{
3093 struct hda_gen_spec *spec = codec->spec;
3094 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003095 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003096 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003097
Takashi Iwai352f7f92012-12-19 12:52:06 +01003098 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003099 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003100 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003101 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003102 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003103 break;
3104 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003105 if (i >= imux->num_items) {
3106 ok_bits |= (1 << n);
3107 nums++;
3108 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003109 }
3110
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003111 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003112 /* check whether ADC-switch is possible */
3113 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003114 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003115 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003116 spec->dyn_adc_idx[i] = n;
3117 break;
3118 }
3119 }
3120 }
3121
Takashi Iwai4e76a882014-02-25 12:21:03 +01003122 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003123 spec->dyn_adc_switch = 1;
3124 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003125 /* shrink the invalid adcs and input paths */
3126 nums = 0;
3127 for (n = 0; n < spec->num_adc_nids; n++) {
3128 if (!(ok_bits & (1 << n)))
3129 continue;
3130 if (n != nums) {
3131 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003132 for (i = 0; i < imux->num_items; i++) {
3133 invalidate_nid_path(codec,
3134 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003135 spec->input_paths[i][nums] =
3136 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003137 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003138 }
3139 nums++;
3140 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003141 spec->num_adc_nids = nums;
3142 }
3143
Takashi Iwai967303d2013-02-19 17:12:42 +01003144 if (imux->num_items == 1 ||
3145 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003146 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003147 spec->num_adc_nids = 1; /* reduce to a single ADC */
3148 }
3149
3150 /* single index for individual volumes ctls */
3151 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3152 spec->num_adc_nids = 1;
3153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 return 0;
3155}
3156
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003157/* parse capture source paths from the given pin and create imux items */
3158static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003159 int cfg_idx, int num_adcs,
3160 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003161{
3162 struct hda_gen_spec *spec = codec->spec;
3163 struct hda_input_mux *imux = &spec->input_mux;
3164 int imux_idx = imux->num_items;
3165 bool imux_added = false;
3166 int c;
3167
3168 for (c = 0; c < num_adcs; c++) {
3169 struct nid_path *path;
3170 hda_nid_t adc = spec->adc_nids[c];
3171
3172 if (!is_reachable_path(codec, pin, adc))
3173 continue;
3174 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3175 if (!path)
3176 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003177 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003178 spec->input_paths[imux_idx][c] =
3179 snd_hda_get_path_idx(codec, path);
3180
3181 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003182 if (spec->hp_mic_pin == pin)
3183 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003184 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003185 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003186 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003187 if (spec->dyn_adc_switch)
3188 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003189 }
3190 }
3191
3192 return 0;
3193}
3194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003196 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003198
Takashi Iwaic9700422013-01-18 10:17:30 +01003199/* fill the label for each input at first */
3200static int fill_input_pin_labels(struct hda_codec *codec)
3201{
3202 struct hda_gen_spec *spec = codec->spec;
3203 const struct auto_pin_cfg *cfg = &spec->autocfg;
3204 int i;
3205
3206 for (i = 0; i < cfg->num_inputs; i++) {
3207 hda_nid_t pin = cfg->inputs[i].pin;
3208 const char *label;
3209 int j, idx;
3210
3211 if (!is_input_pin(codec, pin))
3212 continue;
3213
3214 label = hda_get_autocfg_input_label(codec, cfg, i);
3215 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003216 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003217 if (spec->input_labels[j] &&
3218 !strcmp(spec->input_labels[j], label)) {
3219 idx = spec->input_label_idxs[j] + 1;
3220 break;
3221 }
3222 }
3223
3224 spec->input_labels[i] = label;
3225 spec->input_label_idxs[i] = idx;
3226 }
3227
3228 return 0;
3229}
3230
Takashi Iwai9dba2052013-01-18 10:01:15 +01003231#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3232
Takashi Iwai352f7f92012-12-19 12:52:06 +01003233static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003234{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003235 struct hda_gen_spec *spec = codec->spec;
3236 const struct auto_pin_cfg *cfg = &spec->autocfg;
3237 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003238 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003239 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003240 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003241
Takashi Iwai352f7f92012-12-19 12:52:06 +01003242 num_adcs = fill_adc_nids(codec);
3243 if (num_adcs < 0)
3244 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003245
Takashi Iwaic9700422013-01-18 10:17:30 +01003246 err = fill_input_pin_labels(codec);
3247 if (err < 0)
3248 return err;
3249
Takashi Iwai352f7f92012-12-19 12:52:06 +01003250 for (i = 0; i < cfg->num_inputs; i++) {
3251 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
Takashi Iwai352f7f92012-12-19 12:52:06 +01003253 pin = cfg->inputs[i].pin;
3254 if (!is_input_pin(codec, pin))
3255 continue;
3256
Takashi Iwai2c12c302013-01-10 09:33:29 +01003257 val = PIN_IN;
3258 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3259 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003260 if (pin != spec->hp_mic_pin)
3261 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003262
Takashi Iwai352f7f92012-12-19 12:52:06 +01003263 if (mixer) {
3264 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003265 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003266 spec->input_labels[i],
3267 spec->input_label_idxs[i],
3268 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003269 if (err < 0)
3270 return err;
3271 }
3272 }
3273
Takashi Iwaic9700422013-01-18 10:17:30 +01003274 err = parse_capture_source(codec, pin, i, num_adcs,
3275 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003276 if (err < 0)
3277 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003278
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003279 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003280 err = create_in_jack_mode(codec, pin);
3281 if (err < 0)
3282 return err;
3283 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003284 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003285
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003286 /* add stereo mix when explicitly enabled via hint */
Takashi Iwai74f14b32014-12-15 13:43:59 +01003287 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003288 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003289 "Stereo Mix", 0);
3290 if (err < 0)
3291 return err;
Takashi Iwai82d04e12014-12-15 13:40:42 +01003292 else
3293 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003294 }
3295
3296 return 0;
3297}
3298
3299
3300/*
3301 * input source mux
3302 */
3303
Takashi Iwaic697b712013-01-07 17:09:26 +01003304/* get the input path specified by the given adc and imux indices */
3305static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003306{
3307 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003308 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3309 snd_BUG();
3310 return NULL;
3311 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003312 if (spec->dyn_adc_switch)
3313 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003314 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003315 snd_BUG();
3316 return NULL;
3317 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003318 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003319}
3320
3321static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3322 unsigned int idx);
3323
3324static int mux_enum_info(struct snd_kcontrol *kcontrol,
3325 struct snd_ctl_elem_info *uinfo)
3326{
3327 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3328 struct hda_gen_spec *spec = codec->spec;
3329 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3330}
3331
3332static int mux_enum_get(struct snd_kcontrol *kcontrol,
3333 struct snd_ctl_elem_value *ucontrol)
3334{
3335 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3336 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003337 /* the ctls are created at once with multiple counts */
3338 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003339
3340 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3341 return 0;
3342}
3343
3344static int mux_enum_put(struct snd_kcontrol *kcontrol,
3345 struct snd_ctl_elem_value *ucontrol)
3346{
3347 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003348 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003349 return mux_select(codec, adc_idx,
3350 ucontrol->value.enumerated.item[0]);
3351}
3352
Takashi Iwai352f7f92012-12-19 12:52:06 +01003353static const struct snd_kcontrol_new cap_src_temp = {
3354 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3355 .name = "Input Source",
3356 .info = mux_enum_info,
3357 .get = mux_enum_get,
3358 .put = mux_enum_put,
3359};
3360
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003361/*
3362 * capture volume and capture switch ctls
3363 */
3364
Takashi Iwai352f7f92012-12-19 12:52:06 +01003365typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3366 struct snd_ctl_elem_value *ucontrol);
3367
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003368/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003369static int cap_put_caller(struct snd_kcontrol *kcontrol,
3370 struct snd_ctl_elem_value *ucontrol,
3371 put_call_t func, int type)
3372{
3373 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3374 struct hda_gen_spec *spec = codec->spec;
3375 const struct hda_input_mux *imux;
3376 struct nid_path *path;
3377 int i, adc_idx, err = 0;
3378
3379 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003380 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003381 mutex_lock(&codec->control_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003382 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003383 path = get_input_path(codec, adc_idx, i);
3384 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003385 continue;
3386 kcontrol->private_value = path->ctls[type];
3387 err = func(kcontrol, ucontrol);
3388 if (err < 0)
Takashi Iwaia551d912015-02-26 12:34:49 +01003389 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003390 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003391 mutex_unlock(&codec->control_mutex);
3392 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003393 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003394 return err;
3395}
3396
3397/* capture volume ctl callbacks */
3398#define cap_vol_info snd_hda_mixer_amp_volume_info
3399#define cap_vol_get snd_hda_mixer_amp_volume_get
3400#define cap_vol_tlv snd_hda_mixer_amp_tlv
3401
3402static int cap_vol_put(struct snd_kcontrol *kcontrol,
3403 struct snd_ctl_elem_value *ucontrol)
3404{
3405 return cap_put_caller(kcontrol, ucontrol,
3406 snd_hda_mixer_amp_volume_put,
3407 NID_PATH_VOL_CTL);
3408}
3409
3410static const struct snd_kcontrol_new cap_vol_temp = {
3411 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3412 .name = "Capture Volume",
3413 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3414 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3415 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3416 .info = cap_vol_info,
3417 .get = cap_vol_get,
3418 .put = cap_vol_put,
3419 .tlv = { .c = cap_vol_tlv },
3420};
3421
3422/* capture switch ctl callbacks */
3423#define cap_sw_info snd_ctl_boolean_stereo_info
3424#define cap_sw_get snd_hda_mixer_amp_switch_get
3425
3426static int cap_sw_put(struct snd_kcontrol *kcontrol,
3427 struct snd_ctl_elem_value *ucontrol)
3428{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003429 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003430 snd_hda_mixer_amp_switch_put,
3431 NID_PATH_MUTE_CTL);
3432}
3433
3434static const struct snd_kcontrol_new cap_sw_temp = {
3435 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3436 .name = "Capture Switch",
3437 .info = cap_sw_info,
3438 .get = cap_sw_get,
3439 .put = cap_sw_put,
3440};
3441
3442static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3443{
3444 hda_nid_t nid;
3445 int i, depth;
3446
3447 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3448 for (depth = 0; depth < 3; depth++) {
3449 if (depth >= path->depth)
3450 return -EINVAL;
3451 i = path->depth - depth - 1;
3452 nid = path->path[i];
3453 if (!path->ctls[NID_PATH_VOL_CTL]) {
3454 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3455 path->ctls[NID_PATH_VOL_CTL] =
3456 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3457 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3458 int idx = path->idx[i];
3459 if (!depth && codec->single_adc_amp)
3460 idx = 0;
3461 path->ctls[NID_PATH_VOL_CTL] =
3462 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3463 }
3464 }
3465 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3466 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3467 path->ctls[NID_PATH_MUTE_CTL] =
3468 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3469 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3470 int idx = path->idx[i];
3471 if (!depth && codec->single_adc_amp)
3472 idx = 0;
3473 path->ctls[NID_PATH_MUTE_CTL] =
3474 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3475 }
3476 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 return 0;
3479}
3480
Takashi Iwai352f7f92012-12-19 12:52:06 +01003481static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003483 struct hda_gen_spec *spec = codec->spec;
3484 struct auto_pin_cfg *cfg = &spec->autocfg;
3485 unsigned int val;
3486 int i;
3487
3488 if (!spec->inv_dmic_split)
3489 return false;
3490 for (i = 0; i < cfg->num_inputs; i++) {
3491 if (cfg->inputs[i].pin != nid)
3492 continue;
3493 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3494 return false;
3495 val = snd_hda_codec_get_pincfg(codec, nid);
3496 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3497 }
3498 return false;
3499}
3500
Takashi Iwaia90229e2013-01-18 14:10:00 +01003501/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003502static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3503 struct snd_ctl_elem_value *ucontrol)
3504{
3505 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3506 struct hda_gen_spec *spec = codec->spec;
3507 int ret;
3508
3509 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3510 if (ret < 0)
3511 return ret;
3512
Takashi Iwaia90229e2013-01-18 14:10:00 +01003513 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003514 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003515
3516 return ret;
3517}
3518
Takashi Iwai352f7f92012-12-19 12:52:06 +01003519static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3520 int idx, bool is_switch, unsigned int ctl,
3521 bool inv_dmic)
3522{
3523 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003524 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003525 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3526 const char *sfx = is_switch ? "Switch" : "Volume";
3527 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003528 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003529
3530 if (!ctl)
3531 return 0;
3532
3533 if (label)
3534 snprintf(tmpname, sizeof(tmpname),
3535 "%s Capture %s", label, sfx);
3536 else
3537 snprintf(tmpname, sizeof(tmpname),
3538 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003539 knew = add_control(spec, type, tmpname, idx,
3540 amp_val_replace_channels(ctl, chs));
3541 if (!knew)
3542 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003543 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003544 knew->put = cap_single_sw_put;
3545 if (!inv_dmic)
3546 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003547
3548 /* Make independent right kcontrol */
3549 if (label)
3550 snprintf(tmpname, sizeof(tmpname),
3551 "Inverted %s Capture %s", label, sfx);
3552 else
3553 snprintf(tmpname, sizeof(tmpname),
3554 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003555 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003556 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003557 if (!knew)
3558 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003559 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003560 knew->put = cap_single_sw_put;
3561 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003562}
3563
3564/* create single (and simple) capture volume and switch controls */
3565static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3566 unsigned int vol_ctl, unsigned int sw_ctl,
3567 bool inv_dmic)
3568{
3569 int err;
3570 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3571 if (err < 0)
3572 return err;
3573 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3574 if (err < 0)
3575 return err;
3576 return 0;
3577}
3578
3579/* create bound capture volume and switch controls */
3580static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3581 unsigned int vol_ctl, unsigned int sw_ctl)
3582{
3583 struct hda_gen_spec *spec = codec->spec;
3584 struct snd_kcontrol_new *knew;
3585
3586 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003587 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003588 if (!knew)
3589 return -ENOMEM;
3590 knew->index = idx;
3591 knew->private_value = vol_ctl;
3592 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3593 }
3594 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003595 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003596 if (!knew)
3597 return -ENOMEM;
3598 knew->index = idx;
3599 knew->private_value = sw_ctl;
3600 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3601 }
3602 return 0;
3603}
3604
3605/* return the vol ctl when used first in the imux list */
3606static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3607{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003608 struct nid_path *path;
3609 unsigned int ctl;
3610 int i;
3611
Takashi Iwaic697b712013-01-07 17:09:26 +01003612 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003613 if (!path)
3614 return 0;
3615 ctl = path->ctls[type];
3616 if (!ctl)
3617 return 0;
3618 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003619 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003620 if (path && path->ctls[type] == ctl)
3621 return 0;
3622 }
3623 return ctl;
3624}
3625
3626/* create individual capture volume and switch controls per input */
3627static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3628{
3629 struct hda_gen_spec *spec = codec->spec;
3630 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003631 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003632
3633 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003634 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003635 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003636
Takashi Iwaic9700422013-01-18 10:17:30 +01003637 idx = imux->items[i].index;
3638 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003639 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003640 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3641
3642 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003643 err = add_single_cap_ctl(codec,
3644 spec->input_labels[idx],
3645 spec->input_label_idxs[idx],
3646 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003647 get_first_cap_ctl(codec, i, type),
3648 inv_dmic);
3649 if (err < 0)
3650 return err;
3651 }
3652 }
3653 return 0;
3654}
3655
3656static int create_capture_mixers(struct hda_codec *codec)
3657{
3658 struct hda_gen_spec *spec = codec->spec;
3659 struct hda_input_mux *imux = &spec->input_mux;
3660 int i, n, nums, err;
3661
3662 if (spec->dyn_adc_switch)
3663 nums = 1;
3664 else
3665 nums = spec->num_adc_nids;
3666
3667 if (!spec->auto_mic && imux->num_items > 1) {
3668 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003669 const char *name;
3670 name = nums > 1 ? "Input Source" : "Capture Source";
3671 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003672 if (!knew)
3673 return -ENOMEM;
3674 knew->count = nums;
3675 }
3676
3677 for (n = 0; n < nums; n++) {
3678 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003679 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003680 bool inv_dmic = false;
3681 int vol, sw;
3682
3683 vol = sw = 0;
3684 for (i = 0; i < imux->num_items; i++) {
3685 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003686 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003687 if (!path)
3688 continue;
3689 parse_capvol_in_path(codec, path);
3690 if (!vol)
3691 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003692 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003693 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003694 if (!same_amp_caps(codec, vol,
3695 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3696 multi_cap_vol = true;
3697 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003698 if (!sw)
3699 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003700 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003701 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003702 if (!same_amp_caps(codec, sw,
3703 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3704 multi_cap_vol = true;
3705 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003706 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3707 inv_dmic = true;
3708 }
3709
3710 if (!multi)
3711 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3712 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003713 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003714 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3715 else
3716 err = create_multi_cap_vol_ctl(codec);
3717 if (err < 0)
3718 return err;
3719 }
3720
3721 return 0;
3722}
3723
3724/*
3725 * add mic boosts if needed
3726 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003727
3728/* check whether the given amp is feasible as a boost volume */
3729static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3730 int dir, int idx)
3731{
3732 unsigned int step;
3733
3734 if (!nid_has_volume(codec, nid, dir) ||
3735 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3736 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3737 return false;
3738
3739 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3740 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3741 if (step < 0x20)
3742 return false;
3743 return true;
3744}
3745
3746/* look for a boost amp in a widget close to the pin */
3747static unsigned int look_for_boost_amp(struct hda_codec *codec,
3748 struct nid_path *path)
3749{
3750 unsigned int val = 0;
3751 hda_nid_t nid;
3752 int depth;
3753
3754 for (depth = 0; depth < 3; depth++) {
3755 if (depth >= path->depth - 1)
3756 break;
3757 nid = path->path[depth];
3758 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3759 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3760 break;
3761 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3762 path->idx[depth])) {
3763 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3764 HDA_INPUT);
3765 break;
3766 }
3767 }
3768
3769 return val;
3770}
3771
Takashi Iwai352f7f92012-12-19 12:52:06 +01003772static int parse_mic_boost(struct hda_codec *codec)
3773{
3774 struct hda_gen_spec *spec = codec->spec;
3775 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003776 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003777 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003778
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003779 if (!spec->num_adc_nids)
3780 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003781
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003782 for (i = 0; i < imux->num_items; i++) {
3783 struct nid_path *path;
3784 unsigned int val;
3785 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003786 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003787
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003788 idx = imux->items[i].index;
3789 if (idx >= imux->num_items)
3790 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003791
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003792 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003793 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003794 continue;
3795
3796 path = get_input_path(codec, 0, i);
3797 if (!path)
3798 continue;
3799
3800 val = look_for_boost_amp(codec, path);
3801 if (!val)
3802 continue;
3803
3804 /* create a boost control */
3805 snprintf(boost_label, sizeof(boost_label),
3806 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003807 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3808 spec->input_label_idxs[idx], val))
3809 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003810
3811 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003812 }
3813 return 0;
3814}
3815
3816/*
3817 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3818 */
3819static void parse_digital(struct hda_codec *codec)
3820{
3821 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003822 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003823 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003824 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003825
3826 /* support multiple SPDIFs; the secondary is set up as a slave */
3827 nums = 0;
3828 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003829 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003830 dig_nid = look_for_dac(codec, pin, true);
3831 if (!dig_nid)
3832 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003833 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003834 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003835 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003836 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003837 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003838 path->pin_fixed = true; /* no jack detection */
Takashi Iwai196c17662013-01-04 15:01:40 +01003839 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003840 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003841 if (!nums) {
3842 spec->multiout.dig_out_nid = dig_nid;
3843 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3844 } else {
3845 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3846 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
Dan Carpenterd5764222014-05-14 17:18:31 +03003847 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003848 spec->slave_dig_outs[nums - 1] = dig_nid;
3849 }
3850 nums++;
3851 }
3852
3853 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003854 pin = spec->autocfg.dig_in_pin;
Takashi Iwai7639a062015-03-03 10:07:24 +01003855 for_each_hda_codec_node(dig_nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003856 unsigned int wcaps = get_wcaps(codec, dig_nid);
3857 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3858 continue;
3859 if (!(wcaps & AC_WCAP_DIGITAL))
3860 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003861 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003862 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003863 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003864 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003865 path->pin_fixed = true; /* no jack */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003866 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003867 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003868 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003869 break;
3870 }
3871 }
3872 }
3873}
3874
3875
3876/*
3877 * input MUX handling
3878 */
3879
3880static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3881
3882/* select the given imux item; either unmute exclusively or select the route */
3883static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3884 unsigned int idx)
3885{
3886 struct hda_gen_spec *spec = codec->spec;
3887 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003888 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003889
3890 imux = &spec->input_mux;
3891 if (!imux->num_items)
3892 return 0;
3893
3894 if (idx >= imux->num_items)
3895 idx = imux->num_items - 1;
3896 if (spec->cur_mux[adc_idx] == idx)
3897 return 0;
3898
Takashi Iwai55196ff2013-01-24 17:32:56 +01003899 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3900 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003901 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003902 if (old_path->active)
3903 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003904
3905 spec->cur_mux[adc_idx] = idx;
3906
Takashi Iwai967303d2013-02-19 17:12:42 +01003907 if (spec->hp_mic)
3908 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003909
3910 if (spec->dyn_adc_switch)
3911 dyn_adc_pcm_resetup(codec, idx);
3912
Takashi Iwaic697b712013-01-07 17:09:26 +01003913 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003914 if (!path)
3915 return 0;
3916 if (path->active)
3917 return 0;
3918 snd_hda_activate_path(codec, path, true, false);
3919 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003920 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003921 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003922 return 1;
3923}
3924
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003925/* power up/down widgets in the all paths that match with the given NID
3926 * as terminals (either start- or endpoint)
3927 *
3928 * returns the last changed NID, or zero if unchanged.
3929 */
3930static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
3931 int pin_state, int stream_state)
3932{
3933 struct hda_gen_spec *spec = codec->spec;
3934 hda_nid_t last, changed = 0;
3935 struct nid_path *path;
3936 int n;
3937
3938 for (n = 0; n < spec->paths.used; n++) {
3939 path = snd_array_elem(&spec->paths, n);
3940 if (path->path[0] == nid ||
3941 path->path[path->depth - 1] == nid) {
3942 bool pin_old = path->pin_enabled;
3943 bool stream_old = path->stream_enabled;
3944
3945 if (pin_state >= 0)
3946 path->pin_enabled = pin_state;
3947 if (stream_state >= 0)
3948 path->stream_enabled = stream_state;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003949 if ((!path->pin_fixed && path->pin_enabled != pin_old)
3950 || path->stream_enabled != stream_old) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003951 last = path_power_update(codec, path, true);
3952 if (last)
3953 changed = last;
3954 }
3955 }
3956 }
3957 return changed;
3958}
3959
3960/* power up/down the paths of the given pin according to the jack state;
3961 * power = 0/1 : only power up/down if it matches with the jack state,
3962 * < 0 : force power up/down to follow the jack sate
3963 *
3964 * returns the last changed NID, or zero if unchanged.
3965 */
3966static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
3967 int power)
3968{
3969 bool on;
3970
Takashi Iwai967b1302015-03-20 18:21:03 +01003971 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003972 return 0;
3973
3974 on = snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
3975 if (power >= 0 && on != power)
3976 return 0;
3977 return set_path_power(codec, pin, on, -1);
3978}
3979
3980static void pin_power_callback(struct hda_codec *codec,
3981 struct hda_jack_callback *jack,
3982 bool on)
3983{
3984 if (jack && jack->tbl->nid)
3985 sync_power_state_change(codec,
3986 set_pin_power_jack(codec, jack->tbl->nid, on));
3987}
3988
3989/* callback only doing power up -- called at first */
3990static void pin_power_up_callback(struct hda_codec *codec,
3991 struct hda_jack_callback *jack)
3992{
3993 pin_power_callback(codec, jack, true);
3994}
3995
3996/* callback only doing power down -- called at last */
3997static void pin_power_down_callback(struct hda_codec *codec,
3998 struct hda_jack_callback *jack)
3999{
4000 pin_power_callback(codec, jack, false);
4001}
4002
4003/* set up the power up/down callbacks */
4004static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4005 const hda_nid_t *pins, bool on)
4006{
4007 int i;
4008 hda_jack_callback_fn cb =
4009 on ? pin_power_up_callback : pin_power_down_callback;
4010
4011 for (i = 0; i < num_pins && pins[i]; i++) {
4012 if (is_jack_detectable(codec, pins[i]))
4013 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4014 else
4015 set_path_power(codec, pins[i], true, -1);
4016 }
4017}
4018
4019/* enabled power callback to each available I/O pin with jack detections;
4020 * the digital I/O pins are excluded because of the unreliable detectsion
4021 */
4022static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4023{
4024 struct hda_gen_spec *spec = codec->spec;
4025 struct auto_pin_cfg *cfg = &spec->autocfg;
4026 int i;
4027
Takashi Iwai967b1302015-03-20 18:21:03 +01004028 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004029 return;
4030 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4031 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4032 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4033 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4034 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4035 for (i = 0; i < cfg->num_inputs; i++)
4036 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4037}
4038
4039/* sync path power up/down with the jack states of given pins */
4040static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4041 const hda_nid_t *pins)
4042{
4043 int i;
4044
4045 for (i = 0; i < num_pins && pins[i]; i++)
4046 if (is_jack_detectable(codec, pins[i]))
4047 set_pin_power_jack(codec, pins[i], -1);
4048}
4049
4050/* sync path power up/down with pins; called at init and resume */
4051static void sync_all_pin_power_ctls(struct hda_codec *codec)
4052{
4053 struct hda_gen_spec *spec = codec->spec;
4054 struct auto_pin_cfg *cfg = &spec->autocfg;
4055 int i;
4056
Takashi Iwai967b1302015-03-20 18:21:03 +01004057 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004058 return;
4059 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4060 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4061 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4062 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4063 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4064 for (i = 0; i < cfg->num_inputs; i++)
4065 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4066}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004067
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004068/* add fake paths if not present yet */
4069static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4070 int num_pins, const hda_nid_t *pins)
4071{
4072 struct hda_gen_spec *spec = codec->spec;
4073 struct nid_path *path;
4074 int i;
4075
4076 for (i = 0; i < num_pins; i++) {
4077 if (!pins[i])
4078 break;
4079 if (get_nid_path(codec, nid, pins[i], 0))
4080 continue;
4081 path = snd_array_new(&spec->paths);
4082 if (!path)
4083 return -ENOMEM;
4084 memset(path, 0, sizeof(*path));
4085 path->depth = 2;
4086 path->path[0] = nid;
4087 path->path[1] = pins[i];
4088 path->active = true;
4089 }
4090 return 0;
4091}
4092
4093/* create fake paths to all outputs from beep */
4094static int add_fake_beep_paths(struct hda_codec *codec)
4095{
4096 struct hda_gen_spec *spec = codec->spec;
4097 struct auto_pin_cfg *cfg = &spec->autocfg;
4098 hda_nid_t nid = spec->beep_nid;
4099 int err;
4100
Takashi Iwai967b1302015-03-20 18:21:03 +01004101 if (!codec->power_save_node || !nid)
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004102 return 0;
4103 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4104 if (err < 0)
4105 return err;
4106 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4107 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4108 if (err < 0)
4109 return err;
4110 }
4111 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4112 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4113 cfg->speaker_pins);
4114 if (err < 0)
4115 return err;
4116 }
4117 return 0;
4118}
4119
4120/* power up/down beep widget and its output paths */
4121static void beep_power_hook(struct hda_beep *beep, bool on)
4122{
4123 set_path_power(beep->codec, beep->nid, -1, on);
4124}
4125
Takashi Iwai6b275b12015-03-20 18:11:05 +01004126/**
4127 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4128 * @codec: the HDA codec
4129 * @pin: NID of pin to fix
4130 */
4131int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4132{
4133 struct hda_gen_spec *spec = codec->spec;
4134 struct nid_path *path;
4135
4136 path = snd_array_new(&spec->paths);
4137 if (!path)
4138 return -ENOMEM;
4139 memset(path, 0, sizeof(*path));
4140 path->depth = 1;
4141 path->path[0] = pin;
4142 path->active = true;
4143 path->pin_fixed = true;
4144 path->stream_enabled = true;
4145 return 0;
4146}
4147EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4148
Takashi Iwai352f7f92012-12-19 12:52:06 +01004149/*
4150 * Jack detections for HP auto-mute and mic-switch
4151 */
4152
4153/* check each pin in the given array; returns true if any of them is plugged */
4154static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4155{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004156 int i;
4157 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004158
4159 for (i = 0; i < num_pins; i++) {
4160 hda_nid_t nid = pins[i];
4161 if (!nid)
4162 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01004163 /* don't detect pins retasked as inputs */
4164 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4165 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004166 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4167 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004168 }
4169 return present;
4170}
4171
4172/* standard HP/line-out auto-mute helper */
4173static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004174 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004175{
4176 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004177 int i;
4178
4179 for (i = 0; i < num_pins; i++) {
4180 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01004181 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004182 if (!nid)
4183 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004184
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004185 oldval = snd_hda_codec_get_pin_target(codec, nid);
4186 if (oldval & PIN_IN)
4187 continue; /* no mute for inputs */
4188
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004189 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004190 struct nid_path *path;
4191 hda_nid_t mute_nid;
4192
4193 path = snd_hda_get_path_from_idx(codec, paths[i]);
4194 if (!path)
4195 continue;
4196 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4197 if (!mute_nid)
4198 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004199 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004200 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004201 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004202 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004203 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004204 } else {
4205 /* don't reset VREF value in case it's controlling
4206 * the amp (see alc861_fixup_asus_amp_vref_0f())
4207 */
4208 if (spec->keep_vref_in_automute)
4209 val = oldval & ~PIN_HP;
4210 else
4211 val = 0;
4212 if (!mute)
4213 val |= oldval;
4214 /* here we call update_pin_ctl() so that the pinctl is
4215 * changed without changing the pinctl target value;
4216 * the original target value will be still referred at
4217 * the init / resume again
4218 */
4219 update_pin_ctl(codec, nid, val);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004220 }
4221
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01004222 set_pin_eapd(codec, nid, !mute);
Takashi Iwai967b1302015-03-20 18:21:03 +01004223 if (codec->power_save_node) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004224 bool on = !mute;
4225 if (on)
4226 on = snd_hda_jack_detect_state(codec, nid)
4227 != HDA_JACK_NOT_PRESENT;
4228 set_path_power(codec, nid, on, -1);
4229 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004230 }
4231}
4232
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004233/**
4234 * snd_hda_gen_update_outputs - Toggle outputs muting
4235 * @codec: the HDA codec
4236 *
4237 * Update the mute status of all outputs based on the current jack states.
4238 */
Takashi Iwai5d550e12012-12-19 15:16:44 +01004239void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004240{
4241 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004242 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004243 int on;
4244
4245 /* Control HP pins/amps depending on master_mute state;
4246 * in general, HP pins/amps control should be enabled in all cases,
4247 * but currently set only for master_mute, just to be safe
4248 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004249 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4250 paths = spec->out_paths;
4251 else
4252 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01004253 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004254 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004255
4256 if (!spec->automute_speaker)
4257 on = 0;
4258 else
4259 on = spec->hp_jack_present | spec->line_jack_present;
4260 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01004261 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004262 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4263 paths = spec->out_paths;
4264 else
4265 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004266 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004267 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004268
4269 /* toggle line-out mutes if needed, too */
4270 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4271 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4272 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4273 return;
4274 if (!spec->automute_lo)
4275 on = 0;
4276 else
4277 on = spec->hp_jack_present;
4278 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01004279 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004280 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004281 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004282 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004283}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004284EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004285
4286static void call_update_outputs(struct hda_codec *codec)
4287{
4288 struct hda_gen_spec *spec = codec->spec;
4289 if (spec->automute_hook)
4290 spec->automute_hook(codec);
4291 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01004292 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004293
4294 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4295 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4296 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004297}
4298
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004299/**
4300 * snd_hda_gen_hp_automute - standard HP-automute helper
4301 * @codec: the HDA codec
4302 * @jack: jack object, NULL for the whole
4303 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004304void snd_hda_gen_hp_automute(struct hda_codec *codec,
4305 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004306{
4307 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01004308 hda_nid_t *pins = spec->autocfg.hp_pins;
4309 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004310
Takashi Iwai92603c52013-01-22 07:46:31 +01004311 /* No detection for the first HP jack during indep-HP mode */
4312 if (spec->indep_hp_enabled) {
4313 pins++;
4314 num_pins--;
4315 }
4316
4317 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004318 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4319 return;
4320 call_update_outputs(codec);
4321}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004322EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004323
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004324/**
4325 * snd_hda_gen_line_automute - standard line-out-automute helper
4326 * @codec: the HDA codec
4327 * @jack: jack object, NULL for the whole
4328 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004329void snd_hda_gen_line_automute(struct hda_codec *codec,
4330 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004331{
4332 struct hda_gen_spec *spec = codec->spec;
4333
4334 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4335 return;
4336 /* check LO jack only when it's different from HP */
4337 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4338 return;
4339
4340 spec->line_jack_present =
4341 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4342 spec->autocfg.line_out_pins);
4343 if (!spec->automute_speaker || !spec->detect_lo)
4344 return;
4345 call_update_outputs(codec);
4346}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004347EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004348
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004349/**
4350 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4351 * @codec: the HDA codec
4352 * @jack: jack object, NULL for the whole
4353 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004354void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4355 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004356{
4357 struct hda_gen_spec *spec = codec->spec;
4358 int i;
4359
4360 if (!spec->auto_mic)
4361 return;
4362
4363 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01004364 hda_nid_t pin = spec->am_entry[i].pin;
4365 /* don't detect pins retasked as outputs */
4366 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4367 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004368 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004369 mux_select(codec, 0, spec->am_entry[i].idx);
4370 return;
4371 }
4372 }
4373 mux_select(codec, 0, spec->am_entry[0].idx);
4374}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004375EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004376
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004377/* call appropriate hooks */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004378static void call_hp_automute(struct hda_codec *codec,
4379 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004380{
4381 struct hda_gen_spec *spec = codec->spec;
4382 if (spec->hp_automute_hook)
4383 spec->hp_automute_hook(codec, jack);
4384 else
4385 snd_hda_gen_hp_automute(codec, jack);
4386}
4387
4388static void call_line_automute(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004389 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004390{
4391 struct hda_gen_spec *spec = codec->spec;
4392 if (spec->line_automute_hook)
4393 spec->line_automute_hook(codec, jack);
4394 else
4395 snd_hda_gen_line_automute(codec, jack);
4396}
4397
4398static void call_mic_autoswitch(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004399 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004400{
4401 struct hda_gen_spec *spec = codec->spec;
4402 if (spec->mic_autoswitch_hook)
4403 spec->mic_autoswitch_hook(codec, jack);
4404 else
4405 snd_hda_gen_mic_autoswitch(codec, jack);
4406}
4407
Takashi Iwai963afde2013-05-31 15:20:31 +02004408/* update jack retasking */
4409static void update_automute_all(struct hda_codec *codec)
4410{
4411 call_hp_automute(codec, NULL);
4412 call_line_automute(codec, NULL);
4413 call_mic_autoswitch(codec, NULL);
4414}
4415
Takashi Iwai352f7f92012-12-19 12:52:06 +01004416/*
4417 * Auto-Mute mode mixer enum support
4418 */
4419static int automute_mode_info(struct snd_kcontrol *kcontrol,
4420 struct snd_ctl_elem_info *uinfo)
4421{
4422 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4423 struct hda_gen_spec *spec = codec->spec;
4424 static const char * const texts3[] = {
4425 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004426 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Takashi Iwai352f7f92012-12-19 12:52:06 +01004428 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4429 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4430 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4431}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
Takashi Iwai352f7f92012-12-19 12:52:06 +01004433static int automute_mode_get(struct snd_kcontrol *kcontrol,
4434 struct snd_ctl_elem_value *ucontrol)
4435{
4436 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4437 struct hda_gen_spec *spec = codec->spec;
4438 unsigned int val = 0;
4439 if (spec->automute_speaker)
4440 val++;
4441 if (spec->automute_lo)
4442 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004443
Takashi Iwai352f7f92012-12-19 12:52:06 +01004444 ucontrol->value.enumerated.item[0] = val;
4445 return 0;
4446}
4447
4448static int automute_mode_put(struct snd_kcontrol *kcontrol,
4449 struct snd_ctl_elem_value *ucontrol)
4450{
4451 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4452 struct hda_gen_spec *spec = codec->spec;
4453
4454 switch (ucontrol->value.enumerated.item[0]) {
4455 case 0:
4456 if (!spec->automute_speaker && !spec->automute_lo)
4457 return 0;
4458 spec->automute_speaker = 0;
4459 spec->automute_lo = 0;
4460 break;
4461 case 1:
4462 if (spec->automute_speaker_possible) {
4463 if (!spec->automute_lo && spec->automute_speaker)
4464 return 0;
4465 spec->automute_speaker = 1;
4466 spec->automute_lo = 0;
4467 } else if (spec->automute_lo_possible) {
4468 if (spec->automute_lo)
4469 return 0;
4470 spec->automute_lo = 1;
4471 } else
4472 return -EINVAL;
4473 break;
4474 case 2:
4475 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4476 return -EINVAL;
4477 if (spec->automute_speaker && spec->automute_lo)
4478 return 0;
4479 spec->automute_speaker = 1;
4480 spec->automute_lo = 1;
4481 break;
4482 default:
4483 return -EINVAL;
4484 }
4485 call_update_outputs(codec);
4486 return 1;
4487}
4488
4489static const struct snd_kcontrol_new automute_mode_enum = {
4490 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4491 .name = "Auto-Mute Mode",
4492 .info = automute_mode_info,
4493 .get = automute_mode_get,
4494 .put = automute_mode_put,
4495};
4496
4497static int add_automute_mode_enum(struct hda_codec *codec)
4498{
4499 struct hda_gen_spec *spec = codec->spec;
4500
Takashi Iwai12c93df2012-12-19 14:38:33 +01004501 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004502 return -ENOMEM;
4503 return 0;
4504}
4505
4506/*
4507 * Check the availability of HP/line-out auto-mute;
4508 * Set up appropriately if really supported
4509 */
4510static int check_auto_mute_availability(struct hda_codec *codec)
4511{
4512 struct hda_gen_spec *spec = codec->spec;
4513 struct auto_pin_cfg *cfg = &spec->autocfg;
4514 int present = 0;
4515 int i, err;
4516
Takashi Iwaif72706b2013-01-16 18:20:07 +01004517 if (spec->suppress_auto_mute)
4518 return 0;
4519
Takashi Iwai352f7f92012-12-19 12:52:06 +01004520 if (cfg->hp_pins[0])
4521 present++;
4522 if (cfg->line_out_pins[0])
4523 present++;
4524 if (cfg->speaker_pins[0])
4525 present++;
4526 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004527 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004528
4529 if (!cfg->speaker_pins[0] &&
4530 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4531 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4532 sizeof(cfg->speaker_pins));
4533 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
Takashi Iwai352f7f92012-12-19 12:52:06 +01004536 if (!cfg->hp_pins[0] &&
4537 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4538 memcpy(cfg->hp_pins, cfg->line_out_pins,
4539 sizeof(cfg->hp_pins));
4540 cfg->hp_outs = cfg->line_outs;
4541 }
4542
4543 for (i = 0; i < cfg->hp_outs; i++) {
4544 hda_nid_t nid = cfg->hp_pins[i];
4545 if (!is_jack_detectable(codec, nid))
4546 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004547 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai62f949b2014-09-11 14:06:53 +02004548 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004549 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004550 spec->detect_hp = 1;
4551 }
4552
4553 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4554 if (cfg->speaker_outs)
4555 for (i = 0; i < cfg->line_outs; i++) {
4556 hda_nid_t nid = cfg->line_out_pins[i];
4557 if (!is_jack_detectable(codec, nid))
4558 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004559 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004560 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004561 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004562 spec->detect_lo = 1;
4563 }
4564 spec->automute_lo_possible = spec->detect_hp;
4565 }
4566
4567 spec->automute_speaker_possible = cfg->speaker_outs &&
4568 (spec->detect_hp || spec->detect_lo);
4569
4570 spec->automute_lo = spec->automute_lo_possible;
4571 spec->automute_speaker = spec->automute_speaker_possible;
4572
4573 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4574 /* create a control for automute mode */
4575 err = add_automute_mode_enum(codec);
4576 if (err < 0)
4577 return err;
4578 }
4579 return 0;
4580}
4581
Takashi Iwai352f7f92012-12-19 12:52:06 +01004582/* check whether all auto-mic pins are valid; setup indices if OK */
4583static bool auto_mic_check_imux(struct hda_codec *codec)
4584{
4585 struct hda_gen_spec *spec = codec->spec;
4586 const struct hda_input_mux *imux;
4587 int i;
4588
4589 imux = &spec->input_mux;
4590 for (i = 0; i < spec->am_num_entries; i++) {
4591 spec->am_entry[i].idx =
4592 find_idx_in_nid_list(spec->am_entry[i].pin,
4593 spec->imux_pins, imux->num_items);
4594 if (spec->am_entry[i].idx < 0)
4595 return false; /* no corresponding imux */
4596 }
4597
4598 /* we don't need the jack detection for the first pin */
4599 for (i = 1; i < spec->am_num_entries; i++)
4600 snd_hda_jack_detect_enable_callback(codec,
4601 spec->am_entry[i].pin,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004602 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004603 return true;
4604}
4605
4606static int compare_attr(const void *ap, const void *bp)
4607{
4608 const struct automic_entry *a = ap;
4609 const struct automic_entry *b = bp;
4610 return (int)(a->attr - b->attr);
4611}
4612
4613/*
4614 * Check the availability of auto-mic switch;
4615 * Set up if really supported
4616 */
4617static int check_auto_mic_availability(struct hda_codec *codec)
4618{
4619 struct hda_gen_spec *spec = codec->spec;
4620 struct auto_pin_cfg *cfg = &spec->autocfg;
4621 unsigned int types;
4622 int i, num_pins;
4623
Takashi Iwaid12daf62013-01-07 16:32:11 +01004624 if (spec->suppress_auto_mic)
4625 return 0;
4626
Takashi Iwai352f7f92012-12-19 12:52:06 +01004627 types = 0;
4628 num_pins = 0;
4629 for (i = 0; i < cfg->num_inputs; i++) {
4630 hda_nid_t nid = cfg->inputs[i].pin;
4631 unsigned int attr;
4632 attr = snd_hda_codec_get_pincfg(codec, nid);
4633 attr = snd_hda_get_input_pin_attr(attr);
4634 if (types & (1 << attr))
4635 return 0; /* already occupied */
4636 switch (attr) {
4637 case INPUT_PIN_ATTR_INT:
4638 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4639 return 0; /* invalid type */
4640 break;
4641 case INPUT_PIN_ATTR_UNUSED:
4642 return 0; /* invalid entry */
4643 default:
4644 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4645 return 0; /* invalid type */
4646 if (!spec->line_in_auto_switch &&
4647 cfg->inputs[i].type != AUTO_PIN_MIC)
4648 return 0; /* only mic is allowed */
4649 if (!is_jack_detectable(codec, nid))
4650 return 0; /* no unsol support */
4651 break;
4652 }
4653 if (num_pins >= MAX_AUTO_MIC_PINS)
4654 return 0;
4655 types |= (1 << attr);
4656 spec->am_entry[num_pins].pin = nid;
4657 spec->am_entry[num_pins].attr = attr;
4658 num_pins++;
4659 }
4660
4661 if (num_pins < 2)
4662 return 0;
4663
4664 spec->am_num_entries = num_pins;
4665 /* sort the am_entry in the order of attr so that the pin with a
4666 * higher attr will be selected when the jack is plugged.
4667 */
4668 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4669 compare_attr, NULL);
4670
4671 if (!auto_mic_check_imux(codec))
4672 return 0;
4673
4674 spec->auto_mic = 1;
4675 spec->num_adc_nids = 1;
4676 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004677 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004678 spec->am_entry[0].pin,
4679 spec->am_entry[1].pin,
4680 spec->am_entry[2].pin);
4681
4682 return 0;
4683}
4684
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004685/**
4686 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4687 * into power down
4688 * @codec: the HDA codec
4689 * @nid: NID to evalute
4690 * @power_state: target power state
4691 */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004692unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004693 hda_nid_t nid,
4694 unsigned int power_state)
4695{
Takashi Iwai7639a062015-03-03 10:07:24 +01004696 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004697 return power_state;
4698 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4699 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004700 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004701 return power_state;
4702 return AC_PWRST_D3;
4703}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004704EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004705
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004706/* mute all aamix inputs initially; parse up to the first leaves */
4707static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4708{
4709 int i, nums;
4710 const hda_nid_t *conn;
4711 bool has_amp;
4712
4713 nums = snd_hda_get_conn_list(codec, mix, &conn);
4714 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4715 for (i = 0; i < nums; i++) {
4716 if (has_amp)
Takashi Iwaief403ed2015-03-12 08:30:11 +01004717 update_amp(codec, mix, HDA_INPUT, i,
4718 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004719 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
Takashi Iwaief403ed2015-03-12 08:30:11 +01004720 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4721 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004722 }
4723}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004724
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004725/**
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004726 * snd_hda_gen_stream_pm - Stream power management callback
4727 * @codec: the HDA codec
4728 * @nid: audio widget
4729 * @on: power on/off flag
4730 *
Takashi Iwai967b1302015-03-20 18:21:03 +01004731 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004732 */
4733void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4734{
Takashi Iwai967b1302015-03-20 18:21:03 +01004735 if (codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004736 set_path_power(codec, nid, -1, on);
4737}
4738EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4739
4740/**
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004741 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4742 * set up the hda_gen_spec
4743 * @codec: the HDA codec
4744 * @cfg: Parsed pin configuration
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004745 *
4746 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004747 * or a negative error code
4748 */
4749int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004750 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004751{
4752 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004753 int err;
4754
Takashi Iwai1c70a582013-01-11 17:48:22 +01004755 parse_user_hints(codec);
4756
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004757 if (spec->mixer_nid && !spec->mixer_merge_nid)
4758 spec->mixer_merge_nid = spec->mixer_nid;
4759
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004760 if (cfg != &spec->autocfg) {
4761 spec->autocfg = *cfg;
4762 cfg = &spec->autocfg;
4763 }
4764
Takashi Iwai98bd1112013-03-22 14:53:50 +01004765 if (!spec->main_out_badness)
4766 spec->main_out_badness = &hda_main_out_badness;
4767 if (!spec->extra_out_badness)
4768 spec->extra_out_badness = &hda_extra_out_badness;
4769
David Henningsson6fc4cb92013-01-16 15:58:45 +01004770 fill_all_dac_nids(codec);
4771
Takashi Iwai352f7f92012-12-19 12:52:06 +01004772 if (!cfg->line_outs) {
4773 if (cfg->dig_outs || cfg->dig_in_pin) {
4774 spec->multiout.max_channels = 2;
4775 spec->no_analog = 1;
4776 goto dig_only;
4777 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004778 if (!cfg->num_inputs && !cfg->dig_in_pin)
4779 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004780 }
4781
4782 if (!spec->no_primary_hp &&
4783 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4784 cfg->line_outs <= cfg->hp_outs) {
4785 /* use HP as primary out */
4786 cfg->speaker_outs = cfg->line_outs;
4787 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4788 sizeof(cfg->speaker_pins));
4789 cfg->line_outs = cfg->hp_outs;
4790 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4791 cfg->hp_outs = 0;
4792 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4793 cfg->line_out_type = AUTO_PIN_HP_OUT;
4794 }
4795
4796 err = parse_output_paths(codec);
4797 if (err < 0)
4798 return err;
4799 err = create_multi_channel_mode(codec);
4800 if (err < 0)
4801 return err;
4802 err = create_multi_out_ctls(codec, cfg);
4803 if (err < 0)
4804 return err;
4805 err = create_hp_out_ctls(codec);
4806 if (err < 0)
4807 return err;
4808 err = create_speaker_out_ctls(codec);
4809 if (err < 0)
4810 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004811 err = create_indep_hp_ctls(codec);
4812 if (err < 0)
4813 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004814 err = create_loopback_mixing_ctl(codec);
4815 if (err < 0)
4816 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004817 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004818 if (err < 0)
4819 return err;
4820 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004821 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004822 return err;
4823
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004824 /* add power-down pin callbacks at first */
4825 add_all_pin_power_ctls(codec, false);
4826
Takashi Iwaia07a9492013-01-07 16:44:06 +01004827 spec->const_channel_count = spec->ext_channel_count;
4828 /* check the multiple speaker and headphone pins */
4829 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4830 spec->const_channel_count = max(spec->const_channel_count,
4831 cfg->speaker_outs * 2);
4832 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4833 spec->const_channel_count = max(spec->const_channel_count,
4834 cfg->hp_outs * 2);
4835 spec->multiout.max_channels = max(spec->ext_channel_count,
4836 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004837
4838 err = check_auto_mute_availability(codec);
4839 if (err < 0)
4840 return err;
4841
4842 err = check_dyn_adc_switch(codec);
4843 if (err < 0)
4844 return err;
4845
Takashi Iwai967303d2013-02-19 17:12:42 +01004846 err = check_auto_mic_availability(codec);
4847 if (err < 0)
4848 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004849
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004850 /* add stereo mix if available and not enabled yet */
4851 if (!spec->auto_mic && spec->mixer_nid &&
Takashi Iwai74f14b32014-12-15 13:43:59 +01004852 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4853 spec->input_mux.num_items > 1) {
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004854 err = parse_capture_source(codec, spec->mixer_nid,
4855 CFG_IDX_MIX, spec->num_all_adcs,
4856 "Stereo Mix", 0);
4857 if (err < 0)
4858 return err;
4859 }
4860
4861
Takashi Iwai352f7f92012-12-19 12:52:06 +01004862 err = create_capture_mixers(codec);
4863 if (err < 0)
4864 return err;
4865
4866 err = parse_mic_boost(codec);
4867 if (err < 0)
4868 return err;
4869
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004870 /* create "Headphone Mic Jack Mode" if no input selection is
4871 * available (or user specifies add_jack_modes hint)
4872 */
4873 if (spec->hp_mic_pin &&
4874 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4875 spec->add_jack_modes)) {
4876 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4877 if (err < 0)
4878 return err;
4879 }
4880
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004881 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004882 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4883 err = create_out_jack_modes(codec, cfg->line_outs,
4884 cfg->line_out_pins);
4885 if (err < 0)
4886 return err;
4887 }
4888 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4889 err = create_out_jack_modes(codec, cfg->hp_outs,
4890 cfg->hp_pins);
4891 if (err < 0)
4892 return err;
4893 }
4894 }
4895
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004896 /* add power-up pin callbacks at last */
4897 add_all_pin_power_ctls(codec, true);
4898
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004899 /* mute all aamix input initially */
4900 if (spec->mixer_nid)
4901 mute_all_mixer_nid(codec, spec->mixer_nid);
4902
Takashi Iwai352f7f92012-12-19 12:52:06 +01004903 dig_only:
4904 parse_digital(codec);
4905
Takashi Iwai967b1302015-03-20 18:21:03 +01004906 if (spec->power_down_unused || codec->power_save_node)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004907 codec->power_filter = snd_hda_gen_path_power_filter;
4908
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004909 if (!spec->no_analog && spec->beep_nid) {
4910 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4911 if (err < 0)
4912 return err;
Takashi Iwai967b1302015-03-20 18:21:03 +01004913 if (codec->beep && codec->power_save_node) {
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004914 err = add_fake_beep_paths(codec);
4915 if (err < 0)
4916 return err;
4917 codec->beep->power_hook = beep_power_hook;
4918 }
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004919 }
4920
Takashi Iwai352f7f92012-12-19 12:52:06 +01004921 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004923EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
4925
4926/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004927 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004929
4930/* slave controls for virtual master */
4931static const char * const slave_pfxs[] = {
4932 "Front", "Surround", "Center", "LFE", "Side",
4933 "Headphone", "Speaker", "Mono", "Line Out",
4934 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004935 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4936 "Headphone Front", "Headphone Surround", "Headphone CLFE",
David Henningsson03ad6a82014-10-16 15:33:45 +02004937 "Headphone Side", "Headphone+LO", "Speaker+LO",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004938 NULL,
4939};
4940
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004941/**
4942 * snd_hda_gen_build_controls - Build controls from the parsed results
4943 * @codec: the HDA codec
4944 *
4945 * Pass this to build_controls patch_ops.
4946 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004947int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004949 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
Takashi Iwai36502d02012-12-19 15:15:10 +01004952 if (spec->kctls.used) {
4953 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4954 if (err < 0)
4955 return err;
4956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
Takashi Iwai352f7f92012-12-19 12:52:06 +01004958 if (spec->multiout.dig_out_nid) {
4959 err = snd_hda_create_dig_out_ctls(codec,
4960 spec->multiout.dig_out_nid,
4961 spec->multiout.dig_out_nid,
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004962 spec->pcm_rec[1]->pcm_type);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004963 if (err < 0)
4964 return err;
4965 if (!spec->no_analog) {
4966 err = snd_hda_create_spdif_share_sw(codec,
4967 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 if (err < 0)
4969 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004970 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 }
4972 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004973 if (spec->dig_in_nid) {
4974 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4975 if (err < 0)
4976 return err;
4977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
Takashi Iwai352f7f92012-12-19 12:52:06 +01004979 /* if we have no master control, let's create it */
4980 if (!spec->no_analog &&
4981 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004982 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004983 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004984 "Playback Volume");
4985 if (err < 0)
4986 return err;
4987 }
4988 if (!spec->no_analog &&
4989 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4990 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4991 NULL, slave_pfxs,
4992 "Playback Switch",
4993 true, &spec->vmaster_mute.sw_kctl);
4994 if (err < 0)
4995 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02004996 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01004997 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4998 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02004999 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5000 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
Takashi Iwai352f7f92012-12-19 12:52:06 +01005003 free_kctls(spec); /* no longer needed */
5004
Takashi Iwai352f7f92012-12-19 12:52:06 +01005005 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5006 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 return err;
5008
5009 return 0;
5010}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005011EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005012
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013
5014/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01005015 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005018static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5019 struct hda_codec *codec,
5020 struct snd_pcm_substream *substream,
5021 int action)
5022{
5023 struct hda_gen_spec *spec = codec->spec;
5024 if (spec->pcm_playback_hook)
5025 spec->pcm_playback_hook(hinfo, codec, substream, action);
5026}
5027
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005028static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5029 struct hda_codec *codec,
5030 struct snd_pcm_substream *substream,
5031 int action)
5032{
5033 struct hda_gen_spec *spec = codec->spec;
5034 if (spec->pcm_capture_hook)
5035 spec->pcm_capture_hook(hinfo, codec, substream, action);
5036}
5037
Takashi Iwai352f7f92012-12-19 12:52:06 +01005038/*
5039 * Analog playback callbacks
5040 */
5041static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5042 struct hda_codec *codec,
5043 struct snd_pcm_substream *substream)
5044{
5045 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005046 int err;
5047
5048 mutex_lock(&spec->pcm_mutex);
5049 err = snd_hda_multi_out_analog_open(codec,
5050 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005051 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005052 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005053 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005054 call_pcm_playback_hook(hinfo, codec, substream,
5055 HDA_GEN_PCM_ACT_OPEN);
5056 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005057 mutex_unlock(&spec->pcm_mutex);
5058 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005059}
5060
5061static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01005062 struct hda_codec *codec,
5063 unsigned int stream_tag,
5064 unsigned int format,
5065 struct snd_pcm_substream *substream)
5066{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005067 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005068 int err;
5069
5070 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5071 stream_tag, format, substream);
5072 if (!err)
5073 call_pcm_playback_hook(hinfo, codec, substream,
5074 HDA_GEN_PCM_ACT_PREPARE);
5075 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005076}
Takashi Iwai97ec5582006-03-21 11:29:07 +01005077
Takashi Iwai352f7f92012-12-19 12:52:06 +01005078static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5079 struct hda_codec *codec,
5080 struct snd_pcm_substream *substream)
5081{
5082 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005083 int err;
5084
5085 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5086 if (!err)
5087 call_pcm_playback_hook(hinfo, codec, substream,
5088 HDA_GEN_PCM_ACT_CLEANUP);
5089 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005090}
5091
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005092static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5093 struct hda_codec *codec,
5094 struct snd_pcm_substream *substream)
5095{
5096 struct hda_gen_spec *spec = codec->spec;
5097 mutex_lock(&spec->pcm_mutex);
5098 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005099 call_pcm_playback_hook(hinfo, codec, substream,
5100 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005101 mutex_unlock(&spec->pcm_mutex);
5102 return 0;
5103}
5104
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005105static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5106 struct hda_codec *codec,
5107 struct snd_pcm_substream *substream)
5108{
5109 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5110 return 0;
5111}
5112
5113static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5114 struct hda_codec *codec,
5115 unsigned int stream_tag,
5116 unsigned int format,
5117 struct snd_pcm_substream *substream)
5118{
5119 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5120 call_pcm_capture_hook(hinfo, codec, substream,
5121 HDA_GEN_PCM_ACT_PREPARE);
5122 return 0;
5123}
5124
5125static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5126 struct hda_codec *codec,
5127 struct snd_pcm_substream *substream)
5128{
5129 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5130 call_pcm_capture_hook(hinfo, codec, substream,
5131 HDA_GEN_PCM_ACT_CLEANUP);
5132 return 0;
5133}
5134
5135static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5136 struct hda_codec *codec,
5137 struct snd_pcm_substream *substream)
5138{
5139 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5140 return 0;
5141}
5142
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005143static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5144 struct hda_codec *codec,
5145 struct snd_pcm_substream *substream)
5146{
5147 struct hda_gen_spec *spec = codec->spec;
5148 int err = 0;
5149
5150 mutex_lock(&spec->pcm_mutex);
5151 if (!spec->indep_hp_enabled)
5152 err = -EBUSY;
5153 else
5154 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005155 call_pcm_playback_hook(hinfo, codec, substream,
5156 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005157 mutex_unlock(&spec->pcm_mutex);
5158 return err;
5159}
5160
5161static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5162 struct hda_codec *codec,
5163 struct snd_pcm_substream *substream)
5164{
5165 struct hda_gen_spec *spec = codec->spec;
5166 mutex_lock(&spec->pcm_mutex);
5167 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005168 call_pcm_playback_hook(hinfo, codec, substream,
5169 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005170 mutex_unlock(&spec->pcm_mutex);
5171 return 0;
5172}
5173
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005174static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5175 struct hda_codec *codec,
5176 unsigned int stream_tag,
5177 unsigned int format,
5178 struct snd_pcm_substream *substream)
5179{
5180 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5181 call_pcm_playback_hook(hinfo, codec, substream,
5182 HDA_GEN_PCM_ACT_PREPARE);
5183 return 0;
5184}
5185
5186static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5187 struct hda_codec *codec,
5188 struct snd_pcm_substream *substream)
5189{
5190 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5191 call_pcm_playback_hook(hinfo, codec, substream,
5192 HDA_GEN_PCM_ACT_CLEANUP);
5193 return 0;
5194}
5195
Takashi Iwai352f7f92012-12-19 12:52:06 +01005196/*
5197 * Digital out
5198 */
5199static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5200 struct hda_codec *codec,
5201 struct snd_pcm_substream *substream)
5202{
5203 struct hda_gen_spec *spec = codec->spec;
5204 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5205}
5206
5207static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5208 struct hda_codec *codec,
5209 unsigned int stream_tag,
5210 unsigned int format,
5211 struct snd_pcm_substream *substream)
5212{
5213 struct hda_gen_spec *spec = codec->spec;
5214 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5215 stream_tag, format, substream);
5216}
5217
5218static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5219 struct hda_codec *codec,
5220 struct snd_pcm_substream *substream)
5221{
5222 struct hda_gen_spec *spec = codec->spec;
5223 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5224}
5225
5226static int dig_playback_pcm_close(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_close(codec, &spec->multiout);
5232}
5233
5234/*
5235 * Analog capture
5236 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005237#define alt_capture_pcm_open capture_pcm_open
5238#define alt_capture_pcm_close capture_pcm_close
5239
Takashi Iwai352f7f92012-12-19 12:52:06 +01005240static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5241 struct hda_codec *codec,
5242 unsigned int stream_tag,
5243 unsigned int format,
5244 struct snd_pcm_substream *substream)
5245{
5246 struct hda_gen_spec *spec = codec->spec;
5247
5248 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01005249 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005250 call_pcm_capture_hook(hinfo, codec, substream,
5251 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005252 return 0;
5253}
5254
Takashi Iwai352f7f92012-12-19 12:52:06 +01005255static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5256 struct hda_codec *codec,
5257 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01005258{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005259 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01005260
Takashi Iwai352f7f92012-12-19 12:52:06 +01005261 snd_hda_codec_cleanup_stream(codec,
5262 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005263 call_pcm_capture_hook(hinfo, codec, substream,
5264 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005265 return 0;
5266}
5267
Takashi Iwai352f7f92012-12-19 12:52:06 +01005268/*
5269 */
5270static const struct hda_pcm_stream pcm_analog_playback = {
5271 .substreams = 1,
5272 .channels_min = 2,
5273 .channels_max = 8,
5274 /* NID is set in build_pcms */
5275 .ops = {
5276 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005277 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005278 .prepare = playback_pcm_prepare,
5279 .cleanup = playback_pcm_cleanup
5280 },
5281};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282
Takashi Iwai352f7f92012-12-19 12:52:06 +01005283static const struct hda_pcm_stream pcm_analog_capture = {
5284 .substreams = 1,
5285 .channels_min = 2,
5286 .channels_max = 2,
5287 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005288 .ops = {
5289 .open = capture_pcm_open,
5290 .close = capture_pcm_close,
5291 .prepare = capture_pcm_prepare,
5292 .cleanup = capture_pcm_cleanup
5293 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005294};
5295
5296static const struct hda_pcm_stream pcm_analog_alt_playback = {
5297 .substreams = 1,
5298 .channels_min = 2,
5299 .channels_max = 2,
5300 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005301 .ops = {
5302 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005303 .close = alt_playback_pcm_close,
5304 .prepare = alt_playback_pcm_prepare,
5305 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005306 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005307};
5308
5309static const struct hda_pcm_stream pcm_analog_alt_capture = {
5310 .substreams = 2, /* can be overridden */
5311 .channels_min = 2,
5312 .channels_max = 2,
5313 /* NID is set in build_pcms */
5314 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005315 .open = alt_capture_pcm_open,
5316 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005317 .prepare = alt_capture_pcm_prepare,
5318 .cleanup = alt_capture_pcm_cleanup
5319 },
5320};
5321
5322static const struct hda_pcm_stream pcm_digital_playback = {
5323 .substreams = 1,
5324 .channels_min = 2,
5325 .channels_max = 2,
5326 /* NID is set in build_pcms */
5327 .ops = {
5328 .open = dig_playback_pcm_open,
5329 .close = dig_playback_pcm_close,
5330 .prepare = dig_playback_pcm_prepare,
5331 .cleanup = dig_playback_pcm_cleanup
5332 },
5333};
5334
5335static const struct hda_pcm_stream pcm_digital_capture = {
5336 .substreams = 1,
5337 .channels_min = 2,
5338 .channels_max = 2,
5339 /* NID is set in build_pcms */
5340};
5341
5342/* Used by build_pcms to flag that a PCM has no playback stream */
5343static const struct hda_pcm_stream pcm_null_stream = {
5344 .substreams = 0,
5345 .channels_min = 0,
5346 .channels_max = 0,
5347};
5348
5349/*
5350 * dynamic changing ADC PCM streams
5351 */
5352static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5353{
5354 struct hda_gen_spec *spec = codec->spec;
5355 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5356
5357 if (spec->cur_adc && spec->cur_adc != new_adc) {
5358 /* stream is running, let's swap the current ADC */
5359 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5360 spec->cur_adc = new_adc;
5361 snd_hda_codec_setup_stream(codec, new_adc,
5362 spec->cur_adc_stream_tag, 0,
5363 spec->cur_adc_format);
5364 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005366 return false;
5367}
5368
5369/* analog capture with dynamic dual-adc changes */
5370static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5371 struct hda_codec *codec,
5372 unsigned int stream_tag,
5373 unsigned int format,
5374 struct snd_pcm_substream *substream)
5375{
5376 struct hda_gen_spec *spec = codec->spec;
5377 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5378 spec->cur_adc_stream_tag = stream_tag;
5379 spec->cur_adc_format = format;
5380 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5381 return 0;
5382}
5383
5384static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5385 struct hda_codec *codec,
5386 struct snd_pcm_substream *substream)
5387{
5388 struct hda_gen_spec *spec = codec->spec;
5389 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5390 spec->cur_adc = 0;
5391 return 0;
5392}
5393
5394static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5395 .substreams = 1,
5396 .channels_min = 2,
5397 .channels_max = 2,
5398 .nid = 0, /* fill later */
5399 .ops = {
5400 .prepare = dyn_adc_capture_pcm_prepare,
5401 .cleanup = dyn_adc_capture_pcm_cleanup
5402 },
5403};
5404
Takashi Iwaif873e532012-12-20 16:58:39 +01005405static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5406 const char *chip_name)
5407{
5408 char *p;
5409
5410 if (*str)
5411 return;
5412 strlcpy(str, chip_name, len);
5413
5414 /* drop non-alnum chars after a space */
5415 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5416 if (!isalnum(p[1])) {
5417 *p = 0;
5418 break;
5419 }
5420 }
5421 strlcat(str, sfx, len);
5422}
5423
Takashi Iwaifb83b632015-03-16 23:34:34 +01005424/* copy PCM stream info from @default_str, and override non-NULL entries
5425 * from @spec_str and @nid
5426 */
5427static void setup_pcm_stream(struct hda_pcm_stream *str,
5428 const struct hda_pcm_stream *default_str,
5429 const struct hda_pcm_stream *spec_str,
5430 hda_nid_t nid)
5431{
5432 *str = *default_str;
5433 if (nid)
5434 str->nid = nid;
5435 if (spec_str) {
5436 if (spec_str->substreams)
5437 str->substreams = spec_str->substreams;
5438 if (spec_str->channels_min)
5439 str->channels_min = spec_str->channels_min;
5440 if (spec_str->channels_max)
5441 str->channels_max = spec_str->channels_max;
5442 if (spec_str->rates)
5443 str->rates = spec_str->rates;
5444 if (spec_str->formats)
5445 str->formats = spec_str->formats;
5446 if (spec_str->maxbps)
5447 str->maxbps = spec_str->maxbps;
5448 }
5449}
5450
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005451/**
5452 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5453 * @codec: the HDA codec
5454 *
5455 * Pass this to build_pcms patch_ops.
5456 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005457int snd_hda_gen_build_pcms(struct hda_codec *codec)
5458{
5459 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005460 struct hda_pcm *info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005461 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
Takashi Iwai352f7f92012-12-19 12:52:06 +01005463 if (spec->no_analog)
5464 goto skip_analog;
5465
Takashi Iwaif873e532012-12-20 16:58:39 +01005466 fill_pcm_stream_name(spec->stream_name_analog,
5467 sizeof(spec->stream_name_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005468 " Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005469 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5470 if (!info)
5471 return -ENOMEM;
5472 spec->pcm_rec[0] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005473
5474 if (spec->multiout.num_dacs > 0) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005475 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5476 &pcm_analog_playback,
5477 spec->stream_analog_playback,
5478 spec->multiout.dac_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005479 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5480 spec->multiout.max_channels;
5481 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5482 spec->autocfg.line_outs == 2)
5483 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5484 snd_pcm_2_1_chmaps;
5485 }
5486 if (spec->num_adc_nids) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005487 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5488 (spec->dyn_adc_switch ?
5489 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5490 spec->stream_analog_capture,
5491 spec->adc_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005492 }
5493
Takashi Iwai352f7f92012-12-19 12:52:06 +01005494 skip_analog:
5495 /* SPDIF for stream index #1 */
5496 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005497 fill_pcm_stream_name(spec->stream_name_digital,
5498 sizeof(spec->stream_name_digital),
Takashi Iwai7639a062015-03-03 10:07:24 +01005499 " Digital", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005500 info = snd_hda_codec_pcm_new(codec, "%s",
5501 spec->stream_name_digital);
5502 if (!info)
5503 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005504 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005505 spec->pcm_rec[1] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005506 if (spec->dig_out_type)
5507 info->pcm_type = spec->dig_out_type;
5508 else
5509 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005510 if (spec->multiout.dig_out_nid)
5511 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5512 &pcm_digital_playback,
5513 spec->stream_digital_playback,
5514 spec->multiout.dig_out_nid);
5515 if (spec->dig_in_nid)
5516 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5517 &pcm_digital_capture,
5518 spec->stream_digital_capture,
5519 spec->dig_in_nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005520 }
5521
5522 if (spec->no_analog)
5523 return 0;
5524
5525 /* If the use of more than one ADC is requested for the current
5526 * model, configure a second analog capture-only PCM.
5527 */
5528 have_multi_adcs = (spec->num_adc_nids > 1) &&
5529 !spec->dyn_adc_switch && !spec->auto_mic;
5530 /* Additional Analaog capture for index #2 */
5531 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005532 fill_pcm_stream_name(spec->stream_name_alt_analog,
5533 sizeof(spec->stream_name_alt_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005534 " Alt Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005535 info = snd_hda_codec_pcm_new(codec, "%s",
5536 spec->stream_name_alt_analog);
5537 if (!info)
5538 return -ENOMEM;
5539 spec->pcm_rec[2] = info;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005540 if (spec->alt_dac_nid)
5541 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5542 &pcm_analog_alt_playback,
5543 spec->stream_analog_alt_playback,
5544 spec->alt_dac_nid);
5545 else
5546 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5547 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005548 if (have_multi_adcs) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005549 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5550 &pcm_analog_alt_capture,
5551 spec->stream_analog_alt_capture,
5552 spec->adc_nids[1]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005553 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5554 spec->num_adc_nids - 1;
5555 } else {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005556 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5557 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 }
5560
5561 return 0;
5562}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005563EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005564
5565
5566/*
5567 * Standard auto-parser initializations
5568 */
5569
Takashi Iwaid4156932013-01-07 10:08:02 +01005570/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005571static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005572{
5573 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005574 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005575
Takashi Iwai196c17662013-01-04 15:01:40 +01005576 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005577 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005578 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005579 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005580 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005581 snd_hda_activate_path(codec, path, path->active,
5582 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005583 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005584}
5585
5586/* initialize primary output paths */
5587static void init_multi_out(struct hda_codec *codec)
5588{
5589 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005590 int i;
5591
Takashi Iwaid4156932013-01-07 10:08:02 +01005592 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005593 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005594}
5595
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005596
Takashi Iwai2c12c302013-01-10 09:33:29 +01005597static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005598{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005599 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005600
Takashi Iwaid4156932013-01-07 10:08:02 +01005601 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005602 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005603}
5604
5605/* initialize hp and speaker paths */
5606static void init_extra_out(struct hda_codec *codec)
5607{
5608 struct hda_gen_spec *spec = codec->spec;
5609
5610 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005611 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005612 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5613 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005614 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005615}
5616
5617/* initialize multi-io paths */
5618static void init_multi_io(struct hda_codec *codec)
5619{
5620 struct hda_gen_spec *spec = codec->spec;
5621 int i;
5622
5623 for (i = 0; i < spec->multi_ios; i++) {
5624 hda_nid_t pin = spec->multi_io[i].pin;
5625 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005626 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005627 if (!path)
5628 continue;
5629 if (!spec->multi_io[i].ctl_in)
5630 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005631 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005632 snd_hda_activate_path(codec, path, path->active,
5633 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005634 }
5635}
5636
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005637static void init_aamix_paths(struct hda_codec *codec)
5638{
5639 struct hda_gen_spec *spec = codec->spec;
5640
5641 if (!spec->have_aamix_ctl)
5642 return;
5643 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5644 spec->aamix_out_paths[0],
5645 spec->autocfg.line_out_type);
5646 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5647 spec->aamix_out_paths[1],
5648 AUTO_PIN_HP_OUT);
5649 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5650 spec->aamix_out_paths[2],
5651 AUTO_PIN_SPEAKER_OUT);
5652}
5653
Takashi Iwai352f7f92012-12-19 12:52:06 +01005654/* set up input pins and loopback paths */
5655static void init_analog_input(struct hda_codec *codec)
5656{
5657 struct hda_gen_spec *spec = codec->spec;
5658 struct auto_pin_cfg *cfg = &spec->autocfg;
5659 int i;
5660
5661 for (i = 0; i < cfg->num_inputs; i++) {
5662 hda_nid_t nid = cfg->inputs[i].pin;
5663 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005664 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005665
5666 /* init loopback inputs */
5667 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005668 resume_path_from_idx(codec, spec->loopback_paths[i]);
5669 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005670 }
5671 }
5672}
5673
5674/* initialize ADC paths */
5675static void init_input_src(struct hda_codec *codec)
5676{
5677 struct hda_gen_spec *spec = codec->spec;
5678 struct hda_input_mux *imux = &spec->input_mux;
5679 struct nid_path *path;
5680 int i, c, nums;
5681
5682 if (spec->dyn_adc_switch)
5683 nums = 1;
5684 else
5685 nums = spec->num_adc_nids;
5686
5687 for (c = 0; c < nums; c++) {
5688 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005689 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005690 if (path) {
5691 bool active = path->active;
5692 if (i == spec->cur_mux[c])
5693 active = true;
5694 snd_hda_activate_path(codec, path, active, false);
5695 }
5696 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005697 if (spec->hp_mic)
5698 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005699 }
5700
Takashi Iwai352f7f92012-12-19 12:52:06 +01005701 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005702 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005703}
5704
5705/* set right pin controls for digital I/O */
5706static void init_digital(struct hda_codec *codec)
5707{
5708 struct hda_gen_spec *spec = codec->spec;
5709 int i;
5710 hda_nid_t pin;
5711
Takashi Iwaid4156932013-01-07 10:08:02 +01005712 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005713 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005714 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005715 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005716 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005717 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005718 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005719}
5720
Takashi Iwai973e4972012-12-20 15:16:09 +01005721/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5722 * invalid unsol tags by some reason
5723 */
5724static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5725{
5726 int i;
5727
5728 for (i = 0; i < codec->init_pins.used; i++) {
5729 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5730 hda_nid_t nid = pin->nid;
5731 if (is_jack_detectable(codec, nid) &&
5732 !snd_hda_jack_tbl_get(codec, nid))
5733 snd_hda_codec_update_cache(codec, nid, 0,
5734 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5735 }
5736}
5737
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005738/**
5739 * snd_hda_gen_init - initialize the generic spec
5740 * @codec: the HDA codec
5741 *
5742 * This can be put as patch_ops init function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005743 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005744int snd_hda_gen_init(struct hda_codec *codec)
5745{
5746 struct hda_gen_spec *spec = codec->spec;
5747
5748 if (spec->init_hook)
5749 spec->init_hook(codec);
5750
5751 snd_hda_apply_verbs(codec);
5752
5753 init_multi_out(codec);
5754 init_extra_out(codec);
5755 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005756 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005757 init_analog_input(codec);
5758 init_input_src(codec);
5759 init_digital(codec);
5760
Takashi Iwai973e4972012-12-20 15:16:09 +01005761 clear_unsol_on_unused_pins(codec);
5762
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01005763 sync_all_pin_power_ctls(codec);
5764
Takashi Iwai352f7f92012-12-19 12:52:06 +01005765 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005766 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005767
Takashi Iwaia551d912015-02-26 12:34:49 +01005768 regcache_sync(codec->core.regmap);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005769
Takashi Iwai352f7f92012-12-19 12:52:06 +01005770 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5771 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5772
5773 hda_call_check_power_status(codec, 0x01);
5774 return 0;
5775}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005776EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005777
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005778/**
5779 * snd_hda_gen_free - free the generic spec
5780 * @codec: the HDA codec
5781 *
5782 * This can be put as patch_ops free function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005783 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005784void snd_hda_gen_free(struct hda_codec *codec)
5785{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01005786 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005787 snd_hda_gen_spec_free(codec->spec);
5788 kfree(codec->spec);
5789 codec->spec = NULL;
5790}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005791EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005792
5793#ifdef CONFIG_PM
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005794/**
5795 * snd_hda_gen_check_power_status - check the loopback power save state
5796 * @codec: the HDA codec
5797 * @nid: NID to inspect
5798 *
5799 * This can be put as patch_ops check_power_status function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005800 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005801int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5802{
5803 struct hda_gen_spec *spec = codec->spec;
5804 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5805}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005806EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005807#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005808
5809
5810/*
5811 * the generic codec support
5812 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813
Takashi Iwai352f7f92012-12-19 12:52:06 +01005814static const struct hda_codec_ops generic_patch_ops = {
5815 .build_controls = snd_hda_gen_build_controls,
5816 .build_pcms = snd_hda_gen_build_pcms,
5817 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005818 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005819 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005820#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005821 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005822#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823};
5824
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005825/*
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005826 * snd_hda_parse_generic_codec - Generic codec parser
5827 * @codec: the HDA codec
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005828 */
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005829static int snd_hda_parse_generic_codec(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005831 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 int err;
5833
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005834 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005835 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005837 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005840 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5841 if (err < 0)
5842 return err;
5843
5844 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005845 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 goto error;
5847
5848 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 return 0;
5850
Takashi Iwai352f7f92012-12-19 12:52:06 +01005851error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005852 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 return err;
5854}
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005855
5856static const struct hda_codec_preset snd_hda_preset_generic[] = {
5857 { .id = HDA_CODEC_ID_GENERIC, .patch = snd_hda_parse_generic_codec },
5858 {} /* terminator */
5859};
5860
5861static struct hda_codec_driver generic_driver = {
5862 .preset = snd_hda_preset_generic,
5863};
5864
5865module_hda_codec_driver(generic_driver);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01005866
5867MODULE_LICENSE("GPL");
5868MODULE_DESCRIPTION("Generic HD-audio codec parser");