162306a36Sopenharmony_ci============================== 262306a36Sopenharmony_ciHow to instantiate I2C devices 362306a36Sopenharmony_ci============================== 462306a36Sopenharmony_ci 562306a36Sopenharmony_ciUnlike PCI or USB devices, I2C devices are not enumerated at the hardware 662306a36Sopenharmony_cilevel. Instead, the software must know which devices are connected on each 762306a36Sopenharmony_ciI2C bus segment, and what address these devices are using. For this 862306a36Sopenharmony_cireason, the kernel code must instantiate I2C devices explicitly. There are 962306a36Sopenharmony_ciseveral ways to achieve this, depending on the context and requirements. 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ciMethod 1: Declare the I2C devices statically 1362306a36Sopenharmony_ci-------------------------------------------- 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ciThis method is appropriate when the I2C bus is a system bus as is the case 1662306a36Sopenharmony_cifor many embedded systems. On such systems, each I2C bus has a number which 1762306a36Sopenharmony_ciis known in advance. It is thus possible to pre-declare the I2C devices 1862306a36Sopenharmony_ciwhich live on this bus. 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ciThis information is provided to the kernel in a different way on different 2162306a36Sopenharmony_ciarchitectures: device tree, ACPI or board files. 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ciWhen the I2C bus in question is registered, the I2C devices will be 2462306a36Sopenharmony_ciinstantiated automatically by i2c-core. The devices will be automatically 2562306a36Sopenharmony_ciunbound and destroyed when the I2C bus they sit on goes away (if ever). 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ciDeclare the I2C devices via devicetree 2962306a36Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ciOn platforms using devicetree, the declaration of I2C devices is done in 3262306a36Sopenharmony_cisubnodes of the master controller. 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ciExample: 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci.. code-block:: dts 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci i2c1: i2c@400a0000 { 3962306a36Sopenharmony_ci /* ... master properties skipped ... */ 4062306a36Sopenharmony_ci clock-frequency = <100000>; 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci flash@50 { 4362306a36Sopenharmony_ci compatible = "atmel,24c256"; 4462306a36Sopenharmony_ci reg = <0x50>; 4562306a36Sopenharmony_ci }; 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci pca9532: gpio@60 { 4862306a36Sopenharmony_ci compatible = "nxp,pca9532"; 4962306a36Sopenharmony_ci gpio-controller; 5062306a36Sopenharmony_ci #gpio-cells = <2>; 5162306a36Sopenharmony_ci reg = <0x60>; 5262306a36Sopenharmony_ci }; 5362306a36Sopenharmony_ci }; 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ciHere, two devices are attached to the bus using a speed of 100kHz. For 5662306a36Sopenharmony_ciadditional properties which might be needed to set up the device, please refer 5762306a36Sopenharmony_cito its devicetree documentation in Documentation/devicetree/bindings/. 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ciDeclare the I2C devices via ACPI 6162306a36Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ciACPI can also describe I2C devices. There is special documentation for this 6462306a36Sopenharmony_ciwhich is currently located at Documentation/firmware-guide/acpi/enumeration.rst. 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ciDeclare the I2C devices in board files 6862306a36Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ciIn many embedded architectures, devicetree has replaced the old hardware 7162306a36Sopenharmony_cidescription based on board files, but the latter are still used in old 7262306a36Sopenharmony_cicode. Instantiating I2C devices via board files is done with an array of 7362306a36Sopenharmony_cistruct i2c_board_info which is registered by calling 7462306a36Sopenharmony_cii2c_register_board_info(). 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ciExample (from omap2 h4): 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci.. code-block:: c 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci static struct i2c_board_info h4_i2c_board_info[] __initdata = { 8162306a36Sopenharmony_ci { 8262306a36Sopenharmony_ci I2C_BOARD_INFO("isp1301_omap", 0x2d), 8362306a36Sopenharmony_ci .irq = OMAP_GPIO_IRQ(125), 8462306a36Sopenharmony_ci }, 8562306a36Sopenharmony_ci { /* EEPROM on mainboard */ 8662306a36Sopenharmony_ci I2C_BOARD_INFO("24c01", 0x52), 8762306a36Sopenharmony_ci .platform_data = &m24c01, 8862306a36Sopenharmony_ci }, 8962306a36Sopenharmony_ci { /* EEPROM on cpu card */ 9062306a36Sopenharmony_ci I2C_BOARD_INFO("24c01", 0x57), 9162306a36Sopenharmony_ci .platform_data = &m24c01, 9262306a36Sopenharmony_ci }, 9362306a36Sopenharmony_ci }; 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci static void __init omap_h4_init(void) 9662306a36Sopenharmony_ci { 9762306a36Sopenharmony_ci (...) 9862306a36Sopenharmony_ci i2c_register_board_info(1, h4_i2c_board_info, 9962306a36Sopenharmony_ci ARRAY_SIZE(h4_i2c_board_info)); 10062306a36Sopenharmony_ci (...) 10162306a36Sopenharmony_ci } 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ciThe above code declares 3 devices on I2C bus 1, including their respective 10462306a36Sopenharmony_ciaddresses and custom data needed by their drivers. 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ciMethod 2: Instantiate the devices explicitly 10862306a36Sopenharmony_ci-------------------------------------------- 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ciThis method is appropriate when a larger device uses an I2C bus for 11162306a36Sopenharmony_ciinternal communication. A typical case is TV adapters. These can have a 11262306a36Sopenharmony_cituner, a video decoder, an audio decoder, etc. usually connected to the 11362306a36Sopenharmony_cimain chip by the means of an I2C bus. You won't know the number of the I2C 11462306a36Sopenharmony_cibus in advance, so the method 1 described above can't be used. Instead, 11562306a36Sopenharmony_ciyou can instantiate your I2C devices explicitly. This is done by filling 11662306a36Sopenharmony_cia struct i2c_board_info and calling i2c_new_client_device(). 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ciExample (from the sfe4001 network driver): 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ci.. code-block:: c 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci static struct i2c_board_info sfe4001_hwmon_info = { 12362306a36Sopenharmony_ci I2C_BOARD_INFO("max6647", 0x4e), 12462306a36Sopenharmony_ci }; 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci int sfe4001_init(struct efx_nic *efx) 12762306a36Sopenharmony_ci { 12862306a36Sopenharmony_ci (...) 12962306a36Sopenharmony_ci efx->board_info.hwmon_client = 13062306a36Sopenharmony_ci i2c_new_client_device(&efx->i2c_adap, &sfe4001_hwmon_info); 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci (...) 13362306a36Sopenharmony_ci } 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ciThe above code instantiates 1 I2C device on the I2C bus which is on the 13662306a36Sopenharmony_cinetwork adapter in question. 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ciA variant of this is when you don't know for sure if an I2C device is 13962306a36Sopenharmony_cipresent or not (for example for an optional feature which is not present 14062306a36Sopenharmony_cion cheap variants of a board but you have no way to tell them apart), or 14162306a36Sopenharmony_ciit may have different addresses from one board to the next (manufacturer 14262306a36Sopenharmony_cichanging its design without notice). In this case, you can call 14362306a36Sopenharmony_cii2c_new_scanned_device() instead of i2c_new_client_device(). 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ciExample (from the nxp OHCI driver): 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ci.. code-block:: c 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END }; 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_ci static int usb_hcd_nxp_probe(struct platform_device *pdev) 15262306a36Sopenharmony_ci { 15362306a36Sopenharmony_ci (...) 15462306a36Sopenharmony_ci struct i2c_adapter *i2c_adap; 15562306a36Sopenharmony_ci struct i2c_board_info i2c_info; 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci (...) 15862306a36Sopenharmony_ci i2c_adap = i2c_get_adapter(2); 15962306a36Sopenharmony_ci memset(&i2c_info, 0, sizeof(struct i2c_board_info)); 16062306a36Sopenharmony_ci strscpy(i2c_info.type, "isp1301_nxp", sizeof(i2c_info.type)); 16162306a36Sopenharmony_ci isp1301_i2c_client = i2c_new_scanned_device(i2c_adap, &i2c_info, 16262306a36Sopenharmony_ci normal_i2c, NULL); 16362306a36Sopenharmony_ci i2c_put_adapter(i2c_adap); 16462306a36Sopenharmony_ci (...) 16562306a36Sopenharmony_ci } 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ciThe above code instantiates up to 1 I2C device on the I2C bus which is on 16862306a36Sopenharmony_cithe OHCI adapter in question. It first tries at address 0x2c, if nothing 16962306a36Sopenharmony_ciis found there it tries address 0x2d, and if still nothing is found, it 17062306a36Sopenharmony_cisimply gives up. 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ciThe driver which instantiated the I2C device is responsible for destroying 17362306a36Sopenharmony_ciit on cleanup. This is done by calling i2c_unregister_device() on the 17462306a36Sopenharmony_cipointer that was earlier returned by i2c_new_client_device() or 17562306a36Sopenharmony_cii2c_new_scanned_device(). 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ciMethod 3: Probe an I2C bus for certain devices 17962306a36Sopenharmony_ci---------------------------------------------- 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ciSometimes you do not have enough information about an I2C device, not even 18262306a36Sopenharmony_cito call i2c_new_scanned_device(). The typical case is hardware monitoring 18362306a36Sopenharmony_cichips on PC mainboards. There are several dozen models, which can live 18462306a36Sopenharmony_ciat 25 different addresses. Given the huge number of mainboards out there, 18562306a36Sopenharmony_ciit is next to impossible to build an exhaustive list of the hardware 18662306a36Sopenharmony_cimonitoring chips being used. Fortunately, most of these chips have 18762306a36Sopenharmony_cimanufacturer and device ID registers, so they can be identified by 18862306a36Sopenharmony_ciprobing. 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ciIn that case, I2C devices are neither declared nor instantiated 19162306a36Sopenharmony_ciexplicitly. Instead, i2c-core will probe for such devices as soon as their 19262306a36Sopenharmony_cidrivers are loaded, and if any is found, an I2C device will be 19362306a36Sopenharmony_ciinstantiated automatically. In order to prevent any misbehavior of this 19462306a36Sopenharmony_cimechanism, the following restrictions apply: 19562306a36Sopenharmony_ci 19662306a36Sopenharmony_ci* The I2C device driver must implement the detect() method, which 19762306a36Sopenharmony_ci identifies a supported device by reading from arbitrary registers. 19862306a36Sopenharmony_ci* Only buses which are likely to have a supported device and agree to be 19962306a36Sopenharmony_ci probed, will be probed. For example this avoids probing for hardware 20062306a36Sopenharmony_ci monitoring chips on a TV adapter. 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_ciExample: 20362306a36Sopenharmony_ciSee lm90_driver and lm90_detect() in drivers/hwmon/lm90.c 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ciI2C devices instantiated as a result of such a successful probe will be 20662306a36Sopenharmony_cidestroyed automatically when the driver which detected them is removed, 20762306a36Sopenharmony_cior when the underlying I2C bus is itself destroyed, whichever happens 20862306a36Sopenharmony_cifirst. 20962306a36Sopenharmony_ci 21062306a36Sopenharmony_ciThose of you familiar with the I2C subsystem of 2.4 kernels and early 2.6 21162306a36Sopenharmony_cikernels will find out that this method 3 is essentially similar to what 21262306a36Sopenharmony_ciwas done there. Two significant differences are: 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ci* Probing is only one way to instantiate I2C devices now, while it was the 21562306a36Sopenharmony_ci only way back then. Where possible, methods 1 and 2 should be preferred. 21662306a36Sopenharmony_ci Method 3 should only be used when there is no other way, as it can have 21762306a36Sopenharmony_ci undesirable side effects. 21862306a36Sopenharmony_ci* I2C buses must now explicitly say which I2C driver classes can probe 21962306a36Sopenharmony_ci them (by the means of the class bitfield), while all I2C buses were 22062306a36Sopenharmony_ci probed by default back then. The default is an empty class which means 22162306a36Sopenharmony_ci that no probing happens. The purpose of the class bitfield is to limit 22262306a36Sopenharmony_ci the aforementioned undesirable side effects. 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ciOnce again, method 3 should be avoided wherever possible. Explicit device 22562306a36Sopenharmony_ciinstantiation (methods 1 and 2) is much preferred for it is safer and 22662306a36Sopenharmony_cifaster. 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_ci 22962306a36Sopenharmony_ciMethod 4: Instantiate from user-space 23062306a36Sopenharmony_ci------------------------------------- 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ciIn general, the kernel should know which I2C devices are connected and 23362306a36Sopenharmony_ciwhat addresses they live at. However, in certain cases, it does not, so a 23462306a36Sopenharmony_cisysfs interface was added to let the user provide the information. This 23562306a36Sopenharmony_ciinterface is made of 2 attribute files which are created in every I2C bus 23662306a36Sopenharmony_cidirectory: ``new_device`` and ``delete_device``. Both files are write 23762306a36Sopenharmony_cionly and you must write the right parameters to them in order to properly 23862306a36Sopenharmony_ciinstantiate, respectively delete, an I2C device. 23962306a36Sopenharmony_ci 24062306a36Sopenharmony_ciFile ``new_device`` takes 2 parameters: the name of the I2C device (a 24162306a36Sopenharmony_cistring) and the address of the I2C device (a number, typically expressed 24262306a36Sopenharmony_ciin hexadecimal starting with 0x, but can also be expressed in decimal.) 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ciFile ``delete_device`` takes a single parameter: the address of the I2C 24562306a36Sopenharmony_cidevice. As no two devices can live at the same address on a given I2C 24662306a36Sopenharmony_cisegment, the address is sufficient to uniquely identify the device to be 24762306a36Sopenharmony_cideleted. 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ciExample:: 25062306a36Sopenharmony_ci 25162306a36Sopenharmony_ci # echo eeprom 0x50 > /sys/bus/i2c/devices/i2c-3/new_device 25262306a36Sopenharmony_ci 25362306a36Sopenharmony_ciWhile this interface should only be used when in-kernel device declaration 25462306a36Sopenharmony_cican't be done, there is a variety of cases where it can be helpful: 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_ci* The I2C driver usually detects devices (method 3 above) but the bus 25762306a36Sopenharmony_ci segment your device lives on doesn't have the proper class bit set and 25862306a36Sopenharmony_ci thus detection doesn't trigger. 25962306a36Sopenharmony_ci* The I2C driver usually detects devices, but your device lives at an 26062306a36Sopenharmony_ci unexpected address. 26162306a36Sopenharmony_ci* The I2C driver usually detects devices, but your device is not detected, 26262306a36Sopenharmony_ci either because the detection routine is too strict, or because your 26362306a36Sopenharmony_ci device is not officially supported yet but you know it is compatible. 26462306a36Sopenharmony_ci* You are developing a driver on a test board, where you soldered the I2C 26562306a36Sopenharmony_ci device yourself. 26662306a36Sopenharmony_ci 26762306a36Sopenharmony_ciThis interface is a replacement for the force_* module parameters some I2C 26862306a36Sopenharmony_cidrivers implement. Being implemented in i2c-core rather than in each 26962306a36Sopenharmony_cidevice driver individually, it is much more efficient, and also has the 27062306a36Sopenharmony_ciadvantage that you do not have to reload the driver to change a setting. 27162306a36Sopenharmony_ciYou can also instantiate the device before the driver is loaded or even 27262306a36Sopenharmony_ciavailable, and you don't need to know what driver the device needs. 273