ina2xx.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Driver for Texas Instruments INA219, INA226 power monitor chips
  3. *
  4. * INA219:
  5. * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface
  6. * Datasheet: http://www.ti.com/product/ina219
  7. *
  8. * INA220:
  9. * Bi-Directional Current/Power Monitor with I2C Interface
  10. * Datasheet: http://www.ti.com/product/ina220
  11. *
  12. * INA226:
  13. * Bi-Directional Current/Power Monitor with I2C Interface
  14. * Datasheet: http://www.ti.com/product/ina226
  15. *
  16. * INA230:
  17. * Bi-directional Current/Power Monitor with I2C Interface
  18. * Datasheet: http://www.ti.com/product/ina230
  19. *
  20. * Copyright (C) 2012 Lothar Felten <l-felten@ti.com>
  21. * Thanks to Jan Volkering
  22. *
  23. * This program is free software; you can redistribute it and/or modify
  24. * it under the terms of the GNU General Public License as published by
  25. * the Free Software Foundation; version 2 of the License.
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/err.h>
  31. #include <linux/slab.h>
  32. #include <linux/i2c.h>
  33. #include <linux/hwmon.h>
  34. #include <linux/hwmon-sysfs.h>
  35. #include <linux/jiffies.h>
  36. #include <linux/platform_data/ina2xx.h>
  37. /* common register definitions */
  38. #define INA2XX_CONFIG 0x00
  39. #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
  40. #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
  41. #define INA2XX_POWER 0x03 /* readonly */
  42. #define INA2XX_CURRENT 0x04 /* readonly */
  43. #define INA2XX_CALIBRATION 0x05
  44. /* INA226 register definitions */
  45. #define INA226_MASK_ENABLE 0x06
  46. #define INA226_ALERT_LIMIT 0x07
  47. #define INA226_DIE_ID 0xFF
  48. /* register count */
  49. #define INA219_REGISTERS 6
  50. #define INA226_REGISTERS 8
  51. #define INA2XX_MAX_REGISTERS 8
  52. /* settings - depend on use case */
  53. #define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
  54. #define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
  55. /* worst case is 68.10 ms (~14.6Hz, ina219) */
  56. #define INA2XX_CONVERSION_RATE 15
  57. enum ina2xx_ids { ina219, ina226 };
  58. struct ina2xx_config {
  59. u16 config_default;
  60. int calibration_factor;
  61. int registers;
  62. int shunt_div;
  63. int bus_voltage_shift;
  64. int bus_voltage_lsb; /* uV */
  65. int power_lsb; /* uW */
  66. };
  67. struct ina2xx_data {
  68. struct device *hwmon_dev;
  69. const struct ina2xx_config *config;
  70. struct mutex update_lock;
  71. bool valid;
  72. unsigned long last_updated;
  73. int kind;
  74. u16 regs[INA2XX_MAX_REGISTERS];
  75. };
  76. static const struct ina2xx_config ina2xx_config[] = {
  77. [ina219] = {
  78. .config_default = INA219_CONFIG_DEFAULT,
  79. .calibration_factor = 40960000,
  80. .registers = INA219_REGISTERS,
  81. .shunt_div = 100,
  82. .bus_voltage_shift = 3,
  83. .bus_voltage_lsb = 4000,
  84. .power_lsb = 20000,
  85. },
  86. [ina226] = {
  87. .config_default = INA226_CONFIG_DEFAULT,
  88. .calibration_factor = 5120000,
  89. .registers = INA226_REGISTERS,
  90. .shunt_div = 400,
  91. .bus_voltage_shift = 0,
  92. .bus_voltage_lsb = 1250,
  93. .power_lsb = 25000,
  94. },
  95. };
  96. static struct ina2xx_data *ina2xx_update_device(struct device *dev)
  97. {
  98. struct i2c_client *client = to_i2c_client(dev);
  99. struct ina2xx_data *data = i2c_get_clientdata(client);
  100. struct ina2xx_data *ret = data;
  101. mutex_lock(&data->update_lock);
  102. if (time_after(jiffies, data->last_updated +
  103. HZ / INA2XX_CONVERSION_RATE) || !data->valid) {
  104. int i;
  105. dev_dbg(&client->dev, "Starting ina2xx update\n");
  106. /* Read all registers */
  107. for (i = 0; i < data->config->registers; i++) {
  108. int rv = i2c_smbus_read_word_swapped(client, i);
  109. if (rv < 0) {
  110. ret = ERR_PTR(rv);
  111. goto abort;
  112. }
  113. data->regs[i] = rv;
  114. }
  115. data->last_updated = jiffies;
  116. data->valid = 1;
  117. }
  118. abort:
  119. mutex_unlock(&data->update_lock);
  120. return ret;
  121. }
  122. static int ina2xx_get_value(struct ina2xx_data *data, u8 reg)
  123. {
  124. int val;
  125. switch (reg) {
  126. case INA2XX_SHUNT_VOLTAGE:
  127. val = DIV_ROUND_CLOSEST(data->regs[reg],
  128. data->config->shunt_div);
  129. break;
  130. case INA2XX_BUS_VOLTAGE:
  131. val = (data->regs[reg] >> data->config->bus_voltage_shift)
  132. * data->config->bus_voltage_lsb;
  133. val = DIV_ROUND_CLOSEST(val, 1000);
  134. break;
  135. case INA2XX_POWER:
  136. val = data->regs[reg] * data->config->power_lsb;
  137. break;
  138. case INA2XX_CURRENT:
  139. /* LSB=1mA (selected). Is in mA */
  140. val = data->regs[reg];
  141. break;
  142. default:
  143. /* programmer goofed */
  144. WARN_ON_ONCE(1);
  145. val = 0;
  146. break;
  147. }
  148. return val;
  149. }
  150. static ssize_t ina2xx_show_value(struct device *dev,
  151. struct device_attribute *da, char *buf)
  152. {
  153. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  154. struct ina2xx_data *data = ina2xx_update_device(dev);
  155. if (IS_ERR(data))
  156. return PTR_ERR(data);
  157. return snprintf(buf, PAGE_SIZE, "%d\n",
  158. ina2xx_get_value(data, attr->index));
  159. }
  160. /* shunt voltage */
  161. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, \
  162. ina2xx_show_value, NULL, INA2XX_SHUNT_VOLTAGE);
  163. /* bus voltage */
  164. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, \
  165. ina2xx_show_value, NULL, INA2XX_BUS_VOLTAGE);
  166. /* calculated current */
  167. static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, \
  168. ina2xx_show_value, NULL, INA2XX_CURRENT);
  169. /* calculated power */
  170. static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, \
  171. ina2xx_show_value, NULL, INA2XX_POWER);
  172. /* pointers to created device attributes */
  173. static struct attribute *ina2xx_attributes[] = {
  174. &sensor_dev_attr_in0_input.dev_attr.attr,
  175. &sensor_dev_attr_in1_input.dev_attr.attr,
  176. &sensor_dev_attr_curr1_input.dev_attr.attr,
  177. &sensor_dev_attr_power1_input.dev_attr.attr,
  178. NULL,
  179. };
  180. static const struct attribute_group ina2xx_group = {
  181. .attrs = ina2xx_attributes,
  182. };
  183. static int ina2xx_probe(struct i2c_client *client,
  184. const struct i2c_device_id *id)
  185. {
  186. struct i2c_adapter *adapter = client->adapter;
  187. struct ina2xx_data *data;
  188. struct ina2xx_platform_data *pdata;
  189. int ret;
  190. long shunt = 10000; /* default shunt value 10mOhms */
  191. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
  192. return -ENODEV;
  193. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  194. if (!data)
  195. return -ENOMEM;
  196. if (client->dev.platform_data) {
  197. pdata =
  198. (struct ina2xx_platform_data *)client->dev.platform_data;
  199. shunt = pdata->shunt_uohms;
  200. }
  201. if (shunt <= 0)
  202. return -ENODEV;
  203. /* set the device type */
  204. data->kind = id->driver_data;
  205. data->config = &ina2xx_config[data->kind];
  206. /* device configuration */
  207. i2c_smbus_write_word_swapped(client, INA2XX_CONFIG,
  208. data->config->config_default);
  209. /* set current LSB to 1mA, shunt is in uOhms */
  210. /* (equation 13 in datasheet) */
  211. i2c_smbus_write_word_swapped(client, INA2XX_CALIBRATION,
  212. data->config->calibration_factor / shunt);
  213. i2c_set_clientdata(client, data);
  214. mutex_init(&data->update_lock);
  215. ret = sysfs_create_group(&client->dev.kobj, &ina2xx_group);
  216. if (ret)
  217. return ret;
  218. data->hwmon_dev = hwmon_device_register(&client->dev);
  219. if (IS_ERR(data->hwmon_dev)) {
  220. ret = PTR_ERR(data->hwmon_dev);
  221. goto out_err_hwmon;
  222. }
  223. dev_info(&client->dev, "power monitor %s (Rshunt = %li uOhm)\n",
  224. id->name, shunt);
  225. return 0;
  226. out_err_hwmon:
  227. sysfs_remove_group(&client->dev.kobj, &ina2xx_group);
  228. return ret;
  229. }
  230. static int ina2xx_remove(struct i2c_client *client)
  231. {
  232. struct ina2xx_data *data = i2c_get_clientdata(client);
  233. hwmon_device_unregister(data->hwmon_dev);
  234. sysfs_remove_group(&client->dev.kobj, &ina2xx_group);
  235. return 0;
  236. }
  237. static const struct i2c_device_id ina2xx_id[] = {
  238. { "ina219", ina219 },
  239. { "ina220", ina219 },
  240. { "ina226", ina226 },
  241. { "ina230", ina226 },
  242. { }
  243. };
  244. MODULE_DEVICE_TABLE(i2c, ina2xx_id);
  245. static struct i2c_driver ina2xx_driver = {
  246. .driver = {
  247. .name = "ina2xx",
  248. },
  249. .probe = ina2xx_probe,
  250. .remove = ina2xx_remove,
  251. .id_table = ina2xx_id,
  252. };
  253. module_i2c_driver(ina2xx_driver);
  254. MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
  255. MODULE_DESCRIPTION("ina2xx driver");
  256. MODULE_LICENSE("GPL");