ina2xx.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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 <[email protected]>
  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 sysfs_emit(buf, "%d\n", ina2xx_get_value(data, attr->index, regval));
  266. }
  267. static int ina226_reg_to_alert(struct ina2xx_data *data, u8 bit, u16 regval)
  268. {
  269. int reg;
  270. switch (bit) {
  271. case INA226_SHUNT_OVER_VOLTAGE_BIT:
  272. case INA226_SHUNT_UNDER_VOLTAGE_BIT:
  273. reg = INA2XX_SHUNT_VOLTAGE;
  274. break;
  275. case INA226_BUS_OVER_VOLTAGE_BIT:
  276. case INA226_BUS_UNDER_VOLTAGE_BIT:
  277. reg = INA2XX_BUS_VOLTAGE;
  278. break;
  279. case INA226_POWER_OVER_LIMIT_BIT:
  280. reg = INA2XX_POWER;
  281. break;
  282. default:
  283. /* programmer goofed */
  284. WARN_ON_ONCE(1);
  285. return 0;
  286. }
  287. return ina2xx_get_value(data, reg, regval);
  288. }
  289. /*
  290. * Turns alert limit values into register values.
  291. * Opposite of the formula in ina2xx_get_value().
  292. */
  293. static s16 ina226_alert_to_reg(struct ina2xx_data *data, u8 bit, int val)
  294. {
  295. switch (bit) {
  296. case INA226_SHUNT_OVER_VOLTAGE_BIT:
  297. case INA226_SHUNT_UNDER_VOLTAGE_BIT:
  298. val *= data->config->shunt_div;
  299. return clamp_val(val, SHRT_MIN, SHRT_MAX);
  300. case INA226_BUS_OVER_VOLTAGE_BIT:
  301. case INA226_BUS_UNDER_VOLTAGE_BIT:
  302. val = (val * 1000) << data->config->bus_voltage_shift;
  303. val = DIV_ROUND_CLOSEST(val, data->config->bus_voltage_lsb);
  304. return clamp_val(val, 0, SHRT_MAX);
  305. case INA226_POWER_OVER_LIMIT_BIT:
  306. val = DIV_ROUND_CLOSEST(val, data->power_lsb_uW);
  307. return clamp_val(val, 0, USHRT_MAX);
  308. default:
  309. /* programmer goofed */
  310. WARN_ON_ONCE(1);
  311. return 0;
  312. }
  313. }
  314. static ssize_t ina226_alert_show(struct device *dev,
  315. struct device_attribute *da, char *buf)
  316. {
  317. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  318. struct ina2xx_data *data = dev_get_drvdata(dev);
  319. int regval;
  320. int val = 0;
  321. int ret;
  322. mutex_lock(&data->config_lock);
  323. ret = regmap_read(data->regmap, INA226_MASK_ENABLE, &regval);
  324. if (ret)
  325. goto abort;
  326. if (regval & BIT(attr->index)) {
  327. ret = regmap_read(data->regmap, INA226_ALERT_LIMIT, &regval);
  328. if (ret)
  329. goto abort;
  330. val = ina226_reg_to_alert(data, attr->index, regval);
  331. }
  332. ret = sysfs_emit(buf, "%d\n", val);
  333. abort:
  334. mutex_unlock(&data->config_lock);
  335. return ret;
  336. }
  337. static ssize_t ina226_alert_store(struct device *dev,
  338. struct device_attribute *da,
  339. const char *buf, size_t count)
  340. {
  341. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  342. struct ina2xx_data *data = dev_get_drvdata(dev);
  343. unsigned long val;
  344. int ret;
  345. ret = kstrtoul(buf, 10, &val);
  346. if (ret < 0)
  347. return ret;
  348. /*
  349. * Clear all alerts first to avoid accidentally triggering ALERT pin
  350. * due to register write sequence. Then, only enable the alert
  351. * if the value is non-zero.
  352. */
  353. mutex_lock(&data->config_lock);
  354. ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE,
  355. INA226_ALERT_CONFIG_MASK, 0);
  356. if (ret < 0)
  357. goto abort;
  358. ret = regmap_write(data->regmap, INA226_ALERT_LIMIT,
  359. ina226_alert_to_reg(data, attr->index, val));
  360. if (ret < 0)
  361. goto abort;
  362. if (val != 0) {
  363. ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE,
  364. INA226_ALERT_CONFIG_MASK,
  365. BIT(attr->index));
  366. if (ret < 0)
  367. goto abort;
  368. }
  369. ret = count;
  370. abort:
  371. mutex_unlock(&data->config_lock);
  372. return ret;
  373. }
  374. static ssize_t ina226_alarm_show(struct device *dev,
  375. struct device_attribute *da, char *buf)
  376. {
  377. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  378. struct ina2xx_data *data = dev_get_drvdata(dev);
  379. int regval;
  380. int alarm = 0;
  381. int ret;
  382. ret = regmap_read(data->regmap, INA226_MASK_ENABLE, &regval);
  383. if (ret)
  384. return ret;
  385. alarm = (regval & BIT(attr->index)) &&
  386. (regval & INA226_ALERT_FUNCTION_FLAG);
  387. return sysfs_emit(buf, "%d\n", alarm);
  388. }
  389. /*
  390. * In order to keep calibration register value fixed, the product
  391. * of current_lsb and shunt_resistor should also be fixed and equal
  392. * to shunt_voltage_lsb = 1 / shunt_div multiplied by 10^9 in order
  393. * to keep the scale.
  394. */
  395. static int ina2xx_set_shunt(struct ina2xx_data *data, long val)
  396. {
  397. unsigned int dividend = DIV_ROUND_CLOSEST(1000000000,
  398. data->config->shunt_div);
  399. if (val <= 0 || val > dividend)
  400. return -EINVAL;
  401. mutex_lock(&data->config_lock);
  402. data->rshunt = val;
  403. data->current_lsb_uA = DIV_ROUND_CLOSEST(dividend, val);
  404. data->power_lsb_uW = data->config->power_lsb_factor *
  405. data->current_lsb_uA;
  406. mutex_unlock(&data->config_lock);
  407. return 0;
  408. }
  409. static ssize_t ina2xx_shunt_show(struct device *dev,
  410. struct device_attribute *da, char *buf)
  411. {
  412. struct ina2xx_data *data = dev_get_drvdata(dev);
  413. return sysfs_emit(buf, "%li\n", data->rshunt);
  414. }
  415. static ssize_t ina2xx_shunt_store(struct device *dev,
  416. struct device_attribute *da,
  417. const char *buf, size_t count)
  418. {
  419. unsigned long val;
  420. int status;
  421. struct ina2xx_data *data = dev_get_drvdata(dev);
  422. status = kstrtoul(buf, 10, &val);
  423. if (status < 0)
  424. return status;
  425. status = ina2xx_set_shunt(data, val);
  426. if (status < 0)
  427. return status;
  428. return count;
  429. }
  430. static ssize_t ina226_interval_store(struct device *dev,
  431. struct device_attribute *da,
  432. const char *buf, size_t count)
  433. {
  434. struct ina2xx_data *data = dev_get_drvdata(dev);
  435. unsigned long val;
  436. int status;
  437. status = kstrtoul(buf, 10, &val);
  438. if (status < 0)
  439. return status;
  440. if (val > INT_MAX || val == 0)
  441. return -EINVAL;
  442. status = regmap_update_bits(data->regmap, INA2XX_CONFIG,
  443. INA226_AVG_RD_MASK,
  444. ina226_interval_to_reg(val));
  445. if (status < 0)
  446. return status;
  447. return count;
  448. }
  449. static ssize_t ina226_interval_show(struct device *dev,
  450. struct device_attribute *da, char *buf)
  451. {
  452. struct ina2xx_data *data = dev_get_drvdata(dev);
  453. int status;
  454. unsigned int regval;
  455. status = regmap_read(data->regmap, INA2XX_CONFIG, &regval);
  456. if (status)
  457. return status;
  458. return sysfs_emit(buf, "%d\n", ina226_reg_to_interval(regval));
  459. }
  460. /* shunt voltage */
  461. static SENSOR_DEVICE_ATTR_RO(in0_input, ina2xx_value, INA2XX_SHUNT_VOLTAGE);
  462. /* shunt voltage over/under voltage alert setting and alarm */
  463. static SENSOR_DEVICE_ATTR_RW(in0_crit, ina226_alert,
  464. INA226_SHUNT_OVER_VOLTAGE_BIT);
  465. static SENSOR_DEVICE_ATTR_RW(in0_lcrit, ina226_alert,
  466. INA226_SHUNT_UNDER_VOLTAGE_BIT);
  467. static SENSOR_DEVICE_ATTR_RO(in0_crit_alarm, ina226_alarm,
  468. INA226_SHUNT_OVER_VOLTAGE_BIT);
  469. static SENSOR_DEVICE_ATTR_RO(in0_lcrit_alarm, ina226_alarm,
  470. INA226_SHUNT_UNDER_VOLTAGE_BIT);
  471. /* bus voltage */
  472. static SENSOR_DEVICE_ATTR_RO(in1_input, ina2xx_value, INA2XX_BUS_VOLTAGE);
  473. /* bus voltage over/under voltage alert setting and alarm */
  474. static SENSOR_DEVICE_ATTR_RW(in1_crit, ina226_alert,
  475. INA226_BUS_OVER_VOLTAGE_BIT);
  476. static SENSOR_DEVICE_ATTR_RW(in1_lcrit, ina226_alert,
  477. INA226_BUS_UNDER_VOLTAGE_BIT);
  478. static SENSOR_DEVICE_ATTR_RO(in1_crit_alarm, ina226_alarm,
  479. INA226_BUS_OVER_VOLTAGE_BIT);
  480. static SENSOR_DEVICE_ATTR_RO(in1_lcrit_alarm, ina226_alarm,
  481. INA226_BUS_UNDER_VOLTAGE_BIT);
  482. /* calculated current */
  483. static SENSOR_DEVICE_ATTR_RO(curr1_input, ina2xx_value, INA2XX_CURRENT);
  484. /* calculated power */
  485. static SENSOR_DEVICE_ATTR_RO(power1_input, ina2xx_value, INA2XX_POWER);
  486. /* over-limit power alert setting and alarm */
  487. static SENSOR_DEVICE_ATTR_RW(power1_crit, ina226_alert,
  488. INA226_POWER_OVER_LIMIT_BIT);
  489. static SENSOR_DEVICE_ATTR_RO(power1_crit_alarm, ina226_alarm,
  490. INA226_POWER_OVER_LIMIT_BIT);
  491. /* shunt resistance */
  492. static SENSOR_DEVICE_ATTR_RW(shunt_resistor, ina2xx_shunt, INA2XX_CALIBRATION);
  493. /* update interval (ina226 only) */
  494. static SENSOR_DEVICE_ATTR_RW(update_interval, ina226_interval, 0);
  495. /* pointers to created device attributes */
  496. static struct attribute *ina2xx_attrs[] = {
  497. &sensor_dev_attr_in0_input.dev_attr.attr,
  498. &sensor_dev_attr_in1_input.dev_attr.attr,
  499. &sensor_dev_attr_curr1_input.dev_attr.attr,
  500. &sensor_dev_attr_power1_input.dev_attr.attr,
  501. &sensor_dev_attr_shunt_resistor.dev_attr.attr,
  502. NULL,
  503. };
  504. static const struct attribute_group ina2xx_group = {
  505. .attrs = ina2xx_attrs,
  506. };
  507. static struct attribute *ina226_attrs[] = {
  508. &sensor_dev_attr_in0_crit.dev_attr.attr,
  509. &sensor_dev_attr_in0_lcrit.dev_attr.attr,
  510. &sensor_dev_attr_in0_crit_alarm.dev_attr.attr,
  511. &sensor_dev_attr_in0_lcrit_alarm.dev_attr.attr,
  512. &sensor_dev_attr_in1_crit.dev_attr.attr,
  513. &sensor_dev_attr_in1_lcrit.dev_attr.attr,
  514. &sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
  515. &sensor_dev_attr_in1_lcrit_alarm.dev_attr.attr,
  516. &sensor_dev_attr_power1_crit.dev_attr.attr,
  517. &sensor_dev_attr_power1_crit_alarm.dev_attr.attr,
  518. &sensor_dev_attr_update_interval.dev_attr.attr,
  519. NULL,
  520. };
  521. static const struct attribute_group ina226_group = {
  522. .attrs = ina226_attrs,
  523. };
  524. static const struct i2c_device_id ina2xx_id[];
  525. static int ina2xx_probe(struct i2c_client *client)
  526. {
  527. struct device *dev = &client->dev;
  528. struct ina2xx_data *data;
  529. struct device *hwmon_dev;
  530. u32 val;
  531. int ret, group = 0;
  532. enum ina2xx_ids chip;
  533. if (client->dev.of_node)
  534. chip = (enum ina2xx_ids)of_device_get_match_data(&client->dev);
  535. else
  536. chip = i2c_match_id(ina2xx_id, client)->driver_data;
  537. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  538. if (!data)
  539. return -ENOMEM;
  540. /* set the device type */
  541. data->config = &ina2xx_config[chip];
  542. mutex_init(&data->config_lock);
  543. if (of_property_read_u32(dev->of_node, "shunt-resistor", &val) < 0) {
  544. struct ina2xx_platform_data *pdata = dev_get_platdata(dev);
  545. if (pdata)
  546. val = pdata->shunt_uohms;
  547. else
  548. val = INA2XX_RSHUNT_DEFAULT;
  549. }
  550. ina2xx_set_shunt(data, val);
  551. ina2xx_regmap_config.max_register = data->config->registers;
  552. data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
  553. if (IS_ERR(data->regmap)) {
  554. dev_err(dev, "failed to allocate register map\n");
  555. return PTR_ERR(data->regmap);
  556. }
  557. ret = ina2xx_init(data);
  558. if (ret < 0) {
  559. dev_err(dev, "error configuring the device: %d\n", ret);
  560. return -ENODEV;
  561. }
  562. data->groups[group++] = &ina2xx_group;
  563. if (chip == ina226)
  564. data->groups[group++] = &ina226_group;
  565. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  566. data, data->groups);
  567. if (IS_ERR(hwmon_dev))
  568. return PTR_ERR(hwmon_dev);
  569. dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
  570. client->name, data->rshunt);
  571. return 0;
  572. }
  573. static const struct i2c_device_id ina2xx_id[] = {
  574. { "ina219", ina219 },
  575. { "ina220", ina219 },
  576. { "ina226", ina226 },
  577. { "ina230", ina226 },
  578. { "ina231", ina226 },
  579. { }
  580. };
  581. MODULE_DEVICE_TABLE(i2c, ina2xx_id);
  582. static const struct of_device_id __maybe_unused ina2xx_of_match[] = {
  583. {
  584. .compatible = "ti,ina219",
  585. .data = (void *)ina219
  586. },
  587. {
  588. .compatible = "ti,ina220",
  589. .data = (void *)ina219
  590. },
  591. {
  592. .compatible = "ti,ina226",
  593. .data = (void *)ina226
  594. },
  595. {
  596. .compatible = "ti,ina230",
  597. .data = (void *)ina226
  598. },
  599. {
  600. .compatible = "ti,ina231",
  601. .data = (void *)ina226
  602. },
  603. { },
  604. };
  605. MODULE_DEVICE_TABLE(of, ina2xx_of_match);
  606. static struct i2c_driver ina2xx_driver = {
  607. .driver = {
  608. .name = "ina2xx",
  609. .of_match_table = of_match_ptr(ina2xx_of_match),
  610. },
  611. .probe_new = ina2xx_probe,
  612. .id_table = ina2xx_id,
  613. };
  614. module_i2c_driver(ina2xx_driver);
  615. MODULE_AUTHOR("Lothar Felten <[email protected]>");
  616. MODULE_DESCRIPTION("ina2xx driver");
  617. MODULE_LICENSE("GPL");