Thomas Gleixner | d0fa117 | 2019-05-20 19:07:57 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/init.h> |
| 11 | #include <linux/slab.h> |
Paul Gortmaker | d81a6d7 | 2011-09-22 09:34:58 -0400 | [diff] [blame] | 12 | #include <linux/export.h> |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 13 | #include <linux/sort.h> |
Takashi Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 14 | #include <linux/delay.h> |
Takashi Iwai | f873e53 | 2012-12-20 16:58:39 +0100 | [diff] [blame] | 15 | #include <linux/ctype.h> |
| 16 | #include <linux/string.h> |
Takashi Iwai | 29476558 | 2013-01-17 09:52:11 +0100 | [diff] [blame] | 17 | #include <linux/bitops.h> |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 18 | #include <linux/module.h> |
Takashi Iwai | b380278 | 2018-11-26 17:47:46 +0100 | [diff] [blame] | 19 | #include <linux/leds.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <sound/core.h> |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 21 | #include <sound/jack.h> |
Takashi Iwai | d89c6c0 | 2014-09-01 10:07:04 +0200 | [diff] [blame] | 22 | #include <sound/tlv.h> |
Pierre-Louis Bossart | be57bff | 2018-08-22 15:24:57 -0500 | [diff] [blame] | 23 | #include <sound/hda_codec.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "hda_local.h" |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 25 | #include "hda_auto_parser.h" |
| 26 | #include "hda_jack.h" |
Takashi Iwai | 7504b6c | 2013-03-18 11:25:51 +0100 | [diff] [blame] | 27 | #include "hda_beep.h" |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 28 | #include "hda_generic.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 31 | /** |
| 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 37 | int snd_hda_gen_spec_init(struct hda_gen_spec *spec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 39 | snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 40 | snd_array_init(&spec->paths, sizeof(struct nid_path), 8); |
Takashi Iwai | 0186f4f | 2013-02-07 10:45:11 +0100 | [diff] [blame] | 41 | snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8); |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 42 | mutex_init(&spec->pcm_mutex); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 43 | return 0; |
| 44 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 45 | EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 47 | /** |
| 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 Iwai | 12c93df | 2012-12-19 14:38:33 +0100 | [diff] [blame] | 58 | struct snd_kcontrol_new * |
| 59 | snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name, |
| 60 | const struct snd_kcontrol_new *temp) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 61 | { |
| 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 Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 74 | EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 75 | |
| 76 | static 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 Iwai | a8dca46 | 2014-02-10 18:23:57 +0100 | [diff] [blame] | 87 | static void snd_hda_gen_spec_free(struct hda_gen_spec *spec) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 88 | { |
| 89 | if (!spec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | return; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 91 | free_kctls(spec); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 92 | snd_array_free(&spec->paths); |
Takashi Iwai | 0186f4f | 2013-02-07 10:45:11 +0100 | [diff] [blame] | 93 | snd_array_free(&spec->loopback_list); |
Takashi Iwai | 549f8ff | 2022-01-26 15:50:11 +0100 | [diff] [blame] | 94 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | /* |
Takashi Iwai | 1c70a58 | 2013-01-11 17:48:22 +0100 | [diff] [blame] | 103 | * store user hints |
| 104 | */ |
| 105 | static 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 Iwai | 967b130 | 2015-03-20 18:21:03 +0100 | [diff] [blame] | 137 | val = snd_hda_get_bool_hint(codec, "power_save_node"); |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 138 | if (val >= 0) |
Takashi Iwai | 967b130 | 2015-03-20 18:21:03 +0100 | [diff] [blame] | 139 | codec->power_save_node = !!val; |
Takashi Iwai | 1c70a58 | 2013-01-11 17:48:22 +0100 | [diff] [blame] | 140 | |
Takashi Iwai | f72706b | 2013-01-16 18:20:07 +0100 | [diff] [blame] | 141 | val = snd_hda_get_bool_hint(codec, "auto_mute"); |
| 142 | if (val >= 0) |
| 143 | spec->suppress_auto_mute = !val; |
Takashi Iwai | 1c70a58 | 2013-01-11 17:48:22 +0100 | [diff] [blame] | 144 | 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 Iwai | 7eebffd | 2013-06-24 16:00:21 +0200 | [diff] [blame] | 150 | val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp"); |
| 151 | if (val >= 0) |
| 152 | spec->auto_mute_via_amp = !!val; |
Takashi Iwai | 1c70a58 | 2013-01-11 17:48:22 +0100 | [diff] [blame] | 153 | 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 Iwai | da96fb5 | 2013-07-29 16:54:36 +0200 | [diff] [blame] | 159 | val = snd_hda_get_bool_hint(codec, "multi_io"); |
| 160 | if (val >= 0) |
| 161 | spec->no_multi_io = !val; |
Takashi Iwai | 1c70a58 | 2013-01-11 17:48:22 +0100 | [diff] [blame] | 162 | 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 Iwai | f811c3c | 2013-03-07 18:32:59 +0100 | [diff] [blame] | 174 | /* the following two are just for compatibility */ |
Takashi Iwai | 1c70a58 | 2013-01-11 17:48:22 +0100 | [diff] [blame] | 175 | val = snd_hda_get_bool_hint(codec, "add_out_jack_modes"); |
| 176 | if (val >= 0) |
Takashi Iwai | f811c3c | 2013-03-07 18:32:59 +0100 | [diff] [blame] | 177 | spec->add_jack_modes = !!val; |
Takashi Iwai | 29476558 | 2013-01-17 09:52:11 +0100 | [diff] [blame] | 178 | val = snd_hda_get_bool_hint(codec, "add_in_jack_modes"); |
| 179 | if (val >= 0) |
Takashi Iwai | f811c3c | 2013-03-07 18:32:59 +0100 | [diff] [blame] | 180 | 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 Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 184 | val = snd_hda_get_bool_hint(codec, "power_down_unused"); |
| 185 | if (val >= 0) |
| 186 | spec->power_down_unused = !!val; |
Takashi Iwai | 967303d | 2013-02-19 17:12:42 +0100 | [diff] [blame] | 187 | 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 Iwai | 7480316 | 2017-04-10 17:37:34 +0200 | [diff] [blame] | 193 | val = snd_hda_get_bool_hint(codec, "vmaster"); |
| 194 | if (val >= 0) |
| 195 | spec->suppress_vmaster = !val; |
Takashi Iwai | 1c70a58 | 2013-01-11 17:48:22 +0100 | [diff] [blame] | 196 | |
| 197 | if (!snd_hda_get_int_hint(codec, "mixer_nid", &val)) |
| 198 | spec->mixer_nid = val; |
| 199 | } |
| 200 | |
| 201 | /* |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 202 | * pin control value accesses |
| 203 | */ |
| 204 | |
| 205 | #define update_pin_ctl(codec, pin, val) \ |
Takashi Iwai | 401caff | 2018-06-27 11:43:09 +0200 | [diff] [blame] | 206 | snd_hda_codec_write_cache(codec, pin, 0, \ |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 207 | AC_VERB_SET_PIN_WIDGET_CONTROL, val) |
| 208 | |
| 209 | /* restore the pinctl based on the cached value */ |
| 210 | static 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 */ |
| 216 | static 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 */ |
| 228 | static 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 237 | * parsing paths |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 240 | /* return the position of NID in the list, or -1 if not found */ |
| 241 | static 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 */ |
| 251 | static 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 Iwai | f5172a7 | 2013-01-04 13:19:55 +0100 | [diff] [blame] | 256 | static struct nid_path *get_nid_path(struct hda_codec *codec, |
| 257 | hda_nid_t from_nid, hda_nid_t to_nid, |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 258 | int anchor_nid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 260 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | a9c2dfc | 2018-04-23 17:24:56 +0200 | [diff] [blame] | 261 | struct nid_path *path; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 262 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Takashi Iwai | a9c2dfc | 2018-04-23 17:24:56 +0200 | [diff] [blame] | 264 | snd_array_for_each(&spec->paths, i, path) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 265 | if (path->depth <= 0) |
| 266 | continue; |
| 267 | if ((!from_nid || path->path[0] == from_nid) && |
Takashi Iwai | f5172a7 | 2013-01-04 13:19:55 +0100 | [diff] [blame] | 268 | (!to_nid || path->path[path->depth - 1] == to_nid)) { |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 269 | if (!anchor_nid || |
| 270 | (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) || |
| 271 | (anchor_nid < 0 && !is_nid_contained(path, anchor_nid))) |
Takashi Iwai | f5172a7 | 2013-01-04 13:19:55 +0100 | [diff] [blame] | 272 | return path; |
| 273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
| 275 | return NULL; |
| 276 | } |
Takashi Iwai | f5172a7 | 2013-01-04 13:19:55 +0100 | [diff] [blame] | 277 | |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 278 | /** |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 279 | * 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 Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 286 | */ |
| 287 | int 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 Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 300 | EXPORT_SYMBOL_GPL(snd_hda_get_path_idx); |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 301 | |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 302 | /** |
| 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 Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 308 | struct 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 Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 316 | EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx); |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 317 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 318 | /* check whether the given DAC is already found in any existing paths */ |
| 319 | static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid) |
| 320 | { |
| 321 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | a9c2dfc | 2018-04-23 17:24:56 +0200 | [diff] [blame] | 322 | const struct nid_path *path; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 323 | int i; |
| 324 | |
Takashi Iwai | a9c2dfc | 2018-04-23 17:24:56 +0200 | [diff] [blame] | 325 | snd_array_for_each(&spec->paths, i, path) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 326 | if (path->path[0] == nid) |
| 327 | return true; |
| 328 | } |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | /* check whether the given two widgets can be connected */ |
| 333 | static 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 */ |
| 345 | static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type) |
| 346 | { |
| 347 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | a9c2dfc | 2018-04-23 17:24:56 +0200 | [diff] [blame] | 348 | const struct nid_path *path; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 349 | int i; |
| 350 | |
| 351 | val &= AMP_VAL_COMPARE_MASK; |
Takashi Iwai | a9c2dfc | 2018-04-23 17:24:56 +0200 | [diff] [blame] | 352 | snd_array_for_each(&spec->paths, i, path) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 353 | 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 */ |
| 360 | static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | 8999bf0 | 2013-01-18 11:01:33 +0100 | [diff] [blame] | 361 | int dir, int idx, int type) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 362 | { |
| 363 | unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir); |
Takashi Iwai | 8999bf0 | 2013-01-18 11:01:33 +0100 | [diff] [blame] | 364 | return is_ctl_used(codec, val, type); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 365 | } |
| 366 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 367 | static void print_nid_path(struct hda_codec *codec, |
| 368 | const char *pfx, struct nid_path *path) |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 369 | { |
| 370 | char buf[40]; |
Joe Perches | d82353e | 2014-07-05 13:02:02 -0700 | [diff] [blame] | 371 | char *pos = buf; |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 372 | int i; |
| 373 | |
Joe Perches | d82353e | 2014-07-05 13:02:02 -0700 | [diff] [blame] | 374 | *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 Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 379 | |
Joe Perches | d82353e | 2014-07-05 13:02:02 -0700 | [diff] [blame] | 380 | codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf); |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 381 | } |
| 382 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 383 | /* called recursively */ |
| 384 | static bool __parse_nid_path(struct hda_codec *codec, |
| 385 | hda_nid_t from_nid, hda_nid_t to_nid, |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 386 | int anchor_nid, struct nid_path *path, |
| 387 | int depth) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 388 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 389 | const hda_nid_t *conn; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 390 | int i, nums; |
| 391 | |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 392 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 396 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 397 | nums = snd_hda_get_conn_list(codec, to_nid, &conn); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 398 | 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 Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 408 | /* anchor is not requested or already passed? */ |
| 409 | if (anchor_nid <= 0) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 410 | 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 Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 421 | anchor_nid, path, depth + 1)) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 422 | 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 Iwai | c4a58c3 | 2015-12-08 11:48:39 +0100 | [diff] [blame] | 435 | /* |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 436 | * 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 447 | * when @from_nid is 0, try to find an empty DAC; |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 448 | * 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 453 | */ |
Takashi Iwai | c4a58c3 | 2015-12-08 11:48:39 +0100 | [diff] [blame] | 454 | static bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid, |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 455 | hda_nid_t to_nid, int anchor_nid, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 456 | struct nid_path *path) |
| 457 | { |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 458 | if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 459 | path->path[path->depth] = to_nid; |
| 460 | path->depth++; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 461 | return true; |
| 462 | } |
| 463 | return false; |
| 464 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 466 | /** |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 476 | struct nid_path * |
| 477 | snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid, |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 478 | hda_nid_t to_nid, int anchor_nid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 480 | 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 Iwai | f5172a7 | 2013-01-04 13:19:55 +0100 | [diff] [blame] | 486 | /* check whether the path has been already added */ |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 487 | path = get_nid_path(codec, from_nid, to_nid, anchor_nid); |
Takashi Iwai | f5172a7 | 2013-01-04 13:19:55 +0100 | [diff] [blame] | 488 | if (path) |
| 489 | return path; |
| 490 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 491 | path = snd_array_new(&spec->paths); |
| 492 | if (!path) |
| 493 | return NULL; |
| 494 | memset(path, 0, sizeof(*path)); |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 495 | if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path)) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 496 | return path; |
| 497 | /* push back */ |
| 498 | spec->paths.used--; |
| 499 | return NULL; |
| 500 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 501 | EXPORT_SYMBOL_GPL(snd_hda_add_new_path); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 502 | |
Takashi Iwai | 980428c | 2013-01-09 09:28:20 +0100 | [diff] [blame] | 503 | /* clear the given path as invalid so that it won't be picked up later */ |
| 504 | static 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 Iwai | 3690739 | 2013-12-10 17:29:26 +0100 | [diff] [blame] | 512 | /* return a DAC if paired to the given pin by codec driver */ |
| 513 | static 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 526 | /* look for an empty DAC slot */ |
| 527 | static 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 */ |
| 548 | static 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 Henningsson | 99a5592 | 2013-01-16 15:58:44 +0100 | [diff] [blame] | 555 | static 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 564 | /* look for a widget suitable for assigning a mute switch in the path */ |
| 565 | static 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 */ |
| 581 | static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec, |
| 582 | struct nid_path *path) |
| 583 | { |
Takashi Iwai | a1114a8 | 2013-11-04 16:32:01 +0100 | [diff] [blame] | 584 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 585 | int i; |
| 586 | |
| 587 | for (i = path->depth - 1; i >= 0; i--) { |
Takashi Iwai | a1114a8 | 2013-11-04 16:32:01 +0100 | [diff] [blame] | 588 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 593 | } |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 594 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /* |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 598 | * path activation / deactivation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 600 | |
| 601 | /* can have the amp-in capability? */ |
| 602 | static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 604 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 615 | /* can have the amp-out capability? */ |
| 616 | static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 618 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 629 | /* check whether the given (nid,dir,idx) is active */ |
| 630 | static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | 7dddf2ae | 2013-01-24 16:31:35 +0100 | [diff] [blame] | 631 | unsigned int dir, unsigned int idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 633 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 634 | int type = get_wcaps_type(get_wcaps(codec, nid)); |
Takashi Iwai | a9c2dfc | 2018-04-23 17:24:56 +0200 | [diff] [blame] | 635 | const struct nid_path *path; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 636 | int i, n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 638 | if (nid == codec->core.afg) |
Takashi Iwai | 5ccf835 | 2015-03-18 09:23:10 +0100 | [diff] [blame] | 639 | return true; |
| 640 | |
Takashi Iwai | a9c2dfc | 2018-04-23 17:24:56 +0200 | [diff] [blame] | 641 | snd_array_for_each(&spec->paths, n, path) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 642 | if (!path->active) |
| 643 | continue; |
Takashi Iwai | 967b130 | 2015-03-20 18:21:03 +0100 | [diff] [blame] | 644 | if (codec->power_save_node) { |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 645 | if (!path->stream_enabled) |
| 646 | continue; |
| 647 | /* ignore unplugged paths except for DAC/ADC */ |
Takashi Iwai | 6b275b1 | 2015-03-20 18:11:05 +0100 | [diff] [blame] | 648 | if (!(path->pin_enabled || path->pin_fixed) && |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 649 | type != AC_WID_AUD_OUT && type != AC_WID_AUD_IN) |
| 650 | continue; |
| 651 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 652 | for (i = 0; i < path->depth; i++) { |
| 653 | if (path->path[i] == nid) { |
Takashi Iwai | 9d2b48f | 2015-08-24 10:45:27 +0200 | [diff] [blame] | 654 | if (dir == HDA_OUTPUT || idx == -1 || |
| 655 | path->idx[i] == idx) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 656 | return true; |
| 657 | break; |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | return false; |
| 662 | } |
| 663 | |
Takashi Iwai | b1b9fbd | 2013-05-14 12:58:47 +0200 | [diff] [blame] | 664 | /* check whether the NID is referred by any active paths */ |
| 665 | #define is_active_nid_for_any(codec, nid) \ |
Takashi Iwai | 9d2b48f | 2015-08-24 10:45:27 +0200 | [diff] [blame] | 666 | is_active_nid(codec, nid, HDA_OUTPUT, -1) |
Takashi Iwai | b1b9fbd | 2013-05-14 12:58:47 +0200 | [diff] [blame] | 667 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 668 | /* get the default amp value for the target state */ |
| 669 | static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | 8999bf0 | 2013-01-18 11:01:33 +0100 | [diff] [blame] | 670 | int dir, unsigned int caps, bool enable) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 671 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 672 | unsigned int val = 0; |
| 673 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 674 | 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 Iwai | f69910d | 2013-08-08 09:32:37 +0200 | [diff] [blame] | 679 | if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 680 | if (!enable) |
| 681 | val |= HDA_AMP_MUTE; |
| 682 | } |
| 683 | return val; |
| 684 | } |
| 685 | |
Takashi Iwai | cc26173 | 2015-03-16 10:18:08 +0100 | [diff] [blame] | 686 | /* is this a stereo widget or a stereo-to-mono mix? */ |
| 687 | static 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 703 | /* initialize the amp value (only at the first time) */ |
| 704 | static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx) |
| 705 | { |
Takashi Iwai | 8999bf0 | 2013-01-18 11:01:33 +0100 | [diff] [blame] | 706 | unsigned int caps = query_amp_caps(codec, nid, dir); |
| 707 | int val = get_amp_val_to_activate(codec, nid, dir, caps, false); |
Takashi Iwai | ef403ed | 2015-03-12 08:30:11 +0100 | [diff] [blame] | 708 | |
Takashi Iwai | cc26173 | 2015-03-16 10:18:08 +0100 | [diff] [blame] | 709 | if (is_stereo_amps(codec, nid, dir)) |
Takashi Iwai | ef403ed | 2015-03-12 08:30:11 +0100 | [diff] [blame] | 710 | 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 */ |
| 716 | static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx, |
| 717 | unsigned int mask, unsigned int val) |
| 718 | { |
Takashi Iwai | cc26173 | 2015-03-16 10:18:08 +0100 | [diff] [blame] | 719 | if (is_stereo_amps(codec, nid, dir)) |
Takashi Iwai | ef403ed | 2015-03-12 08:30:11 +0100 | [diff] [blame] | 720 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 725 | } |
| 726 | |
Takashi Iwai | 8999bf0 | 2013-01-18 11:01:33 +0100 | [diff] [blame] | 727 | /* calculate amp value mask we can modify; |
| 728 | * if the given amp is controlled by mixers, don't touch it |
| 729 | */ |
| 730 | static 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 733 | { |
Takashi Iwai | 8999bf0 | 2013-01-18 11:01:33 +0100 | [diff] [blame] | 734 | unsigned int mask = 0xff; |
| 735 | |
Takashi Iwai | f69910d | 2013-08-08 09:32:37 +0200 | [diff] [blame] | 736 | if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) { |
Takashi Iwai | 8999bf0 | 2013-01-18 11:01:33 +0100 | [diff] [blame] | 737 | 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 | |
| 748 | static 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 Iwai | 8999bf0 | 2013-01-18 11:01:33 +0100 | [diff] [blame] | 754 | 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 Iwai | ef403ed | 2015-03-12 08:30:11 +0100 | [diff] [blame] | 761 | update_amp(codec, nid, dir, idx, mask, val); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 762 | } |
| 763 | |
Takashi Iwai | e7fdd52 | 2015-12-08 17:00:42 +0100 | [diff] [blame] | 764 | static 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 774 | static 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 Iwai | e7fdd52 | 2015-12-08 17:00:42 +0100 | [diff] [blame] | 779 | check_and_activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | static 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 Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 786 | const hda_nid_t *conn; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 787 | int n, nums, idx; |
| 788 | int type; |
| 789 | hda_nid_t nid = path->path[i]; |
| 790 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 791 | nums = snd_hda_get_conn_list(codec, nid, &conn); |
Dan Carpenter | 0de7d83 | 2017-10-13 13:57:10 +0300 | [diff] [blame] | 792 | if (nums < 0) |
| 793 | return; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 794 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 805 | /* 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 Iwai | e7fdd52 | 2015-12-08 17:00:42 +0100 | [diff] [blame] | 809 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | |
Randy Dunlap | c7fabbc | 2020-08-05 19:19:26 -0700 | [diff] [blame] | 822 | /* sync power of each widget in the given path */ |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 823 | static 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 Iwai | 50fd498 | 2016-04-17 09:39:41 +0200 | [diff] [blame] | 828 | int i, state, power; |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 829 | |
| 830 | for (i = 0; i < path->depth; i++) { |
| 831 | nid = path->path[i]; |
Takashi Iwai | 2206dc9 | 2015-04-09 10:18:31 +0200 | [diff] [blame] | 832 | if (!(get_wcaps(codec, nid) & AC_WCAP_POWER)) |
| 833 | continue; |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 834 | if (nid == codec->core.afg) |
Takashi Iwai | 5ccf835 | 2015-03-18 09:23:10 +0100 | [diff] [blame] | 835 | continue; |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 836 | if (!allow_powerdown || is_active_nid_for_any(codec, nid)) |
| 837 | state = AC_PWRST_D0; |
| 838 | else |
| 839 | state = AC_PWRST_D3; |
Takashi Iwai | 50fd498 | 2016-04-17 09:39:41 +0200 | [diff] [blame] | 840 | power = snd_hda_codec_read(codec, nid, 0, |
| 841 | AC_VERB_GET_POWER_STATE, 0); |
| 842 | if (power != (state | (state << 4))) { |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 843 | snd_hda_codec_write(codec, nid, 0, |
| 844 | AC_VERB_SET_POWER_STATE, state); |
| 845 | changed = nid; |
Takashi Iwai | 48f4b3a | 2015-05-20 06:49:37 +0200 | [diff] [blame] | 846 | /* 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 Iwai | d545a57 | 2015-04-04 12:17:28 +0200 | [diff] [blame] | 853 | if (state == AC_PWRST_D0) |
| 854 | snd_hdac_regmap_sync_node(&codec->core, nid); |
Takashi Iwai | 48f4b3a | 2015-05-20 06:49:37 +0200 | [diff] [blame] | 855 | #endif |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | return changed; |
| 859 | } |
| 860 | |
| 861 | /* do sync with the last power state change */ |
| 862 | static 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 Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 870 | /** |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 879 | void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path, |
| 880 | bool enable, bool add_aamix) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | { |
Takashi Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 882 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 883 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
Takashi Iwai | c7cd0ef | 2015-08-24 10:52:06 +0200 | [diff] [blame] | 885 | path->active = enable; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 886 | |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 887 | /* make sure the widget is powered up */ |
Takashi Iwai | 967b130 | 2015-03-20 18:21:03 +0100 | [diff] [blame] | 888 | if (enable && (spec->power_down_unused || codec->power_save_node)) |
| 889 | path_power_update(codec, path, codec->power_save_node); |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 890 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 891 | for (i = path->depth - 1; i >= 0; i--) { |
Takashi Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 892 | hda_nid_t nid = path->path[i]; |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 893 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 894 | if (enable && path->multi[i]) |
Takashi Iwai | 401caff | 2018-06-27 11:43:09 +0200 | [diff] [blame] | 895 | snd_hda_codec_write_cache(codec, nid, 0, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 896 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 903 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 904 | EXPORT_SYMBOL_GPL(snd_hda_activate_path); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 905 | |
Takashi Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 906 | /* if the given path is inactive, put widgets into D3 (only if suitable) */ |
| 907 | static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path) |
| 908 | { |
| 909 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 910 | |
Takashi Iwai | 967b130 | 2015-03-20 18:21:03 +0100 | [diff] [blame] | 911 | if (!(spec->power_down_unused || codec->power_save_node) || path->active) |
Takashi Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 912 | return; |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 913 | sync_power_state_change(codec, path_power_update(codec, path, true)); |
Takashi Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 914 | } |
| 915 | |
Takashi Iwai | d5a9f1b | 2012-12-20 15:36:30 +0100 | [diff] [blame] | 916 | /* turn on/off EAPD on the given pin */ |
| 917 | static 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 Iwai | 05909d5c | 2013-05-31 19:55:54 +0200 | [diff] [blame] | 923 | if (spec->keep_eapd_on && !enable) |
| 924 | return; |
Takashi Iwai | 468ac41 | 2013-11-12 11:36:00 +0100 | [diff] [blame] | 925 | if (codec->inv_eapd) |
| 926 | enable = !enable; |
Takashi Iwai | 401caff | 2018-06-27 11:43:09 +0200 | [diff] [blame] | 927 | snd_hda_codec_write_cache(codec, pin, 0, |
Takashi Iwai | d5a9f1b | 2012-12-20 15:36:30 +0100 | [diff] [blame] | 928 | AC_VERB_SET_EAPD_BTLENABLE, |
| 929 | enable ? 0x02 : 0x00); |
| 930 | } |
| 931 | |
Takashi Iwai | 3e367f1 | 2013-01-23 17:07:23 +0100 | [diff] [blame] | 932 | /* re-initialize the path specified by the given path index */ |
| 933 | static 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 940 | |
| 941 | /* |
| 942 | * Helper functions for creating mixer ctl elements |
| 943 | */ |
| 944 | |
Takashi Iwai | 7eebffd | 2013-06-24 16:00:21 +0200 | [diff] [blame] | 945 | static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol, |
| 946 | struct snd_ctl_elem_value *ucontrol); |
Takashi Iwai | 698f5ee | 2017-05-10 12:13:45 +0200 | [diff] [blame] | 947 | static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol, |
| 948 | struct snd_ctl_elem_value *ucontrol); |
Takashi Iwai | bc2eee2 | 2013-08-09 15:05:03 +0200 | [diff] [blame] | 949 | static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol, |
| 950 | struct snd_ctl_elem_value *ucontrol); |
Takashi Iwai | 7eebffd | 2013-06-24 16:00:21 +0200 | [diff] [blame] | 951 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 952 | enum { |
| 953 | HDA_CTL_WIDGET_VOL, |
| 954 | HDA_CTL_WIDGET_MUTE, |
| 955 | HDA_CTL_BIND_MUTE, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 956 | }; |
| 957 | static const struct snd_kcontrol_new control_templates[] = { |
| 958 | HDA_CODEC_VOLUME(NULL, 0, 0, 0), |
Takashi Iwai | 7eebffd | 2013-06-24 16:00:21 +0200 | [diff] [blame] | 959 | /* 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 Iwai | bc2eee2 | 2013-08-09 15:05:03 +0200 | [diff] [blame] | 968 | { |
| 969 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 970 | .info = snd_hda_mixer_amp_switch_info, |
Takashi Iwai | 698f5ee | 2017-05-10 12:13:45 +0200 | [diff] [blame] | 971 | .get = hda_gen_bind_mute_get, |
Takashi Iwai | bc2eee2 | 2013-08-09 15:05:03 +0200 | [diff] [blame] | 972 | .put = hda_gen_bind_mute_put, /* replaced */ |
| 973 | .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0), |
| 974 | }, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 975 | }; |
| 976 | |
| 977 | /* add dynamic controls from template */ |
Takashi Iwai | a35bd1e | 2013-01-18 14:01:14 +0100 | [diff] [blame] | 978 | static struct snd_kcontrol_new * |
| 979 | add_control(struct hda_gen_spec *spec, int type, const char *name, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 980 | int cidx, unsigned long val) |
| 981 | { |
| 982 | struct snd_kcontrol_new *knew; |
| 983 | |
Takashi Iwai | 12c93df | 2012-12-19 14:38:33 +0100 | [diff] [blame] | 984 | knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 985 | if (!knew) |
Takashi Iwai | a35bd1e | 2013-01-18 14:01:14 +0100 | [diff] [blame] | 986 | return NULL; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 987 | knew->index = cidx; |
| 988 | if (get_amp_nid_(val)) |
| 989 | knew->subdevice = HDA_SUBDEV_AMP_FLAG; |
Jaroslav Kysela | e65bf99 | 2021-03-17 18:29:43 +0100 | [diff] [blame] | 990 | if (knew->access == 0) |
| 991 | knew->access = SNDRV_CTL_ELEM_ACCESS_READWRITE; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 992 | knew->private_value = val; |
Takashi Iwai | a35bd1e | 2013-01-18 14:01:14 +0100 | [diff] [blame] | 993 | return knew; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | static 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 Iwai | 975cc02 | 2013-06-28 11:56:49 +0200 | [diff] [blame] | 1000 | char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1001 | snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx); |
Takashi Iwai | a35bd1e | 2013-01-18 14:01:14 +0100 | [diff] [blame] | 1002 | if (!add_control(spec, type, name, cidx, val)) |
| 1003 | return -ENOMEM; |
| 1004 | return 0; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1005 | } |
| 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 | |
| 1016 | static 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[] */ |
| 1030 | static 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 | |
| 1042 | static 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 | */ |
| 1052 | static 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 | |
| 1075 | static 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 Iwai | 7eebffd | 2013-06-24 16:00:21 +0200 | [diff] [blame] | 1082 | /* playback mute control with the software mute bit check */ |
Takashi Iwai | bc2eee2 | 2013-08-09 15:05:03 +0200 | [diff] [blame] | 1083 | static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol, |
| 1084 | struct snd_ctl_elem_value *ucontrol) |
Takashi Iwai | 7eebffd | 2013-06-24 16:00:21 +0200 | [diff] [blame] | 1085 | { |
| 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 Iwai | bc2eee2 | 2013-08-09 15:05:03 +0200 | [diff] [blame] | 1095 | } |
Takashi Iwai | 7eebffd | 2013-06-24 16:00:21 +0200 | [diff] [blame] | 1096 | |
Takashi Iwai | bc2eee2 | 2013-08-09 15:05:03 +0200 | [diff] [blame] | 1097 | static 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 Iwai | 7eebffd | 2013-06-24 16:00:21 +0200 | [diff] [blame] | 1101 | return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); |
| 1102 | } |
| 1103 | |
Takashi Iwai | 698f5ee | 2017-05-10 12:13:45 +0200 | [diff] [blame] | 1104 | /* |
| 1105 | * Bound mute controls |
| 1106 | */ |
| 1107 | #define AMP_VAL_IDX_SHIFT 19 |
| 1108 | #define AMP_VAL_IDX_MASK (0x0f<<19) |
| 1109 | |
| 1110 | static 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 Iwai | bc2eee2 | 2013-08-09 15:05:03 +0200 | [diff] [blame] | 1126 | static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol, |
| 1127 | struct snd_ctl_elem_value *ucontrol) |
| 1128 | { |
Takashi Iwai | 698f5ee | 2017-05-10 12:13:45 +0200 | [diff] [blame] | 1129 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1130 | unsigned long pval; |
| 1131 | int i, indices, err = 0, change = 0; |
| 1132 | |
Takashi Iwai | bc2eee2 | 2013-08-09 15:05:03 +0200 | [diff] [blame] | 1133 | sync_auto_mute_bits(kcontrol, ucontrol); |
Takashi Iwai | 698f5ee | 2017-05-10 12:13:45 +0200 | [diff] [blame] | 1134 | |
| 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 Iwai | bc2eee2 | 2013-08-09 15:05:03 +0200 | [diff] [blame] | 1149 | } |
| 1150 | |
Takashi Iwai | 247d85e | 2013-01-17 16:18:11 +0100 | [diff] [blame] | 1151 | /* any ctl assigned to the path with the given index? */ |
| 1152 | static 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1158 | static 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 Iwai | 247d85e | 2013-01-17 16:18:11 +0100 | [diff] [blame] | 1163 | static const char *get_line_out_pfx(struct hda_codec *codec, int ch, |
| 1164 | int *index, int ctl_type) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1165 | { |
Takashi Iwai | 247d85e | 2013-01-17 16:18:11 +0100 | [diff] [blame] | 1166 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1167 | struct auto_pin_cfg *cfg = &spec->autocfg; |
| 1168 | |
| 1169 | *index = 0; |
| 1170 | if (cfg->line_outs == 1 && !spec->multi_ios && |
Takashi Iwai | 9f3dadb | 2017-04-10 17:12:33 +0200 | [diff] [blame] | 1171 | !codec->force_pin_prefix && |
Takashi Iwai | 247d85e | 2013-01-17 16:18:11 +0100 | [diff] [blame] | 1172 | !cfg->hp_outs && !cfg->speaker_outs) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1173 | 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 Iwai | 9f3dadb | 2017-04-10 17:12:33 +0200 | [diff] [blame] | 1179 | !codec->force_pin_prefix && |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1180 | !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0]) |
| 1181 | return spec->vmaster_mute.hook ? "PCM" : "Master"; |
| 1182 | |
Takashi Iwai | 247d85e | 2013-01-17 16:18:11 +0100 | [diff] [blame] | 1183 | /* multi-io channels */ |
| 1184 | if (ch >= cfg->line_outs) |
| 1185 | return channel_name[ch]; |
| 1186 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1187 | switch (cfg->line_out_type) { |
| 1188 | case AUTO_PIN_SPEAKER_OUT: |
Takashi Iwai | 247d85e | 2013-01-17 16:18:11 +0100 | [diff] [blame] | 1189 | /* 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1195 | 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 Iwai | 247d85e | 2013-01-17 16:18:11 +0100 | [diff] [blame] | 1201 | /* 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1207 | /* for multi-io case, only the primary out */ |
| 1208 | if (ch && spec->multi_ios) |
| 1209 | break; |
| 1210 | *index = ch; |
| 1211 | return "Headphone"; |
David Henningsson | 03ad6a8 | 2014-10-16 15:33:45 +0200 | [diff] [blame] | 1212 | case AUTO_PIN_LINE_OUT: |
Hui Wang | f48652b | 2021-05-04 15:39:17 +0800 | [diff] [blame] | 1213 | /* 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 Henningsson | 03ad6a8 | 2014-10-16 15:33:45 +0200 | [diff] [blame] | 1224 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1231 | } |
Takashi Iwai | 247d85e | 2013-01-17 16:18:11 +0100 | [diff] [blame] | 1232 | |
| 1233 | /* for a single channel output, we don't have to name the channel */ |
| 1234 | if (cfg->line_outs == 1 && !spec->multi_ios) |
David Henningsson | 3abb4f4 | 2014-10-16 15:33:46 +0200 | [diff] [blame] | 1235 | return "Line Out"; |
Takashi Iwai | 247d85e | 2013-01-17 16:18:11 +0100 | [diff] [blame] | 1236 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1237 | 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 */ |
| 1250 | enum { |
| 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 Iwai | 1d73906 | 2013-02-13 14:17:32 +0100 | [diff] [blame] | 1256 | BAD_MULTI_IO = 0x120, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1257 | /* 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 Iwai | 55a63d4 | 2013-03-21 17:20:12 +0100 | [diff] [blame] | 1263 | /* No independent HP possible */ |
Takashi Iwai | bec8e68 | 2013-03-22 15:10:08 +0100 | [diff] [blame] | 1264 | BAD_NO_INDEP_HP = 0x10, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1265 | /* 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 Iwai | 0e614dd | 2013-01-07 15:11:44 +0100 | [diff] [blame] | 1273 | /* look for widgets in the given path which are appropriate for |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1274 | * 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 Iwai | 0e614dd | 2013-01-07 15:11:44 +0100 | [diff] [blame] | 1280 | static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1281 | { |
Takashi Iwai | d89c6c0 | 2014-09-01 10:07:04 +0200 | [diff] [blame] | 1282 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1283 | hda_nid_t nid; |
| 1284 | unsigned int val; |
| 1285 | int badness = 0; |
| 1286 | |
| 1287 | if (!path) |
| 1288 | return BAD_SHARED_VOL * 2; |
Takashi Iwai | 0e614dd | 2013-01-07 15:11:44 +0100 | [diff] [blame] | 1289 | |
| 1290 | if (path->ctls[NID_PATH_VOL_CTL] || |
| 1291 | path->ctls[NID_PATH_MUTE_CTL]) |
| 1292 | return 0; /* already evaluated */ |
| 1293 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1294 | nid = look_for_out_vol_nid(codec, path); |
| 1295 | if (nid) { |
| 1296 | val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT); |
Takashi Iwai | d89c6c0 | 2014-09-01 10:07:04 +0200 | [diff] [blame] | 1297 | if (spec->dac_min_mute) |
| 1298 | val |= HDA_AMP_VAL_MIN_MUTE; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1299 | 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 Iwai | 98bd111 | 2013-03-22 14:53:50 +0100 | [diff] [blame] | 1322 | const struct badness_table hda_main_out_badness = { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1323 | .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 Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1330 | EXPORT_SYMBOL_GPL(hda_main_out_badness); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1331 | |
Takashi Iwai | 98bd111 | 2013-03-22 14:53:50 +0100 | [diff] [blame] | 1332 | const struct badness_table hda_extra_out_badness = { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1333 | .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 Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 1340 | EXPORT_SYMBOL_GPL(hda_extra_out_badness); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1341 | |
Takashi Iwai | 7385df6 | 2013-01-07 09:50:52 +0100 | [diff] [blame] | 1342 | /* get the DAC of the primary output corresponding to the given array index */ |
| 1343 | static 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 */ |
| 1357 | static 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1363 | /* try to assign DACs to pins and return the resultant badness */ |
| 1364 | static int try_assign_dacs(struct hda_codec *codec, int num_outs, |
| 1365 | const hda_nid_t *pins, hda_nid_t *dacs, |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1366 | int *path_idx, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1367 | const struct badness_table *bad) |
| 1368 | { |
| 1369 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1370 | int i, j; |
| 1371 | int badness = 0; |
| 1372 | hda_nid_t dac; |
| 1373 | |
| 1374 | if (!num_outs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | return 0; |
| 1376 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1377 | for (i = 0; i < num_outs; i++) { |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 1378 | struct nid_path *path; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1379 | hda_nid_t pin = pins[i]; |
Takashi Iwai | 1e0b528 | 2013-01-04 12:56:52 +0100 | [diff] [blame] | 1380 | |
Takashi Iwai | 242d990 | 2020-11-27 15:11:03 +0100 | [diff] [blame] | 1381 | 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 Iwai | 1e0b528 | 2013-01-04 12:56:52 +0100 | [diff] [blame] | 1387 | } |
| 1388 | |
Takashi Iwai | 3690739 | 2013-12-10 17:29:26 +0100 | [diff] [blame] | 1389 | 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 Iwai | 242d990 | 2020-11-27 15:11:03 +0100 | [diff] [blame] | 1393 | } else if (spec->obey_preferred_dacs) { |
| 1394 | badness += BAD_NO_PRIMARY_DAC; |
Takashi Iwai | 3690739 | 2013-12-10 17:29:26 +0100 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | if (!dacs[i]) |
| 1398 | dacs[i] = look_for_dac(codec, pin, false); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1399 | if (!dacs[i] && !i) { |
Takashi Iwai | 980428c | 2013-01-09 09:28:20 +0100 | [diff] [blame] | 1400 | /* try to steal the DAC of surrounds for the front */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1401 | 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 Iwai | 980428c | 2013-01-09 09:28:20 +0100 | [diff] [blame] | 1405 | invalidate_nid_path(codec, path_idx[j]); |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1406 | path_idx[j] = 0; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1407 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | } |
| 1409 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1410 | } |
| 1411 | dac = dacs[i]; |
| 1412 | if (!dac) { |
Takashi Iwai | 7385df6 | 2013-01-07 09:50:52 +0100 | [diff] [blame] | 1413 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1419 | 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 Iwai | 1fa335b | 2013-01-21 11:43:19 +0100 | [diff] [blame] | 1434 | if (!dac) |
| 1435 | continue; |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 1436 | path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid); |
Takashi Iwai | 117688a | 2013-01-04 15:41:41 +0100 | [diff] [blame] | 1437 | if (!path && !i && spec->mixer_nid) { |
Takashi Iwai | b3a8c74 | 2012-12-20 18:29:16 +0100 | [diff] [blame] | 1438 | /* try with aamix */ |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 1439 | path = snd_hda_add_new_path(codec, dac, pin, 0); |
Takashi Iwai | b3a8c74 | 2012-12-20 18:29:16 +0100 | [diff] [blame] | 1440 | } |
Takashi Iwai | 1fa335b | 2013-01-21 11:43:19 +0100 | [diff] [blame] | 1441 | if (!path) { |
Jiapeng Chong | e73b4c9 | 2021-05-13 19:11:11 +0800 | [diff] [blame] | 1442 | dacs[i] = 0; |
Takashi Iwai | 1fa335b | 2013-01-21 11:43:19 +0100 | [diff] [blame] | 1443 | badness += bad->no_dac; |
| 1444 | } else { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1445 | /* print_nid_path(codec, "output", path); */ |
Takashi Iwai | e1284af | 2013-01-03 16:33:02 +0100 | [diff] [blame] | 1446 | path->active = true; |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1447 | path_idx[i] = snd_hda_get_path_idx(codec, path); |
Takashi Iwai | 0e614dd | 2013-01-07 15:11:44 +0100 | [diff] [blame] | 1448 | badness += assign_out_path_ctls(codec, path); |
Takashi Iwai | e1284af | 2013-01-03 16:33:02 +0100 | [diff] [blame] | 1449 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | return badness; |
| 1453 | } |
| 1454 | |
| 1455 | /* return NID if the given pin has only a single connection to a certain DAC */ |
| 1456 | static 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 */ |
| 1476 | static 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 Iwai | e22aab7 | 2013-01-04 14:50:04 +0100 | [diff] [blame] | 1492 | /* count the number of input pins that are capable to be multi-io */ |
| 1493 | static 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1514 | /* |
| 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 | */ |
| 1522 | static int fill_multi_ios(struct hda_codec *codec, |
| 1523 | hda_nid_t reference_pin, |
Takashi Iwai | e22aab7 | 2013-01-04 14:50:04 +0100 | [diff] [blame] | 1524 | bool hardwired) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1525 | { |
| 1526 | struct hda_gen_spec *spec = codec->spec; |
| 1527 | struct auto_pin_cfg *cfg = &spec->autocfg; |
Takashi Iwai | e22aab7 | 2013-01-04 14:50:04 +0100 | [diff] [blame] | 1528 | int type, i, j, num_pins, old_pins; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1529 | unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin); |
| 1530 | unsigned int location = get_defcfg_location(defcfg); |
| 1531 | int badness = 0; |
Takashi Iwai | 0e614dd | 2013-01-07 15:11:44 +0100 | [diff] [blame] | 1532 | struct nid_path *path; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1533 | |
| 1534 | old_pins = spec->multi_ios; |
| 1535 | if (old_pins >= 2) |
| 1536 | goto end_fill; |
| 1537 | |
Takashi Iwai | e22aab7 | 2013-01-04 14:50:04 +0100 | [diff] [blame] | 1538 | num_pins = count_multiio_pins(codec, reference_pin); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1539 | if (num_pins < 2) |
| 1540 | goto end_fill; |
| 1541 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1542 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1558 | 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 Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 1566 | path = snd_hda_add_new_path(codec, dac, nid, |
| 1567 | -spec->mixer_nid); |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 1568 | if (!path) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1569 | badness++; |
| 1570 | continue; |
| 1571 | } |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1572 | /* print_nid_path(codec, "multiio", path); */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1573 | spec->multi_io[spec->multi_ios].pin = nid; |
| 1574 | spec->multi_io[spec->multi_ios].dac = dac; |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1575 | spec->out_paths[cfg->line_outs + spec->multi_ios] = |
| 1576 | snd_hda_get_path_idx(codec, path); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1577 | 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 Iwai | 0e614dd | 2013-01-07 15:11:44 +0100 | [diff] [blame] | 1599 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1603 | |
| 1604 | return badness; |
| 1605 | } |
| 1606 | |
| 1607 | /* map DACs for all pins in the list if they are single connections */ |
| 1608 | static bool map_singles(struct hda_codec *codec, int outs, |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1609 | const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1610 | { |
Takashi Iwai | b3a8c74 | 2012-12-20 18:29:16 +0100 | [diff] [blame] | 1611 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1612 | int i; |
| 1613 | bool found = false; |
| 1614 | for (i = 0; i < outs; i++) { |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 1615 | struct nid_path *path; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1616 | 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 Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 1622 | path = snd_hda_add_new_path(codec, dac, pins[i], |
| 1623 | -spec->mixer_nid); |
Takashi Iwai | 117688a | 2013-01-04 15:41:41 +0100 | [diff] [blame] | 1624 | if (!path && !i && spec->mixer_nid) |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 1625 | path = snd_hda_add_new_path(codec, dac, pins[i], 0); |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 1626 | if (path) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1627 | dacs[i] = dac; |
| 1628 | found = true; |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1629 | /* print_nid_path(codec, "output", path); */ |
Takashi Iwai | e1284af | 2013-01-03 16:33:02 +0100 | [diff] [blame] | 1630 | path->active = true; |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1631 | path_idx[i] = snd_hda_get_path_idx(codec, path); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1632 | } |
| 1633 | } |
| 1634 | return found; |
| 1635 | } |
| 1636 | |
Takashi Iwai | e7fdd52 | 2015-12-08 17:00:42 +0100 | [diff] [blame] | 1637 | static 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 Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 1643 | /* create a new path including aamix if available, and return its index */ |
| 1644 | static int check_aamix_out_path(struct hda_codec *codec, int path_idx) |
| 1645 | { |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 1646 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 1647 | struct nid_path *path; |
Takashi Iwai | 5ead56f | 2013-04-16 14:16:54 +0200 | [diff] [blame] | 1648 | hda_nid_t path_dac, dac, pin; |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 1649 | |
| 1650 | path = snd_hda_get_path_from_idx(codec, path_idx); |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 1651 | if (!path || !path->depth || |
| 1652 | is_nid_contained(path, spec->mixer_nid)) |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 1653 | return 0; |
Takashi Iwai | 5ead56f | 2013-04-16 14:16:54 +0200 | [diff] [blame] | 1654 | path_dac = path->path[0]; |
| 1655 | dac = spec->private_dac_nids[0]; |
Takashi Iwai | f87498b | 2013-01-21 14:24:31 +0100 | [diff] [blame] | 1656 | pin = path->path[path->depth - 1]; |
| 1657 | path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid); |
| 1658 | if (!path) { |
Takashi Iwai | 5ead56f | 2013-04-16 14:16:54 +0200 | [diff] [blame] | 1659 | if (dac != path_dac) |
| 1660 | dac = path_dac; |
Takashi Iwai | f87498b | 2013-01-21 14:24:31 +0100 | [diff] [blame] | 1661 | 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 Iwai | 5ead56f | 2013-04-16 14:16:54 +0200 | [diff] [blame] | 1665 | else |
| 1666 | dac = 0; |
Takashi Iwai | f87498b | 2013-01-21 14:24:31 +0100 | [diff] [blame] | 1667 | if (dac) |
| 1668 | path = snd_hda_add_new_path(codec, dac, pin, |
| 1669 | spec->mixer_nid); |
| 1670 | } |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 1671 | if (!path) |
| 1672 | return 0; |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1673 | /* print_nid_path(codec, "output-aamix", path); */ |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 1674 | path->active = false; /* unused as default */ |
Takashi Iwai | 6b275b1 | 2015-03-20 18:11:05 +0100 | [diff] [blame] | 1675 | path->pin_fixed = true; /* static route */ |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 1676 | return snd_hda_get_path_idx(codec, path); |
| 1677 | } |
| 1678 | |
Takashi Iwai | 55a63d4 | 2013-03-21 17:20:12 +0100 | [diff] [blame] | 1679 | /* check whether the independent HP is available with the current config */ |
| 1680 | static 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 Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 1716 | /* fill the empty entries in the dac array for speaker/hp with the |
| 1717 | * shared dac pointed by the paths |
| 1718 | */ |
| 1719 | static 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1735 | /* fill in the dac_nids table from the parsed pin configuration */ |
| 1736 | static 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 Iwai | cd5be3f | 2013-01-07 15:07:00 +0100 | [diff] [blame] | 1752 | |
| 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 Iwai | c697b71 | 2013-01-07 17:09:26 +0100 | [diff] [blame] | 1759 | memset(spec->input_paths, 0, sizeof(spec->input_paths)); |
Takashi Iwai | cd5be3f | 2013-01-07 15:07:00 +0100 | [diff] [blame] | 1760 | memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths)); |
| 1761 | memset(&spec->digin_path, 0, sizeof(spec->digin_path)); |
| 1762 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1763 | 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 Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1771 | spec->private_dac_nids, |
| 1772 | spec->out_paths); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1773 | mapped |= map_singles(codec, cfg->hp_outs, |
| 1774 | cfg->hp_pins, |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1775 | spec->multiout.hp_out_nid, |
| 1776 | spec->hp_paths); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1777 | mapped |= map_singles(codec, cfg->speaker_outs, |
| 1778 | cfg->speaker_pins, |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1779 | spec->multiout.extra_out_nid, |
| 1780 | spec->speaker_paths); |
Takashi Iwai | da96fb5 | 2013-07-29 16:54:36 +0200 | [diff] [blame] | 1781 | if (!spec->no_multi_io && |
| 1782 | fill_mio_first && cfg->line_outs == 1 && |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1783 | cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) { |
Takashi Iwai | e22aab7 | 2013-01-04 14:50:04 +0100 | [diff] [blame] | 1784 | err = fill_multi_ios(codec, cfg->line_out_pins[0], true); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1785 | 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 Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1792 | spec->private_dac_nids, spec->out_paths, |
Takashi Iwai | 98bd111 | 2013-03-22 14:53:50 +0100 | [diff] [blame] | 1793 | spec->main_out_badness); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1794 | |
Takashi Iwai | da96fb5 | 2013-07-29 16:54:36 +0200 | [diff] [blame] | 1795 | if (!spec->no_multi_io && fill_mio_first && |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1796 | cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) { |
| 1797 | /* try to fill multi-io first */ |
Takashi Iwai | e22aab7 | 2013-01-04 14:50:04 +0100 | [diff] [blame] | 1798 | err = fill_multi_ios(codec, cfg->line_out_pins[0], false); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1799 | 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 Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1807 | spec->hp_paths, |
Takashi Iwai | 98bd111 | 2013-03-22 14:53:50 +0100 | [diff] [blame] | 1808 | spec->extra_out_badness); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1809 | 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 Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1817 | spec->speaker_paths, |
Takashi Iwai | 98bd111 | 2013-03-22 14:53:50 +0100 | [diff] [blame] | 1818 | spec->extra_out_badness); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1819 | if (err < 0) |
| 1820 | return err; |
| 1821 | badness += err; |
| 1822 | } |
Takashi Iwai | da96fb5 | 2013-07-29 16:54:36 +0200 | [diff] [blame] | 1823 | if (!spec->no_multi_io && |
| 1824 | cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) { |
Takashi Iwai | e22aab7 | 2013-01-04 14:50:04 +0100 | [diff] [blame] | 1825 | err = fill_multi_ios(codec, cfg->line_out_pins[0], false); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1826 | if (err < 0) |
| 1827 | return err; |
| 1828 | badness += err; |
| 1829 | } |
Takashi Iwai | e22aab7 | 2013-01-04 14:50:04 +0100 | [diff] [blame] | 1830 | |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 1831 | 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 Iwai | da96fb5 | 2013-07-29 16:54:36 +0200 | [diff] [blame] | 1842 | if (!spec->no_multi_io && |
| 1843 | cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) |
Takashi Iwai | e22aab7 | 2013-01-04 14:50:04 +0100 | [diff] [blame] | 1844 | if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2) |
| 1845 | spec->multi_ios = 1; /* give badness */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1846 | |
Takashi Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 1847 | /* 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 Henningsson | c0f3b21 | 2013-01-16 11:45:37 +0100 | [diff] [blame] | 1861 | spec->multiout.num_dacs * 2; |
Takashi Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 1862 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1863 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1867 | } else if (spec->multi_ios) { |
| 1868 | spec->multi_ios = 0; |
| 1869 | badness += BAD_MULTI_IO; |
| 1870 | } |
| 1871 | |
Takashi Iwai | 55a63d4 | 2013-03-21 17:20:12 +0100 | [diff] [blame] | 1872 | if (spec->indep_hp && !indep_hp_possible(codec)) |
| 1873 | badness += BAD_NO_INDEP_HP; |
| 1874 | |
Takashi Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 1875 | /* 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1885 | return badness; |
| 1886 | } |
| 1887 | |
| 1888 | #define DEBUG_BADNESS |
| 1889 | |
| 1890 | #ifdef DEBUG_BADNESS |
Joe Perches | d82353e | 2014-07-05 13:02:02 -0700 | [diff] [blame] | 1891 | #define debug_badness(fmt, ...) \ |
| 1892 | codec_dbg(codec, fmt, ##__VA_ARGS__) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1893 | #else |
Joe Perches | d82353e | 2014-07-05 13:02:02 -0700 | [diff] [blame] | 1894 | #define debug_badness(fmt, ...) \ |
| 1895 | do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1896 | #endif |
| 1897 | |
Takashi Iwai | a769409 | 2013-01-21 10:43:18 +0100 | [diff] [blame] | 1898 | #ifdef DEBUG_BADNESS |
| 1899 | static inline void print_nid_path_idx(struct hda_codec *codec, |
| 1900 | const char *pfx, int idx) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1901 | { |
Takashi Iwai | a769409 | 2013-01-21 10:43:18 +0100 | [diff] [blame] | 1902 | struct nid_path *path; |
| 1903 | |
| 1904 | path = snd_hda_get_path_from_idx(codec, idx); |
| 1905 | if (path) |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1906 | print_nid_path(codec, pfx, path); |
Takashi Iwai | a769409 | 2013-01-21 10:43:18 +0100 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | static void debug_show_configs(struct hda_codec *codec, |
| 1910 | struct auto_pin_cfg *cfg) |
| 1911 | { |
| 1912 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | a769409 | 2013-01-21 10:43:18 +0100 | [diff] [blame] | 1913 | static const char * const lo_type[3] = { "LO", "SP", "HP" }; |
Takashi Iwai | a769409 | 2013-01-21 10:43:18 +0100 | [diff] [blame] | 1914 | int i; |
| 1915 | |
| 1916 | debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n", |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1917 | cfg->line_out_pins[0], cfg->line_out_pins[1], |
Takashi Iwai | 708122e | 2012-12-20 17:56:57 +0100 | [diff] [blame] | 1918 | cfg->line_out_pins[2], cfg->line_out_pins[3], |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1919 | spec->multiout.dac_nids[0], |
| 1920 | spec->multiout.dac_nids[1], |
| 1921 | spec->multiout.dac_nids[2], |
Takashi Iwai | a769409 | 2013-01-21 10:43:18 +0100 | [diff] [blame] | 1922 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1926 | 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 Iwai | a769409 | 2013-01-21 10:43:18 +0100 | [diff] [blame] | 1931 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1936 | cfg->hp_pins[0], cfg->hp_pins[1], |
Takashi Iwai | 708122e | 2012-12-20 17:56:57 +0100 | [diff] [blame] | 1937 | cfg->hp_pins[2], cfg->hp_pins[3], |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1938 | 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 Iwai | a769409 | 2013-01-21 10:43:18 +0100 | [diff] [blame] | 1942 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1946 | 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 Iwai | a769409 | 2013-01-21 10:43:18 +0100 | [diff] [blame] | 1952 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1956 | } |
Takashi Iwai | a769409 | 2013-01-21 10:43:18 +0100 | [diff] [blame] | 1957 | #else |
| 1958 | #define debug_show_configs(codec, cfg) /* NOP */ |
| 1959 | #endif |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1960 | |
| 1961 | /* find all available DACs of the codec */ |
| 1962 | static void fill_all_dac_nids(struct hda_codec *codec) |
| 1963 | { |
| 1964 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 1965 | hda_nid_t nid; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1966 | |
| 1967 | spec->num_all_dacs = 0; |
| 1968 | memset(spec->all_dacs, 0, sizeof(spec->all_dacs)); |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 1969 | for_each_hda_codec_node(nid, codec) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1970 | 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 Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 1973 | codec_err(codec, "Too many DACs!\n"); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1974 | break; |
| 1975 | } |
| 1976 | spec->all_dacs[spec->num_all_dacs++] = nid; |
| 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | static 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 Iwai | 9314a58 | 2013-01-21 10:49:05 +0100 | [diff] [blame] | 1985 | unsigned int val; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1986 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1992 | 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 Iwai | a769409 | 2013-01-21 10:43:18 +0100 | [diff] [blame] | 2007 | debug_show_configs(codec, cfg); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2008 | 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 Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 2057 | debug_badness("==> restoring best_cfg\n"); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2058 | *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 Iwai | a769409 | 2013-01-21 10:43:18 +0100 | [diff] [blame] | 2063 | debug_show_configs(codec, cfg); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2064 | |
| 2065 | if (cfg->line_out_pins[0]) { |
| 2066 | struct nid_path *path; |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 2067 | path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2068 | if (path) |
| 2069 | spec->vmaster_nid = look_for_out_vol_nid(codec, path); |
Takashi Iwai | d89c6c0 | 2014-09-01 10:07:04 +0200 | [diff] [blame] | 2070 | if (spec->vmaster_nid) { |
Takashi Iwai | 7a71bbf | 2013-01-17 10:25:15 +0100 | [diff] [blame] | 2071 | snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid, |
| 2072 | HDA_OUTPUT, spec->vmaster_tlv); |
Takashi Iwai | d89c6c0 | 2014-09-01 10:07:04 +0200 | [diff] [blame] | 2073 | if (spec->dac_min_mute) |
Takashi Sakamoto | 51cdc8b | 2018-05-14 07:09:52 +0900 | [diff] [blame] | 2074 | spec->vmaster_tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] |= TLV_DB_SCALE_MUTE; |
Takashi Iwai | d89c6c0 | 2014-09-01 10:07:04 +0200 | [diff] [blame] | 2075 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2076 | } |
| 2077 | |
Takashi Iwai | 9314a58 | 2013-01-21 10:49:05 +0100 | [diff] [blame] | 2078 | /* 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 Iwai | 55a63d4 | 2013-03-21 17:20:12 +0100 | [diff] [blame] | 2092 | /* clear indep_hp flag if not available */ |
| 2093 | if (spec->indep_hp && !indep_hp_possible(codec)) |
| 2094 | spec->indep_hp = 0; |
| 2095 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2096 | kfree(best_cfg); |
| 2097 | return 0; |
| 2098 | } |
| 2099 | |
| 2100 | /* add playback controls from the parsed DAC table */ |
| 2101 | static 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2114 | struct nid_path *path; |
| 2115 | |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 2116 | path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2117 | if (!path) |
| 2118 | continue; |
Takashi Iwai | 247d85e | 2013-01-17 16:18:11 +0100 | [diff] [blame] | 2119 | |
| 2120 | name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2121 | 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 Iwai | 247d85e | 2013-01-17 16:18:11 +0100 | [diff] [blame] | 2129 | } 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2137 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2144 | err = add_stereo_sw(codec, name, index, path); |
| 2145 | if (err < 0) |
| 2146 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | } |
| 2148 | } |
| 2149 | return 0; |
| 2150 | } |
| 2151 | |
Takashi Iwai | c2c8038 | 2013-01-07 10:33:57 +0100 | [diff] [blame] | 2152 | static int create_extra_out(struct hda_codec *codec, int path_idx, |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 2153 | const char *pfx, int cidx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2155 | struct nid_path *path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | int err; |
| 2157 | |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 2158 | path = snd_hda_get_path_from_idx(codec, path_idx); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2159 | if (!path) |
| 2160 | return 0; |
Takashi Iwai | c2c8038 | 2013-01-07 10:33:57 +0100 | [diff] [blame] | 2161 | err = add_stereo_vol(codec, pfx, cidx, path); |
| 2162 | if (err < 0) |
| 2163 | return err; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2164 | err = add_stereo_sw(codec, pfx, cidx, path); |
| 2165 | if (err < 0) |
| 2166 | return err; |
| 2167 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | } |
| 2169 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2170 | /* add playback controls for speaker and HP outputs */ |
| 2171 | static int create_extra_outs(struct hda_codec *codec, int num_pins, |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 2172 | const int *paths, const char *pfx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | { |
Takashi Iwai | c2c8038 | 2013-01-07 10:33:57 +0100 | [diff] [blame] | 2174 | int i; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2175 | |
| 2176 | for (i = 0; i < num_pins; i++) { |
Takashi Iwai | c2c8038 | 2013-01-07 10:33:57 +0100 | [diff] [blame] | 2177 | const char *name; |
Takashi Iwai | 975cc02 | 2013-06-28 11:56:49 +0200 | [diff] [blame] | 2178 | char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; |
Takashi Iwai | c2c8038 | 2013-01-07 10:33:57 +0100 | [diff] [blame] | 2179 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2185 | pfx, channel_name[i]); |
Takashi Iwai | c2c8038 | 2013-01-07 10:33:57 +0100 | [diff] [blame] | 2186 | name = tmp; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2187 | } else { |
Takashi Iwai | c2c8038 | 2013-01-07 10:33:57 +0100 | [diff] [blame] | 2188 | name = pfx; |
| 2189 | idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | } |
Takashi Iwai | c2c8038 | 2013-01-07 10:33:57 +0100 | [diff] [blame] | 2191 | err = create_extra_out(codec, paths[i], name, idx); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2192 | if (err < 0) |
| 2193 | return err; |
| 2194 | } |
| 2195 | return 0; |
| 2196 | } |
Takashi Iwai | 97ec558 | 2006-03-21 11:29:07 +0100 | [diff] [blame] | 2197 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2198 | static 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 Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 2202 | spec->hp_paths, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2203 | "Headphone"); |
| 2204 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2206 | static 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 Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 2210 | spec->speaker_paths, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2211 | "Speaker"); |
| 2212 | } |
| 2213 | |
| 2214 | /* |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 2215 | * independent HP controls |
| 2216 | */ |
| 2217 | |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 2218 | static void call_hp_automute(struct hda_codec *codec, |
| 2219 | struct hda_jack_callback *jack); |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 2220 | static 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 | |
| 2226 | static 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 Iwai | a1e908e | 2013-01-21 15:11:25 +0100 | [diff] [blame] | 2235 | static 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 Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 2239 | static 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 Iwai | a1e908e | 2013-01-21 15:11:25 +0100 | [diff] [blame] | 2254 | 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 Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 2274 | spec->indep_hp_enabled = select; |
| 2275 | if (spec->indep_hp_enabled) |
Takashi Iwai | a1e908e | 2013-01-21 15:11:25 +0100 | [diff] [blame] | 2276 | *dacp = 0; |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 2277 | else |
Takashi Iwai | a1e908e | 2013-01-21 15:11:25 +0100 | [diff] [blame] | 2278 | *dacp = spec->alt_dac_nid; |
Takashi Iwai | 92603c5 | 2013-01-22 07:46:31 +0100 | [diff] [blame] | 2279 | |
Takashi Iwai | 963afde | 2013-05-31 15:20:31 +0200 | [diff] [blame] | 2280 | call_hp_automute(codec, NULL); |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 2281 | ret = 1; |
| 2282 | } |
| 2283 | unlock: |
| 2284 | mutex_unlock(&spec->pcm_mutex); |
| 2285 | return ret; |
| 2286 | } |
| 2287 | |
| 2288 | static 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 | |
| 2297 | static int create_indep_hp_ctls(struct hda_codec *codec) |
| 2298 | { |
| 2299 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | a1e908e | 2013-01-21 15:11:25 +0100 | [diff] [blame] | 2300 | hda_nid_t dac; |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 2301 | |
| 2302 | if (!spec->indep_hp) |
| 2303 | return 0; |
Takashi Iwai | a1e908e | 2013-01-21 15:11:25 +0100 | [diff] [blame] | 2304 | 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 Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 2309 | spec->indep_hp = 0; |
| 2310 | return 0; |
| 2311 | } |
| 2312 | |
| 2313 | spec->indep_hp_enabled = false; |
Takashi Iwai | a1e908e | 2013-01-21 15:11:25 +0100 | [diff] [blame] | 2314 | spec->alt_dac_nid = dac; |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 2315 | if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl)) |
| 2316 | return -ENOMEM; |
| 2317 | return 0; |
| 2318 | } |
| 2319 | |
| 2320 | /* |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2321 | * channel mode enum control |
| 2322 | */ |
| 2323 | |
| 2324 | static 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 Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 2329 | int chs; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2330 | |
| 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 Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 2336 | chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count; |
| 2337 | sprintf(uinfo->value.enumerated.name, "%dch", chs); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2338 | return 0; |
| 2339 | } |
| 2340 | |
| 2341 | static 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 Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 2346 | ucontrol->value.enumerated.item[0] = |
| 2347 | (spec->ext_channel_count - spec->min_channel_count) / 2; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2348 | return 0; |
| 2349 | } |
| 2350 | |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 2351 | static inline struct nid_path * |
| 2352 | get_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 Iwai | a5cc250 | 2013-01-16 18:08:55 +0100 | [diff] [blame] | 2359 | static void update_automute_all(struct hda_codec *codec); |
| 2360 | |
Takashi Iwai | 65033cc | 2013-04-16 12:31:05 +0200 | [diff] [blame] | 2361 | /* Default value to be passed as aamix argument for snd_hda_activate_path(); |
| 2362 | * used for output paths |
| 2363 | */ |
| 2364 | static bool aamix_default(struct hda_gen_spec *spec) |
| 2365 | { |
| 2366 | return !spec->have_aamix_ctl || spec->aamix_mode; |
| 2367 | } |
| 2368 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2369 | static 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 Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 2375 | path = get_multiio_path(codec, idx); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2376 | if (!path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | return -EINVAL; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2378 | |
| 2379 | if (path->active == output) |
| 2380 | return 0; |
| 2381 | |
| 2382 | if (output) { |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 2383 | set_pin_target(codec, nid, PIN_OUT, true); |
Takashi Iwai | 65033cc | 2013-04-16 12:31:05 +0200 | [diff] [blame] | 2384 | snd_hda_activate_path(codec, path, true, aamix_default(spec)); |
Takashi Iwai | d5a9f1b | 2012-12-20 15:36:30 +0100 | [diff] [blame] | 2385 | set_pin_eapd(codec, nid, true); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2386 | } else { |
Takashi Iwai | d5a9f1b | 2012-12-20 15:36:30 +0100 | [diff] [blame] | 2387 | set_pin_eapd(codec, nid, false); |
Takashi Iwai | 65033cc | 2013-04-16 12:31:05 +0200 | [diff] [blame] | 2388 | snd_hda_activate_path(codec, path, false, aamix_default(spec)); |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 2389 | set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true); |
Takashi Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 2390 | path_power_down_sync(codec, path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | } |
Takashi Iwai | a365fed | 2013-01-10 16:10:06 +0100 | [diff] [blame] | 2392 | |
| 2393 | /* update jack retasking in case it modifies any of them */ |
Takashi Iwai | a5cc250 | 2013-01-16 18:08:55 +0100 | [diff] [blame] | 2394 | update_automute_all(codec); |
Takashi Iwai | a365fed | 2013-01-10 16:10:06 +0100 | [diff] [blame] | 2395 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2396 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | } |
| 2398 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2399 | static int ch_mode_put(struct snd_kcontrol *kcontrol, |
| 2400 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2401 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2402 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2403 | struct hda_gen_spec *spec = codec->spec; |
| 2404 | int i, ch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2406 | ch = ucontrol->value.enumerated.item[0]; |
| 2407 | if (ch < 0 || ch > spec->multi_ios) |
| 2408 | return -EINVAL; |
Takashi Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 2409 | if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2410 | return 0; |
Takashi Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 2411 | spec->ext_channel_count = ch * 2 + spec->min_channel_count; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2412 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | return 1; |
| 2419 | } |
| 2420 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2421 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2429 | static 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 Iwai | 12c93df | 2012-12-19 14:38:33 +0100 | [diff] [blame] | 2434 | if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum)) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2435 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | return 0; |
| 2438 | } |
| 2439 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2440 | /* |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 2441 | * aamix loopback enable/disable switch |
| 2442 | */ |
| 2443 | |
| 2444 | #define loopback_mixing_info indep_hp_info |
| 2445 | |
| 2446 | static 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 | |
| 2455 | static void update_aamix_paths(struct hda_codec *codec, bool do_mix, |
Takashi Iwai | a1e908e | 2013-01-21 15:11:25 +0100 | [diff] [blame] | 2456 | int nomix_path_idx, int mix_path_idx, |
| 2457 | int out_type) |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 2458 | { |
Takashi Iwai | a1e908e | 2013-01-21 15:11:25 +0100 | [diff] [blame] | 2459 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 2460 | 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 Iwai | a1e908e | 2013-01-21 15:11:25 +0100 | [diff] [blame] | 2466 | |
| 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 Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 2474 | if (do_mix) { |
| 2475 | snd_hda_activate_path(codec, nomix_path, false, true); |
| 2476 | snd_hda_activate_path(codec, mix_path, true, true); |
Takashi Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 2477 | path_power_down_sync(codec, nomix_path); |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 2478 | } else { |
Takashi Iwai | 65033cc | 2013-04-16 12:31:05 +0200 | [diff] [blame] | 2479 | snd_hda_activate_path(codec, mix_path, false, false); |
| 2480 | snd_hda_activate_path(codec, nomix_path, true, false); |
Takashi Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 2481 | path_power_down_sync(codec, mix_path); |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 2482 | } |
| 2483 | } |
| 2484 | |
Takashi Iwai | e7fdd52 | 2015-12-08 17:00:42 +0100 | [diff] [blame] | 2485 | /* re-initialize the output paths; only called from loopback_mixing_put() */ |
| 2486 | static 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 Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 2501 | static 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 Iwai | e7fdd52 | 2015-12-08 17:00:42 +0100 | [diff] [blame] | 2506 | const struct auto_pin_cfg *cfg = &spec->autocfg; |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 2507 | unsigned int val = ucontrol->value.enumerated.item[0]; |
| 2508 | |
| 2509 | if (val == spec->aamix_mode) |
| 2510 | return 0; |
| 2511 | spec->aamix_mode = val; |
Takashi Iwai | e7fdd52 | 2015-12-08 17:00:42 +0100 | [diff] [blame] | 2512 | 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 Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 2530 | return 1; |
| 2531 | } |
| 2532 | |
| 2533 | static 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 | |
| 2541 | static 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 Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 2547 | if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum)) |
| 2548 | return -ENOMEM; |
Takashi Iwai | a1e908e | 2013-01-21 15:11:25 +0100 | [diff] [blame] | 2549 | spec->have_aamix_ctl = 1; |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 2550 | return 0; |
| 2551 | } |
| 2552 | |
| 2553 | /* |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2554 | * shared headphone/mic handling |
| 2555 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2556 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2557 | static void call_update_outputs(struct hda_codec *codec); |
| 2558 | |
| 2559 | /* for shared I/O, change the pin-control accordingly */ |
Takashi Iwai | 967303d | 2013-02-19 17:12:42 +0100 | [diff] [blame] | 2560 | static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2561 | { |
| 2562 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 967303d | 2013-02-19 17:12:42 +0100 | [diff] [blame] | 2563 | bool as_mic; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2564 | unsigned int val; |
Takashi Iwai | 967303d | 2013-02-19 17:12:42 +0100 | [diff] [blame] | 2565 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2580 | |
| 2581 | val = snd_hda_get_default_vref(codec, pin); |
Takashi Iwai | 967303d | 2013-02-19 17:12:42 +0100 | [diff] [blame] | 2582 | /* 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2585 | 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 Iwai | 7594aa3 | 2012-12-20 15:38:40 +0100 | [diff] [blame] | 2589 | snd_hda_set_pin_ctl_cache(codec, vref_pin, |
Takashi Iwai | 967303d | 2013-02-19 17:12:42 +0100 | [diff] [blame] | 2590 | PIN_IN | (as_mic ? vref_val : 0)); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2591 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2592 | |
Takashi Iwai | 8ba955c | 2013-03-07 18:40:58 +0100 | [diff] [blame] | 2593 | 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 Iwai | 963afde | 2013-05-31 15:20:31 +0200 | [diff] [blame] | 2599 | call_hp_automute(codec, NULL); |
Takashi Iwai | 8ba955c | 2013-03-07 18:40:58 +0100 | [diff] [blame] | 2600 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | /* create a shared input with the headphone out */ |
Takashi Iwai | 967303d | 2013-02-19 17:12:42 +0100 | [diff] [blame] | 2604 | static int create_hp_mic(struct hda_codec *codec) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2605 | { |
| 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 Iwai | 967303d | 2013-02-19 17:12:42 +0100 | [diff] [blame] | 2611 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2628 | return 0; |
| 2629 | |
Takashi Iwai | 967303d | 2013-02-19 17:12:42 +0100 | [diff] [blame] | 2630 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2637 | |
| 2638 | if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN)) |
| 2639 | return 0; /* no input */ |
| 2640 | |
Takashi Iwai | 967303d | 2013-02-19 17:12:42 +0100 | [diff] [blame] | 2641 | cfg->inputs[cfg->num_inputs].pin = nid; |
| 2642 | cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC; |
David Henningsson | cb420b1 | 2013-04-11 11:30:28 +0200 | [diff] [blame] | 2643 | cfg->inputs[cfg->num_inputs].is_headphone_mic = 1; |
Takashi Iwai | 967303d | 2013-02-19 17:12:42 +0100 | [diff] [blame] | 2644 | 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 Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 2649 | codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2650 | return 0; |
| 2651 | } |
| 2652 | |
Takashi Iwai | 978e77e | 2013-01-10 16:57:58 +0100 | [diff] [blame] | 2653 | /* |
| 2654 | * output jack mode |
| 2655 | */ |
Takashi Iwai | 5f171ba | 2013-02-19 18:14:54 +0100 | [diff] [blame] | 2656 | |
| 2657 | static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin); |
| 2658 | |
| 2659 | static const char * const out_jack_texts[] = { |
| 2660 | "Line Out", "Headphone Out", |
| 2661 | }; |
| 2662 | |
Takashi Iwai | 978e77e | 2013-01-10 16:57:58 +0100 | [diff] [blame] | 2663 | static int out_jack_mode_info(struct snd_kcontrol *kcontrol, |
| 2664 | struct snd_ctl_elem_info *uinfo) |
| 2665 | { |
Takashi Iwai | 5f171ba | 2013-02-19 18:14:54 +0100 | [diff] [blame] | 2666 | return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts); |
Takashi Iwai | 978e77e | 2013-01-10 16:57:58 +0100 | [diff] [blame] | 2667 | } |
| 2668 | |
| 2669 | static 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 | |
| 2681 | static 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 | |
| 2695 | static 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 | |
| 2702 | static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx) |
| 2703 | { |
| 2704 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | a9c2dfc | 2018-04-23 17:24:56 +0200 | [diff] [blame] | 2705 | const struct snd_kcontrol_new *kctl; |
Takashi Iwai | 978e77e | 2013-01-10 16:57:58 +0100 | [diff] [blame] | 2706 | int i; |
| 2707 | |
Takashi Iwai | a9c2dfc | 2018-04-23 17:24:56 +0200 | [diff] [blame] | 2708 | snd_array_for_each(&spec->kctls, i, kctl) { |
Takashi Iwai | 978e77e | 2013-01-10 16:57:58 +0100 | [diff] [blame] | 2709 | if (!strcmp(kctl->name, name) && kctl->index == idx) |
| 2710 | return true; |
| 2711 | } |
| 2712 | return false; |
| 2713 | } |
| 2714 | |
| 2715 | static 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 Iwai | 5f171ba | 2013-02-19 18:14:54 +0100 | [diff] [blame] | 2728 | static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin) |
| 2729 | { |
| 2730 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | f811c3c | 2013-03-07 18:32:59 +0100 | [diff] [blame] | 2731 | if (spec->add_jack_modes) { |
Takashi Iwai | 5f171ba | 2013-02-19 18:14:54 +0100 | [diff] [blame] | 2732 | 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 Iwai | 978e77e | 2013-01-10 16:57:58 +0100 | [diff] [blame] | 2739 | static 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 Iwai | ced4cef | 2013-11-26 08:33:45 +0100 | [diff] [blame] | 2747 | if (pin == spec->hp_mic_pin) |
Takashi Iwai | 5f171ba | 2013-02-19 18:14:54 +0100 | [diff] [blame] | 2748 | continue; |
Takashi Iwai | 5f171ba | 2013-02-19 18:14:54 +0100 | [diff] [blame] | 2749 | if (get_out_jack_num_items(codec, pin) > 1) { |
Takashi Iwai | 978e77e | 2013-01-10 16:57:58 +0100 | [diff] [blame] | 2750 | struct snd_kcontrol_new *knew; |
Takashi Iwai | 975cc02 | 2013-06-28 11:56:49 +0200 | [diff] [blame] | 2751 | char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; |
Takashi Iwai | 978e77e | 2013-01-10 16:57:58 +0100 | [diff] [blame] | 2752 | 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 Iwai | 29476558 | 2013-01-17 09:52:11 +0100 | [diff] [blame] | 2764 | /* |
| 2765 | * input jack mode |
| 2766 | */ |
| 2767 | |
| 2768 | /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */ |
| 2769 | #define NUM_VREFS 6 |
| 2770 | |
| 2771 | static 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 | |
| 2776 | static 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%...) */ |
| 2788 | static 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 */ |
| 2803 | static 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 | |
| 2816 | static 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 | |
| 2831 | static 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 | |
| 2844 | static 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 | |
| 2863 | static 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 Iwai | 5f171ba | 2013-02-19 18:14:54 +0100 | [diff] [blame] | 2870 | static 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 Iwai | f811c3c | 2013-03-07 18:32:59 +0100 | [diff] [blame] | 2874 | if (spec->add_jack_modes) |
Takashi Iwai | 5f171ba | 2013-02-19 18:14:54 +0100 | [diff] [blame] | 2875 | nitems = hweight32(get_vref_caps(codec, pin)); |
| 2876 | return nitems ? nitems : 1; |
| 2877 | } |
| 2878 | |
Takashi Iwai | 29476558 | 2013-01-17 09:52:11 +0100 | [diff] [blame] | 2879 | static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin) |
| 2880 | { |
| 2881 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 29476558 | 2013-01-17 09:52:11 +0100 | [diff] [blame] | 2882 | struct snd_kcontrol_new *knew; |
Takashi Iwai | 975cc02 | 2013-06-28 11:56:49 +0200 | [diff] [blame] | 2883 | char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; |
Takashi Iwai | 5f171ba | 2013-02-19 18:14:54 +0100 | [diff] [blame] | 2884 | unsigned int defcfg; |
| 2885 | |
Takashi Iwai | f811c3c | 2013-03-07 18:32:59 +0100 | [diff] [blame] | 2886 | if (pin == spec->hp_mic_pin) |
| 2887 | return 0; /* already done in create_out_jack_mode() */ |
Takashi Iwai | 29476558 | 2013-01-17 09:52:11 +0100 | [diff] [blame] | 2888 | |
| 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 Iwai | 5f171ba | 2013-02-19 18:14:54 +0100 | [diff] [blame] | 2895 | if (get_in_jack_num_items(codec, pin) <= 1) |
Takashi Iwai | 29476558 | 2013-01-17 09:52:11 +0100 | [diff] [blame] | 2896 | 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 Iwai | 5f171ba | 2013-02-19 18:14:54 +0100 | [diff] [blame] | 2906 | /* |
| 2907 | * HP/mic shared jack mode |
| 2908 | */ |
| 2909 | static 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 | |
| 2943 | static 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 | |
| 2964 | static 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 | |
| 2974 | static 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 Iwai | 3f550e3 | 2013-03-07 18:30:27 +0100 | [diff] [blame] | 2998 | val &= ~(AC_PINCTL_VREFEN | PIN_HP); |
| 2999 | val |= get_vref_idx(vref_caps, idx) | PIN_IN; |
Takashi Iwai | 5f171ba | 2013-02-19 18:14:54 +0100 | [diff] [blame] | 3000 | } else |
Takashi Iwai | 16c0cefe | 2013-11-26 08:44:26 +0100 | [diff] [blame] | 3001 | val = snd_hda_get_default_vref(codec, nid) | PIN_IN; |
Takashi Iwai | 5f171ba | 2013-02-19 18:14:54 +0100 | [diff] [blame] | 3002 | } |
| 3003 | snd_hda_set_pin_ctl_cache(codec, nid, val); |
Takashi Iwai | 963afde | 2013-05-31 15:20:31 +0200 | [diff] [blame] | 3004 | call_hp_automute(codec, NULL); |
Takashi Iwai | 8ba955c | 2013-03-07 18:40:58 +0100 | [diff] [blame] | 3005 | |
Takashi Iwai | 5f171ba | 2013-02-19 18:14:54 +0100 | [diff] [blame] | 3006 | return 1; |
| 3007 | } |
| 3008 | |
| 3009 | static 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 | |
| 3016 | static 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 Iwai | 5f171ba | 2013-02-19 18:14:54 +0100 | [diff] [blame] | 3021 | 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 Iwai | 8ba955c | 2013-03-07 18:40:58 +0100 | [diff] [blame] | 3026 | spec->hp_mic_jack_modes = 1; |
Takashi Iwai | 5f171ba | 2013-02-19 18:14:54 +0100 | [diff] [blame] | 3027 | return 0; |
| 3028 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3029 | |
| 3030 | /* |
| 3031 | * Parse input paths |
| 3032 | */ |
| 3033 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3034 | /* add the powersave loopback-list entry */ |
Takashi Iwai | 0186f4f | 2013-02-07 10:45:11 +0100 | [diff] [blame] | 3035 | static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3036 | { |
| 3037 | struct hda_amp_list *list; |
| 3038 | |
Takashi Iwai | 0186f4f | 2013-02-07 10:45:11 +0100 | [diff] [blame] | 3039 | list = snd_array_new(&spec->loopback_list); |
| 3040 | if (!list) |
| 3041 | return -ENOMEM; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3042 | list->nid = mix; |
| 3043 | list->dir = HDA_INPUT; |
| 3044 | list->idx = idx; |
Takashi Iwai | 0186f4f | 2013-02-07 10:45:11 +0100 | [diff] [blame] | 3045 | spec->loopback.amplist = spec->loopback_list.list; |
| 3046 | return 0; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3047 | } |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 3048 | |
Takashi Iwai | 2ded3e5 | 2013-11-28 11:05:28 +0100 | [diff] [blame] | 3049 | /* 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 | */ |
| 3052 | static 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 Iwai | 43a8e50 | 2014-01-07 18:11:44 +0100 | [diff] [blame] | 3077 | if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) && |
| 3078 | !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL)) |
Takashi Iwai | 2ded3e5 | 2013-11-28 11:05:28 +0100 | [diff] [blame] | 3079 | *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT); |
Takashi Iwai | 43a8e50 | 2014-01-07 18:11:44 +0100 | [diff] [blame] | 3080 | if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) && |
| 3081 | !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL)) |
Takashi Iwai | 2ded3e5 | 2013-11-28 11:05:28 +0100 | [diff] [blame] | 3082 | *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT); |
| 3083 | |
| 3084 | return *mix_val || *mute_val; |
| 3085 | } |
| 3086 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3087 | /* create input playback/capture controls for the given pin */ |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 3088 | static int new_analog_input(struct hda_codec *codec, int input_idx, |
| 3089 | hda_nid_t pin, const char *ctlname, int ctlidx, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3090 | hda_nid_t mix_nid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3091 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3092 | struct hda_gen_spec *spec = codec->spec; |
| 3093 | struct nid_path *path; |
Takashi Iwai | 2ded3e5 | 2013-11-28 11:05:28 +0100 | [diff] [blame] | 3094 | unsigned int mix_val, mute_val; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3095 | int err, idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3096 | |
Takashi Iwai | 2ded3e5 | 2013-11-28 11:05:28 +0100 | [diff] [blame] | 3097 | if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val)) |
| 3098 | return 0; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3099 | |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 3100 | path = snd_hda_add_new_path(codec, pin, mix_nid, 0); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3101 | if (!path) |
| 3102 | return -EINVAL; |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3103 | print_nid_path(codec, "loopback", path); |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 3104 | spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3105 | |
| 3106 | idx = path->idx[path->depth - 1]; |
Takashi Iwai | 2ded3e5 | 2013-11-28 11:05:28 +0100 | [diff] [blame] | 3107 | if (mix_val) { |
| 3108 | err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3109 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3110 | return err; |
Takashi Iwai | 2ded3e5 | 2013-11-28 11:05:28 +0100 | [diff] [blame] | 3111 | path->ctls[NID_PATH_VOL_CTL] = mix_val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | } |
| 3113 | |
Takashi Iwai | 2ded3e5 | 2013-11-28 11:05:28 +0100 | [diff] [blame] | 3114 | if (mute_val) { |
| 3115 | err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3116 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | return err; |
Takashi Iwai | 2ded3e5 | 2013-11-28 11:05:28 +0100 | [diff] [blame] | 3118 | path->ctls[NID_PATH_MUTE_CTL] = mute_val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3119 | } |
| 3120 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3121 | path->active = true; |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 3122 | path->stream_enabled = true; /* no DAC/ADC involved */ |
Takashi Iwai | 0186f4f | 2013-02-07 10:45:11 +0100 | [diff] [blame] | 3123 | err = add_loopback_list(spec, mix_nid, idx); |
| 3124 | if (err < 0) |
| 3125 | return err; |
Takashi Iwai | e4a395e | 2013-01-23 17:00:31 +0100 | [diff] [blame] | 3126 | |
| 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 Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3132 | print_nid_path(codec, "loopback-merge", path); |
Takashi Iwai | e4a395e | 2013-01-23 17:00:31 +0100 | [diff] [blame] | 3133 | path->active = true; |
Takashi Iwai | 6b275b1 | 2015-03-20 18:11:05 +0100 | [diff] [blame] | 3134 | path->pin_fixed = true; /* static route */ |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 3135 | path->stream_enabled = true; /* no DAC/ADC involved */ |
Takashi Iwai | e4a395e | 2013-01-23 17:00:31 +0100 | [diff] [blame] | 3136 | spec->loopback_merge_path = |
| 3137 | snd_hda_get_path_idx(codec, path); |
| 3138 | } |
| 3139 | } |
| 3140 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3141 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3142 | } |
| 3143 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3144 | static int is_input_pin(struct hda_codec *codec, hda_nid_t nid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3145 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3146 | 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 */ |
| 3151 | static 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 Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 3157 | int nums = 0; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3158 | |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 3159 | for_each_hda_codec_node(nid, codec) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3160 | 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 Iwai | 0ffd534 | 2013-01-17 15:53:29 +0100 | [diff] [blame] | 3170 | |
| 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3175 | 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 | */ |
| 3181 | static 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 Iwai | 3a65bcd | 2013-01-09 09:06:18 +0100 | [diff] [blame] | 3185 | unsigned int ok_bits; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3186 | int i, n, nums; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3187 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3188 | nums = 0; |
Takashi Iwai | 3a65bcd | 2013-01-09 09:06:18 +0100 | [diff] [blame] | 3189 | ok_bits = 0; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3190 | for (n = 0; n < spec->num_adc_nids; n++) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3191 | for (i = 0; i < imux->num_items; i++) { |
Takashi Iwai | 3a65bcd | 2013-01-09 09:06:18 +0100 | [diff] [blame] | 3192 | if (!spec->input_paths[i][n]) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3193 | break; |
| 3194 | } |
Takashi Iwai | 3a65bcd | 2013-01-09 09:06:18 +0100 | [diff] [blame] | 3195 | if (i >= imux->num_items) { |
| 3196 | ok_bits |= (1 << n); |
| 3197 | nums++; |
| 3198 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3199 | } |
| 3200 | |
Takashi Iwai | 3a65bcd | 2013-01-09 09:06:18 +0100 | [diff] [blame] | 3201 | if (!ok_bits) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3202 | /* check whether ADC-switch is possible */ |
| 3203 | for (i = 0; i < imux->num_items; i++) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3204 | for (n = 0; n < spec->num_adc_nids; n++) { |
Takashi Iwai | 3a65bcd | 2013-01-09 09:06:18 +0100 | [diff] [blame] | 3205 | if (spec->input_paths[i][n]) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3206 | spec->dyn_adc_idx[i] = n; |
| 3207 | break; |
| 3208 | } |
| 3209 | } |
| 3210 | } |
| 3211 | |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3212 | codec_dbg(codec, "enabling ADC switching\n"); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3213 | spec->dyn_adc_switch = 1; |
| 3214 | } else if (nums != spec->num_adc_nids) { |
Takashi Iwai | 3a65bcd | 2013-01-09 09:06:18 +0100 | [diff] [blame] | 3215 | /* 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 Iwai | 980428c | 2013-01-09 09:28:20 +0100 | [diff] [blame] | 3222 | for (i = 0; i < imux->num_items; i++) { |
| 3223 | invalidate_nid_path(codec, |
| 3224 | spec->input_paths[i][nums]); |
Takashi Iwai | 3a65bcd | 2013-01-09 09:06:18 +0100 | [diff] [blame] | 3225 | spec->input_paths[i][nums] = |
| 3226 | spec->input_paths[i][n]; |
Hui Wang | a8f20fd | 2017-06-28 08:59:16 +0800 | [diff] [blame] | 3227 | spec->input_paths[i][n] = 0; |
Takashi Iwai | 980428c | 2013-01-09 09:28:20 +0100 | [diff] [blame] | 3228 | } |
Takashi Iwai | 3a65bcd | 2013-01-09 09:06:18 +0100 | [diff] [blame] | 3229 | } |
| 3230 | nums++; |
| 3231 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3232 | spec->num_adc_nids = nums; |
| 3233 | } |
| 3234 | |
Takashi Iwai | 967303d | 2013-02-19 17:12:42 +0100 | [diff] [blame] | 3235 | if (imux->num_items == 1 || |
| 3236 | (imux->num_items == 2 && spec->hp_mic)) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3237 | codec_dbg(codec, "reducing to a single ADC\n"); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3238 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | return 0; |
| 3246 | } |
| 3247 | |
Takashi Iwai | f3fc0b0 | 2013-01-09 09:14:23 +0100 | [diff] [blame] | 3248 | /* parse capture source paths from the given pin and create imux items */ |
| 3249 | static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin, |
Takashi Iwai | 9dba205 | 2013-01-18 10:01:15 +0100 | [diff] [blame] | 3250 | int cfg_idx, int num_adcs, |
| 3251 | const char *label, int anchor) |
Takashi Iwai | f3fc0b0 | 2013-01-09 09:14:23 +0100 | [diff] [blame] | 3252 | { |
| 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 Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 3268 | print_nid_path(codec, "input", path); |
Takashi Iwai | f3fc0b0 | 2013-01-09 09:14:23 +0100 | [diff] [blame] | 3269 | spec->input_paths[imux_idx][c] = |
| 3270 | snd_hda_get_path_idx(codec, path); |
| 3271 | |
| 3272 | if (!imux_added) { |
Takashi Iwai | 967303d | 2013-02-19 17:12:42 +0100 | [diff] [blame] | 3273 | if (spec->hp_mic_pin == pin) |
| 3274 | spec->hp_mic_mux_idx = imux->num_items; |
Takashi Iwai | f3fc0b0 | 2013-01-09 09:14:23 +0100 | [diff] [blame] | 3275 | spec->imux_pins[imux->num_items] = pin; |
Takashi Iwai | 6194b99 | 2014-06-06 18:12:16 +0200 | [diff] [blame] | 3276 | snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL); |
Takashi Iwai | f3fc0b0 | 2013-01-09 09:14:23 +0100 | [diff] [blame] | 3277 | imux_added = true; |
Takashi Iwai | f1e762d | 2013-12-09 16:02:24 +0100 | [diff] [blame] | 3278 | if (spec->dyn_adc_switch) |
| 3279 | spec->dyn_adc_idx[imux_idx] = c; |
Takashi Iwai | f3fc0b0 | 2013-01-09 09:14:23 +0100 | [diff] [blame] | 3280 | } |
| 3281 | } |
| 3282 | |
| 3283 | return 0; |
| 3284 | } |
| 3285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3286 | /* |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3287 | * create playback/capture controls for input pins |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3288 | */ |
Takashi Iwai | 9dba205 | 2013-01-18 10:01:15 +0100 | [diff] [blame] | 3289 | |
Takashi Iwai | c970042 | 2013-01-18 10:17:30 +0100 | [diff] [blame] | 3290 | /* fill the label for each input at first */ |
| 3291 | static 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 Henningsson | 8e8db7f | 2013-01-18 15:43:02 +0100 | [diff] [blame] | 3307 | for (j = i - 1; j >= 0; j--) { |
Takashi Iwai | c970042 | 2013-01-18 10:17:30 +0100 | [diff] [blame] | 3308 | 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 Iwai | 9dba205 | 2013-01-18 10:01:15 +0100 | [diff] [blame] | 3322 | #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */ |
| 3323 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3324 | static int create_input_ctls(struct hda_codec *codec) |
Takashi Iwai | a7da6ce | 2006-09-06 14:03:14 +0200 | [diff] [blame] | 3325 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3326 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3329 | int num_adcs; |
Takashi Iwai | c970042 | 2013-01-18 10:17:30 +0100 | [diff] [blame] | 3330 | int i, err; |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 3331 | unsigned int val; |
Takashi Iwai | a7da6ce | 2006-09-06 14:03:14 +0200 | [diff] [blame] | 3332 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3333 | num_adcs = fill_adc_nids(codec); |
| 3334 | if (num_adcs < 0) |
| 3335 | return 0; |
Takashi Iwai | a7da6ce | 2006-09-06 14:03:14 +0200 | [diff] [blame] | 3336 | |
Takashi Iwai | c970042 | 2013-01-18 10:17:30 +0100 | [diff] [blame] | 3337 | err = fill_input_pin_labels(codec); |
| 3338 | if (err < 0) |
| 3339 | return err; |
| 3340 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3341 | for (i = 0; i < cfg->num_inputs; i++) { |
| 3342 | hda_nid_t pin; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3343 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3344 | pin = cfg->inputs[i].pin; |
| 3345 | if (!is_input_pin(codec, pin)) |
| 3346 | continue; |
| 3347 | |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 3348 | val = PIN_IN; |
| 3349 | if (cfg->inputs[i].type == AUTO_PIN_MIC) |
| 3350 | val |= snd_hda_get_default_vref(codec, pin); |
Takashi Iwai | 3e1b0c4 | 2015-04-27 10:43:22 +0200 | [diff] [blame] | 3351 | if (pin != spec->hp_mic_pin && |
| 3352 | !snd_hda_codec_get_pin_target(codec, pin)) |
Takashi Iwai | 93c9d8a | 2013-03-11 09:48:43 +0100 | [diff] [blame] | 3353 | set_pin_target(codec, pin, val, false); |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 3354 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3355 | if (mixer) { |
| 3356 | if (is_reachable_path(codec, pin, mixer)) { |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 3357 | err = new_analog_input(codec, i, pin, |
Takashi Iwai | c970042 | 2013-01-18 10:17:30 +0100 | [diff] [blame] | 3358 | spec->input_labels[i], |
| 3359 | spec->input_label_idxs[i], |
| 3360 | mixer); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3361 | if (err < 0) |
| 3362 | return err; |
| 3363 | } |
| 3364 | } |
| 3365 | |
Takashi Iwai | c970042 | 2013-01-18 10:17:30 +0100 | [diff] [blame] | 3366 | err = parse_capture_source(codec, pin, i, num_adcs, |
| 3367 | spec->input_labels[i], -mixer); |
Takashi Iwai | f3fc0b0 | 2013-01-09 09:14:23 +0100 | [diff] [blame] | 3368 | if (err < 0) |
| 3369 | return err; |
Takashi Iwai | 29476558 | 2013-01-17 09:52:11 +0100 | [diff] [blame] | 3370 | |
Takashi Iwai | f811c3c | 2013-03-07 18:32:59 +0100 | [diff] [blame] | 3371 | if (spec->add_jack_modes) { |
Takashi Iwai | 29476558 | 2013-01-17 09:52:11 +0100 | [diff] [blame] | 3372 | err = create_in_jack_mode(codec, pin); |
| 3373 | if (err < 0) |
| 3374 | return err; |
| 3375 | } |
Takashi Iwai | f3fc0b0 | 2013-01-09 09:14:23 +0100 | [diff] [blame] | 3376 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3377 | |
Takashi Iwai | f1e762d | 2013-12-09 16:02:24 +0100 | [diff] [blame] | 3378 | /* add stereo mix when explicitly enabled via hint */ |
Takashi Iwai | 74f14b3 | 2014-12-15 13:43:59 +0100 | [diff] [blame] | 3379 | if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) { |
Takashi Iwai | 9dba205 | 2013-01-18 10:01:15 +0100 | [diff] [blame] | 3380 | err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs, |
Takashi Iwai | f3fc0b0 | 2013-01-09 09:14:23 +0100 | [diff] [blame] | 3381 | "Stereo Mix", 0); |
| 3382 | if (err < 0) |
| 3383 | return err; |
Takashi Iwai | 82d04e1 | 2014-12-15 13:40:42 +0100 | [diff] [blame] | 3384 | else |
| 3385 | spec->suppress_auto_mic = 1; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3386 | } |
| 3387 | |
| 3388 | return 0; |
| 3389 | } |
| 3390 | |
| 3391 | |
| 3392 | /* |
| 3393 | * input source mux |
| 3394 | */ |
| 3395 | |
Takashi Iwai | c697b71 | 2013-01-07 17:09:26 +0100 | [diff] [blame] | 3396 | /* get the input path specified by the given adc and imux indices */ |
| 3397 | static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3398 | { |
| 3399 | struct hda_gen_spec *spec = codec->spec; |
David Henningsson | b56fa1e | 2013-01-16 11:45:35 +0100 | [diff] [blame] | 3400 | if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) { |
| 3401 | snd_BUG(); |
| 3402 | return NULL; |
| 3403 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3404 | if (spec->dyn_adc_switch) |
| 3405 | adc_idx = spec->dyn_adc_idx[imux_idx]; |
David Henningsson | d3d982f | 2013-01-18 15:43:01 +0100 | [diff] [blame] | 3406 | if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) { |
David Henningsson | b56fa1e | 2013-01-16 11:45:35 +0100 | [diff] [blame] | 3407 | snd_BUG(); |
| 3408 | return NULL; |
| 3409 | } |
Takashi Iwai | c697b71 | 2013-01-07 17:09:26 +0100 | [diff] [blame] | 3410 | return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3411 | } |
| 3412 | |
| 3413 | static int mux_select(struct hda_codec *codec, unsigned int adc_idx, |
| 3414 | unsigned int idx); |
| 3415 | |
| 3416 | static 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 | |
| 3424 | static 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 Iwai | 2a8d539 | 2013-01-18 16:23:25 +0100 | [diff] [blame] | 3429 | /* the ctls are created at once with multiple counts */ |
| 3430 | unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3431 | |
| 3432 | ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx]; |
| 3433 | return 0; |
| 3434 | } |
| 3435 | |
| 3436 | static 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 Iwai | 2a8d539 | 2013-01-18 16:23:25 +0100 | [diff] [blame] | 3440 | unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3441 | return mux_select(codec, adc_idx, |
| 3442 | ucontrol->value.enumerated.item[0]); |
| 3443 | } |
| 3444 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3445 | static 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 Iwai | 47d46ab | 2012-12-20 11:48:54 +0100 | [diff] [blame] | 3453 | /* |
| 3454 | * capture volume and capture switch ctls |
| 3455 | */ |
| 3456 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3457 | typedef int (*put_call_t)(struct snd_kcontrol *kcontrol, |
| 3458 | struct snd_ctl_elem_value *ucontrol); |
| 3459 | |
Takashi Iwai | 47d46ab | 2012-12-20 11:48:54 +0100 | [diff] [blame] | 3460 | /* call the given amp update function for all amps in the imux list at once */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3461 | static 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 Kysela | a2befe9 | 2021-08-11 18:14:41 +0200 | [diff] [blame] | 3469 | int i, adc_idx, ret, err = 0; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3470 | |
| 3471 | imux = &spec->input_mux; |
David Henningsson | a053d1e | 2013-01-16 11:45:36 +0100 | [diff] [blame] | 3472 | adc_idx = kcontrol->id.index; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3473 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3474 | for (i = 0; i < imux->num_items; i++) { |
Takashi Iwai | c697b71 | 2013-01-07 17:09:26 +0100 | [diff] [blame] | 3475 | path = get_input_path(codec, adc_idx, i); |
| 3476 | if (!path || !path->ctls[type]) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3477 | continue; |
| 3478 | kcontrol->private_value = path->ctls[type]; |
Jaroslav Kysela | a2befe9 | 2021-08-11 18:14:41 +0200 | [diff] [blame] | 3479 | ret = func(kcontrol, ucontrol); |
| 3480 | if (ret < 0) { |
| 3481 | err = ret; |
Takashi Iwai | a551d91 | 2015-02-26 12:34:49 +0100 | [diff] [blame] | 3482 | break; |
Jaroslav Kysela | a2befe9 | 2021-08-11 18:14:41 +0200 | [diff] [blame] | 3483 | } |
| 3484 | if (ret > 0) |
| 3485 | err = 1; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3486 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3487 | mutex_unlock(&codec->control_mutex); |
| 3488 | if (err >= 0 && spec->cap_sync_hook) |
Takashi Iwai | 7fe3071 | 2014-01-30 17:59:02 +0100 | [diff] [blame] | 3489 | spec->cap_sync_hook(codec, kcontrol, ucontrol); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3490 | 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 | |
| 3498 | static 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 | |
| 3506 | static 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 | |
| 3522 | static int cap_sw_put(struct snd_kcontrol *kcontrol, |
| 3523 | struct snd_ctl_elem_value *ucontrol) |
| 3524 | { |
Takashi Iwai | a90229e | 2013-01-18 14:10:00 +0100 | [diff] [blame] | 3525 | return cap_put_caller(kcontrol, ucontrol, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3526 | snd_hda_mixer_amp_switch_put, |
| 3527 | NID_PATH_MUTE_CTL); |
| 3528 | } |
| 3529 | |
| 3530 | static const struct snd_kcontrol_new cap_sw_temp = { |
| 3531 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3532 | .name = "Capture Switch", |
Takashi Iwai | 08a4b90 | 2021-05-31 20:06:33 +0200 | [diff] [blame] | 3533 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3534 | .info = cap_sw_info, |
| 3535 | .get = cap_sw_get, |
| 3536 | .put = cap_sw_put, |
| 3537 | }; |
| 3538 | |
| 3539 | static 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 Iwai | 97ec558 | 2006-03-21 11:29:07 +0100 | [diff] [blame] | 3574 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3575 | return 0; |
| 3576 | } |
| 3577 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3578 | static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3579 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3580 | 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 Iwai | a90229e | 2013-01-18 14:10:00 +0100 | [diff] [blame] | 3598 | /* capture switch put callback for a single control with hook call */ |
Takashi Iwai | a35bd1e | 2013-01-18 14:01:14 +0100 | [diff] [blame] | 3599 | static 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 Iwai | a90229e | 2013-01-18 14:10:00 +0100 | [diff] [blame] | 3610 | if (spec->cap_sync_hook) |
Takashi Iwai | 7fe3071 | 2014-01-30 17:59:02 +0100 | [diff] [blame] | 3611 | spec->cap_sync_hook(codec, kcontrol, ucontrol); |
Takashi Iwai | a35bd1e | 2013-01-18 14:01:14 +0100 | [diff] [blame] | 3612 | |
| 3613 | return ret; |
| 3614 | } |
| 3615 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3616 | static 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 Iwai | 975cc02 | 2013-06-28 11:56:49 +0200 | [diff] [blame] | 3621 | char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3622 | 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 Iwai | a35bd1e | 2013-01-18 14:01:14 +0100 | [diff] [blame] | 3625 | struct snd_kcontrol_new *knew; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3626 | |
| 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 Iwai | a35bd1e | 2013-01-18 14:01:14 +0100 | [diff] [blame] | 3636 | knew = add_control(spec, type, tmpname, idx, |
| 3637 | amp_val_replace_channels(ctl, chs)); |
| 3638 | if (!knew) |
| 3639 | return -ENOMEM; |
Jaroslav Kysela | e65bf99 | 2021-03-17 18:29:43 +0100 | [diff] [blame] | 3640 | if (is_switch) { |
Takashi Iwai | a35bd1e | 2013-01-18 14:01:14 +0100 | [diff] [blame] | 3641 | knew->put = cap_single_sw_put; |
Jaroslav Kysela | e65bf99 | 2021-03-17 18:29:43 +0100 | [diff] [blame] | 3642 | if (spec->mic_mute_led) |
| 3643 | knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED; |
| 3644 | } |
Takashi Iwai | a35bd1e | 2013-01-18 14:01:14 +0100 | [diff] [blame] | 3645 | if (!inv_dmic) |
| 3646 | return 0; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3647 | |
| 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 Iwai | a35bd1e | 2013-01-18 14:01:14 +0100 | [diff] [blame] | 3655 | knew = add_control(spec, type, tmpname, idx, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3656 | amp_val_replace_channels(ctl, 2)); |
Takashi Iwai | a35bd1e | 2013-01-18 14:01:14 +0100 | [diff] [blame] | 3657 | if (!knew) |
| 3658 | return -ENOMEM; |
Jaroslav Kysela | e65bf99 | 2021-03-17 18:29:43 +0100 | [diff] [blame] | 3659 | if (is_switch) { |
Takashi Iwai | a35bd1e | 2013-01-18 14:01:14 +0100 | [diff] [blame] | 3660 | knew->put = cap_single_sw_put; |
Jaroslav Kysela | e65bf99 | 2021-03-17 18:29:43 +0100 | [diff] [blame] | 3661 | if (spec->mic_mute_led) |
| 3662 | knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED; |
| 3663 | } |
Takashi Iwai | a35bd1e | 2013-01-18 14:01:14 +0100 | [diff] [blame] | 3664 | return 0; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3665 | } |
| 3666 | |
| 3667 | /* create single (and simple) capture volume and switch controls */ |
| 3668 | static 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 */ |
| 3683 | static 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 Iwai | 12c93df | 2012-12-19 14:38:33 +0100 | [diff] [blame] | 3690 | knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3691 | 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 Iwai | 12c93df | 2012-12-19 14:38:33 +0100 | [diff] [blame] | 3698 | knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3699 | if (!knew) |
| 3700 | return -ENOMEM; |
| 3701 | knew->index = idx; |
| 3702 | knew->private_value = sw_ctl; |
| 3703 | knew->subdevice = HDA_SUBDEV_AMP_FLAG; |
Jaroslav Kysela | e65bf99 | 2021-03-17 18:29:43 +0100 | [diff] [blame] | 3704 | if (spec->mic_mute_led) |
| 3705 | knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3706 | } |
| 3707 | return 0; |
| 3708 | } |
| 3709 | |
| 3710 | /* return the vol ctl when used first in the imux list */ |
| 3711 | static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type) |
| 3712 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3713 | struct nid_path *path; |
| 3714 | unsigned int ctl; |
| 3715 | int i; |
| 3716 | |
Takashi Iwai | c697b71 | 2013-01-07 17:09:26 +0100 | [diff] [blame] | 3717 | path = get_input_path(codec, 0, idx); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3718 | 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 Iwai | c697b71 | 2013-01-07 17:09:26 +0100 | [diff] [blame] | 3724 | path = get_input_path(codec, 0, i); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3725 | 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 */ |
| 3732 | static 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 Iwai | c970042 | 2013-01-18 10:17:30 +0100 | [diff] [blame] | 3736 | int i, err, type; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3737 | |
| 3738 | for (i = 0; i < imux->num_items; i++) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3739 | bool inv_dmic; |
Takashi Iwai | c970042 | 2013-01-18 10:17:30 +0100 | [diff] [blame] | 3740 | int idx; |
Takashi Iwai | 9dba205 | 2013-01-18 10:01:15 +0100 | [diff] [blame] | 3741 | |
Takashi Iwai | c970042 | 2013-01-18 10:17:30 +0100 | [diff] [blame] | 3742 | idx = imux->items[i].index; |
| 3743 | if (idx >= spec->autocfg.num_inputs) |
Takashi Iwai | 9dba205 | 2013-01-18 10:01:15 +0100 | [diff] [blame] | 3744 | continue; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3745 | inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]); |
| 3746 | |
| 3747 | for (type = 0; type < 2; type++) { |
Takashi Iwai | c970042 | 2013-01-18 10:17:30 +0100 | [diff] [blame] | 3748 | err = add_single_cap_ctl(codec, |
| 3749 | spec->input_labels[idx], |
| 3750 | spec->input_label_idxs[idx], |
| 3751 | type, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3752 | get_first_cap_ctl(codec, i, type), |
| 3753 | inv_dmic); |
| 3754 | if (err < 0) |
| 3755 | return err; |
| 3756 | } |
| 3757 | } |
| 3758 | return 0; |
| 3759 | } |
| 3760 | |
| 3761 | static 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 Iwai | 624d914 | 2012-12-19 17:41:52 +0100 | [diff] [blame] | 3774 | const char *name; |
| 3775 | name = nums > 1 ? "Input Source" : "Capture Source"; |
| 3776 | knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3777 | if (!knew) |
| 3778 | return -ENOMEM; |
| 3779 | knew->count = nums; |
| 3780 | } |
| 3781 | |
| 3782 | for (n = 0; n < nums; n++) { |
| 3783 | bool multi = false; |
David Henningsson | 99a5592 | 2013-01-16 15:58:44 +0100 | [diff] [blame] | 3784 | bool multi_cap_vol = spec->multi_cap_vol; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3785 | 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 Iwai | c697b71 | 2013-01-07 17:09:26 +0100 | [diff] [blame] | 3791 | path = get_input_path(codec, n, i); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3792 | if (!path) |
| 3793 | continue; |
| 3794 | parse_capvol_in_path(codec, path); |
| 3795 | if (!vol) |
| 3796 | vol = path->ctls[NID_PATH_VOL_CTL]; |
David Henningsson | 99a5592 | 2013-01-16 15:58:44 +0100 | [diff] [blame] | 3797 | else if (vol != path->ctls[NID_PATH_VOL_CTL]) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3798 | multi = true; |
David Henningsson | 99a5592 | 2013-01-16 15:58:44 +0100 | [diff] [blame] | 3799 | if (!same_amp_caps(codec, vol, |
| 3800 | path->ctls[NID_PATH_VOL_CTL], HDA_INPUT)) |
| 3801 | multi_cap_vol = true; |
| 3802 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3803 | if (!sw) |
| 3804 | sw = path->ctls[NID_PATH_MUTE_CTL]; |
David Henningsson | 99a5592 | 2013-01-16 15:58:44 +0100 | [diff] [blame] | 3805 | else if (sw != path->ctls[NID_PATH_MUTE_CTL]) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3806 | multi = true; |
David Henningsson | 99a5592 | 2013-01-16 15:58:44 +0100 | [diff] [blame] | 3807 | if (!same_amp_caps(codec, sw, |
| 3808 | path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT)) |
| 3809 | multi_cap_vol = true; |
| 3810 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3811 | 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 Henningsson | ccb0415 | 2013-10-14 10:16:22 +0200 | [diff] [blame] | 3818 | else if (!multi_cap_vol && !inv_dmic) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3819 | 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 Iwai | 6f7c83a | 2013-01-18 11:07:15 +0100 | [diff] [blame] | 3832 | |
| 3833 | /* check whether the given amp is feasible as a boost volume */ |
| 3834 | static 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 */ |
| 3852 | static 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3877 | static 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 Iwai | 6f7c83a | 2013-01-18 11:07:15 +0100 | [diff] [blame] | 3881 | struct hda_input_mux *imux = &spec->input_mux; |
Takashi Iwai | a35bd1e | 2013-01-18 14:01:14 +0100 | [diff] [blame] | 3882 | int i; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3883 | |
Takashi Iwai | 6f7c83a | 2013-01-18 11:07:15 +0100 | [diff] [blame] | 3884 | if (!spec->num_adc_nids) |
| 3885 | return 0; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3886 | |
Takashi Iwai | 6f7c83a | 2013-01-18 11:07:15 +0100 | [diff] [blame] | 3887 | for (i = 0; i < imux->num_items; i++) { |
| 3888 | struct nid_path *path; |
| 3889 | unsigned int val; |
| 3890 | int idx; |
Takashi Iwai | 975cc02 | 2013-06-28 11:56:49 +0200 | [diff] [blame] | 3891 | char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; |
David Henningsson | 02aba55 | 2013-01-16 15:58:43 +0100 | [diff] [blame] | 3892 | |
Takashi Iwai | 6f7c83a | 2013-01-18 11:07:15 +0100 | [diff] [blame] | 3893 | idx = imux->items[i].index; |
| 3894 | if (idx >= imux->num_items) |
| 3895 | continue; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3896 | |
Takashi Iwai | 6f7c83a | 2013-01-18 11:07:15 +0100 | [diff] [blame] | 3897 | /* check only line-in and mic pins */ |
Takashi Iwai | 1799cdd5 | 2013-01-18 14:37:16 +0100 | [diff] [blame] | 3898 | if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN) |
Takashi Iwai | 6f7c83a | 2013-01-18 11:07:15 +0100 | [diff] [blame] | 3899 | 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 Iwai | a35bd1e | 2013-01-18 14:01:14 +0100 | [diff] [blame] | 3912 | if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label, |
| 3913 | spec->input_label_idxs[idx], val)) |
| 3914 | return -ENOMEM; |
Takashi Iwai | 6f7c83a | 2013-01-18 11:07:15 +0100 | [diff] [blame] | 3915 | |
| 3916 | path->ctls[NID_PATH_BOOST_CTL] = val; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3917 | } |
| 3918 | return 0; |
| 3919 | } |
| 3920 | |
Takashi Iwai | 7cdf8c4 | 2020-06-18 13:08:31 +0200 | [diff] [blame] | 3921 | #ifdef CONFIG_SND_HDA_GENERIC_LEDS |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3922 | /* |
Takashi Iwai | 15509b6 | 2020-06-18 13:08:37 +0200 | [diff] [blame] | 3923 | * vmaster mute LED hook helpers |
| 3924 | */ |
| 3925 | |
| 3926 | static int create_mute_led_cdev(struct hda_codec *codec, |
| 3927 | int (*callback)(struct led_classdev *, |
| 3928 | enum led_brightness), |
| 3929 | bool micmute) |
| 3930 | { |
Takashi Iwai | 549f8ff | 2022-01-26 15:50:11 +0100 | [diff] [blame] | 3931 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 15509b6 | 2020-06-18 13:08:37 +0200 | [diff] [blame] | 3932 | struct led_classdev *cdev; |
Takashi Iwai | 549f8ff | 2022-01-26 15:50:11 +0100 | [diff] [blame] | 3933 | int idx = micmute ? LED_AUDIO_MICMUTE : LED_AUDIO_MUTE; |
| 3934 | int err; |
Takashi Iwai | 15509b6 | 2020-06-18 13:08:37 +0200 | [diff] [blame] | 3935 | |
| 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 Iwai | 549f8ff | 2022-01-26 15:50:11 +0100 | [diff] [blame] | 3944 | cdev->brightness = ledtrig_audio_get(idx); |
Takashi Iwai | c9e272f | 2020-06-18 13:08:42 +0200 | [diff] [blame] | 3945 | cdev->flags = LED_CORE_SUSPENDRESUME; |
Takashi Iwai | 15509b6 | 2020-06-18 13:08:37 +0200 | [diff] [blame] | 3946 | |
Takashi Iwai | 549f8ff | 2022-01-26 15:50:11 +0100 | [diff] [blame] | 3947 | 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 Iwai | 15509b6 | 2020-06-18 13:08:37 +0200 | [diff] [blame] | 3952 | } |
| 3953 | |
Takashi Iwai | 15509b6 | 2020-06-18 13:08:37 +0200 | [diff] [blame] | 3954 | /** |
Pierre-Louis Bossart | 3531ba2 | 2021-03-01 11:46:17 -0600 | [diff] [blame] | 3955 | * snd_hda_gen_add_mute_led_cdev - Create a LED classdev and enable as vmaster mute LED |
Takashi Iwai | 15509b6 | 2020-06-18 13:08:37 +0200 | [diff] [blame] | 3956 | * @codec: the HDA codec |
| 3957 | * @callback: the callback for LED classdev brightness_set_blocking |
| 3958 | */ |
| 3959 | int 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 Kysela | e65bf99 | 2021-03-17 18:29:43 +0100 | [diff] [blame] | 3977 | spec->vmaster_mute_led = 1; |
Takashi Iwai | 15509b6 | 2020-06-18 13:08:37 +0200 | [diff] [blame] | 3978 | return 0; |
| 3979 | } |
| 3980 | EXPORT_SYMBOL_GPL(snd_hda_gen_add_mute_led_cdev); |
| 3981 | |
Takashi Iwai | b380278 | 2018-11-26 17:47:46 +0100 | [diff] [blame] | 3982 | /** |
Pierre-Louis Bossart | 3531ba2 | 2021-03-01 11:46:17 -0600 | [diff] [blame] | 3983 | * snd_hda_gen_add_micmute_led_cdev - Create a LED classdev and enable as mic-mute LED |
Takashi Iwai | 7cdf8c4 | 2020-06-18 13:08:31 +0200 | [diff] [blame] | 3984 | * @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 | */ |
| 3996 | int snd_hda_gen_add_micmute_led_cdev(struct hda_codec *codec, |
| 3997 | int (*callback)(struct led_classdev *, |
| 3998 | enum led_brightness)) |
| 3999 | { |
Jaroslav Kysela | e65bf99 | 2021-03-17 18:29:43 +0100 | [diff] [blame] | 4000 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 7cdf8c4 | 2020-06-18 13:08:31 +0200 | [diff] [blame] | 4001 | int err; |
Takashi Iwai | 7cdf8c4 | 2020-06-18 13:08:31 +0200 | [diff] [blame] | 4002 | |
| 4003 | if (callback) { |
Takashi Iwai | 15509b6 | 2020-06-18 13:08:37 +0200 | [diff] [blame] | 4004 | err = create_mute_led_cdev(codec, callback, true); |
Takashi Iwai | 7cdf8c4 | 2020-06-18 13:08:31 +0200 | [diff] [blame] | 4005 | if (err) { |
| 4006 | codec_warn(codec, "failed to create a mic-mute LED cdev\n"); |
| 4007 | return err; |
| 4008 | } |
| 4009 | } |
| 4010 | |
Jaroslav Kysela | e65bf99 | 2021-03-17 18:29:43 +0100 | [diff] [blame] | 4011 | spec->mic_mute_led = 1; |
| 4012 | return 0; |
Takashi Iwai | 7cdf8c4 | 2020-06-18 13:08:31 +0200 | [diff] [blame] | 4013 | } |
| 4014 | EXPORT_SYMBOL_GPL(snd_hda_gen_add_micmute_led_cdev); |
| 4015 | #endif /* CONFIG_SND_HDA_GENERIC_LEDS */ |
| 4016 | |
Takashi Iwai | f567b78 | 2018-06-18 17:26:12 +0200 | [diff] [blame] | 4017 | /* |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4018 | * parse digital I/Os and set up NIDs in BIOS auto-parse mode |
| 4019 | */ |
| 4020 | static void parse_digital(struct hda_codec *codec) |
| 4021 | { |
| 4022 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 4023 | struct nid_path *path; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4024 | int i, nums; |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 4025 | hda_nid_t dig_nid, pin; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4026 | |
Takashi Iwai | 9ab0cb3 | 2020-07-17 17:45:17 +0200 | [diff] [blame] | 4027 | /* support multiple SPDIFs; the secondary is set up as a follower */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4028 | nums = 0; |
| 4029 | for (i = 0; i < spec->autocfg.dig_outs; i++) { |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 4030 | pin = spec->autocfg.dig_out_pins[i]; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4031 | dig_nid = look_for_dac(codec, pin, true); |
| 4032 | if (!dig_nid) |
| 4033 | continue; |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 4034 | path = snd_hda_add_new_path(codec, dig_nid, pin, 0); |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 4035 | if (!path) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4036 | continue; |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4037 | print_nid_path(codec, "digout", path); |
Takashi Iwai | e1284af | 2013-01-03 16:33:02 +0100 | [diff] [blame] | 4038 | path->active = true; |
Takashi Iwai | 6b275b1 | 2015-03-20 18:11:05 +0100 | [diff] [blame] | 4039 | path->pin_fixed = true; /* no jack detection */ |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 4040 | spec->digout_paths[i] = snd_hda_get_path_idx(codec, path); |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 4041 | set_pin_target(codec, pin, PIN_OUT, false); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4042 | if (!nums) { |
| 4043 | spec->multiout.dig_out_nid = dig_nid; |
| 4044 | spec->dig_out_type = spec->autocfg.dig_out_type[0]; |
| 4045 | } else { |
Takashi Iwai | 9ab0cb3 | 2020-07-17 17:45:17 +0200 | [diff] [blame] | 4046 | spec->multiout.follower_dig_outs = spec->follower_dig_outs; |
| 4047 | if (nums >= ARRAY_SIZE(spec->follower_dig_outs) - 1) |
Dan Carpenter | d576422e | 2014-05-14 17:18:31 +0300 | [diff] [blame] | 4048 | break; |
Takashi Iwai | 9ab0cb3 | 2020-07-17 17:45:17 +0200 | [diff] [blame] | 4049 | spec->follower_dig_outs[nums - 1] = dig_nid; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4050 | } |
| 4051 | nums++; |
| 4052 | } |
| 4053 | |
| 4054 | if (spec->autocfg.dig_in_pin) { |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 4055 | pin = spec->autocfg.dig_in_pin; |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 4056 | for_each_hda_codec_node(dig_nid, codec) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4057 | 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 Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 4062 | path = snd_hda_add_new_path(codec, pin, dig_nid, 0); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4063 | if (path) { |
Takashi Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4064 | print_nid_path(codec, "digin", path); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4065 | path->active = true; |
Takashi Iwai | 6b275b1 | 2015-03-20 18:11:05 +0100 | [diff] [blame] | 4066 | path->pin_fixed = true; /* no jack */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4067 | spec->dig_in_nid = dig_nid; |
Takashi Iwai | 2430d7b | 2013-01-04 15:09:42 +0100 | [diff] [blame] | 4068 | spec->digin_path = snd_hda_get_path_idx(codec, path); |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 4069 | set_pin_target(codec, pin, PIN_IN, false); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4070 | break; |
| 4071 | } |
| 4072 | } |
| 4073 | } |
| 4074 | } |
| 4075 | |
| 4076 | |
| 4077 | /* |
| 4078 | * input MUX handling |
| 4079 | */ |
| 4080 | |
| 4081 | static 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 */ |
| 4084 | static 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 Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 4089 | struct nid_path *old_path, *path; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4090 | |
| 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 Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 4100 | old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]); |
| 4101 | if (!old_path) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4102 | return 0; |
Takashi Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 4103 | if (old_path->active) |
| 4104 | snd_hda_activate_path(codec, old_path, false, false); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4105 | |
| 4106 | spec->cur_mux[adc_idx] = idx; |
| 4107 | |
Takashi Iwai | 967303d | 2013-02-19 17:12:42 +0100 | [diff] [blame] | 4108 | if (spec->hp_mic) |
| 4109 | update_hp_mic(codec, adc_idx, false); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4110 | |
| 4111 | if (spec->dyn_adc_switch) |
| 4112 | dyn_adc_pcm_resetup(codec, idx); |
| 4113 | |
Takashi Iwai | c697b71 | 2013-01-07 17:09:26 +0100 | [diff] [blame] | 4114 | path = get_input_path(codec, adc_idx, idx); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4115 | 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 Iwai | 7fe3071 | 2014-01-30 17:59:02 +0100 | [diff] [blame] | 4121 | spec->cap_sync_hook(codec, NULL, NULL); |
Takashi Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 4122 | path_power_down_sync(codec, old_path); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4123 | return 1; |
| 4124 | } |
| 4125 | |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 4126 | /* 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 | */ |
| 4131 | static 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 Iwai | a9c2dfc | 2018-04-23 17:24:56 +0200 | [diff] [blame] | 4139 | snd_array_for_each(&spec->paths, n, path) { |
Bob Copeland | 81e4396 | 2016-06-25 07:58:45 -0400 | [diff] [blame] | 4140 | if (!path->depth) |
| 4141 | continue; |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 4142 | 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 Iwai | 6b275b1 | 2015-03-20 18:11:05 +0100 | [diff] [blame] | 4151 | if ((!path->pin_fixed && path->pin_enabled != pin_old) |
| 4152 | || path->stream_enabled != stream_old) { |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 4153 | last = path_power_update(codec, path, true); |
| 4154 | if (last) |
| 4155 | changed = last; |
| 4156 | } |
| 4157 | } |
| 4158 | } |
| 4159 | return changed; |
| 4160 | } |
| 4161 | |
Takashi Iwai | d5ac010 | 2015-04-09 10:21:30 +0200 | [diff] [blame] | 4162 | /* check the jack status for power control */ |
| 4163 | static 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 Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 4170 | /* 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 | */ |
| 4176 | static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin, |
| 4177 | int power) |
| 4178 | { |
| 4179 | bool on; |
| 4180 | |
Takashi Iwai | 967b130 | 2015-03-20 18:21:03 +0100 | [diff] [blame] | 4181 | if (!codec->power_save_node) |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 4182 | return 0; |
| 4183 | |
Takashi Iwai | d5ac010 | 2015-04-09 10:21:30 +0200 | [diff] [blame] | 4184 | on = detect_pin_state(codec, pin); |
| 4185 | |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 4186 | if (power >= 0 && on != power) |
| 4187 | return 0; |
| 4188 | return set_path_power(codec, pin, on, -1); |
| 4189 | } |
| 4190 | |
| 4191 | static void pin_power_callback(struct hda_codec *codec, |
| 4192 | struct hda_jack_callback *jack, |
| 4193 | bool on) |
| 4194 | { |
Takashi Iwai | 2ebab40 | 2016-02-09 10:23:52 +0100 | [diff] [blame] | 4195 | if (jack && jack->nid) |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 4196 | sync_power_state_change(codec, |
Takashi Iwai | 2ebab40 | 2016-02-09 10:23:52 +0100 | [diff] [blame] | 4197 | set_pin_power_jack(codec, jack->nid, on)); |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 4198 | } |
| 4199 | |
| 4200 | /* callback only doing power up -- called at first */ |
| 4201 | static 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 */ |
| 4208 | static 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 */ |
| 4215 | static 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 | */ |
| 4233 | static 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 Iwai | 967b130 | 2015-03-20 18:21:03 +0100 | [diff] [blame] | 4239 | if (!codec->power_save_node) |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 4240 | 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 */ |
| 4251 | static 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 */ |
| 4262 | static 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 Iwai | 967b130 | 2015-03-20 18:21:03 +0100 | [diff] [blame] | 4268 | if (!codec->power_save_node) |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 4269 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4278 | |
Takashi Iwai | 5ccf835 | 2015-03-18 09:23:10 +0100 | [diff] [blame] | 4279 | /* add fake paths if not present yet */ |
| 4280 | static 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 */ |
| 4305 | static 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 Iwai | 967b130 | 2015-03-20 18:21:03 +0100 | [diff] [blame] | 4312 | if (!codec->power_save_node || !nid) |
Takashi Iwai | 5ccf835 | 2015-03-18 09:23:10 +0100 | [diff] [blame] | 4313 | 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 */ |
| 4332 | static void beep_power_hook(struct hda_beep *beep, bool on) |
| 4333 | { |
| 4334 | set_path_power(beep->codec, beep->nid, -1, on); |
| 4335 | } |
| 4336 | |
Takashi Iwai | 6b275b1 | 2015-03-20 18:11:05 +0100 | [diff] [blame] | 4337 | /** |
| 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 | */ |
| 4342 | int 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 | } |
| 4358 | EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power); |
| 4359 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4360 | /* |
| 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ław | caf3c04 | 2020-01-03 10:23:48 +0100 | [diff] [blame] | 4365 | static bool detect_jacks(struct hda_codec *codec, int num_pins, const hda_nid_t *pins) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4366 | { |
Takashi Iwai | 60ea8ca | 2013-07-19 16:59:46 +0200 | [diff] [blame] | 4367 | int i; |
| 4368 | bool present = false; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4369 | |
| 4370 | for (i = 0; i < num_pins; i++) { |
| 4371 | hda_nid_t nid = pins[i]; |
| 4372 | if (!nid) |
| 4373 | break; |
Takashi Iwai | 0b4df93 | 2013-01-10 09:45:13 +0100 | [diff] [blame] | 4374 | /* don't detect pins retasked as inputs */ |
| 4375 | if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN) |
| 4376 | continue; |
Takashi Iwai | 60ea8ca | 2013-07-19 16:59:46 +0200 | [diff] [blame] | 4377 | if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT) |
| 4378 | present = true; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4379 | } |
| 4380 | return present; |
| 4381 | } |
| 4382 | |
| 4383 | /* standard HP/line-out auto-mute helper */ |
Michał Mirosław | caf3c04 | 2020-01-03 10:23:48 +0100 | [diff] [blame] | 4384 | static void do_automute(struct hda_codec *codec, int num_pins, const hda_nid_t *pins, |
Takashi Iwai | e80c60f | 2013-08-12 14:44:59 +0200 | [diff] [blame] | 4385 | int *paths, bool mute) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4386 | { |
| 4387 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4388 | int i; |
| 4389 | |
| 4390 | for (i = 0; i < num_pins; i++) { |
| 4391 | hda_nid_t nid = pins[i]; |
Takashi Iwai | 967303d | 2013-02-19 17:12:42 +0100 | [diff] [blame] | 4392 | unsigned int val, oldval; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4393 | if (!nid) |
| 4394 | break; |
Takashi Iwai | 7eebffd | 2013-06-24 16:00:21 +0200 | [diff] [blame] | 4395 | |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 4396 | oldval = snd_hda_codec_get_pin_target(codec, nid); |
| 4397 | if (oldval & PIN_IN) |
| 4398 | continue; /* no mute for inputs */ |
| 4399 | |
Takashi Iwai | 7eebffd | 2013-06-24 16:00:21 +0200 | [diff] [blame] | 4400 | if (spec->auto_mute_via_amp) { |
Takashi Iwai | e80c60f | 2013-08-12 14:44:59 +0200 | [diff] [blame] | 4401 | 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 Iwai | 7eebffd | 2013-06-24 16:00:21 +0200 | [diff] [blame] | 4410 | if (mute) |
Takashi Iwai | e80c60f | 2013-08-12 14:44:59 +0200 | [diff] [blame] | 4411 | spec->mute_bits |= (1ULL << mute_nid); |
Takashi Iwai | 7eebffd | 2013-06-24 16:00:21 +0200 | [diff] [blame] | 4412 | else |
Takashi Iwai | e80c60f | 2013-08-12 14:44:59 +0200 | [diff] [blame] | 4413 | spec->mute_bits &= ~(1ULL << mute_nid); |
Takashi Iwai | 7eebffd | 2013-06-24 16:00:21 +0200 | [diff] [blame] | 4414 | continue; |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 4415 | } 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 Iwai | 7eebffd | 2013-06-24 16:00:21 +0200 | [diff] [blame] | 4431 | } |
| 4432 | |
Takashi Iwai | d5a9f1b | 2012-12-20 15:36:30 +0100 | [diff] [blame] | 4433 | set_pin_eapd(codec, nid, !mute); |
Takashi Iwai | 967b130 | 2015-03-20 18:21:03 +0100 | [diff] [blame] | 4434 | if (codec->power_save_node) { |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 4435 | bool on = !mute; |
| 4436 | if (on) |
Takashi Iwai | d5ac010 | 2015-04-09 10:21:30 +0200 | [diff] [blame] | 4437 | on = detect_pin_state(codec, nid); |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 4438 | set_path_power(codec, nid, on, -1); |
| 4439 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4440 | } |
| 4441 | } |
| 4442 | |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 4443 | /** |
| 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 Iwai | 5d550e1 | 2012-12-19 15:16:44 +0100 | [diff] [blame] | 4449 | void snd_hda_gen_update_outputs(struct hda_codec *codec) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4450 | { |
| 4451 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | e80c60f | 2013-08-12 14:44:59 +0200 | [diff] [blame] | 4452 | int *paths; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4453 | 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 Iwai | e80c60f | 2013-08-12 14:44:59 +0200 | [diff] [blame] | 4459 | if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT) |
| 4460 | paths = spec->out_paths; |
| 4461 | else |
| 4462 | paths = spec->hp_paths; |
Takashi Iwai | 967303d | 2013-02-19 17:12:42 +0100 | [diff] [blame] | 4463 | do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins), |
Takashi Iwai | e80c60f | 2013-08-12 14:44:59 +0200 | [diff] [blame] | 4464 | spec->autocfg.hp_pins, paths, spec->master_mute); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4465 | |
| 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 Iwai | 47b9ddb8 | 2013-01-16 18:18:00 +0100 | [diff] [blame] | 4471 | spec->speaker_muted = on; |
Takashi Iwai | e80c60f | 2013-08-12 14:44:59 +0200 | [diff] [blame] | 4472 | if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT) |
| 4473 | paths = spec->out_paths; |
| 4474 | else |
| 4475 | paths = spec->speaker_paths; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4476 | do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins), |
Takashi Iwai | e80c60f | 2013-08-12 14:44:59 +0200 | [diff] [blame] | 4477 | spec->autocfg.speaker_pins, paths, on); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4478 | |
| 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 Iwai | 47b9ddb8 | 2013-01-16 18:18:00 +0100 | [diff] [blame] | 4489 | spec->line_out_muted = on; |
Takashi Iwai | e80c60f | 2013-08-12 14:44:59 +0200 | [diff] [blame] | 4490 | paths = spec->out_paths; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4491 | do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins), |
Takashi Iwai | e80c60f | 2013-08-12 14:44:59 +0200 | [diff] [blame] | 4492 | spec->autocfg.line_out_pins, paths, on); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4493 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4494 | EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4495 | |
| 4496 | static 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 Iwai | 5d550e1 | 2012-12-19 15:16:44 +0100 | [diff] [blame] | 4502 | snd_hda_gen_update_outputs(codec); |
Takashi Iwai | 7eebffd | 2013-06-24 16:00:21 +0200 | [diff] [blame] | 4503 | |
Takashi Iwai | 9ab0cb3 | 2020-07-17 17:45:17 +0200 | [diff] [blame] | 4504 | /* sync the whole vmaster followers to reflect the new auto-mute status */ |
Takashi Iwai | 7eebffd | 2013-06-24 16:00:21 +0200 | [diff] [blame] | 4505 | if (spec->auto_mute_via_amp && !codec->bus->shutdown) |
| 4506 | snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4507 | } |
| 4508 | |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 4509 | /** |
| 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 Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 4514 | void snd_hda_gen_hp_automute(struct hda_codec *codec, |
| 4515 | struct hda_jack_callback *jack) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4516 | { |
| 4517 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 92603c5 | 2013-01-22 07:46:31 +0100 | [diff] [blame] | 4518 | hda_nid_t *pins = spec->autocfg.hp_pins; |
| 4519 | int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4520 | |
Takashi Iwai | 92603c5 | 2013-01-22 07:46:31 +0100 | [diff] [blame] | 4521 | /* 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4528 | if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo)) |
| 4529 | return; |
| 4530 | call_update_outputs(codec); |
| 4531 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4532 | EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4533 | |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 4534 | /** |
| 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 Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 4539 | void snd_hda_gen_line_automute(struct hda_codec *codec, |
| 4540 | struct hda_jack_callback *jack) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4541 | { |
| 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 Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4557 | EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4558 | |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 4559 | /** |
| 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 Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 4564 | void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, |
| 4565 | struct hda_jack_callback *jack) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4566 | { |
| 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 Iwai | 0b4df93 | 2013-01-10 09:45:13 +0100 | [diff] [blame] | 4574 | 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 Iwai | 60ea8ca | 2013-07-19 16:59:46 +0200 | [diff] [blame] | 4578 | if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4579 | 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 Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 4585 | EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4586 | |
Takashi Iwai | 77afe0e | 2013-05-31 14:10:03 +0200 | [diff] [blame] | 4587 | /* call appropriate hooks */ |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 4588 | static void call_hp_automute(struct hda_codec *codec, |
| 4589 | struct hda_jack_callback *jack) |
Takashi Iwai | 77afe0e | 2013-05-31 14:10:03 +0200 | [diff] [blame] | 4590 | { |
| 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 | |
| 4598 | static void call_line_automute(struct hda_codec *codec, |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 4599 | struct hda_jack_callback *jack) |
Takashi Iwai | 77afe0e | 2013-05-31 14:10:03 +0200 | [diff] [blame] | 4600 | { |
| 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 | |
| 4608 | static void call_mic_autoswitch(struct hda_codec *codec, |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 4609 | struct hda_jack_callback *jack) |
Takashi Iwai | 77afe0e | 2013-05-31 14:10:03 +0200 | [diff] [blame] | 4610 | { |
| 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 Iwai | 963afde | 2013-05-31 15:20:31 +0200 | [diff] [blame] | 4618 | /* update jack retasking */ |
| 4619 | static 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4626 | /* |
| 4627 | * Auto-Mute mode mixer enum support |
| 4628 | */ |
| 4629 | static 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 Iwai | 071c73a | 2006-08-23 18:34:06 +0200 | [diff] [blame] | 4636 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4637 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4638 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4642 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4643 | static 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 Iwai | 071c73a | 2006-08-23 18:34:06 +0200 | [diff] [blame] | 4653 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4654 | ucontrol->value.enumerated.item[0] = val; |
| 4655 | return 0; |
| 4656 | } |
| 4657 | |
| 4658 | static 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 | |
| 4699 | static 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 | |
| 4707 | static int add_automute_mode_enum(struct hda_codec *codec) |
| 4708 | { |
| 4709 | struct hda_gen_spec *spec = codec->spec; |
| 4710 | |
Takashi Iwai | 12c93df | 2012-12-19 14:38:33 +0100 | [diff] [blame] | 4711 | if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum)) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4712 | 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 | */ |
| 4720 | static 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 Iwai | f72706b | 2013-01-16 18:20:07 +0100 | [diff] [blame] | 4727 | if (spec->suppress_auto_mute) |
| 4728 | return 0; |
| 4729 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4730 | 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 Iwai | 071c73a | 2006-08-23 18:34:06 +0200 | [diff] [blame] | 4737 | return 0; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4738 | |
| 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 Iwai | 071c73a | 2006-08-23 18:34:06 +0200 | [diff] [blame] | 4744 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4745 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4746 | 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 Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4757 | codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid); |
Takashi Iwai | 62f949b | 2014-09-11 14:06:53 +0200 | [diff] [blame] | 4758 | snd_hda_jack_detect_enable_callback(codec, nid, |
Takashi Iwai | 77afe0e | 2013-05-31 14:10:03 +0200 | [diff] [blame] | 4759 | call_hp_automute); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4760 | 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 Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4769 | codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4770 | snd_hda_jack_detect_enable_callback(codec, nid, |
Takashi Iwai | 77afe0e | 2013-05-31 14:10:03 +0200 | [diff] [blame] | 4771 | call_line_automute); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4772 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4792 | /* check whether all auto-mic pins are valid; setup indices if OK */ |
| 4793 | static 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 Iwai | 77afe0e | 2013-05-31 14:10:03 +0200 | [diff] [blame] | 4812 | call_mic_autoswitch); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4813 | return true; |
| 4814 | } |
| 4815 | |
| 4816 | static 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 | */ |
| 4827 | static 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 Iwai | d12daf6 | 2013-01-07 16:32:11 +0100 | [diff] [blame] | 4834 | if (spec->suppress_auto_mic) |
| 4835 | return 0; |
| 4836 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4837 | 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 Iwai | 4e76a88 | 2014-02-25 12:21:03 +0100 | [diff] [blame] | 4887 | codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n", |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4888 | spec->am_entry[0].pin, |
| 4889 | spec->am_entry[1].pin, |
| 4890 | spec->am_entry[2].pin); |
| 4891 | |
| 4892 | return 0; |
| 4893 | } |
| 4894 | |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 4895 | /** |
| 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 Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 4902 | unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec, |
Takashi Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 4903 | hda_nid_t nid, |
| 4904 | unsigned int power_state) |
| 4905 | { |
Takashi Iwai | b6c09b3 | 2015-04-09 10:25:03 +0200 | [diff] [blame] | 4906 | struct hda_gen_spec *spec = codec->spec; |
| 4907 | |
| 4908 | if (!spec->power_down_unused && !codec->power_save_node) |
| 4909 | return power_state; |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 4910 | if (power_state != AC_PWRST_D0 || nid == codec->core.afg) |
Takashi Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 4911 | return power_state; |
| 4912 | if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER) |
| 4913 | return power_state; |
Takashi Iwai | b1b9fbd | 2013-05-14 12:58:47 +0200 | [diff] [blame] | 4914 | if (is_active_nid_for_any(codec, nid)) |
Takashi Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 4915 | return power_state; |
| 4916 | return AC_PWRST_D3; |
| 4917 | } |
Takashi Iwai | dfc6e46 | 2014-01-13 16:09:57 +0100 | [diff] [blame] | 4918 | EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter); |
Takashi Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 4919 | |
Takashi Iwai | ebb93c0 | 2013-12-10 17:33:49 +0100 | [diff] [blame] | 4920 | /* mute all aamix inputs initially; parse up to the first leaves */ |
| 4921 | static 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 Iwai | ef403ed | 2015-03-12 08:30:11 +0100 | [diff] [blame] | 4931 | update_amp(codec, mix, HDA_INPUT, i, |
| 4932 | 0xff, HDA_AMP_MUTE); |
Takashi Iwai | ebb93c0 | 2013-12-10 17:33:49 +0100 | [diff] [blame] | 4933 | else if (nid_has_volume(codec, conn[i], HDA_OUTPUT)) |
Takashi Iwai | ef403ed | 2015-03-12 08:30:11 +0100 | [diff] [blame] | 4934 | update_amp(codec, conn[i], HDA_OUTPUT, 0, |
| 4935 | 0xff, HDA_AMP_MUTE); |
Takashi Iwai | ebb93c0 | 2013-12-10 17:33:49 +0100 | [diff] [blame] | 4936 | } |
| 4937 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4938 | |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 4939 | /** |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 4940 | * 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 Iwai | 967b130 | 2015-03-20 18:21:03 +0100 | [diff] [blame] | 4945 | * Set this in patch_ops.stream_pm. Only valid with power_save_node flag. |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 4946 | */ |
| 4947 | void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on) |
| 4948 | { |
Takashi Iwai | 967b130 | 2015-03-20 18:21:03 +0100 | [diff] [blame] | 4949 | if (codec->power_save_node) |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 4950 | set_path_power(codec, nid, -1, on); |
| 4951 | } |
| 4952 | EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm); |
| 4953 | |
| 4954 | /** |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 4955 | * 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 Iwai | 9eb413e | 2012-12-19 14:41:21 +0100 | [diff] [blame] | 4959 | * |
| 4960 | * return 1 if successful, 0 if the proper config is not found, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4961 | * or a negative error code |
| 4962 | */ |
| 4963 | int snd_hda_gen_parse_auto_config(struct hda_codec *codec, |
Takashi Iwai | 9eb413e | 2012-12-19 14:41:21 +0100 | [diff] [blame] | 4964 | struct auto_pin_cfg *cfg) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4965 | { |
| 4966 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4967 | int err; |
| 4968 | |
Takashi Iwai | 1c70a58 | 2013-01-11 17:48:22 +0100 | [diff] [blame] | 4969 | parse_user_hints(codec); |
| 4970 | |
Jaroslav Kysela | e65bf99 | 2021-03-17 18:29:43 +0100 | [diff] [blame] | 4971 | if (spec->vmaster_mute_led || spec->mic_mute_led) |
| 4972 | snd_ctl_led_request(); |
| 4973 | |
Takashi Iwai | e4a395e | 2013-01-23 17:00:31 +0100 | [diff] [blame] | 4974 | if (spec->mixer_nid && !spec->mixer_merge_nid) |
| 4975 | spec->mixer_merge_nid = spec->mixer_nid; |
| 4976 | |
Takashi Iwai | 9eb413e | 2012-12-19 14:41:21 +0100 | [diff] [blame] | 4977 | if (cfg != &spec->autocfg) { |
| 4978 | spec->autocfg = *cfg; |
| 4979 | cfg = &spec->autocfg; |
| 4980 | } |
| 4981 | |
Takashi Iwai | 98bd111 | 2013-03-22 14:53:50 +0100 | [diff] [blame] | 4982 | 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 Henningsson | 6fc4cb9 | 2013-01-16 15:58:45 +0100 | [diff] [blame] | 4987 | fill_all_dac_nids(codec); |
| 4988 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4989 | 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 Iwai | c9e4bdb | 2013-12-06 09:30:14 +0100 | [diff] [blame] | 4995 | if (!cfg->num_inputs && !cfg->dig_in_pin) |
| 4996 | return 0; /* can't find valid BIOS pin config */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4997 | } |
| 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 Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 5028 | err = create_indep_hp_ctls(codec); |
| 5029 | if (err < 0) |
| 5030 | return err; |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 5031 | err = create_loopback_mixing_ctl(codec); |
| 5032 | if (err < 0) |
| 5033 | return err; |
Takashi Iwai | 967303d | 2013-02-19 17:12:42 +0100 | [diff] [blame] | 5034 | err = create_hp_mic(codec); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5035 | if (err < 0) |
| 5036 | return err; |
| 5037 | err = create_input_ctls(codec); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 5038 | if (err < 0) |
Takashi Iwai | 071c73a | 2006-08-23 18:34:06 +0200 | [diff] [blame] | 5039 | return err; |
| 5040 | |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 5041 | /* add power-down pin callbacks at first */ |
| 5042 | add_all_pin_power_ctls(codec, false); |
| 5043 | |
Takashi Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 5044 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5054 | |
| 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 Iwai | 967303d | 2013-02-19 17:12:42 +0100 | [diff] [blame] | 5063 | err = check_auto_mic_availability(codec); |
| 5064 | if (err < 0) |
| 5065 | return err; |
Takashi Iwai | 071c73a | 2006-08-23 18:34:06 +0200 | [diff] [blame] | 5066 | |
Takashi Iwai | f1e762d | 2013-12-09 16:02:24 +0100 | [diff] [blame] | 5067 | /* add stereo mix if available and not enabled yet */ |
| 5068 | if (!spec->auto_mic && spec->mixer_nid && |
Takashi Iwai | 74f14b3 | 2014-12-15 13:43:59 +0100 | [diff] [blame] | 5069 | spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO && |
| 5070 | spec->input_mux.num_items > 1) { |
Takashi Iwai | f1e762d | 2013-12-09 16:02:24 +0100 | [diff] [blame] | 5071 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5079 | 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 Iwai | ced4cef | 2013-11-26 08:33:45 +0100 | [diff] [blame] | 5087 | /* 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 Iwai | f811c3c | 2013-03-07 18:32:59 +0100 | [diff] [blame] | 5098 | if (spec->add_jack_modes) { |
Takashi Iwai | 978e77e | 2013-01-10 16:57:58 +0100 | [diff] [blame] | 5099 | 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 Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 5113 | /* add power-up pin callbacks at last */ |
| 5114 | add_all_pin_power_ctls(codec, true); |
| 5115 | |
Takashi Iwai | ebb93c0 | 2013-12-10 17:33:49 +0100 | [diff] [blame] | 5116 | /* mute all aamix input initially */ |
| 5117 | if (spec->mixer_nid) |
| 5118 | mute_all_mixer_nid(codec, spec->mixer_nid); |
| 5119 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5120 | dig_only: |
| 5121 | parse_digital(codec); |
| 5122 | |
Takashi Iwai | 49fb189 | 2015-05-27 08:37:19 +0200 | [diff] [blame] | 5123 | if (spec->power_down_unused || codec->power_save_node) { |
Takashi Iwai | 24fef90 | 2015-04-09 10:28:15 +0200 | [diff] [blame] | 5124 | if (!codec->power_filter) |
| 5125 | codec->power_filter = snd_hda_gen_path_power_filter; |
Takashi Iwai | 49fb189 | 2015-05-27 08:37:19 +0200 | [diff] [blame] | 5126 | if (!codec->patch_ops.stream_pm) |
| 5127 | codec->patch_ops.stream_pm = snd_hda_gen_stream_pm; |
| 5128 | } |
Takashi Iwai | 55196ff | 2013-01-24 17:32:56 +0100 | [diff] [blame] | 5129 | |
Takashi Iwai | 7504b6c | 2013-03-18 11:25:51 +0100 | [diff] [blame] | 5130 | 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 Iwai | 967b130 | 2015-03-20 18:21:03 +0100 | [diff] [blame] | 5134 | if (codec->beep && codec->power_save_node) { |
Takashi Iwai | 5ccf835 | 2015-03-18 09:23:10 +0100 | [diff] [blame] | 5135 | err = add_fake_beep_paths(codec); |
| 5136 | if (err < 0) |
| 5137 | return err; |
| 5138 | codec->beep->power_hook = beep_power_hook; |
| 5139 | } |
Takashi Iwai | 7504b6c | 2013-03-18 11:25:51 +0100 | [diff] [blame] | 5140 | } |
| 5141 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5142 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5143 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5144 | EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5145 | |
| 5146 | |
| 5147 | /* |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5148 | * Build control elements |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5149 | */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5150 | |
Takashi Iwai | 9ab0cb3 | 2020-07-17 17:45:17 +0200 | [diff] [blame] | 5151 | /* follower controls for virtual master */ |
| 5152 | static const char * const follower_pfxs[] = { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5153 | "Front", "Surround", "Center", "LFE", "Side", |
| 5154 | "Headphone", "Speaker", "Mono", "Line Out", |
| 5155 | "CLFE", "Bass Speaker", "PCM", |
Takashi Iwai | ee79c69 | 2013-01-07 09:57:42 +0100 | [diff] [blame] | 5156 | "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side", |
| 5157 | "Headphone Front", "Headphone Surround", "Headphone CLFE", |
David Henningsson | 03ad6a8 | 2014-10-16 15:33:45 +0200 | [diff] [blame] | 5158 | "Headphone Side", "Headphone+LO", "Speaker+LO", |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5159 | NULL, |
| 5160 | }; |
| 5161 | |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 5162 | /** |
| 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5168 | int snd_hda_gen_build_controls(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5169 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5170 | struct hda_gen_spec *spec = codec->spec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5171 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5172 | |
Takashi Iwai | 36502d0 | 2012-12-19 15:15:10 +0100 | [diff] [blame] | 5173 | if (spec->kctls.used) { |
| 5174 | err = snd_hda_add_new_ctls(codec, spec->kctls.list); |
| 5175 | if (err < 0) |
| 5176 | return err; |
| 5177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5178 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5179 | 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 Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 5183 | spec->pcm_rec[1]->pcm_type); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5184 | if (err < 0) |
| 5185 | return err; |
| 5186 | if (!spec->no_analog) { |
| 5187 | err = snd_hda_create_spdif_share_sw(codec, |
| 5188 | &spec->multiout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5189 | if (err < 0) |
| 5190 | return err; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5191 | spec->multiout.share_spdif = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | } |
| 5193 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5194 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5200 | /* if we have no master control, let's create it */ |
Takashi Iwai | 7480316 | 2017-04-10 17:37:34 +0200 | [diff] [blame] | 5201 | if (!spec->no_analog && !spec->suppress_vmaster && |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5202 | !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5203 | err = snd_hda_add_vmaster(codec, "Master Playback Volume", |
Takashi Iwai | 9ab0cb3 | 2020-07-17 17:45:17 +0200 | [diff] [blame] | 5204 | spec->vmaster_tlv, follower_pfxs, |
Jaroslav Kysela | e65bf99 | 2021-03-17 18:29:43 +0100 | [diff] [blame] | 5205 | "Playback Volume", 0); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5206 | if (err < 0) |
| 5207 | return err; |
| 5208 | } |
Takashi Iwai | 7480316 | 2017-04-10 17:37:34 +0200 | [diff] [blame] | 5209 | if (!spec->no_analog && !spec->suppress_vmaster && |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5210 | !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) { |
| 5211 | err = __snd_hda_add_vmaster(codec, "Master Playback Switch", |
Takashi Iwai | 9ab0cb3 | 2020-07-17 17:45:17 +0200 | [diff] [blame] | 5212 | NULL, follower_pfxs, |
Jaroslav Kysela | e65bf99 | 2021-03-17 18:29:43 +0100 | [diff] [blame] | 5213 | "Playback Switch", true, |
| 5214 | spec->vmaster_mute_led ? |
| 5215 | SNDRV_CTL_ELEM_ACCESS_SPK_LED : 0, |
| 5216 | &spec->vmaster_mute.sw_kctl); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5217 | if (err < 0) |
| 5218 | return err; |
Takashi Iwai | b63eae0 | 2013-10-25 23:43:10 +0200 | [diff] [blame] | 5219 | if (spec->vmaster_mute.hook) { |
Jaroslav Kysela | e65bf99 | 2021-03-17 18:29:43 +0100 | [diff] [blame] | 5220 | snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute); |
Takashi Iwai | b63eae0 | 2013-10-25 23:43:10 +0200 | [diff] [blame] | 5221 | snd_hda_sync_vmaster_hook(&spec->vmaster_mute); |
| 5222 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5223 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5224 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5225 | free_kctls(spec); /* no longer needed */ |
| 5226 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5227 | err = snd_hda_jack_add_kctls(codec, &spec->autocfg); |
| 5228 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5229 | return err; |
| 5230 | |
| 5231 | return 0; |
| 5232 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5233 | EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5235 | |
| 5236 | /* |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5237 | * PCM definitions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5238 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5239 | |
Takashi Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 5240 | static 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 Iwai | ac2e873 | 2013-01-17 15:57:10 +0100 | [diff] [blame] | 5250 | static 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5260 | /* |
| 5261 | * Analog playback callbacks |
| 5262 | */ |
| 5263 | static 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 Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 5268 | int err; |
| 5269 | |
| 5270 | mutex_lock(&spec->pcm_mutex); |
| 5271 | err = snd_hda_multi_out_analog_open(codec, |
| 5272 | &spec->multiout, substream, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5273 | hinfo); |
Takashi Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 5274 | if (!err) { |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 5275 | spec->active_streams |= 1 << STREAM_MULTI_OUT; |
Takashi Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 5276 | call_pcm_playback_hook(hinfo, codec, substream, |
| 5277 | HDA_GEN_PCM_ACT_OPEN); |
| 5278 | } |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 5279 | mutex_unlock(&spec->pcm_mutex); |
| 5280 | return err; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5281 | } |
| 5282 | |
| 5283 | static int playback_pcm_prepare(struct hda_pcm_stream *hinfo, |
Takashi Iwai | 97ec558 | 2006-03-21 11:29:07 +0100 | [diff] [blame] | 5284 | struct hda_codec *codec, |
| 5285 | unsigned int stream_tag, |
| 5286 | unsigned int format, |
| 5287 | struct snd_pcm_substream *substream) |
| 5288 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5289 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 5290 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5298 | } |
Takashi Iwai | 97ec558 | 2006-03-21 11:29:07 +0100 | [diff] [blame] | 5299 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5300 | static 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 Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 5305 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5312 | } |
| 5313 | |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 5314 | static 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 Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 5321 | call_pcm_playback_hook(hinfo, codec, substream, |
| 5322 | HDA_GEN_PCM_ACT_CLOSE); |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 5323 | mutex_unlock(&spec->pcm_mutex); |
| 5324 | return 0; |
| 5325 | } |
| 5326 | |
Takashi Iwai | ac2e873 | 2013-01-17 15:57:10 +0100 | [diff] [blame] | 5327 | static 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 | |
| 5335 | static 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 | |
| 5347 | static 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 | |
| 5357 | static 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 Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 5365 | static 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 Iwai | d1f15e0 | 2015-07-08 09:22:10 +0200 | [diff] [blame] | 5373 | if (spec->indep_hp && !spec->indep_hp_enabled) |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 5374 | err = -EBUSY; |
| 5375 | else |
| 5376 | spec->active_streams |= 1 << STREAM_INDEP_HP; |
Takashi Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 5377 | call_pcm_playback_hook(hinfo, codec, substream, |
| 5378 | HDA_GEN_PCM_ACT_OPEN); |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 5379 | mutex_unlock(&spec->pcm_mutex); |
| 5380 | return err; |
| 5381 | } |
| 5382 | |
| 5383 | static 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 Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 5390 | call_pcm_playback_hook(hinfo, codec, substream, |
| 5391 | HDA_GEN_PCM_ACT_CLOSE); |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 5392 | mutex_unlock(&spec->pcm_mutex); |
| 5393 | return 0; |
| 5394 | } |
| 5395 | |
Takashi Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 5396 | static 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 | |
| 5408 | static 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5418 | /* |
| 5419 | * Digital out |
| 5420 | */ |
| 5421 | static 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 | |
| 5429 | static 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 | |
| 5440 | static 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 | |
| 5448 | static 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 Iwai | ac2e873 | 2013-01-17 15:57:10 +0100 | [diff] [blame] | 5459 | #define alt_capture_pcm_open capture_pcm_open |
| 5460 | #define alt_capture_pcm_close capture_pcm_close |
| 5461 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5462 | static 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 Iwai | 97ec558 | 2006-03-21 11:29:07 +0100 | [diff] [blame] | 5471 | stream_tag, 0, format); |
Takashi Iwai | ac2e873 | 2013-01-17 15:57:10 +0100 | [diff] [blame] | 5472 | call_pcm_capture_hook(hinfo, codec, substream, |
| 5473 | HDA_GEN_PCM_ACT_PREPARE); |
Takashi Iwai | 97ec558 | 2006-03-21 11:29:07 +0100 | [diff] [blame] | 5474 | return 0; |
| 5475 | } |
| 5476 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5477 | static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo, |
| 5478 | struct hda_codec *codec, |
| 5479 | struct snd_pcm_substream *substream) |
Takashi Iwai | 97ec558 | 2006-03-21 11:29:07 +0100 | [diff] [blame] | 5480 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5481 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 97ec558 | 2006-03-21 11:29:07 +0100 | [diff] [blame] | 5482 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5483 | snd_hda_codec_cleanup_stream(codec, |
| 5484 | spec->adc_nids[substream->number + 1]); |
Takashi Iwai | ac2e873 | 2013-01-17 15:57:10 +0100 | [diff] [blame] | 5485 | call_pcm_capture_hook(hinfo, codec, substream, |
| 5486 | HDA_GEN_PCM_ACT_CLEANUP); |
Takashi Iwai | 97ec558 | 2006-03-21 11:29:07 +0100 | [diff] [blame] | 5487 | return 0; |
| 5488 | } |
| 5489 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5490 | /* |
| 5491 | */ |
| 5492 | static 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 Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 5499 | .close = playback_pcm_close, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5500 | .prepare = playback_pcm_prepare, |
| 5501 | .cleanup = playback_pcm_cleanup |
| 5502 | }, |
| 5503 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5504 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5505 | static 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 Iwai | ac2e873 | 2013-01-17 15:57:10 +0100 | [diff] [blame] | 5510 | .ops = { |
| 5511 | .open = capture_pcm_open, |
| 5512 | .close = capture_pcm_close, |
| 5513 | .prepare = capture_pcm_prepare, |
| 5514 | .cleanup = capture_pcm_cleanup |
| 5515 | }, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5516 | }; |
| 5517 | |
| 5518 | static 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 Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 5523 | .ops = { |
| 5524 | .open = alt_playback_pcm_open, |
Takashi Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 5525 | .close = alt_playback_pcm_close, |
| 5526 | .prepare = alt_playback_pcm_prepare, |
| 5527 | .cleanup = alt_playback_pcm_cleanup |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 5528 | }, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5529 | }; |
| 5530 | |
| 5531 | static 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 Iwai | ac2e873 | 2013-01-17 15:57:10 +0100 | [diff] [blame] | 5537 | .open = alt_capture_pcm_open, |
| 5538 | .close = alt_capture_pcm_close, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5539 | .prepare = alt_capture_pcm_prepare, |
| 5540 | .cleanup = alt_capture_pcm_cleanup |
| 5541 | }, |
| 5542 | }; |
| 5543 | |
| 5544 | static 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 | |
| 5557 | static 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 */ |
| 5565 | static 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 | */ |
| 5574 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5587 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5588 | return false; |
| 5589 | } |
| 5590 | |
| 5591 | /* analog capture with dynamic dual-adc changes */ |
| 5592 | static 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 Iwai | 4f29efc | 2016-04-12 15:16:24 +0200 | [diff] [blame] | 5603 | call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_PREPARE); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5604 | return 0; |
| 5605 | } |
| 5606 | |
| 5607 | static 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 Iwai | 4f29efc | 2016-04-12 15:16:24 +0200 | [diff] [blame] | 5614 | call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLEANUP); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5615 | return 0; |
| 5616 | } |
| 5617 | |
| 5618 | static 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 Iwai | f873e53 | 2012-12-20 16:58:39 +0100 | [diff] [blame] | 5629 | static 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 Perches | 75b1a8f | 2021-01-04 09:17:34 -0800 | [diff] [blame] | 5636 | strscpy(str, chip_name, len); |
Takashi Iwai | f873e53 | 2012-12-20 16:58:39 +0100 | [diff] [blame] | 5637 | |
| 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 Iwai | fb83b63 | 2015-03-16 23:34:34 +0100 | [diff] [blame] | 5648 | /* copy PCM stream info from @default_str, and override non-NULL entries |
| 5649 | * from @spec_str and @nid |
| 5650 | */ |
| 5651 | static 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 Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 5675 | /** |
| 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5681 | int snd_hda_gen_build_pcms(struct hda_codec *codec) |
| 5682 | { |
| 5683 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 5684 | struct hda_pcm *info; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5685 | bool have_multi_adcs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5686 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5687 | if (spec->no_analog) |
| 5688 | goto skip_analog; |
| 5689 | |
Takashi Iwai | f873e53 | 2012-12-20 16:58:39 +0100 | [diff] [blame] | 5690 | fill_pcm_stream_name(spec->stream_name_analog, |
| 5691 | sizeof(spec->stream_name_analog), |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 5692 | " Analog", codec->core.chip_name); |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 5693 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5697 | |
| 5698 | if (spec->multiout.num_dacs > 0) { |
Takashi Iwai | fb83b63 | 2015-03-16 23:34:34 +0100 | [diff] [blame] | 5699 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5703 | 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 Iwai | fb83b63 | 2015-03-16 23:34:34 +0100 | [diff] [blame] | 5711 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5716 | } |
| 5717 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5718 | skip_analog: |
| 5719 | /* SPDIF for stream index #1 */ |
| 5720 | if (spec->multiout.dig_out_nid || spec->dig_in_nid) { |
Takashi Iwai | f873e53 | 2012-12-20 16:58:39 +0100 | [diff] [blame] | 5721 | fill_pcm_stream_name(spec->stream_name_digital, |
| 5722 | sizeof(spec->stream_name_digital), |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 5723 | " Digital", codec->core.chip_name); |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 5724 | info = snd_hda_codec_pcm_new(codec, "%s", |
| 5725 | spec->stream_name_digital); |
| 5726 | if (!info) |
| 5727 | return -ENOMEM; |
Takashi Iwai | 9ab0cb3 | 2020-07-17 17:45:17 +0200 | [diff] [blame] | 5728 | codec->follower_dig_outs = spec->multiout.follower_dig_outs; |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 5729 | spec->pcm_rec[1] = info; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5730 | if (spec->dig_out_type) |
| 5731 | info->pcm_type = spec->dig_out_type; |
| 5732 | else |
| 5733 | info->pcm_type = HDA_PCM_TYPE_SPDIF; |
Takashi Iwai | fb83b63 | 2015-03-16 23:34:34 +0100 | [diff] [blame] | 5734 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5744 | } |
| 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 Iwai | a607148 | 2013-01-21 16:50:09 +0100 | [diff] [blame] | 5756 | fill_pcm_stream_name(spec->stream_name_alt_analog, |
| 5757 | sizeof(spec->stream_name_alt_analog), |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 5758 | " Alt Analog", codec->core.chip_name); |
Takashi Iwai | bbbc7e8 | 2015-02-27 17:43:19 +0100 | [diff] [blame] | 5759 | 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 Iwai | fb83b63 | 2015-03-16 23:34:34 +0100 | [diff] [blame] | 5764 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5772 | if (have_multi_adcs) { |
Takashi Iwai | fb83b63 | 2015-03-16 23:34:34 +0100 | [diff] [blame] | 5773 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5777 | info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = |
| 5778 | spec->num_adc_nids - 1; |
| 5779 | } else { |
Takashi Iwai | fb83b63 | 2015-03-16 23:34:34 +0100 | [diff] [blame] | 5780 | setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE], |
| 5781 | &pcm_null_stream, NULL, 0); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5782 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5783 | } |
| 5784 | |
| 5785 | return 0; |
| 5786 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 5787 | EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5788 | |
| 5789 | |
| 5790 | /* |
| 5791 | * Standard auto-parser initializations |
| 5792 | */ |
| 5793 | |
Takashi Iwai | d415693 | 2013-01-07 10:08:02 +0100 | [diff] [blame] | 5794 | /* configure the given path as a proper output */ |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 5795 | static void set_output_and_unmute(struct hda_codec *codec, int path_idx) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5796 | { |
| 5797 | struct nid_path *path; |
Takashi Iwai | d415693 | 2013-01-07 10:08:02 +0100 | [diff] [blame] | 5798 | hda_nid_t pin; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5799 | |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 5800 | path = snd_hda_get_path_from_idx(codec, path_idx); |
Takashi Iwai | d415693 | 2013-01-07 10:08:02 +0100 | [diff] [blame] | 5801 | if (!path || !path->depth) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5802 | return; |
Takashi Iwai | d415693 | 2013-01-07 10:08:02 +0100 | [diff] [blame] | 5803 | pin = path->path[path->depth - 1]; |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 5804 | restore_pin_ctl(codec, pin); |
Takashi Iwai | 65033cc | 2013-04-16 12:31:05 +0200 | [diff] [blame] | 5805 | snd_hda_activate_path(codec, path, path->active, |
| 5806 | aamix_default(codec->spec)); |
Takashi Iwai | e1284af | 2013-01-03 16:33:02 +0100 | [diff] [blame] | 5807 | set_pin_eapd(codec, pin, path->active); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5808 | } |
| 5809 | |
| 5810 | /* initialize primary output paths */ |
| 5811 | static void init_multi_out(struct hda_codec *codec) |
| 5812 | { |
| 5813 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5814 | int i; |
| 5815 | |
Takashi Iwai | d415693 | 2013-01-07 10:08:02 +0100 | [diff] [blame] | 5816 | for (i = 0; i < spec->autocfg.line_outs; i++) |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 5817 | set_output_and_unmute(codec, spec->out_paths[i]); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5818 | } |
| 5819 | |
Takashi Iwai | db23fd1 | 2012-12-20 15:27:24 +0100 | [diff] [blame] | 5820 | |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 5821 | static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5822 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5823 | int i; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5824 | |
Takashi Iwai | d415693 | 2013-01-07 10:08:02 +0100 | [diff] [blame] | 5825 | for (i = 0; i < num_outs; i++) |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 5826 | set_output_and_unmute(codec, paths[i]); |
Takashi Iwai | db23fd1 | 2012-12-20 15:27:24 +0100 | [diff] [blame] | 5827 | } |
| 5828 | |
| 5829 | /* initialize hp and speaker paths */ |
| 5830 | static 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 Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 5835 | __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths); |
Takashi Iwai | db23fd1 | 2012-12-20 15:27:24 +0100 | [diff] [blame] | 5836 | if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT) |
| 5837 | __init_extra_out(codec, spec->autocfg.speaker_outs, |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 5838 | spec->speaker_paths); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5839 | } |
| 5840 | |
| 5841 | /* initialize multi-io paths */ |
| 5842 | static 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 Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 5850 | path = get_multiio_path(codec, i); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5851 | if (!path) |
| 5852 | continue; |
| 5853 | if (!spec->multi_io[i].ctl_in) |
| 5854 | spec->multi_io[i].ctl_in = |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 5855 | snd_hda_codec_get_pin_target(codec, pin); |
Takashi Iwai | 65033cc | 2013-04-16 12:31:05 +0200 | [diff] [blame] | 5856 | snd_hda_activate_path(codec, path, path->active, |
| 5857 | aamix_default(spec)); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5858 | } |
| 5859 | } |
| 5860 | |
Takashi Iwai | 4f7f67f | 2013-12-03 10:51:37 +0100 | [diff] [blame] | 5861 | static 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 Iwai | e7fdd52 | 2015-12-08 17:00:42 +0100 | [diff] [blame] | 5867 | if (!has_aamix_out_paths(spec)) |
| 5868 | return; |
Takashi Iwai | 4f7f67f | 2013-12-03 10:51:37 +0100 | [diff] [blame] | 5869 | 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 Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5880 | /* set up input pins and loopback paths */ |
| 5881 | static 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 Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 5890 | restore_pin_ctl(codec, nid); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5891 | |
| 5892 | /* init loopback inputs */ |
| 5893 | if (spec->mixer_nid) { |
Takashi Iwai | 3e367f1 | 2013-01-23 17:07:23 +0100 | [diff] [blame] | 5894 | resume_path_from_idx(codec, spec->loopback_paths[i]); |
| 5895 | resume_path_from_idx(codec, spec->loopback_merge_path); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5896 | } |
| 5897 | } |
| 5898 | } |
| 5899 | |
| 5900 | /* initialize ADC paths */ |
| 5901 | static 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 Iwai | c697b71 | 2013-01-07 17:09:26 +0100 | [diff] [blame] | 5915 | path = get_input_path(codec, c, i); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5916 | 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 Iwai | 967303d | 2013-02-19 17:12:42 +0100 | [diff] [blame] | 5923 | if (spec->hp_mic) |
| 5924 | update_hp_mic(codec, c, true); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5925 | } |
| 5926 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5927 | if (spec->cap_sync_hook) |
Takashi Iwai | 7fe3071 | 2014-01-30 17:59:02 +0100 | [diff] [blame] | 5928 | spec->cap_sync_hook(codec, NULL, NULL); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5929 | } |
| 5930 | |
| 5931 | /* set right pin controls for digital I/O */ |
| 5932 | static 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 Iwai | d415693 | 2013-01-07 10:08:02 +0100 | [diff] [blame] | 5938 | for (i = 0; i < spec->autocfg.dig_outs; i++) |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 5939 | set_output_and_unmute(codec, spec->digout_paths[i]); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5940 | pin = spec->autocfg.dig_in_pin; |
Takashi Iwai | 2430d7b | 2013-01-04 15:09:42 +0100 | [diff] [blame] | 5941 | if (pin) { |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 5942 | restore_pin_ctl(codec, pin); |
Takashi Iwai | 3e367f1 | 2013-01-23 17:07:23 +0100 | [diff] [blame] | 5943 | resume_path_from_idx(codec, spec->digin_path); |
Takashi Iwai | 2430d7b | 2013-01-04 15:09:42 +0100 | [diff] [blame] | 5944 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5945 | } |
| 5946 | |
Takashi Iwai | 973e497 | 2012-12-20 15:16:09 +0100 | [diff] [blame] | 5947 | /* clear unsol-event tags on unused pins; Conexant codecs seem to leave |
| 5948 | * invalid unsol tags by some reason |
| 5949 | */ |
| 5950 | static void clear_unsol_on_unused_pins(struct hda_codec *codec) |
| 5951 | { |
Takashi Iwai | a9c2dfc | 2018-04-23 17:24:56 +0200 | [diff] [blame] | 5952 | const struct hda_pincfg *pin; |
Takashi Iwai | 973e497 | 2012-12-20 15:16:09 +0100 | [diff] [blame] | 5953 | int i; |
| 5954 | |
Takashi Iwai | a9c2dfc | 2018-04-23 17:24:56 +0200 | [diff] [blame] | 5955 | snd_array_for_each(&codec->init_pins, i, pin) { |
Takashi Iwai | 973e497 | 2012-12-20 15:16:09 +0100 | [diff] [blame] | 5956 | hda_nid_t nid = pin->nid; |
| 5957 | if (is_jack_detectable(codec, nid) && |
| 5958 | !snd_hda_jack_tbl_get(codec, nid)) |
Takashi Iwai | 401caff | 2018-06-27 11:43:09 +0200 | [diff] [blame] | 5959 | snd_hda_codec_write_cache(codec, nid, 0, |
Takashi Iwai | 973e497 | 2012-12-20 15:16:09 +0100 | [diff] [blame] | 5960 | AC_VERB_SET_UNSOLICITED_ENABLE, 0); |
| 5961 | } |
| 5962 | } |
| 5963 | |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 5964 | /** |
| 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 Iwai | 5187ac1 | 2013-01-07 12:52:16 +0100 | [diff] [blame] | 5969 | */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5970 | int 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 Iwai | 89781d0 | 2019-08-30 12:03:38 +0200 | [diff] [blame] | 5977 | if (!spec->skip_verbs) |
| 5978 | snd_hda_apply_verbs(codec); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5979 | |
| 5980 | init_multi_out(codec); |
| 5981 | init_extra_out(codec); |
| 5982 | init_multi_io(codec); |
Takashi Iwai | 4f7f67f | 2013-12-03 10:51:37 +0100 | [diff] [blame] | 5983 | init_aamix_paths(codec); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5984 | init_analog_input(codec); |
| 5985 | init_input_src(codec); |
| 5986 | init_digital(codec); |
| 5987 | |
Takashi Iwai | 973e497 | 2012-12-20 15:16:09 +0100 | [diff] [blame] | 5988 | clear_unsol_on_unused_pins(codec); |
| 5989 | |
Takashi Iwai | e6feb5d | 2015-03-16 21:32:11 +0100 | [diff] [blame] | 5990 | sync_all_pin_power_ctls(codec); |
| 5991 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5992 | /* call init functions of standard auto-mute helpers */ |
Takashi Iwai | a5cc250 | 2013-01-16 18:08:55 +0100 | [diff] [blame] | 5993 | update_automute_all(codec); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5994 | |
Takashi Iwai | 1a462be | 2020-01-09 10:01:04 +0100 | [diff] [blame] | 5995 | snd_hda_regmap_sync(codec); |
Takashi Iwai | 3bbcd27 | 2012-12-20 11:50:58 +0100 | [diff] [blame] | 5996 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 5997 | 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 Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 6003 | EXPORT_SYMBOL_GPL(snd_hda_gen_init); |
Takashi Iwai | fce52a3 | 2013-01-07 12:42:48 +0100 | [diff] [blame] | 6004 | |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 6005 | /** |
| 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 Iwai | 5187ac1 | 2013-01-07 12:52:16 +0100 | [diff] [blame] | 6010 | */ |
Takashi Iwai | fce52a3 | 2013-01-07 12:42:48 +0100 | [diff] [blame] | 6011 | void snd_hda_gen_free(struct hda_codec *codec) |
| 6012 | { |
Takashi Iwai | 8a02c0c | 2014-02-10 18:09:45 +0100 | [diff] [blame] | 6013 | snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE); |
Takashi Iwai | fce52a3 | 2013-01-07 12:42:48 +0100 | [diff] [blame] | 6014 | snd_hda_gen_spec_free(codec->spec); |
| 6015 | kfree(codec->spec); |
| 6016 | codec->spec = NULL; |
| 6017 | } |
Takashi Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 6018 | EXPORT_SYMBOL_GPL(snd_hda_gen_free); |
Takashi Iwai | fce52a3 | 2013-01-07 12:42:48 +0100 | [diff] [blame] | 6019 | |
| 6020 | #ifdef CONFIG_PM |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 6021 | /** |
| 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 Iwai | 5187ac1 | 2013-01-07 12:52:16 +0100 | [diff] [blame] | 6027 | */ |
Takashi Iwai | fce52a3 | 2013-01-07 12:42:48 +0100 | [diff] [blame] | 6028 | int 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 Iwai | 2698ea9 | 2013-12-18 07:45:52 +0100 | [diff] [blame] | 6033 | EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status); |
Takashi Iwai | fce52a3 | 2013-01-07 12:42:48 +0100 | [diff] [blame] | 6034 | #endif |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 6035 | |
| 6036 | |
| 6037 | /* |
| 6038 | * the generic codec support |
| 6039 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6040 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 6041 | static 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 Iwai | fce52a3 | 2013-01-07 12:42:48 +0100 | [diff] [blame] | 6045 | .free = snd_hda_gen_free, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 6046 | .unsol_event = snd_hda_jack_unsol_event, |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 6047 | #ifdef CONFIG_PM |
Takashi Iwai | fce52a3 | 2013-01-07 12:42:48 +0100 | [diff] [blame] | 6048 | .check_power_status = snd_hda_gen_check_power_status, |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 6049 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6050 | }; |
| 6051 | |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 6052 | /* |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 6053 | * snd_hda_parse_generic_codec - Generic codec parser |
| 6054 | * @codec: the HDA codec |
Takashi Iwai | dda42bd | 2014-10-30 12:04:50 +0100 | [diff] [blame] | 6055 | */ |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 6056 | static int snd_hda_parse_generic_codec(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6057 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 6058 | struct hda_gen_spec *spec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6059 | int err; |
| 6060 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 6061 | spec = kzalloc(sizeof(*spec), GFP_KERNEL); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 6062 | if (!spec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6063 | return -ENOMEM; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 6064 | snd_hda_gen_spec_init(spec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6065 | codec->spec = spec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6066 | |
Takashi Iwai | 9eb413e | 2012-12-19 14:41:21 +0100 | [diff] [blame] | 6067 | err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0); |
| 6068 | if (err < 0) |
Wenwen Wang | cfef67f | 2019-08-09 23:29:48 -0500 | [diff] [blame] | 6069 | goto error; |
Takashi Iwai | 9eb413e | 2012-12-19 14:41:21 +0100 | [diff] [blame] | 6070 | |
| 6071 | err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 6072 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6073 | goto error; |
| 6074 | |
| 6075 | codec->patch_ops = generic_patch_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6076 | return 0; |
| 6077 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 6078 | error: |
Takashi Iwai | fce52a3 | 2013-01-07 12:42:48 +0100 | [diff] [blame] | 6079 | snd_hda_gen_free(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6080 | return err; |
| 6081 | } |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 6082 | |
Takashi Iwai | b9a94a9 | 2015-10-01 16:20:04 +0200 | [diff] [blame] | 6083 | static const struct hda_device_id snd_hda_id_generic[] = { |
| 6084 | HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec), |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 6085 | {} /* terminator */ |
| 6086 | }; |
Takashi Iwai | b9a94a9 | 2015-10-01 16:20:04 +0200 | [diff] [blame] | 6087 | MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 6088 | |
| 6089 | static struct hda_codec_driver generic_driver = { |
Takashi Iwai | b9a94a9 | 2015-10-01 16:20:04 +0200 | [diff] [blame] | 6090 | .id = snd_hda_id_generic, |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 6091 | }; |
| 6092 | |
| 6093 | module_hda_codec_driver(generic_driver); |
Takashi Iwai | b21bdd0 | 2013-11-18 12:03:56 +0100 | [diff] [blame] | 6094 | |
| 6095 | MODULE_LICENSE("GPL"); |
| 6096 | MODULE_DESCRIPTION("Generic HD-audio codec parser"); |