blob: 6df095a6071e44981cd93d17785758d218cb332b [file] [log] [blame]
Jean Delvare764c1692009-03-28 21:34:40 +01001How to instantiate I2C devices
2==============================
3
4Unlike PCI or USB devices, I2C devices are not enumerated at the hardware
5level. Instead, the software must know which devices are connected on each
6I2C bus segment, and what address these devices are using. For this
7reason, the kernel code must instantiate I2C devices explicitly. There are
8several ways to achieve this, depending on the context and requirements.
9
10
Wolfram Sangaeca0fe2014-02-10 11:03:55 +010011Method 1a: Declare the I2C devices by bus number
12------------------------------------------------
Jean Delvare764c1692009-03-28 21:34:40 +010013
14This method is appropriate when the I2C bus is a system bus as is the case
15for many embedded systems. On such systems, each I2C bus has a number
16which is known in advance. It is thus possible to pre-declare the I2C
17devices which live on this bus. This is done with an array of struct
18i2c_board_info which is registered by calling i2c_register_board_info().
19
20Example (from omap2 h4):
21
Sachin Kamat42fa2782013-08-08 10:55:11 +053022static struct i2c_board_info h4_i2c_board_info[] __initdata = {
Jean Delvare764c1692009-03-28 21:34:40 +010023 {
24 I2C_BOARD_INFO("isp1301_omap", 0x2d),
25 .irq = OMAP_GPIO_IRQ(125),
26 },
27 { /* EEPROM on mainboard */
28 I2C_BOARD_INFO("24c01", 0x52),
29 .platform_data = &m24c01,
30 },
31 { /* EEPROM on cpu card */
32 I2C_BOARD_INFO("24c01", 0x57),
33 .platform_data = &m24c01,
34 },
35};
36
37static void __init omap_h4_init(void)
38{
39 (...)
40 i2c_register_board_info(1, h4_i2c_board_info,
41 ARRAY_SIZE(h4_i2c_board_info));
42 (...)
43}
44
45The above code declares 3 devices on I2C bus 1, including their respective
46addresses and custom data needed by their drivers. When the I2C bus in
47question is registered, the I2C devices will be instantiated automatically
48by i2c-core.
49
50The devices will be automatically unbound and destroyed when the I2C bus
51they sit on goes away (if ever.)
52
53
Wolfram Sangaeca0fe2014-02-10 11:03:55 +010054Method 1b: Declare the I2C devices via devicetree
55-------------------------------------------------
56
57This method has the same implications as method 1a. The declaration of I2C
58devices is here done via devicetree as subnodes of the master controller.
59
60Example:
61
62 i2c1: i2c@400a0000 {
63 /* ... master properties skipped ... */
64 clock-frequency = <100000>;
65
66 flash@50 {
67 compatible = "atmel,24c256";
68 reg = <0x50>;
69 };
70
71 pca9532: gpio@60 {
72 compatible = "nxp,pca9532";
73 gpio-controller;
74 #gpio-cells = <2>;
75 reg = <0x60>;
76 };
77 };
78
79Here, two devices are attached to the bus using a speed of 100kHz. For
80additional properties which might be needed to set up the device, please refer
81to its devicetree documentation in Documentation/devicetree/bindings/.
82
83
Jean Delvare764c1692009-03-28 21:34:40 +010084Method 2: Instantiate the devices explicitly
85--------------------------------------------
86
87This method is appropriate when a larger device uses an I2C bus for
88internal communication. A typical case is TV adapters. These can have a
89tuner, a video decoder, an audio decoder, etc. usually connected to the
90main chip by the means of an I2C bus. You won't know the number of the I2C
91bus in advance, so the method 1 described above can't be used. Instead,
92you can instantiate your I2C devices explicitly. This is done by filling
93a struct i2c_board_info and calling i2c_new_device().
94
95Example (from the sfe4001 network driver):
96
97static struct i2c_board_info sfe4001_hwmon_info = {
98 I2C_BOARD_INFO("max6647", 0x4e),
99};
100
101int sfe4001_init(struct efx_nic *efx)
102{
103 (...)
104 efx->board_info.hwmon_client =
105 i2c_new_device(&efx->i2c_adap, &sfe4001_hwmon_info);
106
107 (...)
108}
109
110The above code instantiates 1 I2C device on the I2C bus which is on the
111network adapter in question.
112
113A variant of this is when you don't know for sure if an I2C device is
114present or not (for example for an optional feature which is not present
115on cheap variants of a board but you have no way to tell them apart), or
116it may have different addresses from one board to the next (manufacturer
117changing its design without notice). In this case, you can call
118i2c_new_probed_device() instead of i2c_new_device().
119
Roland Stigge28643102012-03-12 22:54:50 +0100120Example (from the nxp OHCI driver):
Jean Delvare764c1692009-03-28 21:34:40 +0100121
122static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
123
Greg Kroah-Hartman63a29f72012-12-21 15:15:02 -0800124static int usb_hcd_nxp_probe(struct platform_device *pdev)
Jean Delvare764c1692009-03-28 21:34:40 +0100125{
126 (...)
127 struct i2c_adapter *i2c_adap;
128 struct i2c_board_info i2c_info;
129
130 (...)
131 i2c_adap = i2c_get_adapter(2);
132 memset(&i2c_info, 0, sizeof(struct i2c_board_info));
Roland Stigge28643102012-03-12 22:54:50 +0100133 strlcpy(i2c_info.type, "isp1301_nxp", I2C_NAME_SIZE);
Jean Delvare764c1692009-03-28 21:34:40 +0100134 isp1301_i2c_client = i2c_new_probed_device(i2c_adap, &i2c_info,
Jean Delvare9a942412010-08-11 18:20:56 +0200135 normal_i2c, NULL);
Jean Delvare764c1692009-03-28 21:34:40 +0100136 i2c_put_adapter(i2c_adap);
137 (...)
138}
139
140The above code instantiates up to 1 I2C device on the I2C bus which is on
141the OHCI adapter in question. It first tries at address 0x2c, if nothing
142is found there it tries address 0x2d, and if still nothing is found, it
143simply gives up.
144
145The driver which instantiated the I2C device is responsible for destroying
146it on cleanup. This is done by calling i2c_unregister_device() on the
147pointer that was earlier returned by i2c_new_device() or
148i2c_new_probed_device().
149
150
151Method 3: Probe an I2C bus for certain devices
152----------------------------------------------
153
154Sometimes you do not have enough information about an I2C device, not even
155to call i2c_new_probed_device(). The typical case is hardware monitoring
156chips on PC mainboards. There are several dozen models, which can live
157at 25 different addresses. Given the huge number of mainboards out there,
158it is next to impossible to build an exhaustive list of the hardware
159monitoring chips being used. Fortunately, most of these chips have
160manufacturer and device ID registers, so they can be identified by
161probing.
162
163In that case, I2C devices are neither declared nor instantiated
164explicitly. Instead, i2c-core will probe for such devices as soon as their
165drivers are loaded, and if any is found, an I2C device will be
166instantiated automatically. In order to prevent any misbehavior of this
167mechanism, the following restrictions apply:
168* The I2C device driver must implement the detect() method, which
169 identifies a supported device by reading from arbitrary registers.
170* Only buses which are likely to have a supported device and agree to be
171 probed, will be probed. For example this avoids probing for hardware
172 monitoring chips on a TV adapter.
173
174Example:
175See lm90_driver and lm90_detect() in drivers/hwmon/lm90.c
176
177I2C devices instantiated as a result of such a successful probe will be
178destroyed automatically when the driver which detected them is removed,
179or when the underlying I2C bus is itself destroyed, whichever happens
180first.
181
182Those of you familiar with the i2c subsystem of 2.4 kernels and early 2.6
183kernels will find out that this method 3 is essentially similar to what
184was done there. Two significant differences are:
185* Probing is only one way to instantiate I2C devices now, while it was the
186 only way back then. Where possible, methods 1 and 2 should be preferred.
187 Method 3 should only be used when there is no other way, as it can have
188 undesirable side effects.
189* I2C buses must now explicitly say which I2C driver classes can probe
190 them (by the means of the class bitfield), while all I2C buses were
191 probed by default back then. The default is an empty class which means
192 that no probing happens. The purpose of the class bitfield is to limit
193 the aforementioned undesirable side effects.
194
195Once again, method 3 should be avoided wherever possible. Explicit device
196instantiation (methods 1 and 2) is much preferred for it is safer and
197faster.
Jean Delvare99cd8e22009-06-19 16:58:20 +0200198
199
200Method 4: Instantiate from user-space
201-------------------------------------
202
203In general, the kernel should know which I2C devices are connected and
204what addresses they live at. However, in certain cases, it does not, so a
205sysfs interface was added to let the user provide the information. This
206interface is made of 2 attribute files which are created in every I2C bus
207directory: new_device and delete_device. Both files are write only and you
208must write the right parameters to them in order to properly instantiate,
209respectively delete, an I2C device.
210
211File new_device takes 2 parameters: the name of the I2C device (a string)
212and the address of the I2C device (a number, typically expressed in
213hexadecimal starting with 0x, but can also be expressed in decimal.)
214
215File delete_device takes a single parameter: the address of the I2C
216device. As no two devices can live at the same address on a given I2C
217segment, the address is sufficient to uniquely identify the device to be
218deleted.
219
220Example:
Jean Delvare03f18052009-10-04 22:53:45 +0200221# echo eeprom 0x50 > /sys/bus/i2c/devices/i2c-3/new_device
Jean Delvare99cd8e22009-06-19 16:58:20 +0200222
223While this interface should only be used when in-kernel device declaration
224can't be done, there is a variety of cases where it can be helpful:
225* The I2C driver usually detects devices (method 3 above) but the bus
226 segment your device lives on doesn't have the proper class bit set and
227 thus detection doesn't trigger.
228* The I2C driver usually detects devices, but your device lives at an
229 unexpected address.
230* The I2C driver usually detects devices, but your device is not detected,
231 either because the detection routine is too strict, or because your
232 device is not officially supported yet but you know it is compatible.
233* You are developing a driver on a test board, where you soldered the I2C
234 device yourself.
235
236This interface is a replacement for the force_* module parameters some I2C
237drivers implement. Being implemented in i2c-core rather than in each
238device driver individually, it is much more efficient, and also has the
239advantage that you do not have to reload the driver to change a setting.
240You can also instantiate the device before the driver is loaded or even
241available, and you don't need to know what driver the device needs.