Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
| 4 | |
| 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. |
Masanari Iida | 2e049d6 | 2016-02-02 20:41:25 +0900 | [diff] [blame] | 7 | Alternatively, you can run "i2cdetect -l" to obtain a formatted list of all |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 8 | i2c adapters present on your system at a given time. i2cdetect is part of |
| 9 | the i2c-tools package. |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
| 15 | |
| 16 | |
| 17 | C example |
| 18 | ========= |
| 19 | |
Cengiz C | 91b28ae | 2017-12-12 19:43:09 +0300 | [diff] [blame] | 20 | So let's say you want to access an i2c adapter from a C program. |
| 21 | First, you need to include these two headers: |
| 22 | |
| 23 | #include <linux/i2c-dev.h> |
| 24 | #include <i2c/smbus.h> |
| 25 | |
| 26 | (Please note that there are two files named "i2c-dev.h" out there. One is |
| 27 | distributed with the Linux kernel and the other one is included in the |
| 28 | source tree of i2c-tools. They used to be different in content but since 2012 |
| 29 | they're identical. You should use "linux/i2c-dev.h"). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | Now, you have to decide which adapter you want to access. You should |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 32 | inspect /sys/class/i2c-dev/ or run "i2cdetect -l" to decide this. |
| 33 | Adapter numbers are assigned somewhat dynamically, so you can not |
| 34 | assume much about them. They can even change from one boot to the next. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | Next thing, open the device file, as follows: |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | int file; |
| 39 | int adapter_nr = 2; /* probably dynamically determined */ |
| 40 | char filename[20]; |
| 41 | |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 42 | snprintf(filename, 19, "/dev/i2c-%d", adapter_nr); |
| 43 | file = open(filename, O_RDWR); |
| 44 | if (file < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | /* ERROR HANDLING; you can check errno to see what went wrong */ |
| 46 | exit(1); |
| 47 | } |
| 48 | |
| 49 | When you have opened the device, you must specify with what device |
| 50 | address you want to communicate: |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | int addr = 0x40; /* The I2C address */ |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 53 | |
| 54 | if (ioctl(file, I2C_SLAVE, addr) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | /* ERROR HANDLING; you can check errno to see what went wrong */ |
| 56 | exit(1); |
| 57 | } |
| 58 | |
| 59 | Well, you are all set up now. You can now use SMBus commands or plain |
| 60 | I2C to communicate with your device. SMBus commands are preferred if |
| 61 | the device supports them. Both are illustrated below. |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 62 | |
Jose Manuel Alarcon Roldan | 257d6ef | 2014-09-07 11:25:00 -0700 | [diff] [blame] | 63 | __u8 reg = 0x10; /* Device register to access */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | __s32 res; |
| 65 | char buf[10]; |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | /* Using SMBus commands */ |
Jose Manuel Alarcon Roldan | 257d6ef | 2014-09-07 11:25:00 -0700 | [diff] [blame] | 68 | res = i2c_smbus_read_word_data(file, reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | if (res < 0) { |
| 70 | /* ERROR HANDLING: i2c transaction failed */ |
| 71 | } else { |
| 72 | /* res contains the read word */ |
| 73 | } |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | /* Using I2C Write, equivalent of |
Jose Manuel Alarcon Roldan | 257d6ef | 2014-09-07 11:25:00 -0700 | [diff] [blame] | 76 | i2c_smbus_write_word_data(file, reg, 0x6543) */ |
| 77 | buf[0] = reg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | buf[1] = 0x43; |
| 79 | buf[2] = 0x65; |
Jose Manuel Alarcon Roldan | 257d6ef | 2014-09-07 11:25:00 -0700 | [diff] [blame] | 80 | if (write(file, buf, 3) != 3) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | /* ERROR HANDLING: i2c transaction failed */ |
| 82 | } |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | /* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */ |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 85 | if (read(file, buf, 1) != 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | /* ERROR HANDLING: i2c transaction failed */ |
| 87 | } else { |
| 88 | /* buf[0] contains the read byte */ |
| 89 | } |
| 90 | |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 91 | Note that only a subset of the I2C and SMBus protocols can be achieved by |
| 92 | the means of read() and write() calls. In particular, so-called combined |
| 93 | transactions (mixing read and write messages in the same transaction) |
| 94 | aren't supported. For this reason, this interface is almost never used by |
| 95 | user-space programs. |
| 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | IMPORTANT: because of the use of inline functions, you *have* to use |
| 98 | '-O' or some variation when you compile your program! |
| 99 | |
| 100 | |
| 101 | Full interface description |
| 102 | ========================== |
| 103 | |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 104 | The following IOCTLs are defined: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 106 | ioctl(file, I2C_SLAVE, long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | Change slave address. The address is passed in the 7 lower bits of the |
| 108 | argument (except for 10 bit addresses, passed in the 10 lower bits in this |
| 109 | case). |
| 110 | |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 111 | ioctl(file, I2C_TENBIT, long select) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | Selects ten bit addresses if select not equals 0, selects normal 7 bit |
David Brownell | 6662cbb | 2007-10-13 23:56:33 +0200 | [diff] [blame] | 113 | addresses if select equals 0. Default 0. This request is only valid |
| 114 | if the adapter has I2C_FUNC_10BIT_ADDR. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 116 | ioctl(file, I2C_PEC, long select) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | Selects SMBus PEC (packet error checking) generation and verification |
| 118 | if select not equals 0, disables if select equals 0. Default 0. |
David Brownell | 6662cbb | 2007-10-13 23:56:33 +0200 | [diff] [blame] | 119 | Used only for SMBus transactions. This request only has an effect if the |
| 120 | the adapter has I2C_FUNC_SMBUS_PEC; it is still safe if not, it just |
| 121 | doesn't have any effect. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 123 | ioctl(file, I2C_FUNCS, unsigned long *funcs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | Gets the adapter functionality and puts it in *funcs. |
| 125 | |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 126 | ioctl(file, I2C_RDWR, struct i2c_rdwr_ioctl_data *msgset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | Do combined read/write transaction without stop in between. |
David Brownell | 6662cbb | 2007-10-13 23:56:33 +0200 | [diff] [blame] | 128 | Only valid if the adapter has I2C_FUNC_I2C. The argument is |
| 129 | a pointer to a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
David Brownell | 6662cbb | 2007-10-13 23:56:33 +0200 | [diff] [blame] | 131 | struct i2c_rdwr_ioctl_data { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | struct i2c_msg *msgs; /* ptr to array of simple messages */ |
| 133 | int nmsgs; /* number of messages to exchange */ |
| 134 | } |
| 135 | |
| 136 | The msgs[] themselves contain further pointers into data buffers. |
| 137 | The function will write or read data to or from that buffers depending |
| 138 | on whether the I2C_M_RD flag is set in a particular message or not. |
| 139 | The slave address and whether to use ten bit address mode has to be |
| 140 | set in each message, overriding the values set with the above ioctl's. |
| 141 | |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 142 | ioctl(file, I2C_SMBUS, struct i2c_smbus_ioctl_data *args) |
| 143 | Not meant to be called directly; instead, use the access functions |
| 144 | below. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
| 146 | You can do plain i2c transactions by using read(2) and write(2) calls. |
| 147 | You do not need to pass the address byte; instead, set it through |
| 148 | ioctl I2C_SLAVE before you try to access the device. |
| 149 | |
| 150 | You can do SMBus level transactions (see documentation file smbus-protocol |
| 151 | for details) through the following functions: |
| 152 | __s32 i2c_smbus_write_quick(int file, __u8 value); |
| 153 | __s32 i2c_smbus_read_byte(int file); |
| 154 | __s32 i2c_smbus_write_byte(int file, __u8 value); |
| 155 | __s32 i2c_smbus_read_byte_data(int file, __u8 command); |
| 156 | __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value); |
| 157 | __s32 i2c_smbus_read_word_data(int file, __u8 command); |
| 158 | __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value); |
| 159 | __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value); |
| 160 | __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values); |
| 161 | __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length, |
| 162 | __u8 *values); |
| 163 | All these transactions return -1 on failure; you can read errno to see |
| 164 | what happened. The 'write' transactions return 0 on success; the |
| 165 | 'read' transactions return the read value, except for read_block, which |
| 166 | returns the number of values read. The block buffers need not be longer |
| 167 | than 32 bytes. |
| 168 | |
Jean Delvare | fceb2d0 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 169 | The above functions are all inline functions, that resolve to calls to |
| 170 | the i2c_smbus_access function, that on its turn calls a specific ioctl |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | with the data in a specific format. Read the source code if you |
| 172 | want to know what happens behind the screens. |
Jean Delvare | 7c15fd1 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 173 | |
| 174 | |
| 175 | Implementation details |
| 176 | ====================== |
| 177 | |
| 178 | For the interested, here's the code flow which happens inside the kernel |
| 179 | when you use the /dev interface to I2C: |
| 180 | |
| 181 | 1* Your program opens /dev/i2c-N and calls ioctl() on it, as described in |
| 182 | section "C example" above. |
| 183 | |
| 184 | 2* These open() and ioctl() calls are handled by the i2c-dev kernel |
| 185 | driver: see i2c-dev.c:i2cdev_open() and i2c-dev.c:i2cdev_ioctl(), |
| 186 | respectively. You can think of i2c-dev as a generic I2C chip driver |
| 187 | that can be programmed from user-space. |
| 188 | |
| 189 | 3* Some ioctl() calls are for administrative tasks and are handled by |
| 190 | i2c-dev directly. Examples include I2C_SLAVE (set the address of the |
| 191 | device you want to access) and I2C_PEC (enable or disable SMBus error |
| 192 | checking on future transactions.) |
| 193 | |
| 194 | 4* Other ioctl() calls are converted to in-kernel function calls by |
| 195 | i2c-dev. Examples include I2C_FUNCS, which queries the I2C adapter |
| 196 | functionality using i2c.h:i2c_get_functionality(), and I2C_SMBUS, which |
Wolfram Sang | 984b292 | 2017-05-25 22:55:42 +0200 | [diff] [blame] | 197 | performs an SMBus transaction using i2c-core-smbus.c:i2c_smbus_xfer(). |
Jean Delvare | 7c15fd1 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 198 | |
| 199 | The i2c-dev driver is responsible for checking all the parameters that |
| 200 | come from user-space for validity. After this point, there is no |
| 201 | difference between these calls that came from user-space through i2c-dev |
| 202 | and calls that would have been performed by kernel I2C chip drivers |
| 203 | directly. This means that I2C bus drivers don't need to implement |
| 204 | anything special to support access from user-space. |
| 205 | |
Wolfram Sang | 984b292 | 2017-05-25 22:55:42 +0200 | [diff] [blame] | 206 | 5* These i2c.h functions are wrappers to the actual implementation of |
| 207 | your I2C bus driver. Each adapter must declare callback functions |
| 208 | implementing these standard calls. i2c.h:i2c_get_functionality() calls |
| 209 | i2c_adapter.algo->functionality(), while |
| 210 | i2c-core-smbus.c:i2c_smbus_xfer() calls either |
Jean Delvare | 7c15fd1 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 211 | adapter.algo->smbus_xfer() if it is implemented, or if not, |
Wolfram Sang | 984b292 | 2017-05-25 22:55:42 +0200 | [diff] [blame] | 212 | i2c-core-smbus.c:i2c_smbus_xfer_emulated() which in turn calls |
Jean Delvare | 7c15fd1 | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 213 | i2c_adapter.algo->master_xfer(). |
| 214 | |
| 215 | After your I2C bus driver has processed these requests, execution runs |
| 216 | up the call chain, with almost no processing done, except by i2c-dev to |
| 217 | package the returned data, if any, in suitable format for the ioctl. |