blob: 4aae8ed15873d1d3c1cc8fc9d80d3ef9b42e5654 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001INTRODUCTION
2------------
3
4Because not every I2C or SMBus adapter implements everything in the
5I2C specifications, a client can not trust that everything it needs
6is implemented when it is given the option to attach to an adapter:
7the client needs some way to check whether an adapter has the needed
8functionality.
9
10
11FUNCTIONALITY CONSTANTS
12-----------------------
13
14For the most up-to-date list of functionality constants, please check
Baruch Siach8421d4f2015-02-19 12:51:30 +020015<uapi/linux/i2c.h>!
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17 I2C_FUNC_I2C Plain i2c-level commands (Pure SMBus
18 adapters typically can not do these)
19 I2C_FUNC_10BIT_ADDR Handles the 10-bit address extensions
Hideki Iwamotod057c962005-09-25 16:53:04 +020020 I2C_FUNC_PROTOCOL_MANGLING Knows about the I2C_M_IGNORE_NAK,
Mark Brown14674e72012-05-30 10:55:34 +020021 I2C_M_REV_DIR_ADDR and I2C_M_NO_RD_ACK
22 flags (which modify the I2C protocol!)
23 I2C_FUNC_NOSTART Can skip repeated start sequence
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 I2C_FUNC_SMBUS_QUICK Handles the SMBus write_quick command
25 I2C_FUNC_SMBUS_READ_BYTE Handles the SMBus read_byte command
26 I2C_FUNC_SMBUS_WRITE_BYTE Handles the SMBus write_byte command
27 I2C_FUNC_SMBUS_READ_BYTE_DATA Handles the SMBus read_byte_data command
28 I2C_FUNC_SMBUS_WRITE_BYTE_DATA Handles the SMBus write_byte_data command
29 I2C_FUNC_SMBUS_READ_WORD_DATA Handles the SMBus read_word_data command
30 I2C_FUNC_SMBUS_WRITE_WORD_DATA Handles the SMBus write_byte_data command
31 I2C_FUNC_SMBUS_PROC_CALL Handles the SMBus process_call command
32 I2C_FUNC_SMBUS_READ_BLOCK_DATA Handles the SMBus read_block_data command
33 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA Handles the SMBus write_block_data command
34 I2C_FUNC_SMBUS_READ_I2C_BLOCK Handles the SMBus read_i2c_block_data command
35 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK Handles the SMBus write_i2c_block_data command
36
37A few combinations of the above flags are also defined for your convenience:
38
39 I2C_FUNC_SMBUS_BYTE Handles the SMBus read_byte
40 and write_byte commands
41 I2C_FUNC_SMBUS_BYTE_DATA Handles the SMBus read_byte_data
42 and write_byte_data commands
43 I2C_FUNC_SMBUS_WORD_DATA Handles the SMBus read_word_data
44 and write_word_data commands
45 I2C_FUNC_SMBUS_BLOCK_DATA Handles the SMBus read_block_data
46 and write_block_data commands
47 I2C_FUNC_SMBUS_I2C_BLOCK Handles the SMBus read_i2c_block_data
48 and write_i2c_block_data commands
Geert Uytterhoeven6808b002014-03-11 11:23:43 +010049 I2C_FUNC_SMBUS_EMUL Handles all SMBus commands that can be
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 emulated by a real I2C adapter (using
51 the transparent emulation layer)
52
Mark Brown14674e72012-05-30 10:55:34 +020053In kernel versions prior to 3.5 I2C_FUNC_NOSTART was implemented as
54part of I2C_FUNC_PROTOCOL_MANGLING.
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jean Delvare88b28322008-05-11 20:37:05 +020057ADAPTER IMPLEMENTATION
58----------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jean Delvare88b28322008-05-11 20:37:05 +020060When you write a new adapter driver, you will have to implement a
61function callback `functionality'. Typical implementations are given
62below.
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Jean Delvare88b28322008-05-11 20:37:05 +020064A typical SMBus-only adapter would list all the SMBus transactions it
65supports. This example comes from the i2c-piix4 driver:
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Jean Delvare88b28322008-05-11 20:37:05 +020067 static u32 piix4_func(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 {
Jean Delvare88b28322008-05-11 20:37:05 +020069 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
70 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
71 I2C_FUNC_SMBUS_BLOCK_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
73
Jean Delvare88b28322008-05-11 20:37:05 +020074A typical full-I2C adapter would use the following (from the i2c-pxa
75driver):
76
77 static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
78 {
79 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
80 }
81
82I2C_FUNC_SMBUS_EMUL includes all the SMBus transactions (with the
83addition of I2C block transactions) which i2c-core can emulate using
84I2C_FUNC_I2C without any help from the adapter driver. The idea is
85to let the client drivers check for the support of SMBus functions
86without having to care whether the said functions are implemented in
87hardware by the adapter, or emulated in software by i2c-core on top
88of an I2C adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90
91CLIENT CHECKING
92---------------
93
94Before a client tries to attach to an adapter, or even do tests to check
95whether one of the devices it supports is present on an adapter, it should
Jean Delvare88b28322008-05-11 20:37:05 +020096check whether the needed functionality is present. The typical way to do
97this is (from the lm75 driver):
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Jean Delvare88b28322008-05-11 20:37:05 +020099 static int lm75_detect(...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 {
Jean Delvare88b28322008-05-11 20:37:05 +0200101 (...)
102 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
103 I2C_FUNC_SMBUS_WORD_DATA))
104 goto exit;
105 (...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107
Jean Delvare88b28322008-05-11 20:37:05 +0200108Here, the lm75 driver checks if the adapter can do both SMBus byte data
109and SMBus word data transactions. If not, then the driver won't work on
110this adapter and there's no point in going on. If the check above is
111successful, then the driver knows that it can call the following
112functions: i2c_smbus_read_byte_data(), i2c_smbus_write_byte_data(),
113i2c_smbus_read_word_data() and i2c_smbus_write_word_data(). As a rule of
114thumb, the functionality constants you test for with
115i2c_check_functionality() should match exactly the i2c_smbus_* functions
116which you driver is calling.
117
118Note that the check above doesn't tell whether the functionalities are
119implemented in hardware by the underlying adapter or emulated in
120software by i2c-core. Client drivers don't have to care about this, as
121i2c-core will transparently implement SMBus transactions on top of I2C
122adapters.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124
125CHECKING THROUGH /DEV
126---------------------
127
128If you try to access an adapter from a userspace program, you will have
129to use the /dev interface. You will still have to check whether the
130functionality you need is supported, of course. This is done using
Jean Delvare88b28322008-05-11 20:37:05 +0200131the I2C_FUNCS ioctl. An example, adapted from the i2cdetect program, is
132below:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 int file;
Jean Delvare88b28322008-05-11 20:37:05 +0200135 if (file = open("/dev/i2c-0", O_RDWR) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /* Some kind of error handling */
137 exit(1);
138 }
Jean Delvare88b28322008-05-11 20:37:05 +0200139 if (ioctl(file, I2C_FUNCS, &funcs) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 /* Some kind of error handling */
141 exit(1);
142 }
Jean Delvare88b28322008-05-11 20:37:05 +0200143 if (!(funcs & I2C_FUNC_SMBUS_QUICK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /* Oops, the needed functionality (SMBus write_quick function) is
145 not available! */
146 exit(1);
147 }
148 /* Now it is safe to use the SMBus write_quick command */