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