mp2925.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Hardware monitoring driver for MPS Multi-phase Digital VR Controllers(MP2925)
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/i2c.h>
  7. #include <linux/module.h>
  8. #include <linux/of_device.h>
  9. #include "pmbus.h"
  10. /*
  11. * Vender specific register MFR_VR_MULTI_CONFIG(0x08).
  12. * This register is used to obtain vid scale.
  13. */
  14. #define MFR_VR_MULTI_CONFIG 0x08
  15. #define MP2925_VOUT_DIV 512
  16. #define MP2925_VOUT_OVUV_UINT 195
  17. #define MP2925_VOUT_OVUV_DIV 100
  18. #define MP2925_PAGE_NUM 2
  19. #define MP2925_RAIL1_FUNC (PMBUS_HAVE_VIN | PMBUS_HAVE_PIN | \
  20. PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | \
  21. PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP | \
  22. PMBUS_HAVE_STATUS_VOUT | \
  23. PMBUS_HAVE_STATUS_IOUT | \
  24. PMBUS_HAVE_STATUS_TEMP | \
  25. PMBUS_HAVE_STATUS_INPUT)
  26. #define MP2925_RAIL2_FUNC (PMBUS_HAVE_PIN | PMBUS_HAVE_VOUT | \
  27. PMBUS_HAVE_IOUT | PMBUS_HAVE_POUT | \
  28. PMBUS_HAVE_TEMP | PMBUS_HAVE_IIN | \
  29. PMBUS_HAVE_STATUS_VOUT | \
  30. PMBUS_HAVE_STATUS_IOUT | \
  31. PMBUS_HAVE_STATUS_TEMP | \
  32. PMBUS_HAVE_STATUS_INPUT)
  33. struct mp2925_data {
  34. struct pmbus_driver_info info;
  35. int vout_scale[MP2925_PAGE_NUM];
  36. int vid_offset[MP2925_PAGE_NUM];
  37. };
  38. #define to_mp2925_data(x) container_of(x, struct mp2925_data, info)
  39. static u16 mp2925_linear_exp_transfer(u16 word, u16 expect_exponent)
  40. {
  41. s16 exponent, mantissa, target_exponent;
  42. exponent = ((s16)word) >> 11;
  43. mantissa = ((s16)((word & 0x7ff) << 5)) >> 5;
  44. target_exponent = (s16)((expect_exponent & 0x1f) << 11) >> 11;
  45. if (exponent > target_exponent)
  46. mantissa = mantissa << (exponent - target_exponent);
  47. else
  48. mantissa = mantissa >> (target_exponent - exponent);
  49. return (mantissa & 0x7ff) | ((expect_exponent << 11) & 0xf800);
  50. }
  51. static int mp2925_read_byte_data(struct i2c_client *client, int page, int reg)
  52. {
  53. int ret;
  54. switch (reg) {
  55. case PMBUS_VOUT_MODE:
  56. /*
  57. * The MP2925 does not follow standard PMBus protocol completely,
  58. * and the calculation of vout in this driver is based on direct
  59. * format. As a result, the format of vout is enforced to direct.
  60. */
  61. ret = PB_VOUT_MODE_DIRECT;
  62. break;
  63. default:
  64. ret = -ENODATA;
  65. break;
  66. }
  67. return ret;
  68. }
  69. static int mp2925_read_word_data(struct i2c_client *client, int page, int phase,
  70. int reg)
  71. {
  72. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  73. struct mp2925_data *data = to_mp2925_data(info);
  74. int ret;
  75. switch (reg) {
  76. case PMBUS_READ_VOUT:
  77. ret = pmbus_read_word_data(client, page, phase, reg);
  78. if (ret < 0)
  79. return ret;
  80. /*
  81. * In vid mode, the MP2925 vout telemetry has 49 vid step offset, but
  82. * PMBUS_VOUT_OV_FAULT_LIMIT and PMBUS_VOUT_UV_FAULT_LIMIT do not take
  83. * this into consideration, its resolution is 1.95mV/LSB, as a result,
  84. * format[PSC_VOLTAGE_OUT] can not be set to vid directly. Adding extra
  85. * vid_offset variable for vout telemetry.
  86. */
  87. ret = DIV_ROUND_CLOSEST(((ret & GENMASK(11, 0)) + data->vid_offset[page]) *
  88. data->vout_scale[page], MP2925_VOUT_DIV);
  89. break;
  90. case PMBUS_VOUT_OV_FAULT_LIMIT:
  91. case PMBUS_VOUT_UV_FAULT_LIMIT:
  92. ret = pmbus_read_word_data(client, page, phase, reg);
  93. if (ret < 0)
  94. return ret;
  95. ret = DIV_ROUND_CLOSEST((ret & GENMASK(11, 0)) * MP2925_VOUT_OVUV_UINT,
  96. MP2925_VOUT_OVUV_DIV);
  97. break;
  98. case PMBUS_STATUS_WORD:
  99. case PMBUS_READ_VIN:
  100. case PMBUS_READ_IOUT:
  101. case PMBUS_READ_POUT:
  102. case PMBUS_READ_PIN:
  103. case PMBUS_READ_IIN:
  104. case PMBUS_READ_TEMPERATURE_1:
  105. case PMBUS_VIN_OV_FAULT_LIMIT:
  106. case PMBUS_VIN_OV_WARN_LIMIT:
  107. case PMBUS_VIN_UV_WARN_LIMIT:
  108. case PMBUS_VIN_UV_FAULT_LIMIT:
  109. case PMBUS_IOUT_OC_FAULT_LIMIT:
  110. case PMBUS_IOUT_OC_WARN_LIMIT:
  111. case PMBUS_OT_FAULT_LIMIT:
  112. case PMBUS_OT_WARN_LIMIT:
  113. ret = -ENODATA;
  114. break;
  115. default:
  116. ret = -EINVAL;
  117. break;
  118. }
  119. return ret;
  120. }
  121. static int mp2925_write_word_data(struct i2c_client *client, int page, int reg,
  122. u16 word)
  123. {
  124. int ret;
  125. switch (reg) {
  126. case PMBUS_VIN_OV_FAULT_LIMIT:
  127. case PMBUS_VIN_OV_WARN_LIMIT:
  128. case PMBUS_VIN_UV_WARN_LIMIT:
  129. case PMBUS_VIN_UV_FAULT_LIMIT:
  130. /*
  131. * The PMBUS_VIN_OV_FAULT_LIMIT, PMBUS_VIN_OV_WARN_LIMIT,
  132. * PMBUS_VIN_UV_WARN_LIMIT and PMBUS_VIN_UV_FAULT_LIMIT
  133. * of MP2925 is linear11 format, and the exponent is a
  134. * constant value(5'b11100), so the exponent of word
  135. * parameter should be converted to 5'b11100(0x1C).
  136. */
  137. ret = pmbus_write_word_data(client, page, reg,
  138. mp2925_linear_exp_transfer(word, 0x1C));
  139. break;
  140. case PMBUS_VOUT_OV_FAULT_LIMIT:
  141. case PMBUS_VOUT_UV_FAULT_LIMIT:
  142. /*
  143. * The bit0-bit11 is the limit value, and bit12-bit15
  144. * should not be changed.
  145. */
  146. ret = pmbus_read_word_data(client, page, 0xff, reg);
  147. if (ret < 0)
  148. return ret;
  149. ret = pmbus_write_word_data(client, page, reg,
  150. (ret & ~GENMASK(11, 0)) |
  151. FIELD_PREP(GENMASK(11, 0),
  152. DIV_ROUND_CLOSEST(word * MP2925_VOUT_OVUV_DIV,
  153. MP2925_VOUT_OVUV_UINT)));
  154. break;
  155. case PMBUS_OT_FAULT_LIMIT:
  156. case PMBUS_OT_WARN_LIMIT:
  157. /*
  158. * The PMBUS_OT_FAULT_LIMIT and PMBUS_OT_WARN_LIMIT of
  159. * MP2925 is linear11 format, and the exponent is a
  160. * constant value(5'b00000), so the exponent of word
  161. * parameter should be converted to 5'b00000.
  162. */
  163. ret = pmbus_write_word_data(client, page, reg,
  164. mp2925_linear_exp_transfer(word, 0x00));
  165. break;
  166. case PMBUS_IOUT_OC_FAULT_LIMIT:
  167. case PMBUS_IOUT_OC_WARN_LIMIT:
  168. /*
  169. * The PMBUS_IOUT_OC_FAULT_LIMIT and PMBUS_IOUT_OC_WARN_LIMIT
  170. * of MP2925 is linear11 format, and the exponent can not be
  171. * changed.
  172. */
  173. ret = pmbus_read_word_data(client, page, 0xff, reg);
  174. if (ret < 0)
  175. return ret;
  176. ret = pmbus_write_word_data(client, page, reg,
  177. mp2925_linear_exp_transfer(word,
  178. FIELD_GET(GENMASK(15, 11),
  179. ret)));
  180. break;
  181. default:
  182. ret = -EINVAL;
  183. break;
  184. }
  185. return ret;
  186. }
  187. static int
  188. mp2925_identify_vout_scale(struct i2c_client *client, struct pmbus_driver_info *info,
  189. int page)
  190. {
  191. struct mp2925_data *data = to_mp2925_data(info);
  192. int ret;
  193. ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);
  194. if (ret < 0)
  195. return ret;
  196. ret = i2c_smbus_read_byte_data(client, PMBUS_VOUT_MODE);
  197. if (ret < 0)
  198. return ret;
  199. if (FIELD_GET(GENMASK(5, 5), ret)) {
  200. ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE,
  201. page == 0 ? 3 : 4);
  202. if (ret < 0)
  203. return ret;
  204. ret = i2c_smbus_read_word_data(client, MFR_VR_MULTI_CONFIG);
  205. if (ret < 0)
  206. return ret;
  207. if (FIELD_GET(GENMASK(5, 5), ret))
  208. data->vout_scale[page] = 2560;
  209. else
  210. data->vout_scale[page] = 5120;
  211. /*
  212. * In vid mode, the MP2925 vout telemetry has 49 vid step offset, but
  213. * PMBUS_VOUT_OV_FAULT_LIMIT and PMBUS_VOUT_UV_FAULT_LIMIT do not take
  214. * this into consideration, its resolution is 1.95mV/LSB, as a result,
  215. * format[PSC_VOLTAGE_OUT] can not be set to vid directly. Adding extra
  216. * vid_offset variable for vout telemetry.
  217. */
  218. data->vid_offset[page] = 49;
  219. } else if (FIELD_GET(GENMASK(4, 4), ret)) {
  220. data->vout_scale[page] = 1;
  221. data->vid_offset[page] = 0;
  222. } else {
  223. data->vout_scale[page] = 512;
  224. data->vid_offset[page] = 0;
  225. }
  226. return 0;
  227. }
  228. static int mp2925_identify(struct i2c_client *client, struct pmbus_driver_info *info)
  229. {
  230. int ret;
  231. ret = mp2925_identify_vout_scale(client, info, 0);
  232. if (ret < 0)
  233. return ret;
  234. return mp2925_identify_vout_scale(client, info, 1);
  235. }
  236. static const struct pmbus_driver_info mp2925_info = {
  237. .pages = MP2925_PAGE_NUM,
  238. .format[PSC_VOLTAGE_IN] = linear,
  239. .format[PSC_CURRENT_IN] = linear,
  240. .format[PSC_CURRENT_OUT] = linear,
  241. .format[PSC_POWER] = linear,
  242. .format[PSC_TEMPERATURE] = linear,
  243. .format[PSC_VOLTAGE_OUT] = direct,
  244. .m[PSC_VOLTAGE_OUT] = 1,
  245. .R[PSC_VOLTAGE_OUT] = 3,
  246. .b[PSC_VOLTAGE_OUT] = 0,
  247. .func[0] = MP2925_RAIL1_FUNC,
  248. .func[1] = MP2925_RAIL2_FUNC,
  249. .read_word_data = mp2925_read_word_data,
  250. .read_byte_data = mp2925_read_byte_data,
  251. .write_word_data = mp2925_write_word_data,
  252. .identify = mp2925_identify,
  253. };
  254. static int mp2925_probe(struct i2c_client *client)
  255. {
  256. struct mp2925_data *data;
  257. data = devm_kzalloc(&client->dev, sizeof(struct mp2925_data), GFP_KERNEL);
  258. if (!data)
  259. return -ENOMEM;
  260. memcpy(&data->info, &mp2925_info, sizeof(mp2925_info));
  261. return pmbus_do_probe(client, &data->info);
  262. }
  263. static const struct i2c_device_id mp2925_id[] = {
  264. {"mp2925"},
  265. {"mp2929"},
  266. {}
  267. };
  268. MODULE_DEVICE_TABLE(i2c, mp2925_id);
  269. static const struct of_device_id __maybe_unused mp2925_of_match[] = {
  270. {.compatible = "mps,mp2925"},
  271. {.compatible = "mps,mp2929"},
  272. {}
  273. };
  274. MODULE_DEVICE_TABLE(of, mp2925_of_match);
  275. static struct i2c_driver mp2925_driver = {
  276. .driver = {
  277. .name = "mp2925",
  278. .of_match_table = mp2925_of_match,
  279. },
  280. .probe = mp2925_probe,
  281. .id_table = mp2925_id,
  282. };
  283. module_i2c_driver(mp2925_driver);
  284. MODULE_AUTHOR("Wensheng Wang <wenswang@yeah.net>");
  285. MODULE_DESCRIPTION("PMBus driver for MPS MP2925");
  286. MODULE_LICENSE("GPL");
  287. MODULE_IMPORT_NS("PMBUS");