aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation
diff options
context:
space:
mode:
authorJean Delvare <khali@linux-fr.org>2008-10-14 11:30:05 -0400
committerJean Delvare <khali@mahadeva.delvare>2008-10-14 11:30:05 -0400
commitfceb2d06800ddae53095f63843d85fcff4f701ac (patch)
treef5a298b152c54bff38f0941da90ca8e4dc239f6e /Documentation
parent9def255631bb742264d334d77819e1ae5278a515 (diff)
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>
Diffstat (limited to 'Documentation')
-rw-r--r--Documentation/i2c/dev-interface65
1 files changed, 40 insertions, 25 deletions
diff --git a/Documentation/i2c/dev-interface b/Documentation/i2c/dev-interface
index 9dd79123ddd9..689a79ea5ce7 100644
--- a/Documentation/i2c/dev-interface
+++ b/Documentation/i2c/dev-interface
@@ -4,6 +4,10 @@ the /dev interface. You need to load module i2c-dev for this.
4 4
5Each registered i2c adapter gets a number, counting from 0. You can 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. 6examine /sys/class/i2c-dev/ to see what number corresponds to which adapter.
7Alternatively, you can run "i2cdetect -l" to obtain a formated list of all
8i2c adapters present on your system at a given time. i2cdetect is part of
9the i2c-tools package.
10
7I2C device files are character device files with major device number 89 11I2C device files are character device files with major device number 89
8and a minor device number corresponding to the number assigned as 12and a minor device number corresponding to the number assigned as
9explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ..., 13explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ...,
@@ -17,30 +21,34 @@ So let's say you want to access an i2c adapter from a C program. The
17first thing to do is "#include <linux/i2c-dev.h>". Please note that 21first thing to do is "#include <linux/i2c-dev.h>". Please note that
18there are two files named "i2c-dev.h" out there, one is distributed 22there are two files named "i2c-dev.h" out there, one is distributed
19with the Linux kernel and is meant to be included from kernel 23with the Linux kernel and is meant to be included from kernel
20driver code, the other one is distributed with lm_sensors and is 24driver code, the other one is distributed with i2c-tools and is
21meant to be included from user-space programs. You obviously want 25meant to be included from user-space programs. You obviously want
22the second one here. 26the second one here.
23 27
24Now, you have to decide which adapter you want to access. You should 28Now, you have to decide which adapter you want to access. You should
25inspect /sys/class/i2c-dev/ to decide this. Adapter numbers are assigned 29inspect /sys/class/i2c-dev/ or run "i2cdetect -l" to decide this.
26somewhat dynamically, so you can not even assume /dev/i2c-0 is the 30Adapter numbers are assigned somewhat dynamically, so you can not
27first adapter. 31assume much about them. They can even change from one boot to the next.
28 32
29Next thing, open the device file, as follows: 33Next thing, open the device file, as follows:
34
30 int file; 35 int file;
31 int adapter_nr = 2; /* probably dynamically determined */ 36 int adapter_nr = 2; /* probably dynamically determined */
32 char filename[20]; 37 char filename[20];
33 38
34 sprintf(filename,"/dev/i2c-%d",adapter_nr); 39 snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
35 if ((file = open(filename,O_RDWR)) < 0) { 40 file = open(filename, O_RDWR);
41 if (file < 0) {
36 /* ERROR HANDLING; you can check errno to see what went wrong */ 42 /* ERROR HANDLING; you can check errno to see what went wrong */
37 exit(1); 43 exit(1);
38 } 44 }
39 45
40When you have opened the device, you must specify with what device 46When you have opened the device, you must specify with what device
41address you want to communicate: 47address you want to communicate:
48
42 int addr = 0x40; /* The I2C address */ 49 int addr = 0x40; /* The I2C address */
43 if (ioctl(file,I2C_SLAVE,addr) < 0) { 50
51 if (ioctl(file, I2C_SLAVE, addr) < 0) {
44 /* ERROR HANDLING; you can check errno to see what went wrong */ 52 /* ERROR HANDLING; you can check errno to see what went wrong */
45 exit(1); 53 exit(1);
46 } 54 }
@@ -48,31 +56,41 @@ address you want to communicate:
48Well, you are all set up now. You can now use SMBus commands or plain 56Well, you are all set up now. You can now use SMBus commands or plain
49I2C to communicate with your device. SMBus commands are preferred if 57I2C to communicate with your device. SMBus commands are preferred if
50the device supports them. Both are illustrated below. 58the device supports them. Both are illustrated below.
59
51 __u8 register = 0x10; /* Device register to access */ 60 __u8 register = 0x10; /* Device register to access */
52 __s32 res; 61 __s32 res;
53 char buf[10]; 62 char buf[10];
63
54 /* Using SMBus commands */ 64 /* Using SMBus commands */
55 res = i2c_smbus_read_word_data(file,register); 65 res = i2c_smbus_read_word_data(file, register);
56 if (res < 0) { 66 if (res < 0) {
57 /* ERROR HANDLING: i2c transaction failed */ 67 /* ERROR HANDLING: i2c transaction failed */
58 } else { 68 } else {
59 /* res contains the read word */ 69 /* res contains the read word */
60 } 70 }
71
61 /* Using I2C Write, equivalent of 72 /* Using I2C Write, equivalent of
62 i2c_smbus_write_word_data(file,register,0x6543) */ 73 i2c_smbus_write_word_data(file, register, 0x6543) */
63 buf[0] = register; 74 buf[0] = register;
64 buf[1] = 0x43; 75 buf[1] = 0x43;
65 buf[2] = 0x65; 76 buf[2] = 0x65;
66 if ( write(file,buf,3) != 3) { 77 if (write(file, buf, 3) ! =3) {
67 /* ERROR HANDLING: i2c transaction failed */ 78 /* ERROR HANDLING: i2c transaction failed */
68 } 79 }
80
69 /* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */ 81 /* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */
70 if (read(file,buf,1) != 1) { 82 if (read(file, buf, 1) != 1) {
71 /* ERROR HANDLING: i2c transaction failed */ 83 /* ERROR HANDLING: i2c transaction failed */
72 } else { 84 } else {
73 /* buf[0] contains the read byte */ 85 /* buf[0] contains the read byte */
74 } 86 }
75 87
88Note 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
76IMPORTANT: because of the use of inline functions, you *have* to use 94IMPORTANT: because of the use of inline functions, you *have* to use
77'-O' or some variation when you compile your program! 95'-O' or some variation when you compile your program!
78 96
@@ -80,31 +98,29 @@ IMPORTANT: because of the use of inline functions, you *have* to use
80Full interface description 98Full interface description
81========================== 99==========================
82 100
83The following IOCTLs are defined and fully supported 101The following IOCTLs are defined:
84(see also i2c-dev.h):
85 102
86ioctl(file,I2C_SLAVE,long addr) 103ioctl(file, I2C_SLAVE, long addr)
87 Change slave address. The address is passed in the 7 lower bits of the 104 Change slave address. The address is passed in the 7 lower bits of the
88 argument (except for 10 bit addresses, passed in the 10 lower bits in this 105 argument (except for 10 bit addresses, passed in the 10 lower bits in this
89 case). 106 case).
90 107
91ioctl(file,I2C_TENBIT,long select) 108ioctl(file, I2C_TENBIT, long select)
92 Selects ten bit addresses if select not equals 0, selects normal 7 bit 109 Selects ten bit addresses if select not equals 0, selects normal 7 bit
93 addresses if select equals 0. Default 0. This request is only valid 110 addresses if select equals 0. Default 0. This request is only valid
94 if the adapter has I2C_FUNC_10BIT_ADDR. 111 if the adapter has I2C_FUNC_10BIT_ADDR.
95 112
96ioctl(file,I2C_PEC,long select) 113ioctl(file, I2C_PEC, long select)
97 Selects SMBus PEC (packet error checking) generation and verification 114 Selects SMBus PEC (packet error checking) generation and verification
98 if select not equals 0, disables if select equals 0. Default 0. 115 if select not equals 0, disables if select equals 0. Default 0.
99 Used only for SMBus transactions. This request only has an effect if the 116 Used only for SMBus transactions. This request only has an effect if the
100 the adapter has I2C_FUNC_SMBUS_PEC; it is still safe if not, it just 117 the adapter has I2C_FUNC_SMBUS_PEC; it is still safe if not, it just
101 doesn't have any effect. 118 doesn't have any effect.
102 119
103ioctl(file,I2C_FUNCS,unsigned long *funcs) 120ioctl(file, I2C_FUNCS, unsigned long *funcs)
104 Gets the adapter functionality and puts it in *funcs. 121 Gets the adapter functionality and puts it in *funcs.
105 122
106ioctl(file,I2C_RDWR,struct i2c_rdwr_ioctl_data *msgset) 123ioctl(file, I2C_RDWR, struct i2c_rdwr_ioctl_data *msgset)
107
108 Do combined read/write transaction without stop in between. 124 Do combined read/write transaction without stop in between.
109 Only valid if the adapter has I2C_FUNC_I2C. The argument is 125 Only valid if the adapter has I2C_FUNC_I2C. The argument is
110 a pointer to a 126 a pointer to a
@@ -120,10 +136,9 @@ ioctl(file,I2C_RDWR,struct i2c_rdwr_ioctl_data *msgset)
120 The slave address and whether to use ten bit address mode has to be 136 The slave address and whether to use ten bit address mode has to be
121 set in each message, overriding the values set with the above ioctl's. 137 set in each message, overriding the values set with the above ioctl's.
122 138
123 139ioctl(file, I2C_SMBUS, struct i2c_smbus_ioctl_data *args)
124Other values are NOT supported at this moment, except for I2C_SMBUS, 140 Not meant to be called directly; instead, use the access functions
125which you should never directly call; instead, use the access functions 141 below.
126below.
127 142
128You can do plain i2c transactions by using read(2) and write(2) calls. 143You can do plain i2c transactions by using read(2) and write(2) calls.
129You do not need to pass the address byte; instead, set it through 144You do not need to pass the address byte; instead, set it through
@@ -148,7 +163,7 @@ what happened. The 'write' transactions return 0 on success; the
148returns the number of values read. The block buffers need not be longer 163returns the number of values read. The block buffers need not be longer
149than 32 bytes. 164than 32 bytes.
150 165
151The above functions are all macros, that resolve to calls to the 166The above functions are all inline functions, that resolve to calls to
152i2c_smbus_access function, that on its turn calls a specific ioctl 167the i2c_smbus_access function, that on its turn calls a specific ioctl
153with the data in a specific format. Read the source code if you 168with the data in a specific format. Read the source code if you
154want to know what happens behind the screens. 169want to know what happens behind the screens.