History

i2c: core: introduce callbacks for atomic transfers

We had the request to access devices very late when interrupts are not available anymore multiple times now. Mostly to prepare shutdown or reboot. Allow adapters to specify a specific callback for this case. Note that we fall back to the generic {master|smbus}_xfer callback if this new atomic one is not present. This is intentional to preserve the previous behaviour and avoid regressions. Because there are drivers not using interrupts or because it might have worked "accidently" before.

Wolfram Sang committed 7 years ago

Commit 63b96981 parent 83c42214 files · +36 −10Browse files

Unified · Split

4 files
  1. drivers/i2c/i2c-core-base.c +5 −1
  2. drivers/i2c/i2c-core-smbus.c +14 −4
  3. drivers/i2c/i2c-core.h +5 −2
  4. include/linux/i2c.h +12 −3
drivers/i2c/i2c-core-base.c+5−1
@@ -1890,7 +1890,11 @@ int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) · 1889 unmodified lines
1890 /* Retry automatically on arbitration loss */1890 /* Retry automatically on arbitration loss */
1891 orig_jiffies = jiffies;1891 orig_jiffies = jiffies;
1892 for (ret = 0, try = 0; try <= adap->retries; try++) {1892 for (ret = 0, try = 0; try <= adap->retries; try++) {
1893 ret = adap->algo->master_xfer(adap, msgs, num);1893 if (i2c_in_atomic_xfer_mode() && adap->algo->master_xfer_atomic)
1894 ret = adap->algo->master_xfer_atomic(adap, msgs, num);
1895 else
1896 ret = adap->algo->master_xfer(adap, msgs, num);
1897
1894 if (ret != -EAGAIN)1898 if (ret != -EAGAIN)
1895 break;1899 break;
1896 if (time_after(jiffies, orig_jiffies + adap->timeout))1900 if (time_after(jiffies, orig_jiffies + adap->timeout))
drivers/i2c/i2c-core-smbus.c+14−4
@@ -548,6 +548,9 @@ s32 __i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, · 547 unmodified lines
548 unsigned short flags, char read_write,548 unsigned short flags, char read_write,
549 u8 command, int protocol, union i2c_smbus_data *data)549 u8 command, int protocol, union i2c_smbus_data *data)
550{550{
551 int (*xfer_func)(struct i2c_adapter *adap, u16 addr,
552 unsigned short flags, char read_write,
553 u8 command, int size, union i2c_smbus_data *data);
551 unsigned long orig_jiffies;554 unsigned long orig_jiffies;
552 int try;555 int try;
553 s32 res;556 s32 res;
@@ -562,13 +565,20 @@ s32 __i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, · 8 unmodified lines
562 565
563 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;566 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
564 567
565 if (adapter->algo->smbus_xfer) {568 xfer_func = adapter->algo->smbus_xfer;
569 if (i2c_in_atomic_xfer_mode()) {
570 if (adapter->algo->smbus_xfer_atomic)
571 xfer_func = adapter->algo->smbus_xfer_atomic;
572 else if (adapter->algo->master_xfer_atomic)
573 xfer_func = NULL; /* fallback to I2C emulation */
574 }
575
576 if (xfer_func) {
566 /* Retry automatically on arbitration loss */577 /* Retry automatically on arbitration loss */
567 orig_jiffies = jiffies;578 orig_jiffies = jiffies;
568 for (res = 0, try = 0; try <= adapter->retries; try++) {579 for (res = 0, try = 0; try <= adapter->retries; try++) {
569 res = adapter->algo->smbus_xfer(adapter, addr, flags,580 res = xfer_func(adapter, addr, flags, read_write,
570 read_write, command,581 command, protocol, data);
571 protocol, data);
572 if (res != -EAGAIN)582 if (res != -EAGAIN)
573 break;583 break;
574 if (time_after(jiffies,584 if (time_after(jiffies,
drivers/i2c/i2c-core.h+5−2
@@ -43,10 +43,13 @@ static inline int __i2c_lock_bus_helper(struct i2c_adapter *adap) · 42 unmodified lines
43{43{
44 int ret = 0;44 int ret = 0;
45 45
46 if (i2c_in_atomic_xfer_mode())46 if (i2c_in_atomic_xfer_mode()) {
47 WARN(!adap->algo->master_xfer_atomic && !adap->algo->smbus_xfer_atomic,
48 "No atomic I2C transfer handler for '%s'\n", dev_name(&adap->dev));
47 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT) ? 0 : -EAGAIN;49 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT) ? 0 : -EAGAIN;
48 else50 } else {
49 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);51 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
52 }
50 53
51 return ret;54 return ret;
52}55}
include/linux/i2c.h+12−3
@@ -499,9 +499,13 @@ i2c_register_board_info(int busnum, struct i2c_board_info const *info, · 498 unmodified lines
499 * @master_xfer: Issue a set of i2c transactions to the given I2C adapter499 * @master_xfer: Issue a set of i2c transactions to the given I2C adapter
500 * defined by the msgs array, with num messages available to transfer via500 * defined by the msgs array, with num messages available to transfer via
501 * the adapter specified by adap.501 * the adapter specified by adap.
502 * @master_xfer_atomic: same as @master_xfer. Yet, only using atomic context
503 * so e.g. PMICs can be accessed very late before shutdown. Optional.
502 * @smbus_xfer: Issue smbus transactions to the given I2C adapter. If this504 * @smbus_xfer: Issue smbus transactions to the given I2C adapter. If this
503 * is not present, then the bus layer will try and convert the SMBus calls505 * is not present, then the bus layer will try and convert the SMBus calls
504 * into I2C transfers instead.506 * into I2C transfers instead.
507 * @smbus_xfer_atomic: same as @smbus_xfer. Yet, only using atomic context
508 * so e.g. PMICs can be accessed very late before shutdown. Optional.
505 * @functionality: Return the flags that this algorithm/adapter pair supports509 * @functionality: Return the flags that this algorithm/adapter pair supports
506 * from the I2C_FUNC_* flags.510 * from the I2C_FUNC_* flags.
507 * @reg_slave: Register given client to I2C slave mode of this adapter511 * @reg_slave: Register given client to I2C slave mode of this adapter
@@ -512,9 +516,9 @@ i2c_register_board_info(int busnum, struct i2c_board_info const *info, · 4 unmodified lines
512 * be addressed using the same bus algorithms - i.e. bit-banging or the PCF8584516 * be addressed using the same bus algorithms - i.e. bit-banging or the PCF8584
513 * to name two of the most common.517 * to name two of the most common.
514 *518 *
515 * The return codes from the @master_xfer field should indicate the type of519 * The return codes from the @master_xfer{_atomic} fields should indicate the
516 * error code that occurred during the transfer, as documented in the kernel520 * type of error code that occurred during the transfer, as documented in the
517 * Documentation file Documentation/i2c/fault-codes.521 * Kernel Documentation file Documentation/i2c/fault-codes.
518 */522 */
519struct i2c_algorithm {523struct i2c_algorithm {
520 /*524 /*
@@ -528,9 +532,14 @@ struct i2c_algorithm { · 7 unmodified lines
528 */532 */
529 int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,533 int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,
530 int num);534 int num);
535 int (*master_xfer_atomic)(struct i2c_adapter *adap,
536 struct i2c_msg *msgs, int num);
531 int (*smbus_xfer)(struct i2c_adapter *adap, u16 addr,537 int (*smbus_xfer)(struct i2c_adapter *adap, u16 addr,
532 unsigned short flags, char read_write,538 unsigned short flags, char read_write,
533 u8 command, int size, union i2c_smbus_data *data);539 u8 command, int size, union i2c_smbus_data *data);
540 int (*smbus_xfer_atomic)(struct i2c_adapter *adap, u16 addr,
541 unsigned short flags, char read_write,
542 u8 command, int size, union i2c_smbus_data *data);
534 543
535 /* To determine what the adapter supports */544 /* To determine what the adapter supports */
536 u32 (*functionality)(struct i2c_adapter *adap);545 u32 (*functionality)(struct i2c_adapter *adap);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t