blob: 8a5055d296f5292a9f6ce37165d167e8f046b7e6 [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 Iwaie6feb5d2015-03-16 21:32:11 +0100143 val = snd_hda_get_bool_hint(codec, "power_mgmt");
144 if (val >= 0)
145 codec->power_mgmt = !!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 Iwai352f7f92012-12-19 12:52:06 +0100657 for (n = 0; n < spec->paths.used; n++) {
658 struct nid_path *path = snd_array_elem(&spec->paths, n);
659 if (!path->active)
660 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100661 if (codec->power_mgmt) {
662 if (!path->stream_enabled)
663 continue;
664 /* ignore unplugged paths except for DAC/ADC */
665 if (!path->pin_enabled &&
666 type != AC_WID_AUD_OUT && type != AC_WID_AUD_IN)
667 continue;
668 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100669 for (i = 0; i < path->depth; i++) {
670 if (path->path[i] == nid) {
671 if (dir == HDA_OUTPUT || path->idx[i] == idx)
672 return true;
673 break;
674 }
675 }
676 }
677 return false;
678}
679
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200680/* check whether the NID is referred by any active paths */
681#define is_active_nid_for_any(codec, nid) \
682 is_active_nid(codec, nid, HDA_OUTPUT, 0)
683
Takashi Iwai352f7f92012-12-19 12:52:06 +0100684/* get the default amp value for the target state */
685static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100686 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100687{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100688 unsigned int val = 0;
689
Takashi Iwai352f7f92012-12-19 12:52:06 +0100690 if (caps & AC_AMPCAP_NUM_STEPS) {
691 /* set to 0dB */
692 if (enable)
693 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
694 }
Takashi Iwaif69910d2013-08-08 09:32:37 +0200695 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100696 if (!enable)
697 val |= HDA_AMP_MUTE;
698 }
699 return val;
700}
701
Takashi Iwaicc261732015-03-16 10:18:08 +0100702/* is this a stereo widget or a stereo-to-mono mix? */
703static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
704{
705 unsigned int wcaps = get_wcaps(codec, nid);
706 hda_nid_t conn;
707
708 if (wcaps & AC_WCAP_STEREO)
709 return true;
710 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
711 return false;
712 if (snd_hda_get_num_conns(codec, nid) != 1)
713 return false;
714 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
715 return false;
716 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
717}
718
Takashi Iwai352f7f92012-12-19 12:52:06 +0100719/* initialize the amp value (only at the first time) */
720static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
721{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100722 unsigned int caps = query_amp_caps(codec, nid, dir);
723 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwaief403ed2015-03-12 08:30:11 +0100724
Takashi Iwaicc261732015-03-16 10:18:08 +0100725 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100726 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
727 else
728 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
729}
730
731/* update the amp, doing in stereo or mono depending on NID */
732static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
733 unsigned int mask, unsigned int val)
734{
Takashi Iwaicc261732015-03-16 10:18:08 +0100735 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100736 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
737 mask, val);
738 else
739 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
740 mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100741}
742
Takashi Iwai8999bf02013-01-18 11:01:33 +0100743/* calculate amp value mask we can modify;
744 * if the given amp is controlled by mixers, don't touch it
745 */
746static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
747 hda_nid_t nid, int dir, int idx,
748 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100749{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100750 unsigned int mask = 0xff;
751
Takashi Iwaif69910d2013-08-08 09:32:37 +0200752 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100753 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
754 mask &= ~0x80;
755 }
756 if (caps & AC_AMPCAP_NUM_STEPS) {
757 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
758 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
759 mask &= ~0x7f;
760 }
761 return mask;
762}
763
764static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
765 int idx, int idx_to_check, bool enable)
766{
767 unsigned int caps;
768 unsigned int mask, val;
769
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100770 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100771 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100772
773 caps = query_amp_caps(codec, nid, dir);
774 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
775 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
776 if (!mask)
777 return;
778
779 val &= mask;
Takashi Iwaief403ed2015-03-12 08:30:11 +0100780 update_amp(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100781}
782
783static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
784 int i, bool enable)
785{
786 hda_nid_t nid = path->path[i];
787 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100788 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100789}
790
791static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
792 int i, bool enable, bool add_aamix)
793{
794 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100795 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100796 int n, nums, idx;
797 int type;
798 hda_nid_t nid = path->path[i];
799
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100800 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100801 type = get_wcaps_type(get_wcaps(codec, nid));
802 if (type == AC_WID_PIN ||
803 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
804 nums = 1;
805 idx = 0;
806 } else
807 idx = path->idx[i];
808
809 for (n = 0; n < nums; n++)
810 init_amp(codec, nid, HDA_INPUT, n);
811
Takashi Iwai352f7f92012-12-19 12:52:06 +0100812 /* here is a little bit tricky in comparison with activate_amp_out();
813 * when aa-mixer is available, we need to enable the path as well
814 */
815 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100816 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100817 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100818 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820}
821
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100822/* sync power of each widget in the the given path */
823static hda_nid_t path_power_update(struct hda_codec *codec,
824 struct nid_path *path,
825 bool allow_powerdown)
826{
827 hda_nid_t nid, changed = 0;
828 int i, state;
829
830 for (i = 0; i < path->depth; i++) {
831 nid = path->path[i];
832 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
833 state = AC_PWRST_D0;
834 else
835 state = AC_PWRST_D3;
836 if (!snd_hda_check_power_state(codec, nid, state)) {
837 snd_hda_codec_write(codec, nid, 0,
838 AC_VERB_SET_POWER_STATE, state);
839 changed = nid;
840 /* here we assume that widget attributes (e.g. amp,
841 * pinctl connection) don't change with local power
842 * state change. If not, need to sync the cache.
843 */
844 }
845 }
846 return changed;
847}
848
849/* do sync with the last power state change */
850static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
851{
852 if (nid) {
853 msleep(10);
854 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
855 }
856}
857
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100858/**
859 * snd_hda_activate_path - activate or deactivate the given path
860 * @codec: the HDA codec
861 * @path: the path to activate/deactivate
862 * @enable: flag to activate or not
863 * @add_aamix: enable the input from aamix NID
864 *
865 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100867void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
868 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100870 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100871 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Takashi Iwai352f7f92012-12-19 12:52:06 +0100873 if (!enable)
874 path->active = false;
875
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100876 /* make sure the widget is powered up */
877 if (enable && (spec->power_down_unused || codec->power_mgmt))
878 path_power_update(codec, path, codec->power_mgmt);
879
Takashi Iwai352f7f92012-12-19 12:52:06 +0100880 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100881 hda_nid_t nid = path->path[i];
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100882
Takashi Iwai352f7f92012-12-19 12:52:06 +0100883 if (enable && path->multi[i])
Takashi Iwai8f0972d2014-01-27 16:26:16 +0100884 snd_hda_codec_update_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100885 AC_VERB_SET_CONNECT_SEL,
886 path->idx[i]);
887 if (has_amp_in(codec, path, i))
888 activate_amp_in(codec, path, i, enable, add_aamix);
889 if (has_amp_out(codec, path, i))
890 activate_amp_out(codec, path, i, enable);
891 }
892
893 if (enable)
894 path->active = true;
895}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100896EXPORT_SYMBOL_GPL(snd_hda_activate_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100897
Takashi Iwai55196ff2013-01-24 17:32:56 +0100898/* if the given path is inactive, put widgets into D3 (only if suitable) */
899static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
900{
901 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100902
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100903 if (!(spec->power_down_unused || codec->power_mgmt) || path->active)
Takashi Iwai55196ff2013-01-24 17:32:56 +0100904 return;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100905 sync_power_state_change(codec, path_power_update(codec, path, true));
Takashi Iwai55196ff2013-01-24 17:32:56 +0100906}
907
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100908/* turn on/off EAPD on the given pin */
909static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
910{
911 struct hda_gen_spec *spec = codec->spec;
912 if (spec->own_eapd_ctl ||
913 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
914 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200915 if (spec->keep_eapd_on && !enable)
916 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100917 if (codec->inv_eapd)
918 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100919 snd_hda_codec_update_cache(codec, pin, 0,
920 AC_VERB_SET_EAPD_BTLENABLE,
921 enable ? 0x02 : 0x00);
922}
923
Takashi Iwai3e367f12013-01-23 17:07:23 +0100924/* re-initialize the path specified by the given path index */
925static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
926{
927 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
928 if (path)
929 snd_hda_activate_path(codec, path, path->active, false);
930}
931
Takashi Iwai352f7f92012-12-19 12:52:06 +0100932
933/*
934 * Helper functions for creating mixer ctl elements
935 */
936
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200937static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
938 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200939static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
940 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200941
Takashi Iwai352f7f92012-12-19 12:52:06 +0100942enum {
943 HDA_CTL_WIDGET_VOL,
944 HDA_CTL_WIDGET_MUTE,
945 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100946};
947static const struct snd_kcontrol_new control_templates[] = {
948 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200949 /* only the put callback is replaced for handling the special mute */
950 {
951 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
952 .subdevice = HDA_SUBDEV_AMP_FLAG,
953 .info = snd_hda_mixer_amp_switch_info,
954 .get = snd_hda_mixer_amp_switch_get,
955 .put = hda_gen_mixer_mute_put, /* replaced */
956 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
957 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200958 {
959 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
960 .info = snd_hda_mixer_amp_switch_info,
961 .get = snd_hda_mixer_bind_switch_get,
962 .put = hda_gen_bind_mute_put, /* replaced */
963 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
964 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100965};
966
967/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100968static struct snd_kcontrol_new *
969add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100970 int cidx, unsigned long val)
971{
972 struct snd_kcontrol_new *knew;
973
Takashi Iwai12c93df2012-12-19 14:38:33 +0100974 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100975 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100976 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100977 knew->index = cidx;
978 if (get_amp_nid_(val))
979 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
980 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100981 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100982}
983
984static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
985 const char *pfx, const char *dir,
986 const char *sfx, int cidx, unsigned long val)
987{
Takashi Iwai975cc022013-06-28 11:56:49 +0200988 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100989 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100990 if (!add_control(spec, type, name, cidx, val))
991 return -ENOMEM;
992 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100993}
994
995#define add_pb_vol_ctrl(spec, type, pfx, val) \
996 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
997#define add_pb_sw_ctrl(spec, type, pfx, val) \
998 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
999#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1000 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1001#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1002 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1003
1004static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1005 unsigned int chs, struct nid_path *path)
1006{
1007 unsigned int val;
1008 if (!path)
1009 return 0;
1010 val = path->ctls[NID_PATH_VOL_CTL];
1011 if (!val)
1012 return 0;
1013 val = amp_val_replace_channels(val, chs);
1014 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1015}
1016
1017/* return the channel bits suitable for the given path->ctls[] */
1018static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1019 int type)
1020{
1021 int chs = 1; /* mono (left only) */
1022 if (path) {
1023 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1024 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1025 chs = 3; /* stereo */
1026 }
1027 return chs;
1028}
1029
1030static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1031 struct nid_path *path)
1032{
1033 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1034 return add_vol_ctl(codec, pfx, cidx, chs, path);
1035}
1036
1037/* create a mute-switch for the given mixer widget;
1038 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1039 */
1040static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1041 unsigned int chs, struct nid_path *path)
1042{
1043 unsigned int val;
1044 int type = HDA_CTL_WIDGET_MUTE;
1045
1046 if (!path)
1047 return 0;
1048 val = path->ctls[NID_PATH_MUTE_CTL];
1049 if (!val)
1050 return 0;
1051 val = amp_val_replace_channels(val, chs);
1052 if (get_amp_direction_(val) == HDA_INPUT) {
1053 hda_nid_t nid = get_amp_nid_(val);
1054 int nums = snd_hda_get_num_conns(codec, nid);
1055 if (nums > 1) {
1056 type = HDA_CTL_BIND_MUTE;
1057 val |= nums << 19;
1058 }
1059 }
1060 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1061}
1062
1063static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1064 int cidx, struct nid_path *path)
1065{
1066 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1067 return add_sw_ctl(codec, pfx, cidx, chs, path);
1068}
1069
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001070/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001071static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1072 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001073{
1074 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1075 struct hda_gen_spec *spec = codec->spec;
1076
1077 if (spec->auto_mute_via_amp) {
1078 hda_nid_t nid = get_amp_nid(kcontrol);
1079 bool enabled = !((spec->mute_bits >> nid) & 1);
1080 ucontrol->value.integer.value[0] &= enabled;
1081 ucontrol->value.integer.value[1] &= enabled;
1082 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001083}
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001084
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001085static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1086 struct snd_ctl_elem_value *ucontrol)
1087{
1088 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001089 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1090}
1091
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001092static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1093 struct snd_ctl_elem_value *ucontrol)
1094{
1095 sync_auto_mute_bits(kcontrol, ucontrol);
1096 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1097}
1098
Takashi Iwai247d85e2013-01-17 16:18:11 +01001099/* any ctl assigned to the path with the given index? */
1100static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1101{
1102 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1103 return path && path->ctls[ctl_type];
1104}
1105
Takashi Iwai352f7f92012-12-19 12:52:06 +01001106static const char * const channel_name[4] = {
1107 "Front", "Surround", "CLFE", "Side"
1108};
1109
1110/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +01001111static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1112 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001113{
Takashi Iwai247d85e2013-01-17 16:18:11 +01001114 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001115 struct auto_pin_cfg *cfg = &spec->autocfg;
1116
1117 *index = 0;
1118 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001119 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001120 return spec->vmaster_mute.hook ? "PCM" : "Master";
1121
1122 /* if there is really a single DAC used in the whole output paths,
1123 * use it master (or "PCM" if a vmaster hook is present)
1124 */
1125 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1126 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1127 return spec->vmaster_mute.hook ? "PCM" : "Master";
1128
Takashi Iwai247d85e2013-01-17 16:18:11 +01001129 /* multi-io channels */
1130 if (ch >= cfg->line_outs)
1131 return channel_name[ch];
1132
Takashi Iwai352f7f92012-12-19 12:52:06 +01001133 switch (cfg->line_out_type) {
1134 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001135 /* if the primary channel vol/mute is shared with HP volume,
1136 * don't name it as Speaker
1137 */
1138 if (!ch && cfg->hp_outs &&
1139 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1140 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001141 if (cfg->line_outs == 1)
1142 return "Speaker";
1143 if (cfg->line_outs == 2)
1144 return ch ? "Bass Speaker" : "Speaker";
1145 break;
1146 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001147 /* if the primary channel vol/mute is shared with spk volume,
1148 * don't name it as Headphone
1149 */
1150 if (!ch && cfg->speaker_outs &&
1151 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1152 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001153 /* for multi-io case, only the primary out */
1154 if (ch && spec->multi_ios)
1155 break;
1156 *index = ch;
1157 return "Headphone";
David Henningsson03ad6a82014-10-16 15:33:45 +02001158 case AUTO_PIN_LINE_OUT:
1159 /* This deals with the case where we have two DACs and
1160 * one LO, one HP and one Speaker */
1161 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1162 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1163 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1164 if (hp_lo_shared && spk_lo_shared)
1165 return spec->vmaster_mute.hook ? "PCM" : "Master";
1166 if (hp_lo_shared)
1167 return "Headphone+LO";
1168 if (spk_lo_shared)
1169 return "Speaker+LO";
1170 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001171 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001172
1173 /* for a single channel output, we don't have to name the channel */
1174 if (cfg->line_outs == 1 && !spec->multi_ios)
David Henningsson3abb4f42014-10-16 15:33:46 +02001175 return "Line Out";
Takashi Iwai247d85e2013-01-17 16:18:11 +01001176
Takashi Iwai352f7f92012-12-19 12:52:06 +01001177 if (ch >= ARRAY_SIZE(channel_name)) {
1178 snd_BUG();
1179 return "PCM";
1180 }
1181
1182 return channel_name[ch];
1183}
1184
1185/*
1186 * Parse output paths
1187 */
1188
1189/* badness definition */
1190enum {
1191 /* No primary DAC is found for the main output */
1192 BAD_NO_PRIMARY_DAC = 0x10000,
1193 /* No DAC is found for the extra output */
1194 BAD_NO_DAC = 0x4000,
1195 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001196 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001197 /* No individual DAC for extra output */
1198 BAD_NO_EXTRA_DAC = 0x102,
1199 /* No individual DAC for extra surrounds */
1200 BAD_NO_EXTRA_SURR_DAC = 0x101,
1201 /* Primary DAC shared with main surrounds */
1202 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001203 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001204 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001205 /* Primary DAC shared with main CLFE */
1206 BAD_SHARED_CLFE = 0x10,
1207 /* Primary DAC shared with extra surrounds */
1208 BAD_SHARED_EXTRA_SURROUND = 0x10,
1209 /* Volume widget is shared */
1210 BAD_SHARED_VOL = 0x10,
1211};
1212
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001213/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001214 * volume and mute controls, and assign the values to ctls[].
1215 *
1216 * When no appropriate widget is found in the path, the badness value
1217 * is incremented depending on the situation. The function returns the
1218 * total badness for both volume and mute controls.
1219 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001220static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001221{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001222 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001223 hda_nid_t nid;
1224 unsigned int val;
1225 int badness = 0;
1226
1227 if (!path)
1228 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001229
1230 if (path->ctls[NID_PATH_VOL_CTL] ||
1231 path->ctls[NID_PATH_MUTE_CTL])
1232 return 0; /* already evaluated */
1233
Takashi Iwai352f7f92012-12-19 12:52:06 +01001234 nid = look_for_out_vol_nid(codec, path);
1235 if (nid) {
1236 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001237 if (spec->dac_min_mute)
1238 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001239 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1240 badness += BAD_SHARED_VOL;
1241 else
1242 path->ctls[NID_PATH_VOL_CTL] = val;
1243 } else
1244 badness += BAD_SHARED_VOL;
1245 nid = look_for_out_mute_nid(codec, path);
1246 if (nid) {
1247 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1248 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1249 nid_has_mute(codec, nid, HDA_OUTPUT))
1250 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1251 else
1252 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1253 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1254 badness += BAD_SHARED_VOL;
1255 else
1256 path->ctls[NID_PATH_MUTE_CTL] = val;
1257 } else
1258 badness += BAD_SHARED_VOL;
1259 return badness;
1260}
1261
Takashi Iwai98bd1112013-03-22 14:53:50 +01001262const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001263 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1264 .no_dac = BAD_NO_DAC,
1265 .shared_primary = BAD_NO_PRIMARY_DAC,
1266 .shared_surr = BAD_SHARED_SURROUND,
1267 .shared_clfe = BAD_SHARED_CLFE,
1268 .shared_surr_main = BAD_SHARED_SURROUND,
1269};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001270EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001271
Takashi Iwai98bd1112013-03-22 14:53:50 +01001272const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001273 .no_primary_dac = BAD_NO_DAC,
1274 .no_dac = BAD_NO_DAC,
1275 .shared_primary = BAD_NO_EXTRA_DAC,
1276 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1277 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1278 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1279};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001280EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001281
Takashi Iwai7385df62013-01-07 09:50:52 +01001282/* get the DAC of the primary output corresponding to the given array index */
1283static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1284{
1285 struct hda_gen_spec *spec = codec->spec;
1286 struct auto_pin_cfg *cfg = &spec->autocfg;
1287
1288 if (cfg->line_outs > idx)
1289 return spec->private_dac_nids[idx];
1290 idx -= cfg->line_outs;
1291 if (spec->multi_ios > idx)
1292 return spec->multi_io[idx].dac;
1293 return 0;
1294}
1295
1296/* return the DAC if it's reachable, otherwise zero */
1297static inline hda_nid_t try_dac(struct hda_codec *codec,
1298 hda_nid_t dac, hda_nid_t pin)
1299{
1300 return is_reachable_path(codec, dac, pin) ? dac : 0;
1301}
1302
Takashi Iwai352f7f92012-12-19 12:52:06 +01001303/* try to assign DACs to pins and return the resultant badness */
1304static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1305 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001306 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001307 const struct badness_table *bad)
1308{
1309 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001310 int i, j;
1311 int badness = 0;
1312 hda_nid_t dac;
1313
1314 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return 0;
1316
Takashi Iwai352f7f92012-12-19 12:52:06 +01001317 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001318 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001319 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001320
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001321 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1322 if (path) {
1323 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001324 continue;
1325 }
1326
Takashi Iwai36907392013-12-10 17:29:26 +01001327 dacs[i] = get_preferred_dac(codec, pin);
1328 if (dacs[i]) {
1329 if (is_dac_already_used(codec, dacs[i]))
1330 badness += bad->shared_primary;
1331 }
1332
1333 if (!dacs[i])
1334 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001335 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001336 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001337 for (j = 1; j < num_outs; j++) {
1338 if (is_reachable_path(codec, dacs[j], pin)) {
1339 dacs[0] = dacs[j];
1340 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001341 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001342 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001343 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001346 }
1347 dac = dacs[i];
1348 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001349 if (num_outs > 2)
1350 dac = try_dac(codec, get_primary_out(codec, i), pin);
1351 if (!dac)
1352 dac = try_dac(codec, dacs[0], pin);
1353 if (!dac)
1354 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001355 if (dac) {
1356 if (!i)
1357 badness += bad->shared_primary;
1358 else if (i == 1)
1359 badness += bad->shared_surr;
1360 else
1361 badness += bad->shared_clfe;
1362 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1363 dac = spec->private_dac_nids[0];
1364 badness += bad->shared_surr_main;
1365 } else if (!i)
1366 badness += bad->no_primary_dac;
1367 else
1368 badness += bad->no_dac;
1369 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001370 if (!dac)
1371 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001372 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001373 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001374 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001375 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001376 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001377 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001378 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001379 badness += bad->no_dac;
1380 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001381 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001382 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001383 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001384 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001385 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001386 }
1387
1388 return badness;
1389}
1390
1391/* return NID if the given pin has only a single connection to a certain DAC */
1392static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1393{
1394 struct hda_gen_spec *spec = codec->spec;
1395 int i;
1396 hda_nid_t nid_found = 0;
1397
1398 for (i = 0; i < spec->num_all_dacs; i++) {
1399 hda_nid_t nid = spec->all_dacs[i];
1400 if (!nid || is_dac_already_used(codec, nid))
1401 continue;
1402 if (is_reachable_path(codec, nid, pin)) {
1403 if (nid_found)
1404 return 0;
1405 nid_found = nid;
1406 }
1407 }
1408 return nid_found;
1409}
1410
1411/* check whether the given pin can be a multi-io pin */
1412static bool can_be_multiio_pin(struct hda_codec *codec,
1413 unsigned int location, hda_nid_t nid)
1414{
1415 unsigned int defcfg, caps;
1416
1417 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1418 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1419 return false;
1420 if (location && get_defcfg_location(defcfg) != location)
1421 return false;
1422 caps = snd_hda_query_pin_caps(codec, nid);
1423 if (!(caps & AC_PINCAP_OUT))
1424 return false;
1425 return true;
1426}
1427
Takashi Iwaie22aab72013-01-04 14:50:04 +01001428/* count the number of input pins that are capable to be multi-io */
1429static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1430{
1431 struct hda_gen_spec *spec = codec->spec;
1432 struct auto_pin_cfg *cfg = &spec->autocfg;
1433 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1434 unsigned int location = get_defcfg_location(defcfg);
1435 int type, i;
1436 int num_pins = 0;
1437
1438 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1439 for (i = 0; i < cfg->num_inputs; i++) {
1440 if (cfg->inputs[i].type != type)
1441 continue;
1442 if (can_be_multiio_pin(codec, location,
1443 cfg->inputs[i].pin))
1444 num_pins++;
1445 }
1446 }
1447 return num_pins;
1448}
1449
Takashi Iwai352f7f92012-12-19 12:52:06 +01001450/*
1451 * multi-io helper
1452 *
1453 * When hardwired is set, try to fill ony hardwired pins, and returns
1454 * zero if any pins are filled, non-zero if nothing found.
1455 * When hardwired is off, try to fill possible input pins, and returns
1456 * the badness value.
1457 */
1458static int fill_multi_ios(struct hda_codec *codec,
1459 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001460 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001461{
1462 struct hda_gen_spec *spec = codec->spec;
1463 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001464 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001465 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1466 unsigned int location = get_defcfg_location(defcfg);
1467 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001468 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001469
1470 old_pins = spec->multi_ios;
1471 if (old_pins >= 2)
1472 goto end_fill;
1473
Takashi Iwaie22aab72013-01-04 14:50:04 +01001474 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001475 if (num_pins < 2)
1476 goto end_fill;
1477
Takashi Iwai352f7f92012-12-19 12:52:06 +01001478 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1479 for (i = 0; i < cfg->num_inputs; i++) {
1480 hda_nid_t nid = cfg->inputs[i].pin;
1481 hda_nid_t dac = 0;
1482
1483 if (cfg->inputs[i].type != type)
1484 continue;
1485 if (!can_be_multiio_pin(codec, location, nid))
1486 continue;
1487 for (j = 0; j < spec->multi_ios; j++) {
1488 if (nid == spec->multi_io[j].pin)
1489 break;
1490 }
1491 if (j < spec->multi_ios)
1492 continue;
1493
Takashi Iwai352f7f92012-12-19 12:52:06 +01001494 if (hardwired)
1495 dac = get_dac_if_single(codec, nid);
1496 else if (!dac)
1497 dac = look_for_dac(codec, nid, false);
1498 if (!dac) {
1499 badness++;
1500 continue;
1501 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001502 path = snd_hda_add_new_path(codec, dac, nid,
1503 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001504 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001505 badness++;
1506 continue;
1507 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001508 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001509 spec->multi_io[spec->multi_ios].pin = nid;
1510 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001511 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1512 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001513 spec->multi_ios++;
1514 if (spec->multi_ios >= 2)
1515 break;
1516 }
1517 }
1518 end_fill:
1519 if (badness)
1520 badness = BAD_MULTI_IO;
1521 if (old_pins == spec->multi_ios) {
1522 if (hardwired)
1523 return 1; /* nothing found */
1524 else
1525 return badness; /* no badness if nothing found */
1526 }
1527 if (!hardwired && spec->multi_ios < 2) {
1528 /* cancel newly assigned paths */
1529 spec->paths.used -= spec->multi_ios - old_pins;
1530 spec->multi_ios = old_pins;
1531 return badness;
1532 }
1533
1534 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001535 for (i = old_pins; i < spec->multi_ios; i++) {
1536 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1537 badness += assign_out_path_ctls(codec, path);
1538 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001539
1540 return badness;
1541}
1542
1543/* map DACs for all pins in the list if they are single connections */
1544static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001545 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001546{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001547 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001548 int i;
1549 bool found = false;
1550 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001551 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001552 hda_nid_t dac;
1553 if (dacs[i])
1554 continue;
1555 dac = get_dac_if_single(codec, pins[i]);
1556 if (!dac)
1557 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001558 path = snd_hda_add_new_path(codec, dac, pins[i],
1559 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001560 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001561 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001562 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001563 dacs[i] = dac;
1564 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001565 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001566 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001567 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001568 }
1569 }
1570 return found;
1571}
1572
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001573/* create a new path including aamix if available, and return its index */
1574static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1575{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001576 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001577 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001578 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001579
1580 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001581 if (!path || !path->depth ||
1582 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001583 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001584 path_dac = path->path[0];
1585 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001586 pin = path->path[path->depth - 1];
1587 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1588 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001589 if (dac != path_dac)
1590 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001591 else if (spec->multiout.hp_out_nid[0])
1592 dac = spec->multiout.hp_out_nid[0];
1593 else if (spec->multiout.extra_out_nid[0])
1594 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001595 else
1596 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001597 if (dac)
1598 path = snd_hda_add_new_path(codec, dac, pin,
1599 spec->mixer_nid);
1600 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001601 if (!path)
1602 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001603 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001604 path->active = false; /* unused as default */
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001605 path->pin_enabled = true; /* static route */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001606 return snd_hda_get_path_idx(codec, path);
1607}
1608
Takashi Iwai55a63d42013-03-21 17:20:12 +01001609/* check whether the independent HP is available with the current config */
1610static bool indep_hp_possible(struct hda_codec *codec)
1611{
1612 struct hda_gen_spec *spec = codec->spec;
1613 struct auto_pin_cfg *cfg = &spec->autocfg;
1614 struct nid_path *path;
1615 int i, idx;
1616
1617 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1618 idx = spec->out_paths[0];
1619 else
1620 idx = spec->hp_paths[0];
1621 path = snd_hda_get_path_from_idx(codec, idx);
1622 if (!path)
1623 return false;
1624
1625 /* assume no path conflicts unless aamix is involved */
1626 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1627 return true;
1628
1629 /* check whether output paths contain aamix */
1630 for (i = 0; i < cfg->line_outs; i++) {
1631 if (spec->out_paths[i] == idx)
1632 break;
1633 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1634 if (path && is_nid_contained(path, spec->mixer_nid))
1635 return false;
1636 }
1637 for (i = 0; i < cfg->speaker_outs; i++) {
1638 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1639 if (path && is_nid_contained(path, spec->mixer_nid))
1640 return false;
1641 }
1642
1643 return true;
1644}
1645
Takashi Iwaia07a9492013-01-07 16:44:06 +01001646/* fill the empty entries in the dac array for speaker/hp with the
1647 * shared dac pointed by the paths
1648 */
1649static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1650 hda_nid_t *dacs, int *path_idx)
1651{
1652 struct nid_path *path;
1653 int i;
1654
1655 for (i = 0; i < num_outs; i++) {
1656 if (dacs[i])
1657 continue;
1658 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1659 if (!path)
1660 continue;
1661 dacs[i] = path->path[0];
1662 }
1663}
1664
Takashi Iwai352f7f92012-12-19 12:52:06 +01001665/* fill in the dac_nids table from the parsed pin configuration */
1666static int fill_and_eval_dacs(struct hda_codec *codec,
1667 bool fill_hardwired,
1668 bool fill_mio_first)
1669{
1670 struct hda_gen_spec *spec = codec->spec;
1671 struct auto_pin_cfg *cfg = &spec->autocfg;
1672 int i, err, badness;
1673
1674 /* set num_dacs once to full for look_for_dac() */
1675 spec->multiout.num_dacs = cfg->line_outs;
1676 spec->multiout.dac_nids = spec->private_dac_nids;
1677 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1678 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1679 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1680 spec->multi_ios = 0;
1681 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001682
1683 /* clear path indices */
1684 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1685 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1686 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1687 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1688 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001689 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001690 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1691 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1692
Takashi Iwai352f7f92012-12-19 12:52:06 +01001693 badness = 0;
1694
1695 /* fill hard-wired DACs first */
1696 if (fill_hardwired) {
1697 bool mapped;
1698 do {
1699 mapped = map_singles(codec, cfg->line_outs,
1700 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001701 spec->private_dac_nids,
1702 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001703 mapped |= map_singles(codec, cfg->hp_outs,
1704 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001705 spec->multiout.hp_out_nid,
1706 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001707 mapped |= map_singles(codec, cfg->speaker_outs,
1708 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001709 spec->multiout.extra_out_nid,
1710 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001711 if (!spec->no_multi_io &&
1712 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001713 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001714 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001715 if (!err)
1716 mapped = true;
1717 }
1718 } while (mapped);
1719 }
1720
1721 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001722 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001723 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001724
Takashi Iwaida96fb52013-07-29 16:54:36 +02001725 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001726 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1727 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001728 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001729 if (err < 0)
1730 return err;
1731 /* we don't count badness at this stage yet */
1732 }
1733
1734 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1735 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1736 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001737 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001738 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001739 if (err < 0)
1740 return err;
1741 badness += err;
1742 }
1743 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1744 err = try_assign_dacs(codec, cfg->speaker_outs,
1745 cfg->speaker_pins,
1746 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001747 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001748 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001749 if (err < 0)
1750 return err;
1751 badness += err;
1752 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001753 if (!spec->no_multi_io &&
1754 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001755 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001756 if (err < 0)
1757 return err;
1758 badness += err;
1759 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001760
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001761 if (spec->mixer_nid) {
1762 spec->aamix_out_paths[0] =
1763 check_aamix_out_path(codec, spec->out_paths[0]);
1764 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1765 spec->aamix_out_paths[1] =
1766 check_aamix_out_path(codec, spec->hp_paths[0]);
1767 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1768 spec->aamix_out_paths[2] =
1769 check_aamix_out_path(codec, spec->speaker_paths[0]);
1770 }
1771
Takashi Iwaida96fb52013-07-29 16:54:36 +02001772 if (!spec->no_multi_io &&
1773 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001774 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1775 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001776
Takashi Iwaia07a9492013-01-07 16:44:06 +01001777 /* re-count num_dacs and squash invalid entries */
1778 spec->multiout.num_dacs = 0;
1779 for (i = 0; i < cfg->line_outs; i++) {
1780 if (spec->private_dac_nids[i])
1781 spec->multiout.num_dacs++;
1782 else {
1783 memmove(spec->private_dac_nids + i,
1784 spec->private_dac_nids + i + 1,
1785 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1786 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1787 }
1788 }
1789
1790 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001791 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001792
Takashi Iwai352f7f92012-12-19 12:52:06 +01001793 if (spec->multi_ios == 2) {
1794 for (i = 0; i < 2; i++)
1795 spec->private_dac_nids[spec->multiout.num_dacs++] =
1796 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001797 } else if (spec->multi_ios) {
1798 spec->multi_ios = 0;
1799 badness += BAD_MULTI_IO;
1800 }
1801
Takashi Iwai55a63d42013-03-21 17:20:12 +01001802 if (spec->indep_hp && !indep_hp_possible(codec))
1803 badness += BAD_NO_INDEP_HP;
1804
Takashi Iwaia07a9492013-01-07 16:44:06 +01001805 /* re-fill the shared DAC for speaker / headphone */
1806 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1807 refill_shared_dacs(codec, cfg->hp_outs,
1808 spec->multiout.hp_out_nid,
1809 spec->hp_paths);
1810 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1811 refill_shared_dacs(codec, cfg->speaker_outs,
1812 spec->multiout.extra_out_nid,
1813 spec->speaker_paths);
1814
Takashi Iwai352f7f92012-12-19 12:52:06 +01001815 return badness;
1816}
1817
1818#define DEBUG_BADNESS
1819
1820#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001821#define debug_badness(fmt, ...) \
1822 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001823#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001824#define debug_badness(fmt, ...) \
1825 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001826#endif
1827
Takashi Iwaia7694092013-01-21 10:43:18 +01001828#ifdef DEBUG_BADNESS
1829static inline void print_nid_path_idx(struct hda_codec *codec,
1830 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001831{
Takashi Iwaia7694092013-01-21 10:43:18 +01001832 struct nid_path *path;
1833
1834 path = snd_hda_get_path_from_idx(codec, idx);
1835 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001836 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001837}
1838
1839static void debug_show_configs(struct hda_codec *codec,
1840 struct auto_pin_cfg *cfg)
1841{
1842 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001843 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001844 int i;
1845
1846 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001847 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001848 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001849 spec->multiout.dac_nids[0],
1850 spec->multiout.dac_nids[1],
1851 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001852 spec->multiout.dac_nids[3],
1853 lo_type[cfg->line_out_type]);
1854 for (i = 0; i < cfg->line_outs; i++)
1855 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001856 if (spec->multi_ios > 0)
1857 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1858 spec->multi_ios,
1859 spec->multi_io[0].pin, spec->multi_io[1].pin,
1860 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001861 for (i = 0; i < spec->multi_ios; i++)
1862 print_nid_path_idx(codec, " mio",
1863 spec->out_paths[cfg->line_outs + i]);
1864 if (cfg->hp_outs)
1865 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001866 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001867 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001868 spec->multiout.hp_out_nid[0],
1869 spec->multiout.hp_out_nid[1],
1870 spec->multiout.hp_out_nid[2],
1871 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001872 for (i = 0; i < cfg->hp_outs; i++)
1873 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1874 if (cfg->speaker_outs)
1875 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001876 cfg->speaker_pins[0], cfg->speaker_pins[1],
1877 cfg->speaker_pins[2], cfg->speaker_pins[3],
1878 spec->multiout.extra_out_nid[0],
1879 spec->multiout.extra_out_nid[1],
1880 spec->multiout.extra_out_nid[2],
1881 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001882 for (i = 0; i < cfg->speaker_outs; i++)
1883 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1884 for (i = 0; i < 3; i++)
1885 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001886}
Takashi Iwaia7694092013-01-21 10:43:18 +01001887#else
1888#define debug_show_configs(codec, cfg) /* NOP */
1889#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001890
1891/* find all available DACs of the codec */
1892static void fill_all_dac_nids(struct hda_codec *codec)
1893{
1894 struct hda_gen_spec *spec = codec->spec;
1895 int i;
1896 hda_nid_t nid = codec->start_nid;
1897
1898 spec->num_all_dacs = 0;
1899 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1900 for (i = 0; i < codec->num_nodes; i++, nid++) {
1901 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1902 continue;
1903 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001904 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001905 break;
1906 }
1907 spec->all_dacs[spec->num_all_dacs++] = nid;
1908 }
1909}
1910
1911static int parse_output_paths(struct hda_codec *codec)
1912{
1913 struct hda_gen_spec *spec = codec->spec;
1914 struct auto_pin_cfg *cfg = &spec->autocfg;
1915 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001916 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001917 int best_badness = INT_MAX;
1918 int badness;
1919 bool fill_hardwired = true, fill_mio_first = true;
1920 bool best_wired = true, best_mio = true;
1921 bool hp_spk_swapped = false;
1922
Takashi Iwai352f7f92012-12-19 12:52:06 +01001923 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1924 if (!best_cfg)
1925 return -ENOMEM;
1926 *best_cfg = *cfg;
1927
1928 for (;;) {
1929 badness = fill_and_eval_dacs(codec, fill_hardwired,
1930 fill_mio_first);
1931 if (badness < 0) {
1932 kfree(best_cfg);
1933 return badness;
1934 }
1935 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1936 cfg->line_out_type, fill_hardwired, fill_mio_first,
1937 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001938 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001939 if (badness < best_badness) {
1940 best_badness = badness;
1941 *best_cfg = *cfg;
1942 best_wired = fill_hardwired;
1943 best_mio = fill_mio_first;
1944 }
1945 if (!badness)
1946 break;
1947 fill_mio_first = !fill_mio_first;
1948 if (!fill_mio_first)
1949 continue;
1950 fill_hardwired = !fill_hardwired;
1951 if (!fill_hardwired)
1952 continue;
1953 if (hp_spk_swapped)
1954 break;
1955 hp_spk_swapped = true;
1956 if (cfg->speaker_outs > 0 &&
1957 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1958 cfg->hp_outs = cfg->line_outs;
1959 memcpy(cfg->hp_pins, cfg->line_out_pins,
1960 sizeof(cfg->hp_pins));
1961 cfg->line_outs = cfg->speaker_outs;
1962 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1963 sizeof(cfg->speaker_pins));
1964 cfg->speaker_outs = 0;
1965 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1966 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1967 fill_hardwired = true;
1968 continue;
1969 }
1970 if (cfg->hp_outs > 0 &&
1971 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1972 cfg->speaker_outs = cfg->line_outs;
1973 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1974 sizeof(cfg->speaker_pins));
1975 cfg->line_outs = cfg->hp_outs;
1976 memcpy(cfg->line_out_pins, cfg->hp_pins,
1977 sizeof(cfg->hp_pins));
1978 cfg->hp_outs = 0;
1979 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1980 cfg->line_out_type = AUTO_PIN_HP_OUT;
1981 fill_hardwired = true;
1982 continue;
1983 }
1984 break;
1985 }
1986
1987 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001988 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001989 *cfg = *best_cfg;
1990 fill_and_eval_dacs(codec, best_wired, best_mio);
1991 }
1992 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1993 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001994 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001995
1996 if (cfg->line_out_pins[0]) {
1997 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001998 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001999 if (path)
2000 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002001 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01002002 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2003 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002004 if (spec->dac_min_mute)
2005 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2006 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002007 }
2008
Takashi Iwai9314a582013-01-21 10:49:05 +01002009 /* set initial pinctl targets */
2010 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2011 val = PIN_HP;
2012 else
2013 val = PIN_OUT;
2014 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2015 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2016 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2017 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2018 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2019 set_pin_targets(codec, cfg->speaker_outs,
2020 cfg->speaker_pins, val);
2021 }
2022
Takashi Iwai55a63d42013-03-21 17:20:12 +01002023 /* clear indep_hp flag if not available */
2024 if (spec->indep_hp && !indep_hp_possible(codec))
2025 spec->indep_hp = 0;
2026
Takashi Iwai352f7f92012-12-19 12:52:06 +01002027 kfree(best_cfg);
2028 return 0;
2029}
2030
2031/* add playback controls from the parsed DAC table */
2032static int create_multi_out_ctls(struct hda_codec *codec,
2033 const struct auto_pin_cfg *cfg)
2034{
2035 struct hda_gen_spec *spec = codec->spec;
2036 int i, err, noutputs;
2037
2038 noutputs = cfg->line_outs;
2039 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2040 noutputs += spec->multi_ios;
2041
2042 for (i = 0; i < noutputs; i++) {
2043 const char *name;
2044 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002045 struct nid_path *path;
2046
Takashi Iwai196c17662013-01-04 15:01:40 +01002047 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002048 if (!path)
2049 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002050
2051 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002052 if (!name || !strcmp(name, "CLFE")) {
2053 /* Center/LFE */
2054 err = add_vol_ctl(codec, "Center", 0, 1, path);
2055 if (err < 0)
2056 return err;
2057 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2058 if (err < 0)
2059 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002060 } else {
2061 err = add_stereo_vol(codec, name, index, path);
2062 if (err < 0)
2063 return err;
2064 }
2065
2066 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2067 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002068 err = add_sw_ctl(codec, "Center", 0, 1, path);
2069 if (err < 0)
2070 return err;
2071 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2072 if (err < 0)
2073 return err;
2074 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002075 err = add_stereo_sw(codec, name, index, path);
2076 if (err < 0)
2077 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
2079 }
2080 return 0;
2081}
2082
Takashi Iwaic2c80382013-01-07 10:33:57 +01002083static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01002084 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002086 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 int err;
2088
Takashi Iwai196c17662013-01-04 15:01:40 +01002089 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002090 if (!path)
2091 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01002092 err = add_stereo_vol(codec, pfx, cidx, path);
2093 if (err < 0)
2094 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002095 err = add_stereo_sw(codec, pfx, cidx, path);
2096 if (err < 0)
2097 return err;
2098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
Takashi Iwai352f7f92012-12-19 12:52:06 +01002101/* add playback controls for speaker and HP outputs */
2102static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01002103 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Takashi Iwaic2c80382013-01-07 10:33:57 +01002105 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002106
2107 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002108 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02002109 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01002110 int err, idx = 0;
2111
2112 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2113 name = "Bass Speaker";
2114 else if (num_pins >= 3) {
2115 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01002116 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01002117 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002118 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002119 name = pfx;
2120 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01002122 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002123 if (err < 0)
2124 return err;
2125 }
2126 return 0;
2127}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002128
Takashi Iwai352f7f92012-12-19 12:52:06 +01002129static int create_hp_out_ctls(struct hda_codec *codec)
2130{
2131 struct hda_gen_spec *spec = codec->spec;
2132 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002133 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002134 "Headphone");
2135}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Takashi Iwai352f7f92012-12-19 12:52:06 +01002137static int create_speaker_out_ctls(struct hda_codec *codec)
2138{
2139 struct hda_gen_spec *spec = codec->spec;
2140 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002141 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002142 "Speaker");
2143}
2144
2145/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002146 * independent HP controls
2147 */
2148
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02002149static void call_hp_automute(struct hda_codec *codec,
2150 struct hda_jack_callback *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002151static int indep_hp_info(struct snd_kcontrol *kcontrol,
2152 struct snd_ctl_elem_info *uinfo)
2153{
2154 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2155}
2156
2157static int indep_hp_get(struct snd_kcontrol *kcontrol,
2158 struct snd_ctl_elem_value *ucontrol)
2159{
2160 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2161 struct hda_gen_spec *spec = codec->spec;
2162 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2163 return 0;
2164}
2165
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002166static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2167 int nomix_path_idx, int mix_path_idx,
2168 int out_type);
2169
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002170static int indep_hp_put(struct snd_kcontrol *kcontrol,
2171 struct snd_ctl_elem_value *ucontrol)
2172{
2173 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2174 struct hda_gen_spec *spec = codec->spec;
2175 unsigned int select = ucontrol->value.enumerated.item[0];
2176 int ret = 0;
2177
2178 mutex_lock(&spec->pcm_mutex);
2179 if (spec->active_streams) {
2180 ret = -EBUSY;
2181 goto unlock;
2182 }
2183
2184 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002185 hda_nid_t *dacp;
2186 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2187 dacp = &spec->private_dac_nids[0];
2188 else
2189 dacp = &spec->multiout.hp_out_nid[0];
2190
2191 /* update HP aamix paths in case it conflicts with indep HP */
2192 if (spec->have_aamix_ctl) {
2193 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2194 update_aamix_paths(codec, spec->aamix_mode,
2195 spec->out_paths[0],
2196 spec->aamix_out_paths[0],
2197 spec->autocfg.line_out_type);
2198 else
2199 update_aamix_paths(codec, spec->aamix_mode,
2200 spec->hp_paths[0],
2201 spec->aamix_out_paths[1],
2202 AUTO_PIN_HP_OUT);
2203 }
2204
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002205 spec->indep_hp_enabled = select;
2206 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002207 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002208 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002209 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002210
Takashi Iwai963afde2013-05-31 15:20:31 +02002211 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002212 ret = 1;
2213 }
2214 unlock:
2215 mutex_unlock(&spec->pcm_mutex);
2216 return ret;
2217}
2218
2219static const struct snd_kcontrol_new indep_hp_ctl = {
2220 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2221 .name = "Independent HP",
2222 .info = indep_hp_info,
2223 .get = indep_hp_get,
2224 .put = indep_hp_put,
2225};
2226
2227
2228static int create_indep_hp_ctls(struct hda_codec *codec)
2229{
2230 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002231 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002232
2233 if (!spec->indep_hp)
2234 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002235 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2236 dac = spec->multiout.dac_nids[0];
2237 else
2238 dac = spec->multiout.hp_out_nid[0];
2239 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002240 spec->indep_hp = 0;
2241 return 0;
2242 }
2243
2244 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002245 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002246 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2247 return -ENOMEM;
2248 return 0;
2249}
2250
2251/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002252 * channel mode enum control
2253 */
2254
2255static int ch_mode_info(struct snd_kcontrol *kcontrol,
2256 struct snd_ctl_elem_info *uinfo)
2257{
2258 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2259 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002260 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002261
2262 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2263 uinfo->count = 1;
2264 uinfo->value.enumerated.items = spec->multi_ios + 1;
2265 if (uinfo->value.enumerated.item > spec->multi_ios)
2266 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002267 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2268 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002269 return 0;
2270}
2271
2272static int ch_mode_get(struct snd_kcontrol *kcontrol,
2273 struct snd_ctl_elem_value *ucontrol)
2274{
2275 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2276 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002277 ucontrol->value.enumerated.item[0] =
2278 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002279 return 0;
2280}
2281
Takashi Iwai196c17662013-01-04 15:01:40 +01002282static inline struct nid_path *
2283get_multiio_path(struct hda_codec *codec, int idx)
2284{
2285 struct hda_gen_spec *spec = codec->spec;
2286 return snd_hda_get_path_from_idx(codec,
2287 spec->out_paths[spec->autocfg.line_outs + idx]);
2288}
2289
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002290static void update_automute_all(struct hda_codec *codec);
2291
Takashi Iwai65033cc2013-04-16 12:31:05 +02002292/* Default value to be passed as aamix argument for snd_hda_activate_path();
2293 * used for output paths
2294 */
2295static bool aamix_default(struct hda_gen_spec *spec)
2296{
2297 return !spec->have_aamix_ctl || spec->aamix_mode;
2298}
2299
Takashi Iwai352f7f92012-12-19 12:52:06 +01002300static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2301{
2302 struct hda_gen_spec *spec = codec->spec;
2303 hda_nid_t nid = spec->multi_io[idx].pin;
2304 struct nid_path *path;
2305
Takashi Iwai196c17662013-01-04 15:01:40 +01002306 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002307 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002309
2310 if (path->active == output)
2311 return 0;
2312
2313 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002314 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002315 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002316 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002317 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002318 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002319 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002320 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002321 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002323
2324 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002325 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002326
Takashi Iwai352f7f92012-12-19 12:52:06 +01002327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328}
2329
Takashi Iwai352f7f92012-12-19 12:52:06 +01002330static int ch_mode_put(struct snd_kcontrol *kcontrol,
2331 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002333 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2334 struct hda_gen_spec *spec = codec->spec;
2335 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Takashi Iwai352f7f92012-12-19 12:52:06 +01002337 ch = ucontrol->value.enumerated.item[0];
2338 if (ch < 0 || ch > spec->multi_ios)
2339 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002340 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002341 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002342 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002343 for (i = 0; i < spec->multi_ios; i++)
2344 set_multi_io(codec, i, i < ch);
2345 spec->multiout.max_channels = max(spec->ext_channel_count,
2346 spec->const_channel_count);
2347 if (spec->need_dac_fix)
2348 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return 1;
2350}
2351
Takashi Iwai352f7f92012-12-19 12:52:06 +01002352static const struct snd_kcontrol_new channel_mode_enum = {
2353 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2354 .name = "Channel Mode",
2355 .info = ch_mode_info,
2356 .get = ch_mode_get,
2357 .put = ch_mode_put,
2358};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Takashi Iwai352f7f92012-12-19 12:52:06 +01002360static int create_multi_channel_mode(struct hda_codec *codec)
2361{
2362 struct hda_gen_spec *spec = codec->spec;
2363
2364 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002365 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002366 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 return 0;
2369}
2370
Takashi Iwai352f7f92012-12-19 12:52:06 +01002371/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002372 * aamix loopback enable/disable switch
2373 */
2374
2375#define loopback_mixing_info indep_hp_info
2376
2377static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2378 struct snd_ctl_elem_value *ucontrol)
2379{
2380 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2381 struct hda_gen_spec *spec = codec->spec;
2382 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2383 return 0;
2384}
2385
2386static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002387 int nomix_path_idx, int mix_path_idx,
2388 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002389{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002390 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002391 struct nid_path *nomix_path, *mix_path;
2392
2393 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2394 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2395 if (!nomix_path || !mix_path)
2396 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002397
2398 /* if HP aamix path is driven from a different DAC and the
2399 * independent HP mode is ON, can't turn on aamix path
2400 */
2401 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2402 mix_path->path[0] != spec->alt_dac_nid)
2403 do_mix = false;
2404
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002405 if (do_mix) {
2406 snd_hda_activate_path(codec, nomix_path, false, true);
2407 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002408 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002409 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002410 snd_hda_activate_path(codec, mix_path, false, false);
2411 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002412 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002413 }
2414}
2415
2416static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2417 struct snd_ctl_elem_value *ucontrol)
2418{
2419 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2420 struct hda_gen_spec *spec = codec->spec;
2421 unsigned int val = ucontrol->value.enumerated.item[0];
2422
2423 if (val == spec->aamix_mode)
2424 return 0;
2425 spec->aamix_mode = val;
2426 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002427 spec->aamix_out_paths[0],
2428 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002429 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002430 spec->aamix_out_paths[1],
2431 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002432 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002433 spec->aamix_out_paths[2],
2434 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002435 return 1;
2436}
2437
2438static const struct snd_kcontrol_new loopback_mixing_enum = {
2439 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2440 .name = "Loopback Mixing",
2441 .info = loopback_mixing_info,
2442 .get = loopback_mixing_get,
2443 .put = loopback_mixing_put,
2444};
2445
2446static int create_loopback_mixing_ctl(struct hda_codec *codec)
2447{
2448 struct hda_gen_spec *spec = codec->spec;
2449
2450 if (!spec->mixer_nid)
2451 return 0;
2452 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2453 spec->aamix_out_paths[2]))
2454 return 0;
2455 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2456 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002457 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002458 return 0;
2459}
2460
2461/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002462 * shared headphone/mic handling
2463 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002464
Takashi Iwai352f7f92012-12-19 12:52:06 +01002465static void call_update_outputs(struct hda_codec *codec);
2466
2467/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002468static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002469{
2470 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002471 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002472 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002473 hda_nid_t pin;
2474
2475 pin = spec->hp_mic_pin;
2476 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2477
2478 if (!force) {
2479 val = snd_hda_codec_get_pin_target(codec, pin);
2480 if (as_mic) {
2481 if (val & PIN_IN)
2482 return;
2483 } else {
2484 if (val & PIN_OUT)
2485 return;
2486 }
2487 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002488
2489 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002490 /* if the HP pin doesn't support VREF and the codec driver gives an
2491 * alternative pin, set up the VREF on that pin instead
2492 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002493 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2494 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2495 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2496 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002497 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002498 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002499 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002500
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002501 if (!spec->hp_mic_jack_modes) {
2502 if (as_mic)
2503 val |= PIN_IN;
2504 else
2505 val = PIN_HP;
2506 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002507 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002508 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002509}
2510
2511/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002512static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002513{
2514 struct hda_gen_spec *spec = codec->spec;
2515 struct auto_pin_cfg *cfg = &spec->autocfg;
2516 unsigned int defcfg;
2517 hda_nid_t nid;
2518
Takashi Iwai967303d2013-02-19 17:12:42 +01002519 if (!spec->hp_mic) {
2520 if (spec->suppress_hp_mic_detect)
2521 return 0;
2522 /* automatic detection: only if no input or a single internal
2523 * input pin is found, try to detect the shared hp/mic
2524 */
2525 if (cfg->num_inputs > 1)
2526 return 0;
2527 else if (cfg->num_inputs == 1) {
2528 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2529 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2530 return 0;
2531 }
2532 }
2533
2534 spec->hp_mic = 0; /* clear once */
2535 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002536 return 0;
2537
Takashi Iwai967303d2013-02-19 17:12:42 +01002538 nid = 0;
2539 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2540 nid = cfg->line_out_pins[0];
2541 else if (cfg->hp_outs > 0)
2542 nid = cfg->hp_pins[0];
2543 if (!nid)
2544 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002545
2546 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2547 return 0; /* no input */
2548
Takashi Iwai967303d2013-02-19 17:12:42 +01002549 cfg->inputs[cfg->num_inputs].pin = nid;
2550 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002551 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002552 cfg->num_inputs++;
2553 spec->hp_mic = 1;
2554 spec->hp_mic_pin = nid;
2555 /* we can't handle auto-mic together with HP-mic */
2556 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002557 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002558 return 0;
2559}
2560
Takashi Iwai978e77e2013-01-10 16:57:58 +01002561/*
2562 * output jack mode
2563 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002564
2565static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2566
2567static const char * const out_jack_texts[] = {
2568 "Line Out", "Headphone Out",
2569};
2570
Takashi Iwai978e77e2013-01-10 16:57:58 +01002571static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2572 struct snd_ctl_elem_info *uinfo)
2573{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002574 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002575}
2576
2577static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2578 struct snd_ctl_elem_value *ucontrol)
2579{
2580 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2581 hda_nid_t nid = kcontrol->private_value;
2582 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2583 ucontrol->value.enumerated.item[0] = 1;
2584 else
2585 ucontrol->value.enumerated.item[0] = 0;
2586 return 0;
2587}
2588
2589static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2590 struct snd_ctl_elem_value *ucontrol)
2591{
2592 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2593 hda_nid_t nid = kcontrol->private_value;
2594 unsigned int val;
2595
2596 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2597 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2598 return 0;
2599 snd_hda_set_pin_ctl_cache(codec, nid, val);
2600 return 1;
2601}
2602
2603static const struct snd_kcontrol_new out_jack_mode_enum = {
2604 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2605 .info = out_jack_mode_info,
2606 .get = out_jack_mode_get,
2607 .put = out_jack_mode_put,
2608};
2609
2610static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2611{
2612 struct hda_gen_spec *spec = codec->spec;
2613 int i;
2614
2615 for (i = 0; i < spec->kctls.used; i++) {
2616 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2617 if (!strcmp(kctl->name, name) && kctl->index == idx)
2618 return true;
2619 }
2620 return false;
2621}
2622
2623static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2624 char *name, size_t name_len)
2625{
2626 struct hda_gen_spec *spec = codec->spec;
2627 int idx = 0;
2628
2629 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2630 strlcat(name, " Jack Mode", name_len);
2631
2632 for (; find_kctl_name(codec, name, idx); idx++)
2633 ;
2634}
2635
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002636static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2637{
2638 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002639 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002640 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2641 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2642 return 2;
2643 }
2644 return 1;
2645}
2646
Takashi Iwai978e77e2013-01-10 16:57:58 +01002647static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2648 hda_nid_t *pins)
2649{
2650 struct hda_gen_spec *spec = codec->spec;
2651 int i;
2652
2653 for (i = 0; i < num_pins; i++) {
2654 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002655 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002656 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002657 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002658 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002659 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002660 get_jack_mode_name(codec, pin, name, sizeof(name));
2661 knew = snd_hda_gen_add_kctl(spec, name,
2662 &out_jack_mode_enum);
2663 if (!knew)
2664 return -ENOMEM;
2665 knew->private_value = pin;
2666 }
2667 }
2668
2669 return 0;
2670}
2671
Takashi Iwai294765582013-01-17 09:52:11 +01002672/*
2673 * input jack mode
2674 */
2675
2676/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2677#define NUM_VREFS 6
2678
2679static const char * const vref_texts[NUM_VREFS] = {
2680 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2681 "", "Mic 80pc Bias", "Mic 100pc Bias"
2682};
2683
2684static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2685{
2686 unsigned int pincap;
2687
2688 pincap = snd_hda_query_pin_caps(codec, pin);
2689 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2690 /* filter out unusual vrefs */
2691 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2692 return pincap;
2693}
2694
2695/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2696static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2697{
2698 unsigned int i, n = 0;
2699
2700 for (i = 0; i < NUM_VREFS; i++) {
2701 if (vref_caps & (1 << i)) {
2702 if (n == item_idx)
2703 return i;
2704 n++;
2705 }
2706 }
2707 return 0;
2708}
2709
2710/* convert back from the vref ctl index to the enum item index */
2711static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2712{
2713 unsigned int i, n = 0;
2714
2715 for (i = 0; i < NUM_VREFS; i++) {
2716 if (i == idx)
2717 return n;
2718 if (vref_caps & (1 << i))
2719 n++;
2720 }
2721 return 0;
2722}
2723
2724static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2725 struct snd_ctl_elem_info *uinfo)
2726{
2727 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2728 hda_nid_t nid = kcontrol->private_value;
2729 unsigned int vref_caps = get_vref_caps(codec, nid);
2730
2731 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2732 vref_texts);
2733 /* set the right text */
2734 strcpy(uinfo->value.enumerated.name,
2735 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2736 return 0;
2737}
2738
2739static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2740 struct snd_ctl_elem_value *ucontrol)
2741{
2742 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2743 hda_nid_t nid = kcontrol->private_value;
2744 unsigned int vref_caps = get_vref_caps(codec, nid);
2745 unsigned int idx;
2746
2747 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2748 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2749 return 0;
2750}
2751
2752static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2753 struct snd_ctl_elem_value *ucontrol)
2754{
2755 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2756 hda_nid_t nid = kcontrol->private_value;
2757 unsigned int vref_caps = get_vref_caps(codec, nid);
2758 unsigned int val, idx;
2759
2760 val = snd_hda_codec_get_pin_target(codec, nid);
2761 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2762 if (idx == ucontrol->value.enumerated.item[0])
2763 return 0;
2764
2765 val &= ~AC_PINCTL_VREFEN;
2766 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2767 snd_hda_set_pin_ctl_cache(codec, nid, val);
2768 return 1;
2769}
2770
2771static const struct snd_kcontrol_new in_jack_mode_enum = {
2772 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2773 .info = in_jack_mode_info,
2774 .get = in_jack_mode_get,
2775 .put = in_jack_mode_put,
2776};
2777
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002778static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2779{
2780 struct hda_gen_spec *spec = codec->spec;
2781 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002782 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002783 nitems = hweight32(get_vref_caps(codec, pin));
2784 return nitems ? nitems : 1;
2785}
2786
Takashi Iwai294765582013-01-17 09:52:11 +01002787static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2788{
2789 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002790 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002791 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002792 unsigned int defcfg;
2793
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002794 if (pin == spec->hp_mic_pin)
2795 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002796
2797 /* no jack mode for fixed pins */
2798 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2799 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2800 return 0;
2801
2802 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002803 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002804 return 0;
2805
2806 get_jack_mode_name(codec, pin, name, sizeof(name));
2807 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2808 if (!knew)
2809 return -ENOMEM;
2810 knew->private_value = pin;
2811 return 0;
2812}
2813
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002814/*
2815 * HP/mic shared jack mode
2816 */
2817static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2818 struct snd_ctl_elem_info *uinfo)
2819{
2820 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2821 hda_nid_t nid = kcontrol->private_value;
2822 int out_jacks = get_out_jack_num_items(codec, nid);
2823 int in_jacks = get_in_jack_num_items(codec, nid);
2824 const char *text = NULL;
2825 int idx;
2826
2827 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2828 uinfo->count = 1;
2829 uinfo->value.enumerated.items = out_jacks + in_jacks;
2830 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2831 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2832 idx = uinfo->value.enumerated.item;
2833 if (idx < out_jacks) {
2834 if (out_jacks > 1)
2835 text = out_jack_texts[idx];
2836 else
2837 text = "Headphone Out";
2838 } else {
2839 idx -= out_jacks;
2840 if (in_jacks > 1) {
2841 unsigned int vref_caps = get_vref_caps(codec, nid);
2842 text = vref_texts[get_vref_idx(vref_caps, idx)];
2843 } else
2844 text = "Mic In";
2845 }
2846
2847 strcpy(uinfo->value.enumerated.name, text);
2848 return 0;
2849}
2850
2851static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2852{
2853 int out_jacks = get_out_jack_num_items(codec, nid);
2854 int in_jacks = get_in_jack_num_items(codec, nid);
2855 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2856 int idx = 0;
2857
2858 if (val & PIN_OUT) {
2859 if (out_jacks > 1 && val == PIN_HP)
2860 idx = 1;
2861 } else if (val & PIN_IN) {
2862 idx = out_jacks;
2863 if (in_jacks > 1) {
2864 unsigned int vref_caps = get_vref_caps(codec, nid);
2865 val &= AC_PINCTL_VREFEN;
2866 idx += cvt_from_vref_idx(vref_caps, val);
2867 }
2868 }
2869 return idx;
2870}
2871
2872static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2873 struct snd_ctl_elem_value *ucontrol)
2874{
2875 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2876 hda_nid_t nid = kcontrol->private_value;
2877 ucontrol->value.enumerated.item[0] =
2878 get_cur_hp_mic_jack_mode(codec, nid);
2879 return 0;
2880}
2881
2882static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2883 struct snd_ctl_elem_value *ucontrol)
2884{
2885 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2886 hda_nid_t nid = kcontrol->private_value;
2887 int out_jacks = get_out_jack_num_items(codec, nid);
2888 int in_jacks = get_in_jack_num_items(codec, nid);
2889 unsigned int val, oldval, idx;
2890
2891 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2892 idx = ucontrol->value.enumerated.item[0];
2893 if (oldval == idx)
2894 return 0;
2895
2896 if (idx < out_jacks) {
2897 if (out_jacks > 1)
2898 val = idx ? PIN_HP : PIN_OUT;
2899 else
2900 val = PIN_HP;
2901 } else {
2902 idx -= out_jacks;
2903 if (in_jacks > 1) {
2904 unsigned int vref_caps = get_vref_caps(codec, nid);
2905 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002906 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2907 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002908 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002909 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002910 }
2911 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002912 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002913
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002914 return 1;
2915}
2916
2917static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2918 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2919 .info = hp_mic_jack_mode_info,
2920 .get = hp_mic_jack_mode_get,
2921 .put = hp_mic_jack_mode_put,
2922};
2923
2924static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2925{
2926 struct hda_gen_spec *spec = codec->spec;
2927 struct snd_kcontrol_new *knew;
2928
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002929 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2930 &hp_mic_jack_mode_enum);
2931 if (!knew)
2932 return -ENOMEM;
2933 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002934 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002935 return 0;
2936}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002937
2938/*
2939 * Parse input paths
2940 */
2941
Takashi Iwai352f7f92012-12-19 12:52:06 +01002942/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002943static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002944{
2945 struct hda_amp_list *list;
2946
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002947 list = snd_array_new(&spec->loopback_list);
2948 if (!list)
2949 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002950 list->nid = mix;
2951 list->dir = HDA_INPUT;
2952 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002953 spec->loopback.amplist = spec->loopback_list.list;
2954 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002955}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002956
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002957/* return true if either a volume or a mute amp is found for the given
2958 * aamix path; the amp has to be either in the mixer node or its direct leaf
2959 */
2960static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2961 hda_nid_t pin, unsigned int *mix_val,
2962 unsigned int *mute_val)
2963{
2964 int idx, num_conns;
2965 const hda_nid_t *list;
2966 hda_nid_t nid;
2967
2968 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2969 if (idx < 0)
2970 return false;
2971
2972 *mix_val = *mute_val = 0;
2973 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2974 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2975 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2976 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2977 if (*mix_val && *mute_val)
2978 return true;
2979
2980 /* check leaf node */
2981 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2982 if (num_conns < idx)
2983 return false;
2984 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01002985 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2986 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002987 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01002988 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
2989 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002990 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2991
2992 return *mix_val || *mute_val;
2993}
2994
Takashi Iwai352f7f92012-12-19 12:52:06 +01002995/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002996static int new_analog_input(struct hda_codec *codec, int input_idx,
2997 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002998 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003000 struct hda_gen_spec *spec = codec->spec;
3001 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003002 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003003 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003005 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3006 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003007
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003008 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003009 if (!path)
3010 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003011 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01003012 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003013
3014 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003015 if (mix_val) {
3016 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003017 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003019 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 }
3021
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003022 if (mute_val) {
3023 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003024 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003026 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 }
3028
Takashi Iwai352f7f92012-12-19 12:52:06 +01003029 path->active = true;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003030 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003031 err = add_loopback_list(spec, mix_nid, idx);
3032 if (err < 0)
3033 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003034
3035 if (spec->mixer_nid != spec->mixer_merge_nid &&
3036 !spec->loopback_merge_path) {
3037 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3038 spec->mixer_merge_nid, 0);
3039 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003040 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003041 path->active = true;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003042 path->pin_enabled = true; /* static route */
3043 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003044 spec->loopback_merge_path =
3045 snd_hda_get_path_idx(codec, path);
3046 }
3047 }
3048
Takashi Iwai352f7f92012-12-19 12:52:06 +01003049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050}
3051
Takashi Iwai352f7f92012-12-19 12:52:06 +01003052static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003054 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3055 return (pincap & AC_PINCAP_IN) != 0;
3056}
3057
3058/* Parse the codec tree and retrieve ADCs */
3059static int fill_adc_nids(struct hda_codec *codec)
3060{
3061 struct hda_gen_spec *spec = codec->spec;
3062 hda_nid_t nid;
3063 hda_nid_t *adc_nids = spec->adc_nids;
3064 int max_nums = ARRAY_SIZE(spec->adc_nids);
3065 int i, nums = 0;
3066
3067 nid = codec->start_nid;
3068 for (i = 0; i < codec->num_nodes; i++, nid++) {
3069 unsigned int caps = get_wcaps(codec, nid);
3070 int type = get_wcaps_type(caps);
3071
3072 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3073 continue;
3074 adc_nids[nums] = nid;
3075 if (++nums >= max_nums)
3076 break;
3077 }
3078 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01003079
3080 /* copy the detected ADCs to all_adcs[] */
3081 spec->num_all_adcs = nums;
3082 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3083
Takashi Iwai352f7f92012-12-19 12:52:06 +01003084 return nums;
3085}
3086
3087/* filter out invalid adc_nids that don't give all active input pins;
3088 * if needed, check whether dynamic ADC-switching is available
3089 */
3090static int check_dyn_adc_switch(struct hda_codec *codec)
3091{
3092 struct hda_gen_spec *spec = codec->spec;
3093 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003094 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003095 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003096
Takashi Iwai352f7f92012-12-19 12:52:06 +01003097 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003098 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003099 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003100 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003101 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003102 break;
3103 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003104 if (i >= imux->num_items) {
3105 ok_bits |= (1 << n);
3106 nums++;
3107 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003108 }
3109
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003110 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003111 /* check whether ADC-switch is possible */
3112 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003113 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003114 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003115 spec->dyn_adc_idx[i] = n;
3116 break;
3117 }
3118 }
3119 }
3120
Takashi Iwai4e76a882014-02-25 12:21:03 +01003121 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003122 spec->dyn_adc_switch = 1;
3123 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003124 /* shrink the invalid adcs and input paths */
3125 nums = 0;
3126 for (n = 0; n < spec->num_adc_nids; n++) {
3127 if (!(ok_bits & (1 << n)))
3128 continue;
3129 if (n != nums) {
3130 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003131 for (i = 0; i < imux->num_items; i++) {
3132 invalidate_nid_path(codec,
3133 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003134 spec->input_paths[i][nums] =
3135 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003136 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003137 }
3138 nums++;
3139 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003140 spec->num_adc_nids = nums;
3141 }
3142
Takashi Iwai967303d2013-02-19 17:12:42 +01003143 if (imux->num_items == 1 ||
3144 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003145 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003146 spec->num_adc_nids = 1; /* reduce to a single ADC */
3147 }
3148
3149 /* single index for individual volumes ctls */
3150 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3151 spec->num_adc_nids = 1;
3152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 return 0;
3154}
3155
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003156/* parse capture source paths from the given pin and create imux items */
3157static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003158 int cfg_idx, int num_adcs,
3159 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003160{
3161 struct hda_gen_spec *spec = codec->spec;
3162 struct hda_input_mux *imux = &spec->input_mux;
3163 int imux_idx = imux->num_items;
3164 bool imux_added = false;
3165 int c;
3166
3167 for (c = 0; c < num_adcs; c++) {
3168 struct nid_path *path;
3169 hda_nid_t adc = spec->adc_nids[c];
3170
3171 if (!is_reachable_path(codec, pin, adc))
3172 continue;
3173 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3174 if (!path)
3175 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003176 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003177 spec->input_paths[imux_idx][c] =
3178 snd_hda_get_path_idx(codec, path);
3179
3180 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003181 if (spec->hp_mic_pin == pin)
3182 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003183 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003184 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003185 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003186 if (spec->dyn_adc_switch)
3187 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003188 }
3189 }
3190
3191 return 0;
3192}
3193
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003195 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003197
Takashi Iwaic9700422013-01-18 10:17:30 +01003198/* fill the label for each input at first */
3199static int fill_input_pin_labels(struct hda_codec *codec)
3200{
3201 struct hda_gen_spec *spec = codec->spec;
3202 const struct auto_pin_cfg *cfg = &spec->autocfg;
3203 int i;
3204
3205 for (i = 0; i < cfg->num_inputs; i++) {
3206 hda_nid_t pin = cfg->inputs[i].pin;
3207 const char *label;
3208 int j, idx;
3209
3210 if (!is_input_pin(codec, pin))
3211 continue;
3212
3213 label = hda_get_autocfg_input_label(codec, cfg, i);
3214 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003215 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003216 if (spec->input_labels[j] &&
3217 !strcmp(spec->input_labels[j], label)) {
3218 idx = spec->input_label_idxs[j] + 1;
3219 break;
3220 }
3221 }
3222
3223 spec->input_labels[i] = label;
3224 spec->input_label_idxs[i] = idx;
3225 }
3226
3227 return 0;
3228}
3229
Takashi Iwai9dba2052013-01-18 10:01:15 +01003230#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3231
Takashi Iwai352f7f92012-12-19 12:52:06 +01003232static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003233{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003234 struct hda_gen_spec *spec = codec->spec;
3235 const struct auto_pin_cfg *cfg = &spec->autocfg;
3236 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003237 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003238 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003239 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003240
Takashi Iwai352f7f92012-12-19 12:52:06 +01003241 num_adcs = fill_adc_nids(codec);
3242 if (num_adcs < 0)
3243 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003244
Takashi Iwaic9700422013-01-18 10:17:30 +01003245 err = fill_input_pin_labels(codec);
3246 if (err < 0)
3247 return err;
3248
Takashi Iwai352f7f92012-12-19 12:52:06 +01003249 for (i = 0; i < cfg->num_inputs; i++) {
3250 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
Takashi Iwai352f7f92012-12-19 12:52:06 +01003252 pin = cfg->inputs[i].pin;
3253 if (!is_input_pin(codec, pin))
3254 continue;
3255
Takashi Iwai2c12c302013-01-10 09:33:29 +01003256 val = PIN_IN;
3257 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3258 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003259 if (pin != spec->hp_mic_pin)
3260 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003261
Takashi Iwai352f7f92012-12-19 12:52:06 +01003262 if (mixer) {
3263 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003264 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003265 spec->input_labels[i],
3266 spec->input_label_idxs[i],
3267 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003268 if (err < 0)
3269 return err;
3270 }
3271 }
3272
Takashi Iwaic9700422013-01-18 10:17:30 +01003273 err = parse_capture_source(codec, pin, i, num_adcs,
3274 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003275 if (err < 0)
3276 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003277
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003278 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003279 err = create_in_jack_mode(codec, pin);
3280 if (err < 0)
3281 return err;
3282 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003283 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003284
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003285 /* add stereo mix when explicitly enabled via hint */
Takashi Iwai74f14b32014-12-15 13:43:59 +01003286 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003287 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003288 "Stereo Mix", 0);
3289 if (err < 0)
3290 return err;
Takashi Iwai82d04e12014-12-15 13:40:42 +01003291 else
3292 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003293 }
3294
3295 return 0;
3296}
3297
3298
3299/*
3300 * input source mux
3301 */
3302
Takashi Iwaic697b712013-01-07 17:09:26 +01003303/* get the input path specified by the given adc and imux indices */
3304static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003305{
3306 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003307 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3308 snd_BUG();
3309 return NULL;
3310 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003311 if (spec->dyn_adc_switch)
3312 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003313 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003314 snd_BUG();
3315 return NULL;
3316 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003317 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003318}
3319
3320static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3321 unsigned int idx);
3322
3323static int mux_enum_info(struct snd_kcontrol *kcontrol,
3324 struct snd_ctl_elem_info *uinfo)
3325{
3326 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3327 struct hda_gen_spec *spec = codec->spec;
3328 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3329}
3330
3331static int mux_enum_get(struct snd_kcontrol *kcontrol,
3332 struct snd_ctl_elem_value *ucontrol)
3333{
3334 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3335 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003336 /* the ctls are created at once with multiple counts */
3337 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003338
3339 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3340 return 0;
3341}
3342
3343static int mux_enum_put(struct snd_kcontrol *kcontrol,
3344 struct snd_ctl_elem_value *ucontrol)
3345{
3346 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003347 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003348 return mux_select(codec, adc_idx,
3349 ucontrol->value.enumerated.item[0]);
3350}
3351
Takashi Iwai352f7f92012-12-19 12:52:06 +01003352static const struct snd_kcontrol_new cap_src_temp = {
3353 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3354 .name = "Input Source",
3355 .info = mux_enum_info,
3356 .get = mux_enum_get,
3357 .put = mux_enum_put,
3358};
3359
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003360/*
3361 * capture volume and capture switch ctls
3362 */
3363
Takashi Iwai352f7f92012-12-19 12:52:06 +01003364typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3365 struct snd_ctl_elem_value *ucontrol);
3366
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003367/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003368static int cap_put_caller(struct snd_kcontrol *kcontrol,
3369 struct snd_ctl_elem_value *ucontrol,
3370 put_call_t func, int type)
3371{
3372 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3373 struct hda_gen_spec *spec = codec->spec;
3374 const struct hda_input_mux *imux;
3375 struct nid_path *path;
3376 int i, adc_idx, err = 0;
3377
3378 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003379 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003380 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003381 /* we use the cache-only update at first since multiple input paths
3382 * may shared the same amp; by updating only caches, the redundant
3383 * writes to hardware can be reduced.
3384 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003385 codec->cached_write = 1;
3386 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003387 path = get_input_path(codec, adc_idx, i);
3388 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003389 continue;
3390 kcontrol->private_value = path->ctls[type];
3391 err = func(kcontrol, ucontrol);
3392 if (err < 0)
3393 goto error;
3394 }
3395 error:
3396 codec->cached_write = 0;
3397 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003398 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003399 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003400 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003401 return err;
3402}
3403
3404/* capture volume ctl callbacks */
3405#define cap_vol_info snd_hda_mixer_amp_volume_info
3406#define cap_vol_get snd_hda_mixer_amp_volume_get
3407#define cap_vol_tlv snd_hda_mixer_amp_tlv
3408
3409static int cap_vol_put(struct snd_kcontrol *kcontrol,
3410 struct snd_ctl_elem_value *ucontrol)
3411{
3412 return cap_put_caller(kcontrol, ucontrol,
3413 snd_hda_mixer_amp_volume_put,
3414 NID_PATH_VOL_CTL);
3415}
3416
3417static const struct snd_kcontrol_new cap_vol_temp = {
3418 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3419 .name = "Capture Volume",
3420 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3421 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3422 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3423 .info = cap_vol_info,
3424 .get = cap_vol_get,
3425 .put = cap_vol_put,
3426 .tlv = { .c = cap_vol_tlv },
3427};
3428
3429/* capture switch ctl callbacks */
3430#define cap_sw_info snd_ctl_boolean_stereo_info
3431#define cap_sw_get snd_hda_mixer_amp_switch_get
3432
3433static int cap_sw_put(struct snd_kcontrol *kcontrol,
3434 struct snd_ctl_elem_value *ucontrol)
3435{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003436 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003437 snd_hda_mixer_amp_switch_put,
3438 NID_PATH_MUTE_CTL);
3439}
3440
3441static const struct snd_kcontrol_new cap_sw_temp = {
3442 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3443 .name = "Capture Switch",
3444 .info = cap_sw_info,
3445 .get = cap_sw_get,
3446 .put = cap_sw_put,
3447};
3448
3449static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3450{
3451 hda_nid_t nid;
3452 int i, depth;
3453
3454 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3455 for (depth = 0; depth < 3; depth++) {
3456 if (depth >= path->depth)
3457 return -EINVAL;
3458 i = path->depth - depth - 1;
3459 nid = path->path[i];
3460 if (!path->ctls[NID_PATH_VOL_CTL]) {
3461 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3462 path->ctls[NID_PATH_VOL_CTL] =
3463 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3464 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3465 int idx = path->idx[i];
3466 if (!depth && codec->single_adc_amp)
3467 idx = 0;
3468 path->ctls[NID_PATH_VOL_CTL] =
3469 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3470 }
3471 }
3472 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3473 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3474 path->ctls[NID_PATH_MUTE_CTL] =
3475 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3476 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3477 int idx = path->idx[i];
3478 if (!depth && codec->single_adc_amp)
3479 idx = 0;
3480 path->ctls[NID_PATH_MUTE_CTL] =
3481 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3482 }
3483 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 return 0;
3486}
3487
Takashi Iwai352f7f92012-12-19 12:52:06 +01003488static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003490 struct hda_gen_spec *spec = codec->spec;
3491 struct auto_pin_cfg *cfg = &spec->autocfg;
3492 unsigned int val;
3493 int i;
3494
3495 if (!spec->inv_dmic_split)
3496 return false;
3497 for (i = 0; i < cfg->num_inputs; i++) {
3498 if (cfg->inputs[i].pin != nid)
3499 continue;
3500 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3501 return false;
3502 val = snd_hda_codec_get_pincfg(codec, nid);
3503 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3504 }
3505 return false;
3506}
3507
Takashi Iwaia90229e2013-01-18 14:10:00 +01003508/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003509static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3510 struct snd_ctl_elem_value *ucontrol)
3511{
3512 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3513 struct hda_gen_spec *spec = codec->spec;
3514 int ret;
3515
3516 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3517 if (ret < 0)
3518 return ret;
3519
Takashi Iwaia90229e2013-01-18 14:10:00 +01003520 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003521 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003522
3523 return ret;
3524}
3525
Takashi Iwai352f7f92012-12-19 12:52:06 +01003526static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3527 int idx, bool is_switch, unsigned int ctl,
3528 bool inv_dmic)
3529{
3530 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003531 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003532 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3533 const char *sfx = is_switch ? "Switch" : "Volume";
3534 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003535 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003536
3537 if (!ctl)
3538 return 0;
3539
3540 if (label)
3541 snprintf(tmpname, sizeof(tmpname),
3542 "%s Capture %s", label, sfx);
3543 else
3544 snprintf(tmpname, sizeof(tmpname),
3545 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003546 knew = add_control(spec, type, tmpname, idx,
3547 amp_val_replace_channels(ctl, chs));
3548 if (!knew)
3549 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003550 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003551 knew->put = cap_single_sw_put;
3552 if (!inv_dmic)
3553 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003554
3555 /* Make independent right kcontrol */
3556 if (label)
3557 snprintf(tmpname, sizeof(tmpname),
3558 "Inverted %s Capture %s", label, sfx);
3559 else
3560 snprintf(tmpname, sizeof(tmpname),
3561 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003562 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003563 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003564 if (!knew)
3565 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003566 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003567 knew->put = cap_single_sw_put;
3568 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003569}
3570
3571/* create single (and simple) capture volume and switch controls */
3572static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3573 unsigned int vol_ctl, unsigned int sw_ctl,
3574 bool inv_dmic)
3575{
3576 int err;
3577 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3578 if (err < 0)
3579 return err;
3580 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3581 if (err < 0)
3582 return err;
3583 return 0;
3584}
3585
3586/* create bound capture volume and switch controls */
3587static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3588 unsigned int vol_ctl, unsigned int sw_ctl)
3589{
3590 struct hda_gen_spec *spec = codec->spec;
3591 struct snd_kcontrol_new *knew;
3592
3593 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003594 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003595 if (!knew)
3596 return -ENOMEM;
3597 knew->index = idx;
3598 knew->private_value = vol_ctl;
3599 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3600 }
3601 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003602 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003603 if (!knew)
3604 return -ENOMEM;
3605 knew->index = idx;
3606 knew->private_value = sw_ctl;
3607 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3608 }
3609 return 0;
3610}
3611
3612/* return the vol ctl when used first in the imux list */
3613static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3614{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003615 struct nid_path *path;
3616 unsigned int ctl;
3617 int i;
3618
Takashi Iwaic697b712013-01-07 17:09:26 +01003619 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003620 if (!path)
3621 return 0;
3622 ctl = path->ctls[type];
3623 if (!ctl)
3624 return 0;
3625 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003626 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003627 if (path && path->ctls[type] == ctl)
3628 return 0;
3629 }
3630 return ctl;
3631}
3632
3633/* create individual capture volume and switch controls per input */
3634static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3635{
3636 struct hda_gen_spec *spec = codec->spec;
3637 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003638 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003639
3640 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003641 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003642 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003643
Takashi Iwaic9700422013-01-18 10:17:30 +01003644 idx = imux->items[i].index;
3645 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003646 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003647 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3648
3649 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003650 err = add_single_cap_ctl(codec,
3651 spec->input_labels[idx],
3652 spec->input_label_idxs[idx],
3653 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003654 get_first_cap_ctl(codec, i, type),
3655 inv_dmic);
3656 if (err < 0)
3657 return err;
3658 }
3659 }
3660 return 0;
3661}
3662
3663static int create_capture_mixers(struct hda_codec *codec)
3664{
3665 struct hda_gen_spec *spec = codec->spec;
3666 struct hda_input_mux *imux = &spec->input_mux;
3667 int i, n, nums, err;
3668
3669 if (spec->dyn_adc_switch)
3670 nums = 1;
3671 else
3672 nums = spec->num_adc_nids;
3673
3674 if (!spec->auto_mic && imux->num_items > 1) {
3675 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003676 const char *name;
3677 name = nums > 1 ? "Input Source" : "Capture Source";
3678 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003679 if (!knew)
3680 return -ENOMEM;
3681 knew->count = nums;
3682 }
3683
3684 for (n = 0; n < nums; n++) {
3685 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003686 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003687 bool inv_dmic = false;
3688 int vol, sw;
3689
3690 vol = sw = 0;
3691 for (i = 0; i < imux->num_items; i++) {
3692 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003693 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003694 if (!path)
3695 continue;
3696 parse_capvol_in_path(codec, path);
3697 if (!vol)
3698 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003699 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003700 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003701 if (!same_amp_caps(codec, vol,
3702 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3703 multi_cap_vol = true;
3704 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003705 if (!sw)
3706 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003707 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003708 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003709 if (!same_amp_caps(codec, sw,
3710 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3711 multi_cap_vol = true;
3712 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003713 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3714 inv_dmic = true;
3715 }
3716
3717 if (!multi)
3718 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3719 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003720 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003721 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3722 else
3723 err = create_multi_cap_vol_ctl(codec);
3724 if (err < 0)
3725 return err;
3726 }
3727
3728 return 0;
3729}
3730
3731/*
3732 * add mic boosts if needed
3733 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003734
3735/* check whether the given amp is feasible as a boost volume */
3736static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3737 int dir, int idx)
3738{
3739 unsigned int step;
3740
3741 if (!nid_has_volume(codec, nid, dir) ||
3742 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3743 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3744 return false;
3745
3746 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3747 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3748 if (step < 0x20)
3749 return false;
3750 return true;
3751}
3752
3753/* look for a boost amp in a widget close to the pin */
3754static unsigned int look_for_boost_amp(struct hda_codec *codec,
3755 struct nid_path *path)
3756{
3757 unsigned int val = 0;
3758 hda_nid_t nid;
3759 int depth;
3760
3761 for (depth = 0; depth < 3; depth++) {
3762 if (depth >= path->depth - 1)
3763 break;
3764 nid = path->path[depth];
3765 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3766 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3767 break;
3768 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3769 path->idx[depth])) {
3770 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3771 HDA_INPUT);
3772 break;
3773 }
3774 }
3775
3776 return val;
3777}
3778
Takashi Iwai352f7f92012-12-19 12:52:06 +01003779static int parse_mic_boost(struct hda_codec *codec)
3780{
3781 struct hda_gen_spec *spec = codec->spec;
3782 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003783 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003784 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003785
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003786 if (!spec->num_adc_nids)
3787 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003788
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003789 for (i = 0; i < imux->num_items; i++) {
3790 struct nid_path *path;
3791 unsigned int val;
3792 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003793 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003794
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003795 idx = imux->items[i].index;
3796 if (idx >= imux->num_items)
3797 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003798
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003799 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003800 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003801 continue;
3802
3803 path = get_input_path(codec, 0, i);
3804 if (!path)
3805 continue;
3806
3807 val = look_for_boost_amp(codec, path);
3808 if (!val)
3809 continue;
3810
3811 /* create a boost control */
3812 snprintf(boost_label, sizeof(boost_label),
3813 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003814 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3815 spec->input_label_idxs[idx], val))
3816 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003817
3818 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003819 }
3820 return 0;
3821}
3822
3823/*
3824 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3825 */
3826static void parse_digital(struct hda_codec *codec)
3827{
3828 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003829 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003830 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003831 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003832
3833 /* support multiple SPDIFs; the secondary is set up as a slave */
3834 nums = 0;
3835 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003836 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003837 dig_nid = look_for_dac(codec, pin, true);
3838 if (!dig_nid)
3839 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003840 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003841 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003842 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003843 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003844 path->active = true;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003845 path->pin_enabled = true; /* no jack detection */
Takashi Iwai196c17662013-01-04 15:01:40 +01003846 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003847 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003848 if (!nums) {
3849 spec->multiout.dig_out_nid = dig_nid;
3850 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3851 } else {
3852 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3853 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
Dan Carpenterd5764222014-05-14 17:18:31 +03003854 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003855 spec->slave_dig_outs[nums - 1] = dig_nid;
3856 }
3857 nums++;
3858 }
3859
3860 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003861 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003862 dig_nid = codec->start_nid;
3863 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003864 unsigned int wcaps = get_wcaps(codec, dig_nid);
3865 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3866 continue;
3867 if (!(wcaps & AC_WCAP_DIGITAL))
3868 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003869 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003870 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003871 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003872 path->active = true;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003873 path->pin_enabled = true; /* no jack */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003874 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003875 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003876 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003877 break;
3878 }
3879 }
3880 }
3881}
3882
3883
3884/*
3885 * input MUX handling
3886 */
3887
3888static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3889
3890/* select the given imux item; either unmute exclusively or select the route */
3891static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3892 unsigned int idx)
3893{
3894 struct hda_gen_spec *spec = codec->spec;
3895 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003896 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003897
3898 imux = &spec->input_mux;
3899 if (!imux->num_items)
3900 return 0;
3901
3902 if (idx >= imux->num_items)
3903 idx = imux->num_items - 1;
3904 if (spec->cur_mux[adc_idx] == idx)
3905 return 0;
3906
Takashi Iwai55196ff2013-01-24 17:32:56 +01003907 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3908 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003909 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003910 if (old_path->active)
3911 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003912
3913 spec->cur_mux[adc_idx] = idx;
3914
Takashi Iwai967303d2013-02-19 17:12:42 +01003915 if (spec->hp_mic)
3916 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003917
3918 if (spec->dyn_adc_switch)
3919 dyn_adc_pcm_resetup(codec, idx);
3920
Takashi Iwaic697b712013-01-07 17:09:26 +01003921 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003922 if (!path)
3923 return 0;
3924 if (path->active)
3925 return 0;
3926 snd_hda_activate_path(codec, path, true, false);
3927 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003928 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003929 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003930 return 1;
3931}
3932
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003933/* power up/down widgets in the all paths that match with the given NID
3934 * as terminals (either start- or endpoint)
3935 *
3936 * returns the last changed NID, or zero if unchanged.
3937 */
3938static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
3939 int pin_state, int stream_state)
3940{
3941 struct hda_gen_spec *spec = codec->spec;
3942 hda_nid_t last, changed = 0;
3943 struct nid_path *path;
3944 int n;
3945
3946 for (n = 0; n < spec->paths.used; n++) {
3947 path = snd_array_elem(&spec->paths, n);
3948 if (path->path[0] == nid ||
3949 path->path[path->depth - 1] == nid) {
3950 bool pin_old = path->pin_enabled;
3951 bool stream_old = path->stream_enabled;
3952
3953 if (pin_state >= 0)
3954 path->pin_enabled = pin_state;
3955 if (stream_state >= 0)
3956 path->stream_enabled = stream_state;
3957 if (path->pin_enabled != pin_old ||
3958 path->stream_enabled != stream_old) {
3959 last = path_power_update(codec, path, true);
3960 if (last)
3961 changed = last;
3962 }
3963 }
3964 }
3965 return changed;
3966}
3967
3968/* power up/down the paths of the given pin according to the jack state;
3969 * power = 0/1 : only power up/down if it matches with the jack state,
3970 * < 0 : force power up/down to follow the jack sate
3971 *
3972 * returns the last changed NID, or zero if unchanged.
3973 */
3974static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
3975 int power)
3976{
3977 bool on;
3978
3979 if (!codec->power_mgmt)
3980 return 0;
3981
3982 on = snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
3983 if (power >= 0 && on != power)
3984 return 0;
3985 return set_path_power(codec, pin, on, -1);
3986}
3987
3988static void pin_power_callback(struct hda_codec *codec,
3989 struct hda_jack_callback *jack,
3990 bool on)
3991{
3992 if (jack && jack->tbl->nid)
3993 sync_power_state_change(codec,
3994 set_pin_power_jack(codec, jack->tbl->nid, on));
3995}
3996
3997/* callback only doing power up -- called at first */
3998static void pin_power_up_callback(struct hda_codec *codec,
3999 struct hda_jack_callback *jack)
4000{
4001 pin_power_callback(codec, jack, true);
4002}
4003
4004/* callback only doing power down -- called at last */
4005static void pin_power_down_callback(struct hda_codec *codec,
4006 struct hda_jack_callback *jack)
4007{
4008 pin_power_callback(codec, jack, false);
4009}
4010
4011/* set up the power up/down callbacks */
4012static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4013 const hda_nid_t *pins, bool on)
4014{
4015 int i;
4016 hda_jack_callback_fn cb =
4017 on ? pin_power_up_callback : pin_power_down_callback;
4018
4019 for (i = 0; i < num_pins && pins[i]; i++) {
4020 if (is_jack_detectable(codec, pins[i]))
4021 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4022 else
4023 set_path_power(codec, pins[i], true, -1);
4024 }
4025}
4026
4027/* enabled power callback to each available I/O pin with jack detections;
4028 * the digital I/O pins are excluded because of the unreliable detectsion
4029 */
4030static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4031{
4032 struct hda_gen_spec *spec = codec->spec;
4033 struct auto_pin_cfg *cfg = &spec->autocfg;
4034 int i;
4035
4036 if (!codec->power_mgmt)
4037 return;
4038 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4039 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4040 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4041 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4042 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4043 for (i = 0; i < cfg->num_inputs; i++)
4044 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4045}
4046
4047/* sync path power up/down with the jack states of given pins */
4048static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4049 const hda_nid_t *pins)
4050{
4051 int i;
4052
4053 for (i = 0; i < num_pins && pins[i]; i++)
4054 if (is_jack_detectable(codec, pins[i]))
4055 set_pin_power_jack(codec, pins[i], -1);
4056}
4057
4058/* sync path power up/down with pins; called at init and resume */
4059static void sync_all_pin_power_ctls(struct hda_codec *codec)
4060{
4061 struct hda_gen_spec *spec = codec->spec;
4062 struct auto_pin_cfg *cfg = &spec->autocfg;
4063 int i;
4064
4065 if (!codec->power_mgmt)
4066 return;
4067 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4068 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4069 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4070 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4071 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4072 for (i = 0; i < cfg->num_inputs; i++)
4073 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4074}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004075
4076/*
4077 * Jack detections for HP auto-mute and mic-switch
4078 */
4079
4080/* check each pin in the given array; returns true if any of them is plugged */
4081static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4082{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004083 int i;
4084 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004085
4086 for (i = 0; i < num_pins; i++) {
4087 hda_nid_t nid = pins[i];
4088 if (!nid)
4089 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01004090 /* don't detect pins retasked as inputs */
4091 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4092 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004093 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4094 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004095 }
4096 return present;
4097}
4098
4099/* standard HP/line-out auto-mute helper */
4100static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004101 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004102{
4103 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004104 int i;
4105
4106 for (i = 0; i < num_pins; i++) {
4107 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01004108 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004109 if (!nid)
4110 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004111
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004112 oldval = snd_hda_codec_get_pin_target(codec, nid);
4113 if (oldval & PIN_IN)
4114 continue; /* no mute for inputs */
4115
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004116 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004117 struct nid_path *path;
4118 hda_nid_t mute_nid;
4119
4120 path = snd_hda_get_path_from_idx(codec, paths[i]);
4121 if (!path)
4122 continue;
4123 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4124 if (!mute_nid)
4125 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004126 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004127 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004128 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004129 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004130 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004131 } else {
4132 /* don't reset VREF value in case it's controlling
4133 * the amp (see alc861_fixup_asus_amp_vref_0f())
4134 */
4135 if (spec->keep_vref_in_automute)
4136 val = oldval & ~PIN_HP;
4137 else
4138 val = 0;
4139 if (!mute)
4140 val |= oldval;
4141 /* here we call update_pin_ctl() so that the pinctl is
4142 * changed without changing the pinctl target value;
4143 * the original target value will be still referred at
4144 * the init / resume again
4145 */
4146 update_pin_ctl(codec, nid, val);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004147 }
4148
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01004149 set_pin_eapd(codec, nid, !mute);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004150 if (codec->power_mgmt) {
4151 bool on = !mute;
4152 if (on)
4153 on = snd_hda_jack_detect_state(codec, nid)
4154 != HDA_JACK_NOT_PRESENT;
4155 set_path_power(codec, nid, on, -1);
4156 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004157 }
4158}
4159
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004160/**
4161 * snd_hda_gen_update_outputs - Toggle outputs muting
4162 * @codec: the HDA codec
4163 *
4164 * Update the mute status of all outputs based on the current jack states.
4165 */
Takashi Iwai5d550e12012-12-19 15:16:44 +01004166void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004167{
4168 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004169 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004170 int on;
4171
4172 /* Control HP pins/amps depending on master_mute state;
4173 * in general, HP pins/amps control should be enabled in all cases,
4174 * but currently set only for master_mute, just to be safe
4175 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004176 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4177 paths = spec->out_paths;
4178 else
4179 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01004180 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004181 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004182
4183 if (!spec->automute_speaker)
4184 on = 0;
4185 else
4186 on = spec->hp_jack_present | spec->line_jack_present;
4187 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01004188 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004189 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4190 paths = spec->out_paths;
4191 else
4192 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004193 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004194 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004195
4196 /* toggle line-out mutes if needed, too */
4197 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4198 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4199 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4200 return;
4201 if (!spec->automute_lo)
4202 on = 0;
4203 else
4204 on = spec->hp_jack_present;
4205 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01004206 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004207 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004208 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004209 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004210}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004211EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004212
4213static void call_update_outputs(struct hda_codec *codec)
4214{
4215 struct hda_gen_spec *spec = codec->spec;
4216 if (spec->automute_hook)
4217 spec->automute_hook(codec);
4218 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01004219 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004220
4221 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4222 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4223 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004224}
4225
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004226/**
4227 * snd_hda_gen_hp_automute - standard HP-automute helper
4228 * @codec: the HDA codec
4229 * @jack: jack object, NULL for the whole
4230 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004231void snd_hda_gen_hp_automute(struct hda_codec *codec,
4232 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004233{
4234 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01004235 hda_nid_t *pins = spec->autocfg.hp_pins;
4236 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004237
Takashi Iwai92603c52013-01-22 07:46:31 +01004238 /* No detection for the first HP jack during indep-HP mode */
4239 if (spec->indep_hp_enabled) {
4240 pins++;
4241 num_pins--;
4242 }
4243
4244 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004245 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4246 return;
4247 call_update_outputs(codec);
4248}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004249EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004250
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004251/**
4252 * snd_hda_gen_line_automute - standard line-out-automute helper
4253 * @codec: the HDA codec
4254 * @jack: jack object, NULL for the whole
4255 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004256void snd_hda_gen_line_automute(struct hda_codec *codec,
4257 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004258{
4259 struct hda_gen_spec *spec = codec->spec;
4260
4261 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4262 return;
4263 /* check LO jack only when it's different from HP */
4264 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4265 return;
4266
4267 spec->line_jack_present =
4268 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4269 spec->autocfg.line_out_pins);
4270 if (!spec->automute_speaker || !spec->detect_lo)
4271 return;
4272 call_update_outputs(codec);
4273}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004274EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004275
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004276/**
4277 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4278 * @codec: the HDA codec
4279 * @jack: jack object, NULL for the whole
4280 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004281void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4282 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004283{
4284 struct hda_gen_spec *spec = codec->spec;
4285 int i;
4286
4287 if (!spec->auto_mic)
4288 return;
4289
4290 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01004291 hda_nid_t pin = spec->am_entry[i].pin;
4292 /* don't detect pins retasked as outputs */
4293 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4294 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004295 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004296 mux_select(codec, 0, spec->am_entry[i].idx);
4297 return;
4298 }
4299 }
4300 mux_select(codec, 0, spec->am_entry[0].idx);
4301}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004302EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004303
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004304/* call appropriate hooks */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004305static void call_hp_automute(struct hda_codec *codec,
4306 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004307{
4308 struct hda_gen_spec *spec = codec->spec;
4309 if (spec->hp_automute_hook)
4310 spec->hp_automute_hook(codec, jack);
4311 else
4312 snd_hda_gen_hp_automute(codec, jack);
4313}
4314
4315static void call_line_automute(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004316 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004317{
4318 struct hda_gen_spec *spec = codec->spec;
4319 if (spec->line_automute_hook)
4320 spec->line_automute_hook(codec, jack);
4321 else
4322 snd_hda_gen_line_automute(codec, jack);
4323}
4324
4325static void call_mic_autoswitch(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004326 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004327{
4328 struct hda_gen_spec *spec = codec->spec;
4329 if (spec->mic_autoswitch_hook)
4330 spec->mic_autoswitch_hook(codec, jack);
4331 else
4332 snd_hda_gen_mic_autoswitch(codec, jack);
4333}
4334
Takashi Iwai963afde2013-05-31 15:20:31 +02004335/* update jack retasking */
4336static void update_automute_all(struct hda_codec *codec)
4337{
4338 call_hp_automute(codec, NULL);
4339 call_line_automute(codec, NULL);
4340 call_mic_autoswitch(codec, NULL);
4341}
4342
Takashi Iwai352f7f92012-12-19 12:52:06 +01004343/*
4344 * Auto-Mute mode mixer enum support
4345 */
4346static int automute_mode_info(struct snd_kcontrol *kcontrol,
4347 struct snd_ctl_elem_info *uinfo)
4348{
4349 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4350 struct hda_gen_spec *spec = codec->spec;
4351 static const char * const texts3[] = {
4352 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004353 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354
Takashi Iwai352f7f92012-12-19 12:52:06 +01004355 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4356 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4357 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4358}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
Takashi Iwai352f7f92012-12-19 12:52:06 +01004360static int automute_mode_get(struct snd_kcontrol *kcontrol,
4361 struct snd_ctl_elem_value *ucontrol)
4362{
4363 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4364 struct hda_gen_spec *spec = codec->spec;
4365 unsigned int val = 0;
4366 if (spec->automute_speaker)
4367 val++;
4368 if (spec->automute_lo)
4369 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004370
Takashi Iwai352f7f92012-12-19 12:52:06 +01004371 ucontrol->value.enumerated.item[0] = val;
4372 return 0;
4373}
4374
4375static int automute_mode_put(struct snd_kcontrol *kcontrol,
4376 struct snd_ctl_elem_value *ucontrol)
4377{
4378 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4379 struct hda_gen_spec *spec = codec->spec;
4380
4381 switch (ucontrol->value.enumerated.item[0]) {
4382 case 0:
4383 if (!spec->automute_speaker && !spec->automute_lo)
4384 return 0;
4385 spec->automute_speaker = 0;
4386 spec->automute_lo = 0;
4387 break;
4388 case 1:
4389 if (spec->automute_speaker_possible) {
4390 if (!spec->automute_lo && spec->automute_speaker)
4391 return 0;
4392 spec->automute_speaker = 1;
4393 spec->automute_lo = 0;
4394 } else if (spec->automute_lo_possible) {
4395 if (spec->automute_lo)
4396 return 0;
4397 spec->automute_lo = 1;
4398 } else
4399 return -EINVAL;
4400 break;
4401 case 2:
4402 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4403 return -EINVAL;
4404 if (spec->automute_speaker && spec->automute_lo)
4405 return 0;
4406 spec->automute_speaker = 1;
4407 spec->automute_lo = 1;
4408 break;
4409 default:
4410 return -EINVAL;
4411 }
4412 call_update_outputs(codec);
4413 return 1;
4414}
4415
4416static const struct snd_kcontrol_new automute_mode_enum = {
4417 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4418 .name = "Auto-Mute Mode",
4419 .info = automute_mode_info,
4420 .get = automute_mode_get,
4421 .put = automute_mode_put,
4422};
4423
4424static int add_automute_mode_enum(struct hda_codec *codec)
4425{
4426 struct hda_gen_spec *spec = codec->spec;
4427
Takashi Iwai12c93df2012-12-19 14:38:33 +01004428 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004429 return -ENOMEM;
4430 return 0;
4431}
4432
4433/*
4434 * Check the availability of HP/line-out auto-mute;
4435 * Set up appropriately if really supported
4436 */
4437static int check_auto_mute_availability(struct hda_codec *codec)
4438{
4439 struct hda_gen_spec *spec = codec->spec;
4440 struct auto_pin_cfg *cfg = &spec->autocfg;
4441 int present = 0;
4442 int i, err;
4443
Takashi Iwaif72706b2013-01-16 18:20:07 +01004444 if (spec->suppress_auto_mute)
4445 return 0;
4446
Takashi Iwai352f7f92012-12-19 12:52:06 +01004447 if (cfg->hp_pins[0])
4448 present++;
4449 if (cfg->line_out_pins[0])
4450 present++;
4451 if (cfg->speaker_pins[0])
4452 present++;
4453 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004454 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004455
4456 if (!cfg->speaker_pins[0] &&
4457 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4458 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4459 sizeof(cfg->speaker_pins));
4460 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
Takashi Iwai352f7f92012-12-19 12:52:06 +01004463 if (!cfg->hp_pins[0] &&
4464 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4465 memcpy(cfg->hp_pins, cfg->line_out_pins,
4466 sizeof(cfg->hp_pins));
4467 cfg->hp_outs = cfg->line_outs;
4468 }
4469
4470 for (i = 0; i < cfg->hp_outs; i++) {
4471 hda_nid_t nid = cfg->hp_pins[i];
4472 if (!is_jack_detectable(codec, nid))
4473 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004474 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai62f949b2014-09-11 14:06:53 +02004475 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004476 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004477 spec->detect_hp = 1;
4478 }
4479
4480 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4481 if (cfg->speaker_outs)
4482 for (i = 0; i < cfg->line_outs; i++) {
4483 hda_nid_t nid = cfg->line_out_pins[i];
4484 if (!is_jack_detectable(codec, nid))
4485 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004486 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004487 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004488 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004489 spec->detect_lo = 1;
4490 }
4491 spec->automute_lo_possible = spec->detect_hp;
4492 }
4493
4494 spec->automute_speaker_possible = cfg->speaker_outs &&
4495 (spec->detect_hp || spec->detect_lo);
4496
4497 spec->automute_lo = spec->automute_lo_possible;
4498 spec->automute_speaker = spec->automute_speaker_possible;
4499
4500 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4501 /* create a control for automute mode */
4502 err = add_automute_mode_enum(codec);
4503 if (err < 0)
4504 return err;
4505 }
4506 return 0;
4507}
4508
Takashi Iwai352f7f92012-12-19 12:52:06 +01004509/* check whether all auto-mic pins are valid; setup indices if OK */
4510static bool auto_mic_check_imux(struct hda_codec *codec)
4511{
4512 struct hda_gen_spec *spec = codec->spec;
4513 const struct hda_input_mux *imux;
4514 int i;
4515
4516 imux = &spec->input_mux;
4517 for (i = 0; i < spec->am_num_entries; i++) {
4518 spec->am_entry[i].idx =
4519 find_idx_in_nid_list(spec->am_entry[i].pin,
4520 spec->imux_pins, imux->num_items);
4521 if (spec->am_entry[i].idx < 0)
4522 return false; /* no corresponding imux */
4523 }
4524
4525 /* we don't need the jack detection for the first pin */
4526 for (i = 1; i < spec->am_num_entries; i++)
4527 snd_hda_jack_detect_enable_callback(codec,
4528 spec->am_entry[i].pin,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004529 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004530 return true;
4531}
4532
4533static int compare_attr(const void *ap, const void *bp)
4534{
4535 const struct automic_entry *a = ap;
4536 const struct automic_entry *b = bp;
4537 return (int)(a->attr - b->attr);
4538}
4539
4540/*
4541 * Check the availability of auto-mic switch;
4542 * Set up if really supported
4543 */
4544static int check_auto_mic_availability(struct hda_codec *codec)
4545{
4546 struct hda_gen_spec *spec = codec->spec;
4547 struct auto_pin_cfg *cfg = &spec->autocfg;
4548 unsigned int types;
4549 int i, num_pins;
4550
Takashi Iwaid12daf62013-01-07 16:32:11 +01004551 if (spec->suppress_auto_mic)
4552 return 0;
4553
Takashi Iwai352f7f92012-12-19 12:52:06 +01004554 types = 0;
4555 num_pins = 0;
4556 for (i = 0; i < cfg->num_inputs; i++) {
4557 hda_nid_t nid = cfg->inputs[i].pin;
4558 unsigned int attr;
4559 attr = snd_hda_codec_get_pincfg(codec, nid);
4560 attr = snd_hda_get_input_pin_attr(attr);
4561 if (types & (1 << attr))
4562 return 0; /* already occupied */
4563 switch (attr) {
4564 case INPUT_PIN_ATTR_INT:
4565 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4566 return 0; /* invalid type */
4567 break;
4568 case INPUT_PIN_ATTR_UNUSED:
4569 return 0; /* invalid entry */
4570 default:
4571 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4572 return 0; /* invalid type */
4573 if (!spec->line_in_auto_switch &&
4574 cfg->inputs[i].type != AUTO_PIN_MIC)
4575 return 0; /* only mic is allowed */
4576 if (!is_jack_detectable(codec, nid))
4577 return 0; /* no unsol support */
4578 break;
4579 }
4580 if (num_pins >= MAX_AUTO_MIC_PINS)
4581 return 0;
4582 types |= (1 << attr);
4583 spec->am_entry[num_pins].pin = nid;
4584 spec->am_entry[num_pins].attr = attr;
4585 num_pins++;
4586 }
4587
4588 if (num_pins < 2)
4589 return 0;
4590
4591 spec->am_num_entries = num_pins;
4592 /* sort the am_entry in the order of attr so that the pin with a
4593 * higher attr will be selected when the jack is plugged.
4594 */
4595 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4596 compare_attr, NULL);
4597
4598 if (!auto_mic_check_imux(codec))
4599 return 0;
4600
4601 spec->auto_mic = 1;
4602 spec->num_adc_nids = 1;
4603 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004604 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004605 spec->am_entry[0].pin,
4606 spec->am_entry[1].pin,
4607 spec->am_entry[2].pin);
4608
4609 return 0;
4610}
4611
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004612/**
4613 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4614 * into power down
4615 * @codec: the HDA codec
4616 * @nid: NID to evalute
4617 * @power_state: target power state
4618 */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004619unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004620 hda_nid_t nid,
4621 unsigned int power_state)
4622{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004623 if (power_state != AC_PWRST_D0 || nid == codec->afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004624 return power_state;
4625 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4626 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004627 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004628 return power_state;
4629 return AC_PWRST_D3;
4630}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004631EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004632
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004633/* mute all aamix inputs initially; parse up to the first leaves */
4634static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4635{
4636 int i, nums;
4637 const hda_nid_t *conn;
4638 bool has_amp;
4639
4640 nums = snd_hda_get_conn_list(codec, mix, &conn);
4641 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4642 for (i = 0; i < nums; i++) {
4643 if (has_amp)
Takashi Iwaief403ed2015-03-12 08:30:11 +01004644 update_amp(codec, mix, HDA_INPUT, i,
4645 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004646 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
Takashi Iwaief403ed2015-03-12 08:30:11 +01004647 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4648 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004649 }
4650}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004651
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004652/**
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004653 * snd_hda_gen_stream_pm - Stream power management callback
4654 * @codec: the HDA codec
4655 * @nid: audio widget
4656 * @on: power on/off flag
4657 *
4658 * Set this in patch_ops.stream_pm. Only valid with power_mgmt flag.
4659 */
4660void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4661{
4662 if (codec->power_mgmt)
4663 set_path_power(codec, nid, -1, on);
4664}
4665EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4666
4667/**
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004668 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4669 * set up the hda_gen_spec
4670 * @codec: the HDA codec
4671 * @cfg: Parsed pin configuration
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004672 *
4673 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004674 * or a negative error code
4675 */
4676int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004677 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004678{
4679 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004680 int err;
4681
Takashi Iwai1c70a582013-01-11 17:48:22 +01004682 parse_user_hints(codec);
4683
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004684 if (spec->mixer_nid && !spec->mixer_merge_nid)
4685 spec->mixer_merge_nid = spec->mixer_nid;
4686
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004687 if (cfg != &spec->autocfg) {
4688 spec->autocfg = *cfg;
4689 cfg = &spec->autocfg;
4690 }
4691
Takashi Iwai98bd1112013-03-22 14:53:50 +01004692 if (!spec->main_out_badness)
4693 spec->main_out_badness = &hda_main_out_badness;
4694 if (!spec->extra_out_badness)
4695 spec->extra_out_badness = &hda_extra_out_badness;
4696
David Henningsson6fc4cb92013-01-16 15:58:45 +01004697 fill_all_dac_nids(codec);
4698
Takashi Iwai352f7f92012-12-19 12:52:06 +01004699 if (!cfg->line_outs) {
4700 if (cfg->dig_outs || cfg->dig_in_pin) {
4701 spec->multiout.max_channels = 2;
4702 spec->no_analog = 1;
4703 goto dig_only;
4704 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004705 if (!cfg->num_inputs && !cfg->dig_in_pin)
4706 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004707 }
4708
4709 if (!spec->no_primary_hp &&
4710 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4711 cfg->line_outs <= cfg->hp_outs) {
4712 /* use HP as primary out */
4713 cfg->speaker_outs = cfg->line_outs;
4714 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4715 sizeof(cfg->speaker_pins));
4716 cfg->line_outs = cfg->hp_outs;
4717 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4718 cfg->hp_outs = 0;
4719 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4720 cfg->line_out_type = AUTO_PIN_HP_OUT;
4721 }
4722
4723 err = parse_output_paths(codec);
4724 if (err < 0)
4725 return err;
4726 err = create_multi_channel_mode(codec);
4727 if (err < 0)
4728 return err;
4729 err = create_multi_out_ctls(codec, cfg);
4730 if (err < 0)
4731 return err;
4732 err = create_hp_out_ctls(codec);
4733 if (err < 0)
4734 return err;
4735 err = create_speaker_out_ctls(codec);
4736 if (err < 0)
4737 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004738 err = create_indep_hp_ctls(codec);
4739 if (err < 0)
4740 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004741 err = create_loopback_mixing_ctl(codec);
4742 if (err < 0)
4743 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004744 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004745 if (err < 0)
4746 return err;
4747 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004748 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004749 return err;
4750
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004751 /* add power-down pin callbacks at first */
4752 add_all_pin_power_ctls(codec, false);
4753
Takashi Iwaia07a9492013-01-07 16:44:06 +01004754 spec->const_channel_count = spec->ext_channel_count;
4755 /* check the multiple speaker and headphone pins */
4756 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4757 spec->const_channel_count = max(spec->const_channel_count,
4758 cfg->speaker_outs * 2);
4759 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4760 spec->const_channel_count = max(spec->const_channel_count,
4761 cfg->hp_outs * 2);
4762 spec->multiout.max_channels = max(spec->ext_channel_count,
4763 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004764
4765 err = check_auto_mute_availability(codec);
4766 if (err < 0)
4767 return err;
4768
4769 err = check_dyn_adc_switch(codec);
4770 if (err < 0)
4771 return err;
4772
Takashi Iwai967303d2013-02-19 17:12:42 +01004773 err = check_auto_mic_availability(codec);
4774 if (err < 0)
4775 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004776
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004777 /* add stereo mix if available and not enabled yet */
4778 if (!spec->auto_mic && spec->mixer_nid &&
Takashi Iwai74f14b32014-12-15 13:43:59 +01004779 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4780 spec->input_mux.num_items > 1) {
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004781 err = parse_capture_source(codec, spec->mixer_nid,
4782 CFG_IDX_MIX, spec->num_all_adcs,
4783 "Stereo Mix", 0);
4784 if (err < 0)
4785 return err;
4786 }
4787
4788
Takashi Iwai352f7f92012-12-19 12:52:06 +01004789 err = create_capture_mixers(codec);
4790 if (err < 0)
4791 return err;
4792
4793 err = parse_mic_boost(codec);
4794 if (err < 0)
4795 return err;
4796
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004797 /* create "Headphone Mic Jack Mode" if no input selection is
4798 * available (or user specifies add_jack_modes hint)
4799 */
4800 if (spec->hp_mic_pin &&
4801 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4802 spec->add_jack_modes)) {
4803 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4804 if (err < 0)
4805 return err;
4806 }
4807
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004808 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004809 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4810 err = create_out_jack_modes(codec, cfg->line_outs,
4811 cfg->line_out_pins);
4812 if (err < 0)
4813 return err;
4814 }
4815 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4816 err = create_out_jack_modes(codec, cfg->hp_outs,
4817 cfg->hp_pins);
4818 if (err < 0)
4819 return err;
4820 }
4821 }
4822
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004823 /* add power-up pin callbacks at last */
4824 add_all_pin_power_ctls(codec, true);
4825
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004826 /* mute all aamix input initially */
4827 if (spec->mixer_nid)
4828 mute_all_mixer_nid(codec, spec->mixer_nid);
4829
Takashi Iwai352f7f92012-12-19 12:52:06 +01004830 dig_only:
4831 parse_digital(codec);
4832
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004833 if (spec->power_down_unused || codec->power_mgmt)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004834 codec->power_filter = snd_hda_gen_path_power_filter;
4835
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004836 if (!spec->no_analog && spec->beep_nid) {
4837 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4838 if (err < 0)
4839 return err;
4840 }
4841
Takashi Iwai352f7f92012-12-19 12:52:06 +01004842 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004844EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
4846
4847/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004848 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004850
4851/* slave controls for virtual master */
4852static const char * const slave_pfxs[] = {
4853 "Front", "Surround", "Center", "LFE", "Side",
4854 "Headphone", "Speaker", "Mono", "Line Out",
4855 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004856 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4857 "Headphone Front", "Headphone Surround", "Headphone CLFE",
David Henningsson03ad6a82014-10-16 15:33:45 +02004858 "Headphone Side", "Headphone+LO", "Speaker+LO",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004859 NULL,
4860};
4861
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004862/**
4863 * snd_hda_gen_build_controls - Build controls from the parsed results
4864 * @codec: the HDA codec
4865 *
4866 * Pass this to build_controls patch_ops.
4867 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004868int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004870 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
Takashi Iwai36502d02012-12-19 15:15:10 +01004873 if (spec->kctls.used) {
4874 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4875 if (err < 0)
4876 return err;
4877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878
Takashi Iwai352f7f92012-12-19 12:52:06 +01004879 if (spec->multiout.dig_out_nid) {
4880 err = snd_hda_create_dig_out_ctls(codec,
4881 spec->multiout.dig_out_nid,
4882 spec->multiout.dig_out_nid,
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004883 spec->pcm_rec[1]->pcm_type);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004884 if (err < 0)
4885 return err;
4886 if (!spec->no_analog) {
4887 err = snd_hda_create_spdif_share_sw(codec,
4888 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 if (err < 0)
4890 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004891 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 }
4893 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004894 if (spec->dig_in_nid) {
4895 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4896 if (err < 0)
4897 return err;
4898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899
Takashi Iwai352f7f92012-12-19 12:52:06 +01004900 /* if we have no master control, let's create it */
4901 if (!spec->no_analog &&
4902 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004903 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004904 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004905 "Playback Volume");
4906 if (err < 0)
4907 return err;
4908 }
4909 if (!spec->no_analog &&
4910 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4911 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4912 NULL, slave_pfxs,
4913 "Playback Switch",
4914 true, &spec->vmaster_mute.sw_kctl);
4915 if (err < 0)
4916 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02004917 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01004918 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4919 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02004920 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4921 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
Takashi Iwai352f7f92012-12-19 12:52:06 +01004924 free_kctls(spec); /* no longer needed */
4925
Takashi Iwai352f7f92012-12-19 12:52:06 +01004926 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4927 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 return err;
4929
4930 return 0;
4931}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004932EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004933
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934
4935/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004936 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004939static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4940 struct hda_codec *codec,
4941 struct snd_pcm_substream *substream,
4942 int action)
4943{
4944 struct hda_gen_spec *spec = codec->spec;
4945 if (spec->pcm_playback_hook)
4946 spec->pcm_playback_hook(hinfo, codec, substream, action);
4947}
4948
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004949static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4950 struct hda_codec *codec,
4951 struct snd_pcm_substream *substream,
4952 int action)
4953{
4954 struct hda_gen_spec *spec = codec->spec;
4955 if (spec->pcm_capture_hook)
4956 spec->pcm_capture_hook(hinfo, codec, substream, action);
4957}
4958
Takashi Iwai352f7f92012-12-19 12:52:06 +01004959/*
4960 * Analog playback callbacks
4961 */
4962static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4963 struct hda_codec *codec,
4964 struct snd_pcm_substream *substream)
4965{
4966 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004967 int err;
4968
4969 mutex_lock(&spec->pcm_mutex);
4970 err = snd_hda_multi_out_analog_open(codec,
4971 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004972 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004973 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004974 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004975 call_pcm_playback_hook(hinfo, codec, substream,
4976 HDA_GEN_PCM_ACT_OPEN);
4977 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004978 mutex_unlock(&spec->pcm_mutex);
4979 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004980}
4981
4982static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004983 struct hda_codec *codec,
4984 unsigned int stream_tag,
4985 unsigned int format,
4986 struct snd_pcm_substream *substream)
4987{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004988 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004989 int err;
4990
4991 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4992 stream_tag, format, substream);
4993 if (!err)
4994 call_pcm_playback_hook(hinfo, codec, substream,
4995 HDA_GEN_PCM_ACT_PREPARE);
4996 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004997}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004998
Takashi Iwai352f7f92012-12-19 12:52:06 +01004999static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5000 struct hda_codec *codec,
5001 struct snd_pcm_substream *substream)
5002{
5003 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005004 int err;
5005
5006 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5007 if (!err)
5008 call_pcm_playback_hook(hinfo, codec, substream,
5009 HDA_GEN_PCM_ACT_CLEANUP);
5010 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005011}
5012
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005013static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5014 struct hda_codec *codec,
5015 struct snd_pcm_substream *substream)
5016{
5017 struct hda_gen_spec *spec = codec->spec;
5018 mutex_lock(&spec->pcm_mutex);
5019 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005020 call_pcm_playback_hook(hinfo, codec, substream,
5021 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005022 mutex_unlock(&spec->pcm_mutex);
5023 return 0;
5024}
5025
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005026static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5027 struct hda_codec *codec,
5028 struct snd_pcm_substream *substream)
5029{
5030 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5031 return 0;
5032}
5033
5034static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5035 struct hda_codec *codec,
5036 unsigned int stream_tag,
5037 unsigned int format,
5038 struct snd_pcm_substream *substream)
5039{
5040 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5041 call_pcm_capture_hook(hinfo, codec, substream,
5042 HDA_GEN_PCM_ACT_PREPARE);
5043 return 0;
5044}
5045
5046static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5047 struct hda_codec *codec,
5048 struct snd_pcm_substream *substream)
5049{
5050 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5051 call_pcm_capture_hook(hinfo, codec, substream,
5052 HDA_GEN_PCM_ACT_CLEANUP);
5053 return 0;
5054}
5055
5056static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5057 struct hda_codec *codec,
5058 struct snd_pcm_substream *substream)
5059{
5060 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5061 return 0;
5062}
5063
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005064static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5065 struct hda_codec *codec,
5066 struct snd_pcm_substream *substream)
5067{
5068 struct hda_gen_spec *spec = codec->spec;
5069 int err = 0;
5070
5071 mutex_lock(&spec->pcm_mutex);
5072 if (!spec->indep_hp_enabled)
5073 err = -EBUSY;
5074 else
5075 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005076 call_pcm_playback_hook(hinfo, codec, substream,
5077 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005078 mutex_unlock(&spec->pcm_mutex);
5079 return err;
5080}
5081
5082static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5083 struct hda_codec *codec,
5084 struct snd_pcm_substream *substream)
5085{
5086 struct hda_gen_spec *spec = codec->spec;
5087 mutex_lock(&spec->pcm_mutex);
5088 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005089 call_pcm_playback_hook(hinfo, codec, substream,
5090 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005091 mutex_unlock(&spec->pcm_mutex);
5092 return 0;
5093}
5094
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005095static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5096 struct hda_codec *codec,
5097 unsigned int stream_tag,
5098 unsigned int format,
5099 struct snd_pcm_substream *substream)
5100{
5101 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5102 call_pcm_playback_hook(hinfo, codec, substream,
5103 HDA_GEN_PCM_ACT_PREPARE);
5104 return 0;
5105}
5106
5107static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5108 struct hda_codec *codec,
5109 struct snd_pcm_substream *substream)
5110{
5111 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5112 call_pcm_playback_hook(hinfo, codec, substream,
5113 HDA_GEN_PCM_ACT_CLEANUP);
5114 return 0;
5115}
5116
Takashi Iwai352f7f92012-12-19 12:52:06 +01005117/*
5118 * Digital out
5119 */
5120static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5121 struct hda_codec *codec,
5122 struct snd_pcm_substream *substream)
5123{
5124 struct hda_gen_spec *spec = codec->spec;
5125 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5126}
5127
5128static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5129 struct hda_codec *codec,
5130 unsigned int stream_tag,
5131 unsigned int format,
5132 struct snd_pcm_substream *substream)
5133{
5134 struct hda_gen_spec *spec = codec->spec;
5135 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5136 stream_tag, format, substream);
5137}
5138
5139static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5140 struct hda_codec *codec,
5141 struct snd_pcm_substream *substream)
5142{
5143 struct hda_gen_spec *spec = codec->spec;
5144 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5145}
5146
5147static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5148 struct hda_codec *codec,
5149 struct snd_pcm_substream *substream)
5150{
5151 struct hda_gen_spec *spec = codec->spec;
5152 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5153}
5154
5155/*
5156 * Analog capture
5157 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005158#define alt_capture_pcm_open capture_pcm_open
5159#define alt_capture_pcm_close capture_pcm_close
5160
Takashi Iwai352f7f92012-12-19 12:52:06 +01005161static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5162 struct hda_codec *codec,
5163 unsigned int stream_tag,
5164 unsigned int format,
5165 struct snd_pcm_substream *substream)
5166{
5167 struct hda_gen_spec *spec = codec->spec;
5168
5169 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01005170 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005171 call_pcm_capture_hook(hinfo, codec, substream,
5172 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005173 return 0;
5174}
5175
Takashi Iwai352f7f92012-12-19 12:52:06 +01005176static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5177 struct hda_codec *codec,
5178 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01005179{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005180 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01005181
Takashi Iwai352f7f92012-12-19 12:52:06 +01005182 snd_hda_codec_cleanup_stream(codec,
5183 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005184 call_pcm_capture_hook(hinfo, codec, substream,
5185 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005186 return 0;
5187}
5188
Takashi Iwai352f7f92012-12-19 12:52:06 +01005189/*
5190 */
5191static const struct hda_pcm_stream pcm_analog_playback = {
5192 .substreams = 1,
5193 .channels_min = 2,
5194 .channels_max = 8,
5195 /* NID is set in build_pcms */
5196 .ops = {
5197 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005198 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005199 .prepare = playback_pcm_prepare,
5200 .cleanup = playback_pcm_cleanup
5201 },
5202};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
Takashi Iwai352f7f92012-12-19 12:52:06 +01005204static const struct hda_pcm_stream pcm_analog_capture = {
5205 .substreams = 1,
5206 .channels_min = 2,
5207 .channels_max = 2,
5208 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005209 .ops = {
5210 .open = capture_pcm_open,
5211 .close = capture_pcm_close,
5212 .prepare = capture_pcm_prepare,
5213 .cleanup = capture_pcm_cleanup
5214 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005215};
5216
5217static const struct hda_pcm_stream pcm_analog_alt_playback = {
5218 .substreams = 1,
5219 .channels_min = 2,
5220 .channels_max = 2,
5221 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005222 .ops = {
5223 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005224 .close = alt_playback_pcm_close,
5225 .prepare = alt_playback_pcm_prepare,
5226 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005227 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005228};
5229
5230static const struct hda_pcm_stream pcm_analog_alt_capture = {
5231 .substreams = 2, /* can be overridden */
5232 .channels_min = 2,
5233 .channels_max = 2,
5234 /* NID is set in build_pcms */
5235 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005236 .open = alt_capture_pcm_open,
5237 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005238 .prepare = alt_capture_pcm_prepare,
5239 .cleanup = alt_capture_pcm_cleanup
5240 },
5241};
5242
5243static const struct hda_pcm_stream pcm_digital_playback = {
5244 .substreams = 1,
5245 .channels_min = 2,
5246 .channels_max = 2,
5247 /* NID is set in build_pcms */
5248 .ops = {
5249 .open = dig_playback_pcm_open,
5250 .close = dig_playback_pcm_close,
5251 .prepare = dig_playback_pcm_prepare,
5252 .cleanup = dig_playback_pcm_cleanup
5253 },
5254};
5255
5256static const struct hda_pcm_stream pcm_digital_capture = {
5257 .substreams = 1,
5258 .channels_min = 2,
5259 .channels_max = 2,
5260 /* NID is set in build_pcms */
5261};
5262
5263/* Used by build_pcms to flag that a PCM has no playback stream */
5264static const struct hda_pcm_stream pcm_null_stream = {
5265 .substreams = 0,
5266 .channels_min = 0,
5267 .channels_max = 0,
5268};
5269
5270/*
5271 * dynamic changing ADC PCM streams
5272 */
5273static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5274{
5275 struct hda_gen_spec *spec = codec->spec;
5276 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5277
5278 if (spec->cur_adc && spec->cur_adc != new_adc) {
5279 /* stream is running, let's swap the current ADC */
5280 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5281 spec->cur_adc = new_adc;
5282 snd_hda_codec_setup_stream(codec, new_adc,
5283 spec->cur_adc_stream_tag, 0,
5284 spec->cur_adc_format);
5285 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005287 return false;
5288}
5289
5290/* analog capture with dynamic dual-adc changes */
5291static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5292 struct hda_codec *codec,
5293 unsigned int stream_tag,
5294 unsigned int format,
5295 struct snd_pcm_substream *substream)
5296{
5297 struct hda_gen_spec *spec = codec->spec;
5298 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5299 spec->cur_adc_stream_tag = stream_tag;
5300 spec->cur_adc_format = format;
5301 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5302 return 0;
5303}
5304
5305static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5306 struct hda_codec *codec,
5307 struct snd_pcm_substream *substream)
5308{
5309 struct hda_gen_spec *spec = codec->spec;
5310 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5311 spec->cur_adc = 0;
5312 return 0;
5313}
5314
5315static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5316 .substreams = 1,
5317 .channels_min = 2,
5318 .channels_max = 2,
5319 .nid = 0, /* fill later */
5320 .ops = {
5321 .prepare = dyn_adc_capture_pcm_prepare,
5322 .cleanup = dyn_adc_capture_pcm_cleanup
5323 },
5324};
5325
Takashi Iwaif873e532012-12-20 16:58:39 +01005326static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5327 const char *chip_name)
5328{
5329 char *p;
5330
5331 if (*str)
5332 return;
5333 strlcpy(str, chip_name, len);
5334
5335 /* drop non-alnum chars after a space */
5336 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5337 if (!isalnum(p[1])) {
5338 *p = 0;
5339 break;
5340 }
5341 }
5342 strlcat(str, sfx, len);
5343}
5344
Takashi Iwaifb83b632015-03-16 23:34:34 +01005345/* copy PCM stream info from @default_str, and override non-NULL entries
5346 * from @spec_str and @nid
5347 */
5348static void setup_pcm_stream(struct hda_pcm_stream *str,
5349 const struct hda_pcm_stream *default_str,
5350 const struct hda_pcm_stream *spec_str,
5351 hda_nid_t nid)
5352{
5353 *str = *default_str;
5354 if (nid)
5355 str->nid = nid;
5356 if (spec_str) {
5357 if (spec_str->substreams)
5358 str->substreams = spec_str->substreams;
5359 if (spec_str->channels_min)
5360 str->channels_min = spec_str->channels_min;
5361 if (spec_str->channels_max)
5362 str->channels_max = spec_str->channels_max;
5363 if (spec_str->rates)
5364 str->rates = spec_str->rates;
5365 if (spec_str->formats)
5366 str->formats = spec_str->formats;
5367 if (spec_str->maxbps)
5368 str->maxbps = spec_str->maxbps;
5369 }
5370}
5371
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005372/**
5373 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5374 * @codec: the HDA codec
5375 *
5376 * Pass this to build_pcms patch_ops.
5377 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005378int snd_hda_gen_build_pcms(struct hda_codec *codec)
5379{
5380 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005381 struct hda_pcm *info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005382 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383
Takashi Iwai352f7f92012-12-19 12:52:06 +01005384 if (spec->no_analog)
5385 goto skip_analog;
5386
Takashi Iwaif873e532012-12-20 16:58:39 +01005387 fill_pcm_stream_name(spec->stream_name_analog,
5388 sizeof(spec->stream_name_analog),
5389 " Analog", codec->chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005390 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5391 if (!info)
5392 return -ENOMEM;
5393 spec->pcm_rec[0] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005394
5395 if (spec->multiout.num_dacs > 0) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005396 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5397 &pcm_analog_playback,
5398 spec->stream_analog_playback,
5399 spec->multiout.dac_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005400 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5401 spec->multiout.max_channels;
5402 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5403 spec->autocfg.line_outs == 2)
5404 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5405 snd_pcm_2_1_chmaps;
5406 }
5407 if (spec->num_adc_nids) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005408 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5409 (spec->dyn_adc_switch ?
5410 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5411 spec->stream_analog_capture,
5412 spec->adc_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005413 }
5414
Takashi Iwai352f7f92012-12-19 12:52:06 +01005415 skip_analog:
5416 /* SPDIF for stream index #1 */
5417 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005418 fill_pcm_stream_name(spec->stream_name_digital,
5419 sizeof(spec->stream_name_digital),
5420 " Digital", codec->chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005421 info = snd_hda_codec_pcm_new(codec, "%s",
5422 spec->stream_name_digital);
5423 if (!info)
5424 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005425 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005426 spec->pcm_rec[1] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005427 if (spec->dig_out_type)
5428 info->pcm_type = spec->dig_out_type;
5429 else
5430 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005431 if (spec->multiout.dig_out_nid)
5432 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5433 &pcm_digital_playback,
5434 spec->stream_digital_playback,
5435 spec->multiout.dig_out_nid);
5436 if (spec->dig_in_nid)
5437 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5438 &pcm_digital_capture,
5439 spec->stream_digital_capture,
5440 spec->dig_in_nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005441 }
5442
5443 if (spec->no_analog)
5444 return 0;
5445
5446 /* If the use of more than one ADC is requested for the current
5447 * model, configure a second analog capture-only PCM.
5448 */
5449 have_multi_adcs = (spec->num_adc_nids > 1) &&
5450 !spec->dyn_adc_switch && !spec->auto_mic;
5451 /* Additional Analaog capture for index #2 */
5452 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005453 fill_pcm_stream_name(spec->stream_name_alt_analog,
5454 sizeof(spec->stream_name_alt_analog),
5455 " Alt Analog", codec->chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005456 info = snd_hda_codec_pcm_new(codec, "%s",
5457 spec->stream_name_alt_analog);
5458 if (!info)
5459 return -ENOMEM;
5460 spec->pcm_rec[2] = info;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005461 if (spec->alt_dac_nid)
5462 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5463 &pcm_analog_alt_playback,
5464 spec->stream_analog_alt_playback,
5465 spec->alt_dac_nid);
5466 else
5467 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5468 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005469 if (have_multi_adcs) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005470 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5471 &pcm_analog_alt_capture,
5472 spec->stream_analog_alt_capture,
5473 spec->adc_nids[1]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005474 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5475 spec->num_adc_nids - 1;
5476 } else {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005477 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5478 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 }
5481
5482 return 0;
5483}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005484EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005485
5486
5487/*
5488 * Standard auto-parser initializations
5489 */
5490
Takashi Iwaid4156932013-01-07 10:08:02 +01005491/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005492static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005493{
5494 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005495 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005496
Takashi Iwai196c17662013-01-04 15:01:40 +01005497 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005498 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005499 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005500 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005501 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005502 snd_hda_activate_path(codec, path, path->active,
5503 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005504 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005505}
5506
5507/* initialize primary output paths */
5508static void init_multi_out(struct hda_codec *codec)
5509{
5510 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005511 int i;
5512
Takashi Iwaid4156932013-01-07 10:08:02 +01005513 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005514 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005515}
5516
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005517
Takashi Iwai2c12c302013-01-10 09:33:29 +01005518static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005519{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005520 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005521
Takashi Iwaid4156932013-01-07 10:08:02 +01005522 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005523 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005524}
5525
5526/* initialize hp and speaker paths */
5527static void init_extra_out(struct hda_codec *codec)
5528{
5529 struct hda_gen_spec *spec = codec->spec;
5530
5531 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005532 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005533 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5534 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005535 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005536}
5537
5538/* initialize multi-io paths */
5539static void init_multi_io(struct hda_codec *codec)
5540{
5541 struct hda_gen_spec *spec = codec->spec;
5542 int i;
5543
5544 for (i = 0; i < spec->multi_ios; i++) {
5545 hda_nid_t pin = spec->multi_io[i].pin;
5546 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005547 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005548 if (!path)
5549 continue;
5550 if (!spec->multi_io[i].ctl_in)
5551 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005552 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005553 snd_hda_activate_path(codec, path, path->active,
5554 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005555 }
5556}
5557
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005558static void init_aamix_paths(struct hda_codec *codec)
5559{
5560 struct hda_gen_spec *spec = codec->spec;
5561
5562 if (!spec->have_aamix_ctl)
5563 return;
5564 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5565 spec->aamix_out_paths[0],
5566 spec->autocfg.line_out_type);
5567 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5568 spec->aamix_out_paths[1],
5569 AUTO_PIN_HP_OUT);
5570 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5571 spec->aamix_out_paths[2],
5572 AUTO_PIN_SPEAKER_OUT);
5573}
5574
Takashi Iwai352f7f92012-12-19 12:52:06 +01005575/* set up input pins and loopback paths */
5576static void init_analog_input(struct hda_codec *codec)
5577{
5578 struct hda_gen_spec *spec = codec->spec;
5579 struct auto_pin_cfg *cfg = &spec->autocfg;
5580 int i;
5581
5582 for (i = 0; i < cfg->num_inputs; i++) {
5583 hda_nid_t nid = cfg->inputs[i].pin;
5584 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005585 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005586
5587 /* init loopback inputs */
5588 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005589 resume_path_from_idx(codec, spec->loopback_paths[i]);
5590 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005591 }
5592 }
5593}
5594
5595/* initialize ADC paths */
5596static void init_input_src(struct hda_codec *codec)
5597{
5598 struct hda_gen_spec *spec = codec->spec;
5599 struct hda_input_mux *imux = &spec->input_mux;
5600 struct nid_path *path;
5601 int i, c, nums;
5602
5603 if (spec->dyn_adc_switch)
5604 nums = 1;
5605 else
5606 nums = spec->num_adc_nids;
5607
5608 for (c = 0; c < nums; c++) {
5609 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005610 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005611 if (path) {
5612 bool active = path->active;
5613 if (i == spec->cur_mux[c])
5614 active = true;
5615 snd_hda_activate_path(codec, path, active, false);
5616 }
5617 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005618 if (spec->hp_mic)
5619 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005620 }
5621
Takashi Iwai352f7f92012-12-19 12:52:06 +01005622 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005623 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005624}
5625
5626/* set right pin controls for digital I/O */
5627static void init_digital(struct hda_codec *codec)
5628{
5629 struct hda_gen_spec *spec = codec->spec;
5630 int i;
5631 hda_nid_t pin;
5632
Takashi Iwaid4156932013-01-07 10:08:02 +01005633 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005634 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005635 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005636 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005637 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005638 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005639 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005640}
5641
Takashi Iwai973e4972012-12-20 15:16:09 +01005642/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5643 * invalid unsol tags by some reason
5644 */
5645static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5646{
5647 int i;
5648
5649 for (i = 0; i < codec->init_pins.used; i++) {
5650 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5651 hda_nid_t nid = pin->nid;
5652 if (is_jack_detectable(codec, nid) &&
5653 !snd_hda_jack_tbl_get(codec, nid))
5654 snd_hda_codec_update_cache(codec, nid, 0,
5655 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5656 }
5657}
5658
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005659/**
5660 * snd_hda_gen_init - initialize the generic spec
5661 * @codec: the HDA codec
5662 *
5663 * This can be put as patch_ops init function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005664 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005665int snd_hda_gen_init(struct hda_codec *codec)
5666{
5667 struct hda_gen_spec *spec = codec->spec;
5668
5669 if (spec->init_hook)
5670 spec->init_hook(codec);
5671
5672 snd_hda_apply_verbs(codec);
5673
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005674 codec->cached_write = 1;
5675
Takashi Iwai352f7f92012-12-19 12:52:06 +01005676 init_multi_out(codec);
5677 init_extra_out(codec);
5678 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005679 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005680 init_analog_input(codec);
5681 init_input_src(codec);
5682 init_digital(codec);
5683
Takashi Iwai973e4972012-12-20 15:16:09 +01005684 clear_unsol_on_unused_pins(codec);
5685
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01005686 sync_all_pin_power_ctls(codec);
5687
Takashi Iwai352f7f92012-12-19 12:52:06 +01005688 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005689 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005690
Takashi Iwaidc870f32013-01-22 15:24:30 +01005691 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005692
Takashi Iwai352f7f92012-12-19 12:52:06 +01005693 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5694 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5695
5696 hda_call_check_power_status(codec, 0x01);
5697 return 0;
5698}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005699EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005700
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005701/**
5702 * snd_hda_gen_free - free the generic spec
5703 * @codec: the HDA codec
5704 *
5705 * This can be put as patch_ops free function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005706 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005707void snd_hda_gen_free(struct hda_codec *codec)
5708{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01005709 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005710 snd_hda_gen_spec_free(codec->spec);
5711 kfree(codec->spec);
5712 codec->spec = NULL;
5713}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005714EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005715
5716#ifdef CONFIG_PM
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005717/**
5718 * snd_hda_gen_check_power_status - check the loopback power save state
5719 * @codec: the HDA codec
5720 * @nid: NID to inspect
5721 *
5722 * This can be put as patch_ops check_power_status function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005723 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005724int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5725{
5726 struct hda_gen_spec *spec = codec->spec;
5727 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5728}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005729EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005730#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005731
5732
5733/*
5734 * the generic codec support
5735 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736
Takashi Iwai352f7f92012-12-19 12:52:06 +01005737static const struct hda_codec_ops generic_patch_ops = {
5738 .build_controls = snd_hda_gen_build_controls,
5739 .build_pcms = snd_hda_gen_build_pcms,
5740 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005741 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005742 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005743#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005744 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005745#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746};
5747
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005748/*
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005749 * snd_hda_parse_generic_codec - Generic codec parser
5750 * @codec: the HDA codec
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005751 */
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005752static int snd_hda_parse_generic_codec(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005754 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 int err;
5756
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005757 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005758 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005760 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005763 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5764 if (err < 0)
5765 return err;
5766
5767 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005768 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 goto error;
5770
5771 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 return 0;
5773
Takashi Iwai352f7f92012-12-19 12:52:06 +01005774error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005775 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 return err;
5777}
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005778
5779static const struct hda_codec_preset snd_hda_preset_generic[] = {
5780 { .id = HDA_CODEC_ID_GENERIC, .patch = snd_hda_parse_generic_codec },
5781 {} /* terminator */
5782};
5783
5784static struct hda_codec_driver generic_driver = {
5785 .preset = snd_hda_preset_generic,
5786};
5787
5788module_hda_codec_driver(generic_driver);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01005789
5790MODULE_LICENSE("GPL");
5791MODULE_DESCRIPTION("Generic HD-audio codec parser");