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 struct __extcon_info {
48 .type = EXTCON_TYPE_MISC,
53 /* USB external connector */
55 .type = EXTCON_TYPE_USB,
60 .type = EXTCON_TYPE_USB,
61 .id = EXTCON_USB_HOST,
65 /* Charging external connector */
66 [EXTCON_CHG_USB_SDP] = {
67 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
68 .id = EXTCON_CHG_USB_SDP,
71 [EXTCON_CHG_USB_DCP] = {
72 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
73 .id = EXTCON_CHG_USB_DCP,
76 [EXTCON_CHG_USB_CDP] = {
77 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
78 .id = EXTCON_CHG_USB_CDP,
81 [EXTCON_CHG_USB_ACA] = {
82 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
83 .id = EXTCON_CHG_USB_ACA,
86 [EXTCON_CHG_USB_FAST] = {
87 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
88 .id = EXTCON_CHG_USB_FAST,
89 .name = "FAST-CHARGER",
91 [EXTCON_CHG_USB_SLOW] = {
92 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
93 .id = EXTCON_CHG_USB_SLOW,
94 .name = "SLOW-CHARGER",
97 /* Jack external connector */
98 [EXTCON_JACK_MICROPHONE] = {
99 .type = EXTCON_TYPE_JACK,
100 .id = EXTCON_JACK_MICROPHONE,
101 .name = "MICROPHONE",
103 [EXTCON_JACK_HEADPHONE] = {
104 .type = EXTCON_TYPE_JACK,
105 .id = EXTCON_JACK_HEADPHONE,
108 [EXTCON_JACK_LINE_IN] = {
109 .type = EXTCON_TYPE_JACK,
110 .id = EXTCON_JACK_LINE_IN,
113 [EXTCON_JACK_LINE_OUT] = {
114 .type = EXTCON_TYPE_JACK,
115 .id = EXTCON_JACK_LINE_OUT,
118 [EXTCON_JACK_VIDEO_IN] = {
119 .type = EXTCON_TYPE_JACK,
120 .id = EXTCON_JACK_VIDEO_IN,
123 [EXTCON_JACK_VIDEO_OUT] = {
124 .type = EXTCON_TYPE_JACK,
125 .id = EXTCON_JACK_VIDEO_OUT,
128 [EXTCON_JACK_SPDIF_IN] = {
129 .type = EXTCON_TYPE_JACK,
130 .id = EXTCON_JACK_SPDIF_IN,
133 [EXTCON_JACK_SPDIF_OUT] = {
134 .type = EXTCON_TYPE_JACK,
135 .id = EXTCON_JACK_SPDIF_OUT,
139 /* Display external connector */
140 [EXTCON_DISP_HDMI] = {
141 .type = EXTCON_TYPE_DISP,
142 .id = EXTCON_DISP_HDMI,
145 [EXTCON_DISP_MHL] = {
146 .type = EXTCON_TYPE_DISP,
147 .id = EXTCON_DISP_MHL,
150 [EXTCON_DISP_DVI] = {
151 .type = EXTCON_TYPE_DISP,
152 .id = EXTCON_DISP_DVI,
155 [EXTCON_DISP_VGA] = {
156 .type = EXTCON_TYPE_DISP,
157 .id = EXTCON_DISP_VGA,
161 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
162 .id = EXTCON_DISP_DP,
166 /* Miscellaneous external connector */
168 .type = EXTCON_TYPE_MISC,
173 .type = EXTCON_TYPE_MISC,
177 [EXTCON_MECHANICAL] = {
178 .type = EXTCON_TYPE_MISC,
179 .id = EXTCON_MECHANICAL,
180 .name = "MECHANICAL",
187 * struct extcon_cable - An internal data for each cable of extcon device.
188 * @edev: The extcon device
189 * @cable_index: Index of this cable in the edev
190 * @attr_g: Attribute group for the cable
191 * @attr_name: "name" sysfs entry
192 * @attr_state: "state" sysfs entry
193 * @attrs: Array pointing to attr_name and attr_state for attr_g
195 struct extcon_cable {
196 struct extcon_dev *edev;
199 struct attribute_group attr_g;
200 struct device_attribute attr_name;
201 struct device_attribute attr_state;
203 struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
205 union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
206 union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
207 union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
208 union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
210 unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
211 unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
212 unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
213 unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
216 static struct class *extcon_class;
217 #if defined(CONFIG_ANDROID)
218 static struct class_compat *switch_class;
219 #endif /* CONFIG_ANDROID */
221 static LIST_HEAD(extcon_dev_list);
222 static DEFINE_MUTEX(extcon_dev_list_lock);
225 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
227 * @edev: the extcon device
228 * @new_state: new cable attach status for @edev
230 * Returns 0 if nothing violates. Returns the index + 1 for the first
231 * violated condition.
233 static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
237 if (!edev->mutually_exclusive)
240 for (i = 0; edev->mutually_exclusive[i]; i++) {
242 u32 correspondants = new_state & edev->mutually_exclusive[i];
244 /* calculate the total number of bits set */
245 weight = hweight32(correspondants);
253 static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
257 /* Find the the index of extcon cable in edev->supported_cable */
258 for (i = 0; i < edev->max_supported; i++) {
259 if (edev->supported_cable[i] == id)
266 static int get_extcon_type(unsigned int prop)
269 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
270 return EXTCON_TYPE_USB;
271 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
272 return EXTCON_TYPE_CHG;
273 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
274 return EXTCON_TYPE_JACK;
275 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
276 return EXTCON_TYPE_DISP;
282 static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
284 return !!(edev->state & BIT(index));
287 static bool is_extcon_changed(struct extcon_dev *edev, int index,
290 int state = !!(edev->state & BIT(index));
291 return (state != new_state);
294 static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
298 /* Check whether the property is supported or not. */
299 type = get_extcon_type(prop);
303 /* Check whether a specific extcon id supports the property or not. */
304 return !!(extcon_info[id].type & type);
307 static int is_extcon_property_capability(struct extcon_dev *edev,
308 unsigned int id, int index,unsigned int prop)
310 struct extcon_cable *cable;
313 /* Check whether the property is supported or not. */
314 type = get_extcon_type(prop);
318 cable = &edev->cables[index];
321 case EXTCON_TYPE_USB:
322 ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
324 case EXTCON_TYPE_CHG:
325 ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
327 case EXTCON_TYPE_JACK:
328 ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
330 case EXTCON_TYPE_DISP:
331 ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
340 static void init_property(struct extcon_dev *edev, unsigned int id, int index)
342 unsigned int type = extcon_info[id].type;
343 struct extcon_cable *cable = &edev->cables[index];
345 if (EXTCON_TYPE_USB & type)
346 memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
347 if (EXTCON_TYPE_CHG & type)
348 memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
349 if (EXTCON_TYPE_JACK & type)
350 memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
351 if (EXTCON_TYPE_DISP & type)
352 memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
355 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
359 struct extcon_dev *edev = dev_get_drvdata(dev);
361 if (edev->max_supported == 0)
362 return sprintf(buf, "%u\n", edev->state);
364 for (i = 0; i < edev->max_supported; i++) {
365 count += sprintf(buf + count, "%s=%d\n",
366 extcon_info[edev->supported_cable[i]].name,
367 !!(edev->state & (1 << i)));
372 static DEVICE_ATTR_RO(state);
374 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
377 struct extcon_dev *edev = dev_get_drvdata(dev);
379 return sprintf(buf, "%s\n", edev->name);
381 static DEVICE_ATTR_RO(name);
383 static ssize_t cable_name_show(struct device *dev,
384 struct device_attribute *attr, char *buf)
386 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
388 int i = cable->cable_index;
390 return sprintf(buf, "%s\n",
391 extcon_info[cable->edev->supported_cable[i]].name);
394 static ssize_t cable_state_show(struct device *dev,
395 struct device_attribute *attr, char *buf)
397 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
400 int i = cable->cable_index;
402 return sprintf(buf, "%d\n",
403 extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
407 * extcon_sync() - Synchronize the states for both the attached/detached
408 * @edev: the extcon device that has the cable.
410 * This function send a notification to synchronize the all states of a
411 * specific external connector
413 int extcon_sync(struct extcon_dev *edev, unsigned int id)
428 index = find_cable_index_by_id(edev, id);
432 spin_lock_irqsave(&edev->lock, flags);
434 state = !!(edev->state & BIT(index));
435 raw_notifier_call_chain(&edev->nh[index], state, edev);
437 /* This could be in interrupt handler */
438 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
440 /* Unlock early before uevent */
441 spin_unlock_irqrestore(&edev->lock, flags);
443 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
444 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
449 length = name_show(&edev->dev, NULL, prop_buf);
451 if (prop_buf[length - 1] == '\n')
452 prop_buf[length - 1] = 0;
453 snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
454 envp[env_offset++] = name_buf;
457 length = state_show(&edev->dev, NULL, prop_buf);
459 if (prop_buf[length - 1] == '\n')
460 prop_buf[length - 1] = 0;
461 snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
462 envp[env_offset++] = state_buf;
464 envp[env_offset] = NULL;
466 /* Unlock early before uevent */
467 spin_unlock_irqrestore(&edev->lock, flags);
468 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
469 free_page((unsigned long)prop_buf);
473 EXPORT_SYMBOL_GPL(extcon_sync);
476 * extcon_get_state() - Get the state of a external connector.
477 * @edev: the extcon device that has the cable.
478 * @id: the unique id of each external connector in extcon enumeration.
480 int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
488 index = find_cable_index_by_id(edev, id);
492 spin_lock_irqsave(&edev->lock, flags);
493 state = is_extcon_attached(edev, index);
494 spin_unlock_irqrestore(&edev->lock, flags);
498 EXPORT_SYMBOL_GPL(extcon_get_state);
501 * extcon_set_state() - Set the state of a external connector.
502 * without a notification.
503 * @edev: the extcon device that has the cable.
504 * @id: the unique id of each external connector
505 * in extcon enumeration.
506 * @state: the new cable status. The default semantics is
507 * true: attached / false: detached.
509 * This function only set the state of a external connector without
510 * a notification. To synchronize the data of a external connector,
511 * use extcon_set_state_sync() and extcon_sync().
513 int extcon_set_state(struct extcon_dev *edev, unsigned int id,
522 index = find_cable_index_by_id(edev, id);
526 spin_lock_irqsave(&edev->lock, flags);
528 /* Check whether the external connector's state is changed. */
529 if (!is_extcon_changed(edev, index, cable_state))
532 if (check_mutually_exclusive(edev,
533 (edev->state & ~BIT(index)) | (cable_state & BIT(index)))) {
539 * Initialize the value of extcon property before setting
540 * the detached state for an external connector.
543 init_property(edev, id, index);
545 /* Update the state for a external connector. */
547 edev->state |= BIT(index);
549 edev->state &= ~(BIT(index));
551 spin_unlock_irqrestore(&edev->lock, flags);
555 EXPORT_SYMBOL_GPL(extcon_set_state);
558 * extcon_set_state_sync() - Set the state of a external connector
559 * with a notification.
560 * @edev: the extcon device that has the cable.
561 * @id: the unique id of each external connector
562 * in extcon enumeration.
563 * @state: the new cable status. The default semantics is
564 * true: attached / false: detached.
566 * This function set the state of external connector and synchronize the data
567 * by usning a notification.
569 int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id,
575 index = find_cable_index_by_id(edev, id);
579 /* Check whether the external connector's state is changed. */
580 spin_lock_irqsave(&edev->lock, flags);
581 ret = is_extcon_changed(edev, index, cable_state);
582 spin_unlock_irqrestore(&edev->lock, flags);
586 ret = extcon_set_state(edev, id, cable_state);
590 return extcon_sync(edev, id);
592 EXPORT_SYMBOL_GPL(extcon_set_state_sync);
595 * extcon_get_property() - Get the property value of a specific cable.
596 * @edev: the extcon device that has the cable.
597 * @id: the unique id of each external connector
598 * in extcon enumeration.
599 * @prop: the property id among enum extcon_property.
600 * @prop_val: the pointer which store the value of property.
602 * When getting the property value of external connector, the external connector
603 * should be attached. If detached state, function just return 0 without
604 * property value. Also, the each property should be included in the list of
605 * supported properties according to the type of external connectors.
607 * Returns 0 if success or error number if fail
609 int extcon_get_property(struct extcon_dev *edev, unsigned int id,
611 union extcon_property_value *prop_val)
613 struct extcon_cable *cable;
617 *prop_val = (union extcon_property_value)(0);
622 /* Check whether the property is supported or not */
623 if (!is_extcon_property_supported(id, prop))
626 /* Find the cable index of external connector by using id */
627 index = find_cable_index_by_id(edev, id);
631 spin_lock_irqsave(&edev->lock, flags);
633 /* Check whether the property is available or not. */
634 if (!is_extcon_property_capability(edev, id, index, prop)) {
635 spin_unlock_irqrestore(&edev->lock, flags);
640 * Check whether the external connector is attached.
641 * If external connector is detached, the user can not
642 * get the property value.
644 if (!is_extcon_attached(edev, index)) {
645 spin_unlock_irqrestore(&edev->lock, flags);
649 cable = &edev->cables[index];
651 /* Get the property value according to extcon type */
653 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
654 *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
656 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
657 *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
659 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
660 *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
662 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
663 *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
670 spin_unlock_irqrestore(&edev->lock, flags);
674 EXPORT_SYMBOL_GPL(extcon_get_property);
677 * extcon_set_property() - Set the property value of a specific cable.
678 * @edev: the extcon device that has the cable.
679 * @id: the unique id of each external connector
680 * in extcon enumeration.
681 * @prop: the property id among enum extcon_property.
682 * @prop_val: the pointer including the new value of property.
684 * The each property should be included in the list of supported properties
685 * according to the type of external connectors.
687 * Returns 0 if success or error number if fail
689 int extcon_set_property(struct extcon_dev *edev, unsigned int id,
691 union extcon_property_value prop_val)
693 struct extcon_cable *cable;
700 /* Check whether the property is supported or not */
701 if (!is_extcon_property_supported(id, prop))
704 /* Find the cable index of external connector by using id */
705 index = find_cable_index_by_id(edev, id);
709 spin_lock_irqsave(&edev->lock, flags);
711 /* Check whether the property is available or not. */
712 if (!is_extcon_property_capability(edev, id, index, prop)) {
713 spin_unlock_irqrestore(&edev->lock, flags);
717 cable = &edev->cables[index];
719 /* Set the property value according to extcon type */
721 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
722 cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
724 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
725 cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
727 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
728 cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
730 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
731 cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
738 spin_unlock_irqrestore(&edev->lock, flags);
742 EXPORT_SYMBOL_GPL(extcon_set_property);
745 * extcon_set_property_sync() - Set the property value of a specific cable
747 * @prop_val: the pointer including the new value of property.
749 * When setting the property value of external connector, the external connector
750 * should be attached. The each property should be included in the list of
751 * supported properties according to the type of external connectors.
753 * Returns 0 if success or error number if fail
755 int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
757 union extcon_property_value prop_val)
761 ret = extcon_set_property(edev, id, prop, prop_val);
765 return extcon_sync(edev, id);
767 EXPORT_SYMBOL_GPL(extcon_set_property_sync);
770 * extcon_get_property_capability() - Get the capability of property
771 * of an external connector.
772 * @edev: the extcon device that has the cable.
773 * @id: the unique id of each external connector
774 * in extcon enumeration.
775 * @prop: the property id among enum extcon_property.
777 * Returns 1 if the property is available or 0 if not available.
779 int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
787 /* Check whether the property is supported or not */
788 if (!is_extcon_property_supported(id, prop))
791 /* Find the cable index of external connector by using id */
792 index = find_cable_index_by_id(edev, id);
796 return is_extcon_property_capability(edev, id, index, prop);
798 EXPORT_SYMBOL_GPL(extcon_get_property_capability);
801 * extcon_set_property_capability() - Set the capability of a property
802 * of an external connector.
803 * @edev: the extcon device that has the cable.
804 * @id: the unique id of each external connector
805 * in extcon enumeration.
806 * @prop: the property id among enum extcon_property.
808 * This function set the capability of a property for an external connector
809 * to mark the bit in capability bitmap which mean the available state of
812 * Returns 0 if success or error number if fail
814 int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
817 struct extcon_cable *cable;
818 int index, type, ret = 0;
823 /* Check whether the property is supported or not. */
824 if (!is_extcon_property_supported(id, prop))
827 /* Find the cable index of external connector by using id. */
828 index = find_cable_index_by_id(edev, id);
832 type = get_extcon_type(prop);
836 cable = &edev->cables[index];
839 case EXTCON_TYPE_USB:
840 __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
842 case EXTCON_TYPE_CHG:
843 __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
845 case EXTCON_TYPE_JACK:
846 __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
848 case EXTCON_TYPE_DISP:
849 __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
857 EXPORT_SYMBOL_GPL(extcon_set_property_capability);
860 * extcon_get_extcon_dev() - Get the extcon device instance from the name
861 * @extcon_name: The extcon name provided with extcon_dev_register()
863 struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
865 struct extcon_dev *sd;
868 return ERR_PTR(-EINVAL);
870 mutex_lock(&extcon_dev_list_lock);
871 list_for_each_entry(sd, &extcon_dev_list, entry) {
872 if (!strcmp(sd->name, extcon_name))
877 mutex_unlock(&extcon_dev_list_lock);
880 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
883 * extcon_register_notifier() - Register a notifiee to get notified by
884 * any attach status changes from the extcon.
885 * @edev: the extcon device that has the external connecotr.
886 * @id: the unique id of each external connector in extcon enumeration.
887 * @nb: a notifier block to be registered.
889 * Note that the second parameter given to the callback of nb (val) is
890 * "old_state", not the current state. The current state can be retrieved
891 * by looking at the third pameter (edev pointer)'s state value.
893 int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
894 struct notifier_block *nb)
897 int ret, idx = -EINVAL;
903 idx = find_cable_index_by_id(edev, id);
907 spin_lock_irqsave(&edev->lock, flags);
908 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
909 spin_unlock_irqrestore(&edev->lock, flags);
911 struct extcon_dev *extd;
913 mutex_lock(&extcon_dev_list_lock);
914 list_for_each_entry(extd, &extcon_dev_list, entry) {
915 idx = find_cable_index_by_id(extd, id);
919 mutex_unlock(&extcon_dev_list_lock);
923 return extcon_register_notifier(extd, id, nb);
931 EXPORT_SYMBOL_GPL(extcon_register_notifier);
934 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
935 * @edev: the extcon device that has the external connecotr.
936 * @id: the unique id of each external connector in extcon enumeration.
937 * @nb: a notifier block to be registered.
939 int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
940 struct notifier_block *nb)
948 idx = find_cable_index_by_id(edev, id);
952 spin_lock_irqsave(&edev->lock, flags);
953 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
954 spin_unlock_irqrestore(&edev->lock, flags);
958 EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
960 static struct attribute *extcon_attrs[] = {
961 &dev_attr_state.attr,
965 ATTRIBUTE_GROUPS(extcon);
967 static int create_extcon_class(void)
970 extcon_class = class_create(THIS_MODULE, "extcon");
971 if (IS_ERR(extcon_class))
972 return PTR_ERR(extcon_class);
973 extcon_class->dev_groups = extcon_groups;
975 #if defined(CONFIG_ANDROID)
976 switch_class = class_compat_register("switch");
977 if (WARN(!switch_class, "cannot allocate"))
979 #endif /* CONFIG_ANDROID */
985 static void extcon_dev_release(struct device *dev)
989 static const char *muex_name = "mutually_exclusive";
990 static void dummy_sysfs_dev_release(struct device *dev)
995 * extcon_dev_allocate() - Allocate the memory of extcon device.
996 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
997 * If supported_cable is NULL, cable name related APIs
1000 * This function allocates the memory for extcon device without allocating
1001 * memory in each extcon provider driver and initialize default setting for
1004 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
1006 struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
1008 struct extcon_dev *edev;
1010 if (!supported_cable)
1011 return ERR_PTR(-EINVAL);
1013 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
1015 return ERR_PTR(-ENOMEM);
1017 edev->max_supported = 0;
1018 edev->supported_cable = supported_cable;
1024 * extcon_dev_free() - Free the memory of extcon device.
1025 * @edev: the extcon device to free
1027 void extcon_dev_free(struct extcon_dev *edev)
1031 EXPORT_SYMBOL_GPL(extcon_dev_free);
1034 * extcon_dev_register() - Register a new extcon device
1035 * @edev : the new extcon device (should be allocated before calling)
1037 * Among the members of edev struct, please set the "user initializing data"
1038 * in any case and set the "optional callbacks" if required. However, please
1039 * do not set the values of "internal data", which are initialized by
1042 int extcon_dev_register(struct extcon_dev *edev)
1045 static atomic_t edev_no = ATOMIC_INIT(-1);
1047 if (!extcon_class) {
1048 ret = create_extcon_class();
1053 if (!edev || !edev->supported_cable)
1056 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
1058 edev->max_supported = index;
1059 if (index > SUPPORTED_CABLE_MAX) {
1061 "exceed the maximum number of supported cables\n");
1065 edev->dev.class = extcon_class;
1066 edev->dev.release = extcon_dev_release;
1068 edev->name = dev_name(edev->dev.parent);
1069 if (IS_ERR_OR_NULL(edev->name)) {
1071 "extcon device name is null\n");
1074 dev_set_name(&edev->dev, "extcon%lu",
1075 (unsigned long)atomic_inc_return(&edev_no));
1077 if (edev->max_supported) {
1080 struct extcon_cable *cable;
1082 edev->cables = kzalloc(sizeof(struct extcon_cable) *
1083 edev->max_supported, GFP_KERNEL);
1084 if (!edev->cables) {
1086 goto err_sysfs_alloc;
1088 for (index = 0; index < edev->max_supported; index++) {
1089 cable = &edev->cables[index];
1091 snprintf(buf, 10, "cable.%d", index);
1092 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
1095 for (index--; index >= 0; index--) {
1096 cable = &edev->cables[index];
1097 kfree(cable->attr_g.name);
1101 goto err_alloc_cables;
1106 cable->cable_index = index;
1107 cable->attrs[0] = &cable->attr_name.attr;
1108 cable->attrs[1] = &cable->attr_state.attr;
1109 cable->attrs[2] = NULL;
1110 cable->attr_g.name = str;
1111 cable->attr_g.attrs = cable->attrs;
1113 sysfs_attr_init(&cable->attr_name.attr);
1114 cable->attr_name.attr.name = "name";
1115 cable->attr_name.attr.mode = 0444;
1116 cable->attr_name.show = cable_name_show;
1118 sysfs_attr_init(&cable->attr_state.attr);
1119 cable->attr_state.attr.name = "state";
1120 cable->attr_state.attr.mode = 0444;
1121 cable->attr_state.show = cable_state_show;
1125 if (edev->max_supported && edev->mutually_exclusive) {
1129 /* Count the size of mutually_exclusive array */
1130 for (index = 0; edev->mutually_exclusive[index]; index++)
1133 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
1134 (index + 1), GFP_KERNEL);
1135 if (!edev->attrs_muex) {
1140 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
1142 if (!edev->d_attrs_muex) {
1144 kfree(edev->attrs_muex);
1148 for (index = 0; edev->mutually_exclusive[index]; index++) {
1149 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
1150 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
1153 for (index--; index >= 0; index--) {
1154 kfree(edev->d_attrs_muex[index].attr.
1157 kfree(edev->d_attrs_muex);
1158 kfree(edev->attrs_muex);
1163 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
1164 edev->d_attrs_muex[index].attr.name = name;
1165 edev->d_attrs_muex[index].attr.mode = 0000;
1166 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
1169 edev->attr_g_muex.name = muex_name;
1170 edev->attr_g_muex.attrs = edev->attrs_muex;
1174 if (edev->max_supported) {
1175 edev->extcon_dev_type.groups =
1176 kzalloc(sizeof(struct attribute_group *) *
1177 (edev->max_supported + 2), GFP_KERNEL);
1178 if (!edev->extcon_dev_type.groups) {
1180 goto err_alloc_groups;
1183 edev->extcon_dev_type.name = dev_name(&edev->dev);
1184 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
1186 for (index = 0; index < edev->max_supported; index++)
1187 edev->extcon_dev_type.groups[index] =
1188 &edev->cables[index].attr_g;
1189 if (edev->mutually_exclusive)
1190 edev->extcon_dev_type.groups[index] =
1193 edev->dev.type = &edev->extcon_dev_type;
1196 ret = device_register(&edev->dev);
1198 put_device(&edev->dev);
1201 #if defined(CONFIG_ANDROID)
1203 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
1204 #endif /* CONFIG_ANDROID */
1206 spin_lock_init(&edev->lock);
1208 edev->nh = devm_kzalloc(&edev->dev,
1209 sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
1215 for (index = 0; index < edev->max_supported; index++)
1216 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
1218 dev_set_drvdata(&edev->dev, edev);
1221 mutex_lock(&extcon_dev_list_lock);
1222 list_add(&edev->entry, &extcon_dev_list);
1223 mutex_unlock(&extcon_dev_list_lock);
1228 if (edev->max_supported)
1229 kfree(edev->extcon_dev_type.groups);
1231 if (edev->max_supported && edev->mutually_exclusive) {
1232 for (index = 0; edev->mutually_exclusive[index]; index++)
1233 kfree(edev->d_attrs_muex[index].attr.name);
1234 kfree(edev->d_attrs_muex);
1235 kfree(edev->attrs_muex);
1238 for (index = 0; index < edev->max_supported; index++)
1239 kfree(edev->cables[index].attr_g.name);
1241 if (edev->max_supported)
1242 kfree(edev->cables);
1246 EXPORT_SYMBOL_GPL(extcon_dev_register);
1249 * extcon_dev_unregister() - Unregister the extcon device.
1250 * @edev: the extcon device instance to be unregistered.
1252 * Note that this does not call kfree(edev) because edev was not allocated
1255 void extcon_dev_unregister(struct extcon_dev *edev)
1262 mutex_lock(&extcon_dev_list_lock);
1263 list_del(&edev->entry);
1264 mutex_unlock(&extcon_dev_list_lock);
1266 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
1267 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
1268 dev_name(&edev->dev));
1272 device_unregister(&edev->dev);
1274 if (edev->mutually_exclusive && edev->max_supported) {
1275 for (index = 0; edev->mutually_exclusive[index];
1277 kfree(edev->d_attrs_muex[index].attr.name);
1278 kfree(edev->d_attrs_muex);
1279 kfree(edev->attrs_muex);
1282 for (index = 0; index < edev->max_supported; index++)
1283 kfree(edev->cables[index].attr_g.name);
1285 if (edev->max_supported) {
1286 kfree(edev->extcon_dev_type.groups);
1287 kfree(edev->cables);
1290 #if defined(CONFIG_ANDROID)
1292 class_compat_remove_link(switch_class, &edev->dev, NULL);
1294 put_device(&edev->dev);
1296 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1300 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1301 * @dev - instance to the given device
1302 * @index - index into list of extcon_dev
1304 * return the instance of extcon device
1306 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1308 struct device_node *node;
1309 struct extcon_dev *edev;
1312 return ERR_PTR(-EINVAL);
1314 if (!dev->of_node) {
1315 dev_dbg(dev, "device does not have a device node entry\n");
1316 return ERR_PTR(-EINVAL);
1319 node = of_parse_phandle(dev->of_node, "extcon", index);
1321 dev_dbg(dev, "failed to get phandle in %s node\n",
1322 dev->of_node->full_name);
1323 return ERR_PTR(-ENODEV);
1326 mutex_lock(&extcon_dev_list_lock);
1327 list_for_each_entry(edev, &extcon_dev_list, entry) {
1328 if (edev->dev.parent && edev->dev.parent->of_node == node) {
1329 mutex_unlock(&extcon_dev_list_lock);
1334 mutex_unlock(&extcon_dev_list_lock);
1337 return ERR_PTR(-EPROBE_DEFER);
1340 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1342 return ERR_PTR(-ENOSYS);
1344 #endif /* CONFIG_OF */
1345 EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1348 * extcon_get_edev_name() - Get the name of the extcon device.
1349 * @edev: the extcon device
1351 const char *extcon_get_edev_name(struct extcon_dev *edev)
1353 return !edev ? NULL : edev->name;
1356 static int __init extcon_class_init(void)
1358 return create_extcon_class();
1360 module_init(extcon_class_init);
1362 static void __exit extcon_class_exit(void)
1364 #if defined(CONFIG_ANDROID)
1365 class_compat_unregister(switch_class);
1367 class_destroy(extcon_class);
1369 module_exit(extcon_class_exit);
1371 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1372 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1373 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1374 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1375 MODULE_DESCRIPTION("External connector (extcon) class driver");
1376 MODULE_LICENSE("GPL");