Commit 1bfc485b authored by Bartosz Golaszewski's avatar Bartosz Golaszewski Committed by Alexandre Belloni

rtc: shrink devm_rtc_allocate_device()

We don't need to use devres_alloc() & devres_add() manually if all we
want to manage is a single pointer. We can shrink the code by using
devm_add_action_or_reset() instead. The number of allocations stays
the same.
Signed-off-by: default avatarBartosz Golaszewski <bgolaszewski@baylibre.com>
Signed-off-by: default avatarAlexandre Belloni <alexandre.belloni@bootlin.com>
Link: https://lore.kernel.org/r/20201109163409.24301-9-brgl@bgdev.pl
parent fdcfd854
...@@ -337,48 +337,37 @@ static void devm_rtc_unregister_device(void *data) ...@@ -337,48 +337,37 @@ static void devm_rtc_unregister_device(void *data)
put_device(&rtc->dev); put_device(&rtc->dev);
} }
static void devm_rtc_release_device(struct device *dev, void *res) static void devm_rtc_release_device(void *res)
{ {
struct rtc_device *rtc = *(struct rtc_device **)res; struct rtc_device *rtc = res;
put_device(&rtc->dev); put_device(&rtc->dev);
} }
struct rtc_device *devm_rtc_allocate_device(struct device *dev) struct rtc_device *devm_rtc_allocate_device(struct device *dev)
{ {
struct rtc_device **ptr, *rtc; struct rtc_device *rtc;
int id, err; int id, err;
id = rtc_device_get_id(dev); id = rtc_device_get_id(dev);
if (id < 0) if (id < 0)
return ERR_PTR(id); return ERR_PTR(id);
ptr = devres_alloc(devm_rtc_release_device, sizeof(*ptr), GFP_KERNEL);
if (!ptr) {
err = -ENOMEM;
goto exit_ida;
}
rtc = rtc_allocate_device(); rtc = rtc_allocate_device();
if (!rtc) { if (!rtc) {
err = -ENOMEM; ida_simple_remove(&rtc_ida, id);
goto exit_devres; return ERR_PTR(-ENOMEM);
} }
*ptr = rtc;
devres_add(dev, ptr);
rtc->id = id; rtc->id = id;
rtc->dev.parent = dev; rtc->dev.parent = dev;
dev_set_name(&rtc->dev, "rtc%d", id); dev_set_name(&rtc->dev, "rtc%d", id);
return rtc; err = devm_add_action_or_reset(dev, devm_rtc_release_device, rtc);
if (err)
exit_devres:
devres_free(ptr);
exit_ida:
ida_simple_remove(&rtc_ida, id);
return ERR_PTR(err); return ERR_PTR(err);
return rtc;
} }
EXPORT_SYMBOL_GPL(devm_rtc_allocate_device); EXPORT_SYMBOL_GPL(devm_rtc_allocate_device);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment