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