ina2xx-adc.c 19 KB

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