[PATCH v6 4/4] media: i2c: ds90ub953: use guard() to simplify code

Guoniu Zhou
2026-04-24T01:40:15Z

From: Guoniu Zhou <guoniu.zhou@nxp.com>

Use guard() to simplify mutex locking. No functional change.

Reviewed-by: Frank Li <Frank.Li@nxp.com>
Signed-off-by: Guoniu Zhou <guoniu.zhou@nxp.com>
---
Changes in v6:
- Replace label "err" with "out"
---
drivers/media/i2c/ds90ub953.c | 34 +++++++++++++---------------------
1 file changed, 13 insertions(+), 21 deletions(-)

diff --git a/drivers/media/i2c/ds90ub953.c b/drivers/media/i2c/ds90ub953.c
index c37d9cbe88b9..a351d366cc3e 100644
--- a/drivers/media/i2c/ds90ub953.c
+++ b/drivers/media/i2c/ds90ub953.c
@@ -114,20 +114,18 @@ static int ub953_read(struct ub953_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(&priv->client->dev, "Cannot read register 0x%02x: %d\n",
reg, ret);
- goto out_unlock;
+ goto out;
}

*val = v;

-out_unlock:
- mutex_unlock(&priv->reg_lock);
-
+out:
if (ret && err)
*err = ret;

@@ -141,15 +139,13 @@ static int ub953_write(struct ub953_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)
dev_err(&priv->client->dev,
"Cannot write register 0x%02x: %d\n", reg, ret);

- mutex_unlock(&priv->reg_lock);
-
if (ret && err)
*err = ret;

@@ -186,18 +182,18 @@ static int ub953_read_ind(struct ub953_data *priv, u8 block, u8 reg, u8 *val,
if (err && *err)
return *err;

- mutex_lock(&priv->reg_lock);
+ guard(mutex)(&priv->reg_lock);

ret = ub953_select_ind_reg_block(priv, block);
if (ret)
- goto out_unlock;
+ goto out;

ret = regmap_write(priv->regmap, UB953_REG_IND_ACC_ADDR, reg);
if (ret) {
dev_err(&priv->client->dev,
"Write to IND_ACC_ADDR failed when reading %u:0x%02x: %d\n",
block, reg, ret);
- goto out_unlock;
+ goto out;
}

ret = regmap_read(priv->regmap, UB953_REG_IND_ACC_DATA, &v);
@@ -205,14 +201,12 @@ static int ub953_read_ind(struct ub953_data *priv, u8 block, u8 reg, u8 *val,
dev_err(&priv->client->dev,
"Write to IND_ACC_DATA failed when reading %u:0x%02x: %d\n",
block, reg, ret);
- goto out_unlock;
+ goto out;
}

*val = v;

-out_unlock:
- mutex_unlock(&priv->reg_lock);
-
+out:
if (ret && err)
*err = ret;

@@ -228,18 +222,18 @@ static int ub953_write_ind(struct ub953_data *priv, u8 block, u8 reg, u8 val,
if (err && *err)
return *err;

- mutex_lock(&priv->reg_lock);
+ guard(mutex)(&priv->reg_lock);

ret = ub953_select_ind_reg_block(priv, block);
if (ret)
- goto out_unlock;
+ goto out;

ret = regmap_write(priv->regmap, UB953_REG_IND_ACC_ADDR, reg);
if (ret) {
dev_err(&priv->client->dev,
"Write to IND_ACC_ADDR failed when writing %u:0x%02x: %d\n",
block, reg, ret);
- goto out_unlock;
+ goto out;
}

ret = regmap_write(priv->regmap, UB953_REG_IND_ACC_DATA, val);
@@ -249,9 +243,7 @@ static int ub953_write_ind(struct ub953_data *priv, u8 block, u8 reg, u8 val,
block, reg, ret);
}

-out_unlock:
- mutex_unlock(&priv->reg_lock);
-
+out:
if (ret && err)
*err = ret;

--
2.34.1