ltc4151.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/init.h>
  33. #include <linux/err.h>
  34. #include <linux/slab.h>
  35. #include <linux/i2c.h>
  36. #include <linux/hwmon.h>
  37. #include <linux/hwmon-sysfs.h>
  38. #include <linux/jiffies.h>
  39. /* chip registers */
  40. #define LTC4151_SENSE_H 0x00
  41. #define LTC4151_SENSE_L 0x01
  42. #define LTC4151_VIN_H 0x02
  43. #define LTC4151_VIN_L 0x03
  44. #define LTC4151_ADIN_H 0x04
  45. #define LTC4151_ADIN_L 0x05
  46. struct ltc4151_data {
  47. struct i2c_client *client;
  48. struct mutex update_lock;
  49. bool valid;
  50. unsigned long last_updated; /* in jiffies */
  51. /* Registers */
  52. u8 regs[6];
  53. };
  54. static struct ltc4151_data *ltc4151_update_device(struct device *dev)
  55. {
  56. struct ltc4151_data *data = dev_get_drvdata(dev);
  57. struct i2c_client *client = data->client;
  58. struct ltc4151_data *ret = data;
  59. mutex_lock(&data->update_lock);
  60. /*
  61. * The chip's A/D updates 6 times per second
  62. * (Conversion Rate 6 - 9 Hz)
  63. */
  64. if (time_after(jiffies, data->last_updated + HZ / 6) || !data->valid) {
  65. int i;
  66. dev_dbg(&client->dev, "Starting ltc4151 update\n");
  67. /* Read all registers */
  68. for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
  69. int val;
  70. val = i2c_smbus_read_byte_data(client, i);
  71. if (unlikely(val < 0)) {
  72. dev_dbg(dev,
  73. "Failed to read ADC value: error %d\n",
  74. val);
  75. ret = ERR_PTR(val);
  76. goto abort;
  77. }
  78. data->regs[i] = val;
  79. }
  80. data->last_updated = jiffies;
  81. data->valid = 1;
  82. }
  83. abort:
  84. mutex_unlock(&data->update_lock);
  85. return ret;
  86. }
  87. /* Return the voltage from the given register in mV */
  88. static int ltc4151_get_value(struct ltc4151_data *data, u8 reg)
  89. {
  90. u32 val;
  91. val = (data->regs[reg] << 4) + (data->regs[reg + 1] >> 4);
  92. switch (reg) {
  93. case LTC4151_ADIN_H:
  94. /* 500uV resolution. Convert to mV. */
  95. val = val * 500 / 1000;
  96. break;
  97. case LTC4151_SENSE_H:
  98. /*
  99. * 20uV resolution. Convert to current as measured with
  100. * an 1 mOhm sense resistor, in mA.
  101. */
  102. val = val * 20;
  103. break;
  104. case LTC4151_VIN_H:
  105. /* 25 mV per increment */
  106. val = val * 25;
  107. break;
  108. default:
  109. /* If we get here, the developer messed up */
  110. WARN_ON_ONCE(1);
  111. val = 0;
  112. break;
  113. }
  114. return val;
  115. }
  116. static ssize_t ltc4151_show_value(struct device *dev,
  117. struct device_attribute *da, char *buf)
  118. {
  119. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  120. struct ltc4151_data *data = ltc4151_update_device(dev);
  121. int value;
  122. if (IS_ERR(data))
  123. return PTR_ERR(data);
  124. value = ltc4151_get_value(data, attr->index);
  125. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  126. }
  127. /*
  128. * Input voltages.
  129. */
  130. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc4151_show_value, NULL,
  131. LTC4151_VIN_H);
  132. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc4151_show_value, NULL,
  133. LTC4151_ADIN_H);
  134. /* Currents (via sense resistor) */
  135. static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc4151_show_value, NULL,
  136. LTC4151_SENSE_H);
  137. /*
  138. * Finally, construct an array of pointers to members of the above objects,
  139. * as required for sysfs_create_group()
  140. */
  141. static struct attribute *ltc4151_attrs[] = {
  142. &sensor_dev_attr_in1_input.dev_attr.attr,
  143. &sensor_dev_attr_in2_input.dev_attr.attr,
  144. &sensor_dev_attr_curr1_input.dev_attr.attr,
  145. NULL,
  146. };
  147. ATTRIBUTE_GROUPS(ltc4151);
  148. static int ltc4151_probe(struct i2c_client *client,
  149. const struct i2c_device_id *id)
  150. {
  151. struct i2c_adapter *adapter = client->adapter;
  152. struct device *dev = &client->dev;
  153. struct ltc4151_data *data;
  154. struct device *hwmon_dev;
  155. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  156. return -ENODEV;
  157. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  158. if (!data)
  159. return -ENOMEM;
  160. data->client = client;
  161. mutex_init(&data->update_lock);
  162. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  163. data,
  164. ltc4151_groups);
  165. return PTR_ERR_OR_ZERO(hwmon_dev);
  166. }
  167. static const struct i2c_device_id ltc4151_id[] = {
  168. { "ltc4151", 0 },
  169. { }
  170. };
  171. MODULE_DEVICE_TABLE(i2c, ltc4151_id);
  172. /* This is the driver that will be inserted */
  173. static struct i2c_driver ltc4151_driver = {
  174. .driver = {
  175. .name = "ltc4151",
  176. },
  177. .probe = ltc4151_probe,
  178. .id_table = ltc4151_id,
  179. };
  180. module_i2c_driver(ltc4151_driver);
  181. MODULE_AUTHOR("Per Dalen <per.dalen@appeartv.com>");
  182. MODULE_DESCRIPTION("LTC4151 driver");
  183. MODULE_LICENSE("GPL");