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_index_by_name(struct extcon_dev
*edev
, const char *name
)
129 unsigned int id
= EXTCON_NONE
;
132 if (edev
->max_supported
== 0)
135 /* Find the the number of extcon cable */
136 while (extcon_name
[i
]) {
137 if (!strncmp(extcon_name
[i
], name
, CABLE_NAME_MAX
)) {
143 if (id
== EXTCON_NONE
)
146 return find_cable_index_by_id(edev
, id
);
149 static bool is_extcon_changed(u32 prev
, u32
new, int idx
, bool *attached
)
151 if (((prev
>> idx
) & 0x1) != ((new >> idx
) & 0x1)) {
152 *attached
= new ? true : false;
159 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
163 struct extcon_dev
*edev
= dev_get_drvdata(dev
);
165 if (edev
->print_state
) {
166 int ret
= edev
->print_state(edev
, buf
);
170 /* Use default if failed */
173 if (edev
->max_supported
== 0)
174 return sprintf(buf
, "%u\n", edev
->state
);
176 for (i
= 0; i
< edev
->max_supported
; i
++) {
177 count
+= sprintf(buf
+ count
, "%s=%d\n",
178 extcon_name
[edev
->supported_cable
[i
]],
179 !!(edev
->state
& (1 << i
)));
185 static ssize_t
state_store(struct device
*dev
, struct device_attribute
*attr
,
186 const char *buf
, size_t count
)
190 struct extcon_dev
*edev
= dev_get_drvdata(dev
);
192 ret
= sscanf(buf
, "0x%x", &state
);
196 ret
= extcon_set_state(edev
, state
);
203 static DEVICE_ATTR_RW(state
);
205 static ssize_t
name_show(struct device
*dev
, struct device_attribute
*attr
,
208 struct extcon_dev
*edev
= dev_get_drvdata(dev
);
210 return sprintf(buf
, "%s\n", edev
->name
);
212 static DEVICE_ATTR_RO(name
);
214 static ssize_t
cable_name_show(struct device
*dev
,
215 struct device_attribute
*attr
, char *buf
)
217 struct extcon_cable
*cable
= container_of(attr
, struct extcon_cable
,
219 int i
= cable
->cable_index
;
221 return sprintf(buf
, "%s\n",
222 extcon_name
[cable
->edev
->supported_cable
[i
]]);
225 static ssize_t
cable_state_show(struct device
*dev
,
226 struct device_attribute
*attr
, char *buf
)
228 struct extcon_cable
*cable
= container_of(attr
, struct extcon_cable
,
231 return sprintf(buf
, "%d\n",
232 extcon_get_cable_state_(cable
->edev
,
233 cable
->cable_index
));
237 * extcon_update_state() - Update the cable attach states of the extcon device
238 * only for the masked bits.
239 * @edev: the extcon device
240 * @mask: the bit mask to designate updated bits.
241 * @state: new cable attach status for @edev
243 * Changing the state sends uevent with environment variable containing
244 * the name of extcon device (envp[0]) and the state output (envp[1]).
245 * Tizen uses this format for extcon device to get events from ports.
246 * Android uses this format as well.
248 * Note that the notifier provides which bits are changed in the state
249 * variable with the val parameter (second) to the callback.
251 int extcon_update_state(struct extcon_dev
*edev
, u32 mask
, u32 state
)
263 spin_lock_irqsave(&edev
->lock
, flags
);
265 if (edev
->state
!= ((edev
->state
& ~mask
) | (state
& mask
))) {
266 if (check_mutually_exclusive(edev
, (edev
->state
& ~mask
) |
268 spin_unlock_irqrestore(&edev
->lock
, flags
);
272 for (index
= 0; index
< edev
->max_supported
; index
++) {
273 if (is_extcon_changed(edev
->state
, state
, index
, &attached
))
274 raw_notifier_call_chain(&edev
->nh
[index
], attached
, edev
);
277 edev
->state
&= ~mask
;
278 edev
->state
|= state
& mask
;
280 /* This could be in interrupt handler */
281 prop_buf
= (char *)get_zeroed_page(GFP_ATOMIC
);
283 length
= name_show(&edev
->dev
, NULL
, prop_buf
);
285 if (prop_buf
[length
- 1] == '\n')
286 prop_buf
[length
- 1] = 0;
287 snprintf(name_buf
, sizeof(name_buf
),
288 "NAME=%s", prop_buf
);
289 envp
[env_offset
++] = name_buf
;
291 length
= state_show(&edev
->dev
, NULL
, prop_buf
);
293 if (prop_buf
[length
- 1] == '\n')
294 prop_buf
[length
- 1] = 0;
295 snprintf(state_buf
, sizeof(state_buf
),
296 "STATE=%s", prop_buf
);
297 envp
[env_offset
++] = state_buf
;
299 envp
[env_offset
] = NULL
;
300 /* Unlock early before uevent */
301 spin_unlock_irqrestore(&edev
->lock
, flags
);
303 kobject_uevent_env(&edev
->dev
.kobj
, KOBJ_CHANGE
, envp
);
304 free_page((unsigned long)prop_buf
);
306 /* Unlock early before uevent */
307 spin_unlock_irqrestore(&edev
->lock
, flags
);
309 dev_err(&edev
->dev
, "out of memory in extcon_set_state\n");
310 kobject_uevent(&edev
->dev
.kobj
, KOBJ_CHANGE
);
314 spin_unlock_irqrestore(&edev
->lock
, flags
);
319 EXPORT_SYMBOL_GPL(extcon_update_state
);
322 * extcon_set_state() - Set the cable attach states of the extcon device.
323 * @edev: the extcon device
324 * @state: new cable attach status for @edev
326 * Note that notifier provides which bits are changed in the state
327 * variable with the val parameter (second) to the callback.
329 int extcon_set_state(struct extcon_dev
*edev
, u32 state
)
331 return extcon_update_state(edev
, 0xffffffff, state
);
333 EXPORT_SYMBOL_GPL(extcon_set_state
);
336 * extcon_get_cable_state_() - Get the status of a specific cable.
337 * @edev: the extcon device that has the cable.
338 * @id: the unique id of each external connector in extcon enumeration.
340 int extcon_get_cable_state_(struct extcon_dev
*edev
, const unsigned int id
)
344 index
= find_cable_index_by_id(edev
, id
);
348 if (edev
->max_supported
&& edev
->max_supported
<= index
)
351 return !!(edev
->state
& (1 << index
));
353 EXPORT_SYMBOL_GPL(extcon_get_cable_state_
);
356 * extcon_get_cable_state() - Get the status of a specific cable.
357 * @edev: the extcon device that has the cable.
358 * @cable_name: cable name.
360 * Note that this is slower than extcon_get_cable_state_.
362 int extcon_get_cable_state(struct extcon_dev
*edev
, const char *cable_name
)
364 return extcon_get_cable_state_(edev
, find_cable_index_by_name
367 EXPORT_SYMBOL_GPL(extcon_get_cable_state
);
370 * extcon_set_cable_state_() - Set the status of a specific cable.
371 * @edev: the extcon device that has the cable.
372 * @id: the unique id of each external connector
373 * in extcon enumeration.
374 * @state: the new cable status. The default semantics is
375 * true: attached / false: detached.
377 int extcon_set_cable_state_(struct extcon_dev
*edev
, unsigned int id
,
383 index
= find_cable_index_by_id(edev
, id
);
387 if (edev
->max_supported
&& edev
->max_supported
<= index
)
390 state
= cable_state
? (1 << index
) : 0;
391 return extcon_update_state(edev
, 1 << index
, state
);
393 EXPORT_SYMBOL_GPL(extcon_set_cable_state_
);
396 * extcon_set_cable_state() - Set the status of a specific cable.
397 * @edev: the extcon device that has the cable.
398 * @cable_name: cable name.
399 * @cable_state: the new cable status. The default semantics is
400 * true: attached / false: detached.
402 * Note that this is slower than extcon_set_cable_state_.
404 int extcon_set_cable_state(struct extcon_dev
*edev
,
405 const char *cable_name
, bool cable_state
)
407 return extcon_set_cable_state_(edev
, find_cable_index_by_name
408 (edev
, cable_name
), cable_state
);
410 EXPORT_SYMBOL_GPL(extcon_set_cable_state
);
413 * extcon_get_extcon_dev() - Get the extcon device instance from the name
414 * @extcon_name: The extcon name provided with extcon_dev_register()
416 struct extcon_dev
*extcon_get_extcon_dev(const char *extcon_name
)
418 struct extcon_dev
*sd
;
420 mutex_lock(&extcon_dev_list_lock
);
421 list_for_each_entry(sd
, &extcon_dev_list
, entry
) {
422 if (!strcmp(sd
->name
, extcon_name
))
427 mutex_unlock(&extcon_dev_list_lock
);
430 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev
);
433 * extcon_register_interest() - Register a notifier for a state change of a
434 * specific cable, not an entier set of cables of a
436 * @obj: an empty extcon_specific_cable_nb object to be returned.
437 * @extcon_name: the name of extcon device.
438 * if NULL, extcon_register_interest will register
439 * every cable with the target cable_name given.
440 * @cable_name: the target cable name.
441 * @nb: the notifier block to get notified.
443 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
444 * the struct for you.
446 * extcon_register_interest is a helper function for those who want to get
447 * notification for a single specific cable's status change. If a user wants
448 * to get notification for any changes of all cables of a extcon device,
449 * he/she should use the general extcon_register_notifier().
451 * Note that the second parameter given to the callback of nb (val) is
452 * "old_state", not the current state. The current state can be retrieved
453 * by looking at the third pameter (edev pointer)'s state value.
455 int extcon_register_interest(struct extcon_specific_cable_nb
*obj
,
456 const char *extcon_name
, const char *cable_name
,
457 struct notifier_block
*nb
)
462 if (!obj
|| !cable_name
|| !nb
)
466 obj
->edev
= extcon_get_extcon_dev(extcon_name
);
470 obj
->cable_index
= find_cable_index_by_name(obj
->edev
,
472 if (obj
->cable_index
< 0)
473 return obj
->cable_index
;
477 spin_lock_irqsave(&obj
->edev
->lock
, flags
);
478 ret
= raw_notifier_chain_register(
479 &obj
->edev
->nh
[obj
->cable_index
],
481 spin_unlock_irqrestore(&obj
->edev
->lock
, flags
);
483 struct class_dev_iter iter
;
484 struct extcon_dev
*extd
;
489 class_dev_iter_init(&iter
, extcon_class
, NULL
, NULL
);
490 while ((dev
= class_dev_iter_next(&iter
))) {
491 extd
= dev_get_drvdata(dev
);
493 if (find_cable_index_by_name(extd
, cable_name
) < 0)
496 class_dev_iter_exit(&iter
);
497 return extcon_register_interest(obj
, extd
->name
,
506 EXPORT_SYMBOL_GPL(extcon_register_interest
);
509 * extcon_unregister_interest() - Unregister the notifier registered by
510 * extcon_register_interest().
511 * @obj: the extcon_specific_cable_nb object returned by
512 * extcon_register_interest().
514 int extcon_unregister_interest(struct extcon_specific_cable_nb
*obj
)
522 spin_lock_irqsave(&obj
->edev
->lock
, flags
);
523 ret
= raw_notifier_chain_unregister(
524 &obj
->edev
->nh
[obj
->cable_index
], obj
->user_nb
);
525 spin_unlock_irqrestore(&obj
->edev
->lock
, flags
);
529 EXPORT_SYMBOL_GPL(extcon_unregister_interest
);
532 * extcon_register_notifier() - Register a notifiee to get notified by
533 * any attach status changes from the extcon.
534 * @edev: the extcon device that has the external connecotr.
535 * @id: the unique id of each external connector in extcon enumeration.
536 * @nb: a notifier block to be registered.
538 * Note that the second parameter given to the callback of nb (val) is
539 * "old_state", not the current state. The current state can be retrieved
540 * by looking at the third pameter (edev pointer)'s state value.
542 int extcon_register_notifier(struct extcon_dev
*edev
, unsigned int id
,
543 struct notifier_block
*nb
)
548 idx
= find_cable_index_by_id(edev
, id
);
550 spin_lock_irqsave(&edev
->lock
, flags
);
551 ret
= raw_notifier_chain_register(&edev
->nh
[idx
], nb
);
552 spin_unlock_irqrestore(&edev
->lock
, flags
);
556 EXPORT_SYMBOL_GPL(extcon_register_notifier
);
559 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
560 * @edev: the extcon device that has the external connecotr.
561 * @id: the unique id of each external connector in extcon enumeration.
562 * @nb: a notifier block to be registered.
564 int extcon_unregister_notifier(struct extcon_dev
*edev
, unsigned int id
,
565 struct notifier_block
*nb
)
570 idx
= find_cable_index_by_id(edev
, id
);
572 spin_lock_irqsave(&edev
->lock
, flags
);
573 ret
= raw_notifier_chain_unregister(&edev
->nh
[idx
], nb
);
574 spin_unlock_irqrestore(&edev
->lock
, flags
);
578 EXPORT_SYMBOL_GPL(extcon_unregister_notifier
);
580 static struct attribute
*extcon_attrs
[] = {
581 &dev_attr_state
.attr
,
585 ATTRIBUTE_GROUPS(extcon
);
587 static int create_extcon_class(void)
590 extcon_class
= class_create(THIS_MODULE
, "extcon");
591 if (IS_ERR(extcon_class
))
592 return PTR_ERR(extcon_class
);
593 extcon_class
->dev_groups
= extcon_groups
;
595 #if defined(CONFIG_ANDROID)
596 switch_class
= class_compat_register("switch");
597 if (WARN(!switch_class
, "cannot allocate"))
599 #endif /* CONFIG_ANDROID */
605 static void extcon_dev_release(struct device
*dev
)
609 static const char *muex_name
= "mutually_exclusive";
610 static void dummy_sysfs_dev_release(struct device
*dev
)
615 * extcon_dev_allocate() - Allocate the memory of extcon device.
616 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
617 * If supported_cable is NULL, cable name related APIs
620 * This function allocates the memory for extcon device without allocating
621 * memory in each extcon provider driver and initialize default setting for
624 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
626 struct extcon_dev
*extcon_dev_allocate(const unsigned int *supported_cable
)
628 struct extcon_dev
*edev
;
630 edev
= kzalloc(sizeof(*edev
), GFP_KERNEL
);
632 return ERR_PTR(-ENOMEM
);
634 edev
->max_supported
= 0;
635 edev
->supported_cable
= supported_cable
;
641 * extcon_dev_free() - Free the memory of extcon device.
642 * @edev: the extcon device to free
644 void extcon_dev_free(struct extcon_dev
*edev
)
648 EXPORT_SYMBOL_GPL(extcon_dev_free
);
650 static int devm_extcon_dev_match(struct device
*dev
, void *res
, void *data
)
652 struct extcon_dev
**r
= res
;
654 if (WARN_ON(!r
|| !*r
))
660 static void devm_extcon_dev_release(struct device
*dev
, void *res
)
662 extcon_dev_free(*(struct extcon_dev
**)res
);
666 * devm_extcon_dev_allocate - Allocate managed extcon device
667 * @dev: device owning the extcon device being created
668 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
669 * If supported_cable is NULL, cable name related APIs
672 * This function manages automatically the memory of extcon device using device
673 * resource management and simplify the control of freeing the memory of extcon
676 * Returns the pointer memory of allocated extcon_dev if success
677 * or ERR_PTR(err) if fail
679 struct extcon_dev
*devm_extcon_dev_allocate(struct device
*dev
,
680 const unsigned int *supported_cable
)
682 struct extcon_dev
**ptr
, *edev
;
684 ptr
= devres_alloc(devm_extcon_dev_release
, sizeof(*ptr
), GFP_KERNEL
);
686 return ERR_PTR(-ENOMEM
);
688 edev
= extcon_dev_allocate(supported_cable
);
694 edev
->dev
.parent
= dev
;
697 devres_add(dev
, ptr
);
701 EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate
);
703 void devm_extcon_dev_free(struct device
*dev
, struct extcon_dev
*edev
)
705 WARN_ON(devres_release(dev
, devm_extcon_dev_release
,
706 devm_extcon_dev_match
, edev
));
708 EXPORT_SYMBOL_GPL(devm_extcon_dev_free
);
711 * extcon_dev_register() - Register a new extcon device
712 * @edev : the new extcon device (should be allocated before calling)
714 * Among the members of edev struct, please set the "user initializing data"
715 * in any case and set the "optional callbacks" if required. However, please
716 * do not set the values of "internal data", which are initialized by
719 int extcon_dev_register(struct extcon_dev
*edev
)
722 static atomic_t edev_no
= ATOMIC_INIT(-1);
725 ret
= create_extcon_class();
730 if (!edev
->supported_cable
)
733 for (; edev
->supported_cable
[index
] != EXTCON_NONE
; index
++);
735 edev
->max_supported
= index
;
736 if (index
> SUPPORTED_CABLE_MAX
) {
738 "exceed the maximum number of supported cables\n");
742 edev
->dev
.class = extcon_class
;
743 edev
->dev
.release
= extcon_dev_release
;
745 edev
->name
= dev_name(edev
->dev
.parent
);
746 if (IS_ERR_OR_NULL(edev
->name
)) {
748 "extcon device name is null\n");
751 dev_set_name(&edev
->dev
, "extcon%lu",
752 (unsigned long)atomic_inc_return(&edev_no
));
754 if (edev
->max_supported
) {
757 struct extcon_cable
*cable
;
759 edev
->cables
= kzalloc(sizeof(struct extcon_cable
) *
760 edev
->max_supported
, GFP_KERNEL
);
763 goto err_sysfs_alloc
;
765 for (index
= 0; index
< edev
->max_supported
; index
++) {
766 cable
= &edev
->cables
[index
];
768 snprintf(buf
, 10, "cable.%d", index
);
769 str
= kzalloc(sizeof(char) * (strlen(buf
) + 1),
772 for (index
--; index
>= 0; index
--) {
773 cable
= &edev
->cables
[index
];
774 kfree(cable
->attr_g
.name
);
778 goto err_alloc_cables
;
783 cable
->cable_index
= index
;
784 cable
->attrs
[0] = &cable
->attr_name
.attr
;
785 cable
->attrs
[1] = &cable
->attr_state
.attr
;
786 cable
->attrs
[2] = NULL
;
787 cable
->attr_g
.name
= str
;
788 cable
->attr_g
.attrs
= cable
->attrs
;
790 sysfs_attr_init(&cable
->attr_name
.attr
);
791 cable
->attr_name
.attr
.name
= "name";
792 cable
->attr_name
.attr
.mode
= 0444;
793 cable
->attr_name
.show
= cable_name_show
;
795 sysfs_attr_init(&cable
->attr_state
.attr
);
796 cable
->attr_state
.attr
.name
= "state";
797 cable
->attr_state
.attr
.mode
= 0444;
798 cable
->attr_state
.show
= cable_state_show
;
802 if (edev
->max_supported
&& edev
->mutually_exclusive
) {
806 /* Count the size of mutually_exclusive array */
807 for (index
= 0; edev
->mutually_exclusive
[index
]; index
++)
810 edev
->attrs_muex
= kzalloc(sizeof(struct attribute
*) *
811 (index
+ 1), GFP_KERNEL
);
812 if (!edev
->attrs_muex
) {
817 edev
->d_attrs_muex
= kzalloc(sizeof(struct device_attribute
) *
819 if (!edev
->d_attrs_muex
) {
821 kfree(edev
->attrs_muex
);
825 for (index
= 0; edev
->mutually_exclusive
[index
]; index
++) {
826 sprintf(buf
, "0x%x", edev
->mutually_exclusive
[index
]);
827 name
= kzalloc(sizeof(char) * (strlen(buf
) + 1),
830 for (index
--; index
>= 0; index
--) {
831 kfree(edev
->d_attrs_muex
[index
].attr
.
834 kfree(edev
->d_attrs_muex
);
835 kfree(edev
->attrs_muex
);
840 sysfs_attr_init(&edev
->d_attrs_muex
[index
].attr
);
841 edev
->d_attrs_muex
[index
].attr
.name
= name
;
842 edev
->d_attrs_muex
[index
].attr
.mode
= 0000;
843 edev
->attrs_muex
[index
] = &edev
->d_attrs_muex
[index
]
846 edev
->attr_g_muex
.name
= muex_name
;
847 edev
->attr_g_muex
.attrs
= edev
->attrs_muex
;
851 if (edev
->max_supported
) {
852 edev
->extcon_dev_type
.groups
=
853 kzalloc(sizeof(struct attribute_group
*) *
854 (edev
->max_supported
+ 2), GFP_KERNEL
);
855 if (!edev
->extcon_dev_type
.groups
) {
857 goto err_alloc_groups
;
860 edev
->extcon_dev_type
.name
= dev_name(&edev
->dev
);
861 edev
->extcon_dev_type
.release
= dummy_sysfs_dev_release
;
863 for (index
= 0; index
< edev
->max_supported
; index
++)
864 edev
->extcon_dev_type
.groups
[index
] =
865 &edev
->cables
[index
].attr_g
;
866 if (edev
->mutually_exclusive
)
867 edev
->extcon_dev_type
.groups
[index
] =
870 edev
->dev
.type
= &edev
->extcon_dev_type
;
873 ret
= device_register(&edev
->dev
);
875 put_device(&edev
->dev
);
878 #if defined(CONFIG_ANDROID)
880 ret
= class_compat_create_link(switch_class
, &edev
->dev
, NULL
);
881 #endif /* CONFIG_ANDROID */
883 spin_lock_init(&edev
->lock
);
885 edev
->nh
= devm_kzalloc(&edev
->dev
,
886 sizeof(*edev
->nh
) * edev
->max_supported
, GFP_KERNEL
);
892 for (index
= 0; index
< edev
->max_supported
; index
++)
893 RAW_INIT_NOTIFIER_HEAD(&edev
->nh
[index
]);
895 dev_set_drvdata(&edev
->dev
, edev
);
898 mutex_lock(&extcon_dev_list_lock
);
899 list_add(&edev
->entry
, &extcon_dev_list
);
900 mutex_unlock(&extcon_dev_list_lock
);
905 if (edev
->max_supported
)
906 kfree(edev
->extcon_dev_type
.groups
);
908 if (edev
->max_supported
&& edev
->mutually_exclusive
) {
909 for (index
= 0; edev
->mutually_exclusive
[index
]; index
++)
910 kfree(edev
->d_attrs_muex
[index
].attr
.name
);
911 kfree(edev
->d_attrs_muex
);
912 kfree(edev
->attrs_muex
);
915 for (index
= 0; index
< edev
->max_supported
; index
++)
916 kfree(edev
->cables
[index
].attr_g
.name
);
918 if (edev
->max_supported
)
923 EXPORT_SYMBOL_GPL(extcon_dev_register
);
926 * extcon_dev_unregister() - Unregister the extcon device.
927 * @edev: the extcon device instance to be unregistered.
929 * Note that this does not call kfree(edev) because edev was not allocated
932 void extcon_dev_unregister(struct extcon_dev
*edev
)
936 mutex_lock(&extcon_dev_list_lock
);
937 list_del(&edev
->entry
);
938 mutex_unlock(&extcon_dev_list_lock
);
940 if (IS_ERR_OR_NULL(get_device(&edev
->dev
))) {
941 dev_err(&edev
->dev
, "Failed to unregister extcon_dev (%s)\n",
942 dev_name(&edev
->dev
));
946 device_unregister(&edev
->dev
);
948 if (edev
->mutually_exclusive
&& edev
->max_supported
) {
949 for (index
= 0; edev
->mutually_exclusive
[index
];
951 kfree(edev
->d_attrs_muex
[index
].attr
.name
);
952 kfree(edev
->d_attrs_muex
);
953 kfree(edev
->attrs_muex
);
956 for (index
= 0; index
< edev
->max_supported
; index
++)
957 kfree(edev
->cables
[index
].attr_g
.name
);
959 if (edev
->max_supported
) {
960 kfree(edev
->extcon_dev_type
.groups
);
964 #if defined(CONFIG_ANDROID)
966 class_compat_remove_link(switch_class
, &edev
->dev
, NULL
);
968 put_device(&edev
->dev
);
970 EXPORT_SYMBOL_GPL(extcon_dev_unregister
);
972 static void devm_extcon_dev_unreg(struct device
*dev
, void *res
)
974 extcon_dev_unregister(*(struct extcon_dev
**)res
);
978 * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
979 * @dev: device to allocate extcon device
980 * @edev: the new extcon device to register
982 * Managed extcon_dev_register() function. If extcon device is attached with
983 * this function, that extcon device is automatically unregistered on driver
984 * detach. Internally this function calls extcon_dev_register() function.
985 * To get more information, refer that function.
987 * If extcon device is registered with this function and the device needs to be
988 * unregistered separately, devm_extcon_dev_unregister() should be used.
990 * Returns 0 if success or negaive error number if failure.
992 int devm_extcon_dev_register(struct device
*dev
, struct extcon_dev
*edev
)
994 struct extcon_dev
**ptr
;
997 ptr
= devres_alloc(devm_extcon_dev_unreg
, sizeof(*ptr
), GFP_KERNEL
);
1001 ret
= extcon_dev_register(edev
);
1008 devres_add(dev
, ptr
);
1012 EXPORT_SYMBOL_GPL(devm_extcon_dev_register
);
1015 * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
1016 * @dev: device the extcon belongs to
1017 * @edev: the extcon device to unregister
1019 * Unregister extcon device that is registered with devm_extcon_dev_register()
1022 void devm_extcon_dev_unregister(struct device
*dev
, struct extcon_dev
*edev
)
1024 WARN_ON(devres_release(dev
, devm_extcon_dev_unreg
,
1025 devm_extcon_dev_match
, edev
));
1027 EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister
);
1031 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1032 * @dev - instance to the given device
1033 * @index - index into list of extcon_dev
1035 * return the instance of extcon device
1037 struct extcon_dev
*extcon_get_edev_by_phandle(struct device
*dev
, int index
)
1039 struct device_node
*node
;
1040 struct extcon_dev
*edev
;
1042 if (!dev
->of_node
) {
1043 dev_err(dev
, "device does not have a device node entry\n");
1044 return ERR_PTR(-EINVAL
);
1047 node
= of_parse_phandle(dev
->of_node
, "extcon", index
);
1049 dev_err(dev
, "failed to get phandle in %s node\n",
1050 dev
->of_node
->full_name
);
1051 return ERR_PTR(-ENODEV
);
1054 mutex_lock(&extcon_dev_list_lock
);
1055 list_for_each_entry(edev
, &extcon_dev_list
, entry
) {
1056 if (edev
->dev
.parent
&& edev
->dev
.parent
->of_node
== node
) {
1057 mutex_unlock(&extcon_dev_list_lock
);
1061 mutex_unlock(&extcon_dev_list_lock
);
1063 return ERR_PTR(-EPROBE_DEFER
);
1066 struct extcon_dev
*extcon_get_edev_by_phandle(struct device
*dev
, int index
)
1068 return ERR_PTR(-ENOSYS
);
1070 #endif /* CONFIG_OF */
1071 EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle
);
1074 * extcon_get_edev_name() - Get the name of the extcon device.
1075 * @edev: the extcon device
1077 const char *extcon_get_edev_name(struct extcon_dev
*edev
)
1079 return !edev
? NULL
: edev
->name
;
1082 static int __init
extcon_class_init(void)
1084 return create_extcon_class();
1086 module_init(extcon_class_init
);
1088 static void __exit
extcon_class_exit(void)
1090 #if defined(CONFIG_ANDROID)
1091 class_compat_unregister(switch_class
);
1093 class_destroy(extcon_class
);
1095 module_exit(extcon_class_exit
);
1097 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1098 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1099 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1100 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1101 MODULE_DESCRIPTION("External connector (extcon) class driver");
1102 MODULE_LICENSE("GPL");