Merge tag 'mm-hotfixes-stable-2024-06-26-17-28' of git://git.kernel.org/pub/scm/linux...
[linux.git] / drivers / spmi / spmi-devres.c
blob62c4b3f24d0656eea9b6da489b7716b9965bedbe
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright 2023 Google LLC.
4 */
6 #include <linux/device.h>
7 #include <linux/spmi.h>
9 static void devm_spmi_controller_release(struct device *parent, void *res)
11 spmi_controller_put(*(struct spmi_controller **)res);
14 struct spmi_controller *devm_spmi_controller_alloc(struct device *parent, size_t size)
16 struct spmi_controller **ptr, *ctrl;
18 ptr = devres_alloc(devm_spmi_controller_release, sizeof(*ptr), GFP_KERNEL);
19 if (!ptr)
20 return ERR_PTR(-ENOMEM);
22 ctrl = spmi_controller_alloc(parent, size);
23 if (IS_ERR(ctrl)) {
24 devres_free(ptr);
25 return ctrl;
28 *ptr = ctrl;
29 devres_add(parent, ptr);
31 return ctrl;
33 EXPORT_SYMBOL_GPL(devm_spmi_controller_alloc);
35 static void devm_spmi_controller_remove(struct device *parent, void *res)
37 spmi_controller_remove(*(struct spmi_controller **)res);
40 int devm_spmi_controller_add(struct device *parent, struct spmi_controller *ctrl)
42 struct spmi_controller **ptr;
43 int ret;
45 ptr = devres_alloc(devm_spmi_controller_remove, sizeof(*ptr), GFP_KERNEL);
46 if (!ptr)
47 return -ENOMEM;
49 ret = spmi_controller_add(ctrl);
50 if (ret) {
51 devres_free(ptr);
52 return ret;
55 *ptr = ctrl;
56 devres_add(parent, ptr);
58 return 0;
61 EXPORT_SYMBOL_GPL(devm_spmi_controller_add);
63 MODULE_LICENSE("GPL");
64 MODULE_DESCRIPTION("SPMI devres helpers");