ltc4151.c 5.8 KB

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