Thomas Gleixner | 62810db | 2019-05-20 19:08:11 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Jack-detection handling for HD-audio |
| 4 | * |
| 5 | * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de> |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef __SOUND_HDA_JACK_H |
| 9 | #define __SOUND_HDA_JACK_H |
| 10 | |
David Henningsson | a5062de | 2014-09-23 09:04:49 +0200 | [diff] [blame] | 11 | #include <linux/err.h> |
Takashi Iwai | 911761c | 2018-11-28 14:25:37 +0100 | [diff] [blame] | 12 | #include <sound/jack.h> |
David Henningsson | a5062de | 2014-09-23 09:04:49 +0200 | [diff] [blame] | 13 | |
Takashi Iwai | 128bc4b | 2012-05-07 17:42:31 +0200 | [diff] [blame] | 14 | struct auto_pin_cfg; |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 15 | struct hda_jack_tbl; |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 16 | struct hda_jack_callback; |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 17 | |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 18 | typedef void (*hda_jack_callback_fn) (struct hda_codec *, struct hda_jack_callback *); |
| 19 | |
| 20 | struct hda_jack_callback { |
Takashi Iwai | 2ebab40 | 2016-02-09 10:23:52 +0100 | [diff] [blame] | 21 | hda_nid_t nid; |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 22 | int dev_id; |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 23 | hda_jack_callback_fn func; |
| 24 | unsigned int private_data; /* arbitrary data */ |
Takashi Iwai | e6ce180 | 2018-11-28 14:17:58 +0100 | [diff] [blame] | 25 | unsigned int unsol_res; /* unsolicited event bits */ |
| 26 | struct hda_jack_tbl *jack; /* associated jack entry */ |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 27 | struct hda_jack_callback *next; |
| 28 | }; |
Takashi Iwai | 128bc4b | 2012-05-07 17:42:31 +0200 | [diff] [blame] | 29 | |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 30 | struct hda_jack_tbl { |
| 31 | hda_nid_t nid; |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 32 | int dev_id; |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 33 | unsigned char tag; /* unsol event tag */ |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 34 | struct hda_jack_callback *callback; |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 35 | /* jack-detection stuff */ |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 36 | unsigned int pin_sense; /* cached pin-sense value */ |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 37 | unsigned int jack_detect:1; /* capable of jack-detection? */ |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 38 | unsigned int jack_dirty:1; /* needs to update? */ |
David Henningsson | 80c8bfb | 2012-06-04 09:33:51 +0200 | [diff] [blame] | 39 | unsigned int phantom_jack:1; /* a fixed, always present port? */ |
David Henningsson | 0f56895 | 2013-11-07 13:38:24 +0100 | [diff] [blame] | 40 | unsigned int block_report:1; /* in a transitional state - do not report to userspace */ |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 41 | hda_nid_t gating_jack; /* valid when gating jack plugged */ |
| 42 | hda_nid_t gated_jack; /* gated is dependent on this jack */ |
Takashi Iwai | aad37db | 2011-11-02 08:54:51 +0100 | [diff] [blame] | 43 | int type; |
Takashi Iwai | 911761c | 2018-11-28 14:25:37 +0100 | [diff] [blame] | 44 | int button_state; |
Takashi Iwai | aad37db | 2011-11-02 08:54:51 +0100 | [diff] [blame] | 45 | struct snd_jack *jack; |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 46 | }; |
| 47 | |
Takashi Iwai | 911761c | 2018-11-28 14:25:37 +0100 | [diff] [blame] | 48 | struct hda_jack_keymap { |
| 49 | enum snd_jack_types type; |
| 50 | int key; |
| 51 | }; |
| 52 | |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 53 | struct hda_jack_tbl * |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 54 | snd_hda_jack_tbl_get_mst(struct hda_codec *codec, hda_nid_t nid, int dev_id); |
| 55 | |
| 56 | /** |
| 57 | * snd_hda_jack_tbl_get - query the jack-table entry for the given NID |
| 58 | * @codec: the HDA codec |
| 59 | * @nid: pin NID to refer to |
| 60 | */ |
| 61 | static inline struct hda_jack_tbl * |
| 62 | snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid) |
| 63 | { |
| 64 | return snd_hda_jack_tbl_get_mst(codec, nid, 0); |
| 65 | } |
| 66 | |
Takashi Iwai | 3a93897 | 2011-10-28 01:16:55 +0200 | [diff] [blame] | 67 | struct hda_jack_tbl * |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 68 | snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, |
| 69 | unsigned char tag, int dev_id); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 70 | |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 71 | void snd_hda_jack_tbl_clear(struct hda_codec *codec); |
| 72 | |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 73 | void snd_hda_jack_set_dirty_all(struct hda_codec *codec); |
| 74 | |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 75 | int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid, |
| 76 | int dev_id); |
| 77 | |
Takashi Iwai | 1a4f69d | 2014-09-11 15:22:46 +0200 | [diff] [blame] | 78 | struct hda_jack_callback * |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 79 | snd_hda_jack_detect_enable_callback_mst(struct hda_codec *codec, hda_nid_t nid, |
| 80 | int dev_id, hda_jack_callback_fn cb); |
| 81 | |
| 82 | /** |
| 83 | * snd_hda_jack_detect_enable - enable the jack-detection |
| 84 | * @codec: the HDA codec |
| 85 | * @nid: pin NID to enable |
| 86 | * @func: callback function to register |
| 87 | * |
| 88 | * In the case of error, the return value will be a pointer embedded with |
| 89 | * errno. Check and handle the return value appropriately with standard |
| 90 | * macros such as @IS_ERR() and @PTR_ERR(). |
| 91 | */ |
| 92 | static inline struct hda_jack_callback * |
Takashi Iwai | bda17b82 | 2014-09-11 14:39:09 +0200 | [diff] [blame] | 93 | snd_hda_jack_detect_enable_callback(struct hda_codec *codec, hda_nid_t nid, |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 94 | hda_jack_callback_fn cb) |
| 95 | { |
| 96 | return snd_hda_jack_detect_enable_callback_mst(codec, nid, 0, cb); |
| 97 | } |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 98 | |
Dylan Reid | 0619ba8 | 2012-11-19 19:48:07 +0100 | [diff] [blame] | 99 | int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid, |
| 100 | hda_nid_t gating_nid); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 101 | |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 102 | u32 snd_hda_jack_pin_sense(struct hda_codec *codec, hda_nid_t nid, int dev_id); |
Takashi Iwai | 60ea8ca | 2013-07-19 16:59:46 +0200 | [diff] [blame] | 103 | |
| 104 | /* the jack state returned from snd_hda_jack_detect_state() */ |
| 105 | enum { |
| 106 | HDA_JACK_NOT_PRESENT, HDA_JACK_PRESENT, HDA_JACK_PHANTOM, |
| 107 | }; |
| 108 | |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 109 | int snd_hda_jack_detect_state_mst(struct hda_codec *codec, hda_nid_t nid, |
| 110 | int dev_id); |
| 111 | |
| 112 | /** |
| 113 | * snd_hda_jack_detect_state - query pin Presence Detect status |
| 114 | * @codec: the CODEC to sense |
| 115 | * @nid: the pin NID to sense |
| 116 | * |
| 117 | * Query and return the pin's Presence Detect status, as either |
| 118 | * HDA_JACK_NOT_PRESENT, HDA_JACK_PRESENT or HDA_JACK_PHANTOM. |
| 119 | */ |
| 120 | static inline int |
| 121 | snd_hda_jack_detect_state(struct hda_codec *codec, hda_nid_t nid) |
| 122 | { |
| 123 | return snd_hda_jack_detect_state_mst(codec, nid, 0); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * snd_hda_jack_detect_mst - Detect the jack |
| 128 | * @codec: the HDA codec |
| 129 | * @nid: pin NID to check jack detection |
| 130 | * @dev_id: pin device entry id |
| 131 | */ |
| 132 | static inline bool |
| 133 | snd_hda_jack_detect_mst(struct hda_codec *codec, hda_nid_t nid, int dev_id) |
| 134 | { |
| 135 | return snd_hda_jack_detect_state_mst(codec, nid, dev_id) != |
| 136 | HDA_JACK_NOT_PRESENT; |
| 137 | } |
Takashi Iwai | 60ea8ca | 2013-07-19 16:59:46 +0200 | [diff] [blame] | 138 | |
Takashi Iwai | 95a962c | 2014-10-29 16:03:58 +0100 | [diff] [blame] | 139 | /** |
| 140 | * snd_hda_jack_detect - Detect the jack |
| 141 | * @codec: the HDA codec |
| 142 | * @nid: pin NID to check jack detection |
| 143 | */ |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 144 | static inline bool |
| 145 | snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid) |
Takashi Iwai | 60ea8ca | 2013-07-19 16:59:46 +0200 | [diff] [blame] | 146 | { |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 147 | return snd_hda_jack_detect_mst(codec, nid, 0); |
Takashi Iwai | 60ea8ca | 2013-07-19 16:59:46 +0200 | [diff] [blame] | 148 | } |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 149 | |
Takashi Iwai | a9c7417 | 2012-02-13 11:41:42 +0100 | [diff] [blame] | 150 | bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid); |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 151 | |
Nikhil Mahale | 5204a05 | 2019-11-19 14:17:08 +0530 | [diff] [blame] | 152 | int snd_hda_jack_add_kctl_mst(struct hda_codec *codec, hda_nid_t nid, |
| 153 | int dev_id, const char *name, bool phantom_jack, |
| 154 | int type, const struct hda_jack_keymap *keymap); |
| 155 | |
| 156 | /** |
| 157 | * snd_hda_jack_add_kctl - Add a kctl for the given pin |
| 158 | * @codec: the HDA codec |
| 159 | * @nid: pin NID to assign |
| 160 | * @name: string name for the jack |
| 161 | * @phantom_jack: flag to deal as a phantom jack |
| 162 | * @type: jack type bits to be reported, 0 for guessing from pincfg |
| 163 | * @keymap: optional jack / key mapping |
| 164 | * |
| 165 | * This assigns a jack-detection kctl to the given pin. The kcontrol |
| 166 | * will have the given name and index. |
| 167 | */ |
| 168 | static inline int |
| 169 | snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, |
| 170 | const char *name, bool phantom_jack, |
| 171 | int type, const struct hda_jack_keymap *keymap) |
| 172 | { |
| 173 | return snd_hda_jack_add_kctl_mst(codec, nid, 0, |
| 174 | name, phantom_jack, type, keymap); |
| 175 | } |
| 176 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 177 | int snd_hda_jack_add_kctls(struct hda_codec *codec, |
| 178 | const struct auto_pin_cfg *cfg); |
| 179 | |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 180 | void snd_hda_jack_report_sync(struct hda_codec *codec); |
| 181 | |
David Henningsson | 954df2a | 2012-09-25 11:30:59 +0200 | [diff] [blame] | 182 | void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res); |
Takashi Iwai | 01a61e1 | 2011-10-28 00:03:22 +0200 | [diff] [blame] | 183 | |
David Henningsson | 26a6cb6 | 2012-10-09 15:04:21 +0200 | [diff] [blame] | 184 | void snd_hda_jack_poll_all(struct hda_codec *codec); |
| 185 | |
Takashi Iwai | 1835a0f | 2011-10-27 22:12:46 +0200 | [diff] [blame] | 186 | #endif /* __SOUND_HDA_JACK_H */ |