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",
80 static struct class *extcon_class
;
81 #if defined(CONFIG_ANDROID)
82 static struct class_compat
*switch_class
;
83 #endif /* CONFIG_ANDROID */
85 static LIST_HEAD(extcon_dev_list
);
86 static DEFINE_MUTEX(extcon_dev_list_lock
);
89 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
91 * @edev: the extcon device
92 * @new_state: new cable attach status for @edev
94 * Returns 0 if nothing violates. Returns the index + 1 for the first
97 static int check_mutually_exclusive(struct extcon_dev
*edev
, u32 new_state
)
101 if (!edev
->mutually_exclusive
)
104 for (i
= 0; edev
->mutually_exclusive
[i
]; i
++) {
106 u32 correspondants
= new_state
& edev
->mutually_exclusive
[i
];
108 /* calculate the total number of bits set */
109 weight
= hweight32(correspondants
);
117 static int find_cable_index_by_id(struct extcon_dev
*edev
, const unsigned int id
)
121 /* Find the the index of extcon cable in edev->supported_cable */
122 for (i
= 0; i
< edev
->max_supported
; i
++) {
123 if (edev
->supported_cable
[i
] == id
)
130 static bool is_extcon_changed(u32 prev
, u32
new, int idx
, bool *attached
)
132 if (((prev
>> idx
) & 0x1) != ((new >> idx
) & 0x1)) {
133 *attached
= ((new >> idx
) & 0x1) ? true : false;
140 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
144 struct extcon_dev
*edev
= dev_get_drvdata(dev
);
146 if (edev
->max_supported
== 0)
147 return sprintf(buf
, "%u\n", edev
->state
);
149 for (i
= 0; i
< edev
->max_supported
; i
++) {
150 count
+= sprintf(buf
+ count
, "%s=%d\n",
151 extcon_name
[edev
->supported_cable
[i
]],
152 !!(edev
->state
& (1 << i
)));
158 static ssize_t
state_store(struct device
*dev
, struct device_attribute
*attr
,
159 const char *buf
, size_t count
)
163 struct extcon_dev
*edev
= dev_get_drvdata(dev
);
165 ret
= sscanf(buf
, "0x%x", &state
);
169 ret
= extcon_set_state(edev
, state
);
176 static DEVICE_ATTR_RW(state
);
178 static ssize_t
name_show(struct device
*dev
, struct device_attribute
*attr
,
181 struct extcon_dev
*edev
= dev_get_drvdata(dev
);
183 return sprintf(buf
, "%s\n", edev
->name
);
185 static DEVICE_ATTR_RO(name
);
187 static ssize_t
cable_name_show(struct device
*dev
,
188 struct device_attribute
*attr
, char *buf
)
190 struct extcon_cable
*cable
= container_of(attr
, struct extcon_cable
,
192 int i
= cable
->cable_index
;
194 return sprintf(buf
, "%s\n",
195 extcon_name
[cable
->edev
->supported_cable
[i
]]);
198 static ssize_t
cable_state_show(struct device
*dev
,
199 struct device_attribute
*attr
, char *buf
)
201 struct extcon_cable
*cable
= container_of(attr
, struct extcon_cable
,
204 int i
= cable
->cable_index
;
206 return sprintf(buf
, "%d\n",
207 extcon_get_cable_state_(cable
->edev
,
208 cable
->edev
->supported_cable
[i
]));
212 * extcon_update_state() - Update the cable attach states of the extcon device
213 * only for the masked bits.
214 * @edev: the extcon device
215 * @mask: the bit mask to designate updated bits.
216 * @state: new cable attach status for @edev
218 * Changing the state sends uevent with environment variable containing
219 * the name of extcon device (envp[0]) and the state output (envp[1]).
220 * Tizen uses this format for extcon device to get events from ports.
221 * Android uses this format as well.
223 * Note that the notifier provides which bits are changed in the state
224 * variable with the val parameter (second) to the callback.
226 int extcon_update_state(struct extcon_dev
*edev
, u32 mask
, u32 state
)
241 spin_lock_irqsave(&edev
->lock
, flags
);
243 if (edev
->state
!= ((edev
->state
& ~mask
) | (state
& mask
))) {
246 if (check_mutually_exclusive(edev
, (edev
->state
& ~mask
) |
248 spin_unlock_irqrestore(&edev
->lock
, flags
);
252 old_state
= edev
->state
;
253 edev
->state
&= ~mask
;
254 edev
->state
|= state
& mask
;
256 for (index
= 0; index
< edev
->max_supported
; index
++) {
257 if (is_extcon_changed(old_state
, edev
->state
, index
,
259 raw_notifier_call_chain(&edev
->nh
[index
],
263 /* This could be in interrupt handler */
264 prop_buf
= (char *)get_zeroed_page(GFP_ATOMIC
);
266 length
= name_show(&edev
->dev
, NULL
, prop_buf
);
268 if (prop_buf
[length
- 1] == '\n')
269 prop_buf
[length
- 1] = 0;
270 snprintf(name_buf
, sizeof(name_buf
),
271 "NAME=%s", prop_buf
);
272 envp
[env_offset
++] = name_buf
;
274 length
= state_show(&edev
->dev
, NULL
, prop_buf
);
276 if (prop_buf
[length
- 1] == '\n')
277 prop_buf
[length
- 1] = 0;
278 snprintf(state_buf
, sizeof(state_buf
),
279 "STATE=%s", prop_buf
);
280 envp
[env_offset
++] = state_buf
;
282 envp
[env_offset
] = NULL
;
283 /* Unlock early before uevent */
284 spin_unlock_irqrestore(&edev
->lock
, flags
);
286 kobject_uevent_env(&edev
->dev
.kobj
, KOBJ_CHANGE
, envp
);
287 free_page((unsigned long)prop_buf
);
289 /* Unlock early before uevent */
290 spin_unlock_irqrestore(&edev
->lock
, flags
);
292 dev_err(&edev
->dev
, "out of memory in extcon_set_state\n");
293 kobject_uevent(&edev
->dev
.kobj
, KOBJ_CHANGE
);
297 spin_unlock_irqrestore(&edev
->lock
, flags
);
302 EXPORT_SYMBOL_GPL(extcon_update_state
);
305 * extcon_set_state() - Set the cable attach states of the extcon device.
306 * @edev: the extcon device
307 * @state: new cable attach status for @edev
309 * Note that notifier provides which bits are changed in the state
310 * variable with the val parameter (second) to the callback.
312 int extcon_set_state(struct extcon_dev
*edev
, u32 state
)
317 return extcon_update_state(edev
, 0xffffffff, state
);
319 EXPORT_SYMBOL_GPL(extcon_set_state
);
322 * extcon_get_cable_state_() - Get the status of a specific cable.
323 * @edev: the extcon device that has the cable.
324 * @id: the unique id of each external connector in extcon enumeration.
326 int extcon_get_cable_state_(struct extcon_dev
*edev
, const unsigned int id
)
333 index
= find_cable_index_by_id(edev
, id
);
337 if (edev
->max_supported
&& edev
->max_supported
<= index
)
340 return !!(edev
->state
& (1 << index
));
342 EXPORT_SYMBOL_GPL(extcon_get_cable_state_
);
345 * extcon_set_cable_state_() - Set the status of a specific cable.
346 * @edev: the extcon device that has the cable.
347 * @id: the unique id of each external connector
348 * in extcon enumeration.
349 * @state: the new cable status. The default semantics is
350 * true: attached / false: detached.
352 int extcon_set_cable_state_(struct extcon_dev
*edev
, unsigned int id
,
361 index
= find_cable_index_by_id(edev
, id
);
365 if (edev
->max_supported
&& edev
->max_supported
<= index
)
368 state
= cable_state
? (1 << index
) : 0;
369 return extcon_update_state(edev
, 1 << index
, state
);
371 EXPORT_SYMBOL_GPL(extcon_set_cable_state_
);
374 * extcon_get_extcon_dev() - Get the extcon device instance from the name
375 * @extcon_name: The extcon name provided with extcon_dev_register()
377 struct extcon_dev
*extcon_get_extcon_dev(const char *extcon_name
)
379 struct extcon_dev
*sd
;
382 return ERR_PTR(-EINVAL
);
384 mutex_lock(&extcon_dev_list_lock
);
385 list_for_each_entry(sd
, &extcon_dev_list
, entry
) {
386 if (!strcmp(sd
->name
, extcon_name
))
391 mutex_unlock(&extcon_dev_list_lock
);
394 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev
);
397 * extcon_register_notifier() - Register a notifiee to get notified by
398 * any attach status changes from the extcon.
399 * @edev: the extcon device that has the external connecotr.
400 * @id: the unique id of each external connector in extcon enumeration.
401 * @nb: a notifier block to be registered.
403 * Note that the second parameter given to the callback of nb (val) is
404 * "old_state", not the current state. The current state can be retrieved
405 * by looking at the third pameter (edev pointer)'s state value.
407 int extcon_register_notifier(struct extcon_dev
*edev
, unsigned int id
,
408 struct notifier_block
*nb
)
417 idx
= find_cable_index_by_id(edev
, id
);
419 spin_lock_irqsave(&edev
->lock
, flags
);
420 ret
= raw_notifier_chain_register(&edev
->nh
[idx
], nb
);
421 spin_unlock_irqrestore(&edev
->lock
, flags
);
423 struct extcon_dev
*extd
;
425 mutex_lock(&extcon_dev_list_lock
);
426 list_for_each_entry(extd
, &extcon_dev_list
, entry
) {
427 idx
= find_cable_index_by_id(extd
, id
);
431 mutex_unlock(&extcon_dev_list_lock
);
435 return extcon_register_notifier(extd
, id
, nb
);
443 EXPORT_SYMBOL_GPL(extcon_register_notifier
);
446 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
447 * @edev: the extcon device that has the external connecotr.
448 * @id: the unique id of each external connector in extcon enumeration.
449 * @nb: a notifier block to be registered.
451 int extcon_unregister_notifier(struct extcon_dev
*edev
, unsigned int id
,
452 struct notifier_block
*nb
)
460 idx
= find_cable_index_by_id(edev
, id
);
462 spin_lock_irqsave(&edev
->lock
, flags
);
463 ret
= raw_notifier_chain_unregister(&edev
->nh
[idx
], nb
);
464 spin_unlock_irqrestore(&edev
->lock
, flags
);
468 EXPORT_SYMBOL_GPL(extcon_unregister_notifier
);
470 static struct attribute
*extcon_attrs
[] = {
471 &dev_attr_state
.attr
,
475 ATTRIBUTE_GROUPS(extcon
);
477 static int create_extcon_class(void)
480 extcon_class
= class_create(THIS_MODULE
, "extcon");
481 if (IS_ERR(extcon_class
))
482 return PTR_ERR(extcon_class
);
483 extcon_class
->dev_groups
= extcon_groups
;
485 #if defined(CONFIG_ANDROID)
486 switch_class
= class_compat_register("switch");
487 if (WARN(!switch_class
, "cannot allocate"))
489 #endif /* CONFIG_ANDROID */
495 static void extcon_dev_release(struct device
*dev
)
499 static const char *muex_name
= "mutually_exclusive";
500 static void dummy_sysfs_dev_release(struct device
*dev
)
505 * extcon_dev_allocate() - Allocate the memory of extcon device.
506 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
507 * If supported_cable is NULL, cable name related APIs
510 * This function allocates the memory for extcon device without allocating
511 * memory in each extcon provider driver and initialize default setting for
514 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
516 struct extcon_dev
*extcon_dev_allocate(const unsigned int *supported_cable
)
518 struct extcon_dev
*edev
;
520 if (!supported_cable
)
521 return ERR_PTR(-EINVAL
);
523 edev
= kzalloc(sizeof(*edev
), GFP_KERNEL
);
525 return ERR_PTR(-ENOMEM
);
527 edev
->max_supported
= 0;
528 edev
->supported_cable
= supported_cable
;
534 * extcon_dev_free() - Free the memory of extcon device.
535 * @edev: the extcon device to free
537 void extcon_dev_free(struct extcon_dev
*edev
)
541 EXPORT_SYMBOL_GPL(extcon_dev_free
);
543 static int devm_extcon_dev_match(struct device
*dev
, void *res
, void *data
)
545 struct extcon_dev
**r
= res
;
547 if (WARN_ON(!r
|| !*r
))
553 static void devm_extcon_dev_release(struct device
*dev
, void *res
)
555 extcon_dev_free(*(struct extcon_dev
**)res
);
559 * devm_extcon_dev_allocate - Allocate managed extcon device
560 * @dev: device owning the extcon device being created
561 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
562 * If supported_cable is NULL, cable name related APIs
565 * This function manages automatically the memory of extcon device using device
566 * resource management and simplify the control of freeing the memory of extcon
569 * Returns the pointer memory of allocated extcon_dev if success
570 * or ERR_PTR(err) if fail
572 struct extcon_dev
*devm_extcon_dev_allocate(struct device
*dev
,
573 const unsigned int *supported_cable
)
575 struct extcon_dev
**ptr
, *edev
;
577 ptr
= devres_alloc(devm_extcon_dev_release
, sizeof(*ptr
), GFP_KERNEL
);
579 return ERR_PTR(-ENOMEM
);
581 edev
= extcon_dev_allocate(supported_cable
);
587 edev
->dev
.parent
= dev
;
590 devres_add(dev
, ptr
);
594 EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate
);
596 void devm_extcon_dev_free(struct device
*dev
, struct extcon_dev
*edev
)
598 WARN_ON(devres_release(dev
, devm_extcon_dev_release
,
599 devm_extcon_dev_match
, edev
));
601 EXPORT_SYMBOL_GPL(devm_extcon_dev_free
);
604 * extcon_dev_register() - Register a new extcon device
605 * @edev : the new extcon device (should be allocated before calling)
607 * Among the members of edev struct, please set the "user initializing data"
608 * in any case and set the "optional callbacks" if required. However, please
609 * do not set the values of "internal data", which are initialized by
612 int extcon_dev_register(struct extcon_dev
*edev
)
615 static atomic_t edev_no
= ATOMIC_INIT(-1);
618 ret
= create_extcon_class();
623 if (!edev
|| !edev
->supported_cable
)
626 for (; edev
->supported_cable
[index
] != EXTCON_NONE
; index
++);
628 edev
->max_supported
= index
;
629 if (index
> SUPPORTED_CABLE_MAX
) {
631 "exceed the maximum number of supported cables\n");
635 edev
->dev
.class = extcon_class
;
636 edev
->dev
.release
= extcon_dev_release
;
638 edev
->name
= dev_name(edev
->dev
.parent
);
639 if (IS_ERR_OR_NULL(edev
->name
)) {
641 "extcon device name is null\n");
644 dev_set_name(&edev
->dev
, "extcon%lu",
645 (unsigned long)atomic_inc_return(&edev_no
));
647 if (edev
->max_supported
) {
650 struct extcon_cable
*cable
;
652 edev
->cables
= kzalloc(sizeof(struct extcon_cable
) *
653 edev
->max_supported
, GFP_KERNEL
);
656 goto err_sysfs_alloc
;
658 for (index
= 0; index
< edev
->max_supported
; index
++) {
659 cable
= &edev
->cables
[index
];
661 snprintf(buf
, 10, "cable.%d", index
);
662 str
= kzalloc(sizeof(char) * (strlen(buf
) + 1),
665 for (index
--; index
>= 0; index
--) {
666 cable
= &edev
->cables
[index
];
667 kfree(cable
->attr_g
.name
);
671 goto err_alloc_cables
;
676 cable
->cable_index
= index
;
677 cable
->attrs
[0] = &cable
->attr_name
.attr
;
678 cable
->attrs
[1] = &cable
->attr_state
.attr
;
679 cable
->attrs
[2] = NULL
;
680 cable
->attr_g
.name
= str
;
681 cable
->attr_g
.attrs
= cable
->attrs
;
683 sysfs_attr_init(&cable
->attr_name
.attr
);
684 cable
->attr_name
.attr
.name
= "name";
685 cable
->attr_name
.attr
.mode
= 0444;
686 cable
->attr_name
.show
= cable_name_show
;
688 sysfs_attr_init(&cable
->attr_state
.attr
);
689 cable
->attr_state
.attr
.name
= "state";
690 cable
->attr_state
.attr
.mode
= 0444;
691 cable
->attr_state
.show
= cable_state_show
;
695 if (edev
->max_supported
&& edev
->mutually_exclusive
) {
699 /* Count the size of mutually_exclusive array */
700 for (index
= 0; edev
->mutually_exclusive
[index
]; index
++)
703 edev
->attrs_muex
= kzalloc(sizeof(struct attribute
*) *
704 (index
+ 1), GFP_KERNEL
);
705 if (!edev
->attrs_muex
) {
710 edev
->d_attrs_muex
= kzalloc(sizeof(struct device_attribute
) *
712 if (!edev
->d_attrs_muex
) {
714 kfree(edev
->attrs_muex
);
718 for (index
= 0; edev
->mutually_exclusive
[index
]; index
++) {
719 sprintf(buf
, "0x%x", edev
->mutually_exclusive
[index
]);
720 name
= kzalloc(sizeof(char) * (strlen(buf
) + 1),
723 for (index
--; index
>= 0; index
--) {
724 kfree(edev
->d_attrs_muex
[index
].attr
.
727 kfree(edev
->d_attrs_muex
);
728 kfree(edev
->attrs_muex
);
733 sysfs_attr_init(&edev
->d_attrs_muex
[index
].attr
);
734 edev
->d_attrs_muex
[index
].attr
.name
= name
;
735 edev
->d_attrs_muex
[index
].attr
.mode
= 0000;
736 edev
->attrs_muex
[index
] = &edev
->d_attrs_muex
[index
]
739 edev
->attr_g_muex
.name
= muex_name
;
740 edev
->attr_g_muex
.attrs
= edev
->attrs_muex
;
744 if (edev
->max_supported
) {
745 edev
->extcon_dev_type
.groups
=
746 kzalloc(sizeof(struct attribute_group
*) *
747 (edev
->max_supported
+ 2), GFP_KERNEL
);
748 if (!edev
->extcon_dev_type
.groups
) {
750 goto err_alloc_groups
;
753 edev
->extcon_dev_type
.name
= dev_name(&edev
->dev
);
754 edev
->extcon_dev_type
.release
= dummy_sysfs_dev_release
;
756 for (index
= 0; index
< edev
->max_supported
; index
++)
757 edev
->extcon_dev_type
.groups
[index
] =
758 &edev
->cables
[index
].attr_g
;
759 if (edev
->mutually_exclusive
)
760 edev
->extcon_dev_type
.groups
[index
] =
763 edev
->dev
.type
= &edev
->extcon_dev_type
;
766 ret
= device_register(&edev
->dev
);
768 put_device(&edev
->dev
);
771 #if defined(CONFIG_ANDROID)
773 ret
= class_compat_create_link(switch_class
, &edev
->dev
, NULL
);
774 #endif /* CONFIG_ANDROID */
776 spin_lock_init(&edev
->lock
);
778 edev
->nh
= devm_kzalloc(&edev
->dev
,
779 sizeof(*edev
->nh
) * edev
->max_supported
, GFP_KERNEL
);
785 for (index
= 0; index
< edev
->max_supported
; index
++)
786 RAW_INIT_NOTIFIER_HEAD(&edev
->nh
[index
]);
788 dev_set_drvdata(&edev
->dev
, edev
);
791 mutex_lock(&extcon_dev_list_lock
);
792 list_add(&edev
->entry
, &extcon_dev_list
);
793 mutex_unlock(&extcon_dev_list_lock
);
798 if (edev
->max_supported
)
799 kfree(edev
->extcon_dev_type
.groups
);
801 if (edev
->max_supported
&& edev
->mutually_exclusive
) {
802 for (index
= 0; edev
->mutually_exclusive
[index
]; index
++)
803 kfree(edev
->d_attrs_muex
[index
].attr
.name
);
804 kfree(edev
->d_attrs_muex
);
805 kfree(edev
->attrs_muex
);
808 for (index
= 0; index
< edev
->max_supported
; index
++)
809 kfree(edev
->cables
[index
].attr_g
.name
);
811 if (edev
->max_supported
)
816 EXPORT_SYMBOL_GPL(extcon_dev_register
);
819 * extcon_dev_unregister() - Unregister the extcon device.
820 * @edev: the extcon device instance to be unregistered.
822 * Note that this does not call kfree(edev) because edev was not allocated
825 void extcon_dev_unregister(struct extcon_dev
*edev
)
832 mutex_lock(&extcon_dev_list_lock
);
833 list_del(&edev
->entry
);
834 mutex_unlock(&extcon_dev_list_lock
);
836 if (IS_ERR_OR_NULL(get_device(&edev
->dev
))) {
837 dev_err(&edev
->dev
, "Failed to unregister extcon_dev (%s)\n",
838 dev_name(&edev
->dev
));
842 device_unregister(&edev
->dev
);
844 if (edev
->mutually_exclusive
&& edev
->max_supported
) {
845 for (index
= 0; edev
->mutually_exclusive
[index
];
847 kfree(edev
->d_attrs_muex
[index
].attr
.name
);
848 kfree(edev
->d_attrs_muex
);
849 kfree(edev
->attrs_muex
);
852 for (index
= 0; index
< edev
->max_supported
; index
++)
853 kfree(edev
->cables
[index
].attr_g
.name
);
855 if (edev
->max_supported
) {
856 kfree(edev
->extcon_dev_type
.groups
);
860 #if defined(CONFIG_ANDROID)
862 class_compat_remove_link(switch_class
, &edev
->dev
, NULL
);
864 put_device(&edev
->dev
);
866 EXPORT_SYMBOL_GPL(extcon_dev_unregister
);
868 static void devm_extcon_dev_unreg(struct device
*dev
, void *res
)
870 extcon_dev_unregister(*(struct extcon_dev
**)res
);
874 * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
875 * @dev: device to allocate extcon device
876 * @edev: the new extcon device to register
878 * Managed extcon_dev_register() function. If extcon device is attached with
879 * this function, that extcon device is automatically unregistered on driver
880 * detach. Internally this function calls extcon_dev_register() function.
881 * To get more information, refer that function.
883 * If extcon device is registered with this function and the device needs to be
884 * unregistered separately, devm_extcon_dev_unregister() should be used.
886 * Returns 0 if success or negaive error number if failure.
888 int devm_extcon_dev_register(struct device
*dev
, struct extcon_dev
*edev
)
890 struct extcon_dev
**ptr
;
893 ptr
= devres_alloc(devm_extcon_dev_unreg
, sizeof(*ptr
), GFP_KERNEL
);
897 ret
= extcon_dev_register(edev
);
904 devres_add(dev
, ptr
);
908 EXPORT_SYMBOL_GPL(devm_extcon_dev_register
);
911 * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
912 * @dev: device the extcon belongs to
913 * @edev: the extcon device to unregister
915 * Unregister extcon device that is registered with devm_extcon_dev_register()
918 void devm_extcon_dev_unregister(struct device
*dev
, struct extcon_dev
*edev
)
920 WARN_ON(devres_release(dev
, devm_extcon_dev_unreg
,
921 devm_extcon_dev_match
, edev
));
923 EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister
);
927 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
928 * @dev - instance to the given device
929 * @index - index into list of extcon_dev
931 * return the instance of extcon device
933 struct extcon_dev
*extcon_get_edev_by_phandle(struct device
*dev
, int index
)
935 struct device_node
*node
;
936 struct extcon_dev
*edev
;
939 return ERR_PTR(-EINVAL
);
942 dev_err(dev
, "device does not have a device node entry\n");
943 return ERR_PTR(-EINVAL
);
946 node
= of_parse_phandle(dev
->of_node
, "extcon", index
);
948 dev_err(dev
, "failed to get phandle in %s node\n",
949 dev
->of_node
->full_name
);
950 return ERR_PTR(-ENODEV
);
953 mutex_lock(&extcon_dev_list_lock
);
954 list_for_each_entry(edev
, &extcon_dev_list
, entry
) {
955 if (edev
->dev
.parent
&& edev
->dev
.parent
->of_node
== node
) {
956 mutex_unlock(&extcon_dev_list_lock
);
960 mutex_unlock(&extcon_dev_list_lock
);
962 return ERR_PTR(-EPROBE_DEFER
);
965 struct extcon_dev
*extcon_get_edev_by_phandle(struct device
*dev
, int index
)
967 return ERR_PTR(-ENOSYS
);
969 #endif /* CONFIG_OF */
970 EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle
);
973 * extcon_get_edev_name() - Get the name of the extcon device.
974 * @edev: the extcon device
976 const char *extcon_get_edev_name(struct extcon_dev
*edev
)
978 return !edev
? NULL
: edev
->name
;
981 static int __init
extcon_class_init(void)
983 return create_extcon_class();
985 module_init(extcon_class_init
);
987 static void __exit
extcon_class_exit(void)
989 #if defined(CONFIG_ANDROID)
990 class_compat_unregister(switch_class
);
992 class_destroy(extcon_class
);
994 module_exit(extcon_class_exit
);
996 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
997 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
998 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
999 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1000 MODULE_DESCRIPTION("External connector (extcon) class driver");
1001 MODULE_LICENSE("GPL");