James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1 | // clang-format off |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 2 | /* |
| 3 | i2c-dev.h - i2c-bus driver, char device interface |
| 4 | |
| 5 | Copyright (C) 1995-97 Simon G. Vogl |
| 6 | Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl> |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 21 | MA 02110-1301 USA. |
| 22 | */ |
| 23 | |
| 24 | /* $Id: i2c-dev.h 5894 2010-12-12 13:22:29Z khali $ */ |
| 25 | |
| 26 | #ifndef LIB_I2CDEV_H |
| 27 | #define LIB_I2CDEV_H |
| 28 | |
| 29 | #include <linux/types.h> |
| 30 | #include <sys/ioctl.h> |
| 31 | #include <stddef.h> |
| 32 | |
| 33 | |
| 34 | /* -- i2c.h -- */ |
| 35 | |
| 36 | |
| 37 | /* |
| 38 | * I2C Message - used for pure i2c transaction, also from /dev interface |
| 39 | */ |
| 40 | struct i2c_msg { |
| 41 | __u16 addr; /* slave address */ |
| 42 | unsigned short flags; |
| 43 | #define I2C_M_TEN 0x10 /* we have a ten bit chip address */ |
| 44 | #define I2C_M_RD 0x01 |
| 45 | #define I2C_M_NOSTART 0x4000 |
| 46 | #define I2C_M_REV_DIR_ADDR 0x2000 |
| 47 | #define I2C_M_IGNORE_NAK 0x1000 |
| 48 | #define I2C_M_NO_RD_ACK 0x0800 |
| 49 | short len; /* msg length */ |
| 50 | char *buf; /* pointer to msg data */ |
| 51 | }; |
| 52 | |
| 53 | /* To determine what functionality is present */ |
| 54 | |
| 55 | #define I2C_FUNC_I2C 0x00000001 |
| 56 | #define I2C_FUNC_10BIT_ADDR 0x00000002 |
| 57 | #define I2C_FUNC_PROTOCOL_MANGLING 0x00000004 /* I2C_M_{REV_DIR_ADDR,NOSTART,..} */ |
| 58 | #define I2C_FUNC_SMBUS_PEC 0x00000008 |
| 59 | #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000 /* SMBus 2.0 */ |
| 60 | #define I2C_FUNC_SMBUS_QUICK 0x00010000 |
| 61 | #define I2C_FUNC_SMBUS_READ_BYTE 0x00020000 |
| 62 | #define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000 |
| 63 | #define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000 |
| 64 | #define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000 |
| 65 | #define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000 |
| 66 | #define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000 |
| 67 | #define I2C_FUNC_SMBUS_PROC_CALL 0x00800000 |
| 68 | #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000 |
| 69 | #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000 |
| 70 | #define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000 /* I2C-like block xfer */ |
| 71 | #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000 /* w/ 1-byte reg. addr. */ |
| 72 | |
| 73 | #define I2C_FUNC_SMBUS_BYTE (I2C_FUNC_SMBUS_READ_BYTE | \ |
| 74 | I2C_FUNC_SMBUS_WRITE_BYTE) |
| 75 | #define I2C_FUNC_SMBUS_BYTE_DATA (I2C_FUNC_SMBUS_READ_BYTE_DATA | \ |
| 76 | I2C_FUNC_SMBUS_WRITE_BYTE_DATA) |
| 77 | #define I2C_FUNC_SMBUS_WORD_DATA (I2C_FUNC_SMBUS_READ_WORD_DATA | \ |
| 78 | I2C_FUNC_SMBUS_WRITE_WORD_DATA) |
| 79 | #define I2C_FUNC_SMBUS_BLOCK_DATA (I2C_FUNC_SMBUS_READ_BLOCK_DATA | \ |
| 80 | I2C_FUNC_SMBUS_WRITE_BLOCK_DATA) |
| 81 | #define I2C_FUNC_SMBUS_I2C_BLOCK (I2C_FUNC_SMBUS_READ_I2C_BLOCK | \ |
| 82 | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK) |
| 83 | |
| 84 | /* Old name, for compatibility */ |
| 85 | #define I2C_FUNC_SMBUS_HWPEC_CALC I2C_FUNC_SMBUS_PEC |
| 86 | |
| 87 | /* |
| 88 | * Data for SMBus Messages |
| 89 | */ |
| 90 | #define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */ |
| 91 | #define I2C_SMBUS_I2C_BLOCK_MAX 32 /* Not specified but we use same structure */ |
| 92 | union i2c_smbus_data { |
| 93 | __u8 byte; |
| 94 | __u16 word; |
| 95 | __u8 block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */ |
| 96 | /* and one more for PEC */ |
| 97 | }; |
| 98 | |
| 99 | /* smbus_access read or write markers */ |
| 100 | #define I2C_SMBUS_READ 1 |
| 101 | #define I2C_SMBUS_WRITE 0 |
| 102 | |
| 103 | /* SMBus transaction types (size parameter in the above functions) |
| 104 | Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */ |
| 105 | #define I2C_SMBUS_QUICK 0 |
| 106 | #define I2C_SMBUS_BYTE 1 |
| 107 | #define I2C_SMBUS_BYTE_DATA 2 |
| 108 | #define I2C_SMBUS_WORD_DATA 3 |
| 109 | #define I2C_SMBUS_PROC_CALL 4 |
| 110 | #define I2C_SMBUS_BLOCK_DATA 5 |
| 111 | #define I2C_SMBUS_I2C_BLOCK_BROKEN 6 |
| 112 | #define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0 */ |
| 113 | #define I2C_SMBUS_I2C_BLOCK_DATA 8 |
| 114 | |
| 115 | |
| 116 | /* ----- commands for the ioctl like i2c_command call: |
| 117 | * note that additional calls are defined in the algorithm and hw |
| 118 | * dependent layers - these can be listed here, or see the |
| 119 | * corresponding header files. |
| 120 | */ |
| 121 | /* -> bit-adapter specific ioctls */ |
| 122 | #define I2C_RETRIES 0x0701 /* number of times a device address */ |
| 123 | /* should be polled when not */ |
| 124 | /* acknowledging */ |
| 125 | #define I2C_TIMEOUT 0x0702 /* set timeout - call with int */ |
| 126 | |
| 127 | |
| 128 | /* this is for i2c-dev.c */ |
| 129 | #define I2C_SLAVE 0x0703 /* Change slave address */ |
| 130 | /* Attn.: Slave address is 7 or 10 bits */ |
| 131 | #define I2C_SLAVE_FORCE 0x0706 /* Change slave address */ |
| 132 | /* Attn.: Slave address is 7 or 10 bits */ |
| 133 | /* This changes the address, even if it */ |
| 134 | /* is already taken! */ |
| 135 | #define I2C_TENBIT 0x0704 /* 0 for 7 bit addrs, != 0 for 10 bit */ |
| 136 | |
| 137 | #define I2C_FUNCS 0x0705 /* Get the adapter functionality */ |
| 138 | #define I2C_RDWR 0x0707 /* Combined R/W transfer (one stop only)*/ |
| 139 | #define I2C_PEC 0x0708 /* != 0 for SMBus PEC */ |
| 140 | |
| 141 | #define I2C_SMBUS 0x0720 /* SMBus-level access */ |
| 142 | |
| 143 | /* -- i2c.h -- */ |
| 144 | |
| 145 | |
| 146 | /* Note: 10-bit addresses are NOT supported! */ |
| 147 | |
| 148 | /* This is the structure as used in the I2C_SMBUS ioctl call */ |
| 149 | struct i2c_smbus_ioctl_data { |
| 150 | char read_write; |
| 151 | __u8 command; |
| 152 | int size; |
| 153 | union i2c_smbus_data *data; |
| 154 | }; |
| 155 | |
| 156 | /* This is the structure as used in the I2C_RDWR ioctl call */ |
| 157 | struct i2c_rdwr_ioctl_data { |
| 158 | struct i2c_msg *msgs; /* pointers to i2c_msgs */ |
| 159 | int nmsgs; /* number of i2c_msgs */ |
| 160 | }; |
| 161 | |
| 162 | |
| 163 | static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command, |
| 164 | int size, union i2c_smbus_data *data) |
| 165 | { |
| 166 | struct i2c_smbus_ioctl_data args; |
| 167 | |
| 168 | args.read_write = read_write; |
| 169 | args.command = command; |
| 170 | args.size = size; |
| 171 | args.data = data; |
| 172 | return ioctl(file,I2C_SMBUS,&args); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | static inline __s32 i2c_smbus_write_quick(int file, __u8 value) |
| 177 | { |
| 178 | return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL); |
| 179 | } |
| 180 | |
| 181 | static inline __s32 i2c_smbus_read_byte(int file) |
| 182 | { |
| 183 | union i2c_smbus_data data; |
| 184 | if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data)) |
| 185 | return -1; |
| 186 | else |
| 187 | return 0x0FF & data.byte; |
| 188 | } |
| 189 | |
| 190 | static inline __s32 i2c_smbus_write_byte(int file, __u8 value) |
| 191 | { |
| 192 | return i2c_smbus_access(file,I2C_SMBUS_WRITE,value, |
| 193 | I2C_SMBUS_BYTE,NULL); |
| 194 | } |
| 195 | |
| 196 | static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command) |
| 197 | { |
| 198 | union i2c_smbus_data data; |
| 199 | if (i2c_smbus_access(file,I2C_SMBUS_READ,command, |
| 200 | I2C_SMBUS_BYTE_DATA,&data)) |
| 201 | return -1; |
| 202 | else |
| 203 | return 0x0FF & data.byte; |
| 204 | } |
| 205 | |
| 206 | static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command, |
| 207 | __u8 value) |
| 208 | { |
| 209 | union i2c_smbus_data data; |
| 210 | data.byte = value; |
| 211 | return i2c_smbus_access(file,I2C_SMBUS_WRITE,command, |
| 212 | I2C_SMBUS_BYTE_DATA, &data); |
| 213 | } |
| 214 | |
| 215 | static inline __s32 i2c_smbus_read_word_data(int file, __u8 command) |
| 216 | { |
| 217 | union i2c_smbus_data data; |
| 218 | if (i2c_smbus_access(file,I2C_SMBUS_READ,command, |
| 219 | I2C_SMBUS_WORD_DATA,&data)) |
| 220 | return -1; |
| 221 | else |
| 222 | return 0x0FFFF & data.word; |
| 223 | } |
| 224 | |
| 225 | static inline __s32 i2c_smbus_write_word_data(int file, __u8 command, |
| 226 | __u16 value) |
| 227 | { |
| 228 | union i2c_smbus_data data; |
| 229 | data.word = value; |
| 230 | return i2c_smbus_access(file,I2C_SMBUS_WRITE,command, |
| 231 | I2C_SMBUS_WORD_DATA, &data); |
| 232 | } |
| 233 | |
| 234 | static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value) |
| 235 | { |
| 236 | union i2c_smbus_data data; |
| 237 | data.word = value; |
| 238 | if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command, |
| 239 | I2C_SMBUS_PROC_CALL,&data)) |
| 240 | return -1; |
| 241 | else |
| 242 | return 0x0FFFF & data.word; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | /* Returns the number of read bytes */ |
| 247 | static inline __s32 i2c_smbus_read_block_data(int file, __u8 command, |
| 248 | __u8 *values) |
| 249 | { |
| 250 | union i2c_smbus_data data; |
| 251 | int i; |
| 252 | if (i2c_smbus_access(file,I2C_SMBUS_READ,command, |
| 253 | I2C_SMBUS_BLOCK_DATA,&data)) |
| 254 | return -1; |
| 255 | else { |
| 256 | for (i = 1; i <= data.block[0]; i++) |
| 257 | values[i-1] = data.block[i]; |
| 258 | return data.block[0]; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | static inline __s32 i2c_smbus_write_block_data(int file, __u8 command, |
| 263 | __u8 length, const __u8 *values) |
| 264 | { |
| 265 | union i2c_smbus_data data; |
| 266 | int i; |
| 267 | if (length > 32) |
| 268 | length = 32; |
| 269 | for (i = 1; i <= length; i++) |
| 270 | data.block[i] = values[i-1]; |
| 271 | data.block[0] = length; |
| 272 | return i2c_smbus_access(file,I2C_SMBUS_WRITE,command, |
| 273 | I2C_SMBUS_BLOCK_DATA, &data); |
| 274 | } |
| 275 | |
| 276 | /* Returns the number of read bytes */ |
| 277 | /* Until kernel 2.6.22, the length is hardcoded to 32 bytes. If you |
| 278 | ask for less than 32 bytes, your code will only work with kernels |
| 279 | 2.6.23 and later. */ |
| 280 | static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command, |
| 281 | __u8 length, __u8 *values) |
| 282 | { |
| 283 | union i2c_smbus_data data; |
| 284 | int i; |
| 285 | |
| 286 | if (length > 32) |
| 287 | length = 32; |
| 288 | data.block[0] = length; |
| 289 | if (i2c_smbus_access(file,I2C_SMBUS_READ,command, |
| 290 | length == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN : |
| 291 | I2C_SMBUS_I2C_BLOCK_DATA,&data)) |
| 292 | return -1; |
| 293 | else { |
| 294 | for (i = 1; i <= data.block[0]; i++) |
| 295 | values[i-1] = data.block[i]; |
| 296 | return data.block[0]; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command, |
| 301 | __u8 length, |
| 302 | const __u8 *values) |
| 303 | { |
| 304 | union i2c_smbus_data data; |
| 305 | int i; |
| 306 | if (length > 32) |
| 307 | length = 32; |
| 308 | for (i = 1; i <= length; i++) |
| 309 | data.block[i] = values[i-1]; |
| 310 | data.block[0] = length; |
| 311 | return i2c_smbus_access(file,I2C_SMBUS_WRITE,command, |
| 312 | I2C_SMBUS_I2C_BLOCK_BROKEN, &data); |
| 313 | } |
| 314 | |
| 315 | /* Returns the number of read bytes */ |
| 316 | static inline __s32 i2c_smbus_block_process_call(int file, __u8 command, |
| 317 | __u8 length, __u8 *values) |
| 318 | { |
| 319 | union i2c_smbus_data data; |
| 320 | int i; |
| 321 | if (length > 32) |
| 322 | length = 32; |
| 323 | for (i = 1; i <= length; i++) |
| 324 | data.block[i] = values[i-1]; |
| 325 | data.block[0] = length; |
| 326 | if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command, |
| 327 | I2C_SMBUS_BLOCK_PROC_CALL,&data)) |
| 328 | return -1; |
| 329 | else { |
| 330 | for (i = 1; i <= data.block[0]; i++) |
| 331 | values[i-1] = data.block[i]; |
| 332 | return data.block[0]; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | |
| 337 | #endif /* LIB_I2CDEV_H */ |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 338 | // clang-format on |