1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/extcon/extcon.c - External Connector (extcon) framework.
5 * Copyright (C) 2015 Samsung Electronics
6 * Author: Chanwoo Choi <cw00.choi@samsung.com>
8 * Copyright (C) 2012 Samsung Electronics
9 * Author: Donggeun Kim <dg77.kim@samsung.com>
10 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
12 * based on android/drivers/switch/switch_class.c
13 * Copyright (C) 2008 Google, Inc.
14 * Author: Mike Lockwood <lockwood@android.com>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/init.h>
20 #include <linux/device.h>
22 #include <linux/err.h>
24 #include <linux/slab.h>
25 #include <linux/sysfs.h>
29 #define SUPPORTED_CABLE_MAX 32
31 static const struct __extcon_info {
38 .type = EXTCON_TYPE_MISC,
43 /* USB external connector */
45 .type = EXTCON_TYPE_USB,
50 .type = EXTCON_TYPE_USB,
51 .id = EXTCON_USB_HOST,
55 /* Charging external connector */
56 [EXTCON_CHG_USB_SDP] = {
57 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
58 .id = EXTCON_CHG_USB_SDP,
61 [EXTCON_CHG_USB_DCP] = {
62 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
63 .id = EXTCON_CHG_USB_DCP,
66 [EXTCON_CHG_USB_CDP] = {
67 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
68 .id = EXTCON_CHG_USB_CDP,
71 [EXTCON_CHG_USB_ACA] = {
72 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
73 .id = EXTCON_CHG_USB_ACA,
76 [EXTCON_CHG_USB_FAST] = {
77 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
78 .id = EXTCON_CHG_USB_FAST,
79 .name = "FAST-CHARGER",
81 [EXTCON_CHG_USB_SLOW] = {
82 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
83 .id = EXTCON_CHG_USB_SLOW,
84 .name = "SLOW-CHARGER",
87 .type = EXTCON_TYPE_CHG,
91 [EXTCON_CHG_USB_PD] = {
92 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
93 .id = EXTCON_CHG_USB_PD,
97 /* Jack external connector */
98 [EXTCON_JACK_MICROPHONE] = {
99 .type = EXTCON_TYPE_JACK,
100 .id = EXTCON_JACK_MICROPHONE,
101 .name = "MICROPHONE",
103 [EXTCON_JACK_HEADPHONE] = {
104 .type = EXTCON_TYPE_JACK,
105 .id = EXTCON_JACK_HEADPHONE,
108 [EXTCON_JACK_LINE_IN] = {
109 .type = EXTCON_TYPE_JACK,
110 .id = EXTCON_JACK_LINE_IN,
113 [EXTCON_JACK_LINE_OUT] = {
114 .type = EXTCON_TYPE_JACK,
115 .id = EXTCON_JACK_LINE_OUT,
118 [EXTCON_JACK_VIDEO_IN] = {
119 .type = EXTCON_TYPE_JACK,
120 .id = EXTCON_JACK_VIDEO_IN,
123 [EXTCON_JACK_VIDEO_OUT] = {
124 .type = EXTCON_TYPE_JACK,
125 .id = EXTCON_JACK_VIDEO_OUT,
128 [EXTCON_JACK_SPDIF_IN] = {
129 .type = EXTCON_TYPE_JACK,
130 .id = EXTCON_JACK_SPDIF_IN,
133 [EXTCON_JACK_SPDIF_OUT] = {
134 .type = EXTCON_TYPE_JACK,
135 .id = EXTCON_JACK_SPDIF_OUT,
139 /* Display external connector */
140 [EXTCON_DISP_HDMI] = {
141 .type = EXTCON_TYPE_DISP,
142 .id = EXTCON_DISP_HDMI,
145 [EXTCON_DISP_MHL] = {
146 .type = EXTCON_TYPE_DISP,
147 .id = EXTCON_DISP_MHL,
150 [EXTCON_DISP_DVI] = {
151 .type = EXTCON_TYPE_DISP,
152 .id = EXTCON_DISP_DVI,
155 [EXTCON_DISP_VGA] = {
156 .type = EXTCON_TYPE_DISP,
157 .id = EXTCON_DISP_VGA,
161 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
162 .id = EXTCON_DISP_DP,
165 [EXTCON_DISP_HMD] = {
166 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
167 .id = EXTCON_DISP_HMD,
170 [EXTCON_DISP_CVBS] = {
171 .type = EXTCON_TYPE_DISP,
172 .id = EXTCON_DISP_CVBS,
175 [EXTCON_DISP_EDP] = {
176 .type = EXTCON_TYPE_DISP,
177 .id = EXTCON_DISP_EDP,
181 /* Miscellaneous external connector */
183 .type = EXTCON_TYPE_MISC,
188 .type = EXTCON_TYPE_MISC,
192 [EXTCON_MECHANICAL] = {
193 .type = EXTCON_TYPE_MISC,
194 .id = EXTCON_MECHANICAL,
195 .name = "MECHANICAL",
202 * struct extcon_cable - An internal data for an external connector.
203 * @edev: the extcon device
204 * @cable_index: the index of this cable in the edev
205 * @attr_g: the attribute group for the cable
206 * @attr_name: "name" sysfs entry
207 * @attr_state: "state" sysfs entry
208 * @attrs: the array pointing to attr_name and attr_state for attr_g
209 * @usb_propval: the array of USB connector properties
210 * @chg_propval: the array of charger connector properties
211 * @jack_propval: the array of jack connector properties
212 * @disp_propval: the array of display connector properties
213 * @usb_bits: the bit array of the USB connector property capabilities
214 * @chg_bits: the bit array of the charger connector property capabilities
215 * @jack_bits: the bit array of the jack connector property capabilities
216 * @disp_bits: the bit array of the display connector property capabilities
218 struct extcon_cable {
219 struct extcon_dev *edev;
222 struct attribute_group attr_g;
223 struct device_attribute attr_name;
224 struct device_attribute attr_state;
226 struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
228 union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
229 union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
230 union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
231 union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
233 unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
234 unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
235 unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
236 unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
239 static struct class *extcon_class;
241 static LIST_HEAD(extcon_dev_list);
242 static DEFINE_MUTEX(extcon_dev_list_lock);
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 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 & BIT(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 state for an external connector.
419 * @edev: the extcon device
420 * @id: the unique id indicating an external connector
422 * Note that this function send a notification in order to synchronize
423 * the state and property of an external connector.
425 * Returns 0 if success or error number if fail.
427 int extcon_sync(struct extcon_dev *edev, unsigned int id)
442 index = find_cable_index_by_id(edev, id);
446 spin_lock_irqsave(&edev->lock, flags);
447 state = !!(edev->state & BIT(index));
448 spin_unlock_irqrestore(&edev->lock, flags);
451 * Call functions in a raw notifier chain for the specific one
452 * external connector.
454 raw_notifier_call_chain(&edev->nh[index], state, edev);
457 * Call functions in a raw notifier chain for the all supported
458 * external connectors.
460 raw_notifier_call_chain(&edev->nh_all, state, edev);
462 spin_lock_irqsave(&edev->lock, flags);
463 /* This could be in interrupt handler */
464 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
466 /* Unlock early before uevent */
467 spin_unlock_irqrestore(&edev->lock, flags);
469 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
470 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
475 length = name_show(&edev->dev, NULL, prop_buf);
477 if (prop_buf[length - 1] == '\n')
478 prop_buf[length - 1] = 0;
479 snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
480 envp[env_offset++] = name_buf;
483 length = state_show(&edev->dev, NULL, prop_buf);
485 if (prop_buf[length - 1] == '\n')
486 prop_buf[length - 1] = 0;
487 snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
488 envp[env_offset++] = state_buf;
490 envp[env_offset] = NULL;
492 /* Unlock early before uevent */
493 spin_unlock_irqrestore(&edev->lock, flags);
494 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
495 free_page((unsigned long)prop_buf);
499 EXPORT_SYMBOL_GPL(extcon_sync);
502 * extcon_get_state() - Get the state of an external connector.
503 * @edev: the extcon device
504 * @id: the unique id indicating an external connector
506 * Returns 0 if success or error number if fail.
508 int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
516 index = find_cable_index_by_id(edev, id);
520 spin_lock_irqsave(&edev->lock, flags);
521 state = is_extcon_attached(edev, index);
522 spin_unlock_irqrestore(&edev->lock, flags);
526 EXPORT_SYMBOL_GPL(extcon_get_state);
529 * extcon_set_state() - Set the state of an external connector.
530 * @edev: the extcon device
531 * @id: the unique id indicating an external connector
532 * @state: the new state of an external connector.
533 * the default semantics is true: attached / false: detached.
535 * Note that this function set the state of an external connector without
536 * a notification. To synchronize the state of an external connector,
537 * have to use extcon_set_state_sync() and extcon_sync().
539 * Returns 0 if success or error number if fail.
541 int extcon_set_state(struct extcon_dev *edev, unsigned int id, bool state)
549 index = find_cable_index_by_id(edev, id);
553 spin_lock_irqsave(&edev->lock, flags);
555 /* Check whether the external connector's state is changed. */
556 if (!is_extcon_changed(edev, index, state))
559 if (check_mutually_exclusive(edev,
560 (edev->state & ~BIT(index)) | (state & BIT(index)))) {
566 * Initialize the value of extcon property before setting
567 * the detached state for an external connector.
570 init_property(edev, id, index);
572 /* Update the state for an external connector. */
574 edev->state |= BIT(index);
576 edev->state &= ~(BIT(index));
578 spin_unlock_irqrestore(&edev->lock, flags);
582 EXPORT_SYMBOL_GPL(extcon_set_state);
585 * extcon_set_state_sync() - Set the state of an external connector with sync.
586 * @edev: the extcon device
587 * @id: the unique id indicating an external connector
588 * @state: the new state of external connector.
589 * the default semantics is true: attached / false: detached.
591 * Note that this function set the state of external connector
592 * and synchronize the state by sending a notification.
594 * Returns 0 if success or error number if fail.
596 int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id, bool state)
600 ret = extcon_set_state(edev, id, state);
604 return extcon_sync(edev, id);
606 EXPORT_SYMBOL_GPL(extcon_set_state_sync);
609 * extcon_get_property() - Get the property value of an external connector.
610 * @edev: the extcon device
611 * @id: the unique id indicating an external connector
612 * @prop: the property id indicating an extcon property
613 * @prop_val: the pointer which store the value of extcon property
615 * Note that when getting the property value of external connector,
616 * the external connector should be attached. If detached state, function
617 * return 0 without property value. Also, the each property should be
618 * included in the list of supported properties according to extcon type.
620 * Returns 0 if success or error number if fail.
622 int extcon_get_property(struct extcon_dev *edev, unsigned int id,
624 union extcon_property_value *prop_val)
626 struct extcon_cable *cable;
630 *prop_val = (union extcon_property_value){0};
635 /* Check whether the property is supported or not */
636 if (!is_extcon_property_supported(id, prop))
639 /* Find the cable index of external connector by using id */
640 index = find_cable_index_by_id(edev, id);
644 spin_lock_irqsave(&edev->lock, flags);
646 /* Check whether the property is available or not. */
647 if (!is_extcon_property_capability(edev, id, index, prop)) {
648 spin_unlock_irqrestore(&edev->lock, flags);
653 * Check whether the external connector is attached.
654 * If external connector is detached, the user can not
655 * get the property value.
657 if (!is_extcon_attached(edev, index)) {
658 spin_unlock_irqrestore(&edev->lock, flags);
662 cable = &edev->cables[index];
664 /* Get the property value according to extcon type */
666 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
667 *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
669 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
670 *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
672 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
673 *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
675 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
676 *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
683 spin_unlock_irqrestore(&edev->lock, flags);
687 EXPORT_SYMBOL_GPL(extcon_get_property);
690 * extcon_set_property() - Set the property value of an external connector.
691 * @edev: the extcon device
692 * @id: the unique id indicating an external connector
693 * @prop: the property id indicating an extcon property
694 * @prop_val: the pointer including the new value of extcon property
696 * Note that each property should be included in the list of supported
697 * properties according to the extcon type.
699 * Returns 0 if success or error number if fail.
701 int extcon_set_property(struct extcon_dev *edev, unsigned int id,
703 union extcon_property_value prop_val)
705 struct extcon_cable *cable;
712 /* Check whether the property is supported or not */
713 if (!is_extcon_property_supported(id, prop))
716 /* Find the cable index of external connector by using id */
717 index = find_cable_index_by_id(edev, id);
721 spin_lock_irqsave(&edev->lock, flags);
723 /* Check whether the property is available or not. */
724 if (!is_extcon_property_capability(edev, id, index, prop)) {
725 spin_unlock_irqrestore(&edev->lock, flags);
729 cable = &edev->cables[index];
731 /* Set the property value according to extcon type */
733 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
734 cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
736 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
737 cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
739 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
740 cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
742 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
743 cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
750 spin_unlock_irqrestore(&edev->lock, flags);
754 EXPORT_SYMBOL_GPL(extcon_set_property);
757 * extcon_set_property_sync() - Set property of an external connector with sync.
758 * @edev: the extcon device
759 * @id: the unique id indicating an external connector
760 * @prop: the property id indicating an extcon property
761 * @prop_val: the pointer including the new value of extcon property
763 * Note that when setting the property value of external connector,
764 * the external connector should be attached. The each property should
765 * be included in the list of supported properties according to extcon type.
767 * Returns 0 if success or error number if fail.
769 int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
771 union extcon_property_value prop_val)
775 ret = extcon_set_property(edev, id, prop, prop_val);
779 return extcon_sync(edev, id);
781 EXPORT_SYMBOL_GPL(extcon_set_property_sync);
784 * extcon_get_property_capability() - Get the capability of the property
785 * for an external connector.
786 * @edev: the extcon device
787 * @id: the unique id indicating an external connector
788 * @prop: the property id indicating an extcon property
790 * Returns 1 if the property is available or 0 if not available.
792 int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
800 /* Check whether the property is supported or not */
801 if (!is_extcon_property_supported(id, prop))
804 /* Find the cable index of external connector by using id */
805 index = find_cable_index_by_id(edev, id);
809 return is_extcon_property_capability(edev, id, index, prop);
811 EXPORT_SYMBOL_GPL(extcon_get_property_capability);
814 * extcon_set_property_capability() - Set the capability of the property
815 * for an external connector.
816 * @edev: the extcon device
817 * @id: the unique id indicating an external connector
818 * @prop: the property id indicating an extcon property
820 * Note that this function set the capability of the property
821 * for an external connector in order to mark the bit in capability
822 * bitmap which mean the available state of the property.
824 * Returns 0 if success or error number if fail.
826 int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
829 struct extcon_cable *cable;
830 int index, type, ret = 0;
835 /* Check whether the property is supported or not. */
836 if (!is_extcon_property_supported(id, prop))
839 /* Find the cable index of external connector by using id. */
840 index = find_cable_index_by_id(edev, id);
844 type = get_extcon_type(prop);
848 cable = &edev->cables[index];
851 case EXTCON_TYPE_USB:
852 __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
854 case EXTCON_TYPE_CHG:
855 __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
857 case EXTCON_TYPE_JACK:
858 __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
860 case EXTCON_TYPE_DISP:
861 __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
869 EXPORT_SYMBOL_GPL(extcon_set_property_capability);
872 * extcon_get_extcon_dev() - Get the extcon device instance from the name.
873 * @extcon_name: the extcon name provided with extcon_dev_register()
875 * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
876 * NOTE: This function returns -EPROBE_DEFER so it may only be called from
879 struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
881 struct extcon_dev *sd;
884 return ERR_PTR(-EINVAL);
886 mutex_lock(&extcon_dev_list_lock);
887 list_for_each_entry(sd, &extcon_dev_list, entry) {
888 if (!strcmp(sd->name, extcon_name))
891 sd = ERR_PTR(-EPROBE_DEFER);
893 mutex_unlock(&extcon_dev_list_lock);
896 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
899 * extcon_register_notifier() - Register a notifier block to get notified by
900 * any state changes from the extcon.
901 * @edev: the extcon device
902 * @id: the unique id indicating an external connector
903 * @nb: a notifier block to be registered
905 * Note that the second parameter given to the callback of nb (val) is
906 * the current state of an external connector and the third pameter
907 * is the pointer of extcon device.
909 * Returns 0 if success or error number if fail.
911 int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
912 struct notifier_block *nb)
920 idx = find_cable_index_by_id(edev, id);
924 spin_lock_irqsave(&edev->lock, flags);
925 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
926 spin_unlock_irqrestore(&edev->lock, flags);
930 EXPORT_SYMBOL_GPL(extcon_register_notifier);
933 * extcon_unregister_notifier() - Unregister a notifier block from the extcon.
934 * @edev: the extcon device
935 * @id: the unique id indicating an external connector
936 * @nb: a notifier block to be registered
938 * Returns 0 if success or error number if fail.
940 int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
941 struct notifier_block *nb)
949 idx = find_cable_index_by_id(edev, id);
953 spin_lock_irqsave(&edev->lock, flags);
954 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
955 spin_unlock_irqrestore(&edev->lock, flags);
959 EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
962 * extcon_register_notifier_all() - Register a notifier block for all connectors.
963 * @edev: the extcon device
964 * @nb: a notifier block to be registered
966 * Note that this function registers a notifier block in order to receive
967 * the state change of all supported external connectors from extcon device.
968 * And the second parameter given to the callback of nb (val) is
969 * the current state and the third pameter is the pointer of extcon device.
971 * Returns 0 if success or error number if fail.
973 int extcon_register_notifier_all(struct extcon_dev *edev,
974 struct notifier_block *nb)
982 spin_lock_irqsave(&edev->lock, flags);
983 ret = raw_notifier_chain_register(&edev->nh_all, nb);
984 spin_unlock_irqrestore(&edev->lock, flags);
988 EXPORT_SYMBOL_GPL(extcon_register_notifier_all);
991 * extcon_unregister_notifier_all() - Unregister a notifier block from extcon.
992 * @edev: the extcon device
993 * @nb: a notifier block to be registered
995 * Returns 0 if success or error number if fail.
997 int extcon_unregister_notifier_all(struct extcon_dev *edev,
998 struct notifier_block *nb)
1000 unsigned long flags;
1006 spin_lock_irqsave(&edev->lock, flags);
1007 ret = raw_notifier_chain_unregister(&edev->nh_all, nb);
1008 spin_unlock_irqrestore(&edev->lock, flags);
1012 EXPORT_SYMBOL_GPL(extcon_unregister_notifier_all);
1014 static struct attribute *extcon_attrs[] = {
1015 &dev_attr_state.attr,
1016 &dev_attr_name.attr,
1019 ATTRIBUTE_GROUPS(extcon);
1021 static int create_extcon_class(void)
1023 if (!extcon_class) {
1024 extcon_class = class_create(THIS_MODULE, "extcon");
1025 if (IS_ERR(extcon_class))
1026 return PTR_ERR(extcon_class);
1027 extcon_class->dev_groups = extcon_groups;
1033 static void extcon_dev_release(struct device *dev)
1037 static const char *muex_name = "mutually_exclusive";
1038 static void dummy_sysfs_dev_release(struct device *dev)
1043 * extcon_dev_allocate() - Allocate the memory of extcon device.
1044 * @supported_cable: the array of the supported external connectors
1045 * ending with EXTCON_NONE.
1047 * Note that this function allocates the memory for extcon device
1048 * and initialize default setting for the extcon device.
1050 * Returns the pointer memory of allocated extcon_dev if success
1051 * or ERR_PTR(err) if fail.
1053 struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
1055 struct extcon_dev *edev;
1057 if (!supported_cable)
1058 return ERR_PTR(-EINVAL);
1060 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
1062 return ERR_PTR(-ENOMEM);
1064 edev->max_supported = 0;
1065 edev->supported_cable = supported_cable;
1071 * extcon_dev_free() - Free the memory of extcon device.
1072 * @edev: the extcon device
1074 void extcon_dev_free(struct extcon_dev *edev)
1078 EXPORT_SYMBOL_GPL(extcon_dev_free);
1081 * extcon_dev_register() - Register an new extcon device
1082 * @edev: the extcon device to be registered
1084 * Among the members of edev struct, please set the "user initializing data"
1085 * do not set the values of "internal data", which are initialized by
1088 * Note that before calling this funciton, have to allocate the memory
1089 * of an extcon device by using the extcon_dev_allocate(). And the extcon
1090 * dev should include the supported_cable information.
1092 * Returns 0 if success or error number if fail.
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) {
1131 struct extcon_cable *cable;
1133 edev->cables = kcalloc(edev->max_supported,
1134 sizeof(struct extcon_cable),
1136 if (!edev->cables) {
1138 goto err_sysfs_alloc;
1140 for (index = 0; index < edev->max_supported; index++) {
1141 cable = &edev->cables[index];
1143 str = kasprintf(GFP_KERNEL, "cable.%d", index);
1145 for (index--; index >= 0; index--) {
1146 cable = &edev->cables[index];
1147 kfree(cable->attr_g.name);
1151 goto err_alloc_cables;
1155 cable->cable_index = index;
1156 cable->attrs[0] = &cable->attr_name.attr;
1157 cable->attrs[1] = &cable->attr_state.attr;
1158 cable->attrs[2] = NULL;
1159 cable->attr_g.name = str;
1160 cable->attr_g.attrs = cable->attrs;
1162 sysfs_attr_init(&cable->attr_name.attr);
1163 cable->attr_name.attr.name = "name";
1164 cable->attr_name.attr.mode = 0444;
1165 cable->attr_name.show = cable_name_show;
1167 sysfs_attr_init(&cable->attr_state.attr);
1168 cable->attr_state.attr.name = "state";
1169 cable->attr_state.attr.mode = 0444;
1170 cable->attr_state.show = cable_state_show;
1174 if (edev->max_supported && edev->mutually_exclusive) {
1177 /* Count the size of mutually_exclusive array */
1178 for (index = 0; edev->mutually_exclusive[index]; index++)
1181 edev->attrs_muex = kcalloc(index + 1,
1182 sizeof(struct attribute *),
1184 if (!edev->attrs_muex) {
1189 edev->d_attrs_muex = kcalloc(index,
1190 sizeof(struct device_attribute),
1192 if (!edev->d_attrs_muex) {
1194 kfree(edev->attrs_muex);
1198 for (index = 0; edev->mutually_exclusive[index]; index++) {
1199 name = kasprintf(GFP_KERNEL, "0x%x",
1200 edev->mutually_exclusive[index]);
1202 for (index--; index >= 0; index--) {
1203 kfree(edev->d_attrs_muex[index].attr.
1206 kfree(edev->d_attrs_muex);
1207 kfree(edev->attrs_muex);
1211 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
1212 edev->d_attrs_muex[index].attr.name = name;
1213 edev->d_attrs_muex[index].attr.mode = 0000;
1214 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
1217 edev->attr_g_muex.name = muex_name;
1218 edev->attr_g_muex.attrs = edev->attrs_muex;
1222 if (edev->max_supported) {
1223 edev->extcon_dev_type.groups =
1224 kcalloc(edev->max_supported + 2,
1225 sizeof(struct attribute_group *),
1227 if (!edev->extcon_dev_type.groups) {
1229 goto err_alloc_groups;
1232 edev->extcon_dev_type.name = dev_name(&edev->dev);
1233 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
1235 for (index = 0; index < edev->max_supported; index++)
1236 edev->extcon_dev_type.groups[index] =
1237 &edev->cables[index].attr_g;
1238 if (edev->mutually_exclusive)
1239 edev->extcon_dev_type.groups[index] =
1242 edev->dev.type = &edev->extcon_dev_type;
1245 spin_lock_init(&edev->lock);
1246 if (edev->max_supported) {
1247 edev->nh = kcalloc(edev->max_supported, sizeof(*edev->nh),
1255 for (index = 0; index < edev->max_supported; index++)
1256 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
1258 RAW_INIT_NOTIFIER_HEAD(&edev->nh_all);
1260 dev_set_drvdata(&edev->dev, edev);
1263 ret = device_register(&edev->dev);
1265 put_device(&edev->dev);
1269 mutex_lock(&extcon_dev_list_lock);
1270 list_add(&edev->entry, &extcon_dev_list);
1271 mutex_unlock(&extcon_dev_list_lock);
1276 if (edev->max_supported)
1279 if (edev->max_supported)
1280 kfree(edev->extcon_dev_type.groups);
1282 if (edev->max_supported && edev->mutually_exclusive) {
1283 for (index = 0; edev->mutually_exclusive[index]; index++)
1284 kfree(edev->d_attrs_muex[index].attr.name);
1285 kfree(edev->d_attrs_muex);
1286 kfree(edev->attrs_muex);
1289 for (index = 0; index < edev->max_supported; index++)
1290 kfree(edev->cables[index].attr_g.name);
1292 if (edev->max_supported)
1293 kfree(edev->cables);
1297 EXPORT_SYMBOL_GPL(extcon_dev_register);
1300 * extcon_dev_unregister() - Unregister the extcon device.
1301 * @edev: the extcon device to be unregistered.
1303 * Note that this does not call kfree(edev) because edev was not allocated
1306 void extcon_dev_unregister(struct extcon_dev *edev)
1313 mutex_lock(&extcon_dev_list_lock);
1314 list_del(&edev->entry);
1315 mutex_unlock(&extcon_dev_list_lock);
1317 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
1318 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
1319 dev_name(&edev->dev));
1323 device_unregister(&edev->dev);
1325 if (edev->mutually_exclusive && edev->max_supported) {
1326 for (index = 0; edev->mutually_exclusive[index];
1328 kfree(edev->d_attrs_muex[index].attr.name);
1329 kfree(edev->d_attrs_muex);
1330 kfree(edev->attrs_muex);
1333 for (index = 0; index < edev->max_supported; index++)
1334 kfree(edev->cables[index].attr_g.name);
1336 if (edev->max_supported) {
1337 kfree(edev->extcon_dev_type.groups);
1338 kfree(edev->cables);
1342 put_device(&edev->dev);
1344 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1349 * extcon_find_edev_by_node - Find the extcon device from devicetree.
1350 * @node : OF node identifying edev
1352 * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
1354 struct extcon_dev *extcon_find_edev_by_node(struct device_node *node)
1356 struct extcon_dev *edev;
1358 mutex_lock(&extcon_dev_list_lock);
1359 list_for_each_entry(edev, &extcon_dev_list, entry)
1360 if (edev->dev.parent && edev->dev.parent->of_node == node)
1362 edev = ERR_PTR(-EPROBE_DEFER);
1364 mutex_unlock(&extcon_dev_list_lock);
1370 * extcon_get_edev_by_phandle - Get the extcon device from devicetree.
1371 * @dev : the instance to the given device
1372 * @index : the index into list of extcon_dev
1374 * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
1376 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1378 struct device_node *node;
1379 struct extcon_dev *edev;
1382 return ERR_PTR(-EINVAL);
1384 if (!dev->of_node) {
1385 dev_dbg(dev, "device does not have a device node entry\n");
1386 return ERR_PTR(-EINVAL);
1389 node = of_parse_phandle(dev->of_node, "extcon", index);
1391 dev_dbg(dev, "failed to get phandle in %pOF node\n",
1393 return ERR_PTR(-ENODEV);
1396 edev = extcon_find_edev_by_node(node);
1404 struct extcon_dev *extcon_find_edev_by_node(struct device_node *node)
1406 return ERR_PTR(-ENOSYS);
1409 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1411 return ERR_PTR(-ENOSYS);
1414 #endif /* CONFIG_OF */
1416 EXPORT_SYMBOL_GPL(extcon_find_edev_by_node);
1417 EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1420 * extcon_get_edev_name() - Get the name of the extcon device.
1421 * @edev: the extcon device
1423 const char *extcon_get_edev_name(struct extcon_dev *edev)
1425 return !edev ? NULL : edev->name;
1427 EXPORT_SYMBOL_GPL(extcon_get_edev_name);
1429 static int __init extcon_class_init(void)
1431 return create_extcon_class();
1433 module_init(extcon_class_init);
1435 static void __exit extcon_class_exit(void)
1437 class_destroy(extcon_class);
1439 module_exit(extcon_class_exit);
1441 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1442 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1443 MODULE_DESCRIPTION("External Connector (extcon) framework");
1444 MODULE_LICENSE("GPL v2");