ltc4260.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for Linear Technology LTC4260 I2C Positive Voltage Hot Swap Controller
  4. *
  5. * Copyright (c) 2014 Guenter Roeck
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/err.h>
  10. #include <linux/slab.h>
  11. #include <linux/i2c.h>
  12. #include <linux/hwmon.h>
  13. #include <linux/hwmon-sysfs.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/regmap.h>
  16. /* chip registers */
  17. #define LTC4260_CONTROL 0x00
  18. #define LTC4260_ALERT 0x01
  19. #define LTC4260_STATUS 0x02
  20. #define LTC4260_FAULT 0x03
  21. #define LTC4260_SENSE 0x04
  22. #define LTC4260_SOURCE 0x05
  23. #define LTC4260_ADIN 0x06
  24. /*
  25. * Fault register bits
  26. */
  27. #define FAULT_OV (1 << 0)
  28. #define FAULT_UV (1 << 1)
  29. #define FAULT_OC (1 << 2)
  30. #define FAULT_POWER_BAD (1 << 3)
  31. #define FAULT_FET_SHORT (1 << 5)
  32. /* Return the voltage from the given register in mV or mA */
  33. static int ltc4260_get_value(struct device *dev, u8 reg)
  34. {
  35. struct regmap *regmap = dev_get_drvdata(dev);
  36. unsigned int val;
  37. int ret;
  38. ret = regmap_read(regmap, reg, &val);
  39. if (ret < 0)
  40. return ret;
  41. switch (reg) {
  42. case LTC4260_ADIN:
  43. /* 10 mV resolution. Convert to mV. */
  44. val = val * 10;
  45. break;
  46. case LTC4260_SOURCE:
  47. /* 400 mV resolution. Convert to mV. */
  48. val = val * 400;
  49. break;
  50. case LTC4260_SENSE:
  51. /*
  52. * 300 uV resolution. Convert to current as measured with
  53. * an 1 mOhm sense resistor, in mA. If a different sense
  54. * resistor is installed, calculate the actual current by
  55. * dividing the reported current by the sense resistor value
  56. * in mOhm.
  57. */
  58. val = val * 300;
  59. break;
  60. default:
  61. return -EINVAL;
  62. }
  63. return val;
  64. }
  65. static ssize_t ltc4260_value_show(struct device *dev,
  66. struct device_attribute *da, char *buf)
  67. {
  68. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  69. int value;
  70. value = ltc4260_get_value(dev, attr->index);
  71. if (value < 0)
  72. return value;
  73. return sysfs_emit(buf, "%d\n", value);
  74. }
  75. static ssize_t ltc4260_bool_show(struct device *dev,
  76. struct device_attribute *da, char *buf)
  77. {
  78. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  79. struct regmap *regmap = dev_get_drvdata(dev);
  80. unsigned int fault;
  81. int ret;
  82. ret = regmap_read(regmap, LTC4260_FAULT, &fault);
  83. if (ret < 0)
  84. return ret;
  85. fault &= attr->index;
  86. if (fault) /* Clear reported faults in chip register */
  87. regmap_update_bits(regmap, LTC4260_FAULT, attr->index, 0);
  88. return sysfs_emit(buf, "%d\n", !!fault);
  89. }
  90. /* Voltages */
  91. static SENSOR_DEVICE_ATTR_RO(in1_input, ltc4260_value, LTC4260_SOURCE);
  92. static SENSOR_DEVICE_ATTR_RO(in2_input, ltc4260_value, LTC4260_ADIN);
  93. /*
  94. * Voltage alarms
  95. * UV/OV faults are associated with the input voltage, and the POWER BAD and
  96. * FET SHORT faults are associated with the output voltage.
  97. */
  98. static SENSOR_DEVICE_ATTR_RO(in1_min_alarm, ltc4260_bool, FAULT_UV);
  99. static SENSOR_DEVICE_ATTR_RO(in1_max_alarm, ltc4260_bool, FAULT_OV);
  100. static SENSOR_DEVICE_ATTR_RO(in2_alarm, ltc4260_bool,
  101. FAULT_POWER_BAD | FAULT_FET_SHORT);
  102. /* Current (via sense resistor) */
  103. static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc4260_value, LTC4260_SENSE);
  104. /* Overcurrent alarm */
  105. static SENSOR_DEVICE_ATTR_RO(curr1_max_alarm, ltc4260_bool, FAULT_OC);
  106. static struct attribute *ltc4260_attrs[] = {
  107. &sensor_dev_attr_in1_input.dev_attr.attr,
  108. &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
  109. &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
  110. &sensor_dev_attr_in2_input.dev_attr.attr,
  111. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  112. &sensor_dev_attr_curr1_input.dev_attr.attr,
  113. &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
  114. NULL,
  115. };
  116. ATTRIBUTE_GROUPS(ltc4260);
  117. static const struct regmap_config ltc4260_regmap_config = {
  118. .reg_bits = 8,
  119. .val_bits = 8,
  120. .max_register = LTC4260_ADIN,
  121. };
  122. static int ltc4260_probe(struct i2c_client *client)
  123. {
  124. struct device *dev = &client->dev;
  125. struct device *hwmon_dev;
  126. struct regmap *regmap;
  127. regmap = devm_regmap_init_i2c(client, &ltc4260_regmap_config);
  128. if (IS_ERR(regmap)) {
  129. dev_err(dev, "failed to allocate register map\n");
  130. return PTR_ERR(regmap);
  131. }
  132. /* Clear faults */
  133. regmap_write(regmap, LTC4260_FAULT, 0x00);
  134. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  135. regmap,
  136. ltc4260_groups);
  137. return PTR_ERR_OR_ZERO(hwmon_dev);
  138. }
  139. static const struct i2c_device_id ltc4260_id[] = {
  140. {"ltc4260", 0},
  141. { }
  142. };
  143. MODULE_DEVICE_TABLE(i2c, ltc4260_id);
  144. static struct i2c_driver ltc4260_driver = {
  145. .driver = {
  146. .name = "ltc4260",
  147. },
  148. .probe_new = ltc4260_probe,
  149. .id_table = ltc4260_id,
  150. };
  151. module_i2c_driver(ltc4260_driver);
  152. MODULE_AUTHOR("Guenter Roeck <[email protected]>");
  153. MODULE_DESCRIPTION("LTC4260 driver");
  154. MODULE_LICENSE("GPL");