ltc4222.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Driver for Linear Technology LTC4222 Dual Hot Swap controller
  3. *
  4. * Copyright (c) 2014 Guenter Roeck
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/err.h>
  19. #include <linux/slab.h>
  20. #include <linux/bitops.h>
  21. #include <linux/i2c.h>
  22. #include <linux/hwmon.h>
  23. #include <linux/hwmon-sysfs.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/regmap.h>
  26. /* chip registers */
  27. #define LTC4222_CONTROL1 0xd0
  28. #define LTC4222_ALERT1 0xd1
  29. #define LTC4222_STATUS1 0xd2
  30. #define LTC4222_FAULT1 0xd3
  31. #define LTC4222_CONTROL2 0xd4
  32. #define LTC4222_ALERT2 0xd5
  33. #define LTC4222_STATUS2 0xd6
  34. #define LTC4222_FAULT2 0xd7
  35. #define LTC4222_SOURCE1 0xd8
  36. #define LTC4222_SOURCE2 0xda
  37. #define LTC4222_ADIN1 0xdc
  38. #define LTC4222_ADIN2 0xde
  39. #define LTC4222_SENSE1 0xe0
  40. #define LTC4222_SENSE2 0xe2
  41. #define LTC4222_ADC_CONTROL 0xe4
  42. /*
  43. * Fault register bits
  44. */
  45. #define FAULT_OV BIT(0)
  46. #define FAULT_UV BIT(1)
  47. #define FAULT_OC BIT(2)
  48. #define FAULT_POWER_BAD BIT(3)
  49. #define FAULT_FET_BAD BIT(5)
  50. /* Return the voltage from the given register in mV or mA */
  51. static int ltc4222_get_value(struct device *dev, u8 reg)
  52. {
  53. struct regmap *regmap = dev_get_drvdata(dev);
  54. unsigned int val;
  55. u8 buf[2];
  56. int ret;
  57. ret = regmap_bulk_read(regmap, reg, buf, 2);
  58. if (ret < 0)
  59. return ret;
  60. val = ((buf[0] << 8) + buf[1]) >> 6;
  61. switch (reg) {
  62. case LTC4222_ADIN1:
  63. case LTC4222_ADIN2:
  64. /* 1.25 mV resolution. Convert to mV. */
  65. val = DIV_ROUND_CLOSEST(val * 5, 4);
  66. break;
  67. case LTC4222_SOURCE1:
  68. case LTC4222_SOURCE2:
  69. /* 31.25 mV resolution. Convert to mV. */
  70. val = DIV_ROUND_CLOSEST(val * 125, 4);
  71. break;
  72. case LTC4222_SENSE1:
  73. case LTC4222_SENSE2:
  74. /*
  75. * 62.5 uV resolution. Convert to current as measured with
  76. * an 1 mOhm sense resistor, in mA. If a different sense
  77. * resistor is installed, calculate the actual current by
  78. * dividing the reported current by the sense resistor value
  79. * in mOhm.
  80. */
  81. val = DIV_ROUND_CLOSEST(val * 125, 2);
  82. break;
  83. default:
  84. return -EINVAL;
  85. }
  86. return val;
  87. }
  88. static ssize_t ltc4222_show_value(struct device *dev,
  89. struct device_attribute *da, char *buf)
  90. {
  91. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  92. int value;
  93. value = ltc4222_get_value(dev, attr->index);
  94. if (value < 0)
  95. return value;
  96. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  97. }
  98. static ssize_t ltc4222_show_bool(struct device *dev,
  99. struct device_attribute *da, char *buf)
  100. {
  101. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
  102. struct regmap *regmap = dev_get_drvdata(dev);
  103. unsigned int fault;
  104. int ret;
  105. ret = regmap_read(regmap, attr->nr, &fault);
  106. if (ret < 0)
  107. return ret;
  108. fault &= attr->index;
  109. if (fault) /* Clear reported faults in chip register */
  110. regmap_update_bits(regmap, attr->nr, attr->index, 0);
  111. return snprintf(buf, PAGE_SIZE, "%d\n", !!fault);
  112. }
  113. /* Voltages */
  114. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc4222_show_value, NULL,
  115. LTC4222_SOURCE1);
  116. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc4222_show_value, NULL,
  117. LTC4222_ADIN1);
  118. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, ltc4222_show_value, NULL,
  119. LTC4222_SOURCE2);
  120. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, ltc4222_show_value, NULL,
  121. LTC4222_ADIN2);
  122. /*
  123. * Voltage alarms
  124. * UV/OV faults are associated with the input voltage, and power bad and fet
  125. * faults are associated with the output voltage.
  126. */
  127. static SENSOR_DEVICE_ATTR_2(in1_min_alarm, S_IRUGO, ltc4222_show_bool, NULL,
  128. LTC4222_FAULT1, FAULT_UV);
  129. static SENSOR_DEVICE_ATTR_2(in1_max_alarm, S_IRUGO, ltc4222_show_bool, NULL,
  130. LTC4222_FAULT1, FAULT_OV);
  131. static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, ltc4222_show_bool, NULL,
  132. LTC4222_FAULT1, FAULT_POWER_BAD | FAULT_FET_BAD);
  133. static SENSOR_DEVICE_ATTR_2(in3_min_alarm, S_IRUGO, ltc4222_show_bool, NULL,
  134. LTC4222_FAULT2, FAULT_UV);
  135. static SENSOR_DEVICE_ATTR_2(in3_max_alarm, S_IRUGO, ltc4222_show_bool, NULL,
  136. LTC4222_FAULT2, FAULT_OV);
  137. static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, ltc4222_show_bool, NULL,
  138. LTC4222_FAULT2, FAULT_POWER_BAD | FAULT_FET_BAD);
  139. /* Current (via sense resistor) */
  140. static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc4222_show_value, NULL,
  141. LTC4222_SENSE1);
  142. static SENSOR_DEVICE_ATTR(curr2_input, S_IRUGO, ltc4222_show_value, NULL,
  143. LTC4222_SENSE2);
  144. /* Overcurrent alarm */
  145. static SENSOR_DEVICE_ATTR_2(curr1_max_alarm, S_IRUGO, ltc4222_show_bool, NULL,
  146. LTC4222_FAULT1, FAULT_OC);
  147. static SENSOR_DEVICE_ATTR_2(curr2_max_alarm, S_IRUGO, ltc4222_show_bool, NULL,
  148. LTC4222_FAULT2, FAULT_OC);
  149. static struct attribute *ltc4222_attrs[] = {
  150. &sensor_dev_attr_in1_input.dev_attr.attr,
  151. &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
  152. &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
  153. &sensor_dev_attr_in2_input.dev_attr.attr,
  154. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  155. &sensor_dev_attr_in3_input.dev_attr.attr,
  156. &sensor_dev_attr_in3_min_alarm.dev_attr.attr,
  157. &sensor_dev_attr_in3_max_alarm.dev_attr.attr,
  158. &sensor_dev_attr_in4_input.dev_attr.attr,
  159. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  160. &sensor_dev_attr_curr1_input.dev_attr.attr,
  161. &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
  162. &sensor_dev_attr_curr2_input.dev_attr.attr,
  163. &sensor_dev_attr_curr2_max_alarm.dev_attr.attr,
  164. NULL,
  165. };
  166. ATTRIBUTE_GROUPS(ltc4222);
  167. static const struct regmap_config ltc4222_regmap_config = {
  168. .reg_bits = 8,
  169. .val_bits = 8,
  170. .max_register = LTC4222_ADC_CONTROL,
  171. };
  172. static int ltc4222_probe(struct i2c_client *client,
  173. const struct i2c_device_id *id)
  174. {
  175. struct device *dev = &client->dev;
  176. struct device *hwmon_dev;
  177. struct regmap *regmap;
  178. regmap = devm_regmap_init_i2c(client, &ltc4222_regmap_config);
  179. if (IS_ERR(regmap)) {
  180. dev_err(dev, "failed to allocate register map\n");
  181. return PTR_ERR(regmap);
  182. }
  183. /* Clear faults */
  184. regmap_write(regmap, LTC4222_FAULT1, 0x00);
  185. regmap_write(regmap, LTC4222_FAULT2, 0x00);
  186. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  187. regmap,
  188. ltc4222_groups);
  189. return PTR_ERR_OR_ZERO(hwmon_dev);
  190. }
  191. static const struct i2c_device_id ltc4222_id[] = {
  192. {"ltc4222", 0},
  193. { }
  194. };
  195. MODULE_DEVICE_TABLE(i2c, ltc4222_id);
  196. static struct i2c_driver ltc4222_driver = {
  197. .driver = {
  198. .name = "ltc4222",
  199. },
  200. .probe = ltc4222_probe,
  201. .id_table = ltc4222_id,
  202. };
  203. module_i2c_driver(ltc4222_driver);
  204. MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
  205. MODULE_DESCRIPTION("LTC4222 driver");
  206. MODULE_LICENSE("GPL");