ltc4215.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Driver for Linear Technology LTC4215 I2C Hot Swap Controller
  3. *
  4. * Copyright (C) 2009 Ira W. Snyder <iws@ovro.caltech.edu>
  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; version 2 of the License.
  9. *
  10. * Datasheet:
  11. * http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1006,C1163,P17572,D12697
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/err.h>
  17. #include <linux/slab.h>
  18. #include <linux/i2c.h>
  19. #include <linux/hwmon.h>
  20. #include <linux/hwmon-sysfs.h>
  21. /* Here are names of the chip's registers (a.k.a. commands) */
  22. enum ltc4215_cmd {
  23. LTC4215_CONTROL = 0x00, /* rw */
  24. LTC4215_ALERT = 0x01, /* rw */
  25. LTC4215_STATUS = 0x02, /* ro */
  26. LTC4215_FAULT = 0x03, /* rw */
  27. LTC4215_SENSE = 0x04, /* rw */
  28. LTC4215_SOURCE = 0x05, /* rw */
  29. LTC4215_ADIN = 0x06, /* rw */
  30. };
  31. struct ltc4215_data {
  32. struct device *hwmon_dev;
  33. struct mutex update_lock;
  34. bool valid;
  35. unsigned long last_updated; /* in jiffies */
  36. /* Registers */
  37. u8 regs[7];
  38. };
  39. static struct ltc4215_data *ltc4215_update_device(struct device *dev)
  40. {
  41. struct i2c_client *client = to_i2c_client(dev);
  42. struct ltc4215_data *data = i2c_get_clientdata(client);
  43. s32 val;
  44. int i;
  45. mutex_lock(&data->update_lock);
  46. /* The chip's A/D updates 10 times per second */
  47. if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
  48. dev_dbg(&client->dev, "Starting ltc4215 update\n");
  49. /* Read all registers */
  50. for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
  51. val = i2c_smbus_read_byte_data(client, i);
  52. if (unlikely(val < 0))
  53. data->regs[i] = 0;
  54. else
  55. data->regs[i] = val;
  56. }
  57. data->last_updated = jiffies;
  58. data->valid = 1;
  59. }
  60. mutex_unlock(&data->update_lock);
  61. return data;
  62. }
  63. /* Return the voltage from the given register in millivolts */
  64. static int ltc4215_get_voltage(struct device *dev, u8 reg)
  65. {
  66. struct ltc4215_data *data = ltc4215_update_device(dev);
  67. const u8 regval = data->regs[reg];
  68. u32 voltage = 0;
  69. switch (reg) {
  70. case LTC4215_SENSE:
  71. /* 151 uV per increment */
  72. voltage = regval * 151 / 1000;
  73. break;
  74. case LTC4215_SOURCE:
  75. /* 60.5 mV per increment */
  76. voltage = regval * 605 / 10;
  77. break;
  78. case LTC4215_ADIN:
  79. /* The ADIN input is divided by 12.5, and has 4.82 mV
  80. * per increment, so we have the additional multiply */
  81. voltage = regval * 482 * 125 / 1000;
  82. break;
  83. default:
  84. /* If we get here, the developer messed up */
  85. WARN_ON_ONCE(1);
  86. break;
  87. }
  88. return voltage;
  89. }
  90. /* Return the current from the sense resistor in mA */
  91. static unsigned int ltc4215_get_current(struct device *dev)
  92. {
  93. struct ltc4215_data *data = ltc4215_update_device(dev);
  94. /* The strange looking conversions that follow are fixed-point
  95. * math, since we cannot do floating point in the kernel.
  96. *
  97. * Step 1: convert sense register to microVolts
  98. * Step 2: convert voltage to milliAmperes
  99. *
  100. * If you play around with the V=IR equation, you come up with
  101. * the following: X uV / Y mOhm == Z mA
  102. *
  103. * With the resistors that are fractions of a milliOhm, we multiply
  104. * the voltage and resistance by 10, to shift the decimal point.
  105. * Now we can use the normal division operator again.
  106. */
  107. /* Calculate voltage in microVolts (151 uV per increment) */
  108. const unsigned int voltage = data->regs[LTC4215_SENSE] * 151;
  109. /* Calculate current in milliAmperes (4 milliOhm sense resistor) */
  110. const unsigned int curr = voltage / 4;
  111. return curr;
  112. }
  113. static ssize_t ltc4215_show_voltage(struct device *dev,
  114. struct device_attribute *da,
  115. char *buf)
  116. {
  117. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  118. const int voltage = ltc4215_get_voltage(dev, attr->index);
  119. return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
  120. }
  121. static ssize_t ltc4215_show_current(struct device *dev,
  122. struct device_attribute *da,
  123. char *buf)
  124. {
  125. const unsigned int curr = ltc4215_get_current(dev);
  126. return snprintf(buf, PAGE_SIZE, "%u\n", curr);
  127. }
  128. static ssize_t ltc4215_show_power(struct device *dev,
  129. struct device_attribute *da,
  130. char *buf)
  131. {
  132. const unsigned int curr = ltc4215_get_current(dev);
  133. const int output_voltage = ltc4215_get_voltage(dev, LTC4215_ADIN);
  134. /* current in mA * voltage in mV == power in uW */
  135. const unsigned int power = abs(output_voltage * curr);
  136. return snprintf(buf, PAGE_SIZE, "%u\n", power);
  137. }
  138. static ssize_t ltc4215_show_alarm(struct device *dev,
  139. struct device_attribute *da,
  140. char *buf)
  141. {
  142. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
  143. struct ltc4215_data *data = ltc4215_update_device(dev);
  144. const u8 reg = data->regs[attr->index];
  145. const u32 mask = attr->nr;
  146. return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0);
  147. }
  148. /* These macros are used below in constructing device attribute objects
  149. * for use with sysfs_create_group() to make a sysfs device file
  150. * for each register.
  151. */
  152. #define LTC4215_VOLTAGE(name, ltc4215_cmd_idx) \
  153. static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
  154. ltc4215_show_voltage, NULL, ltc4215_cmd_idx)
  155. #define LTC4215_CURRENT(name) \
  156. static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
  157. ltc4215_show_current, NULL, 0);
  158. #define LTC4215_POWER(name) \
  159. static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
  160. ltc4215_show_power, NULL, 0);
  161. #define LTC4215_ALARM(name, mask, reg) \
  162. static SENSOR_DEVICE_ATTR_2(name, S_IRUGO, \
  163. ltc4215_show_alarm, NULL, (mask), reg)
  164. /* Construct a sensor_device_attribute structure for each register */
  165. /* Current */
  166. LTC4215_CURRENT(curr1_input);
  167. LTC4215_ALARM(curr1_max_alarm, (1 << 2), LTC4215_STATUS);
  168. /* Power (virtual) */
  169. LTC4215_POWER(power1_input);
  170. /* Input Voltage */
  171. LTC4215_VOLTAGE(in1_input, LTC4215_ADIN);
  172. LTC4215_ALARM(in1_max_alarm, (1 << 0), LTC4215_STATUS);
  173. LTC4215_ALARM(in1_min_alarm, (1 << 1), LTC4215_STATUS);
  174. /* Output Voltage */
  175. LTC4215_VOLTAGE(in2_input, LTC4215_SOURCE);
  176. LTC4215_ALARM(in2_min_alarm, (1 << 3), LTC4215_STATUS);
  177. /* Finally, construct an array of pointers to members of the above objects,
  178. * as required for sysfs_create_group()
  179. */
  180. static struct attribute *ltc4215_attributes[] = {
  181. &sensor_dev_attr_curr1_input.dev_attr.attr,
  182. &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
  183. &sensor_dev_attr_power1_input.dev_attr.attr,
  184. &sensor_dev_attr_in1_input.dev_attr.attr,
  185. &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
  186. &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
  187. &sensor_dev_attr_in2_input.dev_attr.attr,
  188. &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
  189. NULL,
  190. };
  191. static const struct attribute_group ltc4215_group = {
  192. .attrs = ltc4215_attributes,
  193. };
  194. static int ltc4215_probe(struct i2c_client *client,
  195. const struct i2c_device_id *id)
  196. {
  197. struct i2c_adapter *adapter = client->adapter;
  198. struct ltc4215_data *data;
  199. int ret;
  200. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  201. return -ENODEV;
  202. data = kzalloc(sizeof(*data), GFP_KERNEL);
  203. if (!data) {
  204. ret = -ENOMEM;
  205. goto out_kzalloc;
  206. }
  207. i2c_set_clientdata(client, data);
  208. mutex_init(&data->update_lock);
  209. /* Initialize the LTC4215 chip */
  210. i2c_smbus_write_byte_data(client, LTC4215_FAULT, 0x00);
  211. /* Register sysfs hooks */
  212. ret = sysfs_create_group(&client->dev.kobj, &ltc4215_group);
  213. if (ret)
  214. goto out_sysfs_create_group;
  215. data->hwmon_dev = hwmon_device_register(&client->dev);
  216. if (IS_ERR(data->hwmon_dev)) {
  217. ret = PTR_ERR(data->hwmon_dev);
  218. goto out_hwmon_device_register;
  219. }
  220. return 0;
  221. out_hwmon_device_register:
  222. sysfs_remove_group(&client->dev.kobj, &ltc4215_group);
  223. out_sysfs_create_group:
  224. kfree(data);
  225. out_kzalloc:
  226. return ret;
  227. }
  228. static int ltc4215_remove(struct i2c_client *client)
  229. {
  230. struct ltc4215_data *data = i2c_get_clientdata(client);
  231. hwmon_device_unregister(data->hwmon_dev);
  232. sysfs_remove_group(&client->dev.kobj, &ltc4215_group);
  233. kfree(data);
  234. return 0;
  235. }
  236. static const struct i2c_device_id ltc4215_id[] = {
  237. { "ltc4215", 0 },
  238. { }
  239. };
  240. MODULE_DEVICE_TABLE(i2c, ltc4215_id);
  241. /* This is the driver that will be inserted */
  242. static struct i2c_driver ltc4215_driver = {
  243. .driver = {
  244. .name = "ltc4215",
  245. },
  246. .probe = ltc4215_probe,
  247. .remove = ltc4215_remove,
  248. .id_table = ltc4215_id,
  249. };
  250. static int __init ltc4215_init(void)
  251. {
  252. return i2c_add_driver(&ltc4215_driver);
  253. }
  254. static void __exit ltc4215_exit(void)
  255. {
  256. i2c_del_driver(&ltc4215_driver);
  257. }
  258. MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
  259. MODULE_DESCRIPTION("LTC4215 driver");
  260. MODULE_LICENSE("GPL");
  261. module_init(ltc4215_init);
  262. module_exit(ltc4215_exit);