ltc4151.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Driver for Linear Technology LTC4151 High Voltage I2C Current
  3. * and Voltage Monitor
  4. *
  5. * Copyright (C) 2011 AppearTV AS
  6. *
  7. * Derived from:
  8. *
  9. * Driver for Linear Technology LTC4261 I2C Negative Voltage Hot
  10. * Swap Controller
  11. * Copyright (C) 2010 Ericsson AB.
  12. *
  13. * Datasheet: http://www.linear.com/docs/Datasheet/4151fc.pdf
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. *
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/of.h>
  33. #include <linux/init.h>
  34. #include <linux/err.h>
  35. #include <linux/slab.h>
  36. #include <linux/i2c.h>
  37. #include <linux/hwmon.h>
  38. #include <linux/hwmon-sysfs.h>
  39. #include <linux/jiffies.h>
  40. /* chip registers */
  41. #define LTC4151_SENSE_H 0x00
  42. #define LTC4151_SENSE_L 0x01
  43. #define LTC4151_VIN_H 0x02
  44. #define LTC4151_VIN_L 0x03
  45. #define LTC4151_ADIN_H 0x04
  46. #define LTC4151_ADIN_L 0x05
  47. struct ltc4151_data {
  48. struct i2c_client *client;
  49. struct mutex update_lock;
  50. bool valid;
  51. unsigned long last_updated; /* in jiffies */
  52. unsigned int shunt; /* in micro ohms */
  53. /* Registers */
  54. u8 regs[6];
  55. };
  56. static struct ltc4151_data *ltc4151_update_device(struct device *dev)
  57. {
  58. struct ltc4151_data *data = dev_get_drvdata(dev);
  59. struct i2c_client *client = data->client;
  60. struct ltc4151_data *ret = data;
  61. mutex_lock(&data->update_lock);
  62. /*
  63. * The chip's A/D updates 6 times per second
  64. * (Conversion Rate 6 - 9 Hz)
  65. */
  66. if (time_after(jiffies, data->last_updated + HZ / 6) || !data->valid) {
  67. int i;
  68. dev_dbg(&client->dev, "Starting ltc4151 update\n");
  69. /* Read all registers */
  70. for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
  71. int val;
  72. val = i2c_smbus_read_byte_data(client, i);
  73. if (unlikely(val < 0)) {
  74. dev_dbg(dev,
  75. "Failed to read ADC value: error %d\n",
  76. val);
  77. ret = ERR_PTR(val);
  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 */
  90. static int ltc4151_get_value(struct ltc4151_data *data, u8 reg)
  91. {
  92. u32 val;
  93. val = (data->regs[reg] << 4) + (data->regs[reg + 1] >> 4);
  94. switch (reg) {
  95. case LTC4151_ADIN_H:
  96. /* 500uV resolution. Convert to mV. */
  97. val = val * 500 / 1000;
  98. break;
  99. case LTC4151_SENSE_H:
  100. /*
  101. * 20uV resolution. Convert to current as measured with
  102. * a given sense resistor, in mA.
  103. */
  104. val = val * 20 * 1000 / data->shunt;
  105. break;
  106. case LTC4151_VIN_H:
  107. /* 25 mV per increment */
  108. val = val * 25;
  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 ltc4151_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 ltc4151_data *data = ltc4151_update_device(dev);
  123. int value;
  124. if (IS_ERR(data))
  125. return PTR_ERR(data);
  126. value = ltc4151_get_value(data, attr->index);
  127. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  128. }
  129. /*
  130. * Input voltages.
  131. */
  132. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc4151_show_value, NULL,
  133. LTC4151_VIN_H);
  134. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc4151_show_value, NULL,
  135. LTC4151_ADIN_H);
  136. /* Currents (via sense resistor) */
  137. static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc4151_show_value, NULL,
  138. LTC4151_SENSE_H);
  139. /*
  140. * Finally, construct an array of pointers to members of the above objects,
  141. * as required for sysfs_create_group()
  142. */
  143. static struct attribute *ltc4151_attrs[] = {
  144. &sensor_dev_attr_in1_input.dev_attr.attr,
  145. &sensor_dev_attr_in2_input.dev_attr.attr,
  146. &sensor_dev_attr_curr1_input.dev_attr.attr,
  147. NULL,
  148. };
  149. ATTRIBUTE_GROUPS(ltc4151);
  150. static int ltc4151_probe(struct i2c_client *client,
  151. const struct i2c_device_id *id)
  152. {
  153. struct i2c_adapter *adapter = client->adapter;
  154. struct device *dev = &client->dev;
  155. struct ltc4151_data *data;
  156. struct device *hwmon_dev;
  157. u32 shunt;
  158. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  159. return -ENODEV;
  160. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  161. if (!data)
  162. return -ENOMEM;
  163. if (of_property_read_u32(client->dev.of_node,
  164. "shunt-resistor-micro-ohms", &shunt))
  165. shunt = 1000; /* 1 mOhm if not set via DT */
  166. if (shunt == 0)
  167. return -EINVAL;
  168. data->shunt = shunt;
  169. data->client = client;
  170. mutex_init(&data->update_lock);
  171. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  172. data,
  173. ltc4151_groups);
  174. return PTR_ERR_OR_ZERO(hwmon_dev);
  175. }
  176. static const struct i2c_device_id ltc4151_id[] = {
  177. { "ltc4151", 0 },
  178. { }
  179. };
  180. MODULE_DEVICE_TABLE(i2c, ltc4151_id);
  181. static const struct of_device_id ltc4151_match[] = {
  182. { .compatible = "lltc,ltc4151" },
  183. {},
  184. };
  185. /* This is the driver that will be inserted */
  186. static struct i2c_driver ltc4151_driver = {
  187. .driver = {
  188. .name = "ltc4151",
  189. .of_match_table = of_match_ptr(ltc4151_match),
  190. },
  191. .probe = ltc4151_probe,
  192. .id_table = ltc4151_id,
  193. };
  194. module_i2c_driver(ltc4151_driver);
  195. MODULE_AUTHOR("Per Dalen <per.dalen@appeartv.com>");
  196. MODULE_DESCRIPTION("LTC4151 driver");
  197. MODULE_LICENSE("GPL");