ltc4261.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. data->valid = 0;
  78. goto abort;
  79. }
  80. data->regs[i] = val;
  81. }
  82. data->last_updated = jiffies;
  83. data->valid = 1;
  84. }
  85. abort:
  86. mutex_unlock(&data->update_lock);
  87. return ret;
  88. }
  89. /* Return the voltage from the given register in mV or mA */
  90. static int ltc4261_get_value(struct ltc4261_data *data, u8 reg)
  91. {
  92. u32 val;
  93. val = (data->regs[reg] << 2) + (data->regs[reg + 1] >> 6);
  94. switch (reg) {
  95. case LTC4261_ADIN_H:
  96. case LTC4261_ADIN2_H:
  97. /* 2.5mV resolution. Convert to mV. */
  98. val = val * 25 / 10;
  99. break;
  100. case LTC4261_SENSE_H:
  101. /*
  102. * 62.5uV resolution. Convert to current as measured with
  103. * an 1 mOhm sense resistor, in mA. If a different sense
  104. * resistor is installed, calculate the actual current by
  105. * dividing the reported current by the sense resistor value
  106. * in mOhm.
  107. */
  108. val = val * 625 / 10;
  109. break;
  110. default:
  111. /* If we get here, the developer messed up */
  112. WARN_ON_ONCE(1);
  113. val = 0;
  114. break;
  115. }
  116. return val;
  117. }
  118. static ssize_t ltc4261_show_value(struct device *dev,
  119. struct device_attribute *da, char *buf)
  120. {
  121. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  122. struct ltc4261_data *data = ltc4261_update_device(dev);
  123. int value;
  124. if (IS_ERR(data))
  125. return PTR_ERR(data);
  126. value = ltc4261_get_value(data, attr->index);
  127. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  128. }
  129. static ssize_t ltc4261_show_bool(struct device *dev,
  130. struct device_attribute *da, char *buf)
  131. {
  132. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  133. struct i2c_client *client = to_i2c_client(dev);
  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(client, LTC4261_FAULT, ~fault);
  141. return snprintf(buf, PAGE_SIZE, "%d\n", fault ? 1 : 0);
  142. }
  143. /*
  144. * These macros are used below in constructing device attribute objects
  145. * for use with sysfs_create_group() to make a sysfs device file
  146. * for each register.
  147. */
  148. #define LTC4261_VALUE(name, ltc4261_cmd_idx) \
  149. static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
  150. ltc4261_show_value, NULL, ltc4261_cmd_idx)
  151. #define LTC4261_BOOL(name, mask) \
  152. static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
  153. ltc4261_show_bool, NULL, (mask))
  154. /*
  155. * Input voltages.
  156. */
  157. LTC4261_VALUE(in1_input, LTC4261_ADIN_H);
  158. LTC4261_VALUE(in2_input, LTC4261_ADIN2_H);
  159. /*
  160. * Voltage alarms. The chip has only one set of voltage alarm status bits,
  161. * triggered by input voltage alarms. In many designs, those alarms are
  162. * associated with the ADIN2 sensor, due to the proximity of the ADIN2 pin
  163. * to the OV pin. ADIN2 is, however, not available on all chip variants.
  164. * To ensure that the alarm condition is reported to the user, report it
  165. * with both voltage sensors.
  166. */
  167. LTC4261_BOOL(in1_min_alarm, FAULT_UV);
  168. LTC4261_BOOL(in1_max_alarm, FAULT_OV);
  169. LTC4261_BOOL(in2_min_alarm, FAULT_UV);
  170. LTC4261_BOOL(in2_max_alarm, FAULT_OV);
  171. /* Currents (via sense resistor) */
  172. LTC4261_VALUE(curr1_input, LTC4261_SENSE_H);
  173. /* Overcurrent alarm */
  174. LTC4261_BOOL(curr1_max_alarm, FAULT_OC);
  175. static struct attribute *ltc4261_attributes[] = {
  176. &sensor_dev_attr_in1_input.dev_attr.attr,
  177. &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
  178. &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
  179. &sensor_dev_attr_in2_input.dev_attr.attr,
  180. &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
  181. &sensor_dev_attr_in2_max_alarm.dev_attr.attr,
  182. &sensor_dev_attr_curr1_input.dev_attr.attr,
  183. &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
  184. NULL,
  185. };
  186. static const struct attribute_group ltc4261_group = {
  187. .attrs = ltc4261_attributes,
  188. };
  189. static int ltc4261_probe(struct i2c_client *client,
  190. const struct i2c_device_id *id)
  191. {
  192. struct i2c_adapter *adapter = client->adapter;
  193. struct ltc4261_data *data;
  194. int ret;
  195. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  196. return -ENODEV;
  197. if (i2c_smbus_read_byte_data(client, LTC4261_STATUS) < 0) {
  198. dev_err(&client->dev, "Failed to read status register\n");
  199. return -ENODEV;
  200. }
  201. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  202. if (!data)
  203. return -ENOMEM;
  204. i2c_set_clientdata(client, data);
  205. mutex_init(&data->update_lock);
  206. /* Clear faults */
  207. i2c_smbus_write_byte_data(client, LTC4261_FAULT, 0x00);
  208. /* Register sysfs hooks */
  209. ret = sysfs_create_group(&client->dev.kobj, &ltc4261_group);
  210. if (ret)
  211. return ret;
  212. data->hwmon_dev = hwmon_device_register(&client->dev);
  213. if (IS_ERR(data->hwmon_dev)) {
  214. ret = PTR_ERR(data->hwmon_dev);
  215. goto out_hwmon_device_register;
  216. }
  217. return 0;
  218. out_hwmon_device_register:
  219. sysfs_remove_group(&client->dev.kobj, &ltc4261_group);
  220. return ret;
  221. }
  222. static int ltc4261_remove(struct i2c_client *client)
  223. {
  224. struct ltc4261_data *data = i2c_get_clientdata(client);
  225. hwmon_device_unregister(data->hwmon_dev);
  226. sysfs_remove_group(&client->dev.kobj, &ltc4261_group);
  227. return 0;
  228. }
  229. static const struct i2c_device_id ltc4261_id[] = {
  230. {"ltc4261", 0},
  231. {}
  232. };
  233. MODULE_DEVICE_TABLE(i2c, ltc4261_id);
  234. /* This is the driver that will be inserted */
  235. static struct i2c_driver ltc4261_driver = {
  236. .driver = {
  237. .name = "ltc4261",
  238. },
  239. .probe = ltc4261_probe,
  240. .remove = ltc4261_remove,
  241. .id_table = ltc4261_id,
  242. };
  243. module_i2c_driver(ltc4261_driver);
  244. MODULE_AUTHOR("Guenter Roeck <guenter.roeck@ericsson.com>");
  245. MODULE_DESCRIPTION("LTC4261 driver");
  246. MODULE_LICENSE("GPL");