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,
64 [EXTCON_USB_VBUS_EN] = {
65 .type = EXTCON_TYPE_USB,
66 .id = EXTCON_USB_VBUS_EN,
67 .name = "USB_VBUS_EN",
70 /* Charging external connector */
71 [EXTCON_CHG_USB_SDP] = {
72 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
73 .id = EXTCON_CHG_USB_SDP,
76 [EXTCON_CHG_USB_DCP] = {
77 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
78 .id = EXTCON_CHG_USB_DCP,
81 [EXTCON_CHG_USB_CDP] = {
82 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
83 .id = EXTCON_CHG_USB_CDP,
86 [EXTCON_CHG_USB_ACA] = {
87 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
88 .id = EXTCON_CHG_USB_ACA,
91 [EXTCON_CHG_USB_FAST] = {
92 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
93 .id = EXTCON_CHG_USB_FAST,
94 .name = "FAST-CHARGER",
96 [EXTCON_CHG_USB_SLOW] = {
97 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
98 .id = EXTCON_CHG_USB_SLOW,
99 .name = "SLOW-CHARGER",
102 /* Jack external connector */
103 [EXTCON_JACK_MICROPHONE] = {
104 .type = EXTCON_TYPE_JACK,
105 .id = EXTCON_JACK_MICROPHONE,
106 .name = "MICROPHONE",
108 [EXTCON_JACK_HEADPHONE] = {
109 .type = EXTCON_TYPE_JACK,
110 .id = EXTCON_JACK_HEADPHONE,
113 [EXTCON_JACK_LINE_IN] = {
114 .type = EXTCON_TYPE_JACK,
115 .id = EXTCON_JACK_LINE_IN,
118 [EXTCON_JACK_LINE_OUT] = {
119 .type = EXTCON_TYPE_JACK,
120 .id = EXTCON_JACK_LINE_OUT,
123 [EXTCON_JACK_VIDEO_IN] = {
124 .type = EXTCON_TYPE_JACK,
125 .id = EXTCON_JACK_VIDEO_IN,
128 [EXTCON_JACK_VIDEO_OUT] = {
129 .type = EXTCON_TYPE_JACK,
130 .id = EXTCON_JACK_VIDEO_OUT,
133 [EXTCON_JACK_SPDIF_IN] = {
134 .type = EXTCON_TYPE_JACK,
135 .id = EXTCON_JACK_SPDIF_IN,
138 [EXTCON_JACK_SPDIF_OUT] = {
139 .type = EXTCON_TYPE_JACK,
140 .id = EXTCON_JACK_SPDIF_OUT,
144 /* Display external connector */
145 [EXTCON_DISP_HDMI] = {
146 .type = EXTCON_TYPE_DISP,
147 .id = EXTCON_DISP_HDMI,
150 [EXTCON_DISP_MHL] = {
151 .type = EXTCON_TYPE_DISP,
152 .id = EXTCON_DISP_MHL,
155 [EXTCON_DISP_DVI] = {
156 .type = EXTCON_TYPE_DISP,
157 .id = EXTCON_DISP_DVI,
160 [EXTCON_DISP_VGA] = {
161 .type = EXTCON_TYPE_DISP,
162 .id = EXTCON_DISP_VGA,
166 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
167 .id = EXTCON_DISP_DP,
171 /* Miscellaneous external connector */
173 .type = EXTCON_TYPE_MISC,
178 .type = EXTCON_TYPE_MISC,
182 [EXTCON_MECHANICAL] = {
183 .type = EXTCON_TYPE_MISC,
184 .id = EXTCON_MECHANICAL,
185 .name = "MECHANICAL",
192 * struct extcon_cable - An internal data for each cable of extcon device.
193 * @edev: The extcon device
194 * @cable_index: Index of this cable in the edev
195 * @attr_g: Attribute group for the cable
196 * @attr_name: "name" sysfs entry
197 * @attr_state: "state" sysfs entry
198 * @attrs: Array pointing to attr_name and attr_state for attr_g
200 struct extcon_cable {
201 struct extcon_dev *edev;
204 struct attribute_group attr_g;
205 struct device_attribute attr_name;
206 struct device_attribute attr_state;
208 struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
210 union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
211 union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
212 union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
213 union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
215 unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
216 unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
217 unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
218 unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
221 static struct class *extcon_class;
222 #if defined(CONFIG_ANDROID)
223 static struct class_compat *switch_class;
224 #endif /* CONFIG_ANDROID */
226 static LIST_HEAD(extcon_dev_list);
227 static DEFINE_MUTEX(extcon_dev_list_lock);
230 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
232 * @edev: the extcon device
233 * @new_state: new cable attach status for @edev
235 * Returns 0 if nothing violates. Returns the index + 1 for the first
236 * violated condition.
238 static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
242 if (!edev->mutually_exclusive)
245 for (i = 0; edev->mutually_exclusive[i]; i++) {
247 u32 correspondants = new_state & edev->mutually_exclusive[i];
249 /* calculate the total number of bits set */
250 weight = hweight32(correspondants);
258 static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
262 /* Find the the index of extcon cable in edev->supported_cable */
263 for (i = 0; i < edev->max_supported; i++) {
264 if (edev->supported_cable[i] == id)
271 static int get_extcon_type(unsigned int prop)
274 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
275 return EXTCON_TYPE_USB;
276 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
277 return EXTCON_TYPE_CHG;
278 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
279 return EXTCON_TYPE_JACK;
280 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
281 return EXTCON_TYPE_DISP;
287 static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
289 return !!(edev->state & BIT(index));
292 static bool is_extcon_changed(struct extcon_dev *edev, int index,
295 int state = !!(edev->state & BIT(index));
296 return (state != new_state);
299 static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
303 /* Check whether the property is supported or not. */
304 type = get_extcon_type(prop);
308 /* Check whether a specific extcon id supports the property or not. */
309 return !!(extcon_info[id].type & type);
312 static int is_extcon_property_capability(struct extcon_dev *edev,
313 unsigned int id, int index,unsigned int prop)
315 struct extcon_cable *cable;
318 /* Check whether the property is supported or not. */
319 type = get_extcon_type(prop);
323 cable = &edev->cables[index];
326 case EXTCON_TYPE_USB:
327 ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
329 case EXTCON_TYPE_CHG:
330 ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
332 case EXTCON_TYPE_JACK:
333 ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
335 case EXTCON_TYPE_DISP:
336 ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
345 static void init_property(struct extcon_dev *edev, unsigned int id, int index)
347 unsigned int type = extcon_info[id].type;
348 struct extcon_cable *cable = &edev->cables[index];
350 if (EXTCON_TYPE_USB & type)
351 memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
352 if (EXTCON_TYPE_CHG & type)
353 memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
354 if (EXTCON_TYPE_JACK & type)
355 memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
356 if (EXTCON_TYPE_DISP & type)
357 memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
360 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
364 struct extcon_dev *edev = dev_get_drvdata(dev);
366 if (edev->max_supported == 0)
367 return sprintf(buf, "%u\n", edev->state);
369 for (i = 0; i < edev->max_supported; i++) {
370 count += sprintf(buf + count, "%s=%d\n",
371 extcon_info[edev->supported_cable[i]].name,
372 !!(edev->state & (1 << i)));
377 static DEVICE_ATTR_RO(state);
379 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
382 struct extcon_dev *edev = dev_get_drvdata(dev);
384 return sprintf(buf, "%s\n", edev->name);
386 static DEVICE_ATTR_RO(name);
388 static ssize_t cable_name_show(struct device *dev,
389 struct device_attribute *attr, char *buf)
391 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
393 int i = cable->cable_index;
395 return sprintf(buf, "%s\n",
396 extcon_info[cable->edev->supported_cable[i]].name);
399 static ssize_t cable_state_show(struct device *dev,
400 struct device_attribute *attr, char *buf)
402 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
405 int i = cable->cable_index;
407 return sprintf(buf, "%d\n",
408 extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
412 * extcon_sync() - Synchronize the states for both the attached/detached
413 * @edev: the extcon device that has the cable.
415 * This function send a notification to synchronize the all states of a
416 * specific external connector
418 int extcon_sync(struct extcon_dev *edev, unsigned int id)
433 index = find_cable_index_by_id(edev, id);
437 spin_lock_irqsave(&edev->lock, flags);
439 state = !!(edev->state & BIT(index));
440 raw_notifier_call_chain(&edev->nh[index], state, edev);
442 /* This could be in interrupt handler */
443 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
445 /* Unlock early before uevent */
446 spin_unlock_irqrestore(&edev->lock, flags);
448 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
449 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
454 length = name_show(&edev->dev, NULL, prop_buf);
456 if (prop_buf[length - 1] == '\n')
457 prop_buf[length - 1] = 0;
458 snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
459 envp[env_offset++] = name_buf;
462 length = state_show(&edev->dev, NULL, prop_buf);
464 if (prop_buf[length - 1] == '\n')
465 prop_buf[length - 1] = 0;
466 snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
467 envp[env_offset++] = state_buf;
469 envp[env_offset] = NULL;
471 /* Unlock early before uevent */
472 spin_unlock_irqrestore(&edev->lock, flags);
473 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
474 free_page((unsigned long)prop_buf);
478 EXPORT_SYMBOL_GPL(extcon_sync);
481 * extcon_get_state() - Get the state of a external connector.
482 * @edev: the extcon device that has the cable.
483 * @id: the unique id of each external connector in extcon enumeration.
485 int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
493 index = find_cable_index_by_id(edev, id);
497 spin_lock_irqsave(&edev->lock, flags);
498 state = is_extcon_attached(edev, index);
499 spin_unlock_irqrestore(&edev->lock, flags);
503 EXPORT_SYMBOL_GPL(extcon_get_state);
506 * extcon_set_state() - Set the state of a external connector.
507 * without a notification.
508 * @edev: the extcon device that has the cable.
509 * @id: the unique id of each external connector
510 * in extcon enumeration.
511 * @state: the new cable status. The default semantics is
512 * true: attached / false: detached.
514 * This function only set the state of a external connector without
515 * a notification. To synchronize the data of a external connector,
516 * use extcon_set_state_sync() and extcon_sync().
518 int extcon_set_state(struct extcon_dev *edev, unsigned int id,
527 index = find_cable_index_by_id(edev, id);
531 spin_lock_irqsave(&edev->lock, flags);
533 /* Check whether the external connector's state is changed. */
534 if (!is_extcon_changed(edev, index, cable_state))
537 if (check_mutually_exclusive(edev,
538 (edev->state & ~BIT(index)) | (cable_state & BIT(index)))) {
544 * Initialize the value of extcon property before setting
545 * the detached state for an external connector.
548 init_property(edev, id, index);
550 /* Update the state for a external connector. */
552 edev->state |= BIT(index);
554 edev->state &= ~(BIT(index));
556 spin_unlock_irqrestore(&edev->lock, flags);
560 EXPORT_SYMBOL_GPL(extcon_set_state);
563 * extcon_set_state_sync() - Set the state of a external connector
564 * with a notification.
565 * @edev: the extcon device that has the cable.
566 * @id: the unique id of each external connector
567 * in extcon enumeration.
568 * @state: the new cable status. The default semantics is
569 * true: attached / false: detached.
571 * This function set the state of external connector and synchronize the data
572 * by usning a notification.
574 int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id,
580 index = find_cable_index_by_id(edev, id);
584 /* Check whether the external connector's state is changed. */
585 spin_lock_irqsave(&edev->lock, flags);
586 ret = is_extcon_changed(edev, index, cable_state);
587 spin_unlock_irqrestore(&edev->lock, flags);
591 ret = extcon_set_state(edev, id, cable_state);
595 return extcon_sync(edev, id);
597 EXPORT_SYMBOL_GPL(extcon_set_state_sync);
600 * extcon_get_property() - Get the property value of a specific cable.
601 * @edev: the extcon device that has the cable.
602 * @id: the unique id of each external connector
603 * in extcon enumeration.
604 * @prop: the property id among enum extcon_property.
605 * @prop_val: the pointer which store the value of property.
607 * When getting the property value of external connector, the external connector
608 * should be attached. If detached state, function just return 0 without
609 * property value. Also, the each property should be included in the list of
610 * supported properties according to the type of external connectors.
612 * Returns 0 if success or error number if fail
614 int extcon_get_property(struct extcon_dev *edev, unsigned int id,
616 union extcon_property_value *prop_val)
618 struct extcon_cable *cable;
622 *prop_val = (union extcon_property_value)(0);
627 /* Check whether the property is supported or not */
628 if (!is_extcon_property_supported(id, prop))
631 /* Find the cable index of external connector by using id */
632 index = find_cable_index_by_id(edev, id);
636 spin_lock_irqsave(&edev->lock, flags);
638 /* Check whether the property is available or not. */
639 if (!is_extcon_property_capability(edev, id, index, prop)) {
640 spin_unlock_irqrestore(&edev->lock, flags);
645 * Check whether the external connector is attached.
646 * If external connector is detached, the user can not
647 * get the property value.
649 if (!is_extcon_attached(edev, index)) {
650 spin_unlock_irqrestore(&edev->lock, flags);
654 cable = &edev->cables[index];
656 /* Get the property value according to extcon type */
658 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
659 *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
661 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
662 *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
664 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
665 *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
667 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
668 *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
675 spin_unlock_irqrestore(&edev->lock, flags);
679 EXPORT_SYMBOL_GPL(extcon_get_property);
682 * extcon_set_property() - Set the property value of a specific cable.
683 * @edev: the extcon device that has the cable.
684 * @id: the unique id of each external connector
685 * in extcon enumeration.
686 * @prop: the property id among enum extcon_property.
687 * @prop_val: the pointer including the new value of property.
689 * The each property should be included in the list of supported properties
690 * according to the type of external connectors.
692 * Returns 0 if success or error number if fail
694 int extcon_set_property(struct extcon_dev *edev, unsigned int id,
696 union extcon_property_value prop_val)
698 struct extcon_cable *cable;
705 /* Check whether the property is supported or not */
706 if (!is_extcon_property_supported(id, prop))
709 /* Find the cable index of external connector by using id */
710 index = find_cable_index_by_id(edev, id);
714 spin_lock_irqsave(&edev->lock, flags);
716 /* Check whether the property is available or not. */
717 if (!is_extcon_property_capability(edev, id, index, prop)) {
718 spin_unlock_irqrestore(&edev->lock, flags);
722 cable = &edev->cables[index];
724 /* Set the property value according to extcon type */
726 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
727 cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
729 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
730 cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
732 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
733 cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
735 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
736 cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
743 spin_unlock_irqrestore(&edev->lock, flags);
747 EXPORT_SYMBOL_GPL(extcon_set_property);
750 * extcon_set_property_sync() - Set the property value of a specific cable
752 * @prop_val: the pointer including the new value of property.
754 * When setting the property value of external connector, the external connector
755 * should be attached. The each property should be included in the list of
756 * supported properties according to the type of external connectors.
758 * Returns 0 if success or error number if fail
760 int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
762 union extcon_property_value prop_val)
766 ret = extcon_set_property(edev, id, prop, prop_val);
770 return extcon_sync(edev, id);
772 EXPORT_SYMBOL_GPL(extcon_set_property_sync);
775 * extcon_get_property_capability() - Get the capability of property
776 * of an external connector.
777 * @edev: the extcon device that has the cable.
778 * @id: the unique id of each external connector
779 * in extcon enumeration.
780 * @prop: the property id among enum extcon_property.
782 * Returns 1 if the property is available or 0 if not available.
784 int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
792 /* Check whether the property is supported or not */
793 if (!is_extcon_property_supported(id, prop))
796 /* Find the cable index of external connector by using id */
797 index = find_cable_index_by_id(edev, id);
801 return is_extcon_property_capability(edev, id, index, prop);
803 EXPORT_SYMBOL_GPL(extcon_get_property_capability);
806 * extcon_set_property_capability() - Set the capability of a property
807 * of an external connector.
808 * @edev: the extcon device that has the cable.
809 * @id: the unique id of each external connector
810 * in extcon enumeration.
811 * @prop: the property id among enum extcon_property.
813 * This function set the capability of a property for an external connector
814 * to mark the bit in capability bitmap which mean the available state of
817 * Returns 0 if success or error number if fail
819 int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
822 struct extcon_cable *cable;
823 int index, type, ret = 0;
828 /* Check whether the property is supported or not. */
829 if (!is_extcon_property_supported(id, prop))
832 /* Find the cable index of external connector by using id. */
833 index = find_cable_index_by_id(edev, id);
837 /* Check whether the property is supported or not. */
838 type = get_extcon_type(prop);
842 cable = &edev->cables[index];
845 case EXTCON_TYPE_USB:
846 __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
848 case EXTCON_TYPE_CHG:
849 __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
851 case EXTCON_TYPE_JACK:
852 __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
854 case EXTCON_TYPE_DISP:
855 __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
863 EXPORT_SYMBOL_GPL(extcon_set_property_capability);
866 * extcon_get_extcon_dev() - Get the extcon device instance from the name
867 * @extcon_name: The extcon name provided with extcon_dev_register()
869 struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
871 struct extcon_dev *sd;
874 return ERR_PTR(-EINVAL);
876 mutex_lock(&extcon_dev_list_lock);
877 list_for_each_entry(sd, &extcon_dev_list, entry) {
878 if (!strcmp(sd->name, extcon_name))
883 mutex_unlock(&extcon_dev_list_lock);
886 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
889 * extcon_register_notifier() - Register a notifiee to get notified by
890 * any attach status changes from the extcon.
891 * @edev: the extcon device that has the external connecotr.
892 * @id: the unique id of each external connector in extcon enumeration.
893 * @nb: a notifier block to be registered.
895 * Note that the second parameter given to the callback of nb (val) is
896 * "old_state", not the current state. The current state can be retrieved
897 * by looking at the third pameter (edev pointer)'s state value.
899 int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
900 struct notifier_block *nb)
909 idx = find_cable_index_by_id(edev, id);
913 spin_lock_irqsave(&edev->lock, flags);
914 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
915 spin_unlock_irqrestore(&edev->lock, flags);
917 struct extcon_dev *extd;
919 mutex_lock(&extcon_dev_list_lock);
920 list_for_each_entry(extd, &extcon_dev_list, entry) {
921 idx = find_cable_index_by_id(extd, id);
925 mutex_unlock(&extcon_dev_list_lock);
929 return extcon_register_notifier(extd, id, nb);
937 EXPORT_SYMBOL_GPL(extcon_register_notifier);
940 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
941 * @edev: the extcon device that has the external connecotr.
942 * @id: the unique id of each external connector in extcon enumeration.
943 * @nb: a notifier block to be registered.
945 int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
946 struct notifier_block *nb)
954 idx = find_cable_index_by_id(edev, id);
958 spin_lock_irqsave(&edev->lock, flags);
959 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
960 spin_unlock_irqrestore(&edev->lock, flags);
964 EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
966 static struct attribute *extcon_attrs[] = {
967 &dev_attr_state.attr,
971 ATTRIBUTE_GROUPS(extcon);
973 static int create_extcon_class(void)
976 extcon_class = class_create(THIS_MODULE, "extcon");
977 if (IS_ERR(extcon_class))
978 return PTR_ERR(extcon_class);
979 extcon_class->dev_groups = extcon_groups;
981 #if defined(CONFIG_ANDROID) && !defined(CONFIG_SWITCH)
982 switch_class = class_compat_register("switch");
983 if (WARN(!switch_class, "cannot allocate"))
985 #endif /* CONFIG_ANDROID */
991 static void extcon_dev_release(struct device *dev)
995 static const char *muex_name = "mutually_exclusive";
996 static void dummy_sysfs_dev_release(struct device *dev)
1001 * extcon_dev_allocate() - Allocate the memory of extcon device.
1002 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
1003 * If supported_cable is NULL, cable name related APIs
1006 * This function allocates the memory for extcon device without allocating
1007 * memory in each extcon provider driver and initialize default setting for
1010 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
1012 struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
1014 struct extcon_dev *edev;
1016 if (!supported_cable)
1017 return ERR_PTR(-EINVAL);
1019 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
1021 return ERR_PTR(-ENOMEM);
1023 edev->max_supported = 0;
1024 edev->supported_cable = supported_cable;
1030 * extcon_dev_free() - Free the memory of extcon device.
1031 * @edev: the extcon device to free
1033 void extcon_dev_free(struct extcon_dev *edev)
1037 EXPORT_SYMBOL_GPL(extcon_dev_free);
1040 * extcon_dev_register() - Register a new extcon device
1041 * @edev : the new extcon device (should be allocated before calling)
1043 * Among the members of edev struct, please set the "user initializing data"
1044 * in any case and set the "optional callbacks" if required. However, please
1045 * do not set the values of "internal data", which are initialized by
1048 int extcon_dev_register(struct extcon_dev *edev)
1051 static atomic_t edev_no = ATOMIC_INIT(-1);
1053 if (!extcon_class) {
1054 ret = create_extcon_class();
1059 if (!edev || !edev->supported_cable)
1062 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
1064 edev->max_supported = index;
1065 if (index > SUPPORTED_CABLE_MAX) {
1067 "exceed the maximum number of supported cables\n");
1071 edev->dev.class = extcon_class;
1072 edev->dev.release = extcon_dev_release;
1074 edev->name = dev_name(edev->dev.parent);
1075 if (IS_ERR_OR_NULL(edev->name)) {
1077 "extcon device name is null\n");
1080 dev_set_name(&edev->dev, "extcon%lu",
1081 (unsigned long)atomic_inc_return(&edev_no));
1083 if (edev->max_supported) {
1086 struct extcon_cable *cable;
1088 edev->cables = kzalloc(sizeof(struct extcon_cable) *
1089 edev->max_supported, GFP_KERNEL);
1090 if (!edev->cables) {
1092 goto err_sysfs_alloc;
1094 for (index = 0; index < edev->max_supported; index++) {
1095 cable = &edev->cables[index];
1097 snprintf(buf, 10, "cable.%d", index);
1098 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
1101 for (index--; index >= 0; index--) {
1102 cable = &edev->cables[index];
1103 kfree(cable->attr_g.name);
1107 goto err_alloc_cables;
1112 cable->cable_index = index;
1113 cable->attrs[0] = &cable->attr_name.attr;
1114 cable->attrs[1] = &cable->attr_state.attr;
1115 cable->attrs[2] = NULL;
1116 cable->attr_g.name = str;
1117 cable->attr_g.attrs = cable->attrs;
1119 sysfs_attr_init(&cable->attr_name.attr);
1120 cable->attr_name.attr.name = "name";
1121 cable->attr_name.attr.mode = 0444;
1122 cable->attr_name.show = cable_name_show;
1124 sysfs_attr_init(&cable->attr_state.attr);
1125 cable->attr_state.attr.name = "state";
1126 cable->attr_state.attr.mode = 0444;
1127 cable->attr_state.show = cable_state_show;
1131 if (edev->max_supported && edev->mutually_exclusive) {
1135 /* Count the size of mutually_exclusive array */
1136 for (index = 0; edev->mutually_exclusive[index]; index++)
1139 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
1140 (index + 1), GFP_KERNEL);
1141 if (!edev->attrs_muex) {
1146 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
1148 if (!edev->d_attrs_muex) {
1150 kfree(edev->attrs_muex);
1154 for (index = 0; edev->mutually_exclusive[index]; index++) {
1155 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
1156 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
1159 for (index--; index >= 0; index--) {
1160 kfree(edev->d_attrs_muex[index].attr.
1163 kfree(edev->d_attrs_muex);
1164 kfree(edev->attrs_muex);
1169 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
1170 edev->d_attrs_muex[index].attr.name = name;
1171 edev->d_attrs_muex[index].attr.mode = 0000;
1172 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
1175 edev->attr_g_muex.name = muex_name;
1176 edev->attr_g_muex.attrs = edev->attrs_muex;
1180 if (edev->max_supported) {
1181 edev->extcon_dev_type.groups =
1182 kzalloc(sizeof(struct attribute_group *) *
1183 (edev->max_supported + 2), GFP_KERNEL);
1184 if (!edev->extcon_dev_type.groups) {
1186 goto err_alloc_groups;
1189 edev->extcon_dev_type.name = dev_name(&edev->dev);
1190 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
1192 for (index = 0; index < edev->max_supported; index++)
1193 edev->extcon_dev_type.groups[index] =
1194 &edev->cables[index].attr_g;
1195 if (edev->mutually_exclusive)
1196 edev->extcon_dev_type.groups[index] =
1199 edev->dev.type = &edev->extcon_dev_type;
1202 ret = device_register(&edev->dev);
1204 put_device(&edev->dev);
1207 #if defined(CONFIG_ANDROID)
1209 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
1210 #endif /* CONFIG_ANDROID */
1212 spin_lock_init(&edev->lock);
1214 edev->nh = devm_kzalloc(&edev->dev,
1215 sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
1221 for (index = 0; index < edev->max_supported; index++)
1222 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
1224 dev_set_drvdata(&edev->dev, edev);
1227 mutex_lock(&extcon_dev_list_lock);
1228 list_add(&edev->entry, &extcon_dev_list);
1229 mutex_unlock(&extcon_dev_list_lock);
1234 if (edev->max_supported)
1235 kfree(edev->extcon_dev_type.groups);
1237 if (edev->max_supported && edev->mutually_exclusive) {
1238 for (index = 0; edev->mutually_exclusive[index]; index++)
1239 kfree(edev->d_attrs_muex[index].attr.name);
1240 kfree(edev->d_attrs_muex);
1241 kfree(edev->attrs_muex);
1244 for (index = 0; index < edev->max_supported; index++)
1245 kfree(edev->cables[index].attr_g.name);
1247 if (edev->max_supported)
1248 kfree(edev->cables);
1252 EXPORT_SYMBOL_GPL(extcon_dev_register);
1255 * extcon_dev_unregister() - Unregister the extcon device.
1256 * @edev: the extcon device instance to be unregistered.
1258 * Note that this does not call kfree(edev) because edev was not allocated
1261 void extcon_dev_unregister(struct extcon_dev *edev)
1268 mutex_lock(&extcon_dev_list_lock);
1269 list_del(&edev->entry);
1270 mutex_unlock(&extcon_dev_list_lock);
1272 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
1273 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
1274 dev_name(&edev->dev));
1278 device_unregister(&edev->dev);
1280 if (edev->mutually_exclusive && edev->max_supported) {
1281 for (index = 0; edev->mutually_exclusive[index];
1283 kfree(edev->d_attrs_muex[index].attr.name);
1284 kfree(edev->d_attrs_muex);
1285 kfree(edev->attrs_muex);
1288 for (index = 0; index < edev->max_supported; index++)
1289 kfree(edev->cables[index].attr_g.name);
1291 if (edev->max_supported) {
1292 kfree(edev->extcon_dev_type.groups);
1293 kfree(edev->cables);
1296 #if defined(CONFIG_ANDROID)
1298 class_compat_remove_link(switch_class, &edev->dev, NULL);
1300 put_device(&edev->dev);
1302 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1306 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1307 * @dev - instance to the given device
1308 * @index - index into list of extcon_dev
1310 * return the instance of extcon device
1312 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1314 struct device_node *node;
1315 struct extcon_dev *edev;
1318 return ERR_PTR(-EINVAL);
1320 if (!dev->of_node) {
1321 dev_dbg(dev, "device does not have a device node entry\n");
1322 return ERR_PTR(-EINVAL);
1325 node = of_parse_phandle(dev->of_node, "extcon", index);
1327 dev_dbg(dev, "failed to get phandle in %s node\n",
1328 dev->of_node->full_name);
1329 return ERR_PTR(-ENODEV);
1332 mutex_lock(&extcon_dev_list_lock);
1333 list_for_each_entry(edev, &extcon_dev_list, entry) {
1334 if (edev->dev.parent && edev->dev.parent->of_node == node) {
1335 mutex_unlock(&extcon_dev_list_lock);
1340 mutex_unlock(&extcon_dev_list_lock);
1343 return ERR_PTR(-EPROBE_DEFER);
1346 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1348 return ERR_PTR(-ENOSYS);
1350 #endif /* CONFIG_OF */
1351 EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1354 * extcon_get_edev_name() - Get the name of the extcon device.
1355 * @edev: the extcon device
1357 const char *extcon_get_edev_name(struct extcon_dev *edev)
1359 return !edev ? NULL : edev->name;
1362 static int __init extcon_class_init(void)
1364 return create_extcon_class();
1366 module_init(extcon_class_init);
1368 static void __exit extcon_class_exit(void)
1370 #if defined(CONFIG_ANDROID)
1371 class_compat_unregister(switch_class);
1373 class_destroy(extcon_class);
1375 module_exit(extcon_class_exit);
1377 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1378 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1379 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1380 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1381 MODULE_DESCRIPTION("External connector (extcon) class driver");
1382 MODULE_LICENSE("GPL");