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