ltc4151.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. /* Finally, construct an array of pointers to members of the above objects,
  137. * as required for sysfs_create_group()
  138. */
  139. static struct attribute *ltc4151_attributes[] = {
  140. &sensor_dev_attr_in1_input.dev_attr.attr,
  141. &sensor_dev_attr_in2_input.dev_attr.attr,
  142. &sensor_dev_attr_curr1_input.dev_attr.attr,
  143. NULL,
  144. };
  145. static const struct attribute_group ltc4151_group = {
  146. .attrs = ltc4151_attributes,
  147. };
  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 ltc4151_data *data;
  153. int ret;
  154. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  155. return -ENODEV;
  156. data = kzalloc(sizeof(*data), GFP_KERNEL);
  157. if (!data) {
  158. ret = -ENOMEM;
  159. goto out_kzalloc;
  160. }
  161. i2c_set_clientdata(client, data);
  162. mutex_init(&data->update_lock);
  163. /* Register sysfs hooks */
  164. ret = sysfs_create_group(&client->dev.kobj, &ltc4151_group);
  165. if (ret)
  166. goto out_sysfs_create_group;
  167. data->hwmon_dev = hwmon_device_register(&client->dev);
  168. if (IS_ERR(data->hwmon_dev)) {
  169. ret = PTR_ERR(data->hwmon_dev);
  170. goto out_hwmon_device_register;
  171. }
  172. return 0;
  173. out_hwmon_device_register:
  174. sysfs_remove_group(&client->dev.kobj, &ltc4151_group);
  175. out_sysfs_create_group:
  176. kfree(data);
  177. out_kzalloc:
  178. return ret;
  179. }
  180. static int ltc4151_remove(struct i2c_client *client)
  181. {
  182. struct ltc4151_data *data = i2c_get_clientdata(client);
  183. hwmon_device_unregister(data->hwmon_dev);
  184. sysfs_remove_group(&client->dev.kobj, &ltc4151_group);
  185. kfree(data);
  186. return 0;
  187. }
  188. static const struct i2c_device_id ltc4151_id[] = {
  189. { "ltc4151", 0 },
  190. { }
  191. };
  192. MODULE_DEVICE_TABLE(i2c, ltc4151_id);
  193. /* This is the driver that will be inserted */
  194. static struct i2c_driver ltc4151_driver = {
  195. .driver = {
  196. .name = "ltc4151",
  197. },
  198. .probe = ltc4151_probe,
  199. .remove = ltc4151_remove,
  200. .id_table = ltc4151_id,
  201. };
  202. static int __init ltc4151_init(void)
  203. {
  204. return i2c_add_driver(&ltc4151_driver);
  205. }
  206. static void __exit ltc4151_exit(void)
  207. {
  208. i2c_del_driver(&ltc4151_driver);
  209. }
  210. MODULE_AUTHOR("Per Dalen <per.dalen@appeartv.com>");
  211. MODULE_DESCRIPTION("LTC4151 driver");
  212. MODULE_LICENSE("GPL");
  213. module_init(ltc4151_init);
  214. module_exit(ltc4151_exit);