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