ltc4215.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for Linear Technology LTC4215 I2C Hot Swap Controller
  4. *
  5. * Copyright (C) 2009 Ira W. Snyder <iws@ovro.caltech.edu>
  6. *
  7. * Datasheet:
  8. * http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1006,C1163,P17572,D12697
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/err.h>
  14. #include <linux/slab.h>
  15. #include <linux/i2c.h>
  16. #include <linux/hwmon.h>
  17. #include <linux/hwmon-sysfs.h>
  18. #include <linux/jiffies.h>
  19. /* Here are names of the chip's registers (a.k.a. commands) */
  20. enum ltc4215_cmd {
  21. LTC4215_CONTROL = 0x00, /* rw */
  22. LTC4215_ALERT = 0x01, /* rw */
  23. LTC4215_STATUS = 0x02, /* ro */
  24. LTC4215_FAULT = 0x03, /* rw */
  25. LTC4215_SENSE = 0x04, /* rw */
  26. LTC4215_SOURCE = 0x05, /* rw */
  27. LTC4215_ADIN = 0x06, /* rw */
  28. };
  29. struct ltc4215_data {
  30. struct i2c_client *client;
  31. struct mutex update_lock;
  32. bool valid;
  33. unsigned long last_updated; /* in jiffies */
  34. /* Registers */
  35. u8 regs[7];
  36. };
  37. static struct ltc4215_data *ltc4215_update_device(struct device *dev)
  38. {
  39. struct ltc4215_data *data = dev_get_drvdata(dev);
  40. struct i2c_client *client = data->client;
  41. s32 val;
  42. int i;
  43. mutex_lock(&data->update_lock);
  44. /* The chip's A/D updates 10 times per second */
  45. if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
  46. dev_dbg(&client->dev, "Starting ltc4215 update\n");
  47. /* Read all registers */
  48. for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
  49. val = i2c_smbus_read_byte_data(client, i);
  50. if (unlikely(val < 0))
  51. data->regs[i] = 0;
  52. else
  53. data->regs[i] = val;
  54. }
  55. data->last_updated = jiffies;
  56. data->valid = 1;
  57. }
  58. mutex_unlock(&data->update_lock);
  59. return data;
  60. }
  61. /* Return the voltage from the given register in millivolts */
  62. static int ltc4215_get_voltage(struct device *dev, u8 reg)
  63. {
  64. struct ltc4215_data *data = ltc4215_update_device(dev);
  65. const u8 regval = data->regs[reg];
  66. u32 voltage = 0;
  67. switch (reg) {
  68. case LTC4215_SENSE:
  69. /* 151 uV per increment */
  70. voltage = regval * 151 / 1000;
  71. break;
  72. case LTC4215_SOURCE:
  73. /* 60.5 mV per increment */
  74. voltage = regval * 605 / 10;
  75. break;
  76. case LTC4215_ADIN:
  77. /*
  78. * The ADIN input is divided by 12.5, and has 4.82 mV
  79. * per increment, so we have the additional multiply
  80. */
  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. /*
  95. * The strange looking conversions that follow are fixed-point
  96. * math, since we cannot do floating point in the kernel.
  97. *
  98. * Step 1: convert sense register to microVolts
  99. * Step 2: convert voltage to milliAmperes
  100. *
  101. * If you play around with the V=IR equation, you come up with
  102. * the following: X uV / Y mOhm == Z mA
  103. *
  104. * With the resistors that are fractions of a milliOhm, we multiply
  105. * the voltage and resistance by 10, to shift the decimal point.
  106. * Now we can use the normal division operator again.
  107. */
  108. /* Calculate voltage in microVolts (151 uV per increment) */
  109. const unsigned int voltage = data->regs[LTC4215_SENSE] * 151;
  110. /* Calculate current in milliAmperes (4 milliOhm sense resistor) */
  111. const unsigned int curr = voltage / 4;
  112. return curr;
  113. }
  114. static ssize_t ltc4215_voltage_show(struct device *dev,
  115. struct device_attribute *da, 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_current_show(struct device *dev,
  122. struct device_attribute *da, char *buf)
  123. {
  124. const unsigned int curr = ltc4215_get_current(dev);
  125. return snprintf(buf, PAGE_SIZE, "%u\n", curr);
  126. }
  127. static ssize_t ltc4215_power_show(struct device *dev,
  128. struct device_attribute *da, char *buf)
  129. {
  130. const unsigned int curr = ltc4215_get_current(dev);
  131. const int output_voltage = ltc4215_get_voltage(dev, LTC4215_ADIN);
  132. /* current in mA * voltage in mV == power in uW */
  133. const unsigned int power = abs(output_voltage * curr);
  134. return snprintf(buf, PAGE_SIZE, "%u\n", power);
  135. }
  136. static ssize_t ltc4215_alarm_show(struct device *dev,
  137. struct device_attribute *da, char *buf)
  138. {
  139. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  140. struct ltc4215_data *data = ltc4215_update_device(dev);
  141. const u8 reg = data->regs[LTC4215_STATUS];
  142. const u32 mask = attr->index;
  143. return snprintf(buf, PAGE_SIZE, "%u\n", !!(reg & mask));
  144. }
  145. /*
  146. * These macros are used below in constructing device attribute objects
  147. * for use with sysfs_create_group() to make a sysfs device file
  148. * for each register.
  149. */
  150. /* Construct a sensor_device_attribute structure for each register */
  151. /* Current */
  152. static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc4215_current, 0);
  153. static SENSOR_DEVICE_ATTR_RO(curr1_max_alarm, ltc4215_alarm, 1 << 2);
  154. /* Power (virtual) */
  155. static SENSOR_DEVICE_ATTR_RO(power1_input, ltc4215_power, 0);
  156. /* Input Voltage */
  157. static SENSOR_DEVICE_ATTR_RO(in1_input, ltc4215_voltage, LTC4215_ADIN);
  158. static SENSOR_DEVICE_ATTR_RO(in1_max_alarm, ltc4215_alarm, 1 << 0);
  159. static SENSOR_DEVICE_ATTR_RO(in1_min_alarm, ltc4215_alarm, 1 << 1);
  160. /* Output Voltage */
  161. static SENSOR_DEVICE_ATTR_RO(in2_input, ltc4215_voltage, LTC4215_SOURCE);
  162. static SENSOR_DEVICE_ATTR_RO(in2_min_alarm, ltc4215_alarm, 1 << 3);
  163. /*
  164. * Finally, construct an array of pointers to members of the above objects,
  165. * as required for sysfs_create_group()
  166. */
  167. static struct attribute *ltc4215_attrs[] = {
  168. &sensor_dev_attr_curr1_input.dev_attr.attr,
  169. &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
  170. &sensor_dev_attr_power1_input.dev_attr.attr,
  171. &sensor_dev_attr_in1_input.dev_attr.attr,
  172. &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
  173. &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
  174. &sensor_dev_attr_in2_input.dev_attr.attr,
  175. &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
  176. NULL,
  177. };
  178. ATTRIBUTE_GROUPS(ltc4215);
  179. static int ltc4215_probe(struct i2c_client *client)
  180. {
  181. struct i2c_adapter *adapter = client->adapter;
  182. struct device *dev = &client->dev;
  183. struct ltc4215_data *data;
  184. struct device *hwmon_dev;
  185. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  186. return -ENODEV;
  187. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  188. if (!data)
  189. return -ENOMEM;
  190. data->client = client;
  191. mutex_init(&data->update_lock);
  192. /* Initialize the LTC4215 chip */
  193. i2c_smbus_write_byte_data(client, LTC4215_FAULT, 0x00);
  194. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  195. data,
  196. ltc4215_groups);
  197. return PTR_ERR_OR_ZERO(hwmon_dev);
  198. }
  199. static const struct i2c_device_id ltc4215_id[] = {
  200. { "ltc4215", 0 },
  201. { }
  202. };
  203. MODULE_DEVICE_TABLE(i2c, ltc4215_id);
  204. /* This is the driver that will be inserted */
  205. static struct i2c_driver ltc4215_driver = {
  206. .driver = {
  207. .name = "ltc4215",
  208. },
  209. .probe_new = ltc4215_probe,
  210. .id_table = ltc4215_id,
  211. };
  212. module_i2c_driver(ltc4215_driver);
  213. MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
  214. MODULE_DESCRIPTION("LTC4215 driver");
  215. MODULE_LICENSE("GPL");