11835a0f9STakashi Iwai /* 21835a0f9STakashi Iwai * Jack-detection handling for HD-audio 31835a0f9STakashi Iwai * 41835a0f9STakashi Iwai * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de> 51835a0f9STakashi Iwai * 61835a0f9STakashi Iwai * This driver is free software; you can redistribute it and/or modify 71835a0f9STakashi Iwai * it under the terms of the GNU General Public License as published by 81835a0f9STakashi Iwai * the Free Software Foundation; either version 2 of the License, or 91835a0f9STakashi Iwai * (at your option) any later version. 101835a0f9STakashi Iwai */ 111835a0f9STakashi Iwai 121835a0f9STakashi Iwai #include <linux/init.h> 131835a0f9STakashi Iwai #include <linux/slab.h> 14bf815bf0STakashi Iwai #include <linux/export.h> 151835a0f9STakashi Iwai #include <sound/core.h> 1601a61e12STakashi Iwai #include <sound/control.h> 17aad37dbdSTakashi Iwai #include <sound/jack.h> 181835a0f9STakashi Iwai #include "hda_codec.h" 191835a0f9STakashi Iwai #include "hda_local.h" 20128bc4baSTakashi Iwai #include "hda_auto_parser.h" 211835a0f9STakashi Iwai #include "hda_jack.h" 221835a0f9STakashi Iwai 23a9c74173STakashi Iwai bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid) 24a9c74173STakashi Iwai { 2571b1e9e4STakashi Iwai if (codec->no_jack_detect) 2671b1e9e4STakashi Iwai return false; 27a9c74173STakashi Iwai if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT)) 28a9c74173STakashi Iwai return false; 295fe8e1e6SDavid Henningsson if (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) & 305fe8e1e6SDavid Henningsson AC_DEFCFG_MISC_NO_PRESENCE) 31a9c74173STakashi Iwai return false; 3276a19c69STakashi Iwai if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) && 3376a19c69STakashi Iwai !codec->jackpoll_interval) 34a9c74173STakashi Iwai return false; 35a9c74173STakashi Iwai return true; 36a9c74173STakashi Iwai } 37a9c74173STakashi Iwai EXPORT_SYMBOL_HDA(is_jack_detectable); 38a9c74173STakashi Iwai 391835a0f9STakashi Iwai /* execute pin sense measurement */ 401835a0f9STakashi Iwai static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid) 411835a0f9STakashi Iwai { 421835a0f9STakashi Iwai u32 pincap; 439cc159c6STakashi Iwai u32 val; 441835a0f9STakashi Iwai 451835a0f9STakashi Iwai if (!codec->no_trigger_sense) { 461835a0f9STakashi Iwai pincap = snd_hda_query_pin_caps(codec, nid); 471835a0f9STakashi Iwai if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */ 481835a0f9STakashi Iwai snd_hda_codec_read(codec, nid, 0, 491835a0f9STakashi Iwai AC_VERB_SET_PIN_SENSE, 0); 501835a0f9STakashi Iwai } 519cc159c6STakashi Iwai val = snd_hda_codec_read(codec, nid, 0, 521835a0f9STakashi Iwai AC_VERB_GET_PIN_SENSE, 0); 539cc159c6STakashi Iwai if (codec->inv_jack_detect) 549cc159c6STakashi Iwai val ^= AC_PINSENSE_PRESENCE; 559cc159c6STakashi Iwai return val; 561835a0f9STakashi Iwai } 571835a0f9STakashi Iwai 581835a0f9STakashi Iwai /** 591835a0f9STakashi Iwai * snd_hda_jack_tbl_get - query the jack-table entry for the given NID 601835a0f9STakashi Iwai */ 611835a0f9STakashi Iwai struct hda_jack_tbl * 621835a0f9STakashi Iwai snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid) 631835a0f9STakashi Iwai { 641835a0f9STakashi Iwai struct hda_jack_tbl *jack = codec->jacktbl.list; 651835a0f9STakashi Iwai int i; 661835a0f9STakashi Iwai 671835a0f9STakashi Iwai if (!nid || !jack) 681835a0f9STakashi Iwai return NULL; 691835a0f9STakashi Iwai for (i = 0; i < codec->jacktbl.used; i++, jack++) 701835a0f9STakashi Iwai if (jack->nid == nid) 711835a0f9STakashi Iwai return jack; 721835a0f9STakashi Iwai return NULL; 731835a0f9STakashi Iwai } 741835a0f9STakashi Iwai EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get); 751835a0f9STakashi Iwai 761835a0f9STakashi Iwai /** 773a93897eSTakashi Iwai * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag 783a93897eSTakashi Iwai */ 793a93897eSTakashi Iwai struct hda_jack_tbl * 803a93897eSTakashi Iwai snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag) 813a93897eSTakashi Iwai { 823a93897eSTakashi Iwai struct hda_jack_tbl *jack = codec->jacktbl.list; 833a93897eSTakashi Iwai int i; 843a93897eSTakashi Iwai 853a93897eSTakashi Iwai if (!tag || !jack) 863a93897eSTakashi Iwai return NULL; 873a93897eSTakashi Iwai for (i = 0; i < codec->jacktbl.used; i++, jack++) 883a93897eSTakashi Iwai if (jack->tag == tag) 893a93897eSTakashi Iwai return jack; 903a93897eSTakashi Iwai return NULL; 913a93897eSTakashi Iwai } 923a93897eSTakashi Iwai EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_get_from_tag); 933a93897eSTakashi Iwai 943a93897eSTakashi Iwai /** 951835a0f9STakashi Iwai * snd_hda_jack_tbl_new - create a jack-table entry for the given NID 961835a0f9STakashi Iwai */ 971835a0f9STakashi Iwai struct hda_jack_tbl * 981835a0f9STakashi Iwai snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid) 991835a0f9STakashi Iwai { 1001835a0f9STakashi Iwai struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid); 1011835a0f9STakashi Iwai if (jack) 1021835a0f9STakashi Iwai return jack; 1031835a0f9STakashi Iwai jack = snd_array_new(&codec->jacktbl); 1041835a0f9STakashi Iwai if (!jack) 1051835a0f9STakashi Iwai return NULL; 1061835a0f9STakashi Iwai jack->nid = nid; 1071835a0f9STakashi Iwai jack->jack_dirty = 1; 1083a93897eSTakashi Iwai jack->tag = codec->jacktbl.used; 1091835a0f9STakashi Iwai return jack; 1101835a0f9STakashi Iwai } 111d1cb6200SDavid Henningsson EXPORT_SYMBOL_HDA(snd_hda_jack_tbl_new); 1121835a0f9STakashi Iwai 1131835a0f9STakashi Iwai void snd_hda_jack_tbl_clear(struct hda_codec *codec) 1141835a0f9STakashi Iwai { 11531ef2257STakashi Iwai #ifdef CONFIG_SND_HDA_INPUT_JACK 11631ef2257STakashi Iwai /* free jack instances manually when clearing/reconfiguring */ 11731ef2257STakashi Iwai if (!codec->bus->shutdown && codec->jacktbl.list) { 11831ef2257STakashi Iwai struct hda_jack_tbl *jack = codec->jacktbl.list; 11931ef2257STakashi Iwai int i; 12031ef2257STakashi Iwai for (i = 0; i < codec->jacktbl.used; i++, jack++) { 12131ef2257STakashi Iwai if (jack->jack) 12231ef2257STakashi Iwai snd_device_free(codec->bus->card, jack->jack); 12331ef2257STakashi Iwai } 12431ef2257STakashi Iwai } 12531ef2257STakashi Iwai #endif 1261835a0f9STakashi Iwai snd_array_free(&codec->jacktbl); 1271835a0f9STakashi Iwai } 1281835a0f9STakashi Iwai 1290619ba8cSDylan Reid #define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE) 1300619ba8cSDylan Reid 1311835a0f9STakashi Iwai /* update the cached value and notification flag if needed */ 1321835a0f9STakashi Iwai static void jack_detect_update(struct hda_codec *codec, 1331835a0f9STakashi Iwai struct hda_jack_tbl *jack) 1341835a0f9STakashi Iwai { 13580c8bfbeSDavid Henningsson if (!jack->jack_dirty) 13680c8bfbeSDavid Henningsson return; 13780c8bfbeSDavid Henningsson 13880c8bfbeSDavid Henningsson if (jack->phantom_jack) 13980c8bfbeSDavid Henningsson jack->pin_sense = AC_PINSENSE_PRESENCE; 14080c8bfbeSDavid Henningsson else 14135be544aSTakashi Iwai jack->pin_sense = read_pin_sense(codec, jack->nid); 14280c8bfbeSDavid Henningsson 1430619ba8cSDylan Reid /* A gating jack indicates the jack is invalid if gating is unplugged */ 1440619ba8cSDylan Reid if (jack->gating_jack && !snd_hda_jack_detect(codec, jack->gating_jack)) 1450619ba8cSDylan Reid jack->pin_sense &= ~AC_PINSENSE_PRESENCE; 1460619ba8cSDylan Reid 1471835a0f9STakashi Iwai jack->jack_dirty = 0; 1480619ba8cSDylan Reid 1490619ba8cSDylan Reid /* If a jack is gated by this one update it. */ 1500619ba8cSDylan Reid if (jack->gated_jack) { 1510619ba8cSDylan Reid struct hda_jack_tbl *gated = 1520619ba8cSDylan Reid snd_hda_jack_tbl_get(codec, jack->gated_jack); 1530619ba8cSDylan Reid if (gated) { 1540619ba8cSDylan Reid gated->jack_dirty = 1; 1550619ba8cSDylan Reid jack_detect_update(codec, gated); 1560619ba8cSDylan Reid } 1570619ba8cSDylan Reid } 1581835a0f9STakashi Iwai } 1591835a0f9STakashi Iwai 1601835a0f9STakashi Iwai /** 1611835a0f9STakashi Iwai * snd_hda_set_dirty_all - Mark all the cached as dirty 1621835a0f9STakashi Iwai * 1631835a0f9STakashi Iwai * This function sets the dirty flag to all entries of jack table. 1641835a0f9STakashi Iwai * It's called from the resume path in hda_codec.c. 1651835a0f9STakashi Iwai */ 1661835a0f9STakashi Iwai void snd_hda_jack_set_dirty_all(struct hda_codec *codec) 1671835a0f9STakashi Iwai { 1681835a0f9STakashi Iwai struct hda_jack_tbl *jack = codec->jacktbl.list; 1691835a0f9STakashi Iwai int i; 1701835a0f9STakashi Iwai 1711835a0f9STakashi Iwai for (i = 0; i < codec->jacktbl.used; i++, jack++) 1721835a0f9STakashi Iwai if (jack->nid) 1731835a0f9STakashi Iwai jack->jack_dirty = 1; 1741835a0f9STakashi Iwai } 1751835a0f9STakashi Iwai EXPORT_SYMBOL_HDA(snd_hda_jack_set_dirty_all); 1761835a0f9STakashi Iwai 1771835a0f9STakashi Iwai /** 1781835a0f9STakashi Iwai * snd_hda_pin_sense - execute pin sense measurement 1791835a0f9STakashi Iwai * @codec: the CODEC to sense 1801835a0f9STakashi Iwai * @nid: the pin NID to sense 1811835a0f9STakashi Iwai * 1821835a0f9STakashi Iwai * Execute necessary pin sense measurement and return its Presence Detect, 1831835a0f9STakashi Iwai * Impedance, ELD Valid etc. status bits. 1841835a0f9STakashi Iwai */ 1851835a0f9STakashi Iwai u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid) 1861835a0f9STakashi Iwai { 1871835a0f9STakashi Iwai struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid); 1881835a0f9STakashi Iwai if (jack) { 1891835a0f9STakashi Iwai jack_detect_update(codec, jack); 1901835a0f9STakashi Iwai return jack->pin_sense; 1911835a0f9STakashi Iwai } 1921835a0f9STakashi Iwai return read_pin_sense(codec, nid); 1931835a0f9STakashi Iwai } 1941835a0f9STakashi Iwai EXPORT_SYMBOL_HDA(snd_hda_pin_sense); 1951835a0f9STakashi Iwai 1961835a0f9STakashi Iwai /** 1971835a0f9STakashi Iwai * snd_hda_jack_detect - query pin Presence Detect status 1981835a0f9STakashi Iwai * @codec: the CODEC to sense 1991835a0f9STakashi Iwai * @nid: the pin NID to sense 2001835a0f9STakashi Iwai * 2011835a0f9STakashi Iwai * Query and return the pin's Presence Detect status. 2021835a0f9STakashi Iwai */ 2031835a0f9STakashi Iwai int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid) 2041835a0f9STakashi Iwai { 2051835a0f9STakashi Iwai u32 sense = snd_hda_pin_sense(codec, nid); 20635be544aSTakashi Iwai return get_jack_plug_state(sense); 2071835a0f9STakashi Iwai } 2081835a0f9STakashi Iwai EXPORT_SYMBOL_HDA(snd_hda_jack_detect); 2091835a0f9STakashi Iwai 2101835a0f9STakashi Iwai /** 2111835a0f9STakashi Iwai * snd_hda_jack_detect_enable - enable the jack-detection 2121835a0f9STakashi Iwai */ 213954df2a9SDavid Henningsson int snd_hda_jack_detect_enable_callback(struct hda_codec *codec, hda_nid_t nid, 214954df2a9SDavid Henningsson unsigned char action, 215954df2a9SDavid Henningsson hda_jack_callback cb) 2161835a0f9STakashi Iwai { 2171835a0f9STakashi Iwai struct hda_jack_tbl *jack = snd_hda_jack_tbl_new(codec, nid); 2181835a0f9STakashi Iwai if (!jack) 2191835a0f9STakashi Iwai return -ENOMEM; 2203a93897eSTakashi Iwai if (jack->jack_detect) 22101a61e12STakashi Iwai return 0; /* already registered */ 2223a93897eSTakashi Iwai jack->jack_detect = 1; 2233a93897eSTakashi Iwai if (action) 2243a93897eSTakashi Iwai jack->action = action; 225954df2a9SDavid Henningsson if (cb) 226954df2a9SDavid Henningsson jack->callback = cb; 22734a93187SDavid Henningsson if (codec->jackpoll_interval > 0) 22834a93187SDavid Henningsson return 0; /* No unsol if we're polling instead */ 2291835a0f9STakashi Iwai return snd_hda_codec_write_cache(codec, nid, 0, 2301835a0f9STakashi Iwai AC_VERB_SET_UNSOLICITED_ENABLE, 2313a93897eSTakashi Iwai AC_USRSP_EN | jack->tag); 2321835a0f9STakashi Iwai } 233954df2a9SDavid Henningsson EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable_callback); 234954df2a9SDavid Henningsson 235954df2a9SDavid Henningsson int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid, 236954df2a9SDavid Henningsson unsigned char action) 237954df2a9SDavid Henningsson { 238954df2a9SDavid Henningsson return snd_hda_jack_detect_enable_callback(codec, nid, action, NULL); 239954df2a9SDavid Henningsson } 2401835a0f9STakashi Iwai EXPORT_SYMBOL_HDA(snd_hda_jack_detect_enable); 24101a61e12STakashi Iwai 24201a61e12STakashi Iwai /** 2430619ba8cSDylan Reid * snd_hda_jack_set_gating_jack - Set gating jack. 2440619ba8cSDylan Reid * 2450619ba8cSDylan Reid * Indicates the gated jack is only valid when the gating jack is plugged. 2460619ba8cSDylan Reid */ 2470619ba8cSDylan Reid int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid, 2480619ba8cSDylan Reid hda_nid_t gating_nid) 2490619ba8cSDylan Reid { 2500619ba8cSDylan Reid struct hda_jack_tbl *gated = snd_hda_jack_tbl_get(codec, gated_nid); 2510619ba8cSDylan Reid struct hda_jack_tbl *gating = snd_hda_jack_tbl_get(codec, gating_nid); 2520619ba8cSDylan Reid 2530619ba8cSDylan Reid if (!gated || !gating) 2540619ba8cSDylan Reid return -EINVAL; 2550619ba8cSDylan Reid 2560619ba8cSDylan Reid gated->gating_jack = gating_nid; 2570619ba8cSDylan Reid gating->gated_jack = gated_nid; 2580619ba8cSDylan Reid 2590619ba8cSDylan Reid return 0; 2600619ba8cSDylan Reid } 2610619ba8cSDylan Reid EXPORT_SYMBOL_HDA(snd_hda_jack_set_gating_jack); 2620619ba8cSDylan Reid 2630619ba8cSDylan Reid /** 26401a61e12STakashi Iwai * snd_hda_jack_report_sync - sync the states of all jacks and report if changed 26501a61e12STakashi Iwai */ 26601a61e12STakashi Iwai void snd_hda_jack_report_sync(struct hda_codec *codec) 26701a61e12STakashi Iwai { 2680619ba8cSDylan Reid struct hda_jack_tbl *jack; 26935be544aSTakashi Iwai int i, state; 27001a61e12STakashi Iwai 2710619ba8cSDylan Reid /* update all jacks at first */ 2720619ba8cSDylan Reid jack = codec->jacktbl.list; 2730619ba8cSDylan Reid for (i = 0; i < codec->jacktbl.used; i++, jack++) 2740619ba8cSDylan Reid if (jack->nid) 2750619ba8cSDylan Reid jack_detect_update(codec, jack); 2760619ba8cSDylan Reid 2770619ba8cSDylan Reid /* report the updated jacks; it's done after updating all jacks 2780619ba8cSDylan Reid * to make sure that all gating jacks properly have been set 2790619ba8cSDylan Reid */ 2800619ba8cSDylan Reid jack = codec->jacktbl.list; 28101a61e12STakashi Iwai for (i = 0; i < codec->jacktbl.used; i++, jack++) 28201a61e12STakashi Iwai if (jack->nid) { 283cfc7c9d3STakashi Iwai if (!jack->kctl) 284cfc7c9d3STakashi Iwai continue; 28535be544aSTakashi Iwai state = get_jack_plug_state(jack->pin_sense); 286aad37dbdSTakashi Iwai snd_kctl_jack_report(codec->bus->card, jack->kctl, state); 28731ef2257STakashi Iwai #ifdef CONFIG_SND_HDA_INPUT_JACK 28831ef2257STakashi Iwai if (jack->jack) 28931ef2257STakashi Iwai snd_jack_report(jack->jack, 29031ef2257STakashi Iwai state ? jack->type : 0); 29131ef2257STakashi Iwai #endif 29201a61e12STakashi Iwai } 29301a61e12STakashi Iwai } 29401a61e12STakashi Iwai EXPORT_SYMBOL_HDA(snd_hda_jack_report_sync); 29501a61e12STakashi Iwai 29631ef2257STakashi Iwai #ifdef CONFIG_SND_HDA_INPUT_JACK 29731ef2257STakashi Iwai /* guess the jack type from the pin-config */ 29831ef2257STakashi Iwai static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid) 29931ef2257STakashi Iwai { 30031ef2257STakashi Iwai unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid); 30131ef2257STakashi Iwai switch (get_defcfg_device(def_conf)) { 30231ef2257STakashi Iwai case AC_JACK_LINE_OUT: 30331ef2257STakashi Iwai case AC_JACK_SPEAKER: 30431ef2257STakashi Iwai return SND_JACK_LINEOUT; 30531ef2257STakashi Iwai case AC_JACK_HP_OUT: 30631ef2257STakashi Iwai return SND_JACK_HEADPHONE; 30731ef2257STakashi Iwai case AC_JACK_SPDIF_OUT: 30831ef2257STakashi Iwai case AC_JACK_DIG_OTHER_OUT: 30931ef2257STakashi Iwai return SND_JACK_AVOUT; 31031ef2257STakashi Iwai case AC_JACK_MIC_IN: 31131ef2257STakashi Iwai return SND_JACK_MICROPHONE; 31231ef2257STakashi Iwai default: 31331ef2257STakashi Iwai return SND_JACK_LINEIN; 31431ef2257STakashi Iwai } 31531ef2257STakashi Iwai } 31631ef2257STakashi Iwai 31731ef2257STakashi Iwai static void hda_free_jack_priv(struct snd_jack *jack) 31831ef2257STakashi Iwai { 31931ef2257STakashi Iwai struct hda_jack_tbl *jacks = jack->private_data; 32031ef2257STakashi Iwai jacks->nid = 0; 32131ef2257STakashi Iwai jacks->jack = NULL; 32231ef2257STakashi Iwai } 32331ef2257STakashi Iwai #endif 32431ef2257STakashi Iwai 32501a61e12STakashi Iwai /** 32601a61e12STakashi Iwai * snd_hda_jack_add_kctl - Add a kctl for the given pin 32701a61e12STakashi Iwai * 32801a61e12STakashi Iwai * This assigns a jack-detection kctl to the given pin. The kcontrol 32901a61e12STakashi Iwai * will have the given name and index. 33001a61e12STakashi Iwai */ 33180c8bfbeSDavid Henningsson static int __snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, 33280c8bfbeSDavid Henningsson const char *name, int idx, bool phantom_jack) 33301a61e12STakashi Iwai { 33401a61e12STakashi Iwai struct hda_jack_tbl *jack; 33501a61e12STakashi Iwai struct snd_kcontrol *kctl; 33631ef2257STakashi Iwai int err, state; 33701a61e12STakashi Iwai 3383a93897eSTakashi Iwai jack = snd_hda_jack_tbl_new(codec, nid); 33901a61e12STakashi Iwai if (!jack) 34001a61e12STakashi Iwai return 0; 34101a61e12STakashi Iwai if (jack->kctl) 34201a61e12STakashi Iwai return 0; /* already created */ 34335be544aSTakashi Iwai kctl = snd_kctl_jack_new(name, idx, codec); 34401a61e12STakashi Iwai if (!kctl) 34501a61e12STakashi Iwai return -ENOMEM; 34631ef2257STakashi Iwai err = snd_hda_ctl_add(codec, nid, kctl); 34731ef2257STakashi Iwai if (err < 0) 34831ef2257STakashi Iwai return err; 34901a61e12STakashi Iwai jack->kctl = kctl; 35080c8bfbeSDavid Henningsson jack->phantom_jack = !!phantom_jack; 35180c8bfbeSDavid Henningsson 35231ef2257STakashi Iwai state = snd_hda_jack_detect(codec, nid); 35331ef2257STakashi Iwai snd_kctl_jack_report(codec->bus->card, kctl, state); 35431ef2257STakashi Iwai #ifdef CONFIG_SND_HDA_INPUT_JACK 35580c8bfbeSDavid Henningsson if (!phantom_jack) { 35631ef2257STakashi Iwai jack->type = get_input_jack_type(codec, nid); 35780c8bfbeSDavid Henningsson err = snd_jack_new(codec->bus->card, name, jack->type, 35880c8bfbeSDavid Henningsson &jack->jack); 35931ef2257STakashi Iwai if (err < 0) 36031ef2257STakashi Iwai return err; 36131ef2257STakashi Iwai jack->jack->private_data = jack; 36231ef2257STakashi Iwai jack->jack->private_free = hda_free_jack_priv; 36331ef2257STakashi Iwai snd_jack_report(jack->jack, state ? jack->type : 0); 36480c8bfbeSDavid Henningsson } 36531ef2257STakashi Iwai #endif 36601a61e12STakashi Iwai return 0; 36701a61e12STakashi Iwai } 36880c8bfbeSDavid Henningsson 36980c8bfbeSDavid Henningsson int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, 37080c8bfbeSDavid Henningsson const char *name, int idx) 37180c8bfbeSDavid Henningsson { 37280c8bfbeSDavid Henningsson return __snd_hda_jack_add_kctl(codec, nid, name, idx, false); 37380c8bfbeSDavid Henningsson } 374d1cb6200SDavid Henningsson EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctl); 37501a61e12STakashi Iwai 376f46c3296STakashi Iwai /* get the unique index number for the given kctl name */ 377f46c3296STakashi Iwai static int get_unique_index(struct hda_codec *codec, const char *name, int idx) 378f46c3296STakashi Iwai { 379f46c3296STakashi Iwai struct hda_jack_tbl *jack; 380f46c3296STakashi Iwai int i, len = strlen(name); 381f46c3296STakashi Iwai again: 382f46c3296STakashi Iwai jack = codec->jacktbl.list; 383f46c3296STakashi Iwai for (i = 0; i < codec->jacktbl.used; i++, jack++) { 384f46c3296STakashi Iwai /* jack->kctl.id contains "XXX Jack" name string with index */ 385f46c3296STakashi Iwai if (jack->kctl && 386f46c3296STakashi Iwai !strncmp(name, jack->kctl->id.name, len) && 387f46c3296STakashi Iwai !strcmp(" Jack", jack->kctl->id.name + len) && 388f46c3296STakashi Iwai jack->kctl->id.index == idx) { 389f46c3296STakashi Iwai idx++; 390f46c3296STakashi Iwai goto again; 391f46c3296STakashi Iwai } 392f46c3296STakashi Iwai } 393f46c3296STakashi Iwai return idx; 394f46c3296STakashi Iwai } 395f46c3296STakashi Iwai 396201e06ffSTakashi Iwai static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid, 397b26b5116SDavid Henningsson const struct auto_pin_cfg *cfg, 398b26b5116SDavid Henningsson const char *base_name) 39901a61e12STakashi Iwai { 4003a93897eSTakashi Iwai unsigned int def_conf, conn; 401*975cc02aSTakashi Iwai char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 402201e06ffSTakashi Iwai int idx, err; 40380c8bfbeSDavid Henningsson bool phantom_jack; 4043a93897eSTakashi Iwai 40501a61e12STakashi Iwai if (!nid) 40601a61e12STakashi Iwai return 0; 4073a93897eSTakashi Iwai def_conf = snd_hda_codec_get_pincfg(codec, nid); 4083a93897eSTakashi Iwai conn = get_defcfg_connect(def_conf); 40980c8bfbeSDavid Henningsson if (conn == AC_JACK_PORT_NONE) 4103a93897eSTakashi Iwai return 0; 41180c8bfbeSDavid Henningsson phantom_jack = (conn != AC_JACK_PORT_COMPLEX) || 41280c8bfbeSDavid Henningsson !is_jack_detectable(codec, nid); 4133a93897eSTakashi Iwai 414b26b5116SDavid Henningsson if (base_name) { 415b26b5116SDavid Henningsson strlcpy(name, base_name, sizeof(name)); 416b26b5116SDavid Henningsson idx = 0; 417b26b5116SDavid Henningsson } else 418201e06ffSTakashi Iwai snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), &idx); 41980c8bfbeSDavid Henningsson if (phantom_jack) 42080c8bfbeSDavid Henningsson /* Example final name: "Internal Mic Phantom Jack" */ 42180c8bfbeSDavid Henningsson strncat(name, " Phantom", sizeof(name) - strlen(name) - 1); 422f46c3296STakashi Iwai idx = get_unique_index(codec, name, idx); 42380c8bfbeSDavid Henningsson err = __snd_hda_jack_add_kctl(codec, nid, name, idx, phantom_jack); 4243a93897eSTakashi Iwai if (err < 0) 4253a93897eSTakashi Iwai return err; 42680c8bfbeSDavid Henningsson 42780c8bfbeSDavid Henningsson if (!phantom_jack) 4283a93897eSTakashi Iwai return snd_hda_jack_detect_enable(codec, nid, 0); 42980c8bfbeSDavid Henningsson return 0; 43001a61e12STakashi Iwai } 43101a61e12STakashi Iwai 43201a61e12STakashi Iwai /** 43301a61e12STakashi Iwai * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg 43401a61e12STakashi Iwai */ 43501a61e12STakashi Iwai int snd_hda_jack_add_kctls(struct hda_codec *codec, 43601a61e12STakashi Iwai const struct auto_pin_cfg *cfg) 43701a61e12STakashi Iwai { 43801a61e12STakashi Iwai const hda_nid_t *p; 439f46c3296STakashi Iwai int i, err; 44001a61e12STakashi Iwai 441b26b5116SDavid Henningsson for (i = 0; i < cfg->num_inputs; i++) { 442b26b5116SDavid Henningsson /* If we have headphone mics; make sure they get the right name 443b26b5116SDavid Henningsson before grabbed by output pins */ 444b26b5116SDavid Henningsson if (cfg->inputs[i].is_headphone_mic) { 445b26b5116SDavid Henningsson if (auto_cfg_hp_outs(cfg) == 1) 446b26b5116SDavid Henningsson err = add_jack_kctl(codec, auto_cfg_hp_pins(cfg)[0], 447b26b5116SDavid Henningsson cfg, "Headphone Mic"); 448b26b5116SDavid Henningsson else 449b26b5116SDavid Henningsson err = add_jack_kctl(codec, cfg->inputs[i].pin, 450b26b5116SDavid Henningsson cfg, "Headphone Mic"); 451b26b5116SDavid Henningsson } else 452b26b5116SDavid Henningsson err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg, 453b26b5116SDavid Henningsson NULL); 454b26b5116SDavid Henningsson if (err < 0) 455b26b5116SDavid Henningsson return err; 456b26b5116SDavid Henningsson } 457b26b5116SDavid Henningsson 45801a61e12STakashi Iwai for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) { 459b26b5116SDavid Henningsson err = add_jack_kctl(codec, *p, cfg, NULL); 46001a61e12STakashi Iwai if (err < 0) 46101a61e12STakashi Iwai return err; 46201a61e12STakashi Iwai } 46301a61e12STakashi Iwai for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) { 46401a61e12STakashi Iwai if (*p == *cfg->line_out_pins) /* might be duplicated */ 46501a61e12STakashi Iwai break; 466b26b5116SDavid Henningsson err = add_jack_kctl(codec, *p, cfg, NULL); 46701a61e12STakashi Iwai if (err < 0) 46801a61e12STakashi Iwai return err; 46901a61e12STakashi Iwai } 47001a61e12STakashi Iwai for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) { 47101a61e12STakashi Iwai if (*p == *cfg->line_out_pins) /* might be duplicated */ 47201a61e12STakashi Iwai break; 473b26b5116SDavid Henningsson err = add_jack_kctl(codec, *p, cfg, NULL); 47401a61e12STakashi Iwai if (err < 0) 47501a61e12STakashi Iwai return err; 47601a61e12STakashi Iwai } 47701a61e12STakashi Iwai for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) { 478b26b5116SDavid Henningsson err = add_jack_kctl(codec, *p, cfg, NULL); 47901a61e12STakashi Iwai if (err < 0) 48001a61e12STakashi Iwai return err; 48101a61e12STakashi Iwai } 482b26b5116SDavid Henningsson err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, NULL); 48301a61e12STakashi Iwai if (err < 0) 48401a61e12STakashi Iwai return err; 485b26b5116SDavid Henningsson err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, NULL); 48601a61e12STakashi Iwai if (err < 0) 48701a61e12STakashi Iwai return err; 48801a61e12STakashi Iwai return 0; 48901a61e12STakashi Iwai } 49001a61e12STakashi Iwai EXPORT_SYMBOL_HDA(snd_hda_jack_add_kctls); 491954df2a9SDavid Henningsson 4920619ba8cSDylan Reid static void call_jack_callback(struct hda_codec *codec, 4930619ba8cSDylan Reid struct hda_jack_tbl *jack) 4940619ba8cSDylan Reid { 4950619ba8cSDylan Reid if (jack->callback) 4960619ba8cSDylan Reid jack->callback(codec, jack); 4970619ba8cSDylan Reid if (jack->gated_jack) { 4980619ba8cSDylan Reid struct hda_jack_tbl *gated = 4990619ba8cSDylan Reid snd_hda_jack_tbl_get(codec, jack->gated_jack); 5000619ba8cSDylan Reid if (gated && gated->callback) 5010619ba8cSDylan Reid gated->callback(codec, gated); 5020619ba8cSDylan Reid } 5030619ba8cSDylan Reid } 5040619ba8cSDylan Reid 505954df2a9SDavid Henningsson void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res) 506954df2a9SDavid Henningsson { 507954df2a9SDavid Henningsson struct hda_jack_tbl *event; 508954df2a9SDavid Henningsson int tag = (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x7f; 509954df2a9SDavid Henningsson 510954df2a9SDavid Henningsson event = snd_hda_jack_tbl_get_from_tag(codec, tag); 511954df2a9SDavid Henningsson if (!event) 512954df2a9SDavid Henningsson return; 513954df2a9SDavid Henningsson event->jack_dirty = 1; 514954df2a9SDavid Henningsson 5150619ba8cSDylan Reid call_jack_callback(codec, event); 516954df2a9SDavid Henningsson snd_hda_jack_report_sync(codec); 517954df2a9SDavid Henningsson } 518954df2a9SDavid Henningsson EXPORT_SYMBOL_HDA(snd_hda_jack_unsol_event); 519954df2a9SDavid Henningsson 52026a6cb6cSDavid Henningsson void snd_hda_jack_poll_all(struct hda_codec *codec) 52126a6cb6cSDavid Henningsson { 52226a6cb6cSDavid Henningsson struct hda_jack_tbl *jack = codec->jacktbl.list; 52326a6cb6cSDavid Henningsson int i, changes = 0; 52426a6cb6cSDavid Henningsson 52526a6cb6cSDavid Henningsson for (i = 0; i < codec->jacktbl.used; i++, jack++) { 52626a6cb6cSDavid Henningsson unsigned int old_sense; 52726a6cb6cSDavid Henningsson if (!jack->nid || !jack->jack_dirty || jack->phantom_jack) 52826a6cb6cSDavid Henningsson continue; 52926a6cb6cSDavid Henningsson old_sense = get_jack_plug_state(jack->pin_sense); 53026a6cb6cSDavid Henningsson jack_detect_update(codec, jack); 53126a6cb6cSDavid Henningsson if (old_sense == get_jack_plug_state(jack->pin_sense)) 53226a6cb6cSDavid Henningsson continue; 53326a6cb6cSDavid Henningsson changes = 1; 5340619ba8cSDylan Reid call_jack_callback(codec, jack); 53526a6cb6cSDavid Henningsson } 53626a6cb6cSDavid Henningsson if (changes) 53726a6cb6cSDavid Henningsson snd_hda_jack_report_sync(codec); 53826a6cb6cSDavid Henningsson } 53926a6cb6cSDavid Henningsson EXPORT_SYMBOL_HDA(snd_hda_jack_poll_all); 54026a6cb6cSDavid Henningsson 541