ltc4215.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. /*
  80. * The ADIN input is divided by 12.5, and has 4.82 mV
  81. * per increment, so we have the additional multiply
  82. */
  83. voltage = regval * 482 * 125 / 1000;
  84. break;
  85. default:
  86. /* If we get here, the developer messed up */
  87. WARN_ON_ONCE(1);
  88. break;
  89. }
  90. return voltage;
  91. }
  92. /* Return the current from the sense resistor in mA */
  93. static unsigned int ltc4215_get_current(struct device *dev)
  94. {
  95. struct ltc4215_data *data = ltc4215_update_device(dev);
  96. /*
  97. * The strange looking conversions that follow are fixed-point
  98. * math, since we cannot do floating point in the kernel.
  99. *
  100. * Step 1: convert sense register to microVolts
  101. * Step 2: convert voltage to milliAmperes
  102. *
  103. * If you play around with the V=IR equation, you come up with
  104. * the following: X uV / Y mOhm == Z mA
  105. *
  106. * With the resistors that are fractions of a milliOhm, we multiply
  107. * the voltage and resistance by 10, to shift the decimal point.
  108. * Now we can use the normal division operator again.
  109. */
  110. /* Calculate voltage in microVolts (151 uV per increment) */
  111. const unsigned int voltage = data->regs[LTC4215_SENSE] * 151;
  112. /* Calculate current in milliAmperes (4 milliOhm sense resistor) */
  113. const unsigned int curr = voltage / 4;
  114. return curr;
  115. }
  116. static ssize_t ltc4215_show_voltage(struct device *dev,
  117. struct device_attribute *da,
  118. char *buf)
  119. {
  120. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  121. const int voltage = ltc4215_get_voltage(dev, attr->index);
  122. return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
  123. }
  124. static ssize_t ltc4215_show_current(struct device *dev,
  125. struct device_attribute *da,
  126. char *buf)
  127. {
  128. const unsigned int curr = ltc4215_get_current(dev);
  129. return snprintf(buf, PAGE_SIZE, "%u\n", curr);
  130. }
  131. static ssize_t ltc4215_show_power(struct device *dev,
  132. struct device_attribute *da,
  133. char *buf)
  134. {
  135. const unsigned int curr = ltc4215_get_current(dev);
  136. const int output_voltage = ltc4215_get_voltage(dev, LTC4215_ADIN);
  137. /* current in mA * voltage in mV == power in uW */
  138. const unsigned int power = abs(output_voltage * curr);
  139. return snprintf(buf, PAGE_SIZE, "%u\n", power);
  140. }
  141. static ssize_t ltc4215_show_alarm(struct device *dev,
  142. struct device_attribute *da,
  143. char *buf)
  144. {
  145. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
  146. struct ltc4215_data *data = ltc4215_update_device(dev);
  147. const u8 reg = data->regs[attr->index];
  148. const u32 mask = attr->nr;
  149. return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0);
  150. }
  151. /*
  152. * These macros are used below in constructing device attribute objects
  153. * for use with sysfs_create_group() to make a sysfs device file
  154. * for each register.
  155. */
  156. #define LTC4215_VOLTAGE(name, ltc4215_cmd_idx) \
  157. static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
  158. ltc4215_show_voltage, NULL, ltc4215_cmd_idx)
  159. #define LTC4215_CURRENT(name) \
  160. static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
  161. ltc4215_show_current, NULL, 0);
  162. #define LTC4215_POWER(name) \
  163. static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
  164. ltc4215_show_power, NULL, 0);
  165. #define LTC4215_ALARM(name, mask, reg) \
  166. static SENSOR_DEVICE_ATTR_2(name, S_IRUGO, \
  167. ltc4215_show_alarm, NULL, (mask), reg)
  168. /* Construct a sensor_device_attribute structure for each register */
  169. /* Current */
  170. LTC4215_CURRENT(curr1_input);
  171. LTC4215_ALARM(curr1_max_alarm, (1 << 2), LTC4215_STATUS);
  172. /* Power (virtual) */
  173. LTC4215_POWER(power1_input);
  174. /* Input Voltage */
  175. LTC4215_VOLTAGE(in1_input, LTC4215_ADIN);
  176. LTC4215_ALARM(in1_max_alarm, (1 << 0), LTC4215_STATUS);
  177. LTC4215_ALARM(in1_min_alarm, (1 << 1), LTC4215_STATUS);
  178. /* Output Voltage */
  179. LTC4215_VOLTAGE(in2_input, LTC4215_SOURCE);
  180. LTC4215_ALARM(in2_min_alarm, (1 << 3), LTC4215_STATUS);
  181. /*
  182. * Finally, construct an array of pointers to members of the above objects,
  183. * as required for sysfs_create_group()
  184. */
  185. static struct attribute *ltc4215_attributes[] = {
  186. &sensor_dev_attr_curr1_input.dev_attr.attr,
  187. &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
  188. &sensor_dev_attr_power1_input.dev_attr.attr,
  189. &sensor_dev_attr_in1_input.dev_attr.attr,
  190. &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
  191. &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
  192. &sensor_dev_attr_in2_input.dev_attr.attr,
  193. &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
  194. NULL,
  195. };
  196. static const struct attribute_group ltc4215_group = {
  197. .attrs = ltc4215_attributes,
  198. };
  199. static int ltc4215_probe(struct i2c_client *client,
  200. const struct i2c_device_id *id)
  201. {
  202. struct i2c_adapter *adapter = client->adapter;
  203. struct ltc4215_data *data;
  204. int ret;
  205. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  206. return -ENODEV;
  207. data = kzalloc(sizeof(*data), GFP_KERNEL);
  208. if (!data) {
  209. ret = -ENOMEM;
  210. goto out_kzalloc;
  211. }
  212. i2c_set_clientdata(client, data);
  213. mutex_init(&data->update_lock);
  214. /* Initialize the LTC4215 chip */
  215. i2c_smbus_write_byte_data(client, LTC4215_FAULT, 0x00);
  216. /* Register sysfs hooks */
  217. ret = sysfs_create_group(&client->dev.kobj, &ltc4215_group);
  218. if (ret)
  219. goto out_sysfs_create_group;
  220. data->hwmon_dev = hwmon_device_register(&client->dev);
  221. if (IS_ERR(data->hwmon_dev)) {
  222. ret = PTR_ERR(data->hwmon_dev);
  223. goto out_hwmon_device_register;
  224. }
  225. return 0;
  226. out_hwmon_device_register:
  227. sysfs_remove_group(&client->dev.kobj, &ltc4215_group);
  228. out_sysfs_create_group:
  229. kfree(data);
  230. out_kzalloc:
  231. return ret;
  232. }
  233. static int ltc4215_remove(struct i2c_client *client)
  234. {
  235. struct ltc4215_data *data = i2c_get_clientdata(client);
  236. hwmon_device_unregister(data->hwmon_dev);
  237. sysfs_remove_group(&client->dev.kobj, &ltc4215_group);
  238. kfree(data);
  239. return 0;
  240. }
  241. static const struct i2c_device_id ltc4215_id[] = {
  242. { "ltc4215", 0 },
  243. { }
  244. };
  245. MODULE_DEVICE_TABLE(i2c, ltc4215_id);
  246. /* This is the driver that will be inserted */
  247. static struct i2c_driver ltc4215_driver = {
  248. .driver = {
  249. .name = "ltc4215",
  250. },
  251. .probe = ltc4215_probe,
  252. .remove = ltc4215_remove,
  253. .id_table = ltc4215_id,
  254. };
  255. module_i2c_driver(ltc4215_driver);
  256. MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
  257. MODULE_DESCRIPTION("LTC4215 driver");
  258. MODULE_LICENSE("GPL");