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