Skip to content

Commit 07e0e8e

Browse files
TroyMitchell911Andi Shyti
authored andcommitted
i2c: imx: use guard to take spinlock
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 <[email protected]> Signed-off-by: Yongchao Jia <[email protected]> Signed-off-by: Troy Mitchell <[email protected]> Reviewed-by: Frank Li <[email protected]> Acked-by: Oleksij Rempel <[email protected]> Signed-off-by: Andi Shyti <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent be3b425 commit 07e0e8e

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

drivers/i2c/busses/i2c-imx.c

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <linux/acpi.h>
2525
#include <linux/clk.h>
26+
#include <linux/cleanup.h>
2627
#include <linux/completion.h>
2728
#include <linux/delay.h>
2829
#include <linux/dma-mapping.h>
@@ -891,13 +892,13 @@ static enum hrtimer_restart i2c_imx_slave_timeout(struct hrtimer *t)
891892
struct imx_i2c_struct *i2c_imx = container_of(t, struct imx_i2c_struct,
892893
slave_timer);
893894
unsigned int ctl, status;
894-
unsigned long flags;
895895

896-
spin_lock_irqsave(&i2c_imx->slave_lock, flags);
896+
guard(spinlock_irqsave)(&i2c_imx->slave_lock);
897+
897898
status = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
898899
ctl = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
899900
i2c_imx_slave_handle(i2c_imx, status, ctl);
900-
spin_unlock_irqrestore(&i2c_imx->slave_lock, flags);
901+
901902
return HRTIMER_NORESTART;
902903
}
903904

@@ -1126,32 +1127,26 @@ static irqreturn_t i2c_imx_isr(int irq, void *dev_id)
11261127
{
11271128
struct imx_i2c_struct *i2c_imx = dev_id;
11281129
unsigned int ctl, status;
1129-
unsigned long flags;
11301130

1131-
spin_lock_irqsave(&i2c_imx->slave_lock, flags);
1132-
status = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
1133-
ctl = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
1131+
scoped_guard(spinlock_irqsave, &i2c_imx->slave_lock) {
1132+
status = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
1133+
ctl = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
1134+
1135+
if (!(status & I2SR_IIF))
1136+
return IRQ_NONE;
11341137

1135-
if (status & I2SR_IIF) {
11361138
i2c_imx_clear_irq(i2c_imx, I2SR_IIF);
1139+
11371140
if (i2c_imx->slave) {
1138-
if (!(ctl & I2CR_MSTA)) {
1139-
irqreturn_t ret;
1140-
1141-
ret = i2c_imx_slave_handle(i2c_imx,
1142-
status, ctl);
1143-
spin_unlock_irqrestore(&i2c_imx->slave_lock,
1144-
flags);
1145-
return ret;
1146-
}
1141+
if (!(ctl & I2CR_MSTA))
1142+
return i2c_imx_slave_handle(i2c_imx,
1143+
status, ctl);
1144+
11471145
i2c_imx_slave_finish_op(i2c_imx);
11481146
}
1149-
spin_unlock_irqrestore(&i2c_imx->slave_lock, flags);
1150-
return i2c_imx_master_isr(i2c_imx, status);
11511147
}
1152-
spin_unlock_irqrestore(&i2c_imx->slave_lock, flags);
11531148

1154-
return IRQ_NONE;
1149+
return i2c_imx_master_isr(i2c_imx, status);
11551150
}
11561151

11571152
static int i2c_imx_dma_write(struct imx_i2c_struct *i2c_imx,

0 commit comments

Comments
 (0)