ina2xx.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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/of.h>
  37. #include <linux/platform_data/ina2xx.h>
  38. /* common register definitions */
  39. #define INA2XX_CONFIG 0x00
  40. #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
  41. #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
  42. #define INA2XX_POWER 0x03 /* readonly */
  43. #define INA2XX_CURRENT 0x04 /* readonly */
  44. #define INA2XX_CALIBRATION 0x05
  45. /* INA226 register definitions */
  46. #define INA226_MASK_ENABLE 0x06
  47. #define INA226_ALERT_LIMIT 0x07
  48. #define INA226_DIE_ID 0xFF
  49. /* register count */
  50. #define INA219_REGISTERS 6
  51. #define INA226_REGISTERS 8
  52. #define INA2XX_MAX_REGISTERS 8
  53. /* settings - depend on use case */
  54. #define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
  55. #define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
  56. /* worst case is 68.10 ms (~14.6Hz, ina219) */
  57. #define INA2XX_CONVERSION_RATE 15
  58. enum ina2xx_ids { ina219, ina226 };
  59. struct ina2xx_config {
  60. u16 config_default;
  61. int calibration_factor;
  62. int registers;
  63. int shunt_div;
  64. int bus_voltage_shift;
  65. int bus_voltage_lsb; /* uV */
  66. int power_lsb; /* uW */
  67. };
  68. struct ina2xx_data {
  69. struct i2c_client *client;
  70. const struct ina2xx_config *config;
  71. struct mutex update_lock;
  72. bool valid;
  73. unsigned long last_updated;
  74. int kind;
  75. u16 regs[INA2XX_MAX_REGISTERS];
  76. };
  77. static const struct ina2xx_config ina2xx_config[] = {
  78. [ina219] = {
  79. .config_default = INA219_CONFIG_DEFAULT,
  80. .calibration_factor = 40960000,
  81. .registers = INA219_REGISTERS,
  82. .shunt_div = 100,
  83. .bus_voltage_shift = 3,
  84. .bus_voltage_lsb = 4000,
  85. .power_lsb = 20000,
  86. },
  87. [ina226] = {
  88. .config_default = INA226_CONFIG_DEFAULT,
  89. .calibration_factor = 5120000,
  90. .registers = INA226_REGISTERS,
  91. .shunt_div = 400,
  92. .bus_voltage_shift = 0,
  93. .bus_voltage_lsb = 1250,
  94. .power_lsb = 25000,
  95. },
  96. };
  97. static struct ina2xx_data *ina2xx_update_device(struct device *dev)
  98. {
  99. struct ina2xx_data *data = dev_get_drvdata(dev);
  100. struct i2c_client *client = data->client;
  101. struct ina2xx_data *ret = data;
  102. mutex_lock(&data->update_lock);
  103. if (time_after(jiffies, data->last_updated +
  104. HZ / INA2XX_CONVERSION_RATE) || !data->valid) {
  105. int i;
  106. dev_dbg(&client->dev, "Starting ina2xx update\n");
  107. /* Read all registers */
  108. for (i = 0; i < data->config->registers; i++) {
  109. int rv = i2c_smbus_read_word_swapped(client, i);
  110. if (rv < 0) {
  111. ret = ERR_PTR(rv);
  112. goto abort;
  113. }
  114. data->regs[i] = rv;
  115. }
  116. data->last_updated = jiffies;
  117. data->valid = 1;
  118. }
  119. abort:
  120. mutex_unlock(&data->update_lock);
  121. return ret;
  122. }
  123. static int ina2xx_get_value(struct ina2xx_data *data, u8 reg)
  124. {
  125. int val;
  126. switch (reg) {
  127. case INA2XX_SHUNT_VOLTAGE:
  128. /* signed register */
  129. val = DIV_ROUND_CLOSEST((s16)data->regs[reg],
  130. data->config->shunt_div);
  131. break;
  132. case INA2XX_BUS_VOLTAGE:
  133. val = (data->regs[reg] >> data->config->bus_voltage_shift)
  134. * data->config->bus_voltage_lsb;
  135. val = DIV_ROUND_CLOSEST(val, 1000);
  136. break;
  137. case INA2XX_POWER:
  138. val = data->regs[reg] * data->config->power_lsb;
  139. break;
  140. case INA2XX_CURRENT:
  141. /* signed register, LSB=1mA (selected), in mA */
  142. val = (s16)data->regs[reg];
  143. break;
  144. default:
  145. /* programmer goofed */
  146. WARN_ON_ONCE(1);
  147. val = 0;
  148. break;
  149. }
  150. return val;
  151. }
  152. static ssize_t ina2xx_show_value(struct device *dev,
  153. struct device_attribute *da, char *buf)
  154. {
  155. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  156. struct ina2xx_data *data = ina2xx_update_device(dev);
  157. if (IS_ERR(data))
  158. return PTR_ERR(data);
  159. return snprintf(buf, PAGE_SIZE, "%d\n",
  160. ina2xx_get_value(data, attr->index));
  161. }
  162. /* shunt voltage */
  163. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina2xx_show_value, NULL,
  164. INA2XX_SHUNT_VOLTAGE);
  165. /* bus voltage */
  166. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina2xx_show_value, NULL,
  167. INA2XX_BUS_VOLTAGE);
  168. /* calculated current */
  169. static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina2xx_show_value, NULL,
  170. INA2XX_CURRENT);
  171. /* calculated power */
  172. static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina2xx_show_value, NULL,
  173. INA2XX_POWER);
  174. /* pointers to created device attributes */
  175. static struct attribute *ina2xx_attrs[] = {
  176. &sensor_dev_attr_in0_input.dev_attr.attr,
  177. &sensor_dev_attr_in1_input.dev_attr.attr,
  178. &sensor_dev_attr_curr1_input.dev_attr.attr,
  179. &sensor_dev_attr_power1_input.dev_attr.attr,
  180. NULL,
  181. };
  182. ATTRIBUTE_GROUPS(ina2xx);
  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_platform_data *pdata;
  188. struct device *dev = &client->dev;
  189. struct ina2xx_data *data;
  190. struct device *hwmon_dev;
  191. long shunt = 10000; /* default shunt value 10mOhms */
  192. u32 val;
  193. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
  194. return -ENODEV;
  195. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  196. if (!data)
  197. return -ENOMEM;
  198. if (dev_get_platdata(dev)) {
  199. pdata = dev_get_platdata(dev);
  200. shunt = pdata->shunt_uohms;
  201. } else if (!of_property_read_u32(dev->of_node,
  202. "shunt-resistor", &val)) {
  203. shunt = val;
  204. }
  205. if (shunt <= 0)
  206. return -ENODEV;
  207. /* set the device type */
  208. data->kind = id->driver_data;
  209. data->config = &ina2xx_config[data->kind];
  210. /* device configuration */
  211. i2c_smbus_write_word_swapped(client, INA2XX_CONFIG,
  212. data->config->config_default);
  213. /* set current LSB to 1mA, shunt is in uOhms */
  214. /* (equation 13 in datasheet) */
  215. i2c_smbus_write_word_swapped(client, INA2XX_CALIBRATION,
  216. data->config->calibration_factor / shunt);
  217. data->client = client;
  218. mutex_init(&data->update_lock);
  219. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  220. data, ina2xx_groups);
  221. if (IS_ERR(hwmon_dev))
  222. return PTR_ERR(hwmon_dev);
  223. dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
  224. id->name, shunt);
  225. return 0;
  226. }
  227. static const struct i2c_device_id ina2xx_id[] = {
  228. { "ina219", ina219 },
  229. { "ina220", ina219 },
  230. { "ina226", ina226 },
  231. { "ina230", ina226 },
  232. { }
  233. };
  234. MODULE_DEVICE_TABLE(i2c, ina2xx_id);
  235. static struct i2c_driver ina2xx_driver = {
  236. .driver = {
  237. .name = "ina2xx",
  238. },
  239. .probe = ina2xx_probe,
  240. .id_table = ina2xx_id,
  241. };
  242. module_i2c_driver(ina2xx_driver);
  243. MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
  244. MODULE_DESCRIPTION("ina2xx driver");
  245. MODULE_LICENSE("GPL");