mp2856.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Hardware monitoring driver for MPS2856/2857
  4. * Monolithic Power Systems VR Controllers
  5. *
  6. * Copyright (C) 2023 Quanta Computer lnc.
  7. */
  8. #include <linux/err.h>
  9. #include <linux/i2c.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/pmbus.h>
  14. #include "pmbus.h"
  15. /* Vendor specific registers. */
  16. #define MP2856_MFR_VR_MULTI_CONFIG_R1 0x0d
  17. #define MP2856_MFR_VR_MULTI_CONFIG_R2 0x1d
  18. #define MP2856_MUL1_BOOT_SR_R2 0x10
  19. #define MP2856_VR_ACTIVE BIT(15)
  20. #define MP2856_MFR_VR_CONFIG2 0x5e
  21. #define MP2856_VOUT_MODE BIT(11)
  22. #define MP2856_MFR_VR_CONFIG1 0x68
  23. #define MP2856_DRMOS_KCS GENMASK(13, 12)
  24. #define MP2856_MFR_READ_CS1_2_R1 0x82
  25. #define MP2856_MFR_READ_CS3_4_R1 0x83
  26. #define MP2856_MFR_READ_CS5_6_R1 0x84
  27. #define MP2856_MFR_READ_CS7_8_R1 0x85
  28. #define MP2856_MFR_READ_CS9_10_R1 0x86
  29. #define MP2856_MFR_READ_CS11_12_R1 0x87
  30. #define MP2856_MFR_READ_CS1_2_R2 0x85
  31. #define MP2856_MFR_READ_CS3_4_R2 0x86
  32. #define MP2856_MFR_READ_CS5_6_R2 0x87
  33. #define MP2856_MAX_PHASE_RAIL1 8
  34. #define MP2856_MAX_PHASE_RAIL2 4
  35. #define MP2857_MAX_PHASE_RAIL1 12
  36. #define MP2857_MAX_PHASE_RAIL2 4
  37. #define MP2856_PAGE_NUM 2
  38. enum chips { mp2856, mp2857 };
  39. static const int mp2856_max_phases[][MP2856_PAGE_NUM] = {
  40. [mp2856] = { MP2856_MAX_PHASE_RAIL1, MP2856_MAX_PHASE_RAIL2 },
  41. [mp2857] = { MP2857_MAX_PHASE_RAIL1, MP2857_MAX_PHASE_RAIL2 },
  42. };
  43. static const struct i2c_device_id mp2856_id[] = {
  44. {"mp2856", mp2856},
  45. {"mp2857", mp2857},
  46. {}
  47. };
  48. MODULE_DEVICE_TABLE(i2c, mp2856_id);
  49. struct mp2856_data {
  50. struct pmbus_driver_info info;
  51. int vout_format[MP2856_PAGE_NUM];
  52. int curr_sense_gain[MP2856_PAGE_NUM];
  53. int max_phases[MP2856_PAGE_NUM];
  54. };
  55. #define to_mp2856_data(x) container_of(x, struct mp2856_data, info)
  56. #define MAX_LIN_MANTISSA (1023 * 1000)
  57. #define MIN_LIN_MANTISSA (511 * 1000)
  58. static u16 val2linear11(s64 val)
  59. {
  60. s16 exponent = 0, mantissa;
  61. bool negative = false;
  62. if (val == 0)
  63. return 0;
  64. if (val < 0) {
  65. negative = true;
  66. val = -val;
  67. }
  68. /* Reduce large mantissa until it fits into 10 bit */
  69. while (val >= MAX_LIN_MANTISSA && exponent < 15) {
  70. exponent++;
  71. val >>= 1;
  72. }
  73. /* Increase small mantissa to improve precision */
  74. while (val < MIN_LIN_MANTISSA && exponent > -15) {
  75. exponent--;
  76. val <<= 1;
  77. }
  78. /* Convert mantissa from milli-units to units */
  79. mantissa = clamp_val(DIV_ROUND_CLOSEST_ULL(val, 1000), 0, 0x3ff);
  80. /* restore sign */
  81. if (negative)
  82. mantissa = -mantissa;
  83. /* Convert to 5 bit exponent, 11 bit mantissa */
  84. return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
  85. }
  86. static int
  87. mp2856_read_word_helper(struct i2c_client *client, int page, int phase, u8 reg,
  88. u16 mask)
  89. {
  90. int ret = pmbus_read_word_data(client, page, phase, reg);
  91. return (ret > 0) ? ret & mask : ret;
  92. }
  93. static int
  94. mp2856_read_vout(struct i2c_client *client, struct mp2856_data *data, int page,
  95. int phase, u8 reg)
  96. {
  97. int ret;
  98. ret = mp2856_read_word_helper(client, page, phase, reg,
  99. GENMASK(9, 0));
  100. if (ret < 0)
  101. return ret;
  102. /* convert vout result to direct format */
  103. ret = (data->vout_format[page] == vid) ?
  104. ((ret + 49) * 5) : ((ret * 1000) >> 8);
  105. return ret;
  106. }
  107. static int
  108. mp2856_read_phase(struct i2c_client *client, struct mp2856_data *data,
  109. int page, int phase, u8 reg)
  110. {
  111. int ret;
  112. int val;
  113. ret = pmbus_read_word_data(client, page, phase, reg);
  114. if (ret < 0)
  115. return ret;
  116. if (!((phase + 1) % MP2856_PAGE_NUM))
  117. ret >>= 8;
  118. ret &= 0xff;
  119. /*
  120. * Output value is calculated as: (READ_CSx * 12.5mV - 1.23V) / (Kcs * Rcs)
  121. */
  122. val = (ret * 125) - 12300;
  123. return val2linear11(val);
  124. }
  125. static int
  126. mp2856_read_phases(struct i2c_client *client, struct mp2856_data *data,
  127. int page, int phase)
  128. {
  129. int ret;
  130. if (page == 0) {
  131. switch (phase) {
  132. case 0 ... 1:
  133. ret = mp2856_read_phase(client, data, page, phase,
  134. MP2856_MFR_READ_CS1_2_R1);
  135. break;
  136. case 2 ... 3:
  137. ret = mp2856_read_phase(client, data, page, phase,
  138. MP2856_MFR_READ_CS3_4_R1);
  139. break;
  140. case 4 ... 5:
  141. ret = mp2856_read_phase(client, data, page, phase,
  142. MP2856_MFR_READ_CS5_6_R1);
  143. break;
  144. case 6 ... 7:
  145. ret = mp2856_read_phase(client, data, page, phase,
  146. MP2856_MFR_READ_CS7_8_R1);
  147. break;
  148. default:
  149. return -ENODATA;
  150. }
  151. } else {
  152. switch (phase) {
  153. case 0 ... 1:
  154. ret = mp2856_read_phase(client, data, page, phase,
  155. MP2856_MFR_READ_CS1_2_R2);
  156. break;
  157. case 2 ... 3:
  158. ret = mp2856_read_phase(client, data, page, phase,
  159. MP2856_MFR_READ_CS1_2_R2);
  160. break;
  161. default:
  162. return -ENODATA;
  163. }
  164. }
  165. return ret;
  166. }
  167. static int
  168. mp2856_read_word_data(struct i2c_client *client, int page,
  169. int phase, int reg)
  170. {
  171. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  172. struct mp2856_data *data = to_mp2856_data(info);
  173. int ret;
  174. switch (reg) {
  175. case PMBUS_READ_VOUT:
  176. ret = mp2856_read_vout(client, data, page, phase, reg);
  177. break;
  178. case PMBUS_READ_IOUT:
  179. if (phase != 0xff)
  180. ret = mp2856_read_phases(client, data, page, phase);
  181. else
  182. ret = pmbus_read_word_data(client, page, phase, reg);
  183. break;
  184. default:
  185. return -ENODATA;
  186. }
  187. return ret;
  188. }
  189. static int
  190. mp2856_read_byte_data(struct i2c_client *client, int page, int reg)
  191. {
  192. switch (reg) {
  193. case PMBUS_VOUT_MODE:
  194. /* Enforce VOUT direct format. */
  195. return PB_VOUT_MODE_DIRECT;
  196. default:
  197. return -ENODATA;
  198. }
  199. }
  200. static int
  201. mp2856_identify_multiphase(struct i2c_client *client, u8 reg, u8 max_phase,
  202. u16 mask)
  203. {
  204. int ret;
  205. ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
  206. if (ret < 0)
  207. return ret;
  208. ret = i2c_smbus_read_word_data(client, reg);
  209. if (ret < 0)
  210. return ret;
  211. ret &= mask;
  212. return (ret >= max_phase) ? max_phase : ret;
  213. }
  214. static int
  215. mp2856_identify_multiphase_rail1(struct i2c_client *client,
  216. struct mp2856_data *data)
  217. {
  218. int ret, i;
  219. ret = mp2856_identify_multiphase(client, MP2856_MFR_VR_MULTI_CONFIG_R1,
  220. MP2856_MAX_PHASE_RAIL1, GENMASK(3, 0));
  221. if (ret < 0)
  222. return ret;
  223. data->info.phases[0] = (ret > data->max_phases[0]) ?
  224. data->max_phases[0] : ret;
  225. for (i = 0 ; i < data->info.phases[0]; i++)
  226. data->info.pfunc[i] |= PMBUS_HAVE_IOUT;
  227. return 0;
  228. }
  229. static int
  230. mp2856_identify_multiphase_rail2(struct i2c_client *client,
  231. struct mp2856_data *data)
  232. {
  233. int ret, i;
  234. ret = mp2856_identify_multiphase(client, MP2856_MFR_VR_MULTI_CONFIG_R2,
  235. MP2856_MAX_PHASE_RAIL2, GENMASK(2, 0));
  236. if (ret < 0)
  237. return ret;
  238. data->info.phases[1] = (ret > data->max_phases[1]) ?
  239. data->max_phases[1] : ret;
  240. for (i = 0 ; i < data->info.phases[0]; i++)
  241. data->info.pfunc[i] |= PMBUS_HAVE_IOUT;
  242. return 0;
  243. }
  244. static int
  245. mp2856_current_sense_gain_get(struct i2c_client *client,
  246. struct mp2856_data *data)
  247. {
  248. int i, ret;
  249. /*
  250. * Obtain DrMOS current sense gain of power stage from the register
  251. * MP2856_MFR_VR_CONFIG1, bits 13-12. The value is selected as below:
  252. * 00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A. Other
  253. * values are invalid.
  254. */
  255. for (i = 0 ; i < data->info.pages; i++) {
  256. ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
  257. if (ret < 0)
  258. return ret;
  259. ret = i2c_smbus_read_word_data(client,
  260. MP2856_MFR_VR_CONFIG1);
  261. if (ret < 0)
  262. return ret;
  263. switch ((ret & MP2856_DRMOS_KCS) >> 12) {
  264. case 0:
  265. data->curr_sense_gain[i] = 50;
  266. break;
  267. case 1:
  268. data->curr_sense_gain[i] = 85;
  269. break;
  270. case 2:
  271. data->curr_sense_gain[i] = 97;
  272. break;
  273. default:
  274. data->curr_sense_gain[i] = 100;
  275. break;
  276. }
  277. }
  278. return 0;
  279. }
  280. static int
  281. mp2856_identify_vout_format(struct i2c_client *client,
  282. struct mp2856_data *data)
  283. {
  284. int i, ret;
  285. for (i = 0; i < data->info.pages; i++) {
  286. ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
  287. if (ret < 0)
  288. return ret;
  289. ret = i2c_smbus_read_word_data(client, MP2856_MFR_VR_CONFIG2);
  290. if (ret < 0)
  291. return ret;
  292. data->vout_format[i] = (ret & MP2856_VOUT_MODE) ? linear : vid;
  293. }
  294. return 0;
  295. }
  296. static bool
  297. mp2856_is_rail2_active(struct i2c_client *client)
  298. {
  299. int ret;
  300. ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
  301. if (ret < 0)
  302. return true;
  303. ret = i2c_smbus_read_word_data(client, MP2856_MUL1_BOOT_SR_R2);
  304. if (ret < 0)
  305. return true;
  306. return (ret & MP2856_VR_ACTIVE) ? true : false;
  307. }
  308. static struct pmbus_driver_info mp2856_info = {
  309. .pages = MP2856_PAGE_NUM,
  310. .format[PSC_VOLTAGE_IN] = linear,
  311. .format[PSC_VOLTAGE_OUT] = direct,
  312. .format[PSC_TEMPERATURE] = linear,
  313. .format[PSC_CURRENT_IN] = linear,
  314. .format[PSC_CURRENT_OUT] = linear,
  315. .format[PSC_POWER] = linear,
  316. .m[PSC_VOLTAGE_OUT] = 1,
  317. .R[PSC_VOLTAGE_OUT] = 3,
  318. .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
  319. PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
  320. PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_POUT |
  321. PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT,
  322. .func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_IOUT |
  323. PMBUS_HAVE_STATUS_IOUT | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP,
  324. .read_byte_data = mp2856_read_byte_data,
  325. .read_word_data = mp2856_read_word_data,
  326. };
  327. static int mp2856_probe(struct i2c_client *client)
  328. {
  329. struct pmbus_driver_info *info;
  330. struct mp2856_data *data;
  331. enum chips chip_id;
  332. int ret;
  333. data = devm_kzalloc(&client->dev, sizeof(struct mp2856_data),
  334. GFP_KERNEL);
  335. if (!data)
  336. return -ENOMEM;
  337. chip_id = (kernel_ulong_t)i2c_get_match_data(client);
  338. memcpy(data->max_phases, mp2856_max_phases[chip_id],
  339. sizeof(data->max_phases));
  340. memcpy(&data->info, &mp2856_info, sizeof(*info));
  341. info = &data->info;
  342. /* Identify multiphase configuration. */
  343. ret = mp2856_identify_multiphase_rail1(client, data);
  344. if (ret < 0)
  345. return ret;
  346. if (mp2856_is_rail2_active(client)) {
  347. ret = mp2856_identify_multiphase_rail2(client, data);
  348. if (ret < 0)
  349. return ret;
  350. } else {
  351. /* rail2 is not active */
  352. info->pages = 1;
  353. }
  354. /* Obtain current sense gain of power stage. */
  355. ret = mp2856_current_sense_gain_get(client, data);
  356. if (ret)
  357. return ret;
  358. /* Identify vout format. */
  359. ret = mp2856_identify_vout_format(client, data);
  360. if (ret)
  361. return ret;
  362. /* set the device to page 0 */
  363. i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
  364. return pmbus_do_probe(client, info);
  365. }
  366. static const struct of_device_id __maybe_unused mp2856_of_match[] = {
  367. {.compatible = "mps,mp2856", .data = (void *)mp2856},
  368. {.compatible = "mps,mp2857", .data = (void *)mp2857},
  369. {}
  370. };
  371. MODULE_DEVICE_TABLE(of, mp2856_of_match);
  372. static struct i2c_driver mp2856_driver = {
  373. .driver = {
  374. .name = "mp2856",
  375. .of_match_table = mp2856_of_match,
  376. },
  377. .probe = mp2856_probe,
  378. .id_table = mp2856_id,
  379. };
  380. module_i2c_driver(mp2856_driver);
  381. MODULE_AUTHOR("Peter Yin <peter.yin@quantatw.com>");
  382. MODULE_DESCRIPTION("PMBus driver for MPS MP2856/MP2857 device");
  383. MODULE_LICENSE("GPL");
  384. MODULE_IMPORT_NS("PMBUS");