ina2xx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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/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_factor;
  82. int registers;
  83. int shunt_div;
  84. int bus_voltage_shift;
  85. int bus_voltage_lsb; /* uV */
  86. int power_lsb; /* uW */
  87. };
  88. struct ina2xx_data {
  89. const struct ina2xx_config *config;
  90. long rshunt;
  91. struct mutex config_lock;
  92. struct regmap *regmap;
  93. const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS];
  94. };
  95. static const struct ina2xx_config ina2xx_config[] = {
  96. [ina219] = {
  97. .config_default = INA219_CONFIG_DEFAULT,
  98. .calibration_factor = 40960000,
  99. .registers = INA219_REGISTERS,
  100. .shunt_div = 100,
  101. .bus_voltage_shift = 3,
  102. .bus_voltage_lsb = 4000,
  103. .power_lsb = 20000,
  104. },
  105. [ina226] = {
  106. .config_default = INA226_CONFIG_DEFAULT,
  107. .calibration_factor = 5120000,
  108. .registers = INA226_REGISTERS,
  109. .shunt_div = 400,
  110. .bus_voltage_shift = 0,
  111. .bus_voltage_lsb = 1250,
  112. .power_lsb = 25000,
  113. },
  114. };
  115. /*
  116. * Available averaging rates for ina226. The indices correspond with
  117. * the bit values expected by the chip (according to the ina226 datasheet,
  118. * table 3 AVG bit settings, found at
  119. * http://www.ti.com/lit/ds/symlink/ina226.pdf.
  120. */
  121. static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
  122. static int ina226_reg_to_interval(u16 config)
  123. {
  124. int avg = ina226_avg_tab[INA226_READ_AVG(config)];
  125. /*
  126. * Multiply the total conversion time by the number of averages.
  127. * Return the result in milliseconds.
  128. */
  129. return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000);
  130. }
  131. /*
  132. * Return the new, shifted AVG field value of CONFIG register,
  133. * to use with regmap_update_bits
  134. */
  135. static u16 ina226_interval_to_reg(int interval)
  136. {
  137. int avg, avg_bits;
  138. avg = DIV_ROUND_CLOSEST(interval * 1000,
  139. INA226_TOTAL_CONV_TIME_DEFAULT);
  140. avg_bits = find_closest(avg, ina226_avg_tab,
  141. ARRAY_SIZE(ina226_avg_tab));
  142. return INA226_SHIFT_AVG(avg_bits);
  143. }
  144. static int ina2xx_calibrate(struct ina2xx_data *data)
  145. {
  146. u16 val = DIV_ROUND_CLOSEST(data->config->calibration_factor,
  147. data->rshunt);
  148. return regmap_write(data->regmap, INA2XX_CALIBRATION, val);
  149. }
  150. /*
  151. * Initialize the configuration and calibration registers.
  152. */
  153. static int ina2xx_init(struct ina2xx_data *data)
  154. {
  155. int ret = regmap_write(data->regmap, INA2XX_CONFIG,
  156. data->config->config_default);
  157. if (ret < 0)
  158. return ret;
  159. /*
  160. * Set current LSB to 1mA, shunt is in uOhms
  161. * (equation 13 in datasheet).
  162. */
  163. return ina2xx_calibrate(data);
  164. }
  165. static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval)
  166. {
  167. struct ina2xx_data *data = dev_get_drvdata(dev);
  168. int ret, retry;
  169. dev_dbg(dev, "Starting register %d read\n", reg);
  170. for (retry = 5; retry; retry--) {
  171. ret = regmap_read(data->regmap, reg, regval);
  172. if (ret < 0)
  173. return ret;
  174. dev_dbg(dev, "read %d, val = 0x%04x\n", reg, *regval);
  175. /*
  176. * If the current value in the calibration register is 0, the
  177. * power and current registers will also remain at 0. In case
  178. * the chip has been reset let's check the calibration
  179. * register and reinitialize if needed.
  180. * We do that extra read of the calibration register if there
  181. * is some hint of a chip reset.
  182. */
  183. if (*regval == 0) {
  184. unsigned int cal;
  185. ret = regmap_read(data->regmap, INA2XX_CALIBRATION,
  186. &cal);
  187. if (ret < 0)
  188. return ret;
  189. if (cal == 0) {
  190. dev_warn(dev, "chip not calibrated, reinitializing\n");
  191. ret = ina2xx_init(data);
  192. if (ret < 0)
  193. return ret;
  194. /*
  195. * Let's make sure the power and current
  196. * registers have been updated before trying
  197. * again.
  198. */
  199. msleep(INA2XX_MAX_DELAY);
  200. continue;
  201. }
  202. }
  203. return 0;
  204. }
  205. /*
  206. * If we're here then although all write operations succeeded, the
  207. * chip still returns 0 in the calibration register. Nothing more we
  208. * can do here.
  209. */
  210. dev_err(dev, "unable to reinitialize the chip\n");
  211. return -ENODEV;
  212. }
  213. static int ina2xx_get_value(struct ina2xx_data *data, u8 reg,
  214. unsigned int regval)
  215. {
  216. int val;
  217. switch (reg) {
  218. case INA2XX_SHUNT_VOLTAGE:
  219. /* signed register */
  220. val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div);
  221. break;
  222. case INA2XX_BUS_VOLTAGE:
  223. val = (regval >> data->config->bus_voltage_shift)
  224. * data->config->bus_voltage_lsb;
  225. val = DIV_ROUND_CLOSEST(val, 1000);
  226. break;
  227. case INA2XX_POWER:
  228. val = regval * data->config->power_lsb;
  229. break;
  230. case INA2XX_CURRENT:
  231. /* signed register, LSB=1mA (selected), in mA */
  232. val = (s16)regval;
  233. break;
  234. case INA2XX_CALIBRATION:
  235. val = DIV_ROUND_CLOSEST(data->config->calibration_factor,
  236. regval);
  237. break;
  238. default:
  239. /* programmer goofed */
  240. WARN_ON_ONCE(1);
  241. val = 0;
  242. break;
  243. }
  244. return val;
  245. }
  246. static ssize_t ina2xx_show_value(struct device *dev,
  247. struct device_attribute *da, char *buf)
  248. {
  249. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  250. struct ina2xx_data *data = dev_get_drvdata(dev);
  251. unsigned int regval;
  252. int err = ina2xx_read_reg(dev, attr->index, &regval);
  253. if (err < 0)
  254. return err;
  255. return snprintf(buf, PAGE_SIZE, "%d\n",
  256. ina2xx_get_value(data, attr->index, regval));
  257. }
  258. static ssize_t ina2xx_set_shunt(struct device *dev,
  259. struct device_attribute *da,
  260. const char *buf, size_t count)
  261. {
  262. unsigned long val;
  263. int status;
  264. struct ina2xx_data *data = dev_get_drvdata(dev);
  265. status = kstrtoul(buf, 10, &val);
  266. if (status < 0)
  267. return status;
  268. if (val == 0 ||
  269. /* Values greater than the calibration factor make no sense. */
  270. val > data->config->calibration_factor)
  271. return -EINVAL;
  272. mutex_lock(&data->config_lock);
  273. data->rshunt = val;
  274. status = ina2xx_calibrate(data);
  275. mutex_unlock(&data->config_lock);
  276. if (status < 0)
  277. return status;
  278. return count;
  279. }
  280. static ssize_t ina226_set_interval(struct device *dev,
  281. struct device_attribute *da,
  282. const char *buf, size_t count)
  283. {
  284. struct ina2xx_data *data = dev_get_drvdata(dev);
  285. unsigned long val;
  286. int status;
  287. status = kstrtoul(buf, 10, &val);
  288. if (status < 0)
  289. return status;
  290. if (val > INT_MAX || val == 0)
  291. return -EINVAL;
  292. status = regmap_update_bits(data->regmap, INA2XX_CONFIG,
  293. INA226_AVG_RD_MASK,
  294. ina226_interval_to_reg(val));
  295. if (status < 0)
  296. return status;
  297. return count;
  298. }
  299. static ssize_t ina226_show_interval(struct device *dev,
  300. struct device_attribute *da, char *buf)
  301. {
  302. struct ina2xx_data *data = dev_get_drvdata(dev);
  303. int status;
  304. unsigned int regval;
  305. status = regmap_read(data->regmap, INA2XX_CONFIG, &regval);
  306. if (status)
  307. return status;
  308. return snprintf(buf, PAGE_SIZE, "%d\n", ina226_reg_to_interval(regval));
  309. }
  310. /* shunt voltage */
  311. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina2xx_show_value, NULL,
  312. INA2XX_SHUNT_VOLTAGE);
  313. /* bus voltage */
  314. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina2xx_show_value, NULL,
  315. INA2XX_BUS_VOLTAGE);
  316. /* calculated current */
  317. static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina2xx_show_value, NULL,
  318. INA2XX_CURRENT);
  319. /* calculated power */
  320. static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina2xx_show_value, NULL,
  321. INA2XX_POWER);
  322. /* shunt resistance */
  323. static SENSOR_DEVICE_ATTR(shunt_resistor, S_IRUGO | S_IWUSR,
  324. ina2xx_show_value, ina2xx_set_shunt,
  325. INA2XX_CALIBRATION);
  326. /* update interval (ina226 only) */
  327. static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR,
  328. ina226_show_interval, ina226_set_interval, 0);
  329. /* pointers to created device attributes */
  330. static struct attribute *ina2xx_attrs[] = {
  331. &sensor_dev_attr_in0_input.dev_attr.attr,
  332. &sensor_dev_attr_in1_input.dev_attr.attr,
  333. &sensor_dev_attr_curr1_input.dev_attr.attr,
  334. &sensor_dev_attr_power1_input.dev_attr.attr,
  335. &sensor_dev_attr_shunt_resistor.dev_attr.attr,
  336. NULL,
  337. };
  338. static const struct attribute_group ina2xx_group = {
  339. .attrs = ina2xx_attrs,
  340. };
  341. static struct attribute *ina226_attrs[] = {
  342. &sensor_dev_attr_update_interval.dev_attr.attr,
  343. NULL,
  344. };
  345. static const struct attribute_group ina226_group = {
  346. .attrs = ina226_attrs,
  347. };
  348. static int ina2xx_probe(struct i2c_client *client,
  349. const struct i2c_device_id *id)
  350. {
  351. struct device *dev = &client->dev;
  352. struct ina2xx_data *data;
  353. struct device *hwmon_dev;
  354. u32 val;
  355. int ret, group = 0;
  356. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  357. if (!data)
  358. return -ENOMEM;
  359. /* set the device type */
  360. data->config = &ina2xx_config[id->driver_data];
  361. if (of_property_read_u32(dev->of_node, "shunt-resistor", &val) < 0) {
  362. struct ina2xx_platform_data *pdata = dev_get_platdata(dev);
  363. if (pdata)
  364. val = pdata->shunt_uohms;
  365. else
  366. val = INA2XX_RSHUNT_DEFAULT;
  367. }
  368. if (val <= 0 || val > data->config->calibration_factor)
  369. return -ENODEV;
  370. data->rshunt = val;
  371. ina2xx_regmap_config.max_register = data->config->registers;
  372. data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
  373. if (IS_ERR(data->regmap)) {
  374. dev_err(dev, "failed to allocate register map\n");
  375. return PTR_ERR(data->regmap);
  376. }
  377. ret = ina2xx_init(data);
  378. if (ret < 0) {
  379. dev_err(dev, "error configuring the device: %d\n", ret);
  380. return -ENODEV;
  381. }
  382. mutex_init(&data->config_lock);
  383. data->groups[group++] = &ina2xx_group;
  384. if (id->driver_data == ina226)
  385. data->groups[group++] = &ina226_group;
  386. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  387. data, data->groups);
  388. if (IS_ERR(hwmon_dev))
  389. return PTR_ERR(hwmon_dev);
  390. dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
  391. id->name, data->rshunt);
  392. return 0;
  393. }
  394. static const struct i2c_device_id ina2xx_id[] = {
  395. { "ina219", ina219 },
  396. { "ina220", ina219 },
  397. { "ina226", ina226 },
  398. { "ina230", ina226 },
  399. { "ina231", ina226 },
  400. { }
  401. };
  402. MODULE_DEVICE_TABLE(i2c, ina2xx_id);
  403. static struct i2c_driver ina2xx_driver = {
  404. .driver = {
  405. .name = "ina2xx",
  406. },
  407. .probe = ina2xx_probe,
  408. .id_table = ina2xx_id,
  409. };
  410. module_i2c_driver(ina2xx_driver);
  411. MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
  412. MODULE_DESCRIPTION("ina2xx driver");
  413. MODULE_LICENSE("GPL");