| 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., 51 Franklin Street, Fifth Floor, Boston, |
|---|
| 20 | MA 02110-1301 USA. |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #ifndef _LINUX_I2C_DEV_H |
|---|
| 24 | #define _LINUX_I2C_DEV_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 | }; |
|---|
| 48 | |
|---|
| 49 | /* To determine what functionality is present */ |
|---|
| 50 | |
|---|
| 51 | #define I2C_FUNC_I2C 0x00000001 |
|---|
| 52 | #define I2C_FUNC_10BIT_ADDR 0x00000002 |
|---|
| 53 | #define I2C_FUNC_PROTOCOL_MANGLING 0x00000004 /* I2C_M_{REV_DIR_ADDR,NOSTART,..} */ |
|---|
| 54 | #define I2C_FUNC_SMBUS_PEC 0x00000008 |
|---|
| 55 | #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000 /* SMBus 2.0 */ |
|---|
| 56 | #define I2C_FUNC_SMBUS_QUICK 0x00010000 |
|---|
| 57 | #define I2C_FUNC_SMBUS_READ_BYTE 0x00020000 |
|---|
| 58 | #define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000 |
|---|
| 59 | #define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000 |
|---|
| 60 | #define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000 |
|---|
| 61 | #define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000 |
|---|
| 62 | #define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000 |
|---|
| 63 | #define I2C_FUNC_SMBUS_PROC_CALL 0x00800000 |
|---|
| 64 | #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000 |
|---|
| 65 | #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000 |
|---|
| 66 | #define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000 /* I2C-like block xfer */ |
|---|
| 67 | #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000 /* w/ 1-byte reg. addr. */ |
|---|
| 68 | |
|---|
| 69 | #define I2C_FUNC_SMBUS_BYTE (I2C_FUNC_SMBUS_READ_BYTE | \ |
|---|
| 70 | I2C_FUNC_SMBUS_WRITE_BYTE) |
|---|
| 71 | #define I2C_FUNC_SMBUS_BYTE_DATA (I2C_FUNC_SMBUS_READ_BYTE_DATA | \ |
|---|
| 72 | I2C_FUNC_SMBUS_WRITE_BYTE_DATA) |
|---|
| 73 | #define I2C_FUNC_SMBUS_WORD_DATA (I2C_FUNC_SMBUS_READ_WORD_DATA | \ |
|---|
| 74 | I2C_FUNC_SMBUS_WRITE_WORD_DATA) |
|---|
| 75 | #define I2C_FUNC_SMBUS_BLOCK_DATA (I2C_FUNC_SMBUS_READ_BLOCK_DATA | \ |
|---|
| 76 | I2C_FUNC_SMBUS_WRITE_BLOCK_DATA) |
|---|
| 77 | #define I2C_FUNC_SMBUS_I2C_BLOCK (I2C_FUNC_SMBUS_READ_I2C_BLOCK | \ |
|---|
| 78 | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK) |
|---|
| 79 | |
|---|
| 80 | /* Old name, for compatibility */ |
|---|
| 81 | #define I2C_FUNC_SMBUS_HWPEC_CALC I2C_FUNC_SMBUS_PEC |
|---|
| 82 | |
|---|
| 83 | /* |
|---|
| 84 | * Data for SMBus Messages |
|---|
| 85 | */ |
|---|
| 86 | #define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */ |
|---|
| 87 | #define I2C_SMBUS_I2C_BLOCK_MAX 32 /* Not specified but we use same structure */ |
|---|
| 88 | union i2c_smbus_data { |
|---|
| 89 | __u8 byte; |
|---|
| 90 | __u16 word; |
|---|
| 91 | __u8 block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */ |
|---|
| 92 | /* and one more for PEC */ |
|---|
| 93 | }; |
|---|
| 94 | |
|---|
| 95 | /* smbus_access read or write markers */ |
|---|
| 96 | #define I2C_SMBUS_READ 1 |
|---|
| 97 | #define I2C_SMBUS_WRITE 0 |
|---|
| 98 | |
|---|
| 99 | /* SMBus transaction types (size parameter in the above functions) |
|---|
| 100 | Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */ |
|---|
| 101 | #define I2C_SMBUS_QUICK 0 |
|---|
| 102 | #define I2C_SMBUS_BYTE 1 |
|---|
| 103 | #define I2C_SMBUS_BYTE_DATA 2 |
|---|
| 104 | #define I2C_SMBUS_WORD_DATA 3 |
|---|
| 105 | #define I2C_SMBUS_PROC_CALL 4 |
|---|
| 106 | #define I2C_SMBUS_BLOCK_DATA 5 |
|---|
| 107 | #define I2C_SMBUS_I2C_BLOCK_BROKEN 6 |
|---|
| 108 | #define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0 */ |
|---|
| 109 | #define I2C_SMBUS_I2C_BLOCK_DATA 8 |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | /* /dev/i2c-X ioctl commands. The ioctl's parameter is always an |
|---|
| 113 | * unsigned long, except for: |
|---|
| 114 | * - I2C_FUNCS, takes pointer to an unsigned long |
|---|
| 115 | * - I2C_RDWR, takes pointer to struct i2c_rdwr_ioctl_data |
|---|
| 116 | * - I2C_SMBUS, takes pointer to struct i2c_smbus_ioctl_data |
|---|
| 117 | */ |
|---|
| 118 | #define I2C_RETRIES 0x0701 /* number of times a device address should |
|---|
| 119 | be polled when not acknowledging */ |
|---|
| 120 | #define I2C_TIMEOUT 0x0702 /* set timeout in units of 10 ms */ |
|---|
| 121 | |
|---|
| 122 | /* NOTE: Slave address is 7 or 10 bits, but 10-bit addresses |
|---|
| 123 | * are NOT supported! (due to code brokenness) |
|---|
| 124 | */ |
|---|
| 125 | #define I2C_SLAVE 0x0703 /* Use this slave address */ |
|---|
| 126 | #define I2C_SLAVE_FORCE 0x0706 /* Use this slave address, even if it |
|---|
| 127 | is already in use by a driver! */ |
|---|
| 128 | #define I2C_TENBIT 0x0704 /* 0 for 7 bit addrs, != 0 for 10 bit */ |
|---|
| 129 | |
|---|
| 130 | #define I2C_FUNCS 0x0705 /* Get the adapter functionality mask */ |
|---|
| 131 | |
|---|
| 132 | #define I2C_RDWR 0x0707 /* Combined R/W transfer (one STOP only) */ |
|---|
| 133 | |
|---|
| 134 | #define I2C_PEC 0x0708 /* != 0 to use PEC with SMBus */ |
|---|
| 135 | #define I2C_SMBUS 0x0720 /* SMBus transfer */ |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | /* This is the structure as used in the I2C_SMBUS ioctl call */ |
|---|
| 139 | struct i2c_smbus_ioctl_data { |
|---|
| 140 | __u8 read_write; |
|---|
| 141 | __u8 command; |
|---|
| 142 | __u32 size; |
|---|
| 143 | union i2c_smbus_data *data; |
|---|
| 144 | }; |
|---|
| 145 | |
|---|
| 146 | /* This is the structure as used in the I2C_RDWR ioctl call */ |
|---|
| 147 | struct i2c_rdwr_ioctl_data { |
|---|
| 148 | struct i2c_msg *msgs; /* pointers to i2c_msgs */ |
|---|
| 149 | __u32 nmsgs; /* number of i2c_msgs */ |
|---|
| 150 | }; |
|---|
| 151 | |
|---|
| 152 | #define I2C_RDRW_IOCTL_MAX_MSGS 42 |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command, |
|---|
| 156 | int size, union i2c_smbus_data *data) |
|---|
| 157 | { |
|---|
| 158 | struct i2c_smbus_ioctl_data args; |
|---|
| 159 | |
|---|
| 160 | args.read_write = read_write; |
|---|
| 161 | args.command = command; |
|---|
| 162 | args.size = size; |
|---|
| 163 | args.data = data; |
|---|
| 164 | return ioctl(file,I2C_SMBUS,&args); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | static inline __s32 i2c_smbus_write_quick(int file, __u8 value) |
|---|
| 169 | { |
|---|
| 170 | return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | static inline __s32 i2c_smbus_read_byte(int file) |
|---|
| 174 | { |
|---|
| 175 | union i2c_smbus_data data; |
|---|
| 176 | if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data)) |
|---|
| 177 | return -1; |
|---|
| 178 | else |
|---|
| 179 | return 0x0FF & data.byte; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | static inline __s32 i2c_smbus_write_byte(int file, __u8 value) |
|---|
| 183 | { |
|---|
| 184 | return i2c_smbus_access(file,I2C_SMBUS_WRITE,value, |
|---|
| 185 | I2C_SMBUS_BYTE,NULL); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command) |
|---|
| 189 | { |
|---|
| 190 | union i2c_smbus_data data; |
|---|
| 191 | if (i2c_smbus_access(file,I2C_SMBUS_READ,command, |
|---|
| 192 | I2C_SMBUS_BYTE_DATA,&data)) |
|---|
| 193 | return -1; |
|---|
| 194 | else |
|---|
| 195 | return 0x0FF & data.byte; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command, |
|---|
| 199 | __u8 value) |
|---|
| 200 | { |
|---|
| 201 | union i2c_smbus_data data; |
|---|
| 202 | data.byte = value; |
|---|
| 203 | return i2c_smbus_access(file,I2C_SMBUS_WRITE,command, |
|---|
| 204 | I2C_SMBUS_BYTE_DATA, &data); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | static inline __s32 i2c_smbus_read_word_data(int file, __u8 command) |
|---|
| 208 | { |
|---|
| 209 | union i2c_smbus_data data; |
|---|
| 210 | if (i2c_smbus_access(file,I2C_SMBUS_READ,command, |
|---|
| 211 | I2C_SMBUS_WORD_DATA,&data)) |
|---|
| 212 | return -1; |
|---|
| 213 | else |
|---|
| 214 | return 0x0FFFF & data.word; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | static inline __s32 i2c_smbus_write_word_data(int file, __u8 command, |
|---|
| 218 | __u16 value) |
|---|
| 219 | { |
|---|
| 220 | union i2c_smbus_data data; |
|---|
| 221 | data.word = value; |
|---|
| 222 | return i2c_smbus_access(file,I2C_SMBUS_WRITE,command, |
|---|
| 223 | I2C_SMBUS_WORD_DATA, &data); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value) |
|---|
| 227 | { |
|---|
| 228 | union i2c_smbus_data data; |
|---|
| 229 | data.word = value; |
|---|
| 230 | if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command, |
|---|
| 231 | I2C_SMBUS_PROC_CALL,&data)) |
|---|
| 232 | return -1; |
|---|
| 233 | else |
|---|
| 234 | return 0x0FFFF & data.word; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | /* Returns the number of read bytes */ |
|---|
| 239 | static inline __s32 i2c_smbus_read_block_data(int file, __u8 command, |
|---|
| 240 | __u8 *values) |
|---|
| 241 | { |
|---|
| 242 | union i2c_smbus_data data; |
|---|
| 243 | int i; |
|---|
| 244 | if (i2c_smbus_access(file,I2C_SMBUS_READ,command, |
|---|
| 245 | I2C_SMBUS_BLOCK_DATA,&data)) |
|---|
| 246 | return -1; |
|---|
| 247 | else { |
|---|
| 248 | for (i = 1; i <= data.block[0]; i++) |
|---|
| 249 | values[i-1] = data.block[i]; |
|---|
| 250 | return data.block[0]; |
|---|
| 251 | } |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | static inline __s32 i2c_smbus_write_block_data(int file, __u8 command, |
|---|
| 255 | __u8 length, const __u8 *values) |
|---|
| 256 | { |
|---|
| 257 | union i2c_smbus_data data; |
|---|
| 258 | int i; |
|---|
| 259 | if (length > 32) |
|---|
| 260 | length = 32; |
|---|
| 261 | for (i = 1; i <= length; i++) |
|---|
| 262 | data.block[i] = values[i-1]; |
|---|
| 263 | data.block[0] = length; |
|---|
| 264 | return i2c_smbus_access(file,I2C_SMBUS_WRITE,command, |
|---|
| 265 | I2C_SMBUS_BLOCK_DATA, &data); |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | /* Returns the number of read bytes */ |
|---|
| 269 | /* Until kernel 2.6.22, the length is hardcoded to 32 bytes. If you |
|---|
| 270 | ask for less than 32 bytes, your code will only work with kernels |
|---|
| 271 | 2.6.23 and later. */ |
|---|
| 272 | static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command, |
|---|
| 273 | __u8 length, __u8 *values) |
|---|
| 274 | { |
|---|
| 275 | union i2c_smbus_data data; |
|---|
| 276 | int i; |
|---|
| 277 | |
|---|
| 278 | if (length > 32) |
|---|
| 279 | length = 32; |
|---|
| 280 | data.block[0] = length; |
|---|
| 281 | if (i2c_smbus_access(file,I2C_SMBUS_READ,command, |
|---|
| 282 | length == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN : |
|---|
| 283 | I2C_SMBUS_I2C_BLOCK_DATA,&data)) |
|---|
| 284 | return -1; |
|---|
| 285 | else { |
|---|
| 286 | for (i = 1; i <= data.block[0]; i++) |
|---|
| 287 | values[i-1] = data.block[i]; |
|---|
| 288 | return data.block[0]; |
|---|
| 289 | } |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command, |
|---|
| 293 | __u8 length, |
|---|
| 294 | const __u8 *values) |
|---|
| 295 | { |
|---|
| 296 | union i2c_smbus_data data; |
|---|
| 297 | int i; |
|---|
| 298 | if (length > 32) |
|---|
| 299 | length = 32; |
|---|
| 300 | for (i = 1; i <= length; i++) |
|---|
| 301 | data.block[i] = values[i-1]; |
|---|
| 302 | data.block[0] = length; |
|---|
| 303 | return i2c_smbus_access(file,I2C_SMBUS_WRITE,command, |
|---|
| 304 | I2C_SMBUS_I2C_BLOCK_BROKEN, &data); |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | /* Returns the number of read bytes */ |
|---|
| 308 | static inline __s32 i2c_smbus_block_process_call(int file, __u8 command, |
|---|
| 309 | __u8 length, __u8 *values) |
|---|
| 310 | { |
|---|
| 311 | union i2c_smbus_data data; |
|---|
| 312 | int i; |
|---|
| 313 | if (length > 32) |
|---|
| 314 | length = 32; |
|---|
| 315 | for (i = 1; i <= length; i++) |
|---|
| 316 | data.block[i] = values[i-1]; |
|---|
| 317 | data.block[0] = length; |
|---|
| 318 | if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command, |
|---|
| 319 | I2C_SMBUS_BLOCK_PROC_CALL,&data)) |
|---|
| 320 | return -1; |
|---|
| 321 | else { |
|---|
| 322 | for (i = 1; i <= data.block[0]; i++) |
|---|
| 323 | values[i-1] = data.block[i]; |
|---|
| 324 | return data.block[0]; |
|---|
| 325 | } |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | #endif /* _LINUX_I2C_DEV_H */ |
|---|