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/bits.h> 35 #include <linux/mod_devicetable.h> 36 #include <linux/device.h> 37 #include <linux/sched.h> 38 #include <linux/mutex.h> 39 #include <linux/regulator/consumer.h> 40 #include <linux/irqdomain.h> 41 #include <linux/of.h> 42 43 #define I2C_MAX_ADAPTER_NAME_LENGTH 32 44 45 #define I2C_M_RD 0x0001 46 #define I2C_M_NOSTART 0x0002 47 #define I2C_M_STOP 0x0004 48 49 /* No need for us */ 50 #define I2C_FUNC_I2C 0 51 #define I2C_FUNC_SMBUS_EMUL 0 52 #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0 53 #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0 54 #define I2C_FUNC_10BIT_ADDR 0 55 56 #define I2C_CLASS_HWMON 0x1 57 #define I2C_CLASS_DDC 0x8 58 #define I2C_CLASS_SPD 0x80 59 60 struct i2c_adapter { 61 struct module *owner; 62 unsigned int class; 63 64 char name[I2C_MAX_ADAPTER_NAME_LENGTH]; 65 struct device dev; 66 67 const struct i2c_lock_operations *lock_ops; 68 const struct i2c_algorithm *algo; 69 const struct i2c_adapter_quirks *quirks; 70 void *algo_data; 71 72 int retries; 73 void *data; 74 }; 75 76 struct i2c_msg { 77 uint16_t addr; 78 uint16_t flags; 79 uint16_t len; 80 uint8_t *buf; 81 }; 82 83 struct i2c_algorithm { 84 int (*master_xfer)(struct i2c_adapter *, struct i2c_msg *, int); 85 uint32_t (*functionality)(struct i2c_adapter *); 86 }; 87 88 struct i2c_lock_operations { 89 void (*lock_bus)(struct i2c_adapter *, unsigned int); 90 int (*trylock_bus)(struct i2c_adapter *, unsigned int); 91 void (*unlock_bus)(struct i2c_adapter *, unsigned int); 92 }; 93 94 struct i2c_adapter_quirks { 95 uint64_t flags; 96 int max_num_msgs; 97 uint16_t max_write_len; 98 uint16_t max_read_len; 99 uint16_t max_comb_1st_msg_len; 100 uint16_t max_comb_2nd_msg_len; 101 }; 102 103 #define I2C_AQ_COMB BIT(0) 104 #define I2C_AQ_COMB_WRITE_FIRST BIT(1) 105 #define I2C_AQ_COMB_READ_SECOND BIT(2) 106 #define I2C_AQ_COMB_SAME_ADDR BIT(3) 107 #define I2C_AQ_COMB_WRITE_THEN_READ \ 108 (I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST | \ 109 I2C_AQ_COMB_READ_SECOND | I2C_AQ_COMB_SAME_ADDR) 110 #define I2C_AQ_NO_CLK_STRETCH BIT(4) 111 #define I2C_AQ_NO_ZERO_LEN_READ BIT(5) 112 #define I2C_AQ_NO_ZERO_LEN_WRITE BIT(6) 113 #define I2C_AQ_NO_ZERO_LEN \ 114 (I2C_AQ_NO_ZERO_LEN_READ | I2C_AQ_NO_ZERO_LEN_WRITE) 115 #define I2C_AQ_NO_REP_START BIT(7) 116 117 int lkpi_i2c_add_adapter(struct i2c_adapter *adapter); 118 int lkpi_i2c_del_adapter(struct i2c_adapter *adapter); 119 120 int lkpi_i2cbb_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int nmsgs); 121 122 #define i2c_add_adapter(adapter) lkpi_i2c_add_adapter(adapter) 123 #define i2c_del_adapter(adapter) lkpi_i2c_del_adapter(adapter) 124 125 #define i2c_get_adapter(x) NULL 126 #define i2c_put_adapter(x) 127 128 static inline int 129 do_i2c_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int nmsgs) 130 { 131 int ret, retries; 132 133 retries = adapter->retries == 0 ? 1 : adapter->retries; 134 for (; retries != 0; retries--) { 135 if (adapter->algo != NULL && adapter->algo->master_xfer != NULL) 136 ret = adapter->algo->master_xfer(adapter, msgs, nmsgs); 137 else 138 ret = lkpi_i2cbb_transfer(adapter, msgs, nmsgs); 139 if (ret != -EAGAIN) 140 break; 141 } 142 143 return (ret); 144 } 145 146 static inline int 147 i2c_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int nmsgs) 148 { 149 int ret; 150 151 if (adapter->algo == NULL && adapter->algo_data == NULL) 152 return (-EOPNOTSUPP); 153 154 if (adapter->lock_ops) 155 adapter->lock_ops->lock_bus(adapter, 0); 156 157 ret = do_i2c_transfer(adapter, msgs, nmsgs); 158 159 if (adapter->lock_ops) 160 adapter->lock_ops->unlock_bus(adapter, 0); 161 162 return (ret); 163 } 164 165 /* Unlocked version of i2c_transfer */ 166 static inline int 167 __i2c_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int nmsgs) 168 { 169 return (do_i2c_transfer(adapter, msgs, nmsgs)); 170 } 171 172 static inline void 173 i2c_set_adapdata(struct i2c_adapter *adapter, void *data) 174 { 175 adapter->data = data; 176 } 177 178 static inline void * 179 i2c_get_adapdata(struct i2c_adapter *adapter) 180 { 181 return (adapter->data); 182 } 183 184 #endif /* _LINUX_I2C_H_ */ 185