2 * drivers/extcon/extcon.c - External Connector (extcon) framework.
4 * External connector (extcon) class driver
6 * Copyright (C) 2015 Samsung Electronics
7 * Author: Chanwoo Choi <cw00.choi@samsung.com>
9 * Copyright (C) 2012 Samsung Electronics
10 * Author: Donggeun Kim <dg77.kim@samsung.com>
11 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
13 * based on android/drivers/switch/switch_class.c
14 * Copyright (C) 2008 Google, Inc.
15 * Author: Mike Lockwood <lockwood@android.com>
17 * This software is licensed under the terms of the GNU General Public
18 * License version 2, as published by the Free Software Foundation, and
19 * may be copied, distributed, and modified under those terms.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
27 #include <linux/module.h>
28 #include <linux/types.h>
29 #include <linux/init.h>
30 #include <linux/device.h>
32 #include <linux/err.h>
33 #include <linux/extcon.h>
35 #include <linux/slab.h>
36 #include <linux/sysfs.h>
38 #define SUPPORTED_CABLE_MAX 32
39 #define CABLE_NAME_MAX 30
41 static const char *extcon_name
[] = {
42 [EXTCON_NONE
] = "NONE",
44 /* USB external connector */
46 [EXTCON_USB_HOST
] = "USB-HOST",
48 /* Charger external connector */
50 [EXTCON_FAST_CHARGER
] = "FAST-CHARGER",
51 [EXTCON_SLOW_CHARGER
] = "SLOW-CHARGER",
52 [EXTCON_CHARGE_DOWNSTREAM
] = "CHARGE-DOWNSTREAM",
54 /* Audio/Video external connector */
55 [EXTCON_LINE_IN
] = "LINE-IN",
56 [EXTCON_LINE_OUT
] = "LINE-OUT",
57 [EXTCON_MICROPHONE
] = "MICROPHONE",
58 [EXTCON_HEADPHONE
] = "HEADPHONE",
60 [EXTCON_HDMI
] = "HDMI",
64 [EXTCON_SPDIF_IN
] = "SPDIF-IN",
65 [EXTCON_SPDIF_OUT
] = "SPDIF-OUT",
66 [EXTCON_VIDEO_IN
] = "VIDEO-IN",
67 [EXTCON_VIDEO_OUT
] = "VIDEO-OUT",
69 /* Etc external connector */
70 [EXTCON_DOCK
] = "DOCK",
72 [EXTCON_MECHANICAL
] = "MECHANICAL",
77 static struct class *extcon_class
;
78 #if defined(CONFIG_ANDROID)
79 static struct class_compat
*switch_class
;
80 #endif /* CONFIG_ANDROID */
82 static LIST_HEAD(extcon_dev_list
);
83 static DEFINE_MUTEX(extcon_dev_list_lock
);
86 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
88 * @edev: the extcon device
89 * @new_state: new cable attach status for @edev
91 * Returns 0 if nothing violates. Returns the index + 1 for the first
94 static int check_mutually_exclusive(struct extcon_dev
*edev
, u32 new_state
)
98 if (!edev
->mutually_exclusive
)
101 for (i
= 0; edev
->mutually_exclusive
[i
]; i
++) {
103 u32 correspondants
= new_state
& edev
->mutually_exclusive
[i
];
105 /* calculate the total number of bits set */
106 weight
= hweight32(correspondants
);
114 static int find_cable_index_by_id(struct extcon_dev
*edev
, const unsigned int id
)
118 /* Find the the index of extcon cable in edev->supported_cable */
119 for (i
= 0; i
< edev
->max_supported
; i
++) {
120 if (edev
->supported_cable
[i
] == id
)
127 static int find_cable_id_by_name(struct extcon_dev
*edev
, const char *name
)
132 /* Find the id of extcon cable */
133 while (extcon_name
[i
]) {
134 if (!strncmp(extcon_name
[i
], name
, CABLE_NAME_MAX
)) {
144 static int find_cable_index_by_name(struct extcon_dev
*edev
, const char *name
)
148 if (edev
->max_supported
== 0)
151 /* Find the the number of extcon cable */
152 id
= find_cable_id_by_name(edev
, name
);
156 return find_cable_index_by_id(edev
, id
);
159 static bool is_extcon_changed(u32 prev
, u32
new, int idx
, bool *attached
)
161 if (((prev
>> idx
) & 0x1) != ((new >> idx
) & 0x1)) {
162 *attached
= ((new >> idx
) & 0x1) ? true : false;
169 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
173 struct extcon_dev
*edev
= dev_get_drvdata(dev
);
175 if (edev
->max_supported
== 0)
176 return sprintf(buf
, "%u\n", edev
->state
);
178 for (i
= 0; i
< edev
->max_supported
; i
++) {
179 count
+= sprintf(buf
+ count
, "%s=%d\n",
180 extcon_name
[edev
->supported_cable
[i
]],
181 !!(edev
->state
& (1 << i
)));
187 static ssize_t
state_store(struct device
*dev
, struct device_attribute
*attr
,
188 const char *buf
, size_t count
)
192 struct extcon_dev
*edev
= dev_get_drvdata(dev
);
194 ret
= sscanf(buf
, "0x%x", &state
);
198 ret
= extcon_set_state(edev
, state
);
205 static DEVICE_ATTR_RW(state
);
207 static ssize_t
name_show(struct device
*dev
, struct device_attribute
*attr
,
210 struct extcon_dev
*edev
= dev_get_drvdata(dev
);
212 return sprintf(buf
, "%s\n", edev
->name
);
214 static DEVICE_ATTR_RO(name
);
216 static ssize_t
cable_name_show(struct device
*dev
,
217 struct device_attribute
*attr
, char *buf
)
219 struct extcon_cable
*cable
= container_of(attr
, struct extcon_cable
,
221 int i
= cable
->cable_index
;
223 return sprintf(buf
, "%s\n",
224 extcon_name
[cable
->edev
->supported_cable
[i
]]);
227 static ssize_t
cable_state_show(struct device
*dev
,
228 struct device_attribute
*attr
, char *buf
)
230 struct extcon_cable
*cable
= container_of(attr
, struct extcon_cable
,
233 int i
= cable
->cable_index
;
235 return sprintf(buf
, "%d\n",
236 extcon_get_cable_state_(cable
->edev
,
237 cable
->edev
->supported_cable
[i
]));
241 * extcon_update_state() - Update the cable attach states of the extcon device
242 * only for the masked bits.
243 * @edev: the extcon device
244 * @mask: the bit mask to designate updated bits.
245 * @state: new cable attach status for @edev
247 * Changing the state sends uevent with environment variable containing
248 * the name of extcon device (envp[0]) and the state output (envp[1]).
249 * Tizen uses this format for extcon device to get events from ports.
250 * Android uses this format as well.
252 * Note that the notifier provides which bits are changed in the state
253 * variable with the val parameter (second) to the callback.
255 int extcon_update_state(struct extcon_dev
*edev
, u32 mask
, u32 state
)
270 spin_lock_irqsave(&edev
->lock
, flags
);
272 if (edev
->state
!= ((edev
->state
& ~mask
) | (state
& mask
))) {
275 if (check_mutually_exclusive(edev
, (edev
->state
& ~mask
) |
277 spin_unlock_irqrestore(&edev
->lock
, flags
);
281 old_state
= edev
->state
;
282 edev
->state
&= ~mask
;
283 edev
->state
|= state
& mask
;
285 for (index
= 0; index
< edev
->max_supported
; index
++) {
286 if (is_extcon_changed(old_state
, edev
->state
, index
,
288 raw_notifier_call_chain(&edev
->nh
[index
],
292 /* This could be in interrupt handler */
293 prop_buf
= (char *)get_zeroed_page(GFP_ATOMIC
);
295 length
= name_show(&edev
->dev
, NULL
, prop_buf
);
297 if (prop_buf
[length
- 1] == '\n')
298 prop_buf
[length
- 1] = 0;
299 snprintf(name_buf
, sizeof(name_buf
),
300 "NAME=%s", prop_buf
);
301 envp
[env_offset
++] = name_buf
;
303 length
= state_show(&edev
->dev
, NULL
, prop_buf
);
305 if (prop_buf
[length
- 1] == '\n')
306 prop_buf
[length
- 1] = 0;
307 snprintf(state_buf
, sizeof(state_buf
),
308 "STATE=%s", prop_buf
);
309 envp
[env_offset
++] = state_buf
;
311 envp
[env_offset
] = NULL
;
312 /* Unlock early before uevent */
313 spin_unlock_irqrestore(&edev
->lock
, flags
);
315 kobject_uevent_env(&edev
->dev
.kobj
, KOBJ_CHANGE
, envp
);
316 free_page((unsigned long)prop_buf
);
318 /* Unlock early before uevent */
319 spin_unlock_irqrestore(&edev
->lock
, flags
);
321 dev_err(&edev
->dev
, "out of memory in extcon_set_state\n");
322 kobject_uevent(&edev
->dev
.kobj
, KOBJ_CHANGE
);
326 spin_unlock_irqrestore(&edev
->lock
, flags
);
331 EXPORT_SYMBOL_GPL(extcon_update_state
);
334 * extcon_set_state() - Set the cable attach states of the extcon device.
335 * @edev: the extcon device
336 * @state: new cable attach status for @edev
338 * Note that notifier provides which bits are changed in the state
339 * variable with the val parameter (second) to the callback.
341 int extcon_set_state(struct extcon_dev
*edev
, u32 state
)
346 return extcon_update_state(edev
, 0xffffffff, state
);
348 EXPORT_SYMBOL_GPL(extcon_set_state
);
351 * extcon_get_cable_state_() - Get the status of a specific cable.
352 * @edev: the extcon device that has the cable.
353 * @id: the unique id of each external connector in extcon enumeration.
355 int extcon_get_cable_state_(struct extcon_dev
*edev
, const unsigned int id
)
362 index
= find_cable_index_by_id(edev
, id
);
366 if (edev
->max_supported
&& edev
->max_supported
<= index
)
369 return !!(edev
->state
& (1 << index
));
371 EXPORT_SYMBOL_GPL(extcon_get_cable_state_
);
374 * extcon_get_cable_state() - Get the status of a specific cable.
375 * @edev: the extcon device that has the cable.
376 * @cable_name: cable name.
378 * Note that this is slower than extcon_get_cable_state_.
380 int extcon_get_cable_state(struct extcon_dev
*edev
, const char *cable_name
)
384 id
= find_cable_id_by_name(edev
, cable_name
);
388 return extcon_get_cable_state_(edev
, id
);
390 EXPORT_SYMBOL_GPL(extcon_get_cable_state
);
393 * extcon_set_cable_state_() - Set the status of a specific cable.
394 * @edev: the extcon device that has the cable.
395 * @id: the unique id of each external connector
396 * in extcon enumeration.
397 * @state: the new cable status. The default semantics is
398 * true: attached / false: detached.
400 int extcon_set_cable_state_(struct extcon_dev
*edev
, unsigned int id
,
409 index
= find_cable_index_by_id(edev
, id
);
413 if (edev
->max_supported
&& edev
->max_supported
<= index
)
416 state
= cable_state
? (1 << index
) : 0;
417 return extcon_update_state(edev
, 1 << index
, state
);
419 EXPORT_SYMBOL_GPL(extcon_set_cable_state_
);
422 * extcon_set_cable_state() - Set the status of a specific cable.
423 * @edev: the extcon device that has the cable.
424 * @cable_name: cable name.
425 * @cable_state: the new cable status. The default semantics is
426 * true: attached / false: detached.
428 * Note that this is slower than extcon_set_cable_state_.
430 int extcon_set_cable_state(struct extcon_dev
*edev
,
431 const char *cable_name
, bool cable_state
)
435 id
= find_cable_id_by_name(edev
, cable_name
);
439 return extcon_set_cable_state_(edev
, id
, cable_state
);
441 EXPORT_SYMBOL_GPL(extcon_set_cable_state
);
444 * extcon_get_extcon_dev() - Get the extcon device instance from the name
445 * @extcon_name: The extcon name provided with extcon_dev_register()
447 struct extcon_dev
*extcon_get_extcon_dev(const char *extcon_name
)
449 struct extcon_dev
*sd
;
452 return ERR_PTR(-EINVAL
);
454 mutex_lock(&extcon_dev_list_lock
);
455 list_for_each_entry(sd
, &extcon_dev_list
, entry
) {
456 if (!strcmp(sd
->name
, extcon_name
))
461 mutex_unlock(&extcon_dev_list_lock
);
464 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev
);
467 * extcon_register_interest() - Register a notifier for a state change of a
468 * specific cable, not an entier set of cables of a
470 * @obj: an empty extcon_specific_cable_nb object to be returned.
471 * @extcon_name: the name of extcon device.
472 * if NULL, extcon_register_interest will register
473 * every cable with the target cable_name given.
474 * @cable_name: the target cable name.
475 * @nb: the notifier block to get notified.
477 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
478 * the struct for you.
480 * extcon_register_interest is a helper function for those who want to get
481 * notification for a single specific cable's status change. If a user wants
482 * to get notification for any changes of all cables of a extcon device,
483 * he/she should use the general extcon_register_notifier().
485 * Note that the second parameter given to the callback of nb (val) is
486 * "old_state", not the current state. The current state can be retrieved
487 * by looking at the third pameter (edev pointer)'s state value.
489 int extcon_register_interest(struct extcon_specific_cable_nb
*obj
,
490 const char *extcon_name
, const char *cable_name
,
491 struct notifier_block
*nb
)
496 if (!obj
|| !cable_name
|| !nb
)
500 obj
->edev
= extcon_get_extcon_dev(extcon_name
);
504 obj
->cable_index
= find_cable_index_by_name(obj
->edev
,
506 if (obj
->cable_index
< 0)
507 return obj
->cable_index
;
511 spin_lock_irqsave(&obj
->edev
->lock
, flags
);
512 ret
= raw_notifier_chain_register(
513 &obj
->edev
->nh
[obj
->cable_index
],
515 spin_unlock_irqrestore(&obj
->edev
->lock
, flags
);
517 struct class_dev_iter iter
;
518 struct extcon_dev
*extd
;
523 class_dev_iter_init(&iter
, extcon_class
, NULL
, NULL
);
524 while ((dev
= class_dev_iter_next(&iter
))) {
525 extd
= dev_get_drvdata(dev
);
527 if (find_cable_index_by_name(extd
, cable_name
) < 0)
530 class_dev_iter_exit(&iter
);
531 return extcon_register_interest(obj
, extd
->name
,
540 EXPORT_SYMBOL_GPL(extcon_register_interest
);
543 * extcon_unregister_interest() - Unregister the notifier registered by
544 * extcon_register_interest().
545 * @obj: the extcon_specific_cable_nb object returned by
546 * extcon_register_interest().
548 int extcon_unregister_interest(struct extcon_specific_cable_nb
*obj
)
556 spin_lock_irqsave(&obj
->edev
->lock
, flags
);
557 ret
= raw_notifier_chain_unregister(
558 &obj
->edev
->nh
[obj
->cable_index
], obj
->user_nb
);
559 spin_unlock_irqrestore(&obj
->edev
->lock
, flags
);
563 EXPORT_SYMBOL_GPL(extcon_unregister_interest
);
566 * extcon_register_notifier() - Register a notifiee to get notified by
567 * any attach status changes from the extcon.
568 * @edev: the extcon device that has the external connecotr.
569 * @id: the unique id of each external connector in extcon enumeration.
570 * @nb: a notifier block to be registered.
572 * Note that the second parameter given to the callback of nb (val) is
573 * "old_state", not the current state. The current state can be retrieved
574 * by looking at the third pameter (edev pointer)'s state value.
576 int extcon_register_notifier(struct extcon_dev
*edev
, unsigned int id
,
577 struct notifier_block
*nb
)
585 idx
= find_cable_index_by_id(edev
, id
);
587 spin_lock_irqsave(&edev
->lock
, flags
);
588 ret
= raw_notifier_chain_register(&edev
->nh
[idx
], nb
);
589 spin_unlock_irqrestore(&edev
->lock
, flags
);
593 EXPORT_SYMBOL_GPL(extcon_register_notifier
);
596 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
597 * @edev: the extcon device that has the external connecotr.
598 * @id: the unique id of each external connector in extcon enumeration.
599 * @nb: a notifier block to be registered.
601 int extcon_unregister_notifier(struct extcon_dev
*edev
, unsigned int id
,
602 struct notifier_block
*nb
)
610 idx
= find_cable_index_by_id(edev
, id
);
612 spin_lock_irqsave(&edev
->lock
, flags
);
613 ret
= raw_notifier_chain_unregister(&edev
->nh
[idx
], nb
);
614 spin_unlock_irqrestore(&edev
->lock
, flags
);
618 EXPORT_SYMBOL_GPL(extcon_unregister_notifier
);
620 static struct attribute
*extcon_attrs
[] = {
621 &dev_attr_state
.attr
,
625 ATTRIBUTE_GROUPS(extcon
);
627 static int create_extcon_class(void)
630 extcon_class
= class_create(THIS_MODULE
, "extcon");
631 if (IS_ERR(extcon_class
))
632 return PTR_ERR(extcon_class
);
633 extcon_class
->dev_groups
= extcon_groups
;
635 #if defined(CONFIG_ANDROID)
636 switch_class
= class_compat_register("switch");
637 if (WARN(!switch_class
, "cannot allocate"))
639 #endif /* CONFIG_ANDROID */
645 static void extcon_dev_release(struct device
*dev
)
649 static const char *muex_name
= "mutually_exclusive";
650 static void dummy_sysfs_dev_release(struct device
*dev
)
655 * extcon_dev_allocate() - Allocate the memory of extcon device.
656 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
657 * If supported_cable is NULL, cable name related APIs
660 * This function allocates the memory for extcon device without allocating
661 * memory in each extcon provider driver and initialize default setting for
664 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
666 struct extcon_dev
*extcon_dev_allocate(const unsigned int *supported_cable
)
668 struct extcon_dev
*edev
;
670 if (!supported_cable
)
671 return ERR_PTR(-EINVAL
);
673 edev
= kzalloc(sizeof(*edev
), GFP_KERNEL
);
675 return ERR_PTR(-ENOMEM
);
677 edev
->max_supported
= 0;
678 edev
->supported_cable
= supported_cable
;
684 * extcon_dev_free() - Free the memory of extcon device.
685 * @edev: the extcon device to free
687 void extcon_dev_free(struct extcon_dev
*edev
)
691 EXPORT_SYMBOL_GPL(extcon_dev_free
);
693 static int devm_extcon_dev_match(struct device
*dev
, void *res
, void *data
)
695 struct extcon_dev
**r
= res
;
697 if (WARN_ON(!r
|| !*r
))
703 static void devm_extcon_dev_release(struct device
*dev
, void *res
)
705 extcon_dev_free(*(struct extcon_dev
**)res
);
709 * devm_extcon_dev_allocate - Allocate managed extcon device
710 * @dev: device owning the extcon device being created
711 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
712 * If supported_cable is NULL, cable name related APIs
715 * This function manages automatically the memory of extcon device using device
716 * resource management and simplify the control of freeing the memory of extcon
719 * Returns the pointer memory of allocated extcon_dev if success
720 * or ERR_PTR(err) if fail
722 struct extcon_dev
*devm_extcon_dev_allocate(struct device
*dev
,
723 const unsigned int *supported_cable
)
725 struct extcon_dev
**ptr
, *edev
;
727 ptr
= devres_alloc(devm_extcon_dev_release
, sizeof(*ptr
), GFP_KERNEL
);
729 return ERR_PTR(-ENOMEM
);
731 edev
= extcon_dev_allocate(supported_cable
);
737 edev
->dev
.parent
= dev
;
740 devres_add(dev
, ptr
);
744 EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate
);
746 void devm_extcon_dev_free(struct device
*dev
, struct extcon_dev
*edev
)
748 WARN_ON(devres_release(dev
, devm_extcon_dev_release
,
749 devm_extcon_dev_match
, edev
));
751 EXPORT_SYMBOL_GPL(devm_extcon_dev_free
);
754 * extcon_dev_register() - Register a new extcon device
755 * @edev : the new extcon device (should be allocated before calling)
757 * Among the members of edev struct, please set the "user initializing data"
758 * in any case and set the "optional callbacks" if required. However, please
759 * do not set the values of "internal data", which are initialized by
762 int extcon_dev_register(struct extcon_dev
*edev
)
765 static atomic_t edev_no
= ATOMIC_INIT(-1);
768 ret
= create_extcon_class();
773 if (!edev
|| !edev
->supported_cable
)
776 for (; edev
->supported_cable
[index
] != EXTCON_NONE
; index
++);
778 edev
->max_supported
= index
;
779 if (index
> SUPPORTED_CABLE_MAX
) {
781 "exceed the maximum number of supported cables\n");
785 edev
->dev
.class = extcon_class
;
786 edev
->dev
.release
= extcon_dev_release
;
788 edev
->name
= dev_name(edev
->dev
.parent
);
789 if (IS_ERR_OR_NULL(edev
->name
)) {
791 "extcon device name is null\n");
794 dev_set_name(&edev
->dev
, "extcon%lu",
795 (unsigned long)atomic_inc_return(&edev_no
));
797 if (edev
->max_supported
) {
800 struct extcon_cable
*cable
;
802 edev
->cables
= kzalloc(sizeof(struct extcon_cable
) *
803 edev
->max_supported
, GFP_KERNEL
);
806 goto err_sysfs_alloc
;
808 for (index
= 0; index
< edev
->max_supported
; index
++) {
809 cable
= &edev
->cables
[index
];
811 snprintf(buf
, 10, "cable.%d", index
);
812 str
= kzalloc(sizeof(char) * (strlen(buf
) + 1),
815 for (index
--; index
>= 0; index
--) {
816 cable
= &edev
->cables
[index
];
817 kfree(cable
->attr_g
.name
);
821 goto err_alloc_cables
;
826 cable
->cable_index
= index
;
827 cable
->attrs
[0] = &cable
->attr_name
.attr
;
828 cable
->attrs
[1] = &cable
->attr_state
.attr
;
829 cable
->attrs
[2] = NULL
;
830 cable
->attr_g
.name
= str
;
831 cable
->attr_g
.attrs
= cable
->attrs
;
833 sysfs_attr_init(&cable
->attr_name
.attr
);
834 cable
->attr_name
.attr
.name
= "name";
835 cable
->attr_name
.attr
.mode
= 0444;
836 cable
->attr_name
.show
= cable_name_show
;
838 sysfs_attr_init(&cable
->attr_state
.attr
);
839 cable
->attr_state
.attr
.name
= "state";
840 cable
->attr_state
.attr
.mode
= 0444;
841 cable
->attr_state
.show
= cable_state_show
;
845 if (edev
->max_supported
&& edev
->mutually_exclusive
) {
849 /* Count the size of mutually_exclusive array */
850 for (index
= 0; edev
->mutually_exclusive
[index
]; index
++)
853 edev
->attrs_muex
= kzalloc(sizeof(struct attribute
*) *
854 (index
+ 1), GFP_KERNEL
);
855 if (!edev
->attrs_muex
) {
860 edev
->d_attrs_muex
= kzalloc(sizeof(struct device_attribute
) *
862 if (!edev
->d_attrs_muex
) {
864 kfree(edev
->attrs_muex
);
868 for (index
= 0; edev
->mutually_exclusive
[index
]; index
++) {
869 sprintf(buf
, "0x%x", edev
->mutually_exclusive
[index
]);
870 name
= kzalloc(sizeof(char) * (strlen(buf
) + 1),
873 for (index
--; index
>= 0; index
--) {
874 kfree(edev
->d_attrs_muex
[index
].attr
.
877 kfree(edev
->d_attrs_muex
);
878 kfree(edev
->attrs_muex
);
883 sysfs_attr_init(&edev
->d_attrs_muex
[index
].attr
);
884 edev
->d_attrs_muex
[index
].attr
.name
= name
;
885 edev
->d_attrs_muex
[index
].attr
.mode
= 0000;
886 edev
->attrs_muex
[index
] = &edev
->d_attrs_muex
[index
]
889 edev
->attr_g_muex
.name
= muex_name
;
890 edev
->attr_g_muex
.attrs
= edev
->attrs_muex
;
894 if (edev
->max_supported
) {
895 edev
->extcon_dev_type
.groups
=
896 kzalloc(sizeof(struct attribute_group
*) *
897 (edev
->max_supported
+ 2), GFP_KERNEL
);
898 if (!edev
->extcon_dev_type
.groups
) {
900 goto err_alloc_groups
;
903 edev
->extcon_dev_type
.name
= dev_name(&edev
->dev
);
904 edev
->extcon_dev_type
.release
= dummy_sysfs_dev_release
;
906 for (index
= 0; index
< edev
->max_supported
; index
++)
907 edev
->extcon_dev_type
.groups
[index
] =
908 &edev
->cables
[index
].attr_g
;
909 if (edev
->mutually_exclusive
)
910 edev
->extcon_dev_type
.groups
[index
] =
913 edev
->dev
.type
= &edev
->extcon_dev_type
;
916 ret
= device_register(&edev
->dev
);
918 put_device(&edev
->dev
);
921 #if defined(CONFIG_ANDROID)
923 ret
= class_compat_create_link(switch_class
, &edev
->dev
, NULL
);
924 #endif /* CONFIG_ANDROID */
926 spin_lock_init(&edev
->lock
);
928 edev
->nh
= devm_kzalloc(&edev
->dev
,
929 sizeof(*edev
->nh
) * edev
->max_supported
, GFP_KERNEL
);
935 for (index
= 0; index
< edev
->max_supported
; index
++)
936 RAW_INIT_NOTIFIER_HEAD(&edev
->nh
[index
]);
938 dev_set_drvdata(&edev
->dev
, edev
);
941 mutex_lock(&extcon_dev_list_lock
);
942 list_add(&edev
->entry
, &extcon_dev_list
);
943 mutex_unlock(&extcon_dev_list_lock
);
948 if (edev
->max_supported
)
949 kfree(edev
->extcon_dev_type
.groups
);
951 if (edev
->max_supported
&& edev
->mutually_exclusive
) {
952 for (index
= 0; edev
->mutually_exclusive
[index
]; index
++)
953 kfree(edev
->d_attrs_muex
[index
].attr
.name
);
954 kfree(edev
->d_attrs_muex
);
955 kfree(edev
->attrs_muex
);
958 for (index
= 0; index
< edev
->max_supported
; index
++)
959 kfree(edev
->cables
[index
].attr_g
.name
);
961 if (edev
->max_supported
)
966 EXPORT_SYMBOL_GPL(extcon_dev_register
);
969 * extcon_dev_unregister() - Unregister the extcon device.
970 * @edev: the extcon device instance to be unregistered.
972 * Note that this does not call kfree(edev) because edev was not allocated
975 void extcon_dev_unregister(struct extcon_dev
*edev
)
982 mutex_lock(&extcon_dev_list_lock
);
983 list_del(&edev
->entry
);
984 mutex_unlock(&extcon_dev_list_lock
);
986 if (IS_ERR_OR_NULL(get_device(&edev
->dev
))) {
987 dev_err(&edev
->dev
, "Failed to unregister extcon_dev (%s)\n",
988 dev_name(&edev
->dev
));
992 device_unregister(&edev
->dev
);
994 if (edev
->mutually_exclusive
&& edev
->max_supported
) {
995 for (index
= 0; edev
->mutually_exclusive
[index
];
997 kfree(edev
->d_attrs_muex
[index
].attr
.name
);
998 kfree(edev
->d_attrs_muex
);
999 kfree(edev
->attrs_muex
);
1002 for (index
= 0; index
< edev
->max_supported
; index
++)
1003 kfree(edev
->cables
[index
].attr_g
.name
);
1005 if (edev
->max_supported
) {
1006 kfree(edev
->extcon_dev_type
.groups
);
1007 kfree(edev
->cables
);
1010 #if defined(CONFIG_ANDROID)
1012 class_compat_remove_link(switch_class
, &edev
->dev
, NULL
);
1014 put_device(&edev
->dev
);
1016 EXPORT_SYMBOL_GPL(extcon_dev_unregister
);
1018 static void devm_extcon_dev_unreg(struct device
*dev
, void *res
)
1020 extcon_dev_unregister(*(struct extcon_dev
**)res
);
1024 * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
1025 * @dev: device to allocate extcon device
1026 * @edev: the new extcon device to register
1028 * Managed extcon_dev_register() function. If extcon device is attached with
1029 * this function, that extcon device is automatically unregistered on driver
1030 * detach. Internally this function calls extcon_dev_register() function.
1031 * To get more information, refer that function.
1033 * If extcon device is registered with this function and the device needs to be
1034 * unregistered separately, devm_extcon_dev_unregister() should be used.
1036 * Returns 0 if success or negaive error number if failure.
1038 int devm_extcon_dev_register(struct device
*dev
, struct extcon_dev
*edev
)
1040 struct extcon_dev
**ptr
;
1043 ptr
= devres_alloc(devm_extcon_dev_unreg
, sizeof(*ptr
), GFP_KERNEL
);
1047 ret
= extcon_dev_register(edev
);
1054 devres_add(dev
, ptr
);
1058 EXPORT_SYMBOL_GPL(devm_extcon_dev_register
);
1061 * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
1062 * @dev: device the extcon belongs to
1063 * @edev: the extcon device to unregister
1065 * Unregister extcon device that is registered with devm_extcon_dev_register()
1068 void devm_extcon_dev_unregister(struct device
*dev
, struct extcon_dev
*edev
)
1070 WARN_ON(devres_release(dev
, devm_extcon_dev_unreg
,
1071 devm_extcon_dev_match
, edev
));
1073 EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister
);
1077 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1078 * @dev - instance to the given device
1079 * @index - index into list of extcon_dev
1081 * return the instance of extcon device
1083 struct extcon_dev
*extcon_get_edev_by_phandle(struct device
*dev
, int index
)
1085 struct device_node
*node
;
1086 struct extcon_dev
*edev
;
1089 return ERR_PTR(-EINVAL
);
1091 if (!dev
->of_node
) {
1092 dev_err(dev
, "device does not have a device node entry\n");
1093 return ERR_PTR(-EINVAL
);
1096 node
= of_parse_phandle(dev
->of_node
, "extcon", index
);
1098 dev_err(dev
, "failed to get phandle in %s node\n",
1099 dev
->of_node
->full_name
);
1100 return ERR_PTR(-ENODEV
);
1103 mutex_lock(&extcon_dev_list_lock
);
1104 list_for_each_entry(edev
, &extcon_dev_list
, entry
) {
1105 if (edev
->dev
.parent
&& edev
->dev
.parent
->of_node
== node
) {
1106 mutex_unlock(&extcon_dev_list_lock
);
1110 mutex_unlock(&extcon_dev_list_lock
);
1112 return ERR_PTR(-EPROBE_DEFER
);
1115 struct extcon_dev
*extcon_get_edev_by_phandle(struct device
*dev
, int index
)
1117 return ERR_PTR(-ENOSYS
);
1119 #endif /* CONFIG_OF */
1120 EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle
);
1123 * extcon_get_edev_name() - Get the name of the extcon device.
1124 * @edev: the extcon device
1126 const char *extcon_get_edev_name(struct extcon_dev
*edev
)
1128 return !edev
? NULL
: edev
->name
;
1131 static int __init
extcon_class_init(void)
1133 return create_extcon_class();
1135 module_init(extcon_class_init
);
1137 static void __exit
extcon_class_exit(void)
1139 #if defined(CONFIG_ANDROID)
1140 class_compat_unregister(switch_class
);
1142 class_destroy(extcon_class
);
1144 module_exit(extcon_class_exit
);
1146 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1147 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1148 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1149 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1150 MODULE_DESCRIPTION("External connector (extcon) class driver");
1151 MODULE_LICENSE("GPL");