pli1209bc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Hardware monitoring driver for Vicor PLI1209BC Digital Supervisor
  4. *
  5. * Copyright (c) 2022 9elements GmbH
  6. */
  7. #include <linux/i2c.h>
  8. #include <linux/module.h>
  9. #include <linux/pmbus.h>
  10. #include <linux/regulator/driver.h>
  11. #include "pmbus.h"
  12. /*
  13. * The capability command is only supported at page 0. Probing the device while
  14. * the page register is set to 1 will falsely enable PEC support. Disable
  15. * capability probing accordingly, since the PLI1209BC does not have any
  16. * additional capabilities.
  17. */
  18. static struct pmbus_platform_data pli1209bc_plat_data = {
  19. .flags = PMBUS_NO_CAPABILITY,
  20. };
  21. static int pli1209bc_read_word_data(struct i2c_client *client, int page,
  22. int phase, int reg)
  23. {
  24. int data;
  25. switch (reg) {
  26. /* PMBUS_READ_POUT uses a direct format with R=0 */
  27. case PMBUS_READ_POUT:
  28. data = pmbus_read_word_data(client, page, phase, reg);
  29. if (data < 0)
  30. return data;
  31. data = sign_extend32(data, 15) * 10;
  32. return clamp_val(data, -32768, 32767) & 0xffff;
  33. /*
  34. * PMBUS_READ_VOUT and PMBUS_READ_TEMPERATURE_1 return invalid data
  35. * when the BCM is turned off. Since it is not possible to return
  36. * ENODATA error, return zero instead.
  37. */
  38. case PMBUS_READ_VOUT:
  39. case PMBUS_READ_TEMPERATURE_1:
  40. data = pmbus_read_word_data(client, page, phase,
  41. PMBUS_STATUS_WORD);
  42. if (data < 0)
  43. return data;
  44. if (data & PB_STATUS_POWER_GOOD_N)
  45. return 0;
  46. return pmbus_read_word_data(client, page, phase, reg);
  47. default:
  48. return -ENODATA;
  49. }
  50. }
  51. #if IS_ENABLED(CONFIG_SENSORS_PLI1209BC_REGULATOR)
  52. static const struct regulator_desc pli1209bc_reg_desc = {
  53. .name = "vout2",
  54. .id = 1,
  55. .of_match = of_match_ptr("vout2"),
  56. .regulators_node = of_match_ptr("regulators"),
  57. .ops = &pmbus_regulator_ops,
  58. .type = REGULATOR_VOLTAGE,
  59. .owner = THIS_MODULE,
  60. };
  61. #endif
  62. static struct pmbus_driver_info pli1209bc_info = {
  63. .pages = 2,
  64. .format[PSC_VOLTAGE_IN] = direct,
  65. .format[PSC_VOLTAGE_OUT] = direct,
  66. .format[PSC_CURRENT_IN] = direct,
  67. .format[PSC_CURRENT_OUT] = direct,
  68. .format[PSC_POWER] = direct,
  69. .format[PSC_TEMPERATURE] = direct,
  70. .m[PSC_VOLTAGE_IN] = 1,
  71. .b[PSC_VOLTAGE_IN] = 0,
  72. .R[PSC_VOLTAGE_IN] = 1,
  73. .m[PSC_VOLTAGE_OUT] = 1,
  74. .b[PSC_VOLTAGE_OUT] = 0,
  75. .R[PSC_VOLTAGE_OUT] = 1,
  76. .m[PSC_CURRENT_IN] = 1,
  77. .b[PSC_CURRENT_IN] = 0,
  78. .R[PSC_CURRENT_IN] = 3,
  79. .m[PSC_CURRENT_OUT] = 1,
  80. .b[PSC_CURRENT_OUT] = 0,
  81. .R[PSC_CURRENT_OUT] = 2,
  82. .m[PSC_POWER] = 1,
  83. .b[PSC_POWER] = 0,
  84. .R[PSC_POWER] = 1,
  85. .m[PSC_TEMPERATURE] = 1,
  86. .b[PSC_TEMPERATURE] = 0,
  87. .R[PSC_TEMPERATURE] = 0,
  88. /*
  89. * Page 0 sums up all attributes except voltage readings.
  90. * The pli1209 digital supervisor only contains a single BCM, making
  91. * page 0 redundant.
  92. */
  93. .func[1] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT
  94. | PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT
  95. | PMBUS_HAVE_PIN | PMBUS_HAVE_POUT
  96. | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP
  97. | PMBUS_HAVE_STATUS_IOUT | PMBUS_HAVE_STATUS_INPUT,
  98. .read_word_data = pli1209bc_read_word_data,
  99. #if IS_ENABLED(CONFIG_SENSORS_PLI1209BC_REGULATOR)
  100. .num_regulators = 1,
  101. .reg_desc = &pli1209bc_reg_desc,
  102. #endif
  103. };
  104. static int pli1209bc_probe(struct i2c_client *client)
  105. {
  106. client->dev.platform_data = &pli1209bc_plat_data;
  107. return pmbus_do_probe(client, &pli1209bc_info);
  108. }
  109. static const struct i2c_device_id pli1209bc_id[] = {
  110. {"pli1209bc", 0},
  111. {}
  112. };
  113. MODULE_DEVICE_TABLE(i2c, pli1209bc_id);
  114. #ifdef CONFIG_OF
  115. static const struct of_device_id pli1209bc_of_match[] = {
  116. { .compatible = "vicor,pli1209bc" },
  117. { },
  118. };
  119. MODULE_DEVICE_TABLE(of, pli1209bc_of_match);
  120. #endif
  121. static struct i2c_driver pli1209bc_driver = {
  122. .driver = {
  123. .name = "pli1209bc",
  124. .of_match_table = of_match_ptr(pli1209bc_of_match),
  125. },
  126. .probe_new = pli1209bc_probe,
  127. .id_table = pli1209bc_id,
  128. };
  129. module_i2c_driver(pli1209bc_driver);
  130. MODULE_AUTHOR("Marcello Sylvester Bauer <[email protected]>");
  131. MODULE_DESCRIPTION("PMBus driver for Vicor PLI1209BC");
  132. MODULE_LICENSE("GPL");
  133. MODULE_IMPORT_NS(PMBUS);