2 smbus.c - SMBus level access helper functions
4 Copyright (C) 1995-97 Simon G. Vogl
5 Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
6 Copyright (C) 2012 Jean Delvare <khali@linux-fr.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 #include "smbus.h" // NB: Path changed!
26 #include <sys/ioctl.h>
27 #include <linux/types.h>
28 #include <linux/i2c.h>
29 #include <linux/i2c-dev.h>
31 //NB: Added by John Burns
36 /* Compatibility defines */
37 #ifndef I2C_SMBUS_I2C_BLOCK_BROKEN
38 #define I2C_SMBUS_I2C_BLOCK_BROKEN I2C_SMBUS_I2C_BLOCK_DATA
40 #ifndef I2C_FUNC_SMBUS_PEC
41 #define I2C_FUNC_SMBUS_PEC I2C_FUNC_SMBUS_HWPEC_CALC
44 __s32 i2c_smbus_access(int file, char read_write, __u8 command,
45 int size, union i2c_smbus_data *data)
47 struct i2c_smbus_ioctl_data args;
50 args.read_write = read_write;
51 args.command = command;
55 err = ioctl(file, I2C_SMBUS, &args);
62 __s32 i2c_smbus_write_quick(int file, __u8 value)
64 return i2c_smbus_access(file, value, 0, I2C_SMBUS_QUICK, NULL);
67 __s32 i2c_smbus_read_byte(int file)
69 union i2c_smbus_data data;
72 err = i2c_smbus_access(file, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data);
76 return 0x0FF & data.byte;
79 __s32 i2c_smbus_write_byte(int file, __u8 value)
81 return i2c_smbus_access(file, I2C_SMBUS_WRITE, value,
82 I2C_SMBUS_BYTE, NULL);
85 __s32 i2c_smbus_read_byte_data(int file, __u8 command)
87 union i2c_smbus_data data;
90 err = i2c_smbus_access(file, I2C_SMBUS_READ, command,
91 I2C_SMBUS_BYTE_DATA, &data);
95 return 0x0FF & data.byte;
98 __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value)
100 union i2c_smbus_data data;
102 return i2c_smbus_access(file, I2C_SMBUS_WRITE, command,
103 I2C_SMBUS_BYTE_DATA, &data);
106 __s32 i2c_smbus_read_word_data(int file, __u8 command)
108 union i2c_smbus_data data;
111 err = i2c_smbus_access(file, I2C_SMBUS_READ, command,
112 I2C_SMBUS_WORD_DATA, &data);
116 return 0x0FFFF & data.word;
119 __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value)
121 union i2c_smbus_data data;
123 return i2c_smbus_access(file, I2C_SMBUS_WRITE, command,
124 I2C_SMBUS_WORD_DATA, &data);
127 __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value)
129 union i2c_smbus_data data;
131 if (i2c_smbus_access(file, I2C_SMBUS_WRITE, command,
132 I2C_SMBUS_PROC_CALL, &data))
135 return 0x0FFFF & data.word;
138 /* Returns the number of read bytes */
139 __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values)
141 union i2c_smbus_data data;
144 err = i2c_smbus_access(file, I2C_SMBUS_READ, command,
145 I2C_SMBUS_BLOCK_DATA, &data);
149 for (i = 1; i <= data.block[0]; i++)
150 values[i-1] = data.block[i];
151 return data.block[0];
154 __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length,
157 union i2c_smbus_data data;
159 if (length > I2C_SMBUS_BLOCK_MAX)
160 length = I2C_SMBUS_BLOCK_MAX;
161 for (i = 1; i <= length; i++)
162 data.block[i] = values[i-1];
163 data.block[0] = length;
164 return i2c_smbus_access(file, I2C_SMBUS_WRITE, command,
165 I2C_SMBUS_BLOCK_DATA, &data);
168 /* Returns the number of read bytes */
169 /* Until kernel 2.6.22, the length is hardcoded to 32 bytes. If you
170 ask for less than 32 bytes, your code will only work with kernels
172 __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command, __u8 length,
175 union i2c_smbus_data data;
178 if (length > I2C_SMBUS_BLOCK_MAX)
179 length = I2C_SMBUS_BLOCK_MAX;
180 data.block[0] = length;
182 err = i2c_smbus_access(file, I2C_SMBUS_READ, command,
183 length == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN :
184 I2C_SMBUS_I2C_BLOCK_DATA, &data);
188 for (i = 1; i <= data.block[0]; i++)
189 values[i-1] = data.block[i];
190 return data.block[0];
193 __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command, __u8 length,
196 union i2c_smbus_data data;
198 if (length > I2C_SMBUS_BLOCK_MAX)
199 length = I2C_SMBUS_BLOCK_MAX;
200 for (i = 1; i <= length; i++)
201 data.block[i] = values[i-1];
202 data.block[0] = length;
203 return i2c_smbus_access(file, I2C_SMBUS_WRITE, command,
204 I2C_SMBUS_I2C_BLOCK_BROKEN, &data);
207 /* Returns the number of read bytes */
208 __s32 i2c_smbus_block_process_call(int file, __u8 command, __u8 length,
211 union i2c_smbus_data data;
214 if (length > I2C_SMBUS_BLOCK_MAX)
215 length = I2C_SMBUS_BLOCK_MAX;
216 for (i = 1; i <= length; i++)
217 data.block[i] = values[i-1];
218 data.block[0] = length;
220 err = i2c_smbus_access(file, I2C_SMBUS_WRITE, command,
221 I2C_SMBUS_BLOCK_PROC_CALL, &data);
225 for (i = 1; i <= data.block[0]; i++)
226 values[i-1] = data.block[i];
227 return data.block[0];