ina2xx.c 14 KB

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