Extcon: support mutually exclusive relation between cables.
[deliverable/linux.git] / include / linux / extcon.h
1 /*
2 * External connector (extcon) class driver
3 *
4 * Copyright (C) 2012 Samsung Electronics
5 * Author: Donggeun Kim <dg77.kim@samsung.com>
6 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
7 *
8 * based on switch class driver
9 * Copyright (C) 2008 Google, Inc.
10 * Author: Mike Lockwood <lockwood@android.com>
11 *
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.
15 *
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.
20 *
21 */
22
23 #ifndef __LINUX_EXTCON_H__
24 #define __LINUX_EXTCON_H__
25
26 #include <linux/notifier.h>
27
28 #define SUPPORTED_CABLE_MAX 32
29 #define CABLE_NAME_MAX 30
30
31 /*
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.
38 *
39 * Please add any other "standard" cables used with extcon dev.
40 *
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.
48 */
49 enum extcon_cable_name {
50 EXTCON_USB = 0,
51 EXTCON_USB_HOST,
52 EXTCON_TA, /* Travel Adaptor */
53 EXTCON_FAST_CHARGER,
54 EXTCON_SLOW_CHARGER,
55 EXTCON_CHARGE_DOWNSTREAM, /* Charging an external device */
56 EXTCON_HDMI,
57 EXTCON_MHL,
58 EXTCON_DVI,
59 EXTCON_VGA,
60 EXTCON_DOCK,
61 EXTCON_LINE_IN,
62 EXTCON_LINE_OUT,
63 EXTCON_MIC_IN,
64 EXTCON_HEADPHONE_OUT,
65 EXTCON_SPDIF_IN,
66 EXTCON_SPDIF_OUT,
67 EXTCON_VIDEO_IN,
68 EXTCON_VIDEO_OUT,
69 };
70 extern const char *extcon_cable_name[];
71
72 struct extcon_cable;
73
74 /**
75 * struct extcon_dev - An extcon device represents one external connector.
76 * @name The name of this extcon device. Parent device name is used
77 * if NULL.
78 * @supported_cable Array of supported cable name ending with NULL.
79 * If supported_cable is NULL, cable name related APIs
80 * are disabled.
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
95 * register-time
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.
99 * @lock
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.
104 *
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.
109 */
110 struct extcon_dev {
111 /* --- Optional user initializing data --- */
112 const char *name;
113 const char **supported_cable;
114 const u32 *mutually_exclusive;
115
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);
119
120 /* --- Internal data. Please do not set. --- */
121 struct device *dev;
122 u32 state;
123 struct raw_notifier_head nh;
124 struct list_head entry;
125 spinlock_t lock; /* could be called by irq handler */
126 int max_supported;
127
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;
135 };
136
137 /**
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
145 */
146 struct extcon_cable {
147 struct extcon_dev *edev;
148 int cable_index;
149
150 struct attribute_group attr_g;
151 struct device_attribute attr_name;
152 struct device_attribute attr_state;
153
154 struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
155 };
156
157 /**
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.
165 */
166 struct extcon_specific_cable_nb {
167 struct notifier_block internal_nb;
168 struct notifier_block *user_nb;
169 int cable_index;
170 struct extcon_dev *edev;
171 unsigned long previous_value;
172 };
173
174 #if IS_ENABLED(CONFIG_EXTCON)
175
176 /*
177 * Following APIs are for notifiers or configurations.
178 * Notifiers are the external port and connection devices.
179 */
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);
183
184 /*
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.
189 */
190 static inline u32 extcon_get_state(struct extcon_dev *edev)
191 {
192 return edev->state;
193 }
194
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);
197
198 /*
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
202 */
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,
207 bool cable_state);
208
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);
213
214 /*
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.
219 */
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);
225
226 /*
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.
232 */
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,
239 struct device *dev)
240 {
241 return 0;
242 }
243
244 static inline void extcon_dev_unregister(struct extcon_dev *edev) { }
245
246 static inline u32 extcon_get_state(struct extcon_dev *edev)
247 {
248 return 0;
249 }
250
251 static inline int extcon_set_state(struct extcon_dev *edev, u32 state)
252 {
253 return 0;
254 }
255
256 static inline int extcon_update_state(struct extcon_dev *edev, u32 mask,
257 u32 state)
258 {
259 return 0;
260 }
261
262 static inline int extcon_find_cable_index(struct extcon_dev *edev,
263 const char *cable_name)
264 {
265 return 0;
266 }
267
268 static inline int extcon_get_cable_state_(struct extcon_dev *edev,
269 int cable_index)
270 {
271 return 0;
272 }
273
274 static inline int extcon_set_cable_state_(struct extcon_dev *edev,
275 int cable_index, bool cable_state)
276 {
277 return 0;
278 }
279
280 static inline int extcon_get_cable_state(struct extcon_dev *edev,
281 const char *cable_name)
282 {
283 return 0;
284 }
285
286 static inline int extcon_set_cable_state(struct extcon_dev *edev,
287 const char *cable_name, int state)
288 {
289 return 0;
290 }
291
292 static inline struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
293 {
294 return NULL;
295 }
296
297 static inline int extcon_register_notifier(struct extcon_dev *edev,
298 struct notifier_block *nb)
299 {
300 return 0;
301 }
302
303 static inline int extcon_unregister_notifier(struct extcon_dev *edev,
304 struct notifier_block *nb)
305 {
306 return 0;
307 }
308
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)
313 {
314 return 0;
315 }
316
317 static inline int extcon_unregister_interest(struct extcon_specific_cable_nb
318 *obj)
319 {
320 return 0;
321 }
322 #endif /* CONFIG_EXTCON */
323 #endif /* __LINUX_EXTCON_H__ */
This page took 0.038295 seconds and 5 git commands to generate.