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