blob: 6495f7731400770421352a8deab4281d1e67ab8e [file] [log] [blame]
MyungJoo Hamde55d872012-04-20 14:16:22 +09001/*
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
Donggeun Kim74c5d092012-04-20 14:16:24 +090026#include <linux/notifier.h>
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090027
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 */
49enum 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};
70extern const char *extcon_cable_name[];
71
72struct extcon_cable;
73
MyungJoo Hamde55d872012-04-20 14:16:22 +090074/**
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.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090078 * @supported_cable Array of supported cable name ending with NULL.
79 * If supported_cable is NULL, cable name related APIs
80 * are disabled.
MyungJoo Hambde68e62012-04-20 14:16:26 +090081 * @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.
MyungJoo Hamde55d872012-04-20 14:16:22 +090089 * @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
Donggeun Kim74c5d092012-04-20 14:16:24 +090096 * @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.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090099 * @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.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900104 *
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 */
110struct extcon_dev {
111 /* --- Optional user initializing data --- */
112 const char *name;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900113 const char **supported_cable;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900114 const u32 *mutually_exclusive;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900115
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;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900123 struct raw_notifier_head nh;
124 struct list_head entry;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900125 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;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900131 /* /sys/class/extcon/.../mutually_exclusive/... */
132 struct attribute_group attr_g_muex;
133 struct attribute **attrs_muex;
134 struct device_attribute *d_attrs_muex;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900135};
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 */
146struct 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 */
166struct 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;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900172};
173
174#if IS_ENABLED(CONFIG_EXTCON)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900175
176/*
177 * Following APIs are for notifiers or configurations.
178 * Notifiers are the external port and connection devices.
179 */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900180extern int extcon_dev_register(struct extcon_dev *edev, struct device *dev);
181extern void extcon_dev_unregister(struct extcon_dev *edev);
Donggeun Kim74c5d092012-04-20 14:16:24 +0900182extern struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900183
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900184/*
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 */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900190static inline u32 extcon_get_state(struct extcon_dev *edev)
191{
192 return edev->state;
193}
194
MyungJoo Hambde68e62012-04-20 14:16:26 +0900195extern int extcon_set_state(struct extcon_dev *edev, u32 state);
196extern int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900197
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 */
203extern int extcon_find_cable_index(struct extcon_dev *sdev,
204 const char *cable_name);
205extern int extcon_get_cable_state_(struct extcon_dev *edev, int cable_index);
206extern int extcon_set_cable_state_(struct extcon_dev *edev, int cable_index,
207 bool cable_state);
208
209extern int extcon_get_cable_state(struct extcon_dev *edev,
210 const char *cable_name);
211extern 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 */
220extern 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);
224extern int extcon_unregister_interest(struct extcon_specific_cable_nb *nb);
Donggeun Kim74c5d092012-04-20 14:16:24 +0900225
226/*
227 * Following APIs are to monitor every action of a notifier.
228 * Registerer gets notified for every external port of a connection device.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900229 * 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.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900232 */
233extern int extcon_register_notifier(struct extcon_dev *edev,
234 struct notifier_block *nb);
235extern int extcon_unregister_notifier(struct extcon_dev *edev,
236 struct notifier_block *nb);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900237#else /* CONFIG_EXTCON */
238static inline int extcon_dev_register(struct extcon_dev *edev,
239 struct device *dev)
240{
241 return 0;
242}
243
244static inline void extcon_dev_unregister(struct extcon_dev *edev) { }
245
246static inline u32 extcon_get_state(struct extcon_dev *edev)
247{
248 return 0;
249}
250
MyungJoo Hambde68e62012-04-20 14:16:26 +0900251static inline int extcon_set_state(struct extcon_dev *edev, u32 state)
252{
253 return 0;
254}
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900255
MyungJoo Hambde68e62012-04-20 14:16:26 +0900256static inline int extcon_update_state(struct extcon_dev *edev, u32 mask,
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900257 u32 state)
MyungJoo Hambde68e62012-04-20 14:16:26 +0900258{
259 return 0;
260}
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900261
262static inline int extcon_find_cable_index(struct extcon_dev *edev,
263 const char *cable_name)
264{
265 return 0;
266}
267
268static inline int extcon_get_cable_state_(struct extcon_dev *edev,
269 int cable_index)
270{
271 return 0;
272}
273
274static inline int extcon_set_cable_state_(struct extcon_dev *edev,
275 int cable_index, bool cable_state)
276{
277 return 0;
278}
279
280static inline int extcon_get_cable_state(struct extcon_dev *edev,
281 const char *cable_name)
282{
283 return 0;
284}
285
286static inline int extcon_set_cable_state(struct extcon_dev *edev,
287 const char *cable_name, int state)
288{
289 return 0;
290}
291
Donggeun Kim74c5d092012-04-20 14:16:24 +0900292static inline struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
293{
294 return NULL;
295}
296
297static inline int extcon_register_notifier(struct extcon_dev *edev,
298 struct notifier_block *nb)
299{
300 return 0;
301}
302
303static inline int extcon_unregister_notifier(struct extcon_dev *edev,
304 struct notifier_block *nb)
305{
306 return 0;
307}
308
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900309static 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
317static inline int extcon_unregister_interest(struct extcon_specific_cable_nb
318 *obj)
319{
320 return 0;
321}
MyungJoo Hamde55d872012-04-20 14:16:22 +0900322#endif /* CONFIG_EXTCON */
323#endif /* __LINUX_EXTCON_H__ */