Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Universal Interface for Intel High Definition Audio Codec |
| 3 | * |
| 4 | * Generic widget tree parser |
| 5 | * |
| 6 | * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de> |
| 7 | * |
| 8 | * This driver is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This driver is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/init.h> |
| 24 | #include <linux/slab.h> |
Paul Gortmaker | d81a6d7 | 2011-09-22 09:34:58 -0400 | [diff] [blame] | 25 | #include <linux/export.h> |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 26 | #include <linux/sort.h> |
Takashi Iwai | f873e53 | 2012-12-20 16:58:39 +0100 | [diff] [blame] | 27 | #include <linux/ctype.h> |
| 28 | #include <linux/string.h> |
Takashi Iwai | 29476558 | 2013-01-17 09:52:11 +0100 | [diff] [blame] | 29 | #include <linux/bitops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <sound/core.h> |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 31 | #include <sound/jack.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "hda_codec.h" |
| 33 | #include "hda_local.h" |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 34 | #include "hda_auto_parser.h" |
| 35 | #include "hda_jack.h" |
| 36 | #include "hda_generic.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
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 | /* initialize hda_gen_spec struct */ |
| 40 | int snd_hda_gen_spec_init(struct hda_gen_spec *spec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 42 | snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 43 | snd_array_init(&spec->paths, sizeof(struct nid_path), 8); |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 44 | mutex_init(&spec->pcm_mutex); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 45 | return 0; |
| 46 | } |
| 47 | EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Takashi Iwai | 12c93df | 2012-12-19 14:38:33 +0100 | [diff] [blame] | 49 | struct snd_kcontrol_new * |
| 50 | snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name, |
| 51 | const struct snd_kcontrol_new *temp) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 52 | { |
| 53 | struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls); |
| 54 | if (!knew) |
| 55 | return NULL; |
| 56 | *knew = *temp; |
| 57 | if (name) |
| 58 | knew->name = kstrdup(name, GFP_KERNEL); |
| 59 | else if (knew->name) |
| 60 | knew->name = kstrdup(knew->name, GFP_KERNEL); |
| 61 | if (!knew->name) |
| 62 | return NULL; |
| 63 | return knew; |
| 64 | } |
Takashi Iwai | 12c93df | 2012-12-19 14:38:33 +0100 | [diff] [blame] | 65 | EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 66 | |
| 67 | static void free_kctls(struct hda_gen_spec *spec) |
| 68 | { |
| 69 | if (spec->kctls.list) { |
| 70 | struct snd_kcontrol_new *kctl = spec->kctls.list; |
| 71 | int i; |
| 72 | for (i = 0; i < spec->kctls.used; i++) |
| 73 | kfree(kctl[i].name); |
| 74 | } |
| 75 | snd_array_free(&spec->kctls); |
| 76 | } |
| 77 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 78 | void snd_hda_gen_spec_free(struct hda_gen_spec *spec) |
| 79 | { |
| 80 | if (!spec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | return; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 82 | free_kctls(spec); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 83 | snd_array_free(&spec->paths); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 85 | EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | /* |
Takashi Iwai | 1c70a58 | 2013-01-11 17:48:22 +0100 | [diff] [blame] | 88 | * store user hints |
| 89 | */ |
| 90 | static void parse_user_hints(struct hda_codec *codec) |
| 91 | { |
| 92 | struct hda_gen_spec *spec = codec->spec; |
| 93 | int val; |
| 94 | |
| 95 | val = snd_hda_get_bool_hint(codec, "jack_detect"); |
| 96 | if (val >= 0) |
| 97 | codec->no_jack_detect = !val; |
| 98 | val = snd_hda_get_bool_hint(codec, "inv_jack_detect"); |
| 99 | if (val >= 0) |
| 100 | codec->inv_jack_detect = !!val; |
| 101 | val = snd_hda_get_bool_hint(codec, "trigger_sense"); |
| 102 | if (val >= 0) |
| 103 | codec->no_trigger_sense = !val; |
| 104 | val = snd_hda_get_bool_hint(codec, "inv_eapd"); |
| 105 | if (val >= 0) |
| 106 | codec->inv_eapd = !!val; |
| 107 | val = snd_hda_get_bool_hint(codec, "pcm_format_first"); |
| 108 | if (val >= 0) |
| 109 | codec->pcm_format_first = !!val; |
| 110 | val = snd_hda_get_bool_hint(codec, "sticky_stream"); |
| 111 | if (val >= 0) |
| 112 | codec->no_sticky_stream = !val; |
| 113 | val = snd_hda_get_bool_hint(codec, "spdif_status_reset"); |
| 114 | if (val >= 0) |
| 115 | codec->spdif_status_reset = !!val; |
| 116 | val = snd_hda_get_bool_hint(codec, "pin_amp_workaround"); |
| 117 | if (val >= 0) |
| 118 | codec->pin_amp_workaround = !!val; |
| 119 | val = snd_hda_get_bool_hint(codec, "single_adc_amp"); |
| 120 | if (val >= 0) |
| 121 | codec->single_adc_amp = !!val; |
| 122 | |
Takashi Iwai | f72706b | 2013-01-16 18:20:07 +0100 | [diff] [blame] | 123 | val = snd_hda_get_bool_hint(codec, "auto_mute"); |
| 124 | if (val >= 0) |
| 125 | spec->suppress_auto_mute = !val; |
Takashi Iwai | 1c70a58 | 2013-01-11 17:48:22 +0100 | [diff] [blame] | 126 | val = snd_hda_get_bool_hint(codec, "auto_mic"); |
| 127 | if (val >= 0) |
| 128 | spec->suppress_auto_mic = !val; |
| 129 | val = snd_hda_get_bool_hint(codec, "line_in_auto_switch"); |
| 130 | if (val >= 0) |
| 131 | spec->line_in_auto_switch = !!val; |
| 132 | val = snd_hda_get_bool_hint(codec, "need_dac_fix"); |
| 133 | if (val >= 0) |
| 134 | spec->need_dac_fix = !!val; |
| 135 | val = snd_hda_get_bool_hint(codec, "primary_hp"); |
| 136 | if (val >= 0) |
| 137 | spec->no_primary_hp = !val; |
| 138 | val = snd_hda_get_bool_hint(codec, "multi_cap_vol"); |
| 139 | if (val >= 0) |
| 140 | spec->multi_cap_vol = !!val; |
| 141 | val = snd_hda_get_bool_hint(codec, "inv_dmic_split"); |
| 142 | if (val >= 0) |
| 143 | spec->inv_dmic_split = !!val; |
| 144 | val = snd_hda_get_bool_hint(codec, "indep_hp"); |
| 145 | if (val >= 0) |
| 146 | spec->indep_hp = !!val; |
| 147 | val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input"); |
| 148 | if (val >= 0) |
| 149 | spec->add_stereo_mix_input = !!val; |
| 150 | val = snd_hda_get_bool_hint(codec, "add_out_jack_modes"); |
| 151 | if (val >= 0) |
| 152 | spec->add_out_jack_modes = !!val; |
Takashi Iwai | 29476558 | 2013-01-17 09:52:11 +0100 | [diff] [blame] | 153 | val = snd_hda_get_bool_hint(codec, "add_in_jack_modes"); |
| 154 | if (val >= 0) |
| 155 | spec->add_in_jack_modes = !!val; |
Takashi Iwai | 1c70a58 | 2013-01-11 17:48:22 +0100 | [diff] [blame] | 156 | |
| 157 | if (!snd_hda_get_int_hint(codec, "mixer_nid", &val)) |
| 158 | spec->mixer_nid = val; |
| 159 | } |
| 160 | |
| 161 | /* |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 162 | * pin control value accesses |
| 163 | */ |
| 164 | |
| 165 | #define update_pin_ctl(codec, pin, val) \ |
| 166 | snd_hda_codec_update_cache(codec, pin, 0, \ |
| 167 | AC_VERB_SET_PIN_WIDGET_CONTROL, val) |
| 168 | |
| 169 | /* restore the pinctl based on the cached value */ |
| 170 | static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin) |
| 171 | { |
| 172 | update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin)); |
| 173 | } |
| 174 | |
| 175 | /* set the pinctl target value and write it if requested */ |
| 176 | static void set_pin_target(struct hda_codec *codec, hda_nid_t pin, |
| 177 | unsigned int val, bool do_write) |
| 178 | { |
| 179 | if (!pin) |
| 180 | return; |
| 181 | val = snd_hda_correct_pin_ctl(codec, pin, val); |
| 182 | snd_hda_codec_set_pin_target(codec, pin, val); |
| 183 | if (do_write) |
| 184 | update_pin_ctl(codec, pin, val); |
| 185 | } |
| 186 | |
| 187 | /* set pinctl target values for all given pins */ |
| 188 | static void set_pin_targets(struct hda_codec *codec, int num_pins, |
| 189 | hda_nid_t *pins, unsigned int val) |
| 190 | { |
| 191 | int i; |
| 192 | for (i = 0; i < num_pins; i++) |
| 193 | set_pin_target(codec, pins[i], val, false); |
| 194 | } |
| 195 | |
| 196 | /* |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 197 | * parsing paths |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 200 | /* return the position of NID in the list, or -1 if not found */ |
| 201 | static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums) |
| 202 | { |
| 203 | int i; |
| 204 | for (i = 0; i < nums; i++) |
| 205 | if (list[i] == nid) |
| 206 | return i; |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | /* return true if the given NID is contained in the path */ |
| 211 | static bool is_nid_contained(struct nid_path *path, hda_nid_t nid) |
| 212 | { |
| 213 | return find_idx_in_nid_list(nid, path->path, path->depth) >= 0; |
| 214 | } |
| 215 | |
Takashi Iwai | f5172a7 | 2013-01-04 13:19:55 +0100 | [diff] [blame] | 216 | static struct nid_path *get_nid_path(struct hda_codec *codec, |
| 217 | hda_nid_t from_nid, hda_nid_t to_nid, |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 218 | int anchor_nid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 220 | struct hda_gen_spec *spec = codec->spec; |
| 221 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 223 | for (i = 0; i < spec->paths.used; i++) { |
| 224 | struct nid_path *path = snd_array_elem(&spec->paths, i); |
| 225 | if (path->depth <= 0) |
| 226 | continue; |
| 227 | if ((!from_nid || path->path[0] == from_nid) && |
Takashi Iwai | f5172a7 | 2013-01-04 13:19:55 +0100 | [diff] [blame] | 228 | (!to_nid || path->path[path->depth - 1] == to_nid)) { |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 229 | if (!anchor_nid || |
| 230 | (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) || |
| 231 | (anchor_nid < 0 && !is_nid_contained(path, anchor_nid))) |
Takashi Iwai | f5172a7 | 2013-01-04 13:19:55 +0100 | [diff] [blame] | 232 | return path; |
| 233 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
| 235 | return NULL; |
| 236 | } |
Takashi Iwai | f5172a7 | 2013-01-04 13:19:55 +0100 | [diff] [blame] | 237 | |
| 238 | /* get the path between the given NIDs; |
| 239 | * passing 0 to either @pin or @dac behaves as a wildcard |
| 240 | */ |
| 241 | struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec, |
| 242 | hda_nid_t from_nid, hda_nid_t to_nid) |
| 243 | { |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 244 | return get_nid_path(codec, from_nid, to_nid, 0); |
Takashi Iwai | f5172a7 | 2013-01-04 13:19:55 +0100 | [diff] [blame] | 245 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 246 | EXPORT_SYMBOL_HDA(snd_hda_get_nid_path); |
| 247 | |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 248 | /* get the index number corresponding to the path instance; |
| 249 | * the index starts from 1, for easier checking the invalid value |
| 250 | */ |
| 251 | int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path) |
| 252 | { |
| 253 | struct hda_gen_spec *spec = codec->spec; |
| 254 | struct nid_path *array = spec->paths.list; |
| 255 | ssize_t idx; |
| 256 | |
| 257 | if (!spec->paths.used) |
| 258 | return 0; |
| 259 | idx = path - array; |
| 260 | if (idx < 0 || idx >= spec->paths.used) |
| 261 | return 0; |
| 262 | return idx + 1; |
| 263 | } |
| 264 | |
| 265 | /* get the path instance corresponding to the given index number */ |
| 266 | struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx) |
| 267 | { |
| 268 | struct hda_gen_spec *spec = codec->spec; |
| 269 | |
| 270 | if (idx <= 0 || idx > spec->paths.used) |
| 271 | return NULL; |
| 272 | return snd_array_elem(&spec->paths, idx - 1); |
| 273 | } |
| 274 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 275 | /* check whether the given DAC is already found in any existing paths */ |
| 276 | static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid) |
| 277 | { |
| 278 | struct hda_gen_spec *spec = codec->spec; |
| 279 | int i; |
| 280 | |
| 281 | for (i = 0; i < spec->paths.used; i++) { |
| 282 | struct nid_path *path = snd_array_elem(&spec->paths, i); |
| 283 | if (path->path[0] == nid) |
| 284 | return true; |
| 285 | } |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | /* check whether the given two widgets can be connected */ |
| 290 | static bool is_reachable_path(struct hda_codec *codec, |
| 291 | hda_nid_t from_nid, hda_nid_t to_nid) |
| 292 | { |
| 293 | if (!from_nid || !to_nid) |
| 294 | return false; |
| 295 | return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0; |
| 296 | } |
| 297 | |
| 298 | /* nid, dir and idx */ |
| 299 | #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19)) |
| 300 | |
| 301 | /* check whether the given ctl is already assigned in any path elements */ |
| 302 | static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type) |
| 303 | { |
| 304 | struct hda_gen_spec *spec = codec->spec; |
| 305 | int i; |
| 306 | |
| 307 | val &= AMP_VAL_COMPARE_MASK; |
| 308 | for (i = 0; i < spec->paths.used; i++) { |
| 309 | struct nid_path *path = snd_array_elem(&spec->paths, i); |
| 310 | if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val) |
| 311 | return true; |
| 312 | } |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | /* check whether a control with the given (nid, dir, idx) was assigned */ |
| 317 | static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid, |
| 318 | int dir, int idx) |
| 319 | { |
| 320 | unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir); |
| 321 | return is_ctl_used(codec, val, NID_PATH_VOL_CTL) || |
| 322 | is_ctl_used(codec, val, NID_PATH_MUTE_CTL); |
| 323 | } |
| 324 | |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 325 | static void print_nid_path(const char *pfx, struct nid_path *path) |
| 326 | { |
| 327 | char buf[40]; |
| 328 | int i; |
| 329 | |
| 330 | |
| 331 | buf[0] = 0; |
| 332 | for (i = 0; i < path->depth; i++) { |
| 333 | char tmp[4]; |
| 334 | sprintf(tmp, ":%02x", path->path[i]); |
| 335 | strlcat(buf, tmp, sizeof(buf)); |
| 336 | } |
| 337 | snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf); |
| 338 | } |
| 339 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 340 | /* called recursively */ |
| 341 | static bool __parse_nid_path(struct hda_codec *codec, |
| 342 | hda_nid_t from_nid, hda_nid_t to_nid, |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 343 | int anchor_nid, struct nid_path *path, |
| 344 | int depth) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 345 | { |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 346 | const hda_nid_t *conn; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 347 | int i, nums; |
| 348 | |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 349 | if (to_nid == anchor_nid) |
| 350 | anchor_nid = 0; /* anchor passed */ |
| 351 | else if (to_nid == (hda_nid_t)(-anchor_nid)) |
| 352 | return false; /* hit the exclusive nid */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 353 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 354 | nums = snd_hda_get_conn_list(codec, to_nid, &conn); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 355 | for (i = 0; i < nums; i++) { |
| 356 | if (conn[i] != from_nid) { |
| 357 | /* special case: when from_nid is 0, |
| 358 | * try to find an empty DAC |
| 359 | */ |
| 360 | if (from_nid || |
| 361 | get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT || |
| 362 | is_dac_already_used(codec, conn[i])) |
| 363 | continue; |
| 364 | } |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 365 | /* anchor is not requested or already passed? */ |
| 366 | if (anchor_nid <= 0) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 367 | goto found; |
| 368 | } |
| 369 | if (depth >= MAX_NID_PATH_DEPTH) |
| 370 | return false; |
| 371 | for (i = 0; i < nums; i++) { |
| 372 | unsigned int type; |
| 373 | type = get_wcaps_type(get_wcaps(codec, conn[i])); |
| 374 | if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN || |
| 375 | type == AC_WID_PIN) |
| 376 | continue; |
| 377 | if (__parse_nid_path(codec, from_nid, conn[i], |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 378 | anchor_nid, path, depth + 1)) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 379 | goto found; |
| 380 | } |
| 381 | return false; |
| 382 | |
| 383 | found: |
| 384 | path->path[path->depth] = conn[i]; |
| 385 | path->idx[path->depth + 1] = i; |
| 386 | if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX) |
| 387 | path->multi[path->depth + 1] = 1; |
| 388 | path->depth++; |
| 389 | return true; |
| 390 | } |
| 391 | |
| 392 | /* parse the widget path from the given nid to the target nid; |
| 393 | * when @from_nid is 0, try to find an empty DAC; |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 394 | * when @anchor_nid is set to a positive value, only paths through the widget |
| 395 | * with the given value are evaluated. |
| 396 | * when @anchor_nid is set to a negative value, paths through the widget |
| 397 | * with the negative of given value are excluded, only other paths are chosen. |
| 398 | * when @anchor_nid is zero, no special handling about path selection. |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 399 | */ |
| 400 | 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] | 401 | hda_nid_t to_nid, int anchor_nid, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 402 | struct nid_path *path) |
| 403 | { |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 404 | 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] | 405 | path->path[path->depth] = to_nid; |
| 406 | path->depth++; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 407 | return true; |
| 408 | } |
| 409 | return false; |
| 410 | } |
| 411 | EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 413 | /* |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 414 | * parse the path between the given NIDs and add to the path list. |
| 415 | * if no valid path is found, return NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 417 | struct nid_path * |
| 418 | 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] | 419 | hda_nid_t to_nid, int anchor_nid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 421 | struct hda_gen_spec *spec = codec->spec; |
| 422 | struct nid_path *path; |
| 423 | |
| 424 | if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid)) |
| 425 | return NULL; |
| 426 | |
Takashi Iwai | f5172a7 | 2013-01-04 13:19:55 +0100 | [diff] [blame] | 427 | /* check whether the path has been already added */ |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 428 | path = get_nid_path(codec, from_nid, to_nid, anchor_nid); |
Takashi Iwai | f5172a7 | 2013-01-04 13:19:55 +0100 | [diff] [blame] | 429 | if (path) |
| 430 | return path; |
| 431 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 432 | path = snd_array_new(&spec->paths); |
| 433 | if (!path) |
| 434 | return NULL; |
| 435 | memset(path, 0, sizeof(*path)); |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 436 | 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] | 437 | return path; |
| 438 | /* push back */ |
| 439 | spec->paths.used--; |
| 440 | return NULL; |
| 441 | } |
| 442 | EXPORT_SYMBOL_HDA(snd_hda_add_new_path); |
| 443 | |
Takashi Iwai | 980428c | 2013-01-09 09:28:20 +0100 | [diff] [blame] | 444 | /* clear the given path as invalid so that it won't be picked up later */ |
| 445 | static void invalidate_nid_path(struct hda_codec *codec, int idx) |
| 446 | { |
| 447 | struct nid_path *path = snd_hda_get_path_from_idx(codec, idx); |
| 448 | if (!path) |
| 449 | return; |
| 450 | memset(path, 0, sizeof(*path)); |
| 451 | } |
| 452 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 453 | /* look for an empty DAC slot */ |
| 454 | static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin, |
| 455 | bool is_digital) |
| 456 | { |
| 457 | struct hda_gen_spec *spec = codec->spec; |
| 458 | bool cap_digital; |
| 459 | int i; |
| 460 | |
| 461 | for (i = 0; i < spec->num_all_dacs; i++) { |
| 462 | hda_nid_t nid = spec->all_dacs[i]; |
| 463 | if (!nid || is_dac_already_used(codec, nid)) |
| 464 | continue; |
| 465 | cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL); |
| 466 | if (is_digital != cap_digital) |
| 467 | continue; |
| 468 | if (is_reachable_path(codec, nid, pin)) |
| 469 | return nid; |
| 470 | } |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | /* replace the channels in the composed amp value with the given number */ |
| 475 | static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs) |
| 476 | { |
| 477 | val &= ~(0x3U << 16); |
| 478 | val |= chs << 16; |
| 479 | return val; |
| 480 | } |
| 481 | |
| 482 | /* check whether the widget has the given amp capability for the direction */ |
| 483 | static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid, |
| 484 | int dir, unsigned int bits) |
| 485 | { |
| 486 | if (!nid) |
| 487 | return false; |
| 488 | if (get_wcaps(codec, nid) & (1 << (dir + 1))) |
| 489 | if (query_amp_caps(codec, nid, dir) & bits) |
| 490 | return true; |
| 491 | return false; |
| 492 | } |
| 493 | |
David Henningsson | 99a5592 | 2013-01-16 15:58:44 +0100 | [diff] [blame] | 494 | static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1, |
| 495 | hda_nid_t nid2, int dir) |
| 496 | { |
| 497 | if (!(get_wcaps(codec, nid1) & (1 << (dir + 1)))) |
| 498 | return !(get_wcaps(codec, nid2) & (1 << (dir + 1))); |
| 499 | return (query_amp_caps(codec, nid1, dir) == |
| 500 | query_amp_caps(codec, nid2, dir)); |
| 501 | } |
| 502 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 503 | #define nid_has_mute(codec, nid, dir) \ |
| 504 | check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE) |
| 505 | #define nid_has_volume(codec, nid, dir) \ |
| 506 | check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS) |
| 507 | |
| 508 | /* look for a widget suitable for assigning a mute switch in the path */ |
| 509 | static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec, |
| 510 | struct nid_path *path) |
| 511 | { |
| 512 | int i; |
| 513 | |
| 514 | for (i = path->depth - 1; i >= 0; i--) { |
| 515 | if (nid_has_mute(codec, path->path[i], HDA_OUTPUT)) |
| 516 | return path->path[i]; |
| 517 | if (i != path->depth - 1 && i != 0 && |
| 518 | nid_has_mute(codec, path->path[i], HDA_INPUT)) |
| 519 | return path->path[i]; |
| 520 | } |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | /* look for a widget suitable for assigning a volume ctl in the path */ |
| 525 | static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec, |
| 526 | struct nid_path *path) |
| 527 | { |
| 528 | int i; |
| 529 | |
| 530 | for (i = path->depth - 1; i >= 0; i--) { |
| 531 | if (nid_has_volume(codec, path->path[i], HDA_OUTPUT)) |
| 532 | return path->path[i]; |
| 533 | } |
Takashi Iwai | 82beb8f | 2007-08-10 17:09:26 +0200 | [diff] [blame] | 534 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /* |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 538 | * path activation / deactivation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 540 | |
| 541 | /* can have the amp-in capability? */ |
| 542 | 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] | 543 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 544 | hda_nid_t nid = path->path[idx]; |
| 545 | unsigned int caps = get_wcaps(codec, nid); |
| 546 | unsigned int type = get_wcaps_type(caps); |
| 547 | |
| 548 | if (!(caps & AC_WCAP_IN_AMP)) |
| 549 | return false; |
| 550 | if (type == AC_WID_PIN && idx > 0) /* only for input pins */ |
| 551 | return false; |
| 552 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 555 | /* can have the amp-out capability? */ |
| 556 | 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] | 557 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 558 | hda_nid_t nid = path->path[idx]; |
| 559 | unsigned int caps = get_wcaps(codec, nid); |
| 560 | unsigned int type = get_wcaps_type(caps); |
| 561 | |
| 562 | if (!(caps & AC_WCAP_OUT_AMP)) |
| 563 | return false; |
| 564 | if (type == AC_WID_PIN && !idx) /* only for output pins */ |
| 565 | return false; |
| 566 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 569 | /* check whether the given (nid,dir,idx) is active */ |
| 570 | static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid, |
| 571 | unsigned int idx, unsigned int dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 573 | struct hda_gen_spec *spec = codec->spec; |
| 574 | int i, n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 576 | for (n = 0; n < spec->paths.used; n++) { |
| 577 | struct nid_path *path = snd_array_elem(&spec->paths, n); |
| 578 | if (!path->active) |
| 579 | continue; |
| 580 | for (i = 0; i < path->depth; i++) { |
| 581 | if (path->path[i] == nid) { |
| 582 | if (dir == HDA_OUTPUT || path->idx[i] == idx) |
| 583 | return true; |
| 584 | break; |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | return false; |
| 589 | } |
| 590 | |
| 591 | /* get the default amp value for the target state */ |
| 592 | static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid, |
| 593 | int dir, bool enable) |
| 594 | { |
| 595 | unsigned int caps; |
| 596 | unsigned int val = 0; |
| 597 | |
| 598 | caps = query_amp_caps(codec, nid, dir); |
| 599 | if (caps & AC_AMPCAP_NUM_STEPS) { |
| 600 | /* set to 0dB */ |
| 601 | if (enable) |
| 602 | val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT; |
| 603 | } |
| 604 | if (caps & AC_AMPCAP_MUTE) { |
| 605 | if (!enable) |
| 606 | val |= HDA_AMP_MUTE; |
| 607 | } |
| 608 | return val; |
| 609 | } |
| 610 | |
| 611 | /* initialize the amp value (only at the first time) */ |
| 612 | static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx) |
| 613 | { |
| 614 | int val = get_amp_val_to_activate(codec, nid, dir, false); |
| 615 | snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val); |
| 616 | } |
| 617 | |
| 618 | static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir, |
| 619 | int idx, bool enable) |
| 620 | { |
| 621 | int val; |
| 622 | if (is_ctl_associated(codec, nid, dir, idx) || |
Takashi Iwai | 985803c | 2013-01-03 16:30:04 +0100 | [diff] [blame] | 623 | (!enable && is_active_nid(codec, nid, dir, idx))) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 624 | return; |
| 625 | val = get_amp_val_to_activate(codec, nid, dir, enable); |
| 626 | snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val); |
| 627 | } |
| 628 | |
| 629 | static void activate_amp_out(struct hda_codec *codec, struct nid_path *path, |
| 630 | int i, bool enable) |
| 631 | { |
| 632 | hda_nid_t nid = path->path[i]; |
| 633 | init_amp(codec, nid, HDA_OUTPUT, 0); |
| 634 | activate_amp(codec, nid, HDA_OUTPUT, 0, enable); |
| 635 | } |
| 636 | |
| 637 | static void activate_amp_in(struct hda_codec *codec, struct nid_path *path, |
| 638 | int i, bool enable, bool add_aamix) |
| 639 | { |
| 640 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 641 | const hda_nid_t *conn; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 642 | int n, nums, idx; |
| 643 | int type; |
| 644 | hda_nid_t nid = path->path[i]; |
| 645 | |
Takashi Iwai | ee8e765 | 2013-01-03 15:25:11 +0100 | [diff] [blame] | 646 | nums = snd_hda_get_conn_list(codec, nid, &conn); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 647 | type = get_wcaps_type(get_wcaps(codec, nid)); |
| 648 | if (type == AC_WID_PIN || |
| 649 | (type == AC_WID_AUD_IN && codec->single_adc_amp)) { |
| 650 | nums = 1; |
| 651 | idx = 0; |
| 652 | } else |
| 653 | idx = path->idx[i]; |
| 654 | |
| 655 | for (n = 0; n < nums; n++) |
| 656 | init_amp(codec, nid, HDA_INPUT, n); |
| 657 | |
| 658 | if (is_ctl_associated(codec, nid, HDA_INPUT, idx)) |
| 659 | return; |
| 660 | |
| 661 | /* here is a little bit tricky in comparison with activate_amp_out(); |
| 662 | * when aa-mixer is available, we need to enable the path as well |
| 663 | */ |
| 664 | for (n = 0; n < nums; n++) { |
| 665 | if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid)) |
| 666 | continue; |
| 667 | activate_amp(codec, nid, HDA_INPUT, n, enable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 671 | /* activate or deactivate the given path |
| 672 | * 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] | 673 | */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 674 | void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path, |
| 675 | bool enable, bool add_aamix) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 677 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 679 | if (!enable) |
| 680 | path->active = false; |
| 681 | |
| 682 | for (i = path->depth - 1; i >= 0; i--) { |
| 683 | if (enable && path->multi[i]) |
| 684 | snd_hda_codec_write_cache(codec, path->path[i], 0, |
| 685 | AC_VERB_SET_CONNECT_SEL, |
| 686 | path->idx[i]); |
| 687 | if (has_amp_in(codec, path, i)) |
| 688 | activate_amp_in(codec, path, i, enable, add_aamix); |
| 689 | if (has_amp_out(codec, path, i)) |
| 690 | activate_amp_out(codec, path, i, enable); |
| 691 | } |
| 692 | |
| 693 | if (enable) |
| 694 | path->active = true; |
| 695 | } |
| 696 | EXPORT_SYMBOL_HDA(snd_hda_activate_path); |
| 697 | |
Takashi Iwai | d5a9f1b | 2012-12-20 15:36:30 +0100 | [diff] [blame] | 698 | /* turn on/off EAPD on the given pin */ |
| 699 | static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable) |
| 700 | { |
| 701 | struct hda_gen_spec *spec = codec->spec; |
| 702 | if (spec->own_eapd_ctl || |
| 703 | !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD)) |
| 704 | return; |
Takashi Iwai | ecac3ed | 2012-12-21 15:23:01 +0100 | [diff] [blame] | 705 | if (codec->inv_eapd) |
| 706 | enable = !enable; |
Takashi Iwai | d5a9f1b | 2012-12-20 15:36:30 +0100 | [diff] [blame] | 707 | snd_hda_codec_update_cache(codec, pin, 0, |
| 708 | AC_VERB_SET_EAPD_BTLENABLE, |
| 709 | enable ? 0x02 : 0x00); |
| 710 | } |
| 711 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 712 | |
| 713 | /* |
| 714 | * Helper functions for creating mixer ctl elements |
| 715 | */ |
| 716 | |
| 717 | enum { |
| 718 | HDA_CTL_WIDGET_VOL, |
| 719 | HDA_CTL_WIDGET_MUTE, |
| 720 | HDA_CTL_BIND_MUTE, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 721 | }; |
| 722 | static const struct snd_kcontrol_new control_templates[] = { |
| 723 | HDA_CODEC_VOLUME(NULL, 0, 0, 0), |
| 724 | HDA_CODEC_MUTE(NULL, 0, 0, 0), |
| 725 | HDA_BIND_MUTE(NULL, 0, 0, 0), |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 726 | }; |
| 727 | |
| 728 | /* add dynamic controls from template */ |
| 729 | static int add_control(struct hda_gen_spec *spec, int type, const char *name, |
| 730 | int cidx, unsigned long val) |
| 731 | { |
| 732 | struct snd_kcontrol_new *knew; |
| 733 | |
Takashi Iwai | 12c93df | 2012-12-19 14:38:33 +0100 | [diff] [blame] | 734 | knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 735 | if (!knew) |
| 736 | return -ENOMEM; |
| 737 | knew->index = cidx; |
| 738 | if (get_amp_nid_(val)) |
| 739 | knew->subdevice = HDA_SUBDEV_AMP_FLAG; |
| 740 | knew->private_value = val; |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | static int add_control_with_pfx(struct hda_gen_spec *spec, int type, |
| 745 | const char *pfx, const char *dir, |
| 746 | const char *sfx, int cidx, unsigned long val) |
| 747 | { |
| 748 | char name[32]; |
| 749 | snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx); |
| 750 | return add_control(spec, type, name, cidx, val); |
| 751 | } |
| 752 | |
| 753 | #define add_pb_vol_ctrl(spec, type, pfx, val) \ |
| 754 | add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val) |
| 755 | #define add_pb_sw_ctrl(spec, type, pfx, val) \ |
| 756 | add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val) |
| 757 | #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \ |
| 758 | add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val) |
| 759 | #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \ |
| 760 | add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val) |
| 761 | |
| 762 | static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx, |
| 763 | unsigned int chs, struct nid_path *path) |
| 764 | { |
| 765 | unsigned int val; |
| 766 | if (!path) |
| 767 | return 0; |
| 768 | val = path->ctls[NID_PATH_VOL_CTL]; |
| 769 | if (!val) |
| 770 | return 0; |
| 771 | val = amp_val_replace_channels(val, chs); |
| 772 | return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val); |
| 773 | } |
| 774 | |
| 775 | /* return the channel bits suitable for the given path->ctls[] */ |
| 776 | static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path, |
| 777 | int type) |
| 778 | { |
| 779 | int chs = 1; /* mono (left only) */ |
| 780 | if (path) { |
| 781 | hda_nid_t nid = get_amp_nid_(path->ctls[type]); |
| 782 | if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO)) |
| 783 | chs = 3; /* stereo */ |
| 784 | } |
| 785 | return chs; |
| 786 | } |
| 787 | |
| 788 | static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx, |
| 789 | struct nid_path *path) |
| 790 | { |
| 791 | int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL); |
| 792 | return add_vol_ctl(codec, pfx, cidx, chs, path); |
| 793 | } |
| 794 | |
| 795 | /* create a mute-switch for the given mixer widget; |
| 796 | * if it has multiple sources (e.g. DAC and loopback), create a bind-mute |
| 797 | */ |
| 798 | static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx, |
| 799 | unsigned int chs, struct nid_path *path) |
| 800 | { |
| 801 | unsigned int val; |
| 802 | int type = HDA_CTL_WIDGET_MUTE; |
| 803 | |
| 804 | if (!path) |
| 805 | return 0; |
| 806 | val = path->ctls[NID_PATH_MUTE_CTL]; |
| 807 | if (!val) |
| 808 | return 0; |
| 809 | val = amp_val_replace_channels(val, chs); |
| 810 | if (get_amp_direction_(val) == HDA_INPUT) { |
| 811 | hda_nid_t nid = get_amp_nid_(val); |
| 812 | int nums = snd_hda_get_num_conns(codec, nid); |
| 813 | if (nums > 1) { |
| 814 | type = HDA_CTL_BIND_MUTE; |
| 815 | val |= nums << 19; |
| 816 | } |
| 817 | } |
| 818 | return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val); |
| 819 | } |
| 820 | |
| 821 | static int add_stereo_sw(struct hda_codec *codec, const char *pfx, |
| 822 | int cidx, struct nid_path *path) |
| 823 | { |
| 824 | int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL); |
| 825 | return add_sw_ctl(codec, pfx, cidx, chs, path); |
| 826 | } |
| 827 | |
| 828 | static const char * const channel_name[4] = { |
| 829 | "Front", "Surround", "CLFE", "Side" |
| 830 | }; |
| 831 | |
| 832 | /* give some appropriate ctl name prefix for the given line out channel */ |
| 833 | static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch, |
| 834 | bool can_be_master, int *index) |
| 835 | { |
| 836 | struct auto_pin_cfg *cfg = &spec->autocfg; |
| 837 | |
| 838 | *index = 0; |
| 839 | if (cfg->line_outs == 1 && !spec->multi_ios && |
| 840 | !cfg->hp_outs && !cfg->speaker_outs && can_be_master) |
| 841 | return spec->vmaster_mute.hook ? "PCM" : "Master"; |
| 842 | |
| 843 | /* if there is really a single DAC used in the whole output paths, |
| 844 | * use it master (or "PCM" if a vmaster hook is present) |
| 845 | */ |
| 846 | if (spec->multiout.num_dacs == 1 && !spec->mixer_nid && |
| 847 | !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0]) |
| 848 | return spec->vmaster_mute.hook ? "PCM" : "Master"; |
| 849 | |
| 850 | switch (cfg->line_out_type) { |
| 851 | case AUTO_PIN_SPEAKER_OUT: |
| 852 | if (cfg->line_outs == 1) |
| 853 | return "Speaker"; |
| 854 | if (cfg->line_outs == 2) |
| 855 | return ch ? "Bass Speaker" : "Speaker"; |
| 856 | break; |
| 857 | case AUTO_PIN_HP_OUT: |
| 858 | /* for multi-io case, only the primary out */ |
| 859 | if (ch && spec->multi_ios) |
| 860 | break; |
| 861 | *index = ch; |
| 862 | return "Headphone"; |
| 863 | default: |
| 864 | if (cfg->line_outs == 1 && !spec->multi_ios) |
| 865 | return "PCM"; |
| 866 | break; |
| 867 | } |
| 868 | if (ch >= ARRAY_SIZE(channel_name)) { |
| 869 | snd_BUG(); |
| 870 | return "PCM"; |
| 871 | } |
| 872 | |
| 873 | return channel_name[ch]; |
| 874 | } |
| 875 | |
| 876 | /* |
| 877 | * Parse output paths |
| 878 | */ |
| 879 | |
| 880 | /* badness definition */ |
| 881 | enum { |
| 882 | /* No primary DAC is found for the main output */ |
| 883 | BAD_NO_PRIMARY_DAC = 0x10000, |
| 884 | /* No DAC is found for the extra output */ |
| 885 | BAD_NO_DAC = 0x4000, |
| 886 | /* No possible multi-ios */ |
| 887 | BAD_MULTI_IO = 0x103, |
| 888 | /* No individual DAC for extra output */ |
| 889 | BAD_NO_EXTRA_DAC = 0x102, |
| 890 | /* No individual DAC for extra surrounds */ |
| 891 | BAD_NO_EXTRA_SURR_DAC = 0x101, |
| 892 | /* Primary DAC shared with main surrounds */ |
| 893 | BAD_SHARED_SURROUND = 0x100, |
| 894 | /* Primary DAC shared with main CLFE */ |
| 895 | BAD_SHARED_CLFE = 0x10, |
| 896 | /* Primary DAC shared with extra surrounds */ |
| 897 | BAD_SHARED_EXTRA_SURROUND = 0x10, |
| 898 | /* Volume widget is shared */ |
| 899 | BAD_SHARED_VOL = 0x10, |
| 900 | }; |
| 901 | |
Takashi Iwai | 0e614dd | 2013-01-07 15:11:44 +0100 | [diff] [blame] | 902 | /* look for widgets in the given path which are appropriate for |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 903 | * volume and mute controls, and assign the values to ctls[]. |
| 904 | * |
| 905 | * When no appropriate widget is found in the path, the badness value |
| 906 | * is incremented depending on the situation. The function returns the |
| 907 | * total badness for both volume and mute controls. |
| 908 | */ |
Takashi Iwai | 0e614dd | 2013-01-07 15:11:44 +0100 | [diff] [blame] | 909 | 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] | 910 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 911 | hda_nid_t nid; |
| 912 | unsigned int val; |
| 913 | int badness = 0; |
| 914 | |
| 915 | if (!path) |
| 916 | return BAD_SHARED_VOL * 2; |
Takashi Iwai | 0e614dd | 2013-01-07 15:11:44 +0100 | [diff] [blame] | 917 | |
| 918 | if (path->ctls[NID_PATH_VOL_CTL] || |
| 919 | path->ctls[NID_PATH_MUTE_CTL]) |
| 920 | return 0; /* already evaluated */ |
| 921 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 922 | nid = look_for_out_vol_nid(codec, path); |
| 923 | if (nid) { |
| 924 | val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT); |
| 925 | if (is_ctl_used(codec, val, NID_PATH_VOL_CTL)) |
| 926 | badness += BAD_SHARED_VOL; |
| 927 | else |
| 928 | path->ctls[NID_PATH_VOL_CTL] = val; |
| 929 | } else |
| 930 | badness += BAD_SHARED_VOL; |
| 931 | nid = look_for_out_mute_nid(codec, path); |
| 932 | if (nid) { |
| 933 | unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid)); |
| 934 | if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT || |
| 935 | nid_has_mute(codec, nid, HDA_OUTPUT)) |
| 936 | val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT); |
| 937 | else |
| 938 | val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT); |
| 939 | if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL)) |
| 940 | badness += BAD_SHARED_VOL; |
| 941 | else |
| 942 | path->ctls[NID_PATH_MUTE_CTL] = val; |
| 943 | } else |
| 944 | badness += BAD_SHARED_VOL; |
| 945 | return badness; |
| 946 | } |
| 947 | |
| 948 | struct badness_table { |
| 949 | int no_primary_dac; /* no primary DAC */ |
| 950 | int no_dac; /* no secondary DACs */ |
| 951 | int shared_primary; /* primary DAC is shared with main output */ |
| 952 | int shared_surr; /* secondary DAC shared with main or primary */ |
| 953 | int shared_clfe; /* third DAC shared with main or primary */ |
| 954 | int shared_surr_main; /* secondary DAC sahred with main/DAC0 */ |
| 955 | }; |
| 956 | |
| 957 | static struct badness_table main_out_badness = { |
| 958 | .no_primary_dac = BAD_NO_PRIMARY_DAC, |
| 959 | .no_dac = BAD_NO_DAC, |
| 960 | .shared_primary = BAD_NO_PRIMARY_DAC, |
| 961 | .shared_surr = BAD_SHARED_SURROUND, |
| 962 | .shared_clfe = BAD_SHARED_CLFE, |
| 963 | .shared_surr_main = BAD_SHARED_SURROUND, |
| 964 | }; |
| 965 | |
| 966 | static struct badness_table extra_out_badness = { |
| 967 | .no_primary_dac = BAD_NO_DAC, |
| 968 | .no_dac = BAD_NO_DAC, |
| 969 | .shared_primary = BAD_NO_EXTRA_DAC, |
| 970 | .shared_surr = BAD_SHARED_EXTRA_SURROUND, |
| 971 | .shared_clfe = BAD_SHARED_EXTRA_SURROUND, |
| 972 | .shared_surr_main = BAD_NO_EXTRA_SURR_DAC, |
| 973 | }; |
| 974 | |
Takashi Iwai | 7385df6 | 2013-01-07 09:50:52 +0100 | [diff] [blame] | 975 | /* get the DAC of the primary output corresponding to the given array index */ |
| 976 | static hda_nid_t get_primary_out(struct hda_codec *codec, int idx) |
| 977 | { |
| 978 | struct hda_gen_spec *spec = codec->spec; |
| 979 | struct auto_pin_cfg *cfg = &spec->autocfg; |
| 980 | |
| 981 | if (cfg->line_outs > idx) |
| 982 | return spec->private_dac_nids[idx]; |
| 983 | idx -= cfg->line_outs; |
| 984 | if (spec->multi_ios > idx) |
| 985 | return spec->multi_io[idx].dac; |
| 986 | return 0; |
| 987 | } |
| 988 | |
| 989 | /* return the DAC if it's reachable, otherwise zero */ |
| 990 | static inline hda_nid_t try_dac(struct hda_codec *codec, |
| 991 | hda_nid_t dac, hda_nid_t pin) |
| 992 | { |
| 993 | return is_reachable_path(codec, dac, pin) ? dac : 0; |
| 994 | } |
| 995 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 996 | /* try to assign DACs to pins and return the resultant badness */ |
| 997 | static int try_assign_dacs(struct hda_codec *codec, int num_outs, |
| 998 | const hda_nid_t *pins, hda_nid_t *dacs, |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 999 | int *path_idx, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1000 | const struct badness_table *bad) |
| 1001 | { |
| 1002 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1003 | int i, j; |
| 1004 | int badness = 0; |
| 1005 | hda_nid_t dac; |
| 1006 | |
| 1007 | if (!num_outs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | return 0; |
| 1009 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1010 | for (i = 0; i < num_outs; i++) { |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 1011 | struct nid_path *path; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1012 | hda_nid_t pin = pins[i]; |
Takashi Iwai | 1e0b528 | 2013-01-04 12:56:52 +0100 | [diff] [blame] | 1013 | |
Takashi Iwai | 0e614dd | 2013-01-07 15:11:44 +0100 | [diff] [blame] | 1014 | path = snd_hda_get_path_from_idx(codec, path_idx[i]); |
| 1015 | if (path) { |
| 1016 | badness += assign_out_path_ctls(codec, path); |
Takashi Iwai | 1e0b528 | 2013-01-04 12:56:52 +0100 | [diff] [blame] | 1017 | continue; |
| 1018 | } |
| 1019 | |
| 1020 | dacs[i] = look_for_dac(codec, pin, false); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1021 | if (!dacs[i] && !i) { |
Takashi Iwai | 980428c | 2013-01-09 09:28:20 +0100 | [diff] [blame] | 1022 | /* try to steal the DAC of surrounds for the front */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1023 | for (j = 1; j < num_outs; j++) { |
| 1024 | if (is_reachable_path(codec, dacs[j], pin)) { |
| 1025 | dacs[0] = dacs[j]; |
| 1026 | dacs[j] = 0; |
Takashi Iwai | 980428c | 2013-01-09 09:28:20 +0100 | [diff] [blame] | 1027 | invalidate_nid_path(codec, path_idx[j]); |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1028 | path_idx[j] = 0; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1029 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | } |
| 1031 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1032 | } |
| 1033 | dac = dacs[i]; |
| 1034 | if (!dac) { |
Takashi Iwai | 7385df6 | 2013-01-07 09:50:52 +0100 | [diff] [blame] | 1035 | if (num_outs > 2) |
| 1036 | dac = try_dac(codec, get_primary_out(codec, i), pin); |
| 1037 | if (!dac) |
| 1038 | dac = try_dac(codec, dacs[0], pin); |
| 1039 | if (!dac) |
| 1040 | dac = try_dac(codec, get_primary_out(codec, i), pin); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1041 | if (dac) { |
| 1042 | if (!i) |
| 1043 | badness += bad->shared_primary; |
| 1044 | else if (i == 1) |
| 1045 | badness += bad->shared_surr; |
| 1046 | else |
| 1047 | badness += bad->shared_clfe; |
| 1048 | } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) { |
| 1049 | dac = spec->private_dac_nids[0]; |
| 1050 | badness += bad->shared_surr_main; |
| 1051 | } else if (!i) |
| 1052 | badness += bad->no_primary_dac; |
| 1053 | else |
| 1054 | badness += bad->no_dac; |
| 1055 | } |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 1056 | path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid); |
Takashi Iwai | 117688a | 2013-01-04 15:41:41 +0100 | [diff] [blame] | 1057 | if (!path && !i && spec->mixer_nid) { |
Takashi Iwai | b3a8c74 | 2012-12-20 18:29:16 +0100 | [diff] [blame] | 1058 | /* try with aamix */ |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 1059 | path = snd_hda_add_new_path(codec, dac, pin, 0); |
Takashi Iwai | b3a8c74 | 2012-12-20 18:29:16 +0100 | [diff] [blame] | 1060 | } |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 1061 | if (!path) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1062 | dac = dacs[i] = 0; |
Takashi Iwai | e1284af | 2013-01-03 16:33:02 +0100 | [diff] [blame] | 1063 | else { |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 1064 | print_nid_path("output", path); |
Takashi Iwai | e1284af | 2013-01-03 16:33:02 +0100 | [diff] [blame] | 1065 | path->active = true; |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1066 | path_idx[i] = snd_hda_get_path_idx(codec, path); |
Takashi Iwai | 0e614dd | 2013-01-07 15:11:44 +0100 | [diff] [blame] | 1067 | badness += assign_out_path_ctls(codec, path); |
Takashi Iwai | e1284af | 2013-01-03 16:33:02 +0100 | [diff] [blame] | 1068 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | return badness; |
| 1072 | } |
| 1073 | |
| 1074 | /* return NID if the given pin has only a single connection to a certain DAC */ |
| 1075 | static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin) |
| 1076 | { |
| 1077 | struct hda_gen_spec *spec = codec->spec; |
| 1078 | int i; |
| 1079 | hda_nid_t nid_found = 0; |
| 1080 | |
| 1081 | for (i = 0; i < spec->num_all_dacs; i++) { |
| 1082 | hda_nid_t nid = spec->all_dacs[i]; |
| 1083 | if (!nid || is_dac_already_used(codec, nid)) |
| 1084 | continue; |
| 1085 | if (is_reachable_path(codec, nid, pin)) { |
| 1086 | if (nid_found) |
| 1087 | return 0; |
| 1088 | nid_found = nid; |
| 1089 | } |
| 1090 | } |
| 1091 | return nid_found; |
| 1092 | } |
| 1093 | |
| 1094 | /* check whether the given pin can be a multi-io pin */ |
| 1095 | static bool can_be_multiio_pin(struct hda_codec *codec, |
| 1096 | unsigned int location, hda_nid_t nid) |
| 1097 | { |
| 1098 | unsigned int defcfg, caps; |
| 1099 | |
| 1100 | defcfg = snd_hda_codec_get_pincfg(codec, nid); |
| 1101 | if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX) |
| 1102 | return false; |
| 1103 | if (location && get_defcfg_location(defcfg) != location) |
| 1104 | return false; |
| 1105 | caps = snd_hda_query_pin_caps(codec, nid); |
| 1106 | if (!(caps & AC_PINCAP_OUT)) |
| 1107 | return false; |
| 1108 | return true; |
| 1109 | } |
| 1110 | |
Takashi Iwai | e22aab7 | 2013-01-04 14:50:04 +0100 | [diff] [blame] | 1111 | /* count the number of input pins that are capable to be multi-io */ |
| 1112 | static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin) |
| 1113 | { |
| 1114 | struct hda_gen_spec *spec = codec->spec; |
| 1115 | struct auto_pin_cfg *cfg = &spec->autocfg; |
| 1116 | unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin); |
| 1117 | unsigned int location = get_defcfg_location(defcfg); |
| 1118 | int type, i; |
| 1119 | int num_pins = 0; |
| 1120 | |
| 1121 | for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) { |
| 1122 | for (i = 0; i < cfg->num_inputs; i++) { |
| 1123 | if (cfg->inputs[i].type != type) |
| 1124 | continue; |
| 1125 | if (can_be_multiio_pin(codec, location, |
| 1126 | cfg->inputs[i].pin)) |
| 1127 | num_pins++; |
| 1128 | } |
| 1129 | } |
| 1130 | return num_pins; |
| 1131 | } |
| 1132 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1133 | /* |
| 1134 | * multi-io helper |
| 1135 | * |
| 1136 | * When hardwired is set, try to fill ony hardwired pins, and returns |
| 1137 | * zero if any pins are filled, non-zero if nothing found. |
| 1138 | * When hardwired is off, try to fill possible input pins, and returns |
| 1139 | * the badness value. |
| 1140 | */ |
| 1141 | static int fill_multi_ios(struct hda_codec *codec, |
| 1142 | hda_nid_t reference_pin, |
Takashi Iwai | e22aab7 | 2013-01-04 14:50:04 +0100 | [diff] [blame] | 1143 | bool hardwired) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1144 | { |
| 1145 | struct hda_gen_spec *spec = codec->spec; |
| 1146 | struct auto_pin_cfg *cfg = &spec->autocfg; |
Takashi Iwai | e22aab7 | 2013-01-04 14:50:04 +0100 | [diff] [blame] | 1147 | int type, i, j, num_pins, old_pins; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1148 | unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin); |
| 1149 | unsigned int location = get_defcfg_location(defcfg); |
| 1150 | int badness = 0; |
Takashi Iwai | 0e614dd | 2013-01-07 15:11:44 +0100 | [diff] [blame] | 1151 | struct nid_path *path; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1152 | |
| 1153 | old_pins = spec->multi_ios; |
| 1154 | if (old_pins >= 2) |
| 1155 | goto end_fill; |
| 1156 | |
Takashi Iwai | e22aab7 | 2013-01-04 14:50:04 +0100 | [diff] [blame] | 1157 | num_pins = count_multiio_pins(codec, reference_pin); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1158 | if (num_pins < 2) |
| 1159 | goto end_fill; |
| 1160 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1161 | for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) { |
| 1162 | for (i = 0; i < cfg->num_inputs; i++) { |
| 1163 | hda_nid_t nid = cfg->inputs[i].pin; |
| 1164 | hda_nid_t dac = 0; |
| 1165 | |
| 1166 | if (cfg->inputs[i].type != type) |
| 1167 | continue; |
| 1168 | if (!can_be_multiio_pin(codec, location, nid)) |
| 1169 | continue; |
| 1170 | for (j = 0; j < spec->multi_ios; j++) { |
| 1171 | if (nid == spec->multi_io[j].pin) |
| 1172 | break; |
| 1173 | } |
| 1174 | if (j < spec->multi_ios) |
| 1175 | continue; |
| 1176 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1177 | if (hardwired) |
| 1178 | dac = get_dac_if_single(codec, nid); |
| 1179 | else if (!dac) |
| 1180 | dac = look_for_dac(codec, nid, false); |
| 1181 | if (!dac) { |
| 1182 | badness++; |
| 1183 | continue; |
| 1184 | } |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 1185 | path = snd_hda_add_new_path(codec, dac, nid, |
| 1186 | -spec->mixer_nid); |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 1187 | if (!path) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1188 | badness++; |
| 1189 | continue; |
| 1190 | } |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 1191 | print_nid_path("multiio", path); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1192 | spec->multi_io[spec->multi_ios].pin = nid; |
| 1193 | spec->multi_io[spec->multi_ios].dac = dac; |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1194 | spec->out_paths[cfg->line_outs + spec->multi_ios] = |
| 1195 | snd_hda_get_path_idx(codec, path); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1196 | spec->multi_ios++; |
| 1197 | if (spec->multi_ios >= 2) |
| 1198 | break; |
| 1199 | } |
| 1200 | } |
| 1201 | end_fill: |
| 1202 | if (badness) |
| 1203 | badness = BAD_MULTI_IO; |
| 1204 | if (old_pins == spec->multi_ios) { |
| 1205 | if (hardwired) |
| 1206 | return 1; /* nothing found */ |
| 1207 | else |
| 1208 | return badness; /* no badness if nothing found */ |
| 1209 | } |
| 1210 | if (!hardwired && spec->multi_ios < 2) { |
| 1211 | /* cancel newly assigned paths */ |
| 1212 | spec->paths.used -= spec->multi_ios - old_pins; |
| 1213 | spec->multi_ios = old_pins; |
| 1214 | return badness; |
| 1215 | } |
| 1216 | |
| 1217 | /* assign volume and mute controls */ |
Takashi Iwai | 0e614dd | 2013-01-07 15:11:44 +0100 | [diff] [blame] | 1218 | for (i = old_pins; i < spec->multi_ios; i++) { |
| 1219 | path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]); |
| 1220 | badness += assign_out_path_ctls(codec, path); |
| 1221 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1222 | |
| 1223 | return badness; |
| 1224 | } |
| 1225 | |
| 1226 | /* map DACs for all pins in the list if they are single connections */ |
| 1227 | static bool map_singles(struct hda_codec *codec, int outs, |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1228 | const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1229 | { |
Takashi Iwai | b3a8c74 | 2012-12-20 18:29:16 +0100 | [diff] [blame] | 1230 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1231 | int i; |
| 1232 | bool found = false; |
| 1233 | for (i = 0; i < outs; i++) { |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 1234 | struct nid_path *path; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1235 | hda_nid_t dac; |
| 1236 | if (dacs[i]) |
| 1237 | continue; |
| 1238 | dac = get_dac_if_single(codec, pins[i]); |
| 1239 | if (!dac) |
| 1240 | continue; |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 1241 | path = snd_hda_add_new_path(codec, dac, pins[i], |
| 1242 | -spec->mixer_nid); |
Takashi Iwai | 117688a | 2013-01-04 15:41:41 +0100 | [diff] [blame] | 1243 | if (!path && !i && spec->mixer_nid) |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 1244 | path = snd_hda_add_new_path(codec, dac, pins[i], 0); |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 1245 | if (path) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1246 | dacs[i] = dac; |
| 1247 | found = true; |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 1248 | print_nid_path("output", path); |
Takashi Iwai | e1284af | 2013-01-03 16:33:02 +0100 | [diff] [blame] | 1249 | path->active = true; |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1250 | path_idx[i] = snd_hda_get_path_idx(codec, path); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1251 | } |
| 1252 | } |
| 1253 | return found; |
| 1254 | } |
| 1255 | |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 1256 | /* create a new path including aamix if available, and return its index */ |
| 1257 | static int check_aamix_out_path(struct hda_codec *codec, int path_idx) |
| 1258 | { |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 1259 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 1260 | struct nid_path *path; |
| 1261 | |
| 1262 | path = snd_hda_get_path_from_idx(codec, path_idx); |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 1263 | if (!path || !path->depth || |
| 1264 | is_nid_contained(path, spec->mixer_nid)) |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 1265 | return 0; |
| 1266 | path = snd_hda_add_new_path(codec, path->path[0], |
| 1267 | path->path[path->depth - 1], |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 1268 | spec->mixer_nid); |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 1269 | if (!path) |
| 1270 | return 0; |
| 1271 | print_nid_path("output-aamix", path); |
| 1272 | path->active = false; /* unused as default */ |
| 1273 | return snd_hda_get_path_idx(codec, path); |
| 1274 | } |
| 1275 | |
Takashi Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 1276 | /* fill the empty entries in the dac array for speaker/hp with the |
| 1277 | * shared dac pointed by the paths |
| 1278 | */ |
| 1279 | static void refill_shared_dacs(struct hda_codec *codec, int num_outs, |
| 1280 | hda_nid_t *dacs, int *path_idx) |
| 1281 | { |
| 1282 | struct nid_path *path; |
| 1283 | int i; |
| 1284 | |
| 1285 | for (i = 0; i < num_outs; i++) { |
| 1286 | if (dacs[i]) |
| 1287 | continue; |
| 1288 | path = snd_hda_get_path_from_idx(codec, path_idx[i]); |
| 1289 | if (!path) |
| 1290 | continue; |
| 1291 | dacs[i] = path->path[0]; |
| 1292 | } |
| 1293 | } |
| 1294 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1295 | /* fill in the dac_nids table from the parsed pin configuration */ |
| 1296 | static int fill_and_eval_dacs(struct hda_codec *codec, |
| 1297 | bool fill_hardwired, |
| 1298 | bool fill_mio_first) |
| 1299 | { |
| 1300 | struct hda_gen_spec *spec = codec->spec; |
| 1301 | struct auto_pin_cfg *cfg = &spec->autocfg; |
| 1302 | int i, err, badness; |
Takashi Iwai | ea46c3c | 2013-01-15 18:45:53 +0100 | [diff] [blame] | 1303 | unsigned int val; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1304 | |
| 1305 | /* set num_dacs once to full for look_for_dac() */ |
| 1306 | spec->multiout.num_dacs = cfg->line_outs; |
| 1307 | spec->multiout.dac_nids = spec->private_dac_nids; |
| 1308 | memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids)); |
| 1309 | memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid)); |
| 1310 | memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid)); |
| 1311 | spec->multi_ios = 0; |
| 1312 | snd_array_free(&spec->paths); |
Takashi Iwai | cd5be3f | 2013-01-07 15:07:00 +0100 | [diff] [blame] | 1313 | |
| 1314 | /* clear path indices */ |
| 1315 | memset(spec->out_paths, 0, sizeof(spec->out_paths)); |
| 1316 | memset(spec->hp_paths, 0, sizeof(spec->hp_paths)); |
| 1317 | memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths)); |
| 1318 | memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths)); |
| 1319 | memset(spec->digout_paths, 0, sizeof(spec->digout_paths)); |
Takashi Iwai | c697b71 | 2013-01-07 17:09:26 +0100 | [diff] [blame] | 1320 | memset(spec->input_paths, 0, sizeof(spec->input_paths)); |
Takashi Iwai | cd5be3f | 2013-01-07 15:07:00 +0100 | [diff] [blame] | 1321 | memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths)); |
| 1322 | memset(&spec->digin_path, 0, sizeof(spec->digin_path)); |
| 1323 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1324 | badness = 0; |
| 1325 | |
| 1326 | /* fill hard-wired DACs first */ |
| 1327 | if (fill_hardwired) { |
| 1328 | bool mapped; |
| 1329 | do { |
| 1330 | mapped = map_singles(codec, cfg->line_outs, |
| 1331 | cfg->line_out_pins, |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1332 | spec->private_dac_nids, |
| 1333 | spec->out_paths); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1334 | mapped |= map_singles(codec, cfg->hp_outs, |
| 1335 | cfg->hp_pins, |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1336 | spec->multiout.hp_out_nid, |
| 1337 | spec->hp_paths); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1338 | mapped |= map_singles(codec, cfg->speaker_outs, |
| 1339 | cfg->speaker_pins, |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1340 | spec->multiout.extra_out_nid, |
| 1341 | spec->speaker_paths); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1342 | if (fill_mio_first && cfg->line_outs == 1 && |
| 1343 | cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) { |
Takashi Iwai | e22aab7 | 2013-01-04 14:50:04 +0100 | [diff] [blame] | 1344 | err = fill_multi_ios(codec, cfg->line_out_pins[0], true); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1345 | if (!err) |
| 1346 | mapped = true; |
| 1347 | } |
| 1348 | } while (mapped); |
| 1349 | } |
| 1350 | |
| 1351 | badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins, |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1352 | spec->private_dac_nids, spec->out_paths, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1353 | &main_out_badness); |
| 1354 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1355 | if (fill_mio_first && |
| 1356 | cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) { |
| 1357 | /* try to fill multi-io first */ |
Takashi Iwai | e22aab7 | 2013-01-04 14:50:04 +0100 | [diff] [blame] | 1358 | err = fill_multi_ios(codec, cfg->line_out_pins[0], false); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1359 | if (err < 0) |
| 1360 | return err; |
| 1361 | /* we don't count badness at this stage yet */ |
| 1362 | } |
| 1363 | |
| 1364 | if (cfg->line_out_type != AUTO_PIN_HP_OUT) { |
| 1365 | err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins, |
| 1366 | spec->multiout.hp_out_nid, |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1367 | spec->hp_paths, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1368 | &extra_out_badness); |
| 1369 | if (err < 0) |
| 1370 | return err; |
| 1371 | badness += err; |
| 1372 | } |
| 1373 | if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) { |
| 1374 | err = try_assign_dacs(codec, cfg->speaker_outs, |
| 1375 | cfg->speaker_pins, |
| 1376 | spec->multiout.extra_out_nid, |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1377 | spec->speaker_paths, |
| 1378 | &extra_out_badness); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1379 | if (err < 0) |
| 1380 | return err; |
| 1381 | badness += err; |
| 1382 | } |
| 1383 | if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) { |
Takashi Iwai | e22aab7 | 2013-01-04 14:50:04 +0100 | [diff] [blame] | 1384 | err = fill_multi_ios(codec, cfg->line_out_pins[0], false); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1385 | if (err < 0) |
| 1386 | return err; |
| 1387 | badness += err; |
| 1388 | } |
Takashi Iwai | e22aab7 | 2013-01-04 14:50:04 +0100 | [diff] [blame] | 1389 | |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 1390 | if (spec->mixer_nid) { |
| 1391 | spec->aamix_out_paths[0] = |
| 1392 | check_aamix_out_path(codec, spec->out_paths[0]); |
| 1393 | if (cfg->line_out_type != AUTO_PIN_HP_OUT) |
| 1394 | spec->aamix_out_paths[1] = |
| 1395 | check_aamix_out_path(codec, spec->hp_paths[0]); |
| 1396 | if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) |
| 1397 | spec->aamix_out_paths[2] = |
| 1398 | check_aamix_out_path(codec, spec->speaker_paths[0]); |
| 1399 | } |
| 1400 | |
Takashi Iwai | e22aab7 | 2013-01-04 14:50:04 +0100 | [diff] [blame] | 1401 | if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) |
| 1402 | if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2) |
| 1403 | spec->multi_ios = 1; /* give badness */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1404 | |
Takashi Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 1405 | /* re-count num_dacs and squash invalid entries */ |
| 1406 | spec->multiout.num_dacs = 0; |
| 1407 | for (i = 0; i < cfg->line_outs; i++) { |
| 1408 | if (spec->private_dac_nids[i]) |
| 1409 | spec->multiout.num_dacs++; |
| 1410 | else { |
| 1411 | memmove(spec->private_dac_nids + i, |
| 1412 | spec->private_dac_nids + i + 1, |
| 1413 | sizeof(hda_nid_t) * (cfg->line_outs - i - 1)); |
| 1414 | spec->private_dac_nids[cfg->line_outs - 1] = 0; |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | spec->ext_channel_count = spec->min_channel_count = |
David Henningsson | c0f3b21 | 2013-01-16 11:45:37 +0100 | [diff] [blame] | 1419 | spec->multiout.num_dacs * 2; |
Takashi Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 1420 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1421 | if (spec->multi_ios == 2) { |
| 1422 | for (i = 0; i < 2; i++) |
| 1423 | spec->private_dac_nids[spec->multiout.num_dacs++] = |
| 1424 | spec->multi_io[i].dac; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1425 | } else if (spec->multi_ios) { |
| 1426 | spec->multi_ios = 0; |
| 1427 | badness += BAD_MULTI_IO; |
| 1428 | } |
| 1429 | |
Takashi Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 1430 | /* re-fill the shared DAC for speaker / headphone */ |
| 1431 | if (cfg->line_out_type != AUTO_PIN_HP_OUT) |
| 1432 | refill_shared_dacs(codec, cfg->hp_outs, |
| 1433 | spec->multiout.hp_out_nid, |
| 1434 | spec->hp_paths); |
| 1435 | if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) |
| 1436 | refill_shared_dacs(codec, cfg->speaker_outs, |
| 1437 | spec->multiout.extra_out_nid, |
| 1438 | spec->speaker_paths); |
| 1439 | |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 1440 | /* set initial pinctl targets */ |
Takashi Iwai | ea46c3c | 2013-01-15 18:45:53 +0100 | [diff] [blame] | 1441 | if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT) |
| 1442 | val = PIN_HP; |
| 1443 | else |
| 1444 | val = PIN_OUT; |
| 1445 | set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val); |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 1446 | if (cfg->line_out_type != AUTO_PIN_HP_OUT) |
| 1447 | set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP); |
Takashi Iwai | ea46c3c | 2013-01-15 18:45:53 +0100 | [diff] [blame] | 1448 | if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) { |
| 1449 | val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT; |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 1450 | set_pin_targets(codec, cfg->speaker_outs, |
Takashi Iwai | ea46c3c | 2013-01-15 18:45:53 +0100 | [diff] [blame] | 1451 | cfg->speaker_pins, val); |
| 1452 | } |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 1453 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1454 | return badness; |
| 1455 | } |
| 1456 | |
| 1457 | #define DEBUG_BADNESS |
| 1458 | |
| 1459 | #ifdef DEBUG_BADNESS |
| 1460 | #define debug_badness snd_printdd |
| 1461 | #else |
| 1462 | #define debug_badness(...) |
| 1463 | #endif |
| 1464 | |
| 1465 | static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg) |
| 1466 | { |
| 1467 | debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n", |
| 1468 | cfg->line_out_pins[0], cfg->line_out_pins[1], |
Takashi Iwai | 708122e | 2012-12-20 17:56:57 +0100 | [diff] [blame] | 1469 | cfg->line_out_pins[2], cfg->line_out_pins[3], |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1470 | spec->multiout.dac_nids[0], |
| 1471 | spec->multiout.dac_nids[1], |
| 1472 | spec->multiout.dac_nids[2], |
| 1473 | spec->multiout.dac_nids[3]); |
| 1474 | if (spec->multi_ios > 0) |
| 1475 | debug_badness("multi_ios(%d) = %x/%x : %x/%x\n", |
| 1476 | spec->multi_ios, |
| 1477 | spec->multi_io[0].pin, spec->multi_io[1].pin, |
| 1478 | spec->multi_io[0].dac, spec->multi_io[1].dac); |
| 1479 | debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n", |
| 1480 | cfg->hp_pins[0], cfg->hp_pins[1], |
Takashi Iwai | 708122e | 2012-12-20 17:56:57 +0100 | [diff] [blame] | 1481 | cfg->hp_pins[2], cfg->hp_pins[3], |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1482 | spec->multiout.hp_out_nid[0], |
| 1483 | spec->multiout.hp_out_nid[1], |
| 1484 | spec->multiout.hp_out_nid[2], |
| 1485 | spec->multiout.hp_out_nid[3]); |
| 1486 | debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n", |
| 1487 | cfg->speaker_pins[0], cfg->speaker_pins[1], |
| 1488 | cfg->speaker_pins[2], cfg->speaker_pins[3], |
| 1489 | spec->multiout.extra_out_nid[0], |
| 1490 | spec->multiout.extra_out_nid[1], |
| 1491 | spec->multiout.extra_out_nid[2], |
| 1492 | spec->multiout.extra_out_nid[3]); |
| 1493 | } |
| 1494 | |
| 1495 | /* find all available DACs of the codec */ |
| 1496 | static void fill_all_dac_nids(struct hda_codec *codec) |
| 1497 | { |
| 1498 | struct hda_gen_spec *spec = codec->spec; |
| 1499 | int i; |
| 1500 | hda_nid_t nid = codec->start_nid; |
| 1501 | |
| 1502 | spec->num_all_dacs = 0; |
| 1503 | memset(spec->all_dacs, 0, sizeof(spec->all_dacs)); |
| 1504 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 1505 | if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT) |
| 1506 | continue; |
| 1507 | if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) { |
| 1508 | snd_printk(KERN_ERR "hda: Too many DACs!\n"); |
| 1509 | break; |
| 1510 | } |
| 1511 | spec->all_dacs[spec->num_all_dacs++] = nid; |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | static int parse_output_paths(struct hda_codec *codec) |
| 1516 | { |
| 1517 | struct hda_gen_spec *spec = codec->spec; |
| 1518 | struct auto_pin_cfg *cfg = &spec->autocfg; |
| 1519 | struct auto_pin_cfg *best_cfg; |
| 1520 | int best_badness = INT_MAX; |
| 1521 | int badness; |
| 1522 | bool fill_hardwired = true, fill_mio_first = true; |
| 1523 | bool best_wired = true, best_mio = true; |
| 1524 | bool hp_spk_swapped = false; |
| 1525 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1526 | best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL); |
| 1527 | if (!best_cfg) |
| 1528 | return -ENOMEM; |
| 1529 | *best_cfg = *cfg; |
| 1530 | |
| 1531 | for (;;) { |
| 1532 | badness = fill_and_eval_dacs(codec, fill_hardwired, |
| 1533 | fill_mio_first); |
| 1534 | if (badness < 0) { |
| 1535 | kfree(best_cfg); |
| 1536 | return badness; |
| 1537 | } |
| 1538 | debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n", |
| 1539 | cfg->line_out_type, fill_hardwired, fill_mio_first, |
| 1540 | badness); |
| 1541 | debug_show_configs(spec, cfg); |
| 1542 | if (badness < best_badness) { |
| 1543 | best_badness = badness; |
| 1544 | *best_cfg = *cfg; |
| 1545 | best_wired = fill_hardwired; |
| 1546 | best_mio = fill_mio_first; |
| 1547 | } |
| 1548 | if (!badness) |
| 1549 | break; |
| 1550 | fill_mio_first = !fill_mio_first; |
| 1551 | if (!fill_mio_first) |
| 1552 | continue; |
| 1553 | fill_hardwired = !fill_hardwired; |
| 1554 | if (!fill_hardwired) |
| 1555 | continue; |
| 1556 | if (hp_spk_swapped) |
| 1557 | break; |
| 1558 | hp_spk_swapped = true; |
| 1559 | if (cfg->speaker_outs > 0 && |
| 1560 | cfg->line_out_type == AUTO_PIN_HP_OUT) { |
| 1561 | cfg->hp_outs = cfg->line_outs; |
| 1562 | memcpy(cfg->hp_pins, cfg->line_out_pins, |
| 1563 | sizeof(cfg->hp_pins)); |
| 1564 | cfg->line_outs = cfg->speaker_outs; |
| 1565 | memcpy(cfg->line_out_pins, cfg->speaker_pins, |
| 1566 | sizeof(cfg->speaker_pins)); |
| 1567 | cfg->speaker_outs = 0; |
| 1568 | memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins)); |
| 1569 | cfg->line_out_type = AUTO_PIN_SPEAKER_OUT; |
| 1570 | fill_hardwired = true; |
| 1571 | continue; |
| 1572 | } |
| 1573 | if (cfg->hp_outs > 0 && |
| 1574 | cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) { |
| 1575 | cfg->speaker_outs = cfg->line_outs; |
| 1576 | memcpy(cfg->speaker_pins, cfg->line_out_pins, |
| 1577 | sizeof(cfg->speaker_pins)); |
| 1578 | cfg->line_outs = cfg->hp_outs; |
| 1579 | memcpy(cfg->line_out_pins, cfg->hp_pins, |
| 1580 | sizeof(cfg->hp_pins)); |
| 1581 | cfg->hp_outs = 0; |
| 1582 | memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins)); |
| 1583 | cfg->line_out_type = AUTO_PIN_HP_OUT; |
| 1584 | fill_hardwired = true; |
| 1585 | continue; |
| 1586 | } |
| 1587 | break; |
| 1588 | } |
| 1589 | |
| 1590 | if (badness) { |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 1591 | debug_badness("==> restoring best_cfg\n"); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1592 | *cfg = *best_cfg; |
| 1593 | fill_and_eval_dacs(codec, best_wired, best_mio); |
| 1594 | } |
| 1595 | debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n", |
| 1596 | cfg->line_out_type, best_wired, best_mio); |
| 1597 | debug_show_configs(spec, cfg); |
| 1598 | |
| 1599 | if (cfg->line_out_pins[0]) { |
| 1600 | struct nid_path *path; |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1601 | path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1602 | if (path) |
| 1603 | spec->vmaster_nid = look_for_out_vol_nid(codec, path); |
Takashi Iwai | 7a71bbf | 2013-01-17 10:25:15 +0100 | [diff] [blame^] | 1604 | if (spec->vmaster_nid) |
| 1605 | snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid, |
| 1606 | HDA_OUTPUT, spec->vmaster_tlv); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | kfree(best_cfg); |
| 1610 | return 0; |
| 1611 | } |
| 1612 | |
| 1613 | /* add playback controls from the parsed DAC table */ |
| 1614 | static int create_multi_out_ctls(struct hda_codec *codec, |
| 1615 | const struct auto_pin_cfg *cfg) |
| 1616 | { |
| 1617 | struct hda_gen_spec *spec = codec->spec; |
| 1618 | int i, err, noutputs; |
| 1619 | |
| 1620 | noutputs = cfg->line_outs; |
| 1621 | if (spec->multi_ios > 0 && cfg->line_outs < 3) |
| 1622 | noutputs += spec->multi_ios; |
| 1623 | |
| 1624 | for (i = 0; i < noutputs; i++) { |
| 1625 | const char *name; |
| 1626 | int index; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1627 | struct nid_path *path; |
| 1628 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1629 | if (i >= cfg->line_outs) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1630 | index = 0; |
| 1631 | name = channel_name[i]; |
| 1632 | } else { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1633 | name = get_line_out_pfx(spec, i, true, &index); |
| 1634 | } |
| 1635 | |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1636 | path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1637 | if (!path) |
| 1638 | continue; |
| 1639 | if (!name || !strcmp(name, "CLFE")) { |
| 1640 | /* Center/LFE */ |
| 1641 | err = add_vol_ctl(codec, "Center", 0, 1, path); |
| 1642 | if (err < 0) |
| 1643 | return err; |
| 1644 | err = add_vol_ctl(codec, "LFE", 0, 2, path); |
| 1645 | if (err < 0) |
| 1646 | return err; |
| 1647 | err = add_sw_ctl(codec, "Center", 0, 1, path); |
| 1648 | if (err < 0) |
| 1649 | return err; |
| 1650 | err = add_sw_ctl(codec, "LFE", 0, 2, path); |
| 1651 | if (err < 0) |
| 1652 | return err; |
| 1653 | } else { |
| 1654 | err = add_stereo_vol(codec, name, index, path); |
| 1655 | if (err < 0) |
| 1656 | return err; |
| 1657 | err = add_stereo_sw(codec, name, index, path); |
| 1658 | if (err < 0) |
| 1659 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | } |
| 1661 | } |
| 1662 | return 0; |
| 1663 | } |
| 1664 | |
Takashi Iwai | c2c8038 | 2013-01-07 10:33:57 +0100 | [diff] [blame] | 1665 | static int create_extra_out(struct hda_codec *codec, int path_idx, |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1666 | const char *pfx, int cidx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1668 | struct nid_path *path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | int err; |
| 1670 | |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1671 | path = snd_hda_get_path_from_idx(codec, path_idx); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1672 | if (!path) |
| 1673 | return 0; |
Takashi Iwai | c2c8038 | 2013-01-07 10:33:57 +0100 | [diff] [blame] | 1674 | err = add_stereo_vol(codec, pfx, cidx, path); |
| 1675 | if (err < 0) |
| 1676 | return err; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1677 | err = add_stereo_sw(codec, pfx, cidx, path); |
| 1678 | if (err < 0) |
| 1679 | return err; |
| 1680 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1683 | /* add playback controls for speaker and HP outputs */ |
| 1684 | static int create_extra_outs(struct hda_codec *codec, int num_pins, |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1685 | const int *paths, const char *pfx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | { |
Takashi Iwai | c2c8038 | 2013-01-07 10:33:57 +0100 | [diff] [blame] | 1687 | int i; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1688 | |
| 1689 | for (i = 0; i < num_pins; i++) { |
Takashi Iwai | c2c8038 | 2013-01-07 10:33:57 +0100 | [diff] [blame] | 1690 | const char *name; |
| 1691 | char tmp[44]; |
| 1692 | int err, idx = 0; |
| 1693 | |
| 1694 | if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) |
| 1695 | name = "Bass Speaker"; |
| 1696 | else if (num_pins >= 3) { |
| 1697 | snprintf(tmp, sizeof(tmp), "%s %s", |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1698 | pfx, channel_name[i]); |
Takashi Iwai | c2c8038 | 2013-01-07 10:33:57 +0100 | [diff] [blame] | 1699 | name = tmp; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1700 | } else { |
Takashi Iwai | c2c8038 | 2013-01-07 10:33:57 +0100 | [diff] [blame] | 1701 | name = pfx; |
| 1702 | idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | } |
Takashi Iwai | c2c8038 | 2013-01-07 10:33:57 +0100 | [diff] [blame] | 1704 | err = create_extra_out(codec, paths[i], name, idx); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1705 | if (err < 0) |
| 1706 | return err; |
| 1707 | } |
| 1708 | return 0; |
| 1709 | } |
Takashi Iwai | 97ec558 | 2006-03-21 11:29:07 +0100 | [diff] [blame] | 1710 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1711 | static int create_hp_out_ctls(struct hda_codec *codec) |
| 1712 | { |
| 1713 | struct hda_gen_spec *spec = codec->spec; |
| 1714 | return create_extra_outs(codec, spec->autocfg.hp_outs, |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1715 | spec->hp_paths, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1716 | "Headphone"); |
| 1717 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1719 | static int create_speaker_out_ctls(struct hda_codec *codec) |
| 1720 | { |
| 1721 | struct hda_gen_spec *spec = codec->spec; |
| 1722 | return create_extra_outs(codec, spec->autocfg.speaker_outs, |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1723 | spec->speaker_paths, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1724 | "Speaker"); |
| 1725 | } |
| 1726 | |
| 1727 | /* |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 1728 | * independent HP controls |
| 1729 | */ |
| 1730 | |
| 1731 | static int indep_hp_info(struct snd_kcontrol *kcontrol, |
| 1732 | struct snd_ctl_elem_info *uinfo) |
| 1733 | { |
| 1734 | return snd_hda_enum_bool_helper_info(kcontrol, uinfo); |
| 1735 | } |
| 1736 | |
| 1737 | static int indep_hp_get(struct snd_kcontrol *kcontrol, |
| 1738 | struct snd_ctl_elem_value *ucontrol) |
| 1739 | { |
| 1740 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1741 | struct hda_gen_spec *spec = codec->spec; |
| 1742 | ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled; |
| 1743 | return 0; |
| 1744 | } |
| 1745 | |
| 1746 | static int indep_hp_put(struct snd_kcontrol *kcontrol, |
| 1747 | struct snd_ctl_elem_value *ucontrol) |
| 1748 | { |
| 1749 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1750 | struct hda_gen_spec *spec = codec->spec; |
| 1751 | unsigned int select = ucontrol->value.enumerated.item[0]; |
| 1752 | int ret = 0; |
| 1753 | |
| 1754 | mutex_lock(&spec->pcm_mutex); |
| 1755 | if (spec->active_streams) { |
| 1756 | ret = -EBUSY; |
| 1757 | goto unlock; |
| 1758 | } |
| 1759 | |
| 1760 | if (spec->indep_hp_enabled != select) { |
| 1761 | spec->indep_hp_enabled = select; |
| 1762 | if (spec->indep_hp_enabled) |
| 1763 | spec->multiout.hp_out_nid[0] = 0; |
| 1764 | else |
| 1765 | spec->multiout.hp_out_nid[0] = spec->alt_dac_nid; |
| 1766 | ret = 1; |
| 1767 | } |
| 1768 | unlock: |
| 1769 | mutex_unlock(&spec->pcm_mutex); |
| 1770 | return ret; |
| 1771 | } |
| 1772 | |
| 1773 | static const struct snd_kcontrol_new indep_hp_ctl = { |
| 1774 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1775 | .name = "Independent HP", |
| 1776 | .info = indep_hp_info, |
| 1777 | .get = indep_hp_get, |
| 1778 | .put = indep_hp_put, |
| 1779 | }; |
| 1780 | |
| 1781 | |
| 1782 | static int create_indep_hp_ctls(struct hda_codec *codec) |
| 1783 | { |
| 1784 | struct hda_gen_spec *spec = codec->spec; |
| 1785 | |
| 1786 | if (!spec->indep_hp) |
| 1787 | return 0; |
| 1788 | if (!spec->multiout.hp_out_nid[0]) { |
| 1789 | spec->indep_hp = 0; |
| 1790 | return 0; |
| 1791 | } |
| 1792 | |
| 1793 | spec->indep_hp_enabled = false; |
| 1794 | spec->alt_dac_nid = spec->multiout.hp_out_nid[0]; |
| 1795 | if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl)) |
| 1796 | return -ENOMEM; |
| 1797 | return 0; |
| 1798 | } |
| 1799 | |
| 1800 | /* |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1801 | * channel mode enum control |
| 1802 | */ |
| 1803 | |
| 1804 | static int ch_mode_info(struct snd_kcontrol *kcontrol, |
| 1805 | struct snd_ctl_elem_info *uinfo) |
| 1806 | { |
| 1807 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1808 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 1809 | int chs; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1810 | |
| 1811 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 1812 | uinfo->count = 1; |
| 1813 | uinfo->value.enumerated.items = spec->multi_ios + 1; |
| 1814 | if (uinfo->value.enumerated.item > spec->multi_ios) |
| 1815 | uinfo->value.enumerated.item = spec->multi_ios; |
Takashi Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 1816 | chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count; |
| 1817 | sprintf(uinfo->value.enumerated.name, "%dch", chs); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1818 | return 0; |
| 1819 | } |
| 1820 | |
| 1821 | static int ch_mode_get(struct snd_kcontrol *kcontrol, |
| 1822 | struct snd_ctl_elem_value *ucontrol) |
| 1823 | { |
| 1824 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1825 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 1826 | ucontrol->value.enumerated.item[0] = |
| 1827 | (spec->ext_channel_count - spec->min_channel_count) / 2; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1828 | return 0; |
| 1829 | } |
| 1830 | |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1831 | static inline struct nid_path * |
| 1832 | get_multiio_path(struct hda_codec *codec, int idx) |
| 1833 | { |
| 1834 | struct hda_gen_spec *spec = codec->spec; |
| 1835 | return snd_hda_get_path_from_idx(codec, |
| 1836 | spec->out_paths[spec->autocfg.line_outs + idx]); |
| 1837 | } |
| 1838 | |
Takashi Iwai | a5cc250 | 2013-01-16 18:08:55 +0100 | [diff] [blame] | 1839 | static void update_automute_all(struct hda_codec *codec); |
| 1840 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1841 | static int set_multi_io(struct hda_codec *codec, int idx, bool output) |
| 1842 | { |
| 1843 | struct hda_gen_spec *spec = codec->spec; |
| 1844 | hda_nid_t nid = spec->multi_io[idx].pin; |
| 1845 | struct nid_path *path; |
| 1846 | |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 1847 | path = get_multiio_path(codec, idx); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1848 | if (!path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | return -EINVAL; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1850 | |
| 1851 | if (path->active == output) |
| 1852 | return 0; |
| 1853 | |
| 1854 | if (output) { |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 1855 | set_pin_target(codec, nid, PIN_OUT, true); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1856 | snd_hda_activate_path(codec, path, true, true); |
Takashi Iwai | d5a9f1b | 2012-12-20 15:36:30 +0100 | [diff] [blame] | 1857 | set_pin_eapd(codec, nid, true); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1858 | } else { |
Takashi Iwai | d5a9f1b | 2012-12-20 15:36:30 +0100 | [diff] [blame] | 1859 | set_pin_eapd(codec, nid, false); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1860 | snd_hda_activate_path(codec, path, false, true); |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 1861 | set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | } |
Takashi Iwai | a365fed | 2013-01-10 16:10:06 +0100 | [diff] [blame] | 1863 | |
| 1864 | /* update jack retasking in case it modifies any of them */ |
Takashi Iwai | a5cc250 | 2013-01-16 18:08:55 +0100 | [diff] [blame] | 1865 | update_automute_all(codec); |
Takashi Iwai | a365fed | 2013-01-10 16:10:06 +0100 | [diff] [blame] | 1866 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1867 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | } |
| 1869 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1870 | static int ch_mode_put(struct snd_kcontrol *kcontrol, |
| 1871 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1873 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1874 | struct hda_gen_spec *spec = codec->spec; |
| 1875 | int i, ch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1877 | ch = ucontrol->value.enumerated.item[0]; |
| 1878 | if (ch < 0 || ch > spec->multi_ios) |
| 1879 | return -EINVAL; |
Takashi Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 1880 | if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1881 | return 0; |
Takashi Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 1882 | spec->ext_channel_count = ch * 2 + spec->min_channel_count; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1883 | for (i = 0; i < spec->multi_ios; i++) |
| 1884 | set_multi_io(codec, i, i < ch); |
| 1885 | spec->multiout.max_channels = max(spec->ext_channel_count, |
| 1886 | spec->const_channel_count); |
| 1887 | if (spec->need_dac_fix) |
| 1888 | spec->multiout.num_dacs = spec->multiout.max_channels / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | return 1; |
| 1890 | } |
| 1891 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1892 | static const struct snd_kcontrol_new channel_mode_enum = { |
| 1893 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1894 | .name = "Channel Mode", |
| 1895 | .info = ch_mode_info, |
| 1896 | .get = ch_mode_get, |
| 1897 | .put = ch_mode_put, |
| 1898 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1900 | static int create_multi_channel_mode(struct hda_codec *codec) |
| 1901 | { |
| 1902 | struct hda_gen_spec *spec = codec->spec; |
| 1903 | |
| 1904 | if (spec->multi_ios > 0) { |
Takashi Iwai | 12c93df | 2012-12-19 14:38:33 +0100 | [diff] [blame] | 1905 | if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum)) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1906 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | return 0; |
| 1909 | } |
| 1910 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1911 | /* |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 1912 | * aamix loopback enable/disable switch |
| 1913 | */ |
| 1914 | |
| 1915 | #define loopback_mixing_info indep_hp_info |
| 1916 | |
| 1917 | static int loopback_mixing_get(struct snd_kcontrol *kcontrol, |
| 1918 | struct snd_ctl_elem_value *ucontrol) |
| 1919 | { |
| 1920 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1921 | struct hda_gen_spec *spec = codec->spec; |
| 1922 | ucontrol->value.enumerated.item[0] = spec->aamix_mode; |
| 1923 | return 0; |
| 1924 | } |
| 1925 | |
| 1926 | static void update_aamix_paths(struct hda_codec *codec, bool do_mix, |
| 1927 | int nomix_path_idx, int mix_path_idx) |
| 1928 | { |
| 1929 | struct nid_path *nomix_path, *mix_path; |
| 1930 | |
| 1931 | nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx); |
| 1932 | mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx); |
| 1933 | if (!nomix_path || !mix_path) |
| 1934 | return; |
| 1935 | if (do_mix) { |
| 1936 | snd_hda_activate_path(codec, nomix_path, false, true); |
| 1937 | snd_hda_activate_path(codec, mix_path, true, true); |
| 1938 | } else { |
| 1939 | snd_hda_activate_path(codec, mix_path, false, true); |
| 1940 | snd_hda_activate_path(codec, nomix_path, true, true); |
| 1941 | } |
| 1942 | } |
| 1943 | |
| 1944 | static int loopback_mixing_put(struct snd_kcontrol *kcontrol, |
| 1945 | struct snd_ctl_elem_value *ucontrol) |
| 1946 | { |
| 1947 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1948 | struct hda_gen_spec *spec = codec->spec; |
| 1949 | unsigned int val = ucontrol->value.enumerated.item[0]; |
| 1950 | |
| 1951 | if (val == spec->aamix_mode) |
| 1952 | return 0; |
| 1953 | spec->aamix_mode = val; |
| 1954 | update_aamix_paths(codec, val, spec->out_paths[0], |
| 1955 | spec->aamix_out_paths[0]); |
| 1956 | update_aamix_paths(codec, val, spec->hp_paths[0], |
| 1957 | spec->aamix_out_paths[1]); |
| 1958 | update_aamix_paths(codec, val, spec->speaker_paths[0], |
| 1959 | spec->aamix_out_paths[2]); |
| 1960 | return 1; |
| 1961 | } |
| 1962 | |
| 1963 | static const struct snd_kcontrol_new loopback_mixing_enum = { |
| 1964 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1965 | .name = "Loopback Mixing", |
| 1966 | .info = loopback_mixing_info, |
| 1967 | .get = loopback_mixing_get, |
| 1968 | .put = loopback_mixing_put, |
| 1969 | }; |
| 1970 | |
| 1971 | static int create_loopback_mixing_ctl(struct hda_codec *codec) |
| 1972 | { |
| 1973 | struct hda_gen_spec *spec = codec->spec; |
| 1974 | |
| 1975 | if (!spec->mixer_nid) |
| 1976 | return 0; |
| 1977 | if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] || |
| 1978 | spec->aamix_out_paths[2])) |
| 1979 | return 0; |
| 1980 | if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum)) |
| 1981 | return -ENOMEM; |
| 1982 | return 0; |
| 1983 | } |
| 1984 | |
| 1985 | /* |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1986 | * shared headphone/mic handling |
| 1987 | */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 1988 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 1989 | static void call_update_outputs(struct hda_codec *codec); |
| 1990 | |
| 1991 | /* for shared I/O, change the pin-control accordingly */ |
| 1992 | static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic) |
| 1993 | { |
| 1994 | struct hda_gen_spec *spec = codec->spec; |
| 1995 | unsigned int val; |
| 1996 | hda_nid_t pin = spec->autocfg.inputs[1].pin; |
| 1997 | /* NOTE: this assumes that there are only two inputs, the |
| 1998 | * first is the real internal mic and the second is HP/mic jack. |
| 1999 | */ |
| 2000 | |
| 2001 | val = snd_hda_get_default_vref(codec, pin); |
| 2002 | |
| 2003 | /* This pin does not have vref caps - let's enable vref on pin 0x18 |
| 2004 | instead, as suggested by Realtek */ |
| 2005 | if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) { |
| 2006 | const hda_nid_t vref_pin = spec->shared_mic_vref_pin; |
| 2007 | unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin); |
| 2008 | if (vref_val != AC_PINCTL_VREF_HIZ) |
Takashi Iwai | 7594aa3 | 2012-12-20 15:38:40 +0100 | [diff] [blame] | 2009 | snd_hda_set_pin_ctl_cache(codec, vref_pin, |
| 2010 | PIN_IN | (set_as_mic ? vref_val : 0)); |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2011 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2012 | |
| 2013 | val = set_as_mic ? val | PIN_IN : PIN_HP; |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 2014 | set_pin_target(codec, pin, val, true); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2015 | |
| 2016 | spec->automute_speaker = !set_as_mic; |
| 2017 | call_update_outputs(codec); |
| 2018 | } |
| 2019 | |
| 2020 | /* create a shared input with the headphone out */ |
| 2021 | static int create_shared_input(struct hda_codec *codec) |
| 2022 | { |
| 2023 | struct hda_gen_spec *spec = codec->spec; |
| 2024 | struct auto_pin_cfg *cfg = &spec->autocfg; |
| 2025 | unsigned int defcfg; |
| 2026 | hda_nid_t nid; |
| 2027 | |
| 2028 | /* only one internal input pin? */ |
| 2029 | if (cfg->num_inputs != 1) |
| 2030 | return 0; |
| 2031 | defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin); |
| 2032 | if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT) |
| 2033 | return 0; |
| 2034 | |
| 2035 | if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) |
| 2036 | nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */ |
| 2037 | else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT) |
| 2038 | nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */ |
| 2039 | else |
| 2040 | return 0; /* both not available */ |
| 2041 | |
| 2042 | if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN)) |
| 2043 | return 0; /* no input */ |
| 2044 | |
| 2045 | cfg->inputs[1].pin = nid; |
| 2046 | cfg->inputs[1].type = AUTO_PIN_MIC; |
| 2047 | cfg->num_inputs = 2; |
| 2048 | spec->shared_mic_hp = 1; |
| 2049 | snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid); |
| 2050 | return 0; |
| 2051 | } |
| 2052 | |
Takashi Iwai | 978e77e | 2013-01-10 16:57:58 +0100 | [diff] [blame] | 2053 | /* |
| 2054 | * output jack mode |
| 2055 | */ |
| 2056 | static int out_jack_mode_info(struct snd_kcontrol *kcontrol, |
| 2057 | struct snd_ctl_elem_info *uinfo) |
| 2058 | { |
| 2059 | static const char * const texts[] = { |
| 2060 | "Line Out", "Headphone Out", |
| 2061 | }; |
| 2062 | return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts); |
| 2063 | } |
| 2064 | |
| 2065 | static int out_jack_mode_get(struct snd_kcontrol *kcontrol, |
| 2066 | struct snd_ctl_elem_value *ucontrol) |
| 2067 | { |
| 2068 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2069 | hda_nid_t nid = kcontrol->private_value; |
| 2070 | if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP) |
| 2071 | ucontrol->value.enumerated.item[0] = 1; |
| 2072 | else |
| 2073 | ucontrol->value.enumerated.item[0] = 0; |
| 2074 | return 0; |
| 2075 | } |
| 2076 | |
| 2077 | static int out_jack_mode_put(struct snd_kcontrol *kcontrol, |
| 2078 | struct snd_ctl_elem_value *ucontrol) |
| 2079 | { |
| 2080 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2081 | hda_nid_t nid = kcontrol->private_value; |
| 2082 | unsigned int val; |
| 2083 | |
| 2084 | val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT; |
| 2085 | if (snd_hda_codec_get_pin_target(codec, nid) == val) |
| 2086 | return 0; |
| 2087 | snd_hda_set_pin_ctl_cache(codec, nid, val); |
| 2088 | return 1; |
| 2089 | } |
| 2090 | |
| 2091 | static const struct snd_kcontrol_new out_jack_mode_enum = { |
| 2092 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2093 | .info = out_jack_mode_info, |
| 2094 | .get = out_jack_mode_get, |
| 2095 | .put = out_jack_mode_put, |
| 2096 | }; |
| 2097 | |
| 2098 | static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx) |
| 2099 | { |
| 2100 | struct hda_gen_spec *spec = codec->spec; |
| 2101 | int i; |
| 2102 | |
| 2103 | for (i = 0; i < spec->kctls.used; i++) { |
| 2104 | struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i); |
| 2105 | if (!strcmp(kctl->name, name) && kctl->index == idx) |
| 2106 | return true; |
| 2107 | } |
| 2108 | return false; |
| 2109 | } |
| 2110 | |
| 2111 | static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin, |
| 2112 | char *name, size_t name_len) |
| 2113 | { |
| 2114 | struct hda_gen_spec *spec = codec->spec; |
| 2115 | int idx = 0; |
| 2116 | |
| 2117 | snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx); |
| 2118 | strlcat(name, " Jack Mode", name_len); |
| 2119 | |
| 2120 | for (; find_kctl_name(codec, name, idx); idx++) |
| 2121 | ; |
| 2122 | } |
| 2123 | |
| 2124 | static int create_out_jack_modes(struct hda_codec *codec, int num_pins, |
| 2125 | hda_nid_t *pins) |
| 2126 | { |
| 2127 | struct hda_gen_spec *spec = codec->spec; |
| 2128 | int i; |
| 2129 | |
| 2130 | for (i = 0; i < num_pins; i++) { |
| 2131 | hda_nid_t pin = pins[i]; |
| 2132 | unsigned int pincap = snd_hda_query_pin_caps(codec, pin); |
| 2133 | if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) { |
| 2134 | struct snd_kcontrol_new *knew; |
| 2135 | char name[44]; |
| 2136 | get_jack_mode_name(codec, pin, name, sizeof(name)); |
| 2137 | knew = snd_hda_gen_add_kctl(spec, name, |
| 2138 | &out_jack_mode_enum); |
| 2139 | if (!knew) |
| 2140 | return -ENOMEM; |
| 2141 | knew->private_value = pin; |
| 2142 | } |
| 2143 | } |
| 2144 | |
| 2145 | return 0; |
| 2146 | } |
| 2147 | |
Takashi Iwai | 29476558 | 2013-01-17 09:52:11 +0100 | [diff] [blame] | 2148 | /* |
| 2149 | * input jack mode |
| 2150 | */ |
| 2151 | |
| 2152 | /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */ |
| 2153 | #define NUM_VREFS 6 |
| 2154 | |
| 2155 | static const char * const vref_texts[NUM_VREFS] = { |
| 2156 | "Line In", "Mic 50pc Bias", "Mic 0V Bias", |
| 2157 | "", "Mic 80pc Bias", "Mic 100pc Bias" |
| 2158 | }; |
| 2159 | |
| 2160 | static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin) |
| 2161 | { |
| 2162 | unsigned int pincap; |
| 2163 | |
| 2164 | pincap = snd_hda_query_pin_caps(codec, pin); |
| 2165 | pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; |
| 2166 | /* filter out unusual vrefs */ |
| 2167 | pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100); |
| 2168 | return pincap; |
| 2169 | } |
| 2170 | |
| 2171 | /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */ |
| 2172 | static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx) |
| 2173 | { |
| 2174 | unsigned int i, n = 0; |
| 2175 | |
| 2176 | for (i = 0; i < NUM_VREFS; i++) { |
| 2177 | if (vref_caps & (1 << i)) { |
| 2178 | if (n == item_idx) |
| 2179 | return i; |
| 2180 | n++; |
| 2181 | } |
| 2182 | } |
| 2183 | return 0; |
| 2184 | } |
| 2185 | |
| 2186 | /* convert back from the vref ctl index to the enum item index */ |
| 2187 | static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx) |
| 2188 | { |
| 2189 | unsigned int i, n = 0; |
| 2190 | |
| 2191 | for (i = 0; i < NUM_VREFS; i++) { |
| 2192 | if (i == idx) |
| 2193 | return n; |
| 2194 | if (vref_caps & (1 << i)) |
| 2195 | n++; |
| 2196 | } |
| 2197 | return 0; |
| 2198 | } |
| 2199 | |
| 2200 | static int in_jack_mode_info(struct snd_kcontrol *kcontrol, |
| 2201 | struct snd_ctl_elem_info *uinfo) |
| 2202 | { |
| 2203 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2204 | hda_nid_t nid = kcontrol->private_value; |
| 2205 | unsigned int vref_caps = get_vref_caps(codec, nid); |
| 2206 | |
| 2207 | snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps), |
| 2208 | vref_texts); |
| 2209 | /* set the right text */ |
| 2210 | strcpy(uinfo->value.enumerated.name, |
| 2211 | vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]); |
| 2212 | return 0; |
| 2213 | } |
| 2214 | |
| 2215 | static int in_jack_mode_get(struct snd_kcontrol *kcontrol, |
| 2216 | struct snd_ctl_elem_value *ucontrol) |
| 2217 | { |
| 2218 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2219 | hda_nid_t nid = kcontrol->private_value; |
| 2220 | unsigned int vref_caps = get_vref_caps(codec, nid); |
| 2221 | unsigned int idx; |
| 2222 | |
| 2223 | idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN; |
| 2224 | ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx); |
| 2225 | return 0; |
| 2226 | } |
| 2227 | |
| 2228 | static int in_jack_mode_put(struct snd_kcontrol *kcontrol, |
| 2229 | struct snd_ctl_elem_value *ucontrol) |
| 2230 | { |
| 2231 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2232 | hda_nid_t nid = kcontrol->private_value; |
| 2233 | unsigned int vref_caps = get_vref_caps(codec, nid); |
| 2234 | unsigned int val, idx; |
| 2235 | |
| 2236 | val = snd_hda_codec_get_pin_target(codec, nid); |
| 2237 | idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN); |
| 2238 | if (idx == ucontrol->value.enumerated.item[0]) |
| 2239 | return 0; |
| 2240 | |
| 2241 | val &= ~AC_PINCTL_VREFEN; |
| 2242 | val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]); |
| 2243 | snd_hda_set_pin_ctl_cache(codec, nid, val); |
| 2244 | return 1; |
| 2245 | } |
| 2246 | |
| 2247 | static const struct snd_kcontrol_new in_jack_mode_enum = { |
| 2248 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2249 | .info = in_jack_mode_info, |
| 2250 | .get = in_jack_mode_get, |
| 2251 | .put = in_jack_mode_put, |
| 2252 | }; |
| 2253 | |
| 2254 | static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin) |
| 2255 | { |
| 2256 | struct hda_gen_spec *spec = codec->spec; |
| 2257 | unsigned int defcfg; |
| 2258 | struct snd_kcontrol_new *knew; |
| 2259 | char name[44]; |
| 2260 | |
| 2261 | /* no jack mode for fixed pins */ |
| 2262 | defcfg = snd_hda_codec_get_pincfg(codec, pin); |
| 2263 | if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT) |
| 2264 | return 0; |
| 2265 | |
| 2266 | /* no multiple vref caps? */ |
| 2267 | if (hweight32(get_vref_caps(codec, pin)) <= 1) |
| 2268 | return 0; |
| 2269 | |
| 2270 | get_jack_mode_name(codec, pin, name, sizeof(name)); |
| 2271 | knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum); |
| 2272 | if (!knew) |
| 2273 | return -ENOMEM; |
| 2274 | knew->private_value = pin; |
| 2275 | return 0; |
| 2276 | } |
| 2277 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2278 | |
| 2279 | /* |
| 2280 | * Parse input paths |
| 2281 | */ |
| 2282 | |
| 2283 | #ifdef CONFIG_PM |
| 2284 | /* add the powersave loopback-list entry */ |
| 2285 | static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx) |
| 2286 | { |
| 2287 | struct hda_amp_list *list; |
| 2288 | |
| 2289 | if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1) |
| 2290 | return; |
| 2291 | list = spec->loopback_list + spec->num_loopbacks; |
| 2292 | list->nid = mix; |
| 2293 | list->dir = HDA_INPUT; |
| 2294 | list->idx = idx; |
| 2295 | spec->num_loopbacks++; |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2296 | spec->loopback.amplist = spec->loopback_list; |
| 2297 | } |
| 2298 | #else |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2299 | #define add_loopback_list(spec, mix, idx) /* NOP */ |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 2300 | #endif |
| 2301 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2302 | /* create input playback/capture controls for the given pin */ |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 2303 | static int new_analog_input(struct hda_codec *codec, int input_idx, |
| 2304 | hda_nid_t pin, const char *ctlname, int ctlidx, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2305 | hda_nid_t mix_nid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2307 | struct hda_gen_spec *spec = codec->spec; |
| 2308 | struct nid_path *path; |
| 2309 | unsigned int val; |
| 2310 | int err, idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2311 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2312 | if (!nid_has_volume(codec, mix_nid, HDA_INPUT) && |
| 2313 | !nid_has_mute(codec, mix_nid, HDA_INPUT)) |
| 2314 | return 0; /* no need for analog loopback */ |
| 2315 | |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 2316 | path = snd_hda_add_new_path(codec, pin, mix_nid, 0); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2317 | if (!path) |
| 2318 | return -EINVAL; |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 2319 | print_nid_path("loopback", path); |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 2320 | spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2321 | |
| 2322 | idx = path->idx[path->depth - 1]; |
| 2323 | if (nid_has_volume(codec, mix_nid, HDA_INPUT)) { |
| 2324 | val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT); |
| 2325 | err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2326 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | return err; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2328 | path->ctls[NID_PATH_VOL_CTL] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | } |
| 2330 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2331 | if (nid_has_mute(codec, mix_nid, HDA_INPUT)) { |
| 2332 | val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT); |
| 2333 | err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 2334 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | return err; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2336 | path->ctls[NID_PATH_MUTE_CTL] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | } |
| 2338 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2339 | path->active = true; |
| 2340 | add_loopback_list(spec, mix_nid, idx); |
| 2341 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | } |
| 2343 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2344 | 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] | 2345 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2346 | unsigned int pincap = snd_hda_query_pin_caps(codec, nid); |
| 2347 | return (pincap & AC_PINCAP_IN) != 0; |
| 2348 | } |
| 2349 | |
| 2350 | /* Parse the codec tree and retrieve ADCs */ |
| 2351 | static int fill_adc_nids(struct hda_codec *codec) |
| 2352 | { |
| 2353 | struct hda_gen_spec *spec = codec->spec; |
| 2354 | hda_nid_t nid; |
| 2355 | hda_nid_t *adc_nids = spec->adc_nids; |
| 2356 | int max_nums = ARRAY_SIZE(spec->adc_nids); |
| 2357 | int i, nums = 0; |
| 2358 | |
| 2359 | nid = codec->start_nid; |
| 2360 | for (i = 0; i < codec->num_nodes; i++, nid++) { |
| 2361 | unsigned int caps = get_wcaps(codec, nid); |
| 2362 | int type = get_wcaps_type(caps); |
| 2363 | |
| 2364 | if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL)) |
| 2365 | continue; |
| 2366 | adc_nids[nums] = nid; |
| 2367 | if (++nums >= max_nums) |
| 2368 | break; |
| 2369 | } |
| 2370 | spec->num_adc_nids = nums; |
| 2371 | return nums; |
| 2372 | } |
| 2373 | |
| 2374 | /* filter out invalid adc_nids that don't give all active input pins; |
| 2375 | * if needed, check whether dynamic ADC-switching is available |
| 2376 | */ |
| 2377 | static int check_dyn_adc_switch(struct hda_codec *codec) |
| 2378 | { |
| 2379 | struct hda_gen_spec *spec = codec->spec; |
| 2380 | struct hda_input_mux *imux = &spec->input_mux; |
Takashi Iwai | 3a65bcd | 2013-01-09 09:06:18 +0100 | [diff] [blame] | 2381 | unsigned int ok_bits; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2382 | int i, n, nums; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2383 | |
| 2384 | again: |
| 2385 | nums = 0; |
Takashi Iwai | 3a65bcd | 2013-01-09 09:06:18 +0100 | [diff] [blame] | 2386 | ok_bits = 0; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2387 | for (n = 0; n < spec->num_adc_nids; n++) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2388 | for (i = 0; i < imux->num_items; i++) { |
Takashi Iwai | 3a65bcd | 2013-01-09 09:06:18 +0100 | [diff] [blame] | 2389 | if (!spec->input_paths[i][n]) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2390 | break; |
| 2391 | } |
Takashi Iwai | 3a65bcd | 2013-01-09 09:06:18 +0100 | [diff] [blame] | 2392 | if (i >= imux->num_items) { |
| 2393 | ok_bits |= (1 << n); |
| 2394 | nums++; |
| 2395 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2396 | } |
| 2397 | |
Takashi Iwai | 3a65bcd | 2013-01-09 09:06:18 +0100 | [diff] [blame] | 2398 | if (!ok_bits) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2399 | if (spec->shared_mic_hp) { |
| 2400 | spec->shared_mic_hp = 0; |
| 2401 | imux->num_items = 1; |
| 2402 | goto again; |
| 2403 | } |
| 2404 | |
| 2405 | /* check whether ADC-switch is possible */ |
| 2406 | for (i = 0; i < imux->num_items; i++) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2407 | for (n = 0; n < spec->num_adc_nids; n++) { |
Takashi Iwai | 3a65bcd | 2013-01-09 09:06:18 +0100 | [diff] [blame] | 2408 | if (spec->input_paths[i][n]) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2409 | spec->dyn_adc_idx[i] = n; |
| 2410 | break; |
| 2411 | } |
| 2412 | } |
| 2413 | } |
| 2414 | |
| 2415 | snd_printdd("hda-codec: enabling ADC switching\n"); |
| 2416 | spec->dyn_adc_switch = 1; |
| 2417 | } else if (nums != spec->num_adc_nids) { |
Takashi Iwai | 3a65bcd | 2013-01-09 09:06:18 +0100 | [diff] [blame] | 2418 | /* shrink the invalid adcs and input paths */ |
| 2419 | nums = 0; |
| 2420 | for (n = 0; n < spec->num_adc_nids; n++) { |
| 2421 | if (!(ok_bits & (1 << n))) |
| 2422 | continue; |
| 2423 | if (n != nums) { |
| 2424 | spec->adc_nids[nums] = spec->adc_nids[n]; |
Takashi Iwai | 980428c | 2013-01-09 09:28:20 +0100 | [diff] [blame] | 2425 | for (i = 0; i < imux->num_items; i++) { |
| 2426 | invalidate_nid_path(codec, |
| 2427 | spec->input_paths[i][nums]); |
Takashi Iwai | 3a65bcd | 2013-01-09 09:06:18 +0100 | [diff] [blame] | 2428 | spec->input_paths[i][nums] = |
| 2429 | spec->input_paths[i][n]; |
Takashi Iwai | 980428c | 2013-01-09 09:28:20 +0100 | [diff] [blame] | 2430 | } |
Takashi Iwai | 3a65bcd | 2013-01-09 09:06:18 +0100 | [diff] [blame] | 2431 | } |
| 2432 | nums++; |
| 2433 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2434 | spec->num_adc_nids = nums; |
| 2435 | } |
| 2436 | |
| 2437 | if (imux->num_items == 1 || spec->shared_mic_hp) { |
| 2438 | snd_printdd("hda-codec: reducing to a single ADC\n"); |
| 2439 | spec->num_adc_nids = 1; /* reduce to a single ADC */ |
| 2440 | } |
| 2441 | |
| 2442 | /* single index for individual volumes ctls */ |
| 2443 | if (!spec->dyn_adc_switch && spec->multi_cap_vol) |
| 2444 | spec->num_adc_nids = 1; |
| 2445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | return 0; |
| 2447 | } |
| 2448 | |
Takashi Iwai | f3fc0b0 | 2013-01-09 09:14:23 +0100 | [diff] [blame] | 2449 | /* parse capture source paths from the given pin and create imux items */ |
| 2450 | static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin, |
| 2451 | int num_adcs, const char *label, int anchor) |
| 2452 | { |
| 2453 | struct hda_gen_spec *spec = codec->spec; |
| 2454 | struct hda_input_mux *imux = &spec->input_mux; |
| 2455 | int imux_idx = imux->num_items; |
| 2456 | bool imux_added = false; |
| 2457 | int c; |
| 2458 | |
| 2459 | for (c = 0; c < num_adcs; c++) { |
| 2460 | struct nid_path *path; |
| 2461 | hda_nid_t adc = spec->adc_nids[c]; |
| 2462 | |
| 2463 | if (!is_reachable_path(codec, pin, adc)) |
| 2464 | continue; |
| 2465 | path = snd_hda_add_new_path(codec, pin, adc, anchor); |
| 2466 | if (!path) |
| 2467 | continue; |
| 2468 | print_nid_path("input", path); |
| 2469 | spec->input_paths[imux_idx][c] = |
| 2470 | snd_hda_get_path_idx(codec, path); |
| 2471 | |
| 2472 | if (!imux_added) { |
| 2473 | spec->imux_pins[imux->num_items] = pin; |
| 2474 | snd_hda_add_imux_item(imux, label, |
| 2475 | imux->num_items, NULL); |
| 2476 | imux_added = true; |
| 2477 | } |
| 2478 | } |
| 2479 | |
| 2480 | return 0; |
| 2481 | } |
| 2482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | /* |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2484 | * create playback/capture controls for input pins |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2486 | static int create_input_ctls(struct hda_codec *codec) |
Takashi Iwai | a7da6ce | 2006-09-06 14:03:14 +0200 | [diff] [blame] | 2487 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2488 | struct hda_gen_spec *spec = codec->spec; |
| 2489 | const struct auto_pin_cfg *cfg = &spec->autocfg; |
| 2490 | hda_nid_t mixer = spec->mixer_nid; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2491 | int num_adcs; |
Takashi Iwai | f3fc0b0 | 2013-01-09 09:14:23 +0100 | [diff] [blame] | 2492 | int i, err, type_idx = 0; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2493 | const char *prev_label = NULL; |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 2494 | unsigned int val; |
Takashi Iwai | a7da6ce | 2006-09-06 14:03:14 +0200 | [diff] [blame] | 2495 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2496 | num_adcs = fill_adc_nids(codec); |
| 2497 | if (num_adcs < 0) |
| 2498 | return 0; |
Takashi Iwai | a7da6ce | 2006-09-06 14:03:14 +0200 | [diff] [blame] | 2499 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2500 | for (i = 0; i < cfg->num_inputs; i++) { |
| 2501 | hda_nid_t pin; |
| 2502 | const char *label; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2504 | pin = cfg->inputs[i].pin; |
| 2505 | if (!is_input_pin(codec, pin)) |
| 2506 | continue; |
| 2507 | |
| 2508 | label = hda_get_autocfg_input_label(codec, cfg, i); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2509 | if (prev_label && !strcmp(label, prev_label)) |
| 2510 | type_idx++; |
Takashi Iwai | a7da6ce | 2006-09-06 14:03:14 +0200 | [diff] [blame] | 2511 | else |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2512 | type_idx = 0; |
| 2513 | prev_label = label; |
| 2514 | |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 2515 | val = PIN_IN; |
| 2516 | if (cfg->inputs[i].type == AUTO_PIN_MIC) |
| 2517 | val |= snd_hda_get_default_vref(codec, pin); |
| 2518 | set_pin_target(codec, pin, val, false); |
| 2519 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2520 | if (mixer) { |
| 2521 | if (is_reachable_path(codec, pin, mixer)) { |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 2522 | err = new_analog_input(codec, i, pin, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2523 | label, type_idx, mixer); |
| 2524 | if (err < 0) |
| 2525 | return err; |
| 2526 | } |
| 2527 | } |
| 2528 | |
Takashi Iwai | f3fc0b0 | 2013-01-09 09:14:23 +0100 | [diff] [blame] | 2529 | err = parse_capture_source(codec, pin, num_adcs, label, -mixer); |
| 2530 | if (err < 0) |
| 2531 | return err; |
Takashi Iwai | 29476558 | 2013-01-17 09:52:11 +0100 | [diff] [blame] | 2532 | |
| 2533 | if (spec->add_in_jack_modes) { |
| 2534 | err = create_in_jack_mode(codec, pin); |
| 2535 | if (err < 0) |
| 2536 | return err; |
| 2537 | } |
Takashi Iwai | f3fc0b0 | 2013-01-09 09:14:23 +0100 | [diff] [blame] | 2538 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2539 | |
Takashi Iwai | f3fc0b0 | 2013-01-09 09:14:23 +0100 | [diff] [blame] | 2540 | if (mixer && spec->add_stereo_mix_input) { |
| 2541 | err = parse_capture_source(codec, mixer, num_adcs, |
| 2542 | "Stereo Mix", 0); |
| 2543 | if (err < 0) |
| 2544 | return err; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2545 | } |
| 2546 | |
| 2547 | return 0; |
| 2548 | } |
| 2549 | |
| 2550 | |
| 2551 | /* |
| 2552 | * input source mux |
| 2553 | */ |
| 2554 | |
Takashi Iwai | c697b71 | 2013-01-07 17:09:26 +0100 | [diff] [blame] | 2555 | /* get the input path specified by the given adc and imux indices */ |
| 2556 | 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] | 2557 | { |
| 2558 | struct hda_gen_spec *spec = codec->spec; |
David Henningsson | b56fa1e | 2013-01-16 11:45:35 +0100 | [diff] [blame] | 2559 | if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) { |
| 2560 | snd_BUG(); |
| 2561 | return NULL; |
| 2562 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2563 | if (spec->dyn_adc_switch) |
| 2564 | adc_idx = spec->dyn_adc_idx[imux_idx]; |
David Henningsson | b56fa1e | 2013-01-16 11:45:35 +0100 | [diff] [blame] | 2565 | if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_OUTS) { |
| 2566 | snd_BUG(); |
| 2567 | return NULL; |
| 2568 | } |
Takashi Iwai | c697b71 | 2013-01-07 17:09:26 +0100 | [diff] [blame] | 2569 | 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] | 2570 | } |
| 2571 | |
| 2572 | static int mux_select(struct hda_codec *codec, unsigned int adc_idx, |
| 2573 | unsigned int idx); |
| 2574 | |
| 2575 | static int mux_enum_info(struct snd_kcontrol *kcontrol, |
| 2576 | struct snd_ctl_elem_info *uinfo) |
| 2577 | { |
| 2578 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2579 | struct hda_gen_spec *spec = codec->spec; |
| 2580 | return snd_hda_input_mux_info(&spec->input_mux, uinfo); |
| 2581 | } |
| 2582 | |
| 2583 | static int mux_enum_get(struct snd_kcontrol *kcontrol, |
| 2584 | struct snd_ctl_elem_value *ucontrol) |
| 2585 | { |
| 2586 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2587 | struct hda_gen_spec *spec = codec->spec; |
David Henningsson | a053d1e | 2013-01-16 11:45:36 +0100 | [diff] [blame] | 2588 | unsigned int adc_idx = kcontrol->id.index; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2589 | |
| 2590 | ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx]; |
| 2591 | return 0; |
| 2592 | } |
| 2593 | |
| 2594 | static int mux_enum_put(struct snd_kcontrol *kcontrol, |
| 2595 | struct snd_ctl_elem_value *ucontrol) |
| 2596 | { |
| 2597 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
David Henningsson | a053d1e | 2013-01-16 11:45:36 +0100 | [diff] [blame] | 2598 | unsigned int adc_idx = kcontrol->id.index; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2599 | return mux_select(codec, adc_idx, |
| 2600 | ucontrol->value.enumerated.item[0]); |
| 2601 | } |
| 2602 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2603 | static const struct snd_kcontrol_new cap_src_temp = { |
| 2604 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2605 | .name = "Input Source", |
| 2606 | .info = mux_enum_info, |
| 2607 | .get = mux_enum_get, |
| 2608 | .put = mux_enum_put, |
| 2609 | }; |
| 2610 | |
Takashi Iwai | 47d46ab | 2012-12-20 11:48:54 +0100 | [diff] [blame] | 2611 | /* |
| 2612 | * capture volume and capture switch ctls |
| 2613 | */ |
| 2614 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2615 | typedef int (*put_call_t)(struct snd_kcontrol *kcontrol, |
| 2616 | struct snd_ctl_elem_value *ucontrol); |
| 2617 | |
Takashi Iwai | 47d46ab | 2012-12-20 11:48:54 +0100 | [diff] [blame] | 2618 | /* 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] | 2619 | static int cap_put_caller(struct snd_kcontrol *kcontrol, |
| 2620 | struct snd_ctl_elem_value *ucontrol, |
| 2621 | put_call_t func, int type) |
| 2622 | { |
| 2623 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2624 | struct hda_gen_spec *spec = codec->spec; |
| 2625 | const struct hda_input_mux *imux; |
| 2626 | struct nid_path *path; |
| 2627 | int i, adc_idx, err = 0; |
| 2628 | |
| 2629 | imux = &spec->input_mux; |
David Henningsson | a053d1e | 2013-01-16 11:45:36 +0100 | [diff] [blame] | 2630 | adc_idx = kcontrol->id.index; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2631 | mutex_lock(&codec->control_mutex); |
Takashi Iwai | 47d46ab | 2012-12-20 11:48:54 +0100 | [diff] [blame] | 2632 | /* we use the cache-only update at first since multiple input paths |
| 2633 | * may shared the same amp; by updating only caches, the redundant |
| 2634 | * writes to hardware can be reduced. |
| 2635 | */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2636 | codec->cached_write = 1; |
| 2637 | for (i = 0; i < imux->num_items; i++) { |
Takashi Iwai | c697b71 | 2013-01-07 17:09:26 +0100 | [diff] [blame] | 2638 | path = get_input_path(codec, adc_idx, i); |
| 2639 | if (!path || !path->ctls[type]) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2640 | continue; |
| 2641 | kcontrol->private_value = path->ctls[type]; |
| 2642 | err = func(kcontrol, ucontrol); |
| 2643 | if (err < 0) |
| 2644 | goto error; |
| 2645 | } |
| 2646 | error: |
| 2647 | codec->cached_write = 0; |
| 2648 | mutex_unlock(&codec->control_mutex); |
Takashi Iwai | 47d46ab | 2012-12-20 11:48:54 +0100 | [diff] [blame] | 2649 | snd_hda_codec_flush_amp_cache(codec); /* flush the updates */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2650 | if (err >= 0 && spec->cap_sync_hook) |
| 2651 | spec->cap_sync_hook(codec); |
| 2652 | return err; |
| 2653 | } |
| 2654 | |
| 2655 | /* capture volume ctl callbacks */ |
| 2656 | #define cap_vol_info snd_hda_mixer_amp_volume_info |
| 2657 | #define cap_vol_get snd_hda_mixer_amp_volume_get |
| 2658 | #define cap_vol_tlv snd_hda_mixer_amp_tlv |
| 2659 | |
| 2660 | static int cap_vol_put(struct snd_kcontrol *kcontrol, |
| 2661 | struct snd_ctl_elem_value *ucontrol) |
| 2662 | { |
| 2663 | return cap_put_caller(kcontrol, ucontrol, |
| 2664 | snd_hda_mixer_amp_volume_put, |
| 2665 | NID_PATH_VOL_CTL); |
| 2666 | } |
| 2667 | |
| 2668 | static const struct snd_kcontrol_new cap_vol_temp = { |
| 2669 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2670 | .name = "Capture Volume", |
| 2671 | .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE | |
| 2672 | SNDRV_CTL_ELEM_ACCESS_TLV_READ | |
| 2673 | SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK), |
| 2674 | .info = cap_vol_info, |
| 2675 | .get = cap_vol_get, |
| 2676 | .put = cap_vol_put, |
| 2677 | .tlv = { .c = cap_vol_tlv }, |
| 2678 | }; |
| 2679 | |
| 2680 | /* capture switch ctl callbacks */ |
| 2681 | #define cap_sw_info snd_ctl_boolean_stereo_info |
| 2682 | #define cap_sw_get snd_hda_mixer_amp_switch_get |
| 2683 | |
| 2684 | static int cap_sw_put(struct snd_kcontrol *kcontrol, |
| 2685 | struct snd_ctl_elem_value *ucontrol) |
| 2686 | { |
Takashi Iwai | ae177c3 | 2013-01-14 12:13:06 +0100 | [diff] [blame] | 2687 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 2688 | struct hda_gen_spec *spec = codec->spec; |
| 2689 | int ret; |
| 2690 | |
| 2691 | ret = cap_put_caller(kcontrol, ucontrol, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2692 | snd_hda_mixer_amp_switch_put, |
| 2693 | NID_PATH_MUTE_CTL); |
Takashi Iwai | ae177c3 | 2013-01-14 12:13:06 +0100 | [diff] [blame] | 2694 | if (ret < 0) |
| 2695 | return ret; |
| 2696 | |
| 2697 | if (spec->capture_switch_hook) { |
| 2698 | bool enable = (ucontrol->value.integer.value[0] || |
| 2699 | ucontrol->value.integer.value[1]); |
| 2700 | spec->capture_switch_hook(codec, enable); |
| 2701 | } |
| 2702 | |
| 2703 | return ret; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2704 | } |
| 2705 | |
| 2706 | static const struct snd_kcontrol_new cap_sw_temp = { |
| 2707 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 2708 | .name = "Capture Switch", |
| 2709 | .info = cap_sw_info, |
| 2710 | .get = cap_sw_get, |
| 2711 | .put = cap_sw_put, |
| 2712 | }; |
| 2713 | |
| 2714 | static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path) |
| 2715 | { |
| 2716 | hda_nid_t nid; |
| 2717 | int i, depth; |
| 2718 | |
| 2719 | path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0; |
| 2720 | for (depth = 0; depth < 3; depth++) { |
| 2721 | if (depth >= path->depth) |
| 2722 | return -EINVAL; |
| 2723 | i = path->depth - depth - 1; |
| 2724 | nid = path->path[i]; |
| 2725 | if (!path->ctls[NID_PATH_VOL_CTL]) { |
| 2726 | if (nid_has_volume(codec, nid, HDA_OUTPUT)) |
| 2727 | path->ctls[NID_PATH_VOL_CTL] = |
| 2728 | HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT); |
| 2729 | else if (nid_has_volume(codec, nid, HDA_INPUT)) { |
| 2730 | int idx = path->idx[i]; |
| 2731 | if (!depth && codec->single_adc_amp) |
| 2732 | idx = 0; |
| 2733 | path->ctls[NID_PATH_VOL_CTL] = |
| 2734 | HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT); |
| 2735 | } |
| 2736 | } |
| 2737 | if (!path->ctls[NID_PATH_MUTE_CTL]) { |
| 2738 | if (nid_has_mute(codec, nid, HDA_OUTPUT)) |
| 2739 | path->ctls[NID_PATH_MUTE_CTL] = |
| 2740 | HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT); |
| 2741 | else if (nid_has_mute(codec, nid, HDA_INPUT)) { |
| 2742 | int idx = path->idx[i]; |
| 2743 | if (!depth && codec->single_adc_amp) |
| 2744 | idx = 0; |
| 2745 | path->ctls[NID_PATH_MUTE_CTL] = |
| 2746 | HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT); |
| 2747 | } |
| 2748 | } |
Takashi Iwai | 97ec558 | 2006-03-21 11:29:07 +0100 | [diff] [blame] | 2749 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2750 | return 0; |
| 2751 | } |
| 2752 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2753 | 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] | 2754 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2755 | struct hda_gen_spec *spec = codec->spec; |
| 2756 | struct auto_pin_cfg *cfg = &spec->autocfg; |
| 2757 | unsigned int val; |
| 2758 | int i; |
| 2759 | |
| 2760 | if (!spec->inv_dmic_split) |
| 2761 | return false; |
| 2762 | for (i = 0; i < cfg->num_inputs; i++) { |
| 2763 | if (cfg->inputs[i].pin != nid) |
| 2764 | continue; |
| 2765 | if (cfg->inputs[i].type != AUTO_PIN_MIC) |
| 2766 | return false; |
| 2767 | val = snd_hda_codec_get_pincfg(codec, nid); |
| 2768 | return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT; |
| 2769 | } |
| 2770 | return false; |
| 2771 | } |
| 2772 | |
| 2773 | static int add_single_cap_ctl(struct hda_codec *codec, const char *label, |
| 2774 | int idx, bool is_switch, unsigned int ctl, |
| 2775 | bool inv_dmic) |
| 2776 | { |
| 2777 | struct hda_gen_spec *spec = codec->spec; |
| 2778 | char tmpname[44]; |
| 2779 | int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL; |
| 2780 | const char *sfx = is_switch ? "Switch" : "Volume"; |
| 2781 | unsigned int chs = inv_dmic ? 1 : 3; |
| 2782 | int err; |
| 2783 | |
| 2784 | if (!ctl) |
| 2785 | return 0; |
| 2786 | |
| 2787 | if (label) |
| 2788 | snprintf(tmpname, sizeof(tmpname), |
| 2789 | "%s Capture %s", label, sfx); |
| 2790 | else |
| 2791 | snprintf(tmpname, sizeof(tmpname), |
| 2792 | "Capture %s", sfx); |
| 2793 | err = add_control(spec, type, tmpname, idx, |
| 2794 | amp_val_replace_channels(ctl, chs)); |
| 2795 | if (err < 0 || !inv_dmic) |
| 2796 | return err; |
| 2797 | |
| 2798 | /* Make independent right kcontrol */ |
| 2799 | if (label) |
| 2800 | snprintf(tmpname, sizeof(tmpname), |
| 2801 | "Inverted %s Capture %s", label, sfx); |
| 2802 | else |
| 2803 | snprintf(tmpname, sizeof(tmpname), |
| 2804 | "Inverted Capture %s", sfx); |
| 2805 | return add_control(spec, type, tmpname, idx, |
| 2806 | amp_val_replace_channels(ctl, 2)); |
| 2807 | } |
| 2808 | |
| 2809 | /* create single (and simple) capture volume and switch controls */ |
| 2810 | static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx, |
| 2811 | unsigned int vol_ctl, unsigned int sw_ctl, |
| 2812 | bool inv_dmic) |
| 2813 | { |
| 2814 | int err; |
| 2815 | err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic); |
| 2816 | if (err < 0) |
| 2817 | return err; |
| 2818 | err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic); |
| 2819 | if (err < 0) |
| 2820 | return err; |
| 2821 | return 0; |
| 2822 | } |
| 2823 | |
| 2824 | /* create bound capture volume and switch controls */ |
| 2825 | static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx, |
| 2826 | unsigned int vol_ctl, unsigned int sw_ctl) |
| 2827 | { |
| 2828 | struct hda_gen_spec *spec = codec->spec; |
| 2829 | struct snd_kcontrol_new *knew; |
| 2830 | |
| 2831 | if (vol_ctl) { |
Takashi Iwai | 12c93df | 2012-12-19 14:38:33 +0100 | [diff] [blame] | 2832 | knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2833 | if (!knew) |
| 2834 | return -ENOMEM; |
| 2835 | knew->index = idx; |
| 2836 | knew->private_value = vol_ctl; |
| 2837 | knew->subdevice = HDA_SUBDEV_AMP_FLAG; |
| 2838 | } |
| 2839 | if (sw_ctl) { |
Takashi Iwai | 12c93df | 2012-12-19 14:38:33 +0100 | [diff] [blame] | 2840 | knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2841 | if (!knew) |
| 2842 | return -ENOMEM; |
| 2843 | knew->index = idx; |
| 2844 | knew->private_value = sw_ctl; |
| 2845 | knew->subdevice = HDA_SUBDEV_AMP_FLAG; |
| 2846 | } |
| 2847 | return 0; |
| 2848 | } |
| 2849 | |
| 2850 | /* return the vol ctl when used first in the imux list */ |
| 2851 | static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type) |
| 2852 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2853 | struct nid_path *path; |
| 2854 | unsigned int ctl; |
| 2855 | int i; |
| 2856 | |
Takashi Iwai | c697b71 | 2013-01-07 17:09:26 +0100 | [diff] [blame] | 2857 | path = get_input_path(codec, 0, idx); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2858 | if (!path) |
| 2859 | return 0; |
| 2860 | ctl = path->ctls[type]; |
| 2861 | if (!ctl) |
| 2862 | return 0; |
| 2863 | for (i = 0; i < idx - 1; i++) { |
Takashi Iwai | c697b71 | 2013-01-07 17:09:26 +0100 | [diff] [blame] | 2864 | path = get_input_path(codec, 0, i); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2865 | if (path && path->ctls[type] == ctl) |
| 2866 | return 0; |
| 2867 | } |
| 2868 | return ctl; |
| 2869 | } |
| 2870 | |
| 2871 | /* create individual capture volume and switch controls per input */ |
| 2872 | static int create_multi_cap_vol_ctl(struct hda_codec *codec) |
| 2873 | { |
| 2874 | struct hda_gen_spec *spec = codec->spec; |
| 2875 | struct hda_input_mux *imux = &spec->input_mux; |
| 2876 | int i, err, type, type_idx = 0; |
| 2877 | const char *prev_label = NULL; |
| 2878 | |
| 2879 | for (i = 0; i < imux->num_items; i++) { |
| 2880 | const char *label; |
| 2881 | bool inv_dmic; |
| 2882 | label = hda_get_autocfg_input_label(codec, &spec->autocfg, i); |
| 2883 | if (prev_label && !strcmp(label, prev_label)) |
| 2884 | type_idx++; |
| 2885 | else |
| 2886 | type_idx = 0; |
| 2887 | prev_label = label; |
| 2888 | inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]); |
| 2889 | |
| 2890 | for (type = 0; type < 2; type++) { |
| 2891 | err = add_single_cap_ctl(codec, label, type_idx, type, |
| 2892 | get_first_cap_ctl(codec, i, type), |
| 2893 | inv_dmic); |
| 2894 | if (err < 0) |
| 2895 | return err; |
| 2896 | } |
| 2897 | } |
| 2898 | return 0; |
| 2899 | } |
| 2900 | |
| 2901 | static int create_capture_mixers(struct hda_codec *codec) |
| 2902 | { |
| 2903 | struct hda_gen_spec *spec = codec->spec; |
| 2904 | struct hda_input_mux *imux = &spec->input_mux; |
| 2905 | int i, n, nums, err; |
| 2906 | |
| 2907 | if (spec->dyn_adc_switch) |
| 2908 | nums = 1; |
| 2909 | else |
| 2910 | nums = spec->num_adc_nids; |
| 2911 | |
| 2912 | if (!spec->auto_mic && imux->num_items > 1) { |
| 2913 | struct snd_kcontrol_new *knew; |
Takashi Iwai | 624d914 | 2012-12-19 17:41:52 +0100 | [diff] [blame] | 2914 | const char *name; |
| 2915 | name = nums > 1 ? "Input Source" : "Capture Source"; |
| 2916 | knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2917 | if (!knew) |
| 2918 | return -ENOMEM; |
| 2919 | knew->count = nums; |
| 2920 | } |
| 2921 | |
| 2922 | for (n = 0; n < nums; n++) { |
| 2923 | bool multi = false; |
David Henningsson | 99a5592 | 2013-01-16 15:58:44 +0100 | [diff] [blame] | 2924 | bool multi_cap_vol = spec->multi_cap_vol; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2925 | bool inv_dmic = false; |
| 2926 | int vol, sw; |
| 2927 | |
| 2928 | vol = sw = 0; |
| 2929 | for (i = 0; i < imux->num_items; i++) { |
| 2930 | struct nid_path *path; |
Takashi Iwai | c697b71 | 2013-01-07 17:09:26 +0100 | [diff] [blame] | 2931 | path = get_input_path(codec, n, i); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2932 | if (!path) |
| 2933 | continue; |
| 2934 | parse_capvol_in_path(codec, path); |
| 2935 | if (!vol) |
| 2936 | vol = path->ctls[NID_PATH_VOL_CTL]; |
David Henningsson | 99a5592 | 2013-01-16 15:58:44 +0100 | [diff] [blame] | 2937 | else if (vol != path->ctls[NID_PATH_VOL_CTL]) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2938 | multi = true; |
David Henningsson | 99a5592 | 2013-01-16 15:58:44 +0100 | [diff] [blame] | 2939 | if (!same_amp_caps(codec, vol, |
| 2940 | path->ctls[NID_PATH_VOL_CTL], HDA_INPUT)) |
| 2941 | multi_cap_vol = true; |
| 2942 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2943 | if (!sw) |
| 2944 | sw = path->ctls[NID_PATH_MUTE_CTL]; |
David Henningsson | 99a5592 | 2013-01-16 15:58:44 +0100 | [diff] [blame] | 2945 | else if (sw != path->ctls[NID_PATH_MUTE_CTL]) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2946 | multi = true; |
David Henningsson | 99a5592 | 2013-01-16 15:58:44 +0100 | [diff] [blame] | 2947 | if (!same_amp_caps(codec, sw, |
| 2948 | path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT)) |
| 2949 | multi_cap_vol = true; |
| 2950 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2951 | if (is_inv_dmic_pin(codec, spec->imux_pins[i])) |
| 2952 | inv_dmic = true; |
| 2953 | } |
| 2954 | |
| 2955 | if (!multi) |
| 2956 | err = create_single_cap_vol_ctl(codec, n, vol, sw, |
| 2957 | inv_dmic); |
David Henningsson | 99a5592 | 2013-01-16 15:58:44 +0100 | [diff] [blame] | 2958 | else if (!multi_cap_vol) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2959 | err = create_bind_cap_vol_ctl(codec, n, vol, sw); |
| 2960 | else |
| 2961 | err = create_multi_cap_vol_ctl(codec); |
| 2962 | if (err < 0) |
| 2963 | return err; |
| 2964 | } |
| 2965 | |
| 2966 | return 0; |
| 2967 | } |
| 2968 | |
| 2969 | /* |
| 2970 | * add mic boosts if needed |
| 2971 | */ |
| 2972 | static int parse_mic_boost(struct hda_codec *codec) |
| 2973 | { |
| 2974 | struct hda_gen_spec *spec = codec->spec; |
| 2975 | struct auto_pin_cfg *cfg = &spec->autocfg; |
Takashi Iwai | 071c73a | 2006-08-23 18:34:06 +0200 | [diff] [blame] | 2976 | int i, err; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2977 | int type_idx = 0; |
| 2978 | hda_nid_t nid; |
| 2979 | const char *prev_label = NULL; |
| 2980 | |
| 2981 | for (i = 0; i < cfg->num_inputs; i++) { |
| 2982 | if (cfg->inputs[i].type > AUTO_PIN_MIC) |
| 2983 | break; |
| 2984 | nid = cfg->inputs[i].pin; |
| 2985 | if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) { |
| 2986 | const char *label; |
Takashi Iwai | 5abd488 | 2013-01-07 09:43:18 +0100 | [diff] [blame] | 2987 | char boost_label[44]; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2988 | struct nid_path *path; |
| 2989 | unsigned int val; |
| 2990 | |
David Henningsson | 02aba55 | 2013-01-16 15:58:43 +0100 | [diff] [blame] | 2991 | if (!nid_has_volume(codec, nid, HDA_INPUT)) |
| 2992 | continue; |
| 2993 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2994 | label = hda_get_autocfg_input_label(codec, cfg, i); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 2995 | if (prev_label && !strcmp(label, prev_label)) |
| 2996 | type_idx++; |
| 2997 | else |
| 2998 | type_idx = 0; |
| 2999 | prev_label = label; |
| 3000 | |
| 3001 | snprintf(boost_label, sizeof(boost_label), |
| 3002 | "%s Boost Volume", label); |
| 3003 | val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT); |
| 3004 | err = add_control(spec, HDA_CTL_WIDGET_VOL, |
| 3005 | boost_label, type_idx, val); |
| 3006 | if (err < 0) |
| 3007 | return err; |
| 3008 | |
| 3009 | path = snd_hda_get_nid_path(codec, nid, 0); |
| 3010 | if (path) |
| 3011 | path->ctls[NID_PATH_BOOST_CTL] = val; |
| 3012 | } |
| 3013 | } |
| 3014 | return 0; |
| 3015 | } |
| 3016 | |
| 3017 | /* |
| 3018 | * parse digital I/Os and set up NIDs in BIOS auto-parse mode |
| 3019 | */ |
| 3020 | static void parse_digital(struct hda_codec *codec) |
| 3021 | { |
| 3022 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 3023 | struct nid_path *path; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3024 | int i, nums; |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 3025 | hda_nid_t dig_nid, pin; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3026 | |
| 3027 | /* support multiple SPDIFs; the secondary is set up as a slave */ |
| 3028 | nums = 0; |
| 3029 | for (i = 0; i < spec->autocfg.dig_outs; i++) { |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 3030 | pin = spec->autocfg.dig_out_pins[i]; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3031 | dig_nid = look_for_dac(codec, pin, true); |
| 3032 | if (!dig_nid) |
| 3033 | continue; |
Takashi Iwai | 3ca529d | 2013-01-07 17:25:08 +0100 | [diff] [blame] | 3034 | path = snd_hda_add_new_path(codec, dig_nid, pin, 0); |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 3035 | if (!path) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3036 | continue; |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 3037 | print_nid_path("digout", path); |
Takashi Iwai | e1284af | 2013-01-03 16:33:02 +0100 | [diff] [blame] | 3038 | path->active = true; |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 3039 | spec->digout_paths[i] = snd_hda_get_path_idx(codec, path); |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 3040 | set_pin_target(codec, pin, PIN_OUT, false); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3041 | if (!nums) { |
| 3042 | spec->multiout.dig_out_nid = dig_nid; |
| 3043 | spec->dig_out_type = spec->autocfg.dig_out_type[0]; |
| 3044 | } else { |
| 3045 | spec->multiout.slave_dig_outs = spec->slave_dig_outs; |
| 3046 | if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1) |
| 3047 | break; |
| 3048 | spec->slave_dig_outs[nums - 1] = dig_nid; |
| 3049 | } |
| 3050 | nums++; |
| 3051 | } |
| 3052 | |
| 3053 | if (spec->autocfg.dig_in_pin) { |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 3054 | pin = spec->autocfg.dig_in_pin; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3055 | dig_nid = codec->start_nid; |
| 3056 | for (i = 0; i < codec->num_nodes; i++, dig_nid++) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3057 | unsigned int wcaps = get_wcaps(codec, dig_nid); |
| 3058 | if (get_wcaps_type(wcaps) != AC_WID_AUD_IN) |
| 3059 | continue; |
| 3060 | if (!(wcaps & AC_WCAP_DIGITAL)) |
| 3061 | continue; |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 3062 | path = snd_hda_add_new_path(codec, pin, dig_nid, 0); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3063 | if (path) { |
Takashi Iwai | 0c8c0f5 | 2012-12-20 17:54:22 +0100 | [diff] [blame] | 3064 | print_nid_path("digin", path); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3065 | path->active = true; |
| 3066 | spec->dig_in_nid = dig_nid; |
Takashi Iwai | 2430d7b | 2013-01-04 15:09:42 +0100 | [diff] [blame] | 3067 | spec->digin_path = snd_hda_get_path_idx(codec, path); |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 3068 | set_pin_target(codec, pin, PIN_IN, false); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3069 | break; |
| 3070 | } |
| 3071 | } |
| 3072 | } |
| 3073 | } |
| 3074 | |
| 3075 | |
| 3076 | /* |
| 3077 | * input MUX handling |
| 3078 | */ |
| 3079 | |
| 3080 | static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur); |
| 3081 | |
| 3082 | /* select the given imux item; either unmute exclusively or select the route */ |
| 3083 | static int mux_select(struct hda_codec *codec, unsigned int adc_idx, |
| 3084 | unsigned int idx) |
| 3085 | { |
| 3086 | struct hda_gen_spec *spec = codec->spec; |
| 3087 | const struct hda_input_mux *imux; |
| 3088 | struct nid_path *path; |
| 3089 | |
| 3090 | imux = &spec->input_mux; |
| 3091 | if (!imux->num_items) |
| 3092 | return 0; |
| 3093 | |
| 3094 | if (idx >= imux->num_items) |
| 3095 | idx = imux->num_items - 1; |
| 3096 | if (spec->cur_mux[adc_idx] == idx) |
| 3097 | return 0; |
| 3098 | |
Takashi Iwai | c697b71 | 2013-01-07 17:09:26 +0100 | [diff] [blame] | 3099 | path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3100 | if (!path) |
| 3101 | return 0; |
| 3102 | if (path->active) |
| 3103 | snd_hda_activate_path(codec, path, false, false); |
| 3104 | |
| 3105 | spec->cur_mux[adc_idx] = idx; |
| 3106 | |
| 3107 | if (spec->shared_mic_hp) |
| 3108 | update_shared_mic_hp(codec, spec->cur_mux[adc_idx]); |
| 3109 | |
| 3110 | if (spec->dyn_adc_switch) |
| 3111 | dyn_adc_pcm_resetup(codec, idx); |
| 3112 | |
Takashi Iwai | c697b71 | 2013-01-07 17:09:26 +0100 | [diff] [blame] | 3113 | path = get_input_path(codec, adc_idx, idx); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3114 | if (!path) |
| 3115 | return 0; |
| 3116 | if (path->active) |
| 3117 | return 0; |
| 3118 | snd_hda_activate_path(codec, path, true, false); |
| 3119 | if (spec->cap_sync_hook) |
| 3120 | spec->cap_sync_hook(codec); |
| 3121 | return 1; |
| 3122 | } |
| 3123 | |
| 3124 | |
| 3125 | /* |
| 3126 | * Jack detections for HP auto-mute and mic-switch |
| 3127 | */ |
| 3128 | |
| 3129 | /* check each pin in the given array; returns true if any of them is plugged */ |
| 3130 | static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins) |
| 3131 | { |
| 3132 | int i, present = 0; |
| 3133 | |
| 3134 | for (i = 0; i < num_pins; i++) { |
| 3135 | hda_nid_t nid = pins[i]; |
| 3136 | if (!nid) |
| 3137 | break; |
Takashi Iwai | 0b4df93 | 2013-01-10 09:45:13 +0100 | [diff] [blame] | 3138 | /* don't detect pins retasked as inputs */ |
| 3139 | if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN) |
| 3140 | continue; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3141 | present |= snd_hda_jack_detect(codec, nid); |
| 3142 | } |
| 3143 | return present; |
| 3144 | } |
| 3145 | |
| 3146 | /* standard HP/line-out auto-mute helper */ |
| 3147 | static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins, |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 3148 | bool mute) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3149 | { |
| 3150 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3151 | int i; |
| 3152 | |
| 3153 | for (i = 0; i < num_pins; i++) { |
| 3154 | hda_nid_t nid = pins[i]; |
| 3155 | unsigned int val; |
| 3156 | if (!nid) |
| 3157 | break; |
| 3158 | /* don't reset VREF value in case it's controlling |
| 3159 | * the amp (see alc861_fixup_asus_amp_vref_0f()) |
| 3160 | */ |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 3161 | if (spec->keep_vref_in_automute) |
| 3162 | val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP; |
| 3163 | else |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3164 | val = 0; |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 3165 | if (!mute) |
| 3166 | val |= snd_hda_codec_get_pin_target(codec, nid); |
| 3167 | /* here we call update_pin_ctl() so that the pinctl is changed |
| 3168 | * without changing the pinctl target value; |
| 3169 | * the original target value will be still referred at the |
| 3170 | * init / resume again |
| 3171 | */ |
| 3172 | update_pin_ctl(codec, nid, val); |
Takashi Iwai | d5a9f1b | 2012-12-20 15:36:30 +0100 | [diff] [blame] | 3173 | set_pin_eapd(codec, nid, !mute); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3174 | } |
| 3175 | } |
| 3176 | |
| 3177 | /* Toggle outputs muting */ |
Takashi Iwai | 5d550e1 | 2012-12-19 15:16:44 +0100 | [diff] [blame] | 3178 | void snd_hda_gen_update_outputs(struct hda_codec *codec) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3179 | { |
| 3180 | struct hda_gen_spec *spec = codec->spec; |
| 3181 | int on; |
| 3182 | |
| 3183 | /* Control HP pins/amps depending on master_mute state; |
| 3184 | * in general, HP pins/amps control should be enabled in all cases, |
| 3185 | * but currently set only for master_mute, just to be safe |
| 3186 | */ |
| 3187 | if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */ |
| 3188 | do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins), |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 3189 | spec->autocfg.hp_pins, spec->master_mute); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3190 | |
| 3191 | if (!spec->automute_speaker) |
| 3192 | on = 0; |
| 3193 | else |
| 3194 | on = spec->hp_jack_present | spec->line_jack_present; |
| 3195 | on |= spec->master_mute; |
Takashi Iwai | 47b9ddb | 2013-01-16 18:18:00 +0100 | [diff] [blame] | 3196 | spec->speaker_muted = on; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3197 | do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins), |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 3198 | spec->autocfg.speaker_pins, on); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3199 | |
| 3200 | /* toggle line-out mutes if needed, too */ |
| 3201 | /* if LO is a copy of either HP or Speaker, don't need to handle it */ |
| 3202 | if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] || |
| 3203 | spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0]) |
| 3204 | return; |
| 3205 | if (!spec->automute_lo) |
| 3206 | on = 0; |
| 3207 | else |
| 3208 | on = spec->hp_jack_present; |
| 3209 | on |= spec->master_mute; |
Takashi Iwai | 47b9ddb | 2013-01-16 18:18:00 +0100 | [diff] [blame] | 3210 | spec->line_out_muted = on; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3211 | do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins), |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 3212 | spec->autocfg.line_out_pins, on); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3213 | } |
Takashi Iwai | 5d550e1 | 2012-12-19 15:16:44 +0100 | [diff] [blame] | 3214 | EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3215 | |
| 3216 | static void call_update_outputs(struct hda_codec *codec) |
| 3217 | { |
| 3218 | struct hda_gen_spec *spec = codec->spec; |
| 3219 | if (spec->automute_hook) |
| 3220 | spec->automute_hook(codec); |
| 3221 | else |
Takashi Iwai | 5d550e1 | 2012-12-19 15:16:44 +0100 | [diff] [blame] | 3222 | snd_hda_gen_update_outputs(codec); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3223 | } |
| 3224 | |
| 3225 | /* standard HP-automute helper */ |
Takashi Iwai | 5d550e1 | 2012-12-19 15:16:44 +0100 | [diff] [blame] | 3226 | void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3227 | { |
| 3228 | struct hda_gen_spec *spec = codec->spec; |
| 3229 | |
| 3230 | spec->hp_jack_present = |
| 3231 | detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins), |
| 3232 | spec->autocfg.hp_pins); |
| 3233 | if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo)) |
| 3234 | return; |
| 3235 | call_update_outputs(codec); |
| 3236 | } |
Takashi Iwai | 5d550e1 | 2012-12-19 15:16:44 +0100 | [diff] [blame] | 3237 | EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3238 | |
| 3239 | /* standard line-out-automute helper */ |
Takashi Iwai | 5d550e1 | 2012-12-19 15:16:44 +0100 | [diff] [blame] | 3240 | void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3241 | { |
| 3242 | struct hda_gen_spec *spec = codec->spec; |
| 3243 | |
| 3244 | if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT) |
| 3245 | return; |
| 3246 | /* check LO jack only when it's different from HP */ |
| 3247 | if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0]) |
| 3248 | return; |
| 3249 | |
| 3250 | spec->line_jack_present = |
| 3251 | detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins), |
| 3252 | spec->autocfg.line_out_pins); |
| 3253 | if (!spec->automute_speaker || !spec->detect_lo) |
| 3254 | return; |
| 3255 | call_update_outputs(codec); |
| 3256 | } |
Takashi Iwai | 5d550e1 | 2012-12-19 15:16:44 +0100 | [diff] [blame] | 3257 | EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3258 | |
| 3259 | /* standard mic auto-switch helper */ |
Takashi Iwai | 5d550e1 | 2012-12-19 15:16:44 +0100 | [diff] [blame] | 3260 | void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3261 | { |
| 3262 | struct hda_gen_spec *spec = codec->spec; |
| 3263 | int i; |
| 3264 | |
| 3265 | if (!spec->auto_mic) |
| 3266 | return; |
| 3267 | |
| 3268 | for (i = spec->am_num_entries - 1; i > 0; i--) { |
Takashi Iwai | 0b4df93 | 2013-01-10 09:45:13 +0100 | [diff] [blame] | 3269 | hda_nid_t pin = spec->am_entry[i].pin; |
| 3270 | /* don't detect pins retasked as outputs */ |
| 3271 | if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN) |
| 3272 | continue; |
| 3273 | if (snd_hda_jack_detect(codec, pin)) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3274 | mux_select(codec, 0, spec->am_entry[i].idx); |
| 3275 | return; |
| 3276 | } |
| 3277 | } |
| 3278 | mux_select(codec, 0, spec->am_entry[0].idx); |
| 3279 | } |
Takashi Iwai | 5d550e1 | 2012-12-19 15:16:44 +0100 | [diff] [blame] | 3280 | EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3281 | |
Takashi Iwai | a5cc250 | 2013-01-16 18:08:55 +0100 | [diff] [blame] | 3282 | /* update jack retasking */ |
| 3283 | static void update_automute_all(struct hda_codec *codec) |
| 3284 | { |
| 3285 | struct hda_gen_spec *spec = codec->spec; |
| 3286 | |
| 3287 | if (spec->hp_automute_hook) |
| 3288 | spec->hp_automute_hook(codec, NULL); |
| 3289 | else |
| 3290 | snd_hda_gen_hp_automute(codec, NULL); |
| 3291 | if (spec->line_automute_hook) |
| 3292 | spec->line_automute_hook(codec, NULL); |
| 3293 | else |
| 3294 | snd_hda_gen_line_automute(codec, NULL); |
| 3295 | if (spec->mic_autoswitch_hook) |
| 3296 | spec->mic_autoswitch_hook(codec, NULL); |
| 3297 | else |
| 3298 | snd_hda_gen_mic_autoswitch(codec, NULL); |
| 3299 | } |
| 3300 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3301 | /* |
| 3302 | * Auto-Mute mode mixer enum support |
| 3303 | */ |
| 3304 | static int automute_mode_info(struct snd_kcontrol *kcontrol, |
| 3305 | struct snd_ctl_elem_info *uinfo) |
| 3306 | { |
| 3307 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3308 | struct hda_gen_spec *spec = codec->spec; |
| 3309 | static const char * const texts3[] = { |
| 3310 | "Disabled", "Speaker Only", "Line Out+Speaker" |
Takashi Iwai | 071c73a | 2006-08-23 18:34:06 +0200 | [diff] [blame] | 3311 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3312 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3313 | if (spec->automute_speaker_possible && spec->automute_lo_possible) |
| 3314 | return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3); |
| 3315 | return snd_hda_enum_bool_helper_info(kcontrol, uinfo); |
| 3316 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3317 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3318 | static int automute_mode_get(struct snd_kcontrol *kcontrol, |
| 3319 | struct snd_ctl_elem_value *ucontrol) |
| 3320 | { |
| 3321 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3322 | struct hda_gen_spec *spec = codec->spec; |
| 3323 | unsigned int val = 0; |
| 3324 | if (spec->automute_speaker) |
| 3325 | val++; |
| 3326 | if (spec->automute_lo) |
| 3327 | val++; |
Takashi Iwai | 071c73a | 2006-08-23 18:34:06 +0200 | [diff] [blame] | 3328 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3329 | ucontrol->value.enumerated.item[0] = val; |
| 3330 | return 0; |
| 3331 | } |
| 3332 | |
| 3333 | static int automute_mode_put(struct snd_kcontrol *kcontrol, |
| 3334 | struct snd_ctl_elem_value *ucontrol) |
| 3335 | { |
| 3336 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
| 3337 | struct hda_gen_spec *spec = codec->spec; |
| 3338 | |
| 3339 | switch (ucontrol->value.enumerated.item[0]) { |
| 3340 | case 0: |
| 3341 | if (!spec->automute_speaker && !spec->automute_lo) |
| 3342 | return 0; |
| 3343 | spec->automute_speaker = 0; |
| 3344 | spec->automute_lo = 0; |
| 3345 | break; |
| 3346 | case 1: |
| 3347 | if (spec->automute_speaker_possible) { |
| 3348 | if (!spec->automute_lo && spec->automute_speaker) |
| 3349 | return 0; |
| 3350 | spec->automute_speaker = 1; |
| 3351 | spec->automute_lo = 0; |
| 3352 | } else if (spec->automute_lo_possible) { |
| 3353 | if (spec->automute_lo) |
| 3354 | return 0; |
| 3355 | spec->automute_lo = 1; |
| 3356 | } else |
| 3357 | return -EINVAL; |
| 3358 | break; |
| 3359 | case 2: |
| 3360 | if (!spec->automute_lo_possible || !spec->automute_speaker_possible) |
| 3361 | return -EINVAL; |
| 3362 | if (spec->automute_speaker && spec->automute_lo) |
| 3363 | return 0; |
| 3364 | spec->automute_speaker = 1; |
| 3365 | spec->automute_lo = 1; |
| 3366 | break; |
| 3367 | default: |
| 3368 | return -EINVAL; |
| 3369 | } |
| 3370 | call_update_outputs(codec); |
| 3371 | return 1; |
| 3372 | } |
| 3373 | |
| 3374 | static const struct snd_kcontrol_new automute_mode_enum = { |
| 3375 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 3376 | .name = "Auto-Mute Mode", |
| 3377 | .info = automute_mode_info, |
| 3378 | .get = automute_mode_get, |
| 3379 | .put = automute_mode_put, |
| 3380 | }; |
| 3381 | |
| 3382 | static int add_automute_mode_enum(struct hda_codec *codec) |
| 3383 | { |
| 3384 | struct hda_gen_spec *spec = codec->spec; |
| 3385 | |
Takashi Iwai | 12c93df | 2012-12-19 14:38:33 +0100 | [diff] [blame] | 3386 | if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum)) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3387 | return -ENOMEM; |
| 3388 | return 0; |
| 3389 | } |
| 3390 | |
| 3391 | /* |
| 3392 | * Check the availability of HP/line-out auto-mute; |
| 3393 | * Set up appropriately if really supported |
| 3394 | */ |
| 3395 | static int check_auto_mute_availability(struct hda_codec *codec) |
| 3396 | { |
| 3397 | struct hda_gen_spec *spec = codec->spec; |
| 3398 | struct auto_pin_cfg *cfg = &spec->autocfg; |
| 3399 | int present = 0; |
| 3400 | int i, err; |
| 3401 | |
Takashi Iwai | f72706b | 2013-01-16 18:20:07 +0100 | [diff] [blame] | 3402 | if (spec->suppress_auto_mute) |
| 3403 | return 0; |
| 3404 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3405 | if (cfg->hp_pins[0]) |
| 3406 | present++; |
| 3407 | if (cfg->line_out_pins[0]) |
| 3408 | present++; |
| 3409 | if (cfg->speaker_pins[0]) |
| 3410 | present++; |
| 3411 | if (present < 2) /* need two different output types */ |
Takashi Iwai | 071c73a | 2006-08-23 18:34:06 +0200 | [diff] [blame] | 3412 | return 0; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3413 | |
| 3414 | if (!cfg->speaker_pins[0] && |
| 3415 | cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) { |
| 3416 | memcpy(cfg->speaker_pins, cfg->line_out_pins, |
| 3417 | sizeof(cfg->speaker_pins)); |
| 3418 | cfg->speaker_outs = cfg->line_outs; |
Takashi Iwai | 071c73a | 2006-08-23 18:34:06 +0200 | [diff] [blame] | 3419 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3420 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3421 | if (!cfg->hp_pins[0] && |
| 3422 | cfg->line_out_type == AUTO_PIN_HP_OUT) { |
| 3423 | memcpy(cfg->hp_pins, cfg->line_out_pins, |
| 3424 | sizeof(cfg->hp_pins)); |
| 3425 | cfg->hp_outs = cfg->line_outs; |
| 3426 | } |
| 3427 | |
| 3428 | for (i = 0; i < cfg->hp_outs; i++) { |
| 3429 | hda_nid_t nid = cfg->hp_pins[i]; |
| 3430 | if (!is_jack_detectable(codec, nid)) |
| 3431 | continue; |
| 3432 | snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n", |
| 3433 | nid); |
| 3434 | snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT, |
Takashi Iwai | 2e03e95 | 2013-01-03 15:55:06 +0100 | [diff] [blame] | 3435 | spec->hp_automute_hook ? |
| 3436 | spec->hp_automute_hook : |
Takashi Iwai | 5d550e1 | 2012-12-19 15:16:44 +0100 | [diff] [blame] | 3437 | snd_hda_gen_hp_automute); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3438 | spec->detect_hp = 1; |
| 3439 | } |
| 3440 | |
| 3441 | if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) { |
| 3442 | if (cfg->speaker_outs) |
| 3443 | for (i = 0; i < cfg->line_outs; i++) { |
| 3444 | hda_nid_t nid = cfg->line_out_pins[i]; |
| 3445 | if (!is_jack_detectable(codec, nid)) |
| 3446 | continue; |
| 3447 | snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid); |
| 3448 | snd_hda_jack_detect_enable_callback(codec, nid, |
| 3449 | HDA_GEN_FRONT_EVENT, |
Takashi Iwai | 2e03e95 | 2013-01-03 15:55:06 +0100 | [diff] [blame] | 3450 | spec->line_automute_hook ? |
| 3451 | spec->line_automute_hook : |
Takashi Iwai | 5d550e1 | 2012-12-19 15:16:44 +0100 | [diff] [blame] | 3452 | snd_hda_gen_line_automute); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3453 | spec->detect_lo = 1; |
| 3454 | } |
| 3455 | spec->automute_lo_possible = spec->detect_hp; |
| 3456 | } |
| 3457 | |
| 3458 | spec->automute_speaker_possible = cfg->speaker_outs && |
| 3459 | (spec->detect_hp || spec->detect_lo); |
| 3460 | |
| 3461 | spec->automute_lo = spec->automute_lo_possible; |
| 3462 | spec->automute_speaker = spec->automute_speaker_possible; |
| 3463 | |
| 3464 | if (spec->automute_speaker_possible || spec->automute_lo_possible) { |
| 3465 | /* create a control for automute mode */ |
| 3466 | err = add_automute_mode_enum(codec); |
| 3467 | if (err < 0) |
| 3468 | return err; |
| 3469 | } |
| 3470 | return 0; |
| 3471 | } |
| 3472 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3473 | /* check whether all auto-mic pins are valid; setup indices if OK */ |
| 3474 | static bool auto_mic_check_imux(struct hda_codec *codec) |
| 3475 | { |
| 3476 | struct hda_gen_spec *spec = codec->spec; |
| 3477 | const struct hda_input_mux *imux; |
| 3478 | int i; |
| 3479 | |
| 3480 | imux = &spec->input_mux; |
| 3481 | for (i = 0; i < spec->am_num_entries; i++) { |
| 3482 | spec->am_entry[i].idx = |
| 3483 | find_idx_in_nid_list(spec->am_entry[i].pin, |
| 3484 | spec->imux_pins, imux->num_items); |
| 3485 | if (spec->am_entry[i].idx < 0) |
| 3486 | return false; /* no corresponding imux */ |
| 3487 | } |
| 3488 | |
| 3489 | /* we don't need the jack detection for the first pin */ |
| 3490 | for (i = 1; i < spec->am_num_entries; i++) |
| 3491 | snd_hda_jack_detect_enable_callback(codec, |
| 3492 | spec->am_entry[i].pin, |
| 3493 | HDA_GEN_MIC_EVENT, |
Takashi Iwai | 2e03e95 | 2013-01-03 15:55:06 +0100 | [diff] [blame] | 3494 | spec->mic_autoswitch_hook ? |
| 3495 | spec->mic_autoswitch_hook : |
Takashi Iwai | 5d550e1 | 2012-12-19 15:16:44 +0100 | [diff] [blame] | 3496 | snd_hda_gen_mic_autoswitch); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3497 | return true; |
| 3498 | } |
| 3499 | |
| 3500 | static int compare_attr(const void *ap, const void *bp) |
| 3501 | { |
| 3502 | const struct automic_entry *a = ap; |
| 3503 | const struct automic_entry *b = bp; |
| 3504 | return (int)(a->attr - b->attr); |
| 3505 | } |
| 3506 | |
| 3507 | /* |
| 3508 | * Check the availability of auto-mic switch; |
| 3509 | * Set up if really supported |
| 3510 | */ |
| 3511 | static int check_auto_mic_availability(struct hda_codec *codec) |
| 3512 | { |
| 3513 | struct hda_gen_spec *spec = codec->spec; |
| 3514 | struct auto_pin_cfg *cfg = &spec->autocfg; |
| 3515 | unsigned int types; |
| 3516 | int i, num_pins; |
| 3517 | |
Takashi Iwai | d12daf6 | 2013-01-07 16:32:11 +0100 | [diff] [blame] | 3518 | if (spec->suppress_auto_mic) |
| 3519 | return 0; |
| 3520 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3521 | types = 0; |
| 3522 | num_pins = 0; |
| 3523 | for (i = 0; i < cfg->num_inputs; i++) { |
| 3524 | hda_nid_t nid = cfg->inputs[i].pin; |
| 3525 | unsigned int attr; |
| 3526 | attr = snd_hda_codec_get_pincfg(codec, nid); |
| 3527 | attr = snd_hda_get_input_pin_attr(attr); |
| 3528 | if (types & (1 << attr)) |
| 3529 | return 0; /* already occupied */ |
| 3530 | switch (attr) { |
| 3531 | case INPUT_PIN_ATTR_INT: |
| 3532 | if (cfg->inputs[i].type != AUTO_PIN_MIC) |
| 3533 | return 0; /* invalid type */ |
| 3534 | break; |
| 3535 | case INPUT_PIN_ATTR_UNUSED: |
| 3536 | return 0; /* invalid entry */ |
| 3537 | default: |
| 3538 | if (cfg->inputs[i].type > AUTO_PIN_LINE_IN) |
| 3539 | return 0; /* invalid type */ |
| 3540 | if (!spec->line_in_auto_switch && |
| 3541 | cfg->inputs[i].type != AUTO_PIN_MIC) |
| 3542 | return 0; /* only mic is allowed */ |
| 3543 | if (!is_jack_detectable(codec, nid)) |
| 3544 | return 0; /* no unsol support */ |
| 3545 | break; |
| 3546 | } |
| 3547 | if (num_pins >= MAX_AUTO_MIC_PINS) |
| 3548 | return 0; |
| 3549 | types |= (1 << attr); |
| 3550 | spec->am_entry[num_pins].pin = nid; |
| 3551 | spec->am_entry[num_pins].attr = attr; |
| 3552 | num_pins++; |
| 3553 | } |
| 3554 | |
| 3555 | if (num_pins < 2) |
| 3556 | return 0; |
| 3557 | |
| 3558 | spec->am_num_entries = num_pins; |
| 3559 | /* sort the am_entry in the order of attr so that the pin with a |
| 3560 | * higher attr will be selected when the jack is plugged. |
| 3561 | */ |
| 3562 | sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]), |
| 3563 | compare_attr, NULL); |
| 3564 | |
| 3565 | if (!auto_mic_check_imux(codec)) |
| 3566 | return 0; |
| 3567 | |
| 3568 | spec->auto_mic = 1; |
| 3569 | spec->num_adc_nids = 1; |
| 3570 | spec->cur_mux[0] = spec->am_entry[0].idx; |
| 3571 | snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n", |
| 3572 | spec->am_entry[0].pin, |
| 3573 | spec->am_entry[1].pin, |
| 3574 | spec->am_entry[2].pin); |
| 3575 | |
| 3576 | return 0; |
| 3577 | } |
| 3578 | |
| 3579 | |
Takashi Iwai | 9eb413e | 2012-12-19 14:41:21 +0100 | [diff] [blame] | 3580 | /* |
| 3581 | * Parse the given BIOS configuration and set up the hda_gen_spec |
| 3582 | * |
| 3583 | * return 1 if successful, 0 if the proper config is not found, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3584 | * or a negative error code |
| 3585 | */ |
| 3586 | int snd_hda_gen_parse_auto_config(struct hda_codec *codec, |
Takashi Iwai | 9eb413e | 2012-12-19 14:41:21 +0100 | [diff] [blame] | 3587 | struct auto_pin_cfg *cfg) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3588 | { |
| 3589 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3590 | int err; |
| 3591 | |
Takashi Iwai | 1c70a58 | 2013-01-11 17:48:22 +0100 | [diff] [blame] | 3592 | parse_user_hints(codec); |
| 3593 | |
Takashi Iwai | 9eb413e | 2012-12-19 14:41:21 +0100 | [diff] [blame] | 3594 | if (cfg != &spec->autocfg) { |
| 3595 | spec->autocfg = *cfg; |
| 3596 | cfg = &spec->autocfg; |
| 3597 | } |
| 3598 | |
David Henningsson | 6fc4cb9 | 2013-01-16 15:58:45 +0100 | [diff] [blame] | 3599 | fill_all_dac_nids(codec); |
| 3600 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3601 | if (!cfg->line_outs) { |
| 3602 | if (cfg->dig_outs || cfg->dig_in_pin) { |
| 3603 | spec->multiout.max_channels = 2; |
| 3604 | spec->no_analog = 1; |
| 3605 | goto dig_only; |
| 3606 | } |
| 3607 | return 0; /* can't find valid BIOS pin config */ |
| 3608 | } |
| 3609 | |
| 3610 | if (!spec->no_primary_hp && |
| 3611 | cfg->line_out_type == AUTO_PIN_SPEAKER_OUT && |
| 3612 | cfg->line_outs <= cfg->hp_outs) { |
| 3613 | /* use HP as primary out */ |
| 3614 | cfg->speaker_outs = cfg->line_outs; |
| 3615 | memcpy(cfg->speaker_pins, cfg->line_out_pins, |
| 3616 | sizeof(cfg->speaker_pins)); |
| 3617 | cfg->line_outs = cfg->hp_outs; |
| 3618 | memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins)); |
| 3619 | cfg->hp_outs = 0; |
| 3620 | memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins)); |
| 3621 | cfg->line_out_type = AUTO_PIN_HP_OUT; |
| 3622 | } |
| 3623 | |
| 3624 | err = parse_output_paths(codec); |
| 3625 | if (err < 0) |
| 3626 | return err; |
| 3627 | err = create_multi_channel_mode(codec); |
| 3628 | if (err < 0) |
| 3629 | return err; |
| 3630 | err = create_multi_out_ctls(codec, cfg); |
| 3631 | if (err < 0) |
| 3632 | return err; |
| 3633 | err = create_hp_out_ctls(codec); |
| 3634 | if (err < 0) |
| 3635 | return err; |
| 3636 | err = create_speaker_out_ctls(codec); |
| 3637 | if (err < 0) |
| 3638 | return err; |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 3639 | err = create_indep_hp_ctls(codec); |
| 3640 | if (err < 0) |
| 3641 | return err; |
Takashi Iwai | c30aa7b | 2013-01-04 16:42:48 +0100 | [diff] [blame] | 3642 | err = create_loopback_mixing_ctl(codec); |
| 3643 | if (err < 0) |
| 3644 | return err; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3645 | err = create_shared_input(codec); |
| 3646 | if (err < 0) |
| 3647 | return err; |
| 3648 | err = create_input_ctls(codec); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3649 | if (err < 0) |
Takashi Iwai | 071c73a | 2006-08-23 18:34:06 +0200 | [diff] [blame] | 3650 | return err; |
| 3651 | |
Takashi Iwai | a07a949 | 2013-01-07 16:44:06 +0100 | [diff] [blame] | 3652 | spec->const_channel_count = spec->ext_channel_count; |
| 3653 | /* check the multiple speaker and headphone pins */ |
| 3654 | if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) |
| 3655 | spec->const_channel_count = max(spec->const_channel_count, |
| 3656 | cfg->speaker_outs * 2); |
| 3657 | if (cfg->line_out_type != AUTO_PIN_HP_OUT) |
| 3658 | spec->const_channel_count = max(spec->const_channel_count, |
| 3659 | cfg->hp_outs * 2); |
| 3660 | spec->multiout.max_channels = max(spec->ext_channel_count, |
| 3661 | spec->const_channel_count); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3662 | |
| 3663 | err = check_auto_mute_availability(codec); |
| 3664 | if (err < 0) |
| 3665 | return err; |
| 3666 | |
| 3667 | err = check_dyn_adc_switch(codec); |
| 3668 | if (err < 0) |
| 3669 | return err; |
| 3670 | |
| 3671 | if (!spec->shared_mic_hp) { |
| 3672 | err = check_auto_mic_availability(codec); |
Takashi Iwai | d13bd41 | 2008-07-30 15:01:45 +0200 | [diff] [blame] | 3673 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3674 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3675 | } |
Takashi Iwai | 071c73a | 2006-08-23 18:34:06 +0200 | [diff] [blame] | 3676 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3677 | err = create_capture_mixers(codec); |
| 3678 | if (err < 0) |
| 3679 | return err; |
| 3680 | |
| 3681 | err = parse_mic_boost(codec); |
| 3682 | if (err < 0) |
| 3683 | return err; |
| 3684 | |
Takashi Iwai | 978e77e | 2013-01-10 16:57:58 +0100 | [diff] [blame] | 3685 | if (spec->add_out_jack_modes) { |
| 3686 | if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) { |
| 3687 | err = create_out_jack_modes(codec, cfg->line_outs, |
| 3688 | cfg->line_out_pins); |
| 3689 | if (err < 0) |
| 3690 | return err; |
| 3691 | } |
| 3692 | if (cfg->line_out_type != AUTO_PIN_HP_OUT) { |
| 3693 | err = create_out_jack_modes(codec, cfg->hp_outs, |
| 3694 | cfg->hp_pins); |
| 3695 | if (err < 0) |
| 3696 | return err; |
| 3697 | } |
| 3698 | } |
| 3699 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3700 | dig_only: |
| 3701 | parse_digital(codec); |
| 3702 | |
| 3703 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3705 | EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3706 | |
| 3707 | |
| 3708 | /* |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3709 | * Build control elements |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3710 | */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3711 | |
| 3712 | /* slave controls for virtual master */ |
| 3713 | static const char * const slave_pfxs[] = { |
| 3714 | "Front", "Surround", "Center", "LFE", "Side", |
| 3715 | "Headphone", "Speaker", "Mono", "Line Out", |
| 3716 | "CLFE", "Bass Speaker", "PCM", |
Takashi Iwai | ee79c69 | 2013-01-07 09:57:42 +0100 | [diff] [blame] | 3717 | "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side", |
| 3718 | "Headphone Front", "Headphone Surround", "Headphone CLFE", |
| 3719 | "Headphone Side", |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3720 | NULL, |
| 3721 | }; |
| 3722 | |
| 3723 | int snd_hda_gen_build_controls(struct hda_codec *codec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3724 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3725 | struct hda_gen_spec *spec = codec->spec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3726 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3727 | |
Takashi Iwai | 36502d0 | 2012-12-19 15:15:10 +0100 | [diff] [blame] | 3728 | if (spec->kctls.used) { |
| 3729 | err = snd_hda_add_new_ctls(codec, spec->kctls.list); |
| 3730 | if (err < 0) |
| 3731 | return err; |
| 3732 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3733 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3734 | if (spec->multiout.dig_out_nid) { |
| 3735 | err = snd_hda_create_dig_out_ctls(codec, |
| 3736 | spec->multiout.dig_out_nid, |
| 3737 | spec->multiout.dig_out_nid, |
| 3738 | spec->pcm_rec[1].pcm_type); |
| 3739 | if (err < 0) |
| 3740 | return err; |
| 3741 | if (!spec->no_analog) { |
| 3742 | err = snd_hda_create_spdif_share_sw(codec, |
| 3743 | &spec->multiout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3744 | if (err < 0) |
| 3745 | return err; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3746 | spec->multiout.share_spdif = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3747 | } |
| 3748 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3749 | if (spec->dig_in_nid) { |
| 3750 | err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid); |
| 3751 | if (err < 0) |
| 3752 | return err; |
| 3753 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3754 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3755 | /* if we have no master control, let's create it */ |
| 3756 | if (!spec->no_analog && |
| 3757 | !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3758 | err = snd_hda_add_vmaster(codec, "Master Playback Volume", |
Takashi Iwai | 7a71bbf | 2013-01-17 10:25:15 +0100 | [diff] [blame^] | 3759 | spec->vmaster_tlv, slave_pfxs, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3760 | "Playback Volume"); |
| 3761 | if (err < 0) |
| 3762 | return err; |
| 3763 | } |
| 3764 | if (!spec->no_analog && |
| 3765 | !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) { |
| 3766 | err = __snd_hda_add_vmaster(codec, "Master Playback Switch", |
| 3767 | NULL, slave_pfxs, |
| 3768 | "Playback Switch", |
| 3769 | true, &spec->vmaster_mute.sw_kctl); |
| 3770 | if (err < 0) |
| 3771 | return err; |
| 3772 | if (spec->vmaster_mute.hook) |
Takashi Iwai | fd25a97 | 2012-12-20 14:57:18 +0100 | [diff] [blame] | 3773 | snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute, |
| 3774 | spec->vmaster_mute_enum); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3775 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3777 | free_kctls(spec); /* no longer needed */ |
| 3778 | |
| 3779 | if (spec->shared_mic_hp) { |
| 3780 | int err; |
| 3781 | int nid = spec->autocfg.inputs[1].pin; |
| 3782 | err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0); |
| 3783 | if (err < 0) |
| 3784 | return err; |
| 3785 | err = snd_hda_jack_detect_enable(codec, nid, 0); |
| 3786 | if (err < 0) |
| 3787 | return err; |
| 3788 | } |
| 3789 | |
| 3790 | err = snd_hda_jack_add_kctls(codec, &spec->autocfg); |
| 3791 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3792 | return err; |
| 3793 | |
| 3794 | return 0; |
| 3795 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3796 | EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls); |
| 3797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3798 | |
| 3799 | /* |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3800 | * PCM definitions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3801 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3802 | |
Takashi Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 3803 | static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo, |
| 3804 | struct hda_codec *codec, |
| 3805 | struct snd_pcm_substream *substream, |
| 3806 | int action) |
| 3807 | { |
| 3808 | struct hda_gen_spec *spec = codec->spec; |
| 3809 | if (spec->pcm_playback_hook) |
| 3810 | spec->pcm_playback_hook(hinfo, codec, substream, action); |
| 3811 | } |
| 3812 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3813 | /* |
| 3814 | * Analog playback callbacks |
| 3815 | */ |
| 3816 | static int playback_pcm_open(struct hda_pcm_stream *hinfo, |
| 3817 | struct hda_codec *codec, |
| 3818 | struct snd_pcm_substream *substream) |
| 3819 | { |
| 3820 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 3821 | int err; |
| 3822 | |
| 3823 | mutex_lock(&spec->pcm_mutex); |
| 3824 | err = snd_hda_multi_out_analog_open(codec, |
| 3825 | &spec->multiout, substream, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3826 | hinfo); |
Takashi Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 3827 | if (!err) { |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 3828 | spec->active_streams |= 1 << STREAM_MULTI_OUT; |
Takashi Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 3829 | call_pcm_playback_hook(hinfo, codec, substream, |
| 3830 | HDA_GEN_PCM_ACT_OPEN); |
| 3831 | } |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 3832 | mutex_unlock(&spec->pcm_mutex); |
| 3833 | return err; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3834 | } |
| 3835 | |
| 3836 | static int playback_pcm_prepare(struct hda_pcm_stream *hinfo, |
Takashi Iwai | 97ec558 | 2006-03-21 11:29:07 +0100 | [diff] [blame] | 3837 | struct hda_codec *codec, |
| 3838 | unsigned int stream_tag, |
| 3839 | unsigned int format, |
| 3840 | struct snd_pcm_substream *substream) |
| 3841 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3842 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 3843 | int err; |
| 3844 | |
| 3845 | err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout, |
| 3846 | stream_tag, format, substream); |
| 3847 | if (!err) |
| 3848 | call_pcm_playback_hook(hinfo, codec, substream, |
| 3849 | HDA_GEN_PCM_ACT_PREPARE); |
| 3850 | return err; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3851 | } |
Takashi Iwai | 97ec558 | 2006-03-21 11:29:07 +0100 | [diff] [blame] | 3852 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3853 | static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo, |
| 3854 | struct hda_codec *codec, |
| 3855 | struct snd_pcm_substream *substream) |
| 3856 | { |
| 3857 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 3858 | int err; |
| 3859 | |
| 3860 | err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout); |
| 3861 | if (!err) |
| 3862 | call_pcm_playback_hook(hinfo, codec, substream, |
| 3863 | HDA_GEN_PCM_ACT_CLEANUP); |
| 3864 | return err; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3865 | } |
| 3866 | |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 3867 | static int playback_pcm_close(struct hda_pcm_stream *hinfo, |
| 3868 | struct hda_codec *codec, |
| 3869 | struct snd_pcm_substream *substream) |
| 3870 | { |
| 3871 | struct hda_gen_spec *spec = codec->spec; |
| 3872 | mutex_lock(&spec->pcm_mutex); |
| 3873 | spec->active_streams &= ~(1 << STREAM_MULTI_OUT); |
Takashi Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 3874 | call_pcm_playback_hook(hinfo, codec, substream, |
| 3875 | HDA_GEN_PCM_ACT_CLOSE); |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 3876 | mutex_unlock(&spec->pcm_mutex); |
| 3877 | return 0; |
| 3878 | } |
| 3879 | |
| 3880 | static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo, |
| 3881 | struct hda_codec *codec, |
| 3882 | struct snd_pcm_substream *substream) |
| 3883 | { |
| 3884 | struct hda_gen_spec *spec = codec->spec; |
| 3885 | int err = 0; |
| 3886 | |
| 3887 | mutex_lock(&spec->pcm_mutex); |
| 3888 | if (!spec->indep_hp_enabled) |
| 3889 | err = -EBUSY; |
| 3890 | else |
| 3891 | spec->active_streams |= 1 << STREAM_INDEP_HP; |
Takashi Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 3892 | call_pcm_playback_hook(hinfo, codec, substream, |
| 3893 | HDA_GEN_PCM_ACT_OPEN); |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 3894 | mutex_unlock(&spec->pcm_mutex); |
| 3895 | return err; |
| 3896 | } |
| 3897 | |
| 3898 | static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo, |
| 3899 | struct hda_codec *codec, |
| 3900 | struct snd_pcm_substream *substream) |
| 3901 | { |
| 3902 | struct hda_gen_spec *spec = codec->spec; |
| 3903 | mutex_lock(&spec->pcm_mutex); |
| 3904 | spec->active_streams &= ~(1 << STREAM_INDEP_HP); |
Takashi Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 3905 | call_pcm_playback_hook(hinfo, codec, substream, |
| 3906 | HDA_GEN_PCM_ACT_CLOSE); |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 3907 | mutex_unlock(&spec->pcm_mutex); |
| 3908 | return 0; |
| 3909 | } |
| 3910 | |
Takashi Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 3911 | static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo, |
| 3912 | struct hda_codec *codec, |
| 3913 | unsigned int stream_tag, |
| 3914 | unsigned int format, |
| 3915 | struct snd_pcm_substream *substream) |
| 3916 | { |
| 3917 | snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); |
| 3918 | call_pcm_playback_hook(hinfo, codec, substream, |
| 3919 | HDA_GEN_PCM_ACT_PREPARE); |
| 3920 | return 0; |
| 3921 | } |
| 3922 | |
| 3923 | static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo, |
| 3924 | struct hda_codec *codec, |
| 3925 | struct snd_pcm_substream *substream) |
| 3926 | { |
| 3927 | snd_hda_codec_cleanup_stream(codec, hinfo->nid); |
| 3928 | call_pcm_playback_hook(hinfo, codec, substream, |
| 3929 | HDA_GEN_PCM_ACT_CLEANUP); |
| 3930 | return 0; |
| 3931 | } |
| 3932 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3933 | /* |
| 3934 | * Digital out |
| 3935 | */ |
| 3936 | static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo, |
| 3937 | struct hda_codec *codec, |
| 3938 | struct snd_pcm_substream *substream) |
| 3939 | { |
| 3940 | struct hda_gen_spec *spec = codec->spec; |
| 3941 | return snd_hda_multi_out_dig_open(codec, &spec->multiout); |
| 3942 | } |
| 3943 | |
| 3944 | static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo, |
| 3945 | struct hda_codec *codec, |
| 3946 | unsigned int stream_tag, |
| 3947 | unsigned int format, |
| 3948 | struct snd_pcm_substream *substream) |
| 3949 | { |
| 3950 | struct hda_gen_spec *spec = codec->spec; |
| 3951 | return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, |
| 3952 | stream_tag, format, substream); |
| 3953 | } |
| 3954 | |
| 3955 | static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo, |
| 3956 | struct hda_codec *codec, |
| 3957 | struct snd_pcm_substream *substream) |
| 3958 | { |
| 3959 | struct hda_gen_spec *spec = codec->spec; |
| 3960 | return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout); |
| 3961 | } |
| 3962 | |
| 3963 | static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo, |
| 3964 | struct hda_codec *codec, |
| 3965 | struct snd_pcm_substream *substream) |
| 3966 | { |
| 3967 | struct hda_gen_spec *spec = codec->spec; |
| 3968 | return snd_hda_multi_out_dig_close(codec, &spec->multiout); |
| 3969 | } |
| 3970 | |
| 3971 | /* |
| 3972 | * Analog capture |
| 3973 | */ |
| 3974 | static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo, |
| 3975 | struct hda_codec *codec, |
| 3976 | unsigned int stream_tag, |
| 3977 | unsigned int format, |
| 3978 | struct snd_pcm_substream *substream) |
| 3979 | { |
| 3980 | struct hda_gen_spec *spec = codec->spec; |
| 3981 | |
| 3982 | snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1], |
Takashi Iwai | 97ec558 | 2006-03-21 11:29:07 +0100 | [diff] [blame] | 3983 | stream_tag, 0, format); |
| 3984 | return 0; |
| 3985 | } |
| 3986 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3987 | static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo, |
| 3988 | struct hda_codec *codec, |
| 3989 | struct snd_pcm_substream *substream) |
Takashi Iwai | 97ec558 | 2006-03-21 11:29:07 +0100 | [diff] [blame] | 3990 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3991 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 97ec558 | 2006-03-21 11:29:07 +0100 | [diff] [blame] | 3992 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3993 | snd_hda_codec_cleanup_stream(codec, |
| 3994 | spec->adc_nids[substream->number + 1]); |
Takashi Iwai | 97ec558 | 2006-03-21 11:29:07 +0100 | [diff] [blame] | 3995 | return 0; |
| 3996 | } |
| 3997 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 3998 | /* |
| 3999 | */ |
| 4000 | static const struct hda_pcm_stream pcm_analog_playback = { |
| 4001 | .substreams = 1, |
| 4002 | .channels_min = 2, |
| 4003 | .channels_max = 8, |
| 4004 | /* NID is set in build_pcms */ |
| 4005 | .ops = { |
| 4006 | .open = playback_pcm_open, |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 4007 | .close = playback_pcm_close, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4008 | .prepare = playback_pcm_prepare, |
| 4009 | .cleanup = playback_pcm_cleanup |
| 4010 | }, |
| 4011 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4012 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4013 | static const struct hda_pcm_stream pcm_analog_capture = { |
| 4014 | .substreams = 1, |
| 4015 | .channels_min = 2, |
| 4016 | .channels_max = 2, |
| 4017 | /* NID is set in build_pcms */ |
| 4018 | }; |
| 4019 | |
| 4020 | static const struct hda_pcm_stream pcm_analog_alt_playback = { |
| 4021 | .substreams = 1, |
| 4022 | .channels_min = 2, |
| 4023 | .channels_max = 2, |
| 4024 | /* NID is set in build_pcms */ |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 4025 | .ops = { |
| 4026 | .open = alt_playback_pcm_open, |
Takashi Iwai | e6b85f3 | 2013-01-07 11:54:34 +0100 | [diff] [blame] | 4027 | .close = alt_playback_pcm_close, |
| 4028 | .prepare = alt_playback_pcm_prepare, |
| 4029 | .cleanup = alt_playback_pcm_cleanup |
Takashi Iwai | 38cf6f1 | 2012-12-21 14:09:42 +0100 | [diff] [blame] | 4030 | }, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4031 | }; |
| 4032 | |
| 4033 | static const struct hda_pcm_stream pcm_analog_alt_capture = { |
| 4034 | .substreams = 2, /* can be overridden */ |
| 4035 | .channels_min = 2, |
| 4036 | .channels_max = 2, |
| 4037 | /* NID is set in build_pcms */ |
| 4038 | .ops = { |
| 4039 | .prepare = alt_capture_pcm_prepare, |
| 4040 | .cleanup = alt_capture_pcm_cleanup |
| 4041 | }, |
| 4042 | }; |
| 4043 | |
| 4044 | static const struct hda_pcm_stream pcm_digital_playback = { |
| 4045 | .substreams = 1, |
| 4046 | .channels_min = 2, |
| 4047 | .channels_max = 2, |
| 4048 | /* NID is set in build_pcms */ |
| 4049 | .ops = { |
| 4050 | .open = dig_playback_pcm_open, |
| 4051 | .close = dig_playback_pcm_close, |
| 4052 | .prepare = dig_playback_pcm_prepare, |
| 4053 | .cleanup = dig_playback_pcm_cleanup |
| 4054 | }, |
| 4055 | }; |
| 4056 | |
| 4057 | static const struct hda_pcm_stream pcm_digital_capture = { |
| 4058 | .substreams = 1, |
| 4059 | .channels_min = 2, |
| 4060 | .channels_max = 2, |
| 4061 | /* NID is set in build_pcms */ |
| 4062 | }; |
| 4063 | |
| 4064 | /* Used by build_pcms to flag that a PCM has no playback stream */ |
| 4065 | static const struct hda_pcm_stream pcm_null_stream = { |
| 4066 | .substreams = 0, |
| 4067 | .channels_min = 0, |
| 4068 | .channels_max = 0, |
| 4069 | }; |
| 4070 | |
| 4071 | /* |
| 4072 | * dynamic changing ADC PCM streams |
| 4073 | */ |
| 4074 | static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur) |
| 4075 | { |
| 4076 | struct hda_gen_spec *spec = codec->spec; |
| 4077 | hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]]; |
| 4078 | |
| 4079 | if (spec->cur_adc && spec->cur_adc != new_adc) { |
| 4080 | /* stream is running, let's swap the current ADC */ |
| 4081 | __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1); |
| 4082 | spec->cur_adc = new_adc; |
| 4083 | snd_hda_codec_setup_stream(codec, new_adc, |
| 4084 | spec->cur_adc_stream_tag, 0, |
| 4085 | spec->cur_adc_format); |
| 4086 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4087 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4088 | return false; |
| 4089 | } |
| 4090 | |
| 4091 | /* analog capture with dynamic dual-adc changes */ |
| 4092 | static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo, |
| 4093 | struct hda_codec *codec, |
| 4094 | unsigned int stream_tag, |
| 4095 | unsigned int format, |
| 4096 | struct snd_pcm_substream *substream) |
| 4097 | { |
| 4098 | struct hda_gen_spec *spec = codec->spec; |
| 4099 | spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]]; |
| 4100 | spec->cur_adc_stream_tag = stream_tag; |
| 4101 | spec->cur_adc_format = format; |
| 4102 | snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format); |
| 4103 | return 0; |
| 4104 | } |
| 4105 | |
| 4106 | static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo, |
| 4107 | struct hda_codec *codec, |
| 4108 | struct snd_pcm_substream *substream) |
| 4109 | { |
| 4110 | struct hda_gen_spec *spec = codec->spec; |
| 4111 | snd_hda_codec_cleanup_stream(codec, spec->cur_adc); |
| 4112 | spec->cur_adc = 0; |
| 4113 | return 0; |
| 4114 | } |
| 4115 | |
| 4116 | static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = { |
| 4117 | .substreams = 1, |
| 4118 | .channels_min = 2, |
| 4119 | .channels_max = 2, |
| 4120 | .nid = 0, /* fill later */ |
| 4121 | .ops = { |
| 4122 | .prepare = dyn_adc_capture_pcm_prepare, |
| 4123 | .cleanup = dyn_adc_capture_pcm_cleanup |
| 4124 | }, |
| 4125 | }; |
| 4126 | |
Takashi Iwai | f873e53 | 2012-12-20 16:58:39 +0100 | [diff] [blame] | 4127 | static void fill_pcm_stream_name(char *str, size_t len, const char *sfx, |
| 4128 | const char *chip_name) |
| 4129 | { |
| 4130 | char *p; |
| 4131 | |
| 4132 | if (*str) |
| 4133 | return; |
| 4134 | strlcpy(str, chip_name, len); |
| 4135 | |
| 4136 | /* drop non-alnum chars after a space */ |
| 4137 | for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) { |
| 4138 | if (!isalnum(p[1])) { |
| 4139 | *p = 0; |
| 4140 | break; |
| 4141 | } |
| 4142 | } |
| 4143 | strlcat(str, sfx, len); |
| 4144 | } |
| 4145 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4146 | /* build PCM streams based on the parsed results */ |
| 4147 | int snd_hda_gen_build_pcms(struct hda_codec *codec) |
| 4148 | { |
| 4149 | struct hda_gen_spec *spec = codec->spec; |
| 4150 | struct hda_pcm *info = spec->pcm_rec; |
| 4151 | const struct hda_pcm_stream *p; |
| 4152 | bool have_multi_adcs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4153 | |
| 4154 | codec->num_pcms = 1; |
| 4155 | codec->pcm_info = info; |
| 4156 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4157 | if (spec->no_analog) |
| 4158 | goto skip_analog; |
| 4159 | |
Takashi Iwai | f873e53 | 2012-12-20 16:58:39 +0100 | [diff] [blame] | 4160 | fill_pcm_stream_name(spec->stream_name_analog, |
| 4161 | sizeof(spec->stream_name_analog), |
| 4162 | " Analog", codec->chip_name); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4163 | info->name = spec->stream_name_analog; |
| 4164 | |
| 4165 | if (spec->multiout.num_dacs > 0) { |
| 4166 | p = spec->stream_analog_playback; |
| 4167 | if (!p) |
| 4168 | p = &pcm_analog_playback; |
| 4169 | info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p; |
| 4170 | info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0]; |
| 4171 | info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = |
| 4172 | spec->multiout.max_channels; |
| 4173 | if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT && |
| 4174 | spec->autocfg.line_outs == 2) |
| 4175 | info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap = |
| 4176 | snd_pcm_2_1_chmaps; |
| 4177 | } |
| 4178 | if (spec->num_adc_nids) { |
| 4179 | p = spec->stream_analog_capture; |
| 4180 | if (!p) { |
| 4181 | if (spec->dyn_adc_switch) |
| 4182 | p = &dyn_adc_pcm_analog_capture; |
| 4183 | else |
| 4184 | p = &pcm_analog_capture; |
| 4185 | } |
| 4186 | info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p; |
| 4187 | info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0]; |
| 4188 | } |
| 4189 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4190 | skip_analog: |
| 4191 | /* SPDIF for stream index #1 */ |
| 4192 | if (spec->multiout.dig_out_nid || spec->dig_in_nid) { |
Takashi Iwai | f873e53 | 2012-12-20 16:58:39 +0100 | [diff] [blame] | 4193 | fill_pcm_stream_name(spec->stream_name_digital, |
| 4194 | sizeof(spec->stream_name_digital), |
| 4195 | " Digital", codec->chip_name); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4196 | codec->num_pcms = 2; |
| 4197 | codec->slave_dig_outs = spec->multiout.slave_dig_outs; |
| 4198 | info = spec->pcm_rec + 1; |
| 4199 | info->name = spec->stream_name_digital; |
| 4200 | if (spec->dig_out_type) |
| 4201 | info->pcm_type = spec->dig_out_type; |
| 4202 | else |
| 4203 | info->pcm_type = HDA_PCM_TYPE_SPDIF; |
| 4204 | if (spec->multiout.dig_out_nid) { |
| 4205 | p = spec->stream_digital_playback; |
| 4206 | if (!p) |
| 4207 | p = &pcm_digital_playback; |
| 4208 | info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p; |
| 4209 | info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid; |
| 4210 | } |
| 4211 | if (spec->dig_in_nid) { |
| 4212 | p = spec->stream_digital_capture; |
| 4213 | if (!p) |
| 4214 | p = &pcm_digital_capture; |
| 4215 | info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p; |
| 4216 | info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid; |
| 4217 | } |
| 4218 | } |
| 4219 | |
| 4220 | if (spec->no_analog) |
| 4221 | return 0; |
| 4222 | |
| 4223 | /* If the use of more than one ADC is requested for the current |
| 4224 | * model, configure a second analog capture-only PCM. |
| 4225 | */ |
| 4226 | have_multi_adcs = (spec->num_adc_nids > 1) && |
| 4227 | !spec->dyn_adc_switch && !spec->auto_mic; |
| 4228 | /* Additional Analaog capture for index #2 */ |
| 4229 | if (spec->alt_dac_nid || have_multi_adcs) { |
| 4230 | codec->num_pcms = 3; |
| 4231 | info = spec->pcm_rec + 2; |
| 4232 | info->name = spec->stream_name_analog; |
| 4233 | if (spec->alt_dac_nid) { |
| 4234 | p = spec->stream_analog_alt_playback; |
| 4235 | if (!p) |
| 4236 | p = &pcm_analog_alt_playback; |
| 4237 | info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p; |
| 4238 | info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = |
| 4239 | spec->alt_dac_nid; |
| 4240 | } else { |
| 4241 | info->stream[SNDRV_PCM_STREAM_PLAYBACK] = |
| 4242 | pcm_null_stream; |
| 4243 | info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0; |
| 4244 | } |
| 4245 | if (have_multi_adcs) { |
| 4246 | p = spec->stream_analog_alt_capture; |
| 4247 | if (!p) |
| 4248 | p = &pcm_analog_alt_capture; |
| 4249 | info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p; |
| 4250 | info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = |
| 4251 | spec->adc_nids[1]; |
| 4252 | info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = |
| 4253 | spec->num_adc_nids - 1; |
| 4254 | } else { |
| 4255 | info->stream[SNDRV_PCM_STREAM_CAPTURE] = |
| 4256 | pcm_null_stream; |
| 4257 | info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0; |
| 4258 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4259 | } |
| 4260 | |
| 4261 | return 0; |
| 4262 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4263 | EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms); |
| 4264 | |
| 4265 | |
| 4266 | /* |
| 4267 | * Standard auto-parser initializations |
| 4268 | */ |
| 4269 | |
Takashi Iwai | d415693 | 2013-01-07 10:08:02 +0100 | [diff] [blame] | 4270 | /* configure the given path as a proper output */ |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 4271 | 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] | 4272 | { |
| 4273 | struct nid_path *path; |
Takashi Iwai | d415693 | 2013-01-07 10:08:02 +0100 | [diff] [blame] | 4274 | hda_nid_t pin; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4275 | |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 4276 | path = snd_hda_get_path_from_idx(codec, path_idx); |
Takashi Iwai | d415693 | 2013-01-07 10:08:02 +0100 | [diff] [blame] | 4277 | if (!path || !path->depth) |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4278 | return; |
Takashi Iwai | d415693 | 2013-01-07 10:08:02 +0100 | [diff] [blame] | 4279 | pin = path->path[path->depth - 1]; |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 4280 | restore_pin_ctl(codec, pin); |
Takashi Iwai | e1284af | 2013-01-03 16:33:02 +0100 | [diff] [blame] | 4281 | snd_hda_activate_path(codec, path, path->active, true); |
| 4282 | set_pin_eapd(codec, pin, path->active); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4283 | } |
| 4284 | |
| 4285 | /* initialize primary output paths */ |
| 4286 | static void init_multi_out(struct hda_codec *codec) |
| 4287 | { |
| 4288 | struct hda_gen_spec *spec = codec->spec; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4289 | int i; |
| 4290 | |
Takashi Iwai | d415693 | 2013-01-07 10:08:02 +0100 | [diff] [blame] | 4291 | for (i = 0; i < spec->autocfg.line_outs; i++) |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 4292 | set_output_and_unmute(codec, spec->out_paths[i]); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4293 | } |
| 4294 | |
Takashi Iwai | db23fd1 | 2012-12-20 15:27:24 +0100 | [diff] [blame] | 4295 | |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 4296 | 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] | 4297 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4298 | int i; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4299 | |
Takashi Iwai | d415693 | 2013-01-07 10:08:02 +0100 | [diff] [blame] | 4300 | for (i = 0; i < num_outs; i++) |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 4301 | set_output_and_unmute(codec, paths[i]); |
Takashi Iwai | db23fd1 | 2012-12-20 15:27:24 +0100 | [diff] [blame] | 4302 | } |
| 4303 | |
| 4304 | /* initialize hp and speaker paths */ |
| 4305 | static void init_extra_out(struct hda_codec *codec) |
| 4306 | { |
| 4307 | struct hda_gen_spec *spec = codec->spec; |
| 4308 | |
| 4309 | if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT) |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 4310 | __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths); |
Takashi Iwai | db23fd1 | 2012-12-20 15:27:24 +0100 | [diff] [blame] | 4311 | if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT) |
| 4312 | __init_extra_out(codec, spec->autocfg.speaker_outs, |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 4313 | spec->speaker_paths); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4314 | } |
| 4315 | |
| 4316 | /* initialize multi-io paths */ |
| 4317 | static void init_multi_io(struct hda_codec *codec) |
| 4318 | { |
| 4319 | struct hda_gen_spec *spec = codec->spec; |
| 4320 | int i; |
| 4321 | |
| 4322 | for (i = 0; i < spec->multi_ios; i++) { |
| 4323 | hda_nid_t pin = spec->multi_io[i].pin; |
| 4324 | struct nid_path *path; |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 4325 | path = get_multiio_path(codec, i); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4326 | if (!path) |
| 4327 | continue; |
| 4328 | if (!spec->multi_io[i].ctl_in) |
| 4329 | spec->multi_io[i].ctl_in = |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 4330 | snd_hda_codec_get_pin_target(codec, pin); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4331 | snd_hda_activate_path(codec, path, path->active, true); |
| 4332 | } |
| 4333 | } |
| 4334 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4335 | /* set up input pins and loopback paths */ |
| 4336 | static void init_analog_input(struct hda_codec *codec) |
| 4337 | { |
| 4338 | struct hda_gen_spec *spec = codec->spec; |
| 4339 | struct auto_pin_cfg *cfg = &spec->autocfg; |
| 4340 | int i; |
| 4341 | |
| 4342 | for (i = 0; i < cfg->num_inputs; i++) { |
| 4343 | hda_nid_t nid = cfg->inputs[i].pin; |
| 4344 | if (is_input_pin(codec, nid)) |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 4345 | restore_pin_ctl(codec, nid); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4346 | |
| 4347 | /* init loopback inputs */ |
| 4348 | if (spec->mixer_nid) { |
| 4349 | struct nid_path *path; |
Takashi Iwai | 196c1766 | 2013-01-04 15:01:40 +0100 | [diff] [blame] | 4350 | path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4351 | if (path) |
| 4352 | snd_hda_activate_path(codec, path, |
| 4353 | path->active, false); |
| 4354 | } |
| 4355 | } |
| 4356 | } |
| 4357 | |
| 4358 | /* initialize ADC paths */ |
| 4359 | static void init_input_src(struct hda_codec *codec) |
| 4360 | { |
| 4361 | struct hda_gen_spec *spec = codec->spec; |
| 4362 | struct hda_input_mux *imux = &spec->input_mux; |
| 4363 | struct nid_path *path; |
| 4364 | int i, c, nums; |
| 4365 | |
| 4366 | if (spec->dyn_adc_switch) |
| 4367 | nums = 1; |
| 4368 | else |
| 4369 | nums = spec->num_adc_nids; |
| 4370 | |
| 4371 | for (c = 0; c < nums; c++) { |
| 4372 | for (i = 0; i < imux->num_items; i++) { |
Takashi Iwai | c697b71 | 2013-01-07 17:09:26 +0100 | [diff] [blame] | 4373 | path = get_input_path(codec, c, i); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4374 | if (path) { |
| 4375 | bool active = path->active; |
| 4376 | if (i == spec->cur_mux[c]) |
| 4377 | active = true; |
| 4378 | snd_hda_activate_path(codec, path, active, false); |
| 4379 | } |
| 4380 | } |
| 4381 | } |
| 4382 | |
| 4383 | if (spec->shared_mic_hp) |
| 4384 | update_shared_mic_hp(codec, spec->cur_mux[0]); |
| 4385 | |
| 4386 | if (spec->cap_sync_hook) |
| 4387 | spec->cap_sync_hook(codec); |
| 4388 | } |
| 4389 | |
| 4390 | /* set right pin controls for digital I/O */ |
| 4391 | static void init_digital(struct hda_codec *codec) |
| 4392 | { |
| 4393 | struct hda_gen_spec *spec = codec->spec; |
| 4394 | int i; |
| 4395 | hda_nid_t pin; |
| 4396 | |
Takashi Iwai | d415693 | 2013-01-07 10:08:02 +0100 | [diff] [blame] | 4397 | for (i = 0; i < spec->autocfg.dig_outs; i++) |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 4398 | set_output_and_unmute(codec, spec->digout_paths[i]); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4399 | pin = spec->autocfg.dig_in_pin; |
Takashi Iwai | 2430d7b | 2013-01-04 15:09:42 +0100 | [diff] [blame] | 4400 | if (pin) { |
| 4401 | struct nid_path *path; |
Takashi Iwai | 2c12c30 | 2013-01-10 09:33:29 +0100 | [diff] [blame] | 4402 | restore_pin_ctl(codec, pin); |
Takashi Iwai | 2430d7b | 2013-01-04 15:09:42 +0100 | [diff] [blame] | 4403 | path = snd_hda_get_path_from_idx(codec, spec->digin_path); |
| 4404 | if (path) |
| 4405 | snd_hda_activate_path(codec, path, path->active, false); |
| 4406 | } |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4407 | } |
| 4408 | |
Takashi Iwai | 973e497 | 2012-12-20 15:16:09 +0100 | [diff] [blame] | 4409 | /* clear unsol-event tags on unused pins; Conexant codecs seem to leave |
| 4410 | * invalid unsol tags by some reason |
| 4411 | */ |
| 4412 | static void clear_unsol_on_unused_pins(struct hda_codec *codec) |
| 4413 | { |
| 4414 | int i; |
| 4415 | |
| 4416 | for (i = 0; i < codec->init_pins.used; i++) { |
| 4417 | struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); |
| 4418 | hda_nid_t nid = pin->nid; |
| 4419 | if (is_jack_detectable(codec, nid) && |
| 4420 | !snd_hda_jack_tbl_get(codec, nid)) |
| 4421 | snd_hda_codec_update_cache(codec, nid, 0, |
| 4422 | AC_VERB_SET_UNSOLICITED_ENABLE, 0); |
| 4423 | } |
| 4424 | } |
| 4425 | |
Takashi Iwai | 5187ac1 | 2013-01-07 12:52:16 +0100 | [diff] [blame] | 4426 | /* |
| 4427 | * initialize the generic spec; |
| 4428 | * this can be put as patch_ops.init function |
| 4429 | */ |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4430 | int snd_hda_gen_init(struct hda_codec *codec) |
| 4431 | { |
| 4432 | struct hda_gen_spec *spec = codec->spec; |
| 4433 | |
| 4434 | if (spec->init_hook) |
| 4435 | spec->init_hook(codec); |
| 4436 | |
| 4437 | snd_hda_apply_verbs(codec); |
| 4438 | |
Takashi Iwai | 3bbcd27 | 2012-12-20 11:50:58 +0100 | [diff] [blame] | 4439 | codec->cached_write = 1; |
| 4440 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4441 | init_multi_out(codec); |
| 4442 | init_extra_out(codec); |
| 4443 | init_multi_io(codec); |
| 4444 | init_analog_input(codec); |
| 4445 | init_input_src(codec); |
| 4446 | init_digital(codec); |
| 4447 | |
Takashi Iwai | 973e497 | 2012-12-20 15:16:09 +0100 | [diff] [blame] | 4448 | clear_unsol_on_unused_pins(codec); |
| 4449 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4450 | /* call init functions of standard auto-mute helpers */ |
Takashi Iwai | a5cc250 | 2013-01-16 18:08:55 +0100 | [diff] [blame] | 4451 | update_automute_all(codec); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4452 | |
Takashi Iwai | 3bbcd27 | 2012-12-20 11:50:58 +0100 | [diff] [blame] | 4453 | snd_hda_codec_flush_amp_cache(codec); |
| 4454 | snd_hda_codec_flush_cmd_cache(codec); |
| 4455 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4456 | if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook) |
| 4457 | snd_hda_sync_vmaster_hook(&spec->vmaster_mute); |
| 4458 | |
| 4459 | hda_call_check_power_status(codec, 0x01); |
| 4460 | return 0; |
| 4461 | } |
Takashi Iwai | fce52a3 | 2013-01-07 12:42:48 +0100 | [diff] [blame] | 4462 | EXPORT_SYMBOL_HDA(snd_hda_gen_init); |
| 4463 | |
Takashi Iwai | 5187ac1 | 2013-01-07 12:52:16 +0100 | [diff] [blame] | 4464 | /* |
| 4465 | * free the generic spec; |
| 4466 | * this can be put as patch_ops.free function |
| 4467 | */ |
Takashi Iwai | fce52a3 | 2013-01-07 12:42:48 +0100 | [diff] [blame] | 4468 | void snd_hda_gen_free(struct hda_codec *codec) |
| 4469 | { |
| 4470 | snd_hda_gen_spec_free(codec->spec); |
| 4471 | kfree(codec->spec); |
| 4472 | codec->spec = NULL; |
| 4473 | } |
| 4474 | EXPORT_SYMBOL_HDA(snd_hda_gen_free); |
| 4475 | |
| 4476 | #ifdef CONFIG_PM |
Takashi Iwai | 5187ac1 | 2013-01-07 12:52:16 +0100 | [diff] [blame] | 4477 | /* |
| 4478 | * check the loopback power save state; |
| 4479 | * this can be put as patch_ops.check_power_status function |
| 4480 | */ |
Takashi Iwai | fce52a3 | 2013-01-07 12:42:48 +0100 | [diff] [blame] | 4481 | int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid) |
| 4482 | { |
| 4483 | struct hda_gen_spec *spec = codec->spec; |
| 4484 | return snd_hda_check_amp_list_power(codec, &spec->loopback, nid); |
| 4485 | } |
| 4486 | EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status); |
| 4487 | #endif |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4488 | |
| 4489 | |
| 4490 | /* |
| 4491 | * the generic codec support |
| 4492 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4494 | static const struct hda_codec_ops generic_patch_ops = { |
| 4495 | .build_controls = snd_hda_gen_build_controls, |
| 4496 | .build_pcms = snd_hda_gen_build_pcms, |
| 4497 | .init = snd_hda_gen_init, |
Takashi Iwai | fce52a3 | 2013-01-07 12:42:48 +0100 | [diff] [blame] | 4498 | .free = snd_hda_gen_free, |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4499 | .unsol_event = snd_hda_jack_unsol_event, |
Takashi Iwai | 83012a7 | 2012-08-24 18:38:08 +0200 | [diff] [blame] | 4500 | #ifdef CONFIG_PM |
Takashi Iwai | fce52a3 | 2013-01-07 12:42:48 +0100 | [diff] [blame] | 4501 | .check_power_status = snd_hda_gen_check_power_status, |
Takashi Iwai | cb53c62 | 2007-08-10 17:21:45 +0200 | [diff] [blame] | 4502 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4503 | }; |
| 4504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4505 | int snd_hda_parse_generic_codec(struct hda_codec *codec) |
| 4506 | { |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4507 | struct hda_gen_spec *spec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4508 | int err; |
| 4509 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 4510 | spec = kzalloc(sizeof(*spec), GFP_KERNEL); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4511 | if (!spec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4512 | return -ENOMEM; |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4513 | snd_hda_gen_spec_init(spec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4514 | codec->spec = spec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4515 | |
Takashi Iwai | 9eb413e | 2012-12-19 14:41:21 +0100 | [diff] [blame] | 4516 | err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0); |
| 4517 | if (err < 0) |
| 4518 | return err; |
| 4519 | |
| 4520 | err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg); |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4521 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4522 | goto error; |
| 4523 | |
| 4524 | codec->patch_ops = generic_patch_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4525 | return 0; |
| 4526 | |
Takashi Iwai | 352f7f9 | 2012-12-19 12:52:06 +0100 | [diff] [blame] | 4527 | error: |
Takashi Iwai | fce52a3 | 2013-01-07 12:42:48 +0100 | [diff] [blame] | 4528 | snd_hda_gen_free(codec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4529 | return err; |
| 4530 | } |
Takashi Iwai | fce52a3 | 2013-01-07 12:42:48 +0100 | [diff] [blame] | 4531 | EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec); |