| 
									
										
										
										
											2020-01-29 16:19:51 +01:00
										 |  |  | ============================================
 | 
					
						
							|  |  |  | Implementing I2C device drivers in userspace
 | 
					
						
							|  |  |  | ============================================
 | 
					
						
							| 
									
										
										
										
											2019-07-26 09:51:16 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-29 16:19:29 +01:00
										 |  |  | Usually, I2C devices are controlled by a kernel driver. But it is also
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | possible to access all devices on an adapter from userspace, through
 | 
					
						
							|  |  |  | the /dev interface. You need to load module i2c-dev for this.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-29 16:19:29 +01:00
										 |  |  | Each registered I2C adapter gets a number, counting from 0. You can
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | examine /sys/class/i2c-dev/ to see what number corresponds to which adapter.
 | 
					
						
							| 
									
										
										
										
											2016-02-02 20:41:25 +09:00
										 |  |  | Alternatively, you can run "i2cdetect -l" to obtain a formatted list of all
 | 
					
						
							| 
									
										
										
										
											2020-01-29 16:19:29 +01:00
										 |  |  | I2C adapters present on your system at a given time. i2cdetect is part of
 | 
					
						
							| 
									
										
										
										
											2008-10-14 17:30:05 +02:00
										 |  |  | the i2c-tools package.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | I2C device files are character device files with major device number 89
 | 
					
						
							| 
									
										
										
										
											2018-04-13 10:42:55 -07:00
										 |  |  | and a minor device number corresponding to the number assigned as
 | 
					
						
							|  |  |  | explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ...,
 | 
					
						
							| 
									
										
										
										
											2020-01-29 16:19:29 +01:00
										 |  |  | i2c-10, ...). All 256 minor device numbers are reserved for I2C.
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C example
 | 
					
						
							|  |  |  | =========
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-29 16:19:29 +01:00
										 |  |  | So let's say you want to access an I2C adapter from a C program.
 | 
					
						
							| 
									
										
										
										
											2019-07-26 09:51:16 -03:00
										 |  |  | First, you need to include these two headers::
 | 
					
						
							| 
									
										
										
										
											2017-12-12 19:43:09 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |   #include <linux/i2c-dev.h>
 | 
					
						
							|  |  |  |   #include <i2c/smbus.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | Now, you have to decide which adapter you want to access. You should
 | 
					
						
							| 
									
										
										
										
											2008-10-14 17:30:05 +02:00
										 |  |  | inspect /sys/class/i2c-dev/ or run "i2cdetect -l" to decide this.
 | 
					
						
							|  |  |  | Adapter numbers are assigned somewhat dynamically, so you can not
 | 
					
						
							|  |  |  | assume much about them. They can even change from one boot to the next.
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-26 09:51:16 -03:00
										 |  |  | Next thing, open the device file, as follows::
 | 
					
						
							| 
									
										
										
										
											2008-10-14 17:30:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |   int file;
 | 
					
						
							|  |  |  |   int adapter_nr = 2; /* probably dynamically determined */
 | 
					
						
							|  |  |  |   char filename[20];
 | 
					
						
							| 
									
										
										
										
											2018-04-13 10:42:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-14 17:30:05 +02:00
										 |  |  |   snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
 | 
					
						
							|  |  |  |   file = open(filename, O_RDWR);
 | 
					
						
							|  |  |  |   if (file < 0) {
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |     /* ERROR HANDLING; you can check errno to see what went wrong */
 | 
					
						
							|  |  |  |     exit(1);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | When you have opened the device, you must specify with what device
 | 
					
						
							| 
									
										
										
										
											2019-07-26 09:51:16 -03:00
										 |  |  | address you want to communicate::
 | 
					
						
							| 
									
										
										
										
											2008-10-14 17:30:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |   int addr = 0x40; /* The I2C address */
 | 
					
						
							| 
									
										
										
										
											2008-10-14 17:30:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (ioctl(file, I2C_SLAVE, addr) < 0) {
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |     /* ERROR HANDLING; you can check errno to see what went wrong */
 | 
					
						
							|  |  |  |     exit(1);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Well, you are all set up now. You can now use SMBus commands or plain
 | 
					
						
							|  |  |  | I2C to communicate with your device. SMBus commands are preferred if
 | 
					
						
							| 
									
										
										
										
											2019-07-26 09:51:16 -03:00
										 |  |  | the device supports them. Both are illustrated below::
 | 
					
						
							| 
									
										
										
										
											2008-10-14 17:30:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-07 11:25:00 -07:00
										 |  |  |   __u8 reg = 0x10; /* Device register to access */
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |   __s32 res;
 | 
					
						
							|  |  |  |   char buf[10];
 | 
					
						
							| 
									
										
										
										
											2008-10-14 17:30:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |   /* Using SMBus commands */
 | 
					
						
							| 
									
										
										
										
											2014-09-07 11:25:00 -07:00
										 |  |  |   res = i2c_smbus_read_word_data(file, reg);
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |   if (res < 0) {
 | 
					
						
							| 
									
										
										
										
											2020-01-29 16:19:29 +01:00
										 |  |  |     /* ERROR HANDLING: I2C transaction failed */
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |   } else {
 | 
					
						
							|  |  |  |     /* res contains the read word */
 | 
					
						
							|  |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2008-10-14 17:30:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 10:42:57 -07:00
										 |  |  |   /*
 | 
					
						
							|  |  |  |    * Using I2C Write, equivalent of
 | 
					
						
							|  |  |  |    * i2c_smbus_write_word_data(file, reg, 0x6543)
 | 
					
						
							|  |  |  |    */
 | 
					
						
							| 
									
										
										
										
											2014-09-07 11:25:00 -07:00
										 |  |  |   buf[0] = reg;
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |   buf[1] = 0x43;
 | 
					
						
							|  |  |  |   buf[2] = 0x65;
 | 
					
						
							| 
									
										
										
										
											2014-09-07 11:25:00 -07:00
										 |  |  |   if (write(file, buf, 3) != 3) {
 | 
					
						
							| 
									
										
										
										
											2020-01-29 16:19:29 +01:00
										 |  |  |     /* ERROR HANDLING: I2C transaction failed */
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2008-10-14 17:30:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |   /* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */
 | 
					
						
							| 
									
										
										
										
											2008-10-14 17:30:05 +02:00
										 |  |  |   if (read(file, buf, 1) != 1) {
 | 
					
						
							| 
									
										
										
										
											2020-01-29 16:19:29 +01:00
										 |  |  |     /* ERROR HANDLING: I2C transaction failed */
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |   } else {
 | 
					
						
							|  |  |  |     /* buf[0] contains the read byte */
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-14 17:30:05 +02:00
										 |  |  | Note that only a subset of the I2C and SMBus protocols can be achieved by
 | 
					
						
							|  |  |  | the means of read() and write() calls. In particular, so-called combined
 | 
					
						
							|  |  |  | transactions (mixing read and write messages in the same transaction)
 | 
					
						
							|  |  |  | aren't supported. For this reason, this interface is almost never used by
 | 
					
						
							|  |  |  | user-space programs.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | IMPORTANT: because of the use of inline functions, you *have* to use
 | 
					
						
							|  |  |  | '-O' or some variation when you compile your program!
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Full interface description
 | 
					
						
							|  |  |  | ==========================
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-14 17:30:05 +02:00
										 |  |  | The following IOCTLs are defined:
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-26 09:51:16 -03:00
										 |  |  | ``ioctl(file, I2C_SLAVE, long addr)``
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |   Change slave address. The address is passed in the 7 lower bits of the
 | 
					
						
							|  |  |  |   argument (except for 10 bit addresses, passed in the 10 lower bits in this
 | 
					
						
							|  |  |  |   case).
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-26 09:51:16 -03:00
										 |  |  | ``ioctl(file, I2C_TENBIT, long select)``
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |   Selects ten bit addresses if select not equals 0, selects normal 7 bit
 | 
					
						
							| 
									
										
										
										
											2007-10-13 23:56:33 +02:00
										 |  |  |   addresses if select equals 0. Default 0.  This request is only valid
 | 
					
						
							|  |  |  |   if the adapter has I2C_FUNC_10BIT_ADDR.
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-26 09:51:16 -03:00
										 |  |  | ``ioctl(file, I2C_PEC, long select)``
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |   Selects SMBus PEC (packet error checking) generation and verification
 | 
					
						
							|  |  |  |   if select not equals 0, disables if select equals 0. Default 0.
 | 
					
						
							| 
									
										
										
										
											2007-10-13 23:56:33 +02:00
										 |  |  |   Used only for SMBus transactions.  This request only has an effect if the
 | 
					
						
							|  |  |  |   the adapter has I2C_FUNC_SMBUS_PEC; it is still safe if not, it just
 | 
					
						
							|  |  |  |   doesn't have any effect.
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-26 09:51:16 -03:00
										 |  |  | ``ioctl(file, I2C_FUNCS, unsigned long *funcs)``
 | 
					
						
							|  |  |  |   Gets the adapter functionality and puts it in ``*funcs``.
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-26 09:51:16 -03:00
										 |  |  | ``ioctl(file, I2C_RDWR, struct i2c_rdwr_ioctl_data *msgset)``
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |   Do combined read/write transaction without stop in between.
 | 
					
						
							| 
									
										
										
										
											2007-10-13 23:56:33 +02:00
										 |  |  |   Only valid if the adapter has I2C_FUNC_I2C.  The argument is
 | 
					
						
							| 
									
										
										
										
											2019-07-26 09:51:16 -03:00
										 |  |  |   a pointer to a::
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-26 09:51:16 -03:00
										 |  |  |     struct i2c_rdwr_ioctl_data {
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |       struct i2c_msg *msgs;  /* ptr to array of simple messages */
 | 
					
						
							|  |  |  |       int nmsgs;             /* number of messages to exchange */
 | 
					
						
							| 
									
										
										
										
											2019-07-26 09:51:16 -03:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   The msgs[] themselves contain further pointers into data buffers.
 | 
					
						
							|  |  |  |   The function will write or read data to or from that buffers depending
 | 
					
						
							|  |  |  |   on whether the I2C_M_RD flag is set in a particular message or not.
 | 
					
						
							|  |  |  |   The slave address and whether to use ten bit address mode has to be
 | 
					
						
							|  |  |  |   set in each message, overriding the values set with the above ioctl's.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-26 09:51:16 -03:00
										 |  |  | ``ioctl(file, I2C_SMBUS, struct i2c_smbus_ioctl_data *args)``
 | 
					
						
							|  |  |  |   If possible, use the provided ``i2c_smbus_*`` methods described below instead
 | 
					
						
							| 
									
										
										
										
											2018-04-13 10:42:56 -07:00
										 |  |  |   of issuing direct ioctls.
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-29 16:19:29 +01:00
										 |  |  | You can do plain I2C transactions by using read(2) and write(2) calls.
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | You do not need to pass the address byte; instead, set it through
 | 
					
						
							|  |  |  | ioctl I2C_SLAVE before you try to access the device.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-13 11:05:52 +01:00
										 |  |  | You can do SMBus level transactions (see documentation file smbus-protocol.rst
 | 
					
						
							| 
									
										
										
										
											2019-07-26 09:51:16 -03:00
										 |  |  | for details) through the following functions::
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |   __s32 i2c_smbus_write_quick(int file, __u8 value);
 | 
					
						
							|  |  |  |   __s32 i2c_smbus_read_byte(int file);
 | 
					
						
							|  |  |  |   __s32 i2c_smbus_write_byte(int file, __u8 value);
 | 
					
						
							|  |  |  |   __s32 i2c_smbus_read_byte_data(int file, __u8 command);
 | 
					
						
							|  |  |  |   __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value);
 | 
					
						
							|  |  |  |   __s32 i2c_smbus_read_word_data(int file, __u8 command);
 | 
					
						
							|  |  |  |   __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value);
 | 
					
						
							|  |  |  |   __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value);
 | 
					
						
							| 
									
										
										
										
											2020-08-02 10:21:22 +02:00
										 |  |  |   __s32 i2c_smbus_block_process_call(int file, __u8 command, __u8 length,
 | 
					
						
							|  |  |  |                                      __u8 *values);
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |   __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values);
 | 
					
						
							| 
									
										
										
										
											2018-04-13 10:42:55 -07:00
										 |  |  |   __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length,
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |                                    __u8 *values);
 | 
					
						
							| 
									
										
										
										
											2019-07-26 09:51:16 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | All these transactions return -1 on failure; you can read errno to see
 | 
					
						
							|  |  |  | what happened. The 'write' transactions return 0 on success; the
 | 
					
						
							|  |  |  | 'read' transactions return the read value, except for read_block, which
 | 
					
						
							|  |  |  | returns the number of values read. The block buffers need not be longer
 | 
					
						
							|  |  |  | than 32 bytes.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 10:42:56 -07:00
										 |  |  | The above functions are made available by linking against the libi2c library,
 | 
					
						
							|  |  |  | which is provided by the i2c-tools project.  See:
 | 
					
						
							|  |  |  | https://git.kernel.org/pub/scm/utils/i2c-tools/i2c-tools.git/.
 | 
					
						
							| 
									
										
										
										
											2008-10-14 17:30:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Implementation details
 | 
					
						
							|  |  |  | ======================
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | For the interested, here's the code flow which happens inside the kernel
 | 
					
						
							|  |  |  | when you use the /dev interface to I2C:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-26 09:51:16 -03:00
										 |  |  | 1) Your program opens /dev/i2c-N and calls ioctl() on it, as described in
 | 
					
						
							|  |  |  |    section "C example" above.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 2) These open() and ioctl() calls are handled by the i2c-dev kernel
 | 
					
						
							|  |  |  |    driver: see i2c-dev.c:i2cdev_open() and i2c-dev.c:i2cdev_ioctl(),
 | 
					
						
							|  |  |  |    respectively. You can think of i2c-dev as a generic I2C chip driver
 | 
					
						
							|  |  |  |    that can be programmed from user-space.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 3) Some ioctl() calls are for administrative tasks and are handled by
 | 
					
						
							|  |  |  |    i2c-dev directly. Examples include I2C_SLAVE (set the address of the
 | 
					
						
							|  |  |  |    device you want to access) and I2C_PEC (enable or disable SMBus error
 | 
					
						
							|  |  |  |    checking on future transactions.)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 4) Other ioctl() calls are converted to in-kernel function calls by
 | 
					
						
							|  |  |  |    i2c-dev. Examples include I2C_FUNCS, which queries the I2C adapter
 | 
					
						
							|  |  |  |    functionality using i2c.h:i2c_get_functionality(), and I2C_SMBUS, which
 | 
					
						
							|  |  |  |    performs an SMBus transaction using i2c-core-smbus.c:i2c_smbus_xfer().
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The i2c-dev driver is responsible for checking all the parameters that
 | 
					
						
							|  |  |  |    come from user-space for validity. After this point, there is no
 | 
					
						
							|  |  |  |    difference between these calls that came from user-space through i2c-dev
 | 
					
						
							|  |  |  |    and calls that would have been performed by kernel I2C chip drivers
 | 
					
						
							|  |  |  |    directly. This means that I2C bus drivers don't need to implement
 | 
					
						
							|  |  |  |    anything special to support access from user-space.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 5) These i2c.h functions are wrappers to the actual implementation of
 | 
					
						
							|  |  |  |    your I2C bus driver. Each adapter must declare callback functions
 | 
					
						
							|  |  |  |    implementing these standard calls. i2c.h:i2c_get_functionality() calls
 | 
					
						
							|  |  |  |    i2c_adapter.algo->functionality(), while
 | 
					
						
							|  |  |  |    i2c-core-smbus.c:i2c_smbus_xfer() calls either
 | 
					
						
							|  |  |  |    adapter.algo->smbus_xfer() if it is implemented, or if not,
 | 
					
						
							|  |  |  |    i2c-core-smbus.c:i2c_smbus_xfer_emulated() which in turn calls
 | 
					
						
							|  |  |  |    i2c_adapter.algo->master_xfer().
 | 
					
						
							| 
									
										
										
										
											2008-10-14 17:30:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | After your I2C bus driver has processed these requests, execution runs
 | 
					
						
							|  |  |  | up the call chain, with almost no processing done, except by i2c-dev to
 | 
					
						
							|  |  |  | package the returned data, if any, in suitable format for the ioctl.
 |