File Coverage

i2c-dev.h
Criterion Covered Total %
statement 0 78 0.0
branch 0 38 0.0
condition n/a
subroutine n/a
pod n/a
total 0 116 0.0


line stmt bran cond sub pod time code
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
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
29             #include
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 0           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 0           args.read_write = read_write;
167 0           args.command = command;
168 0           args.size = size;
169 0           args.data = data;
170 0           return ioctl(file,I2C_SMBUS,&args);
171             }
172            
173            
174 0           static inline __s32 i2c_smbus_write_quick(int file, __u8 value)
175             {
176 0           return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL);
177             }
178            
179 0           static inline __s32 i2c_smbus_read_byte(int file)
180             {
181             union i2c_smbus_data data;
182 0 0         if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data))
183 0           return -1;
184             else
185 0           return 0x0FF & data.byte;
186             }
187            
188 0           static inline __s32 i2c_smbus_write_byte(int file, __u8 value)
189             {
190 0           return i2c_smbus_access(file,I2C_SMBUS_WRITE,value,
191             I2C_SMBUS_BYTE,NULL);
192             }
193            
194 0           static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command)
195             {
196             union i2c_smbus_data data;
197 0 0         if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
198             I2C_SMBUS_BYTE_DATA,&data))
199 0           return -1;
200             else
201 0           return 0x0FF & data.byte;
202             }
203            
204 0           static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command,
205             __u8 value)
206             {
207             union i2c_smbus_data data;
208 0           data.byte = value;
209 0           return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
210             I2C_SMBUS_BYTE_DATA, &data);
211             }
212            
213 0           static inline __s32 i2c_smbus_read_word_data(int file, __u8 command)
214             {
215             union i2c_smbus_data data;
216 0 0         if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
217             I2C_SMBUS_WORD_DATA,&data))
218 0           return -1;
219             else
220 0           return 0x0FFFF & data.word;
221             }
222            
223 0           static inline __s32 i2c_smbus_write_word_data(int file, __u8 command,
224             __u16 value)
225             {
226             union i2c_smbus_data data;
227 0           data.word = value;
228 0           return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
229             I2C_SMBUS_WORD_DATA, &data);
230             }
231            
232 0           static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value)
233             {
234             union i2c_smbus_data data;
235 0           data.word = value;
236 0 0         if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
237             I2C_SMBUS_PROC_CALL,&data))
238 0           return -1;
239             else
240 0           return 0x0FFFF & data.word;
241             }
242            
243            
244             /* Returns the number of read bytes */
245 0           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 0 0         if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
251             I2C_SMBUS_BLOCK_DATA,&data))
252 0           return -1;
253             else {
254 0 0         for (i = 1; i <= data.block[0]; i++)
255 0           values[i-1] = data.block[i];
256 0           return data.block[0];
257             }
258             }
259            
260 0           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 0 0         if (length > 32)
266 0           length = 32;
267 0 0         for (i = 1; i <= length; i++)
268 0           data.block[i] = values[i-1];
269 0           data.block[0] = length;
270 0           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 0           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 0 0         if (length > 32)
285 0           length = 32;
286 0           data.block[0] = length;
287 0 0         if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
    0          
288             length == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN :
289             I2C_SMBUS_I2C_BLOCK_DATA,&data))
290 0           return -1;
291             else {
292 0 0         for (i = 1; i <= data.block[0]; i++)
293 0           values[i-1] = data.block[i];
294 0           return data.block[0];
295             }
296             }
297            
298 0           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 0 0         if (length > 32)
304 0           length = 32;
305 0 0         for (i = 1; i <= length; i++)
306 0           data.block[i] = values[i-1];
307 0           data.block[0] = length;
308 0           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 0           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 0 0         if (length > 32)
319 0           length = 32;
320 0 0         for (i = 1; i <= length; i++)
321 0           data.block[i] = values[i-1];
322 0           data.block[0] = length;
323 0 0         if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
324             I2C_SMBUS_BLOCK_PROC_CALL,&data))
325 0           return -1;
326             else {
327 0 0         for (i = 1; i <= data.block[0]; i++)
328 0           values[i-1] = data.block[i];
329 0           return data.block[0];
330             }
331             }
332            
333 0           static inline __s32 i2c_smbus_check_device(int file, int addr) {
334 0 0         if (ioctl(file, I2C_SLAVE, addr) < 0) {
335 0           return (errno == EBUSY) ? 1 : 0;
336             } else {
337 0           int res = i2c_smbus_write_quick(file, I2C_SMBUS_WRITE);
338 0           return (res < 0) ? 0 : 1;
339             }
340             }
341            
342             #endif /* LIB_I2CDEV_H */