blob: fc114e5224806f44cfffd3797997c17fc0a9087b [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);
Takashi Iwai549f8ff2022-01-26 15:50:11 +010094#ifdef CONFIG_SND_HDA_GENERIC_LEDS
95 if (spec->led_cdevs[LED_AUDIO_MUTE])
96 led_classdev_unregister(spec->led_cdevs[LED_AUDIO_MUTE]);
97 if (spec->led_cdevs[LED_AUDIO_MICMUTE])
98 led_classdev_unregister(spec->led_cdevs[LED_AUDIO_MICMUTE]);
99#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/*
Takashi Iwai1c70a582013-01-11 17:48:22 +0100103 * store user hints
104 */
105static void parse_user_hints(struct hda_codec *codec)
106{
107 struct hda_gen_spec *spec = codec->spec;
108 int val;
109
110 val = snd_hda_get_bool_hint(codec, "jack_detect");
111 if (val >= 0)
112 codec->no_jack_detect = !val;
113 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
114 if (val >= 0)
115 codec->inv_jack_detect = !!val;
116 val = snd_hda_get_bool_hint(codec, "trigger_sense");
117 if (val >= 0)
118 codec->no_trigger_sense = !val;
119 val = snd_hda_get_bool_hint(codec, "inv_eapd");
120 if (val >= 0)
121 codec->inv_eapd = !!val;
122 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
123 if (val >= 0)
124 codec->pcm_format_first = !!val;
125 val = snd_hda_get_bool_hint(codec, "sticky_stream");
126 if (val >= 0)
127 codec->no_sticky_stream = !val;
128 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
129 if (val >= 0)
130 codec->spdif_status_reset = !!val;
131 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
132 if (val >= 0)
133 codec->pin_amp_workaround = !!val;
134 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
135 if (val >= 0)
136 codec->single_adc_amp = !!val;
Takashi Iwai967b1302015-03-20 18:21:03 +0100137 val = snd_hda_get_bool_hint(codec, "power_save_node");
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100138 if (val >= 0)
Takashi Iwai967b1302015-03-20 18:21:03 +0100139 codec->power_save_node = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100140
Takashi Iwaif72706b2013-01-16 18:20:07 +0100141 val = snd_hda_get_bool_hint(codec, "auto_mute");
142 if (val >= 0)
143 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100144 val = snd_hda_get_bool_hint(codec, "auto_mic");
145 if (val >= 0)
146 spec->suppress_auto_mic = !val;
147 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
148 if (val >= 0)
149 spec->line_in_auto_switch = !!val;
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200150 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
151 if (val >= 0)
152 spec->auto_mute_via_amp = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100153 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
154 if (val >= 0)
155 spec->need_dac_fix = !!val;
156 val = snd_hda_get_bool_hint(codec, "primary_hp");
157 if (val >= 0)
158 spec->no_primary_hp = !val;
Takashi Iwaida96fb52013-07-29 16:54:36 +0200159 val = snd_hda_get_bool_hint(codec, "multi_io");
160 if (val >= 0)
161 spec->no_multi_io = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100162 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
163 if (val >= 0)
164 spec->multi_cap_vol = !!val;
165 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
166 if (val >= 0)
167 spec->inv_dmic_split = !!val;
168 val = snd_hda_get_bool_hint(codec, "indep_hp");
169 if (val >= 0)
170 spec->indep_hp = !!val;
171 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
172 if (val >= 0)
173 spec->add_stereo_mix_input = !!val;
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100174 /* the following two are just for compatibility */
Takashi Iwai1c70a582013-01-11 17:48:22 +0100175 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
176 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100177 spec->add_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100178 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
179 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100180 spec->add_jack_modes = !!val;
181 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
182 if (val >= 0)
183 spec->add_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100184 val = snd_hda_get_bool_hint(codec, "power_down_unused");
185 if (val >= 0)
186 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100187 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
188 if (val >= 0)
189 spec->hp_mic = !!val;
190 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
191 if (val >= 0)
192 spec->suppress_hp_mic_detect = !val;
Takashi Iwai74803162017-04-10 17:37:34 +0200193 val = snd_hda_get_bool_hint(codec, "vmaster");
194 if (val >= 0)
195 spec->suppress_vmaster = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100196
197 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
198 spec->mixer_nid = val;
199}
200
201/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100202 * pin control value accesses
203 */
204
205#define update_pin_ctl(codec, pin, val) \
Takashi Iwai401caff2018-06-27 11:43:09 +0200206 snd_hda_codec_write_cache(codec, pin, 0, \
Takashi Iwai2c12c302013-01-10 09:33:29 +0100207 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
208
209/* restore the pinctl based on the cached value */
210static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
211{
212 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
213}
214
215/* set the pinctl target value and write it if requested */
216static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
217 unsigned int val, bool do_write)
218{
219 if (!pin)
220 return;
221 val = snd_hda_correct_pin_ctl(codec, pin, val);
222 snd_hda_codec_set_pin_target(codec, pin, val);
223 if (do_write)
224 update_pin_ctl(codec, pin, val);
225}
226
227/* set pinctl target values for all given pins */
228static void set_pin_targets(struct hda_codec *codec, int num_pins,
229 hda_nid_t *pins, unsigned int val)
230{
231 int i;
232 for (i = 0; i < num_pins; i++)
233 set_pin_target(codec, pins[i], val, false);
234}
235
236/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100237 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100240/* return the position of NID in the list, or -1 if not found */
241static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
242{
243 int i;
244 for (i = 0; i < nums; i++)
245 if (list[i] == nid)
246 return i;
247 return -1;
248}
249
250/* return true if the given NID is contained in the path */
251static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
252{
253 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
254}
255
Takashi Iwaif5172a72013-01-04 13:19:55 +0100256static struct nid_path *get_nid_path(struct hda_codec *codec,
257 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100258 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100260 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200261 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100262 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200264 snd_array_for_each(&spec->paths, i, path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100265 if (path->depth <= 0)
266 continue;
267 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100268 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100269 if (!anchor_nid ||
270 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
271 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100272 return path;
273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275 return NULL;
276}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100277
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100278/**
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100279 * snd_hda_get_path_idx - get the index number corresponding to the path
280 * instance
281 * @codec: the HDA codec
282 * @path: nid_path object
283 *
284 * The returned index starts from 1, i.e. the actual array index with offset 1,
285 * and zero is handled as an invalid path
Takashi Iwai196c17662013-01-04 15:01:40 +0100286 */
287int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
288{
289 struct hda_gen_spec *spec = codec->spec;
290 struct nid_path *array = spec->paths.list;
291 ssize_t idx;
292
293 if (!spec->paths.used)
294 return 0;
295 idx = path - array;
296 if (idx < 0 || idx >= spec->paths.used)
297 return 0;
298 return idx + 1;
299}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100300EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100301
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100302/**
303 * snd_hda_get_path_from_idx - get the path instance corresponding to the
304 * given index number
305 * @codec: the HDA codec
306 * @idx: the path index
307 */
Takashi Iwai196c17662013-01-04 15:01:40 +0100308struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
309{
310 struct hda_gen_spec *spec = codec->spec;
311
312 if (idx <= 0 || idx > spec->paths.used)
313 return NULL;
314 return snd_array_elem(&spec->paths, idx - 1);
315}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100316EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100317
Takashi Iwai352f7f92012-12-19 12:52:06 +0100318/* check whether the given DAC is already found in any existing paths */
319static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
320{
321 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200322 const struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100323 int i;
324
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200325 snd_array_for_each(&spec->paths, i, path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100326 if (path->path[0] == nid)
327 return true;
328 }
329 return false;
330}
331
332/* check whether the given two widgets can be connected */
333static bool is_reachable_path(struct hda_codec *codec,
334 hda_nid_t from_nid, hda_nid_t to_nid)
335{
336 if (!from_nid || !to_nid)
337 return false;
338 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
339}
340
341/* nid, dir and idx */
342#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
343
344/* check whether the given ctl is already assigned in any path elements */
345static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
346{
347 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200348 const struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100349 int i;
350
351 val &= AMP_VAL_COMPARE_MASK;
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200352 snd_array_for_each(&spec->paths, i, path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100353 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
354 return true;
355 }
356 return false;
357}
358
359/* check whether a control with the given (nid, dir, idx) was assigned */
360static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100361 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100362{
363 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100364 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100365}
366
Takashi Iwai4e76a882014-02-25 12:21:03 +0100367static void print_nid_path(struct hda_codec *codec,
368 const char *pfx, struct nid_path *path)
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100369{
370 char buf[40];
Joe Perchesd82353e2014-07-05 13:02:02 -0700371 char *pos = buf;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100372 int i;
373
Joe Perchesd82353e2014-07-05 13:02:02 -0700374 *pos = 0;
375 for (i = 0; i < path->depth; i++)
376 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
377 pos != buf ? ":" : "",
378 path->path[i]);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100379
Joe Perchesd82353e2014-07-05 13:02:02 -0700380 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100381}
382
Takashi Iwai352f7f92012-12-19 12:52:06 +0100383/* called recursively */
384static bool __parse_nid_path(struct hda_codec *codec,
385 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100386 int anchor_nid, struct nid_path *path,
387 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100388{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100389 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100390 int i, nums;
391
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100392 if (to_nid == anchor_nid)
393 anchor_nid = 0; /* anchor passed */
394 else if (to_nid == (hda_nid_t)(-anchor_nid))
395 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100396
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100397 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100398 for (i = 0; i < nums; i++) {
399 if (conn[i] != from_nid) {
400 /* special case: when from_nid is 0,
401 * try to find an empty DAC
402 */
403 if (from_nid ||
404 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
405 is_dac_already_used(codec, conn[i]))
406 continue;
407 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100408 /* anchor is not requested or already passed? */
409 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100410 goto found;
411 }
412 if (depth >= MAX_NID_PATH_DEPTH)
413 return false;
414 for (i = 0; i < nums; i++) {
415 unsigned int type;
416 type = get_wcaps_type(get_wcaps(codec, conn[i]));
417 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
418 type == AC_WID_PIN)
419 continue;
420 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100421 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100422 goto found;
423 }
424 return false;
425
426 found:
427 path->path[path->depth] = conn[i];
428 path->idx[path->depth + 1] = i;
429 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
430 path->multi[path->depth + 1] = 1;
431 path->depth++;
432 return true;
433}
434
Takashi Iwaic4a58c32015-12-08 11:48:39 +0100435/*
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100436 * snd_hda_parse_nid_path - parse the widget path from the given nid to
437 * the target nid
438 * @codec: the HDA codec
439 * @from_nid: the NID where the path start from
440 * @to_nid: the NID where the path ends at
441 * @anchor_nid: the anchor indication
442 * @path: the path object to store the result
443 *
444 * Returns true if a matching path is found.
445 *
446 * The parsing behavior depends on parameters:
Takashi Iwai352f7f92012-12-19 12:52:06 +0100447 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100448 * when @anchor_nid is set to a positive value, only paths through the widget
449 * with the given value are evaluated.
450 * when @anchor_nid is set to a negative value, paths through the widget
451 * with the negative of given value are excluded, only other paths are chosen.
452 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100453 */
Takashi Iwaic4a58c32015-12-08 11:48:39 +0100454static bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100455 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100456 struct nid_path *path)
457{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100458 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100459 path->path[path->depth] = to_nid;
460 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100461 return true;
462 }
463 return false;
464}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100466/**
467 * snd_hda_add_new_path - parse the path between the given NIDs and
468 * add to the path list
469 * @codec: the HDA codec
470 * @from_nid: the NID where the path start from
471 * @to_nid: the NID where the path ends at
472 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
473 *
474 * If no valid path is found, returns NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100476struct nid_path *
477snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100478 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100480 struct hda_gen_spec *spec = codec->spec;
481 struct nid_path *path;
482
483 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
484 return NULL;
485
Takashi Iwaif5172a72013-01-04 13:19:55 +0100486 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100487 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100488 if (path)
489 return path;
490
Takashi Iwai352f7f92012-12-19 12:52:06 +0100491 path = snd_array_new(&spec->paths);
492 if (!path)
493 return NULL;
494 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100495 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100496 return path;
497 /* push back */
498 spec->paths.used--;
499 return NULL;
500}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100501EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100502
Takashi Iwai980428c2013-01-09 09:28:20 +0100503/* clear the given path as invalid so that it won't be picked up later */
504static void invalidate_nid_path(struct hda_codec *codec, int idx)
505{
506 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
507 if (!path)
508 return;
509 memset(path, 0, sizeof(*path));
510}
511
Takashi Iwai36907392013-12-10 17:29:26 +0100512/* return a DAC if paired to the given pin by codec driver */
513static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
514{
515 struct hda_gen_spec *spec = codec->spec;
516 const hda_nid_t *list = spec->preferred_dacs;
517
518 if (!list)
519 return 0;
520 for (; *list; list += 2)
521 if (*list == pin)
522 return list[1];
523 return 0;
524}
525
Takashi Iwai352f7f92012-12-19 12:52:06 +0100526/* look for an empty DAC slot */
527static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
528 bool is_digital)
529{
530 struct hda_gen_spec *spec = codec->spec;
531 bool cap_digital;
532 int i;
533
534 for (i = 0; i < spec->num_all_dacs; i++) {
535 hda_nid_t nid = spec->all_dacs[i];
536 if (!nid || is_dac_already_used(codec, nid))
537 continue;
538 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
539 if (is_digital != cap_digital)
540 continue;
541 if (is_reachable_path(codec, nid, pin))
542 return nid;
543 }
544 return 0;
545}
546
547/* replace the channels in the composed amp value with the given number */
548static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
549{
550 val &= ~(0x3U << 16);
551 val |= chs << 16;
552 return val;
553}
554
David Henningsson99a55922013-01-16 15:58:44 +0100555static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
556 hda_nid_t nid2, int dir)
557{
558 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
559 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
560 return (query_amp_caps(codec, nid1, dir) ==
561 query_amp_caps(codec, nid2, dir));
562}
563
Takashi Iwai352f7f92012-12-19 12:52:06 +0100564/* look for a widget suitable for assigning a mute switch in the path */
565static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
566 struct nid_path *path)
567{
568 int i;
569
570 for (i = path->depth - 1; i >= 0; i--) {
571 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
572 return path->path[i];
573 if (i != path->depth - 1 && i != 0 &&
574 nid_has_mute(codec, path->path[i], HDA_INPUT))
575 return path->path[i];
576 }
577 return 0;
578}
579
580/* look for a widget suitable for assigning a volume ctl in the path */
581static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
582 struct nid_path *path)
583{
Takashi Iwaia1114a82013-11-04 16:32:01 +0100584 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100585 int i;
586
587 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwaia1114a82013-11-04 16:32:01 +0100588 hda_nid_t nid = path->path[i];
589 if ((spec->out_vol_mask >> nid) & 1)
590 continue;
591 if (nid_has_volume(codec, nid, HDA_OUTPUT))
592 return nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100593 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200594 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
597/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100598 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100600
601/* can have the amp-in capability? */
602static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100604 hda_nid_t nid = path->path[idx];
605 unsigned int caps = get_wcaps(codec, nid);
606 unsigned int type = get_wcaps_type(caps);
607
608 if (!(caps & AC_WCAP_IN_AMP))
609 return false;
610 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
611 return false;
612 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Takashi Iwai352f7f92012-12-19 12:52:06 +0100615/* can have the amp-out capability? */
616static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100618 hda_nid_t nid = path->path[idx];
619 unsigned int caps = get_wcaps(codec, nid);
620 unsigned int type = get_wcaps_type(caps);
621
622 if (!(caps & AC_WCAP_OUT_AMP))
623 return false;
624 if (type == AC_WID_PIN && !idx) /* only for output pins */
625 return false;
626 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Takashi Iwai352f7f92012-12-19 12:52:06 +0100629/* check whether the given (nid,dir,idx) is active */
630static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100631 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100633 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100634 int type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200635 const struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100636 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Takashi Iwai7639a062015-03-03 10:07:24 +0100638 if (nid == codec->core.afg)
Takashi Iwai5ccf8352015-03-18 09:23:10 +0100639 return true;
640
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200641 snd_array_for_each(&spec->paths, n, path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100642 if (!path->active)
643 continue;
Takashi Iwai967b1302015-03-20 18:21:03 +0100644 if (codec->power_save_node) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100645 if (!path->stream_enabled)
646 continue;
647 /* ignore unplugged paths except for DAC/ADC */
Takashi Iwai6b275b12015-03-20 18:11:05 +0100648 if (!(path->pin_enabled || path->pin_fixed) &&
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100649 type != AC_WID_AUD_OUT && type != AC_WID_AUD_IN)
650 continue;
651 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100652 for (i = 0; i < path->depth; i++) {
653 if (path->path[i] == nid) {
Takashi Iwai9d2b48f2015-08-24 10:45:27 +0200654 if (dir == HDA_OUTPUT || idx == -1 ||
655 path->idx[i] == idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100656 return true;
657 break;
658 }
659 }
660 }
661 return false;
662}
663
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200664/* check whether the NID is referred by any active paths */
665#define is_active_nid_for_any(codec, nid) \
Takashi Iwai9d2b48f2015-08-24 10:45:27 +0200666 is_active_nid(codec, nid, HDA_OUTPUT, -1)
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200667
Takashi Iwai352f7f92012-12-19 12:52:06 +0100668/* get the default amp value for the target state */
669static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100670 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100671{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100672 unsigned int val = 0;
673
Takashi Iwai352f7f92012-12-19 12:52:06 +0100674 if (caps & AC_AMPCAP_NUM_STEPS) {
675 /* set to 0dB */
676 if (enable)
677 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
678 }
Takashi Iwaif69910d2013-08-08 09:32:37 +0200679 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100680 if (!enable)
681 val |= HDA_AMP_MUTE;
682 }
683 return val;
684}
685
Takashi Iwaicc261732015-03-16 10:18:08 +0100686/* is this a stereo widget or a stereo-to-mono mix? */
687static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
688{
689 unsigned int wcaps = get_wcaps(codec, nid);
690 hda_nid_t conn;
691
692 if (wcaps & AC_WCAP_STEREO)
693 return true;
694 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
695 return false;
696 if (snd_hda_get_num_conns(codec, nid) != 1)
697 return false;
698 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
699 return false;
700 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
701}
702
Takashi Iwai352f7f92012-12-19 12:52:06 +0100703/* initialize the amp value (only at the first time) */
704static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
705{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100706 unsigned int caps = query_amp_caps(codec, nid, dir);
707 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwaief403ed2015-03-12 08:30:11 +0100708
Takashi Iwaicc261732015-03-16 10:18:08 +0100709 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100710 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
711 else
712 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
713}
714
715/* update the amp, doing in stereo or mono depending on NID */
716static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
717 unsigned int mask, unsigned int val)
718{
Takashi Iwaicc261732015-03-16 10:18:08 +0100719 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100720 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
721 mask, val);
722 else
723 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
724 mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100725}
726
Takashi Iwai8999bf02013-01-18 11:01:33 +0100727/* calculate amp value mask we can modify;
728 * if the given amp is controlled by mixers, don't touch it
729 */
730static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
731 hda_nid_t nid, int dir, int idx,
732 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100733{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100734 unsigned int mask = 0xff;
735
Takashi Iwaif69910d2013-08-08 09:32:37 +0200736 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100737 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
738 mask &= ~0x80;
739 }
740 if (caps & AC_AMPCAP_NUM_STEPS) {
741 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
742 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
743 mask &= ~0x7f;
744 }
745 return mask;
746}
747
748static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
749 int idx, int idx_to_check, bool enable)
750{
751 unsigned int caps;
752 unsigned int mask, val;
753
Takashi Iwai8999bf02013-01-18 11:01:33 +0100754 caps = query_amp_caps(codec, nid, dir);
755 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
756 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
757 if (!mask)
758 return;
759
760 val &= mask;
Takashi Iwaief403ed2015-03-12 08:30:11 +0100761 update_amp(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100762}
763
Takashi Iwaie7fdd522015-12-08 17:00:42 +0100764static void check_and_activate_amp(struct hda_codec *codec, hda_nid_t nid,
765 int dir, int idx, int idx_to_check,
766 bool enable)
767{
768 /* check whether the given amp is still used by others */
769 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
770 return;
771 activate_amp(codec, nid, dir, idx, idx_to_check, enable);
772}
773
Takashi Iwai352f7f92012-12-19 12:52:06 +0100774static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
775 int i, bool enable)
776{
777 hda_nid_t nid = path->path[i];
778 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwaie7fdd522015-12-08 17:00:42 +0100779 check_and_activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100780}
781
782static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
783 int i, bool enable, bool add_aamix)
784{
785 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100786 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100787 int n, nums, idx;
788 int type;
789 hda_nid_t nid = path->path[i];
790
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100791 nums = snd_hda_get_conn_list(codec, nid, &conn);
Dan Carpenter0de7d832017-10-13 13:57:10 +0300792 if (nums < 0)
793 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100794 type = get_wcaps_type(get_wcaps(codec, nid));
795 if (type == AC_WID_PIN ||
796 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
797 nums = 1;
798 idx = 0;
799 } else
800 idx = path->idx[i];
801
802 for (n = 0; n < nums; n++)
803 init_amp(codec, nid, HDA_INPUT, n);
804
Takashi Iwai352f7f92012-12-19 12:52:06 +0100805 /* here is a little bit tricky in comparison with activate_amp_out();
806 * when aa-mixer is available, we need to enable the path as well
807 */
808 for (n = 0; n < nums; n++) {
Takashi Iwaie7fdd522015-12-08 17:00:42 +0100809 if (n != idx) {
810 if (conn[n] != spec->mixer_merge_nid)
811 continue;
812 /* when aamix is disabled, force to off */
813 if (!add_aamix) {
814 activate_amp(codec, nid, HDA_INPUT, n, n, false);
815 continue;
816 }
817 }
818 check_and_activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820}
821
Randy Dunlapc7fabbc2020-08-05 19:19:26 -0700822/* sync power of each widget in the given path */
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100823static hda_nid_t path_power_update(struct hda_codec *codec,
824 struct nid_path *path,
825 bool allow_powerdown)
826{
827 hda_nid_t nid, changed = 0;
Takashi Iwai50fd4982016-04-17 09:39:41 +0200828 int i, state, power;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100829
830 for (i = 0; i < path->depth; i++) {
831 nid = path->path[i];
Takashi Iwai2206dc92015-04-09 10:18:31 +0200832 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
833 continue;
Takashi Iwai7639a062015-03-03 10:07:24 +0100834 if (nid == codec->core.afg)
Takashi Iwai5ccf8352015-03-18 09:23:10 +0100835 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100836 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
837 state = AC_PWRST_D0;
838 else
839 state = AC_PWRST_D3;
Takashi Iwai50fd4982016-04-17 09:39:41 +0200840 power = snd_hda_codec_read(codec, nid, 0,
841 AC_VERB_GET_POWER_STATE, 0);
842 if (power != (state | (state << 4))) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100843 snd_hda_codec_write(codec, nid, 0,
844 AC_VERB_SET_POWER_STATE, state);
845 changed = nid;
Takashi Iwai48f4b3a2015-05-20 06:49:37 +0200846 /* all known codecs seem to be capable to handl
847 * widgets state even in D3, so far.
848 * if any new codecs need to restore the widget
849 * states after D0 transition, call the function
850 * below.
851 */
852#if 0 /* disabled */
Takashi Iwaid545a572015-04-04 12:17:28 +0200853 if (state == AC_PWRST_D0)
854 snd_hdac_regmap_sync_node(&codec->core, nid);
Takashi Iwai48f4b3a2015-05-20 06:49:37 +0200855#endif
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100856 }
857 }
858 return changed;
859}
860
861/* do sync with the last power state change */
862static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
863{
864 if (nid) {
865 msleep(10);
866 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
867 }
868}
869
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100870/**
871 * snd_hda_activate_path - activate or deactivate the given path
872 * @codec: the HDA codec
873 * @path: the path to activate/deactivate
874 * @enable: flag to activate or not
875 * @add_aamix: enable the input from aamix NID
876 *
877 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100879void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
880 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100882 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100883 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Takashi Iwaic7cd0ef2015-08-24 10:52:06 +0200885 path->active = enable;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100886
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100887 /* make sure the widget is powered up */
Takashi Iwai967b1302015-03-20 18:21:03 +0100888 if (enable && (spec->power_down_unused || codec->power_save_node))
889 path_power_update(codec, path, codec->power_save_node);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100890
Takashi Iwai352f7f92012-12-19 12:52:06 +0100891 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100892 hda_nid_t nid = path->path[i];
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100893
Takashi Iwai352f7f92012-12-19 12:52:06 +0100894 if (enable && path->multi[i])
Takashi Iwai401caff2018-06-27 11:43:09 +0200895 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100896 AC_VERB_SET_CONNECT_SEL,
897 path->idx[i]);
898 if (has_amp_in(codec, path, i))
899 activate_amp_in(codec, path, i, enable, add_aamix);
900 if (has_amp_out(codec, path, i))
901 activate_amp_out(codec, path, i, enable);
902 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100903}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100904EXPORT_SYMBOL_GPL(snd_hda_activate_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100905
Takashi Iwai55196ff2013-01-24 17:32:56 +0100906/* if the given path is inactive, put widgets into D3 (only if suitable) */
907static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
908{
909 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100910
Takashi Iwai967b1302015-03-20 18:21:03 +0100911 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
Takashi Iwai55196ff2013-01-24 17:32:56 +0100912 return;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100913 sync_power_state_change(codec, path_power_update(codec, path, true));
Takashi Iwai55196ff2013-01-24 17:32:56 +0100914}
915
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100916/* turn on/off EAPD on the given pin */
917static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
918{
919 struct hda_gen_spec *spec = codec->spec;
920 if (spec->own_eapd_ctl ||
921 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
922 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200923 if (spec->keep_eapd_on && !enable)
924 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100925 if (codec->inv_eapd)
926 enable = !enable;
Takashi Iwai401caff2018-06-27 11:43:09 +0200927 snd_hda_codec_write_cache(codec, pin, 0,
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100928 AC_VERB_SET_EAPD_BTLENABLE,
929 enable ? 0x02 : 0x00);
930}
931
Takashi Iwai3e367f12013-01-23 17:07:23 +0100932/* re-initialize the path specified by the given path index */
933static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
934{
935 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
936 if (path)
937 snd_hda_activate_path(codec, path, path->active, false);
938}
939
Takashi Iwai352f7f92012-12-19 12:52:06 +0100940
941/*
942 * Helper functions for creating mixer ctl elements
943 */
944
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200945static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
946 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai698f5ee2017-05-10 12:13:45 +0200947static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
948 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200949static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
950 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200951
Takashi Iwai352f7f92012-12-19 12:52:06 +0100952enum {
953 HDA_CTL_WIDGET_VOL,
954 HDA_CTL_WIDGET_MUTE,
955 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100956};
957static const struct snd_kcontrol_new control_templates[] = {
958 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200959 /* only the put callback is replaced for handling the special mute */
960 {
961 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
962 .subdevice = HDA_SUBDEV_AMP_FLAG,
963 .info = snd_hda_mixer_amp_switch_info,
964 .get = snd_hda_mixer_amp_switch_get,
965 .put = hda_gen_mixer_mute_put, /* replaced */
966 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
967 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200968 {
969 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
970 .info = snd_hda_mixer_amp_switch_info,
Takashi Iwai698f5ee2017-05-10 12:13:45 +0200971 .get = hda_gen_bind_mute_get,
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200972 .put = hda_gen_bind_mute_put, /* replaced */
973 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
974 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100975};
976
977/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100978static struct snd_kcontrol_new *
979add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100980 int cidx, unsigned long val)
981{
982 struct snd_kcontrol_new *knew;
983
Takashi Iwai12c93df2012-12-19 14:38:33 +0100984 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100985 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100986 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100987 knew->index = cidx;
988 if (get_amp_nid_(val))
989 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kyselae65bf992021-03-17 18:29:43 +0100990 if (knew->access == 0)
991 knew->access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100992 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100993 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100994}
995
996static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
997 const char *pfx, const char *dir,
998 const char *sfx, int cidx, unsigned long val)
999{
Takashi Iwai975cc022013-06-28 11:56:49 +02001000 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01001001 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01001002 if (!add_control(spec, type, name, cidx, val))
1003 return -ENOMEM;
1004 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001005}
1006
1007#define add_pb_vol_ctrl(spec, type, pfx, val) \
1008 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1009#define add_pb_sw_ctrl(spec, type, pfx, val) \
1010 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1011#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1012 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1013#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1014 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1015
1016static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1017 unsigned int chs, struct nid_path *path)
1018{
1019 unsigned int val;
1020 if (!path)
1021 return 0;
1022 val = path->ctls[NID_PATH_VOL_CTL];
1023 if (!val)
1024 return 0;
1025 val = amp_val_replace_channels(val, chs);
1026 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1027}
1028
1029/* return the channel bits suitable for the given path->ctls[] */
1030static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1031 int type)
1032{
1033 int chs = 1; /* mono (left only) */
1034 if (path) {
1035 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1036 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1037 chs = 3; /* stereo */
1038 }
1039 return chs;
1040}
1041
1042static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1043 struct nid_path *path)
1044{
1045 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1046 return add_vol_ctl(codec, pfx, cidx, chs, path);
1047}
1048
1049/* create a mute-switch for the given mixer widget;
1050 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1051 */
1052static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1053 unsigned int chs, struct nid_path *path)
1054{
1055 unsigned int val;
1056 int type = HDA_CTL_WIDGET_MUTE;
1057
1058 if (!path)
1059 return 0;
1060 val = path->ctls[NID_PATH_MUTE_CTL];
1061 if (!val)
1062 return 0;
1063 val = amp_val_replace_channels(val, chs);
1064 if (get_amp_direction_(val) == HDA_INPUT) {
1065 hda_nid_t nid = get_amp_nid_(val);
1066 int nums = snd_hda_get_num_conns(codec, nid);
1067 if (nums > 1) {
1068 type = HDA_CTL_BIND_MUTE;
1069 val |= nums << 19;
1070 }
1071 }
1072 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1073}
1074
1075static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1076 int cidx, struct nid_path *path)
1077{
1078 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1079 return add_sw_ctl(codec, pfx, cidx, chs, path);
1080}
1081
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001082/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001083static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1084 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001085{
1086 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1087 struct hda_gen_spec *spec = codec->spec;
1088
1089 if (spec->auto_mute_via_amp) {
1090 hda_nid_t nid = get_amp_nid(kcontrol);
1091 bool enabled = !((spec->mute_bits >> nid) & 1);
1092 ucontrol->value.integer.value[0] &= enabled;
1093 ucontrol->value.integer.value[1] &= enabled;
1094 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001095}
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001096
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001097static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1098 struct snd_ctl_elem_value *ucontrol)
1099{
1100 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001101 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1102}
1103
Takashi Iwai698f5ee2017-05-10 12:13:45 +02001104/*
1105 * Bound mute controls
1106 */
1107#define AMP_VAL_IDX_SHIFT 19
1108#define AMP_VAL_IDX_MASK (0x0f<<19)
1109
1110static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
1111 struct snd_ctl_elem_value *ucontrol)
1112{
1113 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1114 unsigned long pval;
1115 int err;
1116
1117 mutex_lock(&codec->control_mutex);
1118 pval = kcontrol->private_value;
1119 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1120 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1121 kcontrol->private_value = pval;
1122 mutex_unlock(&codec->control_mutex);
1123 return err;
1124}
1125
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001126static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1127 struct snd_ctl_elem_value *ucontrol)
1128{
Takashi Iwai698f5ee2017-05-10 12:13:45 +02001129 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1130 unsigned long pval;
1131 int i, indices, err = 0, change = 0;
1132
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001133 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai698f5ee2017-05-10 12:13:45 +02001134
1135 mutex_lock(&codec->control_mutex);
1136 pval = kcontrol->private_value;
1137 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1138 for (i = 0; i < indices; i++) {
1139 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1140 (i << AMP_VAL_IDX_SHIFT);
1141 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1142 if (err < 0)
1143 break;
1144 change |= err;
1145 }
1146 kcontrol->private_value = pval;
1147 mutex_unlock(&codec->control_mutex);
1148 return err < 0 ? err : change;
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001149}
1150
Takashi Iwai247d85e2013-01-17 16:18:11 +01001151/* any ctl assigned to the path with the given index? */
1152static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1153{
1154 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1155 return path && path->ctls[ctl_type];
1156}
1157
Takashi Iwai352f7f92012-12-19 12:52:06 +01001158static const char * const channel_name[4] = {
1159 "Front", "Surround", "CLFE", "Side"
1160};
1161
1162/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +01001163static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1164 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001165{
Takashi Iwai247d85e2013-01-17 16:18:11 +01001166 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001167 struct auto_pin_cfg *cfg = &spec->autocfg;
1168
1169 *index = 0;
1170 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai9f3dadb2017-04-10 17:12:33 +02001171 !codec->force_pin_prefix &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001172 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001173 return spec->vmaster_mute.hook ? "PCM" : "Master";
1174
1175 /* if there is really a single DAC used in the whole output paths,
1176 * use it master (or "PCM" if a vmaster hook is present)
1177 */
1178 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
Takashi Iwai9f3dadb2017-04-10 17:12:33 +02001179 !codec->force_pin_prefix &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001180 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1181 return spec->vmaster_mute.hook ? "PCM" : "Master";
1182
Takashi Iwai247d85e2013-01-17 16:18:11 +01001183 /* multi-io channels */
1184 if (ch >= cfg->line_outs)
1185 return channel_name[ch];
1186
Takashi Iwai352f7f92012-12-19 12:52:06 +01001187 switch (cfg->line_out_type) {
1188 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001189 /* if the primary channel vol/mute is shared with HP volume,
1190 * don't name it as Speaker
1191 */
1192 if (!ch && cfg->hp_outs &&
1193 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1194 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001195 if (cfg->line_outs == 1)
1196 return "Speaker";
1197 if (cfg->line_outs == 2)
1198 return ch ? "Bass Speaker" : "Speaker";
1199 break;
1200 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001201 /* if the primary channel vol/mute is shared with spk volume,
1202 * don't name it as Headphone
1203 */
1204 if (!ch && cfg->speaker_outs &&
1205 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1206 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001207 /* for multi-io case, only the primary out */
1208 if (ch && spec->multi_ios)
1209 break;
1210 *index = ch;
1211 return "Headphone";
David Henningsson03ad6a82014-10-16 15:33:45 +02001212 case AUTO_PIN_LINE_OUT:
Hui Wangf48652b2021-05-04 15:39:17 +08001213 /* This deals with the case where one HP or one Speaker or
1214 * one HP + one Speaker need to share the DAC with LO
1215 */
1216 if (!ch) {
1217 bool hp_lo_shared = false, spk_lo_shared = false;
1218
1219 if (cfg->speaker_outs)
1220 spk_lo_shared = !path_has_mixer(codec,
1221 spec->speaker_paths[0], ctl_type);
1222 if (cfg->hp_outs)
1223 hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
David Henningsson03ad6a82014-10-16 15:33:45 +02001224 if (hp_lo_shared && spk_lo_shared)
1225 return spec->vmaster_mute.hook ? "PCM" : "Master";
1226 if (hp_lo_shared)
1227 return "Headphone+LO";
1228 if (spk_lo_shared)
1229 return "Speaker+LO";
1230 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001231 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001232
1233 /* for a single channel output, we don't have to name the channel */
1234 if (cfg->line_outs == 1 && !spec->multi_ios)
David Henningsson3abb4f42014-10-16 15:33:46 +02001235 return "Line Out";
Takashi Iwai247d85e2013-01-17 16:18:11 +01001236
Takashi Iwai352f7f92012-12-19 12:52:06 +01001237 if (ch >= ARRAY_SIZE(channel_name)) {
1238 snd_BUG();
1239 return "PCM";
1240 }
1241
1242 return channel_name[ch];
1243}
1244
1245/*
1246 * Parse output paths
1247 */
1248
1249/* badness definition */
1250enum {
1251 /* No primary DAC is found for the main output */
1252 BAD_NO_PRIMARY_DAC = 0x10000,
1253 /* No DAC is found for the extra output */
1254 BAD_NO_DAC = 0x4000,
1255 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001256 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001257 /* No individual DAC for extra output */
1258 BAD_NO_EXTRA_DAC = 0x102,
1259 /* No individual DAC for extra surrounds */
1260 BAD_NO_EXTRA_SURR_DAC = 0x101,
1261 /* Primary DAC shared with main surrounds */
1262 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001263 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001264 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001265 /* Primary DAC shared with main CLFE */
1266 BAD_SHARED_CLFE = 0x10,
1267 /* Primary DAC shared with extra surrounds */
1268 BAD_SHARED_EXTRA_SURROUND = 0x10,
1269 /* Volume widget is shared */
1270 BAD_SHARED_VOL = 0x10,
1271};
1272
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001273/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001274 * volume and mute controls, and assign the values to ctls[].
1275 *
1276 * When no appropriate widget is found in the path, the badness value
1277 * is incremented depending on the situation. The function returns the
1278 * total badness for both volume and mute controls.
1279 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001280static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001281{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001282 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001283 hda_nid_t nid;
1284 unsigned int val;
1285 int badness = 0;
1286
1287 if (!path)
1288 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001289
1290 if (path->ctls[NID_PATH_VOL_CTL] ||
1291 path->ctls[NID_PATH_MUTE_CTL])
1292 return 0; /* already evaluated */
1293
Takashi Iwai352f7f92012-12-19 12:52:06 +01001294 nid = look_for_out_vol_nid(codec, path);
1295 if (nid) {
1296 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001297 if (spec->dac_min_mute)
1298 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001299 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1300 badness += BAD_SHARED_VOL;
1301 else
1302 path->ctls[NID_PATH_VOL_CTL] = val;
1303 } else
1304 badness += BAD_SHARED_VOL;
1305 nid = look_for_out_mute_nid(codec, path);
1306 if (nid) {
1307 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1308 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1309 nid_has_mute(codec, nid, HDA_OUTPUT))
1310 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1311 else
1312 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1313 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1314 badness += BAD_SHARED_VOL;
1315 else
1316 path->ctls[NID_PATH_MUTE_CTL] = val;
1317 } else
1318 badness += BAD_SHARED_VOL;
1319 return badness;
1320}
1321
Takashi Iwai98bd1112013-03-22 14:53:50 +01001322const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001323 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1324 .no_dac = BAD_NO_DAC,
1325 .shared_primary = BAD_NO_PRIMARY_DAC,
1326 .shared_surr = BAD_SHARED_SURROUND,
1327 .shared_clfe = BAD_SHARED_CLFE,
1328 .shared_surr_main = BAD_SHARED_SURROUND,
1329};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001330EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001331
Takashi Iwai98bd1112013-03-22 14:53:50 +01001332const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001333 .no_primary_dac = BAD_NO_DAC,
1334 .no_dac = BAD_NO_DAC,
1335 .shared_primary = BAD_NO_EXTRA_DAC,
1336 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1337 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1338 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1339};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001340EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001341
Takashi Iwai7385df62013-01-07 09:50:52 +01001342/* get the DAC of the primary output corresponding to the given array index */
1343static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1344{
1345 struct hda_gen_spec *spec = codec->spec;
1346 struct auto_pin_cfg *cfg = &spec->autocfg;
1347
1348 if (cfg->line_outs > idx)
1349 return spec->private_dac_nids[idx];
1350 idx -= cfg->line_outs;
1351 if (spec->multi_ios > idx)
1352 return spec->multi_io[idx].dac;
1353 return 0;
1354}
1355
1356/* return the DAC if it's reachable, otherwise zero */
1357static inline hda_nid_t try_dac(struct hda_codec *codec,
1358 hda_nid_t dac, hda_nid_t pin)
1359{
1360 return is_reachable_path(codec, dac, pin) ? dac : 0;
1361}
1362
Takashi Iwai352f7f92012-12-19 12:52:06 +01001363/* try to assign DACs to pins and return the resultant badness */
1364static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1365 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001366 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001367 const struct badness_table *bad)
1368{
1369 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001370 int i, j;
1371 int badness = 0;
1372 hda_nid_t dac;
1373
1374 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return 0;
1376
Takashi Iwai352f7f92012-12-19 12:52:06 +01001377 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001378 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001379 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001380
Takashi Iwai242d9902020-11-27 15:11:03 +01001381 if (!spec->obey_preferred_dacs) {
1382 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1383 if (path) {
1384 badness += assign_out_path_ctls(codec, path);
1385 continue;
1386 }
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001387 }
1388
Takashi Iwai36907392013-12-10 17:29:26 +01001389 dacs[i] = get_preferred_dac(codec, pin);
1390 if (dacs[i]) {
1391 if (is_dac_already_used(codec, dacs[i]))
1392 badness += bad->shared_primary;
Takashi Iwai242d9902020-11-27 15:11:03 +01001393 } else if (spec->obey_preferred_dacs) {
1394 badness += BAD_NO_PRIMARY_DAC;
Takashi Iwai36907392013-12-10 17:29:26 +01001395 }
1396
1397 if (!dacs[i])
1398 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001399 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001400 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001401 for (j = 1; j < num_outs; j++) {
1402 if (is_reachable_path(codec, dacs[j], pin)) {
1403 dacs[0] = dacs[j];
1404 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001405 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001406 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001407 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001410 }
1411 dac = dacs[i];
1412 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001413 if (num_outs > 2)
1414 dac = try_dac(codec, get_primary_out(codec, i), pin);
1415 if (!dac)
1416 dac = try_dac(codec, dacs[0], pin);
1417 if (!dac)
1418 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001419 if (dac) {
1420 if (!i)
1421 badness += bad->shared_primary;
1422 else if (i == 1)
1423 badness += bad->shared_surr;
1424 else
1425 badness += bad->shared_clfe;
1426 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1427 dac = spec->private_dac_nids[0];
1428 badness += bad->shared_surr_main;
1429 } else if (!i)
1430 badness += bad->no_primary_dac;
1431 else
1432 badness += bad->no_dac;
1433 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001434 if (!dac)
1435 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001436 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001437 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001438 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001439 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001440 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001441 if (!path) {
Jiapeng Chonge73b4c92021-05-13 19:11:11 +08001442 dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001443 badness += bad->no_dac;
1444 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001445 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001446 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001447 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001448 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001449 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001450 }
1451
1452 return badness;
1453}
1454
1455/* return NID if the given pin has only a single connection to a certain DAC */
1456static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1457{
1458 struct hda_gen_spec *spec = codec->spec;
1459 int i;
1460 hda_nid_t nid_found = 0;
1461
1462 for (i = 0; i < spec->num_all_dacs; i++) {
1463 hda_nid_t nid = spec->all_dacs[i];
1464 if (!nid || is_dac_already_used(codec, nid))
1465 continue;
1466 if (is_reachable_path(codec, nid, pin)) {
1467 if (nid_found)
1468 return 0;
1469 nid_found = nid;
1470 }
1471 }
1472 return nid_found;
1473}
1474
1475/* check whether the given pin can be a multi-io pin */
1476static bool can_be_multiio_pin(struct hda_codec *codec,
1477 unsigned int location, hda_nid_t nid)
1478{
1479 unsigned int defcfg, caps;
1480
1481 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1482 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1483 return false;
1484 if (location && get_defcfg_location(defcfg) != location)
1485 return false;
1486 caps = snd_hda_query_pin_caps(codec, nid);
1487 if (!(caps & AC_PINCAP_OUT))
1488 return false;
1489 return true;
1490}
1491
Takashi Iwaie22aab72013-01-04 14:50:04 +01001492/* count the number of input pins that are capable to be multi-io */
1493static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1494{
1495 struct hda_gen_spec *spec = codec->spec;
1496 struct auto_pin_cfg *cfg = &spec->autocfg;
1497 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1498 unsigned int location = get_defcfg_location(defcfg);
1499 int type, i;
1500 int num_pins = 0;
1501
1502 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1503 for (i = 0; i < cfg->num_inputs; i++) {
1504 if (cfg->inputs[i].type != type)
1505 continue;
1506 if (can_be_multiio_pin(codec, location,
1507 cfg->inputs[i].pin))
1508 num_pins++;
1509 }
1510 }
1511 return num_pins;
1512}
1513
Takashi Iwai352f7f92012-12-19 12:52:06 +01001514/*
1515 * multi-io helper
1516 *
1517 * When hardwired is set, try to fill ony hardwired pins, and returns
1518 * zero if any pins are filled, non-zero if nothing found.
1519 * When hardwired is off, try to fill possible input pins, and returns
1520 * the badness value.
1521 */
1522static int fill_multi_ios(struct hda_codec *codec,
1523 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001524 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001525{
1526 struct hda_gen_spec *spec = codec->spec;
1527 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001528 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001529 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1530 unsigned int location = get_defcfg_location(defcfg);
1531 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001532 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001533
1534 old_pins = spec->multi_ios;
1535 if (old_pins >= 2)
1536 goto end_fill;
1537
Takashi Iwaie22aab72013-01-04 14:50:04 +01001538 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001539 if (num_pins < 2)
1540 goto end_fill;
1541
Takashi Iwai352f7f92012-12-19 12:52:06 +01001542 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1543 for (i = 0; i < cfg->num_inputs; i++) {
1544 hda_nid_t nid = cfg->inputs[i].pin;
1545 hda_nid_t dac = 0;
1546
1547 if (cfg->inputs[i].type != type)
1548 continue;
1549 if (!can_be_multiio_pin(codec, location, nid))
1550 continue;
1551 for (j = 0; j < spec->multi_ios; j++) {
1552 if (nid == spec->multi_io[j].pin)
1553 break;
1554 }
1555 if (j < spec->multi_ios)
1556 continue;
1557
Takashi Iwai352f7f92012-12-19 12:52:06 +01001558 if (hardwired)
1559 dac = get_dac_if_single(codec, nid);
1560 else if (!dac)
1561 dac = look_for_dac(codec, nid, false);
1562 if (!dac) {
1563 badness++;
1564 continue;
1565 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001566 path = snd_hda_add_new_path(codec, dac, nid,
1567 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001568 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001569 badness++;
1570 continue;
1571 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001572 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001573 spec->multi_io[spec->multi_ios].pin = nid;
1574 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001575 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1576 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001577 spec->multi_ios++;
1578 if (spec->multi_ios >= 2)
1579 break;
1580 }
1581 }
1582 end_fill:
1583 if (badness)
1584 badness = BAD_MULTI_IO;
1585 if (old_pins == spec->multi_ios) {
1586 if (hardwired)
1587 return 1; /* nothing found */
1588 else
1589 return badness; /* no badness if nothing found */
1590 }
1591 if (!hardwired && spec->multi_ios < 2) {
1592 /* cancel newly assigned paths */
1593 spec->paths.used -= spec->multi_ios - old_pins;
1594 spec->multi_ios = old_pins;
1595 return badness;
1596 }
1597
1598 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001599 for (i = old_pins; i < spec->multi_ios; i++) {
1600 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1601 badness += assign_out_path_ctls(codec, path);
1602 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001603
1604 return badness;
1605}
1606
1607/* map DACs for all pins in the list if they are single connections */
1608static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001609 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001610{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001611 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001612 int i;
1613 bool found = false;
1614 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001615 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001616 hda_nid_t dac;
1617 if (dacs[i])
1618 continue;
1619 dac = get_dac_if_single(codec, pins[i]);
1620 if (!dac)
1621 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001622 path = snd_hda_add_new_path(codec, dac, pins[i],
1623 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001624 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001625 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001626 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001627 dacs[i] = dac;
1628 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001629 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001630 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001631 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001632 }
1633 }
1634 return found;
1635}
1636
Takashi Iwaie7fdd522015-12-08 17:00:42 +01001637static inline bool has_aamix_out_paths(struct hda_gen_spec *spec)
1638{
1639 return spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1640 spec->aamix_out_paths[2];
1641}
1642
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001643/* create a new path including aamix if available, and return its index */
1644static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1645{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001646 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001647 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001648 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001649
1650 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001651 if (!path || !path->depth ||
1652 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001653 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001654 path_dac = path->path[0];
1655 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001656 pin = path->path[path->depth - 1];
1657 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1658 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001659 if (dac != path_dac)
1660 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001661 else if (spec->multiout.hp_out_nid[0])
1662 dac = spec->multiout.hp_out_nid[0];
1663 else if (spec->multiout.extra_out_nid[0])
1664 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001665 else
1666 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001667 if (dac)
1668 path = snd_hda_add_new_path(codec, dac, pin,
1669 spec->mixer_nid);
1670 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001671 if (!path)
1672 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001673 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001674 path->active = false; /* unused as default */
Takashi Iwai6b275b12015-03-20 18:11:05 +01001675 path->pin_fixed = true; /* static route */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001676 return snd_hda_get_path_idx(codec, path);
1677}
1678
Takashi Iwai55a63d42013-03-21 17:20:12 +01001679/* check whether the independent HP is available with the current config */
1680static bool indep_hp_possible(struct hda_codec *codec)
1681{
1682 struct hda_gen_spec *spec = codec->spec;
1683 struct auto_pin_cfg *cfg = &spec->autocfg;
1684 struct nid_path *path;
1685 int i, idx;
1686
1687 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1688 idx = spec->out_paths[0];
1689 else
1690 idx = spec->hp_paths[0];
1691 path = snd_hda_get_path_from_idx(codec, idx);
1692 if (!path)
1693 return false;
1694
1695 /* assume no path conflicts unless aamix is involved */
1696 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1697 return true;
1698
1699 /* check whether output paths contain aamix */
1700 for (i = 0; i < cfg->line_outs; i++) {
1701 if (spec->out_paths[i] == idx)
1702 break;
1703 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1704 if (path && is_nid_contained(path, spec->mixer_nid))
1705 return false;
1706 }
1707 for (i = 0; i < cfg->speaker_outs; i++) {
1708 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1709 if (path && is_nid_contained(path, spec->mixer_nid))
1710 return false;
1711 }
1712
1713 return true;
1714}
1715
Takashi Iwaia07a9492013-01-07 16:44:06 +01001716/* fill the empty entries in the dac array for speaker/hp with the
1717 * shared dac pointed by the paths
1718 */
1719static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1720 hda_nid_t *dacs, int *path_idx)
1721{
1722 struct nid_path *path;
1723 int i;
1724
1725 for (i = 0; i < num_outs; i++) {
1726 if (dacs[i])
1727 continue;
1728 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1729 if (!path)
1730 continue;
1731 dacs[i] = path->path[0];
1732 }
1733}
1734
Takashi Iwai352f7f92012-12-19 12:52:06 +01001735/* fill in the dac_nids table from the parsed pin configuration */
1736static int fill_and_eval_dacs(struct hda_codec *codec,
1737 bool fill_hardwired,
1738 bool fill_mio_first)
1739{
1740 struct hda_gen_spec *spec = codec->spec;
1741 struct auto_pin_cfg *cfg = &spec->autocfg;
1742 int i, err, badness;
1743
1744 /* set num_dacs once to full for look_for_dac() */
1745 spec->multiout.num_dacs = cfg->line_outs;
1746 spec->multiout.dac_nids = spec->private_dac_nids;
1747 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1748 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1749 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1750 spec->multi_ios = 0;
1751 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001752
1753 /* clear path indices */
1754 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1755 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1756 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1757 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1758 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001759 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001760 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1761 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1762
Takashi Iwai352f7f92012-12-19 12:52:06 +01001763 badness = 0;
1764
1765 /* fill hard-wired DACs first */
1766 if (fill_hardwired) {
1767 bool mapped;
1768 do {
1769 mapped = map_singles(codec, cfg->line_outs,
1770 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001771 spec->private_dac_nids,
1772 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001773 mapped |= map_singles(codec, cfg->hp_outs,
1774 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001775 spec->multiout.hp_out_nid,
1776 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001777 mapped |= map_singles(codec, cfg->speaker_outs,
1778 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001779 spec->multiout.extra_out_nid,
1780 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001781 if (!spec->no_multi_io &&
1782 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001783 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001784 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001785 if (!err)
1786 mapped = true;
1787 }
1788 } while (mapped);
1789 }
1790
1791 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001792 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001793 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001794
Takashi Iwaida96fb52013-07-29 16:54:36 +02001795 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001796 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1797 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001798 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001799 if (err < 0)
1800 return err;
1801 /* we don't count badness at this stage yet */
1802 }
1803
1804 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1805 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1806 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001807 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001808 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001809 if (err < 0)
1810 return err;
1811 badness += err;
1812 }
1813 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1814 err = try_assign_dacs(codec, cfg->speaker_outs,
1815 cfg->speaker_pins,
1816 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001817 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001818 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001819 if (err < 0)
1820 return err;
1821 badness += err;
1822 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001823 if (!spec->no_multi_io &&
1824 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001825 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001826 if (err < 0)
1827 return err;
1828 badness += err;
1829 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001830
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001831 if (spec->mixer_nid) {
1832 spec->aamix_out_paths[0] =
1833 check_aamix_out_path(codec, spec->out_paths[0]);
1834 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1835 spec->aamix_out_paths[1] =
1836 check_aamix_out_path(codec, spec->hp_paths[0]);
1837 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1838 spec->aamix_out_paths[2] =
1839 check_aamix_out_path(codec, spec->speaker_paths[0]);
1840 }
1841
Takashi Iwaida96fb52013-07-29 16:54:36 +02001842 if (!spec->no_multi_io &&
1843 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001844 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1845 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001846
Takashi Iwaia07a9492013-01-07 16:44:06 +01001847 /* re-count num_dacs and squash invalid entries */
1848 spec->multiout.num_dacs = 0;
1849 for (i = 0; i < cfg->line_outs; i++) {
1850 if (spec->private_dac_nids[i])
1851 spec->multiout.num_dacs++;
1852 else {
1853 memmove(spec->private_dac_nids + i,
1854 spec->private_dac_nids + i + 1,
1855 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1856 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1857 }
1858 }
1859
1860 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001861 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001862
Takashi Iwai352f7f92012-12-19 12:52:06 +01001863 if (spec->multi_ios == 2) {
1864 for (i = 0; i < 2; i++)
1865 spec->private_dac_nids[spec->multiout.num_dacs++] =
1866 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001867 } else if (spec->multi_ios) {
1868 spec->multi_ios = 0;
1869 badness += BAD_MULTI_IO;
1870 }
1871
Takashi Iwai55a63d42013-03-21 17:20:12 +01001872 if (spec->indep_hp && !indep_hp_possible(codec))
1873 badness += BAD_NO_INDEP_HP;
1874
Takashi Iwaia07a9492013-01-07 16:44:06 +01001875 /* re-fill the shared DAC for speaker / headphone */
1876 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1877 refill_shared_dacs(codec, cfg->hp_outs,
1878 spec->multiout.hp_out_nid,
1879 spec->hp_paths);
1880 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1881 refill_shared_dacs(codec, cfg->speaker_outs,
1882 spec->multiout.extra_out_nid,
1883 spec->speaker_paths);
1884
Takashi Iwai352f7f92012-12-19 12:52:06 +01001885 return badness;
1886}
1887
1888#define DEBUG_BADNESS
1889
1890#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001891#define debug_badness(fmt, ...) \
1892 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001893#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001894#define debug_badness(fmt, ...) \
1895 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001896#endif
1897
Takashi Iwaia7694092013-01-21 10:43:18 +01001898#ifdef DEBUG_BADNESS
1899static inline void print_nid_path_idx(struct hda_codec *codec,
1900 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001901{
Takashi Iwaia7694092013-01-21 10:43:18 +01001902 struct nid_path *path;
1903
1904 path = snd_hda_get_path_from_idx(codec, idx);
1905 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001906 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001907}
1908
1909static void debug_show_configs(struct hda_codec *codec,
1910 struct auto_pin_cfg *cfg)
1911{
1912 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001913 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001914 int i;
1915
1916 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001917 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001918 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001919 spec->multiout.dac_nids[0],
1920 spec->multiout.dac_nids[1],
1921 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001922 spec->multiout.dac_nids[3],
1923 lo_type[cfg->line_out_type]);
1924 for (i = 0; i < cfg->line_outs; i++)
1925 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001926 if (spec->multi_ios > 0)
1927 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1928 spec->multi_ios,
1929 spec->multi_io[0].pin, spec->multi_io[1].pin,
1930 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001931 for (i = 0; i < spec->multi_ios; i++)
1932 print_nid_path_idx(codec, " mio",
1933 spec->out_paths[cfg->line_outs + i]);
1934 if (cfg->hp_outs)
1935 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001936 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001937 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001938 spec->multiout.hp_out_nid[0],
1939 spec->multiout.hp_out_nid[1],
1940 spec->multiout.hp_out_nid[2],
1941 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001942 for (i = 0; i < cfg->hp_outs; i++)
1943 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1944 if (cfg->speaker_outs)
1945 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001946 cfg->speaker_pins[0], cfg->speaker_pins[1],
1947 cfg->speaker_pins[2], cfg->speaker_pins[3],
1948 spec->multiout.extra_out_nid[0],
1949 spec->multiout.extra_out_nid[1],
1950 spec->multiout.extra_out_nid[2],
1951 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001952 for (i = 0; i < cfg->speaker_outs; i++)
1953 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1954 for (i = 0; i < 3; i++)
1955 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001956}
Takashi Iwaia7694092013-01-21 10:43:18 +01001957#else
1958#define debug_show_configs(codec, cfg) /* NOP */
1959#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001960
1961/* find all available DACs of the codec */
1962static void fill_all_dac_nids(struct hda_codec *codec)
1963{
1964 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001965 hda_nid_t nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001966
1967 spec->num_all_dacs = 0;
1968 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
Takashi Iwai7639a062015-03-03 10:07:24 +01001969 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001970 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1971 continue;
1972 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001973 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001974 break;
1975 }
1976 spec->all_dacs[spec->num_all_dacs++] = nid;
1977 }
1978}
1979
1980static int parse_output_paths(struct hda_codec *codec)
1981{
1982 struct hda_gen_spec *spec = codec->spec;
1983 struct auto_pin_cfg *cfg = &spec->autocfg;
1984 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001985 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001986 int best_badness = INT_MAX;
1987 int badness;
1988 bool fill_hardwired = true, fill_mio_first = true;
1989 bool best_wired = true, best_mio = true;
1990 bool hp_spk_swapped = false;
1991
Takashi Iwai352f7f92012-12-19 12:52:06 +01001992 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1993 if (!best_cfg)
1994 return -ENOMEM;
1995 *best_cfg = *cfg;
1996
1997 for (;;) {
1998 badness = fill_and_eval_dacs(codec, fill_hardwired,
1999 fill_mio_first);
2000 if (badness < 0) {
2001 kfree(best_cfg);
2002 return badness;
2003 }
2004 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
2005 cfg->line_out_type, fill_hardwired, fill_mio_first,
2006 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01002007 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002008 if (badness < best_badness) {
2009 best_badness = badness;
2010 *best_cfg = *cfg;
2011 best_wired = fill_hardwired;
2012 best_mio = fill_mio_first;
2013 }
2014 if (!badness)
2015 break;
2016 fill_mio_first = !fill_mio_first;
2017 if (!fill_mio_first)
2018 continue;
2019 fill_hardwired = !fill_hardwired;
2020 if (!fill_hardwired)
2021 continue;
2022 if (hp_spk_swapped)
2023 break;
2024 hp_spk_swapped = true;
2025 if (cfg->speaker_outs > 0 &&
2026 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2027 cfg->hp_outs = cfg->line_outs;
2028 memcpy(cfg->hp_pins, cfg->line_out_pins,
2029 sizeof(cfg->hp_pins));
2030 cfg->line_outs = cfg->speaker_outs;
2031 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2032 sizeof(cfg->speaker_pins));
2033 cfg->speaker_outs = 0;
2034 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2035 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2036 fill_hardwired = true;
2037 continue;
2038 }
2039 if (cfg->hp_outs > 0 &&
2040 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2041 cfg->speaker_outs = cfg->line_outs;
2042 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2043 sizeof(cfg->speaker_pins));
2044 cfg->line_outs = cfg->hp_outs;
2045 memcpy(cfg->line_out_pins, cfg->hp_pins,
2046 sizeof(cfg->hp_pins));
2047 cfg->hp_outs = 0;
2048 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2049 cfg->line_out_type = AUTO_PIN_HP_OUT;
2050 fill_hardwired = true;
2051 continue;
2052 }
2053 break;
2054 }
2055
2056 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002057 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01002058 *cfg = *best_cfg;
2059 fill_and_eval_dacs(codec, best_wired, best_mio);
2060 }
2061 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2062 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01002063 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002064
2065 if (cfg->line_out_pins[0]) {
2066 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01002067 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002068 if (path)
2069 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002070 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01002071 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2072 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002073 if (spec->dac_min_mute)
Takashi Sakamoto51cdc8b2018-05-14 07:09:52 +09002074 spec->vmaster_tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] |= TLV_DB_SCALE_MUTE;
Takashi Iwaid89c6c02014-09-01 10:07:04 +02002075 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002076 }
2077
Takashi Iwai9314a582013-01-21 10:49:05 +01002078 /* set initial pinctl targets */
2079 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2080 val = PIN_HP;
2081 else
2082 val = PIN_OUT;
2083 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2084 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2085 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2086 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2087 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2088 set_pin_targets(codec, cfg->speaker_outs,
2089 cfg->speaker_pins, val);
2090 }
2091
Takashi Iwai55a63d42013-03-21 17:20:12 +01002092 /* clear indep_hp flag if not available */
2093 if (spec->indep_hp && !indep_hp_possible(codec))
2094 spec->indep_hp = 0;
2095
Takashi Iwai352f7f92012-12-19 12:52:06 +01002096 kfree(best_cfg);
2097 return 0;
2098}
2099
2100/* add playback controls from the parsed DAC table */
2101static int create_multi_out_ctls(struct hda_codec *codec,
2102 const struct auto_pin_cfg *cfg)
2103{
2104 struct hda_gen_spec *spec = codec->spec;
2105 int i, err, noutputs;
2106
2107 noutputs = cfg->line_outs;
2108 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2109 noutputs += spec->multi_ios;
2110
2111 for (i = 0; i < noutputs; i++) {
2112 const char *name;
2113 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002114 struct nid_path *path;
2115
Takashi Iwai196c17662013-01-04 15:01:40 +01002116 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002117 if (!path)
2118 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002119
2120 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002121 if (!name || !strcmp(name, "CLFE")) {
2122 /* Center/LFE */
2123 err = add_vol_ctl(codec, "Center", 0, 1, path);
2124 if (err < 0)
2125 return err;
2126 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2127 if (err < 0)
2128 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002129 } else {
2130 err = add_stereo_vol(codec, name, index, path);
2131 if (err < 0)
2132 return err;
2133 }
2134
2135 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2136 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002137 err = add_sw_ctl(codec, "Center", 0, 1, path);
2138 if (err < 0)
2139 return err;
2140 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2141 if (err < 0)
2142 return err;
2143 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002144 err = add_stereo_sw(codec, name, index, path);
2145 if (err < 0)
2146 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
2148 }
2149 return 0;
2150}
2151
Takashi Iwaic2c80382013-01-07 10:33:57 +01002152static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01002153 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002155 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 int err;
2157
Takashi Iwai196c17662013-01-04 15:01:40 +01002158 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002159 if (!path)
2160 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01002161 err = add_stereo_vol(codec, pfx, cidx, path);
2162 if (err < 0)
2163 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002164 err = add_stereo_sw(codec, pfx, cidx, path);
2165 if (err < 0)
2166 return err;
2167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168}
2169
Takashi Iwai352f7f92012-12-19 12:52:06 +01002170/* add playback controls for speaker and HP outputs */
2171static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01002172 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
Takashi Iwaic2c80382013-01-07 10:33:57 +01002174 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002175
2176 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002177 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02002178 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01002179 int err, idx = 0;
2180
2181 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2182 name = "Bass Speaker";
2183 else if (num_pins >= 3) {
2184 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01002185 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01002186 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002187 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002188 name = pfx;
2189 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01002191 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002192 if (err < 0)
2193 return err;
2194 }
2195 return 0;
2196}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002197
Takashi Iwai352f7f92012-12-19 12:52:06 +01002198static int create_hp_out_ctls(struct hda_codec *codec)
2199{
2200 struct hda_gen_spec *spec = codec->spec;
2201 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002202 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002203 "Headphone");
2204}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Takashi Iwai352f7f92012-12-19 12:52:06 +01002206static int create_speaker_out_ctls(struct hda_codec *codec)
2207{
2208 struct hda_gen_spec *spec = codec->spec;
2209 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002210 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002211 "Speaker");
2212}
2213
2214/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002215 * independent HP controls
2216 */
2217
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02002218static void call_hp_automute(struct hda_codec *codec,
2219 struct hda_jack_callback *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002220static int indep_hp_info(struct snd_kcontrol *kcontrol,
2221 struct snd_ctl_elem_info *uinfo)
2222{
2223 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2224}
2225
2226static int indep_hp_get(struct snd_kcontrol *kcontrol,
2227 struct snd_ctl_elem_value *ucontrol)
2228{
2229 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2230 struct hda_gen_spec *spec = codec->spec;
2231 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2232 return 0;
2233}
2234
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002235static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2236 int nomix_path_idx, int mix_path_idx,
2237 int out_type);
2238
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002239static int indep_hp_put(struct snd_kcontrol *kcontrol,
2240 struct snd_ctl_elem_value *ucontrol)
2241{
2242 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2243 struct hda_gen_spec *spec = codec->spec;
2244 unsigned int select = ucontrol->value.enumerated.item[0];
2245 int ret = 0;
2246
2247 mutex_lock(&spec->pcm_mutex);
2248 if (spec->active_streams) {
2249 ret = -EBUSY;
2250 goto unlock;
2251 }
2252
2253 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002254 hda_nid_t *dacp;
2255 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2256 dacp = &spec->private_dac_nids[0];
2257 else
2258 dacp = &spec->multiout.hp_out_nid[0];
2259
2260 /* update HP aamix paths in case it conflicts with indep HP */
2261 if (spec->have_aamix_ctl) {
2262 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2263 update_aamix_paths(codec, spec->aamix_mode,
2264 spec->out_paths[0],
2265 spec->aamix_out_paths[0],
2266 spec->autocfg.line_out_type);
2267 else
2268 update_aamix_paths(codec, spec->aamix_mode,
2269 spec->hp_paths[0],
2270 spec->aamix_out_paths[1],
2271 AUTO_PIN_HP_OUT);
2272 }
2273
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002274 spec->indep_hp_enabled = select;
2275 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002276 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002277 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002278 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002279
Takashi Iwai963afde2013-05-31 15:20:31 +02002280 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002281 ret = 1;
2282 }
2283 unlock:
2284 mutex_unlock(&spec->pcm_mutex);
2285 return ret;
2286}
2287
2288static const struct snd_kcontrol_new indep_hp_ctl = {
2289 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2290 .name = "Independent HP",
2291 .info = indep_hp_info,
2292 .get = indep_hp_get,
2293 .put = indep_hp_put,
2294};
2295
2296
2297static int create_indep_hp_ctls(struct hda_codec *codec)
2298{
2299 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002300 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002301
2302 if (!spec->indep_hp)
2303 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002304 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2305 dac = spec->multiout.dac_nids[0];
2306 else
2307 dac = spec->multiout.hp_out_nid[0];
2308 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002309 spec->indep_hp = 0;
2310 return 0;
2311 }
2312
2313 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002314 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002315 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2316 return -ENOMEM;
2317 return 0;
2318}
2319
2320/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002321 * channel mode enum control
2322 */
2323
2324static int ch_mode_info(struct snd_kcontrol *kcontrol,
2325 struct snd_ctl_elem_info *uinfo)
2326{
2327 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2328 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002329 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002330
2331 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2332 uinfo->count = 1;
2333 uinfo->value.enumerated.items = spec->multi_ios + 1;
2334 if (uinfo->value.enumerated.item > spec->multi_ios)
2335 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002336 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2337 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002338 return 0;
2339}
2340
2341static int ch_mode_get(struct snd_kcontrol *kcontrol,
2342 struct snd_ctl_elem_value *ucontrol)
2343{
2344 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2345 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002346 ucontrol->value.enumerated.item[0] =
2347 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002348 return 0;
2349}
2350
Takashi Iwai196c17662013-01-04 15:01:40 +01002351static inline struct nid_path *
2352get_multiio_path(struct hda_codec *codec, int idx)
2353{
2354 struct hda_gen_spec *spec = codec->spec;
2355 return snd_hda_get_path_from_idx(codec,
2356 spec->out_paths[spec->autocfg.line_outs + idx]);
2357}
2358
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002359static void update_automute_all(struct hda_codec *codec);
2360
Takashi Iwai65033cc2013-04-16 12:31:05 +02002361/* Default value to be passed as aamix argument for snd_hda_activate_path();
2362 * used for output paths
2363 */
2364static bool aamix_default(struct hda_gen_spec *spec)
2365{
2366 return !spec->have_aamix_ctl || spec->aamix_mode;
2367}
2368
Takashi Iwai352f7f92012-12-19 12:52:06 +01002369static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2370{
2371 struct hda_gen_spec *spec = codec->spec;
2372 hda_nid_t nid = spec->multi_io[idx].pin;
2373 struct nid_path *path;
2374
Takashi Iwai196c17662013-01-04 15:01:40 +01002375 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002376 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002378
2379 if (path->active == output)
2380 return 0;
2381
2382 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002383 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002384 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002385 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002386 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002387 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002388 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002389 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002390 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002392
2393 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002394 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002395
Takashi Iwai352f7f92012-12-19 12:52:06 +01002396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
Takashi Iwai352f7f92012-12-19 12:52:06 +01002399static int ch_mode_put(struct snd_kcontrol *kcontrol,
2400 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002402 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2403 struct hda_gen_spec *spec = codec->spec;
2404 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Takashi Iwai352f7f92012-12-19 12:52:06 +01002406 ch = ucontrol->value.enumerated.item[0];
2407 if (ch < 0 || ch > spec->multi_ios)
2408 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002409 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002410 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002411 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002412 for (i = 0; i < spec->multi_ios; i++)
2413 set_multi_io(codec, i, i < ch);
2414 spec->multiout.max_channels = max(spec->ext_channel_count,
2415 spec->const_channel_count);
2416 if (spec->need_dac_fix)
2417 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return 1;
2419}
2420
Takashi Iwai352f7f92012-12-19 12:52:06 +01002421static const struct snd_kcontrol_new channel_mode_enum = {
2422 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2423 .name = "Channel Mode",
2424 .info = ch_mode_info,
2425 .get = ch_mode_get,
2426 .put = ch_mode_put,
2427};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Takashi Iwai352f7f92012-12-19 12:52:06 +01002429static int create_multi_channel_mode(struct hda_codec *codec)
2430{
2431 struct hda_gen_spec *spec = codec->spec;
2432
2433 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002434 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002435 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return 0;
2438}
2439
Takashi Iwai352f7f92012-12-19 12:52:06 +01002440/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002441 * aamix loopback enable/disable switch
2442 */
2443
2444#define loopback_mixing_info indep_hp_info
2445
2446static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2447 struct snd_ctl_elem_value *ucontrol)
2448{
2449 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2450 struct hda_gen_spec *spec = codec->spec;
2451 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2452 return 0;
2453}
2454
2455static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002456 int nomix_path_idx, int mix_path_idx,
2457 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002458{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002459 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002460 struct nid_path *nomix_path, *mix_path;
2461
2462 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2463 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2464 if (!nomix_path || !mix_path)
2465 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002466
2467 /* if HP aamix path is driven from a different DAC and the
2468 * independent HP mode is ON, can't turn on aamix path
2469 */
2470 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2471 mix_path->path[0] != spec->alt_dac_nid)
2472 do_mix = false;
2473
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002474 if (do_mix) {
2475 snd_hda_activate_path(codec, nomix_path, false, true);
2476 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002477 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002478 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002479 snd_hda_activate_path(codec, mix_path, false, false);
2480 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002481 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002482 }
2483}
2484
Takashi Iwaie7fdd522015-12-08 17:00:42 +01002485/* re-initialize the output paths; only called from loopback_mixing_put() */
2486static void update_output_paths(struct hda_codec *codec, int num_outs,
2487 const int *paths)
2488{
2489 struct hda_gen_spec *spec = codec->spec;
2490 struct nid_path *path;
2491 int i;
2492
2493 for (i = 0; i < num_outs; i++) {
2494 path = snd_hda_get_path_from_idx(codec, paths[i]);
2495 if (path)
2496 snd_hda_activate_path(codec, path, path->active,
2497 spec->aamix_mode);
2498 }
2499}
2500
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002501static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2502 struct snd_ctl_elem_value *ucontrol)
2503{
2504 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2505 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie7fdd522015-12-08 17:00:42 +01002506 const struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002507 unsigned int val = ucontrol->value.enumerated.item[0];
2508
2509 if (val == spec->aamix_mode)
2510 return 0;
2511 spec->aamix_mode = val;
Takashi Iwaie7fdd522015-12-08 17:00:42 +01002512 if (has_aamix_out_paths(spec)) {
2513 update_aamix_paths(codec, val, spec->out_paths[0],
2514 spec->aamix_out_paths[0],
2515 cfg->line_out_type);
2516 update_aamix_paths(codec, val, spec->hp_paths[0],
2517 spec->aamix_out_paths[1],
2518 AUTO_PIN_HP_OUT);
2519 update_aamix_paths(codec, val, spec->speaker_paths[0],
2520 spec->aamix_out_paths[2],
2521 AUTO_PIN_SPEAKER_OUT);
2522 } else {
2523 update_output_paths(codec, cfg->line_outs, spec->out_paths);
2524 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2525 update_output_paths(codec, cfg->hp_outs, spec->hp_paths);
2526 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
2527 update_output_paths(codec, cfg->speaker_outs,
2528 spec->speaker_paths);
2529 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002530 return 1;
2531}
2532
2533static const struct snd_kcontrol_new loopback_mixing_enum = {
2534 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2535 .name = "Loopback Mixing",
2536 .info = loopback_mixing_info,
2537 .get = loopback_mixing_get,
2538 .put = loopback_mixing_put,
2539};
2540
2541static int create_loopback_mixing_ctl(struct hda_codec *codec)
2542{
2543 struct hda_gen_spec *spec = codec->spec;
2544
2545 if (!spec->mixer_nid)
2546 return 0;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002547 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2548 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002549 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002550 return 0;
2551}
2552
2553/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002554 * shared headphone/mic handling
2555 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002556
Takashi Iwai352f7f92012-12-19 12:52:06 +01002557static void call_update_outputs(struct hda_codec *codec);
2558
2559/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002560static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002561{
2562 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002563 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002564 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002565 hda_nid_t pin;
2566
2567 pin = spec->hp_mic_pin;
2568 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2569
2570 if (!force) {
2571 val = snd_hda_codec_get_pin_target(codec, pin);
2572 if (as_mic) {
2573 if (val & PIN_IN)
2574 return;
2575 } else {
2576 if (val & PIN_OUT)
2577 return;
2578 }
2579 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002580
2581 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002582 /* if the HP pin doesn't support VREF and the codec driver gives an
2583 * alternative pin, set up the VREF on that pin instead
2584 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002585 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2586 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2587 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2588 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002589 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002590 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002591 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002592
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002593 if (!spec->hp_mic_jack_modes) {
2594 if (as_mic)
2595 val |= PIN_IN;
2596 else
2597 val = PIN_HP;
2598 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002599 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002600 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002601}
2602
2603/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002604static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002605{
2606 struct hda_gen_spec *spec = codec->spec;
2607 struct auto_pin_cfg *cfg = &spec->autocfg;
2608 unsigned int defcfg;
2609 hda_nid_t nid;
2610
Takashi Iwai967303d2013-02-19 17:12:42 +01002611 if (!spec->hp_mic) {
2612 if (spec->suppress_hp_mic_detect)
2613 return 0;
2614 /* automatic detection: only if no input or a single internal
2615 * input pin is found, try to detect the shared hp/mic
2616 */
2617 if (cfg->num_inputs > 1)
2618 return 0;
2619 else if (cfg->num_inputs == 1) {
2620 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2621 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2622 return 0;
2623 }
2624 }
2625
2626 spec->hp_mic = 0; /* clear once */
2627 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002628 return 0;
2629
Takashi Iwai967303d2013-02-19 17:12:42 +01002630 nid = 0;
2631 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2632 nid = cfg->line_out_pins[0];
2633 else if (cfg->hp_outs > 0)
2634 nid = cfg->hp_pins[0];
2635 if (!nid)
2636 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002637
2638 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2639 return 0; /* no input */
2640
Takashi Iwai967303d2013-02-19 17:12:42 +01002641 cfg->inputs[cfg->num_inputs].pin = nid;
2642 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002643 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002644 cfg->num_inputs++;
2645 spec->hp_mic = 1;
2646 spec->hp_mic_pin = nid;
2647 /* we can't handle auto-mic together with HP-mic */
2648 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002649 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002650 return 0;
2651}
2652
Takashi Iwai978e77e2013-01-10 16:57:58 +01002653/*
2654 * output jack mode
2655 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002656
2657static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2658
2659static const char * const out_jack_texts[] = {
2660 "Line Out", "Headphone Out",
2661};
2662
Takashi Iwai978e77e2013-01-10 16:57:58 +01002663static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2664 struct snd_ctl_elem_info *uinfo)
2665{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002666 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002667}
2668
2669static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2670 struct snd_ctl_elem_value *ucontrol)
2671{
2672 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2673 hda_nid_t nid = kcontrol->private_value;
2674 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2675 ucontrol->value.enumerated.item[0] = 1;
2676 else
2677 ucontrol->value.enumerated.item[0] = 0;
2678 return 0;
2679}
2680
2681static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2682 struct snd_ctl_elem_value *ucontrol)
2683{
2684 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2685 hda_nid_t nid = kcontrol->private_value;
2686 unsigned int val;
2687
2688 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2689 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2690 return 0;
2691 snd_hda_set_pin_ctl_cache(codec, nid, val);
2692 return 1;
2693}
2694
2695static const struct snd_kcontrol_new out_jack_mode_enum = {
2696 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2697 .info = out_jack_mode_info,
2698 .get = out_jack_mode_get,
2699 .put = out_jack_mode_put,
2700};
2701
2702static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2703{
2704 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +02002705 const struct snd_kcontrol_new *kctl;
Takashi Iwai978e77e2013-01-10 16:57:58 +01002706 int i;
2707
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +02002708 snd_array_for_each(&spec->kctls, i, kctl) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002709 if (!strcmp(kctl->name, name) && kctl->index == idx)
2710 return true;
2711 }
2712 return false;
2713}
2714
2715static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2716 char *name, size_t name_len)
2717{
2718 struct hda_gen_spec *spec = codec->spec;
2719 int idx = 0;
2720
2721 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2722 strlcat(name, " Jack Mode", name_len);
2723
2724 for (; find_kctl_name(codec, name, idx); idx++)
2725 ;
2726}
2727
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002728static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2729{
2730 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002731 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002732 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2733 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2734 return 2;
2735 }
2736 return 1;
2737}
2738
Takashi Iwai978e77e2013-01-10 16:57:58 +01002739static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2740 hda_nid_t *pins)
2741{
2742 struct hda_gen_spec *spec = codec->spec;
2743 int i;
2744
2745 for (i = 0; i < num_pins; i++) {
2746 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002747 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002748 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002749 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002750 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002751 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002752 get_jack_mode_name(codec, pin, name, sizeof(name));
2753 knew = snd_hda_gen_add_kctl(spec, name,
2754 &out_jack_mode_enum);
2755 if (!knew)
2756 return -ENOMEM;
2757 knew->private_value = pin;
2758 }
2759 }
2760
2761 return 0;
2762}
2763
Takashi Iwai294765582013-01-17 09:52:11 +01002764/*
2765 * input jack mode
2766 */
2767
2768/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2769#define NUM_VREFS 6
2770
2771static const char * const vref_texts[NUM_VREFS] = {
2772 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2773 "", "Mic 80pc Bias", "Mic 100pc Bias"
2774};
2775
2776static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2777{
2778 unsigned int pincap;
2779
2780 pincap = snd_hda_query_pin_caps(codec, pin);
2781 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2782 /* filter out unusual vrefs */
2783 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2784 return pincap;
2785}
2786
2787/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2788static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2789{
2790 unsigned int i, n = 0;
2791
2792 for (i = 0; i < NUM_VREFS; i++) {
2793 if (vref_caps & (1 << i)) {
2794 if (n == item_idx)
2795 return i;
2796 n++;
2797 }
2798 }
2799 return 0;
2800}
2801
2802/* convert back from the vref ctl index to the enum item index */
2803static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2804{
2805 unsigned int i, n = 0;
2806
2807 for (i = 0; i < NUM_VREFS; i++) {
2808 if (i == idx)
2809 return n;
2810 if (vref_caps & (1 << i))
2811 n++;
2812 }
2813 return 0;
2814}
2815
2816static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2817 struct snd_ctl_elem_info *uinfo)
2818{
2819 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2820 hda_nid_t nid = kcontrol->private_value;
2821 unsigned int vref_caps = get_vref_caps(codec, nid);
2822
2823 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2824 vref_texts);
2825 /* set the right text */
2826 strcpy(uinfo->value.enumerated.name,
2827 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2828 return 0;
2829}
2830
2831static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2832 struct snd_ctl_elem_value *ucontrol)
2833{
2834 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2835 hda_nid_t nid = kcontrol->private_value;
2836 unsigned int vref_caps = get_vref_caps(codec, nid);
2837 unsigned int idx;
2838
2839 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2840 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2841 return 0;
2842}
2843
2844static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2845 struct snd_ctl_elem_value *ucontrol)
2846{
2847 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2848 hda_nid_t nid = kcontrol->private_value;
2849 unsigned int vref_caps = get_vref_caps(codec, nid);
2850 unsigned int val, idx;
2851
2852 val = snd_hda_codec_get_pin_target(codec, nid);
2853 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2854 if (idx == ucontrol->value.enumerated.item[0])
2855 return 0;
2856
2857 val &= ~AC_PINCTL_VREFEN;
2858 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2859 snd_hda_set_pin_ctl_cache(codec, nid, val);
2860 return 1;
2861}
2862
2863static const struct snd_kcontrol_new in_jack_mode_enum = {
2864 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2865 .info = in_jack_mode_info,
2866 .get = in_jack_mode_get,
2867 .put = in_jack_mode_put,
2868};
2869
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002870static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2871{
2872 struct hda_gen_spec *spec = codec->spec;
2873 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002874 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002875 nitems = hweight32(get_vref_caps(codec, pin));
2876 return nitems ? nitems : 1;
2877}
2878
Takashi Iwai294765582013-01-17 09:52:11 +01002879static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2880{
2881 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002882 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002883 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002884 unsigned int defcfg;
2885
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002886 if (pin == spec->hp_mic_pin)
2887 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002888
2889 /* no jack mode for fixed pins */
2890 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2891 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2892 return 0;
2893
2894 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002895 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002896 return 0;
2897
2898 get_jack_mode_name(codec, pin, name, sizeof(name));
2899 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2900 if (!knew)
2901 return -ENOMEM;
2902 knew->private_value = pin;
2903 return 0;
2904}
2905
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002906/*
2907 * HP/mic shared jack mode
2908 */
2909static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2910 struct snd_ctl_elem_info *uinfo)
2911{
2912 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2913 hda_nid_t nid = kcontrol->private_value;
2914 int out_jacks = get_out_jack_num_items(codec, nid);
2915 int in_jacks = get_in_jack_num_items(codec, nid);
2916 const char *text = NULL;
2917 int idx;
2918
2919 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2920 uinfo->count = 1;
2921 uinfo->value.enumerated.items = out_jacks + in_jacks;
2922 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2923 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2924 idx = uinfo->value.enumerated.item;
2925 if (idx < out_jacks) {
2926 if (out_jacks > 1)
2927 text = out_jack_texts[idx];
2928 else
2929 text = "Headphone Out";
2930 } else {
2931 idx -= out_jacks;
2932 if (in_jacks > 1) {
2933 unsigned int vref_caps = get_vref_caps(codec, nid);
2934 text = vref_texts[get_vref_idx(vref_caps, idx)];
2935 } else
2936 text = "Mic In";
2937 }
2938
2939 strcpy(uinfo->value.enumerated.name, text);
2940 return 0;
2941}
2942
2943static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2944{
2945 int out_jacks = get_out_jack_num_items(codec, nid);
2946 int in_jacks = get_in_jack_num_items(codec, nid);
2947 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2948 int idx = 0;
2949
2950 if (val & PIN_OUT) {
2951 if (out_jacks > 1 && val == PIN_HP)
2952 idx = 1;
2953 } else if (val & PIN_IN) {
2954 idx = out_jacks;
2955 if (in_jacks > 1) {
2956 unsigned int vref_caps = get_vref_caps(codec, nid);
2957 val &= AC_PINCTL_VREFEN;
2958 idx += cvt_from_vref_idx(vref_caps, val);
2959 }
2960 }
2961 return idx;
2962}
2963
2964static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2965 struct snd_ctl_elem_value *ucontrol)
2966{
2967 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2968 hda_nid_t nid = kcontrol->private_value;
2969 ucontrol->value.enumerated.item[0] =
2970 get_cur_hp_mic_jack_mode(codec, nid);
2971 return 0;
2972}
2973
2974static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2975 struct snd_ctl_elem_value *ucontrol)
2976{
2977 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2978 hda_nid_t nid = kcontrol->private_value;
2979 int out_jacks = get_out_jack_num_items(codec, nid);
2980 int in_jacks = get_in_jack_num_items(codec, nid);
2981 unsigned int val, oldval, idx;
2982
2983 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2984 idx = ucontrol->value.enumerated.item[0];
2985 if (oldval == idx)
2986 return 0;
2987
2988 if (idx < out_jacks) {
2989 if (out_jacks > 1)
2990 val = idx ? PIN_HP : PIN_OUT;
2991 else
2992 val = PIN_HP;
2993 } else {
2994 idx -= out_jacks;
2995 if (in_jacks > 1) {
2996 unsigned int vref_caps = get_vref_caps(codec, nid);
2997 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002998 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2999 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01003000 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01003001 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01003002 }
3003 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02003004 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01003005
Takashi Iwai5f171ba2013-02-19 18:14:54 +01003006 return 1;
3007}
3008
3009static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
3010 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3011 .info = hp_mic_jack_mode_info,
3012 .get = hp_mic_jack_mode_get,
3013 .put = hp_mic_jack_mode_put,
3014};
3015
3016static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
3017{
3018 struct hda_gen_spec *spec = codec->spec;
3019 struct snd_kcontrol_new *knew;
3020
Takashi Iwai5f171ba2013-02-19 18:14:54 +01003021 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
3022 &hp_mic_jack_mode_enum);
3023 if (!knew)
3024 return -ENOMEM;
3025 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01003026 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01003027 return 0;
3028}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003029
3030/*
3031 * Parse input paths
3032 */
3033
Takashi Iwai352f7f92012-12-19 12:52:06 +01003034/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003035static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003036{
3037 struct hda_amp_list *list;
3038
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003039 list = snd_array_new(&spec->loopback_list);
3040 if (!list)
3041 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003042 list->nid = mix;
3043 list->dir = HDA_INPUT;
3044 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003045 spec->loopback.amplist = spec->loopback_list.list;
3046 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003047}
Takashi Iwaicb53c622007-08-10 17:21:45 +02003048
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003049/* return true if either a volume or a mute amp is found for the given
3050 * aamix path; the amp has to be either in the mixer node or its direct leaf
3051 */
3052static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3053 hda_nid_t pin, unsigned int *mix_val,
3054 unsigned int *mute_val)
3055{
3056 int idx, num_conns;
3057 const hda_nid_t *list;
3058 hda_nid_t nid;
3059
3060 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3061 if (idx < 0)
3062 return false;
3063
3064 *mix_val = *mute_val = 0;
3065 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3066 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3067 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3068 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3069 if (*mix_val && *mute_val)
3070 return true;
3071
3072 /* check leaf node */
3073 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3074 if (num_conns < idx)
3075 return false;
3076 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01003077 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3078 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003079 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01003080 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3081 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003082 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3083
3084 return *mix_val || *mute_val;
3085}
3086
Takashi Iwai352f7f92012-12-19 12:52:06 +01003087/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01003088static int new_analog_input(struct hda_codec *codec, int input_idx,
3089 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003090 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003092 struct hda_gen_spec *spec = codec->spec;
3093 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003094 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003095 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003097 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3098 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003099
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003100 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003101 if (!path)
3102 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003103 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01003104 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003105
3106 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003107 if (mix_val) {
3108 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003109 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003111 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 }
3113
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003114 if (mute_val) {
3115 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003116 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003118 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 }
3120
Takashi Iwai352f7f92012-12-19 12:52:06 +01003121 path->active = true;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003122 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003123 err = add_loopback_list(spec, mix_nid, idx);
3124 if (err < 0)
3125 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003126
3127 if (spec->mixer_nid != spec->mixer_merge_nid &&
3128 !spec->loopback_merge_path) {
3129 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3130 spec->mixer_merge_nid, 0);
3131 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003132 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003133 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003134 path->pin_fixed = true; /* static route */
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003135 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003136 spec->loopback_merge_path =
3137 snd_hda_get_path_idx(codec, path);
3138 }
3139 }
3140
Takashi Iwai352f7f92012-12-19 12:52:06 +01003141 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142}
3143
Takashi Iwai352f7f92012-12-19 12:52:06 +01003144static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003146 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3147 return (pincap & AC_PINCAP_IN) != 0;
3148}
3149
3150/* Parse the codec tree and retrieve ADCs */
3151static int fill_adc_nids(struct hda_codec *codec)
3152{
3153 struct hda_gen_spec *spec = codec->spec;
3154 hda_nid_t nid;
3155 hda_nid_t *adc_nids = spec->adc_nids;
3156 int max_nums = ARRAY_SIZE(spec->adc_nids);
Takashi Iwai7639a062015-03-03 10:07:24 +01003157 int nums = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003158
Takashi Iwai7639a062015-03-03 10:07:24 +01003159 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003160 unsigned int caps = get_wcaps(codec, nid);
3161 int type = get_wcaps_type(caps);
3162
3163 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3164 continue;
3165 adc_nids[nums] = nid;
3166 if (++nums >= max_nums)
3167 break;
3168 }
3169 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01003170
3171 /* copy the detected ADCs to all_adcs[] */
3172 spec->num_all_adcs = nums;
3173 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3174
Takashi Iwai352f7f92012-12-19 12:52:06 +01003175 return nums;
3176}
3177
3178/* filter out invalid adc_nids that don't give all active input pins;
3179 * if needed, check whether dynamic ADC-switching is available
3180 */
3181static int check_dyn_adc_switch(struct hda_codec *codec)
3182{
3183 struct hda_gen_spec *spec = codec->spec;
3184 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003185 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003186 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003187
Takashi Iwai352f7f92012-12-19 12:52:06 +01003188 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003189 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003190 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003191 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003192 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003193 break;
3194 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003195 if (i >= imux->num_items) {
3196 ok_bits |= (1 << n);
3197 nums++;
3198 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003199 }
3200
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003201 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003202 /* check whether ADC-switch is possible */
3203 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003204 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003205 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003206 spec->dyn_adc_idx[i] = n;
3207 break;
3208 }
3209 }
3210 }
3211
Takashi Iwai4e76a882014-02-25 12:21:03 +01003212 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003213 spec->dyn_adc_switch = 1;
3214 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003215 /* shrink the invalid adcs and input paths */
3216 nums = 0;
3217 for (n = 0; n < spec->num_adc_nids; n++) {
3218 if (!(ok_bits & (1 << n)))
3219 continue;
3220 if (n != nums) {
3221 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003222 for (i = 0; i < imux->num_items; i++) {
3223 invalidate_nid_path(codec,
3224 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003225 spec->input_paths[i][nums] =
3226 spec->input_paths[i][n];
Hui Wanga8f20fd2017-06-28 08:59:16 +08003227 spec->input_paths[i][n] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01003228 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003229 }
3230 nums++;
3231 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003232 spec->num_adc_nids = nums;
3233 }
3234
Takashi Iwai967303d2013-02-19 17:12:42 +01003235 if (imux->num_items == 1 ||
3236 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003237 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003238 spec->num_adc_nids = 1; /* reduce to a single ADC */
3239 }
3240
3241 /* single index for individual volumes ctls */
3242 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3243 spec->num_adc_nids = 1;
3244
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 return 0;
3246}
3247
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003248/* parse capture source paths from the given pin and create imux items */
3249static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003250 int cfg_idx, int num_adcs,
3251 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003252{
3253 struct hda_gen_spec *spec = codec->spec;
3254 struct hda_input_mux *imux = &spec->input_mux;
3255 int imux_idx = imux->num_items;
3256 bool imux_added = false;
3257 int c;
3258
3259 for (c = 0; c < num_adcs; c++) {
3260 struct nid_path *path;
3261 hda_nid_t adc = spec->adc_nids[c];
3262
3263 if (!is_reachable_path(codec, pin, adc))
3264 continue;
3265 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3266 if (!path)
3267 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003268 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003269 spec->input_paths[imux_idx][c] =
3270 snd_hda_get_path_idx(codec, path);
3271
3272 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003273 if (spec->hp_mic_pin == pin)
3274 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003275 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003276 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003277 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003278 if (spec->dyn_adc_switch)
3279 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003280 }
3281 }
3282
3283 return 0;
3284}
3285
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003287 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003289
Takashi Iwaic9700422013-01-18 10:17:30 +01003290/* fill the label for each input at first */
3291static int fill_input_pin_labels(struct hda_codec *codec)
3292{
3293 struct hda_gen_spec *spec = codec->spec;
3294 const struct auto_pin_cfg *cfg = &spec->autocfg;
3295 int i;
3296
3297 for (i = 0; i < cfg->num_inputs; i++) {
3298 hda_nid_t pin = cfg->inputs[i].pin;
3299 const char *label;
3300 int j, idx;
3301
3302 if (!is_input_pin(codec, pin))
3303 continue;
3304
3305 label = hda_get_autocfg_input_label(codec, cfg, i);
3306 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003307 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003308 if (spec->input_labels[j] &&
3309 !strcmp(spec->input_labels[j], label)) {
3310 idx = spec->input_label_idxs[j] + 1;
3311 break;
3312 }
3313 }
3314
3315 spec->input_labels[i] = label;
3316 spec->input_label_idxs[i] = idx;
3317 }
3318
3319 return 0;
3320}
3321
Takashi Iwai9dba2052013-01-18 10:01:15 +01003322#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3323
Takashi Iwai352f7f92012-12-19 12:52:06 +01003324static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003325{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003326 struct hda_gen_spec *spec = codec->spec;
3327 const struct auto_pin_cfg *cfg = &spec->autocfg;
3328 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003329 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003330 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003331 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003332
Takashi Iwai352f7f92012-12-19 12:52:06 +01003333 num_adcs = fill_adc_nids(codec);
3334 if (num_adcs < 0)
3335 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003336
Takashi Iwaic9700422013-01-18 10:17:30 +01003337 err = fill_input_pin_labels(codec);
3338 if (err < 0)
3339 return err;
3340
Takashi Iwai352f7f92012-12-19 12:52:06 +01003341 for (i = 0; i < cfg->num_inputs; i++) {
3342 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
Takashi Iwai352f7f92012-12-19 12:52:06 +01003344 pin = cfg->inputs[i].pin;
3345 if (!is_input_pin(codec, pin))
3346 continue;
3347
Takashi Iwai2c12c302013-01-10 09:33:29 +01003348 val = PIN_IN;
3349 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3350 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai3e1b0c42015-04-27 10:43:22 +02003351 if (pin != spec->hp_mic_pin &&
3352 !snd_hda_codec_get_pin_target(codec, pin))
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003353 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003354
Takashi Iwai352f7f92012-12-19 12:52:06 +01003355 if (mixer) {
3356 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003357 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003358 spec->input_labels[i],
3359 spec->input_label_idxs[i],
3360 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003361 if (err < 0)
3362 return err;
3363 }
3364 }
3365
Takashi Iwaic9700422013-01-18 10:17:30 +01003366 err = parse_capture_source(codec, pin, i, num_adcs,
3367 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003368 if (err < 0)
3369 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003370
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003371 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003372 err = create_in_jack_mode(codec, pin);
3373 if (err < 0)
3374 return err;
3375 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003376 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003377
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003378 /* add stereo mix when explicitly enabled via hint */
Takashi Iwai74f14b32014-12-15 13:43:59 +01003379 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003380 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003381 "Stereo Mix", 0);
3382 if (err < 0)
3383 return err;
Takashi Iwai82d04e12014-12-15 13:40:42 +01003384 else
3385 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003386 }
3387
3388 return 0;
3389}
3390
3391
3392/*
3393 * input source mux
3394 */
3395
Takashi Iwaic697b712013-01-07 17:09:26 +01003396/* get the input path specified by the given adc and imux indices */
3397static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003398{
3399 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003400 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3401 snd_BUG();
3402 return NULL;
3403 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003404 if (spec->dyn_adc_switch)
3405 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003406 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003407 snd_BUG();
3408 return NULL;
3409 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003410 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003411}
3412
3413static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3414 unsigned int idx);
3415
3416static int mux_enum_info(struct snd_kcontrol *kcontrol,
3417 struct snd_ctl_elem_info *uinfo)
3418{
3419 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3420 struct hda_gen_spec *spec = codec->spec;
3421 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3422}
3423
3424static int mux_enum_get(struct snd_kcontrol *kcontrol,
3425 struct snd_ctl_elem_value *ucontrol)
3426{
3427 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3428 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003429 /* the ctls are created at once with multiple counts */
3430 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003431
3432 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3433 return 0;
3434}
3435
3436static int mux_enum_put(struct snd_kcontrol *kcontrol,
3437 struct snd_ctl_elem_value *ucontrol)
3438{
3439 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003440 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003441 return mux_select(codec, adc_idx,
3442 ucontrol->value.enumerated.item[0]);
3443}
3444
Takashi Iwai352f7f92012-12-19 12:52:06 +01003445static const struct snd_kcontrol_new cap_src_temp = {
3446 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3447 .name = "Input Source",
3448 .info = mux_enum_info,
3449 .get = mux_enum_get,
3450 .put = mux_enum_put,
3451};
3452
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003453/*
3454 * capture volume and capture switch ctls
3455 */
3456
Takashi Iwai352f7f92012-12-19 12:52:06 +01003457typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3458 struct snd_ctl_elem_value *ucontrol);
3459
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003460/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003461static int cap_put_caller(struct snd_kcontrol *kcontrol,
3462 struct snd_ctl_elem_value *ucontrol,
3463 put_call_t func, int type)
3464{
3465 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3466 struct hda_gen_spec *spec = codec->spec;
3467 const struct hda_input_mux *imux;
3468 struct nid_path *path;
Jaroslav Kyselaa2befe92021-08-11 18:14:41 +02003469 int i, adc_idx, ret, err = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003470
3471 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003472 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003473 mutex_lock(&codec->control_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003474 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003475 path = get_input_path(codec, adc_idx, i);
3476 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003477 continue;
3478 kcontrol->private_value = path->ctls[type];
Jaroslav Kyselaa2befe92021-08-11 18:14:41 +02003479 ret = func(kcontrol, ucontrol);
3480 if (ret < 0) {
3481 err = ret;
Takashi Iwaia551d912015-02-26 12:34:49 +01003482 break;
Jaroslav Kyselaa2befe92021-08-11 18:14:41 +02003483 }
3484 if (ret > 0)
3485 err = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003486 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003487 mutex_unlock(&codec->control_mutex);
3488 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003489 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003490 return err;
3491}
3492
3493/* capture volume ctl callbacks */
3494#define cap_vol_info snd_hda_mixer_amp_volume_info
3495#define cap_vol_get snd_hda_mixer_amp_volume_get
3496#define cap_vol_tlv snd_hda_mixer_amp_tlv
3497
3498static int cap_vol_put(struct snd_kcontrol *kcontrol,
3499 struct snd_ctl_elem_value *ucontrol)
3500{
3501 return cap_put_caller(kcontrol, ucontrol,
3502 snd_hda_mixer_amp_volume_put,
3503 NID_PATH_VOL_CTL);
3504}
3505
3506static const struct snd_kcontrol_new cap_vol_temp = {
3507 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3508 .name = "Capture Volume",
3509 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3510 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3511 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3512 .info = cap_vol_info,
3513 .get = cap_vol_get,
3514 .put = cap_vol_put,
3515 .tlv = { .c = cap_vol_tlv },
3516};
3517
3518/* capture switch ctl callbacks */
3519#define cap_sw_info snd_ctl_boolean_stereo_info
3520#define cap_sw_get snd_hda_mixer_amp_switch_get
3521
3522static int cap_sw_put(struct snd_kcontrol *kcontrol,
3523 struct snd_ctl_elem_value *ucontrol)
3524{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003525 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003526 snd_hda_mixer_amp_switch_put,
3527 NID_PATH_MUTE_CTL);
3528}
3529
3530static const struct snd_kcontrol_new cap_sw_temp = {
3531 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3532 .name = "Capture Switch",
Takashi Iwai08a4b902021-05-31 20:06:33 +02003533 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003534 .info = cap_sw_info,
3535 .get = cap_sw_get,
3536 .put = cap_sw_put,
3537};
3538
3539static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3540{
3541 hda_nid_t nid;
3542 int i, depth;
3543
3544 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3545 for (depth = 0; depth < 3; depth++) {
3546 if (depth >= path->depth)
3547 return -EINVAL;
3548 i = path->depth - depth - 1;
3549 nid = path->path[i];
3550 if (!path->ctls[NID_PATH_VOL_CTL]) {
3551 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3552 path->ctls[NID_PATH_VOL_CTL] =
3553 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3554 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3555 int idx = path->idx[i];
3556 if (!depth && codec->single_adc_amp)
3557 idx = 0;
3558 path->ctls[NID_PATH_VOL_CTL] =
3559 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3560 }
3561 }
3562 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3563 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3564 path->ctls[NID_PATH_MUTE_CTL] =
3565 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3566 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3567 int idx = path->idx[i];
3568 if (!depth && codec->single_adc_amp)
3569 idx = 0;
3570 path->ctls[NID_PATH_MUTE_CTL] =
3571 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3572 }
3573 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 return 0;
3576}
3577
Takashi Iwai352f7f92012-12-19 12:52:06 +01003578static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003580 struct hda_gen_spec *spec = codec->spec;
3581 struct auto_pin_cfg *cfg = &spec->autocfg;
3582 unsigned int val;
3583 int i;
3584
3585 if (!spec->inv_dmic_split)
3586 return false;
3587 for (i = 0; i < cfg->num_inputs; i++) {
3588 if (cfg->inputs[i].pin != nid)
3589 continue;
3590 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3591 return false;
3592 val = snd_hda_codec_get_pincfg(codec, nid);
3593 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3594 }
3595 return false;
3596}
3597
Takashi Iwaia90229e2013-01-18 14:10:00 +01003598/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003599static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3600 struct snd_ctl_elem_value *ucontrol)
3601{
3602 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3603 struct hda_gen_spec *spec = codec->spec;
3604 int ret;
3605
3606 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3607 if (ret < 0)
3608 return ret;
3609
Takashi Iwaia90229e2013-01-18 14:10:00 +01003610 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003611 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003612
3613 return ret;
3614}
3615
Takashi Iwai352f7f92012-12-19 12:52:06 +01003616static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3617 int idx, bool is_switch, unsigned int ctl,
3618 bool inv_dmic)
3619{
3620 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003621 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003622 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3623 const char *sfx = is_switch ? "Switch" : "Volume";
3624 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003625 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003626
3627 if (!ctl)
3628 return 0;
3629
3630 if (label)
3631 snprintf(tmpname, sizeof(tmpname),
3632 "%s Capture %s", label, sfx);
3633 else
3634 snprintf(tmpname, sizeof(tmpname),
3635 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003636 knew = add_control(spec, type, tmpname, idx,
3637 amp_val_replace_channels(ctl, chs));
3638 if (!knew)
3639 return -ENOMEM;
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01003640 if (is_switch) {
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003641 knew->put = cap_single_sw_put;
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01003642 if (spec->mic_mute_led)
3643 knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED;
3644 }
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003645 if (!inv_dmic)
3646 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003647
3648 /* Make independent right kcontrol */
3649 if (label)
3650 snprintf(tmpname, sizeof(tmpname),
3651 "Inverted %s Capture %s", label, sfx);
3652 else
3653 snprintf(tmpname, sizeof(tmpname),
3654 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003655 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003656 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003657 if (!knew)
3658 return -ENOMEM;
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01003659 if (is_switch) {
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003660 knew->put = cap_single_sw_put;
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01003661 if (spec->mic_mute_led)
3662 knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED;
3663 }
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003664 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003665}
3666
3667/* create single (and simple) capture volume and switch controls */
3668static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3669 unsigned int vol_ctl, unsigned int sw_ctl,
3670 bool inv_dmic)
3671{
3672 int err;
3673 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3674 if (err < 0)
3675 return err;
3676 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3677 if (err < 0)
3678 return err;
3679 return 0;
3680}
3681
3682/* create bound capture volume and switch controls */
3683static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3684 unsigned int vol_ctl, unsigned int sw_ctl)
3685{
3686 struct hda_gen_spec *spec = codec->spec;
3687 struct snd_kcontrol_new *knew;
3688
3689 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003690 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003691 if (!knew)
3692 return -ENOMEM;
3693 knew->index = idx;
3694 knew->private_value = vol_ctl;
3695 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3696 }
3697 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003698 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003699 if (!knew)
3700 return -ENOMEM;
3701 knew->index = idx;
3702 knew->private_value = sw_ctl;
3703 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01003704 if (spec->mic_mute_led)
3705 knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003706 }
3707 return 0;
3708}
3709
3710/* return the vol ctl when used first in the imux list */
3711static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3712{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003713 struct nid_path *path;
3714 unsigned int ctl;
3715 int i;
3716
Takashi Iwaic697b712013-01-07 17:09:26 +01003717 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003718 if (!path)
3719 return 0;
3720 ctl = path->ctls[type];
3721 if (!ctl)
3722 return 0;
3723 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003724 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003725 if (path && path->ctls[type] == ctl)
3726 return 0;
3727 }
3728 return ctl;
3729}
3730
3731/* create individual capture volume and switch controls per input */
3732static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3733{
3734 struct hda_gen_spec *spec = codec->spec;
3735 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003736 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003737
3738 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003739 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003740 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003741
Takashi Iwaic9700422013-01-18 10:17:30 +01003742 idx = imux->items[i].index;
3743 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003744 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003745 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3746
3747 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003748 err = add_single_cap_ctl(codec,
3749 spec->input_labels[idx],
3750 spec->input_label_idxs[idx],
3751 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003752 get_first_cap_ctl(codec, i, type),
3753 inv_dmic);
3754 if (err < 0)
3755 return err;
3756 }
3757 }
3758 return 0;
3759}
3760
3761static int create_capture_mixers(struct hda_codec *codec)
3762{
3763 struct hda_gen_spec *spec = codec->spec;
3764 struct hda_input_mux *imux = &spec->input_mux;
3765 int i, n, nums, err;
3766
3767 if (spec->dyn_adc_switch)
3768 nums = 1;
3769 else
3770 nums = spec->num_adc_nids;
3771
3772 if (!spec->auto_mic && imux->num_items > 1) {
3773 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003774 const char *name;
3775 name = nums > 1 ? "Input Source" : "Capture Source";
3776 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003777 if (!knew)
3778 return -ENOMEM;
3779 knew->count = nums;
3780 }
3781
3782 for (n = 0; n < nums; n++) {
3783 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003784 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003785 bool inv_dmic = false;
3786 int vol, sw;
3787
3788 vol = sw = 0;
3789 for (i = 0; i < imux->num_items; i++) {
3790 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003791 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003792 if (!path)
3793 continue;
3794 parse_capvol_in_path(codec, path);
3795 if (!vol)
3796 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003797 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003798 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003799 if (!same_amp_caps(codec, vol,
3800 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3801 multi_cap_vol = true;
3802 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003803 if (!sw)
3804 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003805 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003806 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003807 if (!same_amp_caps(codec, sw,
3808 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3809 multi_cap_vol = true;
3810 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003811 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3812 inv_dmic = true;
3813 }
3814
3815 if (!multi)
3816 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3817 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003818 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003819 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3820 else
3821 err = create_multi_cap_vol_ctl(codec);
3822 if (err < 0)
3823 return err;
3824 }
3825
3826 return 0;
3827}
3828
3829/*
3830 * add mic boosts if needed
3831 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003832
3833/* check whether the given amp is feasible as a boost volume */
3834static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3835 int dir, int idx)
3836{
3837 unsigned int step;
3838
3839 if (!nid_has_volume(codec, nid, dir) ||
3840 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3841 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3842 return false;
3843
3844 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3845 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3846 if (step < 0x20)
3847 return false;
3848 return true;
3849}
3850
3851/* look for a boost amp in a widget close to the pin */
3852static unsigned int look_for_boost_amp(struct hda_codec *codec,
3853 struct nid_path *path)
3854{
3855 unsigned int val = 0;
3856 hda_nid_t nid;
3857 int depth;
3858
3859 for (depth = 0; depth < 3; depth++) {
3860 if (depth >= path->depth - 1)
3861 break;
3862 nid = path->path[depth];
3863 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3864 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3865 break;
3866 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3867 path->idx[depth])) {
3868 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3869 HDA_INPUT);
3870 break;
3871 }
3872 }
3873
3874 return val;
3875}
3876
Takashi Iwai352f7f92012-12-19 12:52:06 +01003877static int parse_mic_boost(struct hda_codec *codec)
3878{
3879 struct hda_gen_spec *spec = codec->spec;
3880 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003881 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003882 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003883
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003884 if (!spec->num_adc_nids)
3885 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003886
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003887 for (i = 0; i < imux->num_items; i++) {
3888 struct nid_path *path;
3889 unsigned int val;
3890 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003891 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003892
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003893 idx = imux->items[i].index;
3894 if (idx >= imux->num_items)
3895 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003896
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003897 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003898 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003899 continue;
3900
3901 path = get_input_path(codec, 0, i);
3902 if (!path)
3903 continue;
3904
3905 val = look_for_boost_amp(codec, path);
3906 if (!val)
3907 continue;
3908
3909 /* create a boost control */
3910 snprintf(boost_label, sizeof(boost_label),
3911 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003912 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3913 spec->input_label_idxs[idx], val))
3914 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003915
3916 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003917 }
3918 return 0;
3919}
3920
Takashi Iwai7cdf8c42020-06-18 13:08:31 +02003921#ifdef CONFIG_SND_HDA_GENERIC_LEDS
Takashi Iwai352f7f92012-12-19 12:52:06 +01003922/*
Takashi Iwai15509b62020-06-18 13:08:37 +02003923 * vmaster mute LED hook helpers
3924 */
3925
3926static int create_mute_led_cdev(struct hda_codec *codec,
3927 int (*callback)(struct led_classdev *,
3928 enum led_brightness),
3929 bool micmute)
3930{
Takashi Iwai549f8ff2022-01-26 15:50:11 +01003931 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai15509b62020-06-18 13:08:37 +02003932 struct led_classdev *cdev;
Takashi Iwai549f8ff2022-01-26 15:50:11 +01003933 int idx = micmute ? LED_AUDIO_MICMUTE : LED_AUDIO_MUTE;
3934 int err;
Takashi Iwai15509b62020-06-18 13:08:37 +02003935
3936 cdev = devm_kzalloc(&codec->core.dev, sizeof(*cdev), GFP_KERNEL);
3937 if (!cdev)
3938 return -ENOMEM;
3939
3940 cdev->name = micmute ? "hda::micmute" : "hda::mute";
3941 cdev->max_brightness = 1;
3942 cdev->default_trigger = micmute ? "audio-micmute" : "audio-mute";
3943 cdev->brightness_set_blocking = callback;
Takashi Iwai549f8ff2022-01-26 15:50:11 +01003944 cdev->brightness = ledtrig_audio_get(idx);
Takashi Iwaic9e272f2020-06-18 13:08:42 +02003945 cdev->flags = LED_CORE_SUSPENDRESUME;
Takashi Iwai15509b62020-06-18 13:08:37 +02003946
Takashi Iwai549f8ff2022-01-26 15:50:11 +01003947 err = led_classdev_register(&codec->core.dev, cdev);
3948 if (err < 0)
3949 return err;
3950 spec->led_cdevs[idx] = cdev;
3951 return 0;
Takashi Iwai15509b62020-06-18 13:08:37 +02003952}
3953
Takashi Iwai15509b62020-06-18 13:08:37 +02003954/**
Pierre-Louis Bossart3531ba22021-03-01 11:46:17 -06003955 * snd_hda_gen_add_mute_led_cdev - Create a LED classdev and enable as vmaster mute LED
Takashi Iwai15509b62020-06-18 13:08:37 +02003956 * @codec: the HDA codec
3957 * @callback: the callback for LED classdev brightness_set_blocking
3958 */
3959int snd_hda_gen_add_mute_led_cdev(struct hda_codec *codec,
3960 int (*callback)(struct led_classdev *,
3961 enum led_brightness))
3962{
3963 struct hda_gen_spec *spec = codec->spec;
3964 int err;
3965
3966 if (callback) {
3967 err = create_mute_led_cdev(codec, callback, false);
3968 if (err) {
3969 codec_warn(codec, "failed to create a mute LED cdev\n");
3970 return err;
3971 }
3972 }
3973
3974 if (spec->vmaster_mute.hook)
3975 codec_err(codec, "vmaster hook already present before cdev!\n");
3976
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01003977 spec->vmaster_mute_led = 1;
Takashi Iwai15509b62020-06-18 13:08:37 +02003978 return 0;
3979}
3980EXPORT_SYMBOL_GPL(snd_hda_gen_add_mute_led_cdev);
3981
Takashi Iwaib3802782018-11-26 17:47:46 +01003982/**
Pierre-Louis Bossart3531ba22021-03-01 11:46:17 -06003983 * snd_hda_gen_add_micmute_led_cdev - Create a LED classdev and enable as mic-mute LED
Takashi Iwai7cdf8c42020-06-18 13:08:31 +02003984 * @codec: the HDA codec
3985 * @callback: the callback for LED classdev brightness_set_blocking
3986 *
3987 * Called from the codec drivers for offering the mic mute LED controls.
3988 * This creates a LED classdev and sets up the cap_sync_hook that is called at
3989 * each time when the capture mixer switch changes.
3990 *
3991 * When NULL is passed to @callback, no classdev is created but only the
3992 * LED-trigger is set up.
3993 *
3994 * Returns 0 or a negative error.
3995 */
3996int snd_hda_gen_add_micmute_led_cdev(struct hda_codec *codec,
3997 int (*callback)(struct led_classdev *,
3998 enum led_brightness))
3999{
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01004000 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai7cdf8c42020-06-18 13:08:31 +02004001 int err;
Takashi Iwai7cdf8c42020-06-18 13:08:31 +02004002
4003 if (callback) {
Takashi Iwai15509b62020-06-18 13:08:37 +02004004 err = create_mute_led_cdev(codec, callback, true);
Takashi Iwai7cdf8c42020-06-18 13:08:31 +02004005 if (err) {
4006 codec_warn(codec, "failed to create a mic-mute LED cdev\n");
4007 return err;
4008 }
4009 }
4010
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01004011 spec->mic_mute_led = 1;
4012 return 0;
Takashi Iwai7cdf8c42020-06-18 13:08:31 +02004013}
4014EXPORT_SYMBOL_GPL(snd_hda_gen_add_micmute_led_cdev);
4015#endif /* CONFIG_SND_HDA_GENERIC_LEDS */
4016
Takashi Iwaif567b782018-06-18 17:26:12 +02004017/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004018 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
4019 */
4020static void parse_digital(struct hda_codec *codec)
4021{
4022 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01004023 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004024 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004025 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004026
Takashi Iwai9ab0cb32020-07-17 17:45:17 +02004027 /* support multiple SPDIFs; the secondary is set up as a follower */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004028 nums = 0;
4029 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01004030 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01004031 dig_nid = look_for_dac(codec, pin, true);
4032 if (!dig_nid)
4033 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01004034 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01004035 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004036 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004037 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004038 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01004039 path->pin_fixed = true; /* no jack detection */
Takashi Iwai196c17662013-01-04 15:01:40 +01004040 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01004041 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004042 if (!nums) {
4043 spec->multiout.dig_out_nid = dig_nid;
4044 spec->dig_out_type = spec->autocfg.dig_out_type[0];
4045 } else {
Takashi Iwai9ab0cb32020-07-17 17:45:17 +02004046 spec->multiout.follower_dig_outs = spec->follower_dig_outs;
4047 if (nums >= ARRAY_SIZE(spec->follower_dig_outs) - 1)
Dan Carpenterd576422e2014-05-14 17:18:31 +03004048 break;
Takashi Iwai9ab0cb32020-07-17 17:45:17 +02004049 spec->follower_dig_outs[nums - 1] = dig_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004050 }
4051 nums++;
4052 }
4053
4054 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01004055 pin = spec->autocfg.dig_in_pin;
Takashi Iwai7639a062015-03-03 10:07:24 +01004056 for_each_hda_codec_node(dig_nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004057 unsigned int wcaps = get_wcaps(codec, dig_nid);
4058 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
4059 continue;
4060 if (!(wcaps & AC_WCAP_DIGITAL))
4061 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004062 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004063 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01004064 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004065 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01004066 path->pin_fixed = true; /* no jack */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004067 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004068 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01004069 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004070 break;
4071 }
4072 }
4073 }
4074}
4075
4076
4077/*
4078 * input MUX handling
4079 */
4080
4081static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
4082
4083/* select the given imux item; either unmute exclusively or select the route */
4084static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
4085 unsigned int idx)
4086{
4087 struct hda_gen_spec *spec = codec->spec;
4088 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01004089 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004090
4091 imux = &spec->input_mux;
4092 if (!imux->num_items)
4093 return 0;
4094
4095 if (idx >= imux->num_items)
4096 idx = imux->num_items - 1;
4097 if (spec->cur_mux[adc_idx] == idx)
4098 return 0;
4099
Takashi Iwai55196ff2013-01-24 17:32:56 +01004100 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
4101 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004102 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01004103 if (old_path->active)
4104 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004105
4106 spec->cur_mux[adc_idx] = idx;
4107
Takashi Iwai967303d2013-02-19 17:12:42 +01004108 if (spec->hp_mic)
4109 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004110
4111 if (spec->dyn_adc_switch)
4112 dyn_adc_pcm_resetup(codec, idx);
4113
Takashi Iwaic697b712013-01-07 17:09:26 +01004114 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004115 if (!path)
4116 return 0;
4117 if (path->active)
4118 return 0;
4119 snd_hda_activate_path(codec, path, true, false);
4120 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01004121 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004122 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004123 return 1;
4124}
4125
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004126/* power up/down widgets in the all paths that match with the given NID
4127 * as terminals (either start- or endpoint)
4128 *
4129 * returns the last changed NID, or zero if unchanged.
4130 */
4131static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
4132 int pin_state, int stream_state)
4133{
4134 struct hda_gen_spec *spec = codec->spec;
4135 hda_nid_t last, changed = 0;
4136 struct nid_path *path;
4137 int n;
4138
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +02004139 snd_array_for_each(&spec->paths, n, path) {
Bob Copeland81e43962016-06-25 07:58:45 -04004140 if (!path->depth)
4141 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004142 if (path->path[0] == nid ||
4143 path->path[path->depth - 1] == nid) {
4144 bool pin_old = path->pin_enabled;
4145 bool stream_old = path->stream_enabled;
4146
4147 if (pin_state >= 0)
4148 path->pin_enabled = pin_state;
4149 if (stream_state >= 0)
4150 path->stream_enabled = stream_state;
Takashi Iwai6b275b12015-03-20 18:11:05 +01004151 if ((!path->pin_fixed && path->pin_enabled != pin_old)
4152 || path->stream_enabled != stream_old) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004153 last = path_power_update(codec, path, true);
4154 if (last)
4155 changed = last;
4156 }
4157 }
4158 }
4159 return changed;
4160}
4161
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004162/* check the jack status for power control */
4163static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4164{
4165 if (!is_jack_detectable(codec, pin))
4166 return true;
4167 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4168}
4169
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004170/* power up/down the paths of the given pin according to the jack state;
4171 * power = 0/1 : only power up/down if it matches with the jack state,
4172 * < 0 : force power up/down to follow the jack sate
4173 *
4174 * returns the last changed NID, or zero if unchanged.
4175 */
4176static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4177 int power)
4178{
4179 bool on;
4180
Takashi Iwai967b1302015-03-20 18:21:03 +01004181 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004182 return 0;
4183
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004184 on = detect_pin_state(codec, pin);
4185
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004186 if (power >= 0 && on != power)
4187 return 0;
4188 return set_path_power(codec, pin, on, -1);
4189}
4190
4191static void pin_power_callback(struct hda_codec *codec,
4192 struct hda_jack_callback *jack,
4193 bool on)
4194{
Takashi Iwai2ebab402016-02-09 10:23:52 +01004195 if (jack && jack->nid)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004196 sync_power_state_change(codec,
Takashi Iwai2ebab402016-02-09 10:23:52 +01004197 set_pin_power_jack(codec, jack->nid, on));
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004198}
4199
4200/* callback only doing power up -- called at first */
4201static void pin_power_up_callback(struct hda_codec *codec,
4202 struct hda_jack_callback *jack)
4203{
4204 pin_power_callback(codec, jack, true);
4205}
4206
4207/* callback only doing power down -- called at last */
4208static void pin_power_down_callback(struct hda_codec *codec,
4209 struct hda_jack_callback *jack)
4210{
4211 pin_power_callback(codec, jack, false);
4212}
4213
4214/* set up the power up/down callbacks */
4215static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4216 const hda_nid_t *pins, bool on)
4217{
4218 int i;
4219 hda_jack_callback_fn cb =
4220 on ? pin_power_up_callback : pin_power_down_callback;
4221
4222 for (i = 0; i < num_pins && pins[i]; i++) {
4223 if (is_jack_detectable(codec, pins[i]))
4224 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4225 else
4226 set_path_power(codec, pins[i], true, -1);
4227 }
4228}
4229
4230/* enabled power callback to each available I/O pin with jack detections;
4231 * the digital I/O pins are excluded because of the unreliable detectsion
4232 */
4233static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4234{
4235 struct hda_gen_spec *spec = codec->spec;
4236 struct auto_pin_cfg *cfg = &spec->autocfg;
4237 int i;
4238
Takashi Iwai967b1302015-03-20 18:21:03 +01004239 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004240 return;
4241 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4242 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4243 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4244 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4245 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4246 for (i = 0; i < cfg->num_inputs; i++)
4247 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4248}
4249
4250/* sync path power up/down with the jack states of given pins */
4251static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4252 const hda_nid_t *pins)
4253{
4254 int i;
4255
4256 for (i = 0; i < num_pins && pins[i]; i++)
4257 if (is_jack_detectable(codec, pins[i]))
4258 set_pin_power_jack(codec, pins[i], -1);
4259}
4260
4261/* sync path power up/down with pins; called at init and resume */
4262static void sync_all_pin_power_ctls(struct hda_codec *codec)
4263{
4264 struct hda_gen_spec *spec = codec->spec;
4265 struct auto_pin_cfg *cfg = &spec->autocfg;
4266 int i;
4267
Takashi Iwai967b1302015-03-20 18:21:03 +01004268 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004269 return;
4270 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4271 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4272 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4273 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4274 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4275 for (i = 0; i < cfg->num_inputs; i++)
4276 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4277}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004278
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004279/* add fake paths if not present yet */
4280static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4281 int num_pins, const hda_nid_t *pins)
4282{
4283 struct hda_gen_spec *spec = codec->spec;
4284 struct nid_path *path;
4285 int i;
4286
4287 for (i = 0; i < num_pins; i++) {
4288 if (!pins[i])
4289 break;
4290 if (get_nid_path(codec, nid, pins[i], 0))
4291 continue;
4292 path = snd_array_new(&spec->paths);
4293 if (!path)
4294 return -ENOMEM;
4295 memset(path, 0, sizeof(*path));
4296 path->depth = 2;
4297 path->path[0] = nid;
4298 path->path[1] = pins[i];
4299 path->active = true;
4300 }
4301 return 0;
4302}
4303
4304/* create fake paths to all outputs from beep */
4305static int add_fake_beep_paths(struct hda_codec *codec)
4306{
4307 struct hda_gen_spec *spec = codec->spec;
4308 struct auto_pin_cfg *cfg = &spec->autocfg;
4309 hda_nid_t nid = spec->beep_nid;
4310 int err;
4311
Takashi Iwai967b1302015-03-20 18:21:03 +01004312 if (!codec->power_save_node || !nid)
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004313 return 0;
4314 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4315 if (err < 0)
4316 return err;
4317 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4318 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4319 if (err < 0)
4320 return err;
4321 }
4322 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4323 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4324 cfg->speaker_pins);
4325 if (err < 0)
4326 return err;
4327 }
4328 return 0;
4329}
4330
4331/* power up/down beep widget and its output paths */
4332static void beep_power_hook(struct hda_beep *beep, bool on)
4333{
4334 set_path_power(beep->codec, beep->nid, -1, on);
4335}
4336
Takashi Iwai6b275b12015-03-20 18:11:05 +01004337/**
4338 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4339 * @codec: the HDA codec
4340 * @pin: NID of pin to fix
4341 */
4342int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4343{
4344 struct hda_gen_spec *spec = codec->spec;
4345 struct nid_path *path;
4346
4347 path = snd_array_new(&spec->paths);
4348 if (!path)
4349 return -ENOMEM;
4350 memset(path, 0, sizeof(*path));
4351 path->depth = 1;
4352 path->path[0] = pin;
4353 path->active = true;
4354 path->pin_fixed = true;
4355 path->stream_enabled = true;
4356 return 0;
4357}
4358EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4359
Takashi Iwai352f7f92012-12-19 12:52:06 +01004360/*
4361 * Jack detections for HP auto-mute and mic-switch
4362 */
4363
4364/* check each pin in the given array; returns true if any of them is plugged */
Michał Mirosławcaf3c042020-01-03 10:23:48 +01004365static bool detect_jacks(struct hda_codec *codec, int num_pins, const hda_nid_t *pins)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004366{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004367 int i;
4368 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004369
4370 for (i = 0; i < num_pins; i++) {
4371 hda_nid_t nid = pins[i];
4372 if (!nid)
4373 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01004374 /* don't detect pins retasked as inputs */
4375 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4376 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004377 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4378 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004379 }
4380 return present;
4381}
4382
4383/* standard HP/line-out auto-mute helper */
Michał Mirosławcaf3c042020-01-03 10:23:48 +01004384static void do_automute(struct hda_codec *codec, int num_pins, const hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004385 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004386{
4387 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004388 int i;
4389
4390 for (i = 0; i < num_pins; i++) {
4391 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01004392 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004393 if (!nid)
4394 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004395
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004396 oldval = snd_hda_codec_get_pin_target(codec, nid);
4397 if (oldval & PIN_IN)
4398 continue; /* no mute for inputs */
4399
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004400 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004401 struct nid_path *path;
4402 hda_nid_t mute_nid;
4403
4404 path = snd_hda_get_path_from_idx(codec, paths[i]);
4405 if (!path)
4406 continue;
4407 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4408 if (!mute_nid)
4409 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004410 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004411 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004412 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004413 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004414 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004415 } else {
4416 /* don't reset VREF value in case it's controlling
4417 * the amp (see alc861_fixup_asus_amp_vref_0f())
4418 */
4419 if (spec->keep_vref_in_automute)
4420 val = oldval & ~PIN_HP;
4421 else
4422 val = 0;
4423 if (!mute)
4424 val |= oldval;
4425 /* here we call update_pin_ctl() so that the pinctl is
4426 * changed without changing the pinctl target value;
4427 * the original target value will be still referred at
4428 * the init / resume again
4429 */
4430 update_pin_ctl(codec, nid, val);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004431 }
4432
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01004433 set_pin_eapd(codec, nid, !mute);
Takashi Iwai967b1302015-03-20 18:21:03 +01004434 if (codec->power_save_node) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004435 bool on = !mute;
4436 if (on)
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004437 on = detect_pin_state(codec, nid);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004438 set_path_power(codec, nid, on, -1);
4439 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004440 }
4441}
4442
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004443/**
4444 * snd_hda_gen_update_outputs - Toggle outputs muting
4445 * @codec: the HDA codec
4446 *
4447 * Update the mute status of all outputs based on the current jack states.
4448 */
Takashi Iwai5d550e12012-12-19 15:16:44 +01004449void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004450{
4451 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004452 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004453 int on;
4454
4455 /* Control HP pins/amps depending on master_mute state;
4456 * in general, HP pins/amps control should be enabled in all cases,
4457 * but currently set only for master_mute, just to be safe
4458 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004459 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4460 paths = spec->out_paths;
4461 else
4462 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01004463 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004464 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004465
4466 if (!spec->automute_speaker)
4467 on = 0;
4468 else
4469 on = spec->hp_jack_present | spec->line_jack_present;
4470 on |= spec->master_mute;
Takashi Iwai47b9ddb82013-01-16 18:18:00 +01004471 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004472 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4473 paths = spec->out_paths;
4474 else
4475 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004476 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004477 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004478
4479 /* toggle line-out mutes if needed, too */
4480 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4481 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4482 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4483 return;
4484 if (!spec->automute_lo)
4485 on = 0;
4486 else
4487 on = spec->hp_jack_present;
4488 on |= spec->master_mute;
Takashi Iwai47b9ddb82013-01-16 18:18:00 +01004489 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004490 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004491 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004492 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004493}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004494EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004495
4496static void call_update_outputs(struct hda_codec *codec)
4497{
4498 struct hda_gen_spec *spec = codec->spec;
4499 if (spec->automute_hook)
4500 spec->automute_hook(codec);
4501 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01004502 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004503
Takashi Iwai9ab0cb32020-07-17 17:45:17 +02004504 /* sync the whole vmaster followers to reflect the new auto-mute status */
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004505 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4506 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004507}
4508
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004509/**
4510 * snd_hda_gen_hp_automute - standard HP-automute helper
4511 * @codec: the HDA codec
4512 * @jack: jack object, NULL for the whole
4513 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004514void snd_hda_gen_hp_automute(struct hda_codec *codec,
4515 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004516{
4517 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01004518 hda_nid_t *pins = spec->autocfg.hp_pins;
4519 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004520
Takashi Iwai92603c52013-01-22 07:46:31 +01004521 /* No detection for the first HP jack during indep-HP mode */
4522 if (spec->indep_hp_enabled) {
4523 pins++;
4524 num_pins--;
4525 }
4526
4527 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004528 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4529 return;
4530 call_update_outputs(codec);
4531}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004532EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004533
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004534/**
4535 * snd_hda_gen_line_automute - standard line-out-automute helper
4536 * @codec: the HDA codec
4537 * @jack: jack object, NULL for the whole
4538 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004539void snd_hda_gen_line_automute(struct hda_codec *codec,
4540 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004541{
4542 struct hda_gen_spec *spec = codec->spec;
4543
4544 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4545 return;
4546 /* check LO jack only when it's different from HP */
4547 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4548 return;
4549
4550 spec->line_jack_present =
4551 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4552 spec->autocfg.line_out_pins);
4553 if (!spec->automute_speaker || !spec->detect_lo)
4554 return;
4555 call_update_outputs(codec);
4556}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004557EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004558
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004559/**
4560 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4561 * @codec: the HDA codec
4562 * @jack: jack object, NULL for the whole
4563 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004564void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4565 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004566{
4567 struct hda_gen_spec *spec = codec->spec;
4568 int i;
4569
4570 if (!spec->auto_mic)
4571 return;
4572
4573 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01004574 hda_nid_t pin = spec->am_entry[i].pin;
4575 /* don't detect pins retasked as outputs */
4576 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4577 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004578 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004579 mux_select(codec, 0, spec->am_entry[i].idx);
4580 return;
4581 }
4582 }
4583 mux_select(codec, 0, spec->am_entry[0].idx);
4584}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004585EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004586
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004587/* call appropriate hooks */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004588static void call_hp_automute(struct hda_codec *codec,
4589 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004590{
4591 struct hda_gen_spec *spec = codec->spec;
4592 if (spec->hp_automute_hook)
4593 spec->hp_automute_hook(codec, jack);
4594 else
4595 snd_hda_gen_hp_automute(codec, jack);
4596}
4597
4598static void call_line_automute(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004599 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004600{
4601 struct hda_gen_spec *spec = codec->spec;
4602 if (spec->line_automute_hook)
4603 spec->line_automute_hook(codec, jack);
4604 else
4605 snd_hda_gen_line_automute(codec, jack);
4606}
4607
4608static void call_mic_autoswitch(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004609 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004610{
4611 struct hda_gen_spec *spec = codec->spec;
4612 if (spec->mic_autoswitch_hook)
4613 spec->mic_autoswitch_hook(codec, jack);
4614 else
4615 snd_hda_gen_mic_autoswitch(codec, jack);
4616}
4617
Takashi Iwai963afde2013-05-31 15:20:31 +02004618/* update jack retasking */
4619static void update_automute_all(struct hda_codec *codec)
4620{
4621 call_hp_automute(codec, NULL);
4622 call_line_automute(codec, NULL);
4623 call_mic_autoswitch(codec, NULL);
4624}
4625
Takashi Iwai352f7f92012-12-19 12:52:06 +01004626/*
4627 * Auto-Mute mode mixer enum support
4628 */
4629static int automute_mode_info(struct snd_kcontrol *kcontrol,
4630 struct snd_ctl_elem_info *uinfo)
4631{
4632 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4633 struct hda_gen_spec *spec = codec->spec;
4634 static const char * const texts3[] = {
4635 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004636 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
Takashi Iwai352f7f92012-12-19 12:52:06 +01004638 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4639 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4640 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4641}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
Takashi Iwai352f7f92012-12-19 12:52:06 +01004643static int automute_mode_get(struct snd_kcontrol *kcontrol,
4644 struct snd_ctl_elem_value *ucontrol)
4645{
4646 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4647 struct hda_gen_spec *spec = codec->spec;
4648 unsigned int val = 0;
4649 if (spec->automute_speaker)
4650 val++;
4651 if (spec->automute_lo)
4652 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004653
Takashi Iwai352f7f92012-12-19 12:52:06 +01004654 ucontrol->value.enumerated.item[0] = val;
4655 return 0;
4656}
4657
4658static int automute_mode_put(struct snd_kcontrol *kcontrol,
4659 struct snd_ctl_elem_value *ucontrol)
4660{
4661 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4662 struct hda_gen_spec *spec = codec->spec;
4663
4664 switch (ucontrol->value.enumerated.item[0]) {
4665 case 0:
4666 if (!spec->automute_speaker && !spec->automute_lo)
4667 return 0;
4668 spec->automute_speaker = 0;
4669 spec->automute_lo = 0;
4670 break;
4671 case 1:
4672 if (spec->automute_speaker_possible) {
4673 if (!spec->automute_lo && spec->automute_speaker)
4674 return 0;
4675 spec->automute_speaker = 1;
4676 spec->automute_lo = 0;
4677 } else if (spec->automute_lo_possible) {
4678 if (spec->automute_lo)
4679 return 0;
4680 spec->automute_lo = 1;
4681 } else
4682 return -EINVAL;
4683 break;
4684 case 2:
4685 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4686 return -EINVAL;
4687 if (spec->automute_speaker && spec->automute_lo)
4688 return 0;
4689 spec->automute_speaker = 1;
4690 spec->automute_lo = 1;
4691 break;
4692 default:
4693 return -EINVAL;
4694 }
4695 call_update_outputs(codec);
4696 return 1;
4697}
4698
4699static const struct snd_kcontrol_new automute_mode_enum = {
4700 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4701 .name = "Auto-Mute Mode",
4702 .info = automute_mode_info,
4703 .get = automute_mode_get,
4704 .put = automute_mode_put,
4705};
4706
4707static int add_automute_mode_enum(struct hda_codec *codec)
4708{
4709 struct hda_gen_spec *spec = codec->spec;
4710
Takashi Iwai12c93df2012-12-19 14:38:33 +01004711 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004712 return -ENOMEM;
4713 return 0;
4714}
4715
4716/*
4717 * Check the availability of HP/line-out auto-mute;
4718 * Set up appropriately if really supported
4719 */
4720static int check_auto_mute_availability(struct hda_codec *codec)
4721{
4722 struct hda_gen_spec *spec = codec->spec;
4723 struct auto_pin_cfg *cfg = &spec->autocfg;
4724 int present = 0;
4725 int i, err;
4726
Takashi Iwaif72706b2013-01-16 18:20:07 +01004727 if (spec->suppress_auto_mute)
4728 return 0;
4729
Takashi Iwai352f7f92012-12-19 12:52:06 +01004730 if (cfg->hp_pins[0])
4731 present++;
4732 if (cfg->line_out_pins[0])
4733 present++;
4734 if (cfg->speaker_pins[0])
4735 present++;
4736 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004737 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004738
4739 if (!cfg->speaker_pins[0] &&
4740 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4741 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4742 sizeof(cfg->speaker_pins));
4743 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745
Takashi Iwai352f7f92012-12-19 12:52:06 +01004746 if (!cfg->hp_pins[0] &&
4747 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4748 memcpy(cfg->hp_pins, cfg->line_out_pins,
4749 sizeof(cfg->hp_pins));
4750 cfg->hp_outs = cfg->line_outs;
4751 }
4752
4753 for (i = 0; i < cfg->hp_outs; i++) {
4754 hda_nid_t nid = cfg->hp_pins[i];
4755 if (!is_jack_detectable(codec, nid))
4756 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004757 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai62f949b2014-09-11 14:06:53 +02004758 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004759 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004760 spec->detect_hp = 1;
4761 }
4762
4763 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4764 if (cfg->speaker_outs)
4765 for (i = 0; i < cfg->line_outs; i++) {
4766 hda_nid_t nid = cfg->line_out_pins[i];
4767 if (!is_jack_detectable(codec, nid))
4768 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004769 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004770 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004771 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004772 spec->detect_lo = 1;
4773 }
4774 spec->automute_lo_possible = spec->detect_hp;
4775 }
4776
4777 spec->automute_speaker_possible = cfg->speaker_outs &&
4778 (spec->detect_hp || spec->detect_lo);
4779
4780 spec->automute_lo = spec->automute_lo_possible;
4781 spec->automute_speaker = spec->automute_speaker_possible;
4782
4783 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4784 /* create a control for automute mode */
4785 err = add_automute_mode_enum(codec);
4786 if (err < 0)
4787 return err;
4788 }
4789 return 0;
4790}
4791
Takashi Iwai352f7f92012-12-19 12:52:06 +01004792/* check whether all auto-mic pins are valid; setup indices if OK */
4793static bool auto_mic_check_imux(struct hda_codec *codec)
4794{
4795 struct hda_gen_spec *spec = codec->spec;
4796 const struct hda_input_mux *imux;
4797 int i;
4798
4799 imux = &spec->input_mux;
4800 for (i = 0; i < spec->am_num_entries; i++) {
4801 spec->am_entry[i].idx =
4802 find_idx_in_nid_list(spec->am_entry[i].pin,
4803 spec->imux_pins, imux->num_items);
4804 if (spec->am_entry[i].idx < 0)
4805 return false; /* no corresponding imux */
4806 }
4807
4808 /* we don't need the jack detection for the first pin */
4809 for (i = 1; i < spec->am_num_entries; i++)
4810 snd_hda_jack_detect_enable_callback(codec,
4811 spec->am_entry[i].pin,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004812 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004813 return true;
4814}
4815
4816static int compare_attr(const void *ap, const void *bp)
4817{
4818 const struct automic_entry *a = ap;
4819 const struct automic_entry *b = bp;
4820 return (int)(a->attr - b->attr);
4821}
4822
4823/*
4824 * Check the availability of auto-mic switch;
4825 * Set up if really supported
4826 */
4827static int check_auto_mic_availability(struct hda_codec *codec)
4828{
4829 struct hda_gen_spec *spec = codec->spec;
4830 struct auto_pin_cfg *cfg = &spec->autocfg;
4831 unsigned int types;
4832 int i, num_pins;
4833
Takashi Iwaid12daf62013-01-07 16:32:11 +01004834 if (spec->suppress_auto_mic)
4835 return 0;
4836
Takashi Iwai352f7f92012-12-19 12:52:06 +01004837 types = 0;
4838 num_pins = 0;
4839 for (i = 0; i < cfg->num_inputs; i++) {
4840 hda_nid_t nid = cfg->inputs[i].pin;
4841 unsigned int attr;
4842 attr = snd_hda_codec_get_pincfg(codec, nid);
4843 attr = snd_hda_get_input_pin_attr(attr);
4844 if (types & (1 << attr))
4845 return 0; /* already occupied */
4846 switch (attr) {
4847 case INPUT_PIN_ATTR_INT:
4848 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4849 return 0; /* invalid type */
4850 break;
4851 case INPUT_PIN_ATTR_UNUSED:
4852 return 0; /* invalid entry */
4853 default:
4854 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4855 return 0; /* invalid type */
4856 if (!spec->line_in_auto_switch &&
4857 cfg->inputs[i].type != AUTO_PIN_MIC)
4858 return 0; /* only mic is allowed */
4859 if (!is_jack_detectable(codec, nid))
4860 return 0; /* no unsol support */
4861 break;
4862 }
4863 if (num_pins >= MAX_AUTO_MIC_PINS)
4864 return 0;
4865 types |= (1 << attr);
4866 spec->am_entry[num_pins].pin = nid;
4867 spec->am_entry[num_pins].attr = attr;
4868 num_pins++;
4869 }
4870
4871 if (num_pins < 2)
4872 return 0;
4873
4874 spec->am_num_entries = num_pins;
4875 /* sort the am_entry in the order of attr so that the pin with a
4876 * higher attr will be selected when the jack is plugged.
4877 */
4878 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4879 compare_attr, NULL);
4880
4881 if (!auto_mic_check_imux(codec))
4882 return 0;
4883
4884 spec->auto_mic = 1;
4885 spec->num_adc_nids = 1;
4886 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004887 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004888 spec->am_entry[0].pin,
4889 spec->am_entry[1].pin,
4890 spec->am_entry[2].pin);
4891
4892 return 0;
4893}
4894
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004895/**
4896 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4897 * into power down
4898 * @codec: the HDA codec
4899 * @nid: NID to evalute
4900 * @power_state: target power state
4901 */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004902unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004903 hda_nid_t nid,
4904 unsigned int power_state)
4905{
Takashi Iwaib6c09b32015-04-09 10:25:03 +02004906 struct hda_gen_spec *spec = codec->spec;
4907
4908 if (!spec->power_down_unused && !codec->power_save_node)
4909 return power_state;
Takashi Iwai7639a062015-03-03 10:07:24 +01004910 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004911 return power_state;
4912 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4913 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004914 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004915 return power_state;
4916 return AC_PWRST_D3;
4917}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004918EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004919
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004920/* mute all aamix inputs initially; parse up to the first leaves */
4921static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4922{
4923 int i, nums;
4924 const hda_nid_t *conn;
4925 bool has_amp;
4926
4927 nums = snd_hda_get_conn_list(codec, mix, &conn);
4928 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4929 for (i = 0; i < nums; i++) {
4930 if (has_amp)
Takashi Iwaief403ed2015-03-12 08:30:11 +01004931 update_amp(codec, mix, HDA_INPUT, i,
4932 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004933 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
Takashi Iwaief403ed2015-03-12 08:30:11 +01004934 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4935 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004936 }
4937}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004938
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004939/**
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004940 * snd_hda_gen_stream_pm - Stream power management callback
4941 * @codec: the HDA codec
4942 * @nid: audio widget
4943 * @on: power on/off flag
4944 *
Takashi Iwai967b1302015-03-20 18:21:03 +01004945 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004946 */
4947void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4948{
Takashi Iwai967b1302015-03-20 18:21:03 +01004949 if (codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004950 set_path_power(codec, nid, -1, on);
4951}
4952EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4953
4954/**
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004955 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4956 * set up the hda_gen_spec
4957 * @codec: the HDA codec
4958 * @cfg: Parsed pin configuration
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004959 *
4960 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004961 * or a negative error code
4962 */
4963int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004964 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004965{
4966 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004967 int err;
4968
Takashi Iwai1c70a582013-01-11 17:48:22 +01004969 parse_user_hints(codec);
4970
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01004971 if (spec->vmaster_mute_led || spec->mic_mute_led)
4972 snd_ctl_led_request();
4973
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004974 if (spec->mixer_nid && !spec->mixer_merge_nid)
4975 spec->mixer_merge_nid = spec->mixer_nid;
4976
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004977 if (cfg != &spec->autocfg) {
4978 spec->autocfg = *cfg;
4979 cfg = &spec->autocfg;
4980 }
4981
Takashi Iwai98bd1112013-03-22 14:53:50 +01004982 if (!spec->main_out_badness)
4983 spec->main_out_badness = &hda_main_out_badness;
4984 if (!spec->extra_out_badness)
4985 spec->extra_out_badness = &hda_extra_out_badness;
4986
David Henningsson6fc4cb92013-01-16 15:58:45 +01004987 fill_all_dac_nids(codec);
4988
Takashi Iwai352f7f92012-12-19 12:52:06 +01004989 if (!cfg->line_outs) {
4990 if (cfg->dig_outs || cfg->dig_in_pin) {
4991 spec->multiout.max_channels = 2;
4992 spec->no_analog = 1;
4993 goto dig_only;
4994 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004995 if (!cfg->num_inputs && !cfg->dig_in_pin)
4996 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004997 }
4998
4999 if (!spec->no_primary_hp &&
5000 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
5001 cfg->line_outs <= cfg->hp_outs) {
5002 /* use HP as primary out */
5003 cfg->speaker_outs = cfg->line_outs;
5004 memcpy(cfg->speaker_pins, cfg->line_out_pins,
5005 sizeof(cfg->speaker_pins));
5006 cfg->line_outs = cfg->hp_outs;
5007 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
5008 cfg->hp_outs = 0;
5009 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
5010 cfg->line_out_type = AUTO_PIN_HP_OUT;
5011 }
5012
5013 err = parse_output_paths(codec);
5014 if (err < 0)
5015 return err;
5016 err = create_multi_channel_mode(codec);
5017 if (err < 0)
5018 return err;
5019 err = create_multi_out_ctls(codec, cfg);
5020 if (err < 0)
5021 return err;
5022 err = create_hp_out_ctls(codec);
5023 if (err < 0)
5024 return err;
5025 err = create_speaker_out_ctls(codec);
5026 if (err < 0)
5027 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005028 err = create_indep_hp_ctls(codec);
5029 if (err < 0)
5030 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01005031 err = create_loopback_mixing_ctl(codec);
5032 if (err < 0)
5033 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01005034 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005035 if (err < 0)
5036 return err;
5037 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02005038 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02005039 return err;
5040
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01005041 /* add power-down pin callbacks at first */
5042 add_all_pin_power_ctls(codec, false);
5043
Takashi Iwaia07a9492013-01-07 16:44:06 +01005044 spec->const_channel_count = spec->ext_channel_count;
5045 /* check the multiple speaker and headphone pins */
5046 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
5047 spec->const_channel_count = max(spec->const_channel_count,
5048 cfg->speaker_outs * 2);
5049 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
5050 spec->const_channel_count = max(spec->const_channel_count,
5051 cfg->hp_outs * 2);
5052 spec->multiout.max_channels = max(spec->ext_channel_count,
5053 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005054
5055 err = check_auto_mute_availability(codec);
5056 if (err < 0)
5057 return err;
5058
5059 err = check_dyn_adc_switch(codec);
5060 if (err < 0)
5061 return err;
5062
Takashi Iwai967303d2013-02-19 17:12:42 +01005063 err = check_auto_mic_availability(codec);
5064 if (err < 0)
5065 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02005066
Takashi Iwaif1e762d2013-12-09 16:02:24 +01005067 /* add stereo mix if available and not enabled yet */
5068 if (!spec->auto_mic && spec->mixer_nid &&
Takashi Iwai74f14b32014-12-15 13:43:59 +01005069 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
5070 spec->input_mux.num_items > 1) {
Takashi Iwaif1e762d2013-12-09 16:02:24 +01005071 err = parse_capture_source(codec, spec->mixer_nid,
5072 CFG_IDX_MIX, spec->num_all_adcs,
5073 "Stereo Mix", 0);
5074 if (err < 0)
5075 return err;
5076 }
5077
5078
Takashi Iwai352f7f92012-12-19 12:52:06 +01005079 err = create_capture_mixers(codec);
5080 if (err < 0)
5081 return err;
5082
5083 err = parse_mic_boost(codec);
5084 if (err < 0)
5085 return err;
5086
Takashi Iwaiced4cef2013-11-26 08:33:45 +01005087 /* create "Headphone Mic Jack Mode" if no input selection is
5088 * available (or user specifies add_jack_modes hint)
5089 */
5090 if (spec->hp_mic_pin &&
5091 (spec->auto_mic || spec->input_mux.num_items == 1 ||
5092 spec->add_jack_modes)) {
5093 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
5094 if (err < 0)
5095 return err;
5096 }
5097
Takashi Iwaif811c3c2013-03-07 18:32:59 +01005098 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01005099 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
5100 err = create_out_jack_modes(codec, cfg->line_outs,
5101 cfg->line_out_pins);
5102 if (err < 0)
5103 return err;
5104 }
5105 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
5106 err = create_out_jack_modes(codec, cfg->hp_outs,
5107 cfg->hp_pins);
5108 if (err < 0)
5109 return err;
5110 }
5111 }
5112
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01005113 /* add power-up pin callbacks at last */
5114 add_all_pin_power_ctls(codec, true);
5115
Takashi Iwaiebb93c02013-12-10 17:33:49 +01005116 /* mute all aamix input initially */
5117 if (spec->mixer_nid)
5118 mute_all_mixer_nid(codec, spec->mixer_nid);
5119
Takashi Iwai352f7f92012-12-19 12:52:06 +01005120 dig_only:
5121 parse_digital(codec);
5122
Takashi Iwai49fb1892015-05-27 08:37:19 +02005123 if (spec->power_down_unused || codec->power_save_node) {
Takashi Iwai24fef902015-04-09 10:28:15 +02005124 if (!codec->power_filter)
5125 codec->power_filter = snd_hda_gen_path_power_filter;
Takashi Iwai49fb1892015-05-27 08:37:19 +02005126 if (!codec->patch_ops.stream_pm)
5127 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
5128 }
Takashi Iwai55196ff2013-01-24 17:32:56 +01005129
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005130 if (!spec->no_analog && spec->beep_nid) {
5131 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
5132 if (err < 0)
5133 return err;
Takashi Iwai967b1302015-03-20 18:21:03 +01005134 if (codec->beep && codec->power_save_node) {
Takashi Iwai5ccf8352015-03-18 09:23:10 +01005135 err = add_fake_beep_paths(codec);
5136 if (err < 0)
5137 return err;
5138 codec->beep->power_hook = beep_power_hook;
5139 }
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005140 }
5141
Takashi Iwai352f7f92012-12-19 12:52:06 +01005142 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005144EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145
5146
5147/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01005148 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005150
Takashi Iwai9ab0cb32020-07-17 17:45:17 +02005151/* follower controls for virtual master */
5152static const char * const follower_pfxs[] = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01005153 "Front", "Surround", "Center", "LFE", "Side",
5154 "Headphone", "Speaker", "Mono", "Line Out",
5155 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01005156 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
5157 "Headphone Front", "Headphone Surround", "Headphone CLFE",
David Henningsson03ad6a82014-10-16 15:33:45 +02005158 "Headphone Side", "Headphone+LO", "Speaker+LO",
Takashi Iwai352f7f92012-12-19 12:52:06 +01005159 NULL,
5160};
5161
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005162/**
5163 * snd_hda_gen_build_controls - Build controls from the parsed results
5164 * @codec: the HDA codec
5165 *
5166 * Pass this to build_controls patch_ops.
5167 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005168int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005170 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172
Takashi Iwai36502d02012-12-19 15:15:10 +01005173 if (spec->kctls.used) {
5174 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5175 if (err < 0)
5176 return err;
5177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178
Takashi Iwai352f7f92012-12-19 12:52:06 +01005179 if (spec->multiout.dig_out_nid) {
5180 err = snd_hda_create_dig_out_ctls(codec,
5181 spec->multiout.dig_out_nid,
5182 spec->multiout.dig_out_nid,
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005183 spec->pcm_rec[1]->pcm_type);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005184 if (err < 0)
5185 return err;
5186 if (!spec->no_analog) {
5187 err = snd_hda_create_spdif_share_sw(codec,
5188 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 if (err < 0)
5190 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005191 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 }
5193 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005194 if (spec->dig_in_nid) {
5195 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5196 if (err < 0)
5197 return err;
5198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199
Takashi Iwai352f7f92012-12-19 12:52:06 +01005200 /* if we have no master control, let's create it */
Takashi Iwai74803162017-04-10 17:37:34 +02005201 if (!spec->no_analog && !spec->suppress_vmaster &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01005202 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01005203 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai9ab0cb32020-07-17 17:45:17 +02005204 spec->vmaster_tlv, follower_pfxs,
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01005205 "Playback Volume", 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005206 if (err < 0)
5207 return err;
5208 }
Takashi Iwai74803162017-04-10 17:37:34 +02005209 if (!spec->no_analog && !spec->suppress_vmaster &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01005210 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5211 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
Takashi Iwai9ab0cb32020-07-17 17:45:17 +02005212 NULL, follower_pfxs,
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01005213 "Playback Switch", true,
5214 spec->vmaster_mute_led ?
5215 SNDRV_CTL_ELEM_ACCESS_SPK_LED : 0,
5216 &spec->vmaster_mute.sw_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005217 if (err < 0)
5218 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02005219 if (spec->vmaster_mute.hook) {
Jaroslav Kyselae65bf992021-03-17 18:29:43 +01005220 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute);
Takashi Iwaib63eae02013-10-25 23:43:10 +02005221 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5222 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224
Takashi Iwai352f7f92012-12-19 12:52:06 +01005225 free_kctls(spec); /* no longer needed */
5226
Takashi Iwai352f7f92012-12-19 12:52:06 +01005227 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5228 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 return err;
5230
5231 return 0;
5232}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005233EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005234
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235
5236/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01005237 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005240static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5241 struct hda_codec *codec,
5242 struct snd_pcm_substream *substream,
5243 int action)
5244{
5245 struct hda_gen_spec *spec = codec->spec;
5246 if (spec->pcm_playback_hook)
5247 spec->pcm_playback_hook(hinfo, codec, substream, action);
5248}
5249
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005250static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5251 struct hda_codec *codec,
5252 struct snd_pcm_substream *substream,
5253 int action)
5254{
5255 struct hda_gen_spec *spec = codec->spec;
5256 if (spec->pcm_capture_hook)
5257 spec->pcm_capture_hook(hinfo, codec, substream, action);
5258}
5259
Takashi Iwai352f7f92012-12-19 12:52:06 +01005260/*
5261 * Analog playback callbacks
5262 */
5263static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5264 struct hda_codec *codec,
5265 struct snd_pcm_substream *substream)
5266{
5267 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005268 int err;
5269
5270 mutex_lock(&spec->pcm_mutex);
5271 err = snd_hda_multi_out_analog_open(codec,
5272 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005273 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005274 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005275 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005276 call_pcm_playback_hook(hinfo, codec, substream,
5277 HDA_GEN_PCM_ACT_OPEN);
5278 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005279 mutex_unlock(&spec->pcm_mutex);
5280 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005281}
5282
5283static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01005284 struct hda_codec *codec,
5285 unsigned int stream_tag,
5286 unsigned int format,
5287 struct snd_pcm_substream *substream)
5288{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005289 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005290 int err;
5291
5292 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5293 stream_tag, format, substream);
5294 if (!err)
5295 call_pcm_playback_hook(hinfo, codec, substream,
5296 HDA_GEN_PCM_ACT_PREPARE);
5297 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005298}
Takashi Iwai97ec5582006-03-21 11:29:07 +01005299
Takashi Iwai352f7f92012-12-19 12:52:06 +01005300static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5301 struct hda_codec *codec,
5302 struct snd_pcm_substream *substream)
5303{
5304 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005305 int err;
5306
5307 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5308 if (!err)
5309 call_pcm_playback_hook(hinfo, codec, substream,
5310 HDA_GEN_PCM_ACT_CLEANUP);
5311 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005312}
5313
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005314static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5315 struct hda_codec *codec,
5316 struct snd_pcm_substream *substream)
5317{
5318 struct hda_gen_spec *spec = codec->spec;
5319 mutex_lock(&spec->pcm_mutex);
5320 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005321 call_pcm_playback_hook(hinfo, codec, substream,
5322 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005323 mutex_unlock(&spec->pcm_mutex);
5324 return 0;
5325}
5326
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005327static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5328 struct hda_codec *codec,
5329 struct snd_pcm_substream *substream)
5330{
5331 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5332 return 0;
5333}
5334
5335static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5336 struct hda_codec *codec,
5337 unsigned int stream_tag,
5338 unsigned int format,
5339 struct snd_pcm_substream *substream)
5340{
5341 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5342 call_pcm_capture_hook(hinfo, codec, substream,
5343 HDA_GEN_PCM_ACT_PREPARE);
5344 return 0;
5345}
5346
5347static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5348 struct hda_codec *codec,
5349 struct snd_pcm_substream *substream)
5350{
5351 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5352 call_pcm_capture_hook(hinfo, codec, substream,
5353 HDA_GEN_PCM_ACT_CLEANUP);
5354 return 0;
5355}
5356
5357static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5358 struct hda_codec *codec,
5359 struct snd_pcm_substream *substream)
5360{
5361 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5362 return 0;
5363}
5364
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005365static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5366 struct hda_codec *codec,
5367 struct snd_pcm_substream *substream)
5368{
5369 struct hda_gen_spec *spec = codec->spec;
5370 int err = 0;
5371
5372 mutex_lock(&spec->pcm_mutex);
Takashi Iwaid1f15e02015-07-08 09:22:10 +02005373 if (spec->indep_hp && !spec->indep_hp_enabled)
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005374 err = -EBUSY;
5375 else
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_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005379 mutex_unlock(&spec->pcm_mutex);
5380 return err;
5381}
5382
5383static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5384 struct hda_codec *codec,
5385 struct snd_pcm_substream *substream)
5386{
5387 struct hda_gen_spec *spec = codec->spec;
5388 mutex_lock(&spec->pcm_mutex);
5389 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005390 call_pcm_playback_hook(hinfo, codec, substream,
5391 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005392 mutex_unlock(&spec->pcm_mutex);
5393 return 0;
5394}
5395
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005396static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5397 struct hda_codec *codec,
5398 unsigned int stream_tag,
5399 unsigned int format,
5400 struct snd_pcm_substream *substream)
5401{
5402 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5403 call_pcm_playback_hook(hinfo, codec, substream,
5404 HDA_GEN_PCM_ACT_PREPARE);
5405 return 0;
5406}
5407
5408static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5409 struct hda_codec *codec,
5410 struct snd_pcm_substream *substream)
5411{
5412 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5413 call_pcm_playback_hook(hinfo, codec, substream,
5414 HDA_GEN_PCM_ACT_CLEANUP);
5415 return 0;
5416}
5417
Takashi Iwai352f7f92012-12-19 12:52:06 +01005418/*
5419 * Digital out
5420 */
5421static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5422 struct hda_codec *codec,
5423 struct snd_pcm_substream *substream)
5424{
5425 struct hda_gen_spec *spec = codec->spec;
5426 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5427}
5428
5429static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5430 struct hda_codec *codec,
5431 unsigned int stream_tag,
5432 unsigned int format,
5433 struct snd_pcm_substream *substream)
5434{
5435 struct hda_gen_spec *spec = codec->spec;
5436 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5437 stream_tag, format, substream);
5438}
5439
5440static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5441 struct hda_codec *codec,
5442 struct snd_pcm_substream *substream)
5443{
5444 struct hda_gen_spec *spec = codec->spec;
5445 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5446}
5447
5448static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5449 struct hda_codec *codec,
5450 struct snd_pcm_substream *substream)
5451{
5452 struct hda_gen_spec *spec = codec->spec;
5453 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5454}
5455
5456/*
5457 * Analog capture
5458 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005459#define alt_capture_pcm_open capture_pcm_open
5460#define alt_capture_pcm_close capture_pcm_close
5461
Takashi Iwai352f7f92012-12-19 12:52:06 +01005462static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5463 struct hda_codec *codec,
5464 unsigned int stream_tag,
5465 unsigned int format,
5466 struct snd_pcm_substream *substream)
5467{
5468 struct hda_gen_spec *spec = codec->spec;
5469
5470 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01005471 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005472 call_pcm_capture_hook(hinfo, codec, substream,
5473 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005474 return 0;
5475}
5476
Takashi Iwai352f7f92012-12-19 12:52:06 +01005477static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5478 struct hda_codec *codec,
5479 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01005480{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005481 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01005482
Takashi Iwai352f7f92012-12-19 12:52:06 +01005483 snd_hda_codec_cleanup_stream(codec,
5484 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005485 call_pcm_capture_hook(hinfo, codec, substream,
5486 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005487 return 0;
5488}
5489
Takashi Iwai352f7f92012-12-19 12:52:06 +01005490/*
5491 */
5492static const struct hda_pcm_stream pcm_analog_playback = {
5493 .substreams = 1,
5494 .channels_min = 2,
5495 .channels_max = 8,
5496 /* NID is set in build_pcms */
5497 .ops = {
5498 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005499 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005500 .prepare = playback_pcm_prepare,
5501 .cleanup = playback_pcm_cleanup
5502 },
5503};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
Takashi Iwai352f7f92012-12-19 12:52:06 +01005505static const struct hda_pcm_stream pcm_analog_capture = {
5506 .substreams = 1,
5507 .channels_min = 2,
5508 .channels_max = 2,
5509 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005510 .ops = {
5511 .open = capture_pcm_open,
5512 .close = capture_pcm_close,
5513 .prepare = capture_pcm_prepare,
5514 .cleanup = capture_pcm_cleanup
5515 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005516};
5517
5518static const struct hda_pcm_stream pcm_analog_alt_playback = {
5519 .substreams = 1,
5520 .channels_min = 2,
5521 .channels_max = 2,
5522 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005523 .ops = {
5524 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005525 .close = alt_playback_pcm_close,
5526 .prepare = alt_playback_pcm_prepare,
5527 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005528 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005529};
5530
5531static const struct hda_pcm_stream pcm_analog_alt_capture = {
5532 .substreams = 2, /* can be overridden */
5533 .channels_min = 2,
5534 .channels_max = 2,
5535 /* NID is set in build_pcms */
5536 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005537 .open = alt_capture_pcm_open,
5538 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005539 .prepare = alt_capture_pcm_prepare,
5540 .cleanup = alt_capture_pcm_cleanup
5541 },
5542};
5543
5544static const struct hda_pcm_stream pcm_digital_playback = {
5545 .substreams = 1,
5546 .channels_min = 2,
5547 .channels_max = 2,
5548 /* NID is set in build_pcms */
5549 .ops = {
5550 .open = dig_playback_pcm_open,
5551 .close = dig_playback_pcm_close,
5552 .prepare = dig_playback_pcm_prepare,
5553 .cleanup = dig_playback_pcm_cleanup
5554 },
5555};
5556
5557static const struct hda_pcm_stream pcm_digital_capture = {
5558 .substreams = 1,
5559 .channels_min = 2,
5560 .channels_max = 2,
5561 /* NID is set in build_pcms */
5562};
5563
5564/* Used by build_pcms to flag that a PCM has no playback stream */
5565static const struct hda_pcm_stream pcm_null_stream = {
5566 .substreams = 0,
5567 .channels_min = 0,
5568 .channels_max = 0,
5569};
5570
5571/*
5572 * dynamic changing ADC PCM streams
5573 */
5574static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5575{
5576 struct hda_gen_spec *spec = codec->spec;
5577 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5578
5579 if (spec->cur_adc && spec->cur_adc != new_adc) {
5580 /* stream is running, let's swap the current ADC */
5581 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5582 spec->cur_adc = new_adc;
5583 snd_hda_codec_setup_stream(codec, new_adc,
5584 spec->cur_adc_stream_tag, 0,
5585 spec->cur_adc_format);
5586 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005588 return false;
5589}
5590
5591/* analog capture with dynamic dual-adc changes */
5592static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5593 struct hda_codec *codec,
5594 unsigned int stream_tag,
5595 unsigned int format,
5596 struct snd_pcm_substream *substream)
5597{
5598 struct hda_gen_spec *spec = codec->spec;
5599 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5600 spec->cur_adc_stream_tag = stream_tag;
5601 spec->cur_adc_format = format;
5602 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
Takashi Iwai4f29efc2016-04-12 15:16:24 +02005603 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005604 return 0;
5605}
5606
5607static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5608 struct hda_codec *codec,
5609 struct snd_pcm_substream *substream)
5610{
5611 struct hda_gen_spec *spec = codec->spec;
5612 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5613 spec->cur_adc = 0;
Takashi Iwai4f29efc2016-04-12 15:16:24 +02005614 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005615 return 0;
5616}
5617
5618static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5619 .substreams = 1,
5620 .channels_min = 2,
5621 .channels_max = 2,
5622 .nid = 0, /* fill later */
5623 .ops = {
5624 .prepare = dyn_adc_capture_pcm_prepare,
5625 .cleanup = dyn_adc_capture_pcm_cleanup
5626 },
5627};
5628
Takashi Iwaif873e532012-12-20 16:58:39 +01005629static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5630 const char *chip_name)
5631{
5632 char *p;
5633
5634 if (*str)
5635 return;
Joe Perches75b1a8f2021-01-04 09:17:34 -08005636 strscpy(str, chip_name, len);
Takashi Iwaif873e532012-12-20 16:58:39 +01005637
5638 /* drop non-alnum chars after a space */
5639 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5640 if (!isalnum(p[1])) {
5641 *p = 0;
5642 break;
5643 }
5644 }
5645 strlcat(str, sfx, len);
5646}
5647
Takashi Iwaifb83b632015-03-16 23:34:34 +01005648/* copy PCM stream info from @default_str, and override non-NULL entries
5649 * from @spec_str and @nid
5650 */
5651static void setup_pcm_stream(struct hda_pcm_stream *str,
5652 const struct hda_pcm_stream *default_str,
5653 const struct hda_pcm_stream *spec_str,
5654 hda_nid_t nid)
5655{
5656 *str = *default_str;
5657 if (nid)
5658 str->nid = nid;
5659 if (spec_str) {
5660 if (spec_str->substreams)
5661 str->substreams = spec_str->substreams;
5662 if (spec_str->channels_min)
5663 str->channels_min = spec_str->channels_min;
5664 if (spec_str->channels_max)
5665 str->channels_max = spec_str->channels_max;
5666 if (spec_str->rates)
5667 str->rates = spec_str->rates;
5668 if (spec_str->formats)
5669 str->formats = spec_str->formats;
5670 if (spec_str->maxbps)
5671 str->maxbps = spec_str->maxbps;
5672 }
5673}
5674
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005675/**
5676 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5677 * @codec: the HDA codec
5678 *
5679 * Pass this to build_pcms patch_ops.
5680 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005681int snd_hda_gen_build_pcms(struct hda_codec *codec)
5682{
5683 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005684 struct hda_pcm *info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005685 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
Takashi Iwai352f7f92012-12-19 12:52:06 +01005687 if (spec->no_analog)
5688 goto skip_analog;
5689
Takashi Iwaif873e532012-12-20 16:58:39 +01005690 fill_pcm_stream_name(spec->stream_name_analog,
5691 sizeof(spec->stream_name_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005692 " Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005693 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5694 if (!info)
5695 return -ENOMEM;
5696 spec->pcm_rec[0] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005697
5698 if (spec->multiout.num_dacs > 0) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005699 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5700 &pcm_analog_playback,
5701 spec->stream_analog_playback,
5702 spec->multiout.dac_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005703 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5704 spec->multiout.max_channels;
5705 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5706 spec->autocfg.line_outs == 2)
5707 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5708 snd_pcm_2_1_chmaps;
5709 }
5710 if (spec->num_adc_nids) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005711 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5712 (spec->dyn_adc_switch ?
5713 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5714 spec->stream_analog_capture,
5715 spec->adc_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005716 }
5717
Takashi Iwai352f7f92012-12-19 12:52:06 +01005718 skip_analog:
5719 /* SPDIF for stream index #1 */
5720 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005721 fill_pcm_stream_name(spec->stream_name_digital,
5722 sizeof(spec->stream_name_digital),
Takashi Iwai7639a062015-03-03 10:07:24 +01005723 " Digital", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005724 info = snd_hda_codec_pcm_new(codec, "%s",
5725 spec->stream_name_digital);
5726 if (!info)
5727 return -ENOMEM;
Takashi Iwai9ab0cb32020-07-17 17:45:17 +02005728 codec->follower_dig_outs = spec->multiout.follower_dig_outs;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005729 spec->pcm_rec[1] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005730 if (spec->dig_out_type)
5731 info->pcm_type = spec->dig_out_type;
5732 else
5733 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005734 if (spec->multiout.dig_out_nid)
5735 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5736 &pcm_digital_playback,
5737 spec->stream_digital_playback,
5738 spec->multiout.dig_out_nid);
5739 if (spec->dig_in_nid)
5740 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5741 &pcm_digital_capture,
5742 spec->stream_digital_capture,
5743 spec->dig_in_nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005744 }
5745
5746 if (spec->no_analog)
5747 return 0;
5748
5749 /* If the use of more than one ADC is requested for the current
5750 * model, configure a second analog capture-only PCM.
5751 */
5752 have_multi_adcs = (spec->num_adc_nids > 1) &&
5753 !spec->dyn_adc_switch && !spec->auto_mic;
5754 /* Additional Analaog capture for index #2 */
5755 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005756 fill_pcm_stream_name(spec->stream_name_alt_analog,
5757 sizeof(spec->stream_name_alt_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005758 " Alt Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005759 info = snd_hda_codec_pcm_new(codec, "%s",
5760 spec->stream_name_alt_analog);
5761 if (!info)
5762 return -ENOMEM;
5763 spec->pcm_rec[2] = info;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005764 if (spec->alt_dac_nid)
5765 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5766 &pcm_analog_alt_playback,
5767 spec->stream_analog_alt_playback,
5768 spec->alt_dac_nid);
5769 else
5770 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5771 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005772 if (have_multi_adcs) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005773 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5774 &pcm_analog_alt_capture,
5775 spec->stream_analog_alt_capture,
5776 spec->adc_nids[1]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005777 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5778 spec->num_adc_nids - 1;
5779 } else {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005780 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5781 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 }
5784
5785 return 0;
5786}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005787EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005788
5789
5790/*
5791 * Standard auto-parser initializations
5792 */
5793
Takashi Iwaid4156932013-01-07 10:08:02 +01005794/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005795static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005796{
5797 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005798 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005799
Takashi Iwai196c17662013-01-04 15:01:40 +01005800 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005801 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005802 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005803 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005804 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005805 snd_hda_activate_path(codec, path, path->active,
5806 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005807 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005808}
5809
5810/* initialize primary output paths */
5811static void init_multi_out(struct hda_codec *codec)
5812{
5813 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005814 int i;
5815
Takashi Iwaid4156932013-01-07 10:08:02 +01005816 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005817 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005818}
5819
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005820
Takashi Iwai2c12c302013-01-10 09:33:29 +01005821static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005822{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005823 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005824
Takashi Iwaid4156932013-01-07 10:08:02 +01005825 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005826 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005827}
5828
5829/* initialize hp and speaker paths */
5830static void init_extra_out(struct hda_codec *codec)
5831{
5832 struct hda_gen_spec *spec = codec->spec;
5833
5834 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005835 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005836 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5837 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005838 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005839}
5840
5841/* initialize multi-io paths */
5842static void init_multi_io(struct hda_codec *codec)
5843{
5844 struct hda_gen_spec *spec = codec->spec;
5845 int i;
5846
5847 for (i = 0; i < spec->multi_ios; i++) {
5848 hda_nid_t pin = spec->multi_io[i].pin;
5849 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005850 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005851 if (!path)
5852 continue;
5853 if (!spec->multi_io[i].ctl_in)
5854 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005855 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005856 snd_hda_activate_path(codec, path, path->active,
5857 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005858 }
5859}
5860
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005861static void init_aamix_paths(struct hda_codec *codec)
5862{
5863 struct hda_gen_spec *spec = codec->spec;
5864
5865 if (!spec->have_aamix_ctl)
5866 return;
Takashi Iwaie7fdd522015-12-08 17:00:42 +01005867 if (!has_aamix_out_paths(spec))
5868 return;
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005869 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5870 spec->aamix_out_paths[0],
5871 spec->autocfg.line_out_type);
5872 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5873 spec->aamix_out_paths[1],
5874 AUTO_PIN_HP_OUT);
5875 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5876 spec->aamix_out_paths[2],
5877 AUTO_PIN_SPEAKER_OUT);
5878}
5879
Takashi Iwai352f7f92012-12-19 12:52:06 +01005880/* set up input pins and loopback paths */
5881static void init_analog_input(struct hda_codec *codec)
5882{
5883 struct hda_gen_spec *spec = codec->spec;
5884 struct auto_pin_cfg *cfg = &spec->autocfg;
5885 int i;
5886
5887 for (i = 0; i < cfg->num_inputs; i++) {
5888 hda_nid_t nid = cfg->inputs[i].pin;
5889 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005890 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005891
5892 /* init loopback inputs */
5893 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005894 resume_path_from_idx(codec, spec->loopback_paths[i]);
5895 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005896 }
5897 }
5898}
5899
5900/* initialize ADC paths */
5901static void init_input_src(struct hda_codec *codec)
5902{
5903 struct hda_gen_spec *spec = codec->spec;
5904 struct hda_input_mux *imux = &spec->input_mux;
5905 struct nid_path *path;
5906 int i, c, nums;
5907
5908 if (spec->dyn_adc_switch)
5909 nums = 1;
5910 else
5911 nums = spec->num_adc_nids;
5912
5913 for (c = 0; c < nums; c++) {
5914 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005915 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005916 if (path) {
5917 bool active = path->active;
5918 if (i == spec->cur_mux[c])
5919 active = true;
5920 snd_hda_activate_path(codec, path, active, false);
5921 }
5922 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005923 if (spec->hp_mic)
5924 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005925 }
5926
Takashi Iwai352f7f92012-12-19 12:52:06 +01005927 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005928 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005929}
5930
5931/* set right pin controls for digital I/O */
5932static void init_digital(struct hda_codec *codec)
5933{
5934 struct hda_gen_spec *spec = codec->spec;
5935 int i;
5936 hda_nid_t pin;
5937
Takashi Iwaid4156932013-01-07 10:08:02 +01005938 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005939 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005940 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005941 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005942 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005943 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005944 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005945}
5946
Takashi Iwai973e4972012-12-20 15:16:09 +01005947/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5948 * invalid unsol tags by some reason
5949 */
5950static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5951{
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +02005952 const struct hda_pincfg *pin;
Takashi Iwai973e4972012-12-20 15:16:09 +01005953 int i;
5954
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +02005955 snd_array_for_each(&codec->init_pins, i, pin) {
Takashi Iwai973e4972012-12-20 15:16:09 +01005956 hda_nid_t nid = pin->nid;
5957 if (is_jack_detectable(codec, nid) &&
5958 !snd_hda_jack_tbl_get(codec, nid))
Takashi Iwai401caff2018-06-27 11:43:09 +02005959 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai973e4972012-12-20 15:16:09 +01005960 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5961 }
5962}
5963
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005964/**
5965 * snd_hda_gen_init - initialize the generic spec
5966 * @codec: the HDA codec
5967 *
5968 * This can be put as patch_ops init function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005969 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005970int snd_hda_gen_init(struct hda_codec *codec)
5971{
5972 struct hda_gen_spec *spec = codec->spec;
5973
5974 if (spec->init_hook)
5975 spec->init_hook(codec);
5976
Takashi Iwai89781d02019-08-30 12:03:38 +02005977 if (!spec->skip_verbs)
5978 snd_hda_apply_verbs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005979
5980 init_multi_out(codec);
5981 init_extra_out(codec);
5982 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005983 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005984 init_analog_input(codec);
5985 init_input_src(codec);
5986 init_digital(codec);
5987
Takashi Iwai973e4972012-12-20 15:16:09 +01005988 clear_unsol_on_unused_pins(codec);
5989
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01005990 sync_all_pin_power_ctls(codec);
5991
Takashi Iwai352f7f92012-12-19 12:52:06 +01005992 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005993 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005994
Takashi Iwai1a462be2020-01-09 10:01:04 +01005995 snd_hda_regmap_sync(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005996
Takashi Iwai352f7f92012-12-19 12:52:06 +01005997 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5998 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5999
6000 hda_call_check_power_status(codec, 0x01);
6001 return 0;
6002}
Takashi Iwai2698ea92013-12-18 07:45:52 +01006003EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01006004
Takashi Iwaidda42bd2014-10-30 12:04:50 +01006005/**
6006 * snd_hda_gen_free - free the generic spec
6007 * @codec: the HDA codec
6008 *
6009 * This can be put as patch_ops free function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01006010 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01006011void snd_hda_gen_free(struct hda_codec *codec)
6012{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01006013 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01006014 snd_hda_gen_spec_free(codec->spec);
6015 kfree(codec->spec);
6016 codec->spec = NULL;
6017}
Takashi Iwai2698ea92013-12-18 07:45:52 +01006018EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01006019
6020#ifdef CONFIG_PM
Takashi Iwaidda42bd2014-10-30 12:04:50 +01006021/**
6022 * snd_hda_gen_check_power_status - check the loopback power save state
6023 * @codec: the HDA codec
6024 * @nid: NID to inspect
6025 *
6026 * This can be put as patch_ops check_power_status function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01006027 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01006028int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
6029{
6030 struct hda_gen_spec *spec = codec->spec;
6031 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
6032}
Takashi Iwai2698ea92013-12-18 07:45:52 +01006033EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01006034#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01006035
6036
6037/*
6038 * the generic codec support
6039 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040
Takashi Iwai352f7f92012-12-19 12:52:06 +01006041static const struct hda_codec_ops generic_patch_ops = {
6042 .build_controls = snd_hda_gen_build_controls,
6043 .build_pcms = snd_hda_gen_build_pcms,
6044 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01006045 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01006046 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02006047#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01006048 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02006049#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050};
6051
Takashi Iwaid8a766a2015-02-17 15:25:37 +01006052/*
Takashi Iwaidda42bd2014-10-30 12:04:50 +01006053 * snd_hda_parse_generic_codec - Generic codec parser
6054 * @codec: the HDA codec
Takashi Iwaidda42bd2014-10-30 12:04:50 +01006055 */
Takashi Iwaid8a766a2015-02-17 15:25:37 +01006056static int snd_hda_parse_generic_codec(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057{
Takashi Iwai352f7f92012-12-19 12:52:06 +01006058 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 int err;
6060
Takashi Iwaie560d8d2005-09-09 14:21:46 +02006061 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01006062 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01006064 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066
Takashi Iwai9eb413e2012-12-19 14:41:21 +01006067 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
6068 if (err < 0)
Wenwen Wangcfef67f2019-08-09 23:29:48 -05006069 goto error;
Takashi Iwai9eb413e2012-12-19 14:41:21 +01006070
6071 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01006072 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 goto error;
6074
6075 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 return 0;
6077
Takashi Iwai352f7f92012-12-19 12:52:06 +01006078error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01006079 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 return err;
6081}
Takashi Iwaid8a766a2015-02-17 15:25:37 +01006082
Takashi Iwaib9a94a92015-10-01 16:20:04 +02006083static const struct hda_device_id snd_hda_id_generic[] = {
6084 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01006085 {} /* terminator */
6086};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02006087MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01006088
6089static struct hda_codec_driver generic_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02006090 .id = snd_hda_id_generic,
Takashi Iwaid8a766a2015-02-17 15:25:37 +01006091};
6092
6093module_hda_codec_driver(generic_driver);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01006094
6095MODULE_LICENSE("GPL");
6096MODULE_DESCRIPTION("Generic HD-audio codec parser");