Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Jack-detection handling for HD-audio |
| 3 | * |
| 4 | * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de> |
| 5 | * |
| 6 | * This driver is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/slab.h> |
Takashi Iwai | bf815bf | 2011-11-16 14:28:33 +0100 | [diff] [blame] | 14 | #include <linux/export.h> |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 15 | #include <sound/core.h> |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 16 | #include <sound/control.h> |
Takashi Iwai | aad37db | 2011-11-02 08:54:51 +0100 | [diff] [blame] | 17 | #include <sound/jack.h> |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 18 | #include "hda_codec.h" |
| 19 | #include "hda_local.h" |
Takashi Iwai | 128bc4b | 2012-05-07 17:42:31 +0200 | [diff] [blame] | 20 | #include "hda_auto_parser.h" |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 21 | #include "hda_jack.h" |
| 22 | |
Takashi Iwai | a9c7417 | 2012-02-13 11:41:42 +0100 | [diff] [blame] | 23 | bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid) |
| 24 | { |
Takashi Iwai | 71b1e9e | 2012-02-13 11:55:02 +0100 | [diff] [blame] | 25 | if (codec->no_jack_detect) |
| 26 | return false; |
Takashi Iwai | a9c7417 | 2012-02-13 11:41:42 +0100 | [diff] [blame] | 27 | if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT)) |
| 28 | return false; |
David Henningsson | 5fe8e1e | 2012-09-07 07:25:44 +0200 | [diff] [blame^] | 29 | if (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) & |
| 30 | AC_DEFCFG_MISC_NO_PRESENCE) |
Takashi Iwai | a9c7417 | 2012-02-13 11:41:42 +0100 | [diff] [blame] | 31 | return false; |
| 32 | if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP)) |
| 33 | return false; |
| 34 | return true; |
| 35 | } |
| 36 | EXPORT_SYMBOL_HDA(is_jack_detectable); |
| 37 | |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 38 | /* execute pin sense measurement */ |
| 39 | static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid) |
| 40 | { |
| 41 | u32 pincap; |
| 42 | |
| 43 | if (!codec->no_trigger_sense) { |
| 44 | pincap = snd_hda_query_pin_caps(codec, nid); |
| 45 | if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */ |
| 46 | snd_hda_codec_read(codec, nid, 0, |
| 47 | AC_VERB_SET_PIN_SENSE, 0); |
| 48 | } |
| 49 | return snd_hda_codec_read(codec, nid, 0, |
| 50 | AC_VERB_GET_PIN_SENSE, 0); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * snd_hda_jack_tbl_get - query the jack-table entry for the given NID |
| 55 | */ |
| 56 | struct hda_jack_tbl * |
| 57 | snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid) |
| 58 | { |
| 59 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
| 60 | int i; |
| 61 | |
| 62 | if (!nid || !jack) |
| 63 | return NULL; |
| 64 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
| 65 | if (jack->nid == nid) |
| 66 | return jack; |
| 67 | return NULL; |
| 68 | } |
| 69 | EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get); |
| 70 | |
| 71 | /** |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 72 | * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag |
| 73 | */ |
| 74 | struct hda_jack_tbl * |
| 75 | snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag) |
| 76 | { |
| 77 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
| 78 | int i; |
| 79 | |
| 80 | if (!tag || !jack) |
| 81 | return NULL; |
| 82 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
| 83 | if (jack->tag == tag) |
| 84 | return jack; |
| 85 | return NULL; |
| 86 | } |
| 87 | EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get_from_tag); |
| 88 | |
| 89 | /** |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 90 | * snd_hda_jack_tbl_new - create a jack-table entry for the given NID |
| 91 | */ |
| 92 | struct hda_jack_tbl * |
| 93 | snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid) |
| 94 | { |
| 95 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid); |
| 96 | if (jack) |
| 97 | return jack; |
| 98 | snd_array_init(&codec->jacktbl, sizeof(*jack), 16); |
| 99 | jack = snd_array_new(&codec->jacktbl); |
| 100 | if (!jack) |
| 101 | return NULL; |
| 102 | jack->nid = nid; |
| 103 | jack->jack_dirty = 1; |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 104 | jack->tag = codec->jacktbl.used; |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 105 | return jack; |
| 106 | } |
David Henningsson | d1cb620 | 2011-11-11 17:13:15 +0100 | [diff] [blame] | 107 | EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_new); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 108 | |
| 109 | void snd_hda_jack_tbl_clear(struct hda_codec *codec) |
| 110 | { |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 111 | #ifdef CONFIG_SND_HDA_INPUT_JACK |
| 112 | /* free jack instances manually when clearing/reconfiguring */ |
| 113 | if (!codec->bus->shutdown && codec->jacktbl.list) { |
| 114 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
| 115 | int i; |
| 116 | for (i = 0; i < codec->jacktbl.used; i++, jack++) { |
| 117 | if (jack->jack) |
| 118 | snd_device_free(codec->bus->card, jack->jack); |
| 119 | } |
| 120 | } |
| 121 | #endif |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 122 | snd_array_free(&codec->jacktbl); |
| 123 | } |
| 124 | |
| 125 | /* update the cached value and notification flag if needed */ |
| 126 | static void jack_detect_update(struct hda_codec *codec, |
| 127 | struct hda_jack_tbl *jack) |
| 128 | { |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 129 | if (!jack->jack_dirty) |
| 130 | return; |
| 131 | |
| 132 | if (jack->phantom_jack) |
| 133 | jack->pin_sense = AC_PINSENSE_PRESENCE; |
| 134 | else |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 135 | jack->pin_sense = read_pin_sense(codec, jack->nid); |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 136 | |
| 137 | jack->jack_dirty = 0; |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
| 141 | * snd_hda_set_dirty_all - Mark all the cached as dirty |
| 142 | * |
| 143 | * This function sets the dirty flag to all entries of jack table. |
| 144 | * It's called from the resume path in hda_codec.c. |
| 145 | */ |
| 146 | void snd_hda_jack_set_dirty_all(struct hda_codec *codec) |
| 147 | { |
| 148 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
| 149 | int i; |
| 150 | |
| 151 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
| 152 | if (jack->nid) |
| 153 | jack->jack_dirty = 1; |
| 154 | } |
| 155 | EXPORT_SYMBOL_HDA(snd_hda_jack_set_dirty_all); |
| 156 | |
| 157 | /** |
| 158 | * snd_hda_pin_sense - execute pin sense measurement |
| 159 | * @codec: the CODEC to sense |
| 160 | * @nid: the pin NID to sense |
| 161 | * |
| 162 | * Execute necessary pin sense measurement and return its Presence Detect, |
| 163 | * Impedance, ELD Valid etc. status bits. |
| 164 | */ |
| 165 | u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid) |
| 166 | { |
| 167 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid); |
| 168 | if (jack) { |
| 169 | jack_detect_update(codec, jack); |
| 170 | return jack->pin_sense; |
| 171 | } |
| 172 | return read_pin_sense(codec, nid); |
| 173 | } |
| 174 | EXPORT_SYMBOL_HDA(snd_hda_pin_sense); |
| 175 | |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 176 | #define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE) |
| 177 | |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 178 | /** |
| 179 | * snd_hda_jack_detect - query pin Presence Detect status |
| 180 | * @codec: the CODEC to sense |
| 181 | * @nid: the pin NID to sense |
| 182 | * |
| 183 | * Query and return the pin's Presence Detect status. |
| 184 | */ |
| 185 | int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid) |
| 186 | { |
| 187 | u32 sense = snd_hda_pin_sense(codec, nid); |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 188 | return get_jack_plug_state(sense); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 189 | } |
| 190 | EXPORT_SYMBOL_HDA(snd_hda_jack_detect); |
| 191 | |
| 192 | /** |
| 193 | * snd_hda_jack_detect_enable - enable the jack-detection |
| 194 | */ |
| 195 | int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 196 | unsigned char action) |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 197 | { |
| 198 | struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid); |
| 199 | if (!jack) |
| 200 | return -ENOMEM; |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 201 | if (jack->jack_detect) |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 202 | return 0; /* already registered */ |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 203 | jack->jack_detect = 1; |
| 204 | if (action) |
| 205 | jack->action = action; |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 206 | return snd_hda_codec_write_cache(codec, nid, 0, |
| 207 | AC_VERB_SET_UNSOLICITED_ENABLE, |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 208 | AC_USRSP_EN | jack->tag); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 209 | } |
| 210 | EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 211 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 212 | /** |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 213 | * snd_hda_jack_report_sync - sync the states of all jacks and report if changed |
| 214 | */ |
| 215 | void snd_hda_jack_report_sync(struct hda_codec *codec) |
| 216 | { |
| 217 | struct hda_jack_tbl *jack = codec->jacktbl.list; |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 218 | int i, state; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 219 | |
| 220 | for (i = 0; i < codec->jacktbl.used; i++, jack++) |
| 221 | if (jack->nid) { |
| 222 | jack_detect_update(codec, jack); |
Takashi Iwai | cfc7c9d | 2011-11-11 17:53:03 +0100 | [diff] [blame] | 223 | if (!jack->kctl) |
| 224 | continue; |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 225 | state = get_jack_plug_state(jack->pin_sense); |
Takashi Iwai | aad37db | 2011-11-02 08:54:51 +0100 | [diff] [blame] | 226 | snd_kctl_jack_report(codec->bus->card, jack->kctl, state); |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 227 | #ifdef CONFIG_SND_HDA_INPUT_JACK |
| 228 | if (jack->jack) |
| 229 | snd_jack_report(jack->jack, |
| 230 | state ? jack->type : 0); |
| 231 | #endif |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync); |
| 235 | |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 236 | #ifdef CONFIG_SND_HDA_INPUT_JACK |
| 237 | /* guess the jack type from the pin-config */ |
| 238 | static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid) |
| 239 | { |
| 240 | unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid); |
| 241 | switch (get_defcfg_device(def_conf)) { |
| 242 | case AC_JACK_LINE_OUT: |
| 243 | case AC_JACK_SPEAKER: |
| 244 | return SND_JACK_LINEOUT; |
| 245 | case AC_JACK_HP_OUT: |
| 246 | return SND_JACK_HEADPHONE; |
| 247 | case AC_JACK_SPDIF_OUT: |
| 248 | case AC_JACK_DIG_OTHER_OUT: |
| 249 | return SND_JACK_AVOUT; |
| 250 | case AC_JACK_MIC_IN: |
| 251 | return SND_JACK_MICROPHONE; |
| 252 | default: |
| 253 | return SND_JACK_LINEIN; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | static void hda_free_jack_priv(struct snd_jack *jack) |
| 258 | { |
| 259 | struct hda_jack_tbl *jacks = jack->private_data; |
| 260 | jacks->nid = 0; |
| 261 | jacks->jack = NULL; |
| 262 | } |
| 263 | #endif |
| 264 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 265 | /** |
| 266 | * snd_hda_jack_add_kctl - Add a kctl for the given pin |
| 267 | * |
| 268 | * This assigns a jack-detection kctl to the given pin. The kcontrol |
| 269 | * will have the given name and index. |
| 270 | */ |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 271 | static int __snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, |
| 272 | const char *name, int idx, bool phantom_jack) |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 273 | { |
| 274 | struct hda_jack_tbl *jack; |
| 275 | struct snd_kcontrol *kctl; |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 276 | int err, state; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 277 | |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 278 | jack = snd_hda_jack_tbl_new(codec, nid); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 279 | if (!jack) |
| 280 | return 0; |
| 281 | if (jack->kctl) |
| 282 | return 0; /* already created */ |
Takashi Iwai | 35be544 | 2011-11-02 08:36:06 +0100 | [diff] [blame] | 283 | kctl = snd_kctl_jack_new(name, idx, codec); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 284 | if (!kctl) |
| 285 | return -ENOMEM; |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 286 | err = snd_hda_ctl_add(codec, nid, kctl); |
| 287 | if (err < 0) |
| 288 | return err; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 289 | jack->kctl = kctl; |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 290 | jack->phantom_jack = !!phantom_jack; |
| 291 | |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 292 | state = snd_hda_jack_detect(codec, nid); |
| 293 | snd_kctl_jack_report(codec->bus->card, kctl, state); |
| 294 | #ifdef CONFIG_SND_HDA_INPUT_JACK |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 295 | if (!phantom_jack) { |
| 296 | jack->type = get_input_jack_type(codec, nid); |
| 297 | err = snd_jack_new(codec->bus->card, name, jack->type, |
| 298 | &jack->jack); |
| 299 | if (err < 0) |
| 300 | return err; |
| 301 | jack->jack->private_data = jack; |
| 302 | jack->jack->private_free = hda_free_jack_priv; |
| 303 | snd_jack_report(jack->jack, state ? jack->type : 0); |
| 304 | } |
Takashi Iwai | 31ef225 | 2011-12-01 17:41:36 +0100 | [diff] [blame] | 305 | #endif |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 306 | return 0; |
| 307 | } |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 308 | |
| 309 | int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, |
| 310 | const char *name, int idx) |
| 311 | { |
| 312 | return __snd_hda_jack_add_kctl(codec, nid, name, idx, false); |
| 313 | } |
David Henningsson | d1cb620 | 2011-11-11 17:13:15 +0100 | [diff] [blame] | 314 | EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctl); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 315 | |
Takashi Iwai | f46c329 | 2012-07-17 16:48:27 +0200 | [diff] [blame] | 316 | /* get the unique index number for the given kctl name */ |
| 317 | static int get_unique_index(struct hda_codec *codec, const char *name, int idx) |
| 318 | { |
| 319 | struct hda_jack_tbl *jack; |
| 320 | int i, len = strlen(name); |
| 321 | again: |
| 322 | jack = codec->jacktbl.list; |
| 323 | for (i = 0; i < codec->jacktbl.used; i++, jack++) { |
| 324 | /* jack->kctl.id contains "XXX Jack" name string with index */ |
| 325 | if (jack->kctl && |
| 326 | !strncmp(name, jack->kctl->id.name, len) && |
| 327 | !strcmp(" Jack", jack->kctl->id.name + len) && |
| 328 | jack->kctl->id.index == idx) { |
| 329 | idx++; |
| 330 | goto again; |
| 331 | } |
| 332 | } |
| 333 | return idx; |
| 334 | } |
| 335 | |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 336 | static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid, |
Takashi Iwai | f46c329 | 2012-07-17 16:48:27 +0200 | [diff] [blame] | 337 | const struct auto_pin_cfg *cfg) |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 338 | { |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 339 | unsigned int def_conf, conn; |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 340 | char name[44]; |
| 341 | int idx, err; |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 342 | bool phantom_jack; |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 343 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 344 | if (!nid) |
| 345 | return 0; |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 346 | def_conf = snd_hda_codec_get_pincfg(codec, nid); |
| 347 | conn = get_defcfg_connect(def_conf); |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 348 | if (conn == AC_JACK_PORT_NONE) |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 349 | return 0; |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 350 | phantom_jack = (conn != AC_JACK_PORT_COMPLEX) || |
| 351 | !is_jack_detectable(codec, nid); |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 352 | |
Takashi Iwai | 201e06f | 2011-11-16 15:33:26 +0100 | [diff] [blame] | 353 | snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), &idx); |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 354 | if (phantom_jack) |
| 355 | /* Example final name: "Internal Mic Phantom Jack" */ |
| 356 | strncat(name, " Phantom", sizeof(name) - strlen(name) - 1); |
Takashi Iwai | f46c329 | 2012-07-17 16:48:27 +0200 | [diff] [blame] | 357 | idx = get_unique_index(codec, name, idx); |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 358 | err = __snd_hda_jack_add_kctl(codec, nid, name, idx, phantom_jack); |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 359 | if (err < 0) |
| 360 | return err; |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 361 | |
| 362 | if (!phantom_jack) |
| 363 | return snd_hda_jack_detect_enable(codec, nid, 0); |
| 364 | return 0; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
| 368 | * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 369 | */ |
| 370 | int snd_hda_jack_add_kctls(struct hda_codec *codec, |
| 371 | const struct auto_pin_cfg *cfg) |
| 372 | { |
| 373 | const hda_nid_t *p; |
Takashi Iwai | f46c329 | 2012-07-17 16:48:27 +0200 | [diff] [blame] | 374 | int i, err; |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 375 | |
| 376 | for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) { |
Takashi Iwai | f46c329 | 2012-07-17 16:48:27 +0200 | [diff] [blame] | 377 | err = add_jack_kctl(codec, *p, cfg); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 378 | if (err < 0) |
| 379 | return err; |
| 380 | } |
| 381 | for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) { |
| 382 | if (*p == *cfg->line_out_pins) /* might be duplicated */ |
| 383 | break; |
Takashi Iwai | f46c329 | 2012-07-17 16:48:27 +0200 | [diff] [blame] | 384 | err = add_jack_kctl(codec, *p, cfg); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 385 | if (err < 0) |
| 386 | return err; |
| 387 | } |
| 388 | for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) { |
| 389 | if (*p == *cfg->line_out_pins) /* might be duplicated */ |
| 390 | break; |
Takashi Iwai | f46c329 | 2012-07-17 16:48:27 +0200 | [diff] [blame] | 391 | err = add_jack_kctl(codec, *p, cfg); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 392 | if (err < 0) |
| 393 | return err; |
| 394 | } |
| 395 | for (i = 0; i < cfg->num_inputs; i++) { |
Takashi Iwai | f46c329 | 2012-07-17 16:48:27 +0200 | [diff] [blame] | 396 | err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 397 | if (err < 0) |
| 398 | return err; |
| 399 | } |
| 400 | for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) { |
Takashi Iwai | f46c329 | 2012-07-17 16:48:27 +0200 | [diff] [blame] | 401 | err = add_jack_kctl(codec, *p, cfg); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 402 | if (err < 0) |
| 403 | return err; |
| 404 | } |
Takashi Iwai | f46c329 | 2012-07-17 16:48:27 +0200 | [diff] [blame] | 405 | err = add_jack_kctl(codec, cfg->dig_in_pin, cfg); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 406 | if (err < 0) |
| 407 | return err; |
Takashi Iwai | f46c329 | 2012-07-17 16:48:27 +0200 | [diff] [blame] | 408 | err = add_jack_kctl(codec, cfg->mono_out_pin, cfg); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 409 | if (err < 0) |
| 410 | return err; |
| 411 | return 0; |
| 412 | } |
| 413 | EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls); |