| 1 | /* |
|---|
| 2 | smbus.h - SMBus level access helper functions |
|---|
| 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 LIB_I2C_SMBUS_H |
|---|
| 24 | #define LIB_I2C_SMBUS_H |
|---|
| 25 | |
|---|
| 26 | #include <sys/ioctl.h> |
|---|
| 27 | #include <linux/types.h> |
|---|
| 28 | #include <linux/i2c.h> |
|---|
| 29 | #include <linux/i2c-dev.h> |
|---|
| 30 | |
|---|
| 31 | /* Compatibility defines */ |
|---|
| 32 | #ifndef I2C_SMBUS_I2C_BLOCK_BROKEN |
|---|
| 33 | #define I2C_SMBUS_I2C_BLOCK_BROKEN I2C_SMBUS_I2C_BLOCK_DATA |
|---|
| 34 | #endif |
|---|
| 35 | #ifndef I2C_FUNC_SMBUS_PEC |
|---|
| 36 | #define I2C_FUNC_SMBUS_PEC I2C_FUNC_SMBUS_HWPEC_CALC |
|---|
| 37 | #endif |
|---|
| 38 | |
|---|
| 39 | static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command, |
|---|
| 40 | int size, union i2c_smbus_data *data) |
|---|
| 41 | { |
|---|
| 42 | struct i2c_smbus_ioctl_data args; |
|---|
| 43 | |
|---|
| 44 | args.read_write = read_write; |
|---|
| 45 | args.command = command; |
|---|
| 46 | args.size = size; |
|---|
| 47 | args.data = data; |
|---|
| 48 | return ioctl(file, I2C_SMBUS, &args); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | static inline __s32 i2c_smbus_write_quick(int file, __u8 value) |
|---|
| 53 | { |
|---|
| 54 | return i2c_smbus_access(file, value, 0, I2C_SMBUS_QUICK, NULL); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | static inline __s32 i2c_smbus_read_byte(int file) |
|---|
| 58 | { |
|---|
| 59 | union i2c_smbus_data data; |
|---|
| 60 | if (i2c_smbus_access(file, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data)) |
|---|
| 61 | return -1; |
|---|
| 62 | else |
|---|
| 63 | return 0x0FF & data.byte; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | static inline __s32 i2c_smbus_write_byte(int file, __u8 value) |
|---|
| 67 | { |
|---|
| 68 | return i2c_smbus_access(file, I2C_SMBUS_WRITE, value, |
|---|
| 69 | I2C_SMBUS_BYTE, NULL); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command) |
|---|
| 73 | { |
|---|
| 74 | union i2c_smbus_data data; |
|---|
| 75 | if (i2c_smbus_access(file, I2C_SMBUS_READ, command, |
|---|
| 76 | I2C_SMBUS_BYTE_DATA, &data)) |
|---|
| 77 | return -1; |
|---|
| 78 | else |
|---|
| 79 | return 0x0FF & data.byte; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command, |
|---|
| 83 | __u8 value) |
|---|
| 84 | { |
|---|
| 85 | union i2c_smbus_data data; |
|---|
| 86 | data.byte = value; |
|---|
| 87 | return i2c_smbus_access(file, I2C_SMBUS_WRITE, command, |
|---|
| 88 | I2C_SMBUS_BYTE_DATA, &data); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | static inline __s32 i2c_smbus_read_word_data(int file, __u8 command) |
|---|
| 92 | { |
|---|
| 93 | union i2c_smbus_data data; |
|---|
| 94 | if (i2c_smbus_access(file, I2C_SMBUS_READ, command, |
|---|
| 95 | I2C_SMBUS_WORD_DATA, &data)) |
|---|
| 96 | return -1; |
|---|
| 97 | else |
|---|
| 98 | return 0x0FFFF & data.word; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | static inline __s32 i2c_smbus_write_word_data(int file, __u8 command, |
|---|
| 102 | __u16 value) |
|---|
| 103 | { |
|---|
| 104 | union i2c_smbus_data data; |
|---|
| 105 | data.word = value; |
|---|
| 106 | return i2c_smbus_access(file, I2C_SMBUS_WRITE, command, |
|---|
| 107 | I2C_SMBUS_WORD_DATA, &data); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value) |
|---|
| 111 | { |
|---|
| 112 | union i2c_smbus_data data; |
|---|
| 113 | data.word = value; |
|---|
| 114 | if (i2c_smbus_access(file, I2C_SMBUS_WRITE, command, |
|---|
| 115 | I2C_SMBUS_PROC_CALL, &data)) |
|---|
| 116 | return -1; |
|---|
| 117 | else |
|---|
| 118 | return 0x0FFFF & data.word; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | /* Returns the number of read bytes */ |
|---|
| 123 | static inline __s32 i2c_smbus_read_block_data(int file, __u8 command, |
|---|
| 124 | __u8 *values) |
|---|
| 125 | { |
|---|
| 126 | union i2c_smbus_data data; |
|---|
| 127 | int i; |
|---|
| 128 | if (i2c_smbus_access(file, I2C_SMBUS_READ, command, |
|---|
| 129 | I2C_SMBUS_BLOCK_DATA, &data)) |
|---|
| 130 | return -1; |
|---|
| 131 | else { |
|---|
| 132 | for (i = 1; i <= data.block[0]; i++) |
|---|
| 133 | values[i-1] = data.block[i]; |
|---|
| 134 | return data.block[0]; |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | static inline __s32 i2c_smbus_write_block_data(int file, __u8 command, |
|---|
| 139 | __u8 length, const __u8 *values) |
|---|
| 140 | { |
|---|
| 141 | union i2c_smbus_data data; |
|---|
| 142 | int i; |
|---|
| 143 | if (length > 32) |
|---|
| 144 | length = 32; |
|---|
| 145 | for (i = 1; i <= length; i++) |
|---|
| 146 | data.block[i] = values[i-1]; |
|---|
| 147 | data.block[0] = length; |
|---|
| 148 | return i2c_smbus_access(file, I2C_SMBUS_WRITE, command, |
|---|
| 149 | I2C_SMBUS_BLOCK_DATA, &data); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | /* Returns the number of read bytes */ |
|---|
| 153 | /* Until kernel 2.6.22, the length is hardcoded to 32 bytes. If you |
|---|
| 154 | ask for less than 32 bytes, your code will only work with kernels |
|---|
| 155 | 2.6.23 and later. */ |
|---|
| 156 | static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command, |
|---|
| 157 | __u8 length, __u8 *values) |
|---|
| 158 | { |
|---|
| 159 | union i2c_smbus_data data; |
|---|
| 160 | int i; |
|---|
| 161 | |
|---|
| 162 | if (length > 32) |
|---|
| 163 | length = 32; |
|---|
| 164 | data.block[0] = length; |
|---|
| 165 | if (i2c_smbus_access(file, I2C_SMBUS_READ, command, |
|---|
| 166 | length == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN : |
|---|
| 167 | I2C_SMBUS_I2C_BLOCK_DATA, &data)) |
|---|
| 168 | return -1; |
|---|
| 169 | else { |
|---|
| 170 | for (i = 1; i <= data.block[0]; i++) |
|---|
| 171 | values[i-1] = data.block[i]; |
|---|
| 172 | return data.block[0]; |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command, |
|---|
| 177 | __u8 length, |
|---|
| 178 | const __u8 *values) |
|---|
| 179 | { |
|---|
| 180 | union i2c_smbus_data data; |
|---|
| 181 | int i; |
|---|
| 182 | if (length > 32) |
|---|
| 183 | length = 32; |
|---|
| 184 | for (i = 1; i <= length; i++) |
|---|
| 185 | data.block[i] = values[i-1]; |
|---|
| 186 | data.block[0] = length; |
|---|
| 187 | return i2c_smbus_access(file, I2C_SMBUS_WRITE, command, |
|---|
| 188 | I2C_SMBUS_I2C_BLOCK_BROKEN, &data); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | /* Returns the number of read bytes */ |
|---|
| 192 | static inline __s32 i2c_smbus_block_process_call(int file, __u8 command, |
|---|
| 193 | __u8 length, __u8 *values) |
|---|
| 194 | { |
|---|
| 195 | union i2c_smbus_data data; |
|---|
| 196 | int i; |
|---|
| 197 | if (length > 32) |
|---|
| 198 | length = 32; |
|---|
| 199 | for (i = 1; i <= length; i++) |
|---|
| 200 | data.block[i] = values[i-1]; |
|---|
| 201 | data.block[0] = length; |
|---|
| 202 | if (i2c_smbus_access(file, I2C_SMBUS_WRITE, command, |
|---|
| 203 | I2C_SMBUS_BLOCK_PROC_CALL, &data)) |
|---|
| 204 | return -1; |
|---|
| 205 | else { |
|---|
| 206 | for (i = 1; i <= data.block[0]; i++) |
|---|
| 207 | values[i-1] = data.block[i]; |
|---|
| 208 | return data.block[0]; |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | #endif /* LIB_I2C_SMBUS_H */ |
|---|