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