instantiating-devices 9.5 KB

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