Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 335 lines (284 sloc) 10.513 kb
59a62e2 adding i2c-dev.h for convenience
daveriess authored
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 /* $Id: i2c-dev.h 5361 2008-10-19 09:47:02Z khali $ */
24
25 #ifndef LIB_I2CDEV_H
26 #define LIB_I2CDEV_H
27
28 #include <linux/types.h>
29 #include <sys/ioctl.h>
30
31
32 /* -- i2c.h -- */
33
34
35 /*
36 * I2C Message - used for pure i2c transaction, also from /dev interface
37 */
38 struct i2c_msg {
39 __u16 addr; /* slave address */
40 unsigned short flags;
41 #define I2C_M_TEN 0x10 /* we have a ten bit chip address */
42 #define I2C_M_RD 0x01
43 #define I2C_M_NOSTART 0x4000
44 #define I2C_M_REV_DIR_ADDR 0x2000
45 #define I2C_M_IGNORE_NAK 0x1000
46 #define I2C_M_NO_RD_ACK 0x0800
47 short len; /* msg length */
48 char *buf; /* pointer to msg data */
49 };
50
51 /* To determine what functionality is present */
52
53 #define I2C_FUNC_I2C 0x00000001
54 #define I2C_FUNC_10BIT_ADDR 0x00000002
55 #define I2C_FUNC_PROTOCOL_MANGLING 0x00000004 /* I2C_M_{REV_DIR_ADDR,NOSTART,..} */
56 #define I2C_FUNC_SMBUS_PEC 0x00000008
57 #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000 /* SMBus 2.0 */
58 #define I2C_FUNC_SMBUS_QUICK 0x00010000
59 #define I2C_FUNC_SMBUS_READ_BYTE 0x00020000
60 #define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000
61 #define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000
62 #define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000
63 #define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000
64 #define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000
65 #define I2C_FUNC_SMBUS_PROC_CALL 0x00800000
66 #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000
67 #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000
68 #define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000 /* I2C-like block xfer */
69 #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000 /* w/ 1-byte reg. addr. */
70
71 #define I2C_FUNC_SMBUS_BYTE (I2C_FUNC_SMBUS_READ_BYTE | \
72 I2C_FUNC_SMBUS_WRITE_BYTE)
73 #define I2C_FUNC_SMBUS_BYTE_DATA (I2C_FUNC_SMBUS_READ_BYTE_DATA | \
74 I2C_FUNC_SMBUS_WRITE_BYTE_DATA)
75 #define I2C_FUNC_SMBUS_WORD_DATA (I2C_FUNC_SMBUS_READ_WORD_DATA | \
76 I2C_FUNC_SMBUS_WRITE_WORD_DATA)
77 #define I2C_FUNC_SMBUS_BLOCK_DATA (I2C_FUNC_SMBUS_READ_BLOCK_DATA | \
78 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)
79 #define I2C_FUNC_SMBUS_I2C_BLOCK (I2C_FUNC_SMBUS_READ_I2C_BLOCK | \
80 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)
81
82 /* Old name, for compatibility */
83 #define I2C_FUNC_SMBUS_HWPEC_CALC I2C_FUNC_SMBUS_PEC
84
85 /*
86 * Data for SMBus Messages
87 */
88 #define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */
89 #define I2C_SMBUS_I2C_BLOCK_MAX 32 /* Not specified but we use same structure */
90 union i2c_smbus_data {
91 __u8 byte;
92 __u16 word;
93 __u8 block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */
94 /* and one more for PEC */
95 };
96
97 /* smbus_access read or write markers */
98 #define I2C_SMBUS_READ 1
99 #define I2C_SMBUS_WRITE 0
100
101 /* SMBus transaction types (size parameter in the above functions)
102 Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */
103 #define I2C_SMBUS_QUICK 0
104 #define I2C_SMBUS_BYTE 1
105 #define I2C_SMBUS_BYTE_DATA 2
106 #define I2C_SMBUS_WORD_DATA 3
107 #define I2C_SMBUS_PROC_CALL 4
108 #define I2C_SMBUS_BLOCK_DATA 5
109 #define I2C_SMBUS_I2C_BLOCK_BROKEN 6
110 #define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0 */
111 #define I2C_SMBUS_I2C_BLOCK_DATA 8
112
113
114 /* ----- commands for the ioctl like i2c_command call:
115 * note that additional calls are defined in the algorithm and hw
116 * dependent layers - these can be listed here, or see the
117 * corresponding header files.
118 */
119 /* -> bit-adapter specific ioctls */
120 #define I2C_RETRIES 0x0701 /* number of times a device address */
121 /* should be polled when not */
122 /* acknowledging */
123 #define I2C_TIMEOUT 0x0702 /* set timeout - call with int */
124
125
126 /* this is for i2c-dev.c */
127 #define I2C_SLAVE 0x0703 /* Change slave address */
128 /* Attn.: Slave address is 7 or 10 bits */
129 #define I2C_SLAVE_FORCE 0x0706 /* Change slave address */
130 /* Attn.: Slave address is 7 or 10 bits */
131 /* This changes the address, even if it */
132 /* is already taken! */
133 #define I2C_TENBIT 0x0704 /* 0 for 7 bit addrs, != 0 for 10 bit */
134
135 #define I2C_FUNCS 0x0705 /* Get the adapter functionality */
136 #define I2C_RDWR 0x0707 /* Combined R/W transfer (one stop only)*/
137 #define I2C_PEC 0x0708 /* != 0 for SMBus PEC */
138
139 #define I2C_SMBUS 0x0720 /* SMBus-level access */
140
141 /* -- i2c.h -- */
142
143
144 /* Note: 10-bit addresses are NOT supported! */
145
146 /* This is the structure as used in the I2C_SMBUS ioctl call */
147 struct i2c_smbus_ioctl_data {
148 char read_write;
149 __u8 command;
150 int size;
151 union i2c_smbus_data *data;
152 };
153
154 /* This is the structure as used in the I2C_RDWR ioctl call */
155 struct i2c_rdwr_ioctl_data {
156 struct i2c_msg *msgs; /* pointers to i2c_msgs */
157 int nmsgs; /* number of i2c_msgs */
158 };
159
160
161 static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,
162 int size, union i2c_smbus_data *data)
163 {
164 struct i2c_smbus_ioctl_data args;
165
166 args.read_write = read_write;
167 args.command = command;
168 args.size = size;
169 args.data = data;
170 return ioctl(file,I2C_SMBUS,&args);
171 }
172
173
174 static inline __s32 i2c_smbus_write_quick(int file, __u8 value)
175 {
176 return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL);
177 }
178
179 static inline __s32 i2c_smbus_read_byte(int file)
180 {
181 union i2c_smbus_data data;
182 if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data))
183 return -1;
184 else
185 return 0x0FF & data.byte;
186 }
187
188 static inline __s32 i2c_smbus_write_byte(int file, __u8 value)
189 {
190 return i2c_smbus_access(file,I2C_SMBUS_WRITE,value,
191 I2C_SMBUS_BYTE,NULL);
192 }
193
194 static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command)
195 {
196 union i2c_smbus_data data;
197 if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
198 I2C_SMBUS_BYTE_DATA,&data))
199 return -1;
200 else
201 return 0x0FF & data.byte;
202 }
203
204 static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command,
205 __u8 value)
206 {
207 union i2c_smbus_data data;
208 data.byte = value;
209 return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
210 I2C_SMBUS_BYTE_DATA, &data);
211 }
212
213 static inline __s32 i2c_smbus_read_word_data(int file, __u8 command)
214 {
215 union i2c_smbus_data data;
216 if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
217 I2C_SMBUS_WORD_DATA,&data))
218 return -1;
219 else
220 return 0x0FFFF & data.word;
221 }
222
223 static inline __s32 i2c_smbus_write_word_data(int file, __u8 command,
224 __u16 value)
225 {
226 union i2c_smbus_data data;
227 data.word = value;
228 return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
229 I2C_SMBUS_WORD_DATA, &data);
230 }
231
232 static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value)
233 {
234 union i2c_smbus_data data;
235 data.word = value;
236 if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
237 I2C_SMBUS_PROC_CALL,&data))
238 return -1;
239 else
240 return 0x0FFFF & data.word;
241 }
242
243
244 /* Returns the number of read bytes */
245 static inline __s32 i2c_smbus_read_block_data(int file, __u8 command,
246 __u8 *values)
247 {
248 union i2c_smbus_data data;
249 int i;
250 if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
251 I2C_SMBUS_BLOCK_DATA,&data))
252 return -1;
253 else {
254 for (i = 1; i <= data.block[0]; i++)
255 values[i-1] = data.block[i];
256 return data.block[0];
257 }
258 }
259
260 static inline __s32 i2c_smbus_write_block_data(int file, __u8 command,
261 __u8 length, __u8 *values)
262 {
263 union i2c_smbus_data data;
264 int i;
265 if (length > 32)
266 length = 32;
267 for (i = 1; i <= length; i++)
268 data.block[i] = values[i-1];
269 data.block[0] = length;
270 return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
271 I2C_SMBUS_BLOCK_DATA, &data);
272 }
273
274 /* Returns the number of read bytes */
275 /* Until kernel 2.6.22, the length is hardcoded to 32 bytes. If you
276 ask for less than 32 bytes, your code will only work with kernels
277 2.6.23 and later. */
278 static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command,
279 __u8 length, __u8 *values)
280 {
281 union i2c_smbus_data data;
282 int i;
283
284 if (length > 32)
285 length = 32;
286 data.block[0] = length;
287 if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
288 length == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN :
289 I2C_SMBUS_I2C_BLOCK_DATA,&data))
290 return -1;
291 else {
292 for (i = 1; i <= data.block[0]; i++)
293 values[i-1] = data.block[i];
294 return data.block[0];
295 }
296 }
297
298 static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command,
299 __u8 length, __u8 *values)
300 {
301 union i2c_smbus_data data;
302 int i;
303 if (length > 32)
304 length = 32;
305 for (i = 1; i <= length; i++)
306 data.block[i] = values[i-1];
307 data.block[0] = length;
308 return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
309 I2C_SMBUS_I2C_BLOCK_BROKEN, &data);
310 }
311
312 /* Returns the number of read bytes */
313 static inline __s32 i2c_smbus_block_process_call(int file, __u8 command,
314 __u8 length, __u8 *values)
315 {
316 union i2c_smbus_data data;
317 int i;
318 if (length > 32)
319 length = 32;
320 for (i = 1; i <= length; i++)
321 data.block[i] = values[i-1];
322 data.block[0] = length;
323 if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
324 I2C_SMBUS_BLOCK_PROC_CALL,&data))
325 return -1;
326 else {
327 for (i = 1; i <= data.block[0]; i++)
328 values[i-1] = data.block[i];
329 return data.block[0];
330 }
331 }
332
333
334 #endif /* LIB_I2CDEV_H */
Something went wrong with that request. Please try again.