3 * Copyright 2010 - 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>
6 * Faculty of Computer Science, Mathematics and Natural Sciences,
7 * Leipzig University of Applied Sciences (HTWK Leipzig)
11 * i2c-dev.h - i2c-bus driver, char device interface
13 * Copyright (C) 1995-97 Simon G. Vogl
14 * Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
35 #include <linux/types.h>
36 #include <sys/ioctl.h>
39 * I2C Message - used for pure i2c transaction, also from /dev interface
42 __u16 addr
; /* slave address */
44 #define I2C_M_TEN 0x10 /* we have a ten bit chip address */
46 #define I2C_M_NOSTART 0x4000
47 #define I2C_M_REV_DIR_ADDR 0x2000
48 #define I2C_M_IGNORE_NAK 0x1000
49 #define I2C_M_NO_RD_ACK 0x0800
50 short len
; /* msg length */
51 char *buf
; /* pointer to msg data */
54 /* To determine what functionality is present */
56 #define I2C_FUNC_I2C 0x00000001
57 #define I2C_FUNC_10BIT_ADDR 0x00000002
58 #define I2C_FUNC_PROTOCOL_MANGLING 0x00000004 /* I2C_M_{REV_DIR_ADDR,NOSTART,..} */
59 #define I2C_FUNC_SMBUS_PEC 0x00000008
60 #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000 /* SMBus 2.0f */
61 #define I2C_FUNC_SMBUS_QUICK 0x00010000
62 #define I2C_FUNC_SMBUS_READ_BYTE 0x00020000
63 #define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000
64 #define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000
65 #define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000
66 #define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000
67 #define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000
68 #define I2C_FUNC_SMBUS_PROC_CALL 0x00800000
69 #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000
70 #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000
71 #define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000 /* I2C-like block xfer */
72 #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000 /* w/ 1-byte reg. addr. */
74 #define I2C_FUNC_SMBUS_BYTE (I2C_FUNC_SMBUS_READ_BYTE | \
75 I2C_FUNC_SMBUS_WRITE_BYTE)
76 #define I2C_FUNC_SMBUS_BYTE_DATA (I2C_FUNC_SMBUS_READ_BYTE_DATA | \
77 I2C_FUNC_SMBUS_WRITE_BYTE_DATA)
78 #define I2C_FUNC_SMBUS_WORD_DATA (I2C_FUNC_SMBUS_READ_WORD_DATA | \
79 I2C_FUNC_SMBUS_WRITE_WORD_DATA)
80 #define I2C_FUNC_SMBUS_BLOCK_DATA (I2C_FUNC_SMBUS_READ_BLOCK_DATA | \
81 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)
82 #define I2C_FUNC_SMBUS_I2C_BLOCK (I2C_FUNC_SMBUS_READ_I2C_BLOCK | \
83 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)
85 /* Old name, for compatibility */
86 #define I2C_FUNC_SMBUS_HWPEC_CALC I2C_FUNC_SMBUS_PEC
89 * Data for SMBus Messages
91 #define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */
92 #define I2C_SMBUS_I2C_BLOCK_MAX 32 /* Not specified but we use same structure */
93 union i2c_smbus_data
{
96 __u8 block
[I2C_SMBUS_BLOCK_MAX
+ 2]; /* block[0] is used for length */
97 /* and one more for PEC */
100 /* smbus_access read or write markers */
101 #define I2C_SMBUS_READ 1
102 #define I2C_SMBUS_WRITE 0
104 /* SMBus transaction types (size parameter in the above functions)
105 Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */
106 #define I2C_SMBUS_QUICK 0
107 #define I2C_SMBUS_BYTE 1
108 #define I2C_SMBUS_BYTE_DATA 2
109 #define I2C_SMBUS_WORD_DATA 3
110 #define I2C_SMBUS_PROC_CALL 4
111 #define I2C_SMBUS_BLOCK_DATA 5
112 #define I2C_SMBUS_I2C_BLOCK_BROKEN 6
113 #define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0f */
114 #define I2C_SMBUS_I2C_BLOCK_DATA 8
117 /* ----- commands for the ioctl like i2c_command call:
118 * note that additional calls are defined in the algorithm and hw
119 * dependent layers - these can be listed here, or see the
120 * corresponding header files.
122 /* -> bit-adapter specific ioctls */
123 #define I2C_RETRIES 0x0701 /* number of times a device address */
124 /* should be polled when not */
126 #define I2C_TIMEOUT 0x0702 /* set timeout - call with int */
129 /* this is for i2c-dev.c */
130 #define I2C_SLAVE 0x0703 /* Change slave address */
131 /* Attn.: Slave address is 7 or 10 bits */
132 #define I2C_SLAVE_FORCE 0x0706 /* Change slave address */
133 /* Attn.: Slave address is 7 or 10 bits */
134 /* This changes the address, even if it */
135 /* is already taken! */
136 #define I2C_TENBIT 0x0704 /* 0 for 7 bit addrs, != 0 for 10 bit */
138 #define I2C_FUNCS 0x0705 /* Get the adapter functionality */
139 #define I2C_RDWR 0x0707 /* Combined R/W transfer (one stop only)*/
140 #define I2C_PEC 0x0708 /* != 0 for SMBus PEC */
142 #define I2C_SMBUS 0x0720 /* SMBus-level access */
146 /* Note: 10-bit addresses are NOT supported! */
148 /* This is the structure as used in the I2C_SMBUS ioctl call */
149 struct i2c_smbus_ioctl_data
{
153 union i2c_smbus_data
*data
;
156 /* This is the structure as used in the I2C_RDWR ioctl call */
157 struct i2c_rdwr_ioctl_data
{
158 struct i2c_msg
*msgs
; /* pointers to i2c_msgs */
159 int nmsgs
; /* number of i2c_msgs */
162 static inline __s32
i2c_smbus_access(int file
, char read_write
, __u8 command
,
163 int size
, union i2c_smbus_data
*data
)
165 struct i2c_smbus_ioctl_data args
;
167 args
.read_write
= read_write
;
168 args
.command
= command
;
172 return ioctl(file
,I2C_SMBUS
,&args
);
176 static inline __s32
i2c_smbus_write_quick(int file
, __u8 value
)
178 return i2c_smbus_access(file
,value
,0,I2C_SMBUS_QUICK
,NULL
);
181 static inline __s32
i2c_smbus_read_byte(int file
)
183 union i2c_smbus_data data
;
185 if (i2c_smbus_access(file
,I2C_SMBUS_READ
,0,I2C_SMBUS_BYTE
,&data
))
188 return 0x0FF & data
.byte
;
191 static inline __s32
i2c_smbus_write_byte(int file
, __u8 value
)
193 return i2c_smbus_access(file
,I2C_SMBUS_WRITE
,value
,
194 I2C_SMBUS_BYTE
,NULL
);
197 static inline __s32
i2c_smbus_read_byte_data(int file
, __u8 command
)
199 union i2c_smbus_data data
;
201 if (i2c_smbus_access(file
,I2C_SMBUS_READ
,command
,
202 I2C_SMBUS_BYTE_DATA
,&data
))
205 return 0x0FF & data
.byte
;
208 static inline __s32
i2c_smbus_write_byte_data(int file
, __u8 command
,
211 union i2c_smbus_data data
;
214 return i2c_smbus_access(file
,I2C_SMBUS_WRITE
,command
,
215 I2C_SMBUS_BYTE_DATA
, &data
);
218 static inline __s32
i2c_smbus_read_word_data(int file
, __u8 command
)
220 union i2c_smbus_data data
;
222 if (i2c_smbus_access(file
,I2C_SMBUS_READ
,command
,
223 I2C_SMBUS_WORD_DATA
,&data
))
226 return 0x0FFFF & data
.word
;
229 static inline __s32
i2c_smbus_write_word_data(int file
, __u8 command
,
232 union i2c_smbus_data data
;
235 return i2c_smbus_access(file
,I2C_SMBUS_WRITE
,command
,
236 I2C_SMBUS_WORD_DATA
, &data
);
239 static inline __s32
i2c_smbus_process_call(int file
, __u8 command
, __u16 value
)
241 union i2c_smbus_data data
;
244 if (i2c_smbus_access(file
,I2C_SMBUS_WRITE
,command
,
245 I2C_SMBUS_PROC_CALL
,&data
))
248 return 0x0FFFF & data
.word
;
252 /* Returns the number of read bytes */
253 static inline __s32
i2c_smbus_read_block_data(int file
, __u8 command
,
256 union i2c_smbus_data data
;
259 if (i2c_smbus_access(file
,I2C_SMBUS_READ
,command
,
260 I2C_SMBUS_BLOCK_DATA
,&data
))
263 for (i
= 1; i
<= data
.block
[0]; i
++)
264 values
[i
-1] = data
.block
[i
];
265 return data
.block
[0];
269 static inline __s32
i2c_smbus_write_block_data(int file
, __u8 command
,
270 __u8 length
, __u8
*values
)
272 union i2c_smbus_data data
;
278 for (i
= 1; i
<= length
; i
++)
279 data
.block
[i
] = values
[i
-1];
280 data
.block
[0] = length
;
281 return i2c_smbus_access(file
,I2C_SMBUS_WRITE
,command
,
282 I2C_SMBUS_BLOCK_DATA
, &data
);
285 /* Returns the number of read bytes */
286 /* Until kernel 2.6f.22, the length is hardcoded to 32 bytes. If you
287 ask for less than 32 bytes, your code will only work with kernels
288 2.6f.23 and later. */
289 static inline __s32
i2c_smbus_read_i2c_block_data(int file
, __u8 command
,
290 __u8 length
, __u8
*values
)
292 union i2c_smbus_data data
;
297 data
.block
[0] = length
;
299 if (i2c_smbus_access(file
,I2C_SMBUS_READ
,command
,
300 length
== 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN
:
301 I2C_SMBUS_I2C_BLOCK_DATA
,&data
))
304 for (i
= 1; i
<= data
.block
[0]; i
++)
305 values
[i
-1] = data
.block
[i
];
306 return data
.block
[0];
310 static inline __s32
i2c_smbus_write_i2c_block_data(int file
, __u8 command
,
311 __u8 length
, __u8
*values
)
313 union i2c_smbus_data data
;
319 for (i
= 1; i
<= length
; i
++)
320 data
.block
[i
] = values
[i
-1];
321 data
.block
[0] = length
;
323 return i2c_smbus_access(file
,I2C_SMBUS_WRITE
,command
,
324 I2C_SMBUS_I2C_BLOCK_BROKEN
, &data
);
327 static inline __s32
i2c_smbus_block_process_call(int file
, __u8 command
,
328 __u8 length
, __u8
*values
)
330 union i2c_smbus_data data
;
336 for (i
= 1; i
<= length
; i
++)
337 data
.block
[i
] = values
[i
-1];
338 data
.block
[0] = length
;
340 if (i2c_smbus_access(file
,I2C_SMBUS_WRITE
,command
,
341 I2C_SMBUS_BLOCK_PROC_CALL
,&data
))
344 for (i
= 1; i
<= data
.block
[0]; i
++)
345 values
[i
-1] = data
.block
[i
];
346 return data
.block
[0];