ltc4261.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for Linear Technology LTC4261 I2C Negative Voltage Hot Swap Controller
  4. *
  5. * Copyright (C) 2010 Ericsson AB.
  6. *
  7. * Derived from:
  8. *
  9. * Driver for Linear Technology LTC4245 I2C Multiple Supply Hot Swap Controller
  10. * Copyright (C) 2008 Ira W. Snyder <[email protected]>
  11. *
  12. * Datasheet: http://cds.linear.com/docs/Datasheet/42612fb.pdf
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/err.h>
  18. #include <linux/slab.h>
  19. #include <linux/i2c.h>
  20. #include <linux/hwmon.h>
  21. #include <linux/hwmon-sysfs.h>
  22. #include <linux/jiffies.h>
  23. /* chip registers */
  24. #define LTC4261_STATUS 0x00 /* readonly */
  25. #define LTC4261_FAULT 0x01
  26. #define LTC4261_ALERT 0x02
  27. #define LTC4261_CONTROL 0x03
  28. #define LTC4261_SENSE_H 0x04
  29. #define LTC4261_SENSE_L 0x05
  30. #define LTC4261_ADIN2_H 0x06
  31. #define LTC4261_ADIN2_L 0x07
  32. #define LTC4261_ADIN_H 0x08
  33. #define LTC4261_ADIN_L 0x09
  34. /*
  35. * Fault register bits
  36. */
  37. #define FAULT_OV (1<<0)
  38. #define FAULT_UV (1<<1)
  39. #define FAULT_OC (1<<2)
  40. struct ltc4261_data {
  41. struct i2c_client *client;
  42. struct mutex update_lock;
  43. bool valid;
  44. unsigned long last_updated; /* in jiffies */
  45. /* Registers */
  46. u8 regs[10];
  47. };
  48. static struct ltc4261_data *ltc4261_update_device(struct device *dev)
  49. {
  50. struct ltc4261_data *data = dev_get_drvdata(dev);
  51. struct i2c_client *client = data->client;
  52. struct ltc4261_data *ret = data;
  53. mutex_lock(&data->update_lock);
  54. if (time_after(jiffies, data->last_updated + HZ / 4) || !data->valid) {
  55. int i;
  56. /* Read registers -- 0x00 to 0x09 */
  57. for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
  58. int val;
  59. val = i2c_smbus_read_byte_data(client, i);
  60. if (unlikely(val < 0)) {
  61. dev_dbg(dev,
  62. "Failed to read ADC value: error %d\n",
  63. val);
  64. ret = ERR_PTR(val);
  65. data->valid = false;
  66. goto abort;
  67. }
  68. data->regs[i] = val;
  69. }
  70. data->last_updated = jiffies;
  71. data->valid = true;
  72. }
  73. abort:
  74. mutex_unlock(&data->update_lock);
  75. return ret;
  76. }
  77. /* Return the voltage from the given register in mV or mA */
  78. static int ltc4261_get_value(struct ltc4261_data *data, u8 reg)
  79. {
  80. u32 val;
  81. val = (data->regs[reg] << 2) + (data->regs[reg + 1] >> 6);
  82. switch (reg) {
  83. case LTC4261_ADIN_H:
  84. case LTC4261_ADIN2_H:
  85. /* 2.5mV resolution. Convert to mV. */
  86. val = val * 25 / 10;
  87. break;
  88. case LTC4261_SENSE_H:
  89. /*
  90. * 62.5uV resolution. Convert to current as measured with
  91. * an 1 mOhm sense resistor, in mA. If a different sense
  92. * resistor is installed, calculate the actual current by
  93. * dividing the reported current by the sense resistor value
  94. * in mOhm.
  95. */
  96. val = val * 625 / 10;
  97. break;
  98. default:
  99. /* If we get here, the developer messed up */
  100. WARN_ON_ONCE(1);
  101. val = 0;
  102. break;
  103. }
  104. return val;
  105. }
  106. static ssize_t ltc4261_value_show(struct device *dev,
  107. struct device_attribute *da, char *buf)
  108. {
  109. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  110. struct ltc4261_data *data = ltc4261_update_device(dev);
  111. int value;
  112. if (IS_ERR(data))
  113. return PTR_ERR(data);
  114. value = ltc4261_get_value(data, attr->index);
  115. return sysfs_emit(buf, "%d\n", value);
  116. }
  117. static ssize_t ltc4261_bool_show(struct device *dev,
  118. struct device_attribute *da, char *buf)
  119. {
  120. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  121. struct ltc4261_data *data = ltc4261_update_device(dev);
  122. u8 fault;
  123. if (IS_ERR(data))
  124. return PTR_ERR(data);
  125. fault = data->regs[LTC4261_FAULT] & attr->index;
  126. if (fault) /* Clear reported faults in chip register */
  127. i2c_smbus_write_byte_data(data->client, LTC4261_FAULT, ~fault);
  128. return sysfs_emit(buf, "%d\n", fault ? 1 : 0);
  129. }
  130. /*
  131. * Input voltages.
  132. */
  133. static SENSOR_DEVICE_ATTR_RO(in1_input, ltc4261_value, LTC4261_ADIN_H);
  134. static SENSOR_DEVICE_ATTR_RO(in2_input, ltc4261_value, LTC4261_ADIN2_H);
  135. /*
  136. * Voltage alarms. The chip has only one set of voltage alarm status bits,
  137. * triggered by input voltage alarms. In many designs, those alarms are
  138. * associated with the ADIN2 sensor, due to the proximity of the ADIN2 pin
  139. * to the OV pin. ADIN2 is, however, not available on all chip variants.
  140. * To ensure that the alarm condition is reported to the user, report it
  141. * with both voltage sensors.
  142. */
  143. static SENSOR_DEVICE_ATTR_RO(in1_min_alarm, ltc4261_bool, FAULT_UV);
  144. static SENSOR_DEVICE_ATTR_RO(in1_max_alarm, ltc4261_bool, FAULT_OV);
  145. static SENSOR_DEVICE_ATTR_RO(in2_min_alarm, ltc4261_bool, FAULT_UV);
  146. static SENSOR_DEVICE_ATTR_RO(in2_max_alarm, ltc4261_bool, FAULT_OV);
  147. /* Currents (via sense resistor) */
  148. static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc4261_value, LTC4261_SENSE_H);
  149. /* Overcurrent alarm */
  150. static SENSOR_DEVICE_ATTR_RO(curr1_max_alarm, ltc4261_bool, FAULT_OC);
  151. static struct attribute *ltc4261_attrs[] = {
  152. &sensor_dev_attr_in1_input.dev_attr.attr,
  153. &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
  154. &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
  155. &sensor_dev_attr_in2_input.dev_attr.attr,
  156. &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
  157. &sensor_dev_attr_in2_max_alarm.dev_attr.attr,
  158. &sensor_dev_attr_curr1_input.dev_attr.attr,
  159. &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
  160. NULL,
  161. };
  162. ATTRIBUTE_GROUPS(ltc4261);
  163. static int ltc4261_probe(struct i2c_client *client)
  164. {
  165. struct i2c_adapter *adapter = client->adapter;
  166. struct device *dev = &client->dev;
  167. struct ltc4261_data *data;
  168. struct device *hwmon_dev;
  169. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  170. return -ENODEV;
  171. if (i2c_smbus_read_byte_data(client, LTC4261_STATUS) < 0) {
  172. dev_err(dev, "Failed to read status register\n");
  173. return -ENODEV;
  174. }
  175. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  176. if (!data)
  177. return -ENOMEM;
  178. data->client = client;
  179. mutex_init(&data->update_lock);
  180. /* Clear faults */
  181. i2c_smbus_write_byte_data(client, LTC4261_FAULT, 0x00);
  182. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  183. data,
  184. ltc4261_groups);
  185. return PTR_ERR_OR_ZERO(hwmon_dev);
  186. }
  187. static const struct i2c_device_id ltc4261_id[] = {
  188. {"ltc4261", 0},
  189. {}
  190. };
  191. MODULE_DEVICE_TABLE(i2c, ltc4261_id);
  192. /* This is the driver that will be inserted */
  193. static struct i2c_driver ltc4261_driver = {
  194. .driver = {
  195. .name = "ltc4261",
  196. },
  197. .probe_new = ltc4261_probe,
  198. .id_table = ltc4261_id,
  199. };
  200. module_i2c_driver(ltc4261_driver);
  201. MODULE_AUTHOR("Guenter Roeck <[email protected]>");
  202. MODULE_DESCRIPTION("LTC4261 driver");
  203. MODULE_LICENSE("GPL");