tps53679.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Hardware monitoring driver for Texas Instruments TPS53679
  4. *
  5. * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
  6. * Copyright (c) 2017 Vadim Pasternak <[email protected]>
  7. */
  8. #include <linux/bits.h>
  9. #include <linux/err.h>
  10. #include <linux/i2c.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of_device.h>
  15. #include "pmbus.h"
  16. enum chips {
  17. tps53647, tps53667, tps53676, tps53679, tps53681, tps53688
  18. };
  19. #define TPS53647_PAGE_NUM 1
  20. #define TPS53676_USER_DATA_03 0xb3
  21. #define TPS53676_MAX_PHASES 7
  22. #define TPS53679_PROT_VR12_5MV 0x01 /* VR12.0 mode, 5-mV DAC */
  23. #define TPS53679_PROT_VR12_5_10MV 0x02 /* VR12.5 mode, 10-mV DAC */
  24. #define TPS53679_PROT_VR13_10MV 0x04 /* VR13.0 mode, 10-mV DAC */
  25. #define TPS53679_PROT_IMVP8_5MV 0x05 /* IMVP8 mode, 5-mV DAC */
  26. #define TPS53679_PROT_VR13_5MV 0x07 /* VR13.0 mode, 5-mV DAC */
  27. #define TPS53679_PAGE_NUM 2
  28. #define TPS53681_DEVICE_ID 0x81
  29. #define TPS53681_PMBUS_REVISION 0x33
  30. #define TPS53681_MFR_SPECIFIC_20 0xe4 /* Number of phases, per page */
  31. static const struct i2c_device_id tps53679_id[];
  32. static int tps53679_identify_mode(struct i2c_client *client,
  33. struct pmbus_driver_info *info)
  34. {
  35. u8 vout_params;
  36. int i, ret;
  37. for (i = 0; i < info->pages; i++) {
  38. /* Read the register with VOUT scaling value.*/
  39. ret = pmbus_read_byte_data(client, i, PMBUS_VOUT_MODE);
  40. if (ret < 0)
  41. return ret;
  42. vout_params = ret & GENMASK(4, 0);
  43. switch (vout_params) {
  44. case TPS53679_PROT_VR13_10MV:
  45. case TPS53679_PROT_VR12_5_10MV:
  46. info->vrm_version[i] = vr13;
  47. break;
  48. case TPS53679_PROT_VR13_5MV:
  49. case TPS53679_PROT_VR12_5MV:
  50. case TPS53679_PROT_IMVP8_5MV:
  51. info->vrm_version[i] = vr12;
  52. break;
  53. default:
  54. return -EINVAL;
  55. }
  56. }
  57. return 0;
  58. }
  59. static int tps53679_identify_phases(struct i2c_client *client,
  60. struct pmbus_driver_info *info)
  61. {
  62. int ret;
  63. /* On TPS53681, only channel A provides per-phase output current */
  64. ret = pmbus_read_byte_data(client, 0, TPS53681_MFR_SPECIFIC_20);
  65. if (ret < 0)
  66. return ret;
  67. info->phases[0] = (ret & 0x07) + 1;
  68. return 0;
  69. }
  70. static int tps53679_identify_chip(struct i2c_client *client,
  71. u8 revision, u16 id)
  72. {
  73. u8 buf[I2C_SMBUS_BLOCK_MAX];
  74. int ret;
  75. ret = pmbus_read_byte_data(client, 0, PMBUS_REVISION);
  76. if (ret < 0)
  77. return ret;
  78. if (ret != revision) {
  79. dev_err(&client->dev, "Unexpected PMBus revision 0x%x\n", ret);
  80. return -ENODEV;
  81. }
  82. ret = i2c_smbus_read_block_data(client, PMBUS_IC_DEVICE_ID, buf);
  83. if (ret < 0)
  84. return ret;
  85. if (ret != 1 || buf[0] != id) {
  86. dev_err(&client->dev, "Unexpected device ID 0x%x\n", buf[0]);
  87. return -ENODEV;
  88. }
  89. return 0;
  90. }
  91. /*
  92. * Common identification function for chips with multi-phase support.
  93. * Since those chips have special configuration registers, we want to have
  94. * some level of reassurance that we are really talking with the chip
  95. * being probed. Check PMBus revision and chip ID.
  96. */
  97. static int tps53679_identify_multiphase(struct i2c_client *client,
  98. struct pmbus_driver_info *info,
  99. int pmbus_rev, int device_id)
  100. {
  101. int ret;
  102. ret = tps53679_identify_chip(client, pmbus_rev, device_id);
  103. if (ret < 0)
  104. return ret;
  105. ret = tps53679_identify_mode(client, info);
  106. if (ret < 0)
  107. return ret;
  108. return tps53679_identify_phases(client, info);
  109. }
  110. static int tps53679_identify(struct i2c_client *client,
  111. struct pmbus_driver_info *info)
  112. {
  113. return tps53679_identify_mode(client, info);
  114. }
  115. static int tps53681_identify(struct i2c_client *client,
  116. struct pmbus_driver_info *info)
  117. {
  118. return tps53679_identify_multiphase(client, info,
  119. TPS53681_PMBUS_REVISION,
  120. TPS53681_DEVICE_ID);
  121. }
  122. static int tps53676_identify(struct i2c_client *client,
  123. struct pmbus_driver_info *info)
  124. {
  125. u8 buf[I2C_SMBUS_BLOCK_MAX];
  126. int phases_a = 0, phases_b = 0;
  127. int i, ret;
  128. ret = i2c_smbus_read_block_data(client, PMBUS_IC_DEVICE_ID, buf);
  129. if (ret < 0)
  130. return ret;
  131. if (strncmp("TI\x53\x67\x60", buf, 5)) {
  132. dev_err(&client->dev, "Unexpected device ID: %s\n", buf);
  133. return -ENODEV;
  134. }
  135. ret = i2c_smbus_read_block_data(client, TPS53676_USER_DATA_03, buf);
  136. if (ret < 0)
  137. return ret;
  138. if (ret != 24)
  139. return -EIO;
  140. for (i = 0; i < 2 * TPS53676_MAX_PHASES; i += 2) {
  141. if (buf[i + 1] & 0x80) {
  142. if (buf[i] & 0x08)
  143. phases_b++;
  144. else
  145. phases_a++;
  146. }
  147. }
  148. info->format[PSC_VOLTAGE_OUT] = linear;
  149. info->pages = 1;
  150. info->phases[0] = phases_a;
  151. if (phases_b > 0) {
  152. info->pages = 2;
  153. info->phases[1] = phases_b;
  154. }
  155. return 0;
  156. }
  157. static int tps53681_read_word_data(struct i2c_client *client, int page,
  158. int phase, int reg)
  159. {
  160. /*
  161. * For reading the total output current (READ_IOUT) for all phases,
  162. * the chip datasheet is a bit vague. It says "PHASE must be set to
  163. * FFh to access all phases simultaneously. PHASE may also be set to
  164. * 80h readack (!) the total phase current".
  165. * Experiments show that the command does _not_ report the total
  166. * current for all phases if the phase is set to 0xff. Instead, it
  167. * appears to report the current of one of the phases. Override phase
  168. * parameter with 0x80 when reading the total output current on page 0.
  169. */
  170. if (reg == PMBUS_READ_IOUT && page == 0 && phase == 0xff)
  171. return pmbus_read_word_data(client, page, 0x80, reg);
  172. return -ENODATA;
  173. }
  174. static struct pmbus_driver_info tps53679_info = {
  175. .format[PSC_VOLTAGE_IN] = linear,
  176. .format[PSC_VOLTAGE_OUT] = vid,
  177. .format[PSC_TEMPERATURE] = linear,
  178. .format[PSC_CURRENT_OUT] = linear,
  179. .format[PSC_POWER] = linear,
  180. .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN | PMBUS_HAVE_PIN |
  181. PMBUS_HAVE_STATUS_INPUT |
  182. PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
  183. PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
  184. PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
  185. PMBUS_HAVE_POUT,
  186. .func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
  187. PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
  188. PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
  189. PMBUS_HAVE_POUT,
  190. .pfunc[0] = PMBUS_HAVE_IOUT,
  191. .pfunc[1] = PMBUS_HAVE_IOUT,
  192. .pfunc[2] = PMBUS_HAVE_IOUT,
  193. .pfunc[3] = PMBUS_HAVE_IOUT,
  194. .pfunc[4] = PMBUS_HAVE_IOUT,
  195. .pfunc[5] = PMBUS_HAVE_IOUT,
  196. .pfunc[6] = PMBUS_HAVE_IOUT,
  197. };
  198. static int tps53679_probe(struct i2c_client *client)
  199. {
  200. struct device *dev = &client->dev;
  201. struct pmbus_driver_info *info;
  202. enum chips chip_id;
  203. if (dev->of_node)
  204. chip_id = (enum chips)of_device_get_match_data(dev);
  205. else
  206. chip_id = i2c_match_id(tps53679_id, client)->driver_data;
  207. info = devm_kmemdup(dev, &tps53679_info, sizeof(*info), GFP_KERNEL);
  208. if (!info)
  209. return -ENOMEM;
  210. switch (chip_id) {
  211. case tps53647:
  212. case tps53667:
  213. info->pages = TPS53647_PAGE_NUM;
  214. info->identify = tps53679_identify;
  215. break;
  216. case tps53676:
  217. info->identify = tps53676_identify;
  218. break;
  219. case tps53679:
  220. case tps53688:
  221. info->pages = TPS53679_PAGE_NUM;
  222. info->identify = tps53679_identify;
  223. break;
  224. case tps53681:
  225. info->pages = TPS53679_PAGE_NUM;
  226. info->phases[0] = 6;
  227. info->identify = tps53681_identify;
  228. info->read_word_data = tps53681_read_word_data;
  229. break;
  230. default:
  231. return -ENODEV;
  232. }
  233. return pmbus_do_probe(client, info);
  234. }
  235. static const struct i2c_device_id tps53679_id[] = {
  236. {"bmr474", tps53676},
  237. {"tps53647", tps53647},
  238. {"tps53667", tps53667},
  239. {"tps53676", tps53676},
  240. {"tps53679", tps53679},
  241. {"tps53681", tps53681},
  242. {"tps53688", tps53688},
  243. {}
  244. };
  245. MODULE_DEVICE_TABLE(i2c, tps53679_id);
  246. static const struct of_device_id __maybe_unused tps53679_of_match[] = {
  247. {.compatible = "ti,tps53647", .data = (void *)tps53647},
  248. {.compatible = "ti,tps53667", .data = (void *)tps53667},
  249. {.compatible = "ti,tps53676", .data = (void *)tps53676},
  250. {.compatible = "ti,tps53679", .data = (void *)tps53679},
  251. {.compatible = "ti,tps53681", .data = (void *)tps53681},
  252. {.compatible = "ti,tps53688", .data = (void *)tps53688},
  253. {}
  254. };
  255. MODULE_DEVICE_TABLE(of, tps53679_of_match);
  256. static struct i2c_driver tps53679_driver = {
  257. .driver = {
  258. .name = "tps53679",
  259. .of_match_table = of_match_ptr(tps53679_of_match),
  260. },
  261. .probe_new = tps53679_probe,
  262. .id_table = tps53679_id,
  263. };
  264. module_i2c_driver(tps53679_driver);
  265. MODULE_AUTHOR("Vadim Pasternak <[email protected]>");
  266. MODULE_DESCRIPTION("PMBus driver for Texas Instruments TPS53679");
  267. MODULE_LICENSE("GPL");
  268. MODULE_IMPORT_NS(PMBUS);