fsp-3y.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Hardware monitoring driver for FSP 3Y-Power PSUs
  4. *
  5. * Copyright (c) 2021 Václav Kubernát, CESNET
  6. *
  7. * This driver is mostly reverse engineered with the help of a tool called pmbus_peek written by
  8. * David Brownell (and later adopted by Jan Kundrát). The device has some sort of a timing issue
  9. * when switching pages, details are explained in the code. The driver support is limited. It
  10. * exposes only the values, that have been tested to work correctly. Unsupported values either
  11. * aren't supported by the devices or their encondings are unknown.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/i2c.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include "pmbus.h"
  18. #define YM2151_PAGE_12V_LOG 0x00
  19. #define YM2151_PAGE_12V_REAL 0x00
  20. #define YM2151_PAGE_5VSB_LOG 0x01
  21. #define YM2151_PAGE_5VSB_REAL 0x20
  22. #define YH5151E_PAGE_12V_LOG 0x00
  23. #define YH5151E_PAGE_12V_REAL 0x00
  24. #define YH5151E_PAGE_5V_LOG 0x01
  25. #define YH5151E_PAGE_5V_REAL 0x10
  26. #define YH5151E_PAGE_3V3_LOG 0x02
  27. #define YH5151E_PAGE_3V3_REAL 0x11
  28. enum chips {
  29. ym2151e,
  30. yh5151e
  31. };
  32. struct fsp3y_data {
  33. struct pmbus_driver_info info;
  34. int chip;
  35. int page;
  36. bool vout_linear_11;
  37. };
  38. #define to_fsp3y_data(x) container_of(x, struct fsp3y_data, info)
  39. static int page_log_to_page_real(int page_log, enum chips chip)
  40. {
  41. switch (chip) {
  42. case ym2151e:
  43. switch (page_log) {
  44. case YM2151_PAGE_12V_LOG:
  45. return YM2151_PAGE_12V_REAL;
  46. case YM2151_PAGE_5VSB_LOG:
  47. return YM2151_PAGE_5VSB_REAL;
  48. }
  49. return -EINVAL;
  50. case yh5151e:
  51. switch (page_log) {
  52. case YH5151E_PAGE_12V_LOG:
  53. return YH5151E_PAGE_12V_REAL;
  54. case YH5151E_PAGE_5V_LOG:
  55. return YH5151E_PAGE_5V_REAL;
  56. case YH5151E_PAGE_3V3_LOG:
  57. return YH5151E_PAGE_3V3_REAL;
  58. }
  59. return -EINVAL;
  60. }
  61. return -EINVAL;
  62. }
  63. static int set_page(struct i2c_client *client, int page_log)
  64. {
  65. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  66. struct fsp3y_data *data = to_fsp3y_data(info);
  67. int rv;
  68. int page_real;
  69. if (page_log < 0)
  70. return 0;
  71. page_real = page_log_to_page_real(page_log, data->chip);
  72. if (page_real < 0)
  73. return page_real;
  74. if (data->page != page_real) {
  75. rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page_real);
  76. if (rv < 0)
  77. return rv;
  78. data->page = page_real;
  79. /*
  80. * Testing showed that the device has a timing issue. After
  81. * setting a page, it takes a while, before the device actually
  82. * gives the correct values from the correct page. 20 ms was
  83. * tested to be enough to not give wrong values (15 ms wasn't
  84. * enough).
  85. */
  86. usleep_range(20000, 30000);
  87. }
  88. return 0;
  89. }
  90. static int fsp3y_read_byte_data(struct i2c_client *client, int page, int reg)
  91. {
  92. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  93. struct fsp3y_data *data = to_fsp3y_data(info);
  94. int rv;
  95. /*
  96. * Inject an exponent for non-compliant YH5151-E.
  97. */
  98. if (data->vout_linear_11 && reg == PMBUS_VOUT_MODE)
  99. return 0x1A;
  100. rv = set_page(client, page);
  101. if (rv < 0)
  102. return rv;
  103. return i2c_smbus_read_byte_data(client, reg);
  104. }
  105. static int fsp3y_read_word_data(struct i2c_client *client, int page, int phase, int reg)
  106. {
  107. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  108. struct fsp3y_data *data = to_fsp3y_data(info);
  109. int rv;
  110. /*
  111. * This masks commands which weren't tested to work correctly. Some of
  112. * the masked commands return 0xFFFF. These would probably get tagged as
  113. * invalid by pmbus_core. Other ones do return values which might be
  114. * useful (that is, they are not 0xFFFF), but their encoding is unknown,
  115. * and so they are unsupported.
  116. */
  117. switch (reg) {
  118. case PMBUS_READ_FAN_SPEED_1:
  119. case PMBUS_READ_IIN:
  120. case PMBUS_READ_IOUT:
  121. case PMBUS_READ_PIN:
  122. case PMBUS_READ_POUT:
  123. case PMBUS_READ_TEMPERATURE_1:
  124. case PMBUS_READ_TEMPERATURE_2:
  125. case PMBUS_READ_TEMPERATURE_3:
  126. case PMBUS_READ_VIN:
  127. case PMBUS_READ_VOUT:
  128. case PMBUS_STATUS_WORD:
  129. break;
  130. default:
  131. return -ENXIO;
  132. }
  133. rv = set_page(client, page);
  134. if (rv < 0)
  135. return rv;
  136. rv = i2c_smbus_read_word_data(client, reg);
  137. if (rv < 0)
  138. return rv;
  139. /*
  140. * Handle YH-5151E non-compliant linear11 vout voltage.
  141. */
  142. if (data->vout_linear_11 && reg == PMBUS_READ_VOUT)
  143. rv = sign_extend32(rv, 10) & 0xffff;
  144. return rv;
  145. }
  146. static struct pmbus_driver_info fsp3y_info[] = {
  147. [ym2151e] = {
  148. .pages = 2,
  149. .func[YM2151_PAGE_12V_LOG] =
  150. PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
  151. PMBUS_HAVE_PIN | PMBUS_HAVE_POUT |
  152. PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 |
  153. PMBUS_HAVE_VIN | PMBUS_HAVE_IIN |
  154. PMBUS_HAVE_FAN12,
  155. .func[YM2151_PAGE_5VSB_LOG] =
  156. PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT,
  157. .read_word_data = fsp3y_read_word_data,
  158. .read_byte_data = fsp3y_read_byte_data,
  159. },
  160. [yh5151e] = {
  161. .pages = 3,
  162. .func[YH5151E_PAGE_12V_LOG] =
  163. PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
  164. PMBUS_HAVE_POUT |
  165. PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3,
  166. .func[YH5151E_PAGE_5V_LOG] =
  167. PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
  168. PMBUS_HAVE_POUT,
  169. .func[YH5151E_PAGE_3V3_LOG] =
  170. PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
  171. PMBUS_HAVE_POUT,
  172. .read_word_data = fsp3y_read_word_data,
  173. .read_byte_data = fsp3y_read_byte_data,
  174. }
  175. };
  176. static int fsp3y_detect(struct i2c_client *client)
  177. {
  178. int rv;
  179. u8 buf[I2C_SMBUS_BLOCK_MAX + 1];
  180. rv = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
  181. if (rv < 0)
  182. return rv;
  183. buf[rv] = '\0';
  184. if (rv == 8) {
  185. if (!strcmp(buf, "YM-2151E"))
  186. return ym2151e;
  187. else if (!strcmp(buf, "YH-5151E"))
  188. return yh5151e;
  189. }
  190. dev_err(&client->dev, "Unsupported model %.*s\n", rv, buf);
  191. return -ENODEV;
  192. }
  193. static const struct i2c_device_id fsp3y_id[] = {
  194. {"ym2151e", ym2151e},
  195. {"yh5151e", yh5151e},
  196. { }
  197. };
  198. static int fsp3y_probe(struct i2c_client *client)
  199. {
  200. struct fsp3y_data *data;
  201. const struct i2c_device_id *id;
  202. int rv;
  203. data = devm_kzalloc(&client->dev, sizeof(struct fsp3y_data), GFP_KERNEL);
  204. if (!data)
  205. return -ENOMEM;
  206. data->chip = fsp3y_detect(client);
  207. if (data->chip < 0)
  208. return data->chip;
  209. id = i2c_match_id(fsp3y_id, client);
  210. if (data->chip != id->driver_data)
  211. dev_warn(&client->dev, "Device mismatch: Configured %s (%d), detected %d\n",
  212. id->name, (int)id->driver_data, data->chip);
  213. rv = i2c_smbus_read_byte_data(client, PMBUS_PAGE);
  214. if (rv < 0)
  215. return rv;
  216. data->page = rv;
  217. data->info = fsp3y_info[data->chip];
  218. /*
  219. * YH-5151E sometimes reports vout in linear11 and sometimes in
  220. * linear16. This depends on the exact individual piece of hardware. One
  221. * YH-5151E can use linear16 and another might use linear11 instead.
  222. *
  223. * The format can be recognized by reading VOUT_MODE - if it doesn't
  224. * report a valid exponent, then vout uses linear11. Otherwise, the
  225. * device is compliant and uses linear16.
  226. */
  227. data->vout_linear_11 = false;
  228. if (data->chip == yh5151e) {
  229. rv = i2c_smbus_read_byte_data(client, PMBUS_VOUT_MODE);
  230. if (rv < 0)
  231. return rv;
  232. if (rv == 0xFF)
  233. data->vout_linear_11 = true;
  234. }
  235. return pmbus_do_probe(client, &data->info);
  236. }
  237. MODULE_DEVICE_TABLE(i2c, fsp3y_id);
  238. static struct i2c_driver fsp3y_driver = {
  239. .driver = {
  240. .name = "fsp3y",
  241. },
  242. .probe_new = fsp3y_probe,
  243. .id_table = fsp3y_id
  244. };
  245. module_i2c_driver(fsp3y_driver);
  246. MODULE_AUTHOR("Václav Kubernát");
  247. MODULE_DESCRIPTION("PMBus driver for FSP/3Y-Power power supplies");
  248. MODULE_LICENSE("GPL");
  249. MODULE_IMPORT_NS(PMBUS);