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.
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
))
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
;
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
);
61 * devm_extcon_dev_allocate - Allocate managed extcon device
62 * @dev: the device owning the extcon device being created
63 * @supported_cable: the array of the supported external connectors
64 * ending with EXTCON_NONE.
66 * This function manages automatically the memory of extcon device using device
67 * resource management and simplify the control of freeing the memory of extcon
70 * Returns the pointer memory of allocated extcon_dev if success
71 * or ERR_PTR(err) if fail
73 struct extcon_dev
*devm_extcon_dev_allocate(struct device
*dev
,
74 const unsigned int *supported_cable
)
76 struct extcon_dev
**ptr
, *edev
;
78 ptr
= devres_alloc(devm_extcon_dev_release
, sizeof(*ptr
), GFP_KERNEL
);
80 return ERR_PTR(-ENOMEM
);
82 edev
= extcon_dev_allocate(supported_cable
);
88 edev
->dev
.parent
= dev
;
95 EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate
);
98 * devm_extcon_dev_free() - Resource-managed extcon_dev_unregister()
99 * @dev: the device owning the extcon device being created
100 * @edev: the extcon device to be freed
102 * Free the memory that is allocated with devm_extcon_dev_allocate()
105 void devm_extcon_dev_free(struct device
*dev
, struct extcon_dev
*edev
)
107 WARN_ON(devres_release(dev
, devm_extcon_dev_release
,
108 devm_extcon_dev_match
, edev
));
110 EXPORT_SYMBOL_GPL(devm_extcon_dev_free
);
113 * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
114 * @dev: the device owning the extcon device being created
115 * @edev: the extcon device to be registered
117 * this function, that extcon device is automatically unregistered on driver
118 * detach. Internally this function calls extcon_dev_register() function.
119 * To get more information, refer that function.
121 * If extcon device is registered with this function and the device needs to be
122 * unregistered separately, devm_extcon_dev_unregister() should be used.
124 * Returns 0 if success or negaive error number if failure.
126 int devm_extcon_dev_register(struct device
*dev
, struct extcon_dev
*edev
)
128 struct extcon_dev
**ptr
;
131 ptr
= devres_alloc(devm_extcon_dev_unreg
, sizeof(*ptr
), GFP_KERNEL
);
135 ret
= extcon_dev_register(edev
);
142 devres_add(dev
, ptr
);
146 EXPORT_SYMBOL_GPL(devm_extcon_dev_register
);
149 * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
150 * @dev: the device owning the extcon device being created
151 * @edev: the extcon device to unregistered
153 * Unregister extcon device that is registered with devm_extcon_dev_register()
156 void devm_extcon_dev_unregister(struct device
*dev
, struct extcon_dev
*edev
)
158 WARN_ON(devres_release(dev
, devm_extcon_dev_unreg
,
159 devm_extcon_dev_match
, edev
));
161 EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister
);
164 * devm_extcon_register_notifier() - Resource-managed extcon_register_notifier()
165 * @dev: the device owning the extcon device being created
166 * @edev: the extcon device
167 * @id: the unique id among the extcon enumeration
168 * @nb: a notifier block to be registered
170 * This function manages automatically the notifier of extcon device using
171 * device resource management and simplify the control of unregistering
172 * the notifier of extcon device.
174 * Note that the second parameter given to the callback of nb (val) is
175 * "old_state", not the current state. The current state can be retrieved
176 * by looking at the third pameter (edev pointer)'s state value.
178 * Returns 0 if success or negaive error number if failure.
180 int devm_extcon_register_notifier(struct device
*dev
, struct extcon_dev
*edev
,
181 unsigned int id
, struct notifier_block
*nb
)
183 struct extcon_dev_notifier_devres
*ptr
;
186 ptr
= devres_alloc(devm_extcon_dev_notifier_unreg
, sizeof(*ptr
),
191 ret
= extcon_register_notifier(edev
, id
, nb
);
200 devres_add(dev
, ptr
);
204 EXPORT_SYMBOL(devm_extcon_register_notifier
);
207 * devm_extcon_unregister_notifier()
208 - Resource-managed extcon_unregister_notifier()
209 * @dev: the device owning the extcon device being created
210 * @edev: the extcon device
211 * @id: the unique id among the extcon enumeration
212 * @nb: a notifier block to be registered
214 void devm_extcon_unregister_notifier(struct device
*dev
,
215 struct extcon_dev
*edev
, unsigned int id
,
216 struct notifier_block
*nb
)
218 WARN_ON(devres_release(dev
, devm_extcon_dev_notifier_unreg
,
219 devm_extcon_dev_match
, edev
));
221 EXPORT_SYMBOL(devm_extcon_unregister_notifier
);
224 * devm_extcon_register_notifier_all()
225 * - Resource-managed extcon_register_notifier_all()
226 * @dev: the device owning the extcon device being created
227 * @edev: the extcon device
228 * @nb: a notifier block to be registered
230 * This function manages automatically the notifier of extcon device using
231 * device resource management and simplify the control of unregistering
232 * the notifier of extcon device. To get more information, refer that function.
234 * Returns 0 if success or negaive error number if failure.
236 int devm_extcon_register_notifier_all(struct device
*dev
, struct extcon_dev
*edev
,
237 struct notifier_block
*nb
)
239 struct extcon_dev_notifier_devres
*ptr
;
242 ptr
= devres_alloc(devm_extcon_dev_notifier_all_unreg
, sizeof(*ptr
),
247 ret
= extcon_register_notifier_all(edev
, nb
);
255 devres_add(dev
, ptr
);
259 EXPORT_SYMBOL(devm_extcon_register_notifier_all
);
262 * devm_extcon_unregister_notifier_all()
263 * - Resource-managed extcon_unregister_notifier_all()
264 * @dev: the device owning the extcon device being created
265 * @edev: the extcon device
266 * @nb: a notifier block to be registered
268 void devm_extcon_unregister_notifier_all(struct device
*dev
,
269 struct extcon_dev
*edev
,
270 struct notifier_block
*nb
)
272 WARN_ON(devres_release(dev
, devm_extcon_dev_notifier_all_unreg
,
273 devm_extcon_dev_match
, edev
));
275 EXPORT_SYMBOL(devm_extcon_unregister_notifier_all
);