1 /*- 2 * Copyright (c) 2021 Beckhoff Automation GmbH & Co. KG 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 */ 26 27 #ifndef _LINUX_I2C_H_ 28 #define _LINUX_I2C_H_ 29 30 #include <sys/types.h> 31 #include <sys/errno.h> 32 #include <sys/systm.h> 33 34 #include <linux/device.h> 35 36 #define I2C_MAX_ADAPTER_NAME_LENGTH 32 37 38 #define I2C_M_RD 0x0001 39 #define I2C_M_NOSTART 0x0002 40 #define I2C_M_STOP 0x0004 41 42 /* No need for us */ 43 #define I2C_FUNC_I2C 0 44 #define I2C_FUNC_SMBUS_EMUL 0 45 #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0 46 #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0 47 #define I2C_FUNC_10BIT_ADDR 0 48 49 #define I2C_CLASS_HWMON 0x1 50 #define I2C_CLASS_DDC 0x8 51 #define I2C_CLASS_SPD 0x80 52 53 struct i2c_adapter { 54 struct module *owner; 55 unsigned int class; 56 57 char name[I2C_MAX_ADAPTER_NAME_LENGTH]; 58 struct device dev; 59 60 const struct i2c_lock_operations *lock_ops; 61 const struct i2c_algorithm *algo; 62 const struct i2c_adapter_quirks *quirks; 63 void *algo_data; 64 65 int retries; 66 void *data; 67 }; 68 69 struct i2c_msg { 70 uint16_t addr; 71 uint16_t flags; 72 uint16_t len; 73 uint8_t *buf; 74 }; 75 76 struct i2c_algorithm { 77 int (*master_xfer)(struct i2c_adapter *, struct i2c_msg *, int); 78 uint32_t (*functionality)(struct i2c_adapter *); 79 }; 80 81 struct i2c_lock_operations { 82 void (*lock_bus)(struct i2c_adapter *, unsigned int); 83 int (*trylock_bus)(struct i2c_adapter *, unsigned int); 84 void (*unlock_bus)(struct i2c_adapter *, unsigned int); 85 }; 86 87 struct i2c_adapter_quirks { 88 uint64_t flags; 89 int max_num_msgs; 90 uint16_t max_write_len; 91 uint16_t max_read_len; 92 uint16_t max_comb_1st_msg_len; 93 uint16_t max_comb_2nd_msg_len; 94 }; 95 96 #define I2C_AQ_COMB BIT(0) 97 #define I2C_AQ_COMB_WRITE_FIRST BIT(1) 98 #define I2C_AQ_COMB_READ_SECOND BIT(2) 99 #define I2C_AQ_COMB_SAME_ADDR BIT(3) 100 #define I2C_AQ_COMB_WRITE_THEN_READ \ 101 (I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST | \ 102 I2C_AQ_COMB_READ_SECOND | I2C_AQ_COMB_SAME_ADDR) 103 #define I2C_AQ_NO_CLK_STRETCH BIT(4) 104 #define I2C_AQ_NO_ZERO_LEN_READ BIT(5) 105 #define I2C_AQ_NO_ZERO_LEN_WRITE BIT(6) 106 #define I2C_AQ_NO_ZERO_LEN \ 107 (I2C_AQ_NO_ZERO_LEN_READ | I2C_AQ_NO_ZERO_LEN_WRITE) 108 #define I2C_AQ_NO_REP_START BIT(7) 109 110 int lkpi_i2c_add_adapter(struct i2c_adapter *adapter); 111 int lkpi_i2c_del_adapter(struct i2c_adapter *adapter); 112 113 int lkpi_i2cbb_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int nmsgs); 114 115 #define i2c_add_adapter(adapter) lkpi_i2c_add_adapter(adapter) 116 #define i2c_del_adapter(adapter) lkpi_i2c_del_adapter(adapter) 117 118 #define i2c_get_adapter(x) NULL 119 #define i2c_put_adapter(x) 120 121 static inline int 122 do_i2c_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int nmsgs) 123 { 124 int ret, retries; 125 126 retries = adapter->retries == 0 ? 1 : adapter->retries; 127 for (; retries != 0; retries--) { 128 if (adapter->algo != NULL && adapter->algo->master_xfer != NULL) 129 ret = adapter->algo->master_xfer(adapter, msgs, nmsgs); 130 else 131 ret = lkpi_i2cbb_transfer(adapter, msgs, nmsgs); 132 if (ret != -EAGAIN) 133 break; 134 } 135 136 return (ret); 137 } 138 139 static inline int 140 i2c_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int nmsgs) 141 { 142 int ret; 143 144 if (adapter->algo == NULL && adapter->algo_data == NULL) 145 return (-EOPNOTSUPP); 146 147 if (adapter->lock_ops) 148 adapter->lock_ops->lock_bus(adapter, 0); 149 150 ret = do_i2c_transfer(adapter, msgs, nmsgs); 151 152 if (adapter->lock_ops) 153 adapter->lock_ops->unlock_bus(adapter, 0); 154 155 return (ret); 156 } 157 158 /* Unlocked version of i2c_transfer */ 159 static inline int 160 __i2c_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int nmsgs) 161 { 162 return (do_i2c_transfer(adapter, msgs, nmsgs)); 163 } 164 165 static inline void 166 i2c_set_adapdata(struct i2c_adapter *adapter, void *data) 167 { 168 adapter->data = data; 169 } 170 171 static inline void * 172 i2c_get_adapdata(struct i2c_adapter *adapter) 173 { 174 return (adapter->data); 175 } 176 177 #endif /* _LINUX_I2C_H_ */ 178