Commit | Line | Data |
---|---|---|
de55d871 | 1 | /* |
b9ec23c0 | 2 | * drivers/extcon/extcon.c - External Connector (extcon) framework. |
de55d871 MH |
3 | * |
4 | * External connector (extcon) class driver | |
5 | * | |
2a9de9c0 CC |
6 | * Copyright (C) 2015 Samsung Electronics |
7 | * Author: Chanwoo Choi <cw00.choi@samsung.com> | |
8 | * | |
de55d871 MH |
9 | * Copyright (C) 2012 Samsung Electronics |
10 | * Author: Donggeun Kim <dg77.kim@samsung.com> | |
11 | * Author: MyungJoo Ham <myungjoo.ham@samsung.com> | |
12 | * | |
13 | * based on android/drivers/switch/switch_class.c | |
14 | * Copyright (C) 2008 Google, Inc. | |
15 | * Author: Mike Lockwood <lockwood@android.com> | |
16 | * | |
17 | * This software is licensed under the terms of the GNU General Public | |
18 | * License version 2, as published by the Free Software Foundation, and | |
19 | * may be copied, distributed, and modified under those terms. | |
20 | * | |
21 | * This program is distributed in the hope that it will be useful, | |
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | * GNU General Public License for more details. | |
b9ec23c0 | 25 | */ |
de55d871 MH |
26 | |
27 | #include <linux/module.h> | |
28 | #include <linux/types.h> | |
29 | #include <linux/init.h> | |
30 | #include <linux/device.h> | |
31 | #include <linux/fs.h> | |
32 | #include <linux/err.h> | |
33 | #include <linux/extcon.h> | |
f841afb1 | 34 | #include <linux/of.h> |
de55d871 | 35 | #include <linux/slab.h> |
9baf3220 | 36 | #include <linux/sysfs.h> |
de55d871 | 37 | |
2a9de9c0 CC |
38 | #define SUPPORTED_CABLE_MAX 32 |
39 | #define CABLE_NAME_MAX 30 | |
40 | ||
41 | static const char *extcon_name[] = { | |
73b6ecdb CC |
42 | [EXTCON_NONE] = "NONE", |
43 | ||
8e9bc36d | 44 | /* USB external connector */ |
806d9dd7 | 45 | [EXTCON_USB] = "USB", |
8e9bc36d CC |
46 | [EXTCON_USB_HOST] = "USB-HOST", |
47 | ||
48 | /* Charger external connector */ | |
806d9dd7 | 49 | [EXTCON_TA] = "TA", |
8e9bc36d CC |
50 | [EXTCON_FAST_CHARGER] = "FAST-CHARGER", |
51 | [EXTCON_SLOW_CHARGER] = "SLOW-CHARGER", | |
52 | [EXTCON_CHARGE_DOWNSTREAM] = "CHARGE-DOWNSTREAM", | |
53 | ||
54 | /* Audio/Video external connector */ | |
55 | [EXTCON_LINE_IN] = "LINE-IN", | |
56 | [EXTCON_LINE_OUT] = "LINE-OUT", | |
57 | [EXTCON_MICROPHONE] = "MICROPHONE", | |
58 | [EXTCON_HEADPHONE] = "HEADPHONE", | |
59 | ||
806d9dd7 MH |
60 | [EXTCON_HDMI] = "HDMI", |
61 | [EXTCON_MHL] = "MHL", | |
62 | [EXTCON_DVI] = "DVI", | |
63 | [EXTCON_VGA] = "VGA", | |
8e9bc36d CC |
64 | [EXTCON_SPDIF_IN] = "SPDIF-IN", |
65 | [EXTCON_SPDIF_OUT] = "SPDIF-OUT", | |
66 | [EXTCON_VIDEO_IN] = "VIDEO-IN", | |
67 | [EXTCON_VIDEO_OUT] = "VIDEO-OUT", | |
68 | ||
69 | /* Etc external connector */ | |
70 | [EXTCON_DOCK] = "DOCK", | |
2a9de9c0 | 71 | [EXTCON_JIG] = "JIG", |
8e9bc36d CC |
72 | [EXTCON_MECHANICAL] = "MECHANICAL", |
73 | ||
2a9de9c0 | 74 | NULL, |
806d9dd7 MH |
75 | }; |
76 | ||
be3a07f7 | 77 | static struct class *extcon_class; |
449a2bf5 | 78 | #if defined(CONFIG_ANDROID) |
de55d871 | 79 | static struct class_compat *switch_class; |
449a2bf5 | 80 | #endif /* CONFIG_ANDROID */ |
de55d871 | 81 | |
74c5d09b DK |
82 | static LIST_HEAD(extcon_dev_list); |
83 | static DEFINE_MUTEX(extcon_dev_list_lock); | |
84 | ||
bde68e60 MH |
85 | /** |
86 | * check_mutually_exclusive - Check if new_state violates mutually_exclusive | |
a75e1c73 | 87 | * condition. |
bde68e60 MH |
88 | * @edev: the extcon device |
89 | * @new_state: new cable attach status for @edev | |
90 | * | |
91 | * Returns 0 if nothing violates. Returns the index + 1 for the first | |
92 | * violated condition. | |
93 | */ | |
94 | static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state) | |
95 | { | |
96 | int i = 0; | |
97 | ||
98 | if (!edev->mutually_exclusive) | |
99 | return 0; | |
100 | ||
101 | for (i = 0; edev->mutually_exclusive[i]; i++) { | |
28c0ada6 | 102 | int weight; |
bde68e60 | 103 | u32 correspondants = new_state & edev->mutually_exclusive[i]; |
bde68e60 | 104 | |
28c0ada6 | 105 | /* calculate the total number of bits set */ |
106 | weight = hweight32(correspondants); | |
107 | if (weight > 1) | |
108 | return i + 1; | |
bde68e60 MH |
109 | } |
110 | ||
111 | return 0; | |
112 | } | |
113 | ||
73b6ecdb | 114 | static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id) |
2a9de9c0 CC |
115 | { |
116 | int i; | |
117 | ||
118 | /* Find the the index of extcon cable in edev->supported_cable */ | |
119 | for (i = 0; i < edev->max_supported; i++) { | |
120 | if (edev->supported_cable[i] == id) | |
121 | return i; | |
122 | } | |
123 | ||
124 | return -EINVAL; | |
125 | } | |
126 | ||
be052cc8 | 127 | static int find_cable_id_by_name(struct extcon_dev *edev, const char *name) |
2a9de9c0 | 128 | { |
be052cc8 | 129 | unsigned int id = -EINVAL; |
73b6ecdb | 130 | int i = 0; |
2a9de9c0 | 131 | |
be052cc8 | 132 | /* Find the id of extcon cable */ |
73b6ecdb | 133 | while (extcon_name[i]) { |
2a9de9c0 CC |
134 | if (!strncmp(extcon_name[i], name, CABLE_NAME_MAX)) { |
135 | id = i; | |
136 | break; | |
137 | } | |
be052cc8 | 138 | i++; |
2a9de9c0 CC |
139 | } |
140 | ||
be052cc8 RQ |
141 | return id; |
142 | } | |
143 | ||
144 | static int find_cable_index_by_name(struct extcon_dev *edev, const char *name) | |
145 | { | |
146 | unsigned int id; | |
147 | ||
148 | if (edev->max_supported == 0) | |
2a9de9c0 CC |
149 | return -EINVAL; |
150 | ||
be052cc8 RQ |
151 | /* Find the the number of extcon cable */ |
152 | id = find_cable_id_by_name(edev, name); | |
153 | if (id < 0) | |
154 | return id; | |
155 | ||
2a9de9c0 CC |
156 | return find_cable_index_by_id(edev, id); |
157 | } | |
158 | ||
046050f6 CC |
159 | static bool is_extcon_changed(u32 prev, u32 new, int idx, bool *attached) |
160 | { | |
161 | if (((prev >> idx) & 0x1) != ((new >> idx) & 0x1)) { | |
162 | *attached = new ? true : false; | |
163 | return true; | |
164 | } | |
165 | ||
166 | return false; | |
167 | } | |
168 | ||
de55d871 MH |
169 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
170 | char *buf) | |
171 | { | |
806d9dd7 | 172 | int i, count = 0; |
cb8bb3a7 | 173 | struct extcon_dev *edev = dev_get_drvdata(dev); |
de55d871 MH |
174 | |
175 | if (edev->print_state) { | |
176 | int ret = edev->print_state(edev, buf); | |
177 | ||
178 | if (ret >= 0) | |
179 | return ret; | |
180 | /* Use default if failed */ | |
181 | } | |
806d9dd7 MH |
182 | |
183 | if (edev->max_supported == 0) | |
184 | return sprintf(buf, "%u\n", edev->state); | |
185 | ||
2a9de9c0 | 186 | for (i = 0; i < edev->max_supported; i++) { |
806d9dd7 | 187 | count += sprintf(buf + count, "%s=%d\n", |
2a9de9c0 | 188 | extcon_name[edev->supported_cable[i]], |
806d9dd7 MH |
189 | !!(edev->state & (1 << i))); |
190 | } | |
191 | ||
192 | return count; | |
193 | } | |
194 | ||
806d9dd7 MH |
195 | static ssize_t state_store(struct device *dev, struct device_attribute *attr, |
196 | const char *buf, size_t count) | |
197 | { | |
198 | u32 state; | |
199 | ssize_t ret = 0; | |
cb8bb3a7 | 200 | struct extcon_dev *edev = dev_get_drvdata(dev); |
806d9dd7 MH |
201 | |
202 | ret = sscanf(buf, "0x%x", &state); | |
203 | if (ret == 0) | |
204 | ret = -EINVAL; | |
205 | else | |
bde68e60 | 206 | ret = extcon_set_state(edev, state); |
806d9dd7 MH |
207 | |
208 | if (ret < 0) | |
209 | return ret; | |
210 | ||
211 | return count; | |
de55d871 | 212 | } |
af01da0e | 213 | static DEVICE_ATTR_RW(state); |
de55d871 MH |
214 | |
215 | static ssize_t name_show(struct device *dev, struct device_attribute *attr, | |
216 | char *buf) | |
217 | { | |
cb8bb3a7 | 218 | struct extcon_dev *edev = dev_get_drvdata(dev); |
de55d871 | 219 | |
71c3ffa5 | 220 | return sprintf(buf, "%s\n", edev->name); |
de55d871 | 221 | } |
af01da0e | 222 | static DEVICE_ATTR_RO(name); |
de55d871 | 223 | |
806d9dd7 MH |
224 | static ssize_t cable_name_show(struct device *dev, |
225 | struct device_attribute *attr, char *buf) | |
226 | { | |
227 | struct extcon_cable *cable = container_of(attr, struct extcon_cable, | |
228 | attr_name); | |
2a9de9c0 | 229 | int i = cable->cable_index; |
806d9dd7 MH |
230 | |
231 | return sprintf(buf, "%s\n", | |
2a9de9c0 | 232 | extcon_name[cable->edev->supported_cable[i]]); |
806d9dd7 MH |
233 | } |
234 | ||
235 | static ssize_t cable_state_show(struct device *dev, | |
236 | struct device_attribute *attr, char *buf) | |
237 | { | |
238 | struct extcon_cable *cable = container_of(attr, struct extcon_cable, | |
239 | attr_state); | |
240 | ||
be052cc8 RQ |
241 | int i = cable->cable_index; |
242 | ||
806d9dd7 MH |
243 | return sprintf(buf, "%d\n", |
244 | extcon_get_cable_state_(cable->edev, | |
be052cc8 | 245 | cable->edev->supported_cable[i])); |
806d9dd7 MH |
246 | } |
247 | ||
de55d871 | 248 | /** |
806d9dd7 | 249 | * extcon_update_state() - Update the cable attach states of the extcon device |
a75e1c73 | 250 | * only for the masked bits. |
de55d871 | 251 | * @edev: the extcon device |
806d9dd7 | 252 | * @mask: the bit mask to designate updated bits. |
de55d871 MH |
253 | * @state: new cable attach status for @edev |
254 | * | |
255 | * Changing the state sends uevent with environment variable containing | |
256 | * the name of extcon device (envp[0]) and the state output (envp[1]). | |
257 | * Tizen uses this format for extcon device to get events from ports. | |
258 | * Android uses this format as well. | |
74c5d09b | 259 | * |
806d9dd7 MH |
260 | * Note that the notifier provides which bits are changed in the state |
261 | * variable with the val parameter (second) to the callback. | |
de55d871 | 262 | */ |
bde68e60 | 263 | int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state) |
de55d871 MH |
264 | { |
265 | char name_buf[120]; | |
266 | char state_buf[120]; | |
267 | char *prop_buf; | |
268 | char *envp[3]; | |
269 | int env_offset = 0; | |
270 | int length; | |
046050f6 | 271 | int index; |
806d9dd7 | 272 | unsigned long flags; |
046050f6 | 273 | bool attached; |
de55d871 | 274 | |
806d9dd7 | 275 | spin_lock_irqsave(&edev->lock, flags); |
de55d871 | 276 | |
806d9dd7 | 277 | if (edev->state != ((edev->state & ~mask) | (state & mask))) { |
bde68e60 MH |
278 | if (check_mutually_exclusive(edev, (edev->state & ~mask) | |
279 | (state & mask))) { | |
280 | spin_unlock_irqrestore(&edev->lock, flags); | |
281 | return -EPERM; | |
282 | } | |
283 | ||
046050f6 CC |
284 | for (index = 0; index < edev->max_supported; index++) { |
285 | if (is_extcon_changed(edev->state, state, index, &attached)) | |
286 | raw_notifier_call_chain(&edev->nh[index], attached, edev); | |
287 | } | |
288 | ||
806d9dd7 MH |
289 | edev->state &= ~mask; |
290 | edev->state |= state & mask; | |
291 | ||
806d9dd7 MH |
292 | /* This could be in interrupt handler */ |
293 | prop_buf = (char *)get_zeroed_page(GFP_ATOMIC); | |
de55d871 | 294 | if (prop_buf) { |
dae61651 | 295 | length = name_show(&edev->dev, NULL, prop_buf); |
de55d871 MH |
296 | if (length > 0) { |
297 | if (prop_buf[length - 1] == '\n') | |
298 | prop_buf[length - 1] = 0; | |
299 | snprintf(name_buf, sizeof(name_buf), | |
300 | "NAME=%s", prop_buf); | |
301 | envp[env_offset++] = name_buf; | |
302 | } | |
dae61651 | 303 | length = state_show(&edev->dev, NULL, prop_buf); |
de55d871 MH |
304 | if (length > 0) { |
305 | if (prop_buf[length - 1] == '\n') | |
306 | prop_buf[length - 1] = 0; | |
307 | snprintf(state_buf, sizeof(state_buf), | |
308 | "STATE=%s", prop_buf); | |
309 | envp[env_offset++] = state_buf; | |
310 | } | |
311 | envp[env_offset] = NULL; | |
806d9dd7 MH |
312 | /* Unlock early before uevent */ |
313 | spin_unlock_irqrestore(&edev->lock, flags); | |
314 | ||
dae61651 | 315 | kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp); |
de55d871 MH |
316 | free_page((unsigned long)prop_buf); |
317 | } else { | |
806d9dd7 MH |
318 | /* Unlock early before uevent */ |
319 | spin_unlock_irqrestore(&edev->lock, flags); | |
320 | ||
dae61651 CC |
321 | dev_err(&edev->dev, "out of memory in extcon_set_state\n"); |
322 | kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE); | |
de55d871 | 323 | } |
806d9dd7 MH |
324 | } else { |
325 | /* No changes */ | |
326 | spin_unlock_irqrestore(&edev->lock, flags); | |
de55d871 | 327 | } |
bde68e60 MH |
328 | |
329 | return 0; | |
de55d871 | 330 | } |
806d9dd7 MH |
331 | EXPORT_SYMBOL_GPL(extcon_update_state); |
332 | ||
333 | /** | |
334 | * extcon_set_state() - Set the cable attach states of the extcon device. | |
335 | * @edev: the extcon device | |
336 | * @state: new cable attach status for @edev | |
337 | * | |
338 | * Note that notifier provides which bits are changed in the state | |
339 | * variable with the val parameter (second) to the callback. | |
340 | */ | |
bde68e60 | 341 | int extcon_set_state(struct extcon_dev *edev, u32 state) |
806d9dd7 | 342 | { |
bde68e60 | 343 | return extcon_update_state(edev, 0xffffffff, state); |
806d9dd7 | 344 | } |
de55d871 MH |
345 | EXPORT_SYMBOL_GPL(extcon_set_state); |
346 | ||
806d9dd7 | 347 | /** |
2a9de9c0 | 348 | * extcon_get_cable_state_() - Get the status of a specific cable. |
806d9dd7 | 349 | * @edev: the extcon device that has the cable. |
2a9de9c0 | 350 | * @id: the unique id of each external connector in extcon enumeration. |
806d9dd7 | 351 | */ |
73b6ecdb | 352 | int extcon_get_cable_state_(struct extcon_dev *edev, const unsigned int id) |
806d9dd7 | 353 | { |
2a9de9c0 | 354 | int index; |
806d9dd7 | 355 | |
2a9de9c0 CC |
356 | index = find_cable_index_by_id(edev, id); |
357 | if (index < 0) | |
358 | return index; | |
806d9dd7 | 359 | |
2a9de9c0 | 360 | if (edev->max_supported && edev->max_supported <= index) |
806d9dd7 MH |
361 | return -EINVAL; |
362 | ||
363 | return !!(edev->state & (1 << index)); | |
364 | } | |
365 | EXPORT_SYMBOL_GPL(extcon_get_cable_state_); | |
366 | ||
367 | /** | |
368 | * extcon_get_cable_state() - Get the status of a specific cable. | |
369 | * @edev: the extcon device that has the cable. | |
370 | * @cable_name: cable name. | |
371 | * | |
372 | * Note that this is slower than extcon_get_cable_state_. | |
373 | */ | |
374 | int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name) | |
375 | { | |
be052cc8 RQ |
376 | unsigned int id; |
377 | ||
378 | id = find_cable_id_by_name(edev, cable_name); | |
379 | if (id < 0) | |
380 | return id; | |
381 | ||
382 | return extcon_get_cable_state_(edev, id); | |
806d9dd7 MH |
383 | } |
384 | EXPORT_SYMBOL_GPL(extcon_get_cable_state); | |
385 | ||
386 | /** | |
909f9ec0 | 387 | * extcon_set_cable_state_() - Set the status of a specific cable. |
a75e1c73 | 388 | * @edev: the extcon device that has the cable. |
2a9de9c0 CC |
389 | * @id: the unique id of each external connector |
390 | * in extcon enumeration. | |
391 | * @state: the new cable status. The default semantics is | |
806d9dd7 MH |
392 | * true: attached / false: detached. |
393 | */ | |
73b6ecdb | 394 | int extcon_set_cable_state_(struct extcon_dev *edev, unsigned int id, |
2a9de9c0 | 395 | bool cable_state) |
806d9dd7 MH |
396 | { |
397 | u32 state; | |
2a9de9c0 | 398 | int index; |
806d9dd7 | 399 | |
2a9de9c0 CC |
400 | index = find_cable_index_by_id(edev, id); |
401 | if (index < 0) | |
402 | return index; | |
403 | ||
404 | if (edev->max_supported && edev->max_supported <= index) | |
806d9dd7 MH |
405 | return -EINVAL; |
406 | ||
407 | state = cable_state ? (1 << index) : 0; | |
bde68e60 | 408 | return extcon_update_state(edev, 1 << index, state); |
806d9dd7 MH |
409 | } |
410 | EXPORT_SYMBOL_GPL(extcon_set_cable_state_); | |
411 | ||
412 | /** | |
909f9ec0 | 413 | * extcon_set_cable_state() - Set the status of a specific cable. |
a75e1c73 CC |
414 | * @edev: the extcon device that has the cable. |
415 | * @cable_name: cable name. | |
806d9dd7 MH |
416 | * @cable_state: the new cable status. The default semantics is |
417 | * true: attached / false: detached. | |
418 | * | |
419 | * Note that this is slower than extcon_set_cable_state_. | |
420 | */ | |
421 | int extcon_set_cable_state(struct extcon_dev *edev, | |
422 | const char *cable_name, bool cable_state) | |
423 | { | |
be052cc8 RQ |
424 | unsigned int id; |
425 | ||
426 | id = find_cable_id_by_name(edev, cable_name); | |
427 | if (id < 0) | |
428 | return id; | |
429 | ||
430 | return extcon_set_cable_state_(edev, id, cable_state); | |
806d9dd7 MH |
431 | } |
432 | EXPORT_SYMBOL_GPL(extcon_set_cable_state); | |
433 | ||
74c5d09b DK |
434 | /** |
435 | * extcon_get_extcon_dev() - Get the extcon device instance from the name | |
436 | * @extcon_name: The extcon name provided with extcon_dev_register() | |
437 | */ | |
438 | struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name) | |
439 | { | |
440 | struct extcon_dev *sd; | |
441 | ||
442 | mutex_lock(&extcon_dev_list_lock); | |
443 | list_for_each_entry(sd, &extcon_dev_list, entry) { | |
444 | if (!strcmp(sd->name, extcon_name)) | |
445 | goto out; | |
446 | } | |
447 | sd = NULL; | |
448 | out: | |
449 | mutex_unlock(&extcon_dev_list_lock); | |
450 | return sd; | |
451 | } | |
452 | EXPORT_SYMBOL_GPL(extcon_get_extcon_dev); | |
453 | ||
806d9dd7 MH |
454 | /** |
455 | * extcon_register_interest() - Register a notifier for a state change of a | |
a75e1c73 CC |
456 | * specific cable, not an entier set of cables of a |
457 | * extcon device. | |
458 | * @obj: an empty extcon_specific_cable_nb object to be returned. | |
806d9dd7 | 459 | * @extcon_name: the name of extcon device. |
4f2de3bf JT |
460 | * if NULL, extcon_register_interest will register |
461 | * every cable with the target cable_name given. | |
806d9dd7 | 462 | * @cable_name: the target cable name. |
a75e1c73 | 463 | * @nb: the notifier block to get notified. |
806d9dd7 MH |
464 | * |
465 | * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets | |
466 | * the struct for you. | |
467 | * | |
468 | * extcon_register_interest is a helper function for those who want to get | |
469 | * notification for a single specific cable's status change. If a user wants | |
470 | * to get notification for any changes of all cables of a extcon device, | |
471 | * he/she should use the general extcon_register_notifier(). | |
472 | * | |
473 | * Note that the second parameter given to the callback of nb (val) is | |
474 | * "old_state", not the current state. The current state can be retrieved | |
475 | * by looking at the third pameter (edev pointer)'s state value. | |
476 | */ | |
477 | int extcon_register_interest(struct extcon_specific_cable_nb *obj, | |
478 | const char *extcon_name, const char *cable_name, | |
479 | struct notifier_block *nb) | |
480 | { | |
66bee35f HG |
481 | unsigned long flags; |
482 | int ret; | |
483 | ||
4f2de3bf | 484 | if (!obj || !cable_name || !nb) |
806d9dd7 MH |
485 | return -EINVAL; |
486 | ||
4f2de3bf JT |
487 | if (extcon_name) { |
488 | obj->edev = extcon_get_extcon_dev(extcon_name); | |
489 | if (!obj->edev) | |
490 | return -ENODEV; | |
806d9dd7 | 491 | |
2a9de9c0 CC |
492 | obj->cable_index = find_cable_index_by_name(obj->edev, |
493 | cable_name); | |
4f2de3bf | 494 | if (obj->cable_index < 0) |
c0c078c3 | 495 | return obj->cable_index; |
806d9dd7 | 496 | |
4f2de3bf | 497 | obj->user_nb = nb; |
806d9dd7 | 498 | |
66bee35f | 499 | spin_lock_irqsave(&obj->edev->lock, flags); |
046050f6 CC |
500 | ret = raw_notifier_chain_register( |
501 | &obj->edev->nh[obj->cable_index], | |
502 | obj->user_nb); | |
66bee35f | 503 | spin_unlock_irqrestore(&obj->edev->lock, flags); |
4f2de3bf JT |
504 | } else { |
505 | struct class_dev_iter iter; | |
506 | struct extcon_dev *extd; | |
507 | struct device *dev; | |
508 | ||
509 | if (!extcon_class) | |
510 | return -ENODEV; | |
511 | class_dev_iter_init(&iter, extcon_class, NULL, NULL); | |
512 | while ((dev = class_dev_iter_next(&iter))) { | |
cb8bb3a7 | 513 | extd = dev_get_drvdata(dev); |
4f2de3bf | 514 | |
2a9de9c0 | 515 | if (find_cable_index_by_name(extd, cable_name) < 0) |
4f2de3bf JT |
516 | continue; |
517 | ||
518 | class_dev_iter_exit(&iter); | |
519 | return extcon_register_interest(obj, extd->name, | |
520 | cable_name, nb); | |
521 | } | |
522 | ||
b9ec23c0 | 523 | ret = -ENODEV; |
4f2de3bf | 524 | } |
b9ec23c0 CC |
525 | |
526 | return ret; | |
806d9dd7 | 527 | } |
9c8a013a | 528 | EXPORT_SYMBOL_GPL(extcon_register_interest); |
806d9dd7 MH |
529 | |
530 | /** | |
531 | * extcon_unregister_interest() - Unregister the notifier registered by | |
a75e1c73 | 532 | * extcon_register_interest(). |
806d9dd7 MH |
533 | * @obj: the extcon_specific_cable_nb object returned by |
534 | * extcon_register_interest(). | |
535 | */ | |
536 | int extcon_unregister_interest(struct extcon_specific_cable_nb *obj) | |
537 | { | |
66bee35f HG |
538 | unsigned long flags; |
539 | int ret; | |
540 | ||
806d9dd7 MH |
541 | if (!obj) |
542 | return -EINVAL; | |
543 | ||
66bee35f | 544 | spin_lock_irqsave(&obj->edev->lock, flags); |
046050f6 CC |
545 | ret = raw_notifier_chain_unregister( |
546 | &obj->edev->nh[obj->cable_index], obj->user_nb); | |
66bee35f HG |
547 | spin_unlock_irqrestore(&obj->edev->lock, flags); |
548 | ||
549 | return ret; | |
806d9dd7 | 550 | } |
9c8a013a | 551 | EXPORT_SYMBOL_GPL(extcon_unregister_interest); |
806d9dd7 | 552 | |
74c5d09b | 553 | /** |
c338bb03 | 554 | * extcon_register_notifier() - Register a notifiee to get notified by |
a75e1c73 | 555 | * any attach status changes from the extcon. |
046050f6 CC |
556 | * @edev: the extcon device that has the external connecotr. |
557 | * @id: the unique id of each external connector in extcon enumeration. | |
74c5d09b | 558 | * @nb: a notifier block to be registered. |
806d9dd7 MH |
559 | * |
560 | * Note that the second parameter given to the callback of nb (val) is | |
561 | * "old_state", not the current state. The current state can be retrieved | |
562 | * by looking at the third pameter (edev pointer)'s state value. | |
74c5d09b | 563 | */ |
73b6ecdb | 564 | int extcon_register_notifier(struct extcon_dev *edev, unsigned int id, |
046050f6 | 565 | struct notifier_block *nb) |
74c5d09b | 566 | { |
66bee35f | 567 | unsigned long flags; |
046050f6 CC |
568 | int ret, idx; |
569 | ||
570 | idx = find_cable_index_by_id(edev, id); | |
66bee35f HG |
571 | |
572 | spin_lock_irqsave(&edev->lock, flags); | |
046050f6 | 573 | ret = raw_notifier_chain_register(&edev->nh[idx], nb); |
66bee35f HG |
574 | spin_unlock_irqrestore(&edev->lock, flags); |
575 | ||
576 | return ret; | |
74c5d09b DK |
577 | } |
578 | EXPORT_SYMBOL_GPL(extcon_register_notifier); | |
579 | ||
580 | /** | |
c338bb03 | 581 | * extcon_unregister_notifier() - Unregister a notifiee from the extcon device. |
046050f6 CC |
582 | * @edev: the extcon device that has the external connecotr. |
583 | * @id: the unique id of each external connector in extcon enumeration. | |
584 | * @nb: a notifier block to be registered. | |
74c5d09b | 585 | */ |
73b6ecdb | 586 | int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id, |
046050f6 | 587 | struct notifier_block *nb) |
74c5d09b | 588 | { |
66bee35f | 589 | unsigned long flags; |
046050f6 CC |
590 | int ret, idx; |
591 | ||
592 | idx = find_cable_index_by_id(edev, id); | |
66bee35f HG |
593 | |
594 | spin_lock_irqsave(&edev->lock, flags); | |
046050f6 | 595 | ret = raw_notifier_chain_unregister(&edev->nh[idx], nb); |
66bee35f HG |
596 | spin_unlock_irqrestore(&edev->lock, flags); |
597 | ||
598 | return ret; | |
74c5d09b DK |
599 | } |
600 | EXPORT_SYMBOL_GPL(extcon_unregister_notifier); | |
601 | ||
af01da0e GKH |
602 | static struct attribute *extcon_attrs[] = { |
603 | &dev_attr_state.attr, | |
604 | &dev_attr_name.attr, | |
605 | NULL, | |
de55d871 | 606 | }; |
af01da0e | 607 | ATTRIBUTE_GROUPS(extcon); |
de55d871 MH |
608 | |
609 | static int create_extcon_class(void) | |
610 | { | |
611 | if (!extcon_class) { | |
612 | extcon_class = class_create(THIS_MODULE, "extcon"); | |
613 | if (IS_ERR(extcon_class)) | |
614 | return PTR_ERR(extcon_class); | |
af01da0e | 615 | extcon_class->dev_groups = extcon_groups; |
de55d871 | 616 | |
449a2bf5 | 617 | #if defined(CONFIG_ANDROID) |
de55d871 MH |
618 | switch_class = class_compat_register("switch"); |
619 | if (WARN(!switch_class, "cannot allocate")) | |
620 | return -ENOMEM; | |
449a2bf5 | 621 | #endif /* CONFIG_ANDROID */ |
de55d871 MH |
622 | } |
623 | ||
624 | return 0; | |
625 | } | |
626 | ||
de55d871 MH |
627 | static void extcon_dev_release(struct device *dev) |
628 | { | |
de55d871 MH |
629 | } |
630 | ||
bde68e60 | 631 | static const char *muex_name = "mutually_exclusive"; |
806d9dd7 MH |
632 | static void dummy_sysfs_dev_release(struct device *dev) |
633 | { | |
634 | } | |
635 | ||
a9af6522 CC |
636 | /* |
637 | * extcon_dev_allocate() - Allocate the memory of extcon device. | |
2a9de9c0 | 638 | * @supported_cable: Array of supported extcon ending with EXTCON_NONE. |
a9af6522 CC |
639 | * If supported_cable is NULL, cable name related APIs |
640 | * are disabled. | |
641 | * | |
642 | * This function allocates the memory for extcon device without allocating | |
643 | * memory in each extcon provider driver and initialize default setting for | |
644 | * extcon device. | |
645 | * | |
646 | * Return the pointer of extcon device if success or ERR_PTR(err) if fail | |
647 | */ | |
73b6ecdb | 648 | struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable) |
a9af6522 CC |
649 | { |
650 | struct extcon_dev *edev; | |
651 | ||
652 | edev = kzalloc(sizeof(*edev), GFP_KERNEL); | |
653 | if (!edev) | |
654 | return ERR_PTR(-ENOMEM); | |
655 | ||
656 | edev->max_supported = 0; | |
657 | edev->supported_cable = supported_cable; | |
658 | ||
659 | return edev; | |
660 | } | |
661 | ||
662 | /* | |
663 | * extcon_dev_free() - Free the memory of extcon device. | |
664 | * @edev: the extcon device to free | |
665 | */ | |
666 | void extcon_dev_free(struct extcon_dev *edev) | |
667 | { | |
668 | kfree(edev); | |
669 | } | |
670 | EXPORT_SYMBOL_GPL(extcon_dev_free); | |
671 | ||
739ba1bf CC |
672 | static int devm_extcon_dev_match(struct device *dev, void *res, void *data) |
673 | { | |
674 | struct extcon_dev **r = res; | |
675 | ||
676 | if (WARN_ON(!r || !*r)) | |
677 | return 0; | |
678 | ||
679 | return *r == data; | |
680 | } | |
681 | ||
682 | static void devm_extcon_dev_release(struct device *dev, void *res) | |
683 | { | |
684 | extcon_dev_free(*(struct extcon_dev **)res); | |
685 | } | |
686 | ||
687 | /** | |
688 | * devm_extcon_dev_allocate - Allocate managed extcon device | |
689 | * @dev: device owning the extcon device being created | |
2a9de9c0 | 690 | * @supported_cable: Array of supported extcon ending with EXTCON_NONE. |
739ba1bf CC |
691 | * If supported_cable is NULL, cable name related APIs |
692 | * are disabled. | |
693 | * | |
694 | * This function manages automatically the memory of extcon device using device | |
695 | * resource management and simplify the control of freeing the memory of extcon | |
696 | * device. | |
697 | * | |
698 | * Returns the pointer memory of allocated extcon_dev if success | |
699 | * or ERR_PTR(err) if fail | |
700 | */ | |
701 | struct extcon_dev *devm_extcon_dev_allocate(struct device *dev, | |
73b6ecdb | 702 | const unsigned int *supported_cable) |
739ba1bf CC |
703 | { |
704 | struct extcon_dev **ptr, *edev; | |
705 | ||
706 | ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL); | |
707 | if (!ptr) | |
708 | return ERR_PTR(-ENOMEM); | |
709 | ||
710 | edev = extcon_dev_allocate(supported_cable); | |
711 | if (IS_ERR(edev)) { | |
712 | devres_free(ptr); | |
713 | return edev; | |
714 | } | |
715 | ||
ac65a625 CC |
716 | edev->dev.parent = dev; |
717 | ||
739ba1bf CC |
718 | *ptr = edev; |
719 | devres_add(dev, ptr); | |
720 | ||
721 | return edev; | |
722 | } | |
723 | EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate); | |
724 | ||
725 | void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev) | |
726 | { | |
727 | WARN_ON(devres_release(dev, devm_extcon_dev_release, | |
728 | devm_extcon_dev_match, edev)); | |
729 | } | |
730 | EXPORT_SYMBOL_GPL(devm_extcon_dev_free); | |
731 | ||
de55d871 MH |
732 | /** |
733 | * extcon_dev_register() - Register a new extcon device | |
734 | * @edev : the new extcon device (should be allocated before calling) | |
de55d871 MH |
735 | * |
736 | * Among the members of edev struct, please set the "user initializing data" | |
737 | * in any case and set the "optional callbacks" if required. However, please | |
738 | * do not set the values of "internal data", which are initialized by | |
739 | * this function. | |
740 | */ | |
42d7d753 | 741 | int extcon_dev_register(struct extcon_dev *edev) |
de55d871 | 742 | { |
806d9dd7 | 743 | int ret, index = 0; |
71c3ffa5 | 744 | static atomic_t edev_no = ATOMIC_INIT(-1); |
de55d871 MH |
745 | |
746 | if (!extcon_class) { | |
747 | ret = create_extcon_class(); | |
748 | if (ret < 0) | |
749 | return ret; | |
750 | } | |
751 | ||
2a9de9c0 CC |
752 | if (!edev->supported_cable) |
753 | return -EINVAL; | |
754 | ||
755 | for (; edev->supported_cable[index] != EXTCON_NONE; index++); | |
806d9dd7 | 756 | |
2a9de9c0 | 757 | edev->max_supported = index; |
806d9dd7 | 758 | if (index > SUPPORTED_CABLE_MAX) { |
2a9de9c0 CC |
759 | dev_err(&edev->dev, |
760 | "exceed the maximum number of supported cables\n"); | |
806d9dd7 MH |
761 | return -EINVAL; |
762 | } | |
763 | ||
dae61651 CC |
764 | edev->dev.class = extcon_class; |
765 | edev->dev.release = extcon_dev_release; | |
de55d871 | 766 | |
71c3ffa5 | 767 | edev->name = dev_name(edev->dev.parent); |
42d7d753 CC |
768 | if (IS_ERR_OR_NULL(edev->name)) { |
769 | dev_err(&edev->dev, | |
770 | "extcon device name is null\n"); | |
771 | return -EINVAL; | |
772 | } | |
71c3ffa5 CC |
773 | dev_set_name(&edev->dev, "extcon%lu", |
774 | (unsigned long)atomic_inc_return(&edev_no)); | |
806d9dd7 MH |
775 | |
776 | if (edev->max_supported) { | |
777 | char buf[10]; | |
778 | char *str; | |
779 | struct extcon_cable *cable; | |
780 | ||
781 | edev->cables = kzalloc(sizeof(struct extcon_cable) * | |
782 | edev->max_supported, GFP_KERNEL); | |
783 | if (!edev->cables) { | |
784 | ret = -ENOMEM; | |
785 | goto err_sysfs_alloc; | |
786 | } | |
787 | for (index = 0; index < edev->max_supported; index++) { | |
788 | cable = &edev->cables[index]; | |
789 | ||
790 | snprintf(buf, 10, "cable.%d", index); | |
791 | str = kzalloc(sizeof(char) * (strlen(buf) + 1), | |
792 | GFP_KERNEL); | |
793 | if (!str) { | |
794 | for (index--; index >= 0; index--) { | |
795 | cable = &edev->cables[index]; | |
796 | kfree(cable->attr_g.name); | |
797 | } | |
798 | ret = -ENOMEM; | |
799 | ||
800 | goto err_alloc_cables; | |
801 | } | |
802 | strcpy(str, buf); | |
803 | ||
804 | cable->edev = edev; | |
805 | cable->cable_index = index; | |
806 | cable->attrs[0] = &cable->attr_name.attr; | |
807 | cable->attrs[1] = &cable->attr_state.attr; | |
808 | cable->attrs[2] = NULL; | |
809 | cable->attr_g.name = str; | |
810 | cable->attr_g.attrs = cable->attrs; | |
811 | ||
9baf3220 | 812 | sysfs_attr_init(&cable->attr_name.attr); |
806d9dd7 MH |
813 | cable->attr_name.attr.name = "name"; |
814 | cable->attr_name.attr.mode = 0444; | |
815 | cable->attr_name.show = cable_name_show; | |
816 | ||
9baf3220 | 817 | sysfs_attr_init(&cable->attr_state.attr); |
806d9dd7 | 818 | cable->attr_state.attr.name = "state"; |
ea9dd9d6 | 819 | cable->attr_state.attr.mode = 0444; |
806d9dd7 | 820 | cable->attr_state.show = cable_state_show; |
806d9dd7 MH |
821 | } |
822 | } | |
823 | ||
bde68e60 MH |
824 | if (edev->max_supported && edev->mutually_exclusive) { |
825 | char buf[80]; | |
826 | char *name; | |
827 | ||
828 | /* Count the size of mutually_exclusive array */ | |
829 | for (index = 0; edev->mutually_exclusive[index]; index++) | |
830 | ; | |
831 | ||
832 | edev->attrs_muex = kzalloc(sizeof(struct attribute *) * | |
833 | (index + 1), GFP_KERNEL); | |
834 | if (!edev->attrs_muex) { | |
835 | ret = -ENOMEM; | |
836 | goto err_muex; | |
837 | } | |
838 | ||
839 | edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) * | |
840 | index, GFP_KERNEL); | |
841 | if (!edev->d_attrs_muex) { | |
842 | ret = -ENOMEM; | |
843 | kfree(edev->attrs_muex); | |
844 | goto err_muex; | |
845 | } | |
846 | ||
847 | for (index = 0; edev->mutually_exclusive[index]; index++) { | |
848 | sprintf(buf, "0x%x", edev->mutually_exclusive[index]); | |
849 | name = kzalloc(sizeof(char) * (strlen(buf) + 1), | |
850 | GFP_KERNEL); | |
851 | if (!name) { | |
852 | for (index--; index >= 0; index--) { | |
853 | kfree(edev->d_attrs_muex[index].attr. | |
854 | name); | |
855 | } | |
856 | kfree(edev->d_attrs_muex); | |
857 | kfree(edev->attrs_muex); | |
858 | ret = -ENOMEM; | |
859 | goto err_muex; | |
860 | } | |
861 | strcpy(name, buf); | |
9baf3220 | 862 | sysfs_attr_init(&edev->d_attrs_muex[index].attr); |
bde68e60 MH |
863 | edev->d_attrs_muex[index].attr.name = name; |
864 | edev->d_attrs_muex[index].attr.mode = 0000; | |
865 | edev->attrs_muex[index] = &edev->d_attrs_muex[index] | |
866 | .attr; | |
867 | } | |
868 | edev->attr_g_muex.name = muex_name; | |
869 | edev->attr_g_muex.attrs = edev->attrs_muex; | |
870 | ||
871 | } | |
872 | ||
806d9dd7 MH |
873 | if (edev->max_supported) { |
874 | edev->extcon_dev_type.groups = | |
875 | kzalloc(sizeof(struct attribute_group *) * | |
bde68e60 | 876 | (edev->max_supported + 2), GFP_KERNEL); |
806d9dd7 MH |
877 | if (!edev->extcon_dev_type.groups) { |
878 | ret = -ENOMEM; | |
879 | goto err_alloc_groups; | |
880 | } | |
881 | ||
dae61651 | 882 | edev->extcon_dev_type.name = dev_name(&edev->dev); |
806d9dd7 MH |
883 | edev->extcon_dev_type.release = dummy_sysfs_dev_release; |
884 | ||
885 | for (index = 0; index < edev->max_supported; index++) | |
886 | edev->extcon_dev_type.groups[index] = | |
887 | &edev->cables[index].attr_g; | |
bde68e60 MH |
888 | if (edev->mutually_exclusive) |
889 | edev->extcon_dev_type.groups[index] = | |
890 | &edev->attr_g_muex; | |
806d9dd7 | 891 | |
dae61651 | 892 | edev->dev.type = &edev->extcon_dev_type; |
806d9dd7 MH |
893 | } |
894 | ||
dae61651 | 895 | ret = device_register(&edev->dev); |
de55d871 | 896 | if (ret) { |
dae61651 | 897 | put_device(&edev->dev); |
de55d871 MH |
898 | goto err_dev; |
899 | } | |
449a2bf5 | 900 | #if defined(CONFIG_ANDROID) |
de55d871 | 901 | if (switch_class) |
dae61651 | 902 | ret = class_compat_create_link(switch_class, &edev->dev, NULL); |
449a2bf5 | 903 | #endif /* CONFIG_ANDROID */ |
de55d871 | 904 | |
806d9dd7 MH |
905 | spin_lock_init(&edev->lock); |
906 | ||
046050f6 CC |
907 | edev->nh = devm_kzalloc(&edev->dev, |
908 | sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL); | |
909 | if (!edev->nh) { | |
910 | ret = -ENOMEM; | |
911 | goto err_dev; | |
912 | } | |
913 | ||
914 | for (index = 0; index < edev->max_supported; index++) | |
915 | RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]); | |
74c5d09b | 916 | |
dae61651 | 917 | dev_set_drvdata(&edev->dev, edev); |
de55d871 | 918 | edev->state = 0; |
74c5d09b DK |
919 | |
920 | mutex_lock(&extcon_dev_list_lock); | |
921 | list_add(&edev->entry, &extcon_dev_list); | |
922 | mutex_unlock(&extcon_dev_list_lock); | |
923 | ||
de55d871 MH |
924 | return 0; |
925 | ||
926 | err_dev: | |
806d9dd7 MH |
927 | if (edev->max_supported) |
928 | kfree(edev->extcon_dev_type.groups); | |
929 | err_alloc_groups: | |
bde68e60 MH |
930 | if (edev->max_supported && edev->mutually_exclusive) { |
931 | for (index = 0; edev->mutually_exclusive[index]; index++) | |
932 | kfree(edev->d_attrs_muex[index].attr.name); | |
933 | kfree(edev->d_attrs_muex); | |
934 | kfree(edev->attrs_muex); | |
935 | } | |
936 | err_muex: | |
806d9dd7 MH |
937 | for (index = 0; index < edev->max_supported; index++) |
938 | kfree(edev->cables[index].attr_g.name); | |
939 | err_alloc_cables: | |
940 | if (edev->max_supported) | |
941 | kfree(edev->cables); | |
942 | err_sysfs_alloc: | |
de55d871 MH |
943 | return ret; |
944 | } | |
945 | EXPORT_SYMBOL_GPL(extcon_dev_register); | |
946 | ||
947 | /** | |
948 | * extcon_dev_unregister() - Unregister the extcon device. | |
c338bb03 | 949 | * @edev: the extcon device instance to be unregistered. |
de55d871 MH |
950 | * |
951 | * Note that this does not call kfree(edev) because edev was not allocated | |
952 | * by this class. | |
953 | */ | |
954 | void extcon_dev_unregister(struct extcon_dev *edev) | |
955 | { | |
57e7cd37 | 956 | int index; |
957 | ||
958 | mutex_lock(&extcon_dev_list_lock); | |
959 | list_del(&edev->entry); | |
960 | mutex_unlock(&extcon_dev_list_lock); | |
961 | ||
dae61651 CC |
962 | if (IS_ERR_OR_NULL(get_device(&edev->dev))) { |
963 | dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n", | |
964 | dev_name(&edev->dev)); | |
57e7cd37 | 965 | return; |
966 | } | |
967 | ||
7585ca0d WX |
968 | device_unregister(&edev->dev); |
969 | ||
57e7cd37 | 970 | if (edev->mutually_exclusive && edev->max_supported) { |
971 | for (index = 0; edev->mutually_exclusive[index]; | |
972 | index++) | |
973 | kfree(edev->d_attrs_muex[index].attr.name); | |
974 | kfree(edev->d_attrs_muex); | |
975 | kfree(edev->attrs_muex); | |
976 | } | |
977 | ||
978 | for (index = 0; index < edev->max_supported; index++) | |
979 | kfree(edev->cables[index].attr_g.name); | |
980 | ||
981 | if (edev->max_supported) { | |
982 | kfree(edev->extcon_dev_type.groups); | |
983 | kfree(edev->cables); | |
984 | } | |
985 | ||
986 | #if defined(CONFIG_ANDROID) | |
987 | if (switch_class) | |
dae61651 | 988 | class_compat_remove_link(switch_class, &edev->dev, NULL); |
57e7cd37 | 989 | #endif |
dae61651 | 990 | put_device(&edev->dev); |
de55d871 MH |
991 | } |
992 | EXPORT_SYMBOL_GPL(extcon_dev_unregister); | |
993 | ||
1111244f SW |
994 | static void devm_extcon_dev_unreg(struct device *dev, void *res) |
995 | { | |
996 | extcon_dev_unregister(*(struct extcon_dev **)res); | |
997 | } | |
998 | ||
1111244f SW |
999 | /** |
1000 | * devm_extcon_dev_register() - Resource-managed extcon_dev_register() | |
1001 | * @dev: device to allocate extcon device | |
1002 | * @edev: the new extcon device to register | |
1003 | * | |
1004 | * Managed extcon_dev_register() function. If extcon device is attached with | |
1005 | * this function, that extcon device is automatically unregistered on driver | |
1006 | * detach. Internally this function calls extcon_dev_register() function. | |
1007 | * To get more information, refer that function. | |
1008 | * | |
1009 | * If extcon device is registered with this function and the device needs to be | |
1010 | * unregistered separately, devm_extcon_dev_unregister() should be used. | |
1011 | * | |
1012 | * Returns 0 if success or negaive error number if failure. | |
1013 | */ | |
1014 | int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev) | |
1015 | { | |
1016 | struct extcon_dev **ptr; | |
1017 | int ret; | |
1018 | ||
1019 | ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL); | |
1020 | if (!ptr) | |
1021 | return -ENOMEM; | |
1022 | ||
1023 | ret = extcon_dev_register(edev); | |
1024 | if (ret) { | |
1025 | devres_free(ptr); | |
1026 | return ret; | |
1027 | } | |
1028 | ||
1029 | *ptr = edev; | |
1030 | devres_add(dev, ptr); | |
1031 | ||
1032 | return 0; | |
1033 | } | |
1034 | EXPORT_SYMBOL_GPL(devm_extcon_dev_register); | |
1035 | ||
1036 | /** | |
1037 | * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister() | |
1038 | * @dev: device the extcon belongs to | |
1039 | * @edev: the extcon device to unregister | |
1040 | * | |
1041 | * Unregister extcon device that is registered with devm_extcon_dev_register() | |
1042 | * function. | |
1043 | */ | |
1044 | void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev) | |
1045 | { | |
1046 | WARN_ON(devres_release(dev, devm_extcon_dev_unreg, | |
1047 | devm_extcon_dev_match, edev)); | |
1048 | } | |
1049 | EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister); | |
1050 | ||
1ad94ffe CC |
1051 | #ifdef CONFIG_OF |
1052 | /* | |
1053 | * extcon_get_edev_by_phandle - Get the extcon device from devicetree | |
1054 | * @dev - instance to the given device | |
1055 | * @index - index into list of extcon_dev | |
1056 | * | |
1057 | * return the instance of extcon device | |
1058 | */ | |
1059 | struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) | |
1060 | { | |
1061 | struct device_node *node; | |
1062 | struct extcon_dev *edev; | |
1063 | ||
1064 | if (!dev->of_node) { | |
1065 | dev_err(dev, "device does not have a device node entry\n"); | |
1066 | return ERR_PTR(-EINVAL); | |
1067 | } | |
1068 | ||
1069 | node = of_parse_phandle(dev->of_node, "extcon", index); | |
1070 | if (!node) { | |
1071 | dev_err(dev, "failed to get phandle in %s node\n", | |
1072 | dev->of_node->full_name); | |
1073 | return ERR_PTR(-ENODEV); | |
1074 | } | |
1075 | ||
f841afb1 TF |
1076 | mutex_lock(&extcon_dev_list_lock); |
1077 | list_for_each_entry(edev, &extcon_dev_list, entry) { | |
1078 | if (edev->dev.parent && edev->dev.parent->of_node == node) { | |
1079 | mutex_unlock(&extcon_dev_list_lock); | |
1080 | return edev; | |
1081 | } | |
1ad94ffe | 1082 | } |
f841afb1 | 1083 | mutex_unlock(&extcon_dev_list_lock); |
1ad94ffe | 1084 | |
f841afb1 | 1085 | return ERR_PTR(-EPROBE_DEFER); |
1ad94ffe CC |
1086 | } |
1087 | #else | |
1088 | struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) | |
1089 | { | |
1090 | return ERR_PTR(-ENOSYS); | |
1091 | } | |
1092 | #endif /* CONFIG_OF */ | |
1093 | EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle); | |
1094 | ||
707d7550 CC |
1095 | /** |
1096 | * extcon_get_edev_name() - Get the name of the extcon device. | |
1097 | * @edev: the extcon device | |
1098 | */ | |
1099 | const char *extcon_get_edev_name(struct extcon_dev *edev) | |
1100 | { | |
1101 | return !edev ? NULL : edev->name; | |
1102 | } | |
1103 | ||
de55d871 MH |
1104 | static int __init extcon_class_init(void) |
1105 | { | |
1106 | return create_extcon_class(); | |
1107 | } | |
1108 | module_init(extcon_class_init); | |
1109 | ||
1110 | static void __exit extcon_class_exit(void) | |
1111 | { | |
0dc77b6d PH |
1112 | #if defined(CONFIG_ANDROID) |
1113 | class_compat_unregister(switch_class); | |
1114 | #endif | |
de55d871 MH |
1115 | class_destroy(extcon_class); |
1116 | } | |
1117 | module_exit(extcon_class_exit); | |
1118 | ||
2a9de9c0 | 1119 | MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>"); |
de55d871 MH |
1120 | MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>"); |
1121 | MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>"); | |
1122 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); | |
1123 | MODULE_DESCRIPTION("External connector (extcon) class driver"); | |
1124 | MODULE_LICENSE("GPL"); |