Skip to content

Commit 58f3865

Browse files
committed
extcon: Add resource-managed functions to register extcon notifier
This patch adds the resource-managed functions for register/unregister the extcon notifier with the id of each external connector. This function will make it easy to handle the extcon notifier. - int devm_extcon_register_notifier(struct device *dev, struct extcon_dev *edev, unsigned int id, struct notifier_block *nb); - void devm_extcon_unregister_notifier(struct device *dev, struct extcon_dev *edev, unsigned int id, struct notifier_block *nb); Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
1 parent b225d00 commit 58f3865

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

‎drivers/extcon/devres.c‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ static void devm_extcon_dev_unreg(struct device *dev, void *res)
3737
extcon_dev_unregister(*(struct extcon_dev **)res);
3838
}
3939

40+
struct extcon_dev_notifier_devres {
41+
struct extcon_dev *edev;
42+
unsigned int id;
43+
struct notifier_block *nb;
44+
};
45+
46+
static void devm_extcon_dev_notifier_unreg(struct device *dev, void *res)
47+
{
48+
struct extcon_dev_notifier_devres *this = res;
49+
50+
extcon_unregister_notifier(this->edev, this->id, this->nb);
51+
}
52+
4053
/**
4154
* devm_extcon_dev_allocate - Allocate managed extcon device
4255
* @dev: device owning the extcon device being created
@@ -141,3 +154,63 @@ void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev)
141154
devm_extcon_dev_match, edev));
142155
}
143156
EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister);
157+
158+
/**
159+
* devm_extcon_register_notifier() - Resource-managed extcon_register_notifier()
160+
* @dev: device to allocate extcon device
161+
* @edev: the extcon device that has the external connecotr.
162+
* @id: the unique id of each external connector in extcon enumeration.
163+
* @nb: a notifier block to be registered.
164+
*
165+
* This function manages automatically the notifier of extcon device using
166+
* device resource management and simplify the control of unregistering
167+
* the notifier of extcon device.
168+
*
169+
* Note that the second parameter given to the callback of nb (val) is
170+
* "old_state", not the current state. The current state can be retrieved
171+
* by looking at the third pameter (edev pointer)'s state value.
172+
*
173+
* Returns 0 if success or negaive error number if failure.
174+
*/
175+
int devm_extcon_register_notifier(struct device *dev, struct extcon_dev *edev,
176+
unsigned int id, struct notifier_block *nb)
177+
{
178+
struct extcon_dev_notifier_devres *ptr;
179+
int ret;
180+
181+
ptr = devres_alloc(devm_extcon_dev_notifier_unreg, sizeof(*ptr),
182+
GFP_KERNEL);
183+
if (!ptr)
184+
return -ENOMEM;
185+
186+
ret = extcon_register_notifier(edev, id, nb);
187+
if (ret) {
188+
devres_free(ptr);
189+
return ret;
190+
}
191+
192+
ptr->edev = edev;
193+
ptr->id = id;
194+
ptr->nb = nb;
195+
devres_add(dev, ptr);
196+
197+
return 0;
198+
}
199+
EXPORT_SYMBOL(devm_extcon_register_notifier);
200+
201+
/**
202+
* devm_extcon_unregister_notifier()
203+
- Resource-managed extcon_unregister_notifier()
204+
* @dev: device to allocate extcon device
205+
* @edev: the extcon device that has the external connecotr.
206+
* @id: the unique id of each external connector in extcon enumeration.
207+
* @nb: a notifier block to be registered.
208+
*/
209+
void devm_extcon_unregister_notifier(struct device *dev,
210+
struct extcon_dev *edev, unsigned int id,
211+
struct notifier_block *nb)
212+
{
213+
WARN_ON(devres_release(dev, devm_extcon_dev_notifier_unreg,
214+
devm_extcon_dev_match, edev));
215+
}
216+
EXPORT_SYMBOL(devm_extcon_unregister_notifier);

‎include/linux/extcon.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ extern int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
182182
struct notifier_block *nb);
183183
extern int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
184184
struct notifier_block *nb);
185+
extern int devm_extcon_register_notifier(struct device *dev,
186+
struct extcon_dev *edev, unsigned int id,
187+
struct notifier_block *nb);
188+
extern void devm_extcon_unregister_notifier(struct device *dev,
189+
struct extcon_dev *edev, unsigned int id,
190+
struct notifier_block *nb);
185191

186192
/*
187193
* Following API get the extcon device from devicetree.
@@ -273,6 +279,17 @@ static inline int extcon_unregister_notifier(struct extcon_dev *edev,
273279
return 0;
274280
}
275281

282+
static inline int devm_extcon_register_notifier(struct device *dev,
283+
struct extcon_dev *edev, unsigned int id,
284+
struct notifier_block *nb)
285+
{
286+
return -ENOSYS;
287+
}
288+
289+
static inline void devm_extcon_unregister_notifier(struct device *dev,
290+
struct extcon_dev *edev, unsigned int id,
291+
struct notifier_block *nb) { }
292+
276293
static inline struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev,
277294
int index)
278295
{

0 commit comments

Comments
 (0)