i2c: Improve dev-interface documentation

* Clarify some points.
* Point developers to i2c-tools instead of lm_sensors.
* Fix coding style in code examples.

Signed-off-by: Jean Delvare <khali@linux-fr.org>
This commit is contained in:
Jean Delvare
2008-10-14 17:30:05 +02:00
committed by Jean Delvare
parent 9def255631
commit fceb2d0680

View File

@@ -4,6 +4,10 @@ the /dev interface. You need to load module i2c-dev for this.
Each registered i2c adapter gets a number, counting from 0. You can Each registered i2c adapter gets a number, counting from 0. You can
examine /sys/class/i2c-dev/ to see what number corresponds to which adapter. examine /sys/class/i2c-dev/ to see what number corresponds to which adapter.
Alternatively, you can run "i2cdetect -l" to obtain a formated list of all
i2c adapters present on your system at a given time. i2cdetect is part of
the i2c-tools package.
I2C device files are character device files with major device number 89 I2C device files are character device files with major device number 89
and a minor device number corresponding to the number assigned as and a minor device number corresponding to the number assigned as
explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ..., explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ...,
@@ -17,29 +21,33 @@ So let's say you want to access an i2c adapter from a C program. The
first thing to do is "#include <linux/i2c-dev.h>". Please note that first thing to do is "#include <linux/i2c-dev.h>". Please note that
there are two files named "i2c-dev.h" out there, one is distributed there are two files named "i2c-dev.h" out there, one is distributed
with the Linux kernel and is meant to be included from kernel with the Linux kernel and is meant to be included from kernel
driver code, the other one is distributed with lm_sensors and is driver code, the other one is distributed with i2c-tools and is
meant to be included from user-space programs. You obviously want meant to be included from user-space programs. You obviously want
the second one here. the second one here.
Now, you have to decide which adapter you want to access. You should Now, you have to decide which adapter you want to access. You should
inspect /sys/class/i2c-dev/ to decide this. Adapter numbers are assigned inspect /sys/class/i2c-dev/ or run "i2cdetect -l" to decide this.
somewhat dynamically, so you can not even assume /dev/i2c-0 is the Adapter numbers are assigned somewhat dynamically, so you can not
first adapter. assume much about them. They can even change from one boot to the next.
Next thing, open the device file, as follows: Next thing, open the device file, as follows:
int file; int file;
int adapter_nr = 2; /* probably dynamically determined */ int adapter_nr = 2; /* probably dynamically determined */
char filename[20]; char filename[20];
sprintf(filename,"/dev/i2c-%d",adapter_nr); snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
if ((file = open(filename,O_RDWR)) < 0) { file = open(filename, O_RDWR);
if (file < 0) {
/* ERROR HANDLING; you can check errno to see what went wrong */ /* ERROR HANDLING; you can check errno to see what went wrong */
exit(1); exit(1);
} }
When you have opened the device, you must specify with what device When you have opened the device, you must specify with what device
address you want to communicate: address you want to communicate:
int addr = 0x40; /* The I2C address */ int addr = 0x40; /* The I2C address */
if (ioctl(file, I2C_SLAVE, addr) < 0) { if (ioctl(file, I2C_SLAVE, addr) < 0) {
/* ERROR HANDLING; you can check errno to see what went wrong */ /* ERROR HANDLING; you can check errno to see what went wrong */
exit(1); exit(1);
@@ -48,9 +56,11 @@ address you want to communicate:
Well, you are all set up now. You can now use SMBus commands or plain 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 I2C to communicate with your device. SMBus commands are preferred if
the device supports them. Both are illustrated below. the device supports them. Both are illustrated below.
__u8 register = 0x10; /* Device register to access */ __u8 register = 0x10; /* Device register to access */
__s32 res; __s32 res;
char buf[10]; char buf[10];
/* Using SMBus commands */ /* Using SMBus commands */
res = i2c_smbus_read_word_data(file, register); res = i2c_smbus_read_word_data(file, register);
if (res < 0) { if (res < 0) {
@@ -58,6 +68,7 @@ the device supports them. Both are illustrated below.
} else { } else {
/* res contains the read word */ /* res contains the read word */
} }
/* Using I2C Write, equivalent of /* Using I2C Write, equivalent of
i2c_smbus_write_word_data(file, register, 0x6543) */ i2c_smbus_write_word_data(file, register, 0x6543) */
buf[0] = register; buf[0] = register;
@@ -66,6 +77,7 @@ the device supports them. Both are illustrated below.
if (write(file, buf, 3) ! =3) { if (write(file, buf, 3) ! =3) {
/* ERROR HANDLING: i2c transaction failed */ /* ERROR HANDLING: i2c transaction failed */
} }
/* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */ /* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */
if (read(file, buf, 1) != 1) { if (read(file, buf, 1) != 1) {
/* ERROR HANDLING: i2c transaction failed */ /* ERROR HANDLING: i2c transaction failed */
@@ -73,6 +85,12 @@ the device supports them. Both are illustrated below.
/* buf[0] contains the read byte */ /* buf[0] contains the read byte */
} }
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.
IMPORTANT: because of the use of inline functions, you *have* to use IMPORTANT: because of the use of inline functions, you *have* to use
'-O' or some variation when you compile your program! '-O' or some variation when you compile your program!
@@ -80,8 +98,7 @@ IMPORTANT: because of the use of inline functions, you *have* to use
Full interface description Full interface description
========================== ==========================
The following IOCTLs are defined and fully supported The following IOCTLs are defined:
(see also i2c-dev.h):
ioctl(file, I2C_SLAVE, long addr) ioctl(file, I2C_SLAVE, long addr)
Change slave address. The address is passed in the 7 lower bits of the Change slave address. The address is passed in the 7 lower bits of the
@@ -104,7 +121,6 @@ ioctl(file,I2C_FUNCS,unsigned long *funcs)
Gets the adapter functionality and puts it in *funcs. Gets the adapter functionality and puts it in *funcs.
ioctl(file, I2C_RDWR, struct i2c_rdwr_ioctl_data *msgset) ioctl(file, I2C_RDWR, struct i2c_rdwr_ioctl_data *msgset)
Do combined read/write transaction without stop in between. Do combined read/write transaction without stop in between.
Only valid if the adapter has I2C_FUNC_I2C. The argument is Only valid if the adapter has I2C_FUNC_I2C. The argument is
a pointer to a a pointer to a
@@ -120,9 +136,8 @@ ioctl(file,I2C_RDWR,struct i2c_rdwr_ioctl_data *msgset)
The slave address and whether to use ten bit address mode has to be 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. set in each message, overriding the values set with the above ioctl's.
ioctl(file, I2C_SMBUS, struct i2c_smbus_ioctl_data *args)
Other values are NOT supported at this moment, except for I2C_SMBUS, Not meant to be called directly; instead, use the access functions
which you should never directly call; instead, use the access functions
below. below.
You can do plain i2c transactions by using read(2) and write(2) calls. You can do plain i2c transactions by using read(2) and write(2) calls.
@@ -148,7 +163,7 @@ what happened. The 'write' transactions return 0 on success; the
returns the number of values read. The block buffers need not be longer returns the number of values read. The block buffers need not be longer
than 32 bytes. than 32 bytes.
The above functions are all macros, that resolve to calls to the The above functions are all inline functions, that resolve to calls to
i2c_smbus_access function, that on its turn calls a specific ioctl the i2c_smbus_access function, that on its turn calls a specific ioctl
with the data in a specific format. Read the source code if you with the data in a specific format. Read the source code if you
want to know what happens behind the screens. want to know what happens behind the screens.