blob: 73ad34849f993ef74d241f1f70107e1bfb83ee56 [file] [log] [blame]
Luca Ceresolif6fcefa2020-01-29 16:19:51 +01001============================================
2Implementing I2C device drivers in userspace
3============================================
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -03004
Luca Ceresoli2f07c052020-01-29 16:19:29 +01005Usually, I2C devices are controlled by a kernel driver. But it is also
Linus Torvalds1da177e2005-04-16 15:20:36 -07006possible to access all devices on an adapter from userspace, through
7the /dev interface. You need to load module i2c-dev for this.
8
Luca Ceresoli2f07c052020-01-29 16:19:29 +01009Each registered I2C adapter gets a number, counting from 0. You can
Linus Torvalds1da177e2005-04-16 15:20:36 -070010examine /sys/class/i2c-dev/ to see what number corresponds to which adapter.
Masanari Iida2e049d62016-02-02 20:41:25 +090011Alternatively, you can run "i2cdetect -l" to obtain a formatted list of all
Luca Ceresoli2f07c052020-01-29 16:19:29 +010012I2C adapters present on your system at a given time. i2cdetect is part of
Jean Delvarefceb2d02008-10-14 17:30:05 +020013the i2c-tools package.
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015I2C device files are character device files with major device number 89
Sam Hansen675edea2018-04-13 10:42:55 -070016and a minor device number corresponding to the number assigned as
17explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ...,
Luca Ceresoli2f07c052020-01-29 16:19:29 +010018i2c-10, ...). All 256 minor device numbers are reserved for I2C.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20
21C example
22=========
23
Luca Ceresoli2f07c052020-01-29 16:19:29 +010024So let's say you want to access an I2C adapter from a C program.
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -030025First, you need to include these two headers::
Cengiz C91b28ae2017-12-12 19:43:09 +030026
27 #include <linux/i2c-dev.h>
28 #include <i2c/smbus.h>
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030Now, you have to decide which adapter you want to access. You should
Jean Delvarefceb2d02008-10-14 17:30:05 +020031inspect /sys/class/i2c-dev/ or run "i2cdetect -l" to decide this.
32Adapter numbers are assigned somewhat dynamically, so you can not
33assume much about them. They can even change from one boot to the next.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -030035Next thing, open the device file, as follows::
Jean Delvarefceb2d02008-10-14 17:30:05 +020036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 int file;
38 int adapter_nr = 2; /* probably dynamically determined */
39 char filename[20];
Sam Hansen675edea2018-04-13 10:42:55 -070040
Jean Delvarefceb2d02008-10-14 17:30:05 +020041 snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
42 file = open(filename, O_RDWR);
43 if (file < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 /* ERROR HANDLING; you can check errno to see what went wrong */
45 exit(1);
46 }
47
48When you have opened the device, you must specify with what device
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -030049address you want to communicate::
Jean Delvarefceb2d02008-10-14 17:30:05 +020050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 int addr = 0x40; /* The I2C address */
Jean Delvarefceb2d02008-10-14 17:30:05 +020052
53 if (ioctl(file, I2C_SLAVE, addr) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 /* ERROR HANDLING; you can check errno to see what went wrong */
55 exit(1);
56 }
57
58Well, you are all set up now. You can now use SMBus commands or plain
59I2C to communicate with your device. SMBus commands are preferred if
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -030060the device supports them. Both are illustrated below::
Jean Delvarefceb2d02008-10-14 17:30:05 +020061
Jose Manuel Alarcon Roldan257d6ef2014-09-07 11:25:00 -070062 __u8 reg = 0x10; /* Device register to access */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 __s32 res;
64 char buf[10];
Jean Delvarefceb2d02008-10-14 17:30:05 +020065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 /* Using SMBus commands */
Jose Manuel Alarcon Roldan257d6ef2014-09-07 11:25:00 -070067 res = i2c_smbus_read_word_data(file, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (res < 0) {
Luca Ceresoli2f07c052020-01-29 16:19:29 +010069 /* ERROR HANDLING: I2C transaction failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 } else {
71 /* res contains the read word */
72 }
Jean Delvarefceb2d02008-10-14 17:30:05 +020073
Sam Hansen40d802c2018-04-13 10:42:57 -070074 /*
75 * Using I2C Write, equivalent of
76 * i2c_smbus_write_word_data(file, reg, 0x6543)
77 */
Jose Manuel Alarcon Roldan257d6ef2014-09-07 11:25:00 -070078 buf[0] = reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 buf[1] = 0x43;
80 buf[2] = 0x65;
Jose Manuel Alarcon Roldan257d6ef2014-09-07 11:25:00 -070081 if (write(file, buf, 3) != 3) {
Luca Ceresoli2f07c052020-01-29 16:19:29 +010082 /* ERROR HANDLING: I2C transaction failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
Jean Delvarefceb2d02008-10-14 17:30:05 +020084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 /* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */
Jean Delvarefceb2d02008-10-14 17:30:05 +020086 if (read(file, buf, 1) != 1) {
Luca Ceresoli2f07c052020-01-29 16:19:29 +010087 /* ERROR HANDLING: I2C transaction failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 } else {
89 /* buf[0] contains the read byte */
90 }
91
Jean Delvarefceb2d02008-10-14 17:30:05 +020092Note that only a subset of the I2C and SMBus protocols can be achieved by
93the means of read() and write() calls. In particular, so-called combined
94transactions (mixing read and write messages in the same transaction)
95aren't supported. For this reason, this interface is almost never used by
96user-space programs.
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098IMPORTANT: because of the use of inline functions, you *have* to use
99'-O' or some variation when you compile your program!
100
101
102Full interface description
103==========================
104
Jean Delvarefceb2d02008-10-14 17:30:05 +0200105The following IOCTLs are defined:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -0300107``ioctl(file, I2C_SLAVE, long addr)``
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 Change slave address. The address is passed in the 7 lower bits of the
109 argument (except for 10 bit addresses, passed in the 10 lower bits in this
110 case).
111
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -0300112``ioctl(file, I2C_TENBIT, long select)``
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 Selects ten bit addresses if select not equals 0, selects normal 7 bit
David Brownell6662cbb2007-10-13 23:56:33 +0200114 addresses if select equals 0. Default 0. This request is only valid
115 if the adapter has I2C_FUNC_10BIT_ADDR.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -0300117``ioctl(file, I2C_PEC, long select)``
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 Selects SMBus PEC (packet error checking) generation and verification
119 if select not equals 0, disables if select equals 0. Default 0.
David Brownell6662cbb2007-10-13 23:56:33 +0200120 Used only for SMBus transactions. This request only has an effect if the
121 the adapter has I2C_FUNC_SMBUS_PEC; it is still safe if not, it just
122 doesn't have any effect.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -0300124``ioctl(file, I2C_FUNCS, unsigned long *funcs)``
125 Gets the adapter functionality and puts it in ``*funcs``.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -0300127``ioctl(file, I2C_RDWR, struct i2c_rdwr_ioctl_data *msgset)``
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 Do combined read/write transaction without stop in between.
David Brownell6662cbb2007-10-13 23:56:33 +0200129 Only valid if the adapter has I2C_FUNC_I2C. The argument is
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -0300130 a pointer to a::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -0300132 struct i2c_rdwr_ioctl_data {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct i2c_msg *msgs; /* ptr to array of simple messages */
134 int nmsgs; /* number of messages to exchange */
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -0300135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 The msgs[] themselves contain further pointers into data buffers.
138 The function will write or read data to or from that buffers depending
139 on whether the I2C_M_RD flag is set in a particular message or not.
140 The slave address and whether to use ten bit address mode has to be
141 set in each message, overriding the values set with the above ioctl's.
142
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -0300143``ioctl(file, I2C_SMBUS, struct i2c_smbus_ioctl_data *args)``
144 If possible, use the provided ``i2c_smbus_*`` methods described below instead
Sam Hansenb50cb3e2018-04-13 10:42:56 -0700145 of issuing direct ioctls.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Luca Ceresoli2f07c052020-01-29 16:19:29 +0100147You can do plain I2C transactions by using read(2) and write(2) calls.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148You do not need to pass the address byte; instead, set it through
149ioctl I2C_SLAVE before you try to access the device.
150
Sam Hansen675edea2018-04-13 10:42:55 -0700151You can do SMBus level transactions (see documentation file smbus-protocol
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -0300152for details) through the following functions::
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 __s32 i2c_smbus_write_quick(int file, __u8 value);
155 __s32 i2c_smbus_read_byte(int file);
156 __s32 i2c_smbus_write_byte(int file, __u8 value);
157 __s32 i2c_smbus_read_byte_data(int file, __u8 command);
158 __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value);
159 __s32 i2c_smbus_read_word_data(int file, __u8 command);
160 __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value);
161 __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value);
Wolfram Sangcee807c2020-08-02 10:21:22 +0200162 __s32 i2c_smbus_block_process_call(int file, __u8 command, __u8 length,
163 __u8 *values);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values);
Sam Hansen675edea2018-04-13 10:42:55 -0700165 __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 __u8 *values);
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -0300167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168All these transactions return -1 on failure; you can read errno to see
169what happened. The 'write' transactions return 0 on success; the
170'read' transactions return the read value, except for read_block, which
171returns the number of values read. The block buffers need not be longer
172than 32 bytes.
173
Sam Hansenb50cb3e2018-04-13 10:42:56 -0700174The above functions are made available by linking against the libi2c library,
175which is provided by the i2c-tools project. See:
176https://git.kernel.org/pub/scm/utils/i2c-tools/i2c-tools.git/.
Jean Delvare7c15fd12008-10-14 17:30:05 +0200177
178
179Implementation details
180======================
181
182For the interested, here's the code flow which happens inside the kernel
183when you use the /dev interface to I2C:
184
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -03001851) Your program opens /dev/i2c-N and calls ioctl() on it, as described in
186 section "C example" above.
Jean Delvare7c15fd12008-10-14 17:30:05 +0200187
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -03001882) These open() and ioctl() calls are handled by the i2c-dev kernel
189 driver: see i2c-dev.c:i2cdev_open() and i2c-dev.c:i2cdev_ioctl(),
190 respectively. You can think of i2c-dev as a generic I2C chip driver
191 that can be programmed from user-space.
Jean Delvare7c15fd12008-10-14 17:30:05 +0200192
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -03001933) Some ioctl() calls are for administrative tasks and are handled by
194 i2c-dev directly. Examples include I2C_SLAVE (set the address of the
195 device you want to access) and I2C_PEC (enable or disable SMBus error
196 checking on future transactions.)
Jean Delvare7c15fd12008-10-14 17:30:05 +0200197
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -03001984) Other ioctl() calls are converted to in-kernel function calls by
199 i2c-dev. Examples include I2C_FUNCS, which queries the I2C adapter
200 functionality using i2c.h:i2c_get_functionality(), and I2C_SMBUS, which
201 performs an SMBus transaction using i2c-core-smbus.c:i2c_smbus_xfer().
Jean Delvare7c15fd12008-10-14 17:30:05 +0200202
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -0300203 The i2c-dev driver is responsible for checking all the parameters that
204 come from user-space for validity. After this point, there is no
205 difference between these calls that came from user-space through i2c-dev
206 and calls that would have been performed by kernel I2C chip drivers
207 directly. This means that I2C bus drivers don't need to implement
208 anything special to support access from user-space.
Jean Delvare7c15fd12008-10-14 17:30:05 +0200209
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -03002105) These i2c.h functions are wrappers to the actual implementation of
211 your I2C bus driver. Each adapter must declare callback functions
212 implementing these standard calls. i2c.h:i2c_get_functionality() calls
213 i2c_adapter.algo->functionality(), while
214 i2c-core-smbus.c:i2c_smbus_xfer() calls either
215 adapter.algo->smbus_xfer() if it is implemented, or if not,
216 i2c-core-smbus.c:i2c_smbus_xfer_emulated() which in turn calls
217 i2c_adapter.algo->master_xfer().
Jean Delvare7c15fd12008-10-14 17:30:05 +0200218
219After your I2C bus driver has processed these requests, execution runs
220up the call chain, with almost no processing done, except by i2c-dev to
221package the returned data, if any, in suitable format for the ioctl.