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