Skip to content

Commit 46fe826

Browse files
jonasjelonekAndi Shyti
authored andcommitted
i2c: rtl9300: use scoped guard instead of explicit lock/unlock
Use the scoped guard infrastructure which unlocks a mutex automatically when the guard goes out of scope, instead of explicit lock and unlock. This simplifies the code and control flow in rtl9300_i2c_smbus_xfer and removes the need of using goto in error cases to unlock before returning. Signed-off-by: Jonas Jelonek <jelonek.jonas@gmail.com> Reviewed-by: Chris Packham <chris.packham@alliedtelesis.co.nz> Tested-by: Chris Packham <chris.packham@alliedtelesis.co.nz> # On RTL9302C based board Tested-by: Markus Stockhausen <markus.stockhausen@gmx.de> Signed-off-by: Andi Shyti <andi.shyti@kernel.org> Link: https://lore.kernel.org/r/20250927101931.71575-8-jelonek.jonas@gmail.com
1 parent 059374a commit 46fe826

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

drivers/i2c/busses/i2c-rtl9300.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ struct rtl9300_i2c {
7272
struct mutex lock;
7373
};
7474

75+
DEFINE_GUARD(rtl9300_i2c, struct rtl9300_i2c *, mutex_lock(&_T->lock), mutex_unlock(&_T->lock))
76+
7577
enum rtl9300_i2c_xfer_type {
7678
RTL9300_I2C_XFER_BYTE,
7779
RTL9300_I2C_XFER_WORD,
@@ -283,11 +285,11 @@ static int rtl9300_i2c_smbus_xfer(struct i2c_adapter *adap, u16 addr, unsigned s
283285
if (addr > 0x7f)
284286
return -EINVAL;
285287

286-
mutex_lock(&i2c->lock);
288+
guard(rtl9300_i2c)(i2c);
287289

288290
ret = rtl9300_i2c_config_chan(i2c, chan);
289291
if (ret)
290-
goto out_unlock;
292+
return ret;
291293

292294
xfer.dev_addr = addr & 0x7f;
293295
xfer.write = (read_write == I2C_SMBUS_WRITE);
@@ -324,20 +326,14 @@ static int rtl9300_i2c_smbus_xfer(struct i2c_adapter *adap, u16 addr, unsigned s
324326
break;
325327
default:
326328
dev_err(&adap->dev, "Unsupported transaction %d\n", size);
327-
ret = -EOPNOTSUPP;
328-
goto out_unlock;
329+
return -EOPNOTSUPP;
329330
}
330331

331332
ret = rtl9300_i2c_prepare_xfer(i2c, &xfer);
332333
if (ret)
333-
goto out_unlock;
334-
335-
ret = rtl9300_i2c_do_xfer(i2c, &xfer);
336-
337-
out_unlock:
338-
mutex_unlock(&i2c->lock);
334+
return ret;
339335

340-
return ret;
336+
return rtl9300_i2c_do_xfer(i2c, &xfer);
341337
}
342338

343339
static u32 rtl9300_i2c_func(struct i2c_adapter *a)

0 commit comments

Comments
 (0)