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
)
129 unsigned int id
= -EINVAL
;
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 ? 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
->print_state
) {
176 int ret
= edev
->print_state(edev
, buf
);
180 /* Use default if failed */
183 if (edev
->max_supported
== 0)
184 return sprintf(buf
, "%u\n", edev
->state
);
186 for (i
= 0; i
< edev
->max_supported
; i
++) {
187 count
+= sprintf(buf
+ count
, "%s=%d\n",
188 extcon_name
[edev
->supported_cable
[i
]],
189 !!(edev
->state
& (1 << i
)));
195 static ssize_t
state_store(struct device
*dev
, struct device_attribute
*attr
,
196 const char *buf
, size_t count
)
200 struct extcon_dev
*edev
= dev_get_drvdata(dev
);
202 ret
= sscanf(buf
, "0x%x", &state
);
206 ret
= extcon_set_state(edev
, state
);
213 static DEVICE_ATTR_RW(state
);
215 static ssize_t
name_show(struct device
*dev
, struct device_attribute
*attr
,
218 struct extcon_dev
*edev
= dev_get_drvdata(dev
);
220 return sprintf(buf
, "%s\n", edev
->name
);
222 static DEVICE_ATTR_RO(name
);
224 static ssize_t
cable_name_show(struct device
*dev
,
225 struct device_attribute
*attr
, char *buf
)
227 struct extcon_cable
*cable
= container_of(attr
, struct extcon_cable
,
229 int i
= cable
->cable_index
;
231 return sprintf(buf
, "%s\n",
232 extcon_name
[cable
->edev
->supported_cable
[i
]]);
235 static ssize_t
cable_state_show(struct device
*dev
,
236 struct device_attribute
*attr
, char *buf
)
238 struct extcon_cable
*cable
= container_of(attr
, struct extcon_cable
,
241 int i
= cable
->cable_index
;
243 return sprintf(buf
, "%d\n",
244 extcon_get_cable_state_(cable
->edev
,
245 cable
->edev
->supported_cable
[i
]));
249 * extcon_update_state() - Update the cable attach states of the extcon device
250 * only for the masked bits.
251 * @edev: the extcon device
252 * @mask: the bit mask to designate updated bits.
253 * @state: new cable attach status for @edev
255 * Changing the state sends uevent with environment variable containing
256 * the name of extcon device (envp[0]) and the state output (envp[1]).
257 * Tizen uses this format for extcon device to get events from ports.
258 * Android uses this format as well.
260 * Note that the notifier provides which bits are changed in the state
261 * variable with the val parameter (second) to the callback.
263 int extcon_update_state(struct extcon_dev
*edev
, u32 mask
, u32 state
)
275 spin_lock_irqsave(&edev
->lock
, flags
);
277 if (edev
->state
!= ((edev
->state
& ~mask
) | (state
& mask
))) {
280 if (check_mutually_exclusive(edev
, (edev
->state
& ~mask
) |
282 spin_unlock_irqrestore(&edev
->lock
, flags
);
286 old_state
= edev
->state
;
287 edev
->state
&= ~mask
;
288 edev
->state
|= state
& mask
;
290 for (index
= 0; index
< edev
->max_supported
; index
++) {
291 if (is_extcon_changed(old_state
, edev
->state
, index
,
293 raw_notifier_call_chain(&edev
->nh
[index
],
297 /* This could be in interrupt handler */
298 prop_buf
= (char *)get_zeroed_page(GFP_ATOMIC
);
300 length
= name_show(&edev
->dev
, NULL
, prop_buf
);
302 if (prop_buf
[length
- 1] == '\n')
303 prop_buf
[length
- 1] = 0;
304 snprintf(name_buf
, sizeof(name_buf
),
305 "NAME=%s", prop_buf
);
306 envp
[env_offset
++] = name_buf
;
308 length
= state_show(&edev
->dev
, NULL
, prop_buf
);
310 if (prop_buf
[length
- 1] == '\n')
311 prop_buf
[length
- 1] = 0;
312 snprintf(state_buf
, sizeof(state_buf
),
313 "STATE=%s", prop_buf
);
314 envp
[env_offset
++] = state_buf
;
316 envp
[env_offset
] = NULL
;
317 /* Unlock early before uevent */
318 spin_unlock_irqrestore(&edev
->lock
, flags
);
320 kobject_uevent_env(&edev
->dev
.kobj
, KOBJ_CHANGE
, envp
);
321 free_page((unsigned long)prop_buf
);
323 /* Unlock early before uevent */
324 spin_unlock_irqrestore(&edev
->lock
, flags
);
326 dev_err(&edev
->dev
, "out of memory in extcon_set_state\n");
327 kobject_uevent(&edev
->dev
.kobj
, KOBJ_CHANGE
);
331 spin_unlock_irqrestore(&edev
->lock
, flags
);
336 EXPORT_SYMBOL_GPL(extcon_update_state
);
339 * extcon_set_state() - Set the cable attach states of the extcon device.
340 * @edev: the extcon device
341 * @state: new cable attach status for @edev
343 * Note that notifier provides which bits are changed in the state
344 * variable with the val parameter (second) to the callback.
346 int extcon_set_state(struct extcon_dev
*edev
, u32 state
)
348 return extcon_update_state(edev
, 0xffffffff, state
);
350 EXPORT_SYMBOL_GPL(extcon_set_state
);
353 * extcon_get_cable_state_() - Get the status of a specific cable.
354 * @edev: the extcon device that has the cable.
355 * @id: the unique id of each external connector in extcon enumeration.
357 int extcon_get_cable_state_(struct extcon_dev
*edev
, const unsigned int id
)
361 index
= find_cable_index_by_id(edev
, id
);
365 if (edev
->max_supported
&& edev
->max_supported
<= index
)
368 return !!(edev
->state
& (1 << index
));
370 EXPORT_SYMBOL_GPL(extcon_get_cable_state_
);
373 * extcon_get_cable_state() - Get the status of a specific cable.
374 * @edev: the extcon device that has the cable.
375 * @cable_name: cable name.
377 * Note that this is slower than extcon_get_cable_state_.
379 int extcon_get_cable_state(struct extcon_dev
*edev
, const char *cable_name
)
383 id
= find_cable_id_by_name(edev
, cable_name
);
387 return extcon_get_cable_state_(edev
, id
);
389 EXPORT_SYMBOL_GPL(extcon_get_cable_state
);
392 * extcon_set_cable_state_() - Set the status of a specific cable.
393 * @edev: the extcon device that has the cable.
394 * @id: the unique id of each external connector
395 * in extcon enumeration.
396 * @state: the new cable status. The default semantics is
397 * true: attached / false: detached.
399 int extcon_set_cable_state_(struct extcon_dev
*edev
, unsigned int id
,
405 index
= find_cable_index_by_id(edev
, id
);
409 if (edev
->max_supported
&& edev
->max_supported
<= index
)
412 state
= cable_state
? (1 << index
) : 0;
413 return extcon_update_state(edev
, 1 << index
, state
);
415 EXPORT_SYMBOL_GPL(extcon_set_cable_state_
);
418 * extcon_set_cable_state() - Set the status of a specific cable.
419 * @edev: the extcon device that has the cable.
420 * @cable_name: cable name.
421 * @cable_state: the new cable status. The default semantics is
422 * true: attached / false: detached.
424 * Note that this is slower than extcon_set_cable_state_.
426 int extcon_set_cable_state(struct extcon_dev
*edev
,
427 const char *cable_name
, bool cable_state
)
431 id
= find_cable_id_by_name(edev
, cable_name
);
435 return extcon_set_cable_state_(edev
, id
, cable_state
);
437 EXPORT_SYMBOL_GPL(extcon_set_cable_state
);
440 * extcon_get_extcon_dev() - Get the extcon device instance from the name
441 * @extcon_name: The extcon name provided with extcon_dev_register()
443 struct extcon_dev
*extcon_get_extcon_dev(const char *extcon_name
)
445 struct extcon_dev
*sd
;
447 mutex_lock(&extcon_dev_list_lock
);
448 list_for_each_entry(sd
, &extcon_dev_list
, entry
) {
449 if (!strcmp(sd
->name
, extcon_name
))
454 mutex_unlock(&extcon_dev_list_lock
);
457 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev
);
460 * extcon_register_interest() - Register a notifier for a state change of a
461 * specific cable, not an entier set of cables of a
463 * @obj: an empty extcon_specific_cable_nb object to be returned.
464 * @extcon_name: the name of extcon device.
465 * if NULL, extcon_register_interest will register
466 * every cable with the target cable_name given.
467 * @cable_name: the target cable name.
468 * @nb: the notifier block to get notified.
470 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
471 * the struct for you.
473 * extcon_register_interest is a helper function for those who want to get
474 * notification for a single specific cable's status change. If a user wants
475 * to get notification for any changes of all cables of a extcon device,
476 * he/she should use the general extcon_register_notifier().
478 * Note that the second parameter given to the callback of nb (val) is
479 * "old_state", not the current state. The current state can be retrieved
480 * by looking at the third pameter (edev pointer)'s state value.
482 int extcon_register_interest(struct extcon_specific_cable_nb
*obj
,
483 const char *extcon_name
, const char *cable_name
,
484 struct notifier_block
*nb
)
489 if (!obj
|| !cable_name
|| !nb
)
493 obj
->edev
= extcon_get_extcon_dev(extcon_name
);
497 obj
->cable_index
= find_cable_index_by_name(obj
->edev
,
499 if (obj
->cable_index
< 0)
500 return obj
->cable_index
;
504 spin_lock_irqsave(&obj
->edev
->lock
, flags
);
505 ret
= raw_notifier_chain_register(
506 &obj
->edev
->nh
[obj
->cable_index
],
508 spin_unlock_irqrestore(&obj
->edev
->lock
, flags
);
510 struct class_dev_iter iter
;
511 struct extcon_dev
*extd
;
516 class_dev_iter_init(&iter
, extcon_class
, NULL
, NULL
);
517 while ((dev
= class_dev_iter_next(&iter
))) {
518 extd
= dev_get_drvdata(dev
);
520 if (find_cable_index_by_name(extd
, cable_name
) < 0)
523 class_dev_iter_exit(&iter
);
524 return extcon_register_interest(obj
, extd
->name
,
533 EXPORT_SYMBOL_GPL(extcon_register_interest
);
536 * extcon_unregister_interest() - Unregister the notifier registered by
537 * extcon_register_interest().
538 * @obj: the extcon_specific_cable_nb object returned by
539 * extcon_register_interest().
541 int extcon_unregister_interest(struct extcon_specific_cable_nb
*obj
)
549 spin_lock_irqsave(&obj
->edev
->lock
, flags
);
550 ret
= raw_notifier_chain_unregister(
551 &obj
->edev
->nh
[obj
->cable_index
], obj
->user_nb
);
552 spin_unlock_irqrestore(&obj
->edev
->lock
, flags
);
556 EXPORT_SYMBOL_GPL(extcon_unregister_interest
);
559 * extcon_register_notifier() - Register a notifiee to get notified by
560 * any attach status changes from the extcon.
561 * @edev: the extcon device that has the external connecotr.
562 * @id: the unique id of each external connector in extcon enumeration.
563 * @nb: a notifier block to be registered.
565 * Note that the second parameter given to the callback of nb (val) is
566 * "old_state", not the current state. The current state can be retrieved
567 * by looking at the third pameter (edev pointer)'s state value.
569 int extcon_register_notifier(struct extcon_dev
*edev
, unsigned int id
,
570 struct notifier_block
*nb
)
575 idx
= find_cable_index_by_id(edev
, id
);
577 spin_lock_irqsave(&edev
->lock
, flags
);
578 ret
= raw_notifier_chain_register(&edev
->nh
[idx
], nb
);
579 spin_unlock_irqrestore(&edev
->lock
, flags
);
583 EXPORT_SYMBOL_GPL(extcon_register_notifier
);
586 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
587 * @edev: the extcon device that has the external connecotr.
588 * @id: the unique id of each external connector in extcon enumeration.
589 * @nb: a notifier block to be registered.
591 int extcon_unregister_notifier(struct extcon_dev
*edev
, unsigned int id
,
592 struct notifier_block
*nb
)
597 idx
= find_cable_index_by_id(edev
, id
);
599 spin_lock_irqsave(&edev
->lock
, flags
);
600 ret
= raw_notifier_chain_unregister(&edev
->nh
[idx
], nb
);
601 spin_unlock_irqrestore(&edev
->lock
, flags
);
605 EXPORT_SYMBOL_GPL(extcon_unregister_notifier
);
607 static struct attribute
*extcon_attrs
[] = {
608 &dev_attr_state
.attr
,
612 ATTRIBUTE_GROUPS(extcon
);
614 static int create_extcon_class(void)
617 extcon_class
= class_create(THIS_MODULE
, "extcon");
618 if (IS_ERR(extcon_class
))
619 return PTR_ERR(extcon_class
);
620 extcon_class
->dev_groups
= extcon_groups
;
622 #if defined(CONFIG_ANDROID)
623 switch_class
= class_compat_register("switch");
624 if (WARN(!switch_class
, "cannot allocate"))
626 #endif /* CONFIG_ANDROID */
632 static void extcon_dev_release(struct device
*dev
)
636 static const char *muex_name
= "mutually_exclusive";
637 static void dummy_sysfs_dev_release(struct device
*dev
)
642 * extcon_dev_allocate() - Allocate the memory of extcon device.
643 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
644 * If supported_cable is NULL, cable name related APIs
647 * This function allocates the memory for extcon device without allocating
648 * memory in each extcon provider driver and initialize default setting for
651 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
653 struct extcon_dev
*extcon_dev_allocate(const unsigned int *supported_cable
)
655 struct extcon_dev
*edev
;
657 edev
= kzalloc(sizeof(*edev
), GFP_KERNEL
);
659 return ERR_PTR(-ENOMEM
);
661 edev
->max_supported
= 0;
662 edev
->supported_cable
= supported_cable
;
668 * extcon_dev_free() - Free the memory of extcon device.
669 * @edev: the extcon device to free
671 void extcon_dev_free(struct extcon_dev
*edev
)
675 EXPORT_SYMBOL_GPL(extcon_dev_free
);
677 static int devm_extcon_dev_match(struct device
*dev
, void *res
, void *data
)
679 struct extcon_dev
**r
= res
;
681 if (WARN_ON(!r
|| !*r
))
687 static void devm_extcon_dev_release(struct device
*dev
, void *res
)
689 extcon_dev_free(*(struct extcon_dev
**)res
);
693 * devm_extcon_dev_allocate - Allocate managed extcon device
694 * @dev: device owning the extcon device being created
695 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
696 * If supported_cable is NULL, cable name related APIs
699 * This function manages automatically the memory of extcon device using device
700 * resource management and simplify the control of freeing the memory of extcon
703 * Returns the pointer memory of allocated extcon_dev if success
704 * or ERR_PTR(err) if fail
706 struct extcon_dev
*devm_extcon_dev_allocate(struct device
*dev
,
707 const unsigned int *supported_cable
)
709 struct extcon_dev
**ptr
, *edev
;
711 ptr
= devres_alloc(devm_extcon_dev_release
, sizeof(*ptr
), GFP_KERNEL
);
713 return ERR_PTR(-ENOMEM
);
715 edev
= extcon_dev_allocate(supported_cable
);
721 edev
->dev
.parent
= dev
;
724 devres_add(dev
, ptr
);
728 EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate
);
730 void devm_extcon_dev_free(struct device
*dev
, struct extcon_dev
*edev
)
732 WARN_ON(devres_release(dev
, devm_extcon_dev_release
,
733 devm_extcon_dev_match
, edev
));
735 EXPORT_SYMBOL_GPL(devm_extcon_dev_free
);
738 * extcon_dev_register() - Register a new extcon device
739 * @edev : the new extcon device (should be allocated before calling)
741 * Among the members of edev struct, please set the "user initializing data"
742 * in any case and set the "optional callbacks" if required. However, please
743 * do not set the values of "internal data", which are initialized by
746 int extcon_dev_register(struct extcon_dev
*edev
)
749 static atomic_t edev_no
= ATOMIC_INIT(-1);
752 ret
= create_extcon_class();
757 if (!edev
->supported_cable
)
760 for (; edev
->supported_cable
[index
] != EXTCON_NONE
; index
++);
762 edev
->max_supported
= index
;
763 if (index
> SUPPORTED_CABLE_MAX
) {
765 "exceed the maximum number of supported cables\n");
769 edev
->dev
.class = extcon_class
;
770 edev
->dev
.release
= extcon_dev_release
;
772 edev
->name
= dev_name(edev
->dev
.parent
);
773 if (IS_ERR_OR_NULL(edev
->name
)) {
775 "extcon device name is null\n");
778 dev_set_name(&edev
->dev
, "extcon%lu",
779 (unsigned long)atomic_inc_return(&edev_no
));
781 if (edev
->max_supported
) {
784 struct extcon_cable
*cable
;
786 edev
->cables
= kzalloc(sizeof(struct extcon_cable
) *
787 edev
->max_supported
, GFP_KERNEL
);
790 goto err_sysfs_alloc
;
792 for (index
= 0; index
< edev
->max_supported
; index
++) {
793 cable
= &edev
->cables
[index
];
795 snprintf(buf
, 10, "cable.%d", index
);
796 str
= kzalloc(sizeof(char) * (strlen(buf
) + 1),
799 for (index
--; index
>= 0; index
--) {
800 cable
= &edev
->cables
[index
];
801 kfree(cable
->attr_g
.name
);
805 goto err_alloc_cables
;
810 cable
->cable_index
= index
;
811 cable
->attrs
[0] = &cable
->attr_name
.attr
;
812 cable
->attrs
[1] = &cable
->attr_state
.attr
;
813 cable
->attrs
[2] = NULL
;
814 cable
->attr_g
.name
= str
;
815 cable
->attr_g
.attrs
= cable
->attrs
;
817 sysfs_attr_init(&cable
->attr_name
.attr
);
818 cable
->attr_name
.attr
.name
= "name";
819 cable
->attr_name
.attr
.mode
= 0444;
820 cable
->attr_name
.show
= cable_name_show
;
822 sysfs_attr_init(&cable
->attr_state
.attr
);
823 cable
->attr_state
.attr
.name
= "state";
824 cable
->attr_state
.attr
.mode
= 0444;
825 cable
->attr_state
.show
= cable_state_show
;
829 if (edev
->max_supported
&& edev
->mutually_exclusive
) {
833 /* Count the size of mutually_exclusive array */
834 for (index
= 0; edev
->mutually_exclusive
[index
]; index
++)
837 edev
->attrs_muex
= kzalloc(sizeof(struct attribute
*) *
838 (index
+ 1), GFP_KERNEL
);
839 if (!edev
->attrs_muex
) {
844 edev
->d_attrs_muex
= kzalloc(sizeof(struct device_attribute
) *
846 if (!edev
->d_attrs_muex
) {
848 kfree(edev
->attrs_muex
);
852 for (index
= 0; edev
->mutually_exclusive
[index
]; index
++) {
853 sprintf(buf
, "0x%x", edev
->mutually_exclusive
[index
]);
854 name
= kzalloc(sizeof(char) * (strlen(buf
) + 1),
857 for (index
--; index
>= 0; index
--) {
858 kfree(edev
->d_attrs_muex
[index
].attr
.
861 kfree(edev
->d_attrs_muex
);
862 kfree(edev
->attrs_muex
);
867 sysfs_attr_init(&edev
->d_attrs_muex
[index
].attr
);
868 edev
->d_attrs_muex
[index
].attr
.name
= name
;
869 edev
->d_attrs_muex
[index
].attr
.mode
= 0000;
870 edev
->attrs_muex
[index
] = &edev
->d_attrs_muex
[index
]
873 edev
->attr_g_muex
.name
= muex_name
;
874 edev
->attr_g_muex
.attrs
= edev
->attrs_muex
;
878 if (edev
->max_supported
) {
879 edev
->extcon_dev_type
.groups
=
880 kzalloc(sizeof(struct attribute_group
*) *
881 (edev
->max_supported
+ 2), GFP_KERNEL
);
882 if (!edev
->extcon_dev_type
.groups
) {
884 goto err_alloc_groups
;
887 edev
->extcon_dev_type
.name
= dev_name(&edev
->dev
);
888 edev
->extcon_dev_type
.release
= dummy_sysfs_dev_release
;
890 for (index
= 0; index
< edev
->max_supported
; index
++)
891 edev
->extcon_dev_type
.groups
[index
] =
892 &edev
->cables
[index
].attr_g
;
893 if (edev
->mutually_exclusive
)
894 edev
->extcon_dev_type
.groups
[index
] =
897 edev
->dev
.type
= &edev
->extcon_dev_type
;
900 ret
= device_register(&edev
->dev
);
902 put_device(&edev
->dev
);
905 #if defined(CONFIG_ANDROID)
907 ret
= class_compat_create_link(switch_class
, &edev
->dev
, NULL
);
908 #endif /* CONFIG_ANDROID */
910 spin_lock_init(&edev
->lock
);
912 edev
->nh
= devm_kzalloc(&edev
->dev
,
913 sizeof(*edev
->nh
) * edev
->max_supported
, GFP_KERNEL
);
919 for (index
= 0; index
< edev
->max_supported
; index
++)
920 RAW_INIT_NOTIFIER_HEAD(&edev
->nh
[index
]);
922 dev_set_drvdata(&edev
->dev
, edev
);
925 mutex_lock(&extcon_dev_list_lock
);
926 list_add(&edev
->entry
, &extcon_dev_list
);
927 mutex_unlock(&extcon_dev_list_lock
);
932 if (edev
->max_supported
)
933 kfree(edev
->extcon_dev_type
.groups
);
935 if (edev
->max_supported
&& edev
->mutually_exclusive
) {
936 for (index
= 0; edev
->mutually_exclusive
[index
]; index
++)
937 kfree(edev
->d_attrs_muex
[index
].attr
.name
);
938 kfree(edev
->d_attrs_muex
);
939 kfree(edev
->attrs_muex
);
942 for (index
= 0; index
< edev
->max_supported
; index
++)
943 kfree(edev
->cables
[index
].attr_g
.name
);
945 if (edev
->max_supported
)
950 EXPORT_SYMBOL_GPL(extcon_dev_register
);
953 * extcon_dev_unregister() - Unregister the extcon device.
954 * @edev: the extcon device instance to be unregistered.
956 * Note that this does not call kfree(edev) because edev was not allocated
959 void extcon_dev_unregister(struct extcon_dev
*edev
)
963 mutex_lock(&extcon_dev_list_lock
);
964 list_del(&edev
->entry
);
965 mutex_unlock(&extcon_dev_list_lock
);
967 if (IS_ERR_OR_NULL(get_device(&edev
->dev
))) {
968 dev_err(&edev
->dev
, "Failed to unregister extcon_dev (%s)\n",
969 dev_name(&edev
->dev
));
973 device_unregister(&edev
->dev
);
975 if (edev
->mutually_exclusive
&& edev
->max_supported
) {
976 for (index
= 0; edev
->mutually_exclusive
[index
];
978 kfree(edev
->d_attrs_muex
[index
].attr
.name
);
979 kfree(edev
->d_attrs_muex
);
980 kfree(edev
->attrs_muex
);
983 for (index
= 0; index
< edev
->max_supported
; index
++)
984 kfree(edev
->cables
[index
].attr_g
.name
);
986 if (edev
->max_supported
) {
987 kfree(edev
->extcon_dev_type
.groups
);
991 #if defined(CONFIG_ANDROID)
993 class_compat_remove_link(switch_class
, &edev
->dev
, NULL
);
995 put_device(&edev
->dev
);
997 EXPORT_SYMBOL_GPL(extcon_dev_unregister
);
999 static void devm_extcon_dev_unreg(struct device
*dev
, void *res
)
1001 extcon_dev_unregister(*(struct extcon_dev
**)res
);
1005 * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
1006 * @dev: device to allocate extcon device
1007 * @edev: the new extcon device to register
1009 * Managed extcon_dev_register() function. If extcon device is attached with
1010 * this function, that extcon device is automatically unregistered on driver
1011 * detach. Internally this function calls extcon_dev_register() function.
1012 * To get more information, refer that function.
1014 * If extcon device is registered with this function and the device needs to be
1015 * unregistered separately, devm_extcon_dev_unregister() should be used.
1017 * Returns 0 if success or negaive error number if failure.
1019 int devm_extcon_dev_register(struct device
*dev
, struct extcon_dev
*edev
)
1021 struct extcon_dev
**ptr
;
1024 ptr
= devres_alloc(devm_extcon_dev_unreg
, sizeof(*ptr
), GFP_KERNEL
);
1028 ret
= extcon_dev_register(edev
);
1035 devres_add(dev
, ptr
);
1039 EXPORT_SYMBOL_GPL(devm_extcon_dev_register
);
1042 * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
1043 * @dev: device the extcon belongs to
1044 * @edev: the extcon device to unregister
1046 * Unregister extcon device that is registered with devm_extcon_dev_register()
1049 void devm_extcon_dev_unregister(struct device
*dev
, struct extcon_dev
*edev
)
1051 WARN_ON(devres_release(dev
, devm_extcon_dev_unreg
,
1052 devm_extcon_dev_match
, edev
));
1054 EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister
);
1058 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1059 * @dev - instance to the given device
1060 * @index - index into list of extcon_dev
1062 * return the instance of extcon device
1064 struct extcon_dev
*extcon_get_edev_by_phandle(struct device
*dev
, int index
)
1066 struct device_node
*node
;
1067 struct extcon_dev
*edev
;
1069 if (!dev
->of_node
) {
1070 dev_err(dev
, "device does not have a device node entry\n");
1071 return ERR_PTR(-EINVAL
);
1074 node
= of_parse_phandle(dev
->of_node
, "extcon", index
);
1076 dev_err(dev
, "failed to get phandle in %s node\n",
1077 dev
->of_node
->full_name
);
1078 return ERR_PTR(-ENODEV
);
1081 mutex_lock(&extcon_dev_list_lock
);
1082 list_for_each_entry(edev
, &extcon_dev_list
, entry
) {
1083 if (edev
->dev
.parent
&& edev
->dev
.parent
->of_node
== node
) {
1084 mutex_unlock(&extcon_dev_list_lock
);
1088 mutex_unlock(&extcon_dev_list_lock
);
1090 return ERR_PTR(-EPROBE_DEFER
);
1093 struct extcon_dev
*extcon_get_edev_by_phandle(struct device
*dev
, int index
)
1095 return ERR_PTR(-ENOSYS
);
1097 #endif /* CONFIG_OF */
1098 EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle
);
1101 * extcon_get_edev_name() - Get the name of the extcon device.
1102 * @edev: the extcon device
1104 const char *extcon_get_edev_name(struct extcon_dev
*edev
)
1106 return !edev
? NULL
: edev
->name
;
1109 static int __init
extcon_class_init(void)
1111 return create_extcon_class();
1113 module_init(extcon_class_init
);
1115 static void __exit
extcon_class_exit(void)
1117 #if defined(CONFIG_ANDROID)
1118 class_compat_unregister(switch_class
);
1120 class_destroy(extcon_class
);
1122 module_exit(extcon_class_exit
);
1124 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1125 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1126 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1127 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1128 MODULE_DESCRIPTION("External connector (extcon) class driver");
1129 MODULE_LICENSE("GPL");