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>
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,
103 /* Jack external connector */
104 [EXTCON_JACK_MICROPHONE] = {
105 .type = EXTCON_TYPE_JACK,
106 .id = EXTCON_JACK_MICROPHONE,
107 .name = "MICROPHONE",
109 [EXTCON_JACK_HEADPHONE] = {
110 .type = EXTCON_TYPE_JACK,
111 .id = EXTCON_JACK_HEADPHONE,
114 [EXTCON_JACK_LINE_IN] = {
115 .type = EXTCON_TYPE_JACK,
116 .id = EXTCON_JACK_LINE_IN,
119 [EXTCON_JACK_LINE_OUT] = {
120 .type = EXTCON_TYPE_JACK,
121 .id = EXTCON_JACK_LINE_OUT,
124 [EXTCON_JACK_VIDEO_IN] = {
125 .type = EXTCON_TYPE_JACK,
126 .id = EXTCON_JACK_VIDEO_IN,
129 [EXTCON_JACK_VIDEO_OUT] = {
130 .type = EXTCON_TYPE_JACK,
131 .id = EXTCON_JACK_VIDEO_OUT,
134 [EXTCON_JACK_SPDIF_IN] = {
135 .type = EXTCON_TYPE_JACK,
136 .id = EXTCON_JACK_SPDIF_IN,
139 [EXTCON_JACK_SPDIF_OUT] = {
140 .type = EXTCON_TYPE_JACK,
141 .id = EXTCON_JACK_SPDIF_OUT,
145 /* Display external connector */
146 [EXTCON_DISP_HDMI] = {
147 .type = EXTCON_TYPE_DISP,
148 .id = EXTCON_DISP_HDMI,
151 [EXTCON_DISP_MHL] = {
152 .type = EXTCON_TYPE_DISP,
153 .id = EXTCON_DISP_MHL,
156 [EXTCON_DISP_DVI] = {
157 .type = EXTCON_TYPE_DISP,
158 .id = EXTCON_DISP_DVI,
161 [EXTCON_DISP_VGA] = {
162 .type = EXTCON_TYPE_DISP,
163 .id = EXTCON_DISP_VGA,
167 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
168 .id = EXTCON_DISP_DP,
171 [EXTCON_DISP_HMD] = {
172 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
173 .id = EXTCON_DISP_HMD,
177 /* Miscellaneous external connector */
179 .type = EXTCON_TYPE_MISC,
184 .type = EXTCON_TYPE_MISC,
188 [EXTCON_MECHANICAL] = {
189 .type = EXTCON_TYPE_MISC,
190 .id = EXTCON_MECHANICAL,
191 .name = "MECHANICAL",
198 * struct extcon_cable - An internal data for each cable of extcon device.
199 * @edev: The extcon device
200 * @cable_index: Index of this cable in the edev
201 * @attr_g: Attribute group for the cable
202 * @attr_name: "name" sysfs entry
203 * @attr_state: "state" sysfs entry
204 * @attrs: Array pointing to attr_name and attr_state for attr_g
206 struct extcon_cable {
207 struct extcon_dev *edev;
210 struct attribute_group attr_g;
211 struct device_attribute attr_name;
212 struct device_attribute attr_state;
214 struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
216 union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
217 union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
218 union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
219 union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
221 unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
222 unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
223 unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
224 unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
227 static struct class *extcon_class;
228 #if defined(CONFIG_ANDROID)
229 static struct class_compat *switch_class;
230 #endif /* CONFIG_ANDROID */
232 static LIST_HEAD(extcon_dev_list);
233 static DEFINE_MUTEX(extcon_dev_list_lock);
236 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
238 * @edev: the extcon device
239 * @new_state: new cable attach status for @edev
241 * Returns 0 if nothing violates. Returns the index + 1 for the first
242 * violated condition.
244 static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
248 if (!edev->mutually_exclusive)
251 for (i = 0; edev->mutually_exclusive[i]; i++) {
253 u32 correspondants = new_state & edev->mutually_exclusive[i];
255 /* calculate the total number of bits set */
256 weight = hweight32(correspondants);
264 static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
268 /* Find the the index of extcon cable in edev->supported_cable */
269 for (i = 0; i < edev->max_supported; i++) {
270 if (edev->supported_cable[i] == id)
277 static int get_extcon_type(unsigned int prop)
280 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
281 return EXTCON_TYPE_USB;
282 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
283 return EXTCON_TYPE_CHG;
284 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
285 return EXTCON_TYPE_JACK;
286 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
287 return EXTCON_TYPE_DISP;
293 static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
295 return !!(edev->state & BIT(index));
298 static bool is_extcon_changed(struct extcon_dev *edev, int index,
301 int state = !!(edev->state & BIT(index));
302 return (state != new_state);
305 static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
309 /* Check whether the property is supported or not. */
310 type = get_extcon_type(prop);
314 /* Check whether a specific extcon id supports the property or not. */
315 return !!(extcon_info[id].type & type);
318 static int is_extcon_property_capability(struct extcon_dev *edev,
319 unsigned int id, int index,unsigned int prop)
321 struct extcon_cable *cable;
324 /* Check whether the property is supported or not. */
325 type = get_extcon_type(prop);
329 cable = &edev->cables[index];
332 case EXTCON_TYPE_USB:
333 ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
335 case EXTCON_TYPE_CHG:
336 ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
338 case EXTCON_TYPE_JACK:
339 ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
341 case EXTCON_TYPE_DISP:
342 ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
351 static void init_property(struct extcon_dev *edev, unsigned int id, int index)
353 unsigned int type = extcon_info[id].type;
354 struct extcon_cable *cable = &edev->cables[index];
356 if (EXTCON_TYPE_USB & type)
357 memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
358 if (EXTCON_TYPE_CHG & type)
359 memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
360 if (EXTCON_TYPE_JACK & type)
361 memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
362 if (EXTCON_TYPE_DISP & type)
363 memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
366 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
370 struct extcon_dev *edev = dev_get_drvdata(dev);
372 if (edev->max_supported == 0)
373 return sprintf(buf, "%u\n", edev->state);
375 for (i = 0; i < edev->max_supported; i++) {
376 count += sprintf(buf + count, "%s=%d\n",
377 extcon_info[edev->supported_cable[i]].name,
378 !!(edev->state & (1 << i)));
383 static DEVICE_ATTR_RO(state);
385 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
388 struct extcon_dev *edev = dev_get_drvdata(dev);
390 return sprintf(buf, "%s\n", edev->name);
392 static DEVICE_ATTR_RO(name);
394 static ssize_t cable_name_show(struct device *dev,
395 struct device_attribute *attr, char *buf)
397 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
399 int i = cable->cable_index;
401 return sprintf(buf, "%s\n",
402 extcon_info[cable->edev->supported_cable[i]].name);
405 static ssize_t cable_state_show(struct device *dev,
406 struct device_attribute *attr, char *buf)
408 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
411 int i = cable->cable_index;
413 return sprintf(buf, "%d\n",
414 extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
418 * extcon_sync() - Synchronize the states for both the attached/detached
419 * @edev: the extcon device that has the cable.
421 * This function send a notification to synchronize the all states of a
422 * specific external connector
424 int extcon_sync(struct extcon_dev *edev, unsigned int id)
439 index = find_cable_index_by_id(edev, id);
443 spin_lock_irqsave(&edev->lock, flags);
445 state = !!(edev->state & BIT(index));
446 raw_notifier_call_chain(&edev->nh[index], state, edev);
448 /* This could be in interrupt handler */
449 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
451 /* Unlock early before uevent */
452 spin_unlock_irqrestore(&edev->lock, flags);
454 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
455 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
460 length = name_show(&edev->dev, NULL, prop_buf);
462 if (prop_buf[length - 1] == '\n')
463 prop_buf[length - 1] = 0;
464 snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
465 envp[env_offset++] = name_buf;
468 length = state_show(&edev->dev, NULL, prop_buf);
470 if (prop_buf[length - 1] == '\n')
471 prop_buf[length - 1] = 0;
472 snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
473 envp[env_offset++] = state_buf;
475 envp[env_offset] = NULL;
477 /* Unlock early before uevent */
478 spin_unlock_irqrestore(&edev->lock, flags);
479 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
480 free_page((unsigned long)prop_buf);
484 EXPORT_SYMBOL_GPL(extcon_sync);
487 * extcon_get_state() - Get the state of a external connector.
488 * @edev: the extcon device that has the cable.
489 * @id: the unique id of each external connector in extcon enumeration.
491 int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
499 index = find_cable_index_by_id(edev, id);
503 spin_lock_irqsave(&edev->lock, flags);
504 state = is_extcon_attached(edev, index);
505 spin_unlock_irqrestore(&edev->lock, flags);
509 EXPORT_SYMBOL_GPL(extcon_get_state);
512 * extcon_set_state() - Set the state of a external connector.
513 * without a notification.
514 * @edev: the extcon device that has the cable.
515 * @id: the unique id of each external connector
516 * in extcon enumeration.
517 * @state: the new cable status. The default semantics is
518 * true: attached / false: detached.
520 * This function only set the state of a external connector without
521 * a notification. To synchronize the data of a external connector,
522 * use extcon_set_state_sync() and extcon_sync().
524 int extcon_set_state(struct extcon_dev *edev, unsigned int id,
533 index = find_cable_index_by_id(edev, id);
537 spin_lock_irqsave(&edev->lock, flags);
539 /* Check whether the external connector's state is changed. */
540 if (!is_extcon_changed(edev, index, cable_state))
543 if (check_mutually_exclusive(edev,
544 (edev->state & ~BIT(index)) | (cable_state & BIT(index)))) {
550 * Initialize the value of extcon property before setting
551 * the detached state for an external connector.
554 init_property(edev, id, index);
556 /* Update the state for a external connector. */
558 edev->state |= BIT(index);
560 edev->state &= ~(BIT(index));
562 spin_unlock_irqrestore(&edev->lock, flags);
566 EXPORT_SYMBOL_GPL(extcon_set_state);
569 * extcon_set_state_sync() - Set the state of a external connector
570 * with a notification.
571 * @edev: the extcon device that has the cable.
572 * @id: the unique id of each external connector
573 * in extcon enumeration.
574 * @state: the new cable status. The default semantics is
575 * true: attached / false: detached.
577 * This function set the state of external connector and synchronize the data
578 * by usning a notification.
580 int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id,
586 index = find_cable_index_by_id(edev, id);
590 /* Check whether the external connector's state is changed. */
591 spin_lock_irqsave(&edev->lock, flags);
592 ret = is_extcon_changed(edev, index, cable_state);
593 spin_unlock_irqrestore(&edev->lock, flags);
597 ret = extcon_set_state(edev, id, cable_state);
601 return extcon_sync(edev, id);
603 EXPORT_SYMBOL_GPL(extcon_set_state_sync);
606 * extcon_get_property() - Get the property value of a specific cable.
607 * @edev: the extcon device that has the cable.
608 * @id: the unique id of each external connector
609 * in extcon enumeration.
610 * @prop: the property id among enum extcon_property.
611 * @prop_val: the pointer which store the value of property.
613 * When getting the property value of external connector, the external connector
614 * should be attached. If detached state, function just return 0 without
615 * property value. Also, the each property should be included in the list of
616 * supported properties according to the type of external connectors.
618 * Returns 0 if success or error number if fail
620 int extcon_get_property(struct extcon_dev *edev, unsigned int id,
622 union extcon_property_value *prop_val)
624 struct extcon_cable *cable;
628 *prop_val = (union extcon_property_value)(0);
633 /* Check whether the property is supported or not */
634 if (!is_extcon_property_supported(id, prop))
637 /* Find the cable index of external connector by using id */
638 index = find_cable_index_by_id(edev, id);
642 spin_lock_irqsave(&edev->lock, flags);
644 /* Check whether the property is available or not. */
645 if (!is_extcon_property_capability(edev, id, index, prop)) {
646 spin_unlock_irqrestore(&edev->lock, flags);
651 * Check whether the external connector is attached.
652 * If external connector is detached, the user can not
653 * get the property value.
655 if (!is_extcon_attached(edev, index)) {
656 spin_unlock_irqrestore(&edev->lock, flags);
660 cable = &edev->cables[index];
662 /* Get the property value according to extcon type */
664 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
665 *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
667 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
668 *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
670 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
671 *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
673 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
674 *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
681 spin_unlock_irqrestore(&edev->lock, flags);
685 EXPORT_SYMBOL_GPL(extcon_get_property);
688 * extcon_set_property() - Set the property value of a specific cable.
689 * @edev: the extcon device that has the cable.
690 * @id: the unique id of each external connector
691 * in extcon enumeration.
692 * @prop: the property id among enum extcon_property.
693 * @prop_val: the pointer including the new value of property.
695 * The each property should be included in the list of supported properties
696 * according to the type of external connectors.
698 * Returns 0 if success or error number if fail
700 int extcon_set_property(struct extcon_dev *edev, unsigned int id,
702 union extcon_property_value prop_val)
704 struct extcon_cable *cable;
711 /* Check whether the property is supported or not */
712 if (!is_extcon_property_supported(id, prop))
715 /* Find the cable index of external connector by using id */
716 index = find_cable_index_by_id(edev, id);
720 spin_lock_irqsave(&edev->lock, flags);
722 /* Check whether the property is available or not. */
723 if (!is_extcon_property_capability(edev, id, index, prop)) {
724 spin_unlock_irqrestore(&edev->lock, flags);
728 cable = &edev->cables[index];
730 /* Set the property value according to extcon type */
732 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
733 cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
735 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
736 cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
738 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
739 cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
741 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
742 cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
749 spin_unlock_irqrestore(&edev->lock, flags);
753 EXPORT_SYMBOL_GPL(extcon_set_property);
756 * extcon_set_property_sync() - Set the property value of a specific cable
758 * @prop_val: the pointer including the new value of property.
760 * When setting the property value of external connector, the external connector
761 * should be attached. The each property should be included in the list of
762 * supported properties according to the type of external connectors.
764 * Returns 0 if success or error number if fail
766 int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
768 union extcon_property_value prop_val)
772 ret = extcon_set_property(edev, id, prop, prop_val);
776 return extcon_sync(edev, id);
778 EXPORT_SYMBOL_GPL(extcon_set_property_sync);
781 * extcon_get_property_capability() - Get the capability of property
782 * of an external connector.
783 * @edev: the extcon device that has the cable.
784 * @id: the unique id of each external connector
785 * in extcon enumeration.
786 * @prop: the property id among enum extcon_property.
788 * Returns 1 if the property is available or 0 if not available.
790 int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
798 /* Check whether the property is supported or not */
799 if (!is_extcon_property_supported(id, prop))
802 /* Find the cable index of external connector by using id */
803 index = find_cable_index_by_id(edev, id);
807 return is_extcon_property_capability(edev, id, index, prop);
809 EXPORT_SYMBOL_GPL(extcon_get_property_capability);
812 * extcon_set_property_capability() - Set the capability of a property
813 * of an external connector.
814 * @edev: the extcon device that has the cable.
815 * @id: the unique id of each external connector
816 * in extcon enumeration.
817 * @prop: the property id among enum extcon_property.
819 * This function set the capability of a property for an external connector
820 * to mark the bit in capability bitmap which mean the available state of
823 * Returns 0 if success or error number if fail
825 int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
828 struct extcon_cable *cable;
829 int index, type, ret = 0;
834 /* Check whether the property is supported or not. */
835 if (!is_extcon_property_supported(id, prop))
838 /* Find the cable index of external connector by using id. */
839 index = find_cable_index_by_id(edev, id);
843 type = get_extcon_type(prop);
847 cable = &edev->cables[index];
850 case EXTCON_TYPE_USB:
851 __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
853 case EXTCON_TYPE_CHG:
854 __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
856 case EXTCON_TYPE_JACK:
857 __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
859 case EXTCON_TYPE_DISP:
860 __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
868 EXPORT_SYMBOL_GPL(extcon_set_property_capability);
871 * extcon_get_extcon_dev() - Get the extcon device instance from the name
872 * @extcon_name: The extcon name provided with extcon_dev_register()
874 struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
876 struct extcon_dev *sd;
879 return ERR_PTR(-EINVAL);
881 mutex_lock(&extcon_dev_list_lock);
882 list_for_each_entry(sd, &extcon_dev_list, entry) {
883 if (!strcmp(sd->name, extcon_name))
888 mutex_unlock(&extcon_dev_list_lock);
891 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
894 * extcon_register_notifier() - Register a notifiee to get notified by
895 * any attach status changes from the extcon.
896 * @edev: the extcon device that has the external connecotr.
897 * @id: the unique id of each external connector in extcon enumeration.
898 * @nb: a notifier block to be registered.
900 * Note that the second parameter given to the callback of nb (val) is
901 * "old_state", not the current state. The current state can be retrieved
902 * by looking at the third pameter (edev pointer)'s state value.
904 int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
905 struct notifier_block *nb)
908 int ret, idx = -EINVAL;
913 idx = find_cable_index_by_id(edev, id);
917 spin_lock_irqsave(&edev->lock, flags);
918 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
919 spin_unlock_irqrestore(&edev->lock, flags);
923 EXPORT_SYMBOL_GPL(extcon_register_notifier);
926 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
927 * @edev: the extcon device that has the external connecotr.
928 * @id: the unique id of each external connector in extcon enumeration.
929 * @nb: a notifier block to be registered.
931 int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
932 struct notifier_block *nb)
940 idx = find_cable_index_by_id(edev, id);
944 spin_lock_irqsave(&edev->lock, flags);
945 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
946 spin_unlock_irqrestore(&edev->lock, flags);
950 EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
952 static struct attribute *extcon_attrs[] = {
953 &dev_attr_state.attr,
957 ATTRIBUTE_GROUPS(extcon);
959 static int create_extcon_class(void)
962 extcon_class = class_create(THIS_MODULE, "extcon");
963 if (IS_ERR(extcon_class))
964 return PTR_ERR(extcon_class);
965 extcon_class->dev_groups = extcon_groups;
967 #if defined(CONFIG_ANDROID)
968 switch_class = class_compat_register("switch");
969 if (WARN(!switch_class, "cannot allocate"))
971 #endif /* CONFIG_ANDROID */
977 static void extcon_dev_release(struct device *dev)
981 static const char *muex_name = "mutually_exclusive";
982 static void dummy_sysfs_dev_release(struct device *dev)
987 * extcon_dev_allocate() - Allocate the memory of extcon device.
988 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
989 * If supported_cable is NULL, cable name related APIs
992 * This function allocates the memory for extcon device without allocating
993 * memory in each extcon provider driver and initialize default setting for
996 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
998 struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
1000 struct extcon_dev *edev;
1002 if (!supported_cable)
1003 return ERR_PTR(-EINVAL);
1005 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
1007 return ERR_PTR(-ENOMEM);
1009 edev->max_supported = 0;
1010 edev->supported_cable = supported_cable;
1016 * extcon_dev_free() - Free the memory of extcon device.
1017 * @edev: the extcon device to free
1019 void extcon_dev_free(struct extcon_dev *edev)
1023 EXPORT_SYMBOL_GPL(extcon_dev_free);
1026 * extcon_dev_register() - Register a new extcon device
1027 * @edev : the new extcon device (should be allocated before calling)
1029 * Among the members of edev struct, please set the "user initializing data"
1030 * in any case and set the "optional callbacks" if required. However, please
1031 * do not set the values of "internal data", which are initialized by
1034 int extcon_dev_register(struct extcon_dev *edev)
1037 static atomic_t edev_no = ATOMIC_INIT(-1);
1039 if (!extcon_class) {
1040 ret = create_extcon_class();
1045 if (!edev || !edev->supported_cable)
1048 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
1050 edev->max_supported = index;
1051 if (index > SUPPORTED_CABLE_MAX) {
1053 "exceed the maximum number of supported cables\n");
1057 edev->dev.class = extcon_class;
1058 edev->dev.release = extcon_dev_release;
1060 edev->name = dev_name(edev->dev.parent);
1061 if (IS_ERR_OR_NULL(edev->name)) {
1063 "extcon device name is null\n");
1066 dev_set_name(&edev->dev, "extcon%lu",
1067 (unsigned long)atomic_inc_return(&edev_no));
1069 if (edev->max_supported) {
1072 struct extcon_cable *cable;
1074 edev->cables = kzalloc(sizeof(struct extcon_cable) *
1075 edev->max_supported, GFP_KERNEL);
1076 if (!edev->cables) {
1078 goto err_sysfs_alloc;
1080 for (index = 0; index < edev->max_supported; index++) {
1081 cable = &edev->cables[index];
1083 snprintf(buf, 10, "cable.%d", index);
1084 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
1087 for (index--; index >= 0; index--) {
1088 cable = &edev->cables[index];
1089 kfree(cable->attr_g.name);
1093 goto err_alloc_cables;
1098 cable->cable_index = index;
1099 cable->attrs[0] = &cable->attr_name.attr;
1100 cable->attrs[1] = &cable->attr_state.attr;
1101 cable->attrs[2] = NULL;
1102 cable->attr_g.name = str;
1103 cable->attr_g.attrs = cable->attrs;
1105 sysfs_attr_init(&cable->attr_name.attr);
1106 cable->attr_name.attr.name = "name";
1107 cable->attr_name.attr.mode = 0444;
1108 cable->attr_name.show = cable_name_show;
1110 sysfs_attr_init(&cable->attr_state.attr);
1111 cable->attr_state.attr.name = "state";
1112 cable->attr_state.attr.mode = 0444;
1113 cable->attr_state.show = cable_state_show;
1117 if (edev->max_supported && edev->mutually_exclusive) {
1121 /* Count the size of mutually_exclusive array */
1122 for (index = 0; edev->mutually_exclusive[index]; index++)
1125 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
1126 (index + 1), GFP_KERNEL);
1127 if (!edev->attrs_muex) {
1132 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
1134 if (!edev->d_attrs_muex) {
1136 kfree(edev->attrs_muex);
1140 for (index = 0; edev->mutually_exclusive[index]; index++) {
1141 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
1142 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
1145 for (index--; index >= 0; index--) {
1146 kfree(edev->d_attrs_muex[index].attr.
1149 kfree(edev->d_attrs_muex);
1150 kfree(edev->attrs_muex);
1155 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
1156 edev->d_attrs_muex[index].attr.name = name;
1157 edev->d_attrs_muex[index].attr.mode = 0000;
1158 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
1161 edev->attr_g_muex.name = muex_name;
1162 edev->attr_g_muex.attrs = edev->attrs_muex;
1166 if (edev->max_supported) {
1167 edev->extcon_dev_type.groups =
1168 kzalloc(sizeof(struct attribute_group *) *
1169 (edev->max_supported + 2), GFP_KERNEL);
1170 if (!edev->extcon_dev_type.groups) {
1172 goto err_alloc_groups;
1175 edev->extcon_dev_type.name = dev_name(&edev->dev);
1176 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
1178 for (index = 0; index < edev->max_supported; index++)
1179 edev->extcon_dev_type.groups[index] =
1180 &edev->cables[index].attr_g;
1181 if (edev->mutually_exclusive)
1182 edev->extcon_dev_type.groups[index] =
1185 edev->dev.type = &edev->extcon_dev_type;
1188 ret = device_register(&edev->dev);
1190 put_device(&edev->dev);
1193 #if defined(CONFIG_ANDROID)
1195 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
1196 #endif /* CONFIG_ANDROID */
1198 spin_lock_init(&edev->lock);
1200 edev->nh = devm_kzalloc(&edev->dev,
1201 sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
1207 for (index = 0; index < edev->max_supported; index++)
1208 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
1210 dev_set_drvdata(&edev->dev, edev);
1213 mutex_lock(&extcon_dev_list_lock);
1214 list_add(&edev->entry, &extcon_dev_list);
1215 mutex_unlock(&extcon_dev_list_lock);
1220 if (edev->max_supported)
1221 kfree(edev->extcon_dev_type.groups);
1223 if (edev->max_supported && edev->mutually_exclusive) {
1224 for (index = 0; edev->mutually_exclusive[index]; index++)
1225 kfree(edev->d_attrs_muex[index].attr.name);
1226 kfree(edev->d_attrs_muex);
1227 kfree(edev->attrs_muex);
1230 for (index = 0; index < edev->max_supported; index++)
1231 kfree(edev->cables[index].attr_g.name);
1233 if (edev->max_supported)
1234 kfree(edev->cables);
1238 EXPORT_SYMBOL_GPL(extcon_dev_register);
1241 * extcon_dev_unregister() - Unregister the extcon device.
1242 * @edev: the extcon device instance to be unregistered.
1244 * Note that this does not call kfree(edev) because edev was not allocated
1247 void extcon_dev_unregister(struct extcon_dev *edev)
1254 mutex_lock(&extcon_dev_list_lock);
1255 list_del(&edev->entry);
1256 mutex_unlock(&extcon_dev_list_lock);
1258 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
1259 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
1260 dev_name(&edev->dev));
1264 device_unregister(&edev->dev);
1266 if (edev->mutually_exclusive && edev->max_supported) {
1267 for (index = 0; edev->mutually_exclusive[index];
1269 kfree(edev->d_attrs_muex[index].attr.name);
1270 kfree(edev->d_attrs_muex);
1271 kfree(edev->attrs_muex);
1274 for (index = 0; index < edev->max_supported; index++)
1275 kfree(edev->cables[index].attr_g.name);
1277 if (edev->max_supported) {
1278 kfree(edev->extcon_dev_type.groups);
1279 kfree(edev->cables);
1282 #if defined(CONFIG_ANDROID)
1284 class_compat_remove_link(switch_class, &edev->dev, NULL);
1286 put_device(&edev->dev);
1288 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1292 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1293 * @dev - instance to the given device
1294 * @index - index into list of extcon_dev
1296 * return the instance of extcon device
1298 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1300 struct device_node *node;
1301 struct extcon_dev *edev;
1304 return ERR_PTR(-EINVAL);
1306 if (!dev->of_node) {
1307 dev_dbg(dev, "device does not have a device node entry\n");
1308 return ERR_PTR(-EINVAL);
1311 node = of_parse_phandle(dev->of_node, "extcon", index);
1313 dev_dbg(dev, "failed to get phandle in %s node\n",
1314 dev->of_node->full_name);
1315 return ERR_PTR(-ENODEV);
1318 mutex_lock(&extcon_dev_list_lock);
1319 list_for_each_entry(edev, &extcon_dev_list, entry) {
1320 if (edev->dev.parent && edev->dev.parent->of_node == node) {
1321 mutex_unlock(&extcon_dev_list_lock);
1326 mutex_unlock(&extcon_dev_list_lock);
1329 return ERR_PTR(-EPROBE_DEFER);
1332 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1334 return ERR_PTR(-ENOSYS);
1336 #endif /* CONFIG_OF */
1337 EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1340 * extcon_get_edev_name() - Get the name of the extcon device.
1341 * @edev: the extcon device
1343 const char *extcon_get_edev_name(struct extcon_dev *edev)
1345 return !edev ? NULL : edev->name;
1348 static int __init extcon_class_init(void)
1350 return create_extcon_class();
1352 module_init(extcon_class_init);
1354 static void __exit extcon_class_exit(void)
1356 #if defined(CONFIG_ANDROID)
1357 class_compat_unregister(switch_class);
1359 class_destroy(extcon_class);
1361 module_exit(extcon_class_exit);
1363 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1364 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1365 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1366 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1367 MODULE_DESCRIPTION("External connector (extcon) class driver");
1368 MODULE_LICENSE("GPL");