ina2xx-adc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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/util_macros.h>
  33. #include <linux/platform_data/ina2xx.h>
  34. /* INA2XX registers definition */
  35. #define INA2XX_CONFIG 0x00
  36. #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
  37. #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
  38. #define INA2XX_POWER 0x03 /* readonly */
  39. #define INA2XX_CURRENT 0x04 /* readonly */
  40. #define INA2XX_CALIBRATION 0x05
  41. #define INA226_MASK_ENABLE 0x06
  42. #define INA226_CVRF BIT(3)
  43. #define INA219_CNVR BIT(1)
  44. #define INA2XX_MAX_REGISTERS 8
  45. /* settings - depend on use case */
  46. #define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
  47. #define INA219_DEFAULT_IT 532
  48. #define INA226_CONFIG_DEFAULT 0x4327
  49. #define INA226_DEFAULT_AVG 4
  50. #define INA226_DEFAULT_IT 1110
  51. #define INA2XX_RSHUNT_DEFAULT 10000
  52. /*
  53. * bit masks for reading the settings in the configuration register
  54. * FIXME: use regmap_fields.
  55. */
  56. #define INA2XX_MODE_MASK GENMASK(3, 0)
  57. /* Averaging for VBus/VShunt/Power */
  58. #define INA226_AVG_MASK GENMASK(11, 9)
  59. #define INA226_SHIFT_AVG(val) ((val) << 9)
  60. /* Integration time for VBus */
  61. #define INA219_ITB_MASK GENMASK(10, 7)
  62. #define INA219_SHIFT_ITB(val) ((val) << 7)
  63. #define INA226_ITB_MASK GENMASK(8, 6)
  64. #define INA226_SHIFT_ITB(val) ((val) << 6)
  65. /* Integration time for VShunt */
  66. #define INA219_ITS_MASK GENMASK(6, 3)
  67. #define INA219_SHIFT_ITS(val) ((val) << 3)
  68. #define INA226_ITS_MASK GENMASK(5, 3)
  69. #define INA226_SHIFT_ITS(val) ((val) << 3)
  70. /* Cosmetic macro giving the sampling period for a full P=UxI cycle */
  71. #define SAMPLING_PERIOD(c) ((c->int_time_vbus + c->int_time_vshunt) \
  72. * c->avg)
  73. static bool ina2xx_is_writeable_reg(struct device *dev, unsigned int reg)
  74. {
  75. return (reg == INA2XX_CONFIG) || (reg > INA2XX_CURRENT);
  76. }
  77. static bool ina2xx_is_volatile_reg(struct device *dev, unsigned int reg)
  78. {
  79. return (reg != INA2XX_CONFIG);
  80. }
  81. static inline bool is_signed_reg(unsigned int reg)
  82. {
  83. return (reg == INA2XX_SHUNT_VOLTAGE) || (reg == INA2XX_CURRENT);
  84. }
  85. static const struct regmap_config ina2xx_regmap_config = {
  86. .reg_bits = 8,
  87. .val_bits = 16,
  88. .max_register = INA2XX_MAX_REGISTERS,
  89. .writeable_reg = ina2xx_is_writeable_reg,
  90. .volatile_reg = ina2xx_is_volatile_reg,
  91. };
  92. enum ina2xx_ids { ina219, ina226 };
  93. struct ina2xx_config {
  94. u16 config_default;
  95. int calibration_factor;
  96. int shunt_div;
  97. int bus_voltage_shift;
  98. int bus_voltage_lsb; /* uV */
  99. int power_lsb; /* uW */
  100. enum ina2xx_ids chip_id;
  101. };
  102. struct ina2xx_chip_info {
  103. struct regmap *regmap;
  104. struct task_struct *task;
  105. const struct ina2xx_config *config;
  106. struct mutex state_lock;
  107. unsigned int shunt_resistor;
  108. int avg;
  109. int int_time_vbus; /* Bus voltage integration time uS */
  110. int int_time_vshunt; /* Shunt voltage integration time uS */
  111. bool allow_async_readout;
  112. };
  113. static const struct ina2xx_config ina2xx_config[] = {
  114. [ina219] = {
  115. .config_default = INA219_CONFIG_DEFAULT,
  116. .calibration_factor = 40960000,
  117. .shunt_div = 100,
  118. .bus_voltage_shift = 3,
  119. .bus_voltage_lsb = 4000,
  120. .power_lsb = 20000,
  121. .chip_id = ina219,
  122. },
  123. [ina226] = {
  124. .config_default = INA226_CONFIG_DEFAULT,
  125. .calibration_factor = 5120000,
  126. .shunt_div = 400,
  127. .bus_voltage_shift = 0,
  128. .bus_voltage_lsb = 1250,
  129. .power_lsb = 25000,
  130. .chip_id = ina226,
  131. },
  132. };
  133. static int ina2xx_read_raw(struct iio_dev *indio_dev,
  134. struct iio_chan_spec const *chan,
  135. int *val, int *val2, long mask)
  136. {
  137. int ret;
  138. struct ina2xx_chip_info *chip = iio_priv(indio_dev);
  139. unsigned int regval;
  140. switch (mask) {
  141. case IIO_CHAN_INFO_RAW:
  142. ret = regmap_read(chip->regmap, chan->address, &regval);
  143. if (ret)
  144. return ret;
  145. if (is_signed_reg(chan->address))
  146. *val = (s16) regval;
  147. else
  148. *val = regval;
  149. return IIO_VAL_INT;
  150. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  151. *val = chip->avg;
  152. return IIO_VAL_INT;
  153. case IIO_CHAN_INFO_INT_TIME:
  154. *val = 0;
  155. if (chan->address == INA2XX_SHUNT_VOLTAGE)
  156. *val2 = chip->int_time_vshunt;
  157. else
  158. *val2 = chip->int_time_vbus;
  159. return IIO_VAL_INT_PLUS_MICRO;
  160. case IIO_CHAN_INFO_SAMP_FREQ:
  161. /*
  162. * Sample freq is read only, it is a consequence of
  163. * 1/AVG*(CT_bus+CT_shunt).
  164. */
  165. *val = DIV_ROUND_CLOSEST(1000000, SAMPLING_PERIOD(chip));
  166. return IIO_VAL_INT;
  167. case IIO_CHAN_INFO_SCALE:
  168. switch (chan->address) {
  169. case INA2XX_SHUNT_VOLTAGE:
  170. /* processed (mV) = raw/shunt_div */
  171. *val2 = chip->config->shunt_div;
  172. *val = 1;
  173. return IIO_VAL_FRACTIONAL;
  174. case INA2XX_BUS_VOLTAGE:
  175. /* processed (mV) = raw*lsb (uV) / (1000 << shift) */
  176. *val = chip->config->bus_voltage_lsb;
  177. *val2 = 1000 << chip->config->bus_voltage_shift;
  178. return IIO_VAL_FRACTIONAL;
  179. case INA2XX_POWER:
  180. /* processed (mW) = raw*lsb (uW) / 1000 */
  181. *val = chip->config->power_lsb;
  182. *val2 = 1000;
  183. return IIO_VAL_FRACTIONAL;
  184. case INA2XX_CURRENT:
  185. /* processed (mA) = raw (mA) */
  186. *val = 1;
  187. return IIO_VAL_INT;
  188. }
  189. }
  190. return -EINVAL;
  191. }
  192. /*
  193. * Available averaging rates for ina226. The indices correspond with
  194. * the bit values expected by the chip (according to the ina226 datasheet,
  195. * table 3 AVG bit settings, found at
  196. * http://www.ti.com/lit/ds/symlink/ina226.pdf.
  197. */
  198. static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
  199. static int ina226_set_average(struct ina2xx_chip_info *chip, unsigned int val,
  200. unsigned int *config)
  201. {
  202. int bits;
  203. if (val > 1024 || val < 1)
  204. return -EINVAL;
  205. bits = find_closest(val, ina226_avg_tab,
  206. ARRAY_SIZE(ina226_avg_tab));
  207. chip->avg = ina226_avg_tab[bits];
  208. *config &= ~INA226_AVG_MASK;
  209. *config |= INA226_SHIFT_AVG(bits) & INA226_AVG_MASK;
  210. return 0;
  211. }
  212. /* Conversion times in uS */
  213. static const int ina226_conv_time_tab[] = { 140, 204, 332, 588, 1100,
  214. 2116, 4156, 8244 };
  215. static int ina226_set_int_time_vbus(struct ina2xx_chip_info *chip,
  216. unsigned int val_us, unsigned int *config)
  217. {
  218. int bits;
  219. if (val_us > 8244 || val_us < 140)
  220. return -EINVAL;
  221. bits = find_closest(val_us, ina226_conv_time_tab,
  222. ARRAY_SIZE(ina226_conv_time_tab));
  223. chip->int_time_vbus = ina226_conv_time_tab[bits];
  224. *config &= ~INA226_ITB_MASK;
  225. *config |= INA226_SHIFT_ITB(bits) & INA226_ITB_MASK;
  226. return 0;
  227. }
  228. static int ina226_set_int_time_vshunt(struct ina2xx_chip_info *chip,
  229. unsigned int val_us, unsigned int *config)
  230. {
  231. int bits;
  232. if (val_us > 8244 || val_us < 140)
  233. return -EINVAL;
  234. bits = find_closest(val_us, ina226_conv_time_tab,
  235. ARRAY_SIZE(ina226_conv_time_tab));
  236. chip->int_time_vshunt = ina226_conv_time_tab[bits];
  237. *config &= ~INA226_ITS_MASK;
  238. *config |= INA226_SHIFT_ITS(bits) & INA226_ITS_MASK;
  239. return 0;
  240. }
  241. /* Conversion times in uS. */
  242. static const int ina219_conv_time_tab_subsample[] = { 84, 148, 276, 532 };
  243. static const int ina219_conv_time_tab_average[] = { 532, 1060, 2130, 4260,
  244. 8510, 17020, 34050, 68100};
  245. static int ina219_lookup_int_time(unsigned int *val_us, int *bits)
  246. {
  247. if (*val_us > 68100 || *val_us < 84)
  248. return -EINVAL;
  249. if (*val_us <= 532) {
  250. *bits = find_closest(*val_us, ina219_conv_time_tab_subsample,
  251. ARRAY_SIZE(ina219_conv_time_tab_subsample));
  252. *val_us = ina219_conv_time_tab_subsample[*bits];
  253. } else {
  254. *bits = find_closest(*val_us, ina219_conv_time_tab_average,
  255. ARRAY_SIZE(ina219_conv_time_tab_average));
  256. *val_us = ina219_conv_time_tab_average[*bits];
  257. *bits |= 0x8;
  258. }
  259. return 0;
  260. }
  261. static int ina219_set_int_time_vbus(struct ina2xx_chip_info *chip,
  262. unsigned int val_us, unsigned int *config)
  263. {
  264. int bits, ret;
  265. unsigned int val_us_best = val_us;
  266. ret = ina219_lookup_int_time(&val_us_best, &bits);
  267. if (ret)
  268. return ret;
  269. chip->int_time_vbus = val_us_best;
  270. *config &= ~INA219_ITB_MASK;
  271. *config |= INA219_SHIFT_ITB(bits) & INA219_ITB_MASK;
  272. return 0;
  273. }
  274. static int ina219_set_int_time_vshunt(struct ina2xx_chip_info *chip,
  275. unsigned int val_us, unsigned int *config)
  276. {
  277. int bits, ret;
  278. unsigned int val_us_best = val_us;
  279. ret = ina219_lookup_int_time(&val_us_best, &bits);
  280. if (ret)
  281. return ret;
  282. chip->int_time_vshunt = val_us_best;
  283. *config &= ~INA219_ITS_MASK;
  284. *config |= INA219_SHIFT_ITS(bits) & INA219_ITS_MASK;
  285. return 0;
  286. }
  287. static int ina2xx_write_raw(struct iio_dev *indio_dev,
  288. struct iio_chan_spec const *chan,
  289. int val, int val2, long mask)
  290. {
  291. struct ina2xx_chip_info *chip = iio_priv(indio_dev);
  292. unsigned int config, tmp;
  293. int ret;
  294. if (iio_buffer_enabled(indio_dev))
  295. return -EBUSY;
  296. mutex_lock(&chip->state_lock);
  297. ret = regmap_read(chip->regmap, INA2XX_CONFIG, &config);
  298. if (ret)
  299. goto err;
  300. tmp = config;
  301. switch (mask) {
  302. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  303. ret = ina226_set_average(chip, val, &tmp);
  304. break;
  305. case IIO_CHAN_INFO_INT_TIME:
  306. if (chip->config->chip_id == ina226) {
  307. if (chan->address == INA2XX_SHUNT_VOLTAGE)
  308. ret = ina226_set_int_time_vshunt(chip, val2,
  309. &tmp);
  310. else
  311. ret = ina226_set_int_time_vbus(chip, val2,
  312. &tmp);
  313. } else {
  314. if (chan->address == INA2XX_SHUNT_VOLTAGE)
  315. ret = ina219_set_int_time_vshunt(chip, val2,
  316. &tmp);
  317. else
  318. ret = ina219_set_int_time_vbus(chip, val2,
  319. &tmp);
  320. }
  321. break;
  322. default:
  323. ret = -EINVAL;
  324. }
  325. if (!ret && (tmp != config))
  326. ret = regmap_write(chip->regmap, INA2XX_CONFIG, tmp);
  327. err:
  328. mutex_unlock(&chip->state_lock);
  329. return ret;
  330. }
  331. static ssize_t ina2xx_allow_async_readout_show(struct device *dev,
  332. struct device_attribute *attr,
  333. char *buf)
  334. {
  335. struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
  336. return sprintf(buf, "%d\n", chip->allow_async_readout);
  337. }
  338. static ssize_t ina2xx_allow_async_readout_store(struct device *dev,
  339. struct device_attribute *attr,
  340. const char *buf, size_t len)
  341. {
  342. struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
  343. bool val;
  344. int ret;
  345. ret = strtobool((const char *) buf, &val);
  346. if (ret)
  347. return ret;
  348. chip->allow_async_readout = val;
  349. return len;
  350. }
  351. /*
  352. * Set current LSB to 1mA, shunt is in uOhms
  353. * (equation 13 in datasheet). We hardcode a Current_LSB
  354. * of 1.0 x10-6. The only remaining parameter is RShunt.
  355. * There is no need to expose the CALIBRATION register
  356. * to the user for now. But we need to reset this register
  357. * if the user updates RShunt after driver init, e.g upon
  358. * reading an EEPROM/Probe-type value.
  359. */
  360. static int ina2xx_set_calibration(struct ina2xx_chip_info *chip)
  361. {
  362. u16 regval = DIV_ROUND_CLOSEST(chip->config->calibration_factor,
  363. chip->shunt_resistor);
  364. return regmap_write(chip->regmap, INA2XX_CALIBRATION, regval);
  365. }
  366. static int set_shunt_resistor(struct ina2xx_chip_info *chip, unsigned int val)
  367. {
  368. if (val <= 0 || val > chip->config->calibration_factor)
  369. return -EINVAL;
  370. chip->shunt_resistor = val;
  371. return 0;
  372. }
  373. static ssize_t ina2xx_shunt_resistor_show(struct device *dev,
  374. struct device_attribute *attr,
  375. char *buf)
  376. {
  377. struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
  378. return sprintf(buf, "%d\n", chip->shunt_resistor);
  379. }
  380. static ssize_t ina2xx_shunt_resistor_store(struct device *dev,
  381. struct device_attribute *attr,
  382. const char *buf, size_t len)
  383. {
  384. struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
  385. unsigned long val;
  386. int ret;
  387. ret = kstrtoul((const char *) buf, 10, &val);
  388. if (ret)
  389. return ret;
  390. ret = set_shunt_resistor(chip, val);
  391. if (ret)
  392. return ret;
  393. /* Update the Calibration register */
  394. ret = ina2xx_set_calibration(chip);
  395. if (ret)
  396. return ret;
  397. return len;
  398. }
  399. #define INA219_CHAN(_type, _index, _address) { \
  400. .type = (_type), \
  401. .address = (_address), \
  402. .indexed = 1, \
  403. .channel = (_index), \
  404. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  405. BIT(IIO_CHAN_INFO_SCALE), \
  406. .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  407. .scan_index = (_index), \
  408. .scan_type = { \
  409. .sign = 'u', \
  410. .realbits = 16, \
  411. .storagebits = 16, \
  412. .endianness = IIO_CPU, \
  413. } \
  414. }
  415. #define INA226_CHAN(_type, _index, _address) { \
  416. .type = (_type), \
  417. .address = (_address), \
  418. .indexed = 1, \
  419. .channel = (_index), \
  420. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  421. BIT(IIO_CHAN_INFO_SCALE), \
  422. .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
  423. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  424. .scan_index = (_index), \
  425. .scan_type = { \
  426. .sign = 'u', \
  427. .realbits = 16, \
  428. .storagebits = 16, \
  429. .endianness = IIO_CPU, \
  430. } \
  431. }
  432. /*
  433. * Sampling Freq is a consequence of the integration times of
  434. * the Voltage channels.
  435. */
  436. #define INA219_CHAN_VOLTAGE(_index, _address) { \
  437. .type = IIO_VOLTAGE, \
  438. .address = (_address), \
  439. .indexed = 1, \
  440. .channel = (_index), \
  441. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  442. BIT(IIO_CHAN_INFO_SCALE) | \
  443. BIT(IIO_CHAN_INFO_INT_TIME), \
  444. .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  445. .scan_index = (_index), \
  446. .scan_type = { \
  447. .sign = 'u', \
  448. .realbits = 16, \
  449. .storagebits = 16, \
  450. .endianness = IIO_LE, \
  451. } \
  452. }
  453. #define INA226_CHAN_VOLTAGE(_index, _address) { \
  454. .type = IIO_VOLTAGE, \
  455. .address = (_address), \
  456. .indexed = 1, \
  457. .channel = (_index), \
  458. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  459. BIT(IIO_CHAN_INFO_SCALE) | \
  460. BIT(IIO_CHAN_INFO_INT_TIME), \
  461. .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
  462. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  463. .scan_index = (_index), \
  464. .scan_type = { \
  465. .sign = 'u', \
  466. .realbits = 16, \
  467. .storagebits = 16, \
  468. .endianness = IIO_LE, \
  469. } \
  470. }
  471. static const struct iio_chan_spec ina226_channels[] = {
  472. INA226_CHAN_VOLTAGE(0, INA2XX_SHUNT_VOLTAGE),
  473. INA226_CHAN_VOLTAGE(1, INA2XX_BUS_VOLTAGE),
  474. INA226_CHAN(IIO_POWER, 2, INA2XX_POWER),
  475. INA226_CHAN(IIO_CURRENT, 3, INA2XX_CURRENT),
  476. IIO_CHAN_SOFT_TIMESTAMP(4),
  477. };
  478. static const struct iio_chan_spec ina219_channels[] = {
  479. INA219_CHAN_VOLTAGE(0, INA2XX_SHUNT_VOLTAGE),
  480. INA219_CHAN_VOLTAGE(1, INA2XX_BUS_VOLTAGE),
  481. INA219_CHAN(IIO_POWER, 2, INA2XX_POWER),
  482. INA219_CHAN(IIO_CURRENT, 3, INA2XX_CURRENT),
  483. IIO_CHAN_SOFT_TIMESTAMP(4),
  484. };
  485. static int ina2xx_work_buffer(struct iio_dev *indio_dev)
  486. {
  487. struct ina2xx_chip_info *chip = iio_priv(indio_dev);
  488. unsigned short data[8];
  489. int bit, ret, i = 0;
  490. s64 time_a, time_b;
  491. unsigned int alert;
  492. int cnvr_need_clear = 0;
  493. time_a = iio_get_time_ns(indio_dev);
  494. /*
  495. * Because the timer thread and the chip conversion clock
  496. * are asynchronous, the period difference will eventually
  497. * result in reading V[k-1] again, or skip V[k] at time Tk.
  498. * In order to resync the timer with the conversion process
  499. * we check the ConVersionReadyFlag.
  500. * On hardware that supports using the ALERT pin to toggle a
  501. * GPIO a triggered buffer could be used instead.
  502. * For now, we do an extra read of the MASK_ENABLE register (INA226)
  503. * resp. the BUS_VOLTAGE register (INA219).
  504. */
  505. if (!chip->allow_async_readout)
  506. do {
  507. if (chip->config->chip_id == ina226) {
  508. ret = regmap_read(chip->regmap,
  509. INA226_MASK_ENABLE, &alert);
  510. alert &= INA226_CVRF;
  511. } else {
  512. ret = regmap_read(chip->regmap,
  513. INA2XX_BUS_VOLTAGE, &alert);
  514. alert &= INA219_CNVR;
  515. cnvr_need_clear = alert;
  516. }
  517. if (ret < 0)
  518. return ret;
  519. } while (!alert);
  520. /*
  521. * Single register reads: bulk_read will not work with ina226/219
  522. * as there is no auto-increment of the register pointer.
  523. */
  524. for_each_set_bit(bit, indio_dev->active_scan_mask,
  525. indio_dev->masklength) {
  526. unsigned int val;
  527. ret = regmap_read(chip->regmap,
  528. INA2XX_SHUNT_VOLTAGE + bit, &val);
  529. if (ret < 0)
  530. return ret;
  531. data[i++] = val;
  532. if (INA2XX_SHUNT_VOLTAGE + bit == INA2XX_POWER)
  533. cnvr_need_clear = 0;
  534. }
  535. /* Dummy read on INA219 power register to clear CNVR flag */
  536. if (cnvr_need_clear && chip->config->chip_id == ina219) {
  537. unsigned int val;
  538. ret = regmap_read(chip->regmap, INA2XX_POWER, &val);
  539. if (ret < 0)
  540. return ret;
  541. }
  542. time_b = iio_get_time_ns(indio_dev);
  543. iio_push_to_buffers_with_timestamp(indio_dev,
  544. (unsigned int *)data, time_a);
  545. return (unsigned long)(time_b - time_a) / 1000;
  546. };
  547. static int ina2xx_capture_thread(void *data)
  548. {
  549. struct iio_dev *indio_dev = data;
  550. struct ina2xx_chip_info *chip = iio_priv(indio_dev);
  551. int sampling_us = SAMPLING_PERIOD(chip);
  552. int buffer_us;
  553. /*
  554. * Poll a bit faster than the chip internal Fs, in case
  555. * we wish to sync with the conversion ready flag.
  556. */
  557. if (!chip->allow_async_readout)
  558. sampling_us -= 200;
  559. do {
  560. buffer_us = ina2xx_work_buffer(indio_dev);
  561. if (buffer_us < 0)
  562. return buffer_us;
  563. if (sampling_us > buffer_us)
  564. udelay(sampling_us - buffer_us);
  565. } while (!kthread_should_stop());
  566. return 0;
  567. }
  568. static int ina2xx_buffer_enable(struct iio_dev *indio_dev)
  569. {
  570. struct ina2xx_chip_info *chip = iio_priv(indio_dev);
  571. unsigned int sampling_us = SAMPLING_PERIOD(chip);
  572. dev_dbg(&indio_dev->dev, "Enabling buffer w/ scan_mask %02x, freq = %d, avg =%u\n",
  573. (unsigned int)(*indio_dev->active_scan_mask),
  574. 1000000 / sampling_us, chip->avg);
  575. dev_dbg(&indio_dev->dev, "Expected work period: %u us\n", sampling_us);
  576. dev_dbg(&indio_dev->dev, "Async readout mode: %d\n",
  577. chip->allow_async_readout);
  578. chip->task = kthread_run(ina2xx_capture_thread, (void *)indio_dev,
  579. "%s:%d-%uus", indio_dev->name, indio_dev->id,
  580. sampling_us);
  581. return PTR_ERR_OR_ZERO(chip->task);
  582. }
  583. static int ina2xx_buffer_disable(struct iio_dev *indio_dev)
  584. {
  585. struct ina2xx_chip_info *chip = iio_priv(indio_dev);
  586. if (chip->task) {
  587. kthread_stop(chip->task);
  588. chip->task = NULL;
  589. }
  590. return 0;
  591. }
  592. static const struct iio_buffer_setup_ops ina2xx_setup_ops = {
  593. .postenable = &ina2xx_buffer_enable,
  594. .predisable = &ina2xx_buffer_disable,
  595. };
  596. static int ina2xx_debug_reg(struct iio_dev *indio_dev,
  597. unsigned reg, unsigned writeval, unsigned *readval)
  598. {
  599. struct ina2xx_chip_info *chip = iio_priv(indio_dev);
  600. if (!readval)
  601. return regmap_write(chip->regmap, reg, writeval);
  602. return regmap_read(chip->regmap, reg, readval);
  603. }
  604. /* Possible integration times for vshunt and vbus */
  605. static IIO_CONST_ATTR_NAMED(ina219_integration_time_available,
  606. integration_time_available,
  607. "0.000084 0.000148 0.000276 0.000532 0.001060 0.002130 0.004260 0.008510 0.017020 0.034050 0.068100");
  608. static IIO_CONST_ATTR_NAMED(ina226_integration_time_available,
  609. integration_time_available,
  610. "0.000140 0.000204 0.000332 0.000588 0.001100 0.002116 0.004156 0.008244");
  611. static IIO_DEVICE_ATTR(in_allow_async_readout, S_IRUGO | S_IWUSR,
  612. ina2xx_allow_async_readout_show,
  613. ina2xx_allow_async_readout_store, 0);
  614. static IIO_DEVICE_ATTR(in_shunt_resistor, S_IRUGO | S_IWUSR,
  615. ina2xx_shunt_resistor_show,
  616. ina2xx_shunt_resistor_store, 0);
  617. static struct attribute *ina219_attributes[] = {
  618. &iio_dev_attr_in_allow_async_readout.dev_attr.attr,
  619. &iio_const_attr_ina219_integration_time_available.dev_attr.attr,
  620. &iio_dev_attr_in_shunt_resistor.dev_attr.attr,
  621. NULL,
  622. };
  623. static struct attribute *ina226_attributes[] = {
  624. &iio_dev_attr_in_allow_async_readout.dev_attr.attr,
  625. &iio_const_attr_ina226_integration_time_available.dev_attr.attr,
  626. &iio_dev_attr_in_shunt_resistor.dev_attr.attr,
  627. NULL,
  628. };
  629. static const struct attribute_group ina219_attribute_group = {
  630. .attrs = ina219_attributes,
  631. };
  632. static const struct attribute_group ina226_attribute_group = {
  633. .attrs = ina226_attributes,
  634. };
  635. static const struct iio_info ina219_info = {
  636. .driver_module = THIS_MODULE,
  637. .attrs = &ina219_attribute_group,
  638. .read_raw = ina2xx_read_raw,
  639. .write_raw = ina2xx_write_raw,
  640. .debugfs_reg_access = ina2xx_debug_reg,
  641. };
  642. static const struct iio_info ina226_info = {
  643. .driver_module = THIS_MODULE,
  644. .attrs = &ina226_attribute_group,
  645. .read_raw = ina2xx_read_raw,
  646. .write_raw = ina2xx_write_raw,
  647. .debugfs_reg_access = ina2xx_debug_reg,
  648. };
  649. /* Initialize the configuration and calibration registers. */
  650. static int ina2xx_init(struct ina2xx_chip_info *chip, unsigned int config)
  651. {
  652. int ret = regmap_write(chip->regmap, INA2XX_CONFIG, config);
  653. if (ret)
  654. return ret;
  655. return ina2xx_set_calibration(chip);
  656. }
  657. static int ina2xx_probe(struct i2c_client *client,
  658. const struct i2c_device_id *id)
  659. {
  660. struct ina2xx_chip_info *chip;
  661. struct iio_dev *indio_dev;
  662. struct iio_buffer *buffer;
  663. unsigned int val;
  664. enum ina2xx_ids type;
  665. int ret;
  666. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
  667. if (!indio_dev)
  668. return -ENOMEM;
  669. chip = iio_priv(indio_dev);
  670. /* This is only used for device removal purposes. */
  671. i2c_set_clientdata(client, indio_dev);
  672. chip->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
  673. if (IS_ERR(chip->regmap)) {
  674. dev_err(&client->dev, "failed to allocate register map\n");
  675. return PTR_ERR(chip->regmap);
  676. }
  677. if (client->dev.of_node)
  678. type = (enum ina2xx_ids)of_device_get_match_data(&client->dev);
  679. else
  680. type = id->driver_data;
  681. chip->config = &ina2xx_config[type];
  682. mutex_init(&chip->state_lock);
  683. if (of_property_read_u32(client->dev.of_node,
  684. "shunt-resistor", &val) < 0) {
  685. struct ina2xx_platform_data *pdata =
  686. dev_get_platdata(&client->dev);
  687. if (pdata)
  688. val = pdata->shunt_uohms;
  689. else
  690. val = INA2XX_RSHUNT_DEFAULT;
  691. }
  692. ret = set_shunt_resistor(chip, val);
  693. if (ret)
  694. return ret;
  695. /* Patch the current config register with default. */
  696. val = chip->config->config_default;
  697. if (id->driver_data == ina226) {
  698. ina226_set_average(chip, INA226_DEFAULT_AVG, &val);
  699. ina226_set_int_time_vbus(chip, INA226_DEFAULT_IT, &val);
  700. ina226_set_int_time_vshunt(chip, INA226_DEFAULT_IT, &val);
  701. } else {
  702. chip->avg = 1;
  703. ina219_set_int_time_vbus(chip, INA219_DEFAULT_IT, &val);
  704. ina219_set_int_time_vshunt(chip, INA219_DEFAULT_IT, &val);
  705. }
  706. ret = ina2xx_init(chip, val);
  707. if (ret) {
  708. dev_err(&client->dev, "error configuring the device\n");
  709. return ret;
  710. }
  711. indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
  712. indio_dev->dev.parent = &client->dev;
  713. indio_dev->dev.of_node = client->dev.of_node;
  714. if (id->driver_data == ina226) {
  715. indio_dev->channels = ina226_channels;
  716. indio_dev->num_channels = ARRAY_SIZE(ina226_channels);
  717. indio_dev->info = &ina226_info;
  718. } else {
  719. indio_dev->channels = ina219_channels;
  720. indio_dev->num_channels = ARRAY_SIZE(ina219_channels);
  721. indio_dev->info = &ina219_info;
  722. }
  723. indio_dev->name = id->name;
  724. indio_dev->setup_ops = &ina2xx_setup_ops;
  725. buffer = devm_iio_kfifo_allocate(&indio_dev->dev);
  726. if (!buffer)
  727. return -ENOMEM;
  728. iio_device_attach_buffer(indio_dev, buffer);
  729. return iio_device_register(indio_dev);
  730. }
  731. static int ina2xx_remove(struct i2c_client *client)
  732. {
  733. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  734. struct ina2xx_chip_info *chip = iio_priv(indio_dev);
  735. iio_device_unregister(indio_dev);
  736. /* Powerdown */
  737. return regmap_update_bits(chip->regmap, INA2XX_CONFIG,
  738. INA2XX_MODE_MASK, 0);
  739. }
  740. static const struct i2c_device_id ina2xx_id[] = {
  741. {"ina219", ina219},
  742. {"ina220", ina219},
  743. {"ina226", ina226},
  744. {"ina230", ina226},
  745. {"ina231", ina226},
  746. {}
  747. };
  748. MODULE_DEVICE_TABLE(i2c, ina2xx_id);
  749. static const struct of_device_id ina2xx_of_match[] = {
  750. {
  751. .compatible = "ti,ina219",
  752. .data = (void *)ina219
  753. },
  754. {
  755. .compatible = "ti,ina220",
  756. .data = (void *)ina219
  757. },
  758. {
  759. .compatible = "ti,ina226",
  760. .data = (void *)ina226
  761. },
  762. {
  763. .compatible = "ti,ina230",
  764. .data = (void *)ina226
  765. },
  766. {
  767. .compatible = "ti,ina231",
  768. .data = (void *)ina226
  769. },
  770. {},
  771. };
  772. MODULE_DEVICE_TABLE(of, ina2xx_of_match);
  773. static struct i2c_driver ina2xx_driver = {
  774. .driver = {
  775. .name = KBUILD_MODNAME,
  776. .of_match_table = ina2xx_of_match,
  777. },
  778. .probe = ina2xx_probe,
  779. .remove = ina2xx_remove,
  780. .id_table = ina2xx_id,
  781. };
  782. module_i2c_driver(ina2xx_driver);
  783. MODULE_AUTHOR("Marc Titinger <marc.titinger@baylibre.com>");
  784. MODULE_DESCRIPTION("Texas Instruments INA2XX ADC driver");
  785. MODULE_LICENSE("GPL v2");