2 * External connector (extcon) class driver
4 * Copyright (C) 2012 Samsung Electronics
5 * Author: Donggeun Kim <dg77.kim@samsung.com>
6 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
8 * based on switch class driver
9 * Copyright (C) 2008 Google, Inc.
10 * Author: Mike Lockwood <lockwood@android.com>
12 * This software is licensed under the terms of the GNU General Public
13 * License version 2, as published by the Free Software Foundation, and
14 * may be copied, distributed, and modified under those terms.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
23 #ifndef __LINUX_EXTCON_H__
24 #define __LINUX_EXTCON_H__
26 #include <linux/device.h>
27 #include <linux/notifier.h>
28 #include <linux/sysfs.h>
30 #define SUPPORTED_CABLE_MAX 32
31 #define CABLE_NAME_MAX 30
34 * The standard cable name is to help support general notifier
35 * and notifiee device drivers to share the common names.
36 * Please use standard cable names unless your notifier device has
37 * a very unique and abnormal cable or
38 * the cable type is supposed to be used with only one unique
39 * pair of notifier/notifiee devices.
41 * Please add any other "standard" cables used with extcon dev.
43 * You may add a dot and number to specify version or specification
44 * of the specific cable if it is required. (e.g., "Fast-charger.18"
45 * and "Fast-charger.10" for 1.8A and 1.0A chargers)
46 * However, the notifiee and notifier should be able to handle such
47 * string and if the notifiee can negotiate the protocol or identify,
48 * you don't need such convention. This convention is helpful when
49 * notifier can distinguish but notifiee cannot.
51 enum extcon_cable_name {
54 EXTCON_TA, /* Travel Adaptor */
57 EXTCON_CHARGE_DOWNSTREAM, /* Charging an external device */
73 extern const char extcon_cable_name[][CABLE_NAME_MAX + 1];
78 * struct extcon_dev - An extcon device represents one external connector.
79 * @name: The name of this extcon device. Parent device name is
81 * @supported_cable: Array of supported cable names ending with NULL.
82 * If supported_cable is NULL, cable name related APIs
84 * @mutually_exclusive: Array of mutually exclusive set of cables that cannot
85 * be attached simultaneously. The array should be
86 * ending with NULL or be NULL (no mutually exclusive
87 * cables). For example, if it is { 0x7, 0x30, 0}, then,
88 * {0, 1}, {0, 1, 2}, {0, 2}, {1, 2}, or {4, 5} cannot
89 * be attached simulataneously. {0x7, 0} is equivalent to
90 * {0x3, 0x6, 0x5, 0}. If it is {0xFFFFFFFF, 0}, there
91 * can be no simultaneous connections.
92 * @print_name: An optional callback to override the method to print the
93 * name of the extcon device.
94 * @print_state: An optional callback to override the method to print the
95 * status of the extcon device.
96 * @dev: Device of this extcon.
97 * @state: Attach/detach state of this extcon. Do not provide at
99 * @nh: Notifier for the state change events from this extcon
100 * @entry: To support list of extcon devices so that users can search
101 * for extcon devices based on the extcon name.
103 * @max_supported: Internal value to store the number of cables.
104 * @extcon_dev_type: Device_type struct to provide attribute_groups
105 * customized for each extcon device.
106 * @cables: Sysfs subdirectories. Each represents one cable.
108 * In most cases, users only need to provide "User initializing data" of
109 * this struct when registering an extcon. In some exceptional cases,
110 * optional callbacks may be needed. However, the values in "internal data"
111 * are overwritten by register function.
114 /* Optional user initializing data */
116 const char **supported_cable;
117 const u32 *mutually_exclusive;
119 /* Optional callbacks to override class functions */
120 ssize_t (*print_name)(struct extcon_dev *edev, char *buf);
121 ssize_t (*print_state)(struct extcon_dev *edev, char *buf);
123 /* Internal data. Please do not set. */
125 struct raw_notifier_head nh;
126 struct list_head entry;
128 spinlock_t lock; /* could be called by irq handler */
131 /* /sys/class/extcon/.../cable.n/... */
132 struct device_type extcon_dev_type;
133 struct extcon_cable *cables;
135 /* /sys/class/extcon/.../mutually_exclusive/... */
136 struct attribute_group attr_g_muex;
137 struct attribute **attrs_muex;
138 struct device_attribute *d_attrs_muex;
142 * struct extcon_cable - An internal data for each cable of extcon device.
143 * @edev: The extcon device
144 * @cable_index: Index of this cable in the edev
145 * @attr_g: Attribute group for the cable
146 * @attr_name: "name" sysfs entry
147 * @attr_state: "state" sysfs entry
148 * @attrs: Array pointing to attr_name and attr_state for attr_g
150 struct extcon_cable {
151 struct extcon_dev *edev;
154 struct attribute_group attr_g;
155 struct device_attribute attr_name;
156 struct device_attribute attr_state;
158 struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
162 * struct extcon_specific_cable_nb - An internal data for
163 * extcon_register_interest().
164 * @internal_nb: A notifier block bridging extcon notifier
165 * and cable notifier.
166 * @user_nb: user provided notifier block for events from
168 * @cable_index: the target cable.
169 * @edev: the target extcon device.
170 * @previous_value: the saved previous event value.
172 struct extcon_specific_cable_nb {
173 struct notifier_block internal_nb;
174 struct notifier_block *user_nb;
176 struct extcon_dev *edev;
177 unsigned long previous_value;
180 #if IS_ENABLED(CONFIG_EXTCON)
183 * Following APIs are for notifiers or configurations.
184 * Notifiers are the external port and connection devices.
186 extern int extcon_dev_register(struct extcon_dev *edev);
187 extern void extcon_dev_unregister(struct extcon_dev *edev);
188 extern struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name);
191 * get/set/update_state access the 32b encoded state value, which represents
192 * states of all possible cables of the multistate port. For example, if one
193 * calls extcon_set_state(edev, 0x7), it may mean that all the three cables
194 * are attached to the port.
196 static inline u32 extcon_get_state(struct extcon_dev *edev)
201 extern int extcon_set_state(struct extcon_dev *edev, u32 state);
202 extern int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state);
205 * get/set_cable_state access each bit of the 32b encoded state value.
206 * They are used to access the status of each cable based on the cable_name
207 * or cable_index, which is retrieved by extcon_find_cable_index
209 extern int extcon_find_cable_index(struct extcon_dev *sdev,
210 const char *cable_name);
211 extern int extcon_get_cable_state_(struct extcon_dev *edev, int cable_index);
212 extern int extcon_set_cable_state_(struct extcon_dev *edev, int cable_index,
215 extern int extcon_get_cable_state(struct extcon_dev *edev,
216 const char *cable_name);
217 extern int extcon_set_cable_state(struct extcon_dev *edev,
218 const char *cable_name, bool cable_state);
221 * Following APIs are for notifiees (those who want to be notified)
222 * to register a callback for events from a specific cable of the extcon.
223 * Notifiees are the connected device drivers wanting to get notified by
224 * a specific external port of a connection device.
226 extern int extcon_register_interest(struct extcon_specific_cable_nb *obj,
227 const char *extcon_name,
228 const char *cable_name,
229 struct notifier_block *nb);
230 extern int extcon_unregister_interest(struct extcon_specific_cable_nb *nb);
233 * Following APIs are to monitor every action of a notifier.
234 * Registrar gets notified for every external port of a connection device.
235 * Probably this could be used to debug an action of notifier; however,
236 * we do not recommend to use this for normal 'notifiee' device drivers who
237 * want to be notified by a specific external port of the notifier.
239 extern int extcon_register_notifier(struct extcon_dev *edev,
240 struct notifier_block *nb);
241 extern int extcon_unregister_notifier(struct extcon_dev *edev,
242 struct notifier_block *nb);
245 * Following API get the extcon device from devicetree.
246 * This function use phandle of devicetree to get extcon device directly.
248 extern struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index);
249 #else /* CONFIG_EXTCON */
250 static inline int extcon_dev_register(struct extcon_dev *edev)
255 static inline void extcon_dev_unregister(struct extcon_dev *edev) { }
257 static inline u32 extcon_get_state(struct extcon_dev *edev)
262 static inline int extcon_set_state(struct extcon_dev *edev, u32 state)
267 static inline int extcon_update_state(struct extcon_dev *edev, u32 mask,
273 static inline int extcon_find_cable_index(struct extcon_dev *edev,
274 const char *cable_name)
279 static inline int extcon_get_cable_state_(struct extcon_dev *edev,
285 static inline int extcon_set_cable_state_(struct extcon_dev *edev,
286 int cable_index, bool cable_state)
291 static inline int extcon_get_cable_state(struct extcon_dev *edev,
292 const char *cable_name)
297 static inline int extcon_set_cable_state(struct extcon_dev *edev,
298 const char *cable_name, int state)
303 static inline struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
308 static inline int extcon_register_notifier(struct extcon_dev *edev,
309 struct notifier_block *nb)
314 static inline int extcon_unregister_notifier(struct extcon_dev *edev,
315 struct notifier_block *nb)
320 static inline int extcon_register_interest(struct extcon_specific_cable_nb *obj,
321 const char *extcon_name,
322 const char *cable_name,
323 struct notifier_block *nb)
328 static inline int extcon_unregister_interest(struct extcon_specific_cable_nb
334 static inline struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev,
337 return ERR_PTR(-ENODEV);
339 #endif /* CONFIG_EXTCON */
340 #endif /* __LINUX_EXTCON_H__ */