Skip to contentSkip to conversation

ACPI: pmic: Replace mutex_lock/unlock() with guard()/scoped_guard()

PATCH Superseded Maxwell Doose 3 participants 4 messages Apr 29

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 ACPI: pmic: Replace mutex_lock/unlock() with guard()/scoped_guard()

Replace mutex_lock() and unlock() macros with the newer guard() and scoped_guard() macros. This will help modernize and clean the code.

In intel_soc_pmic_exec_mipi_pmic_seq_element(): While at it, remove now redundant "ret" variable.

---
v2:
- Refactored control flow in certain functions as suggested by Andy.
- Indent code in scoped_guard block as suggested by Andy.
- Fix parameter list alignment issues that I found while making this v2.

v3:
- Fixed guard(mutex)() and scoped_guard() syntax.
- Added some logical and style changes per Andy's request. The requested changes and my personal thoughts can be found at
https://lore.kernel.org/linux-acpi/[email protected]/

v4:
- Added else keyword to if statement in intel_soc_pmic_exec_mipi_pmic_seq_element() per Andy's request.

 drivers/acpi/pmic/intel_pmic.c | 60 ++++++++++++++--------------------
 1 file changed, 25 insertions(+), 35 deletions(-)
drivers/acpi/pmic/intel_pmic.c +25 -35
index 134e9ca8eaa2..594c07211a8b 100644
@@ -67,14 +67,12 @@ static acpi_status intel_pmic_power_handler(u32 function,
 	if (result == -ENOENT)
 		return AE_BAD_PARAMETER;
 
-	mutex_lock(&opregion->lock);
+	guard(mutex)(&opregion->lock);
 
 	result = function == ACPI_READ ?
 		d->get_power(regmap, reg, bit, value64) :
 		d->update_power(regmap, reg, bit, *value64 == 1);
 
-	mutex_unlock(&opregion->lock);
-
 	return result ? AE_ERROR : AE_OK;
 }
 
@@ -182,19 +180,16 @@ static acpi_status intel_pmic_thermal_handler(u32 function,
 	if (result == -ENOENT)
 		return AE_BAD_PARAMETER;
 
-	mutex_lock(&opregion->lock);
-
-	if (pmic_thermal_is_temp(address))
-		result = pmic_thermal_temp(opregion, reg, function, value64);
-	else if (pmic_thermal_is_aux(address))
-		result = pmic_thermal_aux(opregion, reg, function, value64);
-	else if (pmic_thermal_is_pen(address))
-		result = pmic_thermal_pen(opregion, reg, bit,
-						function, value64);
-	else
-		result = -EINVAL;
-
-	mutex_unlock(&opregion->lock);
+	scoped_guard(mutex, &opregion->lock) {
+		if (pmic_thermal_is_temp(address))
+			result = pmic_thermal_temp(opregion, reg, function, value64);
+		else if (pmic_thermal_is_aux(address))
+			result = pmic_thermal_aux(opregion, reg, function, value64);
+		else if (pmic_thermal_is_pen(address))
+			result = pmic_thermal_pen(opregion, reg, bit, function, value64);
+		else
+			result = -EINVAL;
+	}
 
 	if (result < 0) {
 		if (result == -EINVAL)
@@ -345,7 +340,6 @@ int intel_soc_pmic_exec_mipi_pmic_seq_element(u16 i2c_address, u32 reg_address,
 					      u32 value, u32 mask)
 {
 	const struct intel_pmic_opregion_data *d;
-	int ret;
 
 	if (!intel_pmic_opregion) {
 		pr_warn("%s: No PMIC registered\n", __func__);
@@ -354,30 +348,26 @@ int intel_soc_pmic_exec_mipi_pmic_seq_element(u16 i2c_address, u32 reg_address,
 
 	d = intel_pmic_opregion->data;
 
-	mutex_lock(&intel_pmic_opregion->lock);
+	guard(mutex)(&intel_pmic_opregion->lock);
 
 	if (d->exec_mipi_pmic_seq_element) {
-		ret = d->exec_mipi_pmic_seq_element(intel_pmic_opregion->regmap,
-						    i2c_address, reg_address,
-						    value, mask);
+		return d->exec_mipi_pmic_seq_element(intel_pmic_opregion->regmap,
+						     i2c_address, reg_address,
+						     value, mask);
 	} else if (d->pmic_i2c_address) {
-		if (i2c_address == d->pmic_i2c_address) {
-			ret = regmap_update_bits(intel_pmic_opregion->regmap,
-						 reg_address, mask, value);
-		} else {
+		if (i2c_address != d->pmic_i2c_address) {
 			pr_err("%s: Unexpected i2c-addr: 0x%02x (reg-addr 0x%x value 0x%x mask 0x%x)\n",
-			       __func__, i2c_address, reg_address, value, mask);
-			ret = -ENXIO;
+				__func__, i2c_address, reg_address, value, mask);
+			return -ENXIO;
 		}
-	} else {
-		pr_warn("%s: Not implemented\n", __func__);
-		pr_warn("%s: i2c-addr: 0x%x reg-addr 0x%x value 0x%x mask 0x%x\n",
-			__func__, i2c_address, reg_address, value, mask);
-		ret = -EOPNOTSUPP;
-	}
 
-	mutex_unlock(&intel_pmic_opregion->lock);
+		return regmap_update_bits(intel_pmic_opregion->regmap,
+					  reg_address, mask, value);
+	}
 
-	return ret;
+	pr_warn("%s: Not implemented\n", __func__);
+	pr_warn("%s: i2c-addr: 0x%x reg-addr 0x%x value 0x%x mask 0x%x\n",
+		__func__, i2c_address, reg_address, value, mask);
+	return -EOPNOTSUPP;
 }
 EXPORT_SYMBOL_GPL(intel_soc_pmic_exec_mipi_pmic_seq_element);
Signature · 1 line
2.53.0
#1 lore raw

On Tue, Apr 28, 2026 at 08:24:32PM -0500, Maxwell Doose wrote:

...

Quoted 3 lines

v4:
- Added else keyword to if statement in intel_soc_pmic_exec_mipi_pmic_seq_element() per Andy's request.

Nope, sorry if I was not clear. The idea is to drop 'else'. Also make the patch less invasive.

...

drivers/acpi/pmic/intel_pmic.c int intel_soc_pmic_exec_mipi_pmic_seq_element(u16 i2c_address, u32 reg_address, · line 370

Quoted 3 lines
drivers/acpi/pmic/intel_pmic.c +1 -1
-			       __func__, i2c_address, reg_address, value, mask);
 
+				__func__, i2c_address, reg_address, value, mask);

Still a stray change.

...

It should be

	int ret; // also left untouched, drop that from commit message as well

        if (d->exec_mipi_pmic_seq_element) {
		// the below if-else-if just should be left untouched.
		if (i2c_address == d->pmic_i2c_address) {
			...
		} else {
			...
		}

		return ret;
	}

	if (d->pmic_i2c_address) {
	}

	...warnings...
	return -EOPNOTSUPP;

We may get a second patch for deeper refactoring later on.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 12 lines · Andy Shevchenko

On Tue, Apr 28, 2026 at 08:24:32PM -0500, Maxwell Doose wrote:

...

Quoted 3 lines

v4:
- Added else keyword to if statement in intel_soc_pmic_exec_mipi_pmic_seq_element() per Andy's request.

Nope, sorry if I was not clear. The idea is to drop 'else'. Also make the patch less invasive.

Ah, I see. Just a misunderstanding, I'll make sure to fix that once I'm home.

Quoted 7 lines

...

drivers/acpi/pmic/intel_pmic.c int intel_soc_pmic_exec_mipi_pmic_seq_element(u16 i2c_address, u32 reg_address, · line 370

Quoted 3 lines
drivers/acpi/pmic/intel_pmic.c +1 -1
-                            __func__, i2c_address, reg_address, value, mask);
 
+                             __func__, i2c_address, reg_address, value, mask);

Still a stray change.

Yea, I'm not sure what's going on with this one. I think in my editor it looked like there was some misalignment from before but I'll see about dropping that.

Quoted 14 lines

...

It should be

        int ret; // also left untouched, drop that from commit message as well

        if (d->exec_mipi_pmic_seq_element) {
                // the below if-else-if just should be left untouched.
                if (i2c_address == d->pmic_i2c_address) {
                        ...
                } else {
                        ...
                }
Quoted 9 lines
                return ret;
        }

        if (d->pmic_i2c_address) {
        }

        ...warnings...
        return -EOPNOTSUPP;

Alright, thanks for clarifying this. I'll make sure this gets in for the v4.

best regards,
maxwell

Quoted 7 lines

We may get a second patch for deeper refactoring later on.

--
With Best Regards,
Andy Shevchenko

#3 lore raw
Quoted 3 lines · Maxwell Doose
Quoted 1 line · Andy Shevchenko

On Tue, Apr 28, 2026 at 08:24:32PM -0500, Maxwell Doose wrote:

...

Quoted 5 lines
Quoted 5 lines

It should be

int ret; // also left untouched, drop that from commit message as well

if (d->exec_mipi_pmic_seq_element) {

I realised that the body below should be...

Quoted 11 lines
Quoted 11 lines
                // the below if-else-if just should be left untouched.
                if (i2c_address == d->pmic_i2c_address) {
                        ...
                } else {
                        ...
                }

                return ret;
        }

        if (d->pmic_i2c_address) {

...part of this conditional. But I hope you got the idea.

Quoted 6 lines
Quoted 4 lines
        }

        ...warnings...
        return -EOPNOTSUPP;

Alright, thanks for clarifying this. I'll make sure this gets in for the v4.

Signature · 2 lines
With Best Regards,
Andy Shevchenko