[PATCH 5/5] spmi: Return meaningful errors in spmi_controller_alloc()

From: Fei Shao
Date: Thu Aug 24 2023 - 06:43:06 EST


spmi_controller_alloc() currently returns NULL to all types of errors,
which can be improved.

Use appropriate error code in returns and pass the errors from used
functions where possible.

Signed-off-by: Fei Shao <fshao@xxxxxxxxxxxx>
---

drivers/spmi/devres.c | 4 ++--
drivers/spmi/spmi.c | 6 +++---
2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/spmi/devres.c b/drivers/spmi/devres.c
index f18cbbe28812..c19f09243bc3 100644
--- a/drivers/spmi/devres.c
+++ b/drivers/spmi/devres.c
@@ -20,9 +20,9 @@ struct spmi_controller *devm_spmi_controller_alloc(struct device *parent, size_t
return ERR_PTR(-ENOMEM);

ctrl = spmi_controller_alloc(parent, size);
- if (!ctrl) {
+ if (IS_ERR(ctrl)) {
devres_free(ptr);
- return ERR_PTR(-ENOMEM);
+ return ctrl;
}

*ptr = ctrl;
diff --git a/drivers/spmi/spmi.c b/drivers/spmi/spmi.c
index 7313d4c18a04..5f11ced3f8b9 100644
--- a/drivers/spmi/spmi.c
+++ b/drivers/spmi/spmi.c
@@ -445,11 +445,11 @@ struct spmi_controller *spmi_controller_alloc(struct device *parent,
int id;

if (WARN_ON(!parent))
- return NULL;
+ return ERR_PTR(-EINVAL);

ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
if (!ctrl)
- return NULL;
+ return ERR_PTR(-ENOMEM);

device_initialize(&ctrl->dev);
ctrl->dev.type = &spmi_ctrl_type;
@@ -463,7 +463,7 @@ struct spmi_controller *spmi_controller_alloc(struct device *parent,
dev_err(parent,
"unable to allocate SPMI controller identifier.\n");
spmi_controller_put(ctrl);
- return NULL;
+ return ERR_PTR(id);
}

ctrl->nr = id;
--
2.42.0.rc1.204.g551eb34607-goog