blob: 3bf5e341070388864b8594d75a6959940190b453 [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
Randy Dunlapc7fabbc2020-08-05 19:19:26 -0700816/* sync power of each widget in the given path */
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100817static 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;
Jaroslav Kyselae65bf992021-03-17 18:29:43 +0100984 if (knew->access == 0)
985 knew->access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100986 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100987 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100988}
989
990static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
991 const char *pfx, const char *dir,
992 const char *sfx, int cidx, unsigned long val)
993{
Takashi Iwai975cc022013-06-28 11:56:49 +0200994 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100995 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100996 if (!add_control(spec, type, name, cidx, val))
997 return -ENOMEM;
998 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100999}
1000
1001#define add_pb_vol_ctrl(spec, type, pfx, val) \
1002 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1003#define add_pb_sw_ctrl(spec, type, pfx, val) \
1004 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1005#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1006 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1007#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1008 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1009
1010static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1011 unsigned int chs, struct nid_path *path)
1012{
1013 unsigned int val;
1014 if (!path)
1015 return 0;
1016 val = path->ctls[NID_PATH_VOL_CTL];
1017 if (!val)
1018 return 0;
1019 val = amp_val_replace_channels(val, chs);
1020 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1021}
1022
1023/* return the channel bits suitable for the given path->ctls[] */
1024static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1025 int type)
1026{
1027 int chs = 1; /* mono (left only) */
1028 if (path) {
1029 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1030 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1031 chs = 3; /* stereo */
1032 }
1033 return chs;
1034}
1035
1036static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1037 struct nid_path *path)
1038{
1039 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1040 return add_vol_ctl(codec, pfx, cidx, chs, path);
1041}
1042
1043/* create a mute-switch for the given mixer widget;
1044 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1045 */
1046static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1047 unsigned int chs, struct nid_path *path)
1048{
1049 unsigned int val;
1050 int type = HDA_CTL_WIDGET_MUTE;
1051
1052 if (!path)
1053 return 0;
1054 val = path->ctls[NID_PATH_MUTE_CTL];
1055 if (!val)
1056 return 0;
1057 val = amp_val_replace_channels(val, chs);
1058 if (get_amp_direction_(val) == HDA_INPUT) {
1059 hda_nid_t nid = get_amp_nid_(val);
1060 int nums = snd_hda_get_num_conns(codec, nid);
1061 if (nums > 1) {
1062 type = HDA_CTL_BIND_MUTE;
1063 val |= nums << 19;
1064 }
1065 }
1066 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1067}
1068
1069static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1070 int cidx, struct nid_path *path)
1071{
1072 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1073 return add_sw_ctl(codec, pfx, cidx, chs, path);
1074}
1075
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001076/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001077static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1078 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001079{
1080 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1081 struct hda_gen_spec *spec = codec->spec;
1082
1083 if (spec->auto_mute_via_amp) {
1084 hda_nid_t nid = get_amp_nid(kcontrol);
1085 bool enabled = !((spec->mute_bits >> nid) & 1);
1086 ucontrol->value.integer.value[0] &= enabled;
1087 ucontrol->value.integer.value[1] &= enabled;
1088 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001089}
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001090
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001091static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1092 struct snd_ctl_elem_value *ucontrol)
1093{
1094 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001095 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1096}
1097
Takashi Iwai698f5ee2017-05-10 12:13:45 +02001098/*
1099 * Bound mute controls
1100 */
1101#define AMP_VAL_IDX_SHIFT 19
1102#define AMP_VAL_IDX_MASK (0x0f<<19)
1103
1104static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
1105 struct snd_ctl_elem_value *ucontrol)
1106{
1107 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1108 unsigned long pval;
1109 int err;
1110
1111 mutex_lock(&codec->control_mutex);
1112 pval = kcontrol->private_value;
1113 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1114 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1115 kcontrol->private_value = pval;
1116 mutex_unlock(&codec->control_mutex);
1117 return err;
1118}
1119
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001120static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1121 struct snd_ctl_elem_value *ucontrol)
1122{
Takashi Iwai698f5ee2017-05-10 12:13:45 +02001123 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1124 unsigned long pval;
1125 int i, indices, err = 0, change = 0;
1126
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001127 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai698f5ee2017-05-10 12:13:45 +02001128
1129 mutex_lock(&codec->control_mutex);
1130 pval = kcontrol->private_value;
1131 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1132 for (i = 0; i < indices; i++) {
1133 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1134 (i << AMP_VAL_IDX_SHIFT);
1135 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1136 if (err < 0)
1137 break;
1138 change |= err;
1139 }
1140 kcontrol->private_value = pval;
1141 mutex_unlock(&codec->control_mutex);
1142 return err < 0 ? err : change;
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001143}
1144
Takashi Iwai247d85e2013-01-17 16:18:11 +01001145/* any ctl assigned to the path with the given index? */
1146static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1147{
1148 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1149 return path && path->ctls[ctl_type];
1150}
1151
Takashi Iwai352f7f92012-12-19 12:52:06 +01001152static const char * const channel_name[4] = {
1153 "Front", "Surround", "CLFE", "Side"
1154};
1155
1156/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +01001157static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1158 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001159{
Takashi Iwai247d85e2013-01-17 16:18:11 +01001160 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001161 struct auto_pin_cfg *cfg = &spec->autocfg;
1162
1163 *index = 0;
1164 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai9f3dadb2017-04-10 17:12:33 +02001165 !codec->force_pin_prefix &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001166 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001167 return spec->vmaster_mute.hook ? "PCM" : "Master";
1168
1169 /* if there is really a single DAC used in the whole output paths,
1170 * use it master (or "PCM" if a vmaster hook is present)
1171 */
1172 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
Takashi Iwai9f3dadb2017-04-10 17:12:33 +02001173 !codec->force_pin_prefix &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001174 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1175 return spec->vmaster_mute.hook ? "PCM" : "Master";
1176
Takashi Iwai247d85e2013-01-17 16:18:11 +01001177 /* multi-io channels */
1178 if (ch >= cfg->line_outs)
1179 return channel_name[ch];
1180
Takashi Iwai352f7f92012-12-19 12:52:06 +01001181 switch (cfg->line_out_type) {
1182 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001183 /* if the primary channel vol/mute is shared with HP volume,
1184 * don't name it as Speaker
1185 */
1186 if (!ch && cfg->hp_outs &&
1187 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1188 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001189 if (cfg->line_outs == 1)
1190 return "Speaker";
1191 if (cfg->line_outs == 2)
1192 return ch ? "Bass Speaker" : "Speaker";
1193 break;
1194 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001195 /* if the primary channel vol/mute is shared with spk volume,
1196 * don't name it as Headphone
1197 */
1198 if (!ch && cfg->speaker_outs &&
1199 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1200 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001201 /* for multi-io case, only the primary out */
1202 if (ch && spec->multi_ios)
1203 break;
1204 *index = ch;
1205 return "Headphone";
David Henningsson03ad6a82014-10-16 15:33:45 +02001206 case AUTO_PIN_LINE_OUT:
Hui Wangf48652b2021-05-04 15:39:17 +08001207 /* This deals with the case where one HP or one Speaker or
1208 * one HP + one Speaker need to share the DAC with LO
1209 */
1210 if (!ch) {
1211 bool hp_lo_shared = false, spk_lo_shared = false;
1212
1213 if (cfg->speaker_outs)
1214 spk_lo_shared = !path_has_mixer(codec,
1215 spec->speaker_paths[0], ctl_type);
1216 if (cfg->hp_outs)
1217 hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
David Henningsson03ad6a82014-10-16 15:33:45 +02001218 if (hp_lo_shared && spk_lo_shared)
1219 return spec->vmaster_mute.hook ? "PCM" : "Master";
1220 if (hp_lo_shared)
1221 return "Headphone+LO";
1222 if (spk_lo_shared)
1223 return "Speaker+LO";
1224 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001225 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001226
1227 /* for a single channel output, we don't have to name the channel */
1228 if (cfg->line_outs == 1 && !spec->multi_ios)
David Henningsson3abb4f42014-10-16 15:33:46 +02001229 return "Line Out";
Takashi Iwai247d85e2013-01-17 16:18:11 +01001230
Takashi Iwai352f7f92012-12-19 12:52:06 +01001231 if (ch >= ARRAY_SIZE(channel_name)) {
1232 snd_BUG();
1233 return "PCM";
1234 }
1235
1236 return channel_name[ch];
1237}
1238
1239/*
1240 * Parse output paths
1241 */
1242
1243/* badness definition */
1244enum {
1245 /* No primary DAC is found for the main output */
1246 BAD_NO_PRIMARY_DAC = 0x10000,
1247 /* No DAC is found for the extra output */
1248 BAD_NO_DAC = 0x4000,
1249 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001250 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001251 /* No individual DAC for extra output */
1252 BAD_NO_EXTRA_DAC = 0x102,
1253 /* No individual DAC for extra surrounds */
1254 BAD_NO_EXTRA_SURR_DAC = 0x101,
1255 /* Primary DAC shared with main surrounds */
1256 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001257 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001258 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001259 /* Primary DAC shared with main CLFE */
1260 BAD_SHARED_CLFE = 0x10,
1261 /* Primary DAC shared with extra surrounds */
1262 BAD_SHARED_EXTRA_SURROUND = 0x10,
1263 /* Volume widget is shared */
1264 BAD_SHARED_VOL = 0x10,
1265};
1266
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001267/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001268 * volume and mute controls, and assign the values to ctls[].
1269 *
1270 * When no appropriate widget is found in the path, the badness value
1271 * is incremented depending on the situation. The function returns the
1272 * total badness for both volume and mute controls.
1273 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001274static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001275{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001276 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001277 hda_nid_t nid;
1278 unsigned int val;
1279 int badness = 0;
1280
1281 if (!path)
1282 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001283
1284 if (path->ctls[NID_PATH_VOL_CTL] ||
1285 path->ctls[NID_PATH_MUTE_CTL])
1286 return 0; /* already evaluated */
1287
Takashi Iwai352f7f92012-12-19 12:52:06 +01001288 nid = look_for_out_vol_nid(codec, path);
1289 if (nid) {
1290 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001291 if (spec->dac_min_mute)
1292 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001293 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1294 badness += BAD_SHARED_VOL;
1295 else
1296 path->ctls[NID_PATH_VOL_CTL] = val;
1297 } else
1298 badness += BAD_SHARED_VOL;
1299 nid = look_for_out_mute_nid(codec, path);
1300 if (nid) {
1301 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1302 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1303 nid_has_mute(codec, nid, HDA_OUTPUT))
1304 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1305 else
1306 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1307 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1308 badness += BAD_SHARED_VOL;
1309 else
1310 path->ctls[NID_PATH_MUTE_CTL] = val;
1311 } else
1312 badness += BAD_SHARED_VOL;
1313 return badness;
1314}
1315
Takashi Iwai98bd1112013-03-22 14:53:50 +01001316const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001317 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1318 .no_dac = BAD_NO_DAC,
1319 .shared_primary = BAD_NO_PRIMARY_DAC,
1320 .shared_surr = BAD_SHARED_SURROUND,
1321 .shared_clfe = BAD_SHARED_CLFE,
1322 .shared_surr_main = BAD_SHARED_SURROUND,
1323};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001324EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001325
Takashi Iwai98bd1112013-03-22 14:53:50 +01001326const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001327 .no_primary_dac = BAD_NO_DAC,
1328 .no_dac = BAD_NO_DAC,
1329 .shared_primary = BAD_NO_EXTRA_DAC,
1330 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1331 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1332 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1333};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001334EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001335
Takashi Iwai7385df62013-01-07 09:50:52 +01001336/* get the DAC of the primary output corresponding to the given array index */
1337static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1338{
1339 struct hda_gen_spec *spec = codec->spec;
1340 struct auto_pin_cfg *cfg = &spec->autocfg;
1341
1342 if (cfg->line_outs > idx)
1343 return spec->private_dac_nids[idx];
1344 idx -= cfg->line_outs;
1345 if (spec->multi_ios > idx)
1346 return spec->multi_io[idx].dac;
1347 return 0;
1348}
1349
1350/* return the DAC if it's reachable, otherwise zero */
1351static inline hda_nid_t try_dac(struct hda_codec *codec,
1352 hda_nid_t dac, hda_nid_t pin)
1353{
1354 return is_reachable_path(codec, dac, pin) ? dac : 0;
1355}
1356
Takashi Iwai352f7f92012-12-19 12:52:06 +01001357/* try to assign DACs to pins and return the resultant badness */
1358static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1359 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001360 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001361 const struct badness_table *bad)
1362{
1363 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001364 int i, j;
1365 int badness = 0;
1366 hda_nid_t dac;
1367
1368 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return 0;
1370
Takashi Iwai352f7f92012-12-19 12:52:06 +01001371 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001372 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001373 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001374
Takashi Iwai242d9902020-11-27 15:11:03 +01001375 if (!spec->obey_preferred_dacs) {
1376 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1377 if (path) {
1378 badness += assign_out_path_ctls(codec, path);
1379 continue;
1380 }
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001381 }
1382
Takashi Iwai36907392013-12-10 17:29:26 +01001383 dacs[i] = get_preferred_dac(codec, pin);
1384 if (dacs[i]) {
1385 if (is_dac_already_used(codec, dacs[i]))
1386 badness += bad->shared_primary;
Takashi Iwai242d9902020-11-27 15:11:03 +01001387 } else if (spec->obey_preferred_dacs) {
1388 badness += BAD_NO_PRIMARY_DAC;
Takashi Iwai36907392013-12-10 17:29:26 +01001389 }
1390
1391 if (!dacs[i])
1392 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001393 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001394 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001395 for (j = 1; j < num_outs; j++) {
1396 if (is_reachable_path(codec, dacs[j], pin)) {
1397 dacs[0] = dacs[j];
1398 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001399 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001400 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001401 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001404 }
1405 dac = dacs[i];
1406 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001407 if (num_outs > 2)
1408 dac = try_dac(codec, get_primary_out(codec, i), pin);
1409 if (!dac)
1410 dac = try_dac(codec, dacs[0], pin);
1411 if (!dac)
1412 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001413 if (dac) {
1414 if (!i)
1415 badness += bad->shared_primary;
1416 else if (i == 1)
1417 badness += bad->shared_surr;
1418 else
1419 badness += bad->shared_clfe;
1420 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1421 dac = spec->private_dac_nids[0];
1422 badness += bad->shared_surr_main;
1423 } else if (!i)
1424 badness += bad->no_primary_dac;
1425 else
1426 badness += bad->no_dac;
1427 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001428 if (!dac)
1429 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001430 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001431 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001432 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001433 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001434 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001435 if (!path) {
Jiapeng Chonge73b4c92021-05-13 19:11:11 +08001436 dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001437 badness += bad->no_dac;
1438 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001439 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001440 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001441 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001442 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001443 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001444 }
1445
1446 return badness;
1447}
1448
1449/* return NID if the given pin has only a single connection to a certain DAC */
1450static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1451{
1452 struct hda_gen_spec *spec = codec->spec;
1453 int i;
1454 hda_nid_t nid_found = 0;
1455
1456 for (i = 0; i < spec->num_all_dacs; i++) {
1457 hda_nid_t nid = spec->all_dacs[i];
1458 if (!nid || is_dac_already_used(codec, nid))
1459 continue;
1460 if (is_reachable_path(codec, nid, pin)) {
1461 if (nid_found)
1462 return 0;
1463 nid_found = nid;
1464 }
1465 }
1466 return nid_found;
1467}
1468
1469/* check whether the given pin can be a multi-io pin */
1470static bool can_be_multiio_pin(struct hda_codec *codec,
1471 unsigned int location, hda_nid_t nid)
1472{
1473 unsigned int defcfg, caps;
1474
1475 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1476 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1477 return false;
1478 if (location && get_defcfg_location(defcfg) != location)
1479 return false;
1480 caps = snd_hda_query_pin_caps(codec, nid);
1481 if (!(caps & AC_PINCAP_OUT))
1482 return false;
1483 return true;
1484}
1485
Takashi Iwaie22aab72013-01-04 14:50:04 +01001486/* count the number of input pins that are capable to be multi-io */
1487static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1488{
1489 struct hda_gen_spec *spec = codec->spec;
1490 struct auto_pin_cfg *cfg = &spec->autocfg;
1491 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1492 unsigned int location = get_defcfg_location(defcfg);
1493 int type, i;
1494 int num_pins = 0;
1495
1496 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1497 for (i = 0; i < cfg->num_inputs; i++) {
1498 if (cfg->inputs[i].type != type)
1499 continue;
1500 if (can_be_multiio_pin(codec, location,
1501 cfg->inputs[i].pin))
1502 num_pins++;
1503 }
1504 }
1505 return num_pins;
1506}
1507
Takashi Iwai352f7f92012-12-19 12:52:06 +01001508/*
1509 * multi-io helper
1510 *
1511 * When hardwired is set, try to fill ony hardwired pins, and returns
1512 * zero if any pins are filled, non-zero if nothing found.
1513 * When hardwired is off, try to fill possible input pins, and returns
1514 * the badness value.
1515 */
1516static int fill_multi_ios(struct hda_codec *codec,
1517 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001518 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001519{
1520 struct hda_gen_spec *spec = codec->spec;
1521 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001522 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001523 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1524 unsigned int location = get_defcfg_location(defcfg);
1525 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001526 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001527
1528 old_pins = spec->multi_ios;
1529 if (old_pins >= 2)
1530 goto end_fill;
1531
Takashi Iwaie22aab72013-01-04 14:50:04 +01001532 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001533 if (num_pins < 2)
1534 goto end_fill;
1535
Takashi Iwai352f7f92012-12-19 12:52:06 +01001536 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1537 for (i = 0; i < cfg->num_inputs; i++) {
1538 hda_nid_t nid = cfg->inputs[i].pin;
1539 hda_nid_t dac = 0;
1540
1541 if (cfg->inputs[i].type != type)
1542 continue;
1543 if (!can_be_multiio_pin(codec, location, nid))
1544 continue;
1545 for (j = 0; j < spec->multi_ios; j++) {
1546 if (nid == spec->multi_io[j].pin)
1547 break;
1548 }
1549 if (j < spec->multi_ios)
1550 continue;
1551
Takashi Iwai352f7f92012-12-19 12:52:06 +01001552 if (hardwired)
1553 dac = get_dac_if_single(codec, nid);
1554 else if (!dac)
1555 dac = look_for_dac(codec, nid, false);
1556 if (!dac) {
1557 badness++;
1558 continue;
1559 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001560 path = snd_hda_add_new_path(codec, dac, nid,
1561 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001562 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001563 badness++;
1564 continue;
1565 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001566 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001567 spec->multi_io[spec->multi_ios].pin = nid;
1568 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001569 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1570 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001571 spec->multi_ios++;
1572 if (spec->multi_ios >= 2)
1573 break;
1574 }
1575 }
1576 end_fill:
1577 if (badness)
1578 badness = BAD_MULTI_IO;
1579 if (old_pins == spec->multi_ios) {
1580 if (hardwired)
1581 return 1; /* nothing found */
1582 else
1583 return badness; /* no badness if nothing found */
1584 }
1585 if (!hardwired && spec->multi_ios < 2) {
1586 /* cancel newly assigned paths */
1587 spec->paths.used -= spec->multi_ios - old_pins;
1588 spec->multi_ios = old_pins;
1589 return badness;
1590 }
1591
1592 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001593 for (i = old_pins; i < spec->multi_ios; i++) {
1594 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1595 badness += assign_out_path_ctls(codec, path);
1596 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001597
1598 return badness;
1599}
1600
1601/* map DACs for all pins in the list if they are single connections */
1602static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001603 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001604{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001605 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001606 int i;
1607 bool found = false;
1608 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001609 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001610 hda_nid_t dac;
1611 if (dacs[i])
1612 continue;
1613 dac = get_dac_if_single(codec, pins[i]);
1614 if (!dac)
1615 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001616 path = snd_hda_add_new_path(codec, dac, pins[i],
1617 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001618 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001619 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001620 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001621 dacs[i] = dac;
1622 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001623 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001624 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001625 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001626 }
1627 }
1628 return found;
1629}
1630
Takashi Iwaie7fdd522015-12-08 17:00:42 +01001631static inline bool has_aamix_out_paths(struct hda_gen_spec *spec)
1632{
1633 return spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1634 spec->aamix_out_paths[2];
1635}
1636
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001637/* create a new path including aamix if available, and return its index */
1638static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1639{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001640 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001641 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001642 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001643
1644 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001645 if (!path || !path->depth ||
1646 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001647 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001648 path_dac = path->path[0];
1649 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001650 pin = path->path[path->depth - 1];
1651 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1652 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001653 if (dac != path_dac)
1654 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001655 else if (spec->multiout.hp_out_nid[0])
1656 dac = spec->multiout.hp_out_nid[0];
1657 else if (spec->multiout.extra_out_nid[0])
1658 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001659 else
1660 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001661 if (dac)
1662 path = snd_hda_add_new_path(codec, dac, pin,
1663 spec->mixer_nid);
1664 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001665 if (!path)
1666 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001667 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001668 path->active = false; /* unused as default */
Takashi Iwai6b275b12015-03-20 18:11:05 +01001669 path->pin_fixed = true; /* static route */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001670 return snd_hda_get_path_idx(codec, path);
1671}
1672
Takashi Iwai55a63d42013-03-21 17:20:12 +01001673/* check whether the independent HP is available with the current config */
1674static bool indep_hp_possible(struct hda_codec *codec)
1675{
1676 struct hda_gen_spec *spec = codec->spec;
1677 struct auto_pin_cfg *cfg = &spec->autocfg;
1678 struct nid_path *path;
1679 int i, idx;
1680
1681 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1682 idx = spec->out_paths[0];
1683 else
1684 idx = spec->hp_paths[0];
1685 path = snd_hda_get_path_from_idx(codec, idx);
1686 if (!path)
1687 return false;
1688
1689 /* assume no path conflicts unless aamix is involved */
1690 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1691 return true;
1692
1693 /* check whether output paths contain aamix */
1694 for (i = 0; i < cfg->line_outs; i++) {
1695 if (spec->out_paths[i] == idx)
1696 break;
1697 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1698 if (path && is_nid_contained(path, spec->mixer_nid))
1699 return false;
1700 }
1701 for (i = 0; i < cfg->speaker_outs; i++) {
1702 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1703 if (path && is_nid_contained(path, spec->mixer_nid))
1704 return false;
1705 }
1706
1707 return true;
1708}
1709
Takashi Iwaia07a9492013-01-07 16:44:06 +01001710/* fill the empty entries in the dac array for speaker/hp with the
1711 * shared dac pointed by the paths
1712 */
1713static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1714 hda_nid_t *dacs, int *path_idx)
1715{
1716 struct nid_path *path;
1717 int i;
1718
1719 for (i = 0; i < num_outs; i++) {
1720 if (dacs[i])
1721 continue;
1722 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1723 if (!path)
1724 continue;
1725 dacs[i] = path->path[0];
1726 }
1727}
1728
Takashi Iwai352f7f92012-12-19 12:52:06 +01001729/* fill in the dac_nids table from the parsed pin configuration */
1730static int fill_and_eval_dacs(struct hda_codec *codec,
1731 bool fill_hardwired,
1732 bool fill_mio_first)
1733{
1734 struct hda_gen_spec *spec = codec->spec;
1735 struct auto_pin_cfg *cfg = &spec->autocfg;
1736 int i, err, badness;
1737
1738 /* set num_dacs once to full for look_for_dac() */
1739 spec->multiout.num_dacs = cfg->line_outs;
1740 spec->multiout.dac_nids = spec->private_dac_nids;
1741 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1742 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1743 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1744 spec->multi_ios = 0;
1745 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001746
1747 /* clear path indices */
1748 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1749 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1750 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1751 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1752 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001753 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001754 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1755 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1756
Takashi Iwai352f7f92012-12-19 12:52:06 +01001757 badness = 0;
1758
1759 /* fill hard-wired DACs first */
1760 if (fill_hardwired) {
1761 bool mapped;
1762 do {
1763 mapped = map_singles(codec, cfg->line_outs,
1764 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001765 spec->private_dac_nids,
1766 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001767 mapped |= map_singles(codec, cfg->hp_outs,
1768 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001769 spec->multiout.hp_out_nid,
1770 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001771 mapped |= map_singles(codec, cfg->speaker_outs,
1772 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001773 spec->multiout.extra_out_nid,
1774 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001775 if (!spec->no_multi_io &&
1776 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001777 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001778 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001779 if (!err)
1780 mapped = true;
1781 }
1782 } while (mapped);
1783 }
1784
1785 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001786 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001787 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001788
Takashi Iwaida96fb52013-07-29 16:54:36 +02001789 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001790 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1791 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001792 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001793 if (err < 0)
1794 return err;
1795 /* we don't count badness at this stage yet */
1796 }
1797
1798 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1799 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1800 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001801 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001802 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001803 if (err < 0)
1804 return err;
1805 badness += err;
1806 }
1807 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1808 err = try_assign_dacs(codec, cfg->speaker_outs,
1809 cfg->speaker_pins,
1810 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001811 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001812 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001813 if (err < 0)
1814 return err;
1815 badness += err;
1816 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001817 if (!spec->no_multi_io &&
1818 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001819 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001820 if (err < 0)
1821 return err;
1822 badness += err;
1823 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001824
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001825 if (spec->mixer_nid) {
1826 spec->aamix_out_paths[0] =
1827 check_aamix_out_path(codec, spec->out_paths[0]);
1828 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1829 spec->aamix_out_paths[1] =
1830 check_aamix_out_path(codec, spec->hp_paths[0]);
1831 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1832 spec->aamix_out_paths[2] =
1833 check_aamix_out_path(codec, spec->speaker_paths[0]);
1834 }
1835
Takashi Iwaida96fb52013-07-29 16:54:36 +02001836 if (!spec->no_multi_io &&
1837 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001838 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1839 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001840
Takashi Iwaia07a9492013-01-07 16:44:06 +01001841 /* re-count num_dacs and squash invalid entries */
1842 spec->multiout.num_dacs = 0;
1843 for (i = 0; i < cfg->line_outs; i++) {
1844 if (spec->private_dac_nids[i])
1845 spec->multiout.num_dacs++;
1846 else {
1847 memmove(spec->private_dac_nids + i,
1848 spec->private_dac_nids + i + 1,
1849 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1850 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1851 }
1852 }
1853
1854 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001855 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001856
Takashi Iwai352f7f92012-12-19 12:52:06 +01001857 if (spec->multi_ios == 2) {
1858 for (i = 0; i < 2; i++)
1859 spec->private_dac_nids[spec->multiout.num_dacs++] =
1860 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001861 } else if (spec->multi_ios) {
1862 spec->multi_ios = 0;
1863 badness += BAD_MULTI_IO;
1864 }
1865
Takashi Iwai55a63d42013-03-21 17:20:12 +01001866 if (spec->indep_hp && !indep_hp_possible(codec))
1867 badness += BAD_NO_INDEP_HP;
1868
Takashi Iwaia07a9492013-01-07 16:44:06 +01001869 /* re-fill the shared DAC for speaker / headphone */
1870 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1871 refill_shared_dacs(codec, cfg->hp_outs,
1872 spec->multiout.hp_out_nid,
1873 spec->hp_paths);
1874 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1875 refill_shared_dacs(codec, cfg->speaker_outs,
1876 spec->multiout.extra_out_nid,
1877 spec->speaker_paths);
1878
Takashi Iwai352f7f92012-12-19 12:52:06 +01001879 return badness;
1880}
1881
1882#define DEBUG_BADNESS
1883
1884#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001885#define debug_badness(fmt, ...) \
1886 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001887#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001888#define debug_badness(fmt, ...) \
1889 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001890#endif
1891
Takashi Iwaia7694092013-01-21 10:43:18 +01001892#ifdef DEBUG_BADNESS
1893static inline void print_nid_path_idx(struct hda_codec *codec,
1894 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001895{
Takashi Iwaia7694092013-01-21 10:43:18 +01001896 struct nid_path *path;
1897
1898 path = snd_hda_get_path_from_idx(codec, idx);
1899 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001900 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001901}
1902
1903static void debug_show_configs(struct hda_codec *codec,
1904 struct auto_pin_cfg *cfg)
1905{
1906 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001907 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001908 int i;
1909
1910 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001911 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001912 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001913 spec->multiout.dac_nids[0],
1914 spec->multiout.dac_nids[1],
1915 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001916 spec->multiout.dac_nids[3],
1917 lo_type[cfg->line_out_type]);
1918 for (i = 0; i < cfg->line_outs; i++)
1919 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001920 if (spec->multi_ios > 0)
1921 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1922 spec->multi_ios,
1923 spec->multi_io[0].pin, spec->multi_io[1].pin,
1924 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001925 for (i = 0; i < spec->multi_ios; i++)
1926 print_nid_path_idx(codec, " mio",
1927 spec->out_paths[cfg->line_outs + i]);
1928 if (cfg->hp_outs)
1929 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001930 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001931 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001932 spec->multiout.hp_out_nid[0],
1933 spec->multiout.hp_out_nid[1],
1934 spec->multiout.hp_out_nid[2],
1935 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001936 for (i = 0; i < cfg->hp_outs; i++)
1937 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1938 if (cfg->speaker_outs)
1939 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001940 cfg->speaker_pins[0], cfg->speaker_pins[1],
1941 cfg->speaker_pins[2], cfg->speaker_pins[3],
1942 spec->multiout.extra_out_nid[0],
1943 spec->multiout.extra_out_nid[1],
1944 spec->multiout.extra_out_nid[2],
1945 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001946 for (i = 0; i < cfg->speaker_outs; i++)
1947 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1948 for (i = 0; i < 3; i++)
1949 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001950}
Takashi Iwaia7694092013-01-21 10:43:18 +01001951#else
1952#define debug_show_configs(codec, cfg) /* NOP */
1953#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001954
1955/* find all available DACs of the codec */
1956static void fill_all_dac_nids(struct hda_codec *codec)
1957{
1958 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001959 hda_nid_t nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001960
1961 spec->num_all_dacs = 0;
1962 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
Takashi Iwai7639a062015-03-03 10:07:24 +01001963 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001964 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1965 continue;
1966 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001967 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001968 break;
1969 }
1970 spec->all_dacs[spec->num_all_dacs++] = nid;
1971 }
1972}
1973
1974static int parse_output_paths(struct hda_codec *codec)
1975{
1976 struct hda_gen_spec *spec = codec->spec;
1977 struct auto_pin_cfg *cfg = &spec->autocfg;
1978 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001979 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001980 int best_badness = INT_MAX;
1981 int badness;
1982 bool fill_hardwired = true, fill_mio_first = true;
1983 bool best_wired = true, best_mio = true;
1984 bool hp_spk_swapped = false;
1985
Takashi Iwai352f7f92012-12-19 12:52:06 +01001986 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1987 if (!best_cfg)
1988 return -ENOMEM;
1989 *best_cfg = *cfg;
1990
1991 for (;;) {
1992 badness = fill_and_eval_dacs(codec, fill_hardwired,
1993 fill_mio_first);
1994 if (badness < 0) {
1995 kfree(best_cfg);
1996 return badness;
1997 }
1998 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1999 cfg->line_out_type, fill_hardwired, fill_mio_first,
2000 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01002001 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002002 if (badness < best_badness) {
2003 best_badness = badness;
2004 *best_cfg = *cfg;
2005 best_wired = fill_hardwired;
2006 best_mio = fill_mio_first;
2007 }
2008 if (!badness)
2009 break;
2010 fill_mio_first = !fill_mio_first;
2011 if (!fill_mio_first)
2012 continue;
2013 fill_hardwired = !fill_hardwired;
2014 if (!fill_hardwired)
2015 continue;
2016 if (hp_spk_swapped)
2017 break;
2018 hp_spk_swapped = true;
2019 if (cfg->speaker_outs > 0 &&
2020 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2021 cfg->hp_outs = cfg->line_outs;
2022 memcpy(cfg->hp_pins, cfg->line_out_pins,
2023 sizeof(cfg->hp_pins));
2024 cfg->line_outs = cfg->speaker_outs;
2025 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2026 sizeof(cfg->speaker_pins));
2027 cfg->speaker_outs = 0;
2028 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2029 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2030 fill_hardwired = true;
2031 continue;
2032 }
2033 if (cfg->hp_outs > 0 &&
2034 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2035 cfg->speaker_outs = cfg->line_outs;
2036 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2037 sizeof(cfg->speaker_pins));
2038 cfg->line_outs = cfg->hp_outs;
2039 memcpy(cfg->line_out_pins, cfg->hp_pins,
2040 sizeof(cfg->hp_pins));
2041 cfg->hp_outs = 0;
2042 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2043 cfg->line_out_type = AUTO_PIN_HP_OUT;
2044 fill_hardwired = true;
2045 continue;
2046 }
2047 break;
2048 }
2049
2050 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002051 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01002052 *cfg = *best_cfg;
2053 fill_and_eval_dacs(codec, best_wired, best_mio);
2054 }
2055 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2056 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01002057 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002058
2059 if (cfg->line_out_pins[0]) {
2060 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01002061 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002062 if (path)
2063 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002064 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01002065 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2066 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002067 if (spec->dac_min_mute)
Takashi Sakamoto51cdc8b2018-05-14 07:09:52 +09002068 spec->vmaster_tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] |= TLV_DB_SCALE_MUTE;
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002069 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002070 }
2071
Takashi Iwai9314a582013-01-21 10:49:05 +01002072 /* set initial pinctl targets */
2073 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2074 val = PIN_HP;
2075 else
2076 val = PIN_OUT;
2077 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2078 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2079 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2080 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2081 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2082 set_pin_targets(codec, cfg->speaker_outs,
2083 cfg->speaker_pins, val);
2084 }
2085
Takashi Iwai55a63d42013-03-21 17:20:12 +01002086 /* clear indep_hp flag if not available */
2087 if (spec->indep_hp && !indep_hp_possible(codec))
2088 spec->indep_hp = 0;
2089
Takashi Iwai352f7f92012-12-19 12:52:06 +01002090 kfree(best_cfg);
2091 return 0;
2092}
2093
2094/* add playback controls from the parsed DAC table */
2095static int create_multi_out_ctls(struct hda_codec *codec,
2096 const struct auto_pin_cfg *cfg)
2097{
2098 struct hda_gen_spec *spec = codec->spec;
2099 int i, err, noutputs;
2100
2101 noutputs = cfg->line_outs;
2102 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2103 noutputs += spec->multi_ios;
2104
2105 for (i = 0; i < noutputs; i++) {
2106 const char *name;
2107 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002108 struct nid_path *path;
2109
Takashi Iwai196c17662013-01-04 15:01:40 +01002110 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002111 if (!path)
2112 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002113
2114 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002115 if (!name || !strcmp(name, "CLFE")) {
2116 /* Center/LFE */
2117 err = add_vol_ctl(codec, "Center", 0, 1, path);
2118 if (err < 0)
2119 return err;
2120 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2121 if (err < 0)
2122 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002123 } else {
2124 err = add_stereo_vol(codec, name, index, path);
2125 if (err < 0)
2126 return err;
2127 }
2128
2129 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2130 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002131 err = add_sw_ctl(codec, "Center", 0, 1, path);
2132 if (err < 0)
2133 return err;
2134 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2135 if (err < 0)
2136 return err;
2137 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002138 err = add_stereo_sw(codec, name, index, path);
2139 if (err < 0)
2140 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
2142 }
2143 return 0;
2144}
2145
Takashi Iwaic2c80382013-01-07 10:33:57 +01002146static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01002147 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002149 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 int err;
2151
Takashi Iwai196c17662013-01-04 15:01:40 +01002152 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002153 if (!path)
2154 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01002155 err = add_stereo_vol(codec, pfx, cidx, path);
2156 if (err < 0)
2157 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002158 err = add_stereo_sw(codec, pfx, cidx, path);
2159 if (err < 0)
2160 return err;
2161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163
Takashi Iwai352f7f92012-12-19 12:52:06 +01002164/* add playback controls for speaker and HP outputs */
2165static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01002166 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
Takashi Iwaic2c80382013-01-07 10:33:57 +01002168 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002169
2170 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002171 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02002172 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01002173 int err, idx = 0;
2174
2175 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2176 name = "Bass Speaker";
2177 else if (num_pins >= 3) {
2178 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01002179 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01002180 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002181 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002182 name = pfx;
2183 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01002185 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002186 if (err < 0)
2187 return err;
2188 }
2189 return 0;
2190}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002191
Takashi Iwai352f7f92012-12-19 12:52:06 +01002192static int create_hp_out_ctls(struct hda_codec *codec)
2193{
2194 struct hda_gen_spec *spec = codec->spec;
2195 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002196 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002197 "Headphone");
2198}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Takashi Iwai352f7f92012-12-19 12:52:06 +01002200static int create_speaker_out_ctls(struct hda_codec *codec)
2201{
2202 struct hda_gen_spec *spec = codec->spec;
2203 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002204 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002205 "Speaker");
2206}
2207
2208/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002209 * independent HP controls
2210 */
2211
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02002212static void call_hp_automute(struct hda_codec *codec,
2213 struct hda_jack_callback *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002214static int indep_hp_info(struct snd_kcontrol *kcontrol,
2215 struct snd_ctl_elem_info *uinfo)
2216{
2217 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2218}
2219
2220static int indep_hp_get(struct snd_kcontrol *kcontrol,
2221 struct snd_ctl_elem_value *ucontrol)
2222{
2223 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2224 struct hda_gen_spec *spec = codec->spec;
2225 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2226 return 0;
2227}
2228
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002229static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2230 int nomix_path_idx, int mix_path_idx,
2231 int out_type);
2232
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002233static int indep_hp_put(struct snd_kcontrol *kcontrol,
2234 struct snd_ctl_elem_value *ucontrol)
2235{
2236 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2237 struct hda_gen_spec *spec = codec->spec;
2238 unsigned int select = ucontrol->value.enumerated.item[0];
2239 int ret = 0;
2240
2241 mutex_lock(&spec->pcm_mutex);
2242 if (spec->active_streams) {
2243 ret = -EBUSY;
2244 goto unlock;
2245 }
2246
2247 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002248 hda_nid_t *dacp;
2249 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2250 dacp = &spec->private_dac_nids[0];
2251 else
2252 dacp = &spec->multiout.hp_out_nid[0];
2253
2254 /* update HP aamix paths in case it conflicts with indep HP */
2255 if (spec->have_aamix_ctl) {
2256 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2257 update_aamix_paths(codec, spec->aamix_mode,
2258 spec->out_paths[0],
2259 spec->aamix_out_paths[0],
2260 spec->autocfg.line_out_type);
2261 else
2262 update_aamix_paths(codec, spec->aamix_mode,
2263 spec->hp_paths[0],
2264 spec->aamix_out_paths[1],
2265 AUTO_PIN_HP_OUT);
2266 }
2267
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002268 spec->indep_hp_enabled = select;
2269 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002270 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002271 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002272 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002273
Takashi Iwai963afde2013-05-31 15:20:31 +02002274 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002275 ret = 1;
2276 }
2277 unlock:
2278 mutex_unlock(&spec->pcm_mutex);
2279 return ret;
2280}
2281
2282static const struct snd_kcontrol_new indep_hp_ctl = {
2283 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2284 .name = "Independent HP",
2285 .info = indep_hp_info,
2286 .get = indep_hp_get,
2287 .put = indep_hp_put,
2288};
2289
2290
2291static int create_indep_hp_ctls(struct hda_codec *codec)
2292{
2293 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002294 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002295
2296 if (!spec->indep_hp)
2297 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002298 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2299 dac = spec->multiout.dac_nids[0];
2300 else
2301 dac = spec->multiout.hp_out_nid[0];
2302 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002303 spec->indep_hp = 0;
2304 return 0;
2305 }
2306
2307 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002308 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002309 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2310 return -ENOMEM;
2311 return 0;
2312}
2313
2314/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002315 * channel mode enum control
2316 */
2317
2318static int ch_mode_info(struct snd_kcontrol *kcontrol,
2319 struct snd_ctl_elem_info *uinfo)
2320{
2321 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2322 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002323 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002324
2325 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2326 uinfo->count = 1;
2327 uinfo->value.enumerated.items = spec->multi_ios + 1;
2328 if (uinfo->value.enumerated.item > spec->multi_ios)
2329 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002330 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2331 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002332 return 0;
2333}
2334
2335static int ch_mode_get(struct snd_kcontrol *kcontrol,
2336 struct snd_ctl_elem_value *ucontrol)
2337{
2338 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2339 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002340 ucontrol->value.enumerated.item[0] =
2341 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002342 return 0;
2343}
2344
Takashi Iwai196c17662013-01-04 15:01:40 +01002345static inline struct nid_path *
2346get_multiio_path(struct hda_codec *codec, int idx)
2347{
2348 struct hda_gen_spec *spec = codec->spec;
2349 return snd_hda_get_path_from_idx(codec,
2350 spec->out_paths[spec->autocfg.line_outs + idx]);
2351}
2352
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002353static void update_automute_all(struct hda_codec *codec);
2354
Takashi Iwai65033cc2013-04-16 12:31:05 +02002355/* Default value to be passed as aamix argument for snd_hda_activate_path();
2356 * used for output paths
2357 */
2358static bool aamix_default(struct hda_gen_spec *spec)
2359{
2360 return !spec->have_aamix_ctl || spec->aamix_mode;
2361}
2362
Takashi Iwai352f7f92012-12-19 12:52:06 +01002363static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2364{
2365 struct hda_gen_spec *spec = codec->spec;
2366 hda_nid_t nid = spec->multi_io[idx].pin;
2367 struct nid_path *path;
2368
Takashi Iwai196c17662013-01-04 15:01:40 +01002369 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002370 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002372
2373 if (path->active == output)
2374 return 0;
2375
2376 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002377 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002378 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002379 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002380 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002381 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002382 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002383 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002384 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002386
2387 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002388 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002389
Takashi Iwai352f7f92012-12-19 12:52:06 +01002390 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
Takashi Iwai352f7f92012-12-19 12:52:06 +01002393static int ch_mode_put(struct snd_kcontrol *kcontrol,
2394 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002396 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2397 struct hda_gen_spec *spec = codec->spec;
2398 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Takashi Iwai352f7f92012-12-19 12:52:06 +01002400 ch = ucontrol->value.enumerated.item[0];
2401 if (ch < 0 || ch > spec->multi_ios)
2402 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002403 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002404 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002405 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002406 for (i = 0; i < spec->multi_ios; i++)
2407 set_multi_io(codec, i, i < ch);
2408 spec->multiout.max_channels = max(spec->ext_channel_count,
2409 spec->const_channel_count);
2410 if (spec->need_dac_fix)
2411 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 return 1;
2413}
2414
Takashi Iwai352f7f92012-12-19 12:52:06 +01002415static const struct snd_kcontrol_new channel_mode_enum = {
2416 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2417 .name = "Channel Mode",
2418 .info = ch_mode_info,
2419 .get = ch_mode_get,
2420 .put = ch_mode_put,
2421};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Takashi Iwai352f7f92012-12-19 12:52:06 +01002423static int create_multi_channel_mode(struct hda_codec *codec)
2424{
2425 struct hda_gen_spec *spec = codec->spec;
2426
2427 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002428 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002429 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 return 0;
2432}
2433
Takashi Iwai352f7f92012-12-19 12:52:06 +01002434/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002435 * aamix loopback enable/disable switch
2436 */
2437
2438#define loopback_mixing_info indep_hp_info
2439
2440static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2441 struct snd_ctl_elem_value *ucontrol)
2442{
2443 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2444 struct hda_gen_spec *spec = codec->spec;
2445 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2446 return 0;
2447}
2448
2449static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002450 int nomix_path_idx, int mix_path_idx,
2451 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002452{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002453 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002454 struct nid_path *nomix_path, *mix_path;
2455
2456 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2457 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2458 if (!nomix_path || !mix_path)
2459 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002460
2461 /* if HP aamix path is driven from a different DAC and the
2462 * independent HP mode is ON, can't turn on aamix path
2463 */
2464 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2465 mix_path->path[0] != spec->alt_dac_nid)
2466 do_mix = false;
2467
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002468 if (do_mix) {
2469 snd_hda_activate_path(codec, nomix_path, false, true);
2470 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002471 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002472 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002473 snd_hda_activate_path(codec, mix_path, false, false);
2474 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002475 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002476 }
2477}
2478
Takashi Iwaie7fdd522015-12-08 17:00:42 +01002479/* re-initialize the output paths; only called from loopback_mixing_put() */
2480static void update_output_paths(struct hda_codec *codec, int num_outs,
2481 const int *paths)
2482{
2483 struct hda_gen_spec *spec = codec->spec;
2484 struct nid_path *path;
2485 int i;
2486
2487 for (i = 0; i < num_outs; i++) {
2488 path = snd_hda_get_path_from_idx(codec, paths[i]);
2489 if (path)
2490 snd_hda_activate_path(codec, path, path->active,
2491 spec->aamix_mode);
2492 }
2493}
2494
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002495static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2496 struct snd_ctl_elem_value *ucontrol)
2497{
2498 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2499 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie7fdd522015-12-08 17:00:42 +01002500 const struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002501 unsigned int val = ucontrol->value.enumerated.item[0];
2502
2503 if (val == spec->aamix_mode)
2504 return 0;
2505 spec->aamix_mode = val;
Takashi Iwaie7fdd522015-12-08 17:00:42 +01002506 if (has_aamix_out_paths(spec)) {
2507 update_aamix_paths(codec, val, spec->out_paths[0],
2508 spec->aamix_out_paths[0],
2509 cfg->line_out_type);
2510 update_aamix_paths(codec, val, spec->hp_paths[0],
2511 spec->aamix_out_paths[1],
2512 AUTO_PIN_HP_OUT);
2513 update_aamix_paths(codec, val, spec->speaker_paths[0],
2514 spec->aamix_out_paths[2],
2515 AUTO_PIN_SPEAKER_OUT);
2516 } else {
2517 update_output_paths(codec, cfg->line_outs, spec->out_paths);
2518 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2519 update_output_paths(codec, cfg->hp_outs, spec->hp_paths);
2520 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
2521 update_output_paths(codec, cfg->speaker_outs,
2522 spec->speaker_paths);
2523 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002524 return 1;
2525}
2526
2527static const struct snd_kcontrol_new loopback_mixing_enum = {
2528 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2529 .name = "Loopback Mixing",
2530 .info = loopback_mixing_info,
2531 .get = loopback_mixing_get,
2532 .put = loopback_mixing_put,
2533};
2534
2535static int create_loopback_mixing_ctl(struct hda_codec *codec)
2536{
2537 struct hda_gen_spec *spec = codec->spec;
2538
2539 if (!spec->mixer_nid)
2540 return 0;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002541 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2542 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002543 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002544 return 0;
2545}
2546
2547/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002548 * shared headphone/mic handling
2549 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002550
Takashi Iwai352f7f92012-12-19 12:52:06 +01002551static void call_update_outputs(struct hda_codec *codec);
2552
2553/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002554static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002555{
2556 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002557 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002558 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002559 hda_nid_t pin;
2560
2561 pin = spec->hp_mic_pin;
2562 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2563
2564 if (!force) {
2565 val = snd_hda_codec_get_pin_target(codec, pin);
2566 if (as_mic) {
2567 if (val & PIN_IN)
2568 return;
2569 } else {
2570 if (val & PIN_OUT)
2571 return;
2572 }
2573 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002574
2575 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002576 /* if the HP pin doesn't support VREF and the codec driver gives an
2577 * alternative pin, set up the VREF on that pin instead
2578 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002579 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2580 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2581 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2582 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002583 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002584 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002585 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002586
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002587 if (!spec->hp_mic_jack_modes) {
2588 if (as_mic)
2589 val |= PIN_IN;
2590 else
2591 val = PIN_HP;
2592 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002593 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002594 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002595}
2596
2597/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002598static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002599{
2600 struct hda_gen_spec *spec = codec->spec;
2601 struct auto_pin_cfg *cfg = &spec->autocfg;
2602 unsigned int defcfg;
2603 hda_nid_t nid;
2604
Takashi Iwai967303d2013-02-19 17:12:42 +01002605 if (!spec->hp_mic) {
2606 if (spec->suppress_hp_mic_detect)
2607 return 0;
2608 /* automatic detection: only if no input or a single internal
2609 * input pin is found, try to detect the shared hp/mic
2610 */
2611 if (cfg->num_inputs > 1)
2612 return 0;
2613 else if (cfg->num_inputs == 1) {
2614 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2615 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2616 return 0;
2617 }
2618 }
2619
2620 spec->hp_mic = 0; /* clear once */
2621 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002622 return 0;
2623
Takashi Iwai967303d2013-02-19 17:12:42 +01002624 nid = 0;
2625 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2626 nid = cfg->line_out_pins[0];
2627 else if (cfg->hp_outs > 0)
2628 nid = cfg->hp_pins[0];
2629 if (!nid)
2630 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002631
2632 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2633 return 0; /* no input */
2634
Takashi Iwai967303d2013-02-19 17:12:42 +01002635 cfg->inputs[cfg->num_inputs].pin = nid;
2636 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002637 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002638 cfg->num_inputs++;
2639 spec->hp_mic = 1;
2640 spec->hp_mic_pin = nid;
2641 /* we can't handle auto-mic together with HP-mic */
2642 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002643 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002644 return 0;
2645}
2646
Takashi Iwai978e77e2013-01-10 16:57:58 +01002647/*
2648 * output jack mode
2649 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002650
2651static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2652
2653static const char * const out_jack_texts[] = {
2654 "Line Out", "Headphone Out",
2655};
2656
Takashi Iwai978e77e2013-01-10 16:57:58 +01002657static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2658 struct snd_ctl_elem_info *uinfo)
2659{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002660 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002661}
2662
2663static int out_jack_mode_get(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 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2669 ucontrol->value.enumerated.item[0] = 1;
2670 else
2671 ucontrol->value.enumerated.item[0] = 0;
2672 return 0;
2673}
2674
2675static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2676 struct snd_ctl_elem_value *ucontrol)
2677{
2678 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2679 hda_nid_t nid = kcontrol->private_value;
2680 unsigned int val;
2681
2682 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2683 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2684 return 0;
2685 snd_hda_set_pin_ctl_cache(codec, nid, val);
2686 return 1;
2687}
2688
2689static const struct snd_kcontrol_new out_jack_mode_enum = {
2690 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2691 .info = out_jack_mode_info,
2692 .get = out_jack_mode_get,
2693 .put = out_jack_mode_put,
2694};
2695
2696static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2697{
2698 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +02002699 const struct snd_kcontrol_new *kctl;
Takashi Iwai978e77e2013-01-10 16:57:58 +01002700 int i;
2701
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +02002702 snd_array_for_each(&spec->kctls, i, kctl) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002703 if (!strcmp(kctl->name, name) && kctl->index == idx)
2704 return true;
2705 }
2706 return false;
2707}
2708
2709static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2710 char *name, size_t name_len)
2711{
2712 struct hda_gen_spec *spec = codec->spec;
2713 int idx = 0;
2714
2715 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2716 strlcat(name, " Jack Mode", name_len);
2717
2718 for (; find_kctl_name(codec, name, idx); idx++)
2719 ;
2720}
2721
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002722static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2723{
2724 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002725 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002726 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2727 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2728 return 2;
2729 }
2730 return 1;
2731}
2732
Takashi Iwai978e77e2013-01-10 16:57:58 +01002733static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2734 hda_nid_t *pins)
2735{
2736 struct hda_gen_spec *spec = codec->spec;
2737 int i;
2738
2739 for (i = 0; i < num_pins; i++) {
2740 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002741 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002742 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002743 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002744 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002745 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002746 get_jack_mode_name(codec, pin, name, sizeof(name));
2747 knew = snd_hda_gen_add_kctl(spec, name,
2748 &out_jack_mode_enum);
2749 if (!knew)
2750 return -ENOMEM;
2751 knew->private_value = pin;
2752 }
2753 }
2754
2755 return 0;
2756}
2757
Takashi Iwai294765582013-01-17 09:52:11 +01002758/*
2759 * input jack mode
2760 */
2761
2762/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2763#define NUM_VREFS 6
2764
2765static const char * const vref_texts[NUM_VREFS] = {
2766 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2767 "", "Mic 80pc Bias", "Mic 100pc Bias"
2768};
2769
2770static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2771{
2772 unsigned int pincap;
2773
2774 pincap = snd_hda_query_pin_caps(codec, pin);
2775 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2776 /* filter out unusual vrefs */
2777 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2778 return pincap;
2779}
2780
2781/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2782static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2783{
2784 unsigned int i, n = 0;
2785
2786 for (i = 0; i < NUM_VREFS; i++) {
2787 if (vref_caps & (1 << i)) {
2788 if (n == item_idx)
2789 return i;
2790 n++;
2791 }
2792 }
2793 return 0;
2794}
2795
2796/* convert back from the vref ctl index to the enum item index */
2797static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2798{
2799 unsigned int i, n = 0;
2800
2801 for (i = 0; i < NUM_VREFS; i++) {
2802 if (i == idx)
2803 return n;
2804 if (vref_caps & (1 << i))
2805 n++;
2806 }
2807 return 0;
2808}
2809
2810static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2811 struct snd_ctl_elem_info *uinfo)
2812{
2813 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2814 hda_nid_t nid = kcontrol->private_value;
2815 unsigned int vref_caps = get_vref_caps(codec, nid);
2816
2817 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2818 vref_texts);
2819 /* set the right text */
2820 strcpy(uinfo->value.enumerated.name,
2821 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2822 return 0;
2823}
2824
2825static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2826 struct snd_ctl_elem_value *ucontrol)
2827{
2828 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2829 hda_nid_t nid = kcontrol->private_value;
2830 unsigned int vref_caps = get_vref_caps(codec, nid);
2831 unsigned int idx;
2832
2833 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2834 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2835 return 0;
2836}
2837
2838static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2839 struct snd_ctl_elem_value *ucontrol)
2840{
2841 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2842 hda_nid_t nid = kcontrol->private_value;
2843 unsigned int vref_caps = get_vref_caps(codec, nid);
2844 unsigned int val, idx;
2845
2846 val = snd_hda_codec_get_pin_target(codec, nid);
2847 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2848 if (idx == ucontrol->value.enumerated.item[0])
2849 return 0;
2850
2851 val &= ~AC_PINCTL_VREFEN;
2852 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2853 snd_hda_set_pin_ctl_cache(codec, nid, val);
2854 return 1;
2855}
2856
2857static const struct snd_kcontrol_new in_jack_mode_enum = {
2858 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2859 .info = in_jack_mode_info,
2860 .get = in_jack_mode_get,
2861 .put = in_jack_mode_put,
2862};
2863
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002864static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2865{
2866 struct hda_gen_spec *spec = codec->spec;
2867 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002868 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002869 nitems = hweight32(get_vref_caps(codec, pin));
2870 return nitems ? nitems : 1;
2871}
2872
Takashi Iwai294765582013-01-17 09:52:11 +01002873static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2874{
2875 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002876 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002877 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002878 unsigned int defcfg;
2879
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002880 if (pin == spec->hp_mic_pin)
2881 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002882
2883 /* no jack mode for fixed pins */
2884 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2885 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2886 return 0;
2887
2888 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002889 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002890 return 0;
2891
2892 get_jack_mode_name(codec, pin, name, sizeof(name));
2893 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2894 if (!knew)
2895 return -ENOMEM;
2896 knew->private_value = pin;
2897 return 0;
2898}
2899
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002900/*
2901 * HP/mic shared jack mode
2902 */
2903static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2904 struct snd_ctl_elem_info *uinfo)
2905{
2906 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2907 hda_nid_t nid = kcontrol->private_value;
2908 int out_jacks = get_out_jack_num_items(codec, nid);
2909 int in_jacks = get_in_jack_num_items(codec, nid);
2910 const char *text = NULL;
2911 int idx;
2912
2913 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2914 uinfo->count = 1;
2915 uinfo->value.enumerated.items = out_jacks + in_jacks;
2916 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2917 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2918 idx = uinfo->value.enumerated.item;
2919 if (idx < out_jacks) {
2920 if (out_jacks > 1)
2921 text = out_jack_texts[idx];
2922 else
2923 text = "Headphone Out";
2924 } else {
2925 idx -= out_jacks;
2926 if (in_jacks > 1) {
2927 unsigned int vref_caps = get_vref_caps(codec, nid);
2928 text = vref_texts[get_vref_idx(vref_caps, idx)];
2929 } else
2930 text = "Mic In";
2931 }
2932
2933 strcpy(uinfo->value.enumerated.name, text);
2934 return 0;
2935}
2936
2937static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2938{
2939 int out_jacks = get_out_jack_num_items(codec, nid);
2940 int in_jacks = get_in_jack_num_items(codec, nid);
2941 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2942 int idx = 0;
2943
2944 if (val & PIN_OUT) {
2945 if (out_jacks > 1 && val == PIN_HP)
2946 idx = 1;
2947 } else if (val & PIN_IN) {
2948 idx = out_jacks;
2949 if (in_jacks > 1) {
2950 unsigned int vref_caps = get_vref_caps(codec, nid);
2951 val &= AC_PINCTL_VREFEN;
2952 idx += cvt_from_vref_idx(vref_caps, val);
2953 }
2954 }
2955 return idx;
2956}
2957
2958static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2959 struct snd_ctl_elem_value *ucontrol)
2960{
2961 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2962 hda_nid_t nid = kcontrol->private_value;
2963 ucontrol->value.enumerated.item[0] =
2964 get_cur_hp_mic_jack_mode(codec, nid);
2965 return 0;
2966}
2967
2968static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2969 struct snd_ctl_elem_value *ucontrol)
2970{
2971 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2972 hda_nid_t nid = kcontrol->private_value;
2973 int out_jacks = get_out_jack_num_items(codec, nid);
2974 int in_jacks = get_in_jack_num_items(codec, nid);
2975 unsigned int val, oldval, idx;
2976
2977 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2978 idx = ucontrol->value.enumerated.item[0];
2979 if (oldval == idx)
2980 return 0;
2981
2982 if (idx < out_jacks) {
2983 if (out_jacks > 1)
2984 val = idx ? PIN_HP : PIN_OUT;
2985 else
2986 val = PIN_HP;
2987 } else {
2988 idx -= out_jacks;
2989 if (in_jacks > 1) {
2990 unsigned int vref_caps = get_vref_caps(codec, nid);
2991 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002992 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2993 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002994 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002995 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002996 }
2997 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002998 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002999
Takashi Iwai5f171ba2013-02-19 18:14:54 +01003000 return 1;
3001}
3002
3003static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
3004 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3005 .info = hp_mic_jack_mode_info,
3006 .get = hp_mic_jack_mode_get,
3007 .put = hp_mic_jack_mode_put,
3008};
3009
3010static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
3011{
3012 struct hda_gen_spec *spec = codec->spec;
3013 struct snd_kcontrol_new *knew;
3014
Takashi Iwai5f171ba2013-02-19 18:14:54 +01003015 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
3016 &hp_mic_jack_mode_enum);
3017 if (!knew)
3018 return -ENOMEM;
3019 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01003020 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01003021 return 0;
3022}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003023
3024/*
3025 * Parse input paths
3026 */
3027
Takashi Iwai352f7f92012-12-19 12:52:06 +01003028/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003029static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003030{
3031 struct hda_amp_list *list;
3032
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003033 list = snd_array_new(&spec->loopback_list);
3034 if (!list)
3035 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003036 list->nid = mix;
3037 list->dir = HDA_INPUT;
3038 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003039 spec->loopback.amplist = spec->loopback_list.list;
3040 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003041}
Takashi Iwaicb53c622007-08-10 17:21:45 +02003042
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003043/* return true if either a volume or a mute amp is found for the given
3044 * aamix path; the amp has to be either in the mixer node or its direct leaf
3045 */
3046static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3047 hda_nid_t pin, unsigned int *mix_val,
3048 unsigned int *mute_val)
3049{
3050 int idx, num_conns;
3051 const hda_nid_t *list;
3052 hda_nid_t nid;
3053
3054 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3055 if (idx < 0)
3056 return false;
3057
3058 *mix_val = *mute_val = 0;
3059 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3060 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3061 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3062 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3063 if (*mix_val && *mute_val)
3064 return true;
3065
3066 /* check leaf node */
3067 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3068 if (num_conns < idx)
3069 return false;
3070 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01003071 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3072 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003073 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01003074 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3075 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003076 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3077
3078 return *mix_val || *mute_val;
3079}
3080
Takashi Iwai352f7f92012-12-19 12:52:06 +01003081/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01003082static int new_analog_input(struct hda_codec *codec, int input_idx,
3083 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003084 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003086 struct hda_gen_spec *spec = codec->spec;
3087 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003088 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003089 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003091 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3092 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003093
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003094 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003095 if (!path)
3096 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003097 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01003098 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003099
3100 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003101 if (mix_val) {
3102 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003103 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003105 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 }
3107
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003108 if (mute_val) {
3109 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003110 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003112 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 }
3114
Takashi Iwai352f7f92012-12-19 12:52:06 +01003115 path->active = true;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003116 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003117 err = add_loopback_list(spec, mix_nid, idx);
3118 if (err < 0)
3119 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003120
3121 if (spec->mixer_nid != spec->mixer_merge_nid &&
3122 !spec->loopback_merge_path) {
3123 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3124 spec->mixer_merge_nid, 0);
3125 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003126 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003127 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003128 path->pin_fixed = true; /* static route */
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003129 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003130 spec->loopback_merge_path =
3131 snd_hda_get_path_idx(codec, path);
3132 }
3133 }
3134
Takashi Iwai352f7f92012-12-19 12:52:06 +01003135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
Takashi Iwai352f7f92012-12-19 12:52:06 +01003138static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003140 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3141 return (pincap & AC_PINCAP_IN) != 0;
3142}
3143
3144/* Parse the codec tree and retrieve ADCs */
3145static int fill_adc_nids(struct hda_codec *codec)
3146{
3147 struct hda_gen_spec *spec = codec->spec;
3148 hda_nid_t nid;
3149 hda_nid_t *adc_nids = spec->adc_nids;
3150 int max_nums = ARRAY_SIZE(spec->adc_nids);
Takashi Iwai7639a062015-03-03 10:07:24 +01003151 int nums = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003152
Takashi Iwai7639a062015-03-03 10:07:24 +01003153 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003154 unsigned int caps = get_wcaps(codec, nid);
3155 int type = get_wcaps_type(caps);
3156
3157 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3158 continue;
3159 adc_nids[nums] = nid;
3160 if (++nums >= max_nums)
3161 break;
3162 }
3163 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01003164
3165 /* copy the detected ADCs to all_adcs[] */
3166 spec->num_all_adcs = nums;
3167 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3168
Takashi Iwai352f7f92012-12-19 12:52:06 +01003169 return nums;
3170}
3171
3172/* filter out invalid adc_nids that don't give all active input pins;
3173 * if needed, check whether dynamic ADC-switching is available
3174 */
3175static int check_dyn_adc_switch(struct hda_codec *codec)
3176{
3177 struct hda_gen_spec *spec = codec->spec;
3178 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003179 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003180 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003181
Takashi Iwai352f7f92012-12-19 12:52:06 +01003182 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003183 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003184 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003185 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003186 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003187 break;
3188 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003189 if (i >= imux->num_items) {
3190 ok_bits |= (1 << n);
3191 nums++;
3192 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003193 }
3194
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003195 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003196 /* check whether ADC-switch is possible */
3197 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003198 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003199 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003200 spec->dyn_adc_idx[i] = n;
3201 break;
3202 }
3203 }
3204 }
3205
Takashi Iwai4e76a882014-02-25 12:21:03 +01003206 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003207 spec->dyn_adc_switch = 1;
3208 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003209 /* shrink the invalid adcs and input paths */
3210 nums = 0;
3211 for (n = 0; n < spec->num_adc_nids; n++) {
3212 if (!(ok_bits & (1 << n)))
3213 continue;
3214 if (n != nums) {
3215 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003216 for (i = 0; i < imux->num_items; i++) {
3217 invalidate_nid_path(codec,
3218 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003219 spec->input_paths[i][nums] =
3220 spec->input_paths[i][n];
Hui Wanga8f20fd2017-06-28 08:59:16 +08003221 spec->input_paths[i][n] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01003222 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003223 }
3224 nums++;
3225 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003226 spec->num_adc_nids = nums;
3227 }
3228
Takashi Iwai967303d2013-02-19 17:12:42 +01003229 if (imux->num_items == 1 ||
3230 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003231 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003232 spec->num_adc_nids = 1; /* reduce to a single ADC */
3233 }
3234
3235 /* single index for individual volumes ctls */
3236 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3237 spec->num_adc_nids = 1;
3238
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 return 0;
3240}
3241
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003242/* parse capture source paths from the given pin and create imux items */
3243static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003244 int cfg_idx, int num_adcs,
3245 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003246{
3247 struct hda_gen_spec *spec = codec->spec;
3248 struct hda_input_mux *imux = &spec->input_mux;
3249 int imux_idx = imux->num_items;
3250 bool imux_added = false;
3251 int c;
3252
3253 for (c = 0; c < num_adcs; c++) {
3254 struct nid_path *path;
3255 hda_nid_t adc = spec->adc_nids[c];
3256
3257 if (!is_reachable_path(codec, pin, adc))
3258 continue;
3259 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3260 if (!path)
3261 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003262 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003263 spec->input_paths[imux_idx][c] =
3264 snd_hda_get_path_idx(codec, path);
3265
3266 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003267 if (spec->hp_mic_pin == pin)
3268 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003269 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003270 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003271 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003272 if (spec->dyn_adc_switch)
3273 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003274 }
3275 }
3276
3277 return 0;
3278}
3279
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003281 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003283
Takashi Iwaic9700422013-01-18 10:17:30 +01003284/* fill the label for each input at first */
3285static int fill_input_pin_labels(struct hda_codec *codec)
3286{
3287 struct hda_gen_spec *spec = codec->spec;
3288 const struct auto_pin_cfg *cfg = &spec->autocfg;
3289 int i;
3290
3291 for (i = 0; i < cfg->num_inputs; i++) {
3292 hda_nid_t pin = cfg->inputs[i].pin;
3293 const char *label;
3294 int j, idx;
3295
3296 if (!is_input_pin(codec, pin))
3297 continue;
3298
3299 label = hda_get_autocfg_input_label(codec, cfg, i);
3300 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003301 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003302 if (spec->input_labels[j] &&
3303 !strcmp(spec->input_labels[j], label)) {
3304 idx = spec->input_label_idxs[j] + 1;
3305 break;
3306 }
3307 }
3308
3309 spec->input_labels[i] = label;
3310 spec->input_label_idxs[i] = idx;
3311 }
3312
3313 return 0;
3314}
3315
Takashi Iwai9dba2052013-01-18 10:01:15 +01003316#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3317
Takashi Iwai352f7f92012-12-19 12:52:06 +01003318static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003319{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003320 struct hda_gen_spec *spec = codec->spec;
3321 const struct auto_pin_cfg *cfg = &spec->autocfg;
3322 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003323 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003324 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003325 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003326
Takashi Iwai352f7f92012-12-19 12:52:06 +01003327 num_adcs = fill_adc_nids(codec);
3328 if (num_adcs < 0)
3329 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003330
Takashi Iwaic9700422013-01-18 10:17:30 +01003331 err = fill_input_pin_labels(codec);
3332 if (err < 0)
3333 return err;
3334
Takashi Iwai352f7f92012-12-19 12:52:06 +01003335 for (i = 0; i < cfg->num_inputs; i++) {
3336 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Takashi Iwai352f7f92012-12-19 12:52:06 +01003338 pin = cfg->inputs[i].pin;
3339 if (!is_input_pin(codec, pin))
3340 continue;
3341
Takashi Iwai2c12c302013-01-10 09:33:29 +01003342 val = PIN_IN;
3343 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3344 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai3e1b0c42015-04-27 10:43:22 +02003345 if (pin != spec->hp_mic_pin &&
3346 !snd_hda_codec_get_pin_target(codec, pin))
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003347 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003348
Takashi Iwai352f7f92012-12-19 12:52:06 +01003349 if (mixer) {
3350 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003351 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003352 spec->input_labels[i],
3353 spec->input_label_idxs[i],
3354 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003355 if (err < 0)
3356 return err;
3357 }
3358 }
3359
Takashi Iwaic9700422013-01-18 10:17:30 +01003360 err = parse_capture_source(codec, pin, i, num_adcs,
3361 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003362 if (err < 0)
3363 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003364
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003365 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003366 err = create_in_jack_mode(codec, pin);
3367 if (err < 0)
3368 return err;
3369 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003370 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003371
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003372 /* add stereo mix when explicitly enabled via hint */
Takashi Iwai74f14b32014-12-15 13:43:59 +01003373 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003374 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003375 "Stereo Mix", 0);
3376 if (err < 0)
3377 return err;
Takashi Iwai82d04e12014-12-15 13:40:42 +01003378 else
3379 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003380 }
3381
3382 return 0;
3383}
3384
3385
3386/*
3387 * input source mux
3388 */
3389
Takashi Iwaic697b712013-01-07 17:09:26 +01003390/* get the input path specified by the given adc and imux indices */
3391static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003392{
3393 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003394 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3395 snd_BUG();
3396 return NULL;
3397 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003398 if (spec->dyn_adc_switch)
3399 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003400 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003401 snd_BUG();
3402 return NULL;
3403 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003404 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003405}
3406
3407static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3408 unsigned int idx);
3409
3410static int mux_enum_info(struct snd_kcontrol *kcontrol,
3411 struct snd_ctl_elem_info *uinfo)
3412{
3413 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3414 struct hda_gen_spec *spec = codec->spec;
3415 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3416}
3417
3418static int mux_enum_get(struct snd_kcontrol *kcontrol,
3419 struct snd_ctl_elem_value *ucontrol)
3420{
3421 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3422 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003423 /* the ctls are created at once with multiple counts */
3424 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003425
3426 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3427 return 0;
3428}
3429
3430static int mux_enum_put(struct snd_kcontrol *kcontrol,
3431 struct snd_ctl_elem_value *ucontrol)
3432{
3433 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003434 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003435 return mux_select(codec, adc_idx,
3436 ucontrol->value.enumerated.item[0]);
3437}
3438
Takashi Iwai352f7f92012-12-19 12:52:06 +01003439static const struct snd_kcontrol_new cap_src_temp = {
3440 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3441 .name = "Input Source",
3442 .info = mux_enum_info,
3443 .get = mux_enum_get,
3444 .put = mux_enum_put,
3445};
3446
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003447/*
3448 * capture volume and capture switch ctls
3449 */
3450
Takashi Iwai352f7f92012-12-19 12:52:06 +01003451typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3452 struct snd_ctl_elem_value *ucontrol);
3453
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003454/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003455static int cap_put_caller(struct snd_kcontrol *kcontrol,
3456 struct snd_ctl_elem_value *ucontrol,
3457 put_call_t func, int type)
3458{
3459 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3460 struct hda_gen_spec *spec = codec->spec;
3461 const struct hda_input_mux *imux;
3462 struct nid_path *path;
Jaroslav Kyselaa2befe92021-08-11 18:14:41 +02003463 int i, adc_idx, ret, err = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003464
3465 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003466 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003467 mutex_lock(&codec->control_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003468 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003469 path = get_input_path(codec, adc_idx, i);
3470 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003471 continue;
3472 kcontrol->private_value = path->ctls[type];
Jaroslav Kyselaa2befe92021-08-11 18:14:41 +02003473 ret = func(kcontrol, ucontrol);
3474 if (ret < 0) {
3475 err = ret;
Takashi Iwaia551d912015-02-26 12:34:49 +01003476 break;
Jaroslav Kyselaa2befe92021-08-11 18:14:41 +02003477 }
3478 if (ret > 0)
3479 err = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003480 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003481 mutex_unlock(&codec->control_mutex);
3482 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003483 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003484 return err;
3485}
3486
3487/* capture volume ctl callbacks */
3488#define cap_vol_info snd_hda_mixer_amp_volume_info
3489#define cap_vol_get snd_hda_mixer_amp_volume_get
3490#define cap_vol_tlv snd_hda_mixer_amp_tlv
3491
3492static int cap_vol_put(struct snd_kcontrol *kcontrol,
3493 struct snd_ctl_elem_value *ucontrol)
3494{
3495 return cap_put_caller(kcontrol, ucontrol,
3496 snd_hda_mixer_amp_volume_put,
3497 NID_PATH_VOL_CTL);
3498}
3499
3500static const struct snd_kcontrol_new cap_vol_temp = {
3501 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3502 .name = "Capture Volume",
3503 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3504 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3505 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3506 .info = cap_vol_info,
3507 .get = cap_vol_get,
3508 .put = cap_vol_put,
3509 .tlv = { .c = cap_vol_tlv },
3510};
3511
3512/* capture switch ctl callbacks */
3513#define cap_sw_info snd_ctl_boolean_stereo_info
3514#define cap_sw_get snd_hda_mixer_amp_switch_get
3515
3516static int cap_sw_put(struct snd_kcontrol *kcontrol,
3517 struct snd_ctl_elem_value *ucontrol)
3518{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003519 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003520 snd_hda_mixer_amp_switch_put,
3521 NID_PATH_MUTE_CTL);
3522}
3523
3524static const struct snd_kcontrol_new cap_sw_temp = {
3525 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3526 .name = "Capture Switch",
Takashi Iwai08a4b902021-05-31 20:06:33 +02003527 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003528 .info = cap_sw_info,
3529 .get = cap_sw_get,
3530 .put = cap_sw_put,
3531};
3532
3533static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3534{
3535 hda_nid_t nid;
3536 int i, depth;
3537
3538 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3539 for (depth = 0; depth < 3; depth++) {
3540 if (depth >= path->depth)
3541 return -EINVAL;
3542 i = path->depth - depth - 1;
3543 nid = path->path[i];
3544 if (!path->ctls[NID_PATH_VOL_CTL]) {
3545 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3546 path->ctls[NID_PATH_VOL_CTL] =
3547 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3548 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3549 int idx = path->idx[i];
3550 if (!depth && codec->single_adc_amp)
3551 idx = 0;
3552 path->ctls[NID_PATH_VOL_CTL] =
3553 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3554 }
3555 }
3556 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3557 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3558 path->ctls[NID_PATH_MUTE_CTL] =
3559 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3560 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3561 int idx = path->idx[i];
3562 if (!depth && codec->single_adc_amp)
3563 idx = 0;
3564 path->ctls[NID_PATH_MUTE_CTL] =
3565 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3566 }
3567 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 return 0;
3570}
3571
Takashi Iwai352f7f92012-12-19 12:52:06 +01003572static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003574 struct hda_gen_spec *spec = codec->spec;
3575 struct auto_pin_cfg *cfg = &spec->autocfg;
3576 unsigned int val;
3577 int i;
3578
3579 if (!spec->inv_dmic_split)
3580 return false;
3581 for (i = 0; i < cfg->num_inputs; i++) {
3582 if (cfg->inputs[i].pin != nid)
3583 continue;
3584 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3585 return false;
3586 val = snd_hda_codec_get_pincfg(codec, nid);
3587 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3588 }
3589 return false;
3590}
3591
Takashi Iwaia90229e2013-01-18 14:10:00 +01003592/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003593static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3594 struct snd_ctl_elem_value *ucontrol)
3595{
3596 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3597 struct hda_gen_spec *spec = codec->spec;
3598 int ret;
3599
3600 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3601 if (ret < 0)
3602 return ret;
3603
Takashi Iwaia90229e2013-01-18 14:10:00 +01003604 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003605 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003606
3607 return ret;
3608}
3609
Takashi Iwai352f7f92012-12-19 12:52:06 +01003610static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3611 int idx, bool is_switch, unsigned int ctl,
3612 bool inv_dmic)
3613{
3614 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003615 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003616 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3617 const char *sfx = is_switch ? "Switch" : "Volume";
3618 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003619 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003620
3621 if (!ctl)
3622 return 0;
3623
3624 if (label)
3625 snprintf(tmpname, sizeof(tmpname),
3626 "%s Capture %s", label, sfx);
3627 else
3628 snprintf(tmpname, sizeof(tmpname),
3629 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003630 knew = add_control(spec, type, tmpname, idx,
3631 amp_val_replace_channels(ctl, chs));
3632 if (!knew)
3633 return -ENOMEM;
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01003634 if (is_switch) {
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003635 knew->put = cap_single_sw_put;
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01003636 if (spec->mic_mute_led)
3637 knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED;
3638 }
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003639 if (!inv_dmic)
3640 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003641
3642 /* Make independent right kcontrol */
3643 if (label)
3644 snprintf(tmpname, sizeof(tmpname),
3645 "Inverted %s Capture %s", label, sfx);
3646 else
3647 snprintf(tmpname, sizeof(tmpname),
3648 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003649 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003650 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003651 if (!knew)
3652 return -ENOMEM;
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01003653 if (is_switch) {
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003654 knew->put = cap_single_sw_put;
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01003655 if (spec->mic_mute_led)
3656 knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED;
3657 }
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003658 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003659}
3660
3661/* create single (and simple) capture volume and switch controls */
3662static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3663 unsigned int vol_ctl, unsigned int sw_ctl,
3664 bool inv_dmic)
3665{
3666 int err;
3667 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3668 if (err < 0)
3669 return err;
3670 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3671 if (err < 0)
3672 return err;
3673 return 0;
3674}
3675
3676/* create bound capture volume and switch controls */
3677static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3678 unsigned int vol_ctl, unsigned int sw_ctl)
3679{
3680 struct hda_gen_spec *spec = codec->spec;
3681 struct snd_kcontrol_new *knew;
3682
3683 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003684 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003685 if (!knew)
3686 return -ENOMEM;
3687 knew->index = idx;
3688 knew->private_value = vol_ctl;
3689 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3690 }
3691 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003692 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003693 if (!knew)
3694 return -ENOMEM;
3695 knew->index = idx;
3696 knew->private_value = sw_ctl;
3697 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01003698 if (spec->mic_mute_led)
3699 knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003700 }
3701 return 0;
3702}
3703
3704/* return the vol ctl when used first in the imux list */
3705static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3706{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003707 struct nid_path *path;
3708 unsigned int ctl;
3709 int i;
3710
Takashi Iwaic697b712013-01-07 17:09:26 +01003711 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003712 if (!path)
3713 return 0;
3714 ctl = path->ctls[type];
3715 if (!ctl)
3716 return 0;
3717 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003718 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003719 if (path && path->ctls[type] == ctl)
3720 return 0;
3721 }
3722 return ctl;
3723}
3724
3725/* create individual capture volume and switch controls per input */
3726static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3727{
3728 struct hda_gen_spec *spec = codec->spec;
3729 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003730 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003731
3732 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003733 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003734 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003735
Takashi Iwaic9700422013-01-18 10:17:30 +01003736 idx = imux->items[i].index;
3737 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003738 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003739 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3740
3741 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003742 err = add_single_cap_ctl(codec,
3743 spec->input_labels[idx],
3744 spec->input_label_idxs[idx],
3745 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003746 get_first_cap_ctl(codec, i, type),
3747 inv_dmic);
3748 if (err < 0)
3749 return err;
3750 }
3751 }
3752 return 0;
3753}
3754
3755static int create_capture_mixers(struct hda_codec *codec)
3756{
3757 struct hda_gen_spec *spec = codec->spec;
3758 struct hda_input_mux *imux = &spec->input_mux;
3759 int i, n, nums, err;
3760
3761 if (spec->dyn_adc_switch)
3762 nums = 1;
3763 else
3764 nums = spec->num_adc_nids;
3765
3766 if (!spec->auto_mic && imux->num_items > 1) {
3767 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003768 const char *name;
3769 name = nums > 1 ? "Input Source" : "Capture Source";
3770 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003771 if (!knew)
3772 return -ENOMEM;
3773 knew->count = nums;
3774 }
3775
3776 for (n = 0; n < nums; n++) {
3777 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003778 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003779 bool inv_dmic = false;
3780 int vol, sw;
3781
3782 vol = sw = 0;
3783 for (i = 0; i < imux->num_items; i++) {
3784 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003785 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003786 if (!path)
3787 continue;
3788 parse_capvol_in_path(codec, path);
3789 if (!vol)
3790 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003791 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003792 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003793 if (!same_amp_caps(codec, vol,
3794 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3795 multi_cap_vol = true;
3796 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003797 if (!sw)
3798 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003799 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003800 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003801 if (!same_amp_caps(codec, sw,
3802 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3803 multi_cap_vol = true;
3804 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003805 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3806 inv_dmic = true;
3807 }
3808
3809 if (!multi)
3810 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3811 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003812 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003813 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3814 else
3815 err = create_multi_cap_vol_ctl(codec);
3816 if (err < 0)
3817 return err;
3818 }
3819
3820 return 0;
3821}
3822
3823/*
3824 * add mic boosts if needed
3825 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003826
3827/* check whether the given amp is feasible as a boost volume */
3828static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3829 int dir, int idx)
3830{
3831 unsigned int step;
3832
3833 if (!nid_has_volume(codec, nid, dir) ||
3834 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3835 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3836 return false;
3837
3838 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3839 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3840 if (step < 0x20)
3841 return false;
3842 return true;
3843}
3844
3845/* look for a boost amp in a widget close to the pin */
3846static unsigned int look_for_boost_amp(struct hda_codec *codec,
3847 struct nid_path *path)
3848{
3849 unsigned int val = 0;
3850 hda_nid_t nid;
3851 int depth;
3852
3853 for (depth = 0; depth < 3; depth++) {
3854 if (depth >= path->depth - 1)
3855 break;
3856 nid = path->path[depth];
3857 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3858 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3859 break;
3860 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3861 path->idx[depth])) {
3862 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3863 HDA_INPUT);
3864 break;
3865 }
3866 }
3867
3868 return val;
3869}
3870
Takashi Iwai352f7f92012-12-19 12:52:06 +01003871static int parse_mic_boost(struct hda_codec *codec)
3872{
3873 struct hda_gen_spec *spec = codec->spec;
3874 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003875 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003876 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003877
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003878 if (!spec->num_adc_nids)
3879 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003880
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003881 for (i = 0; i < imux->num_items; i++) {
3882 struct nid_path *path;
3883 unsigned int val;
3884 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003885 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003886
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003887 idx = imux->items[i].index;
3888 if (idx >= imux->num_items)
3889 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003890
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003891 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003892 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003893 continue;
3894
3895 path = get_input_path(codec, 0, i);
3896 if (!path)
3897 continue;
3898
3899 val = look_for_boost_amp(codec, path);
3900 if (!val)
3901 continue;
3902
3903 /* create a boost control */
3904 snprintf(boost_label, sizeof(boost_label),
3905 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003906 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3907 spec->input_label_idxs[idx], val))
3908 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003909
3910 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003911 }
3912 return 0;
3913}
3914
Takashi Iwai7cdf8c42020-06-18 13:08:31 +02003915#ifdef CONFIG_SND_HDA_GENERIC_LEDS
Takashi Iwai352f7f92012-12-19 12:52:06 +01003916/*
Takashi Iwai15509b62020-06-18 13:08:37 +02003917 * vmaster mute LED hook helpers
3918 */
3919
3920static int create_mute_led_cdev(struct hda_codec *codec,
3921 int (*callback)(struct led_classdev *,
3922 enum led_brightness),
3923 bool micmute)
3924{
3925 struct led_classdev *cdev;
3926
3927 cdev = devm_kzalloc(&codec->core.dev, sizeof(*cdev), GFP_KERNEL);
3928 if (!cdev)
3929 return -ENOMEM;
3930
3931 cdev->name = micmute ? "hda::micmute" : "hda::mute";
3932 cdev->max_brightness = 1;
3933 cdev->default_trigger = micmute ? "audio-micmute" : "audio-mute";
3934 cdev->brightness_set_blocking = callback;
3935 cdev->brightness = ledtrig_audio_get(micmute ? LED_AUDIO_MICMUTE : LED_AUDIO_MUTE);
Takashi Iwaic9e272f2020-06-18 13:08:42 +02003936 cdev->flags = LED_CORE_SUSPENDRESUME;
Takashi Iwai15509b62020-06-18 13:08:37 +02003937
3938 return devm_led_classdev_register(&codec->core.dev, cdev);
3939}
3940
Takashi Iwai15509b62020-06-18 13:08:37 +02003941/**
Pierre-Louis Bossart3531ba22021-03-01 11:46:17 -06003942 * snd_hda_gen_add_mute_led_cdev - Create a LED classdev and enable as vmaster mute LED
Takashi Iwai15509b62020-06-18 13:08:37 +02003943 * @codec: the HDA codec
3944 * @callback: the callback for LED classdev brightness_set_blocking
3945 */
3946int snd_hda_gen_add_mute_led_cdev(struct hda_codec *codec,
3947 int (*callback)(struct led_classdev *,
3948 enum led_brightness))
3949{
3950 struct hda_gen_spec *spec = codec->spec;
3951 int err;
3952
3953 if (callback) {
3954 err = create_mute_led_cdev(codec, callback, false);
3955 if (err) {
3956 codec_warn(codec, "failed to create a mute LED cdev\n");
3957 return err;
3958 }
3959 }
3960
3961 if (spec->vmaster_mute.hook)
3962 codec_err(codec, "vmaster hook already present before cdev!\n");
3963
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01003964 spec->vmaster_mute_led = 1;
Takashi Iwai15509b62020-06-18 13:08:37 +02003965 return 0;
3966}
3967EXPORT_SYMBOL_GPL(snd_hda_gen_add_mute_led_cdev);
3968
Takashi Iwaib3802782018-11-26 17:47:46 +01003969/**
Pierre-Louis Bossart3531ba22021-03-01 11:46:17 -06003970 * snd_hda_gen_add_micmute_led_cdev - Create a LED classdev and enable as mic-mute LED
Takashi Iwai7cdf8c42020-06-18 13:08:31 +02003971 * @codec: the HDA codec
3972 * @callback: the callback for LED classdev brightness_set_blocking
3973 *
3974 * Called from the codec drivers for offering the mic mute LED controls.
3975 * This creates a LED classdev and sets up the cap_sync_hook that is called at
3976 * each time when the capture mixer switch changes.
3977 *
3978 * When NULL is passed to @callback, no classdev is created but only the
3979 * LED-trigger is set up.
3980 *
3981 * Returns 0 or a negative error.
3982 */
3983int snd_hda_gen_add_micmute_led_cdev(struct hda_codec *codec,
3984 int (*callback)(struct led_classdev *,
3985 enum led_brightness))
3986{
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01003987 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai7cdf8c42020-06-18 13:08:31 +02003988 int err;
Takashi Iwai7cdf8c42020-06-18 13:08:31 +02003989
3990 if (callback) {
Takashi Iwai15509b62020-06-18 13:08:37 +02003991 err = create_mute_led_cdev(codec, callback, true);
Takashi Iwai7cdf8c42020-06-18 13:08:31 +02003992 if (err) {
3993 codec_warn(codec, "failed to create a mic-mute LED cdev\n");
3994 return err;
3995 }
3996 }
3997
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01003998 spec->mic_mute_led = 1;
3999 return 0;
Takashi Iwai7cdf8c42020-06-18 13:08:31 +02004000}
4001EXPORT_SYMBOL_GPL(snd_hda_gen_add_micmute_led_cdev);
4002#endif /* CONFIG_SND_HDA_GENERIC_LEDS */
4003
Takashi Iwaif567b782018-06-18 17:26:12 +02004004/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004005 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
4006 */
4007static void parse_digital(struct hda_codec *codec)
4008{
4009 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01004010 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004011 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004012 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004013
Takashi Iwai9ab0cb32020-07-17 17:45:17 +02004014 /* support multiple SPDIFs; the secondary is set up as a follower */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004015 nums = 0;
4016 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01004017 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01004018 dig_nid = look_for_dac(codec, pin, true);
4019 if (!dig_nid)
4020 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01004021 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01004022 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004023 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004024 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004025 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01004026 path->pin_fixed = true; /* no jack detection */
Takashi Iwai196c17662013-01-04 15:01:40 +01004027 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01004028 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004029 if (!nums) {
4030 spec->multiout.dig_out_nid = dig_nid;
4031 spec->dig_out_type = spec->autocfg.dig_out_type[0];
4032 } else {
Takashi Iwai9ab0cb32020-07-17 17:45:17 +02004033 spec->multiout.follower_dig_outs = spec->follower_dig_outs;
4034 if (nums >= ARRAY_SIZE(spec->follower_dig_outs) - 1)
Dan Carpenterd576422e2014-05-14 17:18:31 +03004035 break;
Takashi Iwai9ab0cb32020-07-17 17:45:17 +02004036 spec->follower_dig_outs[nums - 1] = dig_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004037 }
4038 nums++;
4039 }
4040
4041 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01004042 pin = spec->autocfg.dig_in_pin;
Takashi Iwai7639a062015-03-03 10:07:24 +01004043 for_each_hda_codec_node(dig_nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004044 unsigned int wcaps = get_wcaps(codec, dig_nid);
4045 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
4046 continue;
4047 if (!(wcaps & AC_WCAP_DIGITAL))
4048 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004049 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004050 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004051 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004052 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01004053 path->pin_fixed = true; /* no jack */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004054 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004055 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01004056 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004057 break;
4058 }
4059 }
4060 }
4061}
4062
4063
4064/*
4065 * input MUX handling
4066 */
4067
4068static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
4069
4070/* select the given imux item; either unmute exclusively or select the route */
4071static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
4072 unsigned int idx)
4073{
4074 struct hda_gen_spec *spec = codec->spec;
4075 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01004076 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004077
4078 imux = &spec->input_mux;
4079 if (!imux->num_items)
4080 return 0;
4081
4082 if (idx >= imux->num_items)
4083 idx = imux->num_items - 1;
4084 if (spec->cur_mux[adc_idx] == idx)
4085 return 0;
4086
Takashi Iwai55196ff2013-01-24 17:32:56 +01004087 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
4088 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004089 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01004090 if (old_path->active)
4091 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004092
4093 spec->cur_mux[adc_idx] = idx;
4094
Takashi Iwai967303d2013-02-19 17:12:42 +01004095 if (spec->hp_mic)
4096 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004097
4098 if (spec->dyn_adc_switch)
4099 dyn_adc_pcm_resetup(codec, idx);
4100
Takashi Iwaic697b712013-01-07 17:09:26 +01004101 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004102 if (!path)
4103 return 0;
4104 if (path->active)
4105 return 0;
4106 snd_hda_activate_path(codec, path, true, false);
4107 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01004108 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004109 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004110 return 1;
4111}
4112
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004113/* power up/down widgets in the all paths that match with the given NID
4114 * as terminals (either start- or endpoint)
4115 *
4116 * returns the last changed NID, or zero if unchanged.
4117 */
4118static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
4119 int pin_state, int stream_state)
4120{
4121 struct hda_gen_spec *spec = codec->spec;
4122 hda_nid_t last, changed = 0;
4123 struct nid_path *path;
4124 int n;
4125
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +02004126 snd_array_for_each(&spec->paths, n, path) {
Bob Copeland81e43962016-06-25 07:58:45 -04004127 if (!path->depth)
4128 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004129 if (path->path[0] == nid ||
4130 path->path[path->depth - 1] == nid) {
4131 bool pin_old = path->pin_enabled;
4132 bool stream_old = path->stream_enabled;
4133
4134 if (pin_state >= 0)
4135 path->pin_enabled = pin_state;
4136 if (stream_state >= 0)
4137 path->stream_enabled = stream_state;
Takashi Iwai6b275b12015-03-20 18:11:05 +01004138 if ((!path->pin_fixed && path->pin_enabled != pin_old)
4139 || path->stream_enabled != stream_old) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004140 last = path_power_update(codec, path, true);
4141 if (last)
4142 changed = last;
4143 }
4144 }
4145 }
4146 return changed;
4147}
4148
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004149/* check the jack status for power control */
4150static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4151{
4152 if (!is_jack_detectable(codec, pin))
4153 return true;
4154 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4155}
4156
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004157/* power up/down the paths of the given pin according to the jack state;
4158 * power = 0/1 : only power up/down if it matches with the jack state,
4159 * < 0 : force power up/down to follow the jack sate
4160 *
4161 * returns the last changed NID, or zero if unchanged.
4162 */
4163static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4164 int power)
4165{
4166 bool on;
4167
Takashi Iwai967b1302015-03-20 18:21:03 +01004168 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004169 return 0;
4170
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004171 on = detect_pin_state(codec, pin);
4172
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004173 if (power >= 0 && on != power)
4174 return 0;
4175 return set_path_power(codec, pin, on, -1);
4176}
4177
4178static void pin_power_callback(struct hda_codec *codec,
4179 struct hda_jack_callback *jack,
4180 bool on)
4181{
Takashi Iwai2ebab402016-02-09 10:23:52 +01004182 if (jack && jack->nid)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004183 sync_power_state_change(codec,
Takashi Iwai2ebab402016-02-09 10:23:52 +01004184 set_pin_power_jack(codec, jack->nid, on));
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004185}
4186
4187/* callback only doing power up -- called at first */
4188static void pin_power_up_callback(struct hda_codec *codec,
4189 struct hda_jack_callback *jack)
4190{
4191 pin_power_callback(codec, jack, true);
4192}
4193
4194/* callback only doing power down -- called at last */
4195static void pin_power_down_callback(struct hda_codec *codec,
4196 struct hda_jack_callback *jack)
4197{
4198 pin_power_callback(codec, jack, false);
4199}
4200
4201/* set up the power up/down callbacks */
4202static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4203 const hda_nid_t *pins, bool on)
4204{
4205 int i;
4206 hda_jack_callback_fn cb =
4207 on ? pin_power_up_callback : pin_power_down_callback;
4208
4209 for (i = 0; i < num_pins && pins[i]; i++) {
4210 if (is_jack_detectable(codec, pins[i]))
4211 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4212 else
4213 set_path_power(codec, pins[i], true, -1);
4214 }
4215}
4216
4217/* enabled power callback to each available I/O pin with jack detections;
4218 * the digital I/O pins are excluded because of the unreliable detectsion
4219 */
4220static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4221{
4222 struct hda_gen_spec *spec = codec->spec;
4223 struct auto_pin_cfg *cfg = &spec->autocfg;
4224 int i;
4225
Takashi Iwai967b1302015-03-20 18:21:03 +01004226 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004227 return;
4228 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4229 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4230 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4231 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4232 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4233 for (i = 0; i < cfg->num_inputs; i++)
4234 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4235}
4236
4237/* sync path power up/down with the jack states of given pins */
4238static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4239 const hda_nid_t *pins)
4240{
4241 int i;
4242
4243 for (i = 0; i < num_pins && pins[i]; i++)
4244 if (is_jack_detectable(codec, pins[i]))
4245 set_pin_power_jack(codec, pins[i], -1);
4246}
4247
4248/* sync path power up/down with pins; called at init and resume */
4249static void sync_all_pin_power_ctls(struct hda_codec *codec)
4250{
4251 struct hda_gen_spec *spec = codec->spec;
4252 struct auto_pin_cfg *cfg = &spec->autocfg;
4253 int i;
4254
Takashi Iwai967b1302015-03-20 18:21:03 +01004255 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004256 return;
4257 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4258 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4259 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4260 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4261 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4262 for (i = 0; i < cfg->num_inputs; i++)
4263 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4264}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004265
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004266/* add fake paths if not present yet */
4267static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4268 int num_pins, const hda_nid_t *pins)
4269{
4270 struct hda_gen_spec *spec = codec->spec;
4271 struct nid_path *path;
4272 int i;
4273
4274 for (i = 0; i < num_pins; i++) {
4275 if (!pins[i])
4276 break;
4277 if (get_nid_path(codec, nid, pins[i], 0))
4278 continue;
4279 path = snd_array_new(&spec->paths);
4280 if (!path)
4281 return -ENOMEM;
4282 memset(path, 0, sizeof(*path));
4283 path->depth = 2;
4284 path->path[0] = nid;
4285 path->path[1] = pins[i];
4286 path->active = true;
4287 }
4288 return 0;
4289}
4290
4291/* create fake paths to all outputs from beep */
4292static int add_fake_beep_paths(struct hda_codec *codec)
4293{
4294 struct hda_gen_spec *spec = codec->spec;
4295 struct auto_pin_cfg *cfg = &spec->autocfg;
4296 hda_nid_t nid = spec->beep_nid;
4297 int err;
4298
Takashi Iwai967b1302015-03-20 18:21:03 +01004299 if (!codec->power_save_node || !nid)
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004300 return 0;
4301 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4302 if (err < 0)
4303 return err;
4304 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4305 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4306 if (err < 0)
4307 return err;
4308 }
4309 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4310 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4311 cfg->speaker_pins);
4312 if (err < 0)
4313 return err;
4314 }
4315 return 0;
4316}
4317
4318/* power up/down beep widget and its output paths */
4319static void beep_power_hook(struct hda_beep *beep, bool on)
4320{
4321 set_path_power(beep->codec, beep->nid, -1, on);
4322}
4323
Takashi Iwai6b275b12015-03-20 18:11:05 +01004324/**
4325 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4326 * @codec: the HDA codec
4327 * @pin: NID of pin to fix
4328 */
4329int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4330{
4331 struct hda_gen_spec *spec = codec->spec;
4332 struct nid_path *path;
4333
4334 path = snd_array_new(&spec->paths);
4335 if (!path)
4336 return -ENOMEM;
4337 memset(path, 0, sizeof(*path));
4338 path->depth = 1;
4339 path->path[0] = pin;
4340 path->active = true;
4341 path->pin_fixed = true;
4342 path->stream_enabled = true;
4343 return 0;
4344}
4345EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4346
Takashi Iwai352f7f92012-12-19 12:52:06 +01004347/*
4348 * Jack detections for HP auto-mute and mic-switch
4349 */
4350
4351/* check each pin in the given array; returns true if any of them is plugged */
Michał Mirosławcaf3c042020-01-03 10:23:48 +01004352static bool detect_jacks(struct hda_codec *codec, int num_pins, const hda_nid_t *pins)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004353{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004354 int i;
4355 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004356
4357 for (i = 0; i < num_pins; i++) {
4358 hda_nid_t nid = pins[i];
4359 if (!nid)
4360 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01004361 /* don't detect pins retasked as inputs */
4362 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4363 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004364 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4365 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004366 }
4367 return present;
4368}
4369
4370/* standard HP/line-out auto-mute helper */
Michał Mirosławcaf3c042020-01-03 10:23:48 +01004371static void do_automute(struct hda_codec *codec, int num_pins, const hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004372 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004373{
4374 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004375 int i;
4376
4377 for (i = 0; i < num_pins; i++) {
4378 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01004379 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004380 if (!nid)
4381 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004382
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004383 oldval = snd_hda_codec_get_pin_target(codec, nid);
4384 if (oldval & PIN_IN)
4385 continue; /* no mute for inputs */
4386
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004387 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004388 struct nid_path *path;
4389 hda_nid_t mute_nid;
4390
4391 path = snd_hda_get_path_from_idx(codec, paths[i]);
4392 if (!path)
4393 continue;
4394 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4395 if (!mute_nid)
4396 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004397 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004398 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004399 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004400 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004401 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004402 } else {
4403 /* don't reset VREF value in case it's controlling
4404 * the amp (see alc861_fixup_asus_amp_vref_0f())
4405 */
4406 if (spec->keep_vref_in_automute)
4407 val = oldval & ~PIN_HP;
4408 else
4409 val = 0;
4410 if (!mute)
4411 val |= oldval;
4412 /* here we call update_pin_ctl() so that the pinctl is
4413 * changed without changing the pinctl target value;
4414 * the original target value will be still referred at
4415 * the init / resume again
4416 */
4417 update_pin_ctl(codec, nid, val);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004418 }
4419
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01004420 set_pin_eapd(codec, nid, !mute);
Takashi Iwai967b1302015-03-20 18:21:03 +01004421 if (codec->power_save_node) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004422 bool on = !mute;
4423 if (on)
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004424 on = detect_pin_state(codec, nid);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004425 set_path_power(codec, nid, on, -1);
4426 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004427 }
4428}
4429
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004430/**
4431 * snd_hda_gen_update_outputs - Toggle outputs muting
4432 * @codec: the HDA codec
4433 *
4434 * Update the mute status of all outputs based on the current jack states.
4435 */
Takashi Iwai5d550e12012-12-19 15:16:44 +01004436void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004437{
4438 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004439 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004440 int on;
4441
4442 /* Control HP pins/amps depending on master_mute state;
4443 * in general, HP pins/amps control should be enabled in all cases,
4444 * but currently set only for master_mute, just to be safe
4445 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004446 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4447 paths = spec->out_paths;
4448 else
4449 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01004450 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004451 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004452
4453 if (!spec->automute_speaker)
4454 on = 0;
4455 else
4456 on = spec->hp_jack_present | spec->line_jack_present;
4457 on |= spec->master_mute;
Takashi Iwai47b9ddb82013-01-16 18:18:00 +01004458 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004459 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4460 paths = spec->out_paths;
4461 else
4462 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004463 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004464 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004465
4466 /* toggle line-out mutes if needed, too */
4467 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4468 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4469 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4470 return;
4471 if (!spec->automute_lo)
4472 on = 0;
4473 else
4474 on = spec->hp_jack_present;
4475 on |= spec->master_mute;
Takashi Iwai47b9ddb82013-01-16 18:18:00 +01004476 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004477 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004478 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004479 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004480}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004481EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004482
4483static void call_update_outputs(struct hda_codec *codec)
4484{
4485 struct hda_gen_spec *spec = codec->spec;
4486 if (spec->automute_hook)
4487 spec->automute_hook(codec);
4488 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01004489 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004490
Takashi Iwai9ab0cb32020-07-17 17:45:17 +02004491 /* sync the whole vmaster followers to reflect the new auto-mute status */
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004492 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4493 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004494}
4495
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004496/**
4497 * snd_hda_gen_hp_automute - standard HP-automute helper
4498 * @codec: the HDA codec
4499 * @jack: jack object, NULL for the whole
4500 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004501void snd_hda_gen_hp_automute(struct hda_codec *codec,
4502 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004503{
4504 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01004505 hda_nid_t *pins = spec->autocfg.hp_pins;
4506 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004507
Takashi Iwai92603c52013-01-22 07:46:31 +01004508 /* No detection for the first HP jack during indep-HP mode */
4509 if (spec->indep_hp_enabled) {
4510 pins++;
4511 num_pins--;
4512 }
4513
4514 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004515 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4516 return;
4517 call_update_outputs(codec);
4518}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004519EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004520
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004521/**
4522 * snd_hda_gen_line_automute - standard line-out-automute helper
4523 * @codec: the HDA codec
4524 * @jack: jack object, NULL for the whole
4525 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004526void snd_hda_gen_line_automute(struct hda_codec *codec,
4527 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004528{
4529 struct hda_gen_spec *spec = codec->spec;
4530
4531 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4532 return;
4533 /* check LO jack only when it's different from HP */
4534 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4535 return;
4536
4537 spec->line_jack_present =
4538 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4539 spec->autocfg.line_out_pins);
4540 if (!spec->automute_speaker || !spec->detect_lo)
4541 return;
4542 call_update_outputs(codec);
4543}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004544EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004545
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004546/**
4547 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4548 * @codec: the HDA codec
4549 * @jack: jack object, NULL for the whole
4550 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004551void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4552 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004553{
4554 struct hda_gen_spec *spec = codec->spec;
4555 int i;
4556
4557 if (!spec->auto_mic)
4558 return;
4559
4560 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01004561 hda_nid_t pin = spec->am_entry[i].pin;
4562 /* don't detect pins retasked as outputs */
4563 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4564 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004565 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004566 mux_select(codec, 0, spec->am_entry[i].idx);
4567 return;
4568 }
4569 }
4570 mux_select(codec, 0, spec->am_entry[0].idx);
4571}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004572EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004573
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004574/* call appropriate hooks */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004575static void call_hp_automute(struct hda_codec *codec,
4576 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004577{
4578 struct hda_gen_spec *spec = codec->spec;
4579 if (spec->hp_automute_hook)
4580 spec->hp_automute_hook(codec, jack);
4581 else
4582 snd_hda_gen_hp_automute(codec, jack);
4583}
4584
4585static void call_line_automute(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004586 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004587{
4588 struct hda_gen_spec *spec = codec->spec;
4589 if (spec->line_automute_hook)
4590 spec->line_automute_hook(codec, jack);
4591 else
4592 snd_hda_gen_line_automute(codec, jack);
4593}
4594
4595static void call_mic_autoswitch(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004596 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004597{
4598 struct hda_gen_spec *spec = codec->spec;
4599 if (spec->mic_autoswitch_hook)
4600 spec->mic_autoswitch_hook(codec, jack);
4601 else
4602 snd_hda_gen_mic_autoswitch(codec, jack);
4603}
4604
Takashi Iwai963afde2013-05-31 15:20:31 +02004605/* update jack retasking */
4606static void update_automute_all(struct hda_codec *codec)
4607{
4608 call_hp_automute(codec, NULL);
4609 call_line_automute(codec, NULL);
4610 call_mic_autoswitch(codec, NULL);
4611}
4612
Takashi Iwai352f7f92012-12-19 12:52:06 +01004613/*
4614 * Auto-Mute mode mixer enum support
4615 */
4616static int automute_mode_info(struct snd_kcontrol *kcontrol,
4617 struct snd_ctl_elem_info *uinfo)
4618{
4619 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4620 struct hda_gen_spec *spec = codec->spec;
4621 static const char * const texts3[] = {
4622 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004623 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
Takashi Iwai352f7f92012-12-19 12:52:06 +01004625 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4626 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4627 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4628}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
Takashi Iwai352f7f92012-12-19 12:52:06 +01004630static int automute_mode_get(struct snd_kcontrol *kcontrol,
4631 struct snd_ctl_elem_value *ucontrol)
4632{
4633 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4634 struct hda_gen_spec *spec = codec->spec;
4635 unsigned int val = 0;
4636 if (spec->automute_speaker)
4637 val++;
4638 if (spec->automute_lo)
4639 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004640
Takashi Iwai352f7f92012-12-19 12:52:06 +01004641 ucontrol->value.enumerated.item[0] = val;
4642 return 0;
4643}
4644
4645static int automute_mode_put(struct snd_kcontrol *kcontrol,
4646 struct snd_ctl_elem_value *ucontrol)
4647{
4648 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4649 struct hda_gen_spec *spec = codec->spec;
4650
4651 switch (ucontrol->value.enumerated.item[0]) {
4652 case 0:
4653 if (!spec->automute_speaker && !spec->automute_lo)
4654 return 0;
4655 spec->automute_speaker = 0;
4656 spec->automute_lo = 0;
4657 break;
4658 case 1:
4659 if (spec->automute_speaker_possible) {
4660 if (!spec->automute_lo && spec->automute_speaker)
4661 return 0;
4662 spec->automute_speaker = 1;
4663 spec->automute_lo = 0;
4664 } else if (spec->automute_lo_possible) {
4665 if (spec->automute_lo)
4666 return 0;
4667 spec->automute_lo = 1;
4668 } else
4669 return -EINVAL;
4670 break;
4671 case 2:
4672 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4673 return -EINVAL;
4674 if (spec->automute_speaker && spec->automute_lo)
4675 return 0;
4676 spec->automute_speaker = 1;
4677 spec->automute_lo = 1;
4678 break;
4679 default:
4680 return -EINVAL;
4681 }
4682 call_update_outputs(codec);
4683 return 1;
4684}
4685
4686static const struct snd_kcontrol_new automute_mode_enum = {
4687 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4688 .name = "Auto-Mute Mode",
4689 .info = automute_mode_info,
4690 .get = automute_mode_get,
4691 .put = automute_mode_put,
4692};
4693
4694static int add_automute_mode_enum(struct hda_codec *codec)
4695{
4696 struct hda_gen_spec *spec = codec->spec;
4697
Takashi Iwai12c93df2012-12-19 14:38:33 +01004698 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004699 return -ENOMEM;
4700 return 0;
4701}
4702
4703/*
4704 * Check the availability of HP/line-out auto-mute;
4705 * Set up appropriately if really supported
4706 */
4707static int check_auto_mute_availability(struct hda_codec *codec)
4708{
4709 struct hda_gen_spec *spec = codec->spec;
4710 struct auto_pin_cfg *cfg = &spec->autocfg;
4711 int present = 0;
4712 int i, err;
4713
Takashi Iwaif72706b2013-01-16 18:20:07 +01004714 if (spec->suppress_auto_mute)
4715 return 0;
4716
Takashi Iwai352f7f92012-12-19 12:52:06 +01004717 if (cfg->hp_pins[0])
4718 present++;
4719 if (cfg->line_out_pins[0])
4720 present++;
4721 if (cfg->speaker_pins[0])
4722 present++;
4723 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004724 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004725
4726 if (!cfg->speaker_pins[0] &&
4727 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4728 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4729 sizeof(cfg->speaker_pins));
4730 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732
Takashi Iwai352f7f92012-12-19 12:52:06 +01004733 if (!cfg->hp_pins[0] &&
4734 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4735 memcpy(cfg->hp_pins, cfg->line_out_pins,
4736 sizeof(cfg->hp_pins));
4737 cfg->hp_outs = cfg->line_outs;
4738 }
4739
4740 for (i = 0; i < cfg->hp_outs; i++) {
4741 hda_nid_t nid = cfg->hp_pins[i];
4742 if (!is_jack_detectable(codec, nid))
4743 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004744 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai62f949b2014-09-11 14:06:53 +02004745 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004746 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004747 spec->detect_hp = 1;
4748 }
4749
4750 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4751 if (cfg->speaker_outs)
4752 for (i = 0; i < cfg->line_outs; i++) {
4753 hda_nid_t nid = cfg->line_out_pins[i];
4754 if (!is_jack_detectable(codec, nid))
4755 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004756 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004757 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004758 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004759 spec->detect_lo = 1;
4760 }
4761 spec->automute_lo_possible = spec->detect_hp;
4762 }
4763
4764 spec->automute_speaker_possible = cfg->speaker_outs &&
4765 (spec->detect_hp || spec->detect_lo);
4766
4767 spec->automute_lo = spec->automute_lo_possible;
4768 spec->automute_speaker = spec->automute_speaker_possible;
4769
4770 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4771 /* create a control for automute mode */
4772 err = add_automute_mode_enum(codec);
4773 if (err < 0)
4774 return err;
4775 }
4776 return 0;
4777}
4778
Takashi Iwai352f7f92012-12-19 12:52:06 +01004779/* check whether all auto-mic pins are valid; setup indices if OK */
4780static bool auto_mic_check_imux(struct hda_codec *codec)
4781{
4782 struct hda_gen_spec *spec = codec->spec;
4783 const struct hda_input_mux *imux;
4784 int i;
4785
4786 imux = &spec->input_mux;
4787 for (i = 0; i < spec->am_num_entries; i++) {
4788 spec->am_entry[i].idx =
4789 find_idx_in_nid_list(spec->am_entry[i].pin,
4790 spec->imux_pins, imux->num_items);
4791 if (spec->am_entry[i].idx < 0)
4792 return false; /* no corresponding imux */
4793 }
4794
4795 /* we don't need the jack detection for the first pin */
4796 for (i = 1; i < spec->am_num_entries; i++)
4797 snd_hda_jack_detect_enable_callback(codec,
4798 spec->am_entry[i].pin,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004799 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004800 return true;
4801}
4802
4803static int compare_attr(const void *ap, const void *bp)
4804{
4805 const struct automic_entry *a = ap;
4806 const struct automic_entry *b = bp;
4807 return (int)(a->attr - b->attr);
4808}
4809
4810/*
4811 * Check the availability of auto-mic switch;
4812 * Set up if really supported
4813 */
4814static int check_auto_mic_availability(struct hda_codec *codec)
4815{
4816 struct hda_gen_spec *spec = codec->spec;
4817 struct auto_pin_cfg *cfg = &spec->autocfg;
4818 unsigned int types;
4819 int i, num_pins;
4820
Takashi Iwaid12daf62013-01-07 16:32:11 +01004821 if (spec->suppress_auto_mic)
4822 return 0;
4823
Takashi Iwai352f7f92012-12-19 12:52:06 +01004824 types = 0;
4825 num_pins = 0;
4826 for (i = 0; i < cfg->num_inputs; i++) {
4827 hda_nid_t nid = cfg->inputs[i].pin;
4828 unsigned int attr;
4829 attr = snd_hda_codec_get_pincfg(codec, nid);
4830 attr = snd_hda_get_input_pin_attr(attr);
4831 if (types & (1 << attr))
4832 return 0; /* already occupied */
4833 switch (attr) {
4834 case INPUT_PIN_ATTR_INT:
4835 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4836 return 0; /* invalid type */
4837 break;
4838 case INPUT_PIN_ATTR_UNUSED:
4839 return 0; /* invalid entry */
4840 default:
4841 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4842 return 0; /* invalid type */
4843 if (!spec->line_in_auto_switch &&
4844 cfg->inputs[i].type != AUTO_PIN_MIC)
4845 return 0; /* only mic is allowed */
4846 if (!is_jack_detectable(codec, nid))
4847 return 0; /* no unsol support */
4848 break;
4849 }
4850 if (num_pins >= MAX_AUTO_MIC_PINS)
4851 return 0;
4852 types |= (1 << attr);
4853 spec->am_entry[num_pins].pin = nid;
4854 spec->am_entry[num_pins].attr = attr;
4855 num_pins++;
4856 }
4857
4858 if (num_pins < 2)
4859 return 0;
4860
4861 spec->am_num_entries = num_pins;
4862 /* sort the am_entry in the order of attr so that the pin with a
4863 * higher attr will be selected when the jack is plugged.
4864 */
4865 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4866 compare_attr, NULL);
4867
4868 if (!auto_mic_check_imux(codec))
4869 return 0;
4870
4871 spec->auto_mic = 1;
4872 spec->num_adc_nids = 1;
4873 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004874 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004875 spec->am_entry[0].pin,
4876 spec->am_entry[1].pin,
4877 spec->am_entry[2].pin);
4878
4879 return 0;
4880}
4881
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004882/**
4883 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4884 * into power down
4885 * @codec: the HDA codec
4886 * @nid: NID to evalute
4887 * @power_state: target power state
4888 */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004889unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004890 hda_nid_t nid,
4891 unsigned int power_state)
4892{
Takashi Iwaib6c09b32015-04-09 10:25:03 +02004893 struct hda_gen_spec *spec = codec->spec;
4894
4895 if (!spec->power_down_unused && !codec->power_save_node)
4896 return power_state;
Takashi Iwai7639a062015-03-03 10:07:24 +01004897 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004898 return power_state;
4899 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4900 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004901 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004902 return power_state;
4903 return AC_PWRST_D3;
4904}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004905EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004906
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004907/* mute all aamix inputs initially; parse up to the first leaves */
4908static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4909{
4910 int i, nums;
4911 const hda_nid_t *conn;
4912 bool has_amp;
4913
4914 nums = snd_hda_get_conn_list(codec, mix, &conn);
4915 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4916 for (i = 0; i < nums; i++) {
4917 if (has_amp)
Takashi Iwaief403ed2015-03-12 08:30:11 +01004918 update_amp(codec, mix, HDA_INPUT, i,
4919 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004920 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
Takashi Iwaief403ed2015-03-12 08:30:11 +01004921 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4922 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004923 }
4924}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004925
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004926/**
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004927 * snd_hda_gen_stream_pm - Stream power management callback
4928 * @codec: the HDA codec
4929 * @nid: audio widget
4930 * @on: power on/off flag
4931 *
Takashi Iwai967b1302015-03-20 18:21:03 +01004932 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004933 */
4934void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4935{
Takashi Iwai967b1302015-03-20 18:21:03 +01004936 if (codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004937 set_path_power(codec, nid, -1, on);
4938}
4939EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4940
4941/**
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004942 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4943 * set up the hda_gen_spec
4944 * @codec: the HDA codec
4945 * @cfg: Parsed pin configuration
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004946 *
4947 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004948 * or a negative error code
4949 */
4950int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004951 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004952{
4953 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004954 int err;
4955
Takashi Iwai1c70a582013-01-11 17:48:22 +01004956 parse_user_hints(codec);
4957
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01004958 if (spec->vmaster_mute_led || spec->mic_mute_led)
4959 snd_ctl_led_request();
4960
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004961 if (spec->mixer_nid && !spec->mixer_merge_nid)
4962 spec->mixer_merge_nid = spec->mixer_nid;
4963
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004964 if (cfg != &spec->autocfg) {
4965 spec->autocfg = *cfg;
4966 cfg = &spec->autocfg;
4967 }
4968
Takashi Iwai98bd1112013-03-22 14:53:50 +01004969 if (!spec->main_out_badness)
4970 spec->main_out_badness = &hda_main_out_badness;
4971 if (!spec->extra_out_badness)
4972 spec->extra_out_badness = &hda_extra_out_badness;
4973
David Henningsson6fc4cb92013-01-16 15:58:45 +01004974 fill_all_dac_nids(codec);
4975
Takashi Iwai352f7f92012-12-19 12:52:06 +01004976 if (!cfg->line_outs) {
4977 if (cfg->dig_outs || cfg->dig_in_pin) {
4978 spec->multiout.max_channels = 2;
4979 spec->no_analog = 1;
4980 goto dig_only;
4981 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004982 if (!cfg->num_inputs && !cfg->dig_in_pin)
4983 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004984 }
4985
4986 if (!spec->no_primary_hp &&
4987 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4988 cfg->line_outs <= cfg->hp_outs) {
4989 /* use HP as primary out */
4990 cfg->speaker_outs = cfg->line_outs;
4991 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4992 sizeof(cfg->speaker_pins));
4993 cfg->line_outs = cfg->hp_outs;
4994 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4995 cfg->hp_outs = 0;
4996 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4997 cfg->line_out_type = AUTO_PIN_HP_OUT;
4998 }
4999
5000 err = parse_output_paths(codec);
5001 if (err < 0)
5002 return err;
5003 err = create_multi_channel_mode(codec);
5004 if (err < 0)
5005 return err;
5006 err = create_multi_out_ctls(codec, cfg);
5007 if (err < 0)
5008 return err;
5009 err = create_hp_out_ctls(codec);
5010 if (err < 0)
5011 return err;
5012 err = create_speaker_out_ctls(codec);
5013 if (err < 0)
5014 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005015 err = create_indep_hp_ctls(codec);
5016 if (err < 0)
5017 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01005018 err = create_loopback_mixing_ctl(codec);
5019 if (err < 0)
5020 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01005021 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005022 if (err < 0)
5023 return err;
5024 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02005025 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02005026 return err;
5027
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01005028 /* add power-down pin callbacks at first */
5029 add_all_pin_power_ctls(codec, false);
5030
Takashi Iwaia07a9492013-01-07 16:44:06 +01005031 spec->const_channel_count = spec->ext_channel_count;
5032 /* check the multiple speaker and headphone pins */
5033 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
5034 spec->const_channel_count = max(spec->const_channel_count,
5035 cfg->speaker_outs * 2);
5036 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
5037 spec->const_channel_count = max(spec->const_channel_count,
5038 cfg->hp_outs * 2);
5039 spec->multiout.max_channels = max(spec->ext_channel_count,
5040 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005041
5042 err = check_auto_mute_availability(codec);
5043 if (err < 0)
5044 return err;
5045
5046 err = check_dyn_adc_switch(codec);
5047 if (err < 0)
5048 return err;
5049
Takashi Iwai967303d2013-02-19 17:12:42 +01005050 err = check_auto_mic_availability(codec);
5051 if (err < 0)
5052 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02005053
Takashi Iwaif1e762d2013-12-09 16:02:24 +01005054 /* add stereo mix if available and not enabled yet */
5055 if (!spec->auto_mic && spec->mixer_nid &&
Takashi Iwai74f14b32014-12-15 13:43:59 +01005056 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
5057 spec->input_mux.num_items > 1) {
Takashi Iwaif1e762d2013-12-09 16:02:24 +01005058 err = parse_capture_source(codec, spec->mixer_nid,
5059 CFG_IDX_MIX, spec->num_all_adcs,
5060 "Stereo Mix", 0);
5061 if (err < 0)
5062 return err;
5063 }
5064
5065
Takashi Iwai352f7f92012-12-19 12:52:06 +01005066 err = create_capture_mixers(codec);
5067 if (err < 0)
5068 return err;
5069
5070 err = parse_mic_boost(codec);
5071 if (err < 0)
5072 return err;
5073
Takashi Iwaiced4cef2013-11-26 08:33:45 +01005074 /* create "Headphone Mic Jack Mode" if no input selection is
5075 * available (or user specifies add_jack_modes hint)
5076 */
5077 if (spec->hp_mic_pin &&
5078 (spec->auto_mic || spec->input_mux.num_items == 1 ||
5079 spec->add_jack_modes)) {
5080 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
5081 if (err < 0)
5082 return err;
5083 }
5084
Takashi Iwaif811c3c2013-03-07 18:32:59 +01005085 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01005086 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
5087 err = create_out_jack_modes(codec, cfg->line_outs,
5088 cfg->line_out_pins);
5089 if (err < 0)
5090 return err;
5091 }
5092 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
5093 err = create_out_jack_modes(codec, cfg->hp_outs,
5094 cfg->hp_pins);
5095 if (err < 0)
5096 return err;
5097 }
5098 }
5099
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01005100 /* add power-up pin callbacks at last */
5101 add_all_pin_power_ctls(codec, true);
5102
Takashi Iwaiebb93c02013-12-10 17:33:49 +01005103 /* mute all aamix input initially */
5104 if (spec->mixer_nid)
5105 mute_all_mixer_nid(codec, spec->mixer_nid);
5106
Takashi Iwai352f7f92012-12-19 12:52:06 +01005107 dig_only:
5108 parse_digital(codec);
5109
Takashi Iwai49fb1892015-05-27 08:37:19 +02005110 if (spec->power_down_unused || codec->power_save_node) {
Takashi Iwai24fef902015-04-09 10:28:15 +02005111 if (!codec->power_filter)
5112 codec->power_filter = snd_hda_gen_path_power_filter;
Takashi Iwai49fb1892015-05-27 08:37:19 +02005113 if (!codec->patch_ops.stream_pm)
5114 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
5115 }
Takashi Iwai55196ff2013-01-24 17:32:56 +01005116
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005117 if (!spec->no_analog && spec->beep_nid) {
5118 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
5119 if (err < 0)
5120 return err;
Takashi Iwai967b1302015-03-20 18:21:03 +01005121 if (codec->beep && codec->power_save_node) {
Takashi Iwai5ccf8352015-03-18 09:23:10 +01005122 err = add_fake_beep_paths(codec);
5123 if (err < 0)
5124 return err;
5125 codec->beep->power_hook = beep_power_hook;
5126 }
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005127 }
5128
Takashi Iwai352f7f92012-12-19 12:52:06 +01005129 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005131EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132
5133
5134/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01005135 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005137
Takashi Iwai9ab0cb32020-07-17 17:45:17 +02005138/* follower controls for virtual master */
5139static const char * const follower_pfxs[] = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01005140 "Front", "Surround", "Center", "LFE", "Side",
5141 "Headphone", "Speaker", "Mono", "Line Out",
5142 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01005143 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
5144 "Headphone Front", "Headphone Surround", "Headphone CLFE",
David Henningsson03ad6a82014-10-16 15:33:45 +02005145 "Headphone Side", "Headphone+LO", "Speaker+LO",
Takashi Iwai352f7f92012-12-19 12:52:06 +01005146 NULL,
5147};
5148
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005149/**
5150 * snd_hda_gen_build_controls - Build controls from the parsed results
5151 * @codec: the HDA codec
5152 *
5153 * Pass this to build_controls patch_ops.
5154 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005155int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005157 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159
Takashi Iwai36502d02012-12-19 15:15:10 +01005160 if (spec->kctls.used) {
5161 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5162 if (err < 0)
5163 return err;
5164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
Takashi Iwai352f7f92012-12-19 12:52:06 +01005166 if (spec->multiout.dig_out_nid) {
5167 err = snd_hda_create_dig_out_ctls(codec,
5168 spec->multiout.dig_out_nid,
5169 spec->multiout.dig_out_nid,
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005170 spec->pcm_rec[1]->pcm_type);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005171 if (err < 0)
5172 return err;
5173 if (!spec->no_analog) {
5174 err = snd_hda_create_spdif_share_sw(codec,
5175 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 if (err < 0)
5177 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005178 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 }
5180 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005181 if (spec->dig_in_nid) {
5182 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5183 if (err < 0)
5184 return err;
5185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186
Takashi Iwai352f7f92012-12-19 12:52:06 +01005187 /* if we have no master control, let's create it */
Takashi Iwai74803162017-04-10 17:37:34 +02005188 if (!spec->no_analog && !spec->suppress_vmaster &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01005189 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01005190 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai9ab0cb32020-07-17 17:45:17 +02005191 spec->vmaster_tlv, follower_pfxs,
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01005192 "Playback Volume", 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005193 if (err < 0)
5194 return err;
5195 }
Takashi Iwai74803162017-04-10 17:37:34 +02005196 if (!spec->no_analog && !spec->suppress_vmaster &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01005197 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5198 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
Takashi Iwai9ab0cb32020-07-17 17:45:17 +02005199 NULL, follower_pfxs,
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01005200 "Playback Switch", true,
5201 spec->vmaster_mute_led ?
5202 SNDRV_CTL_ELEM_ACCESS_SPK_LED : 0,
5203 &spec->vmaster_mute.sw_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005204 if (err < 0)
5205 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02005206 if (spec->vmaster_mute.hook) {
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01005207 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute);
Takashi Iwaib63eae02013-10-25 23:43:10 +02005208 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5209 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211
Takashi Iwai352f7f92012-12-19 12:52:06 +01005212 free_kctls(spec); /* no longer needed */
5213
Takashi Iwai352f7f92012-12-19 12:52:06 +01005214 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5215 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 return err;
5217
5218 return 0;
5219}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005220EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005221
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222
5223/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01005224 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005227static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5228 struct hda_codec *codec,
5229 struct snd_pcm_substream *substream,
5230 int action)
5231{
5232 struct hda_gen_spec *spec = codec->spec;
5233 if (spec->pcm_playback_hook)
5234 spec->pcm_playback_hook(hinfo, codec, substream, action);
5235}
5236
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005237static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5238 struct hda_codec *codec,
5239 struct snd_pcm_substream *substream,
5240 int action)
5241{
5242 struct hda_gen_spec *spec = codec->spec;
5243 if (spec->pcm_capture_hook)
5244 spec->pcm_capture_hook(hinfo, codec, substream, action);
5245}
5246
Takashi Iwai352f7f92012-12-19 12:52:06 +01005247/*
5248 * Analog playback callbacks
5249 */
5250static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5251 struct hda_codec *codec,
5252 struct snd_pcm_substream *substream)
5253{
5254 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005255 int err;
5256
5257 mutex_lock(&spec->pcm_mutex);
5258 err = snd_hda_multi_out_analog_open(codec,
5259 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005260 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005261 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005262 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005263 call_pcm_playback_hook(hinfo, codec, substream,
5264 HDA_GEN_PCM_ACT_OPEN);
5265 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005266 mutex_unlock(&spec->pcm_mutex);
5267 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005268}
5269
5270static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01005271 struct hda_codec *codec,
5272 unsigned int stream_tag,
5273 unsigned int format,
5274 struct snd_pcm_substream *substream)
5275{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005276 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005277 int err;
5278
5279 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5280 stream_tag, format, substream);
5281 if (!err)
5282 call_pcm_playback_hook(hinfo, codec, substream,
5283 HDA_GEN_PCM_ACT_PREPARE);
5284 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005285}
Takashi Iwai97ec5582006-03-21 11:29:07 +01005286
Takashi Iwai352f7f92012-12-19 12:52:06 +01005287static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5288 struct hda_codec *codec,
5289 struct snd_pcm_substream *substream)
5290{
5291 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005292 int err;
5293
5294 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5295 if (!err)
5296 call_pcm_playback_hook(hinfo, codec, substream,
5297 HDA_GEN_PCM_ACT_CLEANUP);
5298 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005299}
5300
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005301static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5302 struct hda_codec *codec,
5303 struct snd_pcm_substream *substream)
5304{
5305 struct hda_gen_spec *spec = codec->spec;
5306 mutex_lock(&spec->pcm_mutex);
5307 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005308 call_pcm_playback_hook(hinfo, codec, substream,
5309 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005310 mutex_unlock(&spec->pcm_mutex);
5311 return 0;
5312}
5313
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005314static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5315 struct hda_codec *codec,
5316 struct snd_pcm_substream *substream)
5317{
5318 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5319 return 0;
5320}
5321
5322static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5323 struct hda_codec *codec,
5324 unsigned int stream_tag,
5325 unsigned int format,
5326 struct snd_pcm_substream *substream)
5327{
5328 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5329 call_pcm_capture_hook(hinfo, codec, substream,
5330 HDA_GEN_PCM_ACT_PREPARE);
5331 return 0;
5332}
5333
5334static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5335 struct hda_codec *codec,
5336 struct snd_pcm_substream *substream)
5337{
5338 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5339 call_pcm_capture_hook(hinfo, codec, substream,
5340 HDA_GEN_PCM_ACT_CLEANUP);
5341 return 0;
5342}
5343
5344static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5345 struct hda_codec *codec,
5346 struct snd_pcm_substream *substream)
5347{
5348 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5349 return 0;
5350}
5351
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005352static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5353 struct hda_codec *codec,
5354 struct snd_pcm_substream *substream)
5355{
5356 struct hda_gen_spec *spec = codec->spec;
5357 int err = 0;
5358
5359 mutex_lock(&spec->pcm_mutex);
Takashi Iwaid1f15e02015-07-08 09:22:10 +02005360 if (spec->indep_hp && !spec->indep_hp_enabled)
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005361 err = -EBUSY;
5362 else
5363 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005364 call_pcm_playback_hook(hinfo, codec, substream,
5365 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005366 mutex_unlock(&spec->pcm_mutex);
5367 return err;
5368}
5369
5370static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5371 struct hda_codec *codec,
5372 struct snd_pcm_substream *substream)
5373{
5374 struct hda_gen_spec *spec = codec->spec;
5375 mutex_lock(&spec->pcm_mutex);
5376 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005377 call_pcm_playback_hook(hinfo, codec, substream,
5378 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005379 mutex_unlock(&spec->pcm_mutex);
5380 return 0;
5381}
5382
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005383static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5384 struct hda_codec *codec,
5385 unsigned int stream_tag,
5386 unsigned int format,
5387 struct snd_pcm_substream *substream)
5388{
5389 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5390 call_pcm_playback_hook(hinfo, codec, substream,
5391 HDA_GEN_PCM_ACT_PREPARE);
5392 return 0;
5393}
5394
5395static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5396 struct hda_codec *codec,
5397 struct snd_pcm_substream *substream)
5398{
5399 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5400 call_pcm_playback_hook(hinfo, codec, substream,
5401 HDA_GEN_PCM_ACT_CLEANUP);
5402 return 0;
5403}
5404
Takashi Iwai352f7f92012-12-19 12:52:06 +01005405/*
5406 * Digital out
5407 */
5408static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5409 struct hda_codec *codec,
5410 struct snd_pcm_substream *substream)
5411{
5412 struct hda_gen_spec *spec = codec->spec;
5413 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5414}
5415
5416static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5417 struct hda_codec *codec,
5418 unsigned int stream_tag,
5419 unsigned int format,
5420 struct snd_pcm_substream *substream)
5421{
5422 struct hda_gen_spec *spec = codec->spec;
5423 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5424 stream_tag, format, substream);
5425}
5426
5427static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5428 struct hda_codec *codec,
5429 struct snd_pcm_substream *substream)
5430{
5431 struct hda_gen_spec *spec = codec->spec;
5432 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5433}
5434
5435static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5436 struct hda_codec *codec,
5437 struct snd_pcm_substream *substream)
5438{
5439 struct hda_gen_spec *spec = codec->spec;
5440 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5441}
5442
5443/*
5444 * Analog capture
5445 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005446#define alt_capture_pcm_open capture_pcm_open
5447#define alt_capture_pcm_close capture_pcm_close
5448
Takashi Iwai352f7f92012-12-19 12:52:06 +01005449static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5450 struct hda_codec *codec,
5451 unsigned int stream_tag,
5452 unsigned int format,
5453 struct snd_pcm_substream *substream)
5454{
5455 struct hda_gen_spec *spec = codec->spec;
5456
5457 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01005458 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005459 call_pcm_capture_hook(hinfo, codec, substream,
5460 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005461 return 0;
5462}
5463
Takashi Iwai352f7f92012-12-19 12:52:06 +01005464static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5465 struct hda_codec *codec,
5466 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01005467{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005468 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01005469
Takashi Iwai352f7f92012-12-19 12:52:06 +01005470 snd_hda_codec_cleanup_stream(codec,
5471 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005472 call_pcm_capture_hook(hinfo, codec, substream,
5473 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005474 return 0;
5475}
5476
Takashi Iwai352f7f92012-12-19 12:52:06 +01005477/*
5478 */
5479static const struct hda_pcm_stream pcm_analog_playback = {
5480 .substreams = 1,
5481 .channels_min = 2,
5482 .channels_max = 8,
5483 /* NID is set in build_pcms */
5484 .ops = {
5485 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005486 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005487 .prepare = playback_pcm_prepare,
5488 .cleanup = playback_pcm_cleanup
5489 },
5490};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491
Takashi Iwai352f7f92012-12-19 12:52:06 +01005492static const struct hda_pcm_stream pcm_analog_capture = {
5493 .substreams = 1,
5494 .channels_min = 2,
5495 .channels_max = 2,
5496 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005497 .ops = {
5498 .open = capture_pcm_open,
5499 .close = capture_pcm_close,
5500 .prepare = capture_pcm_prepare,
5501 .cleanup = capture_pcm_cleanup
5502 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005503};
5504
5505static const struct hda_pcm_stream pcm_analog_alt_playback = {
5506 .substreams = 1,
5507 .channels_min = 2,
5508 .channels_max = 2,
5509 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005510 .ops = {
5511 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005512 .close = alt_playback_pcm_close,
5513 .prepare = alt_playback_pcm_prepare,
5514 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005515 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005516};
5517
5518static const struct hda_pcm_stream pcm_analog_alt_capture = {
5519 .substreams = 2, /* can be overridden */
5520 .channels_min = 2,
5521 .channels_max = 2,
5522 /* NID is set in build_pcms */
5523 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005524 .open = alt_capture_pcm_open,
5525 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005526 .prepare = alt_capture_pcm_prepare,
5527 .cleanup = alt_capture_pcm_cleanup
5528 },
5529};
5530
5531static const struct hda_pcm_stream pcm_digital_playback = {
5532 .substreams = 1,
5533 .channels_min = 2,
5534 .channels_max = 2,
5535 /* NID is set in build_pcms */
5536 .ops = {
5537 .open = dig_playback_pcm_open,
5538 .close = dig_playback_pcm_close,
5539 .prepare = dig_playback_pcm_prepare,
5540 .cleanup = dig_playback_pcm_cleanup
5541 },
5542};
5543
5544static const struct hda_pcm_stream pcm_digital_capture = {
5545 .substreams = 1,
5546 .channels_min = 2,
5547 .channels_max = 2,
5548 /* NID is set in build_pcms */
5549};
5550
5551/* Used by build_pcms to flag that a PCM has no playback stream */
5552static const struct hda_pcm_stream pcm_null_stream = {
5553 .substreams = 0,
5554 .channels_min = 0,
5555 .channels_max = 0,
5556};
5557
5558/*
5559 * dynamic changing ADC PCM streams
5560 */
5561static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5562{
5563 struct hda_gen_spec *spec = codec->spec;
5564 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5565
5566 if (spec->cur_adc && spec->cur_adc != new_adc) {
5567 /* stream is running, let's swap the current ADC */
5568 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5569 spec->cur_adc = new_adc;
5570 snd_hda_codec_setup_stream(codec, new_adc,
5571 spec->cur_adc_stream_tag, 0,
5572 spec->cur_adc_format);
5573 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005575 return false;
5576}
5577
5578/* analog capture with dynamic dual-adc changes */
5579static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5580 struct hda_codec *codec,
5581 unsigned int stream_tag,
5582 unsigned int format,
5583 struct snd_pcm_substream *substream)
5584{
5585 struct hda_gen_spec *spec = codec->spec;
5586 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5587 spec->cur_adc_stream_tag = stream_tag;
5588 spec->cur_adc_format = format;
5589 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
Takashi Iwai4f29efc2016-04-12 15:16:24 +02005590 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005591 return 0;
5592}
5593
5594static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5595 struct hda_codec *codec,
5596 struct snd_pcm_substream *substream)
5597{
5598 struct hda_gen_spec *spec = codec->spec;
5599 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5600 spec->cur_adc = 0;
Takashi Iwai4f29efc2016-04-12 15:16:24 +02005601 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005602 return 0;
5603}
5604
5605static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5606 .substreams = 1,
5607 .channels_min = 2,
5608 .channels_max = 2,
5609 .nid = 0, /* fill later */
5610 .ops = {
5611 .prepare = dyn_adc_capture_pcm_prepare,
5612 .cleanup = dyn_adc_capture_pcm_cleanup
5613 },
5614};
5615
Takashi Iwaif873e532012-12-20 16:58:39 +01005616static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5617 const char *chip_name)
5618{
5619 char *p;
5620
5621 if (*str)
5622 return;
Joe Perches75b1a8f2021-01-04 09:17:34 -08005623 strscpy(str, chip_name, len);
Takashi Iwaif873e532012-12-20 16:58:39 +01005624
5625 /* drop non-alnum chars after a space */
5626 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5627 if (!isalnum(p[1])) {
5628 *p = 0;
5629 break;
5630 }
5631 }
5632 strlcat(str, sfx, len);
5633}
5634
Takashi Iwaifb83b632015-03-16 23:34:34 +01005635/* copy PCM stream info from @default_str, and override non-NULL entries
5636 * from @spec_str and @nid
5637 */
5638static void setup_pcm_stream(struct hda_pcm_stream *str,
5639 const struct hda_pcm_stream *default_str,
5640 const struct hda_pcm_stream *spec_str,
5641 hda_nid_t nid)
5642{
5643 *str = *default_str;
5644 if (nid)
5645 str->nid = nid;
5646 if (spec_str) {
5647 if (spec_str->substreams)
5648 str->substreams = spec_str->substreams;
5649 if (spec_str->channels_min)
5650 str->channels_min = spec_str->channels_min;
5651 if (spec_str->channels_max)
5652 str->channels_max = spec_str->channels_max;
5653 if (spec_str->rates)
5654 str->rates = spec_str->rates;
5655 if (spec_str->formats)
5656 str->formats = spec_str->formats;
5657 if (spec_str->maxbps)
5658 str->maxbps = spec_str->maxbps;
5659 }
5660}
5661
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005662/**
5663 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5664 * @codec: the HDA codec
5665 *
5666 * Pass this to build_pcms patch_ops.
5667 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005668int snd_hda_gen_build_pcms(struct hda_codec *codec)
5669{
5670 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005671 struct hda_pcm *info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005672 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673
Takashi Iwai352f7f92012-12-19 12:52:06 +01005674 if (spec->no_analog)
5675 goto skip_analog;
5676
Takashi Iwaif873e532012-12-20 16:58:39 +01005677 fill_pcm_stream_name(spec->stream_name_analog,
5678 sizeof(spec->stream_name_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005679 " Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005680 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5681 if (!info)
5682 return -ENOMEM;
5683 spec->pcm_rec[0] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005684
5685 if (spec->multiout.num_dacs > 0) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005686 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5687 &pcm_analog_playback,
5688 spec->stream_analog_playback,
5689 spec->multiout.dac_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005690 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5691 spec->multiout.max_channels;
5692 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5693 spec->autocfg.line_outs == 2)
5694 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5695 snd_pcm_2_1_chmaps;
5696 }
5697 if (spec->num_adc_nids) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005698 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5699 (spec->dyn_adc_switch ?
5700 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5701 spec->stream_analog_capture,
5702 spec->adc_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005703 }
5704
Takashi Iwai352f7f92012-12-19 12:52:06 +01005705 skip_analog:
5706 /* SPDIF for stream index #1 */
5707 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005708 fill_pcm_stream_name(spec->stream_name_digital,
5709 sizeof(spec->stream_name_digital),
Takashi Iwai7639a062015-03-03 10:07:24 +01005710 " Digital", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005711 info = snd_hda_codec_pcm_new(codec, "%s",
5712 spec->stream_name_digital);
5713 if (!info)
5714 return -ENOMEM;
Takashi Iwai9ab0cb32020-07-17 17:45:17 +02005715 codec->follower_dig_outs = spec->multiout.follower_dig_outs;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005716 spec->pcm_rec[1] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005717 if (spec->dig_out_type)
5718 info->pcm_type = spec->dig_out_type;
5719 else
5720 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005721 if (spec->multiout.dig_out_nid)
5722 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5723 &pcm_digital_playback,
5724 spec->stream_digital_playback,
5725 spec->multiout.dig_out_nid);
5726 if (spec->dig_in_nid)
5727 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5728 &pcm_digital_capture,
5729 spec->stream_digital_capture,
5730 spec->dig_in_nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005731 }
5732
5733 if (spec->no_analog)
5734 return 0;
5735
5736 /* If the use of more than one ADC is requested for the current
5737 * model, configure a second analog capture-only PCM.
5738 */
5739 have_multi_adcs = (spec->num_adc_nids > 1) &&
5740 !spec->dyn_adc_switch && !spec->auto_mic;
5741 /* Additional Analaog capture for index #2 */
5742 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005743 fill_pcm_stream_name(spec->stream_name_alt_analog,
5744 sizeof(spec->stream_name_alt_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005745 " Alt Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005746 info = snd_hda_codec_pcm_new(codec, "%s",
5747 spec->stream_name_alt_analog);
5748 if (!info)
5749 return -ENOMEM;
5750 spec->pcm_rec[2] = info;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005751 if (spec->alt_dac_nid)
5752 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5753 &pcm_analog_alt_playback,
5754 spec->stream_analog_alt_playback,
5755 spec->alt_dac_nid);
5756 else
5757 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5758 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005759 if (have_multi_adcs) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005760 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5761 &pcm_analog_alt_capture,
5762 spec->stream_analog_alt_capture,
5763 spec->adc_nids[1]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005764 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5765 spec->num_adc_nids - 1;
5766 } else {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005767 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5768 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 }
5771
5772 return 0;
5773}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005774EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005775
5776
5777/*
5778 * Standard auto-parser initializations
5779 */
5780
Takashi Iwaid4156932013-01-07 10:08:02 +01005781/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005782static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005783{
5784 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005785 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005786
Takashi Iwai196c17662013-01-04 15:01:40 +01005787 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005788 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005789 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005790 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005791 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005792 snd_hda_activate_path(codec, path, path->active,
5793 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005794 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005795}
5796
5797/* initialize primary output paths */
5798static void init_multi_out(struct hda_codec *codec)
5799{
5800 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005801 int i;
5802
Takashi Iwaid4156932013-01-07 10:08:02 +01005803 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005804 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005805}
5806
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005807
Takashi Iwai2c12c302013-01-10 09:33:29 +01005808static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005809{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005810 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005811
Takashi Iwaid4156932013-01-07 10:08:02 +01005812 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005813 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005814}
5815
5816/* initialize hp and speaker paths */
5817static void init_extra_out(struct hda_codec *codec)
5818{
5819 struct hda_gen_spec *spec = codec->spec;
5820
5821 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005822 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005823 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5824 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005825 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005826}
5827
5828/* initialize multi-io paths */
5829static void init_multi_io(struct hda_codec *codec)
5830{
5831 struct hda_gen_spec *spec = codec->spec;
5832 int i;
5833
5834 for (i = 0; i < spec->multi_ios; i++) {
5835 hda_nid_t pin = spec->multi_io[i].pin;
5836 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005837 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005838 if (!path)
5839 continue;
5840 if (!spec->multi_io[i].ctl_in)
5841 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005842 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005843 snd_hda_activate_path(codec, path, path->active,
5844 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005845 }
5846}
5847
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005848static void init_aamix_paths(struct hda_codec *codec)
5849{
5850 struct hda_gen_spec *spec = codec->spec;
5851
5852 if (!spec->have_aamix_ctl)
5853 return;
Takashi Iwaie7fdd522015-12-08 17:00:42 +01005854 if (!has_aamix_out_paths(spec))
5855 return;
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005856 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5857 spec->aamix_out_paths[0],
5858 spec->autocfg.line_out_type);
5859 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5860 spec->aamix_out_paths[1],
5861 AUTO_PIN_HP_OUT);
5862 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5863 spec->aamix_out_paths[2],
5864 AUTO_PIN_SPEAKER_OUT);
5865}
5866
Takashi Iwai352f7f92012-12-19 12:52:06 +01005867/* set up input pins and loopback paths */
5868static void init_analog_input(struct hda_codec *codec)
5869{
5870 struct hda_gen_spec *spec = codec->spec;
5871 struct auto_pin_cfg *cfg = &spec->autocfg;
5872 int i;
5873
5874 for (i = 0; i < cfg->num_inputs; i++) {
5875 hda_nid_t nid = cfg->inputs[i].pin;
5876 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005877 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005878
5879 /* init loopback inputs */
5880 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005881 resume_path_from_idx(codec, spec->loopback_paths[i]);
5882 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005883 }
5884 }
5885}
5886
5887/* initialize ADC paths */
5888static void init_input_src(struct hda_codec *codec)
5889{
5890 struct hda_gen_spec *spec = codec->spec;
5891 struct hda_input_mux *imux = &spec->input_mux;
5892 struct nid_path *path;
5893 int i, c, nums;
5894
5895 if (spec->dyn_adc_switch)
5896 nums = 1;
5897 else
5898 nums = spec->num_adc_nids;
5899
5900 for (c = 0; c < nums; c++) {
5901 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005902 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005903 if (path) {
5904 bool active = path->active;
5905 if (i == spec->cur_mux[c])
5906 active = true;
5907 snd_hda_activate_path(codec, path, active, false);
5908 }
5909 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005910 if (spec->hp_mic)
5911 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005912 }
5913
Takashi Iwai352f7f92012-12-19 12:52:06 +01005914 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005915 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005916}
5917
5918/* set right pin controls for digital I/O */
5919static void init_digital(struct hda_codec *codec)
5920{
5921 struct hda_gen_spec *spec = codec->spec;
5922 int i;
5923 hda_nid_t pin;
5924
Takashi Iwaid4156932013-01-07 10:08:02 +01005925 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005926 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005927 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005928 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005929 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005930 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005931 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005932}
5933
Takashi Iwai973e4972012-12-20 15:16:09 +01005934/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5935 * invalid unsol tags by some reason
5936 */
5937static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5938{
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +02005939 const struct hda_pincfg *pin;
Takashi Iwai973e4972012-12-20 15:16:09 +01005940 int i;
5941
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +02005942 snd_array_for_each(&codec->init_pins, i, pin) {
Takashi Iwai973e4972012-12-20 15:16:09 +01005943 hda_nid_t nid = pin->nid;
5944 if (is_jack_detectable(codec, nid) &&
5945 !snd_hda_jack_tbl_get(codec, nid))
Takashi Iwai401caff2018-06-27 11:43:09 +02005946 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai973e4972012-12-20 15:16:09 +01005947 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5948 }
5949}
5950
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005951/**
5952 * snd_hda_gen_init - initialize the generic spec
5953 * @codec: the HDA codec
5954 *
5955 * This can be put as patch_ops init function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005956 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005957int snd_hda_gen_init(struct hda_codec *codec)
5958{
5959 struct hda_gen_spec *spec = codec->spec;
5960
5961 if (spec->init_hook)
5962 spec->init_hook(codec);
5963
Takashi Iwai89781d02019-08-30 12:03:38 +02005964 if (!spec->skip_verbs)
5965 snd_hda_apply_verbs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005966
5967 init_multi_out(codec);
5968 init_extra_out(codec);
5969 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005970 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005971 init_analog_input(codec);
5972 init_input_src(codec);
5973 init_digital(codec);
5974
Takashi Iwai973e4972012-12-20 15:16:09 +01005975 clear_unsol_on_unused_pins(codec);
5976
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01005977 sync_all_pin_power_ctls(codec);
5978
Takashi Iwai352f7f92012-12-19 12:52:06 +01005979 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005980 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005981
Takashi Iwai1a462be2020-01-09 10:01:04 +01005982 snd_hda_regmap_sync(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005983
Takashi Iwai352f7f92012-12-19 12:52:06 +01005984 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5985 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5986
5987 hda_call_check_power_status(codec, 0x01);
5988 return 0;
5989}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005990EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005991
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005992/**
5993 * snd_hda_gen_free - free the generic spec
5994 * @codec: the HDA codec
5995 *
5996 * This can be put as patch_ops free function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005997 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005998void snd_hda_gen_free(struct hda_codec *codec)
5999{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01006000 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01006001 snd_hda_gen_spec_free(codec->spec);
6002 kfree(codec->spec);
6003 codec->spec = NULL;
6004}
Takashi Iwai2698ea92013-12-18 07:45:52 +01006005EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01006006
6007#ifdef CONFIG_PM
Takashi Iwaidda42bd2014-10-30 12:04:50 +01006008/**
6009 * snd_hda_gen_check_power_status - check the loopback power save state
6010 * @codec: the HDA codec
6011 * @nid: NID to inspect
6012 *
6013 * This can be put as patch_ops check_power_status function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01006014 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01006015int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
6016{
6017 struct hda_gen_spec *spec = codec->spec;
6018 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
6019}
Takashi Iwai2698ea92013-12-18 07:45:52 +01006020EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01006021#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01006022
6023
6024/*
6025 * the generic codec support
6026 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027
Takashi Iwai352f7f92012-12-19 12:52:06 +01006028static const struct hda_codec_ops generic_patch_ops = {
6029 .build_controls = snd_hda_gen_build_controls,
6030 .build_pcms = snd_hda_gen_build_pcms,
6031 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01006032 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01006033 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02006034#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01006035 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02006036#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037};
6038
Takashi Iwaid8a766a2015-02-17 15:25:37 +01006039/*
Takashi Iwaidda42bd2014-10-30 12:04:50 +01006040 * snd_hda_parse_generic_codec - Generic codec parser
6041 * @codec: the HDA codec
Takashi Iwaidda42bd2014-10-30 12:04:50 +01006042 */
Takashi Iwaid8a766a2015-02-17 15:25:37 +01006043static int snd_hda_parse_generic_codec(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044{
Takashi Iwai352f7f92012-12-19 12:52:06 +01006045 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 int err;
6047
Takashi Iwaie560d8d2005-09-09 14:21:46 +02006048 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01006049 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01006051 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053
Takashi Iwai9eb413e2012-12-19 14:41:21 +01006054 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
6055 if (err < 0)
Wenwen Wangcfef67f2019-08-09 23:29:48 -05006056 goto error;
Takashi Iwai9eb413e2012-12-19 14:41:21 +01006057
6058 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01006059 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 goto error;
6061
6062 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 return 0;
6064
Takashi Iwai352f7f92012-12-19 12:52:06 +01006065error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01006066 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 return err;
6068}
Takashi Iwaid8a766a2015-02-17 15:25:37 +01006069
Takashi Iwaib9a94a92015-10-01 16:20:04 +02006070static const struct hda_device_id snd_hda_id_generic[] = {
6071 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01006072 {} /* terminator */
6073};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02006074MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01006075
6076static struct hda_codec_driver generic_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02006077 .id = snd_hda_id_generic,
Takashi Iwaid8a766a2015-02-17 15:25:37 +01006078};
6079
6080module_hda_codec_driver(generic_driver);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01006081
6082MODULE_LICENSE("GPL");
6083MODULE_DESCRIPTION("Generic HD-audio codec parser");