blob: 6815f9dc8545d42ddbe25375ae3a08f1d976ddaf [file] [log] [blame]
Thomas Gleixnerd0fa1172019-05-20 19:07:57 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Universal Interface for Intel High Definition Audio Codec
4 *
5 * Generic widget tree parser
6 *
7 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/init.h>
11#include <linux/slab.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040012#include <linux/export.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010013#include <linux/sort.h>
Takashi Iwai55196ff2013-01-24 17:32:56 +010014#include <linux/delay.h>
Takashi Iwaif873e532012-12-20 16:58:39 +010015#include <linux/ctype.h>
16#include <linux/string.h>
Takashi Iwai294765582013-01-17 09:52:11 +010017#include <linux/bitops.h>
Takashi Iwaib21bdd02013-11-18 12:03:56 +010018#include <linux/module.h>
Takashi Iwaib3802782018-11-26 17:47:46 +010019#include <linux/leds.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010021#include <sound/jack.h>
Takashi Iwaid89c6c02014-09-01 10:07:04 +020022#include <sound/tlv.h>
Pierre-Louis Bossartbe57bff2018-08-22 15:24:57 -050023#include <sound/hda_codec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010025#include "hda_auto_parser.h"
26#include "hda_jack.h"
Takashi Iwai7504b6c2013-03-18 11:25:51 +010027#include "hda_beep.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010028#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Takashi Iwaidda42bd2014-10-30 12:04:50 +010031/**
32 * snd_hda_gen_spec_init - initialize hda_gen_spec struct
33 * @spec: hda_gen_spec object to initialize
34 *
35 * Initialize the given hda_gen_spec object.
36 */
Takashi Iwai352f7f92012-12-19 12:52:06 +010037int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Takashi Iwai352f7f92012-12-19 12:52:06 +010039 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010040 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010041 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010042 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010043 return 0;
44}
Takashi Iwai2698ea92013-12-18 07:45:52 +010045EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Takashi Iwaidda42bd2014-10-30 12:04:50 +010047/**
48 * snd_hda_gen_add_kctl - Add a new kctl_new struct from the template
49 * @spec: hda_gen_spec object
50 * @name: name string to override the template, NULL if unchanged
51 * @temp: template for the new kctl
52 *
53 * Add a new kctl (actually snd_kcontrol_new to be instantiated later)
54 * element based on the given snd_kcontrol_new template @temp and the
55 * name string @name to the list in @spec.
56 * Returns the newly created object or NULL as error.
57 */
Takashi Iwai12c93df2012-12-19 14:38:33 +010058struct snd_kcontrol_new *
59snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
60 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010061{
62 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
63 if (!knew)
64 return NULL;
65 *knew = *temp;
66 if (name)
67 knew->name = kstrdup(name, GFP_KERNEL);
68 else if (knew->name)
69 knew->name = kstrdup(knew->name, GFP_KERNEL);
70 if (!knew->name)
71 return NULL;
72 return knew;
73}
Takashi Iwai2698ea92013-12-18 07:45:52 +010074EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010075
76static void free_kctls(struct hda_gen_spec *spec)
77{
78 if (spec->kctls.list) {
79 struct snd_kcontrol_new *kctl = spec->kctls.list;
80 int i;
81 for (i = 0; i < spec->kctls.used; i++)
82 kfree(kctl[i].name);
83 }
84 snd_array_free(&spec->kctls);
85}
86
Takashi Iwaia8dca462014-02-10 18:23:57 +010087static void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
Takashi Iwai352f7f92012-12-19 12:52:06 +010088{
89 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010091 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010092 snd_array_free(&spec->paths);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010093 snd_array_free(&spec->loopback_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/*
Takashi Iwai1c70a582013-01-11 17:48:22 +010097 * store user hints
98 */
99static void parse_user_hints(struct hda_codec *codec)
100{
101 struct hda_gen_spec *spec = codec->spec;
102 int val;
103
104 val = snd_hda_get_bool_hint(codec, "jack_detect");
105 if (val >= 0)
106 codec->no_jack_detect = !val;
107 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
108 if (val >= 0)
109 codec->inv_jack_detect = !!val;
110 val = snd_hda_get_bool_hint(codec, "trigger_sense");
111 if (val >= 0)
112 codec->no_trigger_sense = !val;
113 val = snd_hda_get_bool_hint(codec, "inv_eapd");
114 if (val >= 0)
115 codec->inv_eapd = !!val;
116 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
117 if (val >= 0)
118 codec->pcm_format_first = !!val;
119 val = snd_hda_get_bool_hint(codec, "sticky_stream");
120 if (val >= 0)
121 codec->no_sticky_stream = !val;
122 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
123 if (val >= 0)
124 codec->spdif_status_reset = !!val;
125 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
126 if (val >= 0)
127 codec->pin_amp_workaround = !!val;
128 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
129 if (val >= 0)
130 codec->single_adc_amp = !!val;
Takashi Iwai967b1302015-03-20 18:21:03 +0100131 val = snd_hda_get_bool_hint(codec, "power_save_node");
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100132 if (val >= 0)
Takashi Iwai967b1302015-03-20 18:21:03 +0100133 codec->power_save_node = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100134
Takashi Iwaif72706b2013-01-16 18:20:07 +0100135 val = snd_hda_get_bool_hint(codec, "auto_mute");
136 if (val >= 0)
137 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100138 val = snd_hda_get_bool_hint(codec, "auto_mic");
139 if (val >= 0)
140 spec->suppress_auto_mic = !val;
141 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
142 if (val >= 0)
143 spec->line_in_auto_switch = !!val;
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200144 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
145 if (val >= 0)
146 spec->auto_mute_via_amp = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100147 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
148 if (val >= 0)
149 spec->need_dac_fix = !!val;
150 val = snd_hda_get_bool_hint(codec, "primary_hp");
151 if (val >= 0)
152 spec->no_primary_hp = !val;
Takashi Iwaida96fb52013-07-29 16:54:36 +0200153 val = snd_hda_get_bool_hint(codec, "multi_io");
154 if (val >= 0)
155 spec->no_multi_io = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100156 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
157 if (val >= 0)
158 spec->multi_cap_vol = !!val;
159 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
160 if (val >= 0)
161 spec->inv_dmic_split = !!val;
162 val = snd_hda_get_bool_hint(codec, "indep_hp");
163 if (val >= 0)
164 spec->indep_hp = !!val;
165 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
166 if (val >= 0)
167 spec->add_stereo_mix_input = !!val;
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100168 /* the following two are just for compatibility */
Takashi Iwai1c70a582013-01-11 17:48:22 +0100169 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
170 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100171 spec->add_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100172 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
173 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100174 spec->add_jack_modes = !!val;
175 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
176 if (val >= 0)
177 spec->add_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100178 val = snd_hda_get_bool_hint(codec, "power_down_unused");
179 if (val >= 0)
180 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100181 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
182 if (val >= 0)
183 spec->hp_mic = !!val;
184 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
185 if (val >= 0)
186 spec->suppress_hp_mic_detect = !val;
Takashi Iwai74803162017-04-10 17:37:34 +0200187 val = snd_hda_get_bool_hint(codec, "vmaster");
188 if (val >= 0)
189 spec->suppress_vmaster = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100190
191 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
192 spec->mixer_nid = val;
193}
194
195/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100196 * pin control value accesses
197 */
198
199#define update_pin_ctl(codec, pin, val) \
Takashi Iwai401caff2018-06-27 11:43:09 +0200200 snd_hda_codec_write_cache(codec, pin, 0, \
Takashi Iwai2c12c302013-01-10 09:33:29 +0100201 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
202
203/* restore the pinctl based on the cached value */
204static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
205{
206 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
207}
208
209/* set the pinctl target value and write it if requested */
210static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
211 unsigned int val, bool do_write)
212{
213 if (!pin)
214 return;
215 val = snd_hda_correct_pin_ctl(codec, pin, val);
216 snd_hda_codec_set_pin_target(codec, pin, val);
217 if (do_write)
218 update_pin_ctl(codec, pin, val);
219}
220
221/* set pinctl target values for all given pins */
222static void set_pin_targets(struct hda_codec *codec, int num_pins,
223 hda_nid_t *pins, unsigned int val)
224{
225 int i;
226 for (i = 0; i < num_pins; i++)
227 set_pin_target(codec, pins[i], val, false);
228}
229
230/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100231 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100234/* return the position of NID in the list, or -1 if not found */
235static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
236{
237 int i;
238 for (i = 0; i < nums; i++)
239 if (list[i] == nid)
240 return i;
241 return -1;
242}
243
244/* return true if the given NID is contained in the path */
245static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
246{
247 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
248}
249
Takashi Iwaif5172a72013-01-04 13:19:55 +0100250static struct nid_path *get_nid_path(struct hda_codec *codec,
251 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100252 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100254 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200255 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100256 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200258 snd_array_for_each(&spec->paths, i, path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100259 if (path->depth <= 0)
260 continue;
261 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100262 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100263 if (!anchor_nid ||
264 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
265 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100266 return path;
267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269 return NULL;
270}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100271
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100272/**
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100273 * snd_hda_get_path_idx - get the index number corresponding to the path
274 * instance
275 * @codec: the HDA codec
276 * @path: nid_path object
277 *
278 * The returned index starts from 1, i.e. the actual array index with offset 1,
279 * and zero is handled as an invalid path
Takashi Iwai196c17662013-01-04 15:01:40 +0100280 */
281int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
282{
283 struct hda_gen_spec *spec = codec->spec;
284 struct nid_path *array = spec->paths.list;
285 ssize_t idx;
286
287 if (!spec->paths.used)
288 return 0;
289 idx = path - array;
290 if (idx < 0 || idx >= spec->paths.used)
291 return 0;
292 return idx + 1;
293}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100294EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100295
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100296/**
297 * snd_hda_get_path_from_idx - get the path instance corresponding to the
298 * given index number
299 * @codec: the HDA codec
300 * @idx: the path index
301 */
Takashi Iwai196c17662013-01-04 15:01:40 +0100302struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
303{
304 struct hda_gen_spec *spec = codec->spec;
305
306 if (idx <= 0 || idx > spec->paths.used)
307 return NULL;
308 return snd_array_elem(&spec->paths, idx - 1);
309}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100310EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100311
Takashi Iwai352f7f92012-12-19 12:52:06 +0100312/* check whether the given DAC is already found in any existing paths */
313static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
314{
315 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200316 const struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100317 int i;
318
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200319 snd_array_for_each(&spec->paths, i, path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100320 if (path->path[0] == nid)
321 return true;
322 }
323 return false;
324}
325
326/* check whether the given two widgets can be connected */
327static bool is_reachable_path(struct hda_codec *codec,
328 hda_nid_t from_nid, hda_nid_t to_nid)
329{
330 if (!from_nid || !to_nid)
331 return false;
332 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
333}
334
335/* nid, dir and idx */
336#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
337
338/* check whether the given ctl is already assigned in any path elements */
339static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
340{
341 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200342 const struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100343 int i;
344
345 val &= AMP_VAL_COMPARE_MASK;
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200346 snd_array_for_each(&spec->paths, i, path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100347 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
348 return true;
349 }
350 return false;
351}
352
353/* check whether a control with the given (nid, dir, idx) was assigned */
354static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100355 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100356{
357 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100358 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100359}
360
Takashi Iwai4e76a882014-02-25 12:21:03 +0100361static void print_nid_path(struct hda_codec *codec,
362 const char *pfx, struct nid_path *path)
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100363{
364 char buf[40];
Joe Perchesd82353e2014-07-05 13:02:02 -0700365 char *pos = buf;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100366 int i;
367
Joe Perchesd82353e2014-07-05 13:02:02 -0700368 *pos = 0;
369 for (i = 0; i < path->depth; i++)
370 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
371 pos != buf ? ":" : "",
372 path->path[i]);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100373
Joe Perchesd82353e2014-07-05 13:02:02 -0700374 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100375}
376
Takashi Iwai352f7f92012-12-19 12:52:06 +0100377/* called recursively */
378static bool __parse_nid_path(struct hda_codec *codec,
379 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100380 int anchor_nid, struct nid_path *path,
381 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100382{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100383 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100384 int i, nums;
385
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100386 if (to_nid == anchor_nid)
387 anchor_nid = 0; /* anchor passed */
388 else if (to_nid == (hda_nid_t)(-anchor_nid))
389 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100390
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100391 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100392 for (i = 0; i < nums; i++) {
393 if (conn[i] != from_nid) {
394 /* special case: when from_nid is 0,
395 * try to find an empty DAC
396 */
397 if (from_nid ||
398 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
399 is_dac_already_used(codec, conn[i]))
400 continue;
401 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100402 /* anchor is not requested or already passed? */
403 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100404 goto found;
405 }
406 if (depth >= MAX_NID_PATH_DEPTH)
407 return false;
408 for (i = 0; i < nums; i++) {
409 unsigned int type;
410 type = get_wcaps_type(get_wcaps(codec, conn[i]));
411 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
412 type == AC_WID_PIN)
413 continue;
414 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100415 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100416 goto found;
417 }
418 return false;
419
420 found:
421 path->path[path->depth] = conn[i];
422 path->idx[path->depth + 1] = i;
423 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
424 path->multi[path->depth + 1] = 1;
425 path->depth++;
426 return true;
427}
428
Takashi Iwaic4a58c32015-12-08 11:48:39 +0100429/*
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100430 * snd_hda_parse_nid_path - parse the widget path from the given nid to
431 * the target nid
432 * @codec: the HDA codec
433 * @from_nid: the NID where the path start from
434 * @to_nid: the NID where the path ends at
435 * @anchor_nid: the anchor indication
436 * @path: the path object to store the result
437 *
438 * Returns true if a matching path is found.
439 *
440 * The parsing behavior depends on parameters:
Takashi Iwai352f7f92012-12-19 12:52:06 +0100441 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100442 * when @anchor_nid is set to a positive value, only paths through the widget
443 * with the given value are evaluated.
444 * when @anchor_nid is set to a negative value, paths through the widget
445 * with the negative of given value are excluded, only other paths are chosen.
446 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100447 */
Takashi Iwaic4a58c32015-12-08 11:48:39 +0100448static bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100449 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100450 struct nid_path *path)
451{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100452 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100453 path->path[path->depth] = to_nid;
454 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100455 return true;
456 }
457 return false;
458}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100460/**
461 * snd_hda_add_new_path - parse the path between the given NIDs and
462 * add to the path list
463 * @codec: the HDA codec
464 * @from_nid: the NID where the path start from
465 * @to_nid: the NID where the path ends at
466 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
467 *
468 * If no valid path is found, returns NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100470struct nid_path *
471snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100472 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100474 struct hda_gen_spec *spec = codec->spec;
475 struct nid_path *path;
476
477 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
478 return NULL;
479
Takashi Iwaif5172a72013-01-04 13:19:55 +0100480 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100481 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100482 if (path)
483 return path;
484
Takashi Iwai352f7f92012-12-19 12:52:06 +0100485 path = snd_array_new(&spec->paths);
486 if (!path)
487 return NULL;
488 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100489 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100490 return path;
491 /* push back */
492 spec->paths.used--;
493 return NULL;
494}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100495EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100496
Takashi Iwai980428c2013-01-09 09:28:20 +0100497/* clear the given path as invalid so that it won't be picked up later */
498static void invalidate_nid_path(struct hda_codec *codec, int idx)
499{
500 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
501 if (!path)
502 return;
503 memset(path, 0, sizeof(*path));
504}
505
Takashi Iwai36907392013-12-10 17:29:26 +0100506/* return a DAC if paired to the given pin by codec driver */
507static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
508{
509 struct hda_gen_spec *spec = codec->spec;
510 const hda_nid_t *list = spec->preferred_dacs;
511
512 if (!list)
513 return 0;
514 for (; *list; list += 2)
515 if (*list == pin)
516 return list[1];
517 return 0;
518}
519
Takashi Iwai352f7f92012-12-19 12:52:06 +0100520/* look for an empty DAC slot */
521static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
522 bool is_digital)
523{
524 struct hda_gen_spec *spec = codec->spec;
525 bool cap_digital;
526 int i;
527
528 for (i = 0; i < spec->num_all_dacs; i++) {
529 hda_nid_t nid = spec->all_dacs[i];
530 if (!nid || is_dac_already_used(codec, nid))
531 continue;
532 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
533 if (is_digital != cap_digital)
534 continue;
535 if (is_reachable_path(codec, nid, pin))
536 return nid;
537 }
538 return 0;
539}
540
541/* replace the channels in the composed amp value with the given number */
542static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
543{
544 val &= ~(0x3U << 16);
545 val |= chs << 16;
546 return val;
547}
548
David Henningsson99a55922013-01-16 15:58:44 +0100549static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
550 hda_nid_t nid2, int dir)
551{
552 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
553 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
554 return (query_amp_caps(codec, nid1, dir) ==
555 query_amp_caps(codec, nid2, dir));
556}
557
Takashi Iwai352f7f92012-12-19 12:52:06 +0100558/* look for a widget suitable for assigning a mute switch in the path */
559static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
560 struct nid_path *path)
561{
562 int i;
563
564 for (i = path->depth - 1; i >= 0; i--) {
565 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
566 return path->path[i];
567 if (i != path->depth - 1 && i != 0 &&
568 nid_has_mute(codec, path->path[i], HDA_INPUT))
569 return path->path[i];
570 }
571 return 0;
572}
573
574/* look for a widget suitable for assigning a volume ctl in the path */
575static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
576 struct nid_path *path)
577{
Takashi Iwaia1114a82013-11-04 16:32:01 +0100578 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100579 int i;
580
581 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwaia1114a82013-11-04 16:32:01 +0100582 hda_nid_t nid = path->path[i];
583 if ((spec->out_vol_mask >> nid) & 1)
584 continue;
585 if (nid_has_volume(codec, nid, HDA_OUTPUT))
586 return nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100587 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100592 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100594
595/* can have the amp-in capability? */
596static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100598 hda_nid_t nid = path->path[idx];
599 unsigned int caps = get_wcaps(codec, nid);
600 unsigned int type = get_wcaps_type(caps);
601
602 if (!(caps & AC_WCAP_IN_AMP))
603 return false;
604 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
605 return false;
606 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Takashi Iwai352f7f92012-12-19 12:52:06 +0100609/* can have the amp-out capability? */
610static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100612 hda_nid_t nid = path->path[idx];
613 unsigned int caps = get_wcaps(codec, nid);
614 unsigned int type = get_wcaps_type(caps);
615
616 if (!(caps & AC_WCAP_OUT_AMP))
617 return false;
618 if (type == AC_WID_PIN && !idx) /* only for output pins */
619 return false;
620 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Takashi Iwai352f7f92012-12-19 12:52:06 +0100623/* check whether the given (nid,dir,idx) is active */
624static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100625 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100627 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100628 int type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200629 const struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100630 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Takashi Iwai7639a062015-03-03 10:07:24 +0100632 if (nid == codec->core.afg)
Takashi Iwai5ccf8352015-03-18 09:23:10 +0100633 return true;
634
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200635 snd_array_for_each(&spec->paths, n, path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100636 if (!path->active)
637 continue;
Takashi Iwai967b1302015-03-20 18:21:03 +0100638 if (codec->power_save_node) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100639 if (!path->stream_enabled)
640 continue;
641 /* ignore unplugged paths except for DAC/ADC */
Takashi Iwai6b275b12015-03-20 18:11:05 +0100642 if (!(path->pin_enabled || path->pin_fixed) &&
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100643 type != AC_WID_AUD_OUT && type != AC_WID_AUD_IN)
644 continue;
645 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100646 for (i = 0; i < path->depth; i++) {
647 if (path->path[i] == nid) {
Takashi Iwai9d2b48f2015-08-24 10:45:27 +0200648 if (dir == HDA_OUTPUT || idx == -1 ||
649 path->idx[i] == idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100650 return true;
651 break;
652 }
653 }
654 }
655 return false;
656}
657
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200658/* check whether the NID is referred by any active paths */
659#define is_active_nid_for_any(codec, nid) \
Takashi Iwai9d2b48f2015-08-24 10:45:27 +0200660 is_active_nid(codec, nid, HDA_OUTPUT, -1)
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200661
Takashi Iwai352f7f92012-12-19 12:52:06 +0100662/* get the default amp value for the target state */
663static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100664 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100665{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100666 unsigned int val = 0;
667
Takashi Iwai352f7f92012-12-19 12:52:06 +0100668 if (caps & AC_AMPCAP_NUM_STEPS) {
669 /* set to 0dB */
670 if (enable)
671 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
672 }
Takashi Iwaif69910d2013-08-08 09:32:37 +0200673 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100674 if (!enable)
675 val |= HDA_AMP_MUTE;
676 }
677 return val;
678}
679
Takashi Iwaicc261732015-03-16 10:18:08 +0100680/* is this a stereo widget or a stereo-to-mono mix? */
681static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
682{
683 unsigned int wcaps = get_wcaps(codec, nid);
684 hda_nid_t conn;
685
686 if (wcaps & AC_WCAP_STEREO)
687 return true;
688 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
689 return false;
690 if (snd_hda_get_num_conns(codec, nid) != 1)
691 return false;
692 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
693 return false;
694 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
695}
696
Takashi Iwai352f7f92012-12-19 12:52:06 +0100697/* initialize the amp value (only at the first time) */
698static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
699{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100700 unsigned int caps = query_amp_caps(codec, nid, dir);
701 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwaief403ed2015-03-12 08:30:11 +0100702
Takashi Iwaicc261732015-03-16 10:18:08 +0100703 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100704 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
705 else
706 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
707}
708
709/* update the amp, doing in stereo or mono depending on NID */
710static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
711 unsigned int mask, unsigned int val)
712{
Takashi Iwaicc261732015-03-16 10:18:08 +0100713 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100714 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
715 mask, val);
716 else
717 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
718 mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100719}
720
Takashi Iwai8999bf02013-01-18 11:01:33 +0100721/* calculate amp value mask we can modify;
722 * if the given amp is controlled by mixers, don't touch it
723 */
724static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
725 hda_nid_t nid, int dir, int idx,
726 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100727{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100728 unsigned int mask = 0xff;
729
Takashi Iwaif69910d2013-08-08 09:32:37 +0200730 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100731 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
732 mask &= ~0x80;
733 }
734 if (caps & AC_AMPCAP_NUM_STEPS) {
735 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
736 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
737 mask &= ~0x7f;
738 }
739 return mask;
740}
741
742static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
743 int idx, int idx_to_check, bool enable)
744{
745 unsigned int caps;
746 unsigned int mask, val;
747
Takashi Iwai8999bf02013-01-18 11:01:33 +0100748 caps = query_amp_caps(codec, nid, dir);
749 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
750 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
751 if (!mask)
752 return;
753
754 val &= mask;
Takashi Iwaief403ed2015-03-12 08:30:11 +0100755 update_amp(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100756}
757
Takashi Iwaie7fdd522015-12-08 17:00:42 +0100758static void check_and_activate_amp(struct hda_codec *codec, hda_nid_t nid,
759 int dir, int idx, int idx_to_check,
760 bool enable)
761{
762 /* check whether the given amp is still used by others */
763 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
764 return;
765 activate_amp(codec, nid, dir, idx, idx_to_check, enable);
766}
767
Takashi Iwai352f7f92012-12-19 12:52:06 +0100768static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
769 int i, bool enable)
770{
771 hda_nid_t nid = path->path[i];
772 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwaie7fdd522015-12-08 17:00:42 +0100773 check_and_activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100774}
775
776static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
777 int i, bool enable, bool add_aamix)
778{
779 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100780 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100781 int n, nums, idx;
782 int type;
783 hda_nid_t nid = path->path[i];
784
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100785 nums = snd_hda_get_conn_list(codec, nid, &conn);
Dan Carpenter0de7d832017-10-13 13:57:10 +0300786 if (nums < 0)
787 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100788 type = get_wcaps_type(get_wcaps(codec, nid));
789 if (type == AC_WID_PIN ||
790 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
791 nums = 1;
792 idx = 0;
793 } else
794 idx = path->idx[i];
795
796 for (n = 0; n < nums; n++)
797 init_amp(codec, nid, HDA_INPUT, n);
798
Takashi Iwai352f7f92012-12-19 12:52:06 +0100799 /* here is a little bit tricky in comparison with activate_amp_out();
800 * when aa-mixer is available, we need to enable the path as well
801 */
802 for (n = 0; n < nums; n++) {
Takashi Iwaie7fdd522015-12-08 17:00:42 +0100803 if (n != idx) {
804 if (conn[n] != spec->mixer_merge_nid)
805 continue;
806 /* when aamix is disabled, force to off */
807 if (!add_aamix) {
808 activate_amp(codec, nid, HDA_INPUT, n, n, false);
809 continue;
810 }
811 }
812 check_and_activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814}
815
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100816/* sync power of each widget in the the given path */
817static hda_nid_t path_power_update(struct hda_codec *codec,
818 struct nid_path *path,
819 bool allow_powerdown)
820{
821 hda_nid_t nid, changed = 0;
Takashi Iwai50fd4982016-04-17 09:39:41 +0200822 int i, state, power;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100823
824 for (i = 0; i < path->depth; i++) {
825 nid = path->path[i];
Takashi Iwai2206dc92015-04-09 10:18:31 +0200826 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
827 continue;
Takashi Iwai7639a062015-03-03 10:07:24 +0100828 if (nid == codec->core.afg)
Takashi Iwai5ccf8352015-03-18 09:23:10 +0100829 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100830 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
831 state = AC_PWRST_D0;
832 else
833 state = AC_PWRST_D3;
Takashi Iwai50fd4982016-04-17 09:39:41 +0200834 power = snd_hda_codec_read(codec, nid, 0,
835 AC_VERB_GET_POWER_STATE, 0);
836 if (power != (state | (state << 4))) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100837 snd_hda_codec_write(codec, nid, 0,
838 AC_VERB_SET_POWER_STATE, state);
839 changed = nid;
Takashi Iwai48f4b3a2015-05-20 06:49:37 +0200840 /* all known codecs seem to be capable to handl
841 * widgets state even in D3, so far.
842 * if any new codecs need to restore the widget
843 * states after D0 transition, call the function
844 * below.
845 */
846#if 0 /* disabled */
Takashi Iwaid545a572015-04-04 12:17:28 +0200847 if (state == AC_PWRST_D0)
848 snd_hdac_regmap_sync_node(&codec->core, nid);
Takashi Iwai48f4b3a2015-05-20 06:49:37 +0200849#endif
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100850 }
851 }
852 return changed;
853}
854
855/* do sync with the last power state change */
856static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
857{
858 if (nid) {
859 msleep(10);
860 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
861 }
862}
863
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100864/**
865 * snd_hda_activate_path - activate or deactivate the given path
866 * @codec: the HDA codec
867 * @path: the path to activate/deactivate
868 * @enable: flag to activate or not
869 * @add_aamix: enable the input from aamix NID
870 *
871 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100873void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
874 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100876 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100877 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Takashi Iwaic7cd0ef2015-08-24 10:52:06 +0200879 path->active = enable;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100880
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100881 /* make sure the widget is powered up */
Takashi Iwai967b1302015-03-20 18:21:03 +0100882 if (enable && (spec->power_down_unused || codec->power_save_node))
883 path_power_update(codec, path, codec->power_save_node);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100884
Takashi Iwai352f7f92012-12-19 12:52:06 +0100885 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100886 hda_nid_t nid = path->path[i];
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100887
Takashi Iwai352f7f92012-12-19 12:52:06 +0100888 if (enable && path->multi[i])
Takashi Iwai401caff2018-06-27 11:43:09 +0200889 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100890 AC_VERB_SET_CONNECT_SEL,
891 path->idx[i]);
892 if (has_amp_in(codec, path, i))
893 activate_amp_in(codec, path, i, enable, add_aamix);
894 if (has_amp_out(codec, path, i))
895 activate_amp_out(codec, path, i, enable);
896 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100897}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100898EXPORT_SYMBOL_GPL(snd_hda_activate_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100899
Takashi Iwai55196ff2013-01-24 17:32:56 +0100900/* if the given path is inactive, put widgets into D3 (only if suitable) */
901static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
902{
903 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100904
Takashi Iwai967b1302015-03-20 18:21:03 +0100905 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
Takashi Iwai55196ff2013-01-24 17:32:56 +0100906 return;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100907 sync_power_state_change(codec, path_power_update(codec, path, true));
Takashi Iwai55196ff2013-01-24 17:32:56 +0100908}
909
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100910/* turn on/off EAPD on the given pin */
911static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
912{
913 struct hda_gen_spec *spec = codec->spec;
914 if (spec->own_eapd_ctl ||
915 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
916 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200917 if (spec->keep_eapd_on && !enable)
918 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100919 if (codec->inv_eapd)
920 enable = !enable;
Takashi Iwai401caff2018-06-27 11:43:09 +0200921 snd_hda_codec_write_cache(codec, pin, 0,
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100922 AC_VERB_SET_EAPD_BTLENABLE,
923 enable ? 0x02 : 0x00);
924}
925
Takashi Iwai3e367f12013-01-23 17:07:23 +0100926/* re-initialize the path specified by the given path index */
927static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
928{
929 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
930 if (path)
931 snd_hda_activate_path(codec, path, path->active, false);
932}
933
Takashi Iwai352f7f92012-12-19 12:52:06 +0100934
935/*
936 * Helper functions for creating mixer ctl elements
937 */
938
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200939static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
940 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai698f5ee2017-05-10 12:13:45 +0200941static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
942 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200943static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
944 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200945
Takashi Iwai352f7f92012-12-19 12:52:06 +0100946enum {
947 HDA_CTL_WIDGET_VOL,
948 HDA_CTL_WIDGET_MUTE,
949 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100950};
951static const struct snd_kcontrol_new control_templates[] = {
952 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200953 /* only the put callback is replaced for handling the special mute */
954 {
955 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
956 .subdevice = HDA_SUBDEV_AMP_FLAG,
957 .info = snd_hda_mixer_amp_switch_info,
958 .get = snd_hda_mixer_amp_switch_get,
959 .put = hda_gen_mixer_mute_put, /* replaced */
960 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
961 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200962 {
963 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
964 .info = snd_hda_mixer_amp_switch_info,
Takashi Iwai698f5ee2017-05-10 12:13:45 +0200965 .get = hda_gen_bind_mute_get,
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200966 .put = hda_gen_bind_mute_put, /* replaced */
967 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
968 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100969};
970
971/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100972static struct snd_kcontrol_new *
973add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100974 int cidx, unsigned long val)
975{
976 struct snd_kcontrol_new *knew;
977
Takashi Iwai12c93df2012-12-19 14:38:33 +0100978 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100979 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100980 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100981 knew->index = cidx;
982 if (get_amp_nid_(val))
983 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
984 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100985 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100986}
987
988static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
989 const char *pfx, const char *dir,
990 const char *sfx, int cidx, unsigned long val)
991{
Takashi Iwai975cc022013-06-28 11:56:49 +0200992 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100993 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100994 if (!add_control(spec, type, name, cidx, val))
995 return -ENOMEM;
996 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100997}
998
999#define add_pb_vol_ctrl(spec, type, pfx, val) \
1000 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1001#define add_pb_sw_ctrl(spec, type, pfx, val) \
1002 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1003#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1004 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1005#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1006 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1007
1008static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1009 unsigned int chs, struct nid_path *path)
1010{
1011 unsigned int val;
1012 if (!path)
1013 return 0;
1014 val = path->ctls[NID_PATH_VOL_CTL];
1015 if (!val)
1016 return 0;
1017 val = amp_val_replace_channels(val, chs);
1018 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1019}
1020
1021/* return the channel bits suitable for the given path->ctls[] */
1022static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1023 int type)
1024{
1025 int chs = 1; /* mono (left only) */
1026 if (path) {
1027 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1028 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1029 chs = 3; /* stereo */
1030 }
1031 return chs;
1032}
1033
1034static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1035 struct nid_path *path)
1036{
1037 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1038 return add_vol_ctl(codec, pfx, cidx, chs, path);
1039}
1040
1041/* create a mute-switch for the given mixer widget;
1042 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1043 */
1044static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1045 unsigned int chs, struct nid_path *path)
1046{
1047 unsigned int val;
1048 int type = HDA_CTL_WIDGET_MUTE;
1049
1050 if (!path)
1051 return 0;
1052 val = path->ctls[NID_PATH_MUTE_CTL];
1053 if (!val)
1054 return 0;
1055 val = amp_val_replace_channels(val, chs);
1056 if (get_amp_direction_(val) == HDA_INPUT) {
1057 hda_nid_t nid = get_amp_nid_(val);
1058 int nums = snd_hda_get_num_conns(codec, nid);
1059 if (nums > 1) {
1060 type = HDA_CTL_BIND_MUTE;
1061 val |= nums << 19;
1062 }
1063 }
1064 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1065}
1066
1067static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1068 int cidx, struct nid_path *path)
1069{
1070 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1071 return add_sw_ctl(codec, pfx, cidx, chs, path);
1072}
1073
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001074/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001075static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1076 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001077{
1078 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1079 struct hda_gen_spec *spec = codec->spec;
1080
1081 if (spec->auto_mute_via_amp) {
1082 hda_nid_t nid = get_amp_nid(kcontrol);
1083 bool enabled = !((spec->mute_bits >> nid) & 1);
1084 ucontrol->value.integer.value[0] &= enabled;
1085 ucontrol->value.integer.value[1] &= enabled;
1086 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001087}
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001088
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001089static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1090 struct snd_ctl_elem_value *ucontrol)
1091{
1092 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001093 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1094}
1095
Takashi Iwai698f5ee2017-05-10 12:13:45 +02001096/*
1097 * Bound mute controls
1098 */
1099#define AMP_VAL_IDX_SHIFT 19
1100#define AMP_VAL_IDX_MASK (0x0f<<19)
1101
1102static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
1103 struct snd_ctl_elem_value *ucontrol)
1104{
1105 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1106 unsigned long pval;
1107 int err;
1108
1109 mutex_lock(&codec->control_mutex);
1110 pval = kcontrol->private_value;
1111 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1112 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1113 kcontrol->private_value = pval;
1114 mutex_unlock(&codec->control_mutex);
1115 return err;
1116}
1117
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001118static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1119 struct snd_ctl_elem_value *ucontrol)
1120{
Takashi Iwai698f5ee2017-05-10 12:13:45 +02001121 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1122 unsigned long pval;
1123 int i, indices, err = 0, change = 0;
1124
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001125 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai698f5ee2017-05-10 12:13:45 +02001126
1127 mutex_lock(&codec->control_mutex);
1128 pval = kcontrol->private_value;
1129 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1130 for (i = 0; i < indices; i++) {
1131 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1132 (i << AMP_VAL_IDX_SHIFT);
1133 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1134 if (err < 0)
1135 break;
1136 change |= err;
1137 }
1138 kcontrol->private_value = pval;
1139 mutex_unlock(&codec->control_mutex);
1140 return err < 0 ? err : change;
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001141}
1142
Takashi Iwai247d85e2013-01-17 16:18:11 +01001143/* any ctl assigned to the path with the given index? */
1144static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1145{
1146 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1147 return path && path->ctls[ctl_type];
1148}
1149
Takashi Iwai352f7f92012-12-19 12:52:06 +01001150static const char * const channel_name[4] = {
1151 "Front", "Surround", "CLFE", "Side"
1152};
1153
1154/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +01001155static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1156 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001157{
Takashi Iwai247d85e2013-01-17 16:18:11 +01001158 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001159 struct auto_pin_cfg *cfg = &spec->autocfg;
1160
1161 *index = 0;
1162 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai9f3dadb2017-04-10 17:12:33 +02001163 !codec->force_pin_prefix &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001164 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001165 return spec->vmaster_mute.hook ? "PCM" : "Master";
1166
1167 /* if there is really a single DAC used in the whole output paths,
1168 * use it master (or "PCM" if a vmaster hook is present)
1169 */
1170 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
Takashi Iwai9f3dadb2017-04-10 17:12:33 +02001171 !codec->force_pin_prefix &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001172 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1173 return spec->vmaster_mute.hook ? "PCM" : "Master";
1174
Takashi Iwai247d85e2013-01-17 16:18:11 +01001175 /* multi-io channels */
1176 if (ch >= cfg->line_outs)
1177 return channel_name[ch];
1178
Takashi Iwai352f7f92012-12-19 12:52:06 +01001179 switch (cfg->line_out_type) {
1180 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001181 /* if the primary channel vol/mute is shared with HP volume,
1182 * don't name it as Speaker
1183 */
1184 if (!ch && cfg->hp_outs &&
1185 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1186 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001187 if (cfg->line_outs == 1)
1188 return "Speaker";
1189 if (cfg->line_outs == 2)
1190 return ch ? "Bass Speaker" : "Speaker";
1191 break;
1192 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001193 /* if the primary channel vol/mute is shared with spk volume,
1194 * don't name it as Headphone
1195 */
1196 if (!ch && cfg->speaker_outs &&
1197 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1198 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001199 /* for multi-io case, only the primary out */
1200 if (ch && spec->multi_ios)
1201 break;
1202 *index = ch;
1203 return "Headphone";
David Henningsson03ad6a82014-10-16 15:33:45 +02001204 case AUTO_PIN_LINE_OUT:
1205 /* This deals with the case where we have two DACs and
1206 * one LO, one HP and one Speaker */
1207 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1208 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1209 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1210 if (hp_lo_shared && spk_lo_shared)
1211 return spec->vmaster_mute.hook ? "PCM" : "Master";
1212 if (hp_lo_shared)
1213 return "Headphone+LO";
1214 if (spk_lo_shared)
1215 return "Speaker+LO";
1216 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001217 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001218
1219 /* for a single channel output, we don't have to name the channel */
1220 if (cfg->line_outs == 1 && !spec->multi_ios)
David Henningsson3abb4f42014-10-16 15:33:46 +02001221 return "Line Out";
Takashi Iwai247d85e2013-01-17 16:18:11 +01001222
Takashi Iwai352f7f92012-12-19 12:52:06 +01001223 if (ch >= ARRAY_SIZE(channel_name)) {
1224 snd_BUG();
1225 return "PCM";
1226 }
1227
1228 return channel_name[ch];
1229}
1230
1231/*
1232 * Parse output paths
1233 */
1234
1235/* badness definition */
1236enum {
1237 /* No primary DAC is found for the main output */
1238 BAD_NO_PRIMARY_DAC = 0x10000,
1239 /* No DAC is found for the extra output */
1240 BAD_NO_DAC = 0x4000,
1241 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001242 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001243 /* No individual DAC for extra output */
1244 BAD_NO_EXTRA_DAC = 0x102,
1245 /* No individual DAC for extra surrounds */
1246 BAD_NO_EXTRA_SURR_DAC = 0x101,
1247 /* Primary DAC shared with main surrounds */
1248 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001249 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001250 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001251 /* Primary DAC shared with main CLFE */
1252 BAD_SHARED_CLFE = 0x10,
1253 /* Primary DAC shared with extra surrounds */
1254 BAD_SHARED_EXTRA_SURROUND = 0x10,
1255 /* Volume widget is shared */
1256 BAD_SHARED_VOL = 0x10,
1257};
1258
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001259/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001260 * volume and mute controls, and assign the values to ctls[].
1261 *
1262 * When no appropriate widget is found in the path, the badness value
1263 * is incremented depending on the situation. The function returns the
1264 * total badness for both volume and mute controls.
1265 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001266static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001267{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001268 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001269 hda_nid_t nid;
1270 unsigned int val;
1271 int badness = 0;
1272
1273 if (!path)
1274 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001275
1276 if (path->ctls[NID_PATH_VOL_CTL] ||
1277 path->ctls[NID_PATH_MUTE_CTL])
1278 return 0; /* already evaluated */
1279
Takashi Iwai352f7f92012-12-19 12:52:06 +01001280 nid = look_for_out_vol_nid(codec, path);
1281 if (nid) {
1282 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001283 if (spec->dac_min_mute)
1284 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001285 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1286 badness += BAD_SHARED_VOL;
1287 else
1288 path->ctls[NID_PATH_VOL_CTL] = val;
1289 } else
1290 badness += BAD_SHARED_VOL;
1291 nid = look_for_out_mute_nid(codec, path);
1292 if (nid) {
1293 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1294 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1295 nid_has_mute(codec, nid, HDA_OUTPUT))
1296 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1297 else
1298 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1299 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1300 badness += BAD_SHARED_VOL;
1301 else
1302 path->ctls[NID_PATH_MUTE_CTL] = val;
1303 } else
1304 badness += BAD_SHARED_VOL;
1305 return badness;
1306}
1307
Takashi Iwai98bd1112013-03-22 14:53:50 +01001308const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001309 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1310 .no_dac = BAD_NO_DAC,
1311 .shared_primary = BAD_NO_PRIMARY_DAC,
1312 .shared_surr = BAD_SHARED_SURROUND,
1313 .shared_clfe = BAD_SHARED_CLFE,
1314 .shared_surr_main = BAD_SHARED_SURROUND,
1315};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001316EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001317
Takashi Iwai98bd1112013-03-22 14:53:50 +01001318const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001319 .no_primary_dac = BAD_NO_DAC,
1320 .no_dac = BAD_NO_DAC,
1321 .shared_primary = BAD_NO_EXTRA_DAC,
1322 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1323 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1324 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1325};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001326EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001327
Takashi Iwai7385df62013-01-07 09:50:52 +01001328/* get the DAC of the primary output corresponding to the given array index */
1329static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1330{
1331 struct hda_gen_spec *spec = codec->spec;
1332 struct auto_pin_cfg *cfg = &spec->autocfg;
1333
1334 if (cfg->line_outs > idx)
1335 return spec->private_dac_nids[idx];
1336 idx -= cfg->line_outs;
1337 if (spec->multi_ios > idx)
1338 return spec->multi_io[idx].dac;
1339 return 0;
1340}
1341
1342/* return the DAC if it's reachable, otherwise zero */
1343static inline hda_nid_t try_dac(struct hda_codec *codec,
1344 hda_nid_t dac, hda_nid_t pin)
1345{
1346 return is_reachable_path(codec, dac, pin) ? dac : 0;
1347}
1348
Takashi Iwai352f7f92012-12-19 12:52:06 +01001349/* try to assign DACs to pins and return the resultant badness */
1350static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1351 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001352 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001353 const struct badness_table *bad)
1354{
1355 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001356 int i, j;
1357 int badness = 0;
1358 hda_nid_t dac;
1359
1360 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return 0;
1362
Takashi Iwai352f7f92012-12-19 12:52:06 +01001363 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001364 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001365 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001366
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001367 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1368 if (path) {
1369 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001370 continue;
1371 }
1372
Takashi Iwai36907392013-12-10 17:29:26 +01001373 dacs[i] = get_preferred_dac(codec, pin);
1374 if (dacs[i]) {
1375 if (is_dac_already_used(codec, dacs[i]))
1376 badness += bad->shared_primary;
1377 }
1378
1379 if (!dacs[i])
1380 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001381 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001382 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001383 for (j = 1; j < num_outs; j++) {
1384 if (is_reachable_path(codec, dacs[j], pin)) {
1385 dacs[0] = dacs[j];
1386 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001387 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001388 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001389 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001392 }
1393 dac = dacs[i];
1394 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001395 if (num_outs > 2)
1396 dac = try_dac(codec, get_primary_out(codec, i), pin);
1397 if (!dac)
1398 dac = try_dac(codec, dacs[0], pin);
1399 if (!dac)
1400 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001401 if (dac) {
1402 if (!i)
1403 badness += bad->shared_primary;
1404 else if (i == 1)
1405 badness += bad->shared_surr;
1406 else
1407 badness += bad->shared_clfe;
1408 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1409 dac = spec->private_dac_nids[0];
1410 badness += bad->shared_surr_main;
1411 } else if (!i)
1412 badness += bad->no_primary_dac;
1413 else
1414 badness += bad->no_dac;
1415 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001416 if (!dac)
1417 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001418 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001419 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001420 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001421 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001422 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001423 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001424 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001425 badness += bad->no_dac;
1426 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001427 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001428 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001429 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001430 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001431 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001432 }
1433
1434 return badness;
1435}
1436
1437/* return NID if the given pin has only a single connection to a certain DAC */
1438static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1439{
1440 struct hda_gen_spec *spec = codec->spec;
1441 int i;
1442 hda_nid_t nid_found = 0;
1443
1444 for (i = 0; i < spec->num_all_dacs; i++) {
1445 hda_nid_t nid = spec->all_dacs[i];
1446 if (!nid || is_dac_already_used(codec, nid))
1447 continue;
1448 if (is_reachable_path(codec, nid, pin)) {
1449 if (nid_found)
1450 return 0;
1451 nid_found = nid;
1452 }
1453 }
1454 return nid_found;
1455}
1456
1457/* check whether the given pin can be a multi-io pin */
1458static bool can_be_multiio_pin(struct hda_codec *codec,
1459 unsigned int location, hda_nid_t nid)
1460{
1461 unsigned int defcfg, caps;
1462
1463 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1464 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1465 return false;
1466 if (location && get_defcfg_location(defcfg) != location)
1467 return false;
1468 caps = snd_hda_query_pin_caps(codec, nid);
1469 if (!(caps & AC_PINCAP_OUT))
1470 return false;
1471 return true;
1472}
1473
Takashi Iwaie22aab72013-01-04 14:50:04 +01001474/* count the number of input pins that are capable to be multi-io */
1475static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1476{
1477 struct hda_gen_spec *spec = codec->spec;
1478 struct auto_pin_cfg *cfg = &spec->autocfg;
1479 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1480 unsigned int location = get_defcfg_location(defcfg);
1481 int type, i;
1482 int num_pins = 0;
1483
1484 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1485 for (i = 0; i < cfg->num_inputs; i++) {
1486 if (cfg->inputs[i].type != type)
1487 continue;
1488 if (can_be_multiio_pin(codec, location,
1489 cfg->inputs[i].pin))
1490 num_pins++;
1491 }
1492 }
1493 return num_pins;
1494}
1495
Takashi Iwai352f7f92012-12-19 12:52:06 +01001496/*
1497 * multi-io helper
1498 *
1499 * When hardwired is set, try to fill ony hardwired pins, and returns
1500 * zero if any pins are filled, non-zero if nothing found.
1501 * When hardwired is off, try to fill possible input pins, and returns
1502 * the badness value.
1503 */
1504static int fill_multi_ios(struct hda_codec *codec,
1505 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001506 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001507{
1508 struct hda_gen_spec *spec = codec->spec;
1509 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001510 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001511 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1512 unsigned int location = get_defcfg_location(defcfg);
1513 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001514 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001515
1516 old_pins = spec->multi_ios;
1517 if (old_pins >= 2)
1518 goto end_fill;
1519
Takashi Iwaie22aab72013-01-04 14:50:04 +01001520 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001521 if (num_pins < 2)
1522 goto end_fill;
1523
Takashi Iwai352f7f92012-12-19 12:52:06 +01001524 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1525 for (i = 0; i < cfg->num_inputs; i++) {
1526 hda_nid_t nid = cfg->inputs[i].pin;
1527 hda_nid_t dac = 0;
1528
1529 if (cfg->inputs[i].type != type)
1530 continue;
1531 if (!can_be_multiio_pin(codec, location, nid))
1532 continue;
1533 for (j = 0; j < spec->multi_ios; j++) {
1534 if (nid == spec->multi_io[j].pin)
1535 break;
1536 }
1537 if (j < spec->multi_ios)
1538 continue;
1539
Takashi Iwai352f7f92012-12-19 12:52:06 +01001540 if (hardwired)
1541 dac = get_dac_if_single(codec, nid);
1542 else if (!dac)
1543 dac = look_for_dac(codec, nid, false);
1544 if (!dac) {
1545 badness++;
1546 continue;
1547 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001548 path = snd_hda_add_new_path(codec, dac, nid,
1549 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001550 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001551 badness++;
1552 continue;
1553 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001554 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001555 spec->multi_io[spec->multi_ios].pin = nid;
1556 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001557 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1558 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001559 spec->multi_ios++;
1560 if (spec->multi_ios >= 2)
1561 break;
1562 }
1563 }
1564 end_fill:
1565 if (badness)
1566 badness = BAD_MULTI_IO;
1567 if (old_pins == spec->multi_ios) {
1568 if (hardwired)
1569 return 1; /* nothing found */
1570 else
1571 return badness; /* no badness if nothing found */
1572 }
1573 if (!hardwired && spec->multi_ios < 2) {
1574 /* cancel newly assigned paths */
1575 spec->paths.used -= spec->multi_ios - old_pins;
1576 spec->multi_ios = old_pins;
1577 return badness;
1578 }
1579
1580 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001581 for (i = old_pins; i < spec->multi_ios; i++) {
1582 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1583 badness += assign_out_path_ctls(codec, path);
1584 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001585
1586 return badness;
1587}
1588
1589/* map DACs for all pins in the list if they are single connections */
1590static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001591 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001592{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001593 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001594 int i;
1595 bool found = false;
1596 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001597 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001598 hda_nid_t dac;
1599 if (dacs[i])
1600 continue;
1601 dac = get_dac_if_single(codec, pins[i]);
1602 if (!dac)
1603 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001604 path = snd_hda_add_new_path(codec, dac, pins[i],
1605 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001606 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001607 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001608 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001609 dacs[i] = dac;
1610 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001611 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001612 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001613 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001614 }
1615 }
1616 return found;
1617}
1618
Takashi Iwaie7fdd522015-12-08 17:00:42 +01001619static inline bool has_aamix_out_paths(struct hda_gen_spec *spec)
1620{
1621 return spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1622 spec->aamix_out_paths[2];
1623}
1624
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001625/* create a new path including aamix if available, and return its index */
1626static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1627{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001628 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001629 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001630 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001631
1632 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001633 if (!path || !path->depth ||
1634 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001635 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001636 path_dac = path->path[0];
1637 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001638 pin = path->path[path->depth - 1];
1639 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1640 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001641 if (dac != path_dac)
1642 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001643 else if (spec->multiout.hp_out_nid[0])
1644 dac = spec->multiout.hp_out_nid[0];
1645 else if (spec->multiout.extra_out_nid[0])
1646 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001647 else
1648 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001649 if (dac)
1650 path = snd_hda_add_new_path(codec, dac, pin,
1651 spec->mixer_nid);
1652 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001653 if (!path)
1654 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001655 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001656 path->active = false; /* unused as default */
Takashi Iwai6b275b12015-03-20 18:11:05 +01001657 path->pin_fixed = true; /* static route */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001658 return snd_hda_get_path_idx(codec, path);
1659}
1660
Takashi Iwai55a63d42013-03-21 17:20:12 +01001661/* check whether the independent HP is available with the current config */
1662static bool indep_hp_possible(struct hda_codec *codec)
1663{
1664 struct hda_gen_spec *spec = codec->spec;
1665 struct auto_pin_cfg *cfg = &spec->autocfg;
1666 struct nid_path *path;
1667 int i, idx;
1668
1669 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1670 idx = spec->out_paths[0];
1671 else
1672 idx = spec->hp_paths[0];
1673 path = snd_hda_get_path_from_idx(codec, idx);
1674 if (!path)
1675 return false;
1676
1677 /* assume no path conflicts unless aamix is involved */
1678 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1679 return true;
1680
1681 /* check whether output paths contain aamix */
1682 for (i = 0; i < cfg->line_outs; i++) {
1683 if (spec->out_paths[i] == idx)
1684 break;
1685 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1686 if (path && is_nid_contained(path, spec->mixer_nid))
1687 return false;
1688 }
1689 for (i = 0; i < cfg->speaker_outs; i++) {
1690 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1691 if (path && is_nid_contained(path, spec->mixer_nid))
1692 return false;
1693 }
1694
1695 return true;
1696}
1697
Takashi Iwaia07a9492013-01-07 16:44:06 +01001698/* fill the empty entries in the dac array for speaker/hp with the
1699 * shared dac pointed by the paths
1700 */
1701static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1702 hda_nid_t *dacs, int *path_idx)
1703{
1704 struct nid_path *path;
1705 int i;
1706
1707 for (i = 0; i < num_outs; i++) {
1708 if (dacs[i])
1709 continue;
1710 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1711 if (!path)
1712 continue;
1713 dacs[i] = path->path[0];
1714 }
1715}
1716
Takashi Iwai352f7f92012-12-19 12:52:06 +01001717/* fill in the dac_nids table from the parsed pin configuration */
1718static int fill_and_eval_dacs(struct hda_codec *codec,
1719 bool fill_hardwired,
1720 bool fill_mio_first)
1721{
1722 struct hda_gen_spec *spec = codec->spec;
1723 struct auto_pin_cfg *cfg = &spec->autocfg;
1724 int i, err, badness;
1725
1726 /* set num_dacs once to full for look_for_dac() */
1727 spec->multiout.num_dacs = cfg->line_outs;
1728 spec->multiout.dac_nids = spec->private_dac_nids;
1729 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1730 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1731 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1732 spec->multi_ios = 0;
1733 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001734
1735 /* clear path indices */
1736 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1737 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1738 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1739 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1740 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001741 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001742 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1743 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1744
Takashi Iwai352f7f92012-12-19 12:52:06 +01001745 badness = 0;
1746
1747 /* fill hard-wired DACs first */
1748 if (fill_hardwired) {
1749 bool mapped;
1750 do {
1751 mapped = map_singles(codec, cfg->line_outs,
1752 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001753 spec->private_dac_nids,
1754 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001755 mapped |= map_singles(codec, cfg->hp_outs,
1756 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001757 spec->multiout.hp_out_nid,
1758 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001759 mapped |= map_singles(codec, cfg->speaker_outs,
1760 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001761 spec->multiout.extra_out_nid,
1762 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001763 if (!spec->no_multi_io &&
1764 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001765 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001766 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001767 if (!err)
1768 mapped = true;
1769 }
1770 } while (mapped);
1771 }
1772
1773 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001774 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001775 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001776
Takashi Iwaida96fb52013-07-29 16:54:36 +02001777 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001778 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1779 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001780 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001781 if (err < 0)
1782 return err;
1783 /* we don't count badness at this stage yet */
1784 }
1785
1786 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1787 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1788 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001789 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001790 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001791 if (err < 0)
1792 return err;
1793 badness += err;
1794 }
1795 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1796 err = try_assign_dacs(codec, cfg->speaker_outs,
1797 cfg->speaker_pins,
1798 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001799 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001800 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001801 if (err < 0)
1802 return err;
1803 badness += err;
1804 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001805 if (!spec->no_multi_io &&
1806 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001807 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001808 if (err < 0)
1809 return err;
1810 badness += err;
1811 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001812
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001813 if (spec->mixer_nid) {
1814 spec->aamix_out_paths[0] =
1815 check_aamix_out_path(codec, spec->out_paths[0]);
1816 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1817 spec->aamix_out_paths[1] =
1818 check_aamix_out_path(codec, spec->hp_paths[0]);
1819 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1820 spec->aamix_out_paths[2] =
1821 check_aamix_out_path(codec, spec->speaker_paths[0]);
1822 }
1823
Takashi Iwaida96fb52013-07-29 16:54:36 +02001824 if (!spec->no_multi_io &&
1825 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001826 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1827 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001828
Takashi Iwaia07a9492013-01-07 16:44:06 +01001829 /* re-count num_dacs and squash invalid entries */
1830 spec->multiout.num_dacs = 0;
1831 for (i = 0; i < cfg->line_outs; i++) {
1832 if (spec->private_dac_nids[i])
1833 spec->multiout.num_dacs++;
1834 else {
1835 memmove(spec->private_dac_nids + i,
1836 spec->private_dac_nids + i + 1,
1837 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1838 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1839 }
1840 }
1841
1842 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001843 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001844
Takashi Iwai352f7f92012-12-19 12:52:06 +01001845 if (spec->multi_ios == 2) {
1846 for (i = 0; i < 2; i++)
1847 spec->private_dac_nids[spec->multiout.num_dacs++] =
1848 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001849 } else if (spec->multi_ios) {
1850 spec->multi_ios = 0;
1851 badness += BAD_MULTI_IO;
1852 }
1853
Takashi Iwai55a63d42013-03-21 17:20:12 +01001854 if (spec->indep_hp && !indep_hp_possible(codec))
1855 badness += BAD_NO_INDEP_HP;
1856
Takashi Iwaia07a9492013-01-07 16:44:06 +01001857 /* re-fill the shared DAC for speaker / headphone */
1858 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1859 refill_shared_dacs(codec, cfg->hp_outs,
1860 spec->multiout.hp_out_nid,
1861 spec->hp_paths);
1862 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1863 refill_shared_dacs(codec, cfg->speaker_outs,
1864 spec->multiout.extra_out_nid,
1865 spec->speaker_paths);
1866
Takashi Iwai352f7f92012-12-19 12:52:06 +01001867 return badness;
1868}
1869
1870#define DEBUG_BADNESS
1871
1872#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001873#define debug_badness(fmt, ...) \
1874 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001875#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001876#define debug_badness(fmt, ...) \
1877 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001878#endif
1879
Takashi Iwaia7694092013-01-21 10:43:18 +01001880#ifdef DEBUG_BADNESS
1881static inline void print_nid_path_idx(struct hda_codec *codec,
1882 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001883{
Takashi Iwaia7694092013-01-21 10:43:18 +01001884 struct nid_path *path;
1885
1886 path = snd_hda_get_path_from_idx(codec, idx);
1887 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001888 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001889}
1890
1891static void debug_show_configs(struct hda_codec *codec,
1892 struct auto_pin_cfg *cfg)
1893{
1894 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001895 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001896 int i;
1897
1898 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001899 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001900 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001901 spec->multiout.dac_nids[0],
1902 spec->multiout.dac_nids[1],
1903 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001904 spec->multiout.dac_nids[3],
1905 lo_type[cfg->line_out_type]);
1906 for (i = 0; i < cfg->line_outs; i++)
1907 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001908 if (spec->multi_ios > 0)
1909 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1910 spec->multi_ios,
1911 spec->multi_io[0].pin, spec->multi_io[1].pin,
1912 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001913 for (i = 0; i < spec->multi_ios; i++)
1914 print_nid_path_idx(codec, " mio",
1915 spec->out_paths[cfg->line_outs + i]);
1916 if (cfg->hp_outs)
1917 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001918 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001919 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001920 spec->multiout.hp_out_nid[0],
1921 spec->multiout.hp_out_nid[1],
1922 spec->multiout.hp_out_nid[2],
1923 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001924 for (i = 0; i < cfg->hp_outs; i++)
1925 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1926 if (cfg->speaker_outs)
1927 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001928 cfg->speaker_pins[0], cfg->speaker_pins[1],
1929 cfg->speaker_pins[2], cfg->speaker_pins[3],
1930 spec->multiout.extra_out_nid[0],
1931 spec->multiout.extra_out_nid[1],
1932 spec->multiout.extra_out_nid[2],
1933 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001934 for (i = 0; i < cfg->speaker_outs; i++)
1935 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1936 for (i = 0; i < 3; i++)
1937 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001938}
Takashi Iwaia7694092013-01-21 10:43:18 +01001939#else
1940#define debug_show_configs(codec, cfg) /* NOP */
1941#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001942
1943/* find all available DACs of the codec */
1944static void fill_all_dac_nids(struct hda_codec *codec)
1945{
1946 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001947 hda_nid_t nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001948
1949 spec->num_all_dacs = 0;
1950 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
Takashi Iwai7639a062015-03-03 10:07:24 +01001951 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001952 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1953 continue;
1954 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001955 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001956 break;
1957 }
1958 spec->all_dacs[spec->num_all_dacs++] = nid;
1959 }
1960}
1961
1962static int parse_output_paths(struct hda_codec *codec)
1963{
1964 struct hda_gen_spec *spec = codec->spec;
1965 struct auto_pin_cfg *cfg = &spec->autocfg;
1966 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001967 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001968 int best_badness = INT_MAX;
1969 int badness;
1970 bool fill_hardwired = true, fill_mio_first = true;
1971 bool best_wired = true, best_mio = true;
1972 bool hp_spk_swapped = false;
1973
Takashi Iwai352f7f92012-12-19 12:52:06 +01001974 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1975 if (!best_cfg)
1976 return -ENOMEM;
1977 *best_cfg = *cfg;
1978
1979 for (;;) {
1980 badness = fill_and_eval_dacs(codec, fill_hardwired,
1981 fill_mio_first);
1982 if (badness < 0) {
1983 kfree(best_cfg);
1984 return badness;
1985 }
1986 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1987 cfg->line_out_type, fill_hardwired, fill_mio_first,
1988 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001989 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001990 if (badness < best_badness) {
1991 best_badness = badness;
1992 *best_cfg = *cfg;
1993 best_wired = fill_hardwired;
1994 best_mio = fill_mio_first;
1995 }
1996 if (!badness)
1997 break;
1998 fill_mio_first = !fill_mio_first;
1999 if (!fill_mio_first)
2000 continue;
2001 fill_hardwired = !fill_hardwired;
2002 if (!fill_hardwired)
2003 continue;
2004 if (hp_spk_swapped)
2005 break;
2006 hp_spk_swapped = true;
2007 if (cfg->speaker_outs > 0 &&
2008 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2009 cfg->hp_outs = cfg->line_outs;
2010 memcpy(cfg->hp_pins, cfg->line_out_pins,
2011 sizeof(cfg->hp_pins));
2012 cfg->line_outs = cfg->speaker_outs;
2013 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2014 sizeof(cfg->speaker_pins));
2015 cfg->speaker_outs = 0;
2016 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2017 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2018 fill_hardwired = true;
2019 continue;
2020 }
2021 if (cfg->hp_outs > 0 &&
2022 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2023 cfg->speaker_outs = cfg->line_outs;
2024 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2025 sizeof(cfg->speaker_pins));
2026 cfg->line_outs = cfg->hp_outs;
2027 memcpy(cfg->line_out_pins, cfg->hp_pins,
2028 sizeof(cfg->hp_pins));
2029 cfg->hp_outs = 0;
2030 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2031 cfg->line_out_type = AUTO_PIN_HP_OUT;
2032 fill_hardwired = true;
2033 continue;
2034 }
2035 break;
2036 }
2037
2038 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002039 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01002040 *cfg = *best_cfg;
2041 fill_and_eval_dacs(codec, best_wired, best_mio);
2042 }
2043 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2044 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01002045 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002046
2047 if (cfg->line_out_pins[0]) {
2048 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01002049 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002050 if (path)
2051 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002052 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01002053 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2054 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002055 if (spec->dac_min_mute)
Takashi Sakamoto51cdc8b2018-05-14 07:09:52 +09002056 spec->vmaster_tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] |= TLV_DB_SCALE_MUTE;
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002057 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002058 }
2059
Takashi Iwai9314a582013-01-21 10:49:05 +01002060 /* set initial pinctl targets */
2061 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2062 val = PIN_HP;
2063 else
2064 val = PIN_OUT;
2065 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2066 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2067 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2068 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2069 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2070 set_pin_targets(codec, cfg->speaker_outs,
2071 cfg->speaker_pins, val);
2072 }
2073
Takashi Iwai55a63d42013-03-21 17:20:12 +01002074 /* clear indep_hp flag if not available */
2075 if (spec->indep_hp && !indep_hp_possible(codec))
2076 spec->indep_hp = 0;
2077
Takashi Iwai352f7f92012-12-19 12:52:06 +01002078 kfree(best_cfg);
2079 return 0;
2080}
2081
2082/* add playback controls from the parsed DAC table */
2083static int create_multi_out_ctls(struct hda_codec *codec,
2084 const struct auto_pin_cfg *cfg)
2085{
2086 struct hda_gen_spec *spec = codec->spec;
2087 int i, err, noutputs;
2088
2089 noutputs = cfg->line_outs;
2090 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2091 noutputs += spec->multi_ios;
2092
2093 for (i = 0; i < noutputs; i++) {
2094 const char *name;
2095 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002096 struct nid_path *path;
2097
Takashi Iwai196c17662013-01-04 15:01:40 +01002098 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002099 if (!path)
2100 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002101
2102 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002103 if (!name || !strcmp(name, "CLFE")) {
2104 /* Center/LFE */
2105 err = add_vol_ctl(codec, "Center", 0, 1, path);
2106 if (err < 0)
2107 return err;
2108 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2109 if (err < 0)
2110 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002111 } else {
2112 err = add_stereo_vol(codec, name, index, path);
2113 if (err < 0)
2114 return err;
2115 }
2116
2117 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2118 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002119 err = add_sw_ctl(codec, "Center", 0, 1, path);
2120 if (err < 0)
2121 return err;
2122 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2123 if (err < 0)
2124 return err;
2125 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002126 err = add_stereo_sw(codec, name, index, path);
2127 if (err < 0)
2128 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 }
2130 }
2131 return 0;
2132}
2133
Takashi Iwaic2c80382013-01-07 10:33:57 +01002134static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01002135 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002137 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 int err;
2139
Takashi Iwai196c17662013-01-04 15:01:40 +01002140 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002141 if (!path)
2142 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01002143 err = add_stereo_vol(codec, pfx, cidx, path);
2144 if (err < 0)
2145 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002146 err = add_stereo_sw(codec, pfx, cidx, path);
2147 if (err < 0)
2148 return err;
2149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
Takashi Iwai352f7f92012-12-19 12:52:06 +01002152/* add playback controls for speaker and HP outputs */
2153static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01002154 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
Takashi Iwaic2c80382013-01-07 10:33:57 +01002156 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002157
2158 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002159 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02002160 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01002161 int err, idx = 0;
2162
2163 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2164 name = "Bass Speaker";
2165 else if (num_pins >= 3) {
2166 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01002167 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01002168 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002169 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002170 name = pfx;
2171 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01002173 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002174 if (err < 0)
2175 return err;
2176 }
2177 return 0;
2178}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002179
Takashi Iwai352f7f92012-12-19 12:52:06 +01002180static int create_hp_out_ctls(struct hda_codec *codec)
2181{
2182 struct hda_gen_spec *spec = codec->spec;
2183 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002184 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002185 "Headphone");
2186}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Takashi Iwai352f7f92012-12-19 12:52:06 +01002188static int create_speaker_out_ctls(struct hda_codec *codec)
2189{
2190 struct hda_gen_spec *spec = codec->spec;
2191 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002192 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002193 "Speaker");
2194}
2195
2196/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002197 * independent HP controls
2198 */
2199
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02002200static void call_hp_automute(struct hda_codec *codec,
2201 struct hda_jack_callback *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002202static int indep_hp_info(struct snd_kcontrol *kcontrol,
2203 struct snd_ctl_elem_info *uinfo)
2204{
2205 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2206}
2207
2208static int indep_hp_get(struct snd_kcontrol *kcontrol,
2209 struct snd_ctl_elem_value *ucontrol)
2210{
2211 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2212 struct hda_gen_spec *spec = codec->spec;
2213 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2214 return 0;
2215}
2216
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002217static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2218 int nomix_path_idx, int mix_path_idx,
2219 int out_type);
2220
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002221static int indep_hp_put(struct snd_kcontrol *kcontrol,
2222 struct snd_ctl_elem_value *ucontrol)
2223{
2224 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2225 struct hda_gen_spec *spec = codec->spec;
2226 unsigned int select = ucontrol->value.enumerated.item[0];
2227 int ret = 0;
2228
2229 mutex_lock(&spec->pcm_mutex);
2230 if (spec->active_streams) {
2231 ret = -EBUSY;
2232 goto unlock;
2233 }
2234
2235 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002236 hda_nid_t *dacp;
2237 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2238 dacp = &spec->private_dac_nids[0];
2239 else
2240 dacp = &spec->multiout.hp_out_nid[0];
2241
2242 /* update HP aamix paths in case it conflicts with indep HP */
2243 if (spec->have_aamix_ctl) {
2244 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2245 update_aamix_paths(codec, spec->aamix_mode,
2246 spec->out_paths[0],
2247 spec->aamix_out_paths[0],
2248 spec->autocfg.line_out_type);
2249 else
2250 update_aamix_paths(codec, spec->aamix_mode,
2251 spec->hp_paths[0],
2252 spec->aamix_out_paths[1],
2253 AUTO_PIN_HP_OUT);
2254 }
2255
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002256 spec->indep_hp_enabled = select;
2257 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002258 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002259 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002260 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002261
Takashi Iwai963afde2013-05-31 15:20:31 +02002262 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002263 ret = 1;
2264 }
2265 unlock:
2266 mutex_unlock(&spec->pcm_mutex);
2267 return ret;
2268}
2269
2270static const struct snd_kcontrol_new indep_hp_ctl = {
2271 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2272 .name = "Independent HP",
2273 .info = indep_hp_info,
2274 .get = indep_hp_get,
2275 .put = indep_hp_put,
2276};
2277
2278
2279static int create_indep_hp_ctls(struct hda_codec *codec)
2280{
2281 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002282 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002283
2284 if (!spec->indep_hp)
2285 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002286 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2287 dac = spec->multiout.dac_nids[0];
2288 else
2289 dac = spec->multiout.hp_out_nid[0];
2290 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002291 spec->indep_hp = 0;
2292 return 0;
2293 }
2294
2295 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002296 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002297 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2298 return -ENOMEM;
2299 return 0;
2300}
2301
2302/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002303 * channel mode enum control
2304 */
2305
2306static int ch_mode_info(struct snd_kcontrol *kcontrol,
2307 struct snd_ctl_elem_info *uinfo)
2308{
2309 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2310 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002311 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002312
2313 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2314 uinfo->count = 1;
2315 uinfo->value.enumerated.items = spec->multi_ios + 1;
2316 if (uinfo->value.enumerated.item > spec->multi_ios)
2317 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002318 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2319 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002320 return 0;
2321}
2322
2323static int ch_mode_get(struct snd_kcontrol *kcontrol,
2324 struct snd_ctl_elem_value *ucontrol)
2325{
2326 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2327 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002328 ucontrol->value.enumerated.item[0] =
2329 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002330 return 0;
2331}
2332
Takashi Iwai196c17662013-01-04 15:01:40 +01002333static inline struct nid_path *
2334get_multiio_path(struct hda_codec *codec, int idx)
2335{
2336 struct hda_gen_spec *spec = codec->spec;
2337 return snd_hda_get_path_from_idx(codec,
2338 spec->out_paths[spec->autocfg.line_outs + idx]);
2339}
2340
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002341static void update_automute_all(struct hda_codec *codec);
2342
Takashi Iwai65033cc2013-04-16 12:31:05 +02002343/* Default value to be passed as aamix argument for snd_hda_activate_path();
2344 * used for output paths
2345 */
2346static bool aamix_default(struct hda_gen_spec *spec)
2347{
2348 return !spec->have_aamix_ctl || spec->aamix_mode;
2349}
2350
Takashi Iwai352f7f92012-12-19 12:52:06 +01002351static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2352{
2353 struct hda_gen_spec *spec = codec->spec;
2354 hda_nid_t nid = spec->multi_io[idx].pin;
2355 struct nid_path *path;
2356
Takashi Iwai196c17662013-01-04 15:01:40 +01002357 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002358 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002360
2361 if (path->active == output)
2362 return 0;
2363
2364 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002365 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002366 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002367 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002368 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002369 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002370 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002371 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002372 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002374
2375 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002376 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002377
Takashi Iwai352f7f92012-12-19 12:52:06 +01002378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379}
2380
Takashi Iwai352f7f92012-12-19 12:52:06 +01002381static int ch_mode_put(struct snd_kcontrol *kcontrol,
2382 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002384 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2385 struct hda_gen_spec *spec = codec->spec;
2386 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Takashi Iwai352f7f92012-12-19 12:52:06 +01002388 ch = ucontrol->value.enumerated.item[0];
2389 if (ch < 0 || ch > spec->multi_ios)
2390 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002391 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002392 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002393 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002394 for (i = 0; i < spec->multi_ios; i++)
2395 set_multi_io(codec, i, i < ch);
2396 spec->multiout.max_channels = max(spec->ext_channel_count,
2397 spec->const_channel_count);
2398 if (spec->need_dac_fix)
2399 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 return 1;
2401}
2402
Takashi Iwai352f7f92012-12-19 12:52:06 +01002403static const struct snd_kcontrol_new channel_mode_enum = {
2404 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2405 .name = "Channel Mode",
2406 .info = ch_mode_info,
2407 .get = ch_mode_get,
2408 .put = ch_mode_put,
2409};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Takashi Iwai352f7f92012-12-19 12:52:06 +01002411static int create_multi_channel_mode(struct hda_codec *codec)
2412{
2413 struct hda_gen_spec *spec = codec->spec;
2414
2415 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002416 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002417 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 return 0;
2420}
2421
Takashi Iwai352f7f92012-12-19 12:52:06 +01002422/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002423 * aamix loopback enable/disable switch
2424 */
2425
2426#define loopback_mixing_info indep_hp_info
2427
2428static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2429 struct snd_ctl_elem_value *ucontrol)
2430{
2431 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2432 struct hda_gen_spec *spec = codec->spec;
2433 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2434 return 0;
2435}
2436
2437static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002438 int nomix_path_idx, int mix_path_idx,
2439 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002440{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002441 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002442 struct nid_path *nomix_path, *mix_path;
2443
2444 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2445 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2446 if (!nomix_path || !mix_path)
2447 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002448
2449 /* if HP aamix path is driven from a different DAC and the
2450 * independent HP mode is ON, can't turn on aamix path
2451 */
2452 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2453 mix_path->path[0] != spec->alt_dac_nid)
2454 do_mix = false;
2455
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002456 if (do_mix) {
2457 snd_hda_activate_path(codec, nomix_path, false, true);
2458 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002459 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002460 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002461 snd_hda_activate_path(codec, mix_path, false, false);
2462 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002463 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002464 }
2465}
2466
Takashi Iwaie7fdd522015-12-08 17:00:42 +01002467/* re-initialize the output paths; only called from loopback_mixing_put() */
2468static void update_output_paths(struct hda_codec *codec, int num_outs,
2469 const int *paths)
2470{
2471 struct hda_gen_spec *spec = codec->spec;
2472 struct nid_path *path;
2473 int i;
2474
2475 for (i = 0; i < num_outs; i++) {
2476 path = snd_hda_get_path_from_idx(codec, paths[i]);
2477 if (path)
2478 snd_hda_activate_path(codec, path, path->active,
2479 spec->aamix_mode);
2480 }
2481}
2482
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002483static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2484 struct snd_ctl_elem_value *ucontrol)
2485{
2486 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2487 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie7fdd522015-12-08 17:00:42 +01002488 const struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002489 unsigned int val = ucontrol->value.enumerated.item[0];
2490
2491 if (val == spec->aamix_mode)
2492 return 0;
2493 spec->aamix_mode = val;
Takashi Iwaie7fdd522015-12-08 17:00:42 +01002494 if (has_aamix_out_paths(spec)) {
2495 update_aamix_paths(codec, val, spec->out_paths[0],
2496 spec->aamix_out_paths[0],
2497 cfg->line_out_type);
2498 update_aamix_paths(codec, val, spec->hp_paths[0],
2499 spec->aamix_out_paths[1],
2500 AUTO_PIN_HP_OUT);
2501 update_aamix_paths(codec, val, spec->speaker_paths[0],
2502 spec->aamix_out_paths[2],
2503 AUTO_PIN_SPEAKER_OUT);
2504 } else {
2505 update_output_paths(codec, cfg->line_outs, spec->out_paths);
2506 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2507 update_output_paths(codec, cfg->hp_outs, spec->hp_paths);
2508 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
2509 update_output_paths(codec, cfg->speaker_outs,
2510 spec->speaker_paths);
2511 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002512 return 1;
2513}
2514
2515static const struct snd_kcontrol_new loopback_mixing_enum = {
2516 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2517 .name = "Loopback Mixing",
2518 .info = loopback_mixing_info,
2519 .get = loopback_mixing_get,
2520 .put = loopback_mixing_put,
2521};
2522
2523static int create_loopback_mixing_ctl(struct hda_codec *codec)
2524{
2525 struct hda_gen_spec *spec = codec->spec;
2526
2527 if (!spec->mixer_nid)
2528 return 0;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002529 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2530 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002531 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002532 return 0;
2533}
2534
2535/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002536 * shared headphone/mic handling
2537 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002538
Takashi Iwai352f7f92012-12-19 12:52:06 +01002539static void call_update_outputs(struct hda_codec *codec);
2540
2541/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002542static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002543{
2544 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002545 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002546 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002547 hda_nid_t pin;
2548
2549 pin = spec->hp_mic_pin;
2550 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2551
2552 if (!force) {
2553 val = snd_hda_codec_get_pin_target(codec, pin);
2554 if (as_mic) {
2555 if (val & PIN_IN)
2556 return;
2557 } else {
2558 if (val & PIN_OUT)
2559 return;
2560 }
2561 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002562
2563 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002564 /* if the HP pin doesn't support VREF and the codec driver gives an
2565 * alternative pin, set up the VREF on that pin instead
2566 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002567 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2568 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2569 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2570 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002571 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002572 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002573 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002574
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002575 if (!spec->hp_mic_jack_modes) {
2576 if (as_mic)
2577 val |= PIN_IN;
2578 else
2579 val = PIN_HP;
2580 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002581 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002582 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002583}
2584
2585/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002586static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002587{
2588 struct hda_gen_spec *spec = codec->spec;
2589 struct auto_pin_cfg *cfg = &spec->autocfg;
2590 unsigned int defcfg;
2591 hda_nid_t nid;
2592
Takashi Iwai967303d2013-02-19 17:12:42 +01002593 if (!spec->hp_mic) {
2594 if (spec->suppress_hp_mic_detect)
2595 return 0;
2596 /* automatic detection: only if no input or a single internal
2597 * input pin is found, try to detect the shared hp/mic
2598 */
2599 if (cfg->num_inputs > 1)
2600 return 0;
2601 else if (cfg->num_inputs == 1) {
2602 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2603 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2604 return 0;
2605 }
2606 }
2607
2608 spec->hp_mic = 0; /* clear once */
2609 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002610 return 0;
2611
Takashi Iwai967303d2013-02-19 17:12:42 +01002612 nid = 0;
2613 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2614 nid = cfg->line_out_pins[0];
2615 else if (cfg->hp_outs > 0)
2616 nid = cfg->hp_pins[0];
2617 if (!nid)
2618 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002619
2620 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2621 return 0; /* no input */
2622
Takashi Iwai967303d2013-02-19 17:12:42 +01002623 cfg->inputs[cfg->num_inputs].pin = nid;
2624 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002625 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002626 cfg->num_inputs++;
2627 spec->hp_mic = 1;
2628 spec->hp_mic_pin = nid;
2629 /* we can't handle auto-mic together with HP-mic */
2630 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002631 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002632 return 0;
2633}
2634
Takashi Iwai978e77e2013-01-10 16:57:58 +01002635/*
2636 * output jack mode
2637 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002638
2639static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2640
2641static const char * const out_jack_texts[] = {
2642 "Line Out", "Headphone Out",
2643};
2644
Takashi Iwai978e77e2013-01-10 16:57:58 +01002645static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2646 struct snd_ctl_elem_info *uinfo)
2647{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002648 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002649}
2650
2651static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2652 struct snd_ctl_elem_value *ucontrol)
2653{
2654 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2655 hda_nid_t nid = kcontrol->private_value;
2656 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2657 ucontrol->value.enumerated.item[0] = 1;
2658 else
2659 ucontrol->value.enumerated.item[0] = 0;
2660 return 0;
2661}
2662
2663static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2664 struct snd_ctl_elem_value *ucontrol)
2665{
2666 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2667 hda_nid_t nid = kcontrol->private_value;
2668 unsigned int val;
2669
2670 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2671 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2672 return 0;
2673 snd_hda_set_pin_ctl_cache(codec, nid, val);
2674 return 1;
2675}
2676
2677static const struct snd_kcontrol_new out_jack_mode_enum = {
2678 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2679 .info = out_jack_mode_info,
2680 .get = out_jack_mode_get,
2681 .put = out_jack_mode_put,
2682};
2683
2684static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2685{
2686 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +02002687 const struct snd_kcontrol_new *kctl;
Takashi Iwai978e77e2013-01-10 16:57:58 +01002688 int i;
2689
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +02002690 snd_array_for_each(&spec->kctls, i, kctl) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002691 if (!strcmp(kctl->name, name) && kctl->index == idx)
2692 return true;
2693 }
2694 return false;
2695}
2696
2697static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2698 char *name, size_t name_len)
2699{
2700 struct hda_gen_spec *spec = codec->spec;
2701 int idx = 0;
2702
2703 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2704 strlcat(name, " Jack Mode", name_len);
2705
2706 for (; find_kctl_name(codec, name, idx); idx++)
2707 ;
2708}
2709
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002710static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2711{
2712 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002713 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002714 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2715 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2716 return 2;
2717 }
2718 return 1;
2719}
2720
Takashi Iwai978e77e2013-01-10 16:57:58 +01002721static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2722 hda_nid_t *pins)
2723{
2724 struct hda_gen_spec *spec = codec->spec;
2725 int i;
2726
2727 for (i = 0; i < num_pins; i++) {
2728 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002729 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002730 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002731 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002732 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002733 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002734 get_jack_mode_name(codec, pin, name, sizeof(name));
2735 knew = snd_hda_gen_add_kctl(spec, name,
2736 &out_jack_mode_enum);
2737 if (!knew)
2738 return -ENOMEM;
2739 knew->private_value = pin;
2740 }
2741 }
2742
2743 return 0;
2744}
2745
Takashi Iwai294765582013-01-17 09:52:11 +01002746/*
2747 * input jack mode
2748 */
2749
2750/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2751#define NUM_VREFS 6
2752
2753static const char * const vref_texts[NUM_VREFS] = {
2754 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2755 "", "Mic 80pc Bias", "Mic 100pc Bias"
2756};
2757
2758static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2759{
2760 unsigned int pincap;
2761
2762 pincap = snd_hda_query_pin_caps(codec, pin);
2763 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2764 /* filter out unusual vrefs */
2765 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2766 return pincap;
2767}
2768
2769/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2770static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2771{
2772 unsigned int i, n = 0;
2773
2774 for (i = 0; i < NUM_VREFS; i++) {
2775 if (vref_caps & (1 << i)) {
2776 if (n == item_idx)
2777 return i;
2778 n++;
2779 }
2780 }
2781 return 0;
2782}
2783
2784/* convert back from the vref ctl index to the enum item index */
2785static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2786{
2787 unsigned int i, n = 0;
2788
2789 for (i = 0; i < NUM_VREFS; i++) {
2790 if (i == idx)
2791 return n;
2792 if (vref_caps & (1 << i))
2793 n++;
2794 }
2795 return 0;
2796}
2797
2798static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2799 struct snd_ctl_elem_info *uinfo)
2800{
2801 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2802 hda_nid_t nid = kcontrol->private_value;
2803 unsigned int vref_caps = get_vref_caps(codec, nid);
2804
2805 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2806 vref_texts);
2807 /* set the right text */
2808 strcpy(uinfo->value.enumerated.name,
2809 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2810 return 0;
2811}
2812
2813static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2814 struct snd_ctl_elem_value *ucontrol)
2815{
2816 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2817 hda_nid_t nid = kcontrol->private_value;
2818 unsigned int vref_caps = get_vref_caps(codec, nid);
2819 unsigned int idx;
2820
2821 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2822 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2823 return 0;
2824}
2825
2826static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2827 struct snd_ctl_elem_value *ucontrol)
2828{
2829 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2830 hda_nid_t nid = kcontrol->private_value;
2831 unsigned int vref_caps = get_vref_caps(codec, nid);
2832 unsigned int val, idx;
2833
2834 val = snd_hda_codec_get_pin_target(codec, nid);
2835 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2836 if (idx == ucontrol->value.enumerated.item[0])
2837 return 0;
2838
2839 val &= ~AC_PINCTL_VREFEN;
2840 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2841 snd_hda_set_pin_ctl_cache(codec, nid, val);
2842 return 1;
2843}
2844
2845static const struct snd_kcontrol_new in_jack_mode_enum = {
2846 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2847 .info = in_jack_mode_info,
2848 .get = in_jack_mode_get,
2849 .put = in_jack_mode_put,
2850};
2851
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002852static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2853{
2854 struct hda_gen_spec *spec = codec->spec;
2855 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002856 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002857 nitems = hweight32(get_vref_caps(codec, pin));
2858 return nitems ? nitems : 1;
2859}
2860
Takashi Iwai294765582013-01-17 09:52:11 +01002861static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2862{
2863 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002864 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002865 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002866 unsigned int defcfg;
2867
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002868 if (pin == spec->hp_mic_pin)
2869 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002870
2871 /* no jack mode for fixed pins */
2872 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2873 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2874 return 0;
2875
2876 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002877 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002878 return 0;
2879
2880 get_jack_mode_name(codec, pin, name, sizeof(name));
2881 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2882 if (!knew)
2883 return -ENOMEM;
2884 knew->private_value = pin;
2885 return 0;
2886}
2887
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002888/*
2889 * HP/mic shared jack mode
2890 */
2891static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2892 struct snd_ctl_elem_info *uinfo)
2893{
2894 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2895 hda_nid_t nid = kcontrol->private_value;
2896 int out_jacks = get_out_jack_num_items(codec, nid);
2897 int in_jacks = get_in_jack_num_items(codec, nid);
2898 const char *text = NULL;
2899 int idx;
2900
2901 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2902 uinfo->count = 1;
2903 uinfo->value.enumerated.items = out_jacks + in_jacks;
2904 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2905 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2906 idx = uinfo->value.enumerated.item;
2907 if (idx < out_jacks) {
2908 if (out_jacks > 1)
2909 text = out_jack_texts[idx];
2910 else
2911 text = "Headphone Out";
2912 } else {
2913 idx -= out_jacks;
2914 if (in_jacks > 1) {
2915 unsigned int vref_caps = get_vref_caps(codec, nid);
2916 text = vref_texts[get_vref_idx(vref_caps, idx)];
2917 } else
2918 text = "Mic In";
2919 }
2920
2921 strcpy(uinfo->value.enumerated.name, text);
2922 return 0;
2923}
2924
2925static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2926{
2927 int out_jacks = get_out_jack_num_items(codec, nid);
2928 int in_jacks = get_in_jack_num_items(codec, nid);
2929 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2930 int idx = 0;
2931
2932 if (val & PIN_OUT) {
2933 if (out_jacks > 1 && val == PIN_HP)
2934 idx = 1;
2935 } else if (val & PIN_IN) {
2936 idx = out_jacks;
2937 if (in_jacks > 1) {
2938 unsigned int vref_caps = get_vref_caps(codec, nid);
2939 val &= AC_PINCTL_VREFEN;
2940 idx += cvt_from_vref_idx(vref_caps, val);
2941 }
2942 }
2943 return idx;
2944}
2945
2946static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2947 struct snd_ctl_elem_value *ucontrol)
2948{
2949 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2950 hda_nid_t nid = kcontrol->private_value;
2951 ucontrol->value.enumerated.item[0] =
2952 get_cur_hp_mic_jack_mode(codec, nid);
2953 return 0;
2954}
2955
2956static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2957 struct snd_ctl_elem_value *ucontrol)
2958{
2959 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2960 hda_nid_t nid = kcontrol->private_value;
2961 int out_jacks = get_out_jack_num_items(codec, nid);
2962 int in_jacks = get_in_jack_num_items(codec, nid);
2963 unsigned int val, oldval, idx;
2964
2965 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2966 idx = ucontrol->value.enumerated.item[0];
2967 if (oldval == idx)
2968 return 0;
2969
2970 if (idx < out_jacks) {
2971 if (out_jacks > 1)
2972 val = idx ? PIN_HP : PIN_OUT;
2973 else
2974 val = PIN_HP;
2975 } else {
2976 idx -= out_jacks;
2977 if (in_jacks > 1) {
2978 unsigned int vref_caps = get_vref_caps(codec, nid);
2979 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002980 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2981 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002982 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002983 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002984 }
2985 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002986 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002987
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002988 return 1;
2989}
2990
2991static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2992 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2993 .info = hp_mic_jack_mode_info,
2994 .get = hp_mic_jack_mode_get,
2995 .put = hp_mic_jack_mode_put,
2996};
2997
2998static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2999{
3000 struct hda_gen_spec *spec = codec->spec;
3001 struct snd_kcontrol_new *knew;
3002
Takashi Iwai5f171ba2013-02-19 18:14:54 +01003003 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
3004 &hp_mic_jack_mode_enum);
3005 if (!knew)
3006 return -ENOMEM;
3007 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01003008 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01003009 return 0;
3010}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003011
3012/*
3013 * Parse input paths
3014 */
3015
Takashi Iwai352f7f92012-12-19 12:52:06 +01003016/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003017static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003018{
3019 struct hda_amp_list *list;
3020
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003021 list = snd_array_new(&spec->loopback_list);
3022 if (!list)
3023 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003024 list->nid = mix;
3025 list->dir = HDA_INPUT;
3026 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003027 spec->loopback.amplist = spec->loopback_list.list;
3028 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003029}
Takashi Iwaicb53c622007-08-10 17:21:45 +02003030
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003031/* return true if either a volume or a mute amp is found for the given
3032 * aamix path; the amp has to be either in the mixer node or its direct leaf
3033 */
3034static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3035 hda_nid_t pin, unsigned int *mix_val,
3036 unsigned int *mute_val)
3037{
3038 int idx, num_conns;
3039 const hda_nid_t *list;
3040 hda_nid_t nid;
3041
3042 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3043 if (idx < 0)
3044 return false;
3045
3046 *mix_val = *mute_val = 0;
3047 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3048 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3049 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3050 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3051 if (*mix_val && *mute_val)
3052 return true;
3053
3054 /* check leaf node */
3055 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3056 if (num_conns < idx)
3057 return false;
3058 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01003059 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3060 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003061 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01003062 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3063 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003064 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3065
3066 return *mix_val || *mute_val;
3067}
3068
Takashi Iwai352f7f92012-12-19 12:52:06 +01003069/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01003070static int new_analog_input(struct hda_codec *codec, int input_idx,
3071 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003072 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003074 struct hda_gen_spec *spec = codec->spec;
3075 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003076 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003077 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003079 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3080 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003081
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003082 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003083 if (!path)
3084 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003085 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01003086 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003087
3088 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003089 if (mix_val) {
3090 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003091 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003093 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 }
3095
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003096 if (mute_val) {
3097 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003098 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003100 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 }
3102
Takashi Iwai352f7f92012-12-19 12:52:06 +01003103 path->active = true;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003104 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003105 err = add_loopback_list(spec, mix_nid, idx);
3106 if (err < 0)
3107 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003108
3109 if (spec->mixer_nid != spec->mixer_merge_nid &&
3110 !spec->loopback_merge_path) {
3111 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3112 spec->mixer_merge_nid, 0);
3113 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003114 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003115 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003116 path->pin_fixed = true; /* static route */
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003117 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003118 spec->loopback_merge_path =
3119 snd_hda_get_path_idx(codec, path);
3120 }
3121 }
3122
Takashi Iwai352f7f92012-12-19 12:52:06 +01003123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124}
3125
Takashi Iwai352f7f92012-12-19 12:52:06 +01003126static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003128 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3129 return (pincap & AC_PINCAP_IN) != 0;
3130}
3131
3132/* Parse the codec tree and retrieve ADCs */
3133static int fill_adc_nids(struct hda_codec *codec)
3134{
3135 struct hda_gen_spec *spec = codec->spec;
3136 hda_nid_t nid;
3137 hda_nid_t *adc_nids = spec->adc_nids;
3138 int max_nums = ARRAY_SIZE(spec->adc_nids);
Takashi Iwai7639a062015-03-03 10:07:24 +01003139 int nums = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003140
Takashi Iwai7639a062015-03-03 10:07:24 +01003141 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003142 unsigned int caps = get_wcaps(codec, nid);
3143 int type = get_wcaps_type(caps);
3144
3145 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3146 continue;
3147 adc_nids[nums] = nid;
3148 if (++nums >= max_nums)
3149 break;
3150 }
3151 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01003152
3153 /* copy the detected ADCs to all_adcs[] */
3154 spec->num_all_adcs = nums;
3155 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3156
Takashi Iwai352f7f92012-12-19 12:52:06 +01003157 return nums;
3158}
3159
3160/* filter out invalid adc_nids that don't give all active input pins;
3161 * if needed, check whether dynamic ADC-switching is available
3162 */
3163static int check_dyn_adc_switch(struct hda_codec *codec)
3164{
3165 struct hda_gen_spec *spec = codec->spec;
3166 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003167 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003168 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003169
Takashi Iwai352f7f92012-12-19 12:52:06 +01003170 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003171 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003172 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003173 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003174 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003175 break;
3176 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003177 if (i >= imux->num_items) {
3178 ok_bits |= (1 << n);
3179 nums++;
3180 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003181 }
3182
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003183 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003184 /* check whether ADC-switch is possible */
3185 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003186 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003187 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003188 spec->dyn_adc_idx[i] = n;
3189 break;
3190 }
3191 }
3192 }
3193
Takashi Iwai4e76a882014-02-25 12:21:03 +01003194 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003195 spec->dyn_adc_switch = 1;
3196 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003197 /* shrink the invalid adcs and input paths */
3198 nums = 0;
3199 for (n = 0; n < spec->num_adc_nids; n++) {
3200 if (!(ok_bits & (1 << n)))
3201 continue;
3202 if (n != nums) {
3203 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003204 for (i = 0; i < imux->num_items; i++) {
3205 invalidate_nid_path(codec,
3206 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003207 spec->input_paths[i][nums] =
3208 spec->input_paths[i][n];
Hui Wanga8f20fd2017-06-28 08:59:16 +08003209 spec->input_paths[i][n] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01003210 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003211 }
3212 nums++;
3213 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003214 spec->num_adc_nids = nums;
3215 }
3216
Takashi Iwai967303d2013-02-19 17:12:42 +01003217 if (imux->num_items == 1 ||
3218 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003219 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003220 spec->num_adc_nids = 1; /* reduce to a single ADC */
3221 }
3222
3223 /* single index for individual volumes ctls */
3224 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3225 spec->num_adc_nids = 1;
3226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 return 0;
3228}
3229
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003230/* parse capture source paths from the given pin and create imux items */
3231static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003232 int cfg_idx, int num_adcs,
3233 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003234{
3235 struct hda_gen_spec *spec = codec->spec;
3236 struct hda_input_mux *imux = &spec->input_mux;
3237 int imux_idx = imux->num_items;
3238 bool imux_added = false;
3239 int c;
3240
3241 for (c = 0; c < num_adcs; c++) {
3242 struct nid_path *path;
3243 hda_nid_t adc = spec->adc_nids[c];
3244
3245 if (!is_reachable_path(codec, pin, adc))
3246 continue;
3247 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3248 if (!path)
3249 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003250 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003251 spec->input_paths[imux_idx][c] =
3252 snd_hda_get_path_idx(codec, path);
3253
3254 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003255 if (spec->hp_mic_pin == pin)
3256 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003257 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003258 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003259 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003260 if (spec->dyn_adc_switch)
3261 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003262 }
3263 }
3264
3265 return 0;
3266}
3267
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003269 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003271
Takashi Iwaic9700422013-01-18 10:17:30 +01003272/* fill the label for each input at first */
3273static int fill_input_pin_labels(struct hda_codec *codec)
3274{
3275 struct hda_gen_spec *spec = codec->spec;
3276 const struct auto_pin_cfg *cfg = &spec->autocfg;
3277 int i;
3278
3279 for (i = 0; i < cfg->num_inputs; i++) {
3280 hda_nid_t pin = cfg->inputs[i].pin;
3281 const char *label;
3282 int j, idx;
3283
3284 if (!is_input_pin(codec, pin))
3285 continue;
3286
3287 label = hda_get_autocfg_input_label(codec, cfg, i);
3288 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003289 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003290 if (spec->input_labels[j] &&
3291 !strcmp(spec->input_labels[j], label)) {
3292 idx = spec->input_label_idxs[j] + 1;
3293 break;
3294 }
3295 }
3296
3297 spec->input_labels[i] = label;
3298 spec->input_label_idxs[i] = idx;
3299 }
3300
3301 return 0;
3302}
3303
Takashi Iwai9dba2052013-01-18 10:01:15 +01003304#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3305
Takashi Iwai352f7f92012-12-19 12:52:06 +01003306static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003307{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003308 struct hda_gen_spec *spec = codec->spec;
3309 const struct auto_pin_cfg *cfg = &spec->autocfg;
3310 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003311 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003312 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003313 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003314
Takashi Iwai352f7f92012-12-19 12:52:06 +01003315 num_adcs = fill_adc_nids(codec);
3316 if (num_adcs < 0)
3317 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003318
Takashi Iwaic9700422013-01-18 10:17:30 +01003319 err = fill_input_pin_labels(codec);
3320 if (err < 0)
3321 return err;
3322
Takashi Iwai352f7f92012-12-19 12:52:06 +01003323 for (i = 0; i < cfg->num_inputs; i++) {
3324 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
Takashi Iwai352f7f92012-12-19 12:52:06 +01003326 pin = cfg->inputs[i].pin;
3327 if (!is_input_pin(codec, pin))
3328 continue;
3329
Takashi Iwai2c12c302013-01-10 09:33:29 +01003330 val = PIN_IN;
3331 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3332 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai3e1b0c42015-04-27 10:43:22 +02003333 if (pin != spec->hp_mic_pin &&
3334 !snd_hda_codec_get_pin_target(codec, pin))
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003335 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003336
Takashi Iwai352f7f92012-12-19 12:52:06 +01003337 if (mixer) {
3338 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003339 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003340 spec->input_labels[i],
3341 spec->input_label_idxs[i],
3342 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003343 if (err < 0)
3344 return err;
3345 }
3346 }
3347
Takashi Iwaic9700422013-01-18 10:17:30 +01003348 err = parse_capture_source(codec, pin, i, num_adcs,
3349 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003350 if (err < 0)
3351 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003352
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003353 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003354 err = create_in_jack_mode(codec, pin);
3355 if (err < 0)
3356 return err;
3357 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003358 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003359
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003360 /* add stereo mix when explicitly enabled via hint */
Takashi Iwai74f14b32014-12-15 13:43:59 +01003361 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003362 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003363 "Stereo Mix", 0);
3364 if (err < 0)
3365 return err;
Takashi Iwai82d04e12014-12-15 13:40:42 +01003366 else
3367 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003368 }
3369
3370 return 0;
3371}
3372
3373
3374/*
3375 * input source mux
3376 */
3377
Takashi Iwaic697b712013-01-07 17:09:26 +01003378/* get the input path specified by the given adc and imux indices */
3379static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003380{
3381 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003382 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3383 snd_BUG();
3384 return NULL;
3385 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003386 if (spec->dyn_adc_switch)
3387 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003388 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003389 snd_BUG();
3390 return NULL;
3391 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003392 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003393}
3394
3395static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3396 unsigned int idx);
3397
3398static int mux_enum_info(struct snd_kcontrol *kcontrol,
3399 struct snd_ctl_elem_info *uinfo)
3400{
3401 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3402 struct hda_gen_spec *spec = codec->spec;
3403 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3404}
3405
3406static int mux_enum_get(struct snd_kcontrol *kcontrol,
3407 struct snd_ctl_elem_value *ucontrol)
3408{
3409 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3410 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003411 /* the ctls are created at once with multiple counts */
3412 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003413
3414 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3415 return 0;
3416}
3417
3418static int mux_enum_put(struct snd_kcontrol *kcontrol,
3419 struct snd_ctl_elem_value *ucontrol)
3420{
3421 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003422 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003423 return mux_select(codec, adc_idx,
3424 ucontrol->value.enumerated.item[0]);
3425}
3426
Takashi Iwai352f7f92012-12-19 12:52:06 +01003427static const struct snd_kcontrol_new cap_src_temp = {
3428 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3429 .name = "Input Source",
3430 .info = mux_enum_info,
3431 .get = mux_enum_get,
3432 .put = mux_enum_put,
3433};
3434
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003435/*
3436 * capture volume and capture switch ctls
3437 */
3438
Takashi Iwai352f7f92012-12-19 12:52:06 +01003439typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3440 struct snd_ctl_elem_value *ucontrol);
3441
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003442/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003443static int cap_put_caller(struct snd_kcontrol *kcontrol,
3444 struct snd_ctl_elem_value *ucontrol,
3445 put_call_t func, int type)
3446{
3447 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3448 struct hda_gen_spec *spec = codec->spec;
3449 const struct hda_input_mux *imux;
3450 struct nid_path *path;
3451 int i, adc_idx, err = 0;
3452
3453 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003454 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003455 mutex_lock(&codec->control_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003456 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003457 path = get_input_path(codec, adc_idx, i);
3458 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003459 continue;
3460 kcontrol->private_value = path->ctls[type];
3461 err = func(kcontrol, ucontrol);
3462 if (err < 0)
Takashi Iwaia551d912015-02-26 12:34:49 +01003463 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003464 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003465 mutex_unlock(&codec->control_mutex);
3466 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003467 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003468 return err;
3469}
3470
3471/* capture volume ctl callbacks */
3472#define cap_vol_info snd_hda_mixer_amp_volume_info
3473#define cap_vol_get snd_hda_mixer_amp_volume_get
3474#define cap_vol_tlv snd_hda_mixer_amp_tlv
3475
3476static int cap_vol_put(struct snd_kcontrol *kcontrol,
3477 struct snd_ctl_elem_value *ucontrol)
3478{
3479 return cap_put_caller(kcontrol, ucontrol,
3480 snd_hda_mixer_amp_volume_put,
3481 NID_PATH_VOL_CTL);
3482}
3483
3484static const struct snd_kcontrol_new cap_vol_temp = {
3485 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3486 .name = "Capture Volume",
3487 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3488 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3489 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3490 .info = cap_vol_info,
3491 .get = cap_vol_get,
3492 .put = cap_vol_put,
3493 .tlv = { .c = cap_vol_tlv },
3494};
3495
3496/* capture switch ctl callbacks */
3497#define cap_sw_info snd_ctl_boolean_stereo_info
3498#define cap_sw_get snd_hda_mixer_amp_switch_get
3499
3500static int cap_sw_put(struct snd_kcontrol *kcontrol,
3501 struct snd_ctl_elem_value *ucontrol)
3502{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003503 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003504 snd_hda_mixer_amp_switch_put,
3505 NID_PATH_MUTE_CTL);
3506}
3507
3508static const struct snd_kcontrol_new cap_sw_temp = {
3509 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3510 .name = "Capture Switch",
3511 .info = cap_sw_info,
3512 .get = cap_sw_get,
3513 .put = cap_sw_put,
3514};
3515
3516static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3517{
3518 hda_nid_t nid;
3519 int i, depth;
3520
3521 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3522 for (depth = 0; depth < 3; depth++) {
3523 if (depth >= path->depth)
3524 return -EINVAL;
3525 i = path->depth - depth - 1;
3526 nid = path->path[i];
3527 if (!path->ctls[NID_PATH_VOL_CTL]) {
3528 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3529 path->ctls[NID_PATH_VOL_CTL] =
3530 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3531 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3532 int idx = path->idx[i];
3533 if (!depth && codec->single_adc_amp)
3534 idx = 0;
3535 path->ctls[NID_PATH_VOL_CTL] =
3536 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3537 }
3538 }
3539 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3540 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3541 path->ctls[NID_PATH_MUTE_CTL] =
3542 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3543 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3544 int idx = path->idx[i];
3545 if (!depth && codec->single_adc_amp)
3546 idx = 0;
3547 path->ctls[NID_PATH_MUTE_CTL] =
3548 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3549 }
3550 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 return 0;
3553}
3554
Takashi Iwai352f7f92012-12-19 12:52:06 +01003555static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003557 struct hda_gen_spec *spec = codec->spec;
3558 struct auto_pin_cfg *cfg = &spec->autocfg;
3559 unsigned int val;
3560 int i;
3561
3562 if (!spec->inv_dmic_split)
3563 return false;
3564 for (i = 0; i < cfg->num_inputs; i++) {
3565 if (cfg->inputs[i].pin != nid)
3566 continue;
3567 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3568 return false;
3569 val = snd_hda_codec_get_pincfg(codec, nid);
3570 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3571 }
3572 return false;
3573}
3574
Takashi Iwaia90229e2013-01-18 14:10:00 +01003575/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003576static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3577 struct snd_ctl_elem_value *ucontrol)
3578{
3579 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3580 struct hda_gen_spec *spec = codec->spec;
3581 int ret;
3582
3583 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3584 if (ret < 0)
3585 return ret;
3586
Takashi Iwaia90229e2013-01-18 14:10:00 +01003587 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003588 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003589
3590 return ret;
3591}
3592
Takashi Iwai352f7f92012-12-19 12:52:06 +01003593static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3594 int idx, bool is_switch, unsigned int ctl,
3595 bool inv_dmic)
3596{
3597 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003598 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003599 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3600 const char *sfx = is_switch ? "Switch" : "Volume";
3601 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003602 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003603
3604 if (!ctl)
3605 return 0;
3606
3607 if (label)
3608 snprintf(tmpname, sizeof(tmpname),
3609 "%s Capture %s", label, sfx);
3610 else
3611 snprintf(tmpname, sizeof(tmpname),
3612 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003613 knew = add_control(spec, type, tmpname, idx,
3614 amp_val_replace_channels(ctl, chs));
3615 if (!knew)
3616 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003617 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003618 knew->put = cap_single_sw_put;
3619 if (!inv_dmic)
3620 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003621
3622 /* Make independent right kcontrol */
3623 if (label)
3624 snprintf(tmpname, sizeof(tmpname),
3625 "Inverted %s Capture %s", label, sfx);
3626 else
3627 snprintf(tmpname, sizeof(tmpname),
3628 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003629 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003630 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003631 if (!knew)
3632 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003633 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003634 knew->put = cap_single_sw_put;
3635 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003636}
3637
3638/* create single (and simple) capture volume and switch controls */
3639static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3640 unsigned int vol_ctl, unsigned int sw_ctl,
3641 bool inv_dmic)
3642{
3643 int err;
3644 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3645 if (err < 0)
3646 return err;
3647 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3648 if (err < 0)
3649 return err;
3650 return 0;
3651}
3652
3653/* create bound capture volume and switch controls */
3654static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3655 unsigned int vol_ctl, unsigned int sw_ctl)
3656{
3657 struct hda_gen_spec *spec = codec->spec;
3658 struct snd_kcontrol_new *knew;
3659
3660 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003661 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003662 if (!knew)
3663 return -ENOMEM;
3664 knew->index = idx;
3665 knew->private_value = vol_ctl;
3666 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3667 }
3668 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003669 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003670 if (!knew)
3671 return -ENOMEM;
3672 knew->index = idx;
3673 knew->private_value = sw_ctl;
3674 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3675 }
3676 return 0;
3677}
3678
3679/* return the vol ctl when used first in the imux list */
3680static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3681{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003682 struct nid_path *path;
3683 unsigned int ctl;
3684 int i;
3685
Takashi Iwaic697b712013-01-07 17:09:26 +01003686 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003687 if (!path)
3688 return 0;
3689 ctl = path->ctls[type];
3690 if (!ctl)
3691 return 0;
3692 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003693 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003694 if (path && path->ctls[type] == ctl)
3695 return 0;
3696 }
3697 return ctl;
3698}
3699
3700/* create individual capture volume and switch controls per input */
3701static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3702{
3703 struct hda_gen_spec *spec = codec->spec;
3704 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003705 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003706
3707 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003708 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003709 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003710
Takashi Iwaic9700422013-01-18 10:17:30 +01003711 idx = imux->items[i].index;
3712 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003713 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003714 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3715
3716 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003717 err = add_single_cap_ctl(codec,
3718 spec->input_labels[idx],
3719 spec->input_label_idxs[idx],
3720 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003721 get_first_cap_ctl(codec, i, type),
3722 inv_dmic);
3723 if (err < 0)
3724 return err;
3725 }
3726 }
3727 return 0;
3728}
3729
3730static int create_capture_mixers(struct hda_codec *codec)
3731{
3732 struct hda_gen_spec *spec = codec->spec;
3733 struct hda_input_mux *imux = &spec->input_mux;
3734 int i, n, nums, err;
3735
3736 if (spec->dyn_adc_switch)
3737 nums = 1;
3738 else
3739 nums = spec->num_adc_nids;
3740
3741 if (!spec->auto_mic && imux->num_items > 1) {
3742 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003743 const char *name;
3744 name = nums > 1 ? "Input Source" : "Capture Source";
3745 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003746 if (!knew)
3747 return -ENOMEM;
3748 knew->count = nums;
3749 }
3750
3751 for (n = 0; n < nums; n++) {
3752 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003753 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003754 bool inv_dmic = false;
3755 int vol, sw;
3756
3757 vol = sw = 0;
3758 for (i = 0; i < imux->num_items; i++) {
3759 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003760 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003761 if (!path)
3762 continue;
3763 parse_capvol_in_path(codec, path);
3764 if (!vol)
3765 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003766 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003767 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003768 if (!same_amp_caps(codec, vol,
3769 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3770 multi_cap_vol = true;
3771 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003772 if (!sw)
3773 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003774 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003775 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003776 if (!same_amp_caps(codec, sw,
3777 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3778 multi_cap_vol = true;
3779 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003780 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3781 inv_dmic = true;
3782 }
3783
3784 if (!multi)
3785 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3786 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003787 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003788 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3789 else
3790 err = create_multi_cap_vol_ctl(codec);
3791 if (err < 0)
3792 return err;
3793 }
3794
3795 return 0;
3796}
3797
3798/*
3799 * add mic boosts if needed
3800 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003801
3802/* check whether the given amp is feasible as a boost volume */
3803static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3804 int dir, int idx)
3805{
3806 unsigned int step;
3807
3808 if (!nid_has_volume(codec, nid, dir) ||
3809 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3810 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3811 return false;
3812
3813 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3814 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3815 if (step < 0x20)
3816 return false;
3817 return true;
3818}
3819
3820/* look for a boost amp in a widget close to the pin */
3821static unsigned int look_for_boost_amp(struct hda_codec *codec,
3822 struct nid_path *path)
3823{
3824 unsigned int val = 0;
3825 hda_nid_t nid;
3826 int depth;
3827
3828 for (depth = 0; depth < 3; depth++) {
3829 if (depth >= path->depth - 1)
3830 break;
3831 nid = path->path[depth];
3832 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3833 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3834 break;
3835 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3836 path->idx[depth])) {
3837 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3838 HDA_INPUT);
3839 break;
3840 }
3841 }
3842
3843 return val;
3844}
3845
Takashi Iwai352f7f92012-12-19 12:52:06 +01003846static int parse_mic_boost(struct hda_codec *codec)
3847{
3848 struct hda_gen_spec *spec = codec->spec;
3849 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003850 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003851 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003852
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003853 if (!spec->num_adc_nids)
3854 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003855
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003856 for (i = 0; i < imux->num_items; i++) {
3857 struct nid_path *path;
3858 unsigned int val;
3859 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003860 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003861
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003862 idx = imux->items[i].index;
3863 if (idx >= imux->num_items)
3864 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003865
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003866 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003867 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003868 continue;
3869
3870 path = get_input_path(codec, 0, i);
3871 if (!path)
3872 continue;
3873
3874 val = look_for_boost_amp(codec, path);
3875 if (!val)
3876 continue;
3877
3878 /* create a boost control */
3879 snprintf(boost_label, sizeof(boost_label),
3880 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003881 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3882 spec->input_label_idxs[idx], val))
3883 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003884
3885 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003886 }
3887 return 0;
3888}
3889
3890/*
Takashi Iwaif567b782018-06-18 17:26:12 +02003891 * mic mute LED hook helpers
3892 */
3893enum {
3894 MICMUTE_LED_ON,
3895 MICMUTE_LED_OFF,
3896 MICMUTE_LED_FOLLOW_CAPTURE,
3897 MICMUTE_LED_FOLLOW_MUTE,
3898};
3899
3900static void call_micmute_led_update(struct hda_codec *codec)
3901{
3902 struct hda_gen_spec *spec = codec->spec;
3903 unsigned int val;
3904
3905 switch (spec->micmute_led.led_mode) {
3906 case MICMUTE_LED_ON:
3907 val = 1;
3908 break;
3909 case MICMUTE_LED_OFF:
3910 val = 0;
3911 break;
3912 case MICMUTE_LED_FOLLOW_CAPTURE:
Takashi Iwaic647f802018-06-19 12:42:03 +02003913 val = !!spec->micmute_led.capture;
Takashi Iwaif567b782018-06-18 17:26:12 +02003914 break;
3915 case MICMUTE_LED_FOLLOW_MUTE:
3916 default:
3917 val = !spec->micmute_led.capture;
3918 break;
3919 }
3920
3921 if (val == spec->micmute_led.led_value)
3922 return;
3923 spec->micmute_led.led_value = val;
3924 if (spec->micmute_led.update)
3925 spec->micmute_led.update(codec);
3926}
3927
3928static void update_micmute_led(struct hda_codec *codec,
3929 struct snd_kcontrol *kcontrol,
3930 struct snd_ctl_elem_value *ucontrol)
3931{
3932 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic647f802018-06-19 12:42:03 +02003933 unsigned int mask;
Takashi Iwaif567b782018-06-18 17:26:12 +02003934
3935 if (spec->micmute_led.old_hook)
3936 spec->micmute_led.old_hook(codec, kcontrol, ucontrol);
3937
3938 if (!ucontrol)
3939 return;
Takashi Iwaic647f802018-06-19 12:42:03 +02003940 mask = 1U << snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3941 if (!strcmp("Capture Switch", ucontrol->id.name)) {
Takashi Iwaif567b782018-06-18 17:26:12 +02003942 /* TODO: How do I verify if it's a mono or stereo here? */
Takashi Iwaic647f802018-06-19 12:42:03 +02003943 if (ucontrol->value.integer.value[0] ||
3944 ucontrol->value.integer.value[1])
3945 spec->micmute_led.capture |= mask;
3946 else
3947 spec->micmute_led.capture &= ~mask;
Takashi Iwaif567b782018-06-18 17:26:12 +02003948 call_micmute_led_update(codec);
3949 }
3950}
3951
3952static int micmute_led_mode_info(struct snd_kcontrol *kcontrol,
3953 struct snd_ctl_elem_info *uinfo)
3954{
3955 static const char * const texts[] = {
3956 "On", "Off", "Follow Capture", "Follow Mute",
3957 };
3958
3959 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
3960}
3961
3962static int micmute_led_mode_get(struct snd_kcontrol *kcontrol,
3963 struct snd_ctl_elem_value *ucontrol)
3964{
3965 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3966 struct hda_gen_spec *spec = codec->spec;
3967
3968 ucontrol->value.enumerated.item[0] = spec->micmute_led.led_mode;
3969 return 0;
3970}
3971
3972static int micmute_led_mode_put(struct snd_kcontrol *kcontrol,
3973 struct snd_ctl_elem_value *ucontrol)
3974{
3975 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3976 struct hda_gen_spec *spec = codec->spec;
3977 unsigned int mode;
3978
3979 mode = ucontrol->value.enumerated.item[0];
3980 if (mode > MICMUTE_LED_FOLLOW_MUTE)
3981 mode = MICMUTE_LED_FOLLOW_MUTE;
3982 if (mode == spec->micmute_led.led_mode)
3983 return 0;
3984 spec->micmute_led.led_mode = mode;
3985 call_micmute_led_update(codec);
3986 return 1;
3987}
3988
3989static const struct snd_kcontrol_new micmute_led_mode_ctl = {
3990 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3991 .name = "Mic Mute-LED Mode",
3992 .info = micmute_led_mode_info,
3993 .get = micmute_led_mode_get,
3994 .put = micmute_led_mode_put,
3995};
3996
3997/**
3998 * snd_hda_gen_add_micmute_led - helper for setting up mic mute LED hook
3999 * @codec: the HDA codec
4000 * @hook: the callback for updating LED
4001 *
4002 * Called from the codec drivers for offering the mic mute LED controls.
Takashi Iwaic647f802018-06-19 12:42:03 +02004003 * When established, it sets up cap_sync_hook and triggers the callback at
4004 * each time when the capture mixer switch changes. The callback is supposed
4005 * to update the LED accordingly.
Takashi Iwaif567b782018-06-18 17:26:12 +02004006 *
Takashi Iwaic647f802018-06-19 12:42:03 +02004007 * Returns 0 if the hook is established or a negative error code.
Takashi Iwaif567b782018-06-18 17:26:12 +02004008 */
4009int snd_hda_gen_add_micmute_led(struct hda_codec *codec,
4010 void (*hook)(struct hda_codec *))
4011{
4012 struct hda_gen_spec *spec = codec->spec;
4013
Takashi Iwaif567b782018-06-18 17:26:12 +02004014 spec->micmute_led.led_mode = MICMUTE_LED_FOLLOW_MUTE;
4015 spec->micmute_led.capture = 0;
4016 spec->micmute_led.led_value = 0;
4017 spec->micmute_led.old_hook = spec->cap_sync_hook;
4018 spec->micmute_led.update = hook;
4019 spec->cap_sync_hook = update_micmute_led;
4020 if (!snd_hda_gen_add_kctl(spec, NULL, &micmute_led_mode_ctl))
4021 return -ENOMEM;
Takashi Iwaic647f802018-06-19 12:42:03 +02004022 return 0;
Takashi Iwaif567b782018-06-18 17:26:12 +02004023}
4024EXPORT_SYMBOL_GPL(snd_hda_gen_add_micmute_led);
4025
Takashi Iwaib3802782018-11-26 17:47:46 +01004026#if IS_REACHABLE(CONFIG_LEDS_TRIGGER_AUDIO)
4027static void call_ledtrig_micmute(struct hda_codec *codec)
4028{
4029 struct hda_gen_spec *spec = codec->spec;
4030
4031 ledtrig_audio_set(LED_AUDIO_MICMUTE,
4032 spec->micmute_led.led_value ? LED_ON : LED_OFF);
4033}
4034#endif
4035
4036/**
4037 * snd_hda_gen_fixup_micmute_led - A fixup for mic-mute LED trigger
4038 *
4039 * Pass this function to the quirk entry if another driver supports the
4040 * audio mic-mute LED trigger. Then this will bind the mixer capture switch
4041 * change with the LED.
4042 *
4043 * Note that this fixup has to be called after other fixup that sets
4044 * cap_sync_hook. Otherwise the chaining wouldn't work.
4045 */
4046void snd_hda_gen_fixup_micmute_led(struct hda_codec *codec,
4047 const struct hda_fixup *fix, int action)
4048{
4049#if IS_REACHABLE(CONFIG_LEDS_TRIGGER_AUDIO)
4050 if (action == HDA_FIXUP_ACT_PROBE)
4051 snd_hda_gen_add_micmute_led(codec, call_ledtrig_micmute);
4052#endif
4053}
4054EXPORT_SYMBOL_GPL(snd_hda_gen_fixup_micmute_led);
4055
Takashi Iwaif567b782018-06-18 17:26:12 +02004056/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004057 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
4058 */
4059static void parse_digital(struct hda_codec *codec)
4060{
4061 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01004062 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004063 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004064 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004065
4066 /* support multiple SPDIFs; the secondary is set up as a slave */
4067 nums = 0;
4068 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01004069 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01004070 dig_nid = look_for_dac(codec, pin, true);
4071 if (!dig_nid)
4072 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01004073 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01004074 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004075 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004076 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004077 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01004078 path->pin_fixed = true; /* no jack detection */
Takashi Iwai196c17662013-01-04 15:01:40 +01004079 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01004080 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004081 if (!nums) {
4082 spec->multiout.dig_out_nid = dig_nid;
4083 spec->dig_out_type = spec->autocfg.dig_out_type[0];
4084 } else {
4085 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
4086 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
Dan Carpenterd576422e2014-05-14 17:18:31 +03004087 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004088 spec->slave_dig_outs[nums - 1] = dig_nid;
4089 }
4090 nums++;
4091 }
4092
4093 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01004094 pin = spec->autocfg.dig_in_pin;
Takashi Iwai7639a062015-03-03 10:07:24 +01004095 for_each_hda_codec_node(dig_nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004096 unsigned int wcaps = get_wcaps(codec, dig_nid);
4097 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
4098 continue;
4099 if (!(wcaps & AC_WCAP_DIGITAL))
4100 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004101 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004102 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004103 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004104 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01004105 path->pin_fixed = true; /* no jack */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004106 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004107 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01004108 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004109 break;
4110 }
4111 }
4112 }
4113}
4114
4115
4116/*
4117 * input MUX handling
4118 */
4119
4120static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
4121
4122/* select the given imux item; either unmute exclusively or select the route */
4123static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
4124 unsigned int idx)
4125{
4126 struct hda_gen_spec *spec = codec->spec;
4127 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01004128 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004129
4130 imux = &spec->input_mux;
4131 if (!imux->num_items)
4132 return 0;
4133
4134 if (idx >= imux->num_items)
4135 idx = imux->num_items - 1;
4136 if (spec->cur_mux[adc_idx] == idx)
4137 return 0;
4138
Takashi Iwai55196ff2013-01-24 17:32:56 +01004139 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
4140 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004141 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01004142 if (old_path->active)
4143 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004144
4145 spec->cur_mux[adc_idx] = idx;
4146
Takashi Iwai967303d2013-02-19 17:12:42 +01004147 if (spec->hp_mic)
4148 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004149
4150 if (spec->dyn_adc_switch)
4151 dyn_adc_pcm_resetup(codec, idx);
4152
Takashi Iwaic697b712013-01-07 17:09:26 +01004153 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004154 if (!path)
4155 return 0;
4156 if (path->active)
4157 return 0;
4158 snd_hda_activate_path(codec, path, true, false);
4159 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01004160 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004161 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004162 return 1;
4163}
4164
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004165/* power up/down widgets in the all paths that match with the given NID
4166 * as terminals (either start- or endpoint)
4167 *
4168 * returns the last changed NID, or zero if unchanged.
4169 */
4170static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
4171 int pin_state, int stream_state)
4172{
4173 struct hda_gen_spec *spec = codec->spec;
4174 hda_nid_t last, changed = 0;
4175 struct nid_path *path;
4176 int n;
4177
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +02004178 snd_array_for_each(&spec->paths, n, path) {
Bob Copeland81e43962016-06-25 07:58:45 -04004179 if (!path->depth)
4180 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004181 if (path->path[0] == nid ||
4182 path->path[path->depth - 1] == nid) {
4183 bool pin_old = path->pin_enabled;
4184 bool stream_old = path->stream_enabled;
4185
4186 if (pin_state >= 0)
4187 path->pin_enabled = pin_state;
4188 if (stream_state >= 0)
4189 path->stream_enabled = stream_state;
Takashi Iwai6b275b12015-03-20 18:11:05 +01004190 if ((!path->pin_fixed && path->pin_enabled != pin_old)
4191 || path->stream_enabled != stream_old) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004192 last = path_power_update(codec, path, true);
4193 if (last)
4194 changed = last;
4195 }
4196 }
4197 }
4198 return changed;
4199}
4200
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004201/* check the jack status for power control */
4202static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4203{
4204 if (!is_jack_detectable(codec, pin))
4205 return true;
4206 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4207}
4208
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004209/* power up/down the paths of the given pin according to the jack state;
4210 * power = 0/1 : only power up/down if it matches with the jack state,
4211 * < 0 : force power up/down to follow the jack sate
4212 *
4213 * returns the last changed NID, or zero if unchanged.
4214 */
4215static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4216 int power)
4217{
4218 bool on;
4219
Takashi Iwai967b1302015-03-20 18:21:03 +01004220 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004221 return 0;
4222
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004223 on = detect_pin_state(codec, pin);
4224
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004225 if (power >= 0 && on != power)
4226 return 0;
4227 return set_path_power(codec, pin, on, -1);
4228}
4229
4230static void pin_power_callback(struct hda_codec *codec,
4231 struct hda_jack_callback *jack,
4232 bool on)
4233{
Takashi Iwai2ebab402016-02-09 10:23:52 +01004234 if (jack && jack->nid)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004235 sync_power_state_change(codec,
Takashi Iwai2ebab402016-02-09 10:23:52 +01004236 set_pin_power_jack(codec, jack->nid, on));
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004237}
4238
4239/* callback only doing power up -- called at first */
4240static void pin_power_up_callback(struct hda_codec *codec,
4241 struct hda_jack_callback *jack)
4242{
4243 pin_power_callback(codec, jack, true);
4244}
4245
4246/* callback only doing power down -- called at last */
4247static void pin_power_down_callback(struct hda_codec *codec,
4248 struct hda_jack_callback *jack)
4249{
4250 pin_power_callback(codec, jack, false);
4251}
4252
4253/* set up the power up/down callbacks */
4254static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4255 const hda_nid_t *pins, bool on)
4256{
4257 int i;
4258 hda_jack_callback_fn cb =
4259 on ? pin_power_up_callback : pin_power_down_callback;
4260
4261 for (i = 0; i < num_pins && pins[i]; i++) {
4262 if (is_jack_detectable(codec, pins[i]))
4263 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4264 else
4265 set_path_power(codec, pins[i], true, -1);
4266 }
4267}
4268
4269/* enabled power callback to each available I/O pin with jack detections;
4270 * the digital I/O pins are excluded because of the unreliable detectsion
4271 */
4272static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4273{
4274 struct hda_gen_spec *spec = codec->spec;
4275 struct auto_pin_cfg *cfg = &spec->autocfg;
4276 int i;
4277
Takashi Iwai967b1302015-03-20 18:21:03 +01004278 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004279 return;
4280 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4281 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4282 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4283 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4284 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4285 for (i = 0; i < cfg->num_inputs; i++)
4286 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4287}
4288
4289/* sync path power up/down with the jack states of given pins */
4290static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4291 const hda_nid_t *pins)
4292{
4293 int i;
4294
4295 for (i = 0; i < num_pins && pins[i]; i++)
4296 if (is_jack_detectable(codec, pins[i]))
4297 set_pin_power_jack(codec, pins[i], -1);
4298}
4299
4300/* sync path power up/down with pins; called at init and resume */
4301static void sync_all_pin_power_ctls(struct hda_codec *codec)
4302{
4303 struct hda_gen_spec *spec = codec->spec;
4304 struct auto_pin_cfg *cfg = &spec->autocfg;
4305 int i;
4306
Takashi Iwai967b1302015-03-20 18:21:03 +01004307 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004308 return;
4309 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4310 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4311 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4312 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4313 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4314 for (i = 0; i < cfg->num_inputs; i++)
4315 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4316}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004317
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004318/* add fake paths if not present yet */
4319static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4320 int num_pins, const hda_nid_t *pins)
4321{
4322 struct hda_gen_spec *spec = codec->spec;
4323 struct nid_path *path;
4324 int i;
4325
4326 for (i = 0; i < num_pins; i++) {
4327 if (!pins[i])
4328 break;
4329 if (get_nid_path(codec, nid, pins[i], 0))
4330 continue;
4331 path = snd_array_new(&spec->paths);
4332 if (!path)
4333 return -ENOMEM;
4334 memset(path, 0, sizeof(*path));
4335 path->depth = 2;
4336 path->path[0] = nid;
4337 path->path[1] = pins[i];
4338 path->active = true;
4339 }
4340 return 0;
4341}
4342
4343/* create fake paths to all outputs from beep */
4344static int add_fake_beep_paths(struct hda_codec *codec)
4345{
4346 struct hda_gen_spec *spec = codec->spec;
4347 struct auto_pin_cfg *cfg = &spec->autocfg;
4348 hda_nid_t nid = spec->beep_nid;
4349 int err;
4350
Takashi Iwai967b1302015-03-20 18:21:03 +01004351 if (!codec->power_save_node || !nid)
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004352 return 0;
4353 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4354 if (err < 0)
4355 return err;
4356 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4357 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4358 if (err < 0)
4359 return err;
4360 }
4361 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4362 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4363 cfg->speaker_pins);
4364 if (err < 0)
4365 return err;
4366 }
4367 return 0;
4368}
4369
4370/* power up/down beep widget and its output paths */
4371static void beep_power_hook(struct hda_beep *beep, bool on)
4372{
4373 set_path_power(beep->codec, beep->nid, -1, on);
4374}
4375
Takashi Iwai6b275b12015-03-20 18:11:05 +01004376/**
4377 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4378 * @codec: the HDA codec
4379 * @pin: NID of pin to fix
4380 */
4381int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4382{
4383 struct hda_gen_spec *spec = codec->spec;
4384 struct nid_path *path;
4385
4386 path = snd_array_new(&spec->paths);
4387 if (!path)
4388 return -ENOMEM;
4389 memset(path, 0, sizeof(*path));
4390 path->depth = 1;
4391 path->path[0] = pin;
4392 path->active = true;
4393 path->pin_fixed = true;
4394 path->stream_enabled = true;
4395 return 0;
4396}
4397EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4398
Takashi Iwai352f7f92012-12-19 12:52:06 +01004399/*
4400 * Jack detections for HP auto-mute and mic-switch
4401 */
4402
4403/* check each pin in the given array; returns true if any of them is plugged */
Michał Mirosławcaf3c042020-01-03 10:23:48 +01004404static bool detect_jacks(struct hda_codec *codec, int num_pins, const hda_nid_t *pins)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004405{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004406 int i;
4407 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004408
4409 for (i = 0; i < num_pins; i++) {
4410 hda_nid_t nid = pins[i];
4411 if (!nid)
4412 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01004413 /* don't detect pins retasked as inputs */
4414 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4415 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004416 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4417 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004418 }
4419 return present;
4420}
4421
4422/* standard HP/line-out auto-mute helper */
Michał Mirosławcaf3c042020-01-03 10:23:48 +01004423static void do_automute(struct hda_codec *codec, int num_pins, const hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004424 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004425{
4426 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004427 int i;
4428
4429 for (i = 0; i < num_pins; i++) {
4430 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01004431 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004432 if (!nid)
4433 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004434
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004435 oldval = snd_hda_codec_get_pin_target(codec, nid);
4436 if (oldval & PIN_IN)
4437 continue; /* no mute for inputs */
4438
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004439 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004440 struct nid_path *path;
4441 hda_nid_t mute_nid;
4442
4443 path = snd_hda_get_path_from_idx(codec, paths[i]);
4444 if (!path)
4445 continue;
4446 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4447 if (!mute_nid)
4448 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004449 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004450 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004451 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004452 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004453 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004454 } else {
4455 /* don't reset VREF value in case it's controlling
4456 * the amp (see alc861_fixup_asus_amp_vref_0f())
4457 */
4458 if (spec->keep_vref_in_automute)
4459 val = oldval & ~PIN_HP;
4460 else
4461 val = 0;
4462 if (!mute)
4463 val |= oldval;
4464 /* here we call update_pin_ctl() so that the pinctl is
4465 * changed without changing the pinctl target value;
4466 * the original target value will be still referred at
4467 * the init / resume again
4468 */
4469 update_pin_ctl(codec, nid, val);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004470 }
4471
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01004472 set_pin_eapd(codec, nid, !mute);
Takashi Iwai967b1302015-03-20 18:21:03 +01004473 if (codec->power_save_node) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004474 bool on = !mute;
4475 if (on)
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004476 on = detect_pin_state(codec, nid);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004477 set_path_power(codec, nid, on, -1);
4478 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004479 }
4480}
4481
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004482/**
4483 * snd_hda_gen_update_outputs - Toggle outputs muting
4484 * @codec: the HDA codec
4485 *
4486 * Update the mute status of all outputs based on the current jack states.
4487 */
Takashi Iwai5d550e12012-12-19 15:16:44 +01004488void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004489{
4490 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004491 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004492 int on;
4493
4494 /* Control HP pins/amps depending on master_mute state;
4495 * in general, HP pins/amps control should be enabled in all cases,
4496 * but currently set only for master_mute, just to be safe
4497 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004498 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4499 paths = spec->out_paths;
4500 else
4501 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01004502 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004503 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004504
4505 if (!spec->automute_speaker)
4506 on = 0;
4507 else
4508 on = spec->hp_jack_present | spec->line_jack_present;
4509 on |= spec->master_mute;
Takashi Iwai47b9ddb82013-01-16 18:18:00 +01004510 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004511 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4512 paths = spec->out_paths;
4513 else
4514 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004515 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004516 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004517
4518 /* toggle line-out mutes if needed, too */
4519 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4520 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4521 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4522 return;
4523 if (!spec->automute_lo)
4524 on = 0;
4525 else
4526 on = spec->hp_jack_present;
4527 on |= spec->master_mute;
Takashi Iwai47b9ddb82013-01-16 18:18:00 +01004528 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004529 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004530 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004531 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004532}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004533EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004534
4535static void call_update_outputs(struct hda_codec *codec)
4536{
4537 struct hda_gen_spec *spec = codec->spec;
4538 if (spec->automute_hook)
4539 spec->automute_hook(codec);
4540 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01004541 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004542
4543 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4544 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4545 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004546}
4547
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004548/**
4549 * snd_hda_gen_hp_automute - standard HP-automute helper
4550 * @codec: the HDA codec
4551 * @jack: jack object, NULL for the whole
4552 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004553void snd_hda_gen_hp_automute(struct hda_codec *codec,
4554 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004555{
4556 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01004557 hda_nid_t *pins = spec->autocfg.hp_pins;
4558 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004559
Takashi Iwai92603c52013-01-22 07:46:31 +01004560 /* No detection for the first HP jack during indep-HP mode */
4561 if (spec->indep_hp_enabled) {
4562 pins++;
4563 num_pins--;
4564 }
4565
4566 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004567 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4568 return;
4569 call_update_outputs(codec);
4570}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004571EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004572
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004573/**
4574 * snd_hda_gen_line_automute - standard line-out-automute helper
4575 * @codec: the HDA codec
4576 * @jack: jack object, NULL for the whole
4577 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004578void snd_hda_gen_line_automute(struct hda_codec *codec,
4579 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004580{
4581 struct hda_gen_spec *spec = codec->spec;
4582
4583 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4584 return;
4585 /* check LO jack only when it's different from HP */
4586 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4587 return;
4588
4589 spec->line_jack_present =
4590 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4591 spec->autocfg.line_out_pins);
4592 if (!spec->automute_speaker || !spec->detect_lo)
4593 return;
4594 call_update_outputs(codec);
4595}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004596EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004597
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004598/**
4599 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4600 * @codec: the HDA codec
4601 * @jack: jack object, NULL for the whole
4602 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004603void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4604 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004605{
4606 struct hda_gen_spec *spec = codec->spec;
4607 int i;
4608
4609 if (!spec->auto_mic)
4610 return;
4611
4612 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01004613 hda_nid_t pin = spec->am_entry[i].pin;
4614 /* don't detect pins retasked as outputs */
4615 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4616 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004617 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004618 mux_select(codec, 0, spec->am_entry[i].idx);
4619 return;
4620 }
4621 }
4622 mux_select(codec, 0, spec->am_entry[0].idx);
4623}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004624EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004625
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004626/* call appropriate hooks */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004627static void call_hp_automute(struct hda_codec *codec,
4628 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004629{
4630 struct hda_gen_spec *spec = codec->spec;
4631 if (spec->hp_automute_hook)
4632 spec->hp_automute_hook(codec, jack);
4633 else
4634 snd_hda_gen_hp_automute(codec, jack);
4635}
4636
4637static void call_line_automute(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004638 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004639{
4640 struct hda_gen_spec *spec = codec->spec;
4641 if (spec->line_automute_hook)
4642 spec->line_automute_hook(codec, jack);
4643 else
4644 snd_hda_gen_line_automute(codec, jack);
4645}
4646
4647static void call_mic_autoswitch(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004648 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004649{
4650 struct hda_gen_spec *spec = codec->spec;
4651 if (spec->mic_autoswitch_hook)
4652 spec->mic_autoswitch_hook(codec, jack);
4653 else
4654 snd_hda_gen_mic_autoswitch(codec, jack);
4655}
4656
Takashi Iwai963afde2013-05-31 15:20:31 +02004657/* update jack retasking */
4658static void update_automute_all(struct hda_codec *codec)
4659{
4660 call_hp_automute(codec, NULL);
4661 call_line_automute(codec, NULL);
4662 call_mic_autoswitch(codec, NULL);
4663}
4664
Takashi Iwai352f7f92012-12-19 12:52:06 +01004665/*
4666 * Auto-Mute mode mixer enum support
4667 */
4668static int automute_mode_info(struct snd_kcontrol *kcontrol,
4669 struct snd_ctl_elem_info *uinfo)
4670{
4671 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4672 struct hda_gen_spec *spec = codec->spec;
4673 static const char * const texts3[] = {
4674 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004675 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
Takashi Iwai352f7f92012-12-19 12:52:06 +01004677 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4678 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4679 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4680}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681
Takashi Iwai352f7f92012-12-19 12:52:06 +01004682static int automute_mode_get(struct snd_kcontrol *kcontrol,
4683 struct snd_ctl_elem_value *ucontrol)
4684{
4685 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4686 struct hda_gen_spec *spec = codec->spec;
4687 unsigned int val = 0;
4688 if (spec->automute_speaker)
4689 val++;
4690 if (spec->automute_lo)
4691 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004692
Takashi Iwai352f7f92012-12-19 12:52:06 +01004693 ucontrol->value.enumerated.item[0] = val;
4694 return 0;
4695}
4696
4697static int automute_mode_put(struct snd_kcontrol *kcontrol,
4698 struct snd_ctl_elem_value *ucontrol)
4699{
4700 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4701 struct hda_gen_spec *spec = codec->spec;
4702
4703 switch (ucontrol->value.enumerated.item[0]) {
4704 case 0:
4705 if (!spec->automute_speaker && !spec->automute_lo)
4706 return 0;
4707 spec->automute_speaker = 0;
4708 spec->automute_lo = 0;
4709 break;
4710 case 1:
4711 if (spec->automute_speaker_possible) {
4712 if (!spec->automute_lo && spec->automute_speaker)
4713 return 0;
4714 spec->automute_speaker = 1;
4715 spec->automute_lo = 0;
4716 } else if (spec->automute_lo_possible) {
4717 if (spec->automute_lo)
4718 return 0;
4719 spec->automute_lo = 1;
4720 } else
4721 return -EINVAL;
4722 break;
4723 case 2:
4724 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4725 return -EINVAL;
4726 if (spec->automute_speaker && spec->automute_lo)
4727 return 0;
4728 spec->automute_speaker = 1;
4729 spec->automute_lo = 1;
4730 break;
4731 default:
4732 return -EINVAL;
4733 }
4734 call_update_outputs(codec);
4735 return 1;
4736}
4737
4738static const struct snd_kcontrol_new automute_mode_enum = {
4739 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4740 .name = "Auto-Mute Mode",
4741 .info = automute_mode_info,
4742 .get = automute_mode_get,
4743 .put = automute_mode_put,
4744};
4745
4746static int add_automute_mode_enum(struct hda_codec *codec)
4747{
4748 struct hda_gen_spec *spec = codec->spec;
4749
Takashi Iwai12c93df2012-12-19 14:38:33 +01004750 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004751 return -ENOMEM;
4752 return 0;
4753}
4754
4755/*
4756 * Check the availability of HP/line-out auto-mute;
4757 * Set up appropriately if really supported
4758 */
4759static int check_auto_mute_availability(struct hda_codec *codec)
4760{
4761 struct hda_gen_spec *spec = codec->spec;
4762 struct auto_pin_cfg *cfg = &spec->autocfg;
4763 int present = 0;
4764 int i, err;
4765
Takashi Iwaif72706b2013-01-16 18:20:07 +01004766 if (spec->suppress_auto_mute)
4767 return 0;
4768
Takashi Iwai352f7f92012-12-19 12:52:06 +01004769 if (cfg->hp_pins[0])
4770 present++;
4771 if (cfg->line_out_pins[0])
4772 present++;
4773 if (cfg->speaker_pins[0])
4774 present++;
4775 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004776 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004777
4778 if (!cfg->speaker_pins[0] &&
4779 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4780 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4781 sizeof(cfg->speaker_pins));
4782 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784
Takashi Iwai352f7f92012-12-19 12:52:06 +01004785 if (!cfg->hp_pins[0] &&
4786 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4787 memcpy(cfg->hp_pins, cfg->line_out_pins,
4788 sizeof(cfg->hp_pins));
4789 cfg->hp_outs = cfg->line_outs;
4790 }
4791
4792 for (i = 0; i < cfg->hp_outs; i++) {
4793 hda_nid_t nid = cfg->hp_pins[i];
4794 if (!is_jack_detectable(codec, nid))
4795 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004796 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai62f949b2014-09-11 14:06:53 +02004797 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004798 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004799 spec->detect_hp = 1;
4800 }
4801
4802 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4803 if (cfg->speaker_outs)
4804 for (i = 0; i < cfg->line_outs; i++) {
4805 hda_nid_t nid = cfg->line_out_pins[i];
4806 if (!is_jack_detectable(codec, nid))
4807 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004808 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004809 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004810 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004811 spec->detect_lo = 1;
4812 }
4813 spec->automute_lo_possible = spec->detect_hp;
4814 }
4815
4816 spec->automute_speaker_possible = cfg->speaker_outs &&
4817 (spec->detect_hp || spec->detect_lo);
4818
4819 spec->automute_lo = spec->automute_lo_possible;
4820 spec->automute_speaker = spec->automute_speaker_possible;
4821
4822 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4823 /* create a control for automute mode */
4824 err = add_automute_mode_enum(codec);
4825 if (err < 0)
4826 return err;
4827 }
4828 return 0;
4829}
4830
Takashi Iwai352f7f92012-12-19 12:52:06 +01004831/* check whether all auto-mic pins are valid; setup indices if OK */
4832static bool auto_mic_check_imux(struct hda_codec *codec)
4833{
4834 struct hda_gen_spec *spec = codec->spec;
4835 const struct hda_input_mux *imux;
4836 int i;
4837
4838 imux = &spec->input_mux;
4839 for (i = 0; i < spec->am_num_entries; i++) {
4840 spec->am_entry[i].idx =
4841 find_idx_in_nid_list(spec->am_entry[i].pin,
4842 spec->imux_pins, imux->num_items);
4843 if (spec->am_entry[i].idx < 0)
4844 return false; /* no corresponding imux */
4845 }
4846
4847 /* we don't need the jack detection for the first pin */
4848 for (i = 1; i < spec->am_num_entries; i++)
4849 snd_hda_jack_detect_enable_callback(codec,
4850 spec->am_entry[i].pin,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004851 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004852 return true;
4853}
4854
4855static int compare_attr(const void *ap, const void *bp)
4856{
4857 const struct automic_entry *a = ap;
4858 const struct automic_entry *b = bp;
4859 return (int)(a->attr - b->attr);
4860}
4861
4862/*
4863 * Check the availability of auto-mic switch;
4864 * Set up if really supported
4865 */
4866static int check_auto_mic_availability(struct hda_codec *codec)
4867{
4868 struct hda_gen_spec *spec = codec->spec;
4869 struct auto_pin_cfg *cfg = &spec->autocfg;
4870 unsigned int types;
4871 int i, num_pins;
4872
Takashi Iwaid12daf62013-01-07 16:32:11 +01004873 if (spec->suppress_auto_mic)
4874 return 0;
4875
Takashi Iwai352f7f92012-12-19 12:52:06 +01004876 types = 0;
4877 num_pins = 0;
4878 for (i = 0; i < cfg->num_inputs; i++) {
4879 hda_nid_t nid = cfg->inputs[i].pin;
4880 unsigned int attr;
4881 attr = snd_hda_codec_get_pincfg(codec, nid);
4882 attr = snd_hda_get_input_pin_attr(attr);
4883 if (types & (1 << attr))
4884 return 0; /* already occupied */
4885 switch (attr) {
4886 case INPUT_PIN_ATTR_INT:
4887 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4888 return 0; /* invalid type */
4889 break;
4890 case INPUT_PIN_ATTR_UNUSED:
4891 return 0; /* invalid entry */
4892 default:
4893 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4894 return 0; /* invalid type */
4895 if (!spec->line_in_auto_switch &&
4896 cfg->inputs[i].type != AUTO_PIN_MIC)
4897 return 0; /* only mic is allowed */
4898 if (!is_jack_detectable(codec, nid))
4899 return 0; /* no unsol support */
4900 break;
4901 }
4902 if (num_pins >= MAX_AUTO_MIC_PINS)
4903 return 0;
4904 types |= (1 << attr);
4905 spec->am_entry[num_pins].pin = nid;
4906 spec->am_entry[num_pins].attr = attr;
4907 num_pins++;
4908 }
4909
4910 if (num_pins < 2)
4911 return 0;
4912
4913 spec->am_num_entries = num_pins;
4914 /* sort the am_entry in the order of attr so that the pin with a
4915 * higher attr will be selected when the jack is plugged.
4916 */
4917 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4918 compare_attr, NULL);
4919
4920 if (!auto_mic_check_imux(codec))
4921 return 0;
4922
4923 spec->auto_mic = 1;
4924 spec->num_adc_nids = 1;
4925 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004926 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004927 spec->am_entry[0].pin,
4928 spec->am_entry[1].pin,
4929 spec->am_entry[2].pin);
4930
4931 return 0;
4932}
4933
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004934/**
4935 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4936 * into power down
4937 * @codec: the HDA codec
4938 * @nid: NID to evalute
4939 * @power_state: target power state
4940 */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004941unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004942 hda_nid_t nid,
4943 unsigned int power_state)
4944{
Takashi Iwaib6c09b32015-04-09 10:25:03 +02004945 struct hda_gen_spec *spec = codec->spec;
4946
4947 if (!spec->power_down_unused && !codec->power_save_node)
4948 return power_state;
Takashi Iwai7639a062015-03-03 10:07:24 +01004949 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004950 return power_state;
4951 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4952 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004953 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004954 return power_state;
4955 return AC_PWRST_D3;
4956}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004957EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004958
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004959/* mute all aamix inputs initially; parse up to the first leaves */
4960static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4961{
4962 int i, nums;
4963 const hda_nid_t *conn;
4964 bool has_amp;
4965
4966 nums = snd_hda_get_conn_list(codec, mix, &conn);
4967 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4968 for (i = 0; i < nums; i++) {
4969 if (has_amp)
Takashi Iwaief403ed2015-03-12 08:30:11 +01004970 update_amp(codec, mix, HDA_INPUT, i,
4971 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004972 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
Takashi Iwaief403ed2015-03-12 08:30:11 +01004973 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4974 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004975 }
4976}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004977
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004978/**
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004979 * snd_hda_gen_stream_pm - Stream power management callback
4980 * @codec: the HDA codec
4981 * @nid: audio widget
4982 * @on: power on/off flag
4983 *
Takashi Iwai967b1302015-03-20 18:21:03 +01004984 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004985 */
4986void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4987{
Takashi Iwai967b1302015-03-20 18:21:03 +01004988 if (codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004989 set_path_power(codec, nid, -1, on);
4990}
4991EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4992
4993/**
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004994 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4995 * set up the hda_gen_spec
4996 * @codec: the HDA codec
4997 * @cfg: Parsed pin configuration
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004998 *
4999 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005000 * or a negative error code
5001 */
5002int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005003 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005004{
5005 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005006 int err;
5007
Takashi Iwai1c70a582013-01-11 17:48:22 +01005008 parse_user_hints(codec);
5009
Takashi Iwaie4a395e2013-01-23 17:00:31 +01005010 if (spec->mixer_nid && !spec->mixer_merge_nid)
5011 spec->mixer_merge_nid = spec->mixer_nid;
5012
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005013 if (cfg != &spec->autocfg) {
5014 spec->autocfg = *cfg;
5015 cfg = &spec->autocfg;
5016 }
5017
Takashi Iwai98bd1112013-03-22 14:53:50 +01005018 if (!spec->main_out_badness)
5019 spec->main_out_badness = &hda_main_out_badness;
5020 if (!spec->extra_out_badness)
5021 spec->extra_out_badness = &hda_extra_out_badness;
5022
David Henningsson6fc4cb92013-01-16 15:58:45 +01005023 fill_all_dac_nids(codec);
5024
Takashi Iwai352f7f92012-12-19 12:52:06 +01005025 if (!cfg->line_outs) {
5026 if (cfg->dig_outs || cfg->dig_in_pin) {
5027 spec->multiout.max_channels = 2;
5028 spec->no_analog = 1;
5029 goto dig_only;
5030 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01005031 if (!cfg->num_inputs && !cfg->dig_in_pin)
5032 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005033 }
5034
5035 if (!spec->no_primary_hp &&
5036 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
5037 cfg->line_outs <= cfg->hp_outs) {
5038 /* use HP as primary out */
5039 cfg->speaker_outs = cfg->line_outs;
5040 memcpy(cfg->speaker_pins, cfg->line_out_pins,
5041 sizeof(cfg->speaker_pins));
5042 cfg->line_outs = cfg->hp_outs;
5043 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
5044 cfg->hp_outs = 0;
5045 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
5046 cfg->line_out_type = AUTO_PIN_HP_OUT;
5047 }
5048
5049 err = parse_output_paths(codec);
5050 if (err < 0)
5051 return err;
5052 err = create_multi_channel_mode(codec);
5053 if (err < 0)
5054 return err;
5055 err = create_multi_out_ctls(codec, cfg);
5056 if (err < 0)
5057 return err;
5058 err = create_hp_out_ctls(codec);
5059 if (err < 0)
5060 return err;
5061 err = create_speaker_out_ctls(codec);
5062 if (err < 0)
5063 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005064 err = create_indep_hp_ctls(codec);
5065 if (err < 0)
5066 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01005067 err = create_loopback_mixing_ctl(codec);
5068 if (err < 0)
5069 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01005070 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005071 if (err < 0)
5072 return err;
5073 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02005074 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02005075 return err;
5076
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01005077 /* add power-down pin callbacks at first */
5078 add_all_pin_power_ctls(codec, false);
5079
Takashi Iwaia07a9492013-01-07 16:44:06 +01005080 spec->const_channel_count = spec->ext_channel_count;
5081 /* check the multiple speaker and headphone pins */
5082 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
5083 spec->const_channel_count = max(spec->const_channel_count,
5084 cfg->speaker_outs * 2);
5085 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
5086 spec->const_channel_count = max(spec->const_channel_count,
5087 cfg->hp_outs * 2);
5088 spec->multiout.max_channels = max(spec->ext_channel_count,
5089 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005090
5091 err = check_auto_mute_availability(codec);
5092 if (err < 0)
5093 return err;
5094
5095 err = check_dyn_adc_switch(codec);
5096 if (err < 0)
5097 return err;
5098
Takashi Iwai967303d2013-02-19 17:12:42 +01005099 err = check_auto_mic_availability(codec);
5100 if (err < 0)
5101 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02005102
Takashi Iwaif1e762d2013-12-09 16:02:24 +01005103 /* add stereo mix if available and not enabled yet */
5104 if (!spec->auto_mic && spec->mixer_nid &&
Takashi Iwai74f14b32014-12-15 13:43:59 +01005105 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
5106 spec->input_mux.num_items > 1) {
Takashi Iwaif1e762d2013-12-09 16:02:24 +01005107 err = parse_capture_source(codec, spec->mixer_nid,
5108 CFG_IDX_MIX, spec->num_all_adcs,
5109 "Stereo Mix", 0);
5110 if (err < 0)
5111 return err;
5112 }
5113
5114
Takashi Iwai352f7f92012-12-19 12:52:06 +01005115 err = create_capture_mixers(codec);
5116 if (err < 0)
5117 return err;
5118
5119 err = parse_mic_boost(codec);
5120 if (err < 0)
5121 return err;
5122
Takashi Iwaiced4cef2013-11-26 08:33:45 +01005123 /* create "Headphone Mic Jack Mode" if no input selection is
5124 * available (or user specifies add_jack_modes hint)
5125 */
5126 if (spec->hp_mic_pin &&
5127 (spec->auto_mic || spec->input_mux.num_items == 1 ||
5128 spec->add_jack_modes)) {
5129 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
5130 if (err < 0)
5131 return err;
5132 }
5133
Takashi Iwaif811c3c2013-03-07 18:32:59 +01005134 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01005135 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
5136 err = create_out_jack_modes(codec, cfg->line_outs,
5137 cfg->line_out_pins);
5138 if (err < 0)
5139 return err;
5140 }
5141 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
5142 err = create_out_jack_modes(codec, cfg->hp_outs,
5143 cfg->hp_pins);
5144 if (err < 0)
5145 return err;
5146 }
5147 }
5148
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01005149 /* add power-up pin callbacks at last */
5150 add_all_pin_power_ctls(codec, true);
5151
Takashi Iwaiebb93c02013-12-10 17:33:49 +01005152 /* mute all aamix input initially */
5153 if (spec->mixer_nid)
5154 mute_all_mixer_nid(codec, spec->mixer_nid);
5155
Takashi Iwai352f7f92012-12-19 12:52:06 +01005156 dig_only:
5157 parse_digital(codec);
5158
Takashi Iwai49fb1892015-05-27 08:37:19 +02005159 if (spec->power_down_unused || codec->power_save_node) {
Takashi Iwai24fef902015-04-09 10:28:15 +02005160 if (!codec->power_filter)
5161 codec->power_filter = snd_hda_gen_path_power_filter;
Takashi Iwai49fb1892015-05-27 08:37:19 +02005162 if (!codec->patch_ops.stream_pm)
5163 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
5164 }
Takashi Iwai55196ff2013-01-24 17:32:56 +01005165
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005166 if (!spec->no_analog && spec->beep_nid) {
5167 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
5168 if (err < 0)
5169 return err;
Takashi Iwai967b1302015-03-20 18:21:03 +01005170 if (codec->beep && codec->power_save_node) {
Takashi Iwai5ccf8352015-03-18 09:23:10 +01005171 err = add_fake_beep_paths(codec);
5172 if (err < 0)
5173 return err;
5174 codec->beep->power_hook = beep_power_hook;
5175 }
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005176 }
5177
Takashi Iwai352f7f92012-12-19 12:52:06 +01005178 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005180EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181
5182
5183/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01005184 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005186
5187/* slave controls for virtual master */
5188static const char * const slave_pfxs[] = {
5189 "Front", "Surround", "Center", "LFE", "Side",
5190 "Headphone", "Speaker", "Mono", "Line Out",
5191 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01005192 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
5193 "Headphone Front", "Headphone Surround", "Headphone CLFE",
David Henningsson03ad6a82014-10-16 15:33:45 +02005194 "Headphone Side", "Headphone+LO", "Speaker+LO",
Takashi Iwai352f7f92012-12-19 12:52:06 +01005195 NULL,
5196};
5197
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005198/**
5199 * snd_hda_gen_build_controls - Build controls from the parsed results
5200 * @codec: the HDA codec
5201 *
5202 * Pass this to build_controls patch_ops.
5203 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005204int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005206 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208
Takashi Iwai36502d02012-12-19 15:15:10 +01005209 if (spec->kctls.used) {
5210 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5211 if (err < 0)
5212 return err;
5213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214
Takashi Iwai352f7f92012-12-19 12:52:06 +01005215 if (spec->multiout.dig_out_nid) {
5216 err = snd_hda_create_dig_out_ctls(codec,
5217 spec->multiout.dig_out_nid,
5218 spec->multiout.dig_out_nid,
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005219 spec->pcm_rec[1]->pcm_type);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005220 if (err < 0)
5221 return err;
5222 if (!spec->no_analog) {
5223 err = snd_hda_create_spdif_share_sw(codec,
5224 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 if (err < 0)
5226 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005227 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 }
5229 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005230 if (spec->dig_in_nid) {
5231 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5232 if (err < 0)
5233 return err;
5234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235
Takashi Iwai352f7f92012-12-19 12:52:06 +01005236 /* if we have no master control, let's create it */
Takashi Iwai74803162017-04-10 17:37:34 +02005237 if (!spec->no_analog && !spec->suppress_vmaster &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01005238 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01005239 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01005240 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005241 "Playback Volume");
5242 if (err < 0)
5243 return err;
5244 }
Takashi Iwai74803162017-04-10 17:37:34 +02005245 if (!spec->no_analog && !spec->suppress_vmaster &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01005246 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5247 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5248 NULL, slave_pfxs,
5249 "Playback Switch",
5250 true, &spec->vmaster_mute.sw_kctl);
5251 if (err < 0)
5252 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02005253 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01005254 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5255 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02005256 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5257 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259
Takashi Iwai352f7f92012-12-19 12:52:06 +01005260 free_kctls(spec); /* no longer needed */
5261
Takashi Iwai352f7f92012-12-19 12:52:06 +01005262 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5263 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 return err;
5265
5266 return 0;
5267}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005268EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005269
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270
5271/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01005272 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005275static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5276 struct hda_codec *codec,
5277 struct snd_pcm_substream *substream,
5278 int action)
5279{
5280 struct hda_gen_spec *spec = codec->spec;
5281 if (spec->pcm_playback_hook)
5282 spec->pcm_playback_hook(hinfo, codec, substream, action);
5283}
5284
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005285static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5286 struct hda_codec *codec,
5287 struct snd_pcm_substream *substream,
5288 int action)
5289{
5290 struct hda_gen_spec *spec = codec->spec;
5291 if (spec->pcm_capture_hook)
5292 spec->pcm_capture_hook(hinfo, codec, substream, action);
5293}
5294
Takashi Iwai352f7f92012-12-19 12:52:06 +01005295/*
5296 * Analog playback callbacks
5297 */
5298static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5299 struct hda_codec *codec,
5300 struct snd_pcm_substream *substream)
5301{
5302 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005303 int err;
5304
5305 mutex_lock(&spec->pcm_mutex);
5306 err = snd_hda_multi_out_analog_open(codec,
5307 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005308 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005309 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005310 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005311 call_pcm_playback_hook(hinfo, codec, substream,
5312 HDA_GEN_PCM_ACT_OPEN);
5313 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005314 mutex_unlock(&spec->pcm_mutex);
5315 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005316}
5317
5318static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01005319 struct hda_codec *codec,
5320 unsigned int stream_tag,
5321 unsigned int format,
5322 struct snd_pcm_substream *substream)
5323{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005324 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005325 int err;
5326
5327 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5328 stream_tag, format, substream);
5329 if (!err)
5330 call_pcm_playback_hook(hinfo, codec, substream,
5331 HDA_GEN_PCM_ACT_PREPARE);
5332 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005333}
Takashi Iwai97ec5582006-03-21 11:29:07 +01005334
Takashi Iwai352f7f92012-12-19 12:52:06 +01005335static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5336 struct hda_codec *codec,
5337 struct snd_pcm_substream *substream)
5338{
5339 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005340 int err;
5341
5342 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5343 if (!err)
5344 call_pcm_playback_hook(hinfo, codec, substream,
5345 HDA_GEN_PCM_ACT_CLEANUP);
5346 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005347}
5348
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005349static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5350 struct hda_codec *codec,
5351 struct snd_pcm_substream *substream)
5352{
5353 struct hda_gen_spec *spec = codec->spec;
5354 mutex_lock(&spec->pcm_mutex);
5355 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005356 call_pcm_playback_hook(hinfo, codec, substream,
5357 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005358 mutex_unlock(&spec->pcm_mutex);
5359 return 0;
5360}
5361
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005362static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5363 struct hda_codec *codec,
5364 struct snd_pcm_substream *substream)
5365{
5366 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5367 return 0;
5368}
5369
5370static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5371 struct hda_codec *codec,
5372 unsigned int stream_tag,
5373 unsigned int format,
5374 struct snd_pcm_substream *substream)
5375{
5376 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5377 call_pcm_capture_hook(hinfo, codec, substream,
5378 HDA_GEN_PCM_ACT_PREPARE);
5379 return 0;
5380}
5381
5382static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5383 struct hda_codec *codec,
5384 struct snd_pcm_substream *substream)
5385{
5386 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5387 call_pcm_capture_hook(hinfo, codec, substream,
5388 HDA_GEN_PCM_ACT_CLEANUP);
5389 return 0;
5390}
5391
5392static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5393 struct hda_codec *codec,
5394 struct snd_pcm_substream *substream)
5395{
5396 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5397 return 0;
5398}
5399
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005400static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5401 struct hda_codec *codec,
5402 struct snd_pcm_substream *substream)
5403{
5404 struct hda_gen_spec *spec = codec->spec;
5405 int err = 0;
5406
5407 mutex_lock(&spec->pcm_mutex);
Takashi Iwaid1f15e02015-07-08 09:22:10 +02005408 if (spec->indep_hp && !spec->indep_hp_enabled)
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005409 err = -EBUSY;
5410 else
5411 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005412 call_pcm_playback_hook(hinfo, codec, substream,
5413 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005414 mutex_unlock(&spec->pcm_mutex);
5415 return err;
5416}
5417
5418static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5419 struct hda_codec *codec,
5420 struct snd_pcm_substream *substream)
5421{
5422 struct hda_gen_spec *spec = codec->spec;
5423 mutex_lock(&spec->pcm_mutex);
5424 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005425 call_pcm_playback_hook(hinfo, codec, substream,
5426 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005427 mutex_unlock(&spec->pcm_mutex);
5428 return 0;
5429}
5430
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005431static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5432 struct hda_codec *codec,
5433 unsigned int stream_tag,
5434 unsigned int format,
5435 struct snd_pcm_substream *substream)
5436{
5437 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5438 call_pcm_playback_hook(hinfo, codec, substream,
5439 HDA_GEN_PCM_ACT_PREPARE);
5440 return 0;
5441}
5442
5443static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5444 struct hda_codec *codec,
5445 struct snd_pcm_substream *substream)
5446{
5447 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5448 call_pcm_playback_hook(hinfo, codec, substream,
5449 HDA_GEN_PCM_ACT_CLEANUP);
5450 return 0;
5451}
5452
Takashi Iwai352f7f92012-12-19 12:52:06 +01005453/*
5454 * Digital out
5455 */
5456static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5457 struct hda_codec *codec,
5458 struct snd_pcm_substream *substream)
5459{
5460 struct hda_gen_spec *spec = codec->spec;
5461 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5462}
5463
5464static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5465 struct hda_codec *codec,
5466 unsigned int stream_tag,
5467 unsigned int format,
5468 struct snd_pcm_substream *substream)
5469{
5470 struct hda_gen_spec *spec = codec->spec;
5471 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5472 stream_tag, format, substream);
5473}
5474
5475static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5476 struct hda_codec *codec,
5477 struct snd_pcm_substream *substream)
5478{
5479 struct hda_gen_spec *spec = codec->spec;
5480 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5481}
5482
5483static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5484 struct hda_codec *codec,
5485 struct snd_pcm_substream *substream)
5486{
5487 struct hda_gen_spec *spec = codec->spec;
5488 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5489}
5490
5491/*
5492 * Analog capture
5493 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005494#define alt_capture_pcm_open capture_pcm_open
5495#define alt_capture_pcm_close capture_pcm_close
5496
Takashi Iwai352f7f92012-12-19 12:52:06 +01005497static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5498 struct hda_codec *codec,
5499 unsigned int stream_tag,
5500 unsigned int format,
5501 struct snd_pcm_substream *substream)
5502{
5503 struct hda_gen_spec *spec = codec->spec;
5504
5505 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01005506 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005507 call_pcm_capture_hook(hinfo, codec, substream,
5508 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005509 return 0;
5510}
5511
Takashi Iwai352f7f92012-12-19 12:52:06 +01005512static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5513 struct hda_codec *codec,
5514 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01005515{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005516 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01005517
Takashi Iwai352f7f92012-12-19 12:52:06 +01005518 snd_hda_codec_cleanup_stream(codec,
5519 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005520 call_pcm_capture_hook(hinfo, codec, substream,
5521 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005522 return 0;
5523}
5524
Takashi Iwai352f7f92012-12-19 12:52:06 +01005525/*
5526 */
5527static const struct hda_pcm_stream pcm_analog_playback = {
5528 .substreams = 1,
5529 .channels_min = 2,
5530 .channels_max = 8,
5531 /* NID is set in build_pcms */
5532 .ops = {
5533 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005534 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005535 .prepare = playback_pcm_prepare,
5536 .cleanup = playback_pcm_cleanup
5537 },
5538};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539
Takashi Iwai352f7f92012-12-19 12:52:06 +01005540static const struct hda_pcm_stream pcm_analog_capture = {
5541 .substreams = 1,
5542 .channels_min = 2,
5543 .channels_max = 2,
5544 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005545 .ops = {
5546 .open = capture_pcm_open,
5547 .close = capture_pcm_close,
5548 .prepare = capture_pcm_prepare,
5549 .cleanup = capture_pcm_cleanup
5550 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005551};
5552
5553static const struct hda_pcm_stream pcm_analog_alt_playback = {
5554 .substreams = 1,
5555 .channels_min = 2,
5556 .channels_max = 2,
5557 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005558 .ops = {
5559 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005560 .close = alt_playback_pcm_close,
5561 .prepare = alt_playback_pcm_prepare,
5562 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005563 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005564};
5565
5566static const struct hda_pcm_stream pcm_analog_alt_capture = {
5567 .substreams = 2, /* can be overridden */
5568 .channels_min = 2,
5569 .channels_max = 2,
5570 /* NID is set in build_pcms */
5571 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005572 .open = alt_capture_pcm_open,
5573 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005574 .prepare = alt_capture_pcm_prepare,
5575 .cleanup = alt_capture_pcm_cleanup
5576 },
5577};
5578
5579static const struct hda_pcm_stream pcm_digital_playback = {
5580 .substreams = 1,
5581 .channels_min = 2,
5582 .channels_max = 2,
5583 /* NID is set in build_pcms */
5584 .ops = {
5585 .open = dig_playback_pcm_open,
5586 .close = dig_playback_pcm_close,
5587 .prepare = dig_playback_pcm_prepare,
5588 .cleanup = dig_playback_pcm_cleanup
5589 },
5590};
5591
5592static const struct hda_pcm_stream pcm_digital_capture = {
5593 .substreams = 1,
5594 .channels_min = 2,
5595 .channels_max = 2,
5596 /* NID is set in build_pcms */
5597};
5598
5599/* Used by build_pcms to flag that a PCM has no playback stream */
5600static const struct hda_pcm_stream pcm_null_stream = {
5601 .substreams = 0,
5602 .channels_min = 0,
5603 .channels_max = 0,
5604};
5605
5606/*
5607 * dynamic changing ADC PCM streams
5608 */
5609static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5610{
5611 struct hda_gen_spec *spec = codec->spec;
5612 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5613
5614 if (spec->cur_adc && spec->cur_adc != new_adc) {
5615 /* stream is running, let's swap the current ADC */
5616 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5617 spec->cur_adc = new_adc;
5618 snd_hda_codec_setup_stream(codec, new_adc,
5619 spec->cur_adc_stream_tag, 0,
5620 spec->cur_adc_format);
5621 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005623 return false;
5624}
5625
5626/* analog capture with dynamic dual-adc changes */
5627static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5628 struct hda_codec *codec,
5629 unsigned int stream_tag,
5630 unsigned int format,
5631 struct snd_pcm_substream *substream)
5632{
5633 struct hda_gen_spec *spec = codec->spec;
5634 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5635 spec->cur_adc_stream_tag = stream_tag;
5636 spec->cur_adc_format = format;
5637 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
Takashi Iwai4f29efc2016-04-12 15:16:24 +02005638 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005639 return 0;
5640}
5641
5642static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5643 struct hda_codec *codec,
5644 struct snd_pcm_substream *substream)
5645{
5646 struct hda_gen_spec *spec = codec->spec;
5647 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5648 spec->cur_adc = 0;
Takashi Iwai4f29efc2016-04-12 15:16:24 +02005649 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005650 return 0;
5651}
5652
5653static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5654 .substreams = 1,
5655 .channels_min = 2,
5656 .channels_max = 2,
5657 .nid = 0, /* fill later */
5658 .ops = {
5659 .prepare = dyn_adc_capture_pcm_prepare,
5660 .cleanup = dyn_adc_capture_pcm_cleanup
5661 },
5662};
5663
Takashi Iwaif873e532012-12-20 16:58:39 +01005664static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5665 const char *chip_name)
5666{
5667 char *p;
5668
5669 if (*str)
5670 return;
5671 strlcpy(str, chip_name, len);
5672
5673 /* drop non-alnum chars after a space */
5674 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5675 if (!isalnum(p[1])) {
5676 *p = 0;
5677 break;
5678 }
5679 }
5680 strlcat(str, sfx, len);
5681}
5682
Takashi Iwaifb83b632015-03-16 23:34:34 +01005683/* copy PCM stream info from @default_str, and override non-NULL entries
5684 * from @spec_str and @nid
5685 */
5686static void setup_pcm_stream(struct hda_pcm_stream *str,
5687 const struct hda_pcm_stream *default_str,
5688 const struct hda_pcm_stream *spec_str,
5689 hda_nid_t nid)
5690{
5691 *str = *default_str;
5692 if (nid)
5693 str->nid = nid;
5694 if (spec_str) {
5695 if (spec_str->substreams)
5696 str->substreams = spec_str->substreams;
5697 if (spec_str->channels_min)
5698 str->channels_min = spec_str->channels_min;
5699 if (spec_str->channels_max)
5700 str->channels_max = spec_str->channels_max;
5701 if (spec_str->rates)
5702 str->rates = spec_str->rates;
5703 if (spec_str->formats)
5704 str->formats = spec_str->formats;
5705 if (spec_str->maxbps)
5706 str->maxbps = spec_str->maxbps;
5707 }
5708}
5709
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005710/**
5711 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5712 * @codec: the HDA codec
5713 *
5714 * Pass this to build_pcms patch_ops.
5715 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005716int snd_hda_gen_build_pcms(struct hda_codec *codec)
5717{
5718 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005719 struct hda_pcm *info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005720 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721
Takashi Iwai352f7f92012-12-19 12:52:06 +01005722 if (spec->no_analog)
5723 goto skip_analog;
5724
Takashi Iwaif873e532012-12-20 16:58:39 +01005725 fill_pcm_stream_name(spec->stream_name_analog,
5726 sizeof(spec->stream_name_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005727 " Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005728 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5729 if (!info)
5730 return -ENOMEM;
5731 spec->pcm_rec[0] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005732
5733 if (spec->multiout.num_dacs > 0) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005734 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5735 &pcm_analog_playback,
5736 spec->stream_analog_playback,
5737 spec->multiout.dac_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005738 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5739 spec->multiout.max_channels;
5740 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5741 spec->autocfg.line_outs == 2)
5742 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5743 snd_pcm_2_1_chmaps;
5744 }
5745 if (spec->num_adc_nids) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005746 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5747 (spec->dyn_adc_switch ?
5748 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5749 spec->stream_analog_capture,
5750 spec->adc_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005751 }
5752
Takashi Iwai352f7f92012-12-19 12:52:06 +01005753 skip_analog:
5754 /* SPDIF for stream index #1 */
5755 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005756 fill_pcm_stream_name(spec->stream_name_digital,
5757 sizeof(spec->stream_name_digital),
Takashi Iwai7639a062015-03-03 10:07:24 +01005758 " Digital", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005759 info = snd_hda_codec_pcm_new(codec, "%s",
5760 spec->stream_name_digital);
5761 if (!info)
5762 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005763 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005764 spec->pcm_rec[1] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005765 if (spec->dig_out_type)
5766 info->pcm_type = spec->dig_out_type;
5767 else
5768 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005769 if (spec->multiout.dig_out_nid)
5770 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5771 &pcm_digital_playback,
5772 spec->stream_digital_playback,
5773 spec->multiout.dig_out_nid);
5774 if (spec->dig_in_nid)
5775 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5776 &pcm_digital_capture,
5777 spec->stream_digital_capture,
5778 spec->dig_in_nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005779 }
5780
5781 if (spec->no_analog)
5782 return 0;
5783
5784 /* If the use of more than one ADC is requested for the current
5785 * model, configure a second analog capture-only PCM.
5786 */
5787 have_multi_adcs = (spec->num_adc_nids > 1) &&
5788 !spec->dyn_adc_switch && !spec->auto_mic;
5789 /* Additional Analaog capture for index #2 */
5790 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005791 fill_pcm_stream_name(spec->stream_name_alt_analog,
5792 sizeof(spec->stream_name_alt_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005793 " Alt Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005794 info = snd_hda_codec_pcm_new(codec, "%s",
5795 spec->stream_name_alt_analog);
5796 if (!info)
5797 return -ENOMEM;
5798 spec->pcm_rec[2] = info;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005799 if (spec->alt_dac_nid)
5800 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5801 &pcm_analog_alt_playback,
5802 spec->stream_analog_alt_playback,
5803 spec->alt_dac_nid);
5804 else
5805 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5806 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005807 if (have_multi_adcs) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005808 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5809 &pcm_analog_alt_capture,
5810 spec->stream_analog_alt_capture,
5811 spec->adc_nids[1]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005812 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5813 spec->num_adc_nids - 1;
5814 } else {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005815 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5816 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 }
5819
5820 return 0;
5821}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005822EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005823
5824
5825/*
5826 * Standard auto-parser initializations
5827 */
5828
Takashi Iwaid4156932013-01-07 10:08:02 +01005829/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005830static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005831{
5832 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005833 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005834
Takashi Iwai196c17662013-01-04 15:01:40 +01005835 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005836 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005837 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005838 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005839 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005840 snd_hda_activate_path(codec, path, path->active,
5841 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005842 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005843}
5844
5845/* initialize primary output paths */
5846static void init_multi_out(struct hda_codec *codec)
5847{
5848 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005849 int i;
5850
Takashi Iwaid4156932013-01-07 10:08:02 +01005851 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005852 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005853}
5854
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005855
Takashi Iwai2c12c302013-01-10 09:33:29 +01005856static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005857{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005858 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005859
Takashi Iwaid4156932013-01-07 10:08:02 +01005860 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005861 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005862}
5863
5864/* initialize hp and speaker paths */
5865static void init_extra_out(struct hda_codec *codec)
5866{
5867 struct hda_gen_spec *spec = codec->spec;
5868
5869 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005870 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005871 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5872 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005873 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005874}
5875
5876/* initialize multi-io paths */
5877static void init_multi_io(struct hda_codec *codec)
5878{
5879 struct hda_gen_spec *spec = codec->spec;
5880 int i;
5881
5882 for (i = 0; i < spec->multi_ios; i++) {
5883 hda_nid_t pin = spec->multi_io[i].pin;
5884 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005885 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005886 if (!path)
5887 continue;
5888 if (!spec->multi_io[i].ctl_in)
5889 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005890 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005891 snd_hda_activate_path(codec, path, path->active,
5892 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005893 }
5894}
5895
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005896static void init_aamix_paths(struct hda_codec *codec)
5897{
5898 struct hda_gen_spec *spec = codec->spec;
5899
5900 if (!spec->have_aamix_ctl)
5901 return;
Takashi Iwaie7fdd522015-12-08 17:00:42 +01005902 if (!has_aamix_out_paths(spec))
5903 return;
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005904 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5905 spec->aamix_out_paths[0],
5906 spec->autocfg.line_out_type);
5907 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5908 spec->aamix_out_paths[1],
5909 AUTO_PIN_HP_OUT);
5910 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5911 spec->aamix_out_paths[2],
5912 AUTO_PIN_SPEAKER_OUT);
5913}
5914
Takashi Iwai352f7f92012-12-19 12:52:06 +01005915/* set up input pins and loopback paths */
5916static void init_analog_input(struct hda_codec *codec)
5917{
5918 struct hda_gen_spec *spec = codec->spec;
5919 struct auto_pin_cfg *cfg = &spec->autocfg;
5920 int i;
5921
5922 for (i = 0; i < cfg->num_inputs; i++) {
5923 hda_nid_t nid = cfg->inputs[i].pin;
5924 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005925 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005926
5927 /* init loopback inputs */
5928 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005929 resume_path_from_idx(codec, spec->loopback_paths[i]);
5930 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005931 }
5932 }
5933}
5934
5935/* initialize ADC paths */
5936static void init_input_src(struct hda_codec *codec)
5937{
5938 struct hda_gen_spec *spec = codec->spec;
5939 struct hda_input_mux *imux = &spec->input_mux;
5940 struct nid_path *path;
5941 int i, c, nums;
5942
5943 if (spec->dyn_adc_switch)
5944 nums = 1;
5945 else
5946 nums = spec->num_adc_nids;
5947
5948 for (c = 0; c < nums; c++) {
5949 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005950 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005951 if (path) {
5952 bool active = path->active;
5953 if (i == spec->cur_mux[c])
5954 active = true;
5955 snd_hda_activate_path(codec, path, active, false);
5956 }
5957 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005958 if (spec->hp_mic)
5959 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005960 }
5961
Takashi Iwai352f7f92012-12-19 12:52:06 +01005962 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005963 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005964}
5965
5966/* set right pin controls for digital I/O */
5967static void init_digital(struct hda_codec *codec)
5968{
5969 struct hda_gen_spec *spec = codec->spec;
5970 int i;
5971 hda_nid_t pin;
5972
Takashi Iwaid4156932013-01-07 10:08:02 +01005973 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005974 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005975 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005976 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005977 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005978 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005979 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005980}
5981
Takashi Iwai973e4972012-12-20 15:16:09 +01005982/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5983 * invalid unsol tags by some reason
5984 */
5985static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5986{
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +02005987 const struct hda_pincfg *pin;
Takashi Iwai973e4972012-12-20 15:16:09 +01005988 int i;
5989
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +02005990 snd_array_for_each(&codec->init_pins, i, pin) {
Takashi Iwai973e4972012-12-20 15:16:09 +01005991 hda_nid_t nid = pin->nid;
5992 if (is_jack_detectable(codec, nid) &&
5993 !snd_hda_jack_tbl_get(codec, nid))
Takashi Iwai401caff2018-06-27 11:43:09 +02005994 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai973e4972012-12-20 15:16:09 +01005995 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5996 }
5997}
5998
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005999/**
6000 * snd_hda_gen_init - initialize the generic spec
6001 * @codec: the HDA codec
6002 *
6003 * This can be put as patch_ops init function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01006004 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01006005int snd_hda_gen_init(struct hda_codec *codec)
6006{
6007 struct hda_gen_spec *spec = codec->spec;
6008
6009 if (spec->init_hook)
6010 spec->init_hook(codec);
6011
Takashi Iwai89781d02019-08-30 12:03:38 +02006012 if (!spec->skip_verbs)
6013 snd_hda_apply_verbs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01006014
6015 init_multi_out(codec);
6016 init_extra_out(codec);
6017 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01006018 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01006019 init_analog_input(codec);
6020 init_input_src(codec);
6021 init_digital(codec);
6022
Takashi Iwai973e4972012-12-20 15:16:09 +01006023 clear_unsol_on_unused_pins(codec);
6024
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01006025 sync_all_pin_power_ctls(codec);
6026
Takashi Iwai352f7f92012-12-19 12:52:06 +01006027 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01006028 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01006029
Takashi Iwai1a462be2020-01-09 10:01:04 +01006030 snd_hda_regmap_sync(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01006031
Takashi Iwai352f7f92012-12-19 12:52:06 +01006032 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
6033 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
6034
6035 hda_call_check_power_status(codec, 0x01);
6036 return 0;
6037}
Takashi Iwai2698ea92013-12-18 07:45:52 +01006038EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01006039
Takashi Iwaidda42bd2014-10-30 12:04:50 +01006040/**
6041 * snd_hda_gen_free - free the generic spec
6042 * @codec: the HDA codec
6043 *
6044 * This can be put as patch_ops free function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01006045 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01006046void snd_hda_gen_free(struct hda_codec *codec)
6047{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01006048 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01006049 snd_hda_gen_spec_free(codec->spec);
6050 kfree(codec->spec);
6051 codec->spec = NULL;
6052}
Takashi Iwai2698ea92013-12-18 07:45:52 +01006053EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01006054
Hui Wang871b9062019-08-14 12:09:08 +08006055/**
6056 * snd_hda_gen_reboot_notify - Make codec enter D3 before rebooting
6057 * @codec: the HDA codec
6058 *
6059 * This can be put as patch_ops reboot_notify function.
6060 */
6061void snd_hda_gen_reboot_notify(struct hda_codec *codec)
6062{
6063 /* Make the codec enter D3 to avoid spurious noises from the internal
6064 * speaker during (and after) reboot
6065 */
6066 snd_hda_codec_set_power_to_all(codec, codec->core.afg, AC_PWRST_D3);
6067 snd_hda_codec_write(codec, codec->core.afg, 0,
6068 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
6069 msleep(10);
6070}
6071EXPORT_SYMBOL_GPL(snd_hda_gen_reboot_notify);
6072
Takashi Iwaifce52a32013-01-07 12:42:48 +01006073#ifdef CONFIG_PM
Takashi Iwaidda42bd2014-10-30 12:04:50 +01006074/**
6075 * snd_hda_gen_check_power_status - check the loopback power save state
6076 * @codec: the HDA codec
6077 * @nid: NID to inspect
6078 *
6079 * This can be put as patch_ops check_power_status function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01006080 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01006081int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
6082{
6083 struct hda_gen_spec *spec = codec->spec;
6084 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
6085}
Takashi Iwai2698ea92013-12-18 07:45:52 +01006086EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01006087#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01006088
6089
6090/*
6091 * the generic codec support
6092 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
Takashi Iwai352f7f92012-12-19 12:52:06 +01006094static const struct hda_codec_ops generic_patch_ops = {
6095 .build_controls = snd_hda_gen_build_controls,
6096 .build_pcms = snd_hda_gen_build_pcms,
6097 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01006098 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01006099 .unsol_event = snd_hda_jack_unsol_event,
Hui Wang871b9062019-08-14 12:09:08 +08006100 .reboot_notify = snd_hda_gen_reboot_notify,
Takashi Iwai83012a72012-08-24 18:38:08 +02006101#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01006102 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02006103#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104};
6105
Takashi Iwaid8a766a2015-02-17 15:25:37 +01006106/*
Takashi Iwaidda42bd2014-10-30 12:04:50 +01006107 * snd_hda_parse_generic_codec - Generic codec parser
6108 * @codec: the HDA codec
Takashi Iwaidda42bd2014-10-30 12:04:50 +01006109 */
Takashi Iwaid8a766a2015-02-17 15:25:37 +01006110static int snd_hda_parse_generic_codec(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111{
Takashi Iwai352f7f92012-12-19 12:52:06 +01006112 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 int err;
6114
Takashi Iwaie560d8d2005-09-09 14:21:46 +02006115 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01006116 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01006118 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
Takashi Iwai9eb413e2012-12-19 14:41:21 +01006121 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
6122 if (err < 0)
Wenwen Wangcfef67f2019-08-09 23:29:48 -05006123 goto error;
Takashi Iwai9eb413e2012-12-19 14:41:21 +01006124
6125 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01006126 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 goto error;
6128
6129 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 return 0;
6131
Takashi Iwai352f7f92012-12-19 12:52:06 +01006132error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01006133 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 return err;
6135}
Takashi Iwaid8a766a2015-02-17 15:25:37 +01006136
Takashi Iwaib9a94a92015-10-01 16:20:04 +02006137static const struct hda_device_id snd_hda_id_generic[] = {
6138 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01006139 {} /* terminator */
6140};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02006141MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01006142
6143static struct hda_codec_driver generic_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02006144 .id = snd_hda_id_generic,
Takashi Iwaid8a766a2015-02-17 15:25:37 +01006145};
6146
6147module_hda_codec_driver(generic_driver);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01006148
6149MODULE_LICENSE("GPL");
6150MODULE_DESCRIPTION("Generic HD-audio codec parser");