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