ina2xx-adc.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. /*
  2. * INA2XX Current and Power Monitors
  3. *
  4. * Copyright 2015 Baylibre SAS.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Based on linux/drivers/iio/adc/ad7291.c
  11. * Copyright 2010-2011 Analog Devices Inc.
  12. *
  13. * Based on linux/drivers/hwmon/ina2xx.c
  14. * Copyright 2012 Lothar Felten <l-felten@ti.com>
  15. *
  16. * Licensed under the GPL-2 or later.
  17. *
  18. * IIO driver for INA219-220-226-230-231
  19. *
  20. * Configurable 7-bit I2C slave address from 0x40 to 0x4F
  21. */
  22. #include <linux/delay.h>
  23. #include <linux/i2c.h>
  24. #include <linux/iio/iio.h>
  25. #include <linux/iio/buffer.h>
  26. #include <linux/iio/kfifo_buf.h>
  27. #include <linux/iio/sysfs.h>
  28. #include <linux/kthread.h>
  29. #include <linux/module.h>
  30. #include <linux/of_device.h>
  31. #include <linux/regmap.h>
  32. #include <linux/sched/task.h>
  33. #include <linux/util_macros.h>
  34. #include <linux/platform_data/ina2xx.h>
  35. /* INA2XX registers definition */
  36. #define INA2XX_CONFIG 0x00
  37. #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
  38. #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
  39. #define INA2XX_POWER 0x03 /* readonly */
  40. #define INA2XX_CURRENT 0x04 /* readonly */
  41. #define INA2XX_CALIBRATION 0x05
  42. #define INA226_MASK_ENABLE 0x06
  43. #define INA226_CVRF BIT(3)
  44. #define INA2XX_MAX_REGISTERS 8
  45. /* settings - depend on use case */
  46. #define INA219_CONFIG_DEFAULT 0x399F /* PGA=1/8, BRNG=32V */
  47. #define INA219_DEFAULT_IT 532
  48. #define INA219_DEFAULT_BRNG 1 /* 32V */
  49. #define INA219_DEFAULT_PGA 125 /* 1000/8 */
  50. #define INA226_CONFIG_DEFAULT 0x4327
  51. #define INA226_DEFAULT_AVG 4
  52. #define INA226_DEFAULT_IT 1110
  53. #define INA2XX_RSHUNT_DEFAULT 10000
  54. /*
  55. * bit masks for reading the settings in the configuration register
  56. * FIXME: use regmap_fields.
  57. */
  58. #define INA2XX_MODE_MASK GENMASK(3, 0)
  59. /* Gain for VShunt: 1/8 (default), 1/4, 1/2, 1 */
  60. #define INA219_PGA_MASK GENMASK(12, 11)
  61. #define INA219_SHIFT_PGA(val) ((val) << 11)
  62. /* VBus range: 32V (default), 16V */
  63. #define INA219_BRNG_MASK BIT(13)
  64. #define INA219_SHIFT_BRNG(val) ((val) << 13)
  65. /* Averaging for VBus/VShunt/Power */
  66. #define INA226_AVG_MASK GENMASK(11, 9)
  67. #define INA226_SHIFT_AVG(val) ((val) << 9)
  68. /* Integration time for VBus */
  69. #define INA219_ITB_MASK GENMASK(10, 7)
  70. #define INA219_SHIFT_ITB(val) ((val) << 7)
  71. #define INA226_ITB_MASK GENMASK(8, 6)
  72. #define INA226_SHIFT_ITB(val) ((val) << 6)
  73. /* Integration time for VShunt */
  74. #define INA219_ITS_MASK GENMASK(6, 3)
  75. #define INA219_SHIFT_ITS(val) ((val) << 3)
  76. #define INA226_ITS_MASK GENMASK(5, 3)
  77. #define INA226_SHIFT_ITS(val) ((val) << 3)
  78. /* INA219 Bus voltage register, low bits are flags */
  79. #define INA219_OVF BIT(0)
  80. #define INA219_CNVR BIT(1)
  81. #define INA219_BUS_VOLTAGE_SHIFT 3
  82. /* Cosmetic macro giving the sampling period for a full P=UxI cycle */
  83. #define SAMPLING_PERIOD(c) ((c->int_time_vbus + c->int_time_vshunt) \
  84. * c->avg)
  85. static bool ina2xx_is_writeable_reg(struct device *dev, unsigned int reg)
  86. {
  87. return (reg == INA2XX_CONFIG) || (reg > INA2XX_CURRENT);
  88. }
  89. static bool ina2xx_is_volatile_reg(struct device *dev, unsigned int reg)
  90. {
  91. return (reg != INA2XX_CONFIG);
  92. }
  93. static inline bool is_signed_reg(unsigned int reg)
  94. {
  95. return (reg == INA2XX_SHUNT_VOLTAGE) || (reg == INA2XX_CURRENT);
  96. }
  97. static const struct regmap_config ina2xx_regmap_config = {
  98. .reg_bits = 8,
  99. .val_bits = 16,
  100. .max_register = INA2XX_MAX_REGISTERS,
  101. .writeable_reg = ina2xx_is_writeable_reg,
  102. .volatile_reg = ina2xx_is_volatile_reg,
  103. };
  104. enum ina2xx_ids { ina219, ina226 };
  105. struct ina2xx_config {
  106. u16 config_default;
  107. int calibration_value;
  108. int shunt_voltage_lsb; /* nV */
  109. int bus_voltage_shift; /* position of lsb */
  110. int bus_voltage_lsb; /* uV */
  111. /* fixed relation between current and power lsb, uW/uA */
  112. int power_lsb_factor;
  113. enum ina2xx_ids chip_id;
  114. };
  115. struct ina2xx_chip_info {
  116. struct regmap *regmap;
  117. struct task_struct *task;
  118. const struct ina2xx_config *config;
  119. struct mutex state_lock;
  120. unsigned int shunt_resistor_uohm;
  121. int avg;
  122. int int_time_vbus; /* Bus voltage integration time uS */
  123. int int_time_vshunt; /* Shunt voltage integration time uS */
  124. int range_vbus; /* Bus voltage maximum in V */
  125. int pga_gain_vshunt; /* Shunt voltage PGA gain */
  126. bool allow_async_readout;
  127. };
  128. static const struct ina2xx_config ina2xx_config[] = {
  129. [ina219] = {
  130. .config_default = INA219_CONFIG_DEFAULT,
  131. .calibration_value = 4096,
  132. .shunt_voltage_lsb = 10000,
  133. .bus_voltage_shift = INA219_BUS_VOLTAGE_SHIFT,
  134. .bus_voltage_lsb = 4000,
  135. .power_lsb_factor = 20,
  136. .chip_id = ina219,
  137. },
  138. [ina226] = {
  139. .config_default = INA226_CONFIG_DEFAULT,
  140. .calibration_value = 2048,
  141. .shunt_voltage_lsb = 2500,
  142. .bus_voltage_shift = 0,
  143. .bus_voltage_lsb = 1250,
  144. .power_lsb_factor = 25,
  145. .chip_id = ina226,
  146. },
  147. };
  148. static int ina2xx_read_raw(struct iio_dev *indio_dev,
  149. struct iio_chan_spec const *chan,
  150. int *val, int *val2, long mask)
  151. {
  152. int ret;
  153. struct ina2xx_chip_info *chip = iio_priv(indio_dev);
  154. unsigned int regval;
  155. switch (mask) {
  156. case IIO_CHAN_INFO_RAW:
  157. ret = regmap_read(chip->regmap, chan->address, &regval);
  158. if (ret)
  159. return ret;
  160. if (is_signed_reg(chan->address))
  161. *val = (s16) regval;
  162. else
  163. *val = regval;
  164. if (chan->address == INA2XX_BUS_VOLTAGE)
  165. *val >>= chip->config->bus_voltage_shift;
  166. return IIO_VAL_INT;
  167. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  168. *val = chip->avg;
  169. return IIO_VAL_INT;
  170. case IIO_CHAN_INFO_INT_TIME:
  171. *val = 0;
  172. if (chan->address == INA2XX_SHUNT_VOLTAGE)
  173. *val2 = chip->int_time_vshunt;
  174. else
  175. *val2 = chip->int_time_vbus;
  176. return IIO_VAL_INT_PLUS_MICRO;
  177. case IIO_CHAN_INFO_SAMP_FREQ:
  178. /*
  179. * Sample freq is read only, it is a consequence of
  180. * 1/AVG*(CT_bus+CT_shunt).
  181. */
  182. *val = DIV_ROUND_CLOSEST(1000000, SAMPLING_PERIOD(chip));
  183. return IIO_VAL_INT;
  184. case IIO_CHAN_INFO_SCALE:
  185. switch (chan->address) {
  186. case INA2XX_SHUNT_VOLTAGE:
  187. /* processed (mV) = raw * lsb(nV) / 1000000 */
  188. *val = chip->config->shunt_voltage_lsb;
  189. *val2 = 1000000;
  190. return IIO_VAL_FRACTIONAL;
  191. case INA2XX_BUS_VOLTAGE:
  192. /* processed (mV) = raw * lsb (uV) / 1000 */
  193. *val = chip->config->bus_voltage_lsb;
  194. *val2 = 1000;
  195. return IIO_VAL_FRACTIONAL;
  196. case INA2XX_CURRENT:
  197. /*
  198. * processed (mA) = raw * current_lsb (mA)
  199. * current_lsb (mA) = shunt_voltage_lsb (nV) /
  200. * shunt_resistor (uOhm)
  201. */
  202. *val = chip->config->shunt_voltage_lsb;
  203. *val2 = chip->shunt_resistor_uohm;
  204. return IIO_VAL_FRACTIONAL;
  205. case INA2XX_POWER:
  206. /*
  207. * processed (mW) = raw * power_lsb (mW)
  208. * power_lsb (mW) = power_lsb_factor (mW/mA) *
  209. * current_lsb (mA)
  210. */
  211. *val = chip->config->power_lsb_factor *
  212. chip->config->shunt_voltage_lsb;
  213. *val2 = chip->shunt_resistor_uohm;
  214. return IIO_VAL_FRACTIONAL;
  215. }
  216. case IIO_CHAN_INFO_HARDWAREGAIN:
  217. switch (chan->address) {
  218. case INA2XX_SHUNT_VOLTAGE:
  219. *val = chip->pga_gain_vshunt;
  220. *val2 = 1000;
  221. return IIO_VAL_FRACTIONAL;
  222. case INA2XX_BUS_VOLTAGE:
  223. *val = chip->range_vbus == 32 ? 1 : 2;
  224. return IIO_VAL_INT;
  225. }
  226. }
  227. return -EINVAL;
  228. }
  229. /*
  230. * Available averaging rates for ina226. The indices correspond with
  231. * the bit values expected by the chip (according to the ina226 datasheet,
  232. * table 3 AVG bit settings, found at
  233. * http://www.ti.com/lit/ds/symlink/ina226.pdf.
  234. */
  235. static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
  236. static int ina226_set_average(struct ina2xx_chip_info *chip, unsigned int val,
  237. unsigned int *config)
  238. {
  239. int bits;
  240. if (val > 1024 || val < 1)
  241. return -EINVAL;
  242. bits = find_closest(val, ina226_avg_tab,
  243. ARRAY_SIZE(ina226_avg_tab));
  244. chip->avg = ina226_avg_tab[bits];
  245. *config &= ~INA226_AVG_MASK;
  246. *config |= INA226_SHIFT_AVG(bits) & INA226_AVG_MASK;
  247. return 0;
  248. }
  249. /* Conversion times in uS */
  250. static const int ina226_conv_time_tab[] = { 140, 204, 332, 588, 1100,
  251. 2116, 4156, 8244 };
  252. static int ina226_set_int_time_vbus(struct ina2xx_chip_info *chip,
  253. unsigned int val_us, unsigned int *config)
  254. {
  255. int bits;
  256. if (val_us > 8244 || val_us < 140)
  257. return -EINVAL;
  258. bits = find_closest(val_us, ina226_conv_time_tab,
  259. ARRAY_SIZE(ina226_conv_time_tab));
  260. chip->int_time_vbus = ina226_conv_time_tab[bits];
  261. *config &= ~INA226_ITB_MASK;
  262. *config |= INA226_SHIFT_ITB(bits) & INA226_ITB_MASK;
  263. return 0;
  264. }
  265. static int ina226_set_int_time_vshunt(struct ina2xx_chip_info *chip,
  266. unsigned int val_us, unsigned int *config)
  267. {
  268. int bits;
  269. if (val_us > 8244 || val_us < 140)
  270. return -EINVAL;
  271. bits = find_closest(val_us, ina226_conv_time_tab,
  272. ARRAY_SIZE(ina226_conv_time_tab));
  273. chip->int_time_vshunt = ina226_conv_time_tab[bits];
  274. *config &= ~INA226_ITS_MASK;
  275. *config |= INA226_SHIFT_ITS(bits) & INA226_ITS_MASK;
  276. return 0;
  277. }
  278. /* Conversion times in uS. */
  279. static const int ina219_conv_time_tab_subsample[] = { 84, 148, 276, 532 };
  280. static const int ina219_conv_time_tab_average[] = { 532, 1060, 2130, 4260,
  281. 8510, 17020, 34050, 68100};
  282. static int ina219_lookup_int_time(unsigned int *val_us, int *bits)
  283. {
  284. if (*val_us > 68100 || *val_us < 84)
  285. return -EINVAL;
  286. if (*val_us <= 532) {
  287. *bits = find_closest(*val_us, ina219_conv_time_tab_subsample,
  288. ARRAY_SIZE(ina219_conv_time_tab_subsample));
  289. *val_us = ina219_conv_time_tab_subsample[*bits];
  290. } else {
  291. *bits = find_closest(*val_us, ina219_conv_time_tab_average,
  292. ARRAY_SIZE(ina219_conv_time_tab_average));
  293. *val_us = ina219_conv_time_tab_average[*bits];
  294. *bits |= 0x8;
  295. }
  296. return 0;
  297. }
  298. static int ina219_set_int_time_vbus(struct ina2xx_chip_info *chip,
  299. unsigned int val_us, unsigned int *config)
  300. {
  301. int bits, ret;
  302. unsigned int val_us_best = val_us;
  303. ret = ina219_lookup_int_time(&val_us_best, &bits);
  304. if (ret)
  305. return ret;
  306. chip->int_time_vbus = val_us_best;
  307. *config &= ~INA219_ITB_MASK;
  308. *config |= INA219_SHIFT_ITB(bits) & INA219_ITB_MASK;
  309. return 0;
  310. }
  311. static int ina219_set_int_time_vshunt(struct ina2xx_chip_info *chip,
  312. unsigned int val_us, unsigned int *config)
  313. {
  314. int bits, ret;
  315. unsigned int val_us_best = val_us;
  316. ret = ina219_lookup_int_time(&val_us_best, &bits);
  317. if (ret)
  318. return ret;
  319. chip->int_time_vshunt = val_us_best;
  320. *config &= ~INA219_ITS_MASK;
  321. *config |= INA219_SHIFT_ITS(bits) & INA219_ITS_MASK;
  322. return 0;
  323. }
  324. static const int ina219_vbus_range_tab[] = { 1, 2 };
  325. static int ina219_set_vbus_range_denom(struct ina2xx_chip_info *chip,
  326. unsigned int range,
  327. unsigned int *config)
  328. {
  329. if (range == 1)
  330. chip->range_vbus = 32;
  331. else if (range == 2)
  332. chip->range_vbus = 16;
  333. else
  334. return -EINVAL;
  335. *config &= ~INA219_BRNG_MASK;
  336. *config |= INA219_SHIFT_BRNG(range == 1 ? 1 : 0) & INA219_BRNG_MASK;
  337. return 0;
  338. }
  339. static const int ina219_vshunt_gain_tab[] = { 125, 250, 500, 1000 };
  340. static const int ina219_vshunt_gain_frac[] = {
  341. 125, 1000, 250, 1000, 500, 1000, 1000, 1000 };
  342. static int ina219_set_vshunt_pga_gain(struct ina2xx_chip_info *chip,
  343. unsigned int gain,
  344. unsigned int *config)
  345. {
  346. int bits;
  347. if (gain < 125 || gain > 1000)
  348. return -EINVAL;
  349. bits = find_closest(gain, ina219_vshunt_gain_tab,
  350. ARRAY_SIZE(ina219_vshunt_gain_tab));
  351. chip->pga_gain_vshunt = ina219_vshunt_gain_tab[bits];
  352. bits = 3 - bits;
  353. *config &= ~INA219_PGA_MASK;
  354. *config |= INA219_SHIFT_PGA(bits) & INA219_PGA_MASK;
  355. return 0;
  356. }
  357. static int ina2xx_read_avail(struct iio_dev *indio_dev,
  358. struct iio_chan_spec const *chan,
  359. const int **vals, int *type, int *length,
  360. long mask)
  361. {
  362. switch (mask) {
  363. case IIO_CHAN_INFO_HARDWAREGAIN:
  364. switch (chan->address) {
  365. case INA2XX_SHUNT_VOLTAGE:
  366. *type = IIO_VAL_FRACTIONAL;
  367. *length = sizeof(ina219_vshunt_gain_frac) / sizeof(int);
  368. *vals = ina219_vshunt_gain_frac;
  369. return IIO_AVAIL_LIST;
  370. case INA2XX_BUS_VOLTAGE:
  371. *type = IIO_VAL_INT;
  372. *length = sizeof(ina219_vbus_range_tab) / sizeof(int);
  373. *vals = ina219_vbus_range_tab;
  374. return IIO_AVAIL_LIST;
  375. }
  376. }
  377. return -EINVAL;
  378. }
  379. static int ina2xx_write_raw(struct iio_dev *indio_dev,
  380. struct iio_chan_spec const *chan,
  381. int val, int val2, long mask)
  382. {
  383. struct ina2xx_chip_info *chip = iio_priv(indio_dev);
  384. unsigned int config, tmp;
  385. int ret;
  386. if (iio_buffer_enabled(indio_dev))
  387. return -EBUSY;
  388. mutex_lock(&chip->state_lock);
  389. ret = regmap_read(chip->regmap, INA2XX_CONFIG, &config);
  390. if (ret)
  391. goto err;
  392. tmp = config;
  393. switch (mask) {
  394. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  395. ret = ina226_set_average(chip, val, &tmp);
  396. break;
  397. case IIO_CHAN_INFO_INT_TIME:
  398. if (chip->config->chip_id == ina226) {
  399. if (chan->address == INA2XX_SHUNT_VOLTAGE)
  400. ret = ina226_set_int_time_vshunt(chip, val2,
  401. &tmp);
  402. else
  403. ret = ina226_set_int_time_vbus(chip, val2,
  404. &tmp);
  405. } else {
  406. if (chan->address == INA2XX_SHUNT_VOLTAGE)
  407. ret = ina219_set_int_time_vshunt(chip, val2,
  408. &tmp);
  409. else
  410. ret = ina219_set_int_time_vbus(chip, val2,
  411. &tmp);
  412. }
  413. break;
  414. case IIO_CHAN_INFO_HARDWAREGAIN:
  415. if (chan->address == INA2XX_SHUNT_VOLTAGE)
  416. ret = ina219_set_vshunt_pga_gain(chip, val * 1000 +
  417. val2 / 1000, &tmp);
  418. else
  419. ret = ina219_set_vbus_range_denom(chip, val, &tmp);
  420. break;
  421. default:
  422. ret = -EINVAL;
  423. }
  424. if (!ret && (tmp != config))
  425. ret = regmap_write(chip->regmap, INA2XX_CONFIG, tmp);
  426. err:
  427. mutex_unlock(&chip->state_lock);
  428. return ret;
  429. }
  430. static ssize_t ina2xx_allow_async_readout_show(struct device *dev,
  431. struct device_attribute *attr,
  432. char *buf)
  433. {
  434. struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
  435. return sprintf(buf, "%d\n", chip->allow_async_readout);
  436. }
  437. static ssize_t ina2xx_allow_async_readout_store(struct device *dev,
  438. struct device_attribute *attr,
  439. const char *buf, size_t len)
  440. {
  441. struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
  442. bool val;
  443. int ret;
  444. ret = strtobool((const char *) buf, &val);
  445. if (ret)
  446. return ret;
  447. chip->allow_async_readout = val;
  448. return len;
  449. }
  450. /*
  451. * Calibration register is set to the best value, which eliminates
  452. * truncation errors on calculating current register in hardware.
  453. * According to datasheet (INA 226: eq. 3, INA219: eq. 4) the best values
  454. * are 2048 for ina226 and 4096 for ina219. They are hardcoded as
  455. * calibration_value.
  456. */
  457. static int ina2xx_set_calibration(struct ina2xx_chip_info *chip)
  458. {
  459. return regmap_write(chip->regmap, INA2XX_CALIBRATION,
  460. chip->config->calibration_value);
  461. }
  462. static int set_shunt_resistor(struct ina2xx_chip_info *chip, unsigned int val)
  463. {
  464. if (val == 0 || val > INT_MAX)
  465. return -EINVAL;
  466. chip->shunt_resistor_uohm = val;
  467. return 0;
  468. }
  469. static ssize_t ina2xx_shunt_resistor_show(struct device *dev,
  470. struct device_attribute *attr,
  471. char *buf)
  472. {
  473. struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
  474. int vals[2] = { chip->shunt_resistor_uohm, 1000000 };
  475. return iio_format_value(buf, IIO_VAL_FRACTIONAL, 1, vals);
  476. }
  477. static ssize_t ina2xx_shunt_resistor_store(struct device *dev,
  478. struct device_attribute *attr,
  479. const char *buf, size_t len)
  480. {
  481. struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
  482. int val, val_fract, ret;
  483. ret = iio_str_to_fixpoint(buf, 100000, &val, &val_fract);
  484. if (ret)
  485. return ret;
  486. ret = set_shunt_resistor(chip, val * 1000000 + val_fract);
  487. if (ret)
  488. return ret;
  489. return len;
  490. }
  491. #define INA219_CHAN(_type, _index, _address) { \
  492. .type = (_type), \
  493. .address = (_address), \
  494. .indexed = 1, \
  495. .channel = (_index), \
  496. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  497. BIT(IIO_CHAN_INFO_SCALE), \
  498. .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  499. .scan_index = (_index), \
  500. .scan_type = { \
  501. .sign = 'u', \
  502. .realbits = 16, \
  503. .storagebits = 16, \
  504. .endianness = IIO_CPU, \
  505. } \
  506. }
  507. #define INA226_CHAN(_type, _index, _address) { \
  508. .type = (_type), \
  509. .address = (_address), \
  510. .indexed = 1, \
  511. .channel = (_index), \
  512. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  513. BIT(IIO_CHAN_INFO_SCALE), \
  514. .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
  515. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  516. .scan_index = (_index), \
  517. .scan_type = { \
  518. .sign = 'u', \
  519. .realbits = 16, \
  520. .storagebits = 16, \
  521. .endianness = IIO_CPU, \
  522. } \
  523. }
  524. /*
  525. * Sampling Freq is a consequence of the integration times of
  526. * the Voltage channels.
  527. */
  528. #define INA219_CHAN_VOLTAGE(_index, _address, _shift) { \
  529. .type = IIO_VOLTAGE, \
  530. .address = (_address), \
  531. .indexed = 1, \
  532. .channel = (_index), \
  533. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  534. BIT(IIO_CHAN_INFO_SCALE) | \
  535. BIT(IIO_CHAN_INFO_INT_TIME) | \
  536. BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
  537. .info_mask_separate_available = \
  538. BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
  539. .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  540. .scan_index = (_index), \
  541. .scan_type = { \
  542. .sign = 'u', \
  543. .shift = _shift, \
  544. .realbits = 16 - _shift, \
  545. .storagebits = 16, \
  546. .endianness = IIO_LE, \
  547. } \
  548. }
  549. #define INA226_CHAN_VOLTAGE(_index, _address) { \
  550. .type = IIO_VOLTAGE, \
  551. .address = (_address), \
  552. .indexed = 1, \
  553. .channel = (_index), \
  554. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  555. BIT(IIO_CHAN_INFO_SCALE) | \
  556. BIT(IIO_CHAN_INFO_INT_TIME), \
  557. .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
  558. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  559. .scan_index = (_index), \
  560. .scan_type = { \
  561. .sign = 'u', \
  562. .realbits = 16, \
  563. .storagebits = 16, \
  564. .endianness = IIO_LE, \
  565. } \
  566. }
  567. static const struct iio_chan_spec ina226_channels[] = {
  568. INA226_CHAN_VOLTAGE(0, INA2XX_SHUNT_VOLTAGE),
  569. INA226_CHAN_VOLTAGE(1, INA2XX_BUS_VOLTAGE),
  570. INA226_CHAN(IIO_POWER, 2, INA2XX_POWER),
  571. INA226_CHAN(IIO_CURRENT, 3, INA2XX_CURRENT),
  572. IIO_CHAN_SOFT_TIMESTAMP(4),
  573. };
  574. static const struct iio_chan_spec ina219_channels[] = {
  575. INA219_CHAN_VOLTAGE(0, INA2XX_SHUNT_VOLTAGE, 0),
  576. INA219_CHAN_VOLTAGE(1, INA2XX_BUS_VOLTAGE, INA219_BUS_VOLTAGE_SHIFT),
  577. INA219_CHAN(IIO_POWER, 2, INA2XX_POWER),
  578. INA219_CHAN(IIO_CURRENT, 3, INA2XX_CURRENT),
  579. IIO_CHAN_SOFT_TIMESTAMP(4),
  580. };
  581. static int ina2xx_conversion_ready(struct iio_dev *indio_dev)
  582. {
  583. struct ina2xx_chip_info *chip = iio_priv(indio_dev);
  584. int ret;
  585. unsigned int alert;
  586. /*
  587. * Because the timer thread and the chip conversion clock
  588. * are asynchronous, the period difference will eventually
  589. * result in reading V[k-1] again, or skip V[k] at time Tk.
  590. * In order to resync the timer with the conversion process
  591. * we check the ConVersionReadyFlag.
  592. * On hardware that supports using the ALERT pin to toggle a
  593. * GPIO a triggered buffer could be used instead.
  594. * For now, we do an extra read of the MASK_ENABLE register (INA226)
  595. * resp. the BUS_VOLTAGE register (INA219).
  596. */
  597. if (chip->config->chip_id == ina226) {
  598. ret = regmap_read(chip->regmap,
  599. INA226_MASK_ENABLE, &alert);
  600. alert &= INA226_CVRF;
  601. } else {
  602. ret = regmap_read(chip->regmap,
  603. INA2XX_BUS_VOLTAGE, &alert);
  604. alert &= INA219_CNVR;
  605. }
  606. if (ret < 0)
  607. return ret;
  608. return !!alert;
  609. }
  610. static int ina2xx_work_buffer(struct iio_dev *indio_dev)
  611. {
  612. struct ina2xx_chip_info *chip = iio_priv(indio_dev);
  613. /* data buffer needs space for channel data and timestap */
  614. unsigned short data[4 + sizeof(s64)/sizeof(short)];
  615. int bit, ret, i = 0;
  616. s64 time;
  617. time = iio_get_time_ns(indio_dev);
  618. /*
  619. * Single register reads: bulk_read will not work with ina226/219
  620. * as there is no auto-increment of the register pointer.
  621. */
  622. for_each_set_bit(bit, indio_dev->active_scan_mask,
  623. indio_dev->masklength) {
  624. unsigned int val;
  625. ret = regmap_read(chip->regmap,
  626. INA2XX_SHUNT_VOLTAGE + bit, &val);
  627. if (ret < 0)
  628. return ret;
  629. data[i++] = val;
  630. }
  631. iio_push_to_buffers_with_timestamp(indio_dev, data, time);
  632. return 0;
  633. };
  634. static int ina2xx_capture_thread(void *data)
  635. {
  636. struct iio_dev *indio_dev = data;
  637. struct ina2xx_chip_info *chip = iio_priv(indio_dev);
  638. int sampling_us = SAMPLING_PERIOD(chip);
  639. int ret;
  640. struct timespec64 next, now, delta;
  641. s64 delay_us;
  642. /*
  643. * Poll a bit faster than the chip internal Fs, in case
  644. * we wish to sync with the conversion ready flag.
  645. */
  646. if (!chip->allow_async_readout)
  647. sampling_us -= 200;
  648. ktime_get_ts64(&next);
  649. do {
  650. while (!chip->allow_async_readout) {
  651. ret = ina2xx_conversion_ready(indio_dev);
  652. if (ret < 0)
  653. return ret;
  654. /*
  655. * If the conversion was not yet finished,
  656. * reset the reference timestamp.
  657. */
  658. if (ret == 0)
  659. ktime_get_ts64(&next);
  660. else
  661. break;
  662. }
  663. ret = ina2xx_work_buffer(indio_dev);
  664. if (ret < 0)
  665. return ret;
  666. ktime_get_ts64(&now);
  667. /*
  668. * Advance the timestamp for the next poll by one sampling
  669. * interval, and sleep for the remainder (next - now)
  670. * In case "next" has already passed, the interval is added
  671. * multiple times, i.e. samples are dropped.
  672. */
  673. do {
  674. timespec64_add_ns(&next, 1000 * sampling_us);
  675. delta = timespec64_sub(next, now);
  676. delay_us = div_s64(timespec64_to_ns(&delta), 1000);
  677. } while (delay_us <= 0);
  678. usleep_range(delay_us, (delay_us * 3) >> 1);
  679. } while (!kthread_should_stop());
  680. return 0;
  681. }
  682. static int ina2xx_buffer_enable(struct iio_dev *indio_dev)
  683. {
  684. struct ina2xx_chip_info *chip = iio_priv(indio_dev);
  685. unsigned int sampling_us = SAMPLING_PERIOD(chip);
  686. struct task_struct *task;
  687. dev_dbg(&indio_dev->dev, "Enabling buffer w/ scan_mask %02x, freq = %d, avg =%u\n",
  688. (unsigned int)(*indio_dev->active_scan_mask),
  689. 1000000 / sampling_us, chip->avg);
  690. dev_dbg(&indio_dev->dev, "Expected work period: %u us\n", sampling_us);
  691. dev_dbg(&indio_dev->dev, "Async readout mode: %d\n",
  692. chip->allow_async_readout);
  693. task = kthread_create(ina2xx_capture_thread, (void *)indio_dev,
  694. "%s:%d-%uus", indio_dev->name, indio_dev->id,
  695. sampling_us);
  696. if (IS_ERR(task))
  697. return PTR_ERR(task);
  698. get_task_struct(task);
  699. wake_up_process(task);
  700. chip->task = task;
  701. return 0;
  702. }
  703. static int ina2xx_buffer_disable(struct iio_dev *indio_dev)
  704. {
  705. struct ina2xx_chip_info *chip = iio_priv(indio_dev);
  706. if (chip->task) {
  707. kthread_stop(chip->task);
  708. put_task_struct(chip->task);
  709. chip->task = NULL;
  710. }
  711. return 0;
  712. }
  713. static const struct iio_buffer_setup_ops ina2xx_setup_ops = {
  714. .postenable = &ina2xx_buffer_enable,
  715. .predisable = &ina2xx_buffer_disable,
  716. };
  717. static int ina2xx_debug_reg(struct iio_dev *indio_dev,
  718. unsigned reg, unsigned writeval, unsigned *readval)
  719. {
  720. struct ina2xx_chip_info *chip = iio_priv(indio_dev);
  721. if (!readval)
  722. return regmap_write(chip->regmap, reg, writeval);
  723. return regmap_read(chip->regmap, reg, readval);
  724. }
  725. /* Possible integration times for vshunt and vbus */
  726. static IIO_CONST_ATTR_NAMED(ina219_integration_time_available,
  727. integration_time_available,
  728. "0.000084 0.000148 0.000276 0.000532 0.001060 0.002130 0.004260 0.008510 0.017020 0.034050 0.068100");
  729. static IIO_CONST_ATTR_NAMED(ina226_integration_time_available,
  730. integration_time_available,
  731. "0.000140 0.000204 0.000332 0.000588 0.001100 0.002116 0.004156 0.008244");
  732. static IIO_DEVICE_ATTR(in_allow_async_readout, S_IRUGO | S_IWUSR,
  733. ina2xx_allow_async_readout_show,
  734. ina2xx_allow_async_readout_store, 0);
  735. static IIO_DEVICE_ATTR(in_shunt_resistor, S_IRUGO | S_IWUSR,
  736. ina2xx_shunt_resistor_show,
  737. ina2xx_shunt_resistor_store, 0);
  738. static struct attribute *ina219_attributes[] = {
  739. &iio_dev_attr_in_allow_async_readout.dev_attr.attr,
  740. &iio_const_attr_ina219_integration_time_available.dev_attr.attr,
  741. &iio_dev_attr_in_shunt_resistor.dev_attr.attr,
  742. NULL,
  743. };
  744. static struct attribute *ina226_attributes[] = {
  745. &iio_dev_attr_in_allow_async_readout.dev_attr.attr,
  746. &iio_const_attr_ina226_integration_time_available.dev_attr.attr,
  747. &iio_dev_attr_in_shunt_resistor.dev_attr.attr,
  748. NULL,
  749. };
  750. static const struct attribute_group ina219_attribute_group = {
  751. .attrs = ina219_attributes,
  752. };
  753. static const struct attribute_group ina226_attribute_group = {
  754. .attrs = ina226_attributes,
  755. };
  756. static const struct iio_info ina219_info = {
  757. .attrs = &ina219_attribute_group,
  758. .read_raw = ina2xx_read_raw,
  759. .read_avail = ina2xx_read_avail,
  760. .write_raw = ina2xx_write_raw,
  761. .debugfs_reg_access = ina2xx_debug_reg,
  762. };
  763. static const struct iio_info ina226_info = {
  764. .attrs = &ina226_attribute_group,
  765. .read_raw = ina2xx_read_raw,
  766. .write_raw = ina2xx_write_raw,
  767. .debugfs_reg_access = ina2xx_debug_reg,
  768. };
  769. /* Initialize the configuration and calibration registers. */
  770. static int ina2xx_init(struct ina2xx_chip_info *chip, unsigned int config)
  771. {
  772. int ret = regmap_write(chip->regmap, INA2XX_CONFIG, config);
  773. if (ret)
  774. return ret;
  775. return ina2xx_set_calibration(chip);
  776. }
  777. static int ina2xx_probe(struct i2c_client *client,
  778. const struct i2c_device_id *id)
  779. {
  780. struct ina2xx_chip_info *chip;
  781. struct iio_dev *indio_dev;
  782. struct iio_buffer *buffer;
  783. unsigned int val;
  784. enum ina2xx_ids type;
  785. int ret;
  786. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
  787. if (!indio_dev)
  788. return -ENOMEM;
  789. chip = iio_priv(indio_dev);
  790. /* This is only used for device removal purposes. */
  791. i2c_set_clientdata(client, indio_dev);
  792. chip->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
  793. if (IS_ERR(chip->regmap)) {
  794. dev_err(&client->dev, "failed to allocate register map\n");
  795. return PTR_ERR(chip->regmap);
  796. }
  797. if (client->dev.of_node)
  798. type = (enum ina2xx_ids)of_device_get_match_data(&client->dev);
  799. else
  800. type = id->driver_data;
  801. chip->config = &ina2xx_config[type];
  802. mutex_init(&chip->state_lock);
  803. if (of_property_read_u32(client->dev.of_node,
  804. "shunt-resistor", &val) < 0) {
  805. struct ina2xx_platform_data *pdata =
  806. dev_get_platdata(&client->dev);
  807. if (pdata)
  808. val = pdata->shunt_uohms;
  809. else
  810. val = INA2XX_RSHUNT_DEFAULT;
  811. }
  812. ret = set_shunt_resistor(chip, val);
  813. if (ret)
  814. return ret;
  815. /* Patch the current config register with default. */
  816. val = chip->config->config_default;
  817. if (id->driver_data == ina226) {
  818. ina226_set_average(chip, INA226_DEFAULT_AVG, &val);
  819. ina226_set_int_time_vbus(chip, INA226_DEFAULT_IT, &val);
  820. ina226_set_int_time_vshunt(chip, INA226_DEFAULT_IT, &val);
  821. } else {
  822. chip->avg = 1;
  823. ina219_set_int_time_vbus(chip, INA219_DEFAULT_IT, &val);
  824. ina219_set_int_time_vshunt(chip, INA219_DEFAULT_IT, &val);
  825. ina219_set_vbus_range_denom(chip, INA219_DEFAULT_BRNG, &val);
  826. ina219_set_vshunt_pga_gain(chip, INA219_DEFAULT_PGA, &val);
  827. }
  828. ret = ina2xx_init(chip, val);
  829. if (ret) {
  830. dev_err(&client->dev, "error configuring the device\n");
  831. return ret;
  832. }
  833. indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
  834. indio_dev->dev.parent = &client->dev;
  835. indio_dev->dev.of_node = client->dev.of_node;
  836. if (id->driver_data == ina226) {
  837. indio_dev->channels = ina226_channels;
  838. indio_dev->num_channels = ARRAY_SIZE(ina226_channels);
  839. indio_dev->info = &ina226_info;
  840. } else {
  841. indio_dev->channels = ina219_channels;
  842. indio_dev->num_channels = ARRAY_SIZE(ina219_channels);
  843. indio_dev->info = &ina219_info;
  844. }
  845. indio_dev->name = id->name;
  846. indio_dev->setup_ops = &ina2xx_setup_ops;
  847. buffer = devm_iio_kfifo_allocate(&indio_dev->dev);
  848. if (!buffer)
  849. return -ENOMEM;
  850. iio_device_attach_buffer(indio_dev, buffer);
  851. return iio_device_register(indio_dev);
  852. }
  853. static int ina2xx_remove(struct i2c_client *client)
  854. {
  855. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  856. struct ina2xx_chip_info *chip = iio_priv(indio_dev);
  857. iio_device_unregister(indio_dev);
  858. /* Powerdown */
  859. return regmap_update_bits(chip->regmap, INA2XX_CONFIG,
  860. INA2XX_MODE_MASK, 0);
  861. }
  862. static const struct i2c_device_id ina2xx_id[] = {
  863. {"ina219", ina219},
  864. {"ina220", ina219},
  865. {"ina226", ina226},
  866. {"ina230", ina226},
  867. {"ina231", ina226},
  868. {}
  869. };
  870. MODULE_DEVICE_TABLE(i2c, ina2xx_id);
  871. static const struct of_device_id ina2xx_of_match[] = {
  872. {
  873. .compatible = "ti,ina219",
  874. .data = (void *)ina219
  875. },
  876. {
  877. .compatible = "ti,ina220",
  878. .data = (void *)ina219
  879. },
  880. {
  881. .compatible = "ti,ina226",
  882. .data = (void *)ina226
  883. },
  884. {
  885. .compatible = "ti,ina230",
  886. .data = (void *)ina226
  887. },
  888. {
  889. .compatible = "ti,ina231",
  890. .data = (void *)ina226
  891. },
  892. {},
  893. };
  894. MODULE_DEVICE_TABLE(of, ina2xx_of_match);
  895. static struct i2c_driver ina2xx_driver = {
  896. .driver = {
  897. .name = KBUILD_MODNAME,
  898. .of_match_table = ina2xx_of_match,
  899. },
  900. .probe = ina2xx_probe,
  901. .remove = ina2xx_remove,
  902. .id_table = ina2xx_id,
  903. };
  904. module_i2c_driver(ina2xx_driver);
  905. MODULE_AUTHOR("Marc Titinger <marc.titinger@baylibre.com>");
  906. MODULE_DESCRIPTION("Texas Instruments INA2XX ADC driver");
  907. MODULE_LICENSE("GPL v2");