Blame view

Documentation/i2c/dev-interface 8.7 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
  Usually, i2c devices are controlled by a kernel driver. But it is also
  possible to access all devices on an adapter from userspace, through
  the /dev interface. You need to load module i2c-dev for this.
  
  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.
fceb2d068   Jean Delvare   i2c: Improve dev-...
7
8
9
  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.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
11
12
13
14
15
16
17
18
19
  I2C device files are character device files with major device number 89
  and a minor device number corresponding to the number assigned as 
  explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ..., 
  i2c-10, ...). All 256 minor device numbers are reserved for i2c.
  
  
  C example
  =========
  
  So let's say you want to access an i2c adapter from a C program. The
1d772e258   Jean Delvare   [PATCH] I2C: Clar...
20
21
22
  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
  with the Linux kernel and is meant to be included from kernel
fceb2d068   Jean Delvare   i2c: Improve dev-...
23
  driver code, the other one is distributed with i2c-tools and is
1d772e258   Jean Delvare   [PATCH] I2C: Clar...
24
25
  meant to be included from user-space programs. You obviously want
  the second one here.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
  
  Now, you have to decide which adapter you want to access. You should
fceb2d068   Jean Delvare   i2c: Improve dev-...
28
29
30
  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.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
32
  
  Next thing, open the device file, as follows:
fceb2d068   Jean Delvare   i2c: Improve dev-...
33

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
35
36
37
    int file;
    int adapter_nr = 2; /* probably dynamically determined */
    char filename[20];
    
fceb2d068   Jean Delvare   i2c: Improve dev-...
38
39
40
    snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
    file = open(filename, O_RDWR);
    if (file < 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
43
44
45
46
      /* 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
  address you want to communicate:
fceb2d068   Jean Delvare   i2c: Improve dev-...
47

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48
    int addr = 0x40; /* The I2C address */
fceb2d068   Jean Delvare   i2c: Improve dev-...
49
50
  
    if (ioctl(file, I2C_SLAVE, addr) < 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
52
53
54
55
56
57
      /* 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
  the device supports them. Both are illustrated below.
fceb2d068   Jean Delvare   i2c: Improve dev-...
58

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
60
61
    __u8 register = 0x10; /* Device register to access */
    __s32 res;
    char buf[10];
fceb2d068   Jean Delvare   i2c: Improve dev-...
62

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
63
    /* Using SMBus commands */
fceb2d068   Jean Delvare   i2c: Improve dev-...
64
    res = i2c_smbus_read_word_data(file, register);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65
66
67
68
69
    if (res < 0) {
      /* ERROR HANDLING: i2c transaction failed */
    } else {
      /* res contains the read word */
    }
fceb2d068   Jean Delvare   i2c: Improve dev-...
70

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
    /* Using I2C Write, equivalent of 
fceb2d068   Jean Delvare   i2c: Improve dev-...
72
       i2c_smbus_write_word_data(file, register, 0x6543) */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
73
74
75
    buf[0] = register;
    buf[1] = 0x43;
    buf[2] = 0x65;
fceb2d068   Jean Delvare   i2c: Improve dev-...
76
    if (write(file, buf, 3) ! =3) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
78
      /* ERROR HANDLING: i2c transaction failed */
    }
fceb2d068   Jean Delvare   i2c: Improve dev-...
79

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
    /* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */
fceb2d068   Jean Delvare   i2c: Improve dev-...
81
    if (read(file, buf, 1) != 1) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
83
84
85
      /* ERROR HANDLING: i2c transaction failed */
    } else {
      /* buf[0] contains the read byte */
    }
fceb2d068   Jean Delvare   i2c: Improve dev-...
86
87
88
89
90
  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.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
91
92
93
94
95
96
  IMPORTANT: because of the use of inline functions, you *have* to use
  '-O' or some variation when you compile your program!
  
  
  Full interface description
  ==========================
fceb2d068   Jean Delvare   i2c: Improve dev-...
97
  The following IOCTLs are defined:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98

fceb2d068   Jean Delvare   i2c: Improve dev-...
99
  ioctl(file, I2C_SLAVE, long addr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
101
102
    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).
fceb2d068   Jean Delvare   i2c: Improve dev-...
103
  ioctl(file, I2C_TENBIT, long select)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
    Selects ten bit addresses if select not equals 0, selects normal 7 bit
6662cbb98   David Brownell   i2c: Rename the P...
105
106
    addresses if select equals 0. Default 0.  This request is only valid
    if the adapter has I2C_FUNC_10BIT_ADDR.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107

fceb2d068   Jean Delvare   i2c: Improve dev-...
108
  ioctl(file, I2C_PEC, long select)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
    Selects SMBus PEC (packet error checking) generation and verification
    if select not equals 0, disables if select equals 0. Default 0.
6662cbb98   David Brownell   i2c: Rename the P...
111
112
113
    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.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114

fceb2d068   Jean Delvare   i2c: Improve dev-...
115
  ioctl(file, I2C_FUNCS, unsigned long *funcs)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
    Gets the adapter functionality and puts it in *funcs.
fceb2d068   Jean Delvare   i2c: Improve dev-...
117
  ioctl(file, I2C_RDWR, struct i2c_rdwr_ioctl_data *msgset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
    Do combined read/write transaction without stop in between.
6662cbb98   David Brownell   i2c: Rename the P...
119
120
    Only valid if the adapter has I2C_FUNC_I2C.  The argument is
    a pointer to a
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121

6662cbb98   David Brownell   i2c: Rename the P...
122
    struct i2c_rdwr_ioctl_data {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
123
124
125
126
127
128
129
130
131
        struct i2c_msg *msgs;  /* ptr to array of simple messages */
        int nmsgs;             /* number of messages to exchange */
    }
  
    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.
fceb2d068   Jean Delvare   i2c: Improve dev-...
132
133
134
  ioctl(file, I2C_SMBUS, struct i2c_smbus_ioctl_data *args)
    Not meant to be called  directly; instead, use the access functions
    below.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  
  You can do plain i2c transactions by using read(2) and write(2) calls.
  You do not need to pass the address byte; instead, set it through
  ioctl I2C_SLAVE before you try to access the device.
  
  You can do SMBus level transactions (see documentation file smbus-protocol 
  for details) through the following functions:
    __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);
    __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values);
    __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length, 
                                     __u8 *values);
  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.
fceb2d068   Jean Delvare   i2c: Improve dev-...
158
159
  The above functions are all inline functions, that resolve to calls to
  the i2c_smbus_access function, that on its turn calls a specific ioctl
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160
161
  with the data in a specific format. Read the source code if you
  want to know what happens behind the screens.
7c15fd124   Jean Delvare   i2c: Document the...
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  
  
  Implementation details
  ======================
  
  For the interested, here's the code flow which happens inside the kernel
  when you use the /dev interface to I2C:
  
  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.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-core.c/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.c:i2c_smbus_xfer() calls either
  adapter.algo->smbus_xfer() if it is implemented, or if not,
  i2c-core.c:i2c_smbus_xfer_emulated() which in turn calls
  i2c_adapter.algo->master_xfer().
  
  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.