2 * drivers/extcon/extcon.c - External Connector (extcon) framework.
4 * External connector (extcon) class driver
6 * Copyright (C) 2015 Samsung Electronics
9 * Copyright (C) 2012 Samsung Electronics
13 * based on android/drivers/switch/switch_class.c
14 * Copyright (C) 2008 Google, Inc.
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>
34 #include <linux/slab.h>
35 #include <linux/sysfs.h>
39 #define SUPPORTED_CABLE_MAX 32
40 #define CABLE_NAME_MAX 30
42 struct __extcon_info {
49 .type = EXTCON_TYPE_MISC,
54 /* USB external connector */
56 .type = EXTCON_TYPE_USB,
61 .type = EXTCON_TYPE_USB,
62 .id = EXTCON_USB_HOST,
66 /* Charging external connector */
67 [EXTCON_CHG_USB_SDP] = {
68 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
69 .id = EXTCON_CHG_USB_SDP,
72 [EXTCON_CHG_USB_DCP] = {
73 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
74 .id = EXTCON_CHG_USB_DCP,
77 [EXTCON_CHG_USB_CDP] = {
78 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
79 .id = EXTCON_CHG_USB_CDP,
82 [EXTCON_CHG_USB_ACA] = {
83 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
84 .id = EXTCON_CHG_USB_ACA,
87 [EXTCON_CHG_USB_FAST] = {
88 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
89 .id = EXTCON_CHG_USB_FAST,
90 .name = "FAST-CHARGER",
92 [EXTCON_CHG_USB_SLOW] = {
93 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
94 .id = EXTCON_CHG_USB_SLOW,
95 .name = "SLOW-CHARGER",
98 .type = EXTCON_TYPE_CHG,
102 [EXTCON_CHG_USB_PD] = {
103 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
104 .id = EXTCON_CHG_USB_PD,
108 /* Jack external connector */
109 [EXTCON_JACK_MICROPHONE] = {
110 .type = EXTCON_TYPE_JACK,
111 .id = EXTCON_JACK_MICROPHONE,
112 .name = "MICROPHONE",
114 [EXTCON_JACK_HEADPHONE] = {
115 .type = EXTCON_TYPE_JACK,
116 .id = EXTCON_JACK_HEADPHONE,
119 [EXTCON_JACK_LINE_IN] = {
120 .type = EXTCON_TYPE_JACK,
121 .id = EXTCON_JACK_LINE_IN,
124 [EXTCON_JACK_LINE_OUT] = {
125 .type = EXTCON_TYPE_JACK,
126 .id = EXTCON_JACK_LINE_OUT,
129 [EXTCON_JACK_VIDEO_IN] = {
130 .type = EXTCON_TYPE_JACK,
131 .id = EXTCON_JACK_VIDEO_IN,
134 [EXTCON_JACK_VIDEO_OUT] = {
135 .type = EXTCON_TYPE_JACK,
136 .id = EXTCON_JACK_VIDEO_OUT,
139 [EXTCON_JACK_SPDIF_IN] = {
140 .type = EXTCON_TYPE_JACK,
141 .id = EXTCON_JACK_SPDIF_IN,
144 [EXTCON_JACK_SPDIF_OUT] = {
145 .type = EXTCON_TYPE_JACK,
146 .id = EXTCON_JACK_SPDIF_OUT,
150 /* Display external connector */
151 [EXTCON_DISP_HDMI] = {
152 .type = EXTCON_TYPE_DISP,
153 .id = EXTCON_DISP_HDMI,
156 [EXTCON_DISP_MHL] = {
157 .type = EXTCON_TYPE_DISP,
158 .id = EXTCON_DISP_MHL,
161 [EXTCON_DISP_DVI] = {
162 .type = EXTCON_TYPE_DISP,
163 .id = EXTCON_DISP_DVI,
166 [EXTCON_DISP_VGA] = {
167 .type = EXTCON_TYPE_DISP,
168 .id = EXTCON_DISP_VGA,
172 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
173 .id = EXTCON_DISP_DP,
176 [EXTCON_DISP_HMD] = {
177 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
178 .id = EXTCON_DISP_HMD,
182 /* Miscellaneous external connector */
184 .type = EXTCON_TYPE_MISC,
189 .type = EXTCON_TYPE_MISC,
193 [EXTCON_MECHANICAL] = {
194 .type = EXTCON_TYPE_MISC,
195 .id = EXTCON_MECHANICAL,
196 .name = "MECHANICAL",
203 * struct extcon_cable - An internal data for each cable of extcon device.
204 * @edev: The extcon device
205 * @cable_index: Index of this cable in the edev
206 * @attr_g: Attribute group for the cable
207 * @attr_name: "name" sysfs entry
208 * @attr_state: "state" sysfs entry
209 * @attrs: Array pointing to attr_name and attr_state for attr_g
211 struct extcon_cable {
212 struct extcon_dev *edev;
215 struct attribute_group attr_g;
216 struct device_attribute attr_name;
217 struct device_attribute attr_state;
219 struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
221 union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
222 union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
223 union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
224 union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
226 unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
227 unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
228 unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
229 unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
232 static struct class *extcon_class;
234 static LIST_HEAD(extcon_dev_list);
235 static DEFINE_MUTEX(extcon_dev_list_lock);
238 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
240 * @edev: the extcon device
241 * @new_state: new cable attach status for @edev
243 * Returns 0 if nothing violates. Returns the index + 1 for the first
244 * violated condition.
246 static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
250 if (!edev->mutually_exclusive)
253 for (i = 0; edev->mutually_exclusive[i]; i++) {
255 u32 correspondants = new_state & edev->mutually_exclusive[i];
257 /* calculate the total number of bits set */
258 weight = hweight32(correspondants);
266 static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
270 /* Find the the index of extcon cable in edev->supported_cable */
271 for (i = 0; i < edev->max_supported; i++) {
272 if (edev->supported_cable[i] == id)
279 static int get_extcon_type(unsigned int prop)
282 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
283 return EXTCON_TYPE_USB;
284 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
285 return EXTCON_TYPE_CHG;
286 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
287 return EXTCON_TYPE_JACK;
288 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
289 return EXTCON_TYPE_DISP;
295 static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
297 return !!(edev->state & BIT(index));
300 static bool is_extcon_changed(struct extcon_dev *edev, int index,
303 int state = !!(edev->state & BIT(index));
304 return (state != new_state);
307 static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
311 /* Check whether the property is supported or not. */
312 type = get_extcon_type(prop);
316 /* Check whether a specific extcon id supports the property or not. */
317 return !!(extcon_info[id].type & type);
320 static int is_extcon_property_capability(struct extcon_dev *edev,
321 unsigned int id, int index,unsigned int prop)
323 struct extcon_cable *cable;
326 /* Check whether the property is supported or not. */
327 type = get_extcon_type(prop);
331 cable = &edev->cables[index];
334 case EXTCON_TYPE_USB:
335 ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
337 case EXTCON_TYPE_CHG:
338 ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
340 case EXTCON_TYPE_JACK:
341 ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
343 case EXTCON_TYPE_DISP:
344 ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
353 static void init_property(struct extcon_dev *edev, unsigned int id, int index)
355 unsigned int type = extcon_info[id].type;
356 struct extcon_cable *cable = &edev->cables[index];
358 if (EXTCON_TYPE_USB & type)
359 memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
360 if (EXTCON_TYPE_CHG & type)
361 memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
362 if (EXTCON_TYPE_JACK & type)
363 memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
364 if (EXTCON_TYPE_DISP & type)
365 memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
368 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
372 struct extcon_dev *edev = dev_get_drvdata(dev);
374 if (edev->max_supported == 0)
375 return sprintf(buf, "%u\n", edev->state);
377 for (i = 0; i < edev->max_supported; i++) {
378 count += sprintf(buf + count, "%s=%d\n",
379 extcon_info[edev->supported_cable[i]].name,
380 !!(edev->state & BIT(i)));
385 static DEVICE_ATTR_RO(state);
387 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
390 struct extcon_dev *edev = dev_get_drvdata(dev);
392 return sprintf(buf, "%s\n", edev->name);
394 static DEVICE_ATTR_RO(name);
396 static ssize_t cable_name_show(struct device *dev,
397 struct device_attribute *attr, char *buf)
399 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
401 int i = cable->cable_index;
403 return sprintf(buf, "%s\n",
404 extcon_info[cable->edev->supported_cable[i]].name);
407 static ssize_t cable_state_show(struct device *dev,
408 struct device_attribute *attr, char *buf)
410 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
413 int i = cable->cable_index;
415 return sprintf(buf, "%d\n",
416 extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
420 * extcon_sync() - Synchronize the states for both the attached/detached
421 * @edev: the extcon device that has the cable.
423 * This function send a notification to synchronize the all states of a
424 * specific external connector
426 int extcon_sync(struct extcon_dev *edev, unsigned int id)
441 index = find_cable_index_by_id(edev, id);
445 spin_lock_irqsave(&edev->lock, flags);
447 state = !!(edev->state & BIT(index));
450 * Call functions in a raw notifier chain for the specific one
451 * external connector.
453 raw_notifier_call_chain(&edev->nh[index], state, edev);
456 * Call functions in a raw notifier chain for the all supported
457 * external connectors.
459 raw_notifier_call_chain(&edev->nh_all, state, edev);
461 /* This could be in interrupt handler */
462 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
464 /* Unlock early before uevent */
465 spin_unlock_irqrestore(&edev->lock, flags);
467 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
468 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
473 length = name_show(&edev->dev, NULL, prop_buf);
475 if (prop_buf[length - 1] == '\n')
476 prop_buf[length - 1] = 0;
477 snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
478 envp[env_offset++] = name_buf;
481 length = state_show(&edev->dev, NULL, prop_buf);
483 if (prop_buf[length - 1] == '\n')
484 prop_buf[length - 1] = 0;
485 snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
486 envp[env_offset++] = state_buf;
488 envp[env_offset] = NULL;
490 /* Unlock early before uevent */
491 spin_unlock_irqrestore(&edev->lock, flags);
492 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
493 free_page((unsigned long)prop_buf);
497 EXPORT_SYMBOL_GPL(extcon_sync);
500 * extcon_get_state() - Get the state of a external connector.
501 * @edev: the extcon device that has the cable.
502 * @id: the unique id of each external connector in extcon enumeration.
504 int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
512 index = find_cable_index_by_id(edev, id);
516 spin_lock_irqsave(&edev->lock, flags);
517 state = is_extcon_attached(edev, index);
518 spin_unlock_irqrestore(&edev->lock, flags);
522 EXPORT_SYMBOL_GPL(extcon_get_state);
525 * extcon_set_state() - Set the state of a external connector.
526 * without a notification.
527 * @edev: the extcon device that has the cable.
528 * @id: the unique id of each external connector
529 * in extcon enumeration.
530 * @state: the new cable status. The default semantics is
531 * true: attached / false: detached.
533 * This function only set the state of a external connector without
534 * a notification. To synchronize the data of a external connector,
535 * use extcon_set_state_sync() and extcon_sync().
537 int extcon_set_state(struct extcon_dev *edev, unsigned int id,
546 index = find_cable_index_by_id(edev, id);
550 spin_lock_irqsave(&edev->lock, flags);
552 /* Check whether the external connector's state is changed. */
553 if (!is_extcon_changed(edev, index, cable_state))
556 if (check_mutually_exclusive(edev,
557 (edev->state & ~BIT(index)) | (cable_state & BIT(index)))) {
563 * Initialize the value of extcon property before setting
564 * the detached state for an external connector.
567 init_property(edev, id, index);
569 /* Update the state for a external connector. */
571 edev->state |= BIT(index);
573 edev->state &= ~(BIT(index));
575 spin_unlock_irqrestore(&edev->lock, flags);
579 EXPORT_SYMBOL_GPL(extcon_set_state);
582 * extcon_set_state_sync() - Set the state of a external connector
583 * with a notification.
584 * @edev: the extcon device that has the cable.
585 * @id: the unique id of each external connector
586 * in extcon enumeration.
587 * @state: the new cable status. The default semantics is
588 * true: attached / false: detached.
590 * This function set the state of external connector and synchronize the data
591 * by usning a notification.
593 int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id,
599 index = find_cable_index_by_id(edev, id);
603 /* Check whether the external connector's state is changed. */
604 spin_lock_irqsave(&edev->lock, flags);
605 ret = is_extcon_changed(edev, index, cable_state);
606 spin_unlock_irqrestore(&edev->lock, flags);
610 ret = extcon_set_state(edev, id, cable_state);
614 return extcon_sync(edev, id);
616 EXPORT_SYMBOL_GPL(extcon_set_state_sync);
619 * extcon_get_property() - Get the property value of a specific cable.
620 * @edev: the extcon device that has the cable.
621 * @id: the unique id of each external connector
622 * in extcon enumeration.
623 * @prop: the property id among enum extcon_property.
624 * @prop_val: the pointer which store the value of property.
626 * When getting the property value of external connector, the external connector
627 * should be attached. If detached state, function just return 0 without
628 * property value. Also, the each property should be included in the list of
629 * supported properties according to the type of external connectors.
631 * Returns 0 if success or error number if fail
633 int extcon_get_property(struct extcon_dev *edev, unsigned int id,
635 union extcon_property_value *prop_val)
637 struct extcon_cable *cable;
641 *prop_val = (union extcon_property_value)(0);
646 /* Check whether the property is supported or not */
647 if (!is_extcon_property_supported(id, prop))
650 /* Find the cable index of external connector by using id */
651 index = find_cable_index_by_id(edev, id);
655 spin_lock_irqsave(&edev->lock, flags);
657 /* Check whether the property is available or not. */
658 if (!is_extcon_property_capability(edev, id, index, prop)) {
659 spin_unlock_irqrestore(&edev->lock, flags);
664 * Check whether the external connector is attached.
665 * If external connector is detached, the user can not
666 * get the property value.
668 if (!is_extcon_attached(edev, index)) {
669 spin_unlock_irqrestore(&edev->lock, flags);
673 cable = &edev->cables[index];
675 /* Get the property value according to extcon type */
677 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
678 *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
680 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
681 *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
683 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
684 *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
686 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
687 *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
694 spin_unlock_irqrestore(&edev->lock, flags);
698 EXPORT_SYMBOL_GPL(extcon_get_property);
701 * extcon_set_property() - Set the property value of a specific cable.
702 * @edev: the extcon device that has the cable.
703 * @id: the unique id of each external connector
704 * in extcon enumeration.
705 * @prop: the property id among enum extcon_property.
706 * @prop_val: the pointer including the new value of property.
708 * The each property should be included in the list of supported properties
709 * according to the type of external connectors.
711 * Returns 0 if success or error number if fail
713 int extcon_set_property(struct extcon_dev *edev, unsigned int id,
715 union extcon_property_value prop_val)
717 struct extcon_cable *cable;
724 /* Check whether the property is supported or not */
725 if (!is_extcon_property_supported(id, prop))
728 /* Find the cable index of external connector by using id */
729 index = find_cable_index_by_id(edev, id);
733 spin_lock_irqsave(&edev->lock, flags);
735 /* Check whether the property is available or not. */
736 if (!is_extcon_property_capability(edev, id, index, prop)) {
737 spin_unlock_irqrestore(&edev->lock, flags);
741 cable = &edev->cables[index];
743 /* Set the property value according to extcon type */
745 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
746 cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
748 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
749 cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
751 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
752 cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
754 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
755 cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
762 spin_unlock_irqrestore(&edev->lock, flags);
766 EXPORT_SYMBOL_GPL(extcon_set_property);
769 * extcon_set_property_sync() - Set the property value of a specific cable
771 * @prop_val: the pointer including the new value of property.
773 * When setting the property value of external connector, the external connector
774 * should be attached. The each property should be included in the list of
775 * supported properties according to the type of external connectors.
777 * Returns 0 if success or error number if fail
779 int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
781 union extcon_property_value prop_val)
785 ret = extcon_set_property(edev, id, prop, prop_val);
789 return extcon_sync(edev, id);
791 EXPORT_SYMBOL_GPL(extcon_set_property_sync);
794 * extcon_get_property_capability() - Get the capability of property
795 * of an external connector.
796 * @edev: the extcon device that has the cable.
797 * @id: the unique id of each external connector
798 * in extcon enumeration.
799 * @prop: the property id among enum extcon_property.
801 * Returns 1 if the property is available or 0 if not available.
803 int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
811 /* Check whether the property is supported or not */
812 if (!is_extcon_property_supported(id, prop))
815 /* Find the cable index of external connector by using id */
816 index = find_cable_index_by_id(edev, id);
820 return is_extcon_property_capability(edev, id, index, prop);
822 EXPORT_SYMBOL_GPL(extcon_get_property_capability);
825 * extcon_set_property_capability() - Set the capability of a property
826 * of an external connector.
827 * @edev: the extcon device that has the cable.
828 * @id: the unique id of each external connector
829 * in extcon enumeration.
830 * @prop: the property id among enum extcon_property.
832 * This function set the capability of a property for an external connector
833 * to mark the bit in capability bitmap which mean the available state of
836 * Returns 0 if success or error number if fail
838 int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
841 struct extcon_cable *cable;
842 int index, type, ret = 0;
847 /* Check whether the property is supported or not. */
848 if (!is_extcon_property_supported(id, prop))
851 /* Find the cable index of external connector by using id. */
852 index = find_cable_index_by_id(edev, id);
856 type = get_extcon_type(prop);
860 cable = &edev->cables[index];
863 case EXTCON_TYPE_USB:
864 __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
866 case EXTCON_TYPE_CHG:
867 __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
869 case EXTCON_TYPE_JACK:
870 __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
872 case EXTCON_TYPE_DISP:
873 __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
881 EXPORT_SYMBOL_GPL(extcon_set_property_capability);
884 * extcon_get_extcon_dev() - Get the extcon device instance from the name
885 * @extcon_name: The extcon name provided with extcon_dev_register()
887 struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
889 struct extcon_dev *sd;
892 return ERR_PTR(-EINVAL);
894 mutex_lock(&extcon_dev_list_lock);
895 list_for_each_entry(sd, &extcon_dev_list, entry) {
896 if (!strcmp(sd->name, extcon_name))
901 mutex_unlock(&extcon_dev_list_lock);
904 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
907 * extcon_register_notifier() - Register a notifiee to get notified by
908 * any attach status changes from the extcon.
909 * @edev: the extcon device that has the external connecotr.
910 * @id: the unique id of each external connector in extcon enumeration.
911 * @nb: a notifier block to be registered.
913 * Note that the second parameter given to the callback of nb (val) is
914 * "old_state", not the current state. The current state can be retrieved
915 * by looking at the third pameter (edev pointer)'s state value.
917 int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
918 struct notifier_block *nb)
921 int ret, idx = -EINVAL;
926 idx = find_cable_index_by_id(edev, id);
930 spin_lock_irqsave(&edev->lock, flags);
931 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
932 spin_unlock_irqrestore(&edev->lock, flags);
936 EXPORT_SYMBOL_GPL(extcon_register_notifier);
939 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
940 * @edev: the extcon device that has the external connecotr.
941 * @id: the unique id of each external connector in extcon enumeration.
942 * @nb: a notifier block to be registered.
944 int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
945 struct notifier_block *nb)
953 idx = find_cable_index_by_id(edev, id);
957 spin_lock_irqsave(&edev->lock, flags);
958 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
959 spin_unlock_irqrestore(&edev->lock, flags);
963 EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
966 * extcon_register_notifier_all() - Register a notifier block for all connectors
967 * @edev: the extcon device that has the external connector.
968 * @nb: a notifier block to be registered.
970 * This function registers a notifier block in order to receive the state
971 * change of all supported external connectors from extcon device.
972 * And the second parameter given to the callback of nb (val) is
973 * the current state and third parameter is the edev pointer.
975 * Returns 0 if success or error number if fail
977 int extcon_register_notifier_all(struct extcon_dev *edev,
978 struct notifier_block *nb)
986 spin_lock_irqsave(&edev->lock, flags);
987 ret = raw_notifier_chain_register(&edev->nh_all, nb);
988 spin_unlock_irqrestore(&edev->lock, flags);
992 EXPORT_SYMBOL_GPL(extcon_register_notifier_all);
995 * extcon_unregister_notifier_all() - Unregister a notifier block from extcon.
996 * @edev: the extcon device that has the external connecotr.
997 * @nb: a notifier block to be registered.
999 * Returns 0 if success or error number if fail
1001 int extcon_unregister_notifier_all(struct extcon_dev *edev,
1002 struct notifier_block *nb)
1004 unsigned long flags;
1010 spin_lock_irqsave(&edev->lock, flags);
1011 ret = raw_notifier_chain_unregister(&edev->nh_all, nb);
1012 spin_unlock_irqrestore(&edev->lock, flags);
1016 EXPORT_SYMBOL_GPL(extcon_unregister_notifier_all);
1018 static struct attribute *extcon_attrs[] = {
1019 &dev_attr_state.attr,
1020 &dev_attr_name.attr,
1023 ATTRIBUTE_GROUPS(extcon);
1025 static int create_extcon_class(void)
1027 if (!extcon_class) {
1028 extcon_class = class_create(THIS_MODULE, "extcon");
1029 if (IS_ERR(extcon_class))
1030 return PTR_ERR(extcon_class);
1031 extcon_class->dev_groups = extcon_groups;
1037 static void extcon_dev_release(struct device *dev)
1041 static const char *muex_name = "mutually_exclusive";
1042 static void dummy_sysfs_dev_release(struct device *dev)
1047 * extcon_dev_allocate() - Allocate the memory of extcon device.
1048 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
1049 * If supported_cable is NULL, cable name related APIs
1052 * This function allocates the memory for extcon device without allocating
1053 * memory in each extcon provider driver and initialize default setting for
1056 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
1058 struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
1060 struct extcon_dev *edev;
1062 if (!supported_cable)
1063 return ERR_PTR(-EINVAL);
1065 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
1067 return ERR_PTR(-ENOMEM);
1069 edev->max_supported = 0;
1070 edev->supported_cable = supported_cable;
1076 * extcon_dev_free() - Free the memory of extcon device.
1077 * @edev: the extcon device to free
1079 void extcon_dev_free(struct extcon_dev *edev)
1083 EXPORT_SYMBOL_GPL(extcon_dev_free);
1086 * extcon_dev_register() - Register a new extcon device
1087 * @edev : the new extcon device (should be allocated before calling)
1089 * Among the members of edev struct, please set the "user initializing data"
1090 * in any case and set the "optional callbacks" if required. However, please
1091 * do not set the values of "internal data", which are initialized by
1094 int extcon_dev_register(struct extcon_dev *edev)
1097 static atomic_t edev_no = ATOMIC_INIT(-1);
1099 if (!extcon_class) {
1100 ret = create_extcon_class();
1105 if (!edev || !edev->supported_cable)
1108 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
1110 edev->max_supported = index;
1111 if (index > SUPPORTED_CABLE_MAX) {
1113 "exceed the maximum number of supported cables\n");
1117 edev->dev.class = extcon_class;
1118 edev->dev.release = extcon_dev_release;
1120 edev->name = dev_name(edev->dev.parent);
1121 if (IS_ERR_OR_NULL(edev->name)) {
1123 "extcon device name is null\n");
1126 dev_set_name(&edev->dev, "extcon%lu",
1127 (unsigned long)atomic_inc_return(&edev_no));
1129 if (edev->max_supported) {
1132 struct extcon_cable *cable;
1134 edev->cables = kzalloc(sizeof(struct extcon_cable) *
1135 edev->max_supported, GFP_KERNEL);
1136 if (!edev->cables) {
1138 goto err_sysfs_alloc;
1140 for (index = 0; index < edev->max_supported; index++) {
1141 cable = &edev->cables[index];
1143 snprintf(buf, 10, "cable.%d", index);
1144 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
1147 for (index--; index >= 0; index--) {
1148 cable = &edev->cables[index];
1149 kfree(cable->attr_g.name);
1153 goto err_alloc_cables;
1158 cable->cable_index = index;
1159 cable->attrs[0] = &cable->attr_name.attr;
1160 cable->attrs[1] = &cable->attr_state.attr;
1161 cable->attrs[2] = NULL;
1162 cable->attr_g.name = str;
1163 cable->attr_g.attrs = cable->attrs;
1165 sysfs_attr_init(&cable->attr_name.attr);
1166 cable->attr_name.attr.name = "name";
1167 cable->attr_name.attr.mode = 0444;
1168 cable->attr_name.show = cable_name_show;
1170 sysfs_attr_init(&cable->attr_state.attr);
1171 cable->attr_state.attr.name = "state";
1172 cable->attr_state.attr.mode = 0444;
1173 cable->attr_state.show = cable_state_show;
1177 if (edev->max_supported && edev->mutually_exclusive) {
1181 /* Count the size of mutually_exclusive array */
1182 for (index = 0; edev->mutually_exclusive[index]; index++)
1185 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
1186 (index + 1), GFP_KERNEL);
1187 if (!edev->attrs_muex) {
1192 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
1194 if (!edev->d_attrs_muex) {
1196 kfree(edev->attrs_muex);
1200 for (index = 0; edev->mutually_exclusive[index]; index++) {
1201 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
1202 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
1205 for (index--; index >= 0; index--) {
1206 kfree(edev->d_attrs_muex[index].attr.
1209 kfree(edev->d_attrs_muex);
1210 kfree(edev->attrs_muex);
1215 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
1216 edev->d_attrs_muex[index].attr.name = name;
1217 edev->d_attrs_muex[index].attr.mode = 0000;
1218 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
1221 edev->attr_g_muex.name = muex_name;
1222 edev->attr_g_muex.attrs = edev->attrs_muex;
1226 if (edev->max_supported) {
1227 edev->extcon_dev_type.groups =
1228 kzalloc(sizeof(struct attribute_group *) *
1229 (edev->max_supported + 2), GFP_KERNEL);
1230 if (!edev->extcon_dev_type.groups) {
1232 goto err_alloc_groups;
1235 edev->extcon_dev_type.name = dev_name(&edev->dev);
1236 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
1238 for (index = 0; index < edev->max_supported; index++)
1239 edev->extcon_dev_type.groups[index] =
1240 &edev->cables[index].attr_g;
1241 if (edev->mutually_exclusive)
1242 edev->extcon_dev_type.groups[index] =
1245 edev->dev.type = &edev->extcon_dev_type;
1248 ret = device_register(&edev->dev);
1250 put_device(&edev->dev);
1254 spin_lock_init(&edev->lock);
1255 edev->nh = devm_kcalloc(&edev->dev, edev->max_supported,
1256 sizeof(*edev->nh), GFP_KERNEL);
1262 for (index = 0; index < edev->max_supported; index++)
1263 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
1265 RAW_INIT_NOTIFIER_HEAD(&edev->nh_all);
1267 dev_set_drvdata(&edev->dev, edev);
1270 mutex_lock(&extcon_dev_list_lock);
1271 list_add(&edev->entry, &extcon_dev_list);
1272 mutex_unlock(&extcon_dev_list_lock);
1277 if (edev->max_supported)
1278 kfree(edev->extcon_dev_type.groups);
1280 if (edev->max_supported && edev->mutually_exclusive) {
1281 for (index = 0; edev->mutually_exclusive[index]; index++)
1282 kfree(edev->d_attrs_muex[index].attr.name);
1283 kfree(edev->d_attrs_muex);
1284 kfree(edev->attrs_muex);
1287 for (index = 0; index < edev->max_supported; index++)
1288 kfree(edev->cables[index].attr_g.name);
1290 if (edev->max_supported)
1291 kfree(edev->cables);
1295 EXPORT_SYMBOL_GPL(extcon_dev_register);
1298 * extcon_dev_unregister() - Unregister the extcon device.
1299 * @edev: the extcon device instance to be unregistered.
1301 * Note that this does not call kfree(edev) because edev was not allocated
1304 void extcon_dev_unregister(struct extcon_dev *edev)
1311 mutex_lock(&extcon_dev_list_lock);
1312 list_del(&edev->entry);
1313 mutex_unlock(&extcon_dev_list_lock);
1315 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
1316 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
1317 dev_name(&edev->dev));
1321 device_unregister(&edev->dev);
1323 if (edev->mutually_exclusive && edev->max_supported) {
1324 for (index = 0; edev->mutually_exclusive[index];
1326 kfree(edev->d_attrs_muex[index].attr.name);
1327 kfree(edev->d_attrs_muex);
1328 kfree(edev->attrs_muex);
1331 for (index = 0; index < edev->max_supported; index++)
1332 kfree(edev->cables[index].attr_g.name);
1334 if (edev->max_supported) {
1335 kfree(edev->extcon_dev_type.groups);
1336 kfree(edev->cables);
1339 put_device(&edev->dev);
1341 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1345 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1346 * @dev - instance to the given device
1347 * @index - index into list of extcon_dev
1349 * return the instance of extcon device
1351 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1353 struct device_node *node;
1354 struct extcon_dev *edev;
1357 return ERR_PTR(-EINVAL);
1359 if (!dev->of_node) {
1360 dev_dbg(dev, "device does not have a device node entry\n");
1361 return ERR_PTR(-EINVAL);
1364 node = of_parse_phandle(dev->of_node, "extcon", index);
1366 dev_dbg(dev, "failed to get phandle in %s node\n",
1367 dev->of_node->full_name);
1368 return ERR_PTR(-ENODEV);
1371 mutex_lock(&extcon_dev_list_lock);
1372 list_for_each_entry(edev, &extcon_dev_list, entry) {
1373 if (edev->dev.parent && edev->dev.parent->of_node == node) {
1374 mutex_unlock(&extcon_dev_list_lock);
1379 mutex_unlock(&extcon_dev_list_lock);
1382 return ERR_PTR(-EPROBE_DEFER);
1385 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1387 return ERR_PTR(-ENOSYS);
1389 #endif /* CONFIG_OF */
1390 EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1393 * extcon_get_edev_name() - Get the name of the extcon device.
1394 * @edev: the extcon device
1396 const char *extcon_get_edev_name(struct extcon_dev *edev)
1398 return !edev ? NULL : edev->name;
1401 static int __init extcon_class_init(void)
1403 return create_extcon_class();
1405 module_init(extcon_class_init);
1407 static void __exit extcon_class_exit(void)
1409 class_destroy(extcon_class);
1411 module_exit(extcon_class_exit);
1417 MODULE_DESCRIPTION("External connector (extcon) class driver");
1418 MODULE_LICENSE("GPL");