ina2xx.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. * INA226:
  9. * Bi-Directional Current/Power Monitor with I2C Interface
  10. * Datasheet: http://www.ti.com/product/ina226
  11. *
  12. * Copyright (C) 2012 Lothar Felten <l-felten@ti.com>
  13. * Thanks to Jan Volkering
  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; version 2 of the License.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/err.h>
  23. #include <linux/slab.h>
  24. #include <linux/i2c.h>
  25. #include <linux/hwmon.h>
  26. #include <linux/hwmon-sysfs.h>
  27. #include <linux/platform_data/ina2xx.h>
  28. /* common register definitions */
  29. #define INA2XX_CONFIG 0x00
  30. #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
  31. #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
  32. #define INA2XX_POWER 0x03 /* readonly */
  33. #define INA2XX_CURRENT 0x04 /* readonly */
  34. #define INA2XX_CALIBRATION 0x05
  35. /* INA226 register definitions */
  36. #define INA226_MASK_ENABLE 0x06
  37. #define INA226_ALERT_LIMIT 0x07
  38. #define INA226_DIE_ID 0xFF
  39. /* register count */
  40. #define INA219_REGISTERS 6
  41. #define INA226_REGISTERS 8
  42. #define INA2XX_MAX_REGISTERS 8
  43. /* settings - depend on use case */
  44. #define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
  45. #define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
  46. /* worst case is 68.10 ms (~14.6Hz, ina219) */
  47. #define INA2XX_CONVERSION_RATE 15
  48. enum ina2xx_ids { ina219, ina226 };
  49. struct ina2xx_data {
  50. struct device *hwmon_dev;
  51. struct mutex update_lock;
  52. bool valid;
  53. unsigned long last_updated;
  54. int kind;
  55. int registers;
  56. u16 regs[INA2XX_MAX_REGISTERS];
  57. };
  58. static struct ina2xx_data *ina2xx_update_device(struct device *dev)
  59. {
  60. struct i2c_client *client = to_i2c_client(dev);
  61. struct ina2xx_data *data = i2c_get_clientdata(client);
  62. struct ina2xx_data *ret = data;
  63. mutex_lock(&data->update_lock);
  64. if (time_after(jiffies, data->last_updated +
  65. HZ / INA2XX_CONVERSION_RATE) || !data->valid) {
  66. int i;
  67. dev_dbg(&client->dev, "Starting ina2xx update\n");
  68. /* Read all registers */
  69. for (i = 0; i < data->registers; i++) {
  70. int rv = i2c_smbus_read_word_swapped(client, i);
  71. if (rv < 0) {
  72. ret = ERR_PTR(rv);
  73. goto abort;
  74. }
  75. data->regs[i] = rv;
  76. }
  77. data->last_updated = jiffies;
  78. data->valid = 1;
  79. }
  80. abort:
  81. mutex_unlock(&data->update_lock);
  82. return ret;
  83. }
  84. static int ina219_get_value(struct ina2xx_data *data, u8 reg)
  85. {
  86. /*
  87. * calculate exact value for the given register
  88. * we assume default power-on reset settings:
  89. * bus voltage range 32V
  90. * gain = /8
  91. * adc 1 & 2 -> conversion time 532uS
  92. * mode is continuous shunt and bus
  93. * calibration value is INA219_CALIBRATION_VALUE
  94. */
  95. int val = data->regs[reg];
  96. switch (reg) {
  97. case INA2XX_SHUNT_VOLTAGE:
  98. /* LSB=10uV. Convert to mV. */
  99. val = DIV_ROUND_CLOSEST(val, 100);
  100. break;
  101. case INA2XX_BUS_VOLTAGE:
  102. /* LSB=4mV. Register is not right aligned, convert to mV. */
  103. val = (val >> 3) * 4;
  104. break;
  105. case INA2XX_POWER:
  106. /* LSB=20mW. Convert to uW */
  107. val = val * 20 * 1000;
  108. break;
  109. case INA2XX_CURRENT:
  110. /* LSB=1mA (selected). Is in mA */
  111. break;
  112. default:
  113. /* programmer goofed */
  114. WARN_ON_ONCE(1);
  115. val = 0;
  116. break;
  117. }
  118. return val;
  119. }
  120. static int ina226_get_value(struct ina2xx_data *data, u8 reg)
  121. {
  122. /*
  123. * calculate exact value for the given register
  124. * we assume default power-on reset settings:
  125. * bus voltage range 32V
  126. * gain = /8
  127. * adc 1 & 2 -> conversion time 532uS
  128. * mode is continuous shunt and bus
  129. * calibration value is INA226_CALIBRATION_VALUE
  130. */
  131. int val = data->regs[reg];
  132. switch (reg) {
  133. case INA2XX_SHUNT_VOLTAGE:
  134. /* LSB=2.5uV. Convert to mV. */
  135. val = DIV_ROUND_CLOSEST(val, 400);
  136. break;
  137. case INA2XX_BUS_VOLTAGE:
  138. /* LSB=1.25mV. Convert to mV. */
  139. val = val + DIV_ROUND_CLOSEST(val, 4);
  140. break;
  141. case INA2XX_POWER:
  142. /* LSB=25mW. Convert to uW */
  143. val = val * 25 * 1000;
  144. break;
  145. case INA2XX_CURRENT:
  146. /* LSB=1mA (selected). Is in mA */
  147. break;
  148. default:
  149. /* programmer goofed */
  150. WARN_ON_ONCE(1);
  151. val = 0;
  152. break;
  153. }
  154. return val;
  155. }
  156. static ssize_t ina2xx_show_value(struct device *dev,
  157. struct device_attribute *da, char *buf)
  158. {
  159. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  160. struct ina2xx_data *data = ina2xx_update_device(dev);
  161. int value = 0;
  162. if (IS_ERR(data))
  163. return PTR_ERR(data);
  164. switch (data->kind) {
  165. case ina219:
  166. value = ina219_get_value(data, attr->index);
  167. break;
  168. case ina226:
  169. value = ina226_get_value(data, attr->index);
  170. break;
  171. default:
  172. WARN_ON_ONCE(1);
  173. break;
  174. }
  175. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  176. }
  177. /* shunt voltage */
  178. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, \
  179. ina2xx_show_value, NULL, INA2XX_SHUNT_VOLTAGE);
  180. /* bus voltage */
  181. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, \
  182. ina2xx_show_value, NULL, INA2XX_BUS_VOLTAGE);
  183. /* calculated current */
  184. static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, \
  185. ina2xx_show_value, NULL, INA2XX_CURRENT);
  186. /* calculated power */
  187. static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, \
  188. ina2xx_show_value, NULL, INA2XX_POWER);
  189. /* pointers to created device attributes */
  190. static struct attribute *ina2xx_attributes[] = {
  191. &sensor_dev_attr_in0_input.dev_attr.attr,
  192. &sensor_dev_attr_in1_input.dev_attr.attr,
  193. &sensor_dev_attr_curr1_input.dev_attr.attr,
  194. &sensor_dev_attr_power1_input.dev_attr.attr,
  195. NULL,
  196. };
  197. static const struct attribute_group ina2xx_group = {
  198. .attrs = ina2xx_attributes,
  199. };
  200. static int ina2xx_probe(struct i2c_client *client,
  201. const struct i2c_device_id *id)
  202. {
  203. struct i2c_adapter *adapter = client->adapter;
  204. struct ina2xx_data *data;
  205. struct ina2xx_platform_data *pdata;
  206. int ret = 0;
  207. long shunt = 10000; /* default shunt value 10mOhms */
  208. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
  209. return -ENODEV;
  210. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  211. if (!data)
  212. return -ENOMEM;
  213. if (client->dev.platform_data) {
  214. pdata =
  215. (struct ina2xx_platform_data *)client->dev.platform_data;
  216. shunt = pdata->shunt_uohms;
  217. }
  218. if (shunt <= 0)
  219. return -ENODEV;
  220. /* set the device type */
  221. data->kind = id->driver_data;
  222. switch (data->kind) {
  223. case ina219:
  224. /* device configuration */
  225. i2c_smbus_write_word_swapped(client, INA2XX_CONFIG,
  226. INA219_CONFIG_DEFAULT);
  227. /* set current LSB to 1mA, shunt is in uOhms */
  228. /* (equation 13 in datasheet) */
  229. i2c_smbus_write_word_swapped(client, INA2XX_CALIBRATION,
  230. 40960000 / shunt);
  231. dev_info(&client->dev,
  232. "power monitor INA219 (Rshunt = %li uOhm)\n", shunt);
  233. data->registers = INA219_REGISTERS;
  234. break;
  235. case ina226:
  236. /* device configuration */
  237. i2c_smbus_write_word_swapped(client, INA2XX_CONFIG,
  238. INA226_CONFIG_DEFAULT);
  239. /* set current LSB to 1mA, shunt is in uOhms */
  240. /* (equation 1 in datasheet)*/
  241. i2c_smbus_write_word_swapped(client, INA2XX_CALIBRATION,
  242. 5120000 / shunt);
  243. dev_info(&client->dev,
  244. "power monitor INA226 (Rshunt = %li uOhm)\n", shunt);
  245. data->registers = INA226_REGISTERS;
  246. break;
  247. default:
  248. /* unknown device id */
  249. return -ENODEV;
  250. }
  251. i2c_set_clientdata(client, data);
  252. mutex_init(&data->update_lock);
  253. ret = sysfs_create_group(&client->dev.kobj, &ina2xx_group);
  254. if (ret)
  255. return ret;
  256. data->hwmon_dev = hwmon_device_register(&client->dev);
  257. if (IS_ERR(data->hwmon_dev)) {
  258. ret = PTR_ERR(data->hwmon_dev);
  259. goto out_err_hwmon;
  260. }
  261. return 0;
  262. out_err_hwmon:
  263. sysfs_remove_group(&client->dev.kobj, &ina2xx_group);
  264. return ret;
  265. }
  266. static int ina2xx_remove(struct i2c_client *client)
  267. {
  268. struct ina2xx_data *data = i2c_get_clientdata(client);
  269. hwmon_device_unregister(data->hwmon_dev);
  270. sysfs_remove_group(&client->dev.kobj, &ina2xx_group);
  271. return 0;
  272. }
  273. static const struct i2c_device_id ina2xx_id[] = {
  274. { "ina219", ina219 },
  275. { "ina226", ina226 },
  276. { }
  277. };
  278. MODULE_DEVICE_TABLE(i2c, ina2xx_id);
  279. static struct i2c_driver ina2xx_driver = {
  280. .driver = {
  281. .name = "ina2xx",
  282. },
  283. .probe = ina2xx_probe,
  284. .remove = ina2xx_remove,
  285. .id_table = ina2xx_id,
  286. };
  287. static int __init ina2xx_init(void)
  288. {
  289. return i2c_add_driver(&ina2xx_driver);
  290. }
  291. static void __exit ina2xx_exit(void)
  292. {
  293. i2c_del_driver(&ina2xx_driver);
  294. }
  295. MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
  296. MODULE_DESCRIPTION("ina2xx driver");
  297. MODULE_LICENSE("GPL");
  298. module_init(ina2xx_init);
  299. module_exit(ina2xx_exit);