ina2xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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 <lothar.felten@gmail.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/delay.h>
  38. #include <linux/util_macros.h>
  39. #include <linux/regmap.h>
  40. #include <linux/platform_data/ina2xx.h>
  41. /* common register definitions */
  42. #define INA2XX_CONFIG 0x00
  43. #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
  44. #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
  45. #define INA2XX_POWER 0x03 /* readonly */
  46. #define INA2XX_CURRENT 0x04 /* readonly */
  47. #define INA2XX_CALIBRATION 0x05
  48. /* INA226 register definitions */
  49. #define INA226_MASK_ENABLE 0x06
  50. #define INA226_ALERT_LIMIT 0x07
  51. #define INA226_DIE_ID 0xFF
  52. /* register count */
  53. #define INA219_REGISTERS 6
  54. #define INA226_REGISTERS 8
  55. #define INA2XX_MAX_REGISTERS 8
  56. /* settings - depend on use case */
  57. #define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
  58. #define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
  59. /* worst case is 68.10 ms (~14.6Hz, ina219) */
  60. #define INA2XX_CONVERSION_RATE 15
  61. #define INA2XX_MAX_DELAY 69 /* worst case delay in ms */
  62. #define INA2XX_RSHUNT_DEFAULT 10000
  63. /* bit mask for reading the averaging setting in the configuration register */
  64. #define INA226_AVG_RD_MASK 0x0E00
  65. #define INA226_READ_AVG(reg) (((reg) & INA226_AVG_RD_MASK) >> 9)
  66. #define INA226_SHIFT_AVG(val) ((val) << 9)
  67. /* common attrs, ina226 attrs and NULL */
  68. #define INA2XX_MAX_ATTRIBUTE_GROUPS 3
  69. /*
  70. * Both bus voltage and shunt voltage conversion times for ina226 are set
  71. * to 0b0100 on POR, which translates to 2200 microseconds in total.
  72. */
  73. #define INA226_TOTAL_CONV_TIME_DEFAULT 2200
  74. static struct regmap_config ina2xx_regmap_config = {
  75. .reg_bits = 8,
  76. .val_bits = 16,
  77. };
  78. enum ina2xx_ids { ina219, ina226 };
  79. struct ina2xx_config {
  80. u16 config_default;
  81. int calibration_value;
  82. int registers;
  83. int shunt_div;
  84. int bus_voltage_shift;
  85. int bus_voltage_lsb; /* uV */
  86. int power_lsb_factor;
  87. };
  88. struct ina2xx_data {
  89. const struct ina2xx_config *config;
  90. long rshunt;
  91. long current_lsb_uA;
  92. long power_lsb_uW;
  93. struct mutex config_lock;
  94. struct regmap *regmap;
  95. const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS];
  96. };
  97. static const struct ina2xx_config ina2xx_config[] = {
  98. [ina219] = {
  99. .config_default = INA219_CONFIG_DEFAULT,
  100. .calibration_value = 4096,
  101. .registers = INA219_REGISTERS,
  102. .shunt_div = 100,
  103. .bus_voltage_shift = 3,
  104. .bus_voltage_lsb = 4000,
  105. .power_lsb_factor = 20,
  106. },
  107. [ina226] = {
  108. .config_default = INA226_CONFIG_DEFAULT,
  109. .calibration_value = 2048,
  110. .registers = INA226_REGISTERS,
  111. .shunt_div = 400,
  112. .bus_voltage_shift = 0,
  113. .bus_voltage_lsb = 1250,
  114. .power_lsb_factor = 25,
  115. },
  116. };
  117. /*
  118. * Available averaging rates for ina226. The indices correspond with
  119. * the bit values expected by the chip (according to the ina226 datasheet,
  120. * table 3 AVG bit settings, found at
  121. * http://www.ti.com/lit/ds/symlink/ina226.pdf.
  122. */
  123. static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
  124. static int ina226_reg_to_interval(u16 config)
  125. {
  126. int avg = ina226_avg_tab[INA226_READ_AVG(config)];
  127. /*
  128. * Multiply the total conversion time by the number of averages.
  129. * Return the result in milliseconds.
  130. */
  131. return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000);
  132. }
  133. /*
  134. * Return the new, shifted AVG field value of CONFIG register,
  135. * to use with regmap_update_bits
  136. */
  137. static u16 ina226_interval_to_reg(int interval)
  138. {
  139. int avg, avg_bits;
  140. avg = DIV_ROUND_CLOSEST(interval * 1000,
  141. INA226_TOTAL_CONV_TIME_DEFAULT);
  142. avg_bits = find_closest(avg, ina226_avg_tab,
  143. ARRAY_SIZE(ina226_avg_tab));
  144. return INA226_SHIFT_AVG(avg_bits);
  145. }
  146. /*
  147. * Calibration register is set to the best value, which eliminates
  148. * truncation errors on calculating current register in hardware.
  149. * According to datasheet (eq. 3) the best values are 2048 for
  150. * ina226 and 4096 for ina219. They are hardcoded as calibration_value.
  151. */
  152. static int ina2xx_calibrate(struct ina2xx_data *data)
  153. {
  154. return regmap_write(data->regmap, INA2XX_CALIBRATION,
  155. data->config->calibration_value);
  156. }
  157. /*
  158. * Initialize the configuration and calibration registers.
  159. */
  160. static int ina2xx_init(struct ina2xx_data *data)
  161. {
  162. int ret = regmap_write(data->regmap, INA2XX_CONFIG,
  163. data->config->config_default);
  164. if (ret < 0)
  165. return ret;
  166. return ina2xx_calibrate(data);
  167. }
  168. static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval)
  169. {
  170. struct ina2xx_data *data = dev_get_drvdata(dev);
  171. int ret, retry;
  172. dev_dbg(dev, "Starting register %d read\n", reg);
  173. for (retry = 5; retry; retry--) {
  174. ret = regmap_read(data->regmap, reg, regval);
  175. if (ret < 0)
  176. return ret;
  177. dev_dbg(dev, "read %d, val = 0x%04x\n", reg, *regval);
  178. /*
  179. * If the current value in the calibration register is 0, the
  180. * power and current registers will also remain at 0. In case
  181. * the chip has been reset let's check the calibration
  182. * register and reinitialize if needed.
  183. * We do that extra read of the calibration register if there
  184. * is some hint of a chip reset.
  185. */
  186. if (*regval == 0) {
  187. unsigned int cal;
  188. ret = regmap_read(data->regmap, INA2XX_CALIBRATION,
  189. &cal);
  190. if (ret < 0)
  191. return ret;
  192. if (cal == 0) {
  193. dev_warn(dev, "chip not calibrated, reinitializing\n");
  194. ret = ina2xx_init(data);
  195. if (ret < 0)
  196. return ret;
  197. /*
  198. * Let's make sure the power and current
  199. * registers have been updated before trying
  200. * again.
  201. */
  202. msleep(INA2XX_MAX_DELAY);
  203. continue;
  204. }
  205. }
  206. return 0;
  207. }
  208. /*
  209. * If we're here then although all write operations succeeded, the
  210. * chip still returns 0 in the calibration register. Nothing more we
  211. * can do here.
  212. */
  213. dev_err(dev, "unable to reinitialize the chip\n");
  214. return -ENODEV;
  215. }
  216. static int ina2xx_get_value(struct ina2xx_data *data, u8 reg,
  217. unsigned int regval)
  218. {
  219. int val;
  220. switch (reg) {
  221. case INA2XX_SHUNT_VOLTAGE:
  222. /* signed register */
  223. val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div);
  224. break;
  225. case INA2XX_BUS_VOLTAGE:
  226. val = (regval >> data->config->bus_voltage_shift)
  227. * data->config->bus_voltage_lsb;
  228. val = DIV_ROUND_CLOSEST(val, 1000);
  229. break;
  230. case INA2XX_POWER:
  231. val = regval * data->power_lsb_uW;
  232. break;
  233. case INA2XX_CURRENT:
  234. /* signed register, result in mA */
  235. val = (s16)regval * data->current_lsb_uA;
  236. val = DIV_ROUND_CLOSEST(val, 1000);
  237. break;
  238. case INA2XX_CALIBRATION:
  239. val = regval;
  240. break;
  241. default:
  242. /* programmer goofed */
  243. WARN_ON_ONCE(1);
  244. val = 0;
  245. break;
  246. }
  247. return val;
  248. }
  249. static ssize_t ina2xx_show_value(struct device *dev,
  250. struct device_attribute *da, char *buf)
  251. {
  252. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  253. struct ina2xx_data *data = dev_get_drvdata(dev);
  254. unsigned int regval;
  255. int err = ina2xx_read_reg(dev, attr->index, &regval);
  256. if (err < 0)
  257. return err;
  258. return snprintf(buf, PAGE_SIZE, "%d\n",
  259. ina2xx_get_value(data, attr->index, regval));
  260. }
  261. /*
  262. * In order to keep calibration register value fixed, the product
  263. * of current_lsb and shunt_resistor should also be fixed and equal
  264. * to shunt_voltage_lsb = 1 / shunt_div multiplied by 10^9 in order
  265. * to keep the scale.
  266. */
  267. static int ina2xx_set_shunt(struct ina2xx_data *data, long val)
  268. {
  269. unsigned int dividend = DIV_ROUND_CLOSEST(1000000000,
  270. data->config->shunt_div);
  271. if (val <= 0 || val > dividend)
  272. return -EINVAL;
  273. mutex_lock(&data->config_lock);
  274. data->rshunt = val;
  275. data->current_lsb_uA = DIV_ROUND_CLOSEST(dividend, val);
  276. data->power_lsb_uW = data->config->power_lsb_factor *
  277. data->current_lsb_uA;
  278. mutex_unlock(&data->config_lock);
  279. return 0;
  280. }
  281. static ssize_t ina2xx_show_shunt(struct device *dev,
  282. struct device_attribute *da,
  283. char *buf)
  284. {
  285. struct ina2xx_data *data = dev_get_drvdata(dev);
  286. return snprintf(buf, PAGE_SIZE, "%li\n", data->rshunt);
  287. }
  288. static ssize_t ina2xx_store_shunt(struct device *dev,
  289. struct device_attribute *da,
  290. const char *buf, size_t count)
  291. {
  292. unsigned long val;
  293. int status;
  294. struct ina2xx_data *data = dev_get_drvdata(dev);
  295. status = kstrtoul(buf, 10, &val);
  296. if (status < 0)
  297. return status;
  298. status = ina2xx_set_shunt(data, val);
  299. if (status < 0)
  300. return status;
  301. return count;
  302. }
  303. static ssize_t ina226_set_interval(struct device *dev,
  304. struct device_attribute *da,
  305. const char *buf, size_t count)
  306. {
  307. struct ina2xx_data *data = dev_get_drvdata(dev);
  308. unsigned long val;
  309. int status;
  310. status = kstrtoul(buf, 10, &val);
  311. if (status < 0)
  312. return status;
  313. if (val > INT_MAX || val == 0)
  314. return -EINVAL;
  315. status = regmap_update_bits(data->regmap, INA2XX_CONFIG,
  316. INA226_AVG_RD_MASK,
  317. ina226_interval_to_reg(val));
  318. if (status < 0)
  319. return status;
  320. return count;
  321. }
  322. static ssize_t ina226_show_interval(struct device *dev,
  323. struct device_attribute *da, char *buf)
  324. {
  325. struct ina2xx_data *data = dev_get_drvdata(dev);
  326. int status;
  327. unsigned int regval;
  328. status = regmap_read(data->regmap, INA2XX_CONFIG, &regval);
  329. if (status)
  330. return status;
  331. return snprintf(buf, PAGE_SIZE, "%d\n", ina226_reg_to_interval(regval));
  332. }
  333. /* shunt voltage */
  334. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina2xx_show_value, NULL,
  335. INA2XX_SHUNT_VOLTAGE);
  336. /* bus voltage */
  337. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina2xx_show_value, NULL,
  338. INA2XX_BUS_VOLTAGE);
  339. /* calculated current */
  340. static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina2xx_show_value, NULL,
  341. INA2XX_CURRENT);
  342. /* calculated power */
  343. static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina2xx_show_value, NULL,
  344. INA2XX_POWER);
  345. /* shunt resistance */
  346. static SENSOR_DEVICE_ATTR(shunt_resistor, S_IRUGO | S_IWUSR,
  347. ina2xx_show_shunt, ina2xx_store_shunt,
  348. INA2XX_CALIBRATION);
  349. /* update interval (ina226 only) */
  350. static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR,
  351. ina226_show_interval, ina226_set_interval, 0);
  352. /* pointers to created device attributes */
  353. static struct attribute *ina2xx_attrs[] = {
  354. &sensor_dev_attr_in0_input.dev_attr.attr,
  355. &sensor_dev_attr_in1_input.dev_attr.attr,
  356. &sensor_dev_attr_curr1_input.dev_attr.attr,
  357. &sensor_dev_attr_power1_input.dev_attr.attr,
  358. &sensor_dev_attr_shunt_resistor.dev_attr.attr,
  359. NULL,
  360. };
  361. static const struct attribute_group ina2xx_group = {
  362. .attrs = ina2xx_attrs,
  363. };
  364. static struct attribute *ina226_attrs[] = {
  365. &sensor_dev_attr_update_interval.dev_attr.attr,
  366. NULL,
  367. };
  368. static const struct attribute_group ina226_group = {
  369. .attrs = ina226_attrs,
  370. };
  371. static int ina2xx_probe(struct i2c_client *client,
  372. const struct i2c_device_id *id)
  373. {
  374. struct device *dev = &client->dev;
  375. struct ina2xx_data *data;
  376. struct device *hwmon_dev;
  377. u32 val;
  378. int ret, group = 0;
  379. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  380. if (!data)
  381. return -ENOMEM;
  382. /* set the device type */
  383. data->config = &ina2xx_config[id->driver_data];
  384. mutex_init(&data->config_lock);
  385. if (of_property_read_u32(dev->of_node, "shunt-resistor", &val) < 0) {
  386. struct ina2xx_platform_data *pdata = dev_get_platdata(dev);
  387. if (pdata)
  388. val = pdata->shunt_uohms;
  389. else
  390. val = INA2XX_RSHUNT_DEFAULT;
  391. }
  392. ina2xx_set_shunt(data, val);
  393. ina2xx_regmap_config.max_register = data->config->registers;
  394. data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
  395. if (IS_ERR(data->regmap)) {
  396. dev_err(dev, "failed to allocate register map\n");
  397. return PTR_ERR(data->regmap);
  398. }
  399. ret = ina2xx_init(data);
  400. if (ret < 0) {
  401. dev_err(dev, "error configuring the device: %d\n", ret);
  402. return -ENODEV;
  403. }
  404. data->groups[group++] = &ina2xx_group;
  405. if (id->driver_data == ina226)
  406. data->groups[group++] = &ina226_group;
  407. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  408. data, data->groups);
  409. if (IS_ERR(hwmon_dev))
  410. return PTR_ERR(hwmon_dev);
  411. dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
  412. id->name, data->rshunt);
  413. return 0;
  414. }
  415. static const struct i2c_device_id ina2xx_id[] = {
  416. { "ina219", ina219 },
  417. { "ina220", ina219 },
  418. { "ina226", ina226 },
  419. { "ina230", ina226 },
  420. { "ina231", ina226 },
  421. { }
  422. };
  423. MODULE_DEVICE_TABLE(i2c, ina2xx_id);
  424. static struct i2c_driver ina2xx_driver = {
  425. .driver = {
  426. .name = "ina2xx",
  427. },
  428. .probe = ina2xx_probe,
  429. .id_table = ina2xx_id,
  430. };
  431. module_i2c_driver(ina2xx_driver);
  432. MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
  433. MODULE_DESCRIPTION("ina2xx driver");
  434. MODULE_LICENSE("GPL");