ltc4215.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #include <linux/jiffies.h>
  22. /* Here are names of the chip's registers (a.k.a. commands) */
  23. enum ltc4215_cmd {
  24. LTC4215_CONTROL = 0x00, /* rw */
  25. LTC4215_ALERT = 0x01, /* rw */
  26. LTC4215_STATUS = 0x02, /* ro */
  27. LTC4215_FAULT = 0x03, /* rw */
  28. LTC4215_SENSE = 0x04, /* rw */
  29. LTC4215_SOURCE = 0x05, /* rw */
  30. LTC4215_ADIN = 0x06, /* rw */
  31. };
  32. struct ltc4215_data {
  33. struct i2c_client *client;
  34. struct mutex update_lock;
  35. bool valid;
  36. unsigned long last_updated; /* in jiffies */
  37. /* Registers */
  38. u8 regs[7];
  39. };
  40. static struct ltc4215_data *ltc4215_update_device(struct device *dev)
  41. {
  42. struct ltc4215_data *data = dev_get_drvdata(dev);
  43. struct i2c_client *client = data->client;
  44. s32 val;
  45. int i;
  46. mutex_lock(&data->update_lock);
  47. /* The chip's A/D updates 10 times per second */
  48. if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
  49. dev_dbg(&client->dev, "Starting ltc4215 update\n");
  50. /* Read all registers */
  51. for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
  52. val = i2c_smbus_read_byte_data(client, i);
  53. if (unlikely(val < 0))
  54. data->regs[i] = 0;
  55. else
  56. data->regs[i] = val;
  57. }
  58. data->last_updated = jiffies;
  59. data->valid = 1;
  60. }
  61. mutex_unlock(&data->update_lock);
  62. return data;
  63. }
  64. /* Return the voltage from the given register in millivolts */
  65. static int ltc4215_get_voltage(struct device *dev, u8 reg)
  66. {
  67. struct ltc4215_data *data = ltc4215_update_device(dev);
  68. const u8 regval = data->regs[reg];
  69. u32 voltage = 0;
  70. switch (reg) {
  71. case LTC4215_SENSE:
  72. /* 151 uV per increment */
  73. voltage = regval * 151 / 1000;
  74. break;
  75. case LTC4215_SOURCE:
  76. /* 60.5 mV per increment */
  77. voltage = regval * 605 / 10;
  78. break;
  79. case LTC4215_ADIN:
  80. /*
  81. * The ADIN input is divided by 12.5, and has 4.82 mV
  82. * per increment, so we have the additional multiply
  83. */
  84. voltage = regval * 482 * 125 / 1000;
  85. break;
  86. default:
  87. /* If we get here, the developer messed up */
  88. WARN_ON_ONCE(1);
  89. break;
  90. }
  91. return voltage;
  92. }
  93. /* Return the current from the sense resistor in mA */
  94. static unsigned int ltc4215_get_current(struct device *dev)
  95. {
  96. struct ltc4215_data *data = ltc4215_update_device(dev);
  97. /*
  98. * The strange looking conversions that follow are fixed-point
  99. * math, since we cannot do floating point in the kernel.
  100. *
  101. * Step 1: convert sense register to microVolts
  102. * Step 2: convert voltage to milliAmperes
  103. *
  104. * If you play around with the V=IR equation, you come up with
  105. * the following: X uV / Y mOhm == Z mA
  106. *
  107. * With the resistors that are fractions of a milliOhm, we multiply
  108. * the voltage and resistance by 10, to shift the decimal point.
  109. * Now we can use the normal division operator again.
  110. */
  111. /* Calculate voltage in microVolts (151 uV per increment) */
  112. const unsigned int voltage = data->regs[LTC4215_SENSE] * 151;
  113. /* Calculate current in milliAmperes (4 milliOhm sense resistor) */
  114. const unsigned int curr = voltage / 4;
  115. return curr;
  116. }
  117. static ssize_t ltc4215_show_voltage(struct device *dev,
  118. struct device_attribute *da,
  119. char *buf)
  120. {
  121. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  122. const int voltage = ltc4215_get_voltage(dev, attr->index);
  123. return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
  124. }
  125. static ssize_t ltc4215_show_current(struct device *dev,
  126. struct device_attribute *da,
  127. char *buf)
  128. {
  129. const unsigned int curr = ltc4215_get_current(dev);
  130. return snprintf(buf, PAGE_SIZE, "%u\n", curr);
  131. }
  132. static ssize_t ltc4215_show_power(struct device *dev,
  133. struct device_attribute *da,
  134. char *buf)
  135. {
  136. const unsigned int curr = ltc4215_get_current(dev);
  137. const int output_voltage = ltc4215_get_voltage(dev, LTC4215_ADIN);
  138. /* current in mA * voltage in mV == power in uW */
  139. const unsigned int power = abs(output_voltage * curr);
  140. return snprintf(buf, PAGE_SIZE, "%u\n", power);
  141. }
  142. static ssize_t ltc4215_show_alarm(struct device *dev,
  143. struct device_attribute *da,
  144. char *buf)
  145. {
  146. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  147. struct ltc4215_data *data = ltc4215_update_device(dev);
  148. const u8 reg = data->regs[LTC4215_STATUS];
  149. const u32 mask = attr->index;
  150. return snprintf(buf, PAGE_SIZE, "%u\n", !!(reg & mask));
  151. }
  152. /*
  153. * These macros are used below in constructing device attribute objects
  154. * for use with sysfs_create_group() to make a sysfs device file
  155. * for each register.
  156. */
  157. /* Construct a sensor_device_attribute structure for each register */
  158. /* Current */
  159. static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc4215_show_current, NULL, 0);
  160. static SENSOR_DEVICE_ATTR(curr1_max_alarm, S_IRUGO, ltc4215_show_alarm, NULL,
  161. 1 << 2);
  162. /* Power (virtual) */
  163. static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ltc4215_show_power, NULL, 0);
  164. /* Input Voltage */
  165. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc4215_show_voltage, NULL,
  166. LTC4215_ADIN);
  167. static SENSOR_DEVICE_ATTR(in1_max_alarm, S_IRUGO, ltc4215_show_alarm, NULL,
  168. 1 << 0);
  169. static SENSOR_DEVICE_ATTR(in1_min_alarm, S_IRUGO, ltc4215_show_alarm, NULL,
  170. 1 << 1);
  171. /* Output Voltage */
  172. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc4215_show_voltage, NULL,
  173. LTC4215_SOURCE);
  174. static SENSOR_DEVICE_ATTR(in2_min_alarm, S_IRUGO, ltc4215_show_alarm, NULL,
  175. 1 << 3);
  176. /*
  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_attrs[] = {
  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. ATTRIBUTE_GROUPS(ltc4215);
  192. static int ltc4215_probe(struct i2c_client *client,
  193. const struct i2c_device_id *id)
  194. {
  195. struct i2c_adapter *adapter = client->adapter;
  196. struct device *dev = &client->dev;
  197. struct ltc4215_data *data;
  198. struct device *hwmon_dev;
  199. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  200. return -ENODEV;
  201. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  202. if (!data)
  203. return -ENOMEM;
  204. data->client = client;
  205. mutex_init(&data->update_lock);
  206. /* Initialize the LTC4215 chip */
  207. i2c_smbus_write_byte_data(client, LTC4215_FAULT, 0x00);
  208. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  209. data,
  210. ltc4215_groups);
  211. return PTR_ERR_OR_ZERO(hwmon_dev);
  212. }
  213. static const struct i2c_device_id ltc4215_id[] = {
  214. { "ltc4215", 0 },
  215. { }
  216. };
  217. MODULE_DEVICE_TABLE(i2c, ltc4215_id);
  218. /* This is the driver that will be inserted */
  219. static struct i2c_driver ltc4215_driver = {
  220. .driver = {
  221. .name = "ltc4215",
  222. },
  223. .probe = ltc4215_probe,
  224. .id_table = ltc4215_id,
  225. };
  226. module_i2c_driver(ltc4215_driver);
  227. MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
  228. MODULE_DESCRIPTION("LTC4215 driver");
  229. MODULE_LICENSE("GPL");