[PATCH v2] media: i2c: ds90ub960: use guard(mutex) in simple register accessors
From: Laurian Iacob
Date: Mon Nov 09 2026 - 16:13:41 EST
Convert the manual mutex_lock()/mutex_unlock() of reg_lock in
ub960_read(), ub960_write(), ub960_update_bits() and ub960_read16() to
guard(mutex), and in ub960_reset() to scoped_guard(mutex).
With the lock released automatically on return, the error paths can
return directly, which drops the out_unlock label from ub960_read() and
ub960_read16().
The rxport, txport and indirect register accessors are left unchanged,
as they still rely on goto-based error handling and include/linux/cleanup.h
asks that goto and cleanup helpers not be mixed within one function.
No functional change intended.
Signed-off-by: Laurian Iacob <laurian.iacob@xxxxxxxxx>
---
Notes:
Changes in v2:
- Fix ub960_update_bits() always returning 0 when regmap_update_bits()
fails
- Don't convert the rxport/txport/indirect accessors, which still use
goto-based error handling (see include/linux/cleanup.h)
- Drop unrelated blank-line changes
- Reword subject and changelog to match the reduced scope
v1: https://lore.kernel.org/all/20261106114859.1579-1-laurian.iacob@xxxxxxxxx/
Compile-tested only (W=1); no DS90UB960/954 hardware available.
drivers/media/i2c/ds90ub960.c | 68 +++++++++++++++--------------------
1 file changed, 28 insertions(+), 40 deletions(-)
diff --git a/drivers/media/i2c/ds90ub960.c b/drivers/media/i2c/ds90ub960.c
index 7d7ba6d349fc..ee410bae1216 100644
--- a/drivers/media/i2c/ds90ub960.c
+++ b/drivers/media/i2c/ds90ub960.c
@@ -748,24 +748,20 @@ static int ub960_read(struct ub960_data *priv, u8 reg, u8 *val, int *err)
if (err && *err)
return *err;
- mutex_lock(&priv->reg_lock);
+ guard(mutex)(&priv->reg_lock);
ret = regmap_read(priv->regmap, reg, &v);
if (ret) {
dev_err(dev, "%s: cannot read register 0x%02x (%d)!\n",
__func__, reg, ret);
- goto out_unlock;
+ if (err)
+ *err = ret;
+ return ret;
}
*val = v;
-out_unlock:
- mutex_unlock(&priv->reg_lock);
-
- if (ret && err)
- *err = ret;
-
- return ret;
+ return 0;
}
static int ub960_write(struct ub960_data *priv, u8 reg, u8 val, int *err)
@@ -776,19 +772,18 @@ static int ub960_write(struct ub960_data *priv, u8 reg, u8 val, int *err)
if (err && *err)
return *err;
- mutex_lock(&priv->reg_lock);
+ guard(mutex)(&priv->reg_lock);
ret = regmap_write(priv->regmap, reg, val);
- if (ret)
+ if (ret) {
dev_err(dev, "%s: cannot write register 0x%02x (%d)!\n",
__func__, reg, ret);
+ if (err)
+ *err = ret;
+ return ret;
+ }
- mutex_unlock(&priv->reg_lock);
-
- if (ret && err)
- *err = ret;
-
- return ret;
+ return 0;
}
static int ub960_update_bits(struct ub960_data *priv, u8 reg, u8 mask, u8 val,
@@ -800,19 +795,18 @@ static int ub960_update_bits(struct ub960_data *priv, u8 reg, u8 mask, u8 val,
if (err && *err)
return *err;
- mutex_lock(&priv->reg_lock);
+ guard(mutex)(&priv->reg_lock);
ret = regmap_update_bits(priv->regmap, reg, mask, val);
- if (ret)
+ if (ret) {
dev_err(dev, "%s: cannot update register 0x%02x (%d)!\n",
__func__, reg, ret);
+ if (err)
+ *err = ret;
+ return ret;
+ }
- mutex_unlock(&priv->reg_lock);
-
- if (ret && err)
- *err = ret;
-
- return ret;
+ return 0;
}
static int ub960_read16(struct ub960_data *priv, u8 reg, u16 *val, int *err)
@@ -824,24 +818,20 @@ static int ub960_read16(struct ub960_data *priv, u8 reg, u16 *val, int *err)
if (err && *err)
return *err;
- mutex_lock(&priv->reg_lock);
+ guard(mutex)(&priv->reg_lock);
ret = regmap_bulk_read(priv->regmap, reg, &__v, sizeof(__v));
if (ret) {
dev_err(dev, "%s: cannot read register 0x%02x (%d)!\n",
__func__, reg, ret);
- goto out_unlock;
+ if (err)
+ *err = ret;
+ return ret;
}
*val = be16_to_cpu(__v);
-out_unlock:
- mutex_unlock(&priv->reg_lock);
-
- if (ret && err)
- *err = ret;
-
- return ret;
+ return 0;
}
static int ub960_rxport_select(struct ub960_data *priv, u8 nport)
@@ -1272,12 +1262,10 @@ static int ub960_reset(struct ub960_data *priv, bool reset_regs)
if (ret)
return ret;
- mutex_lock(&priv->reg_lock);
-
- ret = regmap_read_poll_timeout(priv->regmap, UB960_SR_RESET, v,
- (v & bit) == 0, 2000, 100000);
-
- mutex_unlock(&priv->reg_lock);
+ scoped_guard(mutex, &priv->reg_lock) {
+ ret = regmap_read_poll_timeout(priv->regmap, UB960_SR_RESET, v,
+ (v & bit) == 0, 2000, 100000);
+ }
if (ret)
dev_err(dev, "reset failed: %d\n", ret);
--
2.43.0