ina2xx-adc.c 28 KB

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