Skip to content

Commit 83c4221

Browse files
Wolfram SangWolfram Sang
authored andcommitted
i2c: core: use I2C locking behaviour also for SMBUS
If I2C transfers are executed in atomic contexts, trylock is used instead of lock. This behaviour was missing for SMBUS, although a lot of transfers are of SMBUS type, either emulated or direct. So, factor out the locking routine into a helper and use it for I2C and SMBUS. Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com> Reviewed-by Andy Shevchenko <andriy.shevchenko@linux.intel.com> Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
1 parent bae1d3a commit 83c4221

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

drivers/i2c/i2c-core-base.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,14 +1946,9 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
19461946
* one (discarding status on the second message) or errno
19471947
* (discarding status on the first one).
19481948
*/
1949-
if (i2c_in_atomic_xfer_mode()) {
1950-
ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
1951-
if (!ret)
1952-
/* I2C activity is ongoing. */
1953-
return -EAGAIN;
1954-
} else {
1955-
i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
1956-
}
1949+
ret = __i2c_lock_bus_helper(adap);
1950+
if (ret)
1951+
return ret;
19571952

19581953
ret = __i2c_transfer(adap, msgs, num);
19591954
i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);

drivers/i2c/i2c-core-smbus.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <linux/i2c-smbus.h>
2121
#include <linux/slab.h>
2222

23+
#include "i2c-core.h"
24+
2325
#define CREATE_TRACE_POINTS
2426
#include <trace/events/smbus.h>
2527

@@ -530,7 +532,10 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
530532
{
531533
s32 res;
532534

533-
i2c_lock_bus(adapter, I2C_LOCK_SEGMENT);
535+
res = __i2c_lock_bus_helper(adapter);
536+
if (res)
537+
return res;
538+
534539
res = __i2c_smbus_xfer(adapter, addr, flags, read_write,
535540
command, protocol, data);
536541
i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);

drivers/i2c/i2c-core.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ static inline bool i2c_in_atomic_xfer_mode(void)
3939
return system_state > SYSTEM_RUNNING && irqs_disabled();
4040
}
4141

42+
static inline int __i2c_lock_bus_helper(struct i2c_adapter *adap)
43+
{
44+
int ret = 0;
45+
46+
if (i2c_in_atomic_xfer_mode())
47+
ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT) ? 0 : -EAGAIN;
48+
else
49+
i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
50+
51+
return ret;
52+
}
53+
4254
#ifdef CONFIG_ACPI
4355
const struct acpi_device_id *
4456
i2c_acpi_match_device(const struct acpi_device_id *matches,

0 commit comments

Comments
 (0)