2 * drivers/extcon/extcon.c - External Connector (extcon) framework.
4 * External connector (extcon) class driver
6 * Copyright (C) 2015 Samsung Electronics
7 * Author: Chanwoo Choi <cw00.choi@samsung.com>
9 * Copyright (C) 2012 Samsung Electronics
10 * Author: Donggeun Kim <dg77.kim@samsung.com>
11 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
13 * based on android/drivers/switch/switch_class.c
14 * Copyright (C) 2008 Google, Inc.
15 * Author: Mike Lockwood <lockwood@android.com>
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.
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.
27 #include <linux/module.h>
28 #include <linux/types.h>
29 #include <linux/init.h>
30 #include <linux/device.h>
32 #include <linux/err.h>
33 #include <linux/extcon.h>
35 #include <linux/slab.h>
36 #include <linux/sysfs.h>
38 #define SUPPORTED_CABLE_MAX 32
39 #define CABLE_NAME_MAX 30
41 static const char *extcon_name
[] = {
42 [EXTCON_NONE
] = "NONE",
44 /* USB external connector */
46 [EXTCON_USB_HOST
] = "USB-HOST",
48 /* Charging external connector */
49 [EXTCON_CHG_USB_SDP
] = "SDP",
50 [EXTCON_CHG_USB_DCP
] = "DCP",
51 [EXTCON_CHG_USB_CDP
] = "CDP",
52 [EXTCON_CHG_USB_ACA
] = "ACA",
53 [EXTCON_CHG_USB_FAST
] = "FAST-CHARGER",
54 [EXTCON_CHG_USB_SLOW
] = "SLOW-CHARGER",
56 /* Jack external connector */
57 [EXTCON_JACK_MICROPHONE
] = "MICROPHONE",
58 [EXTCON_JACK_HEADPHONE
] = "HEADPHONE",
59 [EXTCON_JACK_LINE_IN
] = "LINE-IN",
60 [EXTCON_JACK_LINE_OUT
] = "LINE-OUT",
61 [EXTCON_JACK_VIDEO_IN
] = "VIDEO-IN",
62 [EXTCON_JACK_VIDEO_OUT
] = "VIDEO-OUT",
63 [EXTCON_JACK_SPDIF_IN
] = "SPDIF-IN",
64 [EXTCON_JACK_SPDIF_OUT
] = "SPDIF-OUT",
66 /* Display external connector */
67 [EXTCON_DISP_HDMI
] = "HDMI",
68 [EXTCON_DISP_MHL
] = "MHL",
69 [EXTCON_DISP_DVI
] = "DVI",
70 [EXTCON_DISP_VGA
] = "VGA",
72 /* Miscellaneous external connector */
73 [EXTCON_DOCK
] = "DOCK",
75 [EXTCON_MECHANICAL
] = "MECHANICAL",
81 * struct extcon_cable - An internal data for each cable of extcon device.
82 * @edev: The extcon device
83 * @cable_index: Index of this cable in the edev
84 * @attr_g: Attribute group for the cable
85 * @attr_name: "name" sysfs entry
86 * @attr_state: "state" sysfs entry
87 * @attrs: Array pointing to attr_name and attr_state for attr_g
90 struct extcon_dev
*edev
;
93 struct attribute_group attr_g
;
94 struct device_attribute attr_name
;
95 struct device_attribute attr_state
;
97 struct attribute
*attrs
[3]; /* to be fed to attr_g.attrs */
100 static struct class *extcon_class
;
101 #if defined(CONFIG_ANDROID)
102 static struct class_compat
*switch_class
;
103 #endif /* CONFIG_ANDROID */
105 static LIST_HEAD(extcon_dev_list
);
106 static DEFINE_MUTEX(extcon_dev_list_lock
);
109 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
111 * @edev: the extcon device
112 * @new_state: new cable attach status for @edev
114 * Returns 0 if nothing violates. Returns the index + 1 for the first
115 * violated condition.
117 static int check_mutually_exclusive(struct extcon_dev
*edev
, u32 new_state
)
121 if (!edev
->mutually_exclusive
)
124 for (i
= 0; edev
->mutually_exclusive
[i
]; i
++) {
126 u32 correspondants
= new_state
& edev
->mutually_exclusive
[i
];
128 /* calculate the total number of bits set */
129 weight
= hweight32(correspondants
);
137 static int find_cable_index_by_id(struct extcon_dev
*edev
, const unsigned int id
)
141 /* Find the the index of extcon cable in edev->supported_cable */
142 for (i
= 0; i
< edev
->max_supported
; i
++) {
143 if (edev
->supported_cable
[i
] == id
)
150 static bool is_extcon_changed(u32 prev
, u32
new, int idx
, bool *attached
)
152 if (((prev
>> idx
) & 0x1) != ((new >> idx
) & 0x1)) {
153 *attached
= ((new >> idx
) & 0x1) ? true : false;
160 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
164 struct extcon_dev
*edev
= dev_get_drvdata(dev
);
166 if (edev
->max_supported
== 0)
167 return sprintf(buf
, "%u\n", edev
->state
);
169 for (i
= 0; i
< edev
->max_supported
; i
++) {
170 count
+= sprintf(buf
+ count
, "%s=%d\n",
171 extcon_name
[edev
->supported_cable
[i
]],
172 !!(edev
->state
& (1 << i
)));
178 static ssize_t
state_store(struct device
*dev
, struct device_attribute
*attr
,
179 const char *buf
, size_t count
)
183 struct extcon_dev
*edev
= dev_get_drvdata(dev
);
185 ret
= sscanf(buf
, "0x%x", &state
);
189 ret
= extcon_set_state(edev
, state
);
196 static DEVICE_ATTR_RW(state
);
198 static ssize_t
name_show(struct device
*dev
, struct device_attribute
*attr
,
201 struct extcon_dev
*edev
= dev_get_drvdata(dev
);
203 return sprintf(buf
, "%s\n", edev
->name
);
205 static DEVICE_ATTR_RO(name
);
207 static ssize_t
cable_name_show(struct device
*dev
,
208 struct device_attribute
*attr
, char *buf
)
210 struct extcon_cable
*cable
= container_of(attr
, struct extcon_cable
,
212 int i
= cable
->cable_index
;
214 return sprintf(buf
, "%s\n",
215 extcon_name
[cable
->edev
->supported_cable
[i
]]);
218 static ssize_t
cable_state_show(struct device
*dev
,
219 struct device_attribute
*attr
, char *buf
)
221 struct extcon_cable
*cable
= container_of(attr
, struct extcon_cable
,
224 int i
= cable
->cable_index
;
226 return sprintf(buf
, "%d\n",
227 extcon_get_cable_state_(cable
->edev
,
228 cable
->edev
->supported_cable
[i
]));
232 * extcon_update_state() - Update the cable attach states of the extcon device
233 * only for the masked bits.
234 * @edev: the extcon device
235 * @mask: the bit mask to designate updated bits.
236 * @state: new cable attach status for @edev
238 * Changing the state sends uevent with environment variable containing
239 * the name of extcon device (envp[0]) and the state output (envp[1]).
240 * Tizen uses this format for extcon device to get events from ports.
241 * Android uses this format as well.
243 * Note that the notifier provides which bits are changed in the state
244 * variable with the val parameter (second) to the callback.
246 int extcon_update_state(struct extcon_dev
*edev
, u32 mask
, u32 state
)
261 spin_lock_irqsave(&edev
->lock
, flags
);
263 if (edev
->state
!= ((edev
->state
& ~mask
) | (state
& mask
))) {
266 if (check_mutually_exclusive(edev
, (edev
->state
& ~mask
) |
268 spin_unlock_irqrestore(&edev
->lock
, flags
);
272 old_state
= edev
->state
;
273 edev
->state
&= ~mask
;
274 edev
->state
|= state
& mask
;
276 for (index
= 0; index
< edev
->max_supported
; index
++) {
277 if (is_extcon_changed(old_state
, edev
->state
, index
,
279 raw_notifier_call_chain(&edev
->nh
[index
],
283 /* This could be in interrupt handler */
284 prop_buf
= (char *)get_zeroed_page(GFP_ATOMIC
);
286 length
= name_show(&edev
->dev
, NULL
, prop_buf
);
288 if (prop_buf
[length
- 1] == '\n')
289 prop_buf
[length
- 1] = 0;
290 snprintf(name_buf
, sizeof(name_buf
),
291 "NAME=%s", prop_buf
);
292 envp
[env_offset
++] = name_buf
;
294 length
= state_show(&edev
->dev
, NULL
, prop_buf
);
296 if (prop_buf
[length
- 1] == '\n')
297 prop_buf
[length
- 1] = 0;
298 snprintf(state_buf
, sizeof(state_buf
),
299 "STATE=%s", prop_buf
);
300 envp
[env_offset
++] = state_buf
;
302 envp
[env_offset
] = NULL
;
303 /* Unlock early before uevent */
304 spin_unlock_irqrestore(&edev
->lock
, flags
);
306 kobject_uevent_env(&edev
->dev
.kobj
, KOBJ_CHANGE
, envp
);
307 free_page((unsigned long)prop_buf
);
309 /* Unlock early before uevent */
310 spin_unlock_irqrestore(&edev
->lock
, flags
);
312 dev_err(&edev
->dev
, "out of memory in extcon_set_state\n");
313 kobject_uevent(&edev
->dev
.kobj
, KOBJ_CHANGE
);
317 spin_unlock_irqrestore(&edev
->lock
, flags
);
322 EXPORT_SYMBOL_GPL(extcon_update_state
);
325 * extcon_set_state() - Set the cable attach states of the extcon device.
326 * @edev: the extcon device
327 * @state: new cable attach status for @edev
329 * Note that notifier provides which bits are changed in the state
330 * variable with the val parameter (second) to the callback.
332 int extcon_set_state(struct extcon_dev
*edev
, u32 state
)
337 return extcon_update_state(edev
, 0xffffffff, state
);
339 EXPORT_SYMBOL_GPL(extcon_set_state
);
342 * extcon_get_cable_state_() - Get the status of a specific cable.
343 * @edev: the extcon device that has the cable.
344 * @id: the unique id of each external connector in extcon enumeration.
346 int extcon_get_cable_state_(struct extcon_dev
*edev
, const unsigned int id
)
353 index
= find_cable_index_by_id(edev
, id
);
357 if (edev
->max_supported
&& edev
->max_supported
<= index
)
360 return !!(edev
->state
& (1 << index
));
362 EXPORT_SYMBOL_GPL(extcon_get_cable_state_
);
365 * extcon_set_cable_state_() - Set the status of a specific cable.
366 * @edev: the extcon device that has the cable.
367 * @id: the unique id of each external connector
368 * in extcon enumeration.
369 * @state: the new cable status. The default semantics is
370 * true: attached / false: detached.
372 int extcon_set_cable_state_(struct extcon_dev
*edev
, unsigned int id
,
381 index
= find_cable_index_by_id(edev
, id
);
385 if (edev
->max_supported
&& edev
->max_supported
<= index
)
388 state
= cable_state
? (1 << index
) : 0;
389 return extcon_update_state(edev
, 1 << index
, state
);
391 EXPORT_SYMBOL_GPL(extcon_set_cable_state_
);
394 * extcon_get_extcon_dev() - Get the extcon device instance from the name
395 * @extcon_name: The extcon name provided with extcon_dev_register()
397 struct extcon_dev
*extcon_get_extcon_dev(const char *extcon_name
)
399 struct extcon_dev
*sd
;
402 return ERR_PTR(-EINVAL
);
404 mutex_lock(&extcon_dev_list_lock
);
405 list_for_each_entry(sd
, &extcon_dev_list
, entry
) {
406 if (!strcmp(sd
->name
, extcon_name
))
411 mutex_unlock(&extcon_dev_list_lock
);
414 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev
);
417 * extcon_register_notifier() - Register a notifiee to get notified by
418 * any attach status changes from the extcon.
419 * @edev: the extcon device that has the external connecotr.
420 * @id: the unique id of each external connector in extcon enumeration.
421 * @nb: a notifier block to be registered.
423 * Note that the second parameter given to the callback of nb (val) is
424 * "old_state", not the current state. The current state can be retrieved
425 * by looking at the third pameter (edev pointer)'s state value.
427 int extcon_register_notifier(struct extcon_dev
*edev
, unsigned int id
,
428 struct notifier_block
*nb
)
437 idx
= find_cable_index_by_id(edev
, id
);
441 spin_lock_irqsave(&edev
->lock
, flags
);
442 ret
= raw_notifier_chain_register(&edev
->nh
[idx
], nb
);
443 spin_unlock_irqrestore(&edev
->lock
, flags
);
445 struct extcon_dev
*extd
;
447 mutex_lock(&extcon_dev_list_lock
);
448 list_for_each_entry(extd
, &extcon_dev_list
, entry
) {
449 idx
= find_cable_index_by_id(extd
, id
);
453 mutex_unlock(&extcon_dev_list_lock
);
457 return extcon_register_notifier(extd
, id
, nb
);
465 EXPORT_SYMBOL_GPL(extcon_register_notifier
);
468 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
469 * @edev: the extcon device that has the external connecotr.
470 * @id: the unique id of each external connector in extcon enumeration.
471 * @nb: a notifier block to be registered.
473 int extcon_unregister_notifier(struct extcon_dev
*edev
, unsigned int id
,
474 struct notifier_block
*nb
)
482 idx
= find_cable_index_by_id(edev
, id
);
486 spin_lock_irqsave(&edev
->lock
, flags
);
487 ret
= raw_notifier_chain_unregister(&edev
->nh
[idx
], nb
);
488 spin_unlock_irqrestore(&edev
->lock
, flags
);
492 EXPORT_SYMBOL_GPL(extcon_unregister_notifier
);
494 static struct attribute
*extcon_attrs
[] = {
495 &dev_attr_state
.attr
,
499 ATTRIBUTE_GROUPS(extcon
);
501 static int create_extcon_class(void)
504 extcon_class
= class_create(THIS_MODULE
, "extcon");
505 if (IS_ERR(extcon_class
))
506 return PTR_ERR(extcon_class
);
507 extcon_class
->dev_groups
= extcon_groups
;
509 #if defined(CONFIG_ANDROID)
510 switch_class
= class_compat_register("switch");
511 if (WARN(!switch_class
, "cannot allocate"))
513 #endif /* CONFIG_ANDROID */
519 static void extcon_dev_release(struct device
*dev
)
523 static const char *muex_name
= "mutually_exclusive";
524 static void dummy_sysfs_dev_release(struct device
*dev
)
529 * extcon_dev_allocate() - Allocate the memory of extcon device.
530 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
531 * If supported_cable is NULL, cable name related APIs
534 * This function allocates the memory for extcon device without allocating
535 * memory in each extcon provider driver and initialize default setting for
538 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
540 struct extcon_dev
*extcon_dev_allocate(const unsigned int *supported_cable
)
542 struct extcon_dev
*edev
;
544 if (!supported_cable
)
545 return ERR_PTR(-EINVAL
);
547 edev
= kzalloc(sizeof(*edev
), GFP_KERNEL
);
549 return ERR_PTR(-ENOMEM
);
551 edev
->max_supported
= 0;
552 edev
->supported_cable
= supported_cable
;
558 * extcon_dev_free() - Free the memory of extcon device.
559 * @edev: the extcon device to free
561 void extcon_dev_free(struct extcon_dev
*edev
)
565 EXPORT_SYMBOL_GPL(extcon_dev_free
);
567 static int devm_extcon_dev_match(struct device
*dev
, void *res
, void *data
)
569 struct extcon_dev
**r
= res
;
571 if (WARN_ON(!r
|| !*r
))
577 static void devm_extcon_dev_release(struct device
*dev
, void *res
)
579 extcon_dev_free(*(struct extcon_dev
**)res
);
583 * devm_extcon_dev_allocate - Allocate managed extcon device
584 * @dev: device owning the extcon device being created
585 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
586 * If supported_cable is NULL, cable name related APIs
589 * This function manages automatically the memory of extcon device using device
590 * resource management and simplify the control of freeing the memory of extcon
593 * Returns the pointer memory of allocated extcon_dev if success
594 * or ERR_PTR(err) if fail
596 struct extcon_dev
*devm_extcon_dev_allocate(struct device
*dev
,
597 const unsigned int *supported_cable
)
599 struct extcon_dev
**ptr
, *edev
;
601 ptr
= devres_alloc(devm_extcon_dev_release
, sizeof(*ptr
), GFP_KERNEL
);
603 return ERR_PTR(-ENOMEM
);
605 edev
= extcon_dev_allocate(supported_cable
);
611 edev
->dev
.parent
= dev
;
614 devres_add(dev
, ptr
);
618 EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate
);
620 void devm_extcon_dev_free(struct device
*dev
, struct extcon_dev
*edev
)
622 WARN_ON(devres_release(dev
, devm_extcon_dev_release
,
623 devm_extcon_dev_match
, edev
));
625 EXPORT_SYMBOL_GPL(devm_extcon_dev_free
);
628 * extcon_dev_register() - Register a new extcon device
629 * @edev : the new extcon device (should be allocated before calling)
631 * Among the members of edev struct, please set the "user initializing data"
632 * in any case and set the "optional callbacks" if required. However, please
633 * do not set the values of "internal data", which are initialized by
636 int extcon_dev_register(struct extcon_dev
*edev
)
639 static atomic_t edev_no
= ATOMIC_INIT(-1);
642 ret
= create_extcon_class();
647 if (!edev
|| !edev
->supported_cable
)
650 for (; edev
->supported_cable
[index
] != EXTCON_NONE
; index
++);
652 edev
->max_supported
= index
;
653 if (index
> SUPPORTED_CABLE_MAX
) {
655 "exceed the maximum number of supported cables\n");
659 edev
->dev
.class = extcon_class
;
660 edev
->dev
.release
= extcon_dev_release
;
662 edev
->name
= dev_name(edev
->dev
.parent
);
663 if (IS_ERR_OR_NULL(edev
->name
)) {
665 "extcon device name is null\n");
668 dev_set_name(&edev
->dev
, "extcon%lu",
669 (unsigned long)atomic_inc_return(&edev_no
));
671 if (edev
->max_supported
) {
674 struct extcon_cable
*cable
;
676 edev
->cables
= kzalloc(sizeof(struct extcon_cable
) *
677 edev
->max_supported
, GFP_KERNEL
);
680 goto err_sysfs_alloc
;
682 for (index
= 0; index
< edev
->max_supported
; index
++) {
683 cable
= &edev
->cables
[index
];
685 snprintf(buf
, 10, "cable.%d", index
);
686 str
= kzalloc(sizeof(char) * (strlen(buf
) + 1),
689 for (index
--; index
>= 0; index
--) {
690 cable
= &edev
->cables
[index
];
691 kfree(cable
->attr_g
.name
);
695 goto err_alloc_cables
;
700 cable
->cable_index
= index
;
701 cable
->attrs
[0] = &cable
->attr_name
.attr
;
702 cable
->attrs
[1] = &cable
->attr_state
.attr
;
703 cable
->attrs
[2] = NULL
;
704 cable
->attr_g
.name
= str
;
705 cable
->attr_g
.attrs
= cable
->attrs
;
707 sysfs_attr_init(&cable
->attr_name
.attr
);
708 cable
->attr_name
.attr
.name
= "name";
709 cable
->attr_name
.attr
.mode
= 0444;
710 cable
->attr_name
.show
= cable_name_show
;
712 sysfs_attr_init(&cable
->attr_state
.attr
);
713 cable
->attr_state
.attr
.name
= "state";
714 cable
->attr_state
.attr
.mode
= 0444;
715 cable
->attr_state
.show
= cable_state_show
;
719 if (edev
->max_supported
&& edev
->mutually_exclusive
) {
723 /* Count the size of mutually_exclusive array */
724 for (index
= 0; edev
->mutually_exclusive
[index
]; index
++)
727 edev
->attrs_muex
= kzalloc(sizeof(struct attribute
*) *
728 (index
+ 1), GFP_KERNEL
);
729 if (!edev
->attrs_muex
) {
734 edev
->d_attrs_muex
= kzalloc(sizeof(struct device_attribute
) *
736 if (!edev
->d_attrs_muex
) {
738 kfree(edev
->attrs_muex
);
742 for (index
= 0; edev
->mutually_exclusive
[index
]; index
++) {
743 sprintf(buf
, "0x%x", edev
->mutually_exclusive
[index
]);
744 name
= kzalloc(sizeof(char) * (strlen(buf
) + 1),
747 for (index
--; index
>= 0; index
--) {
748 kfree(edev
->d_attrs_muex
[index
].attr
.
751 kfree(edev
->d_attrs_muex
);
752 kfree(edev
->attrs_muex
);
757 sysfs_attr_init(&edev
->d_attrs_muex
[index
].attr
);
758 edev
->d_attrs_muex
[index
].attr
.name
= name
;
759 edev
->d_attrs_muex
[index
].attr
.mode
= 0000;
760 edev
->attrs_muex
[index
] = &edev
->d_attrs_muex
[index
]
763 edev
->attr_g_muex
.name
= muex_name
;
764 edev
->attr_g_muex
.attrs
= edev
->attrs_muex
;
768 if (edev
->max_supported
) {
769 edev
->extcon_dev_type
.groups
=
770 kzalloc(sizeof(struct attribute_group
*) *
771 (edev
->max_supported
+ 2), GFP_KERNEL
);
772 if (!edev
->extcon_dev_type
.groups
) {
774 goto err_alloc_groups
;
777 edev
->extcon_dev_type
.name
= dev_name(&edev
->dev
);
778 edev
->extcon_dev_type
.release
= dummy_sysfs_dev_release
;
780 for (index
= 0; index
< edev
->max_supported
; index
++)
781 edev
->extcon_dev_type
.groups
[index
] =
782 &edev
->cables
[index
].attr_g
;
783 if (edev
->mutually_exclusive
)
784 edev
->extcon_dev_type
.groups
[index
] =
787 edev
->dev
.type
= &edev
->extcon_dev_type
;
790 ret
= device_register(&edev
->dev
);
792 put_device(&edev
->dev
);
795 #if defined(CONFIG_ANDROID)
797 ret
= class_compat_create_link(switch_class
, &edev
->dev
, NULL
);
798 #endif /* CONFIG_ANDROID */
800 spin_lock_init(&edev
->lock
);
802 edev
->nh
= devm_kzalloc(&edev
->dev
,
803 sizeof(*edev
->nh
) * edev
->max_supported
, GFP_KERNEL
);
809 for (index
= 0; index
< edev
->max_supported
; index
++)
810 RAW_INIT_NOTIFIER_HEAD(&edev
->nh
[index
]);
812 dev_set_drvdata(&edev
->dev
, edev
);
815 mutex_lock(&extcon_dev_list_lock
);
816 list_add(&edev
->entry
, &extcon_dev_list
);
817 mutex_unlock(&extcon_dev_list_lock
);
822 if (edev
->max_supported
)
823 kfree(edev
->extcon_dev_type
.groups
);
825 if (edev
->max_supported
&& edev
->mutually_exclusive
) {
826 for (index
= 0; edev
->mutually_exclusive
[index
]; index
++)
827 kfree(edev
->d_attrs_muex
[index
].attr
.name
);
828 kfree(edev
->d_attrs_muex
);
829 kfree(edev
->attrs_muex
);
832 for (index
= 0; index
< edev
->max_supported
; index
++)
833 kfree(edev
->cables
[index
].attr_g
.name
);
835 if (edev
->max_supported
)
840 EXPORT_SYMBOL_GPL(extcon_dev_register
);
843 * extcon_dev_unregister() - Unregister the extcon device.
844 * @edev: the extcon device instance to be unregistered.
846 * Note that this does not call kfree(edev) because edev was not allocated
849 void extcon_dev_unregister(struct extcon_dev
*edev
)
856 mutex_lock(&extcon_dev_list_lock
);
857 list_del(&edev
->entry
);
858 mutex_unlock(&extcon_dev_list_lock
);
860 if (IS_ERR_OR_NULL(get_device(&edev
->dev
))) {
861 dev_err(&edev
->dev
, "Failed to unregister extcon_dev (%s)\n",
862 dev_name(&edev
->dev
));
866 device_unregister(&edev
->dev
);
868 if (edev
->mutually_exclusive
&& edev
->max_supported
) {
869 for (index
= 0; edev
->mutually_exclusive
[index
];
871 kfree(edev
->d_attrs_muex
[index
].attr
.name
);
872 kfree(edev
->d_attrs_muex
);
873 kfree(edev
->attrs_muex
);
876 for (index
= 0; index
< edev
->max_supported
; index
++)
877 kfree(edev
->cables
[index
].attr_g
.name
);
879 if (edev
->max_supported
) {
880 kfree(edev
->extcon_dev_type
.groups
);
884 #if defined(CONFIG_ANDROID)
886 class_compat_remove_link(switch_class
, &edev
->dev
, NULL
);
888 put_device(&edev
->dev
);
890 EXPORT_SYMBOL_GPL(extcon_dev_unregister
);
892 static void devm_extcon_dev_unreg(struct device
*dev
, void *res
)
894 extcon_dev_unregister(*(struct extcon_dev
**)res
);
898 * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
899 * @dev: device to allocate extcon device
900 * @edev: the new extcon device to register
902 * Managed extcon_dev_register() function. If extcon device is attached with
903 * this function, that extcon device is automatically unregistered on driver
904 * detach. Internally this function calls extcon_dev_register() function.
905 * To get more information, refer that function.
907 * If extcon device is registered with this function and the device needs to be
908 * unregistered separately, devm_extcon_dev_unregister() should be used.
910 * Returns 0 if success or negaive error number if failure.
912 int devm_extcon_dev_register(struct device
*dev
, struct extcon_dev
*edev
)
914 struct extcon_dev
**ptr
;
917 ptr
= devres_alloc(devm_extcon_dev_unreg
, sizeof(*ptr
), GFP_KERNEL
);
921 ret
= extcon_dev_register(edev
);
928 devres_add(dev
, ptr
);
932 EXPORT_SYMBOL_GPL(devm_extcon_dev_register
);
935 * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
936 * @dev: device the extcon belongs to
937 * @edev: the extcon device to unregister
939 * Unregister extcon device that is registered with devm_extcon_dev_register()
942 void devm_extcon_dev_unregister(struct device
*dev
, struct extcon_dev
*edev
)
944 WARN_ON(devres_release(dev
, devm_extcon_dev_unreg
,
945 devm_extcon_dev_match
, edev
));
947 EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister
);
951 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
952 * @dev - instance to the given device
953 * @index - index into list of extcon_dev
955 * return the instance of extcon device
957 struct extcon_dev
*extcon_get_edev_by_phandle(struct device
*dev
, int index
)
959 struct device_node
*node
;
960 struct extcon_dev
*edev
;
963 return ERR_PTR(-EINVAL
);
966 dev_err(dev
, "device does not have a device node entry\n");
967 return ERR_PTR(-EINVAL
);
970 node
= of_parse_phandle(dev
->of_node
, "extcon", index
);
972 dev_err(dev
, "failed to get phandle in %s node\n",
973 dev
->of_node
->full_name
);
974 return ERR_PTR(-ENODEV
);
977 mutex_lock(&extcon_dev_list_lock
);
978 list_for_each_entry(edev
, &extcon_dev_list
, entry
) {
979 if (edev
->dev
.parent
&& edev
->dev
.parent
->of_node
== node
) {
980 mutex_unlock(&extcon_dev_list_lock
);
984 mutex_unlock(&extcon_dev_list_lock
);
986 return ERR_PTR(-EPROBE_DEFER
);
989 struct extcon_dev
*extcon_get_edev_by_phandle(struct device
*dev
, int index
)
991 return ERR_PTR(-ENOSYS
);
993 #endif /* CONFIG_OF */
994 EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle
);
997 * extcon_get_edev_name() - Get the name of the extcon device.
998 * @edev: the extcon device
1000 const char *extcon_get_edev_name(struct extcon_dev
*edev
)
1002 return !edev
? NULL
: edev
->name
;
1005 static int __init
extcon_class_init(void)
1007 return create_extcon_class();
1009 module_init(extcon_class_init
);
1011 static void __exit
extcon_class_exit(void)
1013 #if defined(CONFIG_ANDROID)
1014 class_compat_unregister(switch_class
);
1016 class_destroy(extcon_class
);
1018 module_exit(extcon_class_exit
);
1020 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1021 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1022 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1023 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1024 MODULE_DESCRIPTION("External connector (extcon) class driver");
1025 MODULE_LICENSE("GPL");