Skip to contentSkip to conversation

drivers/regulator/qcom-labibb-regulator.c:444 qcom_labibb_setup_ocp_irq() warn: 'vreg->ocp_irq' from request_threaded_irq() not released on lines: 444.

PATCH kernel test robot 3 participants 5 messages Nov 25 – Nov 27

Subsystems (2)
lore
Share & tools
lore

kernel test robot

author build bot
#0 lore raw

Unfiltered warning: this smatch report is sent as-is and has not been reviewed for false positives, so please treat it with due skepticism.

BCC: [email protected]
CC: [email protected]
TO: Yaohan Li <[email protected]>
CC: Mark Brown <[email protected]>

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-7.5
head:   d23ba232a8fefe909aeb9366e3905a95a429ebfc
commit: d23ba232a8fefe909aeb9366e3905a95a429ebfc [6/6] regulator: qcom-labibb: stop the recovery workers on teardown

:::::: branch date: 22 hours ago
:::::: commit date: 22 hours ago
config: openrisc-randconfig-2200-20261125 (https://download.01.org/0day-ci/archive/20261125/[email protected]/config)
compiler: or1k-linux-gcc (GCC) 10.5.0
smatch: v0.5.0-9187-g5189e3fb

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

smatch warnings:
drivers/regulator/qcom-labibb-regulator.c:444 qcom_labibb_setup_ocp_irq() warn: 'vreg->ocp_irq' from request_threaded_irq() not released on lines: 444.

vim +444 drivers/regulator/qcom-labibb-regulator.c

d23ba232a8fefe9 Yaohan Li 2026-11-18  384  
d23ba232a8fefe9 Yaohan Li 2026-11-18  385  /*
d23ba232a8fefe9 Yaohan Li 2026-11-18  386   * Request the over-current interrupt once the regulator is registered.
d23ba232a8fefe9 Yaohan Li 2026-11-18  387   * Requesting it from ->set_over_current_protection() is not possible: the
d23ba232a8fefe9 Yaohan Li 2026-11-18  388   * regulator core calls that op from set_machine_constraints(), inside
d23ba232a8fefe9 Yaohan Li 2026-11-18  389   * regulator_register(), and a devres node created there is released after
d23ba232a8fefe9 Yaohan Li 2026-11-18  390   * the regulator itself.  The recovery worker would then still be runnable
d23ba232a8fefe9 Yaohan Li 2026-11-18  391   * while vreg->rdev is already gone.
d23ba232a8fefe9 Yaohan Li 2026-11-18  392   */
d23ba232a8fefe9 Yaohan Li 2026-11-18  393  static int qcom_labibb_setup_ocp_irq(struct labibb_regulator *vreg)
d23ba232a8fefe9 Yaohan Li 2026-11-18  394  {
d23ba232a8fefe9 Yaohan Li 2026-11-18  395  	u32 irq_flags = IRQF_ONESHOT;
d23ba232a8fefe9 Yaohan Li 2026-11-18  396  	int ret;
d23ba232a8fefe9 Yaohan Li 2026-11-18  397  
d23ba232a8fefe9 Yaohan Li 2026-11-18  398  	lockdep_assert_held(&vreg->ocp_lock);
d23ba232a8fefe9 Yaohan Li 2026-11-18  399  
d23ba232a8fefe9 Yaohan Li 2026-11-18  400  	if (vreg->shutting_down)
d23ba232a8fefe9 Yaohan Li 2026-11-18  401  		return -ESHUTDOWN;
d23ba232a8fefe9 Yaohan Li 2026-11-18  402  
d23ba232a8fefe9 Yaohan Li 2026-11-18  403  	if (!vreg->ocp_requested)
d23ba232a8fefe9 Yaohan Li 2026-11-18  404  		return 0;
d23ba232a8fefe9 Yaohan Li 2026-11-18  405  
d23ba232a8fefe9 Yaohan Li 2026-11-18  406  	if (!vreg->ocp_irq_done) {
d23ba232a8fefe9 Yaohan Li 2026-11-18  407  		/* IRQ polarities - LAB: trigger-low, IBB: trigger-high */
d23ba232a8fefe9 Yaohan Li 2026-11-18  408  		switch (vreg->type) {
d23ba232a8fefe9 Yaohan Li 2026-11-18  409  		case QCOM_LAB_TYPE:
d23ba232a8fefe9 Yaohan Li 2026-11-18  410  			irq_flags |= IRQF_TRIGGER_LOW;
d23ba232a8fefe9 Yaohan Li 2026-11-18  411  			break;
d23ba232a8fefe9 Yaohan Li 2026-11-18  412  		case QCOM_IBB_TYPE:
d23ba232a8fefe9 Yaohan Li 2026-11-18  413  			irq_flags |= IRQF_TRIGGER_HIGH;
d23ba232a8fefe9 Yaohan Li 2026-11-18  414  			break;
d23ba232a8fefe9 Yaohan Li 2026-11-18  415  		default:
d23ba232a8fefe9 Yaohan Li 2026-11-18  416  			return -EINVAL;
d23ba232a8fefe9 Yaohan Li 2026-11-18  417  		}
d23ba232a8fefe9 Yaohan Li 2026-11-18  418  
d23ba232a8fefe9 Yaohan Li 2026-11-18  419  		/*
d23ba232a8fefe9 Yaohan Li 2026-11-18  420  		 * The interrupt is owned and released by the cleanup
d23ba232a8fefe9 Yaohan Li 2026-11-18  421  		 * action, so its name has to outlive the interrupt:
d23ba232a8fefe9 Yaohan Li 2026-11-18  422  		 * devres would release a name allocated here before the
d23ba232a8fefe9 Yaohan Li 2026-11-18  423  		 * action runs, while vreg->desc.name is owned by the
d23ba232a8fefe9 Yaohan Li 2026-11-18  424  		 * driver data.
d23ba232a8fefe9 Yaohan Li 2026-11-18  425  		 */
d23ba232a8fefe9 Yaohan Li 2026-11-18  426  		ret = request_threaded_irq(vreg->ocp_irq, NULL,
d23ba232a8fefe9 Yaohan Li 2026-11-18  427  					   qcom_labibb_ocp_isr, irq_flags,
d23ba232a8fefe9 Yaohan Li 2026-11-18  428  					   vreg->desc.name, vreg);
d23ba232a8fefe9 Yaohan Li 2026-11-18  429  		if (ret)
d23ba232a8fefe9 Yaohan Li 2026-11-18  430  			return ret;
d23ba232a8fefe9 Yaohan Li 2026-11-18  431  
d23ba232a8fefe9 Yaohan Li 2026-11-18  432  		vreg->ocp_irq_done = true;
d23ba232a8fefe9 Yaohan Li 2026-11-18  433  	}
d23ba232a8fefe9 Yaohan Li 2026-11-18  434  
d23ba232a8fefe9 Yaohan Li 2026-11-18  435  	/*
d23ba232a8fefe9 Yaohan Li 2026-11-18  436  	 * Enable the hardware source only once a handler exists, and turn
d23ba232a8fefe9 Yaohan Li 2026-11-18  437  	 * it back off if that failed, so that a later attempt can retry
d23ba232a8fefe9 Yaohan Li 2026-11-18  438  	 * without leaving the source enabled on its own.
d23ba232a8fefe9 Yaohan Li 2026-11-18  439  	 */
d23ba232a8fefe9 Yaohan Li 2026-11-18  440  	ret = qcom_labibb_ocp_hw_enable(vreg->rdev);
d23ba232a8fefe9 Yaohan Li 2026-11-18  441  	if (ret)
d23ba232a8fefe9 Yaohan Li 2026-11-18  442  		qcom_labibb_ocp_hw_disable(vreg->rdev);
d23ba232a8fefe9 Yaohan Li 2026-11-18  443  

d23ba232a8fefe9 Yaohan Li 2026-11-18 @444 return ret;
d23ba232a8fefe9 Yaohan Li 2026-11-18 445 }
d23ba232a8fefe9 Yaohan Li 2026-11-18 446

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#1 lore raw

Hi Dan,

Thanks for the report.

Keeping the IRQ registered if qcom_labibb_ocp_hw_enable() fails is intentional. The devm cleanup action, qcom_labibb_stop_works(), is registered before qcom_labibb_setup_ocp_irq() can request the IRQ. Once request_threaded_irq() succeeds, ocp_irq_done is set.

If the failure happens during probe, devres runs the cleanup action and frees the IRQ. If it happens during a later set_ocp() call, keeping the IRQ registered allows a retry without requesting it again. The cleanup action still releases it on unbind.

qcom_labibb_setup_ocp_irq() also runs under ocp_lock, while the cleanup action calls free_irq() after dropping that lock.

I agree that the hardware-enable error handling could be clearer. The helper clears the interrupt latch before writing INT_EN_SET, and the caller currently attempts to disable the source if either step fails. I'll check whether that cleanup would be better handled inside the helper, taking retries and the existing hardware state into account.

Please let me know if I've missed a path where the cleanup action would not run.

Thanks,
Yaohan

#2 lore raw

When I send these emails, the only context I have is what's in the email itself. But I have now downloaded the git tree. The init process is a mess if "->done_this = true" flags. That's a nightmare.

The better way is spelled out in my blog:
https://staticthinking.wordpress.com/2022/04/28/free-the-last-thing-style/

In this way every function either succeeds or cleans up. We only have to keep track of the last thing we have allocated so we don't need ->done_this flags to keep track of it all. There is only just the last thing to keep track of.

Looking at qcom_labibb_ocp_hw_enable() now, can those regmap_update_bits() calls even fail? It's not clear if we're undoing the "Clear irq latch status" or the "Enable OCP HW interrupt"... If we did the clean up inside the qcom_labibb_ocp_hw_enable() instead of the caller, that would make it more clear.

There are two callers so this weird error handling is probably done deliberately, that makes it even less clear what is failing and what we are trying to do. I would prefer to have a normal function the cleaned up after itself and a weird-o qcom_labibb_ocp_hw_enable_no_cleanup() function that had comments explaining why we left it in an inconsistent state.

I asked Claude to review it without any context. Here is the prompt:
"Review qcom_labibb_set_ocp() from drivers/regulator/qcom-labibb-regulator.c to ensure that error handing and cleanup is correct. The error handling ideally should undo all the things that were done so we can fix whatever caused the breakage and retry. Ensure that we don't leave the driver in an usable state."

I haven't looked at Claude's response carefully but it complained about the same code said that there were simpler ways to write this:

@@ static int qcom_labibb_setup_ocp_irq(struct labibb_regulator *vreg)

          ret = qcom_labibb_ocp_hw_enable(vreg->rdev);

- if (ret)
- qcom_labibb_ocp_hw_disable(vreg->rdev);
+ if (ret) {
+ if (!vreg->ocp_hw_enabled)
+ qcom_labibb_ocp_hw_disable(vreg->rdev);
+ return ret;
+ }
+ vreg->ocp_hw_enabled = true;

- return ret;
+ return 0;
}

A simpler alternative: return early from qcom_labibb_setup_ocp_irq() when ocp_irq_done is already true and the source is known to be on. Re-applying the constraints then never touches a working setup.

regards,
dan carpenter

#3 lore raw

I asked Claude to read my blog and re-write the file. It fixes the bug from earlier and two other bugs:

- The "Short-circuit irq not found" message passed vreg->dev before it was set, so it printed without a device. It now uses dev.
- The "error registering" message printed ret, which held the earlier regmap_read() result, not the registration error. It now prints PTR_ERR(vreg->rdev).

Worth checking

- The order inside qcom_labibb_stop() is deliberate: it disables the workers before the interrupt lines. The recovery workers are the only callers of enable_irq(). If the lines were disabled first, a worker still running could re-enable one afterwards.
- The OCP worker is now initialized even without an OCP interrupt. That costs nothing and removes one condition to mirror. The worker can only be queued by the OCP handler.

regards,
dan carpenter

drivers/regulator/qcom-labibb-regulator.c +223 -266
index 49aaa31a539d..c6d066577150 100644
@@ -96,14 +96,18 @@ struct labibb_regulator {
 	int				ocp_irq;
 	int				ocp_irq_count;
 	int				fatal_count;
-	bool				registered;
+	bool				started;
 	bool				ocp_requested;
-	bool				ocp_irq_done;
-	bool				shutting_down;
-	/* serialises the over-current request between set_ocp() and probe */
+	bool				ocp_enabled;
+	/* protects started, ocp_requested and ocp_enabled */
 	struct mutex			ocp_lock;
 };
 
+struct labibb_priv {
+	struct labibb_regulator		*vregs[2];
+	int				num_vregs;
+};
+
 struct labibb_regulator_data {
 	const char			*name;
 	u8				type;
@@ -314,134 +318,26 @@ static irqreturn_t qcom_labibb_ocp_isr(int irq, void *chip)
 }
 
 /*
- * Stop the recovery workers and release the over-current interrupt.  A
- * devres action is used instead of ->remove() so that a probe which fails
- * after the resources were set up goes through the same teardown: the
- * driver core does not call ->remove() for a failed probe, but devres
- * still releases what was registered.
- *
- * This is the only cleanup entry, and the over-current interrupt is
- * requested without devres so that it can be released here: registering a
- * second action from a path that runs under ocp_lock would also make
- * devm_add_action_or_reset() run this function from under that lock on
- * allocation failure.
- */
-static void qcom_labibb_stop_works(void *data)
-{
-	struct labibb_regulator *vreg = data;
-	int irq = 0;
-
-	/*
-	 * Announce the shutdown before anything is drained: a set_ocp()
-	 * that runs from here on must not request the over-current
-	 * interrupt while devres is releasing the action that would stop
-	 * its worker.
-	 */
-	mutex_lock(&vreg->ocp_lock);
-	vreg->shutting_down = true;
-	mutex_unlock(&vreg->ocp_lock);
-
-	/*
-	 * Both recovery workers reschedule themselves, so a plain cancel
-	 * would leave them queued again: disable_delayed_work_sync() also
-	 * blocks the requeue.
-	 */
-	disable_delayed_work_sync(&vreg->sc_recovery_work);
-	if (vreg->ocp_irq > 0) {
-		disable_delayed_work_sync(&vreg->ocp_recovery_work);
-
-		/*
-		 * The over-current handler turns the source off before it
-		 * queues the worker, but a handler that was stopped above
-		 * may have done so after the worker was drained: leave the
-		 * source off now that nobody would turn it back on.
-		 */
-		qcom_labibb_ocp_hw_disable(vreg->rdev);
-
-		mutex_lock(&vreg->ocp_lock);
-		if (vreg->ocp_irq_done) {
-			vreg->ocp_irq_done = false;
-			irq = vreg->ocp_irq;
-		}
-		mutex_unlock(&vreg->ocp_lock);
-
-		/*
-		 * A stopped handler can also have left the interrupt
-		 * disabled with disable_irq_nosync(), while the recovery
-		 * worker - the only caller of enable_irq() - is gone.  The
-		 * depth is deliberately not unwound here: free_irq() shuts
-		 * the line down regardless of it, whereas an enable_irq()
-		 * racing with a handler that is still running would unmask
-		 * a line whose thread has not finished.
-		 *
-		 * free_irq() waits for a running handler, so it is called
-		 * without ocp_lock held.
-		 */
-		if (irq)
-			free_irq(irq, vreg);
-	}
-}
-
-/*
- * Request the over-current interrupt once the regulator is registered.
- * Requesting it from ->set_over_current_protection() is not possible: the
- * regulator core calls that op from set_machine_constraints(), inside
- * regulator_register(), and a devres node created there is released after
- * the regulator itself.  The recovery worker would then still be runnable
- * while vreg->rdev is already gone.
+ * Turn on the over-current source and its interrupt line, once the
+ * protection was requested and the regulator is started.
  */
-static int qcom_labibb_setup_ocp_irq(struct labibb_regulator *vreg)
+static int qcom_labibb_ocp_arm(struct labibb_regulator *vreg)
 {
-	u32 irq_flags = IRQF_ONESHOT;
 	int ret;
 
 	lockdep_assert_held(&vreg->ocp_lock);
 
-	if (vreg->shutting_down)
-		return -ESHUTDOWN;
-
-	if (!vreg->ocp_requested)
+	if (!vreg->ocp_requested || vreg->ocp_enabled)
 		return 0;
 
-	if (!vreg->ocp_irq_done) {
-		/* IRQ polarities - LAB: trigger-low, IBB: trigger-high */
-		switch (vreg->type) {
-		case QCOM_LAB_TYPE:
-			irq_flags |= IRQF_TRIGGER_LOW;
-			break;
-		case QCOM_IBB_TYPE:
-			irq_flags |= IRQF_TRIGGER_HIGH;
-			break;
-		default:
-			return -EINVAL;
-		}
-
-		/*
-		 * The interrupt is owned and released by the cleanup
-		 * action, so its name has to outlive the interrupt:
-		 * devres would release a name allocated here before the
-		 * action runs, while vreg->desc.name is owned by the
-		 * driver data.
-		 */
-		ret = request_threaded_irq(vreg->ocp_irq, NULL,
-					   qcom_labibb_ocp_isr, irq_flags,
-					   vreg->desc.name, vreg);
-		if (ret)
-			return ret;
-
-		vreg->ocp_irq_done = true;
-	}
-
-	/*
-	 * Enable the hardware source only once a handler exists, and turn
-	 * it back off if that failed, so that a later attempt can retry
-	 * without leaving the source enabled on its own.
-	 */
 	ret = qcom_labibb_ocp_hw_enable(vreg->rdev);
 	if (ret)
-		qcom_labibb_ocp_hw_disable(vreg->rdev);
+		return ret;
 
-	return ret;
+	enable_irq(vreg->ocp_irq);
+	vreg->ocp_enabled = true;
+
+	return 0;
 }
 
 static int qcom_labibb_set_ocp(struct regulator_dev *rdev, int lim,
@@ -462,24 +358,10 @@ static int qcom_labibb_set_ocp(struct regulator_dev *rdev, int lim,
 		return -EINVAL;
 
 	/* IRQ polarities - LAB: trigger-low, IBB: trigger-high */
-	switch (vreg->type) {
-	case QCOM_LAB_TYPE:
-		irq_trig_low = 1;
-		break;
-	case QCOM_IBB_TYPE:
-		irq_trig_low = 0;
-		break;
-	default:
-		return -EINVAL;
-	}
+	irq_trig_low = vreg->type == QCOM_LAB_TYPE;
 
 	mutex_lock(&vreg->ocp_lock);
 
-	if (vreg->shutting_down) {
-		ret = -ESHUTDOWN;
-		goto unlock;
-	}
-
 	/* Activate OCP HW level interrupt */
 	ret = regmap_update_bits(rdev->regmap,
 				 vreg->base + REG_LABIBB_INT_SET_TYPE,
@@ -502,13 +384,9 @@ static int qcom_labibb_set_ocp(struct regulator_dev *rdev, int lim,
 
 	vreg->ocp_requested = true;
 
-	/*
-	 * During regulator_register() there is no interrupt yet and no
-	 * cleanup action to pair with it; the probe finishes the job once
-	 * the regulator and its devres nodes exist.
-	 */
-	if (vreg->registered)
-		ret = qcom_labibb_setup_ocp_irq(vreg);
+	/* During regulator_register() this waits for qcom_labibb_start() */
+	if (vreg->started)
+		ret = qcom_labibb_ocp_arm(vreg);
 
 unlock:
 	mutex_unlock(&vreg->ocp_lock);
@@ -899,153 +777,231 @@ static const struct of_device_id qcom_labibb_match[] = {
 };
 MODULE_DEVICE_TABLE(of, qcom_labibb_match);
 
-static int qcom_labibb_regulator_probe(struct platform_device *pdev)
+/* Turn on the interrupts and, if it was requested, over-current protection */
+static int qcom_labibb_start(struct labibb_regulator *vreg)
+{
+	int ret;
+
+	mutex_lock(&vreg->ocp_lock);
+	ret = qcom_labibb_ocp_arm(vreg);
+	if (ret)
+		goto unlock;
+
+	vreg->started = true;
+	enable_irq(vreg->sc_irq);
+unlock:
+	mutex_unlock(&vreg->ocp_lock);
+	return ret;
+}
+
+static void qcom_labibb_stop(struct labibb_regulator *vreg)
+{
+	bool ocp_enabled;
+
+	mutex_lock(&vreg->ocp_lock);
+	vreg->started = false;
+	ocp_enabled = vreg->ocp_enabled;
+	vreg->ocp_enabled = false;
+	mutex_unlock(&vreg->ocp_lock);
+
+	/* The workers requeue themselves, so cancel_delayed_work_sync() isn't enough */
+	disable_delayed_work_sync(&vreg->sc_recovery_work);
+	disable_irq(vreg->sc_irq);
+
+	if (ocp_enabled) {
+		disable_delayed_work_sync(&vreg->ocp_recovery_work);
+		disable_irq(vreg->ocp_irq);
+		qcom_labibb_ocp_hw_disable(vreg->rdev);
+	}
+}
+
+static int qcom_labibb_setup_vreg(struct device *dev, struct regmap *regmap,
+				  const struct labibb_regulator_data *reg_data,
+				  struct labibb_regulator **result)
 {
-	struct labibb_regulator *vreg;
-	struct device *dev = &pdev->dev;
 	struct regulator_config cfg = {};
+	struct labibb_regulator *vreg;
 	struct device_node *reg_node;
-	const struct labibb_regulator_data *reg_data;
-	struct regmap *reg_regmap;
+	unsigned long ocp_irq_flags;
+	char *sc_irq_name;
 	unsigned int type;
 	int ret;
 
-	reg_regmap = dev_get_regmap(pdev->dev.parent, NULL);
-	if (!reg_regmap) {
-		dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
-		return -ENODEV;
+	/* Validate if the type of regulator is indeed what's mentioned in DT */
+	ret = regmap_read(regmap, reg_data->base + REG_PERPH_TYPE, &type);
+	if (ret < 0) {
+		dev_err(dev, "Peripheral type read failed ret=%d\n", ret);
+		return -EINVAL;
 	}
 
-	reg_data = device_get_match_data(&pdev->dev);
-	if (!reg_data)
-		return -ENODEV;
+	if (WARN_ON((type != QCOM_LAB_TYPE) && (type != QCOM_IBB_TYPE)) ||
+	    WARN_ON(type != reg_data->type))
+		return -EINVAL;
 
-	for (; reg_data->name; reg_data++) {
-		char *sc_irq_name;
-		int irq = 0;
+	vreg = devm_kzalloc_obj(dev, *vreg);
+	if (!vreg)
+		return -ENOMEM;
 
-		/* Validate if the type of regulator is indeed
-		 * what's mentioned in DT.
-		 */
-		ret = regmap_read(reg_regmap, reg_data->base + REG_PERPH_TYPE,
-				  &type);
-		if (ret < 0) {
-			dev_err(dev,
-				"Peripheral type read failed ret=%d\n",
-				ret);
-			return -EINVAL;
-		}
+	sc_irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s-short-circuit",
+				     reg_data->name);
+	if (!sc_irq_name)
+		return -ENOMEM;
 
-		if (WARN_ON((type != QCOM_LAB_TYPE) && (type != QCOM_IBB_TYPE)) ||
-		    WARN_ON(type != reg_data->type))
-			return -EINVAL;
-
-		vreg = devm_kzalloc_obj(&pdev->dev, *vreg);
-		if (!vreg)
-			return -ENOMEM;
-
-		sc_irq_name = devm_kasprintf(dev, GFP_KERNEL,
-					     "%s-short-circuit",
-					     reg_data->name);
-		if (!sc_irq_name)
-			return -ENOMEM;
-
-		reg_node = of_get_child_by_name(pdev->dev.of_node,
-						reg_data->name);
-		if (!reg_node)
-			return -EINVAL;
-
-		/* The Short Circuit interrupt is critical */
-		irq = of_irq_get_byname(reg_node, "sc-err");
-		if (irq <= 0) {
-			if (irq == 0)
-				irq = -EINVAL;
-
-			of_node_put(reg_node);
-			return dev_err_probe(vreg->dev, irq,
-					     "Short-circuit irq not found.\n");
-		}
-		vreg->sc_irq = irq;
+	reg_node = of_get_child_by_name(dev->of_node, reg_data->name);
+	if (!reg_node)
+		return -EINVAL;
 
-		/* OverCurrent Protection IRQ is optional */
-		irq = of_irq_get_byname(reg_node, "ocp");
-		vreg->ocp_irq = irq;
-		vreg->ocp_irq_count = 0;
-		of_node_put(reg_node);
-
-		vreg->regmap = reg_regmap;
-		vreg->dev = dev;
-		vreg->base = reg_data->base;
-		vreg->type = reg_data->type;
-		mutex_init(&vreg->ocp_lock);
-		INIT_DELAYED_WORK(&vreg->sc_recovery_work,
-				  qcom_labibb_sc_recovery_worker);
-
-		if (vreg->ocp_irq > 0)
-			INIT_DELAYED_WORK(&vreg->ocp_recovery_work,
-					  qcom_labibb_ocp_recovery_worker);
-
-		switch (vreg->type) {
-		case QCOM_LAB_TYPE:
-			/* LAB Limits: 200-1600mA */
-			vreg->uA_limits.uA_min  = 200000;
-			vreg->uA_limits.uA_step = 200000;
-			vreg->uA_limits.ovr_val = LAB_CURRENT_LIMIT_OVERRIDE_EN;
-			break;
-		case QCOM_IBB_TYPE:
-			/* IBB Limits: 0-1550mA */
-			vreg->uA_limits.uA_min  = 0;
-			vreg->uA_limits.uA_step = 50000;
-			vreg->uA_limits.ovr_val = 0; /* No override bit */
-			break;
-		default:
-			return -EINVAL;
-		}
+	/* The Short Circuit interrupt is critical */
+	vreg->sc_irq = of_irq_get_byname(reg_node, "sc-err");
+	/* OverCurrent Protection IRQ is optional */
+	vreg->ocp_irq = of_irq_get_byname(reg_node, "ocp");
+	of_node_put(reg_node);
 
-		memcpy(&vreg->desc, reg_data->desc, sizeof(vreg->desc));
-		vreg->desc.of_match = reg_data->name;
-		vreg->desc.name = reg_data->name;
+	if (vreg->sc_irq <= 0) {
+		ret = vreg->sc_irq ?: -EINVAL;
+		return dev_err_probe(dev, ret, "Short-circuit irq not found.\n");
+	}
 
-		cfg.dev = vreg->dev;
-		cfg.driver_data = vreg;
-		cfg.regmap = vreg->regmap;
+	vreg->regmap = regmap;
+	vreg->dev = dev;
+	vreg->base = reg_data->base;
+	vreg->type = reg_data->type;
+	mutex_init(&vreg->ocp_lock);
+	INIT_DELAYED_WORK(&vreg->sc_recovery_work,
+			  qcom_labibb_sc_recovery_worker);
+	INIT_DELAYED_WORK(&vreg->ocp_recovery_work,
+			  qcom_labibb_ocp_recovery_worker);
+
+	if (vreg->type == QCOM_LAB_TYPE) {
+		/* LAB Limits: 200-1600mA */
+		vreg->uA_limits.uA_min  = 200000;
+		vreg->uA_limits.uA_step = 200000;
+		vreg->uA_limits.ovr_val = LAB_CURRENT_LIMIT_OVERRIDE_EN;
+		/* IRQ polarities - LAB: trigger-low, IBB: trigger-high */
+		ocp_irq_flags = IRQF_TRIGGER_LOW;
+	} else {
+		/* IBB Limits: 0-1550mA */
+		vreg->uA_limits.uA_min  = 0;
+		vreg->uA_limits.uA_step = 50000;
+		vreg->uA_limits.ovr_val = 0; /* No override bit */
+		ocp_irq_flags = IRQF_TRIGGER_HIGH;
+	}
 
-		vreg->rdev = devm_regulator_register(vreg->dev, &vreg->desc,
-							&cfg);
+	memcpy(&vreg->desc, reg_data->desc, sizeof(vreg->desc));
+	vreg->desc.of_match = reg_data->name;
+	vreg->desc.name = reg_data->name;
 
-		if (IS_ERR(vreg->rdev)) {
-			dev_err(dev, "qcom_labibb: error registering %s : %d\n",
-					reg_data->name, ret);
-			return PTR_ERR(vreg->rdev);
-		}
+	/* The handlers use vreg->rdev, so qcom_labibb_start() enables them */
+	ret = request_threaded_irq(vreg->sc_irq, NULL, qcom_labibb_sc_isr,
+				   IRQF_ONESHOT | IRQF_TRIGGER_RISING |
+				   IRQF_NO_AUTOEN,
+				   sc_irq_name, vreg);
+	if (ret)
+		goto destroy_mutex;
 
-		ret = devm_request_threaded_irq(vreg->dev, vreg->sc_irq, NULL,
-						qcom_labibb_sc_isr,
-						IRQF_ONESHOT |
-						IRQF_TRIGGER_RISING,
-						sc_irq_name, vreg);
+	if (vreg->ocp_irq > 0) {
+		ret = request_threaded_irq(vreg->ocp_irq, NULL,
+					   qcom_labibb_ocp_isr,
+					   IRQF_ONESHOT | ocp_irq_flags |
+					   IRQF_NO_AUTOEN,
+					   vreg->desc.name, vreg);
 		if (ret)
-			return ret;
+			goto free_sc_irq;
+	}
 
-		ret = devm_add_action_or_reset(vreg->dev,
-					       qcom_labibb_stop_works, vreg);
-		if (ret)
-			return ret;
+	cfg.dev = dev;
+	cfg.driver_data = vreg;
+	cfg.regmap = vreg->regmap;
 
-		/*
-		 * Publish the registration only now: a concurrent
-		 * set_ocp() must not be able to request the over-current
-		 * interrupt before the action that stops its worker exists.
-		 */
-		mutex_lock(&vreg->ocp_lock);
-		vreg->registered = true;
-		ret = qcom_labibb_setup_ocp_irq(vreg);
-		mutex_unlock(&vreg->ocp_lock);
+	vreg->rdev = regulator_register(dev, &vreg->desc, &cfg);
+	if (IS_ERR(vreg->rdev)) {
+		ret = PTR_ERR(vreg->rdev);
+		dev_err(dev, "qcom_labibb: error registering %s : %d\n",
+			reg_data->name, ret);
+		goto free_ocp_irq;
+	}
+
+	ret = qcom_labibb_start(vreg);
+	if (ret)
+		goto unregister;
+
+	*result = vreg;
+	return 0;
+
+unregister:
+	regulator_unregister(vreg->rdev);
+free_ocp_irq:
+	if (vreg->ocp_irq > 0)
+		free_irq(vreg->ocp_irq, vreg);
+free_sc_irq:
+	free_irq(vreg->sc_irq, vreg);
+destroy_mutex:
+	mutex_destroy(&vreg->ocp_lock);
+	return ret;
+}
+
+static void qcom_labibb_cleanup_vreg(struct labibb_regulator *vreg)
+{
+	qcom_labibb_stop(vreg);
+	regulator_unregister(vreg->rdev);
+	if (vreg->ocp_irq > 0)
+		free_irq(vreg->ocp_irq, vreg);
+	free_irq(vreg->sc_irq, vreg);
+	mutex_destroy(&vreg->ocp_lock);
+}
+
+static int qcom_labibb_regulator_probe(struct platform_device *pdev)
+{
+	const struct labibb_regulator_data *reg_data;
+	struct device *dev = &pdev->dev;
+	struct labibb_priv *priv;
+	struct regmap *reg_regmap;
+	int i, ret;
+
+	reg_regmap = dev_get_regmap(pdev->dev.parent, NULL);
+	if (!reg_regmap) {
+		dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
+		return -ENODEV;
+	}
+
+	reg_data = device_get_match_data(&pdev->dev);
+	if (!reg_data)
+		return -ENODEV;
+
+	priv = devm_kzalloc_obj(dev, *priv);
+	if (!priv)
+		return -ENOMEM;
+
+	for (i = 0; reg_data[i].name; i++) {
+		if (i >= ARRAY_SIZE(priv->vregs)) {
+			ret = -EINVAL;
+			goto cleanup_vregs;
+		}
+
+		ret = qcom_labibb_setup_vreg(dev, reg_regmap, &reg_data[i],
+					     &priv->vregs[i]);
 		if (ret)
-			return ret;
+			goto cleanup_vregs;
 	}
 
+	priv->num_vregs = i;
+	platform_set_drvdata(pdev, priv);
+
 	return 0;
+
+cleanup_vregs:
+	while (--i >= 0)
+		qcom_labibb_cleanup_vreg(priv->vregs[i]);
+	return ret;
+}
+
+static void qcom_labibb_regulator_remove(struct platform_device *pdev)
+{
+	struct labibb_priv *priv = platform_get_drvdata(pdev);
+	int i;
+
+	for (i = priv->num_vregs - 1; i >= 0; i--)
+		qcom_labibb_cleanup_vreg(priv->vregs[i]);
 }
 
 static struct platform_driver qcom_labibb_regulator_driver = {
@@ -1055,6 +1011,7 @@ static struct platform_driver qcom_labibb_regulator_driver = {
 		.of_match_table	= qcom_labibb_match,
 	},
 	.probe = qcom_labibb_regulator_probe,
+	.remove = qcom_labibb_regulator_remove,
 };
 module_platform_driver(qcom_labibb_regulator_driver);
 
#4 lore raw
In reply to Dan Carpenter

Hi Dan,

Thanks for pulling the tree and taking a closer look. You're right that the state flags and split error cleanup make this harder to follow.

One catch with tracking ocp_hw_enabled only in setup_ocp_irq(): the OCP ISR disables the hardware interrupt and ocp_recovery_work re-enables it later, so a flag maintained only there would go out of sync.

The regmap writes can fail, and an error doesn't necessarily tell us what state the hardware was left in. I'll look at moving the failure cleanup into qcom_labibb_ocp_hw_enable() and whether IRQF_NO_AUTOEN would allow the initial IRQ setup to unwind locally, without breaking the recovery and retry paths.

If I can verify a simpler and safer error path, I'll send a focused follow-up patch rather than reworking all the state tracking at once.

Thanks,
Yaohan