Skip to content

Commit fe61e07

Browse files
author
Jean Delvare
committed
i2c: Move adapter locking helpers to i2c-core
Uninline i2c adapter locking helper functions, move them to i2c-core, and use them in i2c-core itself. The functions are still exported for external users. This makes future updates to the locking model (which will be needed for multiplexing support) possible and transparent. Signed-off-by: Jean Delvare <khali@linux-fr.org> Cc: Michael Lawnick <ml.lawnick@gmx.de>
1 parent d44f19d commit fe61e07

2 files changed

Lines changed: 37 additions & 22 deletions

File tree

‎drivers/i2c/i2c-core.c‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,35 @@ static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
429429
__i2c_check_addr_busy);
430430
}
431431

432+
/**
433+
* i2c_lock_adapter - Get exclusive access to an I2C bus segment
434+
* @adapter: Target I2C bus segment
435+
*/
436+
void i2c_lock_adapter(struct i2c_adapter *adapter)
437+
{
438+
rt_mutex_lock(&adapter->bus_lock);
439+
}
440+
EXPORT_SYMBOL_GPL(i2c_lock_adapter);
441+
442+
/**
443+
* i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
444+
* @adapter: Target I2C bus segment
445+
*/
446+
static int i2c_trylock_adapter(struct i2c_adapter *adapter)
447+
{
448+
return rt_mutex_trylock(&adapter->bus_lock);
449+
}
450+
451+
/**
452+
* i2c_unlock_adapter - Release exclusive access to an I2C bus segment
453+
* @adapter: Target I2C bus segment
454+
*/
455+
void i2c_unlock_adapter(struct i2c_adapter *adapter)
456+
{
457+
rt_mutex_unlock(&adapter->bus_lock);
458+
}
459+
EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
460+
432461
/**
433462
* i2c_new_device - instantiate an i2c device
434463
* @adap: the adapter managing the device
@@ -1238,12 +1267,12 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
12381267
#endif
12391268

12401269
if (in_atomic() || irqs_disabled()) {
1241-
ret = rt_mutex_trylock(&adap->bus_lock);
1270+
ret = i2c_trylock_adapter(adap);
12421271
if (!ret)
12431272
/* I2C activity is ongoing. */
12441273
return -EAGAIN;
12451274
} else {
1246-
rt_mutex_lock(&adap->bus_lock);
1275+
i2c_lock_adapter(adap);
12471276
}
12481277

12491278
/* Retry automatically on arbitration loss */
@@ -1255,7 +1284,7 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
12551284
if (time_after(jiffies, orig_jiffies + adap->timeout))
12561285
break;
12571286
}
1258-
rt_mutex_unlock(&adap->bus_lock);
1287+
i2c_unlock_adapter(adap);
12591288

12601289
return ret;
12611290
} else {
@@ -2013,7 +2042,7 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
20132042
flags &= I2C_M_TEN | I2C_CLIENT_PEC;
20142043

20152044
if (adapter->algo->smbus_xfer) {
2016-
rt_mutex_lock(&adapter->bus_lock);
2045+
i2c_lock_adapter(adapter);
20172046

20182047
/* Retry automatically on arbitration loss */
20192048
orig_jiffies = jiffies;
@@ -2027,7 +2056,7 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
20272056
orig_jiffies + adapter->timeout))
20282057
break;
20292058
}
2030-
rt_mutex_unlock(&adapter->bus_lock);
2059+
i2c_unlock_adapter(adapter);
20312060
} else
20322061
res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
20332062
command, protocol, data);

‎include/linux/i2c.h‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -382,23 +382,9 @@ static inline void i2c_set_adapdata(struct i2c_adapter *dev, void *data)
382382
dev_set_drvdata(&dev->dev, data);
383383
}
384384

385-
/**
386-
* i2c_lock_adapter - Prevent access to an I2C bus segment
387-
* @adapter: Target I2C bus segment
388-
*/
389-
static inline void i2c_lock_adapter(struct i2c_adapter *adapter)
390-
{
391-
rt_mutex_lock(&adapter->bus_lock);
392-
}
393-
394-
/**
395-
* i2c_unlock_adapter - Reauthorize access to an I2C bus segment
396-
* @adapter: Target I2C bus segment
397-
*/
398-
static inline void i2c_unlock_adapter(struct i2c_adapter *adapter)
399-
{
400-
rt_mutex_unlock(&adapter->bus_lock);
401-
}
385+
/* Adapter locking functions, exported for shared pin cases */
386+
void i2c_lock_adapter(struct i2c_adapter *);
387+
void i2c_unlock_adapter(struct i2c_adapter *);
402388

403389
/*flags for the client struct: */
404390
#define I2C_CLIENT_PEC 0x04 /* Use Packet Error Checking */

0 commit comments

Comments
 (0)