[PATCH v6 10/12] i2c: rtl9300: use scoped guard instead of explicit lock/unlock

Jonas Jelonek posted 12 patches 1 month, 1 week ago
There is a newer version of this series
[PATCH v6 10/12] i2c: rtl9300: use scoped guard instead of explicit lock/unlock
Posted by Jonas Jelonek 1 month, 1 week ago
Use the scoped guard infrastructure which unlocks a mutex automatically
when the guard goes out of scope, instead of explicity lock/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>
---
 drivers/i2c/busses/i2c-rtl9300.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/drivers/i2c/busses/i2c-rtl9300.c b/drivers/i2c/busses/i2c-rtl9300.c
index fb3ebbd46a18..c67463228604 100644
--- a/drivers/i2c/busses/i2c-rtl9300.c
+++ b/drivers/i2c/busses/i2c-rtl9300.c
@@ -72,6 +72,8 @@ struct rtl9300_i2c {
 	struct mutex lock;
 };
 
+DEFINE_GUARD(rtl9300_i2c, struct rtl9300_i2c *, mutex_lock(&_T->lock), mutex_unlock(&_T->lock))
+
 enum rtl9300_i2c_xfer_type {
 	RTL9300_I2C_XFER_BYTE,
 	RTL9300_I2C_XFER_WORD,
@@ -283,11 +285,11 @@ static int rtl9300_i2c_smbus_xfer(struct i2c_adapter *adap, u16 addr, unsigned s
 	if (addr > 0x7f)
 		return -EINVAL;
 
-	mutex_lock(&i2c->lock);
+	guard(rtl9300_i2c)(i2c);
 
 	ret = rtl9300_i2c_config_chan(i2c, chan);
 	if (ret)
-		goto out_unlock;
+		return ret;
 
 	xfer.dev_addr = addr & 0x7f;
 	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
 		break;
 	default:
 		dev_err(&adap->dev, "Unsupported transaction %d\n", size);
-		ret = -EOPNOTSUPP;
-		goto out_unlock;
+		return -EOPNOTSUPP;
 	}
 
 	ret = rtl9300_i2c_prepare_xfer(i2c, &xfer);
 	if (ret)
-		goto out_unlock;
-
-	ret = rtl9300_i2c_do_xfer(i2c, &xfer);
-
-out_unlock:
-	mutex_unlock(&i2c->lock);
+		return ret;
 
-	return ret;
+	return rtl9300_i2c_do_xfer(i2c, &xfer);
 }
 
 static u32 rtl9300_i2c_func(struct i2c_adapter *a)
-- 
2.48.1
Re: [PATCH v6 10/12] i2c: rtl9300: use scoped guard instead of explicit lock/unlock
Posted by Markus Elfring 1 month, 1 week ago
…
> when the guard goes out of scope, instead of explicity lock/unlock. This
…

                                               explicit?              Would such a word be nicer in the next line?

Regards,
Markus
Re: [PATCH v6 10/12] i2c: rtl9300: use scoped guard instead of explicit lock/unlock
Posted by Jonas Jelonek 1 month, 1 week ago

On 24.08.25 16:12, Markus Elfring wrote:
> …
>> when the guard goes out of scope, instead of explicity lock/unlock. This
> …
>
> explicit? Would such a word be nicer in the next line?

I think I wanted to write 'explicitly' but not sure which variant is better.
Having it in the next line would probably be better, I'll fix this.

> Regards,
> Markus

Regards,
Jonas