2 * drivers/extcon/extcon.c - External Connector (extcon) framework.
4 * Copyright (C) 2015 Samsung Electronics
5 * Author: Chanwoo Choi <cw00.choi@samsung.com>
7 * Copyright (C) 2012 Samsung Electronics
8 * Author: Donggeun Kim <dg77.kim@samsung.com>
9 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
11 * based on android/drivers/switch/switch_class.c
12 * Copyright (C) 2008 Google, Inc.
13 * Author: Mike Lockwood <lockwood@android.com>
15 * This software is licensed under the terms of the GNU General Public
16 * License version 2, as published by the Free Software Foundation, and
17 * may be copied, distributed, and modified under those terms.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
25 #include <linux/module.h>
26 #include <linux/types.h>
27 #include <linux/init.h>
28 #include <linux/device.h>
30 #include <linux/err.h>
32 #include <linux/slab.h>
33 #include <linux/sysfs.h>
37 #define SUPPORTED_CABLE_MAX 32
39 struct __extcon_info {
46 .type = EXTCON_TYPE_MISC,
51 /* USB external connector */
53 .type = EXTCON_TYPE_USB,
58 .type = EXTCON_TYPE_USB,
59 .id = EXTCON_USB_HOST,
63 /* Charging external connector */
64 [EXTCON_CHG_USB_SDP] = {
65 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
66 .id = EXTCON_CHG_USB_SDP,
69 [EXTCON_CHG_USB_DCP] = {
70 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
71 .id = EXTCON_CHG_USB_DCP,
74 [EXTCON_CHG_USB_CDP] = {
75 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
76 .id = EXTCON_CHG_USB_CDP,
79 [EXTCON_CHG_USB_ACA] = {
80 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
81 .id = EXTCON_CHG_USB_ACA,
84 [EXTCON_CHG_USB_FAST] = {
85 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
86 .id = EXTCON_CHG_USB_FAST,
87 .name = "FAST-CHARGER",
89 [EXTCON_CHG_USB_SLOW] = {
90 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
91 .id = EXTCON_CHG_USB_SLOW,
92 .name = "SLOW-CHARGER",
95 .type = EXTCON_TYPE_CHG,
99 [EXTCON_CHG_USB_PD] = {
100 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
101 .id = EXTCON_CHG_USB_PD,
105 /* Jack external connector */
106 [EXTCON_JACK_MICROPHONE] = {
107 .type = EXTCON_TYPE_JACK,
108 .id = EXTCON_JACK_MICROPHONE,
109 .name = "MICROPHONE",
111 [EXTCON_JACK_HEADPHONE] = {
112 .type = EXTCON_TYPE_JACK,
113 .id = EXTCON_JACK_HEADPHONE,
116 [EXTCON_JACK_LINE_IN] = {
117 .type = EXTCON_TYPE_JACK,
118 .id = EXTCON_JACK_LINE_IN,
121 [EXTCON_JACK_LINE_OUT] = {
122 .type = EXTCON_TYPE_JACK,
123 .id = EXTCON_JACK_LINE_OUT,
126 [EXTCON_JACK_VIDEO_IN] = {
127 .type = EXTCON_TYPE_JACK,
128 .id = EXTCON_JACK_VIDEO_IN,
131 [EXTCON_JACK_VIDEO_OUT] = {
132 .type = EXTCON_TYPE_JACK,
133 .id = EXTCON_JACK_VIDEO_OUT,
136 [EXTCON_JACK_SPDIF_IN] = {
137 .type = EXTCON_TYPE_JACK,
138 .id = EXTCON_JACK_SPDIF_IN,
141 [EXTCON_JACK_SPDIF_OUT] = {
142 .type = EXTCON_TYPE_JACK,
143 .id = EXTCON_JACK_SPDIF_OUT,
147 /* Display external connector */
148 [EXTCON_DISP_HDMI] = {
149 .type = EXTCON_TYPE_DISP,
150 .id = EXTCON_DISP_HDMI,
153 [EXTCON_DISP_MHL] = {
154 .type = EXTCON_TYPE_DISP,
155 .id = EXTCON_DISP_MHL,
158 [EXTCON_DISP_DVI] = {
159 .type = EXTCON_TYPE_DISP,
160 .id = EXTCON_DISP_DVI,
163 [EXTCON_DISP_VGA] = {
164 .type = EXTCON_TYPE_DISP,
165 .id = EXTCON_DISP_VGA,
169 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
170 .id = EXTCON_DISP_DP,
173 [EXTCON_DISP_HMD] = {
174 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
175 .id = EXTCON_DISP_HMD,
179 /* Miscellaneous external connector */
181 .type = EXTCON_TYPE_MISC,
186 .type = EXTCON_TYPE_MISC,
190 [EXTCON_MECHANICAL] = {
191 .type = EXTCON_TYPE_MISC,
192 .id = EXTCON_MECHANICAL,
193 .name = "MECHANICAL",
200 * struct extcon_cable - An internal data for an external connector.
201 * @edev: the extcon device
202 * @cable_index: the index of this cable in the edev
203 * @attr_g: the attribute group for the cable
204 * @attr_name: "name" sysfs entry
205 * @attr_state: "state" sysfs entry
206 * @attrs: the array pointing to attr_name and attr_state for attr_g
207 * @usb_propval: the array of USB connector properties
208 * @chg_propval: the array of charger connector properties
209 * @jack_propval: the array of jack connector properties
210 * @disp_propval: the array of display connector properties
211 * @usb_bits: the bit array of the USB connector property capabilities
212 * @chg_bits: the bit array of the charger connector property capabilities
213 * @jack_bits: the bit array of the jack connector property capabilities
214 * @disp_bits: the bit array of the display connector property capabilities
216 struct extcon_cable {
217 struct extcon_dev *edev;
220 struct attribute_group attr_g;
221 struct device_attribute attr_name;
222 struct device_attribute attr_state;
224 struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
226 union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
227 union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
228 union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
229 union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
231 unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
232 unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
233 unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
234 unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
237 static struct class *extcon_class;
239 static LIST_HEAD(extcon_dev_list);
240 static DEFINE_MUTEX(extcon_dev_list_lock);
242 static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
246 if (!edev->mutually_exclusive)
249 for (i = 0; edev->mutually_exclusive[i]; i++) {
251 u32 correspondants = new_state & edev->mutually_exclusive[i];
253 /* calculate the total number of bits set */
254 weight = hweight32(correspondants);
262 static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
266 /* Find the the index of extcon cable in edev->supported_cable */
267 for (i = 0; i < edev->max_supported; i++) {
268 if (edev->supported_cable[i] == id)
275 static int get_extcon_type(unsigned int prop)
278 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
279 return EXTCON_TYPE_USB;
280 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
281 return EXTCON_TYPE_CHG;
282 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
283 return EXTCON_TYPE_JACK;
284 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
285 return EXTCON_TYPE_DISP;
291 static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
293 return !!(edev->state & BIT(index));
296 static bool is_extcon_changed(struct extcon_dev *edev, int index,
299 int state = !!(edev->state & BIT(index));
300 return (state != new_state);
303 static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
307 /* Check whether the property is supported or not. */
308 type = get_extcon_type(prop);
312 /* Check whether a specific extcon id supports the property or not. */
313 return !!(extcon_info[id].type & type);
316 static int is_extcon_property_capability(struct extcon_dev *edev,
317 unsigned int id, int index,unsigned int prop)
319 struct extcon_cable *cable;
322 /* Check whether the property is supported or not. */
323 type = get_extcon_type(prop);
327 cable = &edev->cables[index];
330 case EXTCON_TYPE_USB:
331 ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
333 case EXTCON_TYPE_CHG:
334 ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
336 case EXTCON_TYPE_JACK:
337 ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
339 case EXTCON_TYPE_DISP:
340 ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
349 static void init_property(struct extcon_dev *edev, unsigned int id, int index)
351 unsigned int type = extcon_info[id].type;
352 struct extcon_cable *cable = &edev->cables[index];
354 if (EXTCON_TYPE_USB & type)
355 memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
356 if (EXTCON_TYPE_CHG & type)
357 memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
358 if (EXTCON_TYPE_JACK & type)
359 memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
360 if (EXTCON_TYPE_DISP & type)
361 memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
364 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
368 struct extcon_dev *edev = dev_get_drvdata(dev);
370 if (edev->max_supported == 0)
371 return sprintf(buf, "%u\n", edev->state);
373 for (i = 0; i < edev->max_supported; i++) {
374 count += sprintf(buf + count, "%s=%d\n",
375 extcon_info[edev->supported_cable[i]].name,
376 !!(edev->state & BIT(i)));
381 static DEVICE_ATTR_RO(state);
383 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
386 struct extcon_dev *edev = dev_get_drvdata(dev);
388 return sprintf(buf, "%s\n", edev->name);
390 static DEVICE_ATTR_RO(name);
392 static ssize_t cable_name_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
395 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
397 int i = cable->cable_index;
399 return sprintf(buf, "%s\n",
400 extcon_info[cable->edev->supported_cable[i]].name);
403 static ssize_t cable_state_show(struct device *dev,
404 struct device_attribute *attr, char *buf)
406 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
409 int i = cable->cable_index;
411 return sprintf(buf, "%d\n",
412 extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
416 * extcon_sync() - Synchronize the state for an external connector.
417 * @edev: the extcon device
419 * Note that this function send a notification in order to synchronize
420 * the state and property of an external connector.
422 * Returns 0 if success or error number if fail.
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);
444 state = !!(edev->state & BIT(index));
445 spin_unlock_irqrestore(&edev->lock, flags);
448 * Call functions in a raw notifier chain for the specific one
449 * external connector.
451 raw_notifier_call_chain(&edev->nh[index], state, edev);
454 * Call functions in a raw notifier chain for the all supported
455 * external connectors.
457 raw_notifier_call_chain(&edev->nh_all, state, edev);
459 spin_lock_irqsave(&edev->lock, flags);
460 /* This could be in interrupt handler */
461 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
463 /* Unlock early before uevent */
464 spin_unlock_irqrestore(&edev->lock, flags);
466 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
467 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
472 length = name_show(&edev->dev, NULL, prop_buf);
474 if (prop_buf[length - 1] == '\n')
475 prop_buf[length - 1] = 0;
476 snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
477 envp[env_offset++] = name_buf;
480 length = state_show(&edev->dev, NULL, prop_buf);
482 if (prop_buf[length - 1] == '\n')
483 prop_buf[length - 1] = 0;
484 snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
485 envp[env_offset++] = state_buf;
487 envp[env_offset] = NULL;
489 /* Unlock early before uevent */
490 spin_unlock_irqrestore(&edev->lock, flags);
491 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
492 free_page((unsigned long)prop_buf);
496 EXPORT_SYMBOL_GPL(extcon_sync);
499 * extcon_get_state() - Get the state of an external connector.
500 * @edev: the extcon device
501 * @id: the unique id indicating an external connector
503 * Returns 0 if success or error number if fail.
505 int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
513 index = find_cable_index_by_id(edev, id);
517 spin_lock_irqsave(&edev->lock, flags);
518 state = is_extcon_attached(edev, index);
519 spin_unlock_irqrestore(&edev->lock, flags);
523 EXPORT_SYMBOL_GPL(extcon_get_state);
526 * extcon_set_state() - Set the state of an external connector.
527 * @edev: the extcon device
528 * @id: the unique id indicating an external connector
529 * @state: the new state of an external connector.
530 * the default semantics is true: attached / false: detached.
532 * Note that this function set the state of an external connector without
533 * a notification. To synchronize the state of an external connector,
534 * have to use extcon_set_state_sync() and extcon_sync().
536 * Returns 0 if success or error number if fail.
538 int extcon_set_state(struct extcon_dev *edev, unsigned int id, bool state)
546 index = find_cable_index_by_id(edev, id);
550 spin_lock_irqsave(&edev->lock, flags);
552 /* Check whether the external connector's state is changed. */
553 if (!is_extcon_changed(edev, index, state))
556 if (check_mutually_exclusive(edev,
557 (edev->state & ~BIT(index)) | (state & BIT(index)))) {
563 * Initialize the value of extcon property before setting
564 * the detached state for an external connector.
567 init_property(edev, id, index);
569 /* Update the state for an external connector. */
571 edev->state |= BIT(index);
573 edev->state &= ~(BIT(index));
575 spin_unlock_irqrestore(&edev->lock, flags);
579 EXPORT_SYMBOL_GPL(extcon_set_state);
582 * extcon_set_state_sync() - Set the state of an external connector with sync.
583 * @edev: the extcon device
584 * @id: the unique id indicating an external connector
585 * @state: the new state of external connector.
586 * the default semantics is true: attached / false: detached.
588 * Note that this function set the state of external connector
589 * and synchronize the state by sending a notification.
591 * Returns 0 if success or error number if fail.
593 int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id, bool state)
598 index = find_cable_index_by_id(edev, id);
602 /* Check whether the external connector's state is changed. */
603 spin_lock_irqsave(&edev->lock, flags);
604 ret = is_extcon_changed(edev, index, state);
605 spin_unlock_irqrestore(&edev->lock, flags);
609 ret = extcon_set_state(edev, id, state);
613 return extcon_sync(edev, id);
615 EXPORT_SYMBOL_GPL(extcon_set_state_sync);
618 * extcon_get_property() - Get the property value of an external connector.
619 * @edev: the extcon device
620 * @id: the unique id indicating an external connector
621 * @prop: the property id indicating an extcon property
622 * @prop_val: the pointer which store the value of extcon property
624 * Note that when getting the property value of external connector,
625 * the external connector should be attached. If detached state, function
626 * return 0 without property value. Also, the each property should be
627 * included in the list of supported properties according to extcon type.
629 * Returns 0 if success or error number if fail.
631 int extcon_get_property(struct extcon_dev *edev, unsigned int id,
633 union extcon_property_value *prop_val)
635 struct extcon_cable *cable;
639 *prop_val = (union extcon_property_value)(0);
644 /* Check whether the property is supported or not */
645 if (!is_extcon_property_supported(id, prop))
648 /* Find the cable index of external connector by using id */
649 index = find_cable_index_by_id(edev, id);
653 spin_lock_irqsave(&edev->lock, flags);
655 /* Check whether the property is available or not. */
656 if (!is_extcon_property_capability(edev, id, index, prop)) {
657 spin_unlock_irqrestore(&edev->lock, flags);
662 * Check whether the external connector is attached.
663 * If external connector is detached, the user can not
664 * get the property value.
666 if (!is_extcon_attached(edev, index)) {
667 spin_unlock_irqrestore(&edev->lock, flags);
671 cable = &edev->cables[index];
673 /* Get the property value according to extcon type */
675 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
676 *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
678 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
679 *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
681 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
682 *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
684 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
685 *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
692 spin_unlock_irqrestore(&edev->lock, flags);
696 EXPORT_SYMBOL_GPL(extcon_get_property);
699 * extcon_set_property() - Set the property value of an external connector.
700 * @edev: the extcon device
701 * @id: the unique id indicating an external connector
702 * @prop: the property id indicating an extcon property
703 * @prop_val: the pointer including the new value of extcon property
705 * Note that each property should be included in the list of supported
706 * properties according to the extcon type.
708 * Returns 0 if success or error number if fail.
710 int extcon_set_property(struct extcon_dev *edev, unsigned int id,
712 union extcon_property_value prop_val)
714 struct extcon_cable *cable;
721 /* Check whether the property is supported or not */
722 if (!is_extcon_property_supported(id, prop))
725 /* Find the cable index of external connector by using id */
726 index = find_cable_index_by_id(edev, id);
730 spin_lock_irqsave(&edev->lock, flags);
732 /* Check whether the property is available or not. */
733 if (!is_extcon_property_capability(edev, id, index, prop)) {
734 spin_unlock_irqrestore(&edev->lock, flags);
738 cable = &edev->cables[index];
740 /* Set the property value according to extcon type */
742 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
743 cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
745 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
746 cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
748 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
749 cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
751 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
752 cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
759 spin_unlock_irqrestore(&edev->lock, flags);
763 EXPORT_SYMBOL_GPL(extcon_set_property);
766 * extcon_set_property_sync() - Set property of an external connector with sync.
767 * @prop_val: the pointer including the new value of extcon property
769 * Note that when setting the property value of external connector,
770 * the external connector should be attached. The each property should
771 * be included in the list of supported properties according to extcon type.
773 * Returns 0 if success or error number if fail.
775 int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
777 union extcon_property_value prop_val)
781 ret = extcon_set_property(edev, id, prop, prop_val);
785 return extcon_sync(edev, id);
787 EXPORT_SYMBOL_GPL(extcon_set_property_sync);
790 * extcon_get_property_capability() - Get the capability of the property
791 * for an external connector.
792 * @edev: the extcon device
793 * @id: the unique id indicating an external connector
794 * @prop: the property id indicating an extcon property
796 * Returns 1 if the property is available or 0 if not available.
798 int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
806 /* Check whether the property is supported or not */
807 if (!is_extcon_property_supported(id, prop))
810 /* Find the cable index of external connector by using id */
811 index = find_cable_index_by_id(edev, id);
815 return is_extcon_property_capability(edev, id, index, prop);
817 EXPORT_SYMBOL_GPL(extcon_get_property_capability);
820 * extcon_set_property_capability() - Set the capability of the property
821 * for an external connector.
822 * @edev: the extcon device
823 * @id: the unique id indicating an external connector
824 * @prop: the property id indicating an extcon property
826 * Note that this function set the capability of the property
827 * for an external connector in order to mark the bit in capability
828 * bitmap which mean the available state of the property.
830 * Returns 0 if success or error number if fail.
832 int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
835 struct extcon_cable *cable;
836 int index, type, ret = 0;
841 /* Check whether the property is supported or not. */
842 if (!is_extcon_property_supported(id, prop))
845 /* Find the cable index of external connector by using id. */
846 index = find_cable_index_by_id(edev, id);
850 type = get_extcon_type(prop);
854 cable = &edev->cables[index];
857 case EXTCON_TYPE_USB:
858 __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
860 case EXTCON_TYPE_CHG:
861 __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
863 case EXTCON_TYPE_JACK:
864 __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
866 case EXTCON_TYPE_DISP:
867 __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
875 EXPORT_SYMBOL_GPL(extcon_set_property_capability);
878 * extcon_get_extcon_dev() - Get the extcon device instance from the name.
879 * @extcon_name: the extcon name provided with extcon_dev_register()
881 * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
883 struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
885 struct extcon_dev *sd;
888 return ERR_PTR(-EINVAL);
890 mutex_lock(&extcon_dev_list_lock);
891 list_for_each_entry(sd, &extcon_dev_list, entry) {
892 if (!strcmp(sd->name, extcon_name))
897 mutex_unlock(&extcon_dev_list_lock);
900 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
903 * extcon_register_notifier() - Register a notifier block to get notified by
904 * any state changes from the extcon.
905 * @edev: the extcon device
906 * @id: the unique id indicating an external connector
907 * @nb: a notifier block to be registered
909 * Note that the second parameter given to the callback of nb (val) is
910 * the current state of an external connector and the third pameter
911 * is the pointer of extcon device.
913 * Returns 0 if success or error number if fail.
915 int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
916 struct notifier_block *nb)
919 int ret, idx = -EINVAL;
924 idx = find_cable_index_by_id(edev, id);
928 spin_lock_irqsave(&edev->lock, flags);
929 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
930 spin_unlock_irqrestore(&edev->lock, flags);
934 EXPORT_SYMBOL_GPL(extcon_register_notifier);
937 * extcon_unregister_notifier() - Unregister a notifier block from the extcon.
938 * @edev: the extcon device
939 * @id: the unique id indicating an external connector
940 * @nb: a notifier block to be registered
942 * Returns 0 if success or error number if fail.
944 int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
945 struct notifier_block *nb)
953 idx = find_cable_index_by_id(edev, id);
957 spin_lock_irqsave(&edev->lock, flags);
958 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
959 spin_unlock_irqrestore(&edev->lock, flags);
963 EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
966 * extcon_register_notifier_all() - Register a notifier block for all connectors.
967 * @edev: the extcon device
968 * @nb: a notifier block to be registered
970 * Note that this function registers a notifier block in order to receive
971 * the state change of all supported external connectors from extcon device.
972 * And the second parameter given to the callback of nb (val) is
973 * the current state and the third pameter is the pointer of extcon device.
975 * Returns 0 if success or error number if fail.
977 int extcon_register_notifier_all(struct extcon_dev *edev,
978 struct notifier_block *nb)
986 spin_lock_irqsave(&edev->lock, flags);
987 ret = raw_notifier_chain_register(&edev->nh_all, nb);
988 spin_unlock_irqrestore(&edev->lock, flags);
992 EXPORT_SYMBOL_GPL(extcon_register_notifier_all);
995 * extcon_unregister_notifier_all() - Unregister a notifier block from extcon.
996 * @edev: the extcon device
997 * @nb: a notifier block to be registered
999 * Returns 0 if success or error number if fail.
1001 int extcon_unregister_notifier_all(struct extcon_dev *edev,
1002 struct notifier_block *nb)
1004 unsigned long flags;
1010 spin_lock_irqsave(&edev->lock, flags);
1011 ret = raw_notifier_chain_unregister(&edev->nh_all, nb);
1012 spin_unlock_irqrestore(&edev->lock, flags);
1016 EXPORT_SYMBOL_GPL(extcon_unregister_notifier_all);
1018 static struct attribute *extcon_attrs[] = {
1019 &dev_attr_state.attr,
1020 &dev_attr_name.attr,
1023 ATTRIBUTE_GROUPS(extcon);
1025 static int create_extcon_class(void)
1027 if (!extcon_class) {
1028 extcon_class = class_create(THIS_MODULE, "extcon");
1029 if (IS_ERR(extcon_class))
1030 return PTR_ERR(extcon_class);
1031 extcon_class->dev_groups = extcon_groups;
1037 static void extcon_dev_release(struct device *dev)
1041 static const char *muex_name = "mutually_exclusive";
1042 static void dummy_sysfs_dev_release(struct device *dev)
1047 * extcon_dev_allocate() - Allocate the memory of extcon device.
1048 * @supported_cable: the array of the supported external connectors
1049 * ending with EXTCON_NONE.
1051 * Note that this function allocates the memory for extcon device
1052 * and initialize default setting for the extcon device.
1054 * Returns the pointer memory of allocated extcon_dev if success
1055 * or ERR_PTR(err) if fail.
1057 struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
1059 struct extcon_dev *edev;
1061 if (!supported_cable)
1062 return ERR_PTR(-EINVAL);
1064 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
1066 return ERR_PTR(-ENOMEM);
1068 edev->max_supported = 0;
1069 edev->supported_cable = supported_cable;
1075 * extcon_dev_free() - Free the memory of extcon device.
1076 * @edev: the extcon device
1078 void extcon_dev_free(struct extcon_dev *edev)
1082 EXPORT_SYMBOL_GPL(extcon_dev_free);
1085 * extcon_dev_register() - Register an new extcon device
1086 * @edev: the extcon device to be registered
1088 * Among the members of edev struct, please set the "user initializing data"
1089 * do not set the values of "internal data", which are initialized by
1092 * Note that before calling this funciton, have to allocate the memory
1093 * of an extcon device by using the extcon_dev_allocate(). And the extcon
1094 * dev should include the supported_cable information.
1096 * Returns 0 if success or error number if fail.
1098 int extcon_dev_register(struct extcon_dev *edev)
1101 static atomic_t edev_no = ATOMIC_INIT(-1);
1103 if (!extcon_class) {
1104 ret = create_extcon_class();
1109 if (!edev || !edev->supported_cable)
1112 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
1114 edev->max_supported = index;
1115 if (index > SUPPORTED_CABLE_MAX) {
1117 "exceed the maximum number of supported cables\n");
1121 edev->dev.class = extcon_class;
1122 edev->dev.release = extcon_dev_release;
1124 edev->name = dev_name(edev->dev.parent);
1125 if (IS_ERR_OR_NULL(edev->name)) {
1127 "extcon device name is null\n");
1130 dev_set_name(&edev->dev, "extcon%lu",
1131 (unsigned long)atomic_inc_return(&edev_no));
1133 if (edev->max_supported) {
1136 struct extcon_cable *cable;
1138 edev->cables = kzalloc(sizeof(struct extcon_cable) *
1139 edev->max_supported, GFP_KERNEL);
1140 if (!edev->cables) {
1142 goto err_sysfs_alloc;
1144 for (index = 0; index < edev->max_supported; index++) {
1145 cable = &edev->cables[index];
1147 snprintf(buf, 10, "cable.%d", index);
1148 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
1151 for (index--; index >= 0; index--) {
1152 cable = &edev->cables[index];
1153 kfree(cable->attr_g.name);
1157 goto err_alloc_cables;
1162 cable->cable_index = index;
1163 cable->attrs[0] = &cable->attr_name.attr;
1164 cable->attrs[1] = &cable->attr_state.attr;
1165 cable->attrs[2] = NULL;
1166 cable->attr_g.name = str;
1167 cable->attr_g.attrs = cable->attrs;
1169 sysfs_attr_init(&cable->attr_name.attr);
1170 cable->attr_name.attr.name = "name";
1171 cable->attr_name.attr.mode = 0444;
1172 cable->attr_name.show = cable_name_show;
1174 sysfs_attr_init(&cable->attr_state.attr);
1175 cable->attr_state.attr.name = "state";
1176 cable->attr_state.attr.mode = 0444;
1177 cable->attr_state.show = cable_state_show;
1181 if (edev->max_supported && edev->mutually_exclusive) {
1185 /* Count the size of mutually_exclusive array */
1186 for (index = 0; edev->mutually_exclusive[index]; index++)
1189 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
1190 (index + 1), GFP_KERNEL);
1191 if (!edev->attrs_muex) {
1196 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
1198 if (!edev->d_attrs_muex) {
1200 kfree(edev->attrs_muex);
1204 for (index = 0; edev->mutually_exclusive[index]; index++) {
1205 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
1206 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
1209 for (index--; index >= 0; index--) {
1210 kfree(edev->d_attrs_muex[index].attr.
1213 kfree(edev->d_attrs_muex);
1214 kfree(edev->attrs_muex);
1219 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
1220 edev->d_attrs_muex[index].attr.name = name;
1221 edev->d_attrs_muex[index].attr.mode = 0000;
1222 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
1225 edev->attr_g_muex.name = muex_name;
1226 edev->attr_g_muex.attrs = edev->attrs_muex;
1230 if (edev->max_supported) {
1231 edev->extcon_dev_type.groups =
1232 kzalloc(sizeof(struct attribute_group *) *
1233 (edev->max_supported + 2), GFP_KERNEL);
1234 if (!edev->extcon_dev_type.groups) {
1236 goto err_alloc_groups;
1239 edev->extcon_dev_type.name = dev_name(&edev->dev);
1240 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
1242 for (index = 0; index < edev->max_supported; index++)
1243 edev->extcon_dev_type.groups[index] =
1244 &edev->cables[index].attr_g;
1245 if (edev->mutually_exclusive)
1246 edev->extcon_dev_type.groups[index] =
1249 edev->dev.type = &edev->extcon_dev_type;
1252 spin_lock_init(&edev->lock);
1253 if (edev->max_supported) {
1254 edev->nh = kcalloc(edev->max_supported, sizeof(*edev->nh),
1262 for (index = 0; index < edev->max_supported; index++)
1263 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
1265 RAW_INIT_NOTIFIER_HEAD(&edev->nh_all);
1267 dev_set_drvdata(&edev->dev, edev);
1270 ret = device_register(&edev->dev);
1272 put_device(&edev->dev);
1276 mutex_lock(&extcon_dev_list_lock);
1277 list_add(&edev->entry, &extcon_dev_list);
1278 mutex_unlock(&extcon_dev_list_lock);
1283 if (edev->max_supported)
1286 if (edev->max_supported)
1287 kfree(edev->extcon_dev_type.groups);
1289 if (edev->max_supported && edev->mutually_exclusive) {
1290 for (index = 0; edev->mutually_exclusive[index]; index++)
1291 kfree(edev->d_attrs_muex[index].attr.name);
1292 kfree(edev->d_attrs_muex);
1293 kfree(edev->attrs_muex);
1296 for (index = 0; index < edev->max_supported; index++)
1297 kfree(edev->cables[index].attr_g.name);
1299 if (edev->max_supported)
1300 kfree(edev->cables);
1304 EXPORT_SYMBOL_GPL(extcon_dev_register);
1307 * extcon_dev_unregister() - Unregister the extcon device.
1308 * @edev: the extcon device to be unregistered.
1310 * Note that this does not call kfree(edev) because edev was not allocated
1313 void extcon_dev_unregister(struct extcon_dev *edev)
1320 mutex_lock(&extcon_dev_list_lock);
1321 list_del(&edev->entry);
1322 mutex_unlock(&extcon_dev_list_lock);
1324 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
1325 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
1326 dev_name(&edev->dev));
1330 device_unregister(&edev->dev);
1332 if (edev->mutually_exclusive && edev->max_supported) {
1333 for (index = 0; edev->mutually_exclusive[index];
1335 kfree(edev->d_attrs_muex[index].attr.name);
1336 kfree(edev->d_attrs_muex);
1337 kfree(edev->attrs_muex);
1340 for (index = 0; index < edev->max_supported; index++)
1341 kfree(edev->cables[index].attr_g.name);
1343 if (edev->max_supported) {
1344 kfree(edev->extcon_dev_type.groups);
1345 kfree(edev->cables);
1349 put_device(&edev->dev);
1351 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1355 * extcon_get_edev_by_phandle - Get the extcon device from devicetree.
1356 * @dev : the instance to the given device
1357 * @index : the index into list of extcon_dev
1359 * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
1361 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1363 struct device_node *node;
1364 struct extcon_dev *edev;
1367 return ERR_PTR(-EINVAL);
1369 if (!dev->of_node) {
1370 dev_dbg(dev, "device does not have a device node entry\n");
1371 return ERR_PTR(-EINVAL);
1374 node = of_parse_phandle(dev->of_node, "extcon", index);
1376 dev_dbg(dev, "failed to get phandle in %pOF node\n",
1378 return ERR_PTR(-ENODEV);
1381 mutex_lock(&extcon_dev_list_lock);
1382 list_for_each_entry(edev, &extcon_dev_list, entry) {
1383 if (edev->dev.parent && edev->dev.parent->of_node == node) {
1384 mutex_unlock(&extcon_dev_list_lock);
1389 mutex_unlock(&extcon_dev_list_lock);
1392 return ERR_PTR(-EPROBE_DEFER);
1395 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1397 return ERR_PTR(-ENOSYS);
1399 #endif /* CONFIG_OF */
1400 EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1403 * extcon_get_edev_name() - Get the name of the extcon device.
1404 * @edev: the extcon device
1406 const char *extcon_get_edev_name(struct extcon_dev *edev)
1408 return !edev ? NULL : edev->name;
1411 static int __init extcon_class_init(void)
1413 return create_extcon_class();
1415 module_init(extcon_class_init);
1417 static void __exit extcon_class_exit(void)
1419 class_destroy(extcon_class);
1421 module_exit(extcon_class_exit);
1423 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1424 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1425 MODULE_DESCRIPTION("External Connector (extcon) framework");
1426 MODULE_LICENSE("GPL v2");