ina2xx.c 13 KB

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