ltc4261.c 7.1 KB

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