blob: 20e24b32a17dba2404a6297f572e90ecf8e521c6 [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 Hamde55d872012-04-20 14:16:22 +090081 * @print_name An optional callback to override the method to print the
82 * name of the extcon device.
83 * @print_state An optional callback to override the method to print the
84 * status of the extcon device.
85 * @dev Device of this extcon. Do not provide at register-time.
86 * @state Attach/detach state of this extcon. Do not provide at
87 * register-time
Donggeun Kim74c5d092012-04-20 14:16:24 +090088 * @nh Notifier for the state change events from this extcon
89 * @entry To support list of extcon devices so that uses can search
90 * for extcon devices based on the extcon name.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +090091 * @lock
92 * @max_supported Internal value to store the number of cables.
93 * @extcon_dev_type Device_type struct to provide attribute_groups
94 * customized for each extcon device.
95 * @cables Sysfs subdirectories. Each represents one cable.
MyungJoo Hamde55d872012-04-20 14:16:22 +090096 *
97 * In most cases, users only need to provide "User initializing data" of
98 * this struct when registering an extcon. In some exceptional cases,
99 * optional callbacks may be needed. However, the values in "internal data"
100 * are overwritten by register function.
101 */
102struct extcon_dev {
103 /* --- Optional user initializing data --- */
104 const char *name;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900105 const char **supported_cable;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900106
107 /* --- Optional callbacks to override class functions --- */
108 ssize_t (*print_name)(struct extcon_dev *edev, char *buf);
109 ssize_t (*print_state)(struct extcon_dev *edev, char *buf);
110
111 /* --- Internal data. Please do not set. --- */
112 struct device *dev;
113 u32 state;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900114 struct raw_notifier_head nh;
115 struct list_head entry;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900116 spinlock_t lock; /* could be called by irq handler */
117 int max_supported;
118
119 /* /sys/class/extcon/.../cable.n/... */
120 struct device_type extcon_dev_type;
121 struct extcon_cable *cables;
122};
123
124/**
125 * struct extcon_cable - An internal data for each cable of extcon device.
126 * @edev The extcon device
127 * @cable_index Index of this cable in the edev
128 * @attr_g Attribute group for the cable
129 * @attr_name "name" sysfs entry
130 * @attr_state "state" sysfs entry
131 * @attrs Array pointing to attr_name and attr_state for attr_g
132 */
133struct extcon_cable {
134 struct extcon_dev *edev;
135 int cable_index;
136
137 struct attribute_group attr_g;
138 struct device_attribute attr_name;
139 struct device_attribute attr_state;
140
141 struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
142};
143
144/**
145 * struct extcon_specific_cable_nb - An internal data for
146 * extcon_register_interest().
147 * @internal_nb a notifier block bridging extcon notifier and cable notifier.
148 * @user_nb user provided notifier block for events from a specific cable.
149 * @cable_index the target cable.
150 * @edev the target extcon device.
151 * @previous_value the saved previous event value.
152 */
153struct extcon_specific_cable_nb {
154 struct notifier_block internal_nb;
155 struct notifier_block *user_nb;
156 int cable_index;
157 struct extcon_dev *edev;
158 unsigned long previous_value;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900159};
160
161#if IS_ENABLED(CONFIG_EXTCON)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900162
163/*
164 * Following APIs are for notifiers or configurations.
165 * Notifiers are the external port and connection devices.
166 */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900167extern int extcon_dev_register(struct extcon_dev *edev, struct device *dev);
168extern void extcon_dev_unregister(struct extcon_dev *edev);
Donggeun Kim74c5d092012-04-20 14:16:24 +0900169extern struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900170
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900171/*
172 * get/set/update_state access the 32b encoded state value, which represents
173 * states of all possible cables of the multistate port. For example, if one
174 * calls extcon_set_state(edev, 0x7), it may mean that all the three cables
175 * are attached to the port.
176 */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900177static inline u32 extcon_get_state(struct extcon_dev *edev)
178{
179 return edev->state;
180}
181
182extern void extcon_set_state(struct extcon_dev *edev, u32 state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900183extern void extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state);
184
185/*
186 * get/set_cable_state access each bit of the 32b encoded state value.
187 * They are used to access the status of each cable based on the cable_name
188 * or cable_index, which is retrived by extcon_find_cable_index
189 */
190extern int extcon_find_cable_index(struct extcon_dev *sdev,
191 const char *cable_name);
192extern int extcon_get_cable_state_(struct extcon_dev *edev, int cable_index);
193extern int extcon_set_cable_state_(struct extcon_dev *edev, int cable_index,
194 bool cable_state);
195
196extern int extcon_get_cable_state(struct extcon_dev *edev,
197 const char *cable_name);
198extern int extcon_set_cable_state(struct extcon_dev *edev,
199 const char *cable_name, bool cable_state);
200
201/*
202 * Following APIs are for notifiees (those who want to be notified)
203 * to register a callback for events from a specific cable of the extcon.
204 * Notifiees are the connected device drivers wanting to get notified by
205 * a specific external port of a connection device.
206 */
207extern int extcon_register_interest(struct extcon_specific_cable_nb *obj,
208 const char *extcon_name,
209 const char *cable_name,
210 struct notifier_block *nb);
211extern int extcon_unregister_interest(struct extcon_specific_cable_nb *nb);
Donggeun Kim74c5d092012-04-20 14:16:24 +0900212
213/*
214 * Following APIs are to monitor every action of a notifier.
215 * Registerer gets notified for every external port of a connection device.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900216 * Probably this could be used to debug an action of notifier; however,
217 * we do not recommend to use this at normal 'notifiee' device drivers who
218 * want to be notified by a specific external port of the notifier.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900219 */
220extern int extcon_register_notifier(struct extcon_dev *edev,
221 struct notifier_block *nb);
222extern int extcon_unregister_notifier(struct extcon_dev *edev,
223 struct notifier_block *nb);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900224#else /* CONFIG_EXTCON */
225static inline int extcon_dev_register(struct extcon_dev *edev,
226 struct device *dev)
227{
228 return 0;
229}
230
231static inline void extcon_dev_unregister(struct extcon_dev *edev) { }
232
233static inline u32 extcon_get_state(struct extcon_dev *edev)
234{
235 return 0;
236}
237
238static inline void extcon_set_state(struct extcon_dev *edev, u32 state) { }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900239
240static inline void extcon_update_state(struct extcon_dev *edev, u32 mask,
241 u32 state)
242{ }
243
244static inline int extcon_find_cable_index(struct extcon_dev *edev,
245 const char *cable_name)
246{
247 return 0;
248}
249
250static inline int extcon_get_cable_state_(struct extcon_dev *edev,
251 int cable_index)
252{
253 return 0;
254}
255
256static inline int extcon_set_cable_state_(struct extcon_dev *edev,
257 int cable_index, bool cable_state)
258{
259 return 0;
260}
261
262static inline int extcon_get_cable_state(struct extcon_dev *edev,
263 const char *cable_name)
264{
265 return 0;
266}
267
268static inline int extcon_set_cable_state(struct extcon_dev *edev,
269 const char *cable_name, int state)
270{
271 return 0;
272}
273
Donggeun Kim74c5d092012-04-20 14:16:24 +0900274static inline struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
275{
276 return NULL;
277}
278
279static inline int extcon_register_notifier(struct extcon_dev *edev,
280 struct notifier_block *nb)
281{
282 return 0;
283}
284
285static inline int extcon_unregister_notifier(struct extcon_dev *edev,
286 struct notifier_block *nb)
287{
288 return 0;
289}
290
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900291static inline int extcon_register_interest(struct extcon_specific_cable_nb *obj,
292 const char *extcon_name,
293 const char *cable_name,
294 struct notifier_block *nb)
295{
296 return 0;
297}
298
299static inline int extcon_unregister_interest(struct extcon_specific_cable_nb
300 *obj)
301{
302 return 0;
303}
MyungJoo Hamde55d872012-04-20 14:16:22 +0900304#endif /* CONFIG_EXTCON */
305#endif /* __LINUX_EXTCON_H__ */