ina2xx.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. int ina2xx_read_word(struct i2c_client *client, int reg)
  59. {
  60. int val = i2c_smbus_read_word_data(client, reg);
  61. if (unlikely(val < 0)) {
  62. dev_dbg(&client->dev,
  63. "Failed to read register: %d\n", reg);
  64. return val;
  65. }
  66. return be16_to_cpu(val);
  67. }
  68. void ina2xx_write_word(struct i2c_client *client, int reg, int data)
  69. {
  70. i2c_smbus_write_word_data(client, reg, cpu_to_be16(data));
  71. }
  72. static struct ina2xx_data *ina2xx_update_device(struct device *dev)
  73. {
  74. struct i2c_client *client = to_i2c_client(dev);
  75. struct ina2xx_data *data = i2c_get_clientdata(client);
  76. struct ina2xx_data *ret = data;
  77. mutex_lock(&data->update_lock);
  78. if (time_after(jiffies, data->last_updated +
  79. HZ / INA2XX_CONVERSION_RATE) || !data->valid) {
  80. int i;
  81. dev_dbg(&client->dev, "Starting ina2xx update\n");
  82. /* Read all registers */
  83. for (i = 0; i < data->registers; i++) {
  84. int rv = ina2xx_read_word(client, i);
  85. if (rv < 0) {
  86. ret = ERR_PTR(rv);
  87. goto abort;
  88. }
  89. data->regs[i] = rv;
  90. }
  91. data->last_updated = jiffies;
  92. data->valid = 1;
  93. }
  94. abort:
  95. mutex_unlock(&data->update_lock);
  96. return ret;
  97. }
  98. static int ina219_get_value(struct ina2xx_data *data, u8 reg)
  99. {
  100. /*
  101. * calculate exact value for the given register
  102. * we assume default power-on reset settings:
  103. * bus voltage range 32V
  104. * gain = /8
  105. * adc 1 & 2 -> conversion time 532uS
  106. * mode is continuous shunt and bus
  107. * calibration value is INA219_CALIBRATION_VALUE
  108. */
  109. int val = data->regs[reg];
  110. switch (reg) {
  111. case INA2XX_SHUNT_VOLTAGE:
  112. /* LSB=10uV. Convert to mV. */
  113. val = DIV_ROUND_CLOSEST(val, 100);
  114. break;
  115. case INA2XX_BUS_VOLTAGE:
  116. /* LSB=4mV. Register is not right aligned, convert to mV. */
  117. val = (val >> 3) * 4;
  118. break;
  119. case INA2XX_POWER:
  120. /* LSB=20mW. Convert to uW */
  121. val = val * 20 * 1000;
  122. break;
  123. case INA2XX_CURRENT:
  124. /* LSB=1mA (selected). Is in mA */
  125. break;
  126. default:
  127. /* programmer goofed */
  128. WARN_ON_ONCE(1);
  129. val = 0;
  130. break;
  131. }
  132. return val;
  133. }
  134. static int ina226_get_value(struct ina2xx_data *data, u8 reg)
  135. {
  136. /*
  137. * calculate exact value for the given register
  138. * we assume default power-on reset settings:
  139. * bus voltage range 32V
  140. * gain = /8
  141. * adc 1 & 2 -> conversion time 532uS
  142. * mode is continuous shunt and bus
  143. * calibration value is INA226_CALIBRATION_VALUE
  144. */
  145. int val = data->regs[reg];
  146. switch (reg) {
  147. case INA2XX_SHUNT_VOLTAGE:
  148. /* LSB=2.5uV. Convert to mV. */
  149. val = DIV_ROUND_CLOSEST(val, 400);
  150. break;
  151. case INA2XX_BUS_VOLTAGE:
  152. /* LSB=1.25mV. Convert to mV. */
  153. val = val + DIV_ROUND_CLOSEST(val, 4);
  154. break;
  155. case INA2XX_POWER:
  156. /* LSB=25mW. Convert to uW */
  157. val = val * 25 * 1000;
  158. break;
  159. case INA2XX_CURRENT:
  160. /* LSB=1mA (selected). Is in mA */
  161. break;
  162. default:
  163. /* programmer goofed */
  164. WARN_ON_ONCE(1);
  165. val = 0;
  166. break;
  167. }
  168. return val;
  169. }
  170. static ssize_t ina2xx_show_value(struct device *dev,
  171. struct device_attribute *da, char *buf)
  172. {
  173. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  174. struct ina2xx_data *data = ina2xx_update_device(dev);
  175. int value = 0;
  176. if (IS_ERR(data))
  177. return PTR_ERR(data);
  178. switch (data->kind) {
  179. case ina219:
  180. value = ina219_get_value(data, attr->index);
  181. break;
  182. case ina226:
  183. value = ina226_get_value(data, attr->index);
  184. break;
  185. default:
  186. WARN_ON_ONCE(1);
  187. break;
  188. }
  189. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  190. }
  191. /* shunt voltage */
  192. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, \
  193. ina2xx_show_value, NULL, INA2XX_SHUNT_VOLTAGE);
  194. /* bus voltage */
  195. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, \
  196. ina2xx_show_value, NULL, INA2XX_BUS_VOLTAGE);
  197. /* calculated current */
  198. static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, \
  199. ina2xx_show_value, NULL, INA2XX_CURRENT);
  200. /* calculated power */
  201. static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, \
  202. ina2xx_show_value, NULL, INA2XX_POWER);
  203. /* pointers to created device attributes */
  204. static struct attribute *ina2xx_attributes[] = {
  205. &sensor_dev_attr_in0_input.dev_attr.attr,
  206. &sensor_dev_attr_in1_input.dev_attr.attr,
  207. &sensor_dev_attr_curr1_input.dev_attr.attr,
  208. &sensor_dev_attr_power1_input.dev_attr.attr,
  209. NULL,
  210. };
  211. static const struct attribute_group ina2xx_group = {
  212. .attrs = ina2xx_attributes,
  213. };
  214. static int ina2xx_probe(struct i2c_client *client,
  215. const struct i2c_device_id *id)
  216. {
  217. struct i2c_adapter *adapter = client->adapter;
  218. struct ina2xx_data *data;
  219. struct ina2xx_platform_data *pdata;
  220. int ret = 0;
  221. long shunt = 10000; /* default shunt value 10mOhms */
  222. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
  223. return -ENODEV;
  224. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  225. if (!data)
  226. return -ENOMEM;
  227. if (client->dev.platform_data) {
  228. pdata =
  229. (struct ina2xx_platform_data *)client->dev.platform_data;
  230. shunt = pdata->shunt_uohms;
  231. }
  232. if (shunt <= 0)
  233. return -ENODEV;
  234. /* set the device type */
  235. data->kind = id->driver_data;
  236. switch (data->kind) {
  237. case ina219:
  238. /* device configuration */
  239. ina2xx_write_word(client, INA2XX_CONFIG, INA219_CONFIG_DEFAULT);
  240. /* set current LSB to 1mA, shunt is in uOhms */
  241. /* (equation 13 in datasheet) */
  242. ina2xx_write_word(client, INA2XX_CALIBRATION, 40960000 / shunt);
  243. dev_info(&client->dev,
  244. "power monitor INA219 (Rshunt = %li uOhm)\n", shunt);
  245. data->registers = INA219_REGISTERS;
  246. break;
  247. case ina226:
  248. /* device configuration */
  249. ina2xx_write_word(client, INA2XX_CONFIG, INA226_CONFIG_DEFAULT);
  250. /* set current LSB to 1mA, shunt is in uOhms */
  251. /* (equation 1 in datasheet)*/
  252. ina2xx_write_word(client, INA2XX_CALIBRATION, 5120000 / shunt);
  253. dev_info(&client->dev,
  254. "power monitor INA226 (Rshunt = %li uOhm)\n", shunt);
  255. data->registers = INA226_REGISTERS;
  256. break;
  257. default:
  258. /* unknown device id */
  259. return -ENODEV;
  260. }
  261. i2c_set_clientdata(client, data);
  262. mutex_init(&data->update_lock);
  263. ret = sysfs_create_group(&client->dev.kobj, &ina2xx_group);
  264. if (ret)
  265. return ret;
  266. data->hwmon_dev = hwmon_device_register(&client->dev);
  267. if (IS_ERR(data->hwmon_dev)) {
  268. ret = PTR_ERR(data->hwmon_dev);
  269. goto out_err_hwmon;
  270. }
  271. return 0;
  272. out_err_hwmon:
  273. sysfs_remove_group(&client->dev.kobj, &ina2xx_group);
  274. return ret;
  275. }
  276. static int ina2xx_remove(struct i2c_client *client)
  277. {
  278. struct ina2xx_data *data = i2c_get_clientdata(client);
  279. hwmon_device_unregister(data->hwmon_dev);
  280. sysfs_remove_group(&client->dev.kobj, &ina2xx_group);
  281. return 0;
  282. }
  283. static const struct i2c_device_id ina2xx_id[] = {
  284. { "ina219", ina219 },
  285. { "ina226", ina226 },
  286. { }
  287. };
  288. MODULE_DEVICE_TABLE(i2c, ina2xx_id);
  289. static struct i2c_driver ina2xx_driver = {
  290. .driver = {
  291. .name = "ina2xx",
  292. },
  293. .probe = ina2xx_probe,
  294. .remove = ina2xx_remove,
  295. .id_table = ina2xx_id,
  296. };
  297. static int __init ina2xx_init(void)
  298. {
  299. return i2c_add_driver(&ina2xx_driver);
  300. }
  301. static void __exit ina2xx_exit(void)
  302. {
  303. i2c_del_driver(&ina2xx_driver);
  304. }
  305. MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
  306. MODULE_DESCRIPTION("ina2xx driver");
  307. MODULE_LICENSE("GPL");
  308. module_init(ina2xx_init);
  309. module_exit(ina2xx_exit);