ltc4261.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. /* chip registers */
  36. #define LTC4261_STATUS 0x00 /* readonly */
  37. #define LTC4261_FAULT 0x01
  38. #define LTC4261_ALERT 0x02
  39. #define LTC4261_CONTROL 0x03
  40. #define LTC4261_SENSE_H 0x04
  41. #define LTC4261_SENSE_L 0x05
  42. #define LTC4261_ADIN2_H 0x06
  43. #define LTC4261_ADIN2_L 0x07
  44. #define LTC4261_ADIN_H 0x08
  45. #define LTC4261_ADIN_L 0x09
  46. /*
  47. * Fault register bits
  48. */
  49. #define FAULT_OV (1<<0)
  50. #define FAULT_UV (1<<1)
  51. #define FAULT_OC (1<<2)
  52. struct ltc4261_data {
  53. struct device *hwmon_dev;
  54. struct mutex update_lock;
  55. bool valid;
  56. unsigned long last_updated; /* in jiffies */
  57. /* Registers */
  58. u8 regs[10];
  59. };
  60. static struct ltc4261_data *ltc4261_update_device(struct device *dev)
  61. {
  62. struct i2c_client *client = to_i2c_client(dev);
  63. struct ltc4261_data *data = i2c_get_clientdata(client);
  64. struct ltc4261_data *ret = data;
  65. mutex_lock(&data->update_lock);
  66. if (time_after(jiffies, data->last_updated + HZ / 4) || !data->valid) {
  67. int i;
  68. /* Read registers -- 0x00 to 0x09 */
  69. for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
  70. int val;
  71. val = i2c_smbus_read_byte_data(client, i);
  72. if (unlikely(val < 0)) {
  73. dev_dbg(dev,
  74. "Failed to read ADC value: error %d\n",
  75. val);
  76. ret = ERR_PTR(val);
  77. goto abort;
  78. }
  79. data->regs[i] = val;
  80. }
  81. data->last_updated = jiffies;
  82. data->valid = 1;
  83. }
  84. abort:
  85. mutex_unlock(&data->update_lock);
  86. return ret;
  87. }
  88. /* Return the voltage from the given register in mV or mA */
  89. static int ltc4261_get_value(struct ltc4261_data *data, u8 reg)
  90. {
  91. u32 val;
  92. val = (data->regs[reg] << 2) + (data->regs[reg + 1] >> 6);
  93. switch (reg) {
  94. case LTC4261_ADIN_H:
  95. case LTC4261_ADIN2_H:
  96. /* 2.5mV resolution. Convert to mV. */
  97. val = val * 25 / 10;
  98. break;
  99. case LTC4261_SENSE_H:
  100. /*
  101. * 62.5uV resolution. Convert to current as measured with
  102. * an 1 mOhm sense resistor, in mA. If a different sense
  103. * resistor is installed, calculate the actual current by
  104. * dividing the reported current by the sense resistor value
  105. * in mOhm.
  106. */
  107. val = val * 625 / 10;
  108. break;
  109. default:
  110. /* If we get here, the developer messed up */
  111. WARN_ON_ONCE(1);
  112. val = 0;
  113. break;
  114. }
  115. return val;
  116. }
  117. static ssize_t ltc4261_show_value(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. int value;
  123. if (IS_ERR(data))
  124. return PTR_ERR(data);
  125. value = ltc4261_get_value(data, attr->index);
  126. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  127. }
  128. static ssize_t ltc4261_show_bool(struct device *dev,
  129. struct device_attribute *da, char *buf)
  130. {
  131. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  132. struct i2c_client *client = to_i2c_client(dev);
  133. struct ltc4261_data *data = ltc4261_update_device(dev);
  134. u8 fault;
  135. if (IS_ERR(data))
  136. return PTR_ERR(data);
  137. fault = data->regs[LTC4261_FAULT] & attr->index;
  138. if (fault) /* Clear reported faults in chip register */
  139. i2c_smbus_write_byte_data(client, LTC4261_FAULT, ~fault);
  140. return snprintf(buf, PAGE_SIZE, "%d\n", fault ? 1 : 0);
  141. }
  142. /*
  143. * These macros are used below in constructing device attribute objects
  144. * for use with sysfs_create_group() to make a sysfs device file
  145. * for each register.
  146. */
  147. #define LTC4261_VALUE(name, ltc4261_cmd_idx) \
  148. static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
  149. ltc4261_show_value, NULL, ltc4261_cmd_idx)
  150. #define LTC4261_BOOL(name, mask) \
  151. static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
  152. ltc4261_show_bool, NULL, (mask))
  153. /*
  154. * Input voltages.
  155. */
  156. LTC4261_VALUE(in1_input, LTC4261_ADIN_H);
  157. LTC4261_VALUE(in2_input, LTC4261_ADIN2_H);
  158. /*
  159. * Voltage alarms. The chip has only one set of voltage alarm status bits,
  160. * triggered by input voltage alarms. In many designs, those alarms are
  161. * associated with the ADIN2 sensor, due to the proximity of the ADIN2 pin
  162. * to the OV pin. ADIN2 is, however, not available on all chip variants.
  163. * To ensure that the alarm condition is reported to the user, report it
  164. * with both voltage sensors.
  165. */
  166. LTC4261_BOOL(in1_min_alarm, FAULT_UV);
  167. LTC4261_BOOL(in1_max_alarm, FAULT_OV);
  168. LTC4261_BOOL(in2_min_alarm, FAULT_UV);
  169. LTC4261_BOOL(in2_max_alarm, FAULT_OV);
  170. /* Currents (via sense resistor) */
  171. LTC4261_VALUE(curr1_input, LTC4261_SENSE_H);
  172. /* Overcurrent alarm */
  173. LTC4261_BOOL(curr1_max_alarm, FAULT_OC);
  174. static struct attribute *ltc4261_attributes[] = {
  175. &sensor_dev_attr_in1_input.dev_attr.attr,
  176. &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
  177. &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
  178. &sensor_dev_attr_in2_input.dev_attr.attr,
  179. &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
  180. &sensor_dev_attr_in2_max_alarm.dev_attr.attr,
  181. &sensor_dev_attr_curr1_input.dev_attr.attr,
  182. &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
  183. NULL,
  184. };
  185. static const struct attribute_group ltc4261_group = {
  186. .attrs = ltc4261_attributes,
  187. };
  188. static int ltc4261_probe(struct i2c_client *client,
  189. const struct i2c_device_id *id)
  190. {
  191. struct i2c_adapter *adapter = client->adapter;
  192. struct ltc4261_data *data;
  193. int ret;
  194. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  195. return -ENODEV;
  196. if (i2c_smbus_read_byte_data(client, LTC4261_STATUS) < 0) {
  197. dev_err(&client->dev, "Failed to read status register\n");
  198. return -ENODEV;
  199. }
  200. data = kzalloc(sizeof(*data), GFP_KERNEL);
  201. if (!data) {
  202. ret = -ENOMEM;
  203. goto out_kzalloc;
  204. }
  205. i2c_set_clientdata(client, data);
  206. mutex_init(&data->update_lock);
  207. /* Clear faults */
  208. i2c_smbus_write_byte_data(client, LTC4261_FAULT, 0x00);
  209. /* Register sysfs hooks */
  210. ret = sysfs_create_group(&client->dev.kobj, &ltc4261_group);
  211. if (ret)
  212. goto out_sysfs_create_group;
  213. data->hwmon_dev = hwmon_device_register(&client->dev);
  214. if (IS_ERR(data->hwmon_dev)) {
  215. ret = PTR_ERR(data->hwmon_dev);
  216. goto out_hwmon_device_register;
  217. }
  218. return 0;
  219. out_hwmon_device_register:
  220. sysfs_remove_group(&client->dev.kobj, &ltc4261_group);
  221. out_sysfs_create_group:
  222. kfree(data);
  223. out_kzalloc:
  224. return ret;
  225. }
  226. static int ltc4261_remove(struct i2c_client *client)
  227. {
  228. struct ltc4261_data *data = i2c_get_clientdata(client);
  229. hwmon_device_unregister(data->hwmon_dev);
  230. sysfs_remove_group(&client->dev.kobj, &ltc4261_group);
  231. kfree(data);
  232. return 0;
  233. }
  234. static const struct i2c_device_id ltc4261_id[] = {
  235. {"ltc4261", 0},
  236. {}
  237. };
  238. MODULE_DEVICE_TABLE(i2c, ltc4261_id);
  239. /* This is the driver that will be inserted */
  240. static struct i2c_driver ltc4261_driver = {
  241. .driver = {
  242. .name = "ltc4261",
  243. },
  244. .probe = ltc4261_probe,
  245. .remove = ltc4261_remove,
  246. .id_table = ltc4261_id,
  247. };
  248. static int __init ltc4261_init(void)
  249. {
  250. return i2c_add_driver(&ltc4261_driver);
  251. }
  252. static void __exit ltc4261_exit(void)
  253. {
  254. i2c_del_driver(&ltc4261_driver);
  255. }
  256. MODULE_AUTHOR("Guenter Roeck <guenter.roeck@ericsson.com>");
  257. MODULE_DESCRIPTION("LTC4261 driver");
  258. MODULE_LICENSE("GPL");
  259. module_init(ltc4261_init);
  260. module_exit(ltc4261_exit);