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/notifier.h>
28 #define SUPPORTED_CABLE_MAX 32
29 #define CABLE_NAME_MAX 30
32 * The standard cable name is to help support general notifier
33 * and notifee device drivers to share the common names.
34 * Please use standard cable names unless your notifier device has
35 * a very unique and abnormal cable or
36 * the cable type is supposed to be used with only one unique
37 * pair of notifier/notifee devices.
39 * Please add any other "standard" cables used with extcon dev.
41 * You may add a dot and number to specify version or specification
42 * of the specific cable if it is required. (e.g., "Fast-charger.18"
43 * and "Fast-charger.10" for 1.8A and 1.0A chargers)
44 * However, the notifee and notifier should be able to handle such
45 * string and if the notifee can negotiate the protocol or idenify,
46 * you don't need such convention. This convention is helpful when
47 * notifier can distinguish but notifiee cannot.
49 enum extcon_cable_name
{
52 EXTCON_TA
, /* Travel Adaptor */
55 EXTCON_CHARGE_DOWNSTREAM
, /* Charging an external device */
70 extern const char *extcon_cable_name
[];
75 * struct extcon_dev - An extcon device represents one external connector.
76 * @name The name of this extcon device. Parent device name is used
78 * @supported_cable Array of supported cable name ending with NULL.
79 * If supported_cable is NULL, cable name related APIs
81 * @mutually_exclusive Array of mutually exclusive set of cables that cannot
82 * be attached simultaneously. The array should be
83 * ending with NULL or be NULL (no mutually exclusive
84 * cables). For example, if it is { 0x7, 0x30, 0}, then,
85 * {0, 1}, {0, 1, 2}, {0, 2}, {1, 2}, or {4, 5} cannot
86 * be attached simulataneously. {0x7, 0} is equivalent to
87 * {0x3, 0x6, 0x5, 0}. If it is {0xFFFFFFFF, 0}, there
88 * can be no simultaneous connections.
89 * @print_name An optional callback to override the method to print the
90 * name of the extcon device.
91 * @print_state An optional callback to override the method to print the
92 * status of the extcon device.
93 * @dev Device of this extcon. Do not provide at register-time.
94 * @state Attach/detach state of this extcon. Do not provide at
96 * @nh Notifier for the state change events from this extcon
97 * @entry To support list of extcon devices so that uses can search
98 * for extcon devices based on the extcon name.
100 * @max_supported Internal value to store the number of cables.
101 * @extcon_dev_type Device_type struct to provide attribute_groups
102 * customized for each extcon device.
103 * @cables Sysfs subdirectories. Each represents one cable.
105 * In most cases, users only need to provide "User initializing data" of
106 * this struct when registering an extcon. In some exceptional cases,
107 * optional callbacks may be needed. However, the values in "internal data"
108 * are overwritten by register function.
111 /* --- Optional user initializing data --- */
113 const char **supported_cable
;
114 const u32
*mutually_exclusive
;
116 /* --- Optional callbacks to override class functions --- */
117 ssize_t (*print_name
)(struct extcon_dev
*edev
, char *buf
);
118 ssize_t (*print_state
)(struct extcon_dev
*edev
, char *buf
);
120 /* --- Internal data. Please do not set. --- */
123 struct raw_notifier_head nh
;
124 struct list_head entry
;
125 spinlock_t lock
; /* could be called by irq handler */
128 /* /sys/class/extcon/.../cable.n/... */
129 struct device_type extcon_dev_type
;
130 struct extcon_cable
*cables
;
131 /* /sys/class/extcon/.../mutually_exclusive/... */
132 struct attribute_group attr_g_muex
;
133 struct attribute
**attrs_muex
;
134 struct device_attribute
*d_attrs_muex
;
138 * struct extcon_cable - An internal data for each cable of extcon device.
139 * @edev The extcon device
140 * @cable_index Index of this cable in the edev
141 * @attr_g Attribute group for the cable
142 * @attr_name "name" sysfs entry
143 * @attr_state "state" sysfs entry
144 * @attrs Array pointing to attr_name and attr_state for attr_g
146 struct extcon_cable
{
147 struct extcon_dev
*edev
;
150 struct attribute_group attr_g
;
151 struct device_attribute attr_name
;
152 struct device_attribute attr_state
;
154 struct attribute
*attrs
[3]; /* to be fed to attr_g.attrs */
158 * struct extcon_specific_cable_nb - An internal data for
159 * extcon_register_interest().
160 * @internal_nb a notifier block bridging extcon notifier and cable notifier.
161 * @user_nb user provided notifier block for events from a specific cable.
162 * @cable_index the target cable.
163 * @edev the target extcon device.
164 * @previous_value the saved previous event value.
166 struct extcon_specific_cable_nb
{
167 struct notifier_block internal_nb
;
168 struct notifier_block
*user_nb
;
170 struct extcon_dev
*edev
;
171 unsigned long previous_value
;
174 #if IS_ENABLED(CONFIG_EXTCON)
177 * Following APIs are for notifiers or configurations.
178 * Notifiers are the external port and connection devices.
180 extern int extcon_dev_register(struct extcon_dev
*edev
, struct device
*dev
);
181 extern void extcon_dev_unregister(struct extcon_dev
*edev
);
182 extern struct extcon_dev
*extcon_get_extcon_dev(const char *extcon_name
);
185 * get/set/update_state access the 32b encoded state value, which represents
186 * states of all possible cables of the multistate port. For example, if one
187 * calls extcon_set_state(edev, 0x7), it may mean that all the three cables
188 * are attached to the port.
190 static inline u32
extcon_get_state(struct extcon_dev
*edev
)
195 extern int extcon_set_state(struct extcon_dev
*edev
, u32 state
);
196 extern int extcon_update_state(struct extcon_dev
*edev
, u32 mask
, u32 state
);
199 * get/set_cable_state access each bit of the 32b encoded state value.
200 * They are used to access the status of each cable based on the cable_name
201 * or cable_index, which is retrived by extcon_find_cable_index
203 extern int extcon_find_cable_index(struct extcon_dev
*sdev
,
204 const char *cable_name
);
205 extern int extcon_get_cable_state_(struct extcon_dev
*edev
, int cable_index
);
206 extern int extcon_set_cable_state_(struct extcon_dev
*edev
, int cable_index
,
209 extern int extcon_get_cable_state(struct extcon_dev
*edev
,
210 const char *cable_name
);
211 extern int extcon_set_cable_state(struct extcon_dev
*edev
,
212 const char *cable_name
, bool cable_state
);
215 * Following APIs are for notifiees (those who want to be notified)
216 * to register a callback for events from a specific cable of the extcon.
217 * Notifiees are the connected device drivers wanting to get notified by
218 * a specific external port of a connection device.
220 extern int extcon_register_interest(struct extcon_specific_cable_nb
*obj
,
221 const char *extcon_name
,
222 const char *cable_name
,
223 struct notifier_block
*nb
);
224 extern int extcon_unregister_interest(struct extcon_specific_cable_nb
*nb
);
227 * Following APIs are to monitor every action of a notifier.
228 * Registerer gets notified for every external port of a connection device.
229 * Probably this could be used to debug an action of notifier; however,
230 * we do not recommend to use this at normal 'notifiee' device drivers who
231 * want to be notified by a specific external port of the notifier.
233 extern int extcon_register_notifier(struct extcon_dev
*edev
,
234 struct notifier_block
*nb
);
235 extern int extcon_unregister_notifier(struct extcon_dev
*edev
,
236 struct notifier_block
*nb
);
237 #else /* CONFIG_EXTCON */
238 static inline int extcon_dev_register(struct extcon_dev
*edev
,
244 static inline void extcon_dev_unregister(struct extcon_dev
*edev
) { }
246 static inline u32
extcon_get_state(struct extcon_dev
*edev
)
251 static inline int extcon_set_state(struct extcon_dev
*edev
, u32 state
)
256 static inline int extcon_update_state(struct extcon_dev
*edev
, u32 mask
,
262 static inline int extcon_find_cable_index(struct extcon_dev
*edev
,
263 const char *cable_name
)
268 static inline int extcon_get_cable_state_(struct extcon_dev
*edev
,
274 static inline int extcon_set_cable_state_(struct extcon_dev
*edev
,
275 int cable_index
, bool cable_state
)
280 static inline int extcon_get_cable_state(struct extcon_dev
*edev
,
281 const char *cable_name
)
286 static inline int extcon_set_cable_state(struct extcon_dev
*edev
,
287 const char *cable_name
, int state
)
292 static inline struct extcon_dev
*extcon_get_extcon_dev(const char *extcon_name
)
297 static inline int extcon_register_notifier(struct extcon_dev
*edev
,
298 struct notifier_block
*nb
)
303 static inline int extcon_unregister_notifier(struct extcon_dev
*edev
,
304 struct notifier_block
*nb
)
309 static inline int extcon_register_interest(struct extcon_specific_cable_nb
*obj
,
310 const char *extcon_name
,
311 const char *cable_name
,
312 struct notifier_block
*nb
)
317 static inline int extcon_unregister_interest(struct extcon_specific_cable_nb
322 #endif /* CONFIG_EXTCON */
323 #endif /* __LINUX_EXTCON_H__ */