inspur-ipsps.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2019 Inspur Corp.
  4. */
  5. #include <linux/debugfs.h>
  6. #include <linux/device.h>
  7. #include <linux/fs.h>
  8. #include <linux/i2c.h>
  9. #include <linux/module.h>
  10. #include <linux/pmbus.h>
  11. #include <linux/hwmon-sysfs.h>
  12. #include "pmbus.h"
  13. #define IPSPS_REG_VENDOR_ID 0x99
  14. #define IPSPS_REG_MODEL 0x9A
  15. #define IPSPS_REG_FW_VERSION 0x9B
  16. #define IPSPS_REG_PN 0x9C
  17. #define IPSPS_REG_SN 0x9E
  18. #define IPSPS_REG_HW_VERSION 0xB0
  19. #define IPSPS_REG_MODE 0xFC
  20. #define MODE_ACTIVE 0x55
  21. #define MODE_STANDBY 0x0E
  22. #define MODE_REDUNDANCY 0x00
  23. #define MODE_ACTIVE_STRING "active"
  24. #define MODE_STANDBY_STRING "standby"
  25. #define MODE_REDUNDANCY_STRING "redundancy"
  26. enum ipsps_index {
  27. vendor,
  28. model,
  29. fw_version,
  30. part_number,
  31. serial_number,
  32. hw_version,
  33. mode,
  34. num_regs,
  35. };
  36. static const u8 ipsps_regs[num_regs] = {
  37. [vendor] = IPSPS_REG_VENDOR_ID,
  38. [model] = IPSPS_REG_MODEL,
  39. [fw_version] = IPSPS_REG_FW_VERSION,
  40. [part_number] = IPSPS_REG_PN,
  41. [serial_number] = IPSPS_REG_SN,
  42. [hw_version] = IPSPS_REG_HW_VERSION,
  43. [mode] = IPSPS_REG_MODE,
  44. };
  45. static ssize_t ipsps_string_show(struct device *dev,
  46. struct device_attribute *devattr,
  47. char *buf)
  48. {
  49. u8 reg;
  50. int rc;
  51. char *p;
  52. char data[I2C_SMBUS_BLOCK_MAX + 1];
  53. struct i2c_client *client = to_i2c_client(dev->parent);
  54. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  55. reg = ipsps_regs[attr->index];
  56. rc = i2c_smbus_read_block_data(client, reg, data);
  57. if (rc < 0)
  58. return rc;
  59. /* filled with printable characters, ending with # */
  60. p = memscan(data, '#', rc);
  61. *p = '\0';
  62. return sysfs_emit(buf, "%s\n", data);
  63. }
  64. static ssize_t ipsps_fw_version_show(struct device *dev,
  65. struct device_attribute *devattr,
  66. char *buf)
  67. {
  68. u8 reg;
  69. int rc;
  70. u8 data[I2C_SMBUS_BLOCK_MAX] = { 0 };
  71. struct i2c_client *client = to_i2c_client(dev->parent);
  72. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  73. reg = ipsps_regs[attr->index];
  74. rc = i2c_smbus_read_block_data(client, reg, data);
  75. if (rc < 0)
  76. return rc;
  77. if (rc != 6)
  78. return -EPROTO;
  79. return sysfs_emit(buf, "%u.%02u%u-%u.%02u\n",
  80. data[1], data[2]/* < 100 */, data[3]/*< 10*/,
  81. data[4], data[5]/* < 100 */);
  82. }
  83. static ssize_t ipsps_mode_show(struct device *dev,
  84. struct device_attribute *devattr, char *buf)
  85. {
  86. u8 reg;
  87. int rc;
  88. struct i2c_client *client = to_i2c_client(dev->parent);
  89. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  90. reg = ipsps_regs[attr->index];
  91. rc = i2c_smbus_read_byte_data(client, reg);
  92. if (rc < 0)
  93. return rc;
  94. switch (rc) {
  95. case MODE_ACTIVE:
  96. return sysfs_emit(buf, "[%s] %s %s\n",
  97. MODE_ACTIVE_STRING,
  98. MODE_STANDBY_STRING, MODE_REDUNDANCY_STRING);
  99. case MODE_STANDBY:
  100. return sysfs_emit(buf, "%s [%s] %s\n",
  101. MODE_ACTIVE_STRING,
  102. MODE_STANDBY_STRING, MODE_REDUNDANCY_STRING);
  103. case MODE_REDUNDANCY:
  104. return sysfs_emit(buf, "%s %s [%s]\n",
  105. MODE_ACTIVE_STRING,
  106. MODE_STANDBY_STRING, MODE_REDUNDANCY_STRING);
  107. default:
  108. return sysfs_emit(buf, "unspecified\n");
  109. }
  110. }
  111. static ssize_t ipsps_mode_store(struct device *dev,
  112. struct device_attribute *devattr,
  113. const char *buf, size_t count)
  114. {
  115. u8 reg;
  116. int rc;
  117. struct i2c_client *client = to_i2c_client(dev->parent);
  118. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  119. reg = ipsps_regs[attr->index];
  120. if (sysfs_streq(MODE_STANDBY_STRING, buf)) {
  121. rc = i2c_smbus_write_byte_data(client, reg,
  122. MODE_STANDBY);
  123. if (rc < 0)
  124. return rc;
  125. return count;
  126. } else if (sysfs_streq(MODE_ACTIVE_STRING, buf)) {
  127. rc = i2c_smbus_write_byte_data(client, reg,
  128. MODE_ACTIVE);
  129. if (rc < 0)
  130. return rc;
  131. return count;
  132. }
  133. return -EINVAL;
  134. }
  135. static SENSOR_DEVICE_ATTR_RO(vendor, ipsps_string, vendor);
  136. static SENSOR_DEVICE_ATTR_RO(model, ipsps_string, model);
  137. static SENSOR_DEVICE_ATTR_RO(part_number, ipsps_string, part_number);
  138. static SENSOR_DEVICE_ATTR_RO(serial_number, ipsps_string, serial_number);
  139. static SENSOR_DEVICE_ATTR_RO(hw_version, ipsps_string, hw_version);
  140. static SENSOR_DEVICE_ATTR_RO(fw_version, ipsps_fw_version, fw_version);
  141. static SENSOR_DEVICE_ATTR_RW(mode, ipsps_mode, mode);
  142. static struct attribute *ipsps_attrs[] = {
  143. &sensor_dev_attr_vendor.dev_attr.attr,
  144. &sensor_dev_attr_model.dev_attr.attr,
  145. &sensor_dev_attr_part_number.dev_attr.attr,
  146. &sensor_dev_attr_serial_number.dev_attr.attr,
  147. &sensor_dev_attr_hw_version.dev_attr.attr,
  148. &sensor_dev_attr_fw_version.dev_attr.attr,
  149. &sensor_dev_attr_mode.dev_attr.attr,
  150. NULL,
  151. };
  152. ATTRIBUTE_GROUPS(ipsps);
  153. static struct pmbus_driver_info ipsps_info = {
  154. .pages = 1,
  155. .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
  156. PMBUS_HAVE_IIN | PMBUS_HAVE_POUT | PMBUS_HAVE_PIN |
  157. PMBUS_HAVE_FAN12 | PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 |
  158. PMBUS_HAVE_TEMP3 | PMBUS_HAVE_STATUS_VOUT |
  159. PMBUS_HAVE_STATUS_IOUT | PMBUS_HAVE_STATUS_INPUT |
  160. PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_STATUS_FAN12,
  161. .groups = ipsps_groups,
  162. };
  163. static struct pmbus_platform_data ipsps_pdata = {
  164. .flags = PMBUS_SKIP_STATUS_CHECK,
  165. };
  166. static int ipsps_probe(struct i2c_client *client)
  167. {
  168. client->dev.platform_data = &ipsps_pdata;
  169. return pmbus_do_probe(client, &ipsps_info);
  170. }
  171. static const struct i2c_device_id ipsps_id[] = {
  172. { "ipsps1", 0 },
  173. {}
  174. };
  175. MODULE_DEVICE_TABLE(i2c, ipsps_id);
  176. #ifdef CONFIG_OF
  177. static const struct of_device_id ipsps_of_match[] = {
  178. { .compatible = "inspur,ipsps1" },
  179. {}
  180. };
  181. MODULE_DEVICE_TABLE(of, ipsps_of_match);
  182. #endif
  183. static struct i2c_driver ipsps_driver = {
  184. .driver = {
  185. .name = "inspur-ipsps",
  186. .of_match_table = of_match_ptr(ipsps_of_match),
  187. },
  188. .probe_new = ipsps_probe,
  189. .id_table = ipsps_id,
  190. };
  191. module_i2c_driver(ipsps_driver);
  192. MODULE_AUTHOR("John Wang");
  193. MODULE_DESCRIPTION("PMBus driver for Inspur Power System power supplies");
  194. MODULE_LICENSE("GPL");
  195. MODULE_IMPORT_NS(PMBUS);