1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/extcon/devres.c - EXTCON device's resource management
5 * Copyright (C) 2016 Samsung Electronics
6 * Author: Chanwoo Choi <cw00.choi@samsung.com>
11 static int devm_extcon_dev_match(struct device *dev, void *res, void *data)
13 struct extcon_dev **r = res;
15 if (WARN_ON(!r || !*r))
21 static void devm_extcon_dev_release(struct device *dev, void *res)
23 extcon_dev_free(*(struct extcon_dev **)res);
27 static void devm_extcon_dev_unreg(struct device *dev, void *res)
29 extcon_dev_unregister(*(struct extcon_dev **)res);
32 struct extcon_dev_notifier_devres {
33 struct extcon_dev *edev;
35 struct notifier_block *nb;
38 static void devm_extcon_dev_notifier_unreg(struct device *dev, void *res)
40 struct extcon_dev_notifier_devres *this = res;
42 extcon_unregister_notifier(this->edev, this->id, this->nb);
45 static void devm_extcon_dev_notifier_all_unreg(struct device *dev, void *res)
47 struct extcon_dev_notifier_devres *this = res;
49 extcon_unregister_notifier_all(this->edev, this->nb);
53 * devm_extcon_dev_allocate - Allocate managed extcon device
54 * @dev: the device owning the extcon device being created
55 * @supported_cable: the array of the supported external connectors
56 * ending with EXTCON_NONE.
58 * This function manages automatically the memory of extcon device using device
59 * resource management and simplify the control of freeing the memory of extcon
62 * Returns the pointer memory of allocated extcon_dev if success
63 * or ERR_PTR(err) if fail
65 struct extcon_dev *devm_extcon_dev_allocate(struct device *dev,
66 const unsigned int *supported_cable)
68 struct extcon_dev **ptr, *edev;
70 ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL);
72 return ERR_PTR(-ENOMEM);
74 edev = extcon_dev_allocate(supported_cable);
80 edev->dev.parent = dev;
87 EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate);
90 * devm_extcon_dev_free() - Resource-managed extcon_dev_unregister()
91 * @dev: the device owning the extcon device being created
92 * @edev: the extcon device to be freed
94 * Free the memory that is allocated with devm_extcon_dev_allocate()
97 void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev)
99 WARN_ON(devres_release(dev, devm_extcon_dev_release,
100 devm_extcon_dev_match, edev));
102 EXPORT_SYMBOL_GPL(devm_extcon_dev_free);
105 * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
106 * @dev: the device owning the extcon device being created
107 * @edev: the extcon device to be registered
109 * this function, that extcon device is automatically unregistered on driver
110 * detach. Internally this function calls extcon_dev_register() function.
111 * To get more information, refer that function.
113 * If extcon device is registered with this function and the device needs to be
114 * unregistered separately, devm_extcon_dev_unregister() should be used.
116 * Returns 0 if success or negaive error number if failure.
118 int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev)
120 struct extcon_dev **ptr;
123 ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL);
127 ret = extcon_dev_register(edev);
134 devres_add(dev, ptr);
138 EXPORT_SYMBOL_GPL(devm_extcon_dev_register);
141 * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
142 * @dev: the device owning the extcon device being created
143 * @edev: the extcon device to unregistered
145 * Unregister extcon device that is registered with devm_extcon_dev_register()
148 void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev)
150 WARN_ON(devres_release(dev, devm_extcon_dev_unreg,
151 devm_extcon_dev_match, edev));
153 EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister);
156 * devm_extcon_register_notifier() - Resource-managed extcon_register_notifier()
157 * @dev: the device owning the extcon device being created
158 * @edev: the extcon device
159 * @id: the unique id among the extcon enumeration
160 * @nb: a notifier block to be registered
162 * This function manages automatically the notifier of extcon device using
163 * device resource management and simplify the control of unregistering
164 * the notifier of extcon device.
166 * Note that the second parameter given to the callback of nb (val) is
167 * "old_state", not the current state. The current state can be retrieved
168 * by looking at the third pameter (edev pointer)'s state value.
170 * Returns 0 if success or negaive error number if failure.
172 int devm_extcon_register_notifier(struct device *dev, struct extcon_dev *edev,
173 unsigned int id, struct notifier_block *nb)
175 struct extcon_dev_notifier_devres *ptr;
178 ptr = devres_alloc(devm_extcon_dev_notifier_unreg, sizeof(*ptr),
183 ret = extcon_register_notifier(edev, id, nb);
192 devres_add(dev, ptr);
196 EXPORT_SYMBOL(devm_extcon_register_notifier);
199 * devm_extcon_unregister_notifier()
200 * - Resource-managed extcon_unregister_notifier()
201 * @dev: the device owning the extcon device being created
202 * @edev: the extcon device
203 * @id: the unique id among the extcon enumeration
204 * @nb: a notifier block to be registered
206 void devm_extcon_unregister_notifier(struct device *dev,
207 struct extcon_dev *edev, unsigned int id,
208 struct notifier_block *nb)
210 WARN_ON(devres_release(dev, devm_extcon_dev_notifier_unreg,
211 devm_extcon_dev_match, edev));
213 EXPORT_SYMBOL(devm_extcon_unregister_notifier);
216 * devm_extcon_register_notifier_all()
217 * - Resource-managed extcon_register_notifier_all()
218 * @dev: the device owning the extcon device being created
219 * @edev: the extcon device
220 * @nb: a notifier block to be registered
222 * This function manages automatically the notifier of extcon device using
223 * device resource management and simplify the control of unregistering
224 * the notifier of extcon device. To get more information, refer that function.
226 * Returns 0 if success or negaive error number if failure.
228 int devm_extcon_register_notifier_all(struct device *dev, struct extcon_dev *edev,
229 struct notifier_block *nb)
231 struct extcon_dev_notifier_devres *ptr;
234 ptr = devres_alloc(devm_extcon_dev_notifier_all_unreg, sizeof(*ptr),
239 ret = extcon_register_notifier_all(edev, nb);
247 devres_add(dev, ptr);
251 EXPORT_SYMBOL(devm_extcon_register_notifier_all);
254 * devm_extcon_unregister_notifier_all()
255 * - Resource-managed extcon_unregister_notifier_all()
256 * @dev: the device owning the extcon device being created
257 * @edev: the extcon device
258 * @nb: a notifier block to be registered
260 void devm_extcon_unregister_notifier_all(struct device *dev,
261 struct extcon_dev *edev,
262 struct notifier_block *nb)
264 WARN_ON(devres_release(dev, devm_extcon_dev_notifier_all_unreg,
265 devm_extcon_dev_match, edev));
267 EXPORT_SYMBOL(devm_extcon_unregister_notifier_all);