diff options
author | Chanwoo Choi <cw00.choi@samsung.com> | 2016-06-27 06:28:04 -0400 |
---|---|---|
committer | Chanwoo Choi <cw00.choi@samsung.com> | 2016-06-27 07:31:23 -0400 |
commit | b225d00f3ad2d996f914790a0f6324a4efd18768 (patch) | |
tree | 445154094bc804c52795753a8f05e14bd2ee4326 | |
parent | 20f7b53dfc24e0caa984087691af82e442229680 (diff) |
extcon: Split out the resource-managed functions from extcon core
This patch split out the resource-managed related functions
from extcon core driver.
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
-rw-r--r-- | drivers/extcon/Makefile | 2 | ||||
-rw-r--r-- | drivers/extcon/devres.c | 143 | ||||
-rw-r--r-- | drivers/extcon/extcon.c | 117 |
3 files changed, 144 insertions, 118 deletions
diff --git a/drivers/extcon/Makefile b/drivers/extcon/Makefile index 2a0e4f45d5b2..423ebc8714c5 100644 --- a/drivers/extcon/Makefile +++ b/drivers/extcon/Makefile | |||
@@ -2,7 +2,7 @@ | |||
2 | # Makefile for external connector class (extcon) devices | 2 | # Makefile for external connector class (extcon) devices |
3 | # | 3 | # |
4 | 4 | ||
5 | obj-$(CONFIG_EXTCON) += extcon.o | 5 | obj-$(CONFIG_EXTCON) += extcon.o devres.o |
6 | obj-$(CONFIG_EXTCON_ADC_JACK) += extcon-adc-jack.o | 6 | obj-$(CONFIG_EXTCON_ADC_JACK) += extcon-adc-jack.o |
7 | obj-$(CONFIG_EXTCON_ARIZONA) += extcon-arizona.o | 7 | obj-$(CONFIG_EXTCON_ARIZONA) += extcon-arizona.o |
8 | obj-$(CONFIG_EXTCON_AXP288) += extcon-axp288.o | 8 | obj-$(CONFIG_EXTCON_AXP288) += extcon-axp288.o |
diff --git a/drivers/extcon/devres.c b/drivers/extcon/devres.c new file mode 100644 index 000000000000..694ca85d5a70 --- /dev/null +++ b/drivers/extcon/devres.c | |||
@@ -0,0 +1,143 @@ | |||
1 | /* | ||
2 | * drivers/extcon/devres.c - EXTCON device's resource management | ||
3 | * | ||
4 | * Copyright (C) 2016 Samsung Electronics | ||
5 | * Author: Chanwoo Choi <cw00.choi@samsung.com> | ||
6 | * | ||
7 | * This software is licensed under the terms of the GNU General Public | ||
8 | * License version 2, as published by the Free Software Foundation, and | ||
9 | * may be copied, distributed, and modified under those terms. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | */ | ||
16 | |||
17 | #include <linux/extcon.h> | ||
18 | |||
19 | static int devm_extcon_dev_match(struct device *dev, void *res, void *data) | ||
20 | { | ||
21 | struct extcon_dev **r = res; | ||
22 | |||
23 | if (WARN_ON(!r || !*r)) | ||
24 | return 0; | ||
25 | |||
26 | return *r == data; | ||
27 | } | ||
28 | |||
29 | static void devm_extcon_dev_release(struct device *dev, void *res) | ||
30 | { | ||
31 | extcon_dev_free(*(struct extcon_dev **)res); | ||
32 | } | ||
33 | |||
34 | |||
35 | static void devm_extcon_dev_unreg(struct device *dev, void *res) | ||
36 | { | ||
37 | extcon_dev_unregister(*(struct extcon_dev **)res); | ||
38 | } | ||
39 | |||
40 | /** | ||
41 | * devm_extcon_dev_allocate - Allocate managed extcon device | ||
42 | * @dev: device owning the extcon device being created | ||
43 | * @supported_cable: Array of supported extcon ending with EXTCON_NONE. | ||
44 | * If supported_cable is NULL, cable name related APIs | ||
45 | * are disabled. | ||
46 | * | ||
47 | * This function manages automatically the memory of extcon device using device | ||
48 | * resource management and simplify the control of freeing the memory of extcon | ||
49 | * device. | ||
50 | * | ||
51 | * Returns the pointer memory of allocated extcon_dev if success | ||
52 | * or ERR_PTR(err) if fail | ||
53 | */ | ||
54 | struct extcon_dev *devm_extcon_dev_allocate(struct device *dev, | ||
55 | const unsigned int *supported_cable) | ||
56 | { | ||
57 | struct extcon_dev **ptr, *edev; | ||
58 | |||
59 | ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL); | ||
60 | if (!ptr) | ||
61 | return ERR_PTR(-ENOMEM); | ||
62 | |||
63 | edev = extcon_dev_allocate(supported_cable); | ||
64 | if (IS_ERR(edev)) { | ||
65 | devres_free(ptr); | ||
66 | return edev; | ||
67 | } | ||
68 | |||
69 | edev->dev.parent = dev; | ||
70 | |||
71 | *ptr = edev; | ||
72 | devres_add(dev, ptr); | ||
73 | |||
74 | return edev; | ||
75 | } | ||
76 | EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate); | ||
77 | |||
78 | /** | ||
79 | * devm_extcon_dev_free() - Resource-managed extcon_dev_unregister() | ||
80 | * @dev: device the extcon belongs to | ||
81 | * @edev: the extcon device to unregister | ||
82 | * | ||
83 | * Free the memory that is allocated with devm_extcon_dev_allocate() | ||
84 | * function. | ||
85 | */ | ||
86 | void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev) | ||
87 | { | ||
88 | WARN_ON(devres_release(dev, devm_extcon_dev_release, | ||
89 | devm_extcon_dev_match, edev)); | ||
90 | } | ||
91 | EXPORT_SYMBOL_GPL(devm_extcon_dev_free); | ||
92 | |||
93 | /** | ||
94 | * devm_extcon_dev_register() - Resource-managed extcon_dev_register() | ||
95 | * @dev: device to allocate extcon device | ||
96 | * @edev: the new extcon device to register | ||
97 | * | ||
98 | * Managed extcon_dev_register() function. If extcon device is attached with | ||
99 | * this function, that extcon device is automatically unregistered on driver | ||
100 | * detach. Internally this function calls extcon_dev_register() function. | ||
101 | * To get more information, refer that function. | ||
102 | * | ||
103 | * If extcon device is registered with this function and the device needs to be | ||
104 | * unregistered separately, devm_extcon_dev_unregister() should be used. | ||
105 | * | ||
106 | * Returns 0 if success or negaive error number if failure. | ||
107 | */ | ||
108 | int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev) | ||
109 | { | ||
110 | struct extcon_dev **ptr; | ||
111 | int ret; | ||
112 | |||
113 | ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL); | ||
114 | if (!ptr) | ||
115 | return -ENOMEM; | ||
116 | |||
117 | ret = extcon_dev_register(edev); | ||
118 | if (ret) { | ||
119 | devres_free(ptr); | ||
120 | return ret; | ||
121 | } | ||
122 | |||
123 | *ptr = edev; | ||
124 | devres_add(dev, ptr); | ||
125 | |||
126 | return 0; | ||
127 | } | ||
128 | EXPORT_SYMBOL_GPL(devm_extcon_dev_register); | ||
129 | |||
130 | /** | ||
131 | * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister() | ||
132 | * @dev: device the extcon belongs to | ||
133 | * @edev: the extcon device to unregister | ||
134 | * | ||
135 | * Unregister extcon device that is registered with devm_extcon_dev_register() | ||
136 | * function. | ||
137 | */ | ||
138 | void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev) | ||
139 | { | ||
140 | WARN_ON(devres_release(dev, devm_extcon_dev_unreg, | ||
141 | devm_extcon_dev_match, edev)); | ||
142 | } | ||
143 | EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister); | ||
diff --git a/drivers/extcon/extcon.c b/drivers/extcon/extcon.c index b5fdb5d9dbad..862334e69cf1 100644 --- a/drivers/extcon/extcon.c +++ b/drivers/extcon/extcon.c | |||
@@ -564,66 +564,6 @@ void extcon_dev_free(struct extcon_dev *edev) | |||
564 | } | 564 | } |
565 | EXPORT_SYMBOL_GPL(extcon_dev_free); | 565 | EXPORT_SYMBOL_GPL(extcon_dev_free); |
566 | 566 | ||
567 | static int devm_extcon_dev_match(struct device *dev, void *res, void *data) | ||
568 | { | ||
569 | struct extcon_dev **r = res; | ||
570 | |||
571 | if (WARN_ON(!r || !*r)) | ||
572 | return 0; | ||
573 | |||
574 | return *r == data; | ||
575 | } | ||
576 | |||
577 | static void devm_extcon_dev_release(struct device *dev, void *res) | ||
578 | { | ||
579 | extcon_dev_free(*(struct extcon_dev **)res); | ||
580 | } | ||
581 | |||
582 | /** | ||
583 | * devm_extcon_dev_allocate - Allocate managed extcon device | ||
584 | * @dev: device owning the extcon device being created | ||
585 | * @supported_cable: Array of supported extcon ending with EXTCON_NONE. | ||
586 | * If supported_cable is NULL, cable name related APIs | ||
587 | * are disabled. | ||
588 | * | ||
589 | * This function manages automatically the memory of extcon device using device | ||
590 | * resource management and simplify the control of freeing the memory of extcon | ||
591 | * device. | ||
592 | * | ||
593 | * Returns the pointer memory of allocated extcon_dev if success | ||
594 | * or ERR_PTR(err) if fail | ||
595 | */ | ||
596 | struct extcon_dev *devm_extcon_dev_allocate(struct device *dev, | ||
597 | const unsigned int *supported_cable) | ||
598 | { | ||
599 | struct extcon_dev **ptr, *edev; | ||
600 | |||
601 | ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL); | ||
602 | if (!ptr) | ||
603 | return ERR_PTR(-ENOMEM); | ||
604 | |||
605 | edev = extcon_dev_allocate(supported_cable); | ||
606 | if (IS_ERR(edev)) { | ||
607 | devres_free(ptr); | ||
608 | return edev; | ||
609 | } | ||
610 | |||
611 | edev->dev.parent = dev; | ||
612 | |||
613 | *ptr = edev; | ||
614 | devres_add(dev, ptr); | ||
615 | |||
616 | return edev; | ||
617 | } | ||
618 | EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate); | ||
619 | |||
620 | void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev) | ||
621 | { | ||
622 | WARN_ON(devres_release(dev, devm_extcon_dev_release, | ||
623 | devm_extcon_dev_match, edev)); | ||
624 | } | ||
625 | EXPORT_SYMBOL_GPL(devm_extcon_dev_free); | ||
626 | |||
627 | /** | 567 | /** |
628 | * extcon_dev_register() - Register a new extcon device | 568 | * extcon_dev_register() - Register a new extcon device |
629 | * @edev : the new extcon device (should be allocated before calling) | 569 | * @edev : the new extcon device (should be allocated before calling) |
@@ -889,63 +829,6 @@ void extcon_dev_unregister(struct extcon_dev *edev) | |||
889 | } | 829 | } |
890 | EXPORT_SYMBOL_GPL(extcon_dev_unregister); | 830 | EXPORT_SYMBOL_GPL(extcon_dev_unregister); |
891 | 831 | ||
892 | static void devm_extcon_dev_unreg(struct device *dev, void *res) | ||
893 | { | ||
894 | extcon_dev_unregister(*(struct extcon_dev **)res); | ||
895 | } | ||
896 | |||
897 | /** | ||
898 | * devm_extcon_dev_register() - Resource-managed extcon_dev_register() | ||
899 | * @dev: device to allocate extcon device | ||
900 | * @edev: the new extcon device to register | ||
901 | * | ||
902 | * Managed extcon_dev_register() function. If extcon device is attached with | ||
903 | * this function, that extcon device is automatically unregistered on driver | ||
904 | * detach. Internally this function calls extcon_dev_register() function. | ||
905 | * To get more information, refer that function. | ||
906 | * | ||
907 | * If extcon device is registered with this function and the device needs to be | ||
908 | * unregistered separately, devm_extcon_dev_unregister() should be used. | ||
909 | * | ||
910 | * Returns 0 if success or negaive error number if failure. | ||
911 | */ | ||
912 | int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev) | ||
913 | { | ||
914 | struct extcon_dev **ptr; | ||
915 | int ret; | ||
916 | |||
917 | ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL); | ||
918 | if (!ptr) | ||
919 | return -ENOMEM; | ||
920 | |||
921 | ret = extcon_dev_register(edev); | ||
922 | if (ret) { | ||
923 | devres_free(ptr); | ||
924 | return ret; | ||
925 | } | ||
926 | |||
927 | *ptr = edev; | ||
928 | devres_add(dev, ptr); | ||
929 | |||
930 | return 0; | ||
931 | } | ||
932 | EXPORT_SYMBOL_GPL(devm_extcon_dev_register); | ||
933 | |||
934 | /** | ||
935 | * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister() | ||
936 | * @dev: device the extcon belongs to | ||
937 | * @edev: the extcon device to unregister | ||
938 | * | ||
939 | * Unregister extcon device that is registered with devm_extcon_dev_register() | ||
940 | * function. | ||
941 | */ | ||
942 | void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev) | ||
943 | { | ||
944 | WARN_ON(devres_release(dev, devm_extcon_dev_unreg, | ||
945 | devm_extcon_dev_match, edev)); | ||
946 | } | ||
947 | EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister); | ||
948 | |||
949 | #ifdef CONFIG_OF | 832 | #ifdef CONFIG_OF |
950 | /* | 833 | /* |
951 | * extcon_get_edev_by_phandle - Get the extcon device from devicetree | 834 | * extcon_get_edev_by_phandle - Get the extcon device from devicetree |