[PATCH RFC v3 1/5] regulator: move monitor handling into own function

From: Benjamin Bara
Date: Sun May 21 2023 - 07:47:39 EST


From: Benjamin Bara <benjamin.bara@xxxxxxxxxxx>

Similar to the existing implementation, the new function does not handle
EOPNOTSUPP as an error. The initial monitoring state is set to the
regulator state.

Signed-off-by: Benjamin Bara <benjamin.bara@xxxxxxxxxxx>
---
drivers/regulator/core.c | 134 ++++++++++++++++++++++++++++-------------------
1 file changed, 80 insertions(+), 54 deletions(-)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index dc741ac156c3..76f112817f9d 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -1426,7 +1426,7 @@ static int notif_set_limit(struct regulator_dev *rdev,

static int handle_notify_limits(struct regulator_dev *rdev,
int (*set)(struct regulator_dev *, int, int, bool),
- struct notification_limit *limits)
+ const struct notification_limit *limits)
{
int ret = 0;

@@ -1451,6 +1451,80 @@ static int handle_notify_limits(struct regulator_dev *rdev,

return ret;
}
+
+static const struct notification_limit disable_limits = {
+ .prot = REGULATOR_NOTIF_LIMIT_DISABLE,
+ .err = REGULATOR_NOTIF_LIMIT_DISABLE,
+ .warn = REGULATOR_NOTIF_LIMIT_DISABLE,
+};
+
+static int monitors_set_state(struct regulator_dev *rdev, bool enable)
+{
+ const struct regulation_constraints *reg_c = rdev->constraints;
+ const struct regulator_ops *ops = rdev->desc->ops;
+ int ret;
+
+ /* only set the state if monitoring is activated in the device-tree. */
+ if (reg_c->over_voltage_detection) {
+ ret = handle_notify_limits(rdev, ops->set_over_voltage_protection,
+ enable ? &reg_c->over_voltage_limits
+ : &disable_limits);
+ if (ret) {
+ if (ret != -EOPNOTSUPP) {
+ rdev_err(rdev, "failed to set over voltage limits %pe\n",
+ ERR_PTR(ret));
+ return ret;
+ }
+ rdev_warn(rdev,
+ "IC does not support requested over voltage limits\n");
+ }
+ }
+ if (reg_c->under_voltage_detection) {
+ ret = handle_notify_limits(rdev, ops->set_under_voltage_protection,
+ enable ? &reg_c->under_voltage_limits
+ : &disable_limits);
+ if (ret) {
+ if (ret != -EOPNOTSUPP) {
+ rdev_err(rdev, "failed to set under voltage limits %pe\n",
+ ERR_PTR(ret));
+ return ret;
+ }
+ rdev_warn(rdev,
+ "IC does not support requested under voltage limits\n");
+ }
+ }
+ if (reg_c->over_current_detection) {
+ ret = handle_notify_limits(rdev, ops->set_over_current_protection,
+ enable ? &reg_c->over_curr_limits
+ : &disable_limits);
+ if (ret) {
+ if (ret != -EOPNOTSUPP) {
+ rdev_err(rdev, "failed to set over current limits: %pe\n",
+ ERR_PTR(ret));
+ return ret;
+ }
+ rdev_warn(rdev,
+ "IC does not support requested over-current limits\n");
+ }
+ }
+ if (reg_c->over_temp_detection) {
+ ret = handle_notify_limits(rdev, ops->set_thermal_protection,
+ enable ? &reg_c->temp_limits
+ : &disable_limits);
+ if (ret) {
+ if (ret != -EOPNOTSUPP) {
+ rdev_err(rdev, "failed to set temperature limits %pe\n",
+ ERR_PTR(ret));
+ return ret;
+ }
+ rdev_warn(rdev,
+ "IC does not support requested temperature limits\n");
+ }
+ }
+
+ return 0;
+}
+
/**
* set_machine_constraints - sets regulator constraints
* @rdev: regulator source
@@ -1564,60 +1638,12 @@ static int set_machine_constraints(struct regulator_dev *rdev)
}
}

- if (rdev->constraints->over_current_detection)
- ret = handle_notify_limits(rdev,
- ops->set_over_current_protection,
- &rdev->constraints->over_curr_limits);
- if (ret) {
- if (ret != -EOPNOTSUPP) {
- rdev_err(rdev, "failed to set over current limits: %pe\n",
- ERR_PTR(ret));
- return ret;
- }
- rdev_warn(rdev,
- "IC does not support requested over-current limits\n");
- }
-
- if (rdev->constraints->over_voltage_detection)
- ret = handle_notify_limits(rdev,
- ops->set_over_voltage_protection,
- &rdev->constraints->over_voltage_limits);
- if (ret) {
- if (ret != -EOPNOTSUPP) {
- rdev_err(rdev, "failed to set over voltage limits %pe\n",
- ERR_PTR(ret));
- return ret;
- }
- rdev_warn(rdev,
- "IC does not support requested over voltage limits\n");
- }
-
- if (rdev->constraints->under_voltage_detection)
- ret = handle_notify_limits(rdev,
- ops->set_under_voltage_protection,
- &rdev->constraints->under_voltage_limits);
- if (ret) {
- if (ret != -EOPNOTSUPP) {
- rdev_err(rdev, "failed to set under voltage limits %pe\n",
- ERR_PTR(ret));
- return ret;
- }
- rdev_warn(rdev,
- "IC does not support requested under voltage limits\n");
- }
-
- if (rdev->constraints->over_temp_detection)
- ret = handle_notify_limits(rdev,
- ops->set_thermal_protection,
- &rdev->constraints->temp_limits);
- if (ret) {
- if (ret != -EOPNOTSUPP) {
- rdev_err(rdev, "failed to set temperature limits %pe\n",
- ERR_PTR(ret));
+ /* set initial monitor state to current regulator state. */
+ ret = _regulator_is_enabled(rdev);
+ if (ret >= 0) {
+ ret = monitors_set_state(rdev, !!ret);
+ if (ret)
return ret;
- }
- rdev_warn(rdev,
- "IC does not support requested temperature limits\n");
}

if (rdev->constraints->active_discharge && ops->set_active_discharge) {

--
2.34.1