1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * External Connector (extcon) framework
4  * - linux/include/linux/extcon.h for extcon consumer device driver.
5  *
6  * Copyright (C) 2015 Samsung Electronics
7  * Author: Chanwoo Choi <cw00.choi@samsung.com>
8  *
9  * Copyright (C) 2012 Samsung Electronics
10  * Author: Donggeun Kim <dg77.kim@samsung.com>
11  * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
12  *
13  * based on switch class driver
14  * Copyright (C) 2008 Google, Inc.
15  * Author: Mike Lockwood <lockwood@android.com>
16  */
17 
18 #ifndef __LINUX_EXTCON_H__
19 #define __LINUX_EXTCON_H__
20 
21 #include <linux/device.h>
22 
23 /*
24  * Define the type of supported external connectors
25  */
26 #define EXTCON_TYPE_USB BIT(0)  /* USB connector */
27 #define EXTCON_TYPE_CHG BIT(1)  /* Charger connector */
28 #define EXTCON_TYPE_JACK BIT(2) /* Jack connector */
29 #define EXTCON_TYPE_DISP BIT(3) /* Display connector */
30 #define EXTCON_TYPE_MISC BIT(4) /* Miscellaneous connector */
31 
32 /*
33  * Define the unique id of supported external connectors
34  */
35 #define EXTCON_NONE 0
36 
37 /* USB external connector */
38 #define EXTCON_USB 1
39 #define EXTCON_USB_HOST 2
40 #define EXTCON_USB_VBUS_EN 3
41 
42 /*
43  * Charging external connector
44  *
45  * When one SDP charger connector was reported, we should also report
46  * the USB connector, which means EXTCON_CHG_USB_SDP should always
47  * appear together with EXTCON_USB. The same as ACA charger connector,
48  * EXTCON_CHG_USB_ACA would normally appear with EXTCON_USB_HOST.
49  *
50  * The EXTCON_CHG_USB_SLOW connector can provide at least 500mA of
51  * current at 5V. The EXTCON_CHG_USB_FAST connector can provide at
52  * least 1A of current at 5V.
53  */
54 #define EXTCON_CHG_USB_SDP 5 /* Standard Downstream Port */
55 #define EXTCON_CHG_USB_DCP 6 /* Dedicated Charging Port */
56 #define EXTCON_CHG_USB_CDP 7 /* Charging Downstream Port */
57 #define EXTCON_CHG_USB_ACA 8 /* Accessory Charger Adapter */
58 #define EXTCON_CHG_USB_FAST 9
59 #define EXTCON_CHG_USB_SLOW 10
60 #define EXTCON_CHG_WPT 11    /* Wireless Power Transfer */
61 #define EXTCON_CHG_USB_PD 12 /* USB Power Delivery */
62 
63 /* Jack external connector */
64 #define EXTCON_JACK_MICROPHONE 20
65 #define EXTCON_JACK_HEADPHONE 21
66 #define EXTCON_JACK_LINE_IN 22
67 #define EXTCON_JACK_LINE_OUT 23
68 #define EXTCON_JACK_VIDEO_IN 24
69 #define EXTCON_JACK_VIDEO_OUT 25
70 #define EXTCON_JACK_SPDIF_IN 26 /* Sony Philips Digital InterFace */
71 #define EXTCON_JACK_SPDIF_OUT 27
72 
73 /* Display external connector */
74 #define EXTCON_DISP_HDMI 40 /* High-Definition Multimedia Interface */
75 #define EXTCON_DISP_MHL 41  /* Mobile High-Definition Link */
76 #define EXTCON_DISP_DVI 42  /* Digital Visual Interface */
77 #define EXTCON_DISP_VGA 43  /* Video Graphics Array */
78 #define EXTCON_DISP_DP 44   /* Display Port */
79 #define EXTCON_DISP_HMD 45  /* Head-Mounted Display */
80 #define EXTCON_DISP_CVBS 46 /* Composite Video Broadcast Signal */
81 #define EXTCON_DISP_EDP 47  /* Embedded Display Port */
82 
83 /* Miscellaneous external connector */
84 #define EXTCON_DOCK 60
85 #define EXTCON_JIG 61
86 #define EXTCON_MECHANICAL 62
87 
88 #define EXTCON_NUM 63
89 
90 /*
91  * Define the properties of supported external connectors.
92  *
93  * When adding the new extcon property, they *must* have
94  * the type/value/default information. Also, you *have to*
95  * modify the EXTCON_PROP_[type]_START/END definitions
96  * which mean the range of the supported properties
97  * for each extcon type.
98  *
99  * The naming style of property
100  * : EXTCON_PROP_[type]_[property name]
101  *
102  * EXTCON_PROP_USB_[property name]    : USB property
103  * EXTCON_PROP_CHG_[property name]    : Charger property
104  * EXTCON_PROP_JACK_[property name]    : Jack property
105  * EXTCON_PROP_DISP_[property name]    : Display property
106  */
107 
108 /*
109  * Properties of EXTCON_TYPE_USB.
110  *
111  * - EXTCON_PROP_USB_VBUS
112  * @type:    integer (intval)
113  * @value:    0 (low) or 1 (high)
114  * @default:    0 (low)
115  * - EXTCON_PROP_USB_TYPEC_POLARITY
116  * @type:    integer (intval)
117  * @value:    0 (normal) or 1 (flip)
118  * @default:    0 (normal)
119  * - EXTCON_PROP_USB_SS (SuperSpeed)
120  * @type:       integer (intval)
121  * @value:      0 (USB/USB2) or 1 (USB3)
122  * @default:    0 (USB/USB2)
123  *
124  */
125 #define EXTCON_PROP_USB_VBUS 0
126 #define EXTCON_PROP_USB_TYPEC_POLARITY 1
127 #define EXTCON_PROP_USB_SS 2
128 
129 #define EXTCON_PROP_USB_MIN 0
130 #define EXTCON_PROP_USB_MAX 2
131 #define EXTCON_PROP_USB_CNT (EXTCON_PROP_USB_MAX - EXTCON_PROP_USB_MIN + 1)
132 
133 /* Properties of EXTCON_TYPE_CHG. */
134 #define EXTCON_PROP_CHG_MIN 50
135 #define EXTCON_PROP_CHG_MAX 50
136 #define EXTCON_PROP_CHG_CNT (EXTCON_PROP_CHG_MAX - EXTCON_PROP_CHG_MIN + 1)
137 
138 /* Properties of EXTCON_TYPE_JACK. */
139 #define EXTCON_PROP_JACK_MIN 100
140 #define EXTCON_PROP_JACK_MAX 100
141 #define EXTCON_PROP_JACK_CNT (EXTCON_PROP_JACK_MAX - EXTCON_PROP_JACK_MIN + 1)
142 
143 /*
144  * Properties of EXTCON_TYPE_DISP.
145  *
146  * - EXTCON_PROP_DISP_HPD (Hot Plug Detect)
147  * @type:       integer (intval)
148  * @value:      0 (no hpd) or 1 (hpd)
149  * @default:    0 (no hpd)
150  *
151  */
152 #define EXTCON_PROP_DISP_HPD 150
153 
154 /* Properties of EXTCON_TYPE_DISP. */
155 #define EXTCON_PROP_DISP_MIN 150
156 #define EXTCON_PROP_DISP_MAX 151
157 #define EXTCON_PROP_DISP_CNT (EXTCON_PROP_DISP_MAX - EXTCON_PROP_DISP_MIN + 1)
158 
159 /*
160  * Define the type of property's value.
161  *
162  * Define the property's value as union type. Because each property
163  * would need the different data type to store it.
164  */
165 union extcon_property_value {
166     int intval; /* type : integer (intval) */
167 };
168 
169 struct extcon_dev;
170 
171 #if IS_ENABLED(CONFIG_EXTCON)
172 /*
173  * Following APIs get the connected state of each external connector.
174  * The 'id' argument indicates the defined external connector.
175  */
176 int extcon_get_state(struct extcon_dev *edev, unsigned int id);
177 
178 /*
179  * Following APIs get the property of each external connector.
180  * The 'id' argument indicates the defined external connector
181  * and the 'prop' indicates the extcon property.
182  *
183  * And extcon_get_property_capability() get the capability of the property
184  * for each external connector. They are used to get the capability of the
185  * property of each external connector based on the id and property.
186  */
187 int extcon_get_property(struct extcon_dev *edev, unsigned int id, unsigned int prop,
188                         union extcon_property_value *prop_val);
189 int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id, unsigned int prop);
190 
191 /*
192  * Following APIs register the notifier block in order to detect
193  * the change of both state and property value for each external connector.
194  *
195  * extcon_register_notifier(*edev, id, *nb) : Register a notifier block
196  *            for specific external connector of the extcon.
197  * extcon_register_notifier_all(*edev, *nb) : Register a notifier block
198  *            for all supported external connectors of the extcon.
199  */
200 int extcon_register_notifier(struct extcon_dev *edev, unsigned int id, struct notifier_block *nb);
201 int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id, struct notifier_block *nb);
202 int devm_extcon_register_notifier(struct device *dev, struct extcon_dev *edev, unsigned int id,
203                                   struct notifier_block *nb);
204 void devm_extcon_unregister_notifier(struct device *dev, struct extcon_dev *edev, unsigned int id,
205                                      struct notifier_block *nb);
206 
207 int extcon_register_notifier_all(struct extcon_dev *edev, struct notifier_block *nb);
208 int extcon_unregister_notifier_all(struct extcon_dev *edev, struct notifier_block *nb);
209 int devm_extcon_register_notifier_all(struct device *dev, struct extcon_dev *edev, struct notifier_block *nb);
210 void devm_extcon_unregister_notifier_all(struct device *dev, struct extcon_dev *edev, struct notifier_block *nb);
211 
212 /*
213  * Following APIs get the extcon_dev from devicetree or by through extcon name.
214  */
215 struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name);
216 struct extcon_dev *extcon_find_edev_by_node(struct device_node *node);
217 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index);
218 
219 /* Following API get the name of extcon device. */
220 const char *extcon_get_edev_name(struct extcon_dev *edev);
221 
222 #else  /* CONFIG_EXTCON */
extcon_get_state(struct extcon_dev *edev, unsigned int id)223 static inline int extcon_get_state(struct extcon_dev *edev, unsigned int id)
224 {
225     return 0;
226 }
227 
extcon_get_property(struct extcon_dev *edev, unsigned int id, unsigned int prop, union extcon_property_value *prop_val)228 static inline int extcon_get_property(struct extcon_dev *edev, unsigned int id, unsigned int prop,
229                                       union extcon_property_value *prop_val)
230 {
231     return 0;
232 }
233 
extcon_get_property_capability(struct extcon_dev *edev, unsigned int id, unsigned int prop)234 static inline int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id, unsigned int prop)
235 {
236     return 0;
237 }
238 
extcon_register_notifier(struct extcon_dev *edev, unsigned int id, struct notifier_block *nb)239 static inline int extcon_register_notifier(struct extcon_dev *edev, unsigned int id, struct notifier_block *nb)
240 {
241     return 0;
242 }
243 
extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id, struct notifier_block *nb)244 static inline int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id, struct notifier_block *nb)
245 {
246     return 0;
247 }
248 
devm_extcon_register_notifier(struct device *dev, struct extcon_dev *edev, unsigned int id, struct notifier_block *nb)249 static inline int devm_extcon_register_notifier(struct device *dev, struct extcon_dev *edev, unsigned int id,
250                                                 struct notifier_block *nb)
251 {
252     return -ENOSYS;
253 }
254 
devm_extcon_unregister_notifier(struct device *dev, struct extcon_dev *edev, unsigned int id, struct notifier_block *nb)255 static inline void devm_extcon_unregister_notifier(struct device *dev, struct extcon_dev *edev, unsigned int id,
256                                                    struct notifier_block *nb)
257 {
258 }
259 
extcon_register_notifier_all(struct extcon_dev *edev, struct notifier_block *nb)260 static inline int extcon_register_notifier_all(struct extcon_dev *edev, struct notifier_block *nb)
261 {
262     return 0;
263 }
264 
extcon_unregister_notifier_all(struct extcon_dev *edev, struct notifier_block *nb)265 static inline int extcon_unregister_notifier_all(struct extcon_dev *edev, struct notifier_block *nb)
266 {
267     return 0;
268 }
269 
devm_extcon_register_notifier_all(struct device *dev, struct extcon_dev *edev, struct notifier_block *nb)270 static inline int devm_extcon_register_notifier_all(struct device *dev, struct extcon_dev *edev,
271                                                     struct notifier_block *nb)
272 {
273     return 0;
274 }
275 
devm_extcon_unregister_notifier_all(struct device *dev, struct extcon_dev *edev, struct notifier_block *nb)276 static inline void devm_extcon_unregister_notifier_all(struct device *dev, struct extcon_dev *edev,
277                                                        struct notifier_block *nb)
278 {
279 }
280 
extcon_get_extcon_dev(const char *extcon_name)281 static inline struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
282 {
283     return ERR_PTR(-ENODEV);
284 }
285 
extcon_find_edev_by_node(struct device_node *node)286 static inline struct extcon_dev *extcon_find_edev_by_node(struct device_node *node)
287 {
288     return ERR_PTR(-ENODEV);
289 }
290 
extcon_get_edev_by_phandle(struct device *dev, int index)291 static inline struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
292 {
293     return ERR_PTR(-ENODEV);
294 }
295 
extcon_get_edev_name(struct extcon_dev *edev)296 static inline const char *extcon_get_edev_name(struct extcon_dev *edev)
297 {
298     return NULL;
299 }
300 #endif /* CONFIG_EXTCON */
301 
302 /*
303  * Following structure and API are deprecated. EXTCON remains the function
304  * definition to prevent the build break.
305  */
306 struct extcon_specific_cable_nb {
307     struct notifier_block *user_nb;
308     int cable_index;
309     struct extcon_dev *edev;
310     unsigned long previous_value;
311 };
312 
extcon_register_interest(struct extcon_specific_cable_nb *obj, const char *extcon_name, const char *cable_name, struct notifier_block *nb)313 static inline int extcon_register_interest(struct extcon_specific_cable_nb *obj, const char *extcon_name,
314                                            const char *cable_name, struct notifier_block *nb)
315 {
316     return -EINVAL;
317 }
318 
extcon_unregister_interest(struct extcon_specific_cable_nb *obj)319 static inline int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
320 {
321     return -EINVAL;
322 }
323 #endif /* __LINUX_EXTCON_H__ */
324