blob: 788f969b1a680e50f61940e31fe1a4609ccce402 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Generic widget tree parser
5 *
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 *
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/slab.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040025#include <linux/export.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010026#include <linux/sort.h>
Takashi Iwai55196ff2013-01-24 17:32:56 +010027#include <linux/delay.h>
Takashi Iwaif873e532012-12-20 16:58:39 +010028#include <linux/ctype.h>
29#include <linux/string.h>
Takashi Iwai294765582013-01-17 09:52:11 +010030#include <linux/bitops.h>
Takashi Iwaib21bdd02013-11-18 12:03:56 +010031#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010033#include <sound/jack.h>
Takashi Iwaid89c6c02014-09-01 10:07:04 +020034#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hda_codec.h"
36#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010037#include "hda_auto_parser.h"
38#include "hda_jack.h"
Takashi Iwai7504b6c2013-03-18 11:25:51 +010039#include "hda_beep.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010040#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Takashi Iwaidda42bd2014-10-30 12:04:50 +010043/**
44 * snd_hda_gen_spec_init - initialize hda_gen_spec struct
45 * @spec: hda_gen_spec object to initialize
46 *
47 * Initialize the given hda_gen_spec object.
48 */
Takashi Iwai352f7f92012-12-19 12:52:06 +010049int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Takashi Iwai352f7f92012-12-19 12:52:06 +010051 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010052 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010053 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010054 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010055 return 0;
56}
Takashi Iwai2698ea92013-12-18 07:45:52 +010057EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Takashi Iwaidda42bd2014-10-30 12:04:50 +010059/**
60 * snd_hda_gen_add_kctl - Add a new kctl_new struct from the template
61 * @spec: hda_gen_spec object
62 * @name: name string to override the template, NULL if unchanged
63 * @temp: template for the new kctl
64 *
65 * Add a new kctl (actually snd_kcontrol_new to be instantiated later)
66 * element based on the given snd_kcontrol_new template @temp and the
67 * name string @name to the list in @spec.
68 * Returns the newly created object or NULL as error.
69 */
Takashi Iwai12c93df2012-12-19 14:38:33 +010070struct snd_kcontrol_new *
71snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
72 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010073{
74 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
75 if (!knew)
76 return NULL;
77 *knew = *temp;
78 if (name)
79 knew->name = kstrdup(name, GFP_KERNEL);
80 else if (knew->name)
81 knew->name = kstrdup(knew->name, GFP_KERNEL);
82 if (!knew->name)
83 return NULL;
84 return knew;
85}
Takashi Iwai2698ea92013-12-18 07:45:52 +010086EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010087
88static void free_kctls(struct hda_gen_spec *spec)
89{
90 if (spec->kctls.list) {
91 struct snd_kcontrol_new *kctl = spec->kctls.list;
92 int i;
93 for (i = 0; i < spec->kctls.used; i++)
94 kfree(kctl[i].name);
95 }
96 snd_array_free(&spec->kctls);
97}
98
Takashi Iwaia8dca462014-02-10 18:23:57 +010099static void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100100{
101 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100103 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100104 snd_array_free(&spec->paths);
Takashi Iwai0186f4f2013-02-07 10:45:11 +0100105 snd_array_free(&spec->loopback_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/*
Takashi Iwai1c70a582013-01-11 17:48:22 +0100109 * store user hints
110 */
111static void parse_user_hints(struct hda_codec *codec)
112{
113 struct hda_gen_spec *spec = codec->spec;
114 int val;
115
116 val = snd_hda_get_bool_hint(codec, "jack_detect");
117 if (val >= 0)
118 codec->no_jack_detect = !val;
119 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
120 if (val >= 0)
121 codec->inv_jack_detect = !!val;
122 val = snd_hda_get_bool_hint(codec, "trigger_sense");
123 if (val >= 0)
124 codec->no_trigger_sense = !val;
125 val = snd_hda_get_bool_hint(codec, "inv_eapd");
126 if (val >= 0)
127 codec->inv_eapd = !!val;
128 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
129 if (val >= 0)
130 codec->pcm_format_first = !!val;
131 val = snd_hda_get_bool_hint(codec, "sticky_stream");
132 if (val >= 0)
133 codec->no_sticky_stream = !val;
134 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
135 if (val >= 0)
136 codec->spdif_status_reset = !!val;
137 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
138 if (val >= 0)
139 codec->pin_amp_workaround = !!val;
140 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
141 if (val >= 0)
142 codec->single_adc_amp = !!val;
Takashi Iwai967b1302015-03-20 18:21:03 +0100143 val = snd_hda_get_bool_hint(codec, "power_save_node");
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100144 if (val >= 0)
Takashi Iwai967b1302015-03-20 18:21:03 +0100145 codec->power_save_node = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100146
Takashi Iwaif72706b2013-01-16 18:20:07 +0100147 val = snd_hda_get_bool_hint(codec, "auto_mute");
148 if (val >= 0)
149 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100150 val = snd_hda_get_bool_hint(codec, "auto_mic");
151 if (val >= 0)
152 spec->suppress_auto_mic = !val;
153 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
154 if (val >= 0)
155 spec->line_in_auto_switch = !!val;
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200156 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
157 if (val >= 0)
158 spec->auto_mute_via_amp = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100159 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
160 if (val >= 0)
161 spec->need_dac_fix = !!val;
162 val = snd_hda_get_bool_hint(codec, "primary_hp");
163 if (val >= 0)
164 spec->no_primary_hp = !val;
Takashi Iwaida96fb52013-07-29 16:54:36 +0200165 val = snd_hda_get_bool_hint(codec, "multi_io");
166 if (val >= 0)
167 spec->no_multi_io = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100168 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
169 if (val >= 0)
170 spec->multi_cap_vol = !!val;
171 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
172 if (val >= 0)
173 spec->inv_dmic_split = !!val;
174 val = snd_hda_get_bool_hint(codec, "indep_hp");
175 if (val >= 0)
176 spec->indep_hp = !!val;
177 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
178 if (val >= 0)
179 spec->add_stereo_mix_input = !!val;
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100180 /* the following two are just for compatibility */
Takashi Iwai1c70a582013-01-11 17:48:22 +0100181 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
182 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100183 spec->add_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100184 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
185 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100186 spec->add_jack_modes = !!val;
187 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
188 if (val >= 0)
189 spec->add_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100190 val = snd_hda_get_bool_hint(codec, "power_down_unused");
191 if (val >= 0)
192 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100193 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
194 if (val >= 0)
195 spec->hp_mic = !!val;
196 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
197 if (val >= 0)
198 spec->suppress_hp_mic_detect = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100199
200 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
201 spec->mixer_nid = val;
202}
203
204/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100205 * pin control value accesses
206 */
207
208#define update_pin_ctl(codec, pin, val) \
209 snd_hda_codec_update_cache(codec, pin, 0, \
210 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
211
212/* restore the pinctl based on the cached value */
213static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
214{
215 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
216}
217
218/* set the pinctl target value and write it if requested */
219static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
220 unsigned int val, bool do_write)
221{
222 if (!pin)
223 return;
224 val = snd_hda_correct_pin_ctl(codec, pin, val);
225 snd_hda_codec_set_pin_target(codec, pin, val);
226 if (do_write)
227 update_pin_ctl(codec, pin, val);
228}
229
230/* set pinctl target values for all given pins */
231static void set_pin_targets(struct hda_codec *codec, int num_pins,
232 hda_nid_t *pins, unsigned int val)
233{
234 int i;
235 for (i = 0; i < num_pins; i++)
236 set_pin_target(codec, pins[i], val, false);
237}
238
239/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100240 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100243/* return the position of NID in the list, or -1 if not found */
244static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
245{
246 int i;
247 for (i = 0; i < nums; i++)
248 if (list[i] == nid)
249 return i;
250 return -1;
251}
252
253/* return true if the given NID is contained in the path */
254static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
255{
256 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
257}
258
Takashi Iwaif5172a72013-01-04 13:19:55 +0100259static struct nid_path *get_nid_path(struct hda_codec *codec,
260 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100261 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100263 struct hda_gen_spec *spec = codec->spec;
264 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Takashi Iwai352f7f92012-12-19 12:52:06 +0100266 for (i = 0; i < spec->paths.used; i++) {
267 struct nid_path *path = snd_array_elem(&spec->paths, i);
268 if (path->depth <= 0)
269 continue;
270 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100271 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100272 if (!anchor_nid ||
273 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
274 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100275 return path;
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278 return NULL;
279}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100280
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100281/**
282 * snd_hda_get_nid_path - get the path between the given NIDs
283 * @codec: the HDA codec
284 * @from_nid: the NID where the path start from
285 * @to_nid: the NID where the path ends at
286 *
287 * Return the found nid_path object or NULL for error.
288 * Passing 0 to either @from_nid or @to_nid behaves as a wildcard.
Takashi Iwaif5172a72013-01-04 13:19:55 +0100289 */
290struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
291 hda_nid_t from_nid, hda_nid_t to_nid)
292{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100293 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100294}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100295EXPORT_SYMBOL_GPL(snd_hda_get_nid_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100296
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100297/**
298 * snd_hda_get_path_idx - get the index number corresponding to the path
299 * instance
300 * @codec: the HDA codec
301 * @path: nid_path object
302 *
303 * The returned index starts from 1, i.e. the actual array index with offset 1,
304 * and zero is handled as an invalid path
Takashi Iwai196c17662013-01-04 15:01:40 +0100305 */
306int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
307{
308 struct hda_gen_spec *spec = codec->spec;
309 struct nid_path *array = spec->paths.list;
310 ssize_t idx;
311
312 if (!spec->paths.used)
313 return 0;
314 idx = path - array;
315 if (idx < 0 || idx >= spec->paths.used)
316 return 0;
317 return idx + 1;
318}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100319EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100320
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100321/**
322 * snd_hda_get_path_from_idx - get the path instance corresponding to the
323 * given index number
324 * @codec: the HDA codec
325 * @idx: the path index
326 */
Takashi Iwai196c17662013-01-04 15:01:40 +0100327struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
328{
329 struct hda_gen_spec *spec = codec->spec;
330
331 if (idx <= 0 || idx > spec->paths.used)
332 return NULL;
333 return snd_array_elem(&spec->paths, idx - 1);
334}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100335EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100336
Takashi Iwai352f7f92012-12-19 12:52:06 +0100337/* check whether the given DAC is already found in any existing paths */
338static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
339{
340 struct hda_gen_spec *spec = codec->spec;
341 int i;
342
343 for (i = 0; i < spec->paths.used; i++) {
344 struct nid_path *path = snd_array_elem(&spec->paths, i);
345 if (path->path[0] == nid)
346 return true;
347 }
348 return false;
349}
350
351/* check whether the given two widgets can be connected */
352static bool is_reachable_path(struct hda_codec *codec,
353 hda_nid_t from_nid, hda_nid_t to_nid)
354{
355 if (!from_nid || !to_nid)
356 return false;
357 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
358}
359
360/* nid, dir and idx */
361#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
362
363/* check whether the given ctl is already assigned in any path elements */
364static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
365{
366 struct hda_gen_spec *spec = codec->spec;
367 int i;
368
369 val &= AMP_VAL_COMPARE_MASK;
370 for (i = 0; i < spec->paths.used; i++) {
371 struct nid_path *path = snd_array_elem(&spec->paths, i);
372 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
373 return true;
374 }
375 return false;
376}
377
378/* check whether a control with the given (nid, dir, idx) was assigned */
379static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100380 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100381{
382 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100383 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100384}
385
Takashi Iwai4e76a882014-02-25 12:21:03 +0100386static void print_nid_path(struct hda_codec *codec,
387 const char *pfx, struct nid_path *path)
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100388{
389 char buf[40];
Joe Perchesd82353e2014-07-05 13:02:02 -0700390 char *pos = buf;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100391 int i;
392
Joe Perchesd82353e2014-07-05 13:02:02 -0700393 *pos = 0;
394 for (i = 0; i < path->depth; i++)
395 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
396 pos != buf ? ":" : "",
397 path->path[i]);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100398
Joe Perchesd82353e2014-07-05 13:02:02 -0700399 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100400}
401
Takashi Iwai352f7f92012-12-19 12:52:06 +0100402/* called recursively */
403static bool __parse_nid_path(struct hda_codec *codec,
404 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100405 int anchor_nid, struct nid_path *path,
406 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100407{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100408 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100409 int i, nums;
410
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100411 if (to_nid == anchor_nid)
412 anchor_nid = 0; /* anchor passed */
413 else if (to_nid == (hda_nid_t)(-anchor_nid))
414 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100415
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100416 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100417 for (i = 0; i < nums; i++) {
418 if (conn[i] != from_nid) {
419 /* special case: when from_nid is 0,
420 * try to find an empty DAC
421 */
422 if (from_nid ||
423 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
424 is_dac_already_used(codec, conn[i]))
425 continue;
426 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100427 /* anchor is not requested or already passed? */
428 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100429 goto found;
430 }
431 if (depth >= MAX_NID_PATH_DEPTH)
432 return false;
433 for (i = 0; i < nums; i++) {
434 unsigned int type;
435 type = get_wcaps_type(get_wcaps(codec, conn[i]));
436 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
437 type == AC_WID_PIN)
438 continue;
439 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100440 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100441 goto found;
442 }
443 return false;
444
445 found:
446 path->path[path->depth] = conn[i];
447 path->idx[path->depth + 1] = i;
448 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
449 path->multi[path->depth + 1] = 1;
450 path->depth++;
451 return true;
452}
453
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100454/**
455 * snd_hda_parse_nid_path - parse the widget path from the given nid to
456 * the target nid
457 * @codec: the HDA codec
458 * @from_nid: the NID where the path start from
459 * @to_nid: the NID where the path ends at
460 * @anchor_nid: the anchor indication
461 * @path: the path object to store the result
462 *
463 * Returns true if a matching path is found.
464 *
465 * The parsing behavior depends on parameters:
Takashi Iwai352f7f92012-12-19 12:52:06 +0100466 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100467 * when @anchor_nid is set to a positive value, only paths through the widget
468 * with the given value are evaluated.
469 * when @anchor_nid is set to a negative value, paths through the widget
470 * with the negative of given value are excluded, only other paths are chosen.
471 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100472 */
473bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100474 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100475 struct nid_path *path)
476{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100477 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100478 path->path[path->depth] = to_nid;
479 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100480 return true;
481 }
482 return false;
483}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100484EXPORT_SYMBOL_GPL(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100486/**
487 * snd_hda_add_new_path - parse the path between the given NIDs and
488 * add to the path list
489 * @codec: the HDA codec
490 * @from_nid: the NID where the path start from
491 * @to_nid: the NID where the path ends at
492 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
493 *
494 * If no valid path is found, returns NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100496struct nid_path *
497snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100498 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100500 struct hda_gen_spec *spec = codec->spec;
501 struct nid_path *path;
502
503 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
504 return NULL;
505
Takashi Iwaif5172a72013-01-04 13:19:55 +0100506 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100507 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100508 if (path)
509 return path;
510
Takashi Iwai352f7f92012-12-19 12:52:06 +0100511 path = snd_array_new(&spec->paths);
512 if (!path)
513 return NULL;
514 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100515 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100516 return path;
517 /* push back */
518 spec->paths.used--;
519 return NULL;
520}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100521EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100522
Takashi Iwai980428c2013-01-09 09:28:20 +0100523/* clear the given path as invalid so that it won't be picked up later */
524static void invalidate_nid_path(struct hda_codec *codec, int idx)
525{
526 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
527 if (!path)
528 return;
529 memset(path, 0, sizeof(*path));
530}
531
Takashi Iwai36907392013-12-10 17:29:26 +0100532/* return a DAC if paired to the given pin by codec driver */
533static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
534{
535 struct hda_gen_spec *spec = codec->spec;
536 const hda_nid_t *list = spec->preferred_dacs;
537
538 if (!list)
539 return 0;
540 for (; *list; list += 2)
541 if (*list == pin)
542 return list[1];
543 return 0;
544}
545
Takashi Iwai352f7f92012-12-19 12:52:06 +0100546/* look for an empty DAC slot */
547static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
548 bool is_digital)
549{
550 struct hda_gen_spec *spec = codec->spec;
551 bool cap_digital;
552 int i;
553
554 for (i = 0; i < spec->num_all_dacs; i++) {
555 hda_nid_t nid = spec->all_dacs[i];
556 if (!nid || is_dac_already_used(codec, nid))
557 continue;
558 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
559 if (is_digital != cap_digital)
560 continue;
561 if (is_reachable_path(codec, nid, pin))
562 return nid;
563 }
564 return 0;
565}
566
567/* replace the channels in the composed amp value with the given number */
568static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
569{
570 val &= ~(0x3U << 16);
571 val |= chs << 16;
572 return val;
573}
574
David Henningsson99a55922013-01-16 15:58:44 +0100575static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
576 hda_nid_t nid2, int dir)
577{
578 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
579 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
580 return (query_amp_caps(codec, nid1, dir) ==
581 query_amp_caps(codec, nid2, dir));
582}
583
Takashi Iwai352f7f92012-12-19 12:52:06 +0100584/* look for a widget suitable for assigning a mute switch in the path */
585static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
586 struct nid_path *path)
587{
588 int i;
589
590 for (i = path->depth - 1; i >= 0; i--) {
591 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
592 return path->path[i];
593 if (i != path->depth - 1 && i != 0 &&
594 nid_has_mute(codec, path->path[i], HDA_INPUT))
595 return path->path[i];
596 }
597 return 0;
598}
599
600/* look for a widget suitable for assigning a volume ctl in the path */
601static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
602 struct nid_path *path)
603{
Takashi Iwaia1114a82013-11-04 16:32:01 +0100604 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100605 int i;
606
607 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwaia1114a82013-11-04 16:32:01 +0100608 hda_nid_t nid = path->path[i];
609 if ((spec->out_vol_mask >> nid) & 1)
610 continue;
611 if (nid_has_volume(codec, nid, HDA_OUTPUT))
612 return nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100613 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200614 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100618 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100620
621/* can have the amp-in capability? */
622static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100624 hda_nid_t nid = path->path[idx];
625 unsigned int caps = get_wcaps(codec, nid);
626 unsigned int type = get_wcaps_type(caps);
627
628 if (!(caps & AC_WCAP_IN_AMP))
629 return false;
630 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
631 return false;
632 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Takashi Iwai352f7f92012-12-19 12:52:06 +0100635/* can have the amp-out capability? */
636static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100638 hda_nid_t nid = path->path[idx];
639 unsigned int caps = get_wcaps(codec, nid);
640 unsigned int type = get_wcaps_type(caps);
641
642 if (!(caps & AC_WCAP_OUT_AMP))
643 return false;
644 if (type == AC_WID_PIN && !idx) /* only for output pins */
645 return false;
646 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Takashi Iwai352f7f92012-12-19 12:52:06 +0100649/* check whether the given (nid,dir,idx) is active */
650static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100651 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100653 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100654 int type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai352f7f92012-12-19 12:52:06 +0100655 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Takashi Iwai7639a062015-03-03 10:07:24 +0100657 if (nid == codec->core.afg)
Takashi Iwai5ccf8352015-03-18 09:23:10 +0100658 return true;
659
Takashi Iwai352f7f92012-12-19 12:52:06 +0100660 for (n = 0; n < spec->paths.used; n++) {
661 struct nid_path *path = snd_array_elem(&spec->paths, n);
662 if (!path->active)
663 continue;
Takashi Iwai967b1302015-03-20 18:21:03 +0100664 if (codec->power_save_node) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100665 if (!path->stream_enabled)
666 continue;
667 /* ignore unplugged paths except for DAC/ADC */
Takashi Iwai6b275b12015-03-20 18:11:05 +0100668 if (!(path->pin_enabled || path->pin_fixed) &&
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100669 type != AC_WID_AUD_OUT && type != AC_WID_AUD_IN)
670 continue;
671 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100672 for (i = 0; i < path->depth; i++) {
673 if (path->path[i] == nid) {
674 if (dir == HDA_OUTPUT || path->idx[i] == idx)
675 return true;
676 break;
677 }
678 }
679 }
680 return false;
681}
682
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200683/* check whether the NID is referred by any active paths */
684#define is_active_nid_for_any(codec, nid) \
685 is_active_nid(codec, nid, HDA_OUTPUT, 0)
686
Takashi Iwai352f7f92012-12-19 12:52:06 +0100687/* get the default amp value for the target state */
688static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100689 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100690{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100691 unsigned int val = 0;
692
Takashi Iwai352f7f92012-12-19 12:52:06 +0100693 if (caps & AC_AMPCAP_NUM_STEPS) {
694 /* set to 0dB */
695 if (enable)
696 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
697 }
Takashi Iwaif69910d2013-08-08 09:32:37 +0200698 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100699 if (!enable)
700 val |= HDA_AMP_MUTE;
701 }
702 return val;
703}
704
Takashi Iwaicc261732015-03-16 10:18:08 +0100705/* is this a stereo widget or a stereo-to-mono mix? */
706static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
707{
708 unsigned int wcaps = get_wcaps(codec, nid);
709 hda_nid_t conn;
710
711 if (wcaps & AC_WCAP_STEREO)
712 return true;
713 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
714 return false;
715 if (snd_hda_get_num_conns(codec, nid) != 1)
716 return false;
717 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
718 return false;
719 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
720}
721
Takashi Iwai352f7f92012-12-19 12:52:06 +0100722/* initialize the amp value (only at the first time) */
723static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
724{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100725 unsigned int caps = query_amp_caps(codec, nid, dir);
726 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwaief403ed2015-03-12 08:30:11 +0100727
Takashi Iwaicc261732015-03-16 10:18:08 +0100728 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100729 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
730 else
731 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
732}
733
734/* update the amp, doing in stereo or mono depending on NID */
735static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
736 unsigned int mask, unsigned int val)
737{
Takashi Iwaicc261732015-03-16 10:18:08 +0100738 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100739 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
740 mask, val);
741 else
742 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
743 mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100744}
745
Takashi Iwai8999bf02013-01-18 11:01:33 +0100746/* calculate amp value mask we can modify;
747 * if the given amp is controlled by mixers, don't touch it
748 */
749static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
750 hda_nid_t nid, int dir, int idx,
751 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100752{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100753 unsigned int mask = 0xff;
754
Takashi Iwaif69910d2013-08-08 09:32:37 +0200755 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100756 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
757 mask &= ~0x80;
758 }
759 if (caps & AC_AMPCAP_NUM_STEPS) {
760 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
761 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
762 mask &= ~0x7f;
763 }
764 return mask;
765}
766
767static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
768 int idx, int idx_to_check, bool enable)
769{
770 unsigned int caps;
771 unsigned int mask, val;
772
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100773 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100774 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100775
776 caps = query_amp_caps(codec, nid, dir);
777 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
778 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
779 if (!mask)
780 return;
781
782 val &= mask;
Takashi Iwaief403ed2015-03-12 08:30:11 +0100783 update_amp(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100784}
785
786static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
787 int i, bool enable)
788{
789 hda_nid_t nid = path->path[i];
790 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100791 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100792}
793
794static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
795 int i, bool enable, bool add_aamix)
796{
797 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100798 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100799 int n, nums, idx;
800 int type;
801 hda_nid_t nid = path->path[i];
802
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100803 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100804 type = get_wcaps_type(get_wcaps(codec, nid));
805 if (type == AC_WID_PIN ||
806 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
807 nums = 1;
808 idx = 0;
809 } else
810 idx = path->idx[i];
811
812 for (n = 0; n < nums; n++)
813 init_amp(codec, nid, HDA_INPUT, n);
814
Takashi Iwai352f7f92012-12-19 12:52:06 +0100815 /* here is a little bit tricky in comparison with activate_amp_out();
816 * when aa-mixer is available, we need to enable the path as well
817 */
818 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100819 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100820 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100821 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823}
824
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100825/* sync power of each widget in the the given path */
826static hda_nid_t path_power_update(struct hda_codec *codec,
827 struct nid_path *path,
828 bool allow_powerdown)
829{
830 hda_nid_t nid, changed = 0;
831 int i, state;
832
833 for (i = 0; i < path->depth; i++) {
834 nid = path->path[i];
Takashi Iwai2206dc92015-04-09 10:18:31 +0200835 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
836 continue;
Takashi Iwai7639a062015-03-03 10:07:24 +0100837 if (nid == codec->core.afg)
Takashi Iwai5ccf8352015-03-18 09:23:10 +0100838 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100839 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
840 state = AC_PWRST_D0;
841 else
842 state = AC_PWRST_D3;
843 if (!snd_hda_check_power_state(codec, nid, state)) {
844 snd_hda_codec_write(codec, nid, 0,
845 AC_VERB_SET_POWER_STATE, state);
846 changed = nid;
Takashi Iwaid545a572015-04-04 12:17:28 +0200847 if (state == AC_PWRST_D0)
848 snd_hdac_regmap_sync_node(&codec->core, nid);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100849 }
850 }
851 return changed;
852}
853
854/* do sync with the last power state change */
855static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
856{
857 if (nid) {
858 msleep(10);
859 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
860 }
861}
862
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100863/**
864 * snd_hda_activate_path - activate or deactivate the given path
865 * @codec: the HDA codec
866 * @path: the path to activate/deactivate
867 * @enable: flag to activate or not
868 * @add_aamix: enable the input from aamix NID
869 *
870 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100872void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
873 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100875 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100876 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Takashi Iwai352f7f92012-12-19 12:52:06 +0100878 if (!enable)
879 path->active = false;
880
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100881 /* make sure the widget is powered up */
Takashi Iwai967b1302015-03-20 18:21:03 +0100882 if (enable && (spec->power_down_unused || codec->power_save_node))
883 path_power_update(codec, path, codec->power_save_node);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100884
Takashi Iwai352f7f92012-12-19 12:52:06 +0100885 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100886 hda_nid_t nid = path->path[i];
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100887
Takashi Iwai352f7f92012-12-19 12:52:06 +0100888 if (enable && path->multi[i])
Takashi Iwai8f0972d2014-01-27 16:26:16 +0100889 snd_hda_codec_update_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100890 AC_VERB_SET_CONNECT_SEL,
891 path->idx[i]);
892 if (has_amp_in(codec, path, i))
893 activate_amp_in(codec, path, i, enable, add_aamix);
894 if (has_amp_out(codec, path, i))
895 activate_amp_out(codec, path, i, enable);
896 }
897
898 if (enable)
899 path->active = true;
900}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100901EXPORT_SYMBOL_GPL(snd_hda_activate_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100902
Takashi Iwai55196ff2013-01-24 17:32:56 +0100903/* if the given path is inactive, put widgets into D3 (only if suitable) */
904static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
905{
906 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100907
Takashi Iwai967b1302015-03-20 18:21:03 +0100908 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
Takashi Iwai55196ff2013-01-24 17:32:56 +0100909 return;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100910 sync_power_state_change(codec, path_power_update(codec, path, true));
Takashi Iwai55196ff2013-01-24 17:32:56 +0100911}
912
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100913/* turn on/off EAPD on the given pin */
914static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
915{
916 struct hda_gen_spec *spec = codec->spec;
917 if (spec->own_eapd_ctl ||
918 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
919 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200920 if (spec->keep_eapd_on && !enable)
921 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100922 if (codec->inv_eapd)
923 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100924 snd_hda_codec_update_cache(codec, pin, 0,
925 AC_VERB_SET_EAPD_BTLENABLE,
926 enable ? 0x02 : 0x00);
927}
928
Takashi Iwai3e367f12013-01-23 17:07:23 +0100929/* re-initialize the path specified by the given path index */
930static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
931{
932 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
933 if (path)
934 snd_hda_activate_path(codec, path, path->active, false);
935}
936
Takashi Iwai352f7f92012-12-19 12:52:06 +0100937
938/*
939 * Helper functions for creating mixer ctl elements
940 */
941
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200942static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
943 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200944static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
945 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200946
Takashi Iwai352f7f92012-12-19 12:52:06 +0100947enum {
948 HDA_CTL_WIDGET_VOL,
949 HDA_CTL_WIDGET_MUTE,
950 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100951};
952static const struct snd_kcontrol_new control_templates[] = {
953 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200954 /* only the put callback is replaced for handling the special mute */
955 {
956 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
957 .subdevice = HDA_SUBDEV_AMP_FLAG,
958 .info = snd_hda_mixer_amp_switch_info,
959 .get = snd_hda_mixer_amp_switch_get,
960 .put = hda_gen_mixer_mute_put, /* replaced */
961 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
962 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200963 {
964 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
965 .info = snd_hda_mixer_amp_switch_info,
966 .get = snd_hda_mixer_bind_switch_get,
967 .put = hda_gen_bind_mute_put, /* replaced */
968 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
969 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100970};
971
972/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100973static struct snd_kcontrol_new *
974add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100975 int cidx, unsigned long val)
976{
977 struct snd_kcontrol_new *knew;
978
Takashi Iwai12c93df2012-12-19 14:38:33 +0100979 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100980 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100981 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100982 knew->index = cidx;
983 if (get_amp_nid_(val))
984 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
985 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100986 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100987}
988
989static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
990 const char *pfx, const char *dir,
991 const char *sfx, int cidx, unsigned long val)
992{
Takashi Iwai975cc022013-06-28 11:56:49 +0200993 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100994 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100995 if (!add_control(spec, type, name, cidx, val))
996 return -ENOMEM;
997 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100998}
999
1000#define add_pb_vol_ctrl(spec, type, pfx, val) \
1001 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1002#define add_pb_sw_ctrl(spec, type, pfx, val) \
1003 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1004#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1005 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1006#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1007 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1008
1009static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1010 unsigned int chs, struct nid_path *path)
1011{
1012 unsigned int val;
1013 if (!path)
1014 return 0;
1015 val = path->ctls[NID_PATH_VOL_CTL];
1016 if (!val)
1017 return 0;
1018 val = amp_val_replace_channels(val, chs);
1019 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1020}
1021
1022/* return the channel bits suitable for the given path->ctls[] */
1023static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1024 int type)
1025{
1026 int chs = 1; /* mono (left only) */
1027 if (path) {
1028 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1029 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1030 chs = 3; /* stereo */
1031 }
1032 return chs;
1033}
1034
1035static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1036 struct nid_path *path)
1037{
1038 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1039 return add_vol_ctl(codec, pfx, cidx, chs, path);
1040}
1041
1042/* create a mute-switch for the given mixer widget;
1043 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1044 */
1045static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1046 unsigned int chs, struct nid_path *path)
1047{
1048 unsigned int val;
1049 int type = HDA_CTL_WIDGET_MUTE;
1050
1051 if (!path)
1052 return 0;
1053 val = path->ctls[NID_PATH_MUTE_CTL];
1054 if (!val)
1055 return 0;
1056 val = amp_val_replace_channels(val, chs);
1057 if (get_amp_direction_(val) == HDA_INPUT) {
1058 hda_nid_t nid = get_amp_nid_(val);
1059 int nums = snd_hda_get_num_conns(codec, nid);
1060 if (nums > 1) {
1061 type = HDA_CTL_BIND_MUTE;
1062 val |= nums << 19;
1063 }
1064 }
1065 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1066}
1067
1068static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1069 int cidx, struct nid_path *path)
1070{
1071 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1072 return add_sw_ctl(codec, pfx, cidx, chs, path);
1073}
1074
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001075/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001076static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1077 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001078{
1079 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1080 struct hda_gen_spec *spec = codec->spec;
1081
1082 if (spec->auto_mute_via_amp) {
1083 hda_nid_t nid = get_amp_nid(kcontrol);
1084 bool enabled = !((spec->mute_bits >> nid) & 1);
1085 ucontrol->value.integer.value[0] &= enabled;
1086 ucontrol->value.integer.value[1] &= enabled;
1087 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001088}
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001089
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001090static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1091 struct snd_ctl_elem_value *ucontrol)
1092{
1093 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001094 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1095}
1096
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001097static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1098 struct snd_ctl_elem_value *ucontrol)
1099{
1100 sync_auto_mute_bits(kcontrol, ucontrol);
1101 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1102}
1103
Takashi Iwai247d85e2013-01-17 16:18:11 +01001104/* any ctl assigned to the path with the given index? */
1105static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1106{
1107 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1108 return path && path->ctls[ctl_type];
1109}
1110
Takashi Iwai352f7f92012-12-19 12:52:06 +01001111static const char * const channel_name[4] = {
1112 "Front", "Surround", "CLFE", "Side"
1113};
1114
1115/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +01001116static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1117 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001118{
Takashi Iwai247d85e2013-01-17 16:18:11 +01001119 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001120 struct auto_pin_cfg *cfg = &spec->autocfg;
1121
1122 *index = 0;
1123 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001124 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001125 return spec->vmaster_mute.hook ? "PCM" : "Master";
1126
1127 /* if there is really a single DAC used in the whole output paths,
1128 * use it master (or "PCM" if a vmaster hook is present)
1129 */
1130 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1131 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1132 return spec->vmaster_mute.hook ? "PCM" : "Master";
1133
Takashi Iwai247d85e2013-01-17 16:18:11 +01001134 /* multi-io channels */
1135 if (ch >= cfg->line_outs)
1136 return channel_name[ch];
1137
Takashi Iwai352f7f92012-12-19 12:52:06 +01001138 switch (cfg->line_out_type) {
1139 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001140 /* if the primary channel vol/mute is shared with HP volume,
1141 * don't name it as Speaker
1142 */
1143 if (!ch && cfg->hp_outs &&
1144 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1145 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001146 if (cfg->line_outs == 1)
1147 return "Speaker";
1148 if (cfg->line_outs == 2)
1149 return ch ? "Bass Speaker" : "Speaker";
1150 break;
1151 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001152 /* if the primary channel vol/mute is shared with spk volume,
1153 * don't name it as Headphone
1154 */
1155 if (!ch && cfg->speaker_outs &&
1156 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1157 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001158 /* for multi-io case, only the primary out */
1159 if (ch && spec->multi_ios)
1160 break;
1161 *index = ch;
1162 return "Headphone";
David Henningsson03ad6a82014-10-16 15:33:45 +02001163 case AUTO_PIN_LINE_OUT:
1164 /* This deals with the case where we have two DACs and
1165 * one LO, one HP and one Speaker */
1166 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1167 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1168 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1169 if (hp_lo_shared && spk_lo_shared)
1170 return spec->vmaster_mute.hook ? "PCM" : "Master";
1171 if (hp_lo_shared)
1172 return "Headphone+LO";
1173 if (spk_lo_shared)
1174 return "Speaker+LO";
1175 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001176 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001177
1178 /* for a single channel output, we don't have to name the channel */
1179 if (cfg->line_outs == 1 && !spec->multi_ios)
David Henningsson3abb4f42014-10-16 15:33:46 +02001180 return "Line Out";
Takashi Iwai247d85e2013-01-17 16:18:11 +01001181
Takashi Iwai352f7f92012-12-19 12:52:06 +01001182 if (ch >= ARRAY_SIZE(channel_name)) {
1183 snd_BUG();
1184 return "PCM";
1185 }
1186
1187 return channel_name[ch];
1188}
1189
1190/*
1191 * Parse output paths
1192 */
1193
1194/* badness definition */
1195enum {
1196 /* No primary DAC is found for the main output */
1197 BAD_NO_PRIMARY_DAC = 0x10000,
1198 /* No DAC is found for the extra output */
1199 BAD_NO_DAC = 0x4000,
1200 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001201 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001202 /* No individual DAC for extra output */
1203 BAD_NO_EXTRA_DAC = 0x102,
1204 /* No individual DAC for extra surrounds */
1205 BAD_NO_EXTRA_SURR_DAC = 0x101,
1206 /* Primary DAC shared with main surrounds */
1207 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001208 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001209 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001210 /* Primary DAC shared with main CLFE */
1211 BAD_SHARED_CLFE = 0x10,
1212 /* Primary DAC shared with extra surrounds */
1213 BAD_SHARED_EXTRA_SURROUND = 0x10,
1214 /* Volume widget is shared */
1215 BAD_SHARED_VOL = 0x10,
1216};
1217
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001218/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001219 * volume and mute controls, and assign the values to ctls[].
1220 *
1221 * When no appropriate widget is found in the path, the badness value
1222 * is incremented depending on the situation. The function returns the
1223 * total badness for both volume and mute controls.
1224 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001225static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001226{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001227 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001228 hda_nid_t nid;
1229 unsigned int val;
1230 int badness = 0;
1231
1232 if (!path)
1233 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001234
1235 if (path->ctls[NID_PATH_VOL_CTL] ||
1236 path->ctls[NID_PATH_MUTE_CTL])
1237 return 0; /* already evaluated */
1238
Takashi Iwai352f7f92012-12-19 12:52:06 +01001239 nid = look_for_out_vol_nid(codec, path);
1240 if (nid) {
1241 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001242 if (spec->dac_min_mute)
1243 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001244 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1245 badness += BAD_SHARED_VOL;
1246 else
1247 path->ctls[NID_PATH_VOL_CTL] = val;
1248 } else
1249 badness += BAD_SHARED_VOL;
1250 nid = look_for_out_mute_nid(codec, path);
1251 if (nid) {
1252 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1253 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1254 nid_has_mute(codec, nid, HDA_OUTPUT))
1255 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1256 else
1257 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1258 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1259 badness += BAD_SHARED_VOL;
1260 else
1261 path->ctls[NID_PATH_MUTE_CTL] = val;
1262 } else
1263 badness += BAD_SHARED_VOL;
1264 return badness;
1265}
1266
Takashi Iwai98bd1112013-03-22 14:53:50 +01001267const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001268 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1269 .no_dac = BAD_NO_DAC,
1270 .shared_primary = BAD_NO_PRIMARY_DAC,
1271 .shared_surr = BAD_SHARED_SURROUND,
1272 .shared_clfe = BAD_SHARED_CLFE,
1273 .shared_surr_main = BAD_SHARED_SURROUND,
1274};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001275EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001276
Takashi Iwai98bd1112013-03-22 14:53:50 +01001277const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001278 .no_primary_dac = BAD_NO_DAC,
1279 .no_dac = BAD_NO_DAC,
1280 .shared_primary = BAD_NO_EXTRA_DAC,
1281 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1282 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1283 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1284};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001285EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001286
Takashi Iwai7385df62013-01-07 09:50:52 +01001287/* get the DAC of the primary output corresponding to the given array index */
1288static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1289{
1290 struct hda_gen_spec *spec = codec->spec;
1291 struct auto_pin_cfg *cfg = &spec->autocfg;
1292
1293 if (cfg->line_outs > idx)
1294 return spec->private_dac_nids[idx];
1295 idx -= cfg->line_outs;
1296 if (spec->multi_ios > idx)
1297 return spec->multi_io[idx].dac;
1298 return 0;
1299}
1300
1301/* return the DAC if it's reachable, otherwise zero */
1302static inline hda_nid_t try_dac(struct hda_codec *codec,
1303 hda_nid_t dac, hda_nid_t pin)
1304{
1305 return is_reachable_path(codec, dac, pin) ? dac : 0;
1306}
1307
Takashi Iwai352f7f92012-12-19 12:52:06 +01001308/* try to assign DACs to pins and return the resultant badness */
1309static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1310 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001311 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001312 const struct badness_table *bad)
1313{
1314 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001315 int i, j;
1316 int badness = 0;
1317 hda_nid_t dac;
1318
1319 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return 0;
1321
Takashi Iwai352f7f92012-12-19 12:52:06 +01001322 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001323 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001324 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001325
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001326 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1327 if (path) {
1328 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001329 continue;
1330 }
1331
Takashi Iwai36907392013-12-10 17:29:26 +01001332 dacs[i] = get_preferred_dac(codec, pin);
1333 if (dacs[i]) {
1334 if (is_dac_already_used(codec, dacs[i]))
1335 badness += bad->shared_primary;
1336 }
1337
1338 if (!dacs[i])
1339 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001340 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001341 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001342 for (j = 1; j < num_outs; j++) {
1343 if (is_reachable_path(codec, dacs[j], pin)) {
1344 dacs[0] = dacs[j];
1345 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001346 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001347 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001348 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001351 }
1352 dac = dacs[i];
1353 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001354 if (num_outs > 2)
1355 dac = try_dac(codec, get_primary_out(codec, i), pin);
1356 if (!dac)
1357 dac = try_dac(codec, dacs[0], pin);
1358 if (!dac)
1359 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001360 if (dac) {
1361 if (!i)
1362 badness += bad->shared_primary;
1363 else if (i == 1)
1364 badness += bad->shared_surr;
1365 else
1366 badness += bad->shared_clfe;
1367 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1368 dac = spec->private_dac_nids[0];
1369 badness += bad->shared_surr_main;
1370 } else if (!i)
1371 badness += bad->no_primary_dac;
1372 else
1373 badness += bad->no_dac;
1374 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001375 if (!dac)
1376 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001377 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001378 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001379 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001380 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001381 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001382 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001383 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001384 badness += bad->no_dac;
1385 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001386 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001387 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001388 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001389 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001390 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001391 }
1392
1393 return badness;
1394}
1395
1396/* return NID if the given pin has only a single connection to a certain DAC */
1397static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1398{
1399 struct hda_gen_spec *spec = codec->spec;
1400 int i;
1401 hda_nid_t nid_found = 0;
1402
1403 for (i = 0; i < spec->num_all_dacs; i++) {
1404 hda_nid_t nid = spec->all_dacs[i];
1405 if (!nid || is_dac_already_used(codec, nid))
1406 continue;
1407 if (is_reachable_path(codec, nid, pin)) {
1408 if (nid_found)
1409 return 0;
1410 nid_found = nid;
1411 }
1412 }
1413 return nid_found;
1414}
1415
1416/* check whether the given pin can be a multi-io pin */
1417static bool can_be_multiio_pin(struct hda_codec *codec,
1418 unsigned int location, hda_nid_t nid)
1419{
1420 unsigned int defcfg, caps;
1421
1422 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1423 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1424 return false;
1425 if (location && get_defcfg_location(defcfg) != location)
1426 return false;
1427 caps = snd_hda_query_pin_caps(codec, nid);
1428 if (!(caps & AC_PINCAP_OUT))
1429 return false;
1430 return true;
1431}
1432
Takashi Iwaie22aab72013-01-04 14:50:04 +01001433/* count the number of input pins that are capable to be multi-io */
1434static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1435{
1436 struct hda_gen_spec *spec = codec->spec;
1437 struct auto_pin_cfg *cfg = &spec->autocfg;
1438 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1439 unsigned int location = get_defcfg_location(defcfg);
1440 int type, i;
1441 int num_pins = 0;
1442
1443 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1444 for (i = 0; i < cfg->num_inputs; i++) {
1445 if (cfg->inputs[i].type != type)
1446 continue;
1447 if (can_be_multiio_pin(codec, location,
1448 cfg->inputs[i].pin))
1449 num_pins++;
1450 }
1451 }
1452 return num_pins;
1453}
1454
Takashi Iwai352f7f92012-12-19 12:52:06 +01001455/*
1456 * multi-io helper
1457 *
1458 * When hardwired is set, try to fill ony hardwired pins, and returns
1459 * zero if any pins are filled, non-zero if nothing found.
1460 * When hardwired is off, try to fill possible input pins, and returns
1461 * the badness value.
1462 */
1463static int fill_multi_ios(struct hda_codec *codec,
1464 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001465 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001466{
1467 struct hda_gen_spec *spec = codec->spec;
1468 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001469 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001470 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1471 unsigned int location = get_defcfg_location(defcfg);
1472 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001473 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001474
1475 old_pins = spec->multi_ios;
1476 if (old_pins >= 2)
1477 goto end_fill;
1478
Takashi Iwaie22aab72013-01-04 14:50:04 +01001479 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001480 if (num_pins < 2)
1481 goto end_fill;
1482
Takashi Iwai352f7f92012-12-19 12:52:06 +01001483 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1484 for (i = 0; i < cfg->num_inputs; i++) {
1485 hda_nid_t nid = cfg->inputs[i].pin;
1486 hda_nid_t dac = 0;
1487
1488 if (cfg->inputs[i].type != type)
1489 continue;
1490 if (!can_be_multiio_pin(codec, location, nid))
1491 continue;
1492 for (j = 0; j < spec->multi_ios; j++) {
1493 if (nid == spec->multi_io[j].pin)
1494 break;
1495 }
1496 if (j < spec->multi_ios)
1497 continue;
1498
Takashi Iwai352f7f92012-12-19 12:52:06 +01001499 if (hardwired)
1500 dac = get_dac_if_single(codec, nid);
1501 else if (!dac)
1502 dac = look_for_dac(codec, nid, false);
1503 if (!dac) {
1504 badness++;
1505 continue;
1506 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001507 path = snd_hda_add_new_path(codec, dac, nid,
1508 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001509 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001510 badness++;
1511 continue;
1512 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001513 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001514 spec->multi_io[spec->multi_ios].pin = nid;
1515 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001516 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1517 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001518 spec->multi_ios++;
1519 if (spec->multi_ios >= 2)
1520 break;
1521 }
1522 }
1523 end_fill:
1524 if (badness)
1525 badness = BAD_MULTI_IO;
1526 if (old_pins == spec->multi_ios) {
1527 if (hardwired)
1528 return 1; /* nothing found */
1529 else
1530 return badness; /* no badness if nothing found */
1531 }
1532 if (!hardwired && spec->multi_ios < 2) {
1533 /* cancel newly assigned paths */
1534 spec->paths.used -= spec->multi_ios - old_pins;
1535 spec->multi_ios = old_pins;
1536 return badness;
1537 }
1538
1539 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001540 for (i = old_pins; i < spec->multi_ios; i++) {
1541 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1542 badness += assign_out_path_ctls(codec, path);
1543 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001544
1545 return badness;
1546}
1547
1548/* map DACs for all pins in the list if they are single connections */
1549static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001550 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001551{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001552 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001553 int i;
1554 bool found = false;
1555 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001556 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001557 hda_nid_t dac;
1558 if (dacs[i])
1559 continue;
1560 dac = get_dac_if_single(codec, pins[i]);
1561 if (!dac)
1562 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001563 path = snd_hda_add_new_path(codec, dac, pins[i],
1564 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001565 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001566 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001567 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001568 dacs[i] = dac;
1569 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001570 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001571 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001572 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001573 }
1574 }
1575 return found;
1576}
1577
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001578/* create a new path including aamix if available, and return its index */
1579static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1580{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001581 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001582 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001583 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001584
1585 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001586 if (!path || !path->depth ||
1587 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001588 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001589 path_dac = path->path[0];
1590 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001591 pin = path->path[path->depth - 1];
1592 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1593 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001594 if (dac != path_dac)
1595 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001596 else if (spec->multiout.hp_out_nid[0])
1597 dac = spec->multiout.hp_out_nid[0];
1598 else if (spec->multiout.extra_out_nid[0])
1599 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001600 else
1601 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001602 if (dac)
1603 path = snd_hda_add_new_path(codec, dac, pin,
1604 spec->mixer_nid);
1605 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001606 if (!path)
1607 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001608 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001609 path->active = false; /* unused as default */
Takashi Iwai6b275b12015-03-20 18:11:05 +01001610 path->pin_fixed = true; /* static route */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001611 return snd_hda_get_path_idx(codec, path);
1612}
1613
Takashi Iwai55a63d42013-03-21 17:20:12 +01001614/* check whether the independent HP is available with the current config */
1615static bool indep_hp_possible(struct hda_codec *codec)
1616{
1617 struct hda_gen_spec *spec = codec->spec;
1618 struct auto_pin_cfg *cfg = &spec->autocfg;
1619 struct nid_path *path;
1620 int i, idx;
1621
1622 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1623 idx = spec->out_paths[0];
1624 else
1625 idx = spec->hp_paths[0];
1626 path = snd_hda_get_path_from_idx(codec, idx);
1627 if (!path)
1628 return false;
1629
1630 /* assume no path conflicts unless aamix is involved */
1631 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1632 return true;
1633
1634 /* check whether output paths contain aamix */
1635 for (i = 0; i < cfg->line_outs; i++) {
1636 if (spec->out_paths[i] == idx)
1637 break;
1638 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1639 if (path && is_nid_contained(path, spec->mixer_nid))
1640 return false;
1641 }
1642 for (i = 0; i < cfg->speaker_outs; i++) {
1643 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1644 if (path && is_nid_contained(path, spec->mixer_nid))
1645 return false;
1646 }
1647
1648 return true;
1649}
1650
Takashi Iwaia07a9492013-01-07 16:44:06 +01001651/* fill the empty entries in the dac array for speaker/hp with the
1652 * shared dac pointed by the paths
1653 */
1654static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1655 hda_nid_t *dacs, int *path_idx)
1656{
1657 struct nid_path *path;
1658 int i;
1659
1660 for (i = 0; i < num_outs; i++) {
1661 if (dacs[i])
1662 continue;
1663 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1664 if (!path)
1665 continue;
1666 dacs[i] = path->path[0];
1667 }
1668}
1669
Takashi Iwai352f7f92012-12-19 12:52:06 +01001670/* fill in the dac_nids table from the parsed pin configuration */
1671static int fill_and_eval_dacs(struct hda_codec *codec,
1672 bool fill_hardwired,
1673 bool fill_mio_first)
1674{
1675 struct hda_gen_spec *spec = codec->spec;
1676 struct auto_pin_cfg *cfg = &spec->autocfg;
1677 int i, err, badness;
1678
1679 /* set num_dacs once to full for look_for_dac() */
1680 spec->multiout.num_dacs = cfg->line_outs;
1681 spec->multiout.dac_nids = spec->private_dac_nids;
1682 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1683 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1684 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1685 spec->multi_ios = 0;
1686 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001687
1688 /* clear path indices */
1689 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1690 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1691 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1692 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1693 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001694 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001695 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1696 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1697
Takashi Iwai352f7f92012-12-19 12:52:06 +01001698 badness = 0;
1699
1700 /* fill hard-wired DACs first */
1701 if (fill_hardwired) {
1702 bool mapped;
1703 do {
1704 mapped = map_singles(codec, cfg->line_outs,
1705 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001706 spec->private_dac_nids,
1707 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001708 mapped |= map_singles(codec, cfg->hp_outs,
1709 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001710 spec->multiout.hp_out_nid,
1711 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001712 mapped |= map_singles(codec, cfg->speaker_outs,
1713 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001714 spec->multiout.extra_out_nid,
1715 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001716 if (!spec->no_multi_io &&
1717 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001718 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001719 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001720 if (!err)
1721 mapped = true;
1722 }
1723 } while (mapped);
1724 }
1725
1726 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001727 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001728 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001729
Takashi Iwaida96fb52013-07-29 16:54:36 +02001730 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001731 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1732 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001733 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001734 if (err < 0)
1735 return err;
1736 /* we don't count badness at this stage yet */
1737 }
1738
1739 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1740 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1741 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001742 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001743 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001744 if (err < 0)
1745 return err;
1746 badness += err;
1747 }
1748 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1749 err = try_assign_dacs(codec, cfg->speaker_outs,
1750 cfg->speaker_pins,
1751 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001752 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001753 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001754 if (err < 0)
1755 return err;
1756 badness += err;
1757 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001758 if (!spec->no_multi_io &&
1759 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001760 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001761 if (err < 0)
1762 return err;
1763 badness += err;
1764 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001765
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001766 if (spec->mixer_nid) {
1767 spec->aamix_out_paths[0] =
1768 check_aamix_out_path(codec, spec->out_paths[0]);
1769 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1770 spec->aamix_out_paths[1] =
1771 check_aamix_out_path(codec, spec->hp_paths[0]);
1772 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1773 spec->aamix_out_paths[2] =
1774 check_aamix_out_path(codec, spec->speaker_paths[0]);
1775 }
1776
Takashi Iwaida96fb52013-07-29 16:54:36 +02001777 if (!spec->no_multi_io &&
1778 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001779 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1780 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001781
Takashi Iwaia07a9492013-01-07 16:44:06 +01001782 /* re-count num_dacs and squash invalid entries */
1783 spec->multiout.num_dacs = 0;
1784 for (i = 0; i < cfg->line_outs; i++) {
1785 if (spec->private_dac_nids[i])
1786 spec->multiout.num_dacs++;
1787 else {
1788 memmove(spec->private_dac_nids + i,
1789 spec->private_dac_nids + i + 1,
1790 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1791 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1792 }
1793 }
1794
1795 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001796 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001797
Takashi Iwai352f7f92012-12-19 12:52:06 +01001798 if (spec->multi_ios == 2) {
1799 for (i = 0; i < 2; i++)
1800 spec->private_dac_nids[spec->multiout.num_dacs++] =
1801 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001802 } else if (spec->multi_ios) {
1803 spec->multi_ios = 0;
1804 badness += BAD_MULTI_IO;
1805 }
1806
Takashi Iwai55a63d42013-03-21 17:20:12 +01001807 if (spec->indep_hp && !indep_hp_possible(codec))
1808 badness += BAD_NO_INDEP_HP;
1809
Takashi Iwaia07a9492013-01-07 16:44:06 +01001810 /* re-fill the shared DAC for speaker / headphone */
1811 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1812 refill_shared_dacs(codec, cfg->hp_outs,
1813 spec->multiout.hp_out_nid,
1814 spec->hp_paths);
1815 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1816 refill_shared_dacs(codec, cfg->speaker_outs,
1817 spec->multiout.extra_out_nid,
1818 spec->speaker_paths);
1819
Takashi Iwai352f7f92012-12-19 12:52:06 +01001820 return badness;
1821}
1822
1823#define DEBUG_BADNESS
1824
1825#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001826#define debug_badness(fmt, ...) \
1827 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001828#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001829#define debug_badness(fmt, ...) \
1830 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001831#endif
1832
Takashi Iwaia7694092013-01-21 10:43:18 +01001833#ifdef DEBUG_BADNESS
1834static inline void print_nid_path_idx(struct hda_codec *codec,
1835 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001836{
Takashi Iwaia7694092013-01-21 10:43:18 +01001837 struct nid_path *path;
1838
1839 path = snd_hda_get_path_from_idx(codec, idx);
1840 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001841 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001842}
1843
1844static void debug_show_configs(struct hda_codec *codec,
1845 struct auto_pin_cfg *cfg)
1846{
1847 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001848 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001849 int i;
1850
1851 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001852 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001853 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001854 spec->multiout.dac_nids[0],
1855 spec->multiout.dac_nids[1],
1856 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001857 spec->multiout.dac_nids[3],
1858 lo_type[cfg->line_out_type]);
1859 for (i = 0; i < cfg->line_outs; i++)
1860 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001861 if (spec->multi_ios > 0)
1862 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1863 spec->multi_ios,
1864 spec->multi_io[0].pin, spec->multi_io[1].pin,
1865 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001866 for (i = 0; i < spec->multi_ios; i++)
1867 print_nid_path_idx(codec, " mio",
1868 spec->out_paths[cfg->line_outs + i]);
1869 if (cfg->hp_outs)
1870 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001871 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001872 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001873 spec->multiout.hp_out_nid[0],
1874 spec->multiout.hp_out_nid[1],
1875 spec->multiout.hp_out_nid[2],
1876 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001877 for (i = 0; i < cfg->hp_outs; i++)
1878 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1879 if (cfg->speaker_outs)
1880 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001881 cfg->speaker_pins[0], cfg->speaker_pins[1],
1882 cfg->speaker_pins[2], cfg->speaker_pins[3],
1883 spec->multiout.extra_out_nid[0],
1884 spec->multiout.extra_out_nid[1],
1885 spec->multiout.extra_out_nid[2],
1886 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001887 for (i = 0; i < cfg->speaker_outs; i++)
1888 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1889 for (i = 0; i < 3; i++)
1890 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001891}
Takashi Iwaia7694092013-01-21 10:43:18 +01001892#else
1893#define debug_show_configs(codec, cfg) /* NOP */
1894#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001895
1896/* find all available DACs of the codec */
1897static void fill_all_dac_nids(struct hda_codec *codec)
1898{
1899 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001900 hda_nid_t nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001901
1902 spec->num_all_dacs = 0;
1903 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
Takashi Iwai7639a062015-03-03 10:07:24 +01001904 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001905 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1906 continue;
1907 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001908 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001909 break;
1910 }
1911 spec->all_dacs[spec->num_all_dacs++] = nid;
1912 }
1913}
1914
1915static int parse_output_paths(struct hda_codec *codec)
1916{
1917 struct hda_gen_spec *spec = codec->spec;
1918 struct auto_pin_cfg *cfg = &spec->autocfg;
1919 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001920 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001921 int best_badness = INT_MAX;
1922 int badness;
1923 bool fill_hardwired = true, fill_mio_first = true;
1924 bool best_wired = true, best_mio = true;
1925 bool hp_spk_swapped = false;
1926
Takashi Iwai352f7f92012-12-19 12:52:06 +01001927 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1928 if (!best_cfg)
1929 return -ENOMEM;
1930 *best_cfg = *cfg;
1931
1932 for (;;) {
1933 badness = fill_and_eval_dacs(codec, fill_hardwired,
1934 fill_mio_first);
1935 if (badness < 0) {
1936 kfree(best_cfg);
1937 return badness;
1938 }
1939 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1940 cfg->line_out_type, fill_hardwired, fill_mio_first,
1941 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001942 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001943 if (badness < best_badness) {
1944 best_badness = badness;
1945 *best_cfg = *cfg;
1946 best_wired = fill_hardwired;
1947 best_mio = fill_mio_first;
1948 }
1949 if (!badness)
1950 break;
1951 fill_mio_first = !fill_mio_first;
1952 if (!fill_mio_first)
1953 continue;
1954 fill_hardwired = !fill_hardwired;
1955 if (!fill_hardwired)
1956 continue;
1957 if (hp_spk_swapped)
1958 break;
1959 hp_spk_swapped = true;
1960 if (cfg->speaker_outs > 0 &&
1961 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1962 cfg->hp_outs = cfg->line_outs;
1963 memcpy(cfg->hp_pins, cfg->line_out_pins,
1964 sizeof(cfg->hp_pins));
1965 cfg->line_outs = cfg->speaker_outs;
1966 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1967 sizeof(cfg->speaker_pins));
1968 cfg->speaker_outs = 0;
1969 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1970 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1971 fill_hardwired = true;
1972 continue;
1973 }
1974 if (cfg->hp_outs > 0 &&
1975 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1976 cfg->speaker_outs = cfg->line_outs;
1977 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1978 sizeof(cfg->speaker_pins));
1979 cfg->line_outs = cfg->hp_outs;
1980 memcpy(cfg->line_out_pins, cfg->hp_pins,
1981 sizeof(cfg->hp_pins));
1982 cfg->hp_outs = 0;
1983 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1984 cfg->line_out_type = AUTO_PIN_HP_OUT;
1985 fill_hardwired = true;
1986 continue;
1987 }
1988 break;
1989 }
1990
1991 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001992 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001993 *cfg = *best_cfg;
1994 fill_and_eval_dacs(codec, best_wired, best_mio);
1995 }
1996 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1997 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001998 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001999
2000 if (cfg->line_out_pins[0]) {
2001 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01002002 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002003 if (path)
2004 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002005 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01002006 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2007 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002008 if (spec->dac_min_mute)
2009 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2010 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002011 }
2012
Takashi Iwai9314a582013-01-21 10:49:05 +01002013 /* set initial pinctl targets */
2014 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2015 val = PIN_HP;
2016 else
2017 val = PIN_OUT;
2018 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2019 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2020 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2021 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2022 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2023 set_pin_targets(codec, cfg->speaker_outs,
2024 cfg->speaker_pins, val);
2025 }
2026
Takashi Iwai55a63d42013-03-21 17:20:12 +01002027 /* clear indep_hp flag if not available */
2028 if (spec->indep_hp && !indep_hp_possible(codec))
2029 spec->indep_hp = 0;
2030
Takashi Iwai352f7f92012-12-19 12:52:06 +01002031 kfree(best_cfg);
2032 return 0;
2033}
2034
2035/* add playback controls from the parsed DAC table */
2036static int create_multi_out_ctls(struct hda_codec *codec,
2037 const struct auto_pin_cfg *cfg)
2038{
2039 struct hda_gen_spec *spec = codec->spec;
2040 int i, err, noutputs;
2041
2042 noutputs = cfg->line_outs;
2043 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2044 noutputs += spec->multi_ios;
2045
2046 for (i = 0; i < noutputs; i++) {
2047 const char *name;
2048 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002049 struct nid_path *path;
2050
Takashi Iwai196c17662013-01-04 15:01:40 +01002051 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002052 if (!path)
2053 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002054
2055 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002056 if (!name || !strcmp(name, "CLFE")) {
2057 /* Center/LFE */
2058 err = add_vol_ctl(codec, "Center", 0, 1, path);
2059 if (err < 0)
2060 return err;
2061 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2062 if (err < 0)
2063 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002064 } else {
2065 err = add_stereo_vol(codec, name, index, path);
2066 if (err < 0)
2067 return err;
2068 }
2069
2070 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2071 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002072 err = add_sw_ctl(codec, "Center", 0, 1, path);
2073 if (err < 0)
2074 return err;
2075 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2076 if (err < 0)
2077 return err;
2078 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002079 err = add_stereo_sw(codec, name, index, path);
2080 if (err < 0)
2081 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
2083 }
2084 return 0;
2085}
2086
Takashi Iwaic2c80382013-01-07 10:33:57 +01002087static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01002088 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002090 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 int err;
2092
Takashi Iwai196c17662013-01-04 15:01:40 +01002093 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002094 if (!path)
2095 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01002096 err = add_stereo_vol(codec, pfx, cidx, path);
2097 if (err < 0)
2098 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002099 err = add_stereo_sw(codec, pfx, cidx, path);
2100 if (err < 0)
2101 return err;
2102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103}
2104
Takashi Iwai352f7f92012-12-19 12:52:06 +01002105/* add playback controls for speaker and HP outputs */
2106static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01002107 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Takashi Iwaic2c80382013-01-07 10:33:57 +01002109 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002110
2111 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002112 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02002113 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01002114 int err, idx = 0;
2115
2116 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2117 name = "Bass Speaker";
2118 else if (num_pins >= 3) {
2119 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01002120 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01002121 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002122 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002123 name = pfx;
2124 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01002126 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002127 if (err < 0)
2128 return err;
2129 }
2130 return 0;
2131}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002132
Takashi Iwai352f7f92012-12-19 12:52:06 +01002133static int create_hp_out_ctls(struct hda_codec *codec)
2134{
2135 struct hda_gen_spec *spec = codec->spec;
2136 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002137 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002138 "Headphone");
2139}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Takashi Iwai352f7f92012-12-19 12:52:06 +01002141static int create_speaker_out_ctls(struct hda_codec *codec)
2142{
2143 struct hda_gen_spec *spec = codec->spec;
2144 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002145 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002146 "Speaker");
2147}
2148
2149/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002150 * independent HP controls
2151 */
2152
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02002153static void call_hp_automute(struct hda_codec *codec,
2154 struct hda_jack_callback *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002155static int indep_hp_info(struct snd_kcontrol *kcontrol,
2156 struct snd_ctl_elem_info *uinfo)
2157{
2158 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2159}
2160
2161static int indep_hp_get(struct snd_kcontrol *kcontrol,
2162 struct snd_ctl_elem_value *ucontrol)
2163{
2164 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2165 struct hda_gen_spec *spec = codec->spec;
2166 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2167 return 0;
2168}
2169
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002170static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2171 int nomix_path_idx, int mix_path_idx,
2172 int out_type);
2173
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002174static int indep_hp_put(struct snd_kcontrol *kcontrol,
2175 struct snd_ctl_elem_value *ucontrol)
2176{
2177 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2178 struct hda_gen_spec *spec = codec->spec;
2179 unsigned int select = ucontrol->value.enumerated.item[0];
2180 int ret = 0;
2181
2182 mutex_lock(&spec->pcm_mutex);
2183 if (spec->active_streams) {
2184 ret = -EBUSY;
2185 goto unlock;
2186 }
2187
2188 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002189 hda_nid_t *dacp;
2190 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2191 dacp = &spec->private_dac_nids[0];
2192 else
2193 dacp = &spec->multiout.hp_out_nid[0];
2194
2195 /* update HP aamix paths in case it conflicts with indep HP */
2196 if (spec->have_aamix_ctl) {
2197 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2198 update_aamix_paths(codec, spec->aamix_mode,
2199 spec->out_paths[0],
2200 spec->aamix_out_paths[0],
2201 spec->autocfg.line_out_type);
2202 else
2203 update_aamix_paths(codec, spec->aamix_mode,
2204 spec->hp_paths[0],
2205 spec->aamix_out_paths[1],
2206 AUTO_PIN_HP_OUT);
2207 }
2208
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002209 spec->indep_hp_enabled = select;
2210 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002211 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002212 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002213 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002214
Takashi Iwai963afde2013-05-31 15:20:31 +02002215 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002216 ret = 1;
2217 }
2218 unlock:
2219 mutex_unlock(&spec->pcm_mutex);
2220 return ret;
2221}
2222
2223static const struct snd_kcontrol_new indep_hp_ctl = {
2224 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2225 .name = "Independent HP",
2226 .info = indep_hp_info,
2227 .get = indep_hp_get,
2228 .put = indep_hp_put,
2229};
2230
2231
2232static int create_indep_hp_ctls(struct hda_codec *codec)
2233{
2234 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002235 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002236
2237 if (!spec->indep_hp)
2238 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002239 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2240 dac = spec->multiout.dac_nids[0];
2241 else
2242 dac = spec->multiout.hp_out_nid[0];
2243 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002244 spec->indep_hp = 0;
2245 return 0;
2246 }
2247
2248 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002249 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002250 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2251 return -ENOMEM;
2252 return 0;
2253}
2254
2255/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002256 * channel mode enum control
2257 */
2258
2259static int ch_mode_info(struct snd_kcontrol *kcontrol,
2260 struct snd_ctl_elem_info *uinfo)
2261{
2262 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2263 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002264 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002265
2266 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2267 uinfo->count = 1;
2268 uinfo->value.enumerated.items = spec->multi_ios + 1;
2269 if (uinfo->value.enumerated.item > spec->multi_ios)
2270 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002271 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2272 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002273 return 0;
2274}
2275
2276static int ch_mode_get(struct snd_kcontrol *kcontrol,
2277 struct snd_ctl_elem_value *ucontrol)
2278{
2279 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2280 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002281 ucontrol->value.enumerated.item[0] =
2282 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002283 return 0;
2284}
2285
Takashi Iwai196c17662013-01-04 15:01:40 +01002286static inline struct nid_path *
2287get_multiio_path(struct hda_codec *codec, int idx)
2288{
2289 struct hda_gen_spec *spec = codec->spec;
2290 return snd_hda_get_path_from_idx(codec,
2291 spec->out_paths[spec->autocfg.line_outs + idx]);
2292}
2293
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002294static void update_automute_all(struct hda_codec *codec);
2295
Takashi Iwai65033cc2013-04-16 12:31:05 +02002296/* Default value to be passed as aamix argument for snd_hda_activate_path();
2297 * used for output paths
2298 */
2299static bool aamix_default(struct hda_gen_spec *spec)
2300{
2301 return !spec->have_aamix_ctl || spec->aamix_mode;
2302}
2303
Takashi Iwai352f7f92012-12-19 12:52:06 +01002304static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2305{
2306 struct hda_gen_spec *spec = codec->spec;
2307 hda_nid_t nid = spec->multi_io[idx].pin;
2308 struct nid_path *path;
2309
Takashi Iwai196c17662013-01-04 15:01:40 +01002310 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002311 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002313
2314 if (path->active == output)
2315 return 0;
2316
2317 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002318 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002319 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002320 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002321 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002322 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002323 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002324 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002325 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002327
2328 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002329 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002330
Takashi Iwai352f7f92012-12-19 12:52:06 +01002331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
2333
Takashi Iwai352f7f92012-12-19 12:52:06 +01002334static int ch_mode_put(struct snd_kcontrol *kcontrol,
2335 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002337 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2338 struct hda_gen_spec *spec = codec->spec;
2339 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Takashi Iwai352f7f92012-12-19 12:52:06 +01002341 ch = ucontrol->value.enumerated.item[0];
2342 if (ch < 0 || ch > spec->multi_ios)
2343 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002344 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002345 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002346 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002347 for (i = 0; i < spec->multi_ios; i++)
2348 set_multi_io(codec, i, i < ch);
2349 spec->multiout.max_channels = max(spec->ext_channel_count,
2350 spec->const_channel_count);
2351 if (spec->need_dac_fix)
2352 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return 1;
2354}
2355
Takashi Iwai352f7f92012-12-19 12:52:06 +01002356static const struct snd_kcontrol_new channel_mode_enum = {
2357 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2358 .name = "Channel Mode",
2359 .info = ch_mode_info,
2360 .get = ch_mode_get,
2361 .put = ch_mode_put,
2362};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Takashi Iwai352f7f92012-12-19 12:52:06 +01002364static int create_multi_channel_mode(struct hda_codec *codec)
2365{
2366 struct hda_gen_spec *spec = codec->spec;
2367
2368 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002369 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002370 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 return 0;
2373}
2374
Takashi Iwai352f7f92012-12-19 12:52:06 +01002375/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002376 * aamix loopback enable/disable switch
2377 */
2378
2379#define loopback_mixing_info indep_hp_info
2380
2381static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2382 struct snd_ctl_elem_value *ucontrol)
2383{
2384 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2385 struct hda_gen_spec *spec = codec->spec;
2386 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2387 return 0;
2388}
2389
2390static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002391 int nomix_path_idx, int mix_path_idx,
2392 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002393{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002394 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002395 struct nid_path *nomix_path, *mix_path;
2396
2397 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2398 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2399 if (!nomix_path || !mix_path)
2400 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002401
2402 /* if HP aamix path is driven from a different DAC and the
2403 * independent HP mode is ON, can't turn on aamix path
2404 */
2405 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2406 mix_path->path[0] != spec->alt_dac_nid)
2407 do_mix = false;
2408
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002409 if (do_mix) {
2410 snd_hda_activate_path(codec, nomix_path, false, true);
2411 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002412 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002413 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002414 snd_hda_activate_path(codec, mix_path, false, false);
2415 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002416 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002417 }
2418}
2419
2420static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2421 struct snd_ctl_elem_value *ucontrol)
2422{
2423 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2424 struct hda_gen_spec *spec = codec->spec;
2425 unsigned int val = ucontrol->value.enumerated.item[0];
2426
2427 if (val == spec->aamix_mode)
2428 return 0;
2429 spec->aamix_mode = val;
2430 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002431 spec->aamix_out_paths[0],
2432 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002433 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002434 spec->aamix_out_paths[1],
2435 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002436 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002437 spec->aamix_out_paths[2],
2438 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002439 return 1;
2440}
2441
2442static const struct snd_kcontrol_new loopback_mixing_enum = {
2443 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2444 .name = "Loopback Mixing",
2445 .info = loopback_mixing_info,
2446 .get = loopback_mixing_get,
2447 .put = loopback_mixing_put,
2448};
2449
2450static int create_loopback_mixing_ctl(struct hda_codec *codec)
2451{
2452 struct hda_gen_spec *spec = codec->spec;
2453
2454 if (!spec->mixer_nid)
2455 return 0;
2456 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2457 spec->aamix_out_paths[2]))
2458 return 0;
2459 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2460 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002461 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002462 return 0;
2463}
2464
2465/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002466 * shared headphone/mic handling
2467 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002468
Takashi Iwai352f7f92012-12-19 12:52:06 +01002469static void call_update_outputs(struct hda_codec *codec);
2470
2471/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002472static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002473{
2474 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002475 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002476 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002477 hda_nid_t pin;
2478
2479 pin = spec->hp_mic_pin;
2480 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2481
2482 if (!force) {
2483 val = snd_hda_codec_get_pin_target(codec, pin);
2484 if (as_mic) {
2485 if (val & PIN_IN)
2486 return;
2487 } else {
2488 if (val & PIN_OUT)
2489 return;
2490 }
2491 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002492
2493 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002494 /* if the HP pin doesn't support VREF and the codec driver gives an
2495 * alternative pin, set up the VREF on that pin instead
2496 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002497 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2498 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2499 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2500 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002501 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002502 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002503 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002504
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002505 if (!spec->hp_mic_jack_modes) {
2506 if (as_mic)
2507 val |= PIN_IN;
2508 else
2509 val = PIN_HP;
2510 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002511 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002512 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002513}
2514
2515/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002516static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002517{
2518 struct hda_gen_spec *spec = codec->spec;
2519 struct auto_pin_cfg *cfg = &spec->autocfg;
2520 unsigned int defcfg;
2521 hda_nid_t nid;
2522
Takashi Iwai967303d2013-02-19 17:12:42 +01002523 if (!spec->hp_mic) {
2524 if (spec->suppress_hp_mic_detect)
2525 return 0;
2526 /* automatic detection: only if no input or a single internal
2527 * input pin is found, try to detect the shared hp/mic
2528 */
2529 if (cfg->num_inputs > 1)
2530 return 0;
2531 else if (cfg->num_inputs == 1) {
2532 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2533 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2534 return 0;
2535 }
2536 }
2537
2538 spec->hp_mic = 0; /* clear once */
2539 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002540 return 0;
2541
Takashi Iwai967303d2013-02-19 17:12:42 +01002542 nid = 0;
2543 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2544 nid = cfg->line_out_pins[0];
2545 else if (cfg->hp_outs > 0)
2546 nid = cfg->hp_pins[0];
2547 if (!nid)
2548 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002549
2550 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2551 return 0; /* no input */
2552
Takashi Iwai967303d2013-02-19 17:12:42 +01002553 cfg->inputs[cfg->num_inputs].pin = nid;
2554 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002555 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002556 cfg->num_inputs++;
2557 spec->hp_mic = 1;
2558 spec->hp_mic_pin = nid;
2559 /* we can't handle auto-mic together with HP-mic */
2560 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002561 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002562 return 0;
2563}
2564
Takashi Iwai978e77e2013-01-10 16:57:58 +01002565/*
2566 * output jack mode
2567 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002568
2569static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2570
2571static const char * const out_jack_texts[] = {
2572 "Line Out", "Headphone Out",
2573};
2574
Takashi Iwai978e77e2013-01-10 16:57:58 +01002575static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2576 struct snd_ctl_elem_info *uinfo)
2577{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002578 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002579}
2580
2581static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2582 struct snd_ctl_elem_value *ucontrol)
2583{
2584 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2585 hda_nid_t nid = kcontrol->private_value;
2586 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2587 ucontrol->value.enumerated.item[0] = 1;
2588 else
2589 ucontrol->value.enumerated.item[0] = 0;
2590 return 0;
2591}
2592
2593static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2594 struct snd_ctl_elem_value *ucontrol)
2595{
2596 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2597 hda_nid_t nid = kcontrol->private_value;
2598 unsigned int val;
2599
2600 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2601 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2602 return 0;
2603 snd_hda_set_pin_ctl_cache(codec, nid, val);
2604 return 1;
2605}
2606
2607static const struct snd_kcontrol_new out_jack_mode_enum = {
2608 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2609 .info = out_jack_mode_info,
2610 .get = out_jack_mode_get,
2611 .put = out_jack_mode_put,
2612};
2613
2614static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2615{
2616 struct hda_gen_spec *spec = codec->spec;
2617 int i;
2618
2619 for (i = 0; i < spec->kctls.used; i++) {
2620 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2621 if (!strcmp(kctl->name, name) && kctl->index == idx)
2622 return true;
2623 }
2624 return false;
2625}
2626
2627static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2628 char *name, size_t name_len)
2629{
2630 struct hda_gen_spec *spec = codec->spec;
2631 int idx = 0;
2632
2633 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2634 strlcat(name, " Jack Mode", name_len);
2635
2636 for (; find_kctl_name(codec, name, idx); idx++)
2637 ;
2638}
2639
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002640static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2641{
2642 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002643 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002644 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2645 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2646 return 2;
2647 }
2648 return 1;
2649}
2650
Takashi Iwai978e77e2013-01-10 16:57:58 +01002651static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2652 hda_nid_t *pins)
2653{
2654 struct hda_gen_spec *spec = codec->spec;
2655 int i;
2656
2657 for (i = 0; i < num_pins; i++) {
2658 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002659 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002660 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002661 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002662 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002663 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002664 get_jack_mode_name(codec, pin, name, sizeof(name));
2665 knew = snd_hda_gen_add_kctl(spec, name,
2666 &out_jack_mode_enum);
2667 if (!knew)
2668 return -ENOMEM;
2669 knew->private_value = pin;
2670 }
2671 }
2672
2673 return 0;
2674}
2675
Takashi Iwai294765582013-01-17 09:52:11 +01002676/*
2677 * input jack mode
2678 */
2679
2680/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2681#define NUM_VREFS 6
2682
2683static const char * const vref_texts[NUM_VREFS] = {
2684 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2685 "", "Mic 80pc Bias", "Mic 100pc Bias"
2686};
2687
2688static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2689{
2690 unsigned int pincap;
2691
2692 pincap = snd_hda_query_pin_caps(codec, pin);
2693 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2694 /* filter out unusual vrefs */
2695 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2696 return pincap;
2697}
2698
2699/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2700static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2701{
2702 unsigned int i, n = 0;
2703
2704 for (i = 0; i < NUM_VREFS; i++) {
2705 if (vref_caps & (1 << i)) {
2706 if (n == item_idx)
2707 return i;
2708 n++;
2709 }
2710 }
2711 return 0;
2712}
2713
2714/* convert back from the vref ctl index to the enum item index */
2715static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2716{
2717 unsigned int i, n = 0;
2718
2719 for (i = 0; i < NUM_VREFS; i++) {
2720 if (i == idx)
2721 return n;
2722 if (vref_caps & (1 << i))
2723 n++;
2724 }
2725 return 0;
2726}
2727
2728static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2729 struct snd_ctl_elem_info *uinfo)
2730{
2731 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2732 hda_nid_t nid = kcontrol->private_value;
2733 unsigned int vref_caps = get_vref_caps(codec, nid);
2734
2735 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2736 vref_texts);
2737 /* set the right text */
2738 strcpy(uinfo->value.enumerated.name,
2739 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2740 return 0;
2741}
2742
2743static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2744 struct snd_ctl_elem_value *ucontrol)
2745{
2746 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2747 hda_nid_t nid = kcontrol->private_value;
2748 unsigned int vref_caps = get_vref_caps(codec, nid);
2749 unsigned int idx;
2750
2751 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2752 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2753 return 0;
2754}
2755
2756static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2757 struct snd_ctl_elem_value *ucontrol)
2758{
2759 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2760 hda_nid_t nid = kcontrol->private_value;
2761 unsigned int vref_caps = get_vref_caps(codec, nid);
2762 unsigned int val, idx;
2763
2764 val = snd_hda_codec_get_pin_target(codec, nid);
2765 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2766 if (idx == ucontrol->value.enumerated.item[0])
2767 return 0;
2768
2769 val &= ~AC_PINCTL_VREFEN;
2770 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2771 snd_hda_set_pin_ctl_cache(codec, nid, val);
2772 return 1;
2773}
2774
2775static const struct snd_kcontrol_new in_jack_mode_enum = {
2776 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2777 .info = in_jack_mode_info,
2778 .get = in_jack_mode_get,
2779 .put = in_jack_mode_put,
2780};
2781
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002782static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2783{
2784 struct hda_gen_spec *spec = codec->spec;
2785 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002786 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002787 nitems = hweight32(get_vref_caps(codec, pin));
2788 return nitems ? nitems : 1;
2789}
2790
Takashi Iwai294765582013-01-17 09:52:11 +01002791static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2792{
2793 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002794 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002795 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002796 unsigned int defcfg;
2797
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002798 if (pin == spec->hp_mic_pin)
2799 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002800
2801 /* no jack mode for fixed pins */
2802 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2803 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2804 return 0;
2805
2806 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002807 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002808 return 0;
2809
2810 get_jack_mode_name(codec, pin, name, sizeof(name));
2811 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2812 if (!knew)
2813 return -ENOMEM;
2814 knew->private_value = pin;
2815 return 0;
2816}
2817
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002818/*
2819 * HP/mic shared jack mode
2820 */
2821static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2822 struct snd_ctl_elem_info *uinfo)
2823{
2824 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2825 hda_nid_t nid = kcontrol->private_value;
2826 int out_jacks = get_out_jack_num_items(codec, nid);
2827 int in_jacks = get_in_jack_num_items(codec, nid);
2828 const char *text = NULL;
2829 int idx;
2830
2831 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2832 uinfo->count = 1;
2833 uinfo->value.enumerated.items = out_jacks + in_jacks;
2834 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2835 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2836 idx = uinfo->value.enumerated.item;
2837 if (idx < out_jacks) {
2838 if (out_jacks > 1)
2839 text = out_jack_texts[idx];
2840 else
2841 text = "Headphone Out";
2842 } else {
2843 idx -= out_jacks;
2844 if (in_jacks > 1) {
2845 unsigned int vref_caps = get_vref_caps(codec, nid);
2846 text = vref_texts[get_vref_idx(vref_caps, idx)];
2847 } else
2848 text = "Mic In";
2849 }
2850
2851 strcpy(uinfo->value.enumerated.name, text);
2852 return 0;
2853}
2854
2855static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2856{
2857 int out_jacks = get_out_jack_num_items(codec, nid);
2858 int in_jacks = get_in_jack_num_items(codec, nid);
2859 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2860 int idx = 0;
2861
2862 if (val & PIN_OUT) {
2863 if (out_jacks > 1 && val == PIN_HP)
2864 idx = 1;
2865 } else if (val & PIN_IN) {
2866 idx = out_jacks;
2867 if (in_jacks > 1) {
2868 unsigned int vref_caps = get_vref_caps(codec, nid);
2869 val &= AC_PINCTL_VREFEN;
2870 idx += cvt_from_vref_idx(vref_caps, val);
2871 }
2872 }
2873 return idx;
2874}
2875
2876static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2877 struct snd_ctl_elem_value *ucontrol)
2878{
2879 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2880 hda_nid_t nid = kcontrol->private_value;
2881 ucontrol->value.enumerated.item[0] =
2882 get_cur_hp_mic_jack_mode(codec, nid);
2883 return 0;
2884}
2885
2886static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2887 struct snd_ctl_elem_value *ucontrol)
2888{
2889 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2890 hda_nid_t nid = kcontrol->private_value;
2891 int out_jacks = get_out_jack_num_items(codec, nid);
2892 int in_jacks = get_in_jack_num_items(codec, nid);
2893 unsigned int val, oldval, idx;
2894
2895 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2896 idx = ucontrol->value.enumerated.item[0];
2897 if (oldval == idx)
2898 return 0;
2899
2900 if (idx < out_jacks) {
2901 if (out_jacks > 1)
2902 val = idx ? PIN_HP : PIN_OUT;
2903 else
2904 val = PIN_HP;
2905 } else {
2906 idx -= out_jacks;
2907 if (in_jacks > 1) {
2908 unsigned int vref_caps = get_vref_caps(codec, nid);
2909 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002910 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2911 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002912 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002913 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002914 }
2915 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002916 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002917
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002918 return 1;
2919}
2920
2921static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2922 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2923 .info = hp_mic_jack_mode_info,
2924 .get = hp_mic_jack_mode_get,
2925 .put = hp_mic_jack_mode_put,
2926};
2927
2928static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2929{
2930 struct hda_gen_spec *spec = codec->spec;
2931 struct snd_kcontrol_new *knew;
2932
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002933 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2934 &hp_mic_jack_mode_enum);
2935 if (!knew)
2936 return -ENOMEM;
2937 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002938 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002939 return 0;
2940}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002941
2942/*
2943 * Parse input paths
2944 */
2945
Takashi Iwai352f7f92012-12-19 12:52:06 +01002946/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002947static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002948{
2949 struct hda_amp_list *list;
2950
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002951 list = snd_array_new(&spec->loopback_list);
2952 if (!list)
2953 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002954 list->nid = mix;
2955 list->dir = HDA_INPUT;
2956 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002957 spec->loopback.amplist = spec->loopback_list.list;
2958 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002959}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002960
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002961/* return true if either a volume or a mute amp is found for the given
2962 * aamix path; the amp has to be either in the mixer node or its direct leaf
2963 */
2964static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2965 hda_nid_t pin, unsigned int *mix_val,
2966 unsigned int *mute_val)
2967{
2968 int idx, num_conns;
2969 const hda_nid_t *list;
2970 hda_nid_t nid;
2971
2972 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2973 if (idx < 0)
2974 return false;
2975
2976 *mix_val = *mute_val = 0;
2977 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2978 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2979 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2980 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2981 if (*mix_val && *mute_val)
2982 return true;
2983
2984 /* check leaf node */
2985 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2986 if (num_conns < idx)
2987 return false;
2988 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01002989 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2990 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002991 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01002992 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
2993 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002994 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2995
2996 return *mix_val || *mute_val;
2997}
2998
Takashi Iwai352f7f92012-12-19 12:52:06 +01002999/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01003000static int new_analog_input(struct hda_codec *codec, int input_idx,
3001 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003002 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003004 struct hda_gen_spec *spec = codec->spec;
3005 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003006 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003007 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003009 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3010 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003011
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003012 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003013 if (!path)
3014 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003015 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01003016 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003017
3018 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003019 if (mix_val) {
3020 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003021 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003023 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 }
3025
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003026 if (mute_val) {
3027 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003028 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003030 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 }
3032
Takashi Iwai352f7f92012-12-19 12:52:06 +01003033 path->active = true;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003034 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003035 err = add_loopback_list(spec, mix_nid, idx);
3036 if (err < 0)
3037 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003038
3039 if (spec->mixer_nid != spec->mixer_merge_nid &&
3040 !spec->loopback_merge_path) {
3041 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3042 spec->mixer_merge_nid, 0);
3043 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003044 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003045 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003046 path->pin_fixed = true; /* static route */
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003047 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003048 spec->loopback_merge_path =
3049 snd_hda_get_path_idx(codec, path);
3050 }
3051 }
3052
Takashi Iwai352f7f92012-12-19 12:52:06 +01003053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054}
3055
Takashi Iwai352f7f92012-12-19 12:52:06 +01003056static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003058 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3059 return (pincap & AC_PINCAP_IN) != 0;
3060}
3061
3062/* Parse the codec tree and retrieve ADCs */
3063static int fill_adc_nids(struct hda_codec *codec)
3064{
3065 struct hda_gen_spec *spec = codec->spec;
3066 hda_nid_t nid;
3067 hda_nid_t *adc_nids = spec->adc_nids;
3068 int max_nums = ARRAY_SIZE(spec->adc_nids);
Takashi Iwai7639a062015-03-03 10:07:24 +01003069 int nums = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003070
Takashi Iwai7639a062015-03-03 10:07:24 +01003071 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003072 unsigned int caps = get_wcaps(codec, nid);
3073 int type = get_wcaps_type(caps);
3074
3075 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3076 continue;
3077 adc_nids[nums] = nid;
3078 if (++nums >= max_nums)
3079 break;
3080 }
3081 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01003082
3083 /* copy the detected ADCs to all_adcs[] */
3084 spec->num_all_adcs = nums;
3085 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3086
Takashi Iwai352f7f92012-12-19 12:52:06 +01003087 return nums;
3088}
3089
3090/* filter out invalid adc_nids that don't give all active input pins;
3091 * if needed, check whether dynamic ADC-switching is available
3092 */
3093static int check_dyn_adc_switch(struct hda_codec *codec)
3094{
3095 struct hda_gen_spec *spec = codec->spec;
3096 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003097 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003098 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003099
Takashi Iwai352f7f92012-12-19 12:52:06 +01003100 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003101 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003102 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003103 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003104 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003105 break;
3106 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003107 if (i >= imux->num_items) {
3108 ok_bits |= (1 << n);
3109 nums++;
3110 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003111 }
3112
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003113 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003114 /* check whether ADC-switch is possible */
3115 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003116 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003117 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003118 spec->dyn_adc_idx[i] = n;
3119 break;
3120 }
3121 }
3122 }
3123
Takashi Iwai4e76a882014-02-25 12:21:03 +01003124 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003125 spec->dyn_adc_switch = 1;
3126 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003127 /* shrink the invalid adcs and input paths */
3128 nums = 0;
3129 for (n = 0; n < spec->num_adc_nids; n++) {
3130 if (!(ok_bits & (1 << n)))
3131 continue;
3132 if (n != nums) {
3133 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003134 for (i = 0; i < imux->num_items; i++) {
3135 invalidate_nid_path(codec,
3136 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003137 spec->input_paths[i][nums] =
3138 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003139 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003140 }
3141 nums++;
3142 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003143 spec->num_adc_nids = nums;
3144 }
3145
Takashi Iwai967303d2013-02-19 17:12:42 +01003146 if (imux->num_items == 1 ||
3147 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003148 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003149 spec->num_adc_nids = 1; /* reduce to a single ADC */
3150 }
3151
3152 /* single index for individual volumes ctls */
3153 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3154 spec->num_adc_nids = 1;
3155
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 return 0;
3157}
3158
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003159/* parse capture source paths from the given pin and create imux items */
3160static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003161 int cfg_idx, int num_adcs,
3162 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003163{
3164 struct hda_gen_spec *spec = codec->spec;
3165 struct hda_input_mux *imux = &spec->input_mux;
3166 int imux_idx = imux->num_items;
3167 bool imux_added = false;
3168 int c;
3169
3170 for (c = 0; c < num_adcs; c++) {
3171 struct nid_path *path;
3172 hda_nid_t adc = spec->adc_nids[c];
3173
3174 if (!is_reachable_path(codec, pin, adc))
3175 continue;
3176 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3177 if (!path)
3178 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003179 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003180 spec->input_paths[imux_idx][c] =
3181 snd_hda_get_path_idx(codec, path);
3182
3183 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003184 if (spec->hp_mic_pin == pin)
3185 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003186 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003187 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003188 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003189 if (spec->dyn_adc_switch)
3190 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003191 }
3192 }
3193
3194 return 0;
3195}
3196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003198 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003200
Takashi Iwaic9700422013-01-18 10:17:30 +01003201/* fill the label for each input at first */
3202static int fill_input_pin_labels(struct hda_codec *codec)
3203{
3204 struct hda_gen_spec *spec = codec->spec;
3205 const struct auto_pin_cfg *cfg = &spec->autocfg;
3206 int i;
3207
3208 for (i = 0; i < cfg->num_inputs; i++) {
3209 hda_nid_t pin = cfg->inputs[i].pin;
3210 const char *label;
3211 int j, idx;
3212
3213 if (!is_input_pin(codec, pin))
3214 continue;
3215
3216 label = hda_get_autocfg_input_label(codec, cfg, i);
3217 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003218 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003219 if (spec->input_labels[j] &&
3220 !strcmp(spec->input_labels[j], label)) {
3221 idx = spec->input_label_idxs[j] + 1;
3222 break;
3223 }
3224 }
3225
3226 spec->input_labels[i] = label;
3227 spec->input_label_idxs[i] = idx;
3228 }
3229
3230 return 0;
3231}
3232
Takashi Iwai9dba2052013-01-18 10:01:15 +01003233#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3234
Takashi Iwai352f7f92012-12-19 12:52:06 +01003235static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003236{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003237 struct hda_gen_spec *spec = codec->spec;
3238 const struct auto_pin_cfg *cfg = &spec->autocfg;
3239 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003240 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003241 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003242 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003243
Takashi Iwai352f7f92012-12-19 12:52:06 +01003244 num_adcs = fill_adc_nids(codec);
3245 if (num_adcs < 0)
3246 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003247
Takashi Iwaic9700422013-01-18 10:17:30 +01003248 err = fill_input_pin_labels(codec);
3249 if (err < 0)
3250 return err;
3251
Takashi Iwai352f7f92012-12-19 12:52:06 +01003252 for (i = 0; i < cfg->num_inputs; i++) {
3253 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Takashi Iwai352f7f92012-12-19 12:52:06 +01003255 pin = cfg->inputs[i].pin;
3256 if (!is_input_pin(codec, pin))
3257 continue;
3258
Takashi Iwai2c12c302013-01-10 09:33:29 +01003259 val = PIN_IN;
3260 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3261 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai3e1b0c42015-04-27 10:43:22 +02003262 if (pin != spec->hp_mic_pin &&
3263 !snd_hda_codec_get_pin_target(codec, pin))
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003264 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003265
Takashi Iwai352f7f92012-12-19 12:52:06 +01003266 if (mixer) {
3267 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003268 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003269 spec->input_labels[i],
3270 spec->input_label_idxs[i],
3271 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003272 if (err < 0)
3273 return err;
3274 }
3275 }
3276
Takashi Iwaic9700422013-01-18 10:17:30 +01003277 err = parse_capture_source(codec, pin, i, num_adcs,
3278 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003279 if (err < 0)
3280 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003281
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003282 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003283 err = create_in_jack_mode(codec, pin);
3284 if (err < 0)
3285 return err;
3286 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003287 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003288
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003289 /* add stereo mix when explicitly enabled via hint */
Takashi Iwai74f14b32014-12-15 13:43:59 +01003290 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003291 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003292 "Stereo Mix", 0);
3293 if (err < 0)
3294 return err;
Takashi Iwai82d04e12014-12-15 13:40:42 +01003295 else
3296 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003297 }
3298
3299 return 0;
3300}
3301
3302
3303/*
3304 * input source mux
3305 */
3306
Takashi Iwaic697b712013-01-07 17:09:26 +01003307/* get the input path specified by the given adc and imux indices */
3308static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003309{
3310 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003311 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3312 snd_BUG();
3313 return NULL;
3314 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003315 if (spec->dyn_adc_switch)
3316 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003317 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003318 snd_BUG();
3319 return NULL;
3320 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003321 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003322}
3323
3324static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3325 unsigned int idx);
3326
3327static int mux_enum_info(struct snd_kcontrol *kcontrol,
3328 struct snd_ctl_elem_info *uinfo)
3329{
3330 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3331 struct hda_gen_spec *spec = codec->spec;
3332 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3333}
3334
3335static int mux_enum_get(struct snd_kcontrol *kcontrol,
3336 struct snd_ctl_elem_value *ucontrol)
3337{
3338 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3339 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003340 /* the ctls are created at once with multiple counts */
3341 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003342
3343 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3344 return 0;
3345}
3346
3347static int mux_enum_put(struct snd_kcontrol *kcontrol,
3348 struct snd_ctl_elem_value *ucontrol)
3349{
3350 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003351 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003352 return mux_select(codec, adc_idx,
3353 ucontrol->value.enumerated.item[0]);
3354}
3355
Takashi Iwai352f7f92012-12-19 12:52:06 +01003356static const struct snd_kcontrol_new cap_src_temp = {
3357 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3358 .name = "Input Source",
3359 .info = mux_enum_info,
3360 .get = mux_enum_get,
3361 .put = mux_enum_put,
3362};
3363
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003364/*
3365 * capture volume and capture switch ctls
3366 */
3367
Takashi Iwai352f7f92012-12-19 12:52:06 +01003368typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3369 struct snd_ctl_elem_value *ucontrol);
3370
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003371/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003372static int cap_put_caller(struct snd_kcontrol *kcontrol,
3373 struct snd_ctl_elem_value *ucontrol,
3374 put_call_t func, int type)
3375{
3376 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3377 struct hda_gen_spec *spec = codec->spec;
3378 const struct hda_input_mux *imux;
3379 struct nid_path *path;
3380 int i, adc_idx, err = 0;
3381
3382 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003383 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003384 mutex_lock(&codec->control_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003385 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003386 path = get_input_path(codec, adc_idx, i);
3387 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003388 continue;
3389 kcontrol->private_value = path->ctls[type];
3390 err = func(kcontrol, ucontrol);
3391 if (err < 0)
Takashi Iwaia551d912015-02-26 12:34:49 +01003392 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003393 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003394 mutex_unlock(&codec->control_mutex);
3395 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003396 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003397 return err;
3398}
3399
3400/* capture volume ctl callbacks */
3401#define cap_vol_info snd_hda_mixer_amp_volume_info
3402#define cap_vol_get snd_hda_mixer_amp_volume_get
3403#define cap_vol_tlv snd_hda_mixer_amp_tlv
3404
3405static int cap_vol_put(struct snd_kcontrol *kcontrol,
3406 struct snd_ctl_elem_value *ucontrol)
3407{
3408 return cap_put_caller(kcontrol, ucontrol,
3409 snd_hda_mixer_amp_volume_put,
3410 NID_PATH_VOL_CTL);
3411}
3412
3413static const struct snd_kcontrol_new cap_vol_temp = {
3414 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3415 .name = "Capture Volume",
3416 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3417 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3418 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3419 .info = cap_vol_info,
3420 .get = cap_vol_get,
3421 .put = cap_vol_put,
3422 .tlv = { .c = cap_vol_tlv },
3423};
3424
3425/* capture switch ctl callbacks */
3426#define cap_sw_info snd_ctl_boolean_stereo_info
3427#define cap_sw_get snd_hda_mixer_amp_switch_get
3428
3429static int cap_sw_put(struct snd_kcontrol *kcontrol,
3430 struct snd_ctl_elem_value *ucontrol)
3431{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003432 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003433 snd_hda_mixer_amp_switch_put,
3434 NID_PATH_MUTE_CTL);
3435}
3436
3437static const struct snd_kcontrol_new cap_sw_temp = {
3438 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3439 .name = "Capture Switch",
3440 .info = cap_sw_info,
3441 .get = cap_sw_get,
3442 .put = cap_sw_put,
3443};
3444
3445static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3446{
3447 hda_nid_t nid;
3448 int i, depth;
3449
3450 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3451 for (depth = 0; depth < 3; depth++) {
3452 if (depth >= path->depth)
3453 return -EINVAL;
3454 i = path->depth - depth - 1;
3455 nid = path->path[i];
3456 if (!path->ctls[NID_PATH_VOL_CTL]) {
3457 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3458 path->ctls[NID_PATH_VOL_CTL] =
3459 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3460 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3461 int idx = path->idx[i];
3462 if (!depth && codec->single_adc_amp)
3463 idx = 0;
3464 path->ctls[NID_PATH_VOL_CTL] =
3465 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3466 }
3467 }
3468 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3469 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3470 path->ctls[NID_PATH_MUTE_CTL] =
3471 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3472 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3473 int idx = path->idx[i];
3474 if (!depth && codec->single_adc_amp)
3475 idx = 0;
3476 path->ctls[NID_PATH_MUTE_CTL] =
3477 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3478 }
3479 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 return 0;
3482}
3483
Takashi Iwai352f7f92012-12-19 12:52:06 +01003484static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003486 struct hda_gen_spec *spec = codec->spec;
3487 struct auto_pin_cfg *cfg = &spec->autocfg;
3488 unsigned int val;
3489 int i;
3490
3491 if (!spec->inv_dmic_split)
3492 return false;
3493 for (i = 0; i < cfg->num_inputs; i++) {
3494 if (cfg->inputs[i].pin != nid)
3495 continue;
3496 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3497 return false;
3498 val = snd_hda_codec_get_pincfg(codec, nid);
3499 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3500 }
3501 return false;
3502}
3503
Takashi Iwaia90229e2013-01-18 14:10:00 +01003504/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003505static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3506 struct snd_ctl_elem_value *ucontrol)
3507{
3508 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3509 struct hda_gen_spec *spec = codec->spec;
3510 int ret;
3511
3512 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3513 if (ret < 0)
3514 return ret;
3515
Takashi Iwaia90229e2013-01-18 14:10:00 +01003516 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003517 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003518
3519 return ret;
3520}
3521
Takashi Iwai352f7f92012-12-19 12:52:06 +01003522static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3523 int idx, bool is_switch, unsigned int ctl,
3524 bool inv_dmic)
3525{
3526 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003527 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003528 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3529 const char *sfx = is_switch ? "Switch" : "Volume";
3530 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003531 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003532
3533 if (!ctl)
3534 return 0;
3535
3536 if (label)
3537 snprintf(tmpname, sizeof(tmpname),
3538 "%s Capture %s", label, sfx);
3539 else
3540 snprintf(tmpname, sizeof(tmpname),
3541 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003542 knew = add_control(spec, type, tmpname, idx,
3543 amp_val_replace_channels(ctl, chs));
3544 if (!knew)
3545 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003546 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003547 knew->put = cap_single_sw_put;
3548 if (!inv_dmic)
3549 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003550
3551 /* Make independent right kcontrol */
3552 if (label)
3553 snprintf(tmpname, sizeof(tmpname),
3554 "Inverted %s Capture %s", label, sfx);
3555 else
3556 snprintf(tmpname, sizeof(tmpname),
3557 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003558 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003559 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003560 if (!knew)
3561 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003562 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003563 knew->put = cap_single_sw_put;
3564 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003565}
3566
3567/* create single (and simple) capture volume and switch controls */
3568static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3569 unsigned int vol_ctl, unsigned int sw_ctl,
3570 bool inv_dmic)
3571{
3572 int err;
3573 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3574 if (err < 0)
3575 return err;
3576 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3577 if (err < 0)
3578 return err;
3579 return 0;
3580}
3581
3582/* create bound capture volume and switch controls */
3583static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3584 unsigned int vol_ctl, unsigned int sw_ctl)
3585{
3586 struct hda_gen_spec *spec = codec->spec;
3587 struct snd_kcontrol_new *knew;
3588
3589 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003590 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003591 if (!knew)
3592 return -ENOMEM;
3593 knew->index = idx;
3594 knew->private_value = vol_ctl;
3595 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3596 }
3597 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003598 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003599 if (!knew)
3600 return -ENOMEM;
3601 knew->index = idx;
3602 knew->private_value = sw_ctl;
3603 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3604 }
3605 return 0;
3606}
3607
3608/* return the vol ctl when used first in the imux list */
3609static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3610{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003611 struct nid_path *path;
3612 unsigned int ctl;
3613 int i;
3614
Takashi Iwaic697b712013-01-07 17:09:26 +01003615 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003616 if (!path)
3617 return 0;
3618 ctl = path->ctls[type];
3619 if (!ctl)
3620 return 0;
3621 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003622 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003623 if (path && path->ctls[type] == ctl)
3624 return 0;
3625 }
3626 return ctl;
3627}
3628
3629/* create individual capture volume and switch controls per input */
3630static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3631{
3632 struct hda_gen_spec *spec = codec->spec;
3633 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003634 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003635
3636 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003637 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003638 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003639
Takashi Iwaic9700422013-01-18 10:17:30 +01003640 idx = imux->items[i].index;
3641 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003642 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003643 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3644
3645 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003646 err = add_single_cap_ctl(codec,
3647 spec->input_labels[idx],
3648 spec->input_label_idxs[idx],
3649 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003650 get_first_cap_ctl(codec, i, type),
3651 inv_dmic);
3652 if (err < 0)
3653 return err;
3654 }
3655 }
3656 return 0;
3657}
3658
3659static int create_capture_mixers(struct hda_codec *codec)
3660{
3661 struct hda_gen_spec *spec = codec->spec;
3662 struct hda_input_mux *imux = &spec->input_mux;
3663 int i, n, nums, err;
3664
3665 if (spec->dyn_adc_switch)
3666 nums = 1;
3667 else
3668 nums = spec->num_adc_nids;
3669
3670 if (!spec->auto_mic && imux->num_items > 1) {
3671 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003672 const char *name;
3673 name = nums > 1 ? "Input Source" : "Capture Source";
3674 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003675 if (!knew)
3676 return -ENOMEM;
3677 knew->count = nums;
3678 }
3679
3680 for (n = 0; n < nums; n++) {
3681 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003682 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003683 bool inv_dmic = false;
3684 int vol, sw;
3685
3686 vol = sw = 0;
3687 for (i = 0; i < imux->num_items; i++) {
3688 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003689 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003690 if (!path)
3691 continue;
3692 parse_capvol_in_path(codec, path);
3693 if (!vol)
3694 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003695 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003696 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003697 if (!same_amp_caps(codec, vol,
3698 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3699 multi_cap_vol = true;
3700 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003701 if (!sw)
3702 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003703 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003704 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003705 if (!same_amp_caps(codec, sw,
3706 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3707 multi_cap_vol = true;
3708 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003709 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3710 inv_dmic = true;
3711 }
3712
3713 if (!multi)
3714 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3715 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003716 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003717 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3718 else
3719 err = create_multi_cap_vol_ctl(codec);
3720 if (err < 0)
3721 return err;
3722 }
3723
3724 return 0;
3725}
3726
3727/*
3728 * add mic boosts if needed
3729 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003730
3731/* check whether the given amp is feasible as a boost volume */
3732static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3733 int dir, int idx)
3734{
3735 unsigned int step;
3736
3737 if (!nid_has_volume(codec, nid, dir) ||
3738 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3739 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3740 return false;
3741
3742 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3743 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3744 if (step < 0x20)
3745 return false;
3746 return true;
3747}
3748
3749/* look for a boost amp in a widget close to the pin */
3750static unsigned int look_for_boost_amp(struct hda_codec *codec,
3751 struct nid_path *path)
3752{
3753 unsigned int val = 0;
3754 hda_nid_t nid;
3755 int depth;
3756
3757 for (depth = 0; depth < 3; depth++) {
3758 if (depth >= path->depth - 1)
3759 break;
3760 nid = path->path[depth];
3761 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3762 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3763 break;
3764 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3765 path->idx[depth])) {
3766 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3767 HDA_INPUT);
3768 break;
3769 }
3770 }
3771
3772 return val;
3773}
3774
Takashi Iwai352f7f92012-12-19 12:52:06 +01003775static int parse_mic_boost(struct hda_codec *codec)
3776{
3777 struct hda_gen_spec *spec = codec->spec;
3778 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003779 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003780 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003781
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003782 if (!spec->num_adc_nids)
3783 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003784
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003785 for (i = 0; i < imux->num_items; i++) {
3786 struct nid_path *path;
3787 unsigned int val;
3788 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003789 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003790
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003791 idx = imux->items[i].index;
3792 if (idx >= imux->num_items)
3793 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003794
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003795 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003796 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003797 continue;
3798
3799 path = get_input_path(codec, 0, i);
3800 if (!path)
3801 continue;
3802
3803 val = look_for_boost_amp(codec, path);
3804 if (!val)
3805 continue;
3806
3807 /* create a boost control */
3808 snprintf(boost_label, sizeof(boost_label),
3809 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003810 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3811 spec->input_label_idxs[idx], val))
3812 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003813
3814 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003815 }
3816 return 0;
3817}
3818
3819/*
3820 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3821 */
3822static void parse_digital(struct hda_codec *codec)
3823{
3824 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003825 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003826 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003827 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003828
3829 /* support multiple SPDIFs; the secondary is set up as a slave */
3830 nums = 0;
3831 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003832 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003833 dig_nid = look_for_dac(codec, pin, true);
3834 if (!dig_nid)
3835 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003836 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003837 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003838 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003839 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003840 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003841 path->pin_fixed = true; /* no jack detection */
Takashi Iwai196c17662013-01-04 15:01:40 +01003842 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003843 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003844 if (!nums) {
3845 spec->multiout.dig_out_nid = dig_nid;
3846 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3847 } else {
3848 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3849 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
Dan Carpenterd576422e2014-05-14 17:18:31 +03003850 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003851 spec->slave_dig_outs[nums - 1] = dig_nid;
3852 }
3853 nums++;
3854 }
3855
3856 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003857 pin = spec->autocfg.dig_in_pin;
Takashi Iwai7639a062015-03-03 10:07:24 +01003858 for_each_hda_codec_node(dig_nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003859 unsigned int wcaps = get_wcaps(codec, dig_nid);
3860 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3861 continue;
3862 if (!(wcaps & AC_WCAP_DIGITAL))
3863 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003864 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003865 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003866 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003867 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003868 path->pin_fixed = true; /* no jack */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003869 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003870 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003871 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003872 break;
3873 }
3874 }
3875 }
3876}
3877
3878
3879/*
3880 * input MUX handling
3881 */
3882
3883static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3884
3885/* select the given imux item; either unmute exclusively or select the route */
3886static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3887 unsigned int idx)
3888{
3889 struct hda_gen_spec *spec = codec->spec;
3890 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003891 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003892
3893 imux = &spec->input_mux;
3894 if (!imux->num_items)
3895 return 0;
3896
3897 if (idx >= imux->num_items)
3898 idx = imux->num_items - 1;
3899 if (spec->cur_mux[adc_idx] == idx)
3900 return 0;
3901
Takashi Iwai55196ff2013-01-24 17:32:56 +01003902 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3903 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003904 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003905 if (old_path->active)
3906 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003907
3908 spec->cur_mux[adc_idx] = idx;
3909
Takashi Iwai967303d2013-02-19 17:12:42 +01003910 if (spec->hp_mic)
3911 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003912
3913 if (spec->dyn_adc_switch)
3914 dyn_adc_pcm_resetup(codec, idx);
3915
Takashi Iwaic697b712013-01-07 17:09:26 +01003916 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003917 if (!path)
3918 return 0;
3919 if (path->active)
3920 return 0;
3921 snd_hda_activate_path(codec, path, true, false);
3922 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003923 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003924 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003925 return 1;
3926}
3927
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003928/* power up/down widgets in the all paths that match with the given NID
3929 * as terminals (either start- or endpoint)
3930 *
3931 * returns the last changed NID, or zero if unchanged.
3932 */
3933static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
3934 int pin_state, int stream_state)
3935{
3936 struct hda_gen_spec *spec = codec->spec;
3937 hda_nid_t last, changed = 0;
3938 struct nid_path *path;
3939 int n;
3940
3941 for (n = 0; n < spec->paths.used; n++) {
3942 path = snd_array_elem(&spec->paths, n);
3943 if (path->path[0] == nid ||
3944 path->path[path->depth - 1] == nid) {
3945 bool pin_old = path->pin_enabled;
3946 bool stream_old = path->stream_enabled;
3947
3948 if (pin_state >= 0)
3949 path->pin_enabled = pin_state;
3950 if (stream_state >= 0)
3951 path->stream_enabled = stream_state;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003952 if ((!path->pin_fixed && path->pin_enabled != pin_old)
3953 || path->stream_enabled != stream_old) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003954 last = path_power_update(codec, path, true);
3955 if (last)
3956 changed = last;
3957 }
3958 }
3959 }
3960 return changed;
3961}
3962
Takashi Iwaid5ac0102015-04-09 10:21:30 +02003963/* check the jack status for power control */
3964static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
3965{
3966 if (!is_jack_detectable(codec, pin))
3967 return true;
3968 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
3969}
3970
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003971/* power up/down the paths of the given pin according to the jack state;
3972 * power = 0/1 : only power up/down if it matches with the jack state,
3973 * < 0 : force power up/down to follow the jack sate
3974 *
3975 * returns the last changed NID, or zero if unchanged.
3976 */
3977static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
3978 int power)
3979{
3980 bool on;
3981
Takashi Iwai967b1302015-03-20 18:21:03 +01003982 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003983 return 0;
3984
Takashi Iwaid5ac0102015-04-09 10:21:30 +02003985 on = detect_pin_state(codec, pin);
3986
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003987 if (power >= 0 && on != power)
3988 return 0;
3989 return set_path_power(codec, pin, on, -1);
3990}
3991
3992static void pin_power_callback(struct hda_codec *codec,
3993 struct hda_jack_callback *jack,
3994 bool on)
3995{
3996 if (jack && jack->tbl->nid)
3997 sync_power_state_change(codec,
3998 set_pin_power_jack(codec, jack->tbl->nid, on));
3999}
4000
4001/* callback only doing power up -- called at first */
4002static void pin_power_up_callback(struct hda_codec *codec,
4003 struct hda_jack_callback *jack)
4004{
4005 pin_power_callback(codec, jack, true);
4006}
4007
4008/* callback only doing power down -- called at last */
4009static void pin_power_down_callback(struct hda_codec *codec,
4010 struct hda_jack_callback *jack)
4011{
4012 pin_power_callback(codec, jack, false);
4013}
4014
4015/* set up the power up/down callbacks */
4016static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4017 const hda_nid_t *pins, bool on)
4018{
4019 int i;
4020 hda_jack_callback_fn cb =
4021 on ? pin_power_up_callback : pin_power_down_callback;
4022
4023 for (i = 0; i < num_pins && pins[i]; i++) {
4024 if (is_jack_detectable(codec, pins[i]))
4025 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4026 else
4027 set_path_power(codec, pins[i], true, -1);
4028 }
4029}
4030
4031/* enabled power callback to each available I/O pin with jack detections;
4032 * the digital I/O pins are excluded because of the unreliable detectsion
4033 */
4034static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4035{
4036 struct hda_gen_spec *spec = codec->spec;
4037 struct auto_pin_cfg *cfg = &spec->autocfg;
4038 int i;
4039
Takashi Iwai967b1302015-03-20 18:21:03 +01004040 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004041 return;
4042 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4043 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4044 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4045 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4046 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4047 for (i = 0; i < cfg->num_inputs; i++)
4048 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4049}
4050
4051/* sync path power up/down with the jack states of given pins */
4052static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4053 const hda_nid_t *pins)
4054{
4055 int i;
4056
4057 for (i = 0; i < num_pins && pins[i]; i++)
4058 if (is_jack_detectable(codec, pins[i]))
4059 set_pin_power_jack(codec, pins[i], -1);
4060}
4061
4062/* sync path power up/down with pins; called at init and resume */
4063static void sync_all_pin_power_ctls(struct hda_codec *codec)
4064{
4065 struct hda_gen_spec *spec = codec->spec;
4066 struct auto_pin_cfg *cfg = &spec->autocfg;
4067 int i;
4068
Takashi Iwai967b1302015-03-20 18:21:03 +01004069 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004070 return;
4071 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4072 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4073 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4074 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4075 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4076 for (i = 0; i < cfg->num_inputs; i++)
4077 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4078}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004079
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004080/* add fake paths if not present yet */
4081static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4082 int num_pins, const hda_nid_t *pins)
4083{
4084 struct hda_gen_spec *spec = codec->spec;
4085 struct nid_path *path;
4086 int i;
4087
4088 for (i = 0; i < num_pins; i++) {
4089 if (!pins[i])
4090 break;
4091 if (get_nid_path(codec, nid, pins[i], 0))
4092 continue;
4093 path = snd_array_new(&spec->paths);
4094 if (!path)
4095 return -ENOMEM;
4096 memset(path, 0, sizeof(*path));
4097 path->depth = 2;
4098 path->path[0] = nid;
4099 path->path[1] = pins[i];
4100 path->active = true;
4101 }
4102 return 0;
4103}
4104
4105/* create fake paths to all outputs from beep */
4106static int add_fake_beep_paths(struct hda_codec *codec)
4107{
4108 struct hda_gen_spec *spec = codec->spec;
4109 struct auto_pin_cfg *cfg = &spec->autocfg;
4110 hda_nid_t nid = spec->beep_nid;
4111 int err;
4112
Takashi Iwai967b1302015-03-20 18:21:03 +01004113 if (!codec->power_save_node || !nid)
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004114 return 0;
4115 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4116 if (err < 0)
4117 return err;
4118 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4119 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4120 if (err < 0)
4121 return err;
4122 }
4123 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4124 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4125 cfg->speaker_pins);
4126 if (err < 0)
4127 return err;
4128 }
4129 return 0;
4130}
4131
4132/* power up/down beep widget and its output paths */
4133static void beep_power_hook(struct hda_beep *beep, bool on)
4134{
4135 set_path_power(beep->codec, beep->nid, -1, on);
4136}
4137
Takashi Iwai6b275b12015-03-20 18:11:05 +01004138/**
4139 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4140 * @codec: the HDA codec
4141 * @pin: NID of pin to fix
4142 */
4143int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4144{
4145 struct hda_gen_spec *spec = codec->spec;
4146 struct nid_path *path;
4147
4148 path = snd_array_new(&spec->paths);
4149 if (!path)
4150 return -ENOMEM;
4151 memset(path, 0, sizeof(*path));
4152 path->depth = 1;
4153 path->path[0] = pin;
4154 path->active = true;
4155 path->pin_fixed = true;
4156 path->stream_enabled = true;
4157 return 0;
4158}
4159EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4160
Takashi Iwai352f7f92012-12-19 12:52:06 +01004161/*
4162 * Jack detections for HP auto-mute and mic-switch
4163 */
4164
4165/* check each pin in the given array; returns true if any of them is plugged */
4166static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4167{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004168 int i;
4169 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004170
4171 for (i = 0; i < num_pins; i++) {
4172 hda_nid_t nid = pins[i];
4173 if (!nid)
4174 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01004175 /* don't detect pins retasked as inputs */
4176 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4177 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004178 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4179 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004180 }
4181 return present;
4182}
4183
4184/* standard HP/line-out auto-mute helper */
4185static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004186 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004187{
4188 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004189 int i;
4190
4191 for (i = 0; i < num_pins; i++) {
4192 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01004193 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004194 if (!nid)
4195 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004196
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004197 oldval = snd_hda_codec_get_pin_target(codec, nid);
4198 if (oldval & PIN_IN)
4199 continue; /* no mute for inputs */
4200
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004201 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004202 struct nid_path *path;
4203 hda_nid_t mute_nid;
4204
4205 path = snd_hda_get_path_from_idx(codec, paths[i]);
4206 if (!path)
4207 continue;
4208 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4209 if (!mute_nid)
4210 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004211 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004212 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004213 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004214 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004215 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004216 } else {
4217 /* don't reset VREF value in case it's controlling
4218 * the amp (see alc861_fixup_asus_amp_vref_0f())
4219 */
4220 if (spec->keep_vref_in_automute)
4221 val = oldval & ~PIN_HP;
4222 else
4223 val = 0;
4224 if (!mute)
4225 val |= oldval;
4226 /* here we call update_pin_ctl() so that the pinctl is
4227 * changed without changing the pinctl target value;
4228 * the original target value will be still referred at
4229 * the init / resume again
4230 */
4231 update_pin_ctl(codec, nid, val);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004232 }
4233
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01004234 set_pin_eapd(codec, nid, !mute);
Takashi Iwai967b1302015-03-20 18:21:03 +01004235 if (codec->power_save_node) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004236 bool on = !mute;
4237 if (on)
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004238 on = detect_pin_state(codec, nid);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004239 set_path_power(codec, nid, on, -1);
4240 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004241 }
4242}
4243
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004244/**
4245 * snd_hda_gen_update_outputs - Toggle outputs muting
4246 * @codec: the HDA codec
4247 *
4248 * Update the mute status of all outputs based on the current jack states.
4249 */
Takashi Iwai5d550e12012-12-19 15:16:44 +01004250void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004251{
4252 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004253 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004254 int on;
4255
4256 /* Control HP pins/amps depending on master_mute state;
4257 * in general, HP pins/amps control should be enabled in all cases,
4258 * but currently set only for master_mute, just to be safe
4259 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004260 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4261 paths = spec->out_paths;
4262 else
4263 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01004264 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004265 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004266
4267 if (!spec->automute_speaker)
4268 on = 0;
4269 else
4270 on = spec->hp_jack_present | spec->line_jack_present;
4271 on |= spec->master_mute;
Takashi Iwai47b9ddb82013-01-16 18:18:00 +01004272 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004273 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4274 paths = spec->out_paths;
4275 else
4276 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004277 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004278 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004279
4280 /* toggle line-out mutes if needed, too */
4281 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4282 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4283 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4284 return;
4285 if (!spec->automute_lo)
4286 on = 0;
4287 else
4288 on = spec->hp_jack_present;
4289 on |= spec->master_mute;
Takashi Iwai47b9ddb82013-01-16 18:18:00 +01004290 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004291 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004292 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004293 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004294}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004295EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004296
4297static void call_update_outputs(struct hda_codec *codec)
4298{
4299 struct hda_gen_spec *spec = codec->spec;
4300 if (spec->automute_hook)
4301 spec->automute_hook(codec);
4302 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01004303 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004304
4305 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4306 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4307 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004308}
4309
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004310/**
4311 * snd_hda_gen_hp_automute - standard HP-automute helper
4312 * @codec: the HDA codec
4313 * @jack: jack object, NULL for the whole
4314 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004315void snd_hda_gen_hp_automute(struct hda_codec *codec,
4316 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004317{
4318 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01004319 hda_nid_t *pins = spec->autocfg.hp_pins;
4320 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004321
Takashi Iwai92603c52013-01-22 07:46:31 +01004322 /* No detection for the first HP jack during indep-HP mode */
4323 if (spec->indep_hp_enabled) {
4324 pins++;
4325 num_pins--;
4326 }
4327
4328 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004329 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4330 return;
4331 call_update_outputs(codec);
4332}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004333EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004334
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004335/**
4336 * snd_hda_gen_line_automute - standard line-out-automute helper
4337 * @codec: the HDA codec
4338 * @jack: jack object, NULL for the whole
4339 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004340void snd_hda_gen_line_automute(struct hda_codec *codec,
4341 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004342{
4343 struct hda_gen_spec *spec = codec->spec;
4344
4345 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4346 return;
4347 /* check LO jack only when it's different from HP */
4348 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4349 return;
4350
4351 spec->line_jack_present =
4352 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4353 spec->autocfg.line_out_pins);
4354 if (!spec->automute_speaker || !spec->detect_lo)
4355 return;
4356 call_update_outputs(codec);
4357}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004358EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004359
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004360/**
4361 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4362 * @codec: the HDA codec
4363 * @jack: jack object, NULL for the whole
4364 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004365void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4366 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004367{
4368 struct hda_gen_spec *spec = codec->spec;
4369 int i;
4370
4371 if (!spec->auto_mic)
4372 return;
4373
4374 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01004375 hda_nid_t pin = spec->am_entry[i].pin;
4376 /* don't detect pins retasked as outputs */
4377 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4378 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004379 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004380 mux_select(codec, 0, spec->am_entry[i].idx);
4381 return;
4382 }
4383 }
4384 mux_select(codec, 0, spec->am_entry[0].idx);
4385}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004386EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004387
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004388/* call appropriate hooks */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004389static void call_hp_automute(struct hda_codec *codec,
4390 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004391{
4392 struct hda_gen_spec *spec = codec->spec;
4393 if (spec->hp_automute_hook)
4394 spec->hp_automute_hook(codec, jack);
4395 else
4396 snd_hda_gen_hp_automute(codec, jack);
4397}
4398
4399static void call_line_automute(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004400 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004401{
4402 struct hda_gen_spec *spec = codec->spec;
4403 if (spec->line_automute_hook)
4404 spec->line_automute_hook(codec, jack);
4405 else
4406 snd_hda_gen_line_automute(codec, jack);
4407}
4408
4409static void call_mic_autoswitch(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004410 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004411{
4412 struct hda_gen_spec *spec = codec->spec;
4413 if (spec->mic_autoswitch_hook)
4414 spec->mic_autoswitch_hook(codec, jack);
4415 else
4416 snd_hda_gen_mic_autoswitch(codec, jack);
4417}
4418
Takashi Iwai963afde2013-05-31 15:20:31 +02004419/* update jack retasking */
4420static void update_automute_all(struct hda_codec *codec)
4421{
4422 call_hp_automute(codec, NULL);
4423 call_line_automute(codec, NULL);
4424 call_mic_autoswitch(codec, NULL);
4425}
4426
Takashi Iwai352f7f92012-12-19 12:52:06 +01004427/*
4428 * Auto-Mute mode mixer enum support
4429 */
4430static int automute_mode_info(struct snd_kcontrol *kcontrol,
4431 struct snd_ctl_elem_info *uinfo)
4432{
4433 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4434 struct hda_gen_spec *spec = codec->spec;
4435 static const char * const texts3[] = {
4436 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004437 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
Takashi Iwai352f7f92012-12-19 12:52:06 +01004439 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4440 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4441 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4442}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
Takashi Iwai352f7f92012-12-19 12:52:06 +01004444static int automute_mode_get(struct snd_kcontrol *kcontrol,
4445 struct snd_ctl_elem_value *ucontrol)
4446{
4447 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4448 struct hda_gen_spec *spec = codec->spec;
4449 unsigned int val = 0;
4450 if (spec->automute_speaker)
4451 val++;
4452 if (spec->automute_lo)
4453 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004454
Takashi Iwai352f7f92012-12-19 12:52:06 +01004455 ucontrol->value.enumerated.item[0] = val;
4456 return 0;
4457}
4458
4459static int automute_mode_put(struct snd_kcontrol *kcontrol,
4460 struct snd_ctl_elem_value *ucontrol)
4461{
4462 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4463 struct hda_gen_spec *spec = codec->spec;
4464
4465 switch (ucontrol->value.enumerated.item[0]) {
4466 case 0:
4467 if (!spec->automute_speaker && !spec->automute_lo)
4468 return 0;
4469 spec->automute_speaker = 0;
4470 spec->automute_lo = 0;
4471 break;
4472 case 1:
4473 if (spec->automute_speaker_possible) {
4474 if (!spec->automute_lo && spec->automute_speaker)
4475 return 0;
4476 spec->automute_speaker = 1;
4477 spec->automute_lo = 0;
4478 } else if (spec->automute_lo_possible) {
4479 if (spec->automute_lo)
4480 return 0;
4481 spec->automute_lo = 1;
4482 } else
4483 return -EINVAL;
4484 break;
4485 case 2:
4486 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4487 return -EINVAL;
4488 if (spec->automute_speaker && spec->automute_lo)
4489 return 0;
4490 spec->automute_speaker = 1;
4491 spec->automute_lo = 1;
4492 break;
4493 default:
4494 return -EINVAL;
4495 }
4496 call_update_outputs(codec);
4497 return 1;
4498}
4499
4500static const struct snd_kcontrol_new automute_mode_enum = {
4501 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4502 .name = "Auto-Mute Mode",
4503 .info = automute_mode_info,
4504 .get = automute_mode_get,
4505 .put = automute_mode_put,
4506};
4507
4508static int add_automute_mode_enum(struct hda_codec *codec)
4509{
4510 struct hda_gen_spec *spec = codec->spec;
4511
Takashi Iwai12c93df2012-12-19 14:38:33 +01004512 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004513 return -ENOMEM;
4514 return 0;
4515}
4516
4517/*
4518 * Check the availability of HP/line-out auto-mute;
4519 * Set up appropriately if really supported
4520 */
4521static int check_auto_mute_availability(struct hda_codec *codec)
4522{
4523 struct hda_gen_spec *spec = codec->spec;
4524 struct auto_pin_cfg *cfg = &spec->autocfg;
4525 int present = 0;
4526 int i, err;
4527
Takashi Iwaif72706b2013-01-16 18:20:07 +01004528 if (spec->suppress_auto_mute)
4529 return 0;
4530
Takashi Iwai352f7f92012-12-19 12:52:06 +01004531 if (cfg->hp_pins[0])
4532 present++;
4533 if (cfg->line_out_pins[0])
4534 present++;
4535 if (cfg->speaker_pins[0])
4536 present++;
4537 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004538 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004539
4540 if (!cfg->speaker_pins[0] &&
4541 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4542 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4543 sizeof(cfg->speaker_pins));
4544 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
Takashi Iwai352f7f92012-12-19 12:52:06 +01004547 if (!cfg->hp_pins[0] &&
4548 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4549 memcpy(cfg->hp_pins, cfg->line_out_pins,
4550 sizeof(cfg->hp_pins));
4551 cfg->hp_outs = cfg->line_outs;
4552 }
4553
4554 for (i = 0; i < cfg->hp_outs; i++) {
4555 hda_nid_t nid = cfg->hp_pins[i];
4556 if (!is_jack_detectable(codec, nid))
4557 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004558 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai62f949b2014-09-11 14:06:53 +02004559 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004560 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004561 spec->detect_hp = 1;
4562 }
4563
4564 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4565 if (cfg->speaker_outs)
4566 for (i = 0; i < cfg->line_outs; i++) {
4567 hda_nid_t nid = cfg->line_out_pins[i];
4568 if (!is_jack_detectable(codec, nid))
4569 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004570 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004571 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004572 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004573 spec->detect_lo = 1;
4574 }
4575 spec->automute_lo_possible = spec->detect_hp;
4576 }
4577
4578 spec->automute_speaker_possible = cfg->speaker_outs &&
4579 (spec->detect_hp || spec->detect_lo);
4580
4581 spec->automute_lo = spec->automute_lo_possible;
4582 spec->automute_speaker = spec->automute_speaker_possible;
4583
4584 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4585 /* create a control for automute mode */
4586 err = add_automute_mode_enum(codec);
4587 if (err < 0)
4588 return err;
4589 }
4590 return 0;
4591}
4592
Takashi Iwai352f7f92012-12-19 12:52:06 +01004593/* check whether all auto-mic pins are valid; setup indices if OK */
4594static bool auto_mic_check_imux(struct hda_codec *codec)
4595{
4596 struct hda_gen_spec *spec = codec->spec;
4597 const struct hda_input_mux *imux;
4598 int i;
4599
4600 imux = &spec->input_mux;
4601 for (i = 0; i < spec->am_num_entries; i++) {
4602 spec->am_entry[i].idx =
4603 find_idx_in_nid_list(spec->am_entry[i].pin,
4604 spec->imux_pins, imux->num_items);
4605 if (spec->am_entry[i].idx < 0)
4606 return false; /* no corresponding imux */
4607 }
4608
4609 /* we don't need the jack detection for the first pin */
4610 for (i = 1; i < spec->am_num_entries; i++)
4611 snd_hda_jack_detect_enable_callback(codec,
4612 spec->am_entry[i].pin,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004613 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004614 return true;
4615}
4616
4617static int compare_attr(const void *ap, const void *bp)
4618{
4619 const struct automic_entry *a = ap;
4620 const struct automic_entry *b = bp;
4621 return (int)(a->attr - b->attr);
4622}
4623
4624/*
4625 * Check the availability of auto-mic switch;
4626 * Set up if really supported
4627 */
4628static int check_auto_mic_availability(struct hda_codec *codec)
4629{
4630 struct hda_gen_spec *spec = codec->spec;
4631 struct auto_pin_cfg *cfg = &spec->autocfg;
4632 unsigned int types;
4633 int i, num_pins;
4634
Takashi Iwaid12daf62013-01-07 16:32:11 +01004635 if (spec->suppress_auto_mic)
4636 return 0;
4637
Takashi Iwai352f7f92012-12-19 12:52:06 +01004638 types = 0;
4639 num_pins = 0;
4640 for (i = 0; i < cfg->num_inputs; i++) {
4641 hda_nid_t nid = cfg->inputs[i].pin;
4642 unsigned int attr;
4643 attr = snd_hda_codec_get_pincfg(codec, nid);
4644 attr = snd_hda_get_input_pin_attr(attr);
4645 if (types & (1 << attr))
4646 return 0; /* already occupied */
4647 switch (attr) {
4648 case INPUT_PIN_ATTR_INT:
4649 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4650 return 0; /* invalid type */
4651 break;
4652 case INPUT_PIN_ATTR_UNUSED:
4653 return 0; /* invalid entry */
4654 default:
4655 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4656 return 0; /* invalid type */
4657 if (!spec->line_in_auto_switch &&
4658 cfg->inputs[i].type != AUTO_PIN_MIC)
4659 return 0; /* only mic is allowed */
4660 if (!is_jack_detectable(codec, nid))
4661 return 0; /* no unsol support */
4662 break;
4663 }
4664 if (num_pins >= MAX_AUTO_MIC_PINS)
4665 return 0;
4666 types |= (1 << attr);
4667 spec->am_entry[num_pins].pin = nid;
4668 spec->am_entry[num_pins].attr = attr;
4669 num_pins++;
4670 }
4671
4672 if (num_pins < 2)
4673 return 0;
4674
4675 spec->am_num_entries = num_pins;
4676 /* sort the am_entry in the order of attr so that the pin with a
4677 * higher attr will be selected when the jack is plugged.
4678 */
4679 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4680 compare_attr, NULL);
4681
4682 if (!auto_mic_check_imux(codec))
4683 return 0;
4684
4685 spec->auto_mic = 1;
4686 spec->num_adc_nids = 1;
4687 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004688 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004689 spec->am_entry[0].pin,
4690 spec->am_entry[1].pin,
4691 spec->am_entry[2].pin);
4692
4693 return 0;
4694}
4695
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004696/**
4697 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4698 * into power down
4699 * @codec: the HDA codec
4700 * @nid: NID to evalute
4701 * @power_state: target power state
4702 */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004703unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004704 hda_nid_t nid,
4705 unsigned int power_state)
4706{
Takashi Iwaib6c09b32015-04-09 10:25:03 +02004707 struct hda_gen_spec *spec = codec->spec;
4708
4709 if (!spec->power_down_unused && !codec->power_save_node)
4710 return power_state;
Takashi Iwai7639a062015-03-03 10:07:24 +01004711 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004712 return power_state;
4713 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4714 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004715 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004716 return power_state;
4717 return AC_PWRST_D3;
4718}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004719EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004720
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004721/* mute all aamix inputs initially; parse up to the first leaves */
4722static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4723{
4724 int i, nums;
4725 const hda_nid_t *conn;
4726 bool has_amp;
4727
4728 nums = snd_hda_get_conn_list(codec, mix, &conn);
4729 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4730 for (i = 0; i < nums; i++) {
4731 if (has_amp)
Takashi Iwaief403ed2015-03-12 08:30:11 +01004732 update_amp(codec, mix, HDA_INPUT, i,
4733 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004734 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
Takashi Iwaief403ed2015-03-12 08:30:11 +01004735 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4736 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004737 }
4738}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004739
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004740/**
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004741 * snd_hda_gen_stream_pm - Stream power management callback
4742 * @codec: the HDA codec
4743 * @nid: audio widget
4744 * @on: power on/off flag
4745 *
Takashi Iwai967b1302015-03-20 18:21:03 +01004746 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004747 */
4748void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4749{
Takashi Iwai967b1302015-03-20 18:21:03 +01004750 if (codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004751 set_path_power(codec, nid, -1, on);
4752}
4753EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4754
4755/**
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004756 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4757 * set up the hda_gen_spec
4758 * @codec: the HDA codec
4759 * @cfg: Parsed pin configuration
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004760 *
4761 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004762 * or a negative error code
4763 */
4764int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004765 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004766{
4767 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004768 int err;
4769
Takashi Iwai1c70a582013-01-11 17:48:22 +01004770 parse_user_hints(codec);
4771
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004772 if (spec->mixer_nid && !spec->mixer_merge_nid)
4773 spec->mixer_merge_nid = spec->mixer_nid;
4774
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004775 if (cfg != &spec->autocfg) {
4776 spec->autocfg = *cfg;
4777 cfg = &spec->autocfg;
4778 }
4779
Takashi Iwai98bd1112013-03-22 14:53:50 +01004780 if (!spec->main_out_badness)
4781 spec->main_out_badness = &hda_main_out_badness;
4782 if (!spec->extra_out_badness)
4783 spec->extra_out_badness = &hda_extra_out_badness;
4784
David Henningsson6fc4cb92013-01-16 15:58:45 +01004785 fill_all_dac_nids(codec);
4786
Takashi Iwai352f7f92012-12-19 12:52:06 +01004787 if (!cfg->line_outs) {
4788 if (cfg->dig_outs || cfg->dig_in_pin) {
4789 spec->multiout.max_channels = 2;
4790 spec->no_analog = 1;
4791 goto dig_only;
4792 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004793 if (!cfg->num_inputs && !cfg->dig_in_pin)
4794 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004795 }
4796
4797 if (!spec->no_primary_hp &&
4798 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4799 cfg->line_outs <= cfg->hp_outs) {
4800 /* use HP as primary out */
4801 cfg->speaker_outs = cfg->line_outs;
4802 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4803 sizeof(cfg->speaker_pins));
4804 cfg->line_outs = cfg->hp_outs;
4805 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4806 cfg->hp_outs = 0;
4807 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4808 cfg->line_out_type = AUTO_PIN_HP_OUT;
4809 }
4810
4811 err = parse_output_paths(codec);
4812 if (err < 0)
4813 return err;
4814 err = create_multi_channel_mode(codec);
4815 if (err < 0)
4816 return err;
4817 err = create_multi_out_ctls(codec, cfg);
4818 if (err < 0)
4819 return err;
4820 err = create_hp_out_ctls(codec);
4821 if (err < 0)
4822 return err;
4823 err = create_speaker_out_ctls(codec);
4824 if (err < 0)
4825 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004826 err = create_indep_hp_ctls(codec);
4827 if (err < 0)
4828 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004829 err = create_loopback_mixing_ctl(codec);
4830 if (err < 0)
4831 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004832 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004833 if (err < 0)
4834 return err;
4835 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004836 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004837 return err;
4838
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004839 /* add power-down pin callbacks at first */
4840 add_all_pin_power_ctls(codec, false);
4841
Takashi Iwaia07a9492013-01-07 16:44:06 +01004842 spec->const_channel_count = spec->ext_channel_count;
4843 /* check the multiple speaker and headphone pins */
4844 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4845 spec->const_channel_count = max(spec->const_channel_count,
4846 cfg->speaker_outs * 2);
4847 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4848 spec->const_channel_count = max(spec->const_channel_count,
4849 cfg->hp_outs * 2);
4850 spec->multiout.max_channels = max(spec->ext_channel_count,
4851 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004852
4853 err = check_auto_mute_availability(codec);
4854 if (err < 0)
4855 return err;
4856
4857 err = check_dyn_adc_switch(codec);
4858 if (err < 0)
4859 return err;
4860
Takashi Iwai967303d2013-02-19 17:12:42 +01004861 err = check_auto_mic_availability(codec);
4862 if (err < 0)
4863 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004864
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004865 /* add stereo mix if available and not enabled yet */
4866 if (!spec->auto_mic && spec->mixer_nid &&
Takashi Iwai74f14b32014-12-15 13:43:59 +01004867 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4868 spec->input_mux.num_items > 1) {
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004869 err = parse_capture_source(codec, spec->mixer_nid,
4870 CFG_IDX_MIX, spec->num_all_adcs,
4871 "Stereo Mix", 0);
4872 if (err < 0)
4873 return err;
4874 }
4875
4876
Takashi Iwai352f7f92012-12-19 12:52:06 +01004877 err = create_capture_mixers(codec);
4878 if (err < 0)
4879 return err;
4880
4881 err = parse_mic_boost(codec);
4882 if (err < 0)
4883 return err;
4884
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004885 /* create "Headphone Mic Jack Mode" if no input selection is
4886 * available (or user specifies add_jack_modes hint)
4887 */
4888 if (spec->hp_mic_pin &&
4889 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4890 spec->add_jack_modes)) {
4891 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4892 if (err < 0)
4893 return err;
4894 }
4895
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004896 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004897 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4898 err = create_out_jack_modes(codec, cfg->line_outs,
4899 cfg->line_out_pins);
4900 if (err < 0)
4901 return err;
4902 }
4903 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4904 err = create_out_jack_modes(codec, cfg->hp_outs,
4905 cfg->hp_pins);
4906 if (err < 0)
4907 return err;
4908 }
4909 }
4910
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004911 /* add power-up pin callbacks at last */
4912 add_all_pin_power_ctls(codec, true);
4913
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004914 /* mute all aamix input initially */
4915 if (spec->mixer_nid)
4916 mute_all_mixer_nid(codec, spec->mixer_nid);
4917
Takashi Iwai352f7f92012-12-19 12:52:06 +01004918 dig_only:
4919 parse_digital(codec);
4920
Takashi Iwai967b1302015-03-20 18:21:03 +01004921 if (spec->power_down_unused || codec->power_save_node)
Takashi Iwai24fef902015-04-09 10:28:15 +02004922 if (!codec->power_filter)
4923 codec->power_filter = snd_hda_gen_path_power_filter;
Takashi Iwai55196ff2013-01-24 17:32:56 +01004924
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004925 if (!spec->no_analog && spec->beep_nid) {
4926 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4927 if (err < 0)
4928 return err;
Takashi Iwai967b1302015-03-20 18:21:03 +01004929 if (codec->beep && codec->power_save_node) {
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004930 err = add_fake_beep_paths(codec);
4931 if (err < 0)
4932 return err;
4933 codec->beep->power_hook = beep_power_hook;
4934 }
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004935 }
4936
Takashi Iwai352f7f92012-12-19 12:52:06 +01004937 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004939EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940
4941
4942/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004943 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004945
4946/* slave controls for virtual master */
4947static const char * const slave_pfxs[] = {
4948 "Front", "Surround", "Center", "LFE", "Side",
4949 "Headphone", "Speaker", "Mono", "Line Out",
4950 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004951 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4952 "Headphone Front", "Headphone Surround", "Headphone CLFE",
David Henningsson03ad6a82014-10-16 15:33:45 +02004953 "Headphone Side", "Headphone+LO", "Speaker+LO",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004954 NULL,
4955};
4956
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004957/**
4958 * snd_hda_gen_build_controls - Build controls from the parsed results
4959 * @codec: the HDA codec
4960 *
4961 * Pass this to build_controls patch_ops.
4962 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004963int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004965 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
Takashi Iwai36502d02012-12-19 15:15:10 +01004968 if (spec->kctls.used) {
4969 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4970 if (err < 0)
4971 return err;
4972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
Takashi Iwai352f7f92012-12-19 12:52:06 +01004974 if (spec->multiout.dig_out_nid) {
4975 err = snd_hda_create_dig_out_ctls(codec,
4976 spec->multiout.dig_out_nid,
4977 spec->multiout.dig_out_nid,
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004978 spec->pcm_rec[1]->pcm_type);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004979 if (err < 0)
4980 return err;
4981 if (!spec->no_analog) {
4982 err = snd_hda_create_spdif_share_sw(codec,
4983 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 if (err < 0)
4985 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004986 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 }
4988 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004989 if (spec->dig_in_nid) {
4990 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4991 if (err < 0)
4992 return err;
4993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
Takashi Iwai352f7f92012-12-19 12:52:06 +01004995 /* if we have no master control, let's create it */
4996 if (!spec->no_analog &&
4997 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004998 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004999 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005000 "Playback Volume");
5001 if (err < 0)
5002 return err;
5003 }
5004 if (!spec->no_analog &&
5005 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5006 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5007 NULL, slave_pfxs,
5008 "Playback Switch",
5009 true, &spec->vmaster_mute.sw_kctl);
5010 if (err < 0)
5011 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02005012 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01005013 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5014 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02005015 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5016 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018
Takashi Iwai352f7f92012-12-19 12:52:06 +01005019 free_kctls(spec); /* no longer needed */
5020
Takashi Iwai352f7f92012-12-19 12:52:06 +01005021 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5022 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 return err;
5024
5025 return 0;
5026}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005027EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005028
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
5030/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01005031 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005034static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5035 struct hda_codec *codec,
5036 struct snd_pcm_substream *substream,
5037 int action)
5038{
5039 struct hda_gen_spec *spec = codec->spec;
5040 if (spec->pcm_playback_hook)
5041 spec->pcm_playback_hook(hinfo, codec, substream, action);
5042}
5043
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005044static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5045 struct hda_codec *codec,
5046 struct snd_pcm_substream *substream,
5047 int action)
5048{
5049 struct hda_gen_spec *spec = codec->spec;
5050 if (spec->pcm_capture_hook)
5051 spec->pcm_capture_hook(hinfo, codec, substream, action);
5052}
5053
Takashi Iwai352f7f92012-12-19 12:52:06 +01005054/*
5055 * Analog playback callbacks
5056 */
5057static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5058 struct hda_codec *codec,
5059 struct snd_pcm_substream *substream)
5060{
5061 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005062 int err;
5063
5064 mutex_lock(&spec->pcm_mutex);
5065 err = snd_hda_multi_out_analog_open(codec,
5066 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005067 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005068 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005069 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005070 call_pcm_playback_hook(hinfo, codec, substream,
5071 HDA_GEN_PCM_ACT_OPEN);
5072 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005073 mutex_unlock(&spec->pcm_mutex);
5074 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005075}
5076
5077static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01005078 struct hda_codec *codec,
5079 unsigned int stream_tag,
5080 unsigned int format,
5081 struct snd_pcm_substream *substream)
5082{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005083 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005084 int err;
5085
5086 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5087 stream_tag, format, substream);
5088 if (!err)
5089 call_pcm_playback_hook(hinfo, codec, substream,
5090 HDA_GEN_PCM_ACT_PREPARE);
5091 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005092}
Takashi Iwai97ec5582006-03-21 11:29:07 +01005093
Takashi Iwai352f7f92012-12-19 12:52:06 +01005094static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5095 struct hda_codec *codec,
5096 struct snd_pcm_substream *substream)
5097{
5098 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005099 int err;
5100
5101 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5102 if (!err)
5103 call_pcm_playback_hook(hinfo, codec, substream,
5104 HDA_GEN_PCM_ACT_CLEANUP);
5105 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005106}
5107
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005108static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5109 struct hda_codec *codec,
5110 struct snd_pcm_substream *substream)
5111{
5112 struct hda_gen_spec *spec = codec->spec;
5113 mutex_lock(&spec->pcm_mutex);
5114 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005115 call_pcm_playback_hook(hinfo, codec, substream,
5116 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005117 mutex_unlock(&spec->pcm_mutex);
5118 return 0;
5119}
5120
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005121static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5122 struct hda_codec *codec,
5123 struct snd_pcm_substream *substream)
5124{
5125 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5126 return 0;
5127}
5128
5129static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5130 struct hda_codec *codec,
5131 unsigned int stream_tag,
5132 unsigned int format,
5133 struct snd_pcm_substream *substream)
5134{
5135 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5136 call_pcm_capture_hook(hinfo, codec, substream,
5137 HDA_GEN_PCM_ACT_PREPARE);
5138 return 0;
5139}
5140
5141static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5142 struct hda_codec *codec,
5143 struct snd_pcm_substream *substream)
5144{
5145 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5146 call_pcm_capture_hook(hinfo, codec, substream,
5147 HDA_GEN_PCM_ACT_CLEANUP);
5148 return 0;
5149}
5150
5151static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5152 struct hda_codec *codec,
5153 struct snd_pcm_substream *substream)
5154{
5155 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5156 return 0;
5157}
5158
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005159static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5160 struct hda_codec *codec,
5161 struct snd_pcm_substream *substream)
5162{
5163 struct hda_gen_spec *spec = codec->spec;
5164 int err = 0;
5165
5166 mutex_lock(&spec->pcm_mutex);
5167 if (!spec->indep_hp_enabled)
5168 err = -EBUSY;
5169 else
5170 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005171 call_pcm_playback_hook(hinfo, codec, substream,
5172 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005173 mutex_unlock(&spec->pcm_mutex);
5174 return err;
5175}
5176
5177static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5178 struct hda_codec *codec,
5179 struct snd_pcm_substream *substream)
5180{
5181 struct hda_gen_spec *spec = codec->spec;
5182 mutex_lock(&spec->pcm_mutex);
5183 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005184 call_pcm_playback_hook(hinfo, codec, substream,
5185 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005186 mutex_unlock(&spec->pcm_mutex);
5187 return 0;
5188}
5189
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005190static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5191 struct hda_codec *codec,
5192 unsigned int stream_tag,
5193 unsigned int format,
5194 struct snd_pcm_substream *substream)
5195{
5196 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5197 call_pcm_playback_hook(hinfo, codec, substream,
5198 HDA_GEN_PCM_ACT_PREPARE);
5199 return 0;
5200}
5201
5202static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5203 struct hda_codec *codec,
5204 struct snd_pcm_substream *substream)
5205{
5206 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5207 call_pcm_playback_hook(hinfo, codec, substream,
5208 HDA_GEN_PCM_ACT_CLEANUP);
5209 return 0;
5210}
5211
Takashi Iwai352f7f92012-12-19 12:52:06 +01005212/*
5213 * Digital out
5214 */
5215static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5216 struct hda_codec *codec,
5217 struct snd_pcm_substream *substream)
5218{
5219 struct hda_gen_spec *spec = codec->spec;
5220 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5221}
5222
5223static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5224 struct hda_codec *codec,
5225 unsigned int stream_tag,
5226 unsigned int format,
5227 struct snd_pcm_substream *substream)
5228{
5229 struct hda_gen_spec *spec = codec->spec;
5230 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5231 stream_tag, format, substream);
5232}
5233
5234static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5235 struct hda_codec *codec,
5236 struct snd_pcm_substream *substream)
5237{
5238 struct hda_gen_spec *spec = codec->spec;
5239 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5240}
5241
5242static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5243 struct hda_codec *codec,
5244 struct snd_pcm_substream *substream)
5245{
5246 struct hda_gen_spec *spec = codec->spec;
5247 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5248}
5249
5250/*
5251 * Analog capture
5252 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005253#define alt_capture_pcm_open capture_pcm_open
5254#define alt_capture_pcm_close capture_pcm_close
5255
Takashi Iwai352f7f92012-12-19 12:52:06 +01005256static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5257 struct hda_codec *codec,
5258 unsigned int stream_tag,
5259 unsigned int format,
5260 struct snd_pcm_substream *substream)
5261{
5262 struct hda_gen_spec *spec = codec->spec;
5263
5264 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01005265 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005266 call_pcm_capture_hook(hinfo, codec, substream,
5267 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005268 return 0;
5269}
5270
Takashi Iwai352f7f92012-12-19 12:52:06 +01005271static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5272 struct hda_codec *codec,
5273 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01005274{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005275 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01005276
Takashi Iwai352f7f92012-12-19 12:52:06 +01005277 snd_hda_codec_cleanup_stream(codec,
5278 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005279 call_pcm_capture_hook(hinfo, codec, substream,
5280 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005281 return 0;
5282}
5283
Takashi Iwai352f7f92012-12-19 12:52:06 +01005284/*
5285 */
5286static const struct hda_pcm_stream pcm_analog_playback = {
5287 .substreams = 1,
5288 .channels_min = 2,
5289 .channels_max = 8,
5290 /* NID is set in build_pcms */
5291 .ops = {
5292 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005293 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005294 .prepare = playback_pcm_prepare,
5295 .cleanup = playback_pcm_cleanup
5296 },
5297};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298
Takashi Iwai352f7f92012-12-19 12:52:06 +01005299static const struct hda_pcm_stream pcm_analog_capture = {
5300 .substreams = 1,
5301 .channels_min = 2,
5302 .channels_max = 2,
5303 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005304 .ops = {
5305 .open = capture_pcm_open,
5306 .close = capture_pcm_close,
5307 .prepare = capture_pcm_prepare,
5308 .cleanup = capture_pcm_cleanup
5309 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005310};
5311
5312static const struct hda_pcm_stream pcm_analog_alt_playback = {
5313 .substreams = 1,
5314 .channels_min = 2,
5315 .channels_max = 2,
5316 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005317 .ops = {
5318 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005319 .close = alt_playback_pcm_close,
5320 .prepare = alt_playback_pcm_prepare,
5321 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005322 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005323};
5324
5325static const struct hda_pcm_stream pcm_analog_alt_capture = {
5326 .substreams = 2, /* can be overridden */
5327 .channels_min = 2,
5328 .channels_max = 2,
5329 /* NID is set in build_pcms */
5330 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005331 .open = alt_capture_pcm_open,
5332 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005333 .prepare = alt_capture_pcm_prepare,
5334 .cleanup = alt_capture_pcm_cleanup
5335 },
5336};
5337
5338static const struct hda_pcm_stream pcm_digital_playback = {
5339 .substreams = 1,
5340 .channels_min = 2,
5341 .channels_max = 2,
5342 /* NID is set in build_pcms */
5343 .ops = {
5344 .open = dig_playback_pcm_open,
5345 .close = dig_playback_pcm_close,
5346 .prepare = dig_playback_pcm_prepare,
5347 .cleanup = dig_playback_pcm_cleanup
5348 },
5349};
5350
5351static const struct hda_pcm_stream pcm_digital_capture = {
5352 .substreams = 1,
5353 .channels_min = 2,
5354 .channels_max = 2,
5355 /* NID is set in build_pcms */
5356};
5357
5358/* Used by build_pcms to flag that a PCM has no playback stream */
5359static const struct hda_pcm_stream pcm_null_stream = {
5360 .substreams = 0,
5361 .channels_min = 0,
5362 .channels_max = 0,
5363};
5364
5365/*
5366 * dynamic changing ADC PCM streams
5367 */
5368static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5369{
5370 struct hda_gen_spec *spec = codec->spec;
5371 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5372
5373 if (spec->cur_adc && spec->cur_adc != new_adc) {
5374 /* stream is running, let's swap the current ADC */
5375 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5376 spec->cur_adc = new_adc;
5377 snd_hda_codec_setup_stream(codec, new_adc,
5378 spec->cur_adc_stream_tag, 0,
5379 spec->cur_adc_format);
5380 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005382 return false;
5383}
5384
5385/* analog capture with dynamic dual-adc changes */
5386static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5387 struct hda_codec *codec,
5388 unsigned int stream_tag,
5389 unsigned int format,
5390 struct snd_pcm_substream *substream)
5391{
5392 struct hda_gen_spec *spec = codec->spec;
5393 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5394 spec->cur_adc_stream_tag = stream_tag;
5395 spec->cur_adc_format = format;
5396 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5397 return 0;
5398}
5399
5400static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5401 struct hda_codec *codec,
5402 struct snd_pcm_substream *substream)
5403{
5404 struct hda_gen_spec *spec = codec->spec;
5405 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5406 spec->cur_adc = 0;
5407 return 0;
5408}
5409
5410static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5411 .substreams = 1,
5412 .channels_min = 2,
5413 .channels_max = 2,
5414 .nid = 0, /* fill later */
5415 .ops = {
5416 .prepare = dyn_adc_capture_pcm_prepare,
5417 .cleanup = dyn_adc_capture_pcm_cleanup
5418 },
5419};
5420
Takashi Iwaif873e532012-12-20 16:58:39 +01005421static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5422 const char *chip_name)
5423{
5424 char *p;
5425
5426 if (*str)
5427 return;
5428 strlcpy(str, chip_name, len);
5429
5430 /* drop non-alnum chars after a space */
5431 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5432 if (!isalnum(p[1])) {
5433 *p = 0;
5434 break;
5435 }
5436 }
5437 strlcat(str, sfx, len);
5438}
5439
Takashi Iwaifb83b632015-03-16 23:34:34 +01005440/* copy PCM stream info from @default_str, and override non-NULL entries
5441 * from @spec_str and @nid
5442 */
5443static void setup_pcm_stream(struct hda_pcm_stream *str,
5444 const struct hda_pcm_stream *default_str,
5445 const struct hda_pcm_stream *spec_str,
5446 hda_nid_t nid)
5447{
5448 *str = *default_str;
5449 if (nid)
5450 str->nid = nid;
5451 if (spec_str) {
5452 if (spec_str->substreams)
5453 str->substreams = spec_str->substreams;
5454 if (spec_str->channels_min)
5455 str->channels_min = spec_str->channels_min;
5456 if (spec_str->channels_max)
5457 str->channels_max = spec_str->channels_max;
5458 if (spec_str->rates)
5459 str->rates = spec_str->rates;
5460 if (spec_str->formats)
5461 str->formats = spec_str->formats;
5462 if (spec_str->maxbps)
5463 str->maxbps = spec_str->maxbps;
5464 }
5465}
5466
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005467/**
5468 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5469 * @codec: the HDA codec
5470 *
5471 * Pass this to build_pcms patch_ops.
5472 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005473int snd_hda_gen_build_pcms(struct hda_codec *codec)
5474{
5475 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005476 struct hda_pcm *info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005477 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
Takashi Iwai352f7f92012-12-19 12:52:06 +01005479 if (spec->no_analog)
5480 goto skip_analog;
5481
Takashi Iwaif873e532012-12-20 16:58:39 +01005482 fill_pcm_stream_name(spec->stream_name_analog,
5483 sizeof(spec->stream_name_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005484 " Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005485 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5486 if (!info)
5487 return -ENOMEM;
5488 spec->pcm_rec[0] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005489
5490 if (spec->multiout.num_dacs > 0) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005491 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5492 &pcm_analog_playback,
5493 spec->stream_analog_playback,
5494 spec->multiout.dac_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005495 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5496 spec->multiout.max_channels;
5497 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5498 spec->autocfg.line_outs == 2)
5499 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5500 snd_pcm_2_1_chmaps;
5501 }
5502 if (spec->num_adc_nids) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005503 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5504 (spec->dyn_adc_switch ?
5505 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5506 spec->stream_analog_capture,
5507 spec->adc_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005508 }
5509
Takashi Iwai352f7f92012-12-19 12:52:06 +01005510 skip_analog:
5511 /* SPDIF for stream index #1 */
5512 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005513 fill_pcm_stream_name(spec->stream_name_digital,
5514 sizeof(spec->stream_name_digital),
Takashi Iwai7639a062015-03-03 10:07:24 +01005515 " Digital", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005516 info = snd_hda_codec_pcm_new(codec, "%s",
5517 spec->stream_name_digital);
5518 if (!info)
5519 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005520 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005521 spec->pcm_rec[1] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005522 if (spec->dig_out_type)
5523 info->pcm_type = spec->dig_out_type;
5524 else
5525 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005526 if (spec->multiout.dig_out_nid)
5527 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5528 &pcm_digital_playback,
5529 spec->stream_digital_playback,
5530 spec->multiout.dig_out_nid);
5531 if (spec->dig_in_nid)
5532 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5533 &pcm_digital_capture,
5534 spec->stream_digital_capture,
5535 spec->dig_in_nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005536 }
5537
5538 if (spec->no_analog)
5539 return 0;
5540
5541 /* If the use of more than one ADC is requested for the current
5542 * model, configure a second analog capture-only PCM.
5543 */
5544 have_multi_adcs = (spec->num_adc_nids > 1) &&
5545 !spec->dyn_adc_switch && !spec->auto_mic;
5546 /* Additional Analaog capture for index #2 */
5547 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005548 fill_pcm_stream_name(spec->stream_name_alt_analog,
5549 sizeof(spec->stream_name_alt_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005550 " Alt Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005551 info = snd_hda_codec_pcm_new(codec, "%s",
5552 spec->stream_name_alt_analog);
5553 if (!info)
5554 return -ENOMEM;
5555 spec->pcm_rec[2] = info;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005556 if (spec->alt_dac_nid)
5557 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5558 &pcm_analog_alt_playback,
5559 spec->stream_analog_alt_playback,
5560 spec->alt_dac_nid);
5561 else
5562 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5563 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005564 if (have_multi_adcs) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005565 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5566 &pcm_analog_alt_capture,
5567 spec->stream_analog_alt_capture,
5568 spec->adc_nids[1]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005569 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5570 spec->num_adc_nids - 1;
5571 } else {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005572 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5573 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 }
5576
5577 return 0;
5578}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005579EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005580
5581
5582/*
5583 * Standard auto-parser initializations
5584 */
5585
Takashi Iwaid4156932013-01-07 10:08:02 +01005586/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005587static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005588{
5589 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005590 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005591
Takashi Iwai196c17662013-01-04 15:01:40 +01005592 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005593 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005594 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005595 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005596 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005597 snd_hda_activate_path(codec, path, path->active,
5598 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005599 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005600}
5601
5602/* initialize primary output paths */
5603static void init_multi_out(struct hda_codec *codec)
5604{
5605 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005606 int i;
5607
Takashi Iwaid4156932013-01-07 10:08:02 +01005608 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005609 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005610}
5611
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005612
Takashi Iwai2c12c302013-01-10 09:33:29 +01005613static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005614{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005615 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005616
Takashi Iwaid4156932013-01-07 10:08:02 +01005617 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005618 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005619}
5620
5621/* initialize hp and speaker paths */
5622static void init_extra_out(struct hda_codec *codec)
5623{
5624 struct hda_gen_spec *spec = codec->spec;
5625
5626 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005627 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005628 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5629 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005630 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005631}
5632
5633/* initialize multi-io paths */
5634static void init_multi_io(struct hda_codec *codec)
5635{
5636 struct hda_gen_spec *spec = codec->spec;
5637 int i;
5638
5639 for (i = 0; i < spec->multi_ios; i++) {
5640 hda_nid_t pin = spec->multi_io[i].pin;
5641 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005642 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005643 if (!path)
5644 continue;
5645 if (!spec->multi_io[i].ctl_in)
5646 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005647 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005648 snd_hda_activate_path(codec, path, path->active,
5649 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005650 }
5651}
5652
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005653static void init_aamix_paths(struct hda_codec *codec)
5654{
5655 struct hda_gen_spec *spec = codec->spec;
5656
5657 if (!spec->have_aamix_ctl)
5658 return;
5659 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5660 spec->aamix_out_paths[0],
5661 spec->autocfg.line_out_type);
5662 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5663 spec->aamix_out_paths[1],
5664 AUTO_PIN_HP_OUT);
5665 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5666 spec->aamix_out_paths[2],
5667 AUTO_PIN_SPEAKER_OUT);
5668}
5669
Takashi Iwai352f7f92012-12-19 12:52:06 +01005670/* set up input pins and loopback paths */
5671static void init_analog_input(struct hda_codec *codec)
5672{
5673 struct hda_gen_spec *spec = codec->spec;
5674 struct auto_pin_cfg *cfg = &spec->autocfg;
5675 int i;
5676
5677 for (i = 0; i < cfg->num_inputs; i++) {
5678 hda_nid_t nid = cfg->inputs[i].pin;
5679 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005680 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005681
5682 /* init loopback inputs */
5683 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005684 resume_path_from_idx(codec, spec->loopback_paths[i]);
5685 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005686 }
5687 }
5688}
5689
5690/* initialize ADC paths */
5691static void init_input_src(struct hda_codec *codec)
5692{
5693 struct hda_gen_spec *spec = codec->spec;
5694 struct hda_input_mux *imux = &spec->input_mux;
5695 struct nid_path *path;
5696 int i, c, nums;
5697
5698 if (spec->dyn_adc_switch)
5699 nums = 1;
5700 else
5701 nums = spec->num_adc_nids;
5702
5703 for (c = 0; c < nums; c++) {
5704 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005705 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005706 if (path) {
5707 bool active = path->active;
5708 if (i == spec->cur_mux[c])
5709 active = true;
5710 snd_hda_activate_path(codec, path, active, false);
5711 }
5712 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005713 if (spec->hp_mic)
5714 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005715 }
5716
Takashi Iwai352f7f92012-12-19 12:52:06 +01005717 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005718 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005719}
5720
5721/* set right pin controls for digital I/O */
5722static void init_digital(struct hda_codec *codec)
5723{
5724 struct hda_gen_spec *spec = codec->spec;
5725 int i;
5726 hda_nid_t pin;
5727
Takashi Iwaid4156932013-01-07 10:08:02 +01005728 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005729 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005730 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005731 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005732 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005733 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005734 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005735}
5736
Takashi Iwai973e4972012-12-20 15:16:09 +01005737/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5738 * invalid unsol tags by some reason
5739 */
5740static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5741{
5742 int i;
5743
5744 for (i = 0; i < codec->init_pins.used; i++) {
5745 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5746 hda_nid_t nid = pin->nid;
5747 if (is_jack_detectable(codec, nid) &&
5748 !snd_hda_jack_tbl_get(codec, nid))
5749 snd_hda_codec_update_cache(codec, nid, 0,
5750 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5751 }
5752}
5753
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005754/**
5755 * snd_hda_gen_init - initialize the generic spec
5756 * @codec: the HDA codec
5757 *
5758 * This can be put as patch_ops init function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005759 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005760int snd_hda_gen_init(struct hda_codec *codec)
5761{
5762 struct hda_gen_spec *spec = codec->spec;
5763
5764 if (spec->init_hook)
5765 spec->init_hook(codec);
5766
5767 snd_hda_apply_verbs(codec);
5768
5769 init_multi_out(codec);
5770 init_extra_out(codec);
5771 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005772 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005773 init_analog_input(codec);
5774 init_input_src(codec);
5775 init_digital(codec);
5776
Takashi Iwai973e4972012-12-20 15:16:09 +01005777 clear_unsol_on_unused_pins(codec);
5778
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01005779 sync_all_pin_power_ctls(codec);
5780
Takashi Iwai352f7f92012-12-19 12:52:06 +01005781 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005782 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005783
Takashi Iwaia551d912015-02-26 12:34:49 +01005784 regcache_sync(codec->core.regmap);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005785
Takashi Iwai352f7f92012-12-19 12:52:06 +01005786 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5787 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5788
5789 hda_call_check_power_status(codec, 0x01);
5790 return 0;
5791}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005792EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005793
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005794/**
5795 * snd_hda_gen_free - free the generic spec
5796 * @codec: the HDA codec
5797 *
5798 * This can be put as patch_ops free function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005799 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005800void snd_hda_gen_free(struct hda_codec *codec)
5801{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01005802 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005803 snd_hda_gen_spec_free(codec->spec);
5804 kfree(codec->spec);
5805 codec->spec = NULL;
5806}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005807EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005808
5809#ifdef CONFIG_PM
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005810/**
5811 * snd_hda_gen_check_power_status - check the loopback power save state
5812 * @codec: the HDA codec
5813 * @nid: NID to inspect
5814 *
5815 * This can be put as patch_ops check_power_status function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005816 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005817int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5818{
5819 struct hda_gen_spec *spec = codec->spec;
5820 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5821}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005822EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005823#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005824
5825
5826/*
5827 * the generic codec support
5828 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829
Takashi Iwai352f7f92012-12-19 12:52:06 +01005830static const struct hda_codec_ops generic_patch_ops = {
5831 .build_controls = snd_hda_gen_build_controls,
5832 .build_pcms = snd_hda_gen_build_pcms,
5833 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005834 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005835 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005836#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005837 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005838#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839};
5840
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005841/*
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005842 * snd_hda_parse_generic_codec - Generic codec parser
5843 * @codec: the HDA codec
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005844 */
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005845static int snd_hda_parse_generic_codec(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005847 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 int err;
5849
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005850 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005851 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005853 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005856 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5857 if (err < 0)
5858 return err;
5859
5860 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005861 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 goto error;
5863
5864 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 return 0;
5866
Takashi Iwai352f7f92012-12-19 12:52:06 +01005867error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005868 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 return err;
5870}
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005871
5872static const struct hda_codec_preset snd_hda_preset_generic[] = {
5873 { .id = HDA_CODEC_ID_GENERIC, .patch = snd_hda_parse_generic_codec },
5874 {} /* terminator */
5875};
5876
5877static struct hda_codec_driver generic_driver = {
5878 .preset = snd_hda_preset_generic,
5879};
5880
5881module_hda_codec_driver(generic_driver);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01005882
5883MODULE_LICENSE("GPL");
5884MODULE_DESCRIPTION("Generic HD-audio codec parser");