media: staging: atomisp: Remove dead code for MID (#2)
[linux-2.6/btrfs-unstable.git] / drivers / extcon / devres.c
blob186fd735eb2866ba087ac155bbcb845de6159bc9
1 /*
2 * drivers/extcon/devres.c - EXTCON device's resource management
4 * Copyright (C) 2016 Samsung Electronics
5 * Author: Chanwoo Choi <cw00.choi@samsung.com>
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.
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.
17 #include "extcon.h"
19 static int devm_extcon_dev_match(struct device *dev, void *res, void *data)
21 struct extcon_dev **r = res;
23 if (WARN_ON(!r || !*r))
24 return 0;
26 return *r == data;
29 static void devm_extcon_dev_release(struct device *dev, void *res)
31 extcon_dev_free(*(struct extcon_dev **)res);
35 static void devm_extcon_dev_unreg(struct device *dev, void *res)
37 extcon_dev_unregister(*(struct extcon_dev **)res);
40 struct extcon_dev_notifier_devres {
41 struct extcon_dev *edev;
42 unsigned int id;
43 struct notifier_block *nb;
46 static void devm_extcon_dev_notifier_unreg(struct device *dev, void *res)
48 struct extcon_dev_notifier_devres *this = res;
50 extcon_unregister_notifier(this->edev, this->id, this->nb);
53 static void devm_extcon_dev_notifier_all_unreg(struct device *dev, void *res)
55 struct extcon_dev_notifier_devres *this = res;
57 extcon_unregister_notifier_all(this->edev, this->nb);
60 /**
61 * devm_extcon_dev_allocate - Allocate managed extcon device
62 * @dev: device owning the extcon device being created
63 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
64 * If supported_cable is NULL, cable name related APIs
65 * are disabled.
67 * This function manages automatically the memory of extcon device using device
68 * resource management and simplify the control of freeing the memory of extcon
69 * device.
71 * Returns the pointer memory of allocated extcon_dev if success
72 * or ERR_PTR(err) if fail
74 struct extcon_dev *devm_extcon_dev_allocate(struct device *dev,
75 const unsigned int *supported_cable)
77 struct extcon_dev **ptr, *edev;
79 ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL);
80 if (!ptr)
81 return ERR_PTR(-ENOMEM);
83 edev = extcon_dev_allocate(supported_cable);
84 if (IS_ERR(edev)) {
85 devres_free(ptr);
86 return edev;
89 edev->dev.parent = dev;
91 *ptr = edev;
92 devres_add(dev, ptr);
94 return edev;
96 EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate);
98 /**
99 * devm_extcon_dev_free() - Resource-managed extcon_dev_unregister()
100 * @dev: device the extcon belongs to
101 * @edev: the extcon device to unregister
103 * Free the memory that is allocated with devm_extcon_dev_allocate()
104 * function.
106 void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev)
108 WARN_ON(devres_release(dev, devm_extcon_dev_release,
109 devm_extcon_dev_match, edev));
111 EXPORT_SYMBOL_GPL(devm_extcon_dev_free);
114 * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
115 * @dev: device to allocate extcon device
116 * @edev: the new extcon device to register
118 * Managed extcon_dev_register() function. If extcon device is attached with
119 * this function, that extcon device is automatically unregistered on driver
120 * detach. Internally this function calls extcon_dev_register() function.
121 * To get more information, refer that function.
123 * If extcon device is registered with this function and the device needs to be
124 * unregistered separately, devm_extcon_dev_unregister() should be used.
126 * Returns 0 if success or negaive error number if failure.
128 int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev)
130 struct extcon_dev **ptr;
131 int ret;
133 ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL);
134 if (!ptr)
135 return -ENOMEM;
137 ret = extcon_dev_register(edev);
138 if (ret) {
139 devres_free(ptr);
140 return ret;
143 *ptr = edev;
144 devres_add(dev, ptr);
146 return 0;
148 EXPORT_SYMBOL_GPL(devm_extcon_dev_register);
151 * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
152 * @dev: device the extcon belongs to
153 * @edev: the extcon device to unregister
155 * Unregister extcon device that is registered with devm_extcon_dev_register()
156 * function.
158 void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev)
160 WARN_ON(devres_release(dev, devm_extcon_dev_unreg,
161 devm_extcon_dev_match, edev));
163 EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister);
166 * devm_extcon_register_notifier() - Resource-managed extcon_register_notifier()
167 * @dev: device to allocate extcon device
168 * @edev: the extcon device that has the external connecotr.
169 * @id: the unique id of each external connector in extcon enumeration.
170 * @nb: a notifier block to be registered.
172 * This function manages automatically the notifier of extcon device using
173 * device resource management and simplify the control of unregistering
174 * the notifier of extcon device.
176 * Note that the second parameter given to the callback of nb (val) is
177 * "old_state", not the current state. The current state can be retrieved
178 * by looking at the third pameter (edev pointer)'s state value.
180 * Returns 0 if success or negaive error number if failure.
182 int devm_extcon_register_notifier(struct device *dev, struct extcon_dev *edev,
183 unsigned int id, struct notifier_block *nb)
185 struct extcon_dev_notifier_devres *ptr;
186 int ret;
188 ptr = devres_alloc(devm_extcon_dev_notifier_unreg, sizeof(*ptr),
189 GFP_KERNEL);
190 if (!ptr)
191 return -ENOMEM;
193 ret = extcon_register_notifier(edev, id, nb);
194 if (ret) {
195 devres_free(ptr);
196 return ret;
199 ptr->edev = edev;
200 ptr->id = id;
201 ptr->nb = nb;
202 devres_add(dev, ptr);
204 return 0;
206 EXPORT_SYMBOL(devm_extcon_register_notifier);
209 * devm_extcon_unregister_notifier()
210 - Resource-managed extcon_unregister_notifier()
211 * @dev: device to allocate extcon device
212 * @edev: the extcon device that has the external connecotr.
213 * @id: the unique id of each external connector in extcon enumeration.
214 * @nb: a notifier block to be registered.
216 void devm_extcon_unregister_notifier(struct device *dev,
217 struct extcon_dev *edev, unsigned int id,
218 struct notifier_block *nb)
220 WARN_ON(devres_release(dev, devm_extcon_dev_notifier_unreg,
221 devm_extcon_dev_match, edev));
223 EXPORT_SYMBOL(devm_extcon_unregister_notifier);
226 * devm_extcon_register_notifier_all()
227 * - Resource-managed extcon_register_notifier_all()
228 * @dev: device to allocate extcon device
229 * @edev: the extcon device that has the external connecotr.
230 * @nb: a notifier block to be registered.
232 * This function manages automatically the notifier of extcon device using
233 * device resource management and simplify the control of unregistering
234 * the notifier of extcon device. To get more information, refer that function.
236 * Returns 0 if success or negaive error number if failure.
238 int devm_extcon_register_notifier_all(struct device *dev, struct extcon_dev *edev,
239 struct notifier_block *nb)
241 struct extcon_dev_notifier_devres *ptr;
242 int ret;
244 ptr = devres_alloc(devm_extcon_dev_notifier_all_unreg, sizeof(*ptr),
245 GFP_KERNEL);
246 if (!ptr)
247 return -ENOMEM;
249 ret = extcon_register_notifier_all(edev, nb);
250 if (ret) {
251 devres_free(ptr);
252 return ret;
255 ptr->edev = edev;
256 ptr->nb = nb;
257 devres_add(dev, ptr);
259 return 0;
261 EXPORT_SYMBOL(devm_extcon_register_notifier_all);
264 * devm_extcon_unregister_notifier_all()
265 * - Resource-managed extcon_unregister_notifier_all()
266 * @dev: device to allocate extcon device
267 * @edev: the extcon device that has the external connecotr.
268 * @nb: a notifier block to be registered.
270 void devm_extcon_unregister_notifier_all(struct device *dev,
271 struct extcon_dev *edev,
272 struct notifier_block *nb)
274 WARN_ON(devres_release(dev, devm_extcon_dev_notifier_all_unreg,
275 devm_extcon_dev_match, edev));
277 EXPORT_SYMBOL(devm_extcon_unregister_notifier_all);