123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- #ifndef _LINUX_I2C_DEV_H
- #define _LINUX_I2C_DEV_H
- #include <linux/types.h>
- #include <sys/ioctl.h>
- #include <stddef.h>
- struct i2c_msg {
- __u16 addr;
- unsigned short flags;
- #define I2C_M_TEN 0x10
- #define I2C_M_RD 0x01
- #define I2C_M_NOSTART 0x4000
- #define I2C_M_REV_DIR_ADDR 0x2000
- #define I2C_M_IGNORE_NAK 0x1000
- #define I2C_M_NO_RD_ACK 0x0800
- short len;
- char *buf;
- };
- #define I2C_FUNC_I2C 0x00000001
- #define I2C_FUNC_10BIT_ADDR 0x00000002
- #define I2C_FUNC_PROTOCOL_MANGLING 0x00000004
- #define I2C_FUNC_SMBUS_PEC 0x00000008
- #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000
- #define I2C_FUNC_SMBUS_QUICK 0x00010000
- #define I2C_FUNC_SMBUS_READ_BYTE 0x00020000
- #define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000
- #define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000
- #define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000
- #define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000
- #define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000
- #define I2C_FUNC_SMBUS_PROC_CALL 0x00800000
- #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000
- #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000
- #define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000
- #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000
- #define I2C_FUNC_SMBUS_BYTE (I2C_FUNC_SMBUS_READ_BYTE | \
- I2C_FUNC_SMBUS_WRITE_BYTE)
- #define I2C_FUNC_SMBUS_BYTE_DATA (I2C_FUNC_SMBUS_READ_BYTE_DATA | \
- I2C_FUNC_SMBUS_WRITE_BYTE_DATA)
- #define I2C_FUNC_SMBUS_WORD_DATA (I2C_FUNC_SMBUS_READ_WORD_DATA | \
- I2C_FUNC_SMBUS_WRITE_WORD_DATA)
- #define I2C_FUNC_SMBUS_BLOCK_DATA (I2C_FUNC_SMBUS_READ_BLOCK_DATA | \
- I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)
- #define I2C_FUNC_SMBUS_I2C_BLOCK (I2C_FUNC_SMBUS_READ_I2C_BLOCK | \
- I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)
- #define I2C_FUNC_SMBUS_HWPEC_CALC I2C_FUNC_SMBUS_PEC
- #define I2C_SMBUS_BLOCK_MAX 32
- #define I2C_SMBUS_I2C_BLOCK_MAX 32
- union i2c_smbus_data {
- __u8 byte;
- __u16 word;
- __u8 block[I2C_SMBUS_BLOCK_MAX + 2];
-
- };
- #define I2C_SMBUS_READ 1
- #define I2C_SMBUS_WRITE 0
- #define I2C_SMBUS_QUICK 0
- #define I2C_SMBUS_BYTE 1
- #define I2C_SMBUS_BYTE_DATA 2
- #define I2C_SMBUS_WORD_DATA 3
- #define I2C_SMBUS_PROC_CALL 4
- #define I2C_SMBUS_BLOCK_DATA 5
- #define I2C_SMBUS_I2C_BLOCK_BROKEN 6
- #define I2C_SMBUS_BLOCK_PROC_CALL 7
- #define I2C_SMBUS_I2C_BLOCK_DATA 8
- #define I2C_RETRIES 0x0701
- #define I2C_TIMEOUT 0x0702
- #define I2C_SLAVE 0x0703
- #define I2C_SLAVE_FORCE 0x0706
- #define I2C_TENBIT 0x0704
- #define I2C_FUNCS 0x0705
- #define I2C_RDWR 0x0707
- #define I2C_PEC 0x0708
- #define I2C_SMBUS 0x0720
- struct i2c_smbus_ioctl_data {
- __u8 read_write;
- __u8 command;
- __u32 size;
- union i2c_smbus_data *data;
- };
- struct i2c_rdwr_ioctl_data {
- struct i2c_msg *msgs;
- __u32 nmsgs;
- };
- #define I2C_RDRW_IOCTL_MAX_MSGS 42
- static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,
- int size, union i2c_smbus_data *data)
- {
- struct i2c_smbus_ioctl_data args;
- args.read_write = read_write;
- args.command = command;
- args.size = size;
- args.data = data;
- return ioctl(file,I2C_SMBUS,&args);
- }
- static inline __s32 i2c_smbus_write_quick(int file, __u8 value)
- {
- return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL);
- }
- static inline __s32 i2c_smbus_read_byte(int file)
- {
- union i2c_smbus_data data;
- if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data))
- return -1;
- else
- return 0x0FF & data.byte;
- }
- static inline __s32 i2c_smbus_write_byte(int file, __u8 value)
- {
- return i2c_smbus_access(file,I2C_SMBUS_WRITE,value,
- I2C_SMBUS_BYTE,NULL);
- }
- static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command)
- {
- union i2c_smbus_data data;
- if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
- I2C_SMBUS_BYTE_DATA,&data))
- return -1;
- else
- return 0x0FF & data.byte;
- }
- static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command,
- __u8 value)
- {
- union i2c_smbus_data data;
- data.byte = value;
- return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
- I2C_SMBUS_BYTE_DATA, &data);
- }
- static inline __s32 i2c_smbus_read_word_data(int file, __u8 command)
- {
- union i2c_smbus_data data;
- if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
- I2C_SMBUS_WORD_DATA,&data))
- return -1;
- else
- return 0x0FFFF & data.word;
- }
- static inline __s32 i2c_smbus_write_word_data(int file, __u8 command,
- __u16 value)
- {
- union i2c_smbus_data data;
- data.word = value;
- return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
- I2C_SMBUS_WORD_DATA, &data);
- }
- static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value)
- {
- union i2c_smbus_data data;
- data.word = value;
- if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
- I2C_SMBUS_PROC_CALL,&data))
- return -1;
- else
- return 0x0FFFF & data.word;
- }
- static inline __s32 i2c_smbus_read_block_data(int file, __u8 command,
- __u8 *values)
- {
- union i2c_smbus_data data;
- int i;
- if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
- I2C_SMBUS_BLOCK_DATA,&data))
- return -1;
- else {
- for (i = 1; i <= data.block[0]; i++)
- values[i-1] = data.block[i];
- return data.block[0];
- }
- }
- static inline __s32 i2c_smbus_write_block_data(int file, __u8 command,
- __u8 length, const __u8 *values)
- {
- union i2c_smbus_data data;
- int i;
- if (length > 32)
- length = 32;
- for (i = 1; i <= length; i++)
- data.block[i] = values[i-1];
- data.block[0] = length;
- return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
- I2C_SMBUS_BLOCK_DATA, &data);
- }
- static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command,
- __u8 length, __u8 *values)
- {
- union i2c_smbus_data data;
- int i;
- if (length > 32)
- length = 32;
- data.block[0] = length;
- if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
- length == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN :
- I2C_SMBUS_I2C_BLOCK_DATA,&data))
- return -1;
- else {
- for (i = 1; i <= data.block[0]; i++)
- values[i-1] = data.block[i];
- return data.block[0];
- }
- }
- static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command,
- __u8 length,
- const __u8 *values)
- {
- union i2c_smbus_data data;
- int i;
- if (length > 32)
- length = 32;
- for (i = 1; i <= length; i++)
- data.block[i] = values[i-1];
- data.block[0] = length;
- return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
- I2C_SMBUS_I2C_BLOCK_BROKEN, &data);
- }
- static inline __s32 i2c_smbus_block_process_call(int file, __u8 command,
- __u8 length, __u8 *values)
- {
- union i2c_smbus_data data;
- int i;
- if (length > 32)
- length = 32;
- for (i = 1; i <= length; i++)
- data.block[i] = values[i-1];
- data.block[0] = length;
- if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
- I2C_SMBUS_BLOCK_PROC_CALL,&data))
- return -1;
- else {
- for (i = 1; i <= data.block[0]; i++)
- values[i-1] = data.block[i];
- return data.block[0];
- }
- }
- #endif
|