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 /* USB external connector */
44 [EXTCON_USB_HOST] = "USB-HOST",
46 /* Charger external connector */
48 [EXTCON_FAST_CHARGER] = "FAST-CHARGER",
49 [EXTCON_SLOW_CHARGER] = "SLOW-CHARGER",
50 [EXTCON_CHARGE_DOWNSTREAM] = "CHARGE-DOWNSTREAM",
52 /* Audio/Video external connector */
53 [EXTCON_LINE_IN] = "LINE-IN",
54 [EXTCON_LINE_OUT] = "LINE-OUT",
55 [EXTCON_MICROPHONE] = "MICROPHONE",
56 [EXTCON_HEADPHONE] = "HEADPHONE",
58 [EXTCON_HDMI] = "HDMI",
62 [EXTCON_SPDIF_IN] = "SPDIF-IN",
63 [EXTCON_SPDIF_OUT] = "SPDIF-OUT",
64 [EXTCON_VIDEO_IN] = "VIDEO-IN",
65 [EXTCON_VIDEO_OUT] = "VIDEO-OUT",
67 /* Etc external connector */
68 [EXTCON_DOCK] = "DOCK",
70 [EXTCON_MECHANICAL] = "MECHANICAL",
75 static struct class *extcon_class;
76 #if defined(CONFIG_ANDROID)
77 static struct class_compat *switch_class;
78 #endif /* CONFIG_ANDROID */
80 static LIST_HEAD(extcon_dev_list);
81 static DEFINE_MUTEX(extcon_dev_list_lock);
84 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
86 * @edev: the extcon device
87 * @new_state: new cable attach status for @edev
89 * Returns 0 if nothing violates. Returns the index + 1 for the first
92 static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
96 if (!edev->mutually_exclusive)
99 for (i = 0; edev->mutually_exclusive[i]; i++) {
101 u32 correspondants = new_state & edev->mutually_exclusive[i];
103 /* calculate the total number of bits set */
104 weight = hweight32(correspondants);
112 static int find_cable_index_by_id(struct extcon_dev *edev, const enum extcon id)
116 /* Find the the index of extcon cable in edev->supported_cable */
117 for (i = 0; i < edev->max_supported; i++) {
118 if (edev->supported_cable[i] == id)
125 static int find_cable_index_by_name(struct extcon_dev *edev, const char *name)
127 enum extcon id = EXTCON_NONE;
130 if (edev->max_supported == 0)
133 /* Find the the number of extcon cable */
134 for (i = 0; i < EXTCON_END; i++) {
137 if (!strncmp(extcon_name[i], name, CABLE_NAME_MAX)) {
143 if (id == EXTCON_NONE)
146 return find_cable_index_by_id(edev, id);
149 static bool is_extcon_changed(u32 prev, u32 new, int idx, bool *attached)
151 if (((prev >> idx) & 0x1) != ((new >> idx) & 0x1)) {
152 *attached = new ? true : false;
159 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
163 struct extcon_dev *edev = dev_get_drvdata(dev);
165 if (edev->print_state) {
166 int ret = edev->print_state(edev, buf);
170 /* Use default if failed */
173 if (edev->max_supported == 0)
174 return sprintf(buf, "%u\n", edev->state);
176 for (i = 0; i < edev->max_supported; i++) {
177 count += sprintf(buf + count, "%s=%d\n",
178 extcon_name[edev->supported_cable[i]],
179 !!(edev->state & (1 << i)));
185 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
186 const char *buf, size_t count)
190 struct extcon_dev *edev = dev_get_drvdata(dev);
192 ret = sscanf(buf, "0x%x", &state);
196 ret = extcon_set_state(edev, state);
203 static DEVICE_ATTR_RW(state);
205 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
208 struct extcon_dev *edev = dev_get_drvdata(dev);
210 /* Optional callback given by the user */
211 if (edev->print_name) {
212 int ret = edev->print_name(edev, buf);
218 return sprintf(buf, "%s\n", edev->name);
220 static DEVICE_ATTR_RO(name);
222 static ssize_t cable_name_show(struct device *dev,
223 struct device_attribute *attr, char *buf)
225 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
227 int i = cable->cable_index;
229 return sprintf(buf, "%s\n",
230 extcon_name[cable->edev->supported_cable[i]]);
233 static ssize_t cable_state_show(struct device *dev,
234 struct device_attribute *attr, char *buf)
236 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
239 return sprintf(buf, "%d\n",
240 extcon_get_cable_state_(cable->edev,
241 cable->cable_index));
245 * extcon_update_state() - Update the cable attach states of the extcon device
246 * only for the masked bits.
247 * @edev: the extcon device
248 * @mask: the bit mask to designate updated bits.
249 * @state: new cable attach status for @edev
251 * Changing the state sends uevent with environment variable containing
252 * the name of extcon device (envp[0]) and the state output (envp[1]).
253 * Tizen uses this format for extcon device to get events from ports.
254 * Android uses this format as well.
256 * Note that the notifier provides which bits are changed in the state
257 * variable with the val parameter (second) to the callback.
259 int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
271 spin_lock_irqsave(&edev->lock, flags);
273 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
274 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
276 spin_unlock_irqrestore(&edev->lock, flags);
280 for (index = 0; index < edev->max_supported; index++) {
281 if (is_extcon_changed(edev->state, state, index, &attached))
282 raw_notifier_call_chain(&edev->nh[index], attached, edev);
285 edev->state &= ~mask;
286 edev->state |= state & mask;
288 /* This could be in interrupt handler */
289 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
291 length = name_show(&edev->dev, NULL, prop_buf);
293 if (prop_buf[length - 1] == '\n')
294 prop_buf[length - 1] = 0;
295 snprintf(name_buf, sizeof(name_buf),
296 "NAME=%s", prop_buf);
297 envp[env_offset++] = name_buf;
299 length = state_show(&edev->dev, NULL, prop_buf);
301 if (prop_buf[length - 1] == '\n')
302 prop_buf[length - 1] = 0;
303 snprintf(state_buf, sizeof(state_buf),
304 "STATE=%s", prop_buf);
305 envp[env_offset++] = state_buf;
307 envp[env_offset] = NULL;
308 /* Unlock early before uevent */
309 spin_unlock_irqrestore(&edev->lock, flags);
311 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
312 free_page((unsigned long)prop_buf);
314 /* Unlock early before uevent */
315 spin_unlock_irqrestore(&edev->lock, flags);
317 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
318 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
322 spin_unlock_irqrestore(&edev->lock, flags);
327 EXPORT_SYMBOL_GPL(extcon_update_state);
330 * extcon_set_state() - Set the cable attach states of the extcon device.
331 * @edev: the extcon device
332 * @state: new cable attach status for @edev
334 * Note that notifier provides which bits are changed in the state
335 * variable with the val parameter (second) to the callback.
337 int extcon_set_state(struct extcon_dev *edev, u32 state)
339 return extcon_update_state(edev, 0xffffffff, state);
341 EXPORT_SYMBOL_GPL(extcon_set_state);
344 * extcon_get_cable_state_() - Get the status of a specific cable.
345 * @edev: the extcon device that has the cable.
346 * @id: the unique id of each external connector in extcon enumeration.
348 int extcon_get_cable_state_(struct extcon_dev *edev, const enum extcon id)
352 index = find_cable_index_by_id(edev, id);
356 if (edev->max_supported && edev->max_supported <= index)
359 return !!(edev->state & (1 << index));
361 EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
364 * extcon_get_cable_state() - Get the status of a specific cable.
365 * @edev: the extcon device that has the cable.
366 * @cable_name: cable name.
368 * Note that this is slower than extcon_get_cable_state_.
370 int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
372 return extcon_get_cable_state_(edev, find_cable_index_by_name
375 EXPORT_SYMBOL_GPL(extcon_get_cable_state);
378 * extcon_set_cable_state_() - Set the status of a specific cable.
379 * @edev: the extcon device that has the cable.
380 * @id: the unique id of each external connector
381 * in extcon enumeration.
382 * @state: the new cable status. The default semantics is
383 * true: attached / false: detached.
385 int extcon_set_cable_state_(struct extcon_dev *edev, enum extcon id,
391 index = find_cable_index_by_id(edev, id);
395 if (edev->max_supported && edev->max_supported <= index)
398 state = cable_state ? (1 << index) : 0;
399 return extcon_update_state(edev, 1 << index, state);
401 EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
404 * extcon_set_cable_state() - Set the status of a specific cable.
405 * @edev: the extcon device that has the cable.
406 * @cable_name: cable name.
407 * @cable_state: the new cable status. The default semantics is
408 * true: attached / false: detached.
410 * Note that this is slower than extcon_set_cable_state_.
412 int extcon_set_cable_state(struct extcon_dev *edev,
413 const char *cable_name, bool cable_state)
415 return extcon_set_cable_state_(edev, find_cable_index_by_name
416 (edev, cable_name), cable_state);
418 EXPORT_SYMBOL_GPL(extcon_set_cable_state);
421 * extcon_get_extcon_dev() - Get the extcon device instance from the name
422 * @extcon_name: The extcon name provided with extcon_dev_register()
424 struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
426 struct extcon_dev *sd;
428 mutex_lock(&extcon_dev_list_lock);
429 list_for_each_entry(sd, &extcon_dev_list, entry) {
430 if (!strcmp(sd->name, extcon_name))
435 mutex_unlock(&extcon_dev_list_lock);
438 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
441 * extcon_register_interest() - Register a notifier for a state change of a
442 * specific cable, not an entier set of cables of a
444 * @obj: an empty extcon_specific_cable_nb object to be returned.
445 * @extcon_name: the name of extcon device.
446 * if NULL, extcon_register_interest will register
447 * every cable with the target cable_name given.
448 * @cable_name: the target cable name.
449 * @nb: the notifier block to get notified.
451 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
452 * the struct for you.
454 * extcon_register_interest is a helper function for those who want to get
455 * notification for a single specific cable's status change. If a user wants
456 * to get notification for any changes of all cables of a extcon device,
457 * he/she should use the general extcon_register_notifier().
459 * Note that the second parameter given to the callback of nb (val) is
460 * "old_state", not the current state. The current state can be retrieved
461 * by looking at the third pameter (edev pointer)'s state value.
463 int extcon_register_interest(struct extcon_specific_cable_nb *obj,
464 const char *extcon_name, const char *cable_name,
465 struct notifier_block *nb)
470 if (!obj || !cable_name || !nb)
474 obj->edev = extcon_get_extcon_dev(extcon_name);
478 obj->cable_index = find_cable_index_by_name(obj->edev,
480 if (obj->cable_index < 0)
481 return obj->cable_index;
485 spin_lock_irqsave(&obj->edev->lock, flags);
486 ret = raw_notifier_chain_register(
487 &obj->edev->nh[obj->cable_index],
489 spin_unlock_irqrestore(&obj->edev->lock, flags);
491 struct class_dev_iter iter;
492 struct extcon_dev *extd;
497 class_dev_iter_init(&iter, extcon_class, NULL, NULL);
498 while ((dev = class_dev_iter_next(&iter))) {
499 extd = dev_get_drvdata(dev);
501 if (find_cable_index_by_name(extd, cable_name) < 0)
504 class_dev_iter_exit(&iter);
505 return extcon_register_interest(obj, extd->name,
514 EXPORT_SYMBOL_GPL(extcon_register_interest);
517 * extcon_unregister_interest() - Unregister the notifier registered by
518 * extcon_register_interest().
519 * @obj: the extcon_specific_cable_nb object returned by
520 * extcon_register_interest().
522 int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
530 spin_lock_irqsave(&obj->edev->lock, flags);
531 ret = raw_notifier_chain_unregister(
532 &obj->edev->nh[obj->cable_index], obj->user_nb);
533 spin_unlock_irqrestore(&obj->edev->lock, flags);
537 EXPORT_SYMBOL_GPL(extcon_unregister_interest);
540 * extcon_register_notifier() - Register a notifiee to get notified by
541 * any attach status changes from the extcon.
542 * @edev: the extcon device that has the external connecotr.
543 * @id: the unique id of each external connector in extcon enumeration.
544 * @nb: a notifier block to be registered.
546 * Note that the second parameter given to the callback of nb (val) is
547 * "old_state", not the current state. The current state can be retrieved
548 * by looking at the third pameter (edev pointer)'s state value.
550 int extcon_register_notifier(struct extcon_dev *edev, enum extcon id,
551 struct notifier_block *nb)
556 idx = find_cable_index_by_id(edev, id);
558 spin_lock_irqsave(&edev->lock, flags);
559 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
560 spin_unlock_irqrestore(&edev->lock, flags);
564 EXPORT_SYMBOL_GPL(extcon_register_notifier);
567 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
568 * @edev: the extcon device that has the external connecotr.
569 * @id: the unique id of each external connector in extcon enumeration.
570 * @nb: a notifier block to be registered.
572 int extcon_unregister_notifier(struct extcon_dev *edev, enum extcon id,
573 struct notifier_block *nb)
578 idx = find_cable_index_by_id(edev, id);
580 spin_lock_irqsave(&edev->lock, flags);
581 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
582 spin_unlock_irqrestore(&edev->lock, flags);
586 EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
588 static struct attribute *extcon_attrs[] = {
589 &dev_attr_state.attr,
593 ATTRIBUTE_GROUPS(extcon);
595 static int create_extcon_class(void)
598 extcon_class = class_create(THIS_MODULE, "extcon");
599 if (IS_ERR(extcon_class))
600 return PTR_ERR(extcon_class);
601 extcon_class->dev_groups = extcon_groups;
603 #if defined(CONFIG_ANDROID)
604 switch_class = class_compat_register("switch");
605 if (WARN(!switch_class, "cannot allocate"))
607 #endif /* CONFIG_ANDROID */
613 static void extcon_dev_release(struct device *dev)
617 static const char *muex_name = "mutually_exclusive";
618 static void dummy_sysfs_dev_release(struct device *dev)
623 * extcon_dev_allocate() - Allocate the memory of extcon device.
624 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
625 * If supported_cable is NULL, cable name related APIs
628 * This function allocates the memory for extcon device without allocating
629 * memory in each extcon provider driver and initialize default setting for
632 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
634 struct extcon_dev *extcon_dev_allocate(const enum extcon *supported_cable)
636 struct extcon_dev *edev;
638 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
640 return ERR_PTR(-ENOMEM);
642 edev->max_supported = 0;
643 edev->supported_cable = supported_cable;
649 * extcon_dev_free() - Free the memory of extcon device.
650 * @edev: the extcon device to free
652 void extcon_dev_free(struct extcon_dev *edev)
656 EXPORT_SYMBOL_GPL(extcon_dev_free);
658 static int devm_extcon_dev_match(struct device *dev, void *res, void *data)
660 struct extcon_dev **r = res;
662 if (WARN_ON(!r || !*r))
668 static void devm_extcon_dev_release(struct device *dev, void *res)
670 extcon_dev_free(*(struct extcon_dev **)res);
674 * devm_extcon_dev_allocate - Allocate managed extcon device
675 * @dev: device owning the extcon device being created
676 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
677 * If supported_cable is NULL, cable name related APIs
680 * This function manages automatically the memory of extcon device using device
681 * resource management and simplify the control of freeing the memory of extcon
684 * Returns the pointer memory of allocated extcon_dev if success
685 * or ERR_PTR(err) if fail
687 struct extcon_dev *devm_extcon_dev_allocate(struct device *dev,
688 const enum extcon *supported_cable)
690 struct extcon_dev **ptr, *edev;
692 ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL);
694 return ERR_PTR(-ENOMEM);
696 edev = extcon_dev_allocate(supported_cable);
702 edev->dev.parent = dev;
705 devres_add(dev, ptr);
709 EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate);
711 void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev)
713 WARN_ON(devres_release(dev, devm_extcon_dev_release,
714 devm_extcon_dev_match, edev));
716 EXPORT_SYMBOL_GPL(devm_extcon_dev_free);
719 * extcon_dev_register() - Register a new extcon device
720 * @edev : the new extcon device (should be allocated before calling)
722 * Among the members of edev struct, please set the "user initializing data"
723 * in any case and set the "optional callbacks" if required. However, please
724 * do not set the values of "internal data", which are initialized by
727 int extcon_dev_register(struct extcon_dev *edev)
730 static atomic_t edev_no = ATOMIC_INIT(-1);
733 ret = create_extcon_class();
738 if (!edev->supported_cable)
741 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
743 edev->max_supported = index;
744 if (index > SUPPORTED_CABLE_MAX) {
746 "exceed the maximum number of supported cables\n");
750 edev->dev.class = extcon_class;
751 edev->dev.release = extcon_dev_release;
753 edev->name = dev_name(edev->dev.parent);
754 if (IS_ERR_OR_NULL(edev->name)) {
756 "extcon device name is null\n");
759 dev_set_name(&edev->dev, "extcon%lu",
760 (unsigned long)atomic_inc_return(&edev_no));
762 if (edev->max_supported) {
765 struct extcon_cable *cable;
767 edev->cables = kzalloc(sizeof(struct extcon_cable) *
768 edev->max_supported, GFP_KERNEL);
771 goto err_sysfs_alloc;
773 for (index = 0; index < edev->max_supported; index++) {
774 cable = &edev->cables[index];
776 snprintf(buf, 10, "cable.%d", index);
777 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
780 for (index--; index >= 0; index--) {
781 cable = &edev->cables[index];
782 kfree(cable->attr_g.name);
786 goto err_alloc_cables;
791 cable->cable_index = index;
792 cable->attrs[0] = &cable->attr_name.attr;
793 cable->attrs[1] = &cable->attr_state.attr;
794 cable->attrs[2] = NULL;
795 cable->attr_g.name = str;
796 cable->attr_g.attrs = cable->attrs;
798 sysfs_attr_init(&cable->attr_name.attr);
799 cable->attr_name.attr.name = "name";
800 cable->attr_name.attr.mode = 0444;
801 cable->attr_name.show = cable_name_show;
803 sysfs_attr_init(&cable->attr_state.attr);
804 cable->attr_state.attr.name = "state";
805 cable->attr_state.attr.mode = 0444;
806 cable->attr_state.show = cable_state_show;
810 if (edev->max_supported && edev->mutually_exclusive) {
814 /* Count the size of mutually_exclusive array */
815 for (index = 0; edev->mutually_exclusive[index]; index++)
818 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
819 (index + 1), GFP_KERNEL);
820 if (!edev->attrs_muex) {
825 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
827 if (!edev->d_attrs_muex) {
829 kfree(edev->attrs_muex);
833 for (index = 0; edev->mutually_exclusive[index]; index++) {
834 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
835 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
838 for (index--; index >= 0; index--) {
839 kfree(edev->d_attrs_muex[index].attr.
842 kfree(edev->d_attrs_muex);
843 kfree(edev->attrs_muex);
848 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
849 edev->d_attrs_muex[index].attr.name = name;
850 edev->d_attrs_muex[index].attr.mode = 0000;
851 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
854 edev->attr_g_muex.name = muex_name;
855 edev->attr_g_muex.attrs = edev->attrs_muex;
859 if (edev->max_supported) {
860 edev->extcon_dev_type.groups =
861 kzalloc(sizeof(struct attribute_group *) *
862 (edev->max_supported + 2), GFP_KERNEL);
863 if (!edev->extcon_dev_type.groups) {
865 goto err_alloc_groups;
868 edev->extcon_dev_type.name = dev_name(&edev->dev);
869 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
871 for (index = 0; index < edev->max_supported; index++)
872 edev->extcon_dev_type.groups[index] =
873 &edev->cables[index].attr_g;
874 if (edev->mutually_exclusive)
875 edev->extcon_dev_type.groups[index] =
878 edev->dev.type = &edev->extcon_dev_type;
881 ret = device_register(&edev->dev);
883 put_device(&edev->dev);
886 #if defined(CONFIG_ANDROID)
888 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
889 #endif /* CONFIG_ANDROID */
891 spin_lock_init(&edev->lock);
893 edev->nh = devm_kzalloc(&edev->dev,
894 sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
900 for (index = 0; index < edev->max_supported; index++)
901 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
903 dev_set_drvdata(&edev->dev, edev);
906 mutex_lock(&extcon_dev_list_lock);
907 list_add(&edev->entry, &extcon_dev_list);
908 mutex_unlock(&extcon_dev_list_lock);
913 if (edev->max_supported)
914 kfree(edev->extcon_dev_type.groups);
916 if (edev->max_supported && edev->mutually_exclusive) {
917 for (index = 0; edev->mutually_exclusive[index]; index++)
918 kfree(edev->d_attrs_muex[index].attr.name);
919 kfree(edev->d_attrs_muex);
920 kfree(edev->attrs_muex);
923 for (index = 0; index < edev->max_supported; index++)
924 kfree(edev->cables[index].attr_g.name);
926 if (edev->max_supported)
931 EXPORT_SYMBOL_GPL(extcon_dev_register);
934 * extcon_dev_unregister() - Unregister the extcon device.
935 * @edev: the extcon device instance to be unregistered.
937 * Note that this does not call kfree(edev) because edev was not allocated
940 void extcon_dev_unregister(struct extcon_dev *edev)
944 mutex_lock(&extcon_dev_list_lock);
945 list_del(&edev->entry);
946 mutex_unlock(&extcon_dev_list_lock);
948 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
949 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
950 dev_name(&edev->dev));
954 device_unregister(&edev->dev);
956 if (edev->mutually_exclusive && edev->max_supported) {
957 for (index = 0; edev->mutually_exclusive[index];
959 kfree(edev->d_attrs_muex[index].attr.name);
960 kfree(edev->d_attrs_muex);
961 kfree(edev->attrs_muex);
964 for (index = 0; index < edev->max_supported; index++)
965 kfree(edev->cables[index].attr_g.name);
967 if (edev->max_supported) {
968 kfree(edev->extcon_dev_type.groups);
972 #if defined(CONFIG_ANDROID)
974 class_compat_remove_link(switch_class, &edev->dev, NULL);
976 put_device(&edev->dev);
978 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
980 static void devm_extcon_dev_unreg(struct device *dev, void *res)
982 extcon_dev_unregister(*(struct extcon_dev **)res);
986 * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
987 * @dev: device to allocate extcon device
988 * @edev: the new extcon device to register
990 * Managed extcon_dev_register() function. If extcon device is attached with
991 * this function, that extcon device is automatically unregistered on driver
992 * detach. Internally this function calls extcon_dev_register() function.
993 * To get more information, refer that function.
995 * If extcon device is registered with this function and the device needs to be
996 * unregistered separately, devm_extcon_dev_unregister() should be used.
998 * Returns 0 if success or negaive error number if failure.
1000 int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev)
1002 struct extcon_dev **ptr;
1005 ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL);
1009 ret = extcon_dev_register(edev);
1016 devres_add(dev, ptr);
1020 EXPORT_SYMBOL_GPL(devm_extcon_dev_register);
1023 * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
1024 * @dev: device the extcon belongs to
1025 * @edev: the extcon device to unregister
1027 * Unregister extcon device that is registered with devm_extcon_dev_register()
1030 void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev)
1032 WARN_ON(devres_release(dev, devm_extcon_dev_unreg,
1033 devm_extcon_dev_match, edev));
1035 EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister);
1039 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1040 * @dev - instance to the given device
1041 * @index - index into list of extcon_dev
1043 * return the instance of extcon device
1045 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1047 struct device_node *node;
1048 struct extcon_dev *edev;
1050 if (!dev->of_node) {
1051 dev_err(dev, "device does not have a device node entry\n");
1052 return ERR_PTR(-EINVAL);
1055 node = of_parse_phandle(dev->of_node, "extcon", index);
1057 dev_err(dev, "failed to get phandle in %s node\n",
1058 dev->of_node->full_name);
1059 return ERR_PTR(-ENODEV);
1062 mutex_lock(&extcon_dev_list_lock);
1063 list_for_each_entry(edev, &extcon_dev_list, entry) {
1064 if (edev->dev.parent && edev->dev.parent->of_node == node) {
1065 mutex_unlock(&extcon_dev_list_lock);
1069 mutex_unlock(&extcon_dev_list_lock);
1071 return ERR_PTR(-EPROBE_DEFER);
1074 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1076 return ERR_PTR(-ENOSYS);
1078 #endif /* CONFIG_OF */
1079 EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1082 * extcon_get_edev_name() - Get the name of the extcon device.
1083 * @edev: the extcon device
1085 const char *extcon_get_edev_name(struct extcon_dev *edev)
1087 return !edev ? NULL : edev->name;
1090 static int __init extcon_class_init(void)
1092 return create_extcon_class();
1094 module_init(extcon_class_init);
1096 static void __exit extcon_class_exit(void)
1098 #if defined(CONFIG_ANDROID)
1099 class_compat_unregister(switch_class);
1101 class_destroy(extcon_class);
1103 module_exit(extcon_class_exit);
1105 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1106 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1107 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1108 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1109 MODULE_DESCRIPTION("External connector (extcon) class driver");
1110 MODULE_LICENSE("GPL");