1 Usually, i2c devices are controlled by a kernel driver. But it is also
2 possible to access all devices on an adapter from userspace, through
3 the /dev interface. You need to load module i2c-dev for this.
5 Each registered i2c adapter gets a number, counting from 0. You can
6 examine /sys/class/i2c-dev/ to see what number corresponds to which adapter.
7 Alternatively, you can run "i2cdetect -l" to obtain a formatted list of all
8 i2c adapters present on your system at a given time. i2cdetect is part of
11 I2C device files are character device files with major device number 89
12 and a minor device number corresponding to the number assigned as
13 explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ...,
14 i2c-10, ...). All 256 minor device numbers are reserved for i2c.
20 So let's say you want to access an i2c adapter from a C program. The
21 first thing to do is "#include <linux/i2c-dev.h>". Please note that
22 there are two files named "i2c-dev.h" out there, one is distributed
23 with the Linux kernel and is meant to be included from kernel
24 driver code, the other one is distributed with i2c-tools and is
25 meant to be included from user-space programs. You obviously want
28 Now, you have to decide which adapter you want to access. You should
29 inspect /sys/class/i2c-dev/ or run "i2cdetect -l" to decide this.
30 Adapter numbers are assigned somewhat dynamically, so you can not
31 assume much about them. They can even change from one boot to the next.
33 Next thing, open the device file, as follows:
36 int adapter_nr = 2; /* probably dynamically determined */
39 snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
40 file = open(filename, O_RDWR);
42 /* ERROR HANDLING; you can check errno to see what went wrong */
46 When you have opened the device, you must specify with what device
47 address you want to communicate:
49 int addr = 0x40; /* The I2C address */
51 if (ioctl(file, I2C_SLAVE, addr) < 0) {
52 /* ERROR HANDLING; you can check errno to see what went wrong */
56 Well, you are all set up now. You can now use SMBus commands or plain
57 I2C to communicate with your device. SMBus commands are preferred if
58 the device supports them. Both are illustrated below.
60 __u8 reg = 0x10; /* Device register to access */
64 /* Using SMBus commands */
65 res = i2c_smbus_read_word_data(file, reg);
67 /* ERROR HANDLING: i2c transaction failed */
69 /* res contains the read word */
72 /* Using I2C Write, equivalent of
73 i2c_smbus_write_word_data(file, reg, 0x6543) */
77 if (write(file, buf, 3) != 3) {
78 /* ERROR HANDLING: i2c transaction failed */
81 /* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */
82 if (read(file, buf, 1) != 1) {
83 /* ERROR HANDLING: i2c transaction failed */
85 /* buf[0] contains the read byte */
88 Note that only a subset of the I2C and SMBus protocols can be achieved by
89 the means of read() and write() calls. In particular, so-called combined
90 transactions (mixing read and write messages in the same transaction)
91 aren't supported. For this reason, this interface is almost never used by
94 IMPORTANT: because of the use of inline functions, you *have* to use
95 '-O' or some variation when you compile your program!
98 Full interface description
99 ==========================
101 The following IOCTLs are defined:
103 ioctl(file, I2C_SLAVE, long addr)
104 Change slave address. The address is passed in the 7 lower bits of the
105 argument (except for 10 bit addresses, passed in the 10 lower bits in this
108 ioctl(file, I2C_TENBIT, long select)
109 Selects ten bit addresses if select not equals 0, selects normal 7 bit
110 addresses if select equals 0. Default 0. This request is only valid
111 if the adapter has I2C_FUNC_10BIT_ADDR.
113 ioctl(file, I2C_PEC, long select)
114 Selects SMBus PEC (packet error checking) generation and verification
115 if select not equals 0, disables if select equals 0. Default 0.
116 Used only for SMBus transactions. This request only has an effect if the
117 the adapter has I2C_FUNC_SMBUS_PEC; it is still safe if not, it just
118 doesn't have any effect.
120 ioctl(file, I2C_FUNCS, unsigned long *funcs)
121 Gets the adapter functionality and puts it in *funcs.
123 ioctl(file, I2C_RDWR, struct i2c_rdwr_ioctl_data *msgset)
124 Do combined read/write transaction without stop in between.
125 Only valid if the adapter has I2C_FUNC_I2C. The argument is
128 struct i2c_rdwr_ioctl_data {
129 struct i2c_msg *msgs; /* ptr to array of simple messages */
130 int nmsgs; /* number of messages to exchange */
133 The msgs[] themselves contain further pointers into data buffers.
134 The function will write or read data to or from that buffers depending
135 on whether the I2C_M_RD flag is set in a particular message or not.
136 The slave address and whether to use ten bit address mode has to be
137 set in each message, overriding the values set with the above ioctl's.
139 ioctl(file, I2C_SMBUS, struct i2c_smbus_ioctl_data *args)
140 Not meant to be called directly; instead, use the access functions
143 You can do plain i2c transactions by using read(2) and write(2) calls.
144 You do not need to pass the address byte; instead, set it through
145 ioctl I2C_SLAVE before you try to access the device.
147 You can do SMBus level transactions (see documentation file smbus-protocol
148 for details) through the following functions:
149 __s32 i2c_smbus_write_quick(int file, __u8 value);
150 __s32 i2c_smbus_read_byte(int file);
151 __s32 i2c_smbus_write_byte(int file, __u8 value);
152 __s32 i2c_smbus_read_byte_data(int file, __u8 command);
153 __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value);
154 __s32 i2c_smbus_read_word_data(int file, __u8 command);
155 __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value);
156 __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value);
157 __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values);
158 __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length,
160 All these transactions return -1 on failure; you can read errno to see
161 what happened. The 'write' transactions return 0 on success; the
162 'read' transactions return the read value, except for read_block, which
163 returns the number of values read. The block buffers need not be longer
166 The above functions are all inline functions, that resolve to calls to
167 the i2c_smbus_access function, that on its turn calls a specific ioctl
168 with the data in a specific format. Read the source code if you
169 want to know what happens behind the screens.
172 Implementation details
173 ======================
175 For the interested, here's the code flow which happens inside the kernel
176 when you use the /dev interface to I2C:
178 1* Your program opens /dev/i2c-N and calls ioctl() on it, as described in
179 section "C example" above.
181 2* These open() and ioctl() calls are handled by the i2c-dev kernel
182 driver: see i2c-dev.c:i2cdev_open() and i2c-dev.c:i2cdev_ioctl(),
183 respectively. You can think of i2c-dev as a generic I2C chip driver
184 that can be programmed from user-space.
186 3* Some ioctl() calls are for administrative tasks and are handled by
187 i2c-dev directly. Examples include I2C_SLAVE (set the address of the
188 device you want to access) and I2C_PEC (enable or disable SMBus error
189 checking on future transactions.)
191 4* Other ioctl() calls are converted to in-kernel function calls by
192 i2c-dev. Examples include I2C_FUNCS, which queries the I2C adapter
193 functionality using i2c.h:i2c_get_functionality(), and I2C_SMBUS, which
194 performs an SMBus transaction using i2c-core.c:i2c_smbus_xfer().
196 The i2c-dev driver is responsible for checking all the parameters that
197 come from user-space for validity. After this point, there is no
198 difference between these calls that came from user-space through i2c-dev
199 and calls that would have been performed by kernel I2C chip drivers
200 directly. This means that I2C bus drivers don't need to implement
201 anything special to support access from user-space.
203 5* These i2c-core.c/i2c.h functions are wrappers to the actual
204 implementation of your I2C bus driver. Each adapter must declare
205 callback functions implementing these standard calls.
206 i2c.h:i2c_get_functionality() calls i2c_adapter.algo->functionality(),
207 while i2c-core.c:i2c_smbus_xfer() calls either
208 adapter.algo->smbus_xfer() if it is implemented, or if not,
209 i2c-core.c:i2c_smbus_xfer_emulated() which in turn calls
210 i2c_adapter.algo->master_xfer().
212 After your I2C bus driver has processed these requests, execution runs
213 up the call chain, with almost no processing done, except by i2c-dev to
214 package the returned data, if any, in suitable format for the ioctl.