ina2xx.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for Texas Instruments INA219, INA226 power monitor chips
  4. *
  5. * INA219:
  6. * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface
  7. * Datasheet: https://www.ti.com/product/ina219
  8. *
  9. * INA220:
  10. * Bi-Directional Current/Power Monitor with I2C Interface
  11. * Datasheet: https://www.ti.com/product/ina220
  12. *
  13. * INA226:
  14. * Bi-Directional Current/Power Monitor with I2C Interface
  15. * Datasheet: https://www.ti.com/product/ina226
  16. *
  17. * INA230:
  18. * Bi-directional Current/Power Monitor with I2C Interface
  19. * Datasheet: https://www.ti.com/product/ina230
  20. *
  21. * Copyright (C) 2012 Lothar Felten <lothar.felten@gmail.com>
  22. * Thanks to Jan Volkering
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/err.h>
  28. #include <linux/slab.h>
  29. #include <linux/i2c.h>
  30. #include <linux/hwmon.h>
  31. #include <linux/hwmon-sysfs.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/of_device.h>
  34. #include <linux/of.h>
  35. #include <linux/delay.h>
  36. #include <linux/util_macros.h>
  37. #include <linux/regmap.h>
  38. #include <linux/platform_data/ina2xx.h>
  39. /* common register definitions */
  40. #define INA2XX_CONFIG 0x00
  41. #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
  42. #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
  43. #define INA2XX_POWER 0x03 /* readonly */
  44. #define INA2XX_CURRENT 0x04 /* readonly */
  45. #define INA2XX_CALIBRATION 0x05
  46. /* INA226 register definitions */
  47. #define INA226_MASK_ENABLE 0x06
  48. #define INA226_ALERT_LIMIT 0x07
  49. #define INA226_DIE_ID 0xFF
  50. /* register count */
  51. #define INA219_REGISTERS 6
  52. #define INA226_REGISTERS 8
  53. #define INA2XX_MAX_REGISTERS 8
  54. /* settings - depend on use case */
  55. #define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
  56. #define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
  57. /* worst case is 68.10 ms (~14.6Hz, ina219) */
  58. #define INA2XX_CONVERSION_RATE 15
  59. #define INA2XX_MAX_DELAY 69 /* worst case delay in ms */
  60. #define INA2XX_RSHUNT_DEFAULT 10000
  61. /* bit mask for reading the averaging setting in the configuration register */
  62. #define INA226_AVG_RD_MASK 0x0E00
  63. #define INA226_READ_AVG(reg) (((reg) & INA226_AVG_RD_MASK) >> 9)
  64. #define INA226_SHIFT_AVG(val) ((val) << 9)
  65. /* bit number of alert functions in Mask/Enable Register */
  66. #define INA226_SHUNT_OVER_VOLTAGE_BIT 15
  67. #define INA226_SHUNT_UNDER_VOLTAGE_BIT 14
  68. #define INA226_BUS_OVER_VOLTAGE_BIT 13
  69. #define INA226_BUS_UNDER_VOLTAGE_BIT 12
  70. #define INA226_POWER_OVER_LIMIT_BIT 11
  71. /* bit mask for alert config bits of Mask/Enable Register */
  72. #define INA226_ALERT_CONFIG_MASK 0xFC00
  73. #define INA226_ALERT_FUNCTION_FLAG BIT(4)
  74. /* common attrs, ina226 attrs and NULL */
  75. #define INA2XX_MAX_ATTRIBUTE_GROUPS 3
  76. /*
  77. * Both bus voltage and shunt voltage conversion times for ina226 are set
  78. * to 0b0100 on POR, which translates to 2200 microseconds in total.
  79. */
  80. #define INA226_TOTAL_CONV_TIME_DEFAULT 2200
  81. static struct regmap_config ina2xx_regmap_config = {
  82. .reg_bits = 8,
  83. .val_bits = 16,
  84. };
  85. enum ina2xx_ids { ina219, ina226 };
  86. struct ina2xx_config {
  87. u16 config_default;
  88. int calibration_value;
  89. int registers;
  90. int shunt_div;
  91. int bus_voltage_shift;
  92. int bus_voltage_lsb; /* uV */
  93. int power_lsb_factor;
  94. };
  95. struct ina2xx_data {
  96. const struct ina2xx_config *config;
  97. long rshunt;
  98. long current_lsb_uA;
  99. long power_lsb_uW;
  100. struct mutex config_lock;
  101. struct regmap *regmap;
  102. const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS];
  103. };
  104. static const struct ina2xx_config ina2xx_config[] = {
  105. [ina219] = {
  106. .config_default = INA219_CONFIG_DEFAULT,
  107. .calibration_value = 4096,
  108. .registers = INA219_REGISTERS,
  109. .shunt_div = 100,
  110. .bus_voltage_shift = 3,
  111. .bus_voltage_lsb = 4000,
  112. .power_lsb_factor = 20,
  113. },
  114. [ina226] = {
  115. .config_default = INA226_CONFIG_DEFAULT,
  116. .calibration_value = 2048,
  117. .registers = INA226_REGISTERS,
  118. .shunt_div = 400,
  119. .bus_voltage_shift = 0,
  120. .bus_voltage_lsb = 1250,
  121. .power_lsb_factor = 25,
  122. },
  123. };
  124. /*
  125. * Available averaging rates for ina226. The indices correspond with
  126. * the bit values expected by the chip (according to the ina226 datasheet,
  127. * table 3 AVG bit settings, found at
  128. * https://www.ti.com/lit/ds/symlink/ina226.pdf.
  129. */
  130. static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
  131. static int ina226_reg_to_interval(u16 config)
  132. {
  133. int avg = ina226_avg_tab[INA226_READ_AVG(config)];
  134. /*
  135. * Multiply the total conversion time by the number of averages.
  136. * Return the result in milliseconds.
  137. */
  138. return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000);
  139. }
  140. /*
  141. * Return the new, shifted AVG field value of CONFIG register,
  142. * to use with regmap_update_bits
  143. */
  144. static u16 ina226_interval_to_reg(int interval)
  145. {
  146. int avg, avg_bits;
  147. avg = DIV_ROUND_CLOSEST(interval * 1000,
  148. INA226_TOTAL_CONV_TIME_DEFAULT);
  149. avg_bits = find_closest(avg, ina226_avg_tab,
  150. ARRAY_SIZE(ina226_avg_tab));
  151. return INA226_SHIFT_AVG(avg_bits);
  152. }
  153. /*
  154. * Calibration register is set to the best value, which eliminates
  155. * truncation errors on calculating current register in hardware.
  156. * According to datasheet (eq. 3) the best values are 2048 for
  157. * ina226 and 4096 for ina219. They are hardcoded as calibration_value.
  158. */
  159. static int ina2xx_calibrate(struct ina2xx_data *data)
  160. {
  161. return regmap_write(data->regmap, INA2XX_CALIBRATION,
  162. data->config->calibration_value);
  163. }
  164. /*
  165. * Initialize the configuration and calibration registers.
  166. */
  167. static int ina2xx_init(struct ina2xx_data *data)
  168. {
  169. int ret = regmap_write(data->regmap, INA2XX_CONFIG,
  170. data->config->config_default);
  171. if (ret < 0)
  172. return ret;
  173. return ina2xx_calibrate(data);
  174. }
  175. static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval)
  176. {
  177. struct ina2xx_data *data = dev_get_drvdata(dev);
  178. int ret, retry;
  179. dev_dbg(dev, "Starting register %d read\n", reg);
  180. for (retry = 5; retry; retry--) {
  181. ret = regmap_read(data->regmap, reg, regval);
  182. if (ret < 0)
  183. return ret;
  184. dev_dbg(dev, "read %d, val = 0x%04x\n", reg, *regval);
  185. /*
  186. * If the current value in the calibration register is 0, the
  187. * power and current registers will also remain at 0. In case
  188. * the chip has been reset let's check the calibration
  189. * register and reinitialize if needed.
  190. * We do that extra read of the calibration register if there
  191. * is some hint of a chip reset.
  192. */
  193. if (*regval == 0) {
  194. unsigned int cal;
  195. ret = regmap_read(data->regmap, INA2XX_CALIBRATION,
  196. &cal);
  197. if (ret < 0)
  198. return ret;
  199. if (cal == 0) {
  200. dev_warn(dev, "chip not calibrated, reinitializing\n");
  201. ret = ina2xx_init(data);
  202. if (ret < 0)
  203. return ret;
  204. /*
  205. * Let's make sure the power and current
  206. * registers have been updated before trying
  207. * again.
  208. */
  209. msleep(INA2XX_MAX_DELAY);
  210. continue;
  211. }
  212. }
  213. return 0;
  214. }
  215. /*
  216. * If we're here then although all write operations succeeded, the
  217. * chip still returns 0 in the calibration register. Nothing more we
  218. * can do here.
  219. */
  220. dev_err(dev, "unable to reinitialize the chip\n");
  221. return -ENODEV;
  222. }
  223. static int ina2xx_get_value(struct ina2xx_data *data, u8 reg,
  224. unsigned int regval)
  225. {
  226. int val;
  227. switch (reg) {
  228. case INA2XX_SHUNT_VOLTAGE:
  229. /* signed register */
  230. val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div);
  231. break;
  232. case INA2XX_BUS_VOLTAGE:
  233. val = (regval >> data->config->bus_voltage_shift)
  234. * data->config->bus_voltage_lsb;
  235. val = DIV_ROUND_CLOSEST(val, 1000);
  236. break;
  237. case INA2XX_POWER:
  238. val = regval * data->power_lsb_uW;
  239. break;
  240. case INA2XX_CURRENT:
  241. /* signed register, result in mA */
  242. val = (s16)regval * data->current_lsb_uA;
  243. val = DIV_ROUND_CLOSEST(val, 1000);
  244. break;
  245. case INA2XX_CALIBRATION:
  246. val = regval;
  247. break;
  248. default:
  249. /* programmer goofed */
  250. WARN_ON_ONCE(1);
  251. val = 0;
  252. break;
  253. }
  254. return val;
  255. }
  256. static ssize_t ina2xx_value_show(struct device *dev,
  257. struct device_attribute *da, char *buf)
  258. {
  259. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  260. struct ina2xx_data *data = dev_get_drvdata(dev);
  261. unsigned int regval;
  262. int err = ina2xx_read_reg(dev, attr->index, &regval);
  263. if (err < 0)
  264. return err;
  265. return snprintf(buf, PAGE_SIZE, "%d\n",
  266. ina2xx_get_value(data, attr->index, regval));
  267. }
  268. static int ina226_reg_to_alert(struct ina2xx_data *data, u8 bit, u16 regval)
  269. {
  270. int reg;
  271. switch (bit) {
  272. case INA226_SHUNT_OVER_VOLTAGE_BIT:
  273. case INA226_SHUNT_UNDER_VOLTAGE_BIT:
  274. reg = INA2XX_SHUNT_VOLTAGE;
  275. break;
  276. case INA226_BUS_OVER_VOLTAGE_BIT:
  277. case INA226_BUS_UNDER_VOLTAGE_BIT:
  278. reg = INA2XX_BUS_VOLTAGE;
  279. break;
  280. case INA226_POWER_OVER_LIMIT_BIT:
  281. reg = INA2XX_POWER;
  282. break;
  283. default:
  284. /* programmer goofed */
  285. WARN_ON_ONCE(1);
  286. return 0;
  287. }
  288. return ina2xx_get_value(data, reg, regval);
  289. }
  290. /*
  291. * Turns alert limit values into register values.
  292. * Opposite of the formula in ina2xx_get_value().
  293. */
  294. static s16 ina226_alert_to_reg(struct ina2xx_data *data, u8 bit, int val)
  295. {
  296. switch (bit) {
  297. case INA226_SHUNT_OVER_VOLTAGE_BIT:
  298. case INA226_SHUNT_UNDER_VOLTAGE_BIT:
  299. val *= data->config->shunt_div;
  300. return clamp_val(val, SHRT_MIN, SHRT_MAX);
  301. case INA226_BUS_OVER_VOLTAGE_BIT:
  302. case INA226_BUS_UNDER_VOLTAGE_BIT:
  303. val = (val * 1000) << data->config->bus_voltage_shift;
  304. val = DIV_ROUND_CLOSEST(val, data->config->bus_voltage_lsb);
  305. return clamp_val(val, 0, SHRT_MAX);
  306. case INA226_POWER_OVER_LIMIT_BIT:
  307. val = DIV_ROUND_CLOSEST(val, data->power_lsb_uW);
  308. return clamp_val(val, 0, USHRT_MAX);
  309. default:
  310. /* programmer goofed */
  311. WARN_ON_ONCE(1);
  312. return 0;
  313. }
  314. }
  315. static ssize_t ina226_alert_show(struct device *dev,
  316. struct device_attribute *da, char *buf)
  317. {
  318. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  319. struct ina2xx_data *data = dev_get_drvdata(dev);
  320. int regval;
  321. int val = 0;
  322. int ret;
  323. mutex_lock(&data->config_lock);
  324. ret = regmap_read(data->regmap, INA226_MASK_ENABLE, &regval);
  325. if (ret)
  326. goto abort;
  327. if (regval & BIT(attr->index)) {
  328. ret = regmap_read(data->regmap, INA226_ALERT_LIMIT, &regval);
  329. if (ret)
  330. goto abort;
  331. val = ina226_reg_to_alert(data, attr->index, regval);
  332. }
  333. ret = snprintf(buf, PAGE_SIZE, "%d\n", val);
  334. abort:
  335. mutex_unlock(&data->config_lock);
  336. return ret;
  337. }
  338. static ssize_t ina226_alert_store(struct device *dev,
  339. struct device_attribute *da,
  340. const char *buf, size_t count)
  341. {
  342. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  343. struct ina2xx_data *data = dev_get_drvdata(dev);
  344. unsigned long val;
  345. int ret;
  346. ret = kstrtoul(buf, 10, &val);
  347. if (ret < 0)
  348. return ret;
  349. /*
  350. * Clear all alerts first to avoid accidentally triggering ALERT pin
  351. * due to register write sequence. Then, only enable the alert
  352. * if the value is non-zero.
  353. */
  354. mutex_lock(&data->config_lock);
  355. ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE,
  356. INA226_ALERT_CONFIG_MASK, 0);
  357. if (ret < 0)
  358. goto abort;
  359. ret = regmap_write(data->regmap, INA226_ALERT_LIMIT,
  360. ina226_alert_to_reg(data, attr->index, val));
  361. if (ret < 0)
  362. goto abort;
  363. if (val != 0) {
  364. ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE,
  365. INA226_ALERT_CONFIG_MASK,
  366. BIT(attr->index));
  367. if (ret < 0)
  368. goto abort;
  369. }
  370. ret = count;
  371. abort:
  372. mutex_unlock(&data->config_lock);
  373. return ret;
  374. }
  375. static ssize_t ina226_alarm_show(struct device *dev,
  376. struct device_attribute *da, char *buf)
  377. {
  378. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  379. struct ina2xx_data *data = dev_get_drvdata(dev);
  380. int regval;
  381. int alarm = 0;
  382. int ret;
  383. ret = regmap_read(data->regmap, INA226_MASK_ENABLE, &regval);
  384. if (ret)
  385. return ret;
  386. alarm = (regval & BIT(attr->index)) &&
  387. (regval & INA226_ALERT_FUNCTION_FLAG);
  388. return snprintf(buf, PAGE_SIZE, "%d\n", alarm);
  389. }
  390. /*
  391. * In order to keep calibration register value fixed, the product
  392. * of current_lsb and shunt_resistor should also be fixed and equal
  393. * to shunt_voltage_lsb = 1 / shunt_div multiplied by 10^9 in order
  394. * to keep the scale.
  395. */
  396. static int ina2xx_set_shunt(struct ina2xx_data *data, long val)
  397. {
  398. unsigned int dividend = DIV_ROUND_CLOSEST(1000000000,
  399. data->config->shunt_div);
  400. if (val <= 0 || val > dividend)
  401. return -EINVAL;
  402. mutex_lock(&data->config_lock);
  403. data->rshunt = val;
  404. data->current_lsb_uA = DIV_ROUND_CLOSEST(dividend, val);
  405. data->power_lsb_uW = data->config->power_lsb_factor *
  406. data->current_lsb_uA;
  407. mutex_unlock(&data->config_lock);
  408. return 0;
  409. }
  410. static ssize_t ina2xx_shunt_show(struct device *dev,
  411. struct device_attribute *da, char *buf)
  412. {
  413. struct ina2xx_data *data = dev_get_drvdata(dev);
  414. return snprintf(buf, PAGE_SIZE, "%li\n", data->rshunt);
  415. }
  416. static ssize_t ina2xx_shunt_store(struct device *dev,
  417. struct device_attribute *da,
  418. const char *buf, size_t count)
  419. {
  420. unsigned long val;
  421. int status;
  422. struct ina2xx_data *data = dev_get_drvdata(dev);
  423. status = kstrtoul(buf, 10, &val);
  424. if (status < 0)
  425. return status;
  426. status = ina2xx_set_shunt(data, val);
  427. if (status < 0)
  428. return status;
  429. return count;
  430. }
  431. static ssize_t ina226_interval_store(struct device *dev,
  432. struct device_attribute *da,
  433. const char *buf, size_t count)
  434. {
  435. struct ina2xx_data *data = dev_get_drvdata(dev);
  436. unsigned long val;
  437. int status;
  438. status = kstrtoul(buf, 10, &val);
  439. if (status < 0)
  440. return status;
  441. if (val > INT_MAX || val == 0)
  442. return -EINVAL;
  443. status = regmap_update_bits(data->regmap, INA2XX_CONFIG,
  444. INA226_AVG_RD_MASK,
  445. ina226_interval_to_reg(val));
  446. if (status < 0)
  447. return status;
  448. return count;
  449. }
  450. static ssize_t ina226_interval_show(struct device *dev,
  451. struct device_attribute *da, char *buf)
  452. {
  453. struct ina2xx_data *data = dev_get_drvdata(dev);
  454. int status;
  455. unsigned int regval;
  456. status = regmap_read(data->regmap, INA2XX_CONFIG, &regval);
  457. if (status)
  458. return status;
  459. return snprintf(buf, PAGE_SIZE, "%d\n", ina226_reg_to_interval(regval));
  460. }
  461. /* shunt voltage */
  462. static SENSOR_DEVICE_ATTR_RO(in0_input, ina2xx_value, INA2XX_SHUNT_VOLTAGE);
  463. /* shunt voltage over/under voltage alert setting and alarm */
  464. static SENSOR_DEVICE_ATTR_RW(in0_crit, ina226_alert,
  465. INA226_SHUNT_OVER_VOLTAGE_BIT);
  466. static SENSOR_DEVICE_ATTR_RW(in0_lcrit, ina226_alert,
  467. INA226_SHUNT_UNDER_VOLTAGE_BIT);
  468. static SENSOR_DEVICE_ATTR_RO(in0_crit_alarm, ina226_alarm,
  469. INA226_SHUNT_OVER_VOLTAGE_BIT);
  470. static SENSOR_DEVICE_ATTR_RO(in0_lcrit_alarm, ina226_alarm,
  471. INA226_SHUNT_UNDER_VOLTAGE_BIT);
  472. /* bus voltage */
  473. static SENSOR_DEVICE_ATTR_RO(in1_input, ina2xx_value, INA2XX_BUS_VOLTAGE);
  474. /* bus voltage over/under voltage alert setting and alarm */
  475. static SENSOR_DEVICE_ATTR_RW(in1_crit, ina226_alert,
  476. INA226_BUS_OVER_VOLTAGE_BIT);
  477. static SENSOR_DEVICE_ATTR_RW(in1_lcrit, ina226_alert,
  478. INA226_BUS_UNDER_VOLTAGE_BIT);
  479. static SENSOR_DEVICE_ATTR_RO(in1_crit_alarm, ina226_alarm,
  480. INA226_BUS_OVER_VOLTAGE_BIT);
  481. static SENSOR_DEVICE_ATTR_RO(in1_lcrit_alarm, ina226_alarm,
  482. INA226_BUS_UNDER_VOLTAGE_BIT);
  483. /* calculated current */
  484. static SENSOR_DEVICE_ATTR_RO(curr1_input, ina2xx_value, INA2XX_CURRENT);
  485. /* calculated power */
  486. static SENSOR_DEVICE_ATTR_RO(power1_input, ina2xx_value, INA2XX_POWER);
  487. /* over-limit power alert setting and alarm */
  488. static SENSOR_DEVICE_ATTR_RW(power1_crit, ina226_alert,
  489. INA226_POWER_OVER_LIMIT_BIT);
  490. static SENSOR_DEVICE_ATTR_RO(power1_crit_alarm, ina226_alarm,
  491. INA226_POWER_OVER_LIMIT_BIT);
  492. /* shunt resistance */
  493. static SENSOR_DEVICE_ATTR_RW(shunt_resistor, ina2xx_shunt, INA2XX_CALIBRATION);
  494. /* update interval (ina226 only) */
  495. static SENSOR_DEVICE_ATTR_RW(update_interval, ina226_interval, 0);
  496. /* pointers to created device attributes */
  497. static struct attribute *ina2xx_attrs[] = {
  498. &sensor_dev_attr_in0_input.dev_attr.attr,
  499. &sensor_dev_attr_in1_input.dev_attr.attr,
  500. &sensor_dev_attr_curr1_input.dev_attr.attr,
  501. &sensor_dev_attr_power1_input.dev_attr.attr,
  502. &sensor_dev_attr_shunt_resistor.dev_attr.attr,
  503. NULL,
  504. };
  505. static const struct attribute_group ina2xx_group = {
  506. .attrs = ina2xx_attrs,
  507. };
  508. static struct attribute *ina226_attrs[] = {
  509. &sensor_dev_attr_in0_crit.dev_attr.attr,
  510. &sensor_dev_attr_in0_lcrit.dev_attr.attr,
  511. &sensor_dev_attr_in0_crit_alarm.dev_attr.attr,
  512. &sensor_dev_attr_in0_lcrit_alarm.dev_attr.attr,
  513. &sensor_dev_attr_in1_crit.dev_attr.attr,
  514. &sensor_dev_attr_in1_lcrit.dev_attr.attr,
  515. &sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
  516. &sensor_dev_attr_in1_lcrit_alarm.dev_attr.attr,
  517. &sensor_dev_attr_power1_crit.dev_attr.attr,
  518. &sensor_dev_attr_power1_crit_alarm.dev_attr.attr,
  519. &sensor_dev_attr_update_interval.dev_attr.attr,
  520. NULL,
  521. };
  522. static const struct attribute_group ina226_group = {
  523. .attrs = ina226_attrs,
  524. };
  525. static const struct i2c_device_id ina2xx_id[];
  526. static int ina2xx_probe(struct i2c_client *client)
  527. {
  528. struct device *dev = &client->dev;
  529. struct ina2xx_data *data;
  530. struct device *hwmon_dev;
  531. u32 val;
  532. int ret, group = 0;
  533. enum ina2xx_ids chip;
  534. if (client->dev.of_node)
  535. chip = (enum ina2xx_ids)of_device_get_match_data(&client->dev);
  536. else
  537. chip = i2c_match_id(ina2xx_id, client)->driver_data;
  538. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  539. if (!data)
  540. return -ENOMEM;
  541. /* set the device type */
  542. data->config = &ina2xx_config[chip];
  543. mutex_init(&data->config_lock);
  544. if (of_property_read_u32(dev->of_node, "shunt-resistor", &val) < 0) {
  545. struct ina2xx_platform_data *pdata = dev_get_platdata(dev);
  546. if (pdata)
  547. val = pdata->shunt_uohms;
  548. else
  549. val = INA2XX_RSHUNT_DEFAULT;
  550. }
  551. ina2xx_set_shunt(data, val);
  552. ina2xx_regmap_config.max_register = data->config->registers;
  553. data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
  554. if (IS_ERR(data->regmap)) {
  555. dev_err(dev, "failed to allocate register map\n");
  556. return PTR_ERR(data->regmap);
  557. }
  558. ret = ina2xx_init(data);
  559. if (ret < 0) {
  560. dev_err(dev, "error configuring the device: %d\n", ret);
  561. return -ENODEV;
  562. }
  563. data->groups[group++] = &ina2xx_group;
  564. if (chip == ina226)
  565. data->groups[group++] = &ina226_group;
  566. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  567. data, data->groups);
  568. if (IS_ERR(hwmon_dev))
  569. return PTR_ERR(hwmon_dev);
  570. dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
  571. client->name, data->rshunt);
  572. return 0;
  573. }
  574. static const struct i2c_device_id ina2xx_id[] = {
  575. { "ina219", ina219 },
  576. { "ina220", ina219 },
  577. { "ina226", ina226 },
  578. { "ina230", ina226 },
  579. { "ina231", ina226 },
  580. { }
  581. };
  582. MODULE_DEVICE_TABLE(i2c, ina2xx_id);
  583. static const struct of_device_id __maybe_unused ina2xx_of_match[] = {
  584. {
  585. .compatible = "ti,ina219",
  586. .data = (void *)ina219
  587. },
  588. {
  589. .compatible = "ti,ina220",
  590. .data = (void *)ina219
  591. },
  592. {
  593. .compatible = "ti,ina226",
  594. .data = (void *)ina226
  595. },
  596. {
  597. .compatible = "ti,ina230",
  598. .data = (void *)ina226
  599. },
  600. {
  601. .compatible = "ti,ina231",
  602. .data = (void *)ina226
  603. },
  604. { },
  605. };
  606. MODULE_DEVICE_TABLE(of, ina2xx_of_match);
  607. static struct i2c_driver ina2xx_driver = {
  608. .driver = {
  609. .name = "ina2xx",
  610. .of_match_table = of_match_ptr(ina2xx_of_match),
  611. },
  612. .probe_new = ina2xx_probe,
  613. .id_table = ina2xx_id,
  614. };
  615. module_i2c_driver(ina2xx_driver);
  616. MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
  617. MODULE_DESCRIPTION("ina2xx driver");
  618. MODULE_LICENSE("GPL");