Ben Pai | 44cee31 | 2020-03-16 15:24:46 +0800 | [diff] [blame] | 1 | #include <linux/i2c-dev.h> |
| 2 | #include <linux/i2c.h> |
| 3 | #include <linux/types.h> |
| 4 | #include <stddef.h> |
| 5 | #include <sys/ioctl.h> |
| 6 | |
| 7 | /* |
| 8 | * Data for SMBus Messages |
| 9 | */ |
| 10 | #define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */ |
| 11 | #define I2C_DATA_MAX 256 |
| 12 | |
| 13 | /* smbus_access read or write markers */ |
| 14 | #define I2C_SMBUS_READ 1 |
| 15 | #define I2C_SMBUS_WRITE 0 |
| 16 | |
| 17 | /* SMBus transaction types (size parameter in the above functions) |
| 18 | Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */ |
| 19 | #define I2C_SMBUS_BYTE_DATA 2 |
| 20 | |
| 21 | /* NOTE: Slave address is 7 or 10 bits, but 10-bit addresses |
| 22 | * are NOT supported! (due to code brokenness) |
| 23 | */ |
| 24 | #define I2C_SLAVE 0x0703 /* Use this slave address */ |
| 25 | #define I2C_SLAVE_FORCE 0x0706 |
| 26 | #define I2C_SMBUS 0x0720 /* SMBus transfer */ |
| 27 | |
| 28 | static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command, |
| 29 | int size, union i2c_smbus_data* data) |
| 30 | { |
| 31 | struct i2c_smbus_ioctl_data args; |
| 32 | |
| 33 | args.read_write = read_write; |
| 34 | args.command = command; |
| 35 | args.size = size; |
| 36 | args.data = data; |
| 37 | return ioctl(file, I2C_SMBUS, &args); |
| 38 | } |
| 39 | |
| 40 | static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command, |
| 41 | __u8 value) |
| 42 | { |
| 43 | union i2c_smbus_data data; |
| 44 | data.byte = value; |
| 45 | return i2c_smbus_access(file, I2C_SMBUS_WRITE, command, I2C_SMBUS_BYTE_DATA, |
| 46 | &data); |
| 47 | } |
| 48 | |
| 49 | static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command) |
| 50 | { |
| 51 | union i2c_smbus_data data; |
| 52 | if (i2c_smbus_access(file, I2C_SMBUS_READ, command, I2C_SMBUS_BYTE_DATA, |
| 53 | &data)) |
| 54 | return -1; |
| 55 | else |
| 56 | return 0x0FF & data.byte; |
| 57 | } |