blob: fbed645ccd7563e90e86796d13cae7cfa7329748 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001Usually, i2c devices are controlled by a kernel driver. But it is also
2possible to access all devices on an adapter from userspace, through
3the /dev interface. You need to load module i2c-dev for this.
4
5Each registered i2c adapter gets a number, counting from 0. You can
6examine /sys/class/i2c-dev/ to see what number corresponds to which adapter.
Masanari Iida2e049d62016-02-02 20:41:25 +09007Alternatively, you can run "i2cdetect -l" to obtain a formatted list of all
Jean Delvarefceb2d02008-10-14 17:30:05 +02008i2c adapters present on your system at a given time. i2cdetect is part of
9the i2c-tools package.
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011I2C device files are character device files with major device number 89
Sam Hansen675edea2018-04-13 10:42:55 -070012and a minor device number corresponding to the number assigned as
13explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ...,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014i2c-10, ...). All 256 minor device numbers are reserved for i2c.
15
16
17C example
18=========
19
Cengiz C91b28ae2017-12-12 19:43:09 +030020So let's say you want to access an i2c adapter from a C program.
21First, you need to include these two headers:
22
23 #include <linux/i2c-dev.h>
24 #include <i2c/smbus.h>
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026Now, you have to decide which adapter you want to access. You should
Jean Delvarefceb2d02008-10-14 17:30:05 +020027inspect /sys/class/i2c-dev/ or run "i2cdetect -l" to decide this.
28Adapter numbers are assigned somewhat dynamically, so you can not
29assume much about them. They can even change from one boot to the next.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31Next thing, open the device file, as follows:
Jean Delvarefceb2d02008-10-14 17:30:05 +020032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 int file;
34 int adapter_nr = 2; /* probably dynamically determined */
35 char filename[20];
Sam Hansen675edea2018-04-13 10:42:55 -070036
Jean Delvarefceb2d02008-10-14 17:30:05 +020037 snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
38 file = open(filename, O_RDWR);
39 if (file < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 /* ERROR HANDLING; you can check errno to see what went wrong */
41 exit(1);
42 }
43
44When you have opened the device, you must specify with what device
45address you want to communicate:
Jean Delvarefceb2d02008-10-14 17:30:05 +020046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 int addr = 0x40; /* The I2C address */
Jean Delvarefceb2d02008-10-14 17:30:05 +020048
49 if (ioctl(file, I2C_SLAVE, addr) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 /* ERROR HANDLING; you can check errno to see what went wrong */
51 exit(1);
52 }
53
54Well, you are all set up now. You can now use SMBus commands or plain
55I2C to communicate with your device. SMBus commands are preferred if
56the device supports them. Both are illustrated below.
Jean Delvarefceb2d02008-10-14 17:30:05 +020057
Jose Manuel Alarcon Roldan257d6ef2014-09-07 11:25:00 -070058 __u8 reg = 0x10; /* Device register to access */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 __s32 res;
60 char buf[10];
Jean Delvarefceb2d02008-10-14 17:30:05 +020061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 /* Using SMBus commands */
Jose Manuel Alarcon Roldan257d6ef2014-09-07 11:25:00 -070063 res = i2c_smbus_read_word_data(file, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (res < 0) {
65 /* ERROR HANDLING: i2c transaction failed */
66 } else {
67 /* res contains the read word */
68 }
Jean Delvarefceb2d02008-10-14 17:30:05 +020069
Sam Hansen40d802c2018-04-13 10:42:57 -070070 /*
71 * Using I2C Write, equivalent of
72 * i2c_smbus_write_word_data(file, reg, 0x6543)
73 */
Jose Manuel Alarcon Roldan257d6ef2014-09-07 11:25:00 -070074 buf[0] = reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 buf[1] = 0x43;
76 buf[2] = 0x65;
Jose Manuel Alarcon Roldan257d6ef2014-09-07 11:25:00 -070077 if (write(file, buf, 3) != 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 /* ERROR HANDLING: i2c transaction failed */
79 }
Jean Delvarefceb2d02008-10-14 17:30:05 +020080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 /* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */
Jean Delvarefceb2d02008-10-14 17:30:05 +020082 if (read(file, buf, 1) != 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 /* ERROR HANDLING: i2c transaction failed */
84 } else {
85 /* buf[0] contains the read byte */
86 }
87
Jean Delvarefceb2d02008-10-14 17:30:05 +020088Note that only a subset of the I2C and SMBus protocols can be achieved by
89the means of read() and write() calls. In particular, so-called combined
90transactions (mixing read and write messages in the same transaction)
91aren't supported. For this reason, this interface is almost never used by
92user-space programs.
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094IMPORTANT: because of the use of inline functions, you *have* to use
95'-O' or some variation when you compile your program!
96
97
98Full interface description
99==========================
100
Jean Delvarefceb2d02008-10-14 17:30:05 +0200101The following IOCTLs are defined:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Jean Delvarefceb2d02008-10-14 17:30:05 +0200103ioctl(file, I2C_SLAVE, long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 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
106 case).
107
Jean Delvarefceb2d02008-10-14 17:30:05 +0200108ioctl(file, I2C_TENBIT, long select)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 Selects ten bit addresses if select not equals 0, selects normal 7 bit
David Brownell6662cbb2007-10-13 23:56:33 +0200110 addresses if select equals 0. Default 0. This request is only valid
111 if the adapter has I2C_FUNC_10BIT_ADDR.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Jean Delvarefceb2d02008-10-14 17:30:05 +0200113ioctl(file, I2C_PEC, long select)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 Selects SMBus PEC (packet error checking) generation and verification
115 if select not equals 0, disables if select equals 0. Default 0.
David Brownell6662cbb2007-10-13 23:56:33 +0200116 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Jean Delvarefceb2d02008-10-14 17:30:05 +0200120ioctl(file, I2C_FUNCS, unsigned long *funcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 Gets the adapter functionality and puts it in *funcs.
122
Jean Delvarefceb2d02008-10-14 17:30:05 +0200123ioctl(file, I2C_RDWR, struct i2c_rdwr_ioctl_data *msgset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 Do combined read/write transaction without stop in between.
David Brownell6662cbb2007-10-13 23:56:33 +0200125 Only valid if the adapter has I2C_FUNC_I2C. The argument is
126 a pointer to a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
David Brownell6662cbb2007-10-13 23:56:33 +0200128 struct i2c_rdwr_ioctl_data {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct i2c_msg *msgs; /* ptr to array of simple messages */
130 int nmsgs; /* number of messages to exchange */
131 }
132
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.
138
Jean Delvarefceb2d02008-10-14 17:30:05 +0200139ioctl(file, I2C_SMBUS, struct i2c_smbus_ioctl_data *args)
Sam Hansenb50cb3e2018-04-13 10:42:56 -0700140 If possible, use the provided i2c_smbus_* methods described below instead
141 of issuing direct ioctls.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143You can do plain i2c transactions by using read(2) and write(2) calls.
144You do not need to pass the address byte; instead, set it through
145ioctl I2C_SLAVE before you try to access the device.
146
Sam Hansen675edea2018-04-13 10:42:55 -0700147You can do SMBus level transactions (see documentation file smbus-protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148for 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);
Sam Hansen675edea2018-04-13 10:42:55 -0700158 __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 __u8 *values);
160All these transactions return -1 on failure; you can read errno to see
161what happened. The 'write' transactions return 0 on success; the
162'read' transactions return the read value, except for read_block, which
163returns the number of values read. The block buffers need not be longer
164than 32 bytes.
165
Sam Hansenb50cb3e2018-04-13 10:42:56 -0700166The above functions are made available by linking against the libi2c library,
167which is provided by the i2c-tools project. See:
168https://git.kernel.org/pub/scm/utils/i2c-tools/i2c-tools.git/.
Jean Delvare7c15fd12008-10-14 17:30:05 +0200169
170
171Implementation details
172======================
173
174For the interested, here's the code flow which happens inside the kernel
175when you use the /dev interface to I2C:
176
1771* Your program opens /dev/i2c-N and calls ioctl() on it, as described in
178section "C example" above.
179
1802* These open() and ioctl() calls are handled by the i2c-dev kernel
181driver: see i2c-dev.c:i2cdev_open() and i2c-dev.c:i2cdev_ioctl(),
182respectively. You can think of i2c-dev as a generic I2C chip driver
183that can be programmed from user-space.
184
1853* Some ioctl() calls are for administrative tasks and are handled by
186i2c-dev directly. Examples include I2C_SLAVE (set the address of the
187device you want to access) and I2C_PEC (enable or disable SMBus error
188checking on future transactions.)
189
1904* Other ioctl() calls are converted to in-kernel function calls by
191i2c-dev. Examples include I2C_FUNCS, which queries the I2C adapter
192functionality using i2c.h:i2c_get_functionality(), and I2C_SMBUS, which
Wolfram Sang984b2922017-05-25 22:55:42 +0200193performs an SMBus transaction using i2c-core-smbus.c:i2c_smbus_xfer().
Jean Delvare7c15fd12008-10-14 17:30:05 +0200194
195The i2c-dev driver is responsible for checking all the parameters that
196come from user-space for validity. After this point, there is no
197difference between these calls that came from user-space through i2c-dev
198and calls that would have been performed by kernel I2C chip drivers
199directly. This means that I2C bus drivers don't need to implement
200anything special to support access from user-space.
201
Wolfram Sang984b2922017-05-25 22:55:42 +02002025* These i2c.h functions are wrappers to the actual implementation of
203your I2C bus driver. Each adapter must declare callback functions
204implementing these standard calls. i2c.h:i2c_get_functionality() calls
205i2c_adapter.algo->functionality(), while
206i2c-core-smbus.c:i2c_smbus_xfer() calls either
Jean Delvare7c15fd12008-10-14 17:30:05 +0200207adapter.algo->smbus_xfer() if it is implemented, or if not,
Wolfram Sang984b2922017-05-25 22:55:42 +0200208i2c-core-smbus.c:i2c_smbus_xfer_emulated() which in turn calls
Jean Delvare7c15fd12008-10-14 17:30:05 +0200209i2c_adapter.algo->master_xfer().
210
211After your I2C bus driver has processed these requests, execution runs
212up the call chain, with almost no processing done, except by i2c-dev to
213package the returned data, if any, in suitable format for the ioctl.