]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
i2c: imx: use guard to take spinlock
authorTroy Mitchell <troymitchell988@gmail.com>
Sat, 31 May 2025 06:57:26 +0000 (14:57 +0800)
committerAndi Shyti <andi.shyti@kernel.org>
Wed, 23 Jul 2025 22:37:59 +0000 (00:37 +0200)
Use guard to automatically release the lock after going out of scope
instead of calling it manually.

i2c_imx_slave_handle() can safely be entered with the lock held.

Refactored the i2c_imx_isr function so that i2c_imx_master_isr
does not participate in the guard scope

So Using scoped_guard simplifies the control flow
by ensuring consistent and automatic unlock,
which improves readability without affecting correctness.

Co-developed-by: Yongchao Jia <jyc0019@gmail.com>
Signed-off-by: Yongchao Jia <jyc0019@gmail.com>
Signed-off-by: Troy Mitchell <troymitchell988@gmail.com>
Reviewed-by: Frank Li <Frank.Li@nxp.com>
Acked-by: Oleksij Rempel <o.rempel@pengutronix.de>
Signed-off-by: Andi Shyti <andi.shyti@kernel.org>
Link: https://lore.kernel.org/r/20250531-i2c-imx-update-v4-1-bfad0c8fd45c@gmail.com
drivers/i2c/busses/i2c-imx.c

index 205cc132fdecba870cc3180c93203b0f9db7c8d4..60f5c790ad7c9f13b4e51af5090d869bace933a4 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <linux/acpi.h>
 #include <linux/clk.h>
+#include <linux/cleanup.h>
 #include <linux/completion.h>
 #include <linux/delay.h>
 #include <linux/dma-mapping.h>
@@ -891,13 +892,13 @@ static enum hrtimer_restart i2c_imx_slave_timeout(struct hrtimer *t)
        struct imx_i2c_struct *i2c_imx = container_of(t, struct imx_i2c_struct,
                                                      slave_timer);
        unsigned int ctl, status;
-       unsigned long flags;
 
-       spin_lock_irqsave(&i2c_imx->slave_lock, flags);
+       guard(spinlock_irqsave)(&i2c_imx->slave_lock);
+
        status = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
        ctl = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
        i2c_imx_slave_handle(i2c_imx, status, ctl);
-       spin_unlock_irqrestore(&i2c_imx->slave_lock, flags);
+
        return HRTIMER_NORESTART;
 }
 
@@ -1126,32 +1127,26 @@ static irqreturn_t i2c_imx_isr(int irq, void *dev_id)
 {
        struct imx_i2c_struct *i2c_imx = dev_id;
        unsigned int ctl, status;
-       unsigned long flags;
 
-       spin_lock_irqsave(&i2c_imx->slave_lock, flags);
-       status = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
-       ctl = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
+       scoped_guard(spinlock_irqsave, &i2c_imx->slave_lock) {
+               status = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
+               ctl = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
+
+               if (!(status & I2SR_IIF))
+                       return IRQ_NONE;
 
-       if (status & I2SR_IIF) {
                i2c_imx_clear_irq(i2c_imx, I2SR_IIF);
+
                if (i2c_imx->slave) {
-                       if (!(ctl & I2CR_MSTA)) {
-                               irqreturn_t ret;
-
-                               ret = i2c_imx_slave_handle(i2c_imx,
-                                                          status, ctl);
-                               spin_unlock_irqrestore(&i2c_imx->slave_lock,
-                                                      flags);
-                               return ret;
-                       }
+                       if (!(ctl & I2CR_MSTA))
+                               return i2c_imx_slave_handle(i2c_imx,
+                                                           status, ctl);
+
                        i2c_imx_slave_finish_op(i2c_imx);
                }
-               spin_unlock_irqrestore(&i2c_imx->slave_lock, flags);
-               return i2c_imx_master_isr(i2c_imx, status);
        }
-       spin_unlock_irqrestore(&i2c_imx->slave_lock, flags);
 
-       return IRQ_NONE;
+       return i2c_imx_master_isr(i2c_imx, status);
 }
 
 static int i2c_imx_dma_write(struct imx_i2c_struct *i2c_imx,