Back to home page

OSCL-LXR

 
 

    


0001 ==============================
0002 How to instantiate I2C devices
0003 ==============================
0004 
0005 Unlike PCI or USB devices, I2C devices are not enumerated at the hardware
0006 level. Instead, the software must know which devices are connected on each
0007 I2C bus segment, and what address these devices are using. For this
0008 reason, the kernel code must instantiate I2C devices explicitly. There are
0009 several ways to achieve this, depending on the context and requirements.
0010 
0011 
0012 Method 1: Declare the I2C devices statically
0013 --------------------------------------------
0014 
0015 This method is appropriate when the I2C bus is a system bus as is the case
0016 for many embedded systems. On such systems, each I2C bus has a number which
0017 is known in advance. It is thus possible to pre-declare the I2C devices
0018 which live on this bus.
0019 
0020 This information is provided to the kernel in a different way on different
0021 architectures: device tree, ACPI or board files.
0022 
0023 When the I2C bus in question is registered, the I2C devices will be
0024 instantiated automatically by i2c-core. The devices will be automatically
0025 unbound and destroyed when the I2C bus they sit on goes away (if ever).
0026 
0027 
0028 Declare the I2C devices via devicetree
0029 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0030 
0031 On platforms using devicetree, the declaration of I2C devices is done in
0032 subnodes of the master controller.
0033 
0034 Example:
0035 
0036 .. code-block:: dts
0037 
0038         i2c1: i2c@400a0000 {
0039                 /* ... master properties skipped ... */
0040                 clock-frequency = <100000>;
0041 
0042                 flash@50 {
0043                         compatible = "atmel,24c256";
0044                         reg = <0x50>;
0045                 };
0046 
0047                 pca9532: gpio@60 {
0048                         compatible = "nxp,pca9532";
0049                         gpio-controller;
0050                         #gpio-cells = <2>;
0051                         reg = <0x60>;
0052                 };
0053         };
0054 
0055 Here, two devices are attached to the bus using a speed of 100kHz. For
0056 additional properties which might be needed to set up the device, please refer
0057 to its devicetree documentation in Documentation/devicetree/bindings/.
0058 
0059 
0060 Declare the I2C devices via ACPI
0061 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0062 
0063 ACPI can also describe I2C devices. There is special documentation for this
0064 which is currently located at Documentation/firmware-guide/acpi/enumeration.rst.
0065 
0066 
0067 Declare the I2C devices in board files
0068 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0069 
0070 In many embedded architectures, devicetree has replaced the old hardware
0071 description based on board files, but the latter are still used in old
0072 code. Instantiating I2C devices via board files is done with an array of
0073 struct i2c_board_info which is registered by calling
0074 i2c_register_board_info().
0075 
0076 Example (from omap2 h4):
0077 
0078 .. code-block:: c
0079 
0080   static struct i2c_board_info h4_i2c_board_info[] __initdata = {
0081         {
0082                 I2C_BOARD_INFO("isp1301_omap", 0x2d),
0083                 .irq            = OMAP_GPIO_IRQ(125),
0084         },
0085         {       /* EEPROM on mainboard */
0086                 I2C_BOARD_INFO("24c01", 0x52),
0087                 .platform_data  = &m24c01,
0088         },
0089         {       /* EEPROM on cpu card */
0090                 I2C_BOARD_INFO("24c01", 0x57),
0091                 .platform_data  = &m24c01,
0092         },
0093   };
0094 
0095   static void __init omap_h4_init(void)
0096   {
0097         (...)
0098         i2c_register_board_info(1, h4_i2c_board_info,
0099                         ARRAY_SIZE(h4_i2c_board_info));
0100         (...)
0101   }
0102 
0103 The above code declares 3 devices on I2C bus 1, including their respective
0104 addresses and custom data needed by their drivers.
0105 
0106 
0107 Method 2: Instantiate the devices explicitly
0108 --------------------------------------------
0109 
0110 This method is appropriate when a larger device uses an I2C bus for
0111 internal communication. A typical case is TV adapters. These can have a
0112 tuner, a video decoder, an audio decoder, etc. usually connected to the
0113 main chip by the means of an I2C bus. You won't know the number of the I2C
0114 bus in advance, so the method 1 described above can't be used. Instead,
0115 you can instantiate your I2C devices explicitly. This is done by filling
0116 a struct i2c_board_info and calling i2c_new_client_device().
0117 
0118 Example (from the sfe4001 network driver):
0119 
0120 .. code-block:: c
0121 
0122   static struct i2c_board_info sfe4001_hwmon_info = {
0123         I2C_BOARD_INFO("max6647", 0x4e),
0124   };
0125 
0126   int sfe4001_init(struct efx_nic *efx)
0127   {
0128         (...)
0129         efx->board_info.hwmon_client =
0130                 i2c_new_client_device(&efx->i2c_adap, &sfe4001_hwmon_info);
0131 
0132         (...)
0133   }
0134 
0135 The above code instantiates 1 I2C device on the I2C bus which is on the
0136 network adapter in question.
0137 
0138 A variant of this is when you don't know for sure if an I2C device is
0139 present or not (for example for an optional feature which is not present
0140 on cheap variants of a board but you have no way to tell them apart), or
0141 it may have different addresses from one board to the next (manufacturer
0142 changing its design without notice). In this case, you can call
0143 i2c_new_scanned_device() instead of i2c_new_client_device().
0144 
0145 Example (from the nxp OHCI driver):
0146 
0147 .. code-block:: c
0148 
0149   static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
0150 
0151   static int usb_hcd_nxp_probe(struct platform_device *pdev)
0152   {
0153         (...)
0154         struct i2c_adapter *i2c_adap;
0155         struct i2c_board_info i2c_info;
0156 
0157         (...)
0158         i2c_adap = i2c_get_adapter(2);
0159         memset(&i2c_info, 0, sizeof(struct i2c_board_info));
0160         strscpy(i2c_info.type, "isp1301_nxp", sizeof(i2c_info.type));
0161         isp1301_i2c_client = i2c_new_scanned_device(i2c_adap, &i2c_info,
0162                                                     normal_i2c, NULL);
0163         i2c_put_adapter(i2c_adap);
0164         (...)
0165   }
0166 
0167 The above code instantiates up to 1 I2C device on the I2C bus which is on
0168 the OHCI adapter in question. It first tries at address 0x2c, if nothing
0169 is found there it tries address 0x2d, and if still nothing is found, it
0170 simply gives up.
0171 
0172 The driver which instantiated the I2C device is responsible for destroying
0173 it on cleanup. This is done by calling i2c_unregister_device() on the
0174 pointer that was earlier returned by i2c_new_client_device() or
0175 i2c_new_scanned_device().
0176 
0177 
0178 Method 3: Probe an I2C bus for certain devices
0179 ----------------------------------------------
0180 
0181 Sometimes you do not have enough information about an I2C device, not even
0182 to call i2c_new_scanned_device(). The typical case is hardware monitoring
0183 chips on PC mainboards. There are several dozen models, which can live
0184 at 25 different addresses. Given the huge number of mainboards out there,
0185 it is next to impossible to build an exhaustive list of the hardware
0186 monitoring chips being used. Fortunately, most of these chips have
0187 manufacturer and device ID registers, so they can be identified by
0188 probing.
0189 
0190 In that case, I2C devices are neither declared nor instantiated
0191 explicitly. Instead, i2c-core will probe for such devices as soon as their
0192 drivers are loaded, and if any is found, an I2C device will be
0193 instantiated automatically. In order to prevent any misbehavior of this
0194 mechanism, the following restrictions apply:
0195 
0196 * The I2C device driver must implement the detect() method, which
0197   identifies a supported device by reading from arbitrary registers.
0198 * Only buses which are likely to have a supported device and agree to be
0199   probed, will be probed. For example this avoids probing for hardware
0200   monitoring chips on a TV adapter.
0201 
0202 Example:
0203 See lm90_driver and lm90_detect() in drivers/hwmon/lm90.c
0204 
0205 I2C devices instantiated as a result of such a successful probe will be
0206 destroyed automatically when the driver which detected them is removed,
0207 or when the underlying I2C bus is itself destroyed, whichever happens
0208 first.
0209 
0210 Those of you familiar with the I2C subsystem of 2.4 kernels and early 2.6
0211 kernels will find out that this method 3 is essentially similar to what
0212 was done there. Two significant differences are:
0213 
0214 * Probing is only one way to instantiate I2C devices now, while it was the
0215   only way back then. Where possible, methods 1 and 2 should be preferred.
0216   Method 3 should only be used when there is no other way, as it can have
0217   undesirable side effects.
0218 * I2C buses must now explicitly say which I2C driver classes can probe
0219   them (by the means of the class bitfield), while all I2C buses were
0220   probed by default back then. The default is an empty class which means
0221   that no probing happens. The purpose of the class bitfield is to limit
0222   the aforementioned undesirable side effects.
0223 
0224 Once again, method 3 should be avoided wherever possible. Explicit device
0225 instantiation (methods 1 and 2) is much preferred for it is safer and
0226 faster.
0227 
0228 
0229 Method 4: Instantiate from user-space
0230 -------------------------------------
0231 
0232 In general, the kernel should know which I2C devices are connected and
0233 what addresses they live at. However, in certain cases, it does not, so a
0234 sysfs interface was added to let the user provide the information. This
0235 interface is made of 2 attribute files which are created in every I2C bus
0236 directory: ``new_device`` and ``delete_device``. Both files are write
0237 only and you must write the right parameters to them in order to properly
0238 instantiate, respectively delete, an I2C device.
0239 
0240 File ``new_device`` takes 2 parameters: the name of the I2C device (a
0241 string) and the address of the I2C device (a number, typically expressed
0242 in hexadecimal starting with 0x, but can also be expressed in decimal.)
0243 
0244 File ``delete_device`` takes a single parameter: the address of the I2C
0245 device. As no two devices can live at the same address on a given I2C
0246 segment, the address is sufficient to uniquely identify the device to be
0247 deleted.
0248 
0249 Example::
0250 
0251   # echo eeprom 0x50 > /sys/bus/i2c/devices/i2c-3/new_device
0252 
0253 While this interface should only be used when in-kernel device declaration
0254 can't be done, there is a variety of cases where it can be helpful:
0255 
0256 * The I2C driver usually detects devices (method 3 above) but the bus
0257   segment your device lives on doesn't have the proper class bit set and
0258   thus detection doesn't trigger.
0259 * The I2C driver usually detects devices, but your device lives at an
0260   unexpected address.
0261 * The I2C driver usually detects devices, but your device is not detected,
0262   either because the detection routine is too strict, or because your
0263   device is not officially supported yet but you know it is compatible.
0264 * You are developing a driver on a test board, where you soldered the I2C
0265   device yourself.
0266 
0267 This interface is a replacement for the force_* module parameters some I2C
0268 drivers implement. Being implemented in i2c-core rather than in each
0269 device driver individually, it is much more efficient, and also has the
0270 advantage that you do not have to reload the driver to change a setting.
0271 You can also instantiate the device before the driver is loaded or even
0272 available, and you don't need to know what driver the device needs.