2 * drivers/extcon/extcon_class.c
4 * External connector (extcon) class driver
6 * Copyright (C) 2012 Samsung Electronics
7 * Author: Donggeun Kim <dg77.kim@samsung.com>
8 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
10 * based on android/drivers/switch/switch_class.c
11 * Copyright (C) 2008 Google, Inc.
12 * Author: Mike Lockwood <lockwood@android.com>
14 * This software is licensed under the terms of the GNU General Public
15 * License version 2, as published by the Free Software Foundation, and
16 * may be copied, distributed, and modified under those terms.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
25 #include <linux/module.h>
26 #include <linux/types.h>
27 #include <linux/init.h>
28 #include <linux/device.h>
30 #include <linux/err.h>
31 #include <linux/extcon.h>
32 #include <linux/slab.h>
33 #include <linux/sysfs.h>
37 * extcon_cable_name suggests the standard cable names for commonly used
40 * However, please do not use extcon_cable_name directly for extcon_dev
41 * struct's supported_cable pointer unless your device really supports
42 * every single port-type of the following cable names. Please choose cable
43 * names that are actually used in your extcon device.
45 const char extcon_cable_name
[][CABLE_NAME_MAX
+ 1] = {
47 [EXTCON_USB_HOST
] = "USB-Host",
49 [EXTCON_FAST_CHARGER
] = "Fast-charger",
50 [EXTCON_SLOW_CHARGER
] = "Slow-charger",
51 [EXTCON_CHARGE_DOWNSTREAM
] = "Charge-downstream",
52 [EXTCON_HDMI
] = "HDMI",
56 [EXTCON_DOCK
] = "Dock",
57 [EXTCON_LINE_IN
] = "Line-in",
58 [EXTCON_LINE_OUT
] = "Line-out",
59 [EXTCON_MIC_IN
] = "Microphone",
60 [EXTCON_HEADPHONE_OUT
] = "Headphone",
61 [EXTCON_SPDIF_IN
] = "SPDIF-in",
62 [EXTCON_SPDIF_OUT
] = "SPDIF-out",
63 [EXTCON_VIDEO_IN
] = "Video-in",
64 [EXTCON_VIDEO_OUT
] = "Video-out",
65 [EXTCON_MECHANICAL
] = "Mechanical",
68 static struct class *extcon_class
;
69 #if defined(CONFIG_ANDROID)
70 static struct class_compat
*switch_class
;
71 #endif /* CONFIG_ANDROID */
73 static LIST_HEAD(extcon_dev_list
);
74 static DEFINE_MUTEX(extcon_dev_list_lock
);
77 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
79 * @edev: the extcon device
80 * @new_state: new cable attach status for @edev
82 * Returns 0 if nothing violates. Returns the index + 1 for the first
85 static int check_mutually_exclusive(struct extcon_dev
*edev
, u32 new_state
)
89 if (!edev
->mutually_exclusive
)
92 for (i
= 0; edev
->mutually_exclusive
[i
]; i
++) {
94 u32 correspondants
= new_state
& edev
->mutually_exclusive
[i
];
96 /* calculate the total number of bits set */
97 weight
= hweight32(correspondants
);
105 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
109 struct extcon_dev
*edev
= dev_get_drvdata(dev
);
111 if (edev
->print_state
) {
112 int ret
= edev
->print_state(edev
, buf
);
116 /* Use default if failed */
119 if (edev
->max_supported
== 0)
120 return sprintf(buf
, "%u\n", edev
->state
);
122 for (i
= 0; i
< SUPPORTED_CABLE_MAX
; i
++) {
123 if (!edev
->supported_cable
[i
])
125 count
+= sprintf(buf
+ count
, "%s=%d\n",
126 edev
->supported_cable
[i
],
127 !!(edev
->state
& (1 << i
)));
133 static ssize_t
state_store(struct device
*dev
, struct device_attribute
*attr
,
134 const char *buf
, size_t count
)
138 struct extcon_dev
*edev
= dev_get_drvdata(dev
);
140 ret
= sscanf(buf
, "0x%x", &state
);
144 ret
= extcon_set_state(edev
, state
);
151 static DEVICE_ATTR_RW(state
);
153 static ssize_t
name_show(struct device
*dev
, struct device_attribute
*attr
,
156 struct extcon_dev
*edev
= dev_get_drvdata(dev
);
158 /* Optional callback given by the user */
159 if (edev
->print_name
) {
160 int ret
= edev
->print_name(edev
, buf
);
165 return sprintf(buf
, "%s\n", dev_name(&edev
->dev
));
167 static DEVICE_ATTR_RO(name
);
169 static ssize_t
cable_name_show(struct device
*dev
,
170 struct device_attribute
*attr
, char *buf
)
172 struct extcon_cable
*cable
= container_of(attr
, struct extcon_cable
,
175 return sprintf(buf
, "%s\n",
176 cable
->edev
->supported_cable
[cable
->cable_index
]);
179 static ssize_t
cable_state_show(struct device
*dev
,
180 struct device_attribute
*attr
, char *buf
)
182 struct extcon_cable
*cable
= container_of(attr
, struct extcon_cable
,
185 return sprintf(buf
, "%d\n",
186 extcon_get_cable_state_(cable
->edev
,
187 cable
->cable_index
));
191 * extcon_update_state() - Update the cable attach states of the extcon device
192 * only for the masked bits.
193 * @edev: the extcon device
194 * @mask: the bit mask to designate updated bits.
195 * @state: new cable attach status for @edev
197 * Changing the state sends uevent with environment variable containing
198 * the name of extcon device (envp[0]) and the state output (envp[1]).
199 * Tizen uses this format for extcon device to get events from ports.
200 * Android uses this format as well.
202 * Note that the notifier provides which bits are changed in the state
203 * variable with the val parameter (second) to the callback.
205 int extcon_update_state(struct extcon_dev
*edev
, u32 mask
, u32 state
)
215 spin_lock_irqsave(&edev
->lock
, flags
);
217 if (edev
->state
!= ((edev
->state
& ~mask
) | (state
& mask
))) {
218 u32 old_state
= edev
->state
;
220 if (check_mutually_exclusive(edev
, (edev
->state
& ~mask
) |
222 spin_unlock_irqrestore(&edev
->lock
, flags
);
226 edev
->state
&= ~mask
;
227 edev
->state
|= state
& mask
;
229 raw_notifier_call_chain(&edev
->nh
, old_state
, edev
);
230 /* This could be in interrupt handler */
231 prop_buf
= (char *)get_zeroed_page(GFP_ATOMIC
);
233 length
= name_show(&edev
->dev
, NULL
, prop_buf
);
235 if (prop_buf
[length
- 1] == '\n')
236 prop_buf
[length
- 1] = 0;
237 snprintf(name_buf
, sizeof(name_buf
),
238 "NAME=%s", prop_buf
);
239 envp
[env_offset
++] = name_buf
;
241 length
= state_show(&edev
->dev
, NULL
, prop_buf
);
243 if (prop_buf
[length
- 1] == '\n')
244 prop_buf
[length
- 1] = 0;
245 snprintf(state_buf
, sizeof(state_buf
),
246 "STATE=%s", prop_buf
);
247 envp
[env_offset
++] = state_buf
;
249 envp
[env_offset
] = NULL
;
250 /* Unlock early before uevent */
251 spin_unlock_irqrestore(&edev
->lock
, flags
);
253 kobject_uevent_env(&edev
->dev
.kobj
, KOBJ_CHANGE
, envp
);
254 free_page((unsigned long)prop_buf
);
256 /* Unlock early before uevent */
257 spin_unlock_irqrestore(&edev
->lock
, flags
);
259 dev_err(&edev
->dev
, "out of memory in extcon_set_state\n");
260 kobject_uevent(&edev
->dev
.kobj
, KOBJ_CHANGE
);
264 spin_unlock_irqrestore(&edev
->lock
, flags
);
269 EXPORT_SYMBOL_GPL(extcon_update_state
);
272 * extcon_set_state() - Set the cable attach states of the extcon device.
273 * @edev: the extcon device
274 * @state: new cable attach status for @edev
276 * Note that notifier provides which bits are changed in the state
277 * variable with the val parameter (second) to the callback.
279 int extcon_set_state(struct extcon_dev
*edev
, u32 state
)
281 return extcon_update_state(edev
, 0xffffffff, state
);
283 EXPORT_SYMBOL_GPL(extcon_set_state
);
286 * extcon_find_cable_index() - Get the cable index based on the cable name.
287 * @edev: the extcon device that has the cable.
288 * @cable_name: cable name to be searched.
290 * Note that accessing a cable state based on cable_index is faster than
291 * cable_name because using cable_name induces a loop with strncmp().
292 * Thus, when get/set_cable_state is repeatedly used, using cable_index
295 int extcon_find_cable_index(struct extcon_dev
*edev
, const char *cable_name
)
299 if (edev
->supported_cable
) {
300 for (i
= 0; edev
->supported_cable
[i
]; i
++) {
301 if (!strncmp(edev
->supported_cable
[i
],
302 cable_name
, CABLE_NAME_MAX
))
309 EXPORT_SYMBOL_GPL(extcon_find_cable_index
);
312 * extcon_get_cable_state_() - Get the status of a specific cable.
313 * @edev: the extcon device that has the cable.
314 * @index: cable index that can be retrieved by extcon_find_cable_index().
316 int extcon_get_cable_state_(struct extcon_dev
*edev
, int index
)
318 if (index
< 0 || (edev
->max_supported
&& edev
->max_supported
<= index
))
321 return !!(edev
->state
& (1 << index
));
323 EXPORT_SYMBOL_GPL(extcon_get_cable_state_
);
326 * extcon_get_cable_state() - Get the status of a specific cable.
327 * @edev: the extcon device that has the cable.
328 * @cable_name: cable name.
330 * Note that this is slower than extcon_get_cable_state_.
332 int extcon_get_cable_state(struct extcon_dev
*edev
, const char *cable_name
)
334 return extcon_get_cable_state_(edev
, extcon_find_cable_index
337 EXPORT_SYMBOL_GPL(extcon_get_cable_state
);
340 * extcon_set_cable_state_() - Set the status of a specific cable.
341 * @edev: the extcon device that has the cable.
342 * @index: cable index that can be retrieved by
343 * extcon_find_cable_index().
344 * @cable_state: the new cable status. The default semantics is
345 * true: attached / false: detached.
347 int extcon_set_cable_state_(struct extcon_dev
*edev
,
348 int index
, bool cable_state
)
352 if (index
< 0 || (edev
->max_supported
&& edev
->max_supported
<= index
))
355 state
= cable_state
? (1 << index
) : 0;
356 return extcon_update_state(edev
, 1 << index
, state
);
358 EXPORT_SYMBOL_GPL(extcon_set_cable_state_
);
361 * extcon_set_cable_state() - Set the status of a specific cable.
362 * @edev: the extcon device that has the cable.
363 * @cable_name: cable name.
364 * @cable_state: the new cable status. The default semantics is
365 * true: attached / false: detached.
367 * Note that this is slower than extcon_set_cable_state_.
369 int extcon_set_cable_state(struct extcon_dev
*edev
,
370 const char *cable_name
, bool cable_state
)
372 return extcon_set_cable_state_(edev
, extcon_find_cable_index
373 (edev
, cable_name
), cable_state
);
375 EXPORT_SYMBOL_GPL(extcon_set_cable_state
);
378 * extcon_get_extcon_dev() - Get the extcon device instance from the name
379 * @extcon_name: The extcon name provided with extcon_dev_register()
381 struct extcon_dev
*extcon_get_extcon_dev(const char *extcon_name
)
383 struct extcon_dev
*sd
;
385 mutex_lock(&extcon_dev_list_lock
);
386 list_for_each_entry(sd
, &extcon_dev_list
, entry
) {
387 if (!strcmp(sd
->name
, extcon_name
))
392 mutex_unlock(&extcon_dev_list_lock
);
395 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev
);
397 static int _call_per_cable(struct notifier_block
*nb
, unsigned long val
,
400 struct extcon_specific_cable_nb
*obj
= container_of(nb
,
401 struct extcon_specific_cable_nb
, internal_nb
);
402 struct extcon_dev
*edev
= ptr
;
404 if ((val
& (1 << obj
->cable_index
)) !=
405 (edev
->state
& (1 << obj
->cable_index
))) {
406 bool cable_state
= true;
408 obj
->previous_value
= val
;
410 if (val
& (1 << obj
->cable_index
))
413 return obj
->user_nb
->notifier_call(obj
->user_nb
,
421 * extcon_register_interest() - Register a notifier for a state change of a
422 * specific cable, not an entier set of cables of a
424 * @obj: an empty extcon_specific_cable_nb object to be returned.
425 * @extcon_name: the name of extcon device.
426 * if NULL, extcon_register_interest will register
427 * every cable with the target cable_name given.
428 * @cable_name: the target cable name.
429 * @nb: the notifier block to get notified.
431 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
432 * the struct for you.
434 * extcon_register_interest is a helper function for those who want to get
435 * notification for a single specific cable's status change. If a user wants
436 * to get notification for any changes of all cables of a extcon device,
437 * he/she should use the general extcon_register_notifier().
439 * Note that the second parameter given to the callback of nb (val) is
440 * "old_state", not the current state. The current state can be retrieved
441 * by looking at the third pameter (edev pointer)'s state value.
443 int extcon_register_interest(struct extcon_specific_cable_nb
*obj
,
444 const char *extcon_name
, const char *cable_name
,
445 struct notifier_block
*nb
)
447 if (!obj
|| !cable_name
|| !nb
)
451 obj
->edev
= extcon_get_extcon_dev(extcon_name
);
455 obj
->cable_index
= extcon_find_cable_index(obj
->edev
,
457 if (obj
->cable_index
< 0)
458 return obj
->cable_index
;
462 obj
->internal_nb
.notifier_call
= _call_per_cable
;
464 return raw_notifier_chain_register(&obj
->edev
->nh
,
467 struct class_dev_iter iter
;
468 struct extcon_dev
*extd
;
473 class_dev_iter_init(&iter
, extcon_class
, NULL
, NULL
);
474 while ((dev
= class_dev_iter_next(&iter
))) {
475 extd
= dev_get_drvdata(dev
);
477 if (extcon_find_cable_index(extd
, cable_name
) < 0)
480 class_dev_iter_exit(&iter
);
481 return extcon_register_interest(obj
, extd
->name
,
488 EXPORT_SYMBOL_GPL(extcon_register_interest
);
491 * extcon_unregister_interest() - Unregister the notifier registered by
492 * extcon_register_interest().
493 * @obj: the extcon_specific_cable_nb object returned by
494 * extcon_register_interest().
496 int extcon_unregister_interest(struct extcon_specific_cable_nb
*obj
)
501 return raw_notifier_chain_unregister(&obj
->edev
->nh
, &obj
->internal_nb
);
503 EXPORT_SYMBOL_GPL(extcon_unregister_interest
);
506 * extcon_register_notifier() - Register a notifiee to get notified by
507 * any attach status changes from the extcon.
508 * @edev: the extcon device.
509 * @nb: a notifier block to be registered.
511 * Note that the second parameter given to the callback of nb (val) is
512 * "old_state", not the current state. The current state can be retrieved
513 * by looking at the third pameter (edev pointer)'s state value.
515 int extcon_register_notifier(struct extcon_dev
*edev
,
516 struct notifier_block
*nb
)
518 return raw_notifier_chain_register(&edev
->nh
, nb
);
520 EXPORT_SYMBOL_GPL(extcon_register_notifier
);
523 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
524 * @edev: the extcon device.
525 * @nb: a registered notifier block to be unregistered.
527 int extcon_unregister_notifier(struct extcon_dev
*edev
,
528 struct notifier_block
*nb
)
530 return raw_notifier_chain_unregister(&edev
->nh
, nb
);
532 EXPORT_SYMBOL_GPL(extcon_unregister_notifier
);
534 static struct attribute
*extcon_attrs
[] = {
535 &dev_attr_state
.attr
,
539 ATTRIBUTE_GROUPS(extcon
);
541 static int create_extcon_class(void)
544 extcon_class
= class_create(THIS_MODULE
, "extcon");
545 if (IS_ERR(extcon_class
))
546 return PTR_ERR(extcon_class
);
547 extcon_class
->dev_groups
= extcon_groups
;
549 #if defined(CONFIG_ANDROID)
550 switch_class
= class_compat_register("switch");
551 if (WARN(!switch_class
, "cannot allocate"))
553 #endif /* CONFIG_ANDROID */
559 static void extcon_dev_release(struct device
*dev
)
563 static const char *muex_name
= "mutually_exclusive";
564 static void dummy_sysfs_dev_release(struct device
*dev
)
569 * extcon_dev_allocate() - Allocate the memory of extcon device.
570 * @supported_cable: Array of supported cable names ending with NULL.
571 * If supported_cable is NULL, cable name related APIs
574 * This function allocates the memory for extcon device without allocating
575 * memory in each extcon provider driver and initialize default setting for
578 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
580 struct extcon_dev
*extcon_dev_allocate(const char **supported_cable
)
582 struct extcon_dev
*edev
;
584 edev
= kzalloc(sizeof(*edev
), GFP_KERNEL
);
586 return ERR_PTR(-ENOMEM
);
588 edev
->max_supported
= 0;
589 edev
->supported_cable
= supported_cable
;
595 * extcon_dev_free() - Free the memory of extcon device.
596 * @edev: the extcon device to free
598 void extcon_dev_free(struct extcon_dev
*edev
)
602 EXPORT_SYMBOL_GPL(extcon_dev_free
);
604 static int devm_extcon_dev_match(struct device
*dev
, void *res
, void *data
)
606 struct extcon_dev
**r
= res
;
608 if (WARN_ON(!r
|| !*r
))
614 static void devm_extcon_dev_release(struct device
*dev
, void *res
)
616 extcon_dev_free(*(struct extcon_dev
**)res
);
620 * devm_extcon_dev_allocate - Allocate managed extcon device
621 * @dev: device owning the extcon device being created
622 * @supported_cable: Array of supported cable names ending with NULL.
623 * If supported_cable is NULL, cable name related APIs
626 * This function manages automatically the memory of extcon device using device
627 * resource management and simplify the control of freeing the memory of extcon
630 * Returns the pointer memory of allocated extcon_dev if success
631 * or ERR_PTR(err) if fail
633 struct extcon_dev
*devm_extcon_dev_allocate(struct device
*dev
,
634 const char **supported_cable
)
636 struct extcon_dev
**ptr
, *edev
;
638 ptr
= devres_alloc(devm_extcon_dev_release
, sizeof(*ptr
), GFP_KERNEL
);
640 return ERR_PTR(-ENOMEM
);
642 edev
= extcon_dev_allocate(supported_cable
);
649 devres_add(dev
, ptr
);
653 EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate
);
655 void devm_extcon_dev_free(struct device
*dev
, struct extcon_dev
*edev
)
657 WARN_ON(devres_release(dev
, devm_extcon_dev_release
,
658 devm_extcon_dev_match
, edev
));
660 EXPORT_SYMBOL_GPL(devm_extcon_dev_free
);
663 * extcon_dev_register() - Register a new extcon device
664 * @edev : the new extcon device (should be allocated before calling)
666 * Among the members of edev struct, please set the "user initializing data"
667 * in any case and set the "optional callbacks" if required. However, please
668 * do not set the values of "internal data", which are initialized by
671 int extcon_dev_register(struct extcon_dev
*edev
)
676 ret
= create_extcon_class();
681 if (edev
->supported_cable
) {
682 /* Get size of array */
683 for (index
= 0; edev
->supported_cable
[index
]; index
++)
685 edev
->max_supported
= index
;
687 edev
->max_supported
= 0;
690 if (index
> SUPPORTED_CABLE_MAX
) {
691 dev_err(&edev
->dev
, "extcon: maximum number of supported cables exceeded.\n");
695 edev
->dev
.class = extcon_class
;
696 edev
->dev
.release
= extcon_dev_release
;
698 edev
->name
= edev
->name
? edev
->name
: dev_name(edev
->dev
.parent
);
699 if (IS_ERR_OR_NULL(edev
->name
)) {
701 "extcon device name is null\n");
704 dev_set_name(&edev
->dev
, "%s", edev
->name
);
706 if (edev
->max_supported
) {
709 struct extcon_cable
*cable
;
711 edev
->cables
= kzalloc(sizeof(struct extcon_cable
) *
712 edev
->max_supported
, GFP_KERNEL
);
715 goto err_sysfs_alloc
;
717 for (index
= 0; index
< edev
->max_supported
; index
++) {
718 cable
= &edev
->cables
[index
];
720 snprintf(buf
, 10, "cable.%d", index
);
721 str
= kzalloc(sizeof(char) * (strlen(buf
) + 1),
724 for (index
--; index
>= 0; index
--) {
725 cable
= &edev
->cables
[index
];
726 kfree(cable
->attr_g
.name
);
730 goto err_alloc_cables
;
735 cable
->cable_index
= index
;
736 cable
->attrs
[0] = &cable
->attr_name
.attr
;
737 cable
->attrs
[1] = &cable
->attr_state
.attr
;
738 cable
->attrs
[2] = NULL
;
739 cable
->attr_g
.name
= str
;
740 cable
->attr_g
.attrs
= cable
->attrs
;
742 sysfs_attr_init(&cable
->attr_name
.attr
);
743 cable
->attr_name
.attr
.name
= "name";
744 cable
->attr_name
.attr
.mode
= 0444;
745 cable
->attr_name
.show
= cable_name_show
;
747 sysfs_attr_init(&cable
->attr_state
.attr
);
748 cable
->attr_state
.attr
.name
= "state";
749 cable
->attr_state
.attr
.mode
= 0444;
750 cable
->attr_state
.show
= cable_state_show
;
754 if (edev
->max_supported
&& edev
->mutually_exclusive
) {
758 /* Count the size of mutually_exclusive array */
759 for (index
= 0; edev
->mutually_exclusive
[index
]; index
++)
762 edev
->attrs_muex
= kzalloc(sizeof(struct attribute
*) *
763 (index
+ 1), GFP_KERNEL
);
764 if (!edev
->attrs_muex
) {
769 edev
->d_attrs_muex
= kzalloc(sizeof(struct device_attribute
) *
771 if (!edev
->d_attrs_muex
) {
773 kfree(edev
->attrs_muex
);
777 for (index
= 0; edev
->mutually_exclusive
[index
]; index
++) {
778 sprintf(buf
, "0x%x", edev
->mutually_exclusive
[index
]);
779 name
= kzalloc(sizeof(char) * (strlen(buf
) + 1),
782 for (index
--; index
>= 0; index
--) {
783 kfree(edev
->d_attrs_muex
[index
].attr
.
786 kfree(edev
->d_attrs_muex
);
787 kfree(edev
->attrs_muex
);
792 sysfs_attr_init(&edev
->d_attrs_muex
[index
].attr
);
793 edev
->d_attrs_muex
[index
].attr
.name
= name
;
794 edev
->d_attrs_muex
[index
].attr
.mode
= 0000;
795 edev
->attrs_muex
[index
] = &edev
->d_attrs_muex
[index
]
798 edev
->attr_g_muex
.name
= muex_name
;
799 edev
->attr_g_muex
.attrs
= edev
->attrs_muex
;
803 if (edev
->max_supported
) {
804 edev
->extcon_dev_type
.groups
=
805 kzalloc(sizeof(struct attribute_group
*) *
806 (edev
->max_supported
+ 2), GFP_KERNEL
);
807 if (!edev
->extcon_dev_type
.groups
) {
809 goto err_alloc_groups
;
812 edev
->extcon_dev_type
.name
= dev_name(&edev
->dev
);
813 edev
->extcon_dev_type
.release
= dummy_sysfs_dev_release
;
815 for (index
= 0; index
< edev
->max_supported
; index
++)
816 edev
->extcon_dev_type
.groups
[index
] =
817 &edev
->cables
[index
].attr_g
;
818 if (edev
->mutually_exclusive
)
819 edev
->extcon_dev_type
.groups
[index
] =
822 edev
->dev
.type
= &edev
->extcon_dev_type
;
825 ret
= device_register(&edev
->dev
);
827 put_device(&edev
->dev
);
830 #if defined(CONFIG_ANDROID)
832 ret
= class_compat_create_link(switch_class
, &edev
->dev
, NULL
);
833 #endif /* CONFIG_ANDROID */
835 spin_lock_init(&edev
->lock
);
837 RAW_INIT_NOTIFIER_HEAD(&edev
->nh
);
839 dev_set_drvdata(&edev
->dev
, edev
);
842 mutex_lock(&extcon_dev_list_lock
);
843 list_add(&edev
->entry
, &extcon_dev_list
);
844 mutex_unlock(&extcon_dev_list_lock
);
849 if (edev
->max_supported
)
850 kfree(edev
->extcon_dev_type
.groups
);
852 if (edev
->max_supported
&& edev
->mutually_exclusive
) {
853 for (index
= 0; edev
->mutually_exclusive
[index
]; index
++)
854 kfree(edev
->d_attrs_muex
[index
].attr
.name
);
855 kfree(edev
->d_attrs_muex
);
856 kfree(edev
->attrs_muex
);
859 for (index
= 0; index
< edev
->max_supported
; index
++)
860 kfree(edev
->cables
[index
].attr_g
.name
);
862 if (edev
->max_supported
)
867 EXPORT_SYMBOL_GPL(extcon_dev_register
);
870 * extcon_dev_unregister() - Unregister the extcon device.
871 * @edev: the extcon device instance to be unregistered.
873 * Note that this does not call kfree(edev) because edev was not allocated
876 void extcon_dev_unregister(struct extcon_dev
*edev
)
880 mutex_lock(&extcon_dev_list_lock
);
881 list_del(&edev
->entry
);
882 mutex_unlock(&extcon_dev_list_lock
);
884 if (IS_ERR_OR_NULL(get_device(&edev
->dev
))) {
885 dev_err(&edev
->dev
, "Failed to unregister extcon_dev (%s)\n",
886 dev_name(&edev
->dev
));
890 device_unregister(&edev
->dev
);
892 if (edev
->mutually_exclusive
&& edev
->max_supported
) {
893 for (index
= 0; edev
->mutually_exclusive
[index
];
895 kfree(edev
->d_attrs_muex
[index
].attr
.name
);
896 kfree(edev
->d_attrs_muex
);
897 kfree(edev
->attrs_muex
);
900 for (index
= 0; index
< edev
->max_supported
; index
++)
901 kfree(edev
->cables
[index
].attr_g
.name
);
903 if (edev
->max_supported
) {
904 kfree(edev
->extcon_dev_type
.groups
);
908 #if defined(CONFIG_ANDROID)
910 class_compat_remove_link(switch_class
, &edev
->dev
, NULL
);
912 put_device(&edev
->dev
);
914 EXPORT_SYMBOL_GPL(extcon_dev_unregister
);
916 static void devm_extcon_dev_unreg(struct device
*dev
, void *res
)
918 extcon_dev_unregister(*(struct extcon_dev
**)res
);
922 * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
923 * @dev: device to allocate extcon device
924 * @edev: the new extcon device to register
926 * Managed extcon_dev_register() function. If extcon device is attached with
927 * this function, that extcon device is automatically unregistered on driver
928 * detach. Internally this function calls extcon_dev_register() function.
929 * To get more information, refer that function.
931 * If extcon device is registered with this function and the device needs to be
932 * unregistered separately, devm_extcon_dev_unregister() should be used.
934 * Returns 0 if success or negaive error number if failure.
936 int devm_extcon_dev_register(struct device
*dev
, struct extcon_dev
*edev
)
938 struct extcon_dev
**ptr
;
941 ptr
= devres_alloc(devm_extcon_dev_unreg
, sizeof(*ptr
), GFP_KERNEL
);
945 ret
= extcon_dev_register(edev
);
952 devres_add(dev
, ptr
);
956 EXPORT_SYMBOL_GPL(devm_extcon_dev_register
);
959 * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
960 * @dev: device the extcon belongs to
961 * @edev: the extcon device to unregister
963 * Unregister extcon device that is registered with devm_extcon_dev_register()
966 void devm_extcon_dev_unregister(struct device
*dev
, struct extcon_dev
*edev
)
968 WARN_ON(devres_release(dev
, devm_extcon_dev_unreg
,
969 devm_extcon_dev_match
, edev
));
971 EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister
);
975 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
976 * @dev - instance to the given device
977 * @index - index into list of extcon_dev
979 * return the instance of extcon device
981 struct extcon_dev
*extcon_get_edev_by_phandle(struct device
*dev
, int index
)
983 struct device_node
*node
;
984 struct extcon_dev
*edev
;
987 dev_err(dev
, "device does not have a device node entry\n");
988 return ERR_PTR(-EINVAL
);
991 node
= of_parse_phandle(dev
->of_node
, "extcon", index
);
993 dev_err(dev
, "failed to get phandle in %s node\n",
994 dev
->of_node
->full_name
);
995 return ERR_PTR(-ENODEV
);
998 edev
= extcon_get_extcon_dev(node
->name
);
1000 dev_err(dev
, "unable to get extcon device : %s\n", node
->name
);
1001 return ERR_PTR(-ENODEV
);
1007 struct extcon_dev
*extcon_get_edev_by_phandle(struct device
*dev
, int index
)
1009 return ERR_PTR(-ENOSYS
);
1011 #endif /* CONFIG_OF */
1012 EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle
);
1014 static int __init
extcon_class_init(void)
1016 return create_extcon_class();
1018 module_init(extcon_class_init
);
1020 static void __exit
extcon_class_exit(void)
1022 #if defined(CONFIG_ANDROID)
1023 class_compat_unregister(switch_class
);
1025 class_destroy(extcon_class
);
1027 module_exit(extcon_class_exit
);
1029 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1030 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1031 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1032 MODULE_DESCRIPTION("External connector (extcon) class driver");
1033 MODULE_LICENSE("GPL");