pmbus-core 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. PMBus core driver and internal API
  2. ==================================
  3. Introduction
  4. ============
  5. [from pmbus.org] The Power Management Bus (PMBus) is an open standard
  6. power-management protocol with a fully defined command language that facilitates
  7. communication with power converters and other devices in a power system. The
  8. protocol is implemented over the industry-standard SMBus serial interface and
  9. enables programming, control, and real-time monitoring of compliant power
  10. conversion products. This flexible and highly versatile standard allows for
  11. communication between devices based on both analog and digital technologies, and
  12. provides true interoperability which will reduce design complexity and shorten
  13. time to market for power system designers. Pioneered by leading power supply and
  14. semiconductor companies, this open power system standard is maintained and
  15. promoted by the PMBus Implementers Forum (PMBus-IF), comprising 30+ adopters
  16. with the objective to provide support to, and facilitate adoption among, users.
  17. Unfortunately, while PMBus commands are standardized, there are no mandatory
  18. commands, and manufacturers can add as many non-standard commands as they like.
  19. Also, different PMBUs devices act differently if non-supported commands are
  20. executed. Some devices return an error, some devices return 0xff or 0xffff and
  21. set a status error flag, and some devices may simply hang up.
  22. Despite all those difficulties, a generic PMBus device driver is still useful
  23. and supported since kernel version 2.6.39. However, it was necessary to support
  24. device specific extensions in addition to the core PMBus driver, since it is
  25. simply unknown what new device specific functionality PMBus device developers
  26. come up with next.
  27. To make device specific extensions as scalable as possible, and to avoid having
  28. to modify the core PMBus driver repeatedly for new devices, the PMBus driver was
  29. split into core, generic, and device specific code. The core code (in
  30. pmbus_core.c) provides generic functionality. The generic code (in pmbus.c)
  31. provides support for generic PMBus devices. Device specific code is responsible
  32. for device specific initialization and, if needed, maps device specific
  33. functionality into generic functionality. This is to some degree comparable
  34. to PCI code, where generic code is augmented as needed with quirks for all kinds
  35. of devices.
  36. PMBus device capabilities auto-detection
  37. ========================================
  38. For generic PMBus devices, code in pmbus.c attempts to auto-detect all supported
  39. PMBus commands. Auto-detection is somewhat limited, since there are simply too
  40. many variables to consider. For example, it is almost impossible to autodetect
  41. which PMBus commands are paged and which commands are replicated across all
  42. pages (see the PMBus specification for details on multi-page PMBus devices).
  43. For this reason, it often makes sense to provide a device specific driver if not
  44. all commands can be auto-detected. The data structures in this driver can be
  45. used to inform the core driver about functionality supported by individual
  46. chips.
  47. Some commands are always auto-detected. This applies to all limit commands
  48. (lcrit, min, max, and crit attributes) as well as associated alarm attributes.
  49. Limits and alarm attributes are auto-detected because there are simply too many
  50. possible combinations to provide a manual configuration interface.
  51. PMBus internal API
  52. ==================
  53. The API between core and device specific PMBus code is defined in
  54. drivers/hwmon/pmbus/pmbus.h. In addition to the internal API, pmbus.h defines
  55. standard PMBus commands and virtual PMBus commands.
  56. Standard PMBus commands
  57. -----------------------
  58. Standard PMBus commands (commands values 0x00 to 0xff) are defined in the PMBUs
  59. specification.
  60. Virtual PMBus commands
  61. ----------------------
  62. Virtual PMBus commands are provided to enable support for non-standard
  63. functionality which has been implemented by several chip vendors and is thus
  64. desirable to support.
  65. Virtual PMBus commands start with command value 0x100 and can thus easily be
  66. distinguished from standard PMBus commands (which can not have values larger
  67. than 0xff). Support for virtual PMBus commands is device specific and thus has
  68. to be implemented in device specific code.
  69. Virtual commands are named PMBUS_VIRT_xxx and start with PMBUS_VIRT_BASE. All
  70. virtual commands are word sized.
  71. There are currently two types of virtual commands.
  72. - READ commands are read-only; writes are either ignored or return an error.
  73. - RESET commands are read/write. Reading reset registers returns zero
  74. (used for detection), writing any value causes the associated history to be
  75. reset.
  76. Virtual commands have to be handled in device specific driver code. Chip driver
  77. code returns non-negative values if a virtual command is supported, or a
  78. negative error code if not. The chip driver may return -ENODATA or any other
  79. Linux error code in this case, though an error code other than -ENODATA is
  80. handled more efficiently and thus preferred. Either case, the calling PMBus
  81. core code will abort if the chip driver returns an error code when reading
  82. or writing virtual registers (in other words, the PMBus core code will never
  83. send a virtual command to a chip).
  84. PMBus driver information
  85. ------------------------
  86. PMBus driver information, defined in struct pmbus_driver_info, is the main means
  87. for device specific drivers to pass information to the core PMBus driver.
  88. Specifically, it provides the following information.
  89. - For devices supporting its data in Direct Data Format, it provides coefficients
  90. for converting register values into normalized data. This data is usually
  91. provided by chip manufacturers in device datasheets.
  92. - Supported chip functionality can be provided to the core driver. This may be
  93. necessary for chips which react badly if non-supported commands are executed,
  94. and/or to speed up device detection and initialization.
  95. - Several function entry points are provided to support overriding and/or
  96. augmenting generic command execution. This functionality can be used to map
  97. non-standard PMBus commands to standard commands, or to augment standard
  98. command return values with device specific information.
  99. API functions
  100. -------------
  101. Functions provided by chip driver
  102. ---------------------------------
  103. All functions return the command return value (read) or zero (write) if
  104. successful. A return value of -ENODATA indicates that there is no manufacturer
  105. specific command, but that a standard PMBus command may exist. Any other
  106. negative return value indicates that the commands does not exist for this
  107. chip, and that no attempt should be made to read or write the standard
  108. command.
  109. As mentioned above, an exception to this rule applies to virtual commands,
  110. which _must_ be handled in driver specific code. See "Virtual PMBus Commands"
  111. above for more details.
  112. Command execution in the core PMBus driver code is as follows.
  113. if (chip_access_function) {
  114. status = chip_access_function();
  115. if (status != -ENODATA)
  116. return status;
  117. }
  118. if (command >= PMBUS_VIRT_BASE) /* For word commands/registers only */
  119. return -EINVAL;
  120. return generic_access();
  121. Chip drivers may provide pointers to the following functions in struct
  122. pmbus_driver_info. All functions are optional.
  123. int (*read_byte_data)(struct i2c_client *client, int page, int reg);
  124. Read byte from page <page>, register <reg>.
  125. <page> may be -1, which means "current page".
  126. int (*read_word_data)(struct i2c_client *client, int page, int reg);
  127. Read word from page <page>, register <reg>.
  128. int (*write_word_data)(struct i2c_client *client, int page, int reg,
  129. u16 word);
  130. Write word to page <page>, register <reg>.
  131. int (*write_byte)(struct i2c_client *client, int page, u8 value);
  132. Write byte to page <page>, register <reg>.
  133. <page> may be -1, which means "current page".
  134. int (*identify)(struct i2c_client *client, struct pmbus_driver_info *info);
  135. Determine supported PMBus functionality. This function is only necessary
  136. if a chip driver supports multiple chips, and the chip functionality is not
  137. pre-determined. It is currently only used by the generic pmbus driver
  138. (pmbus.c).
  139. Functions exported by core driver
  140. ---------------------------------
  141. Chip drivers are expected to use the following functions to read or write
  142. PMBus registers. Chip drivers may also use direct I2C commands. If direct I2C
  143. commands are used, the chip driver code must not directly modify the current
  144. page, since the selected page is cached in the core driver and the core driver
  145. will assume that it is selected. Using pmbus_set_page() to select a new page
  146. is mandatory.
  147. int pmbus_set_page(struct i2c_client *client, u8 page);
  148. Set PMBus page register to <page> for subsequent commands.
  149. int pmbus_read_word_data(struct i2c_client *client, u8 page, u8 reg);
  150. Read word data from <page>, <reg>. Similar to i2c_smbus_read_word_data(), but
  151. selects page first.
  152. int pmbus_write_word_data(struct i2c_client *client, u8 page, u8 reg,
  153. u16 word);
  154. Write word data to <page>, <reg>. Similar to i2c_smbus_write_word_data(), but
  155. selects page first.
  156. int pmbus_read_byte_data(struct i2c_client *client, int page, u8 reg);
  157. Read byte data from <page>, <reg>. Similar to i2c_smbus_read_byte_data(), but
  158. selects page first. <page> may be -1, which means "current page".
  159. int pmbus_write_byte(struct i2c_client *client, int page, u8 value);
  160. Write byte data to <page>, <reg>. Similar to i2c_smbus_write_byte(), but
  161. selects page first. <page> may be -1, which means "current page".
  162. void pmbus_clear_faults(struct i2c_client *client);
  163. Execute PMBus "Clear Fault" command on all chip pages.
  164. This function calls the device specific write_byte function if defined.
  165. Therefore, it must _not_ be called from that function.
  166. bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg);
  167. Check if byte register exists. Return true if the register exists, false
  168. otherwise.
  169. This function calls the device specific write_byte function if defined to
  170. obtain the chip status. Therefore, it must _not_ be called from that function.
  171. bool pmbus_check_word_register(struct i2c_client *client, int page, int reg);
  172. Check if word register exists. Return true if the register exists, false
  173. otherwise.
  174. This function calls the device specific write_byte function if defined to
  175. obtain the chip status. Therefore, it must _not_ be called from that function.
  176. int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
  177. struct pmbus_driver_info *info);
  178. Execute probe function. Similar to standard probe function for other drivers,
  179. with the pointer to struct pmbus_driver_info as additional argument. Calls
  180. identify function if supported. Must only be called from device probe
  181. function.
  182. void pmbus_do_remove(struct i2c_client *client);
  183. Execute driver remove function. Similar to standard driver remove function.
  184. const struct pmbus_driver_info
  185. *pmbus_get_driver_info(struct i2c_client *client);
  186. Return pointer to struct pmbus_driver_info as passed to pmbus_do_probe().
  187. PMBus driver platform data
  188. ==========================
  189. PMBus platform data is defined in include/linux/i2c/pmbus.h. Platform data
  190. currently only provides a flag field with a single bit used.
  191. #define PMBUS_SKIP_STATUS_CHECK (1 << 0)
  192. struct pmbus_platform_data {
  193. u32 flags; /* Device specific flags */
  194. };
  195. Flags
  196. -----
  197. PMBUS_SKIP_STATUS_CHECK
  198. During register detection, skip checking the status register for
  199. communication or command errors.
  200. Some PMBus chips respond with valid data when trying to read an unsupported
  201. register. For such chips, checking the status register is mandatory when
  202. trying to determine if a chip register exists or not.
  203. Other PMBus chips don't support the STATUS_CML register, or report
  204. communication errors for no explicable reason. For such chips, checking the
  205. status register must be disabled.
  206. Some i2c controllers do not support single-byte commands (write commands with
  207. no data, i2c_smbus_write_byte()). With such controllers, clearing the status
  208. register is impossible, and the PMBUS_SKIP_STATUS_CHECK flag must be set.