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