ina2xx-adc.c 28 KB

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