1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * axp288_fuel_gauge.c - Xpower AXP288 PMIC Fuel Gauge Driver 4 * 5 * Copyright (C) 2020-2021 Andrejus Basovas <xxx@yyy.tld> 6 * Copyright (C) 2016-2021 Hans de Goede <hdegoede@redhat.com> 7 * Copyright (C) 2014 Intel Corporation 8 * 9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 10 */ 11 12 #include <linux/acpi.h> 13 #include <linux/dmi.h> 14 #include <linux/module.h> 15 #include <linux/kernel.h> 16 #include <linux/device.h> 17 #include <linux/regmap.h> 18 #include <linux/jiffies.h> 19 #include <linux/interrupt.h> 20 #include <linux/mfd/axp20x.h> 21 #include <linux/platform_device.h> 22 #include <linux/power_supply.h> 23 #include <linux/iio/consumer.h> 24 #include <linux/unaligned.h> 25 #include <asm/iosf_mbi.h> 26 27 #define PS_STAT_VBUS_TRIGGER (1 << 0) 28 #define PS_STAT_BAT_CHRG_DIR (1 << 2) 29 #define PS_STAT_VBAT_ABOVE_VHOLD (1 << 3) 30 #define PS_STAT_VBUS_VALID (1 << 4) 31 #define PS_STAT_VBUS_PRESENT (1 << 5) 32 33 #define CHRG_STAT_BAT_SAFE_MODE (1 << 3) 34 #define CHRG_STAT_BAT_VALID (1 << 4) 35 #define CHRG_STAT_BAT_PRESENT (1 << 5) 36 #define CHRG_STAT_CHARGING (1 << 6) 37 #define CHRG_STAT_PMIC_OTP (1 << 7) 38 39 #define CHRG_CCCV_CC_MASK 0xf /* 4 bits */ 40 #define CHRG_CCCV_CC_BIT_POS 0 41 #define CHRG_CCCV_CC_OFFSET 200 /* 200mA */ 42 #define CHRG_CCCV_CC_LSB_RES 200 /* 200mA */ 43 #define CHRG_CCCV_ITERM_20P (1 << 4) /* 20% of CC */ 44 #define CHRG_CCCV_CV_MASK 0x60 /* 2 bits */ 45 #define CHRG_CCCV_CV_BIT_POS 5 46 #define CHRG_CCCV_CV_4100MV 0x0 /* 4.10V */ 47 #define CHRG_CCCV_CV_4150MV 0x1 /* 4.15V */ 48 #define CHRG_CCCV_CV_4200MV 0x2 /* 4.20V */ 49 #define CHRG_CCCV_CV_4350MV 0x3 /* 4.35V */ 50 #define CHRG_CCCV_CHG_EN (1 << 7) 51 52 #define FG_CNTL_OCV_ADJ_STAT (1 << 2) 53 #define FG_CNTL_OCV_ADJ_EN (1 << 3) 54 #define FG_CNTL_CAP_ADJ_STAT (1 << 4) 55 #define FG_CNTL_CAP_ADJ_EN (1 << 5) 56 #define FG_CNTL_CC_EN (1 << 6) 57 #define FG_CNTL_GAUGE_EN (1 << 7) 58 59 #define FG_15BIT_WORD_VALID (1 << 15) 60 #define FG_15BIT_VAL_MASK 0x7fff 61 62 #define FG_REP_CAP_VALID (1 << 7) 63 #define FG_REP_CAP_VAL_MASK 0x7F 64 65 #define FG_DES_CAP1_VALID (1 << 7) 66 #define FG_DES_CAP_RES_LSB 1456 /* 1.456mAhr */ 67 68 #define FG_DES_CC_RES_LSB 1456 /* 1.456mAhr */ 69 70 #define FG_OCV_CAP_VALID (1 << 7) 71 #define FG_OCV_CAP_VAL_MASK 0x7F 72 #define FG_CC_CAP_VALID (1 << 7) 73 #define FG_CC_CAP_VAL_MASK 0x7F 74 75 #define FG_LOW_CAP_THR1_MASK 0xf0 /* 5% tp 20% */ 76 #define FG_LOW_CAP_THR1_VAL 0xa0 /* 15 perc */ 77 #define FG_LOW_CAP_THR2_MASK 0x0f /* 0% to 15% */ 78 #define FG_LOW_CAP_WARN_THR 14 /* 14 perc */ 79 #define FG_LOW_CAP_CRIT_THR 4 /* 4 perc */ 80 #define FG_LOW_CAP_SHDN_THR 0 /* 0 perc */ 81 82 #define DEV_NAME "axp288_fuel_gauge" 83 84 /* 1.1mV per LSB expressed in uV */ 85 #define VOLTAGE_FROM_ADC(a) ((a * 11) / 10) 86 /* properties converted to uV, uA */ 87 #define PROP_VOLT(a) ((a) * 1000) 88 #define PROP_CURR(a) ((a) * 1000) 89 90 #define AXP288_REG_UPDATE_INTERVAL (60 * HZ) 91 #define AXP288_FG_INTR_NUM 6 92 93 #define AXP288_QUIRK_NO_BATTERY BIT(0) 94 95 static bool no_current_sense_res; 96 module_param(no_current_sense_res, bool, 0444); 97 MODULE_PARM_DESC(no_current_sense_res, "No (or broken) current sense resistor"); 98 99 enum { 100 QWBTU_IRQ = 0, 101 WBTU_IRQ, 102 QWBTO_IRQ, 103 WBTO_IRQ, 104 WL2_IRQ, 105 WL1_IRQ, 106 }; 107 108 enum { 109 BAT_CHRG_CURR, 110 BAT_D_CURR, 111 BAT_VOLT, 112 IIO_CHANNEL_NUM 113 }; 114 115 struct axp288_fg_info { 116 struct device *dev; 117 struct regmap *regmap; 118 int irq[AXP288_FG_INTR_NUM]; 119 struct iio_channel *iio_channel[IIO_CHANNEL_NUM]; 120 struct power_supply *bat; 121 struct mutex lock; 122 int status; 123 int max_volt; 124 int pwr_op; 125 int low_cap; 126 struct dentry *debug_file; 127 128 char valid; /* zero until following fields are valid */ 129 unsigned long last_updated; /* in jiffies */ 130 131 int pwr_stat; 132 int fg_res; 133 int bat_volt; 134 int d_curr; 135 int c_curr; 136 int ocv; 137 int fg_cc_mtr1; 138 int fg_des_cap1; 139 }; 140 141 static enum power_supply_property fuel_gauge_props[] = { 142 POWER_SUPPLY_PROP_STATUS, 143 POWER_SUPPLY_PROP_PRESENT, 144 POWER_SUPPLY_PROP_HEALTH, 145 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 146 POWER_SUPPLY_PROP_VOLTAGE_NOW, 147 POWER_SUPPLY_PROP_VOLTAGE_OCV, 148 POWER_SUPPLY_PROP_CAPACITY, 149 POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN, 150 POWER_SUPPLY_PROP_TECHNOLOGY, 151 /* The 3 props below are not used when no_current_sense_res is set */ 152 POWER_SUPPLY_PROP_CHARGE_FULL, 153 POWER_SUPPLY_PROP_CHARGE_NOW, 154 POWER_SUPPLY_PROP_CURRENT_NOW, 155 }; 156 157 static int fuel_gauge_reg_readb(struct axp288_fg_info *info, int reg) 158 { 159 unsigned int val; 160 int ret; 161 162 ret = regmap_read(info->regmap, reg, &val); 163 if (ret < 0) { 164 dev_err(info->dev, "Error reading reg 0x%02x err: %d\n", reg, ret); 165 return ret; 166 } 167 168 return val; 169 } 170 171 static int fuel_gauge_reg_writeb(struct axp288_fg_info *info, int reg, u8 val) 172 { 173 int ret; 174 175 ret = regmap_write(info->regmap, reg, (unsigned int)val); 176 177 if (ret < 0) 178 dev_err(info->dev, "Error writing reg 0x%02x err: %d\n", reg, ret); 179 180 return ret; 181 } 182 183 static int fuel_gauge_read_15bit_word(struct axp288_fg_info *info, int reg) 184 { 185 unsigned char buf[2]; 186 int ret; 187 188 ret = regmap_bulk_read(info->regmap, reg, buf, 2); 189 if (ret < 0) { 190 dev_err(info->dev, "Error reading reg 0x%02x err: %d\n", reg, ret); 191 return ret; 192 } 193 194 ret = get_unaligned_be16(buf); 195 if (!(ret & FG_15BIT_WORD_VALID)) { 196 dev_err(info->dev, "Error reg 0x%02x contents not valid\n", reg); 197 return -ENXIO; 198 } 199 200 return ret & FG_15BIT_VAL_MASK; 201 } 202 203 static int fuel_gauge_read_12bit_word(struct axp288_fg_info *info, int reg) 204 { 205 unsigned char buf[2]; 206 int ret; 207 208 ret = regmap_bulk_read(info->regmap, reg, buf, 2); 209 if (ret < 0) { 210 dev_err(info->dev, "Error reading reg 0x%02x err: %d\n", reg, ret); 211 return ret; 212 } 213 214 /* 12-bit data values have upper 8 bits in buf[0], lower 4 in buf[1] */ 215 return (buf[0] << 4) | ((buf[1] >> 4) & 0x0f); 216 } 217 218 static int fuel_gauge_update_registers(struct axp288_fg_info *info) 219 { 220 int ret; 221 222 if (info->valid && time_before(jiffies, info->last_updated + AXP288_REG_UPDATE_INTERVAL)) 223 return 0; 224 225 dev_dbg(info->dev, "Fuel Gauge updating register values...\n"); 226 227 ret = iosf_mbi_block_punit_i2c_access(); 228 if (ret < 0) 229 return ret; 230 231 ret = fuel_gauge_reg_readb(info, AXP20X_PWR_INPUT_STATUS); 232 if (ret < 0) 233 goto out; 234 info->pwr_stat = ret; 235 236 if (no_current_sense_res) 237 ret = fuel_gauge_reg_readb(info, AXP288_FG_OCV_CAP_REG); 238 else 239 ret = fuel_gauge_reg_readb(info, AXP20X_FG_RES); 240 if (ret < 0) 241 goto out; 242 info->fg_res = ret; 243 244 ret = iio_read_channel_raw(info->iio_channel[BAT_VOLT], &info->bat_volt); 245 if (ret < 0) 246 goto out; 247 248 ret = fuel_gauge_read_12bit_word(info, AXP288_FG_OCVH_REG); 249 if (ret < 0) 250 goto out; 251 info->ocv = ret; 252 253 if (no_current_sense_res) 254 goto out_no_current_sense_res; 255 256 if (info->pwr_stat & PS_STAT_BAT_CHRG_DIR) { 257 info->d_curr = 0; 258 ret = iio_read_channel_raw(info->iio_channel[BAT_CHRG_CURR], &info->c_curr); 259 if (ret < 0) 260 goto out; 261 } else { 262 info->c_curr = 0; 263 ret = iio_read_channel_raw(info->iio_channel[BAT_D_CURR], &info->d_curr); 264 if (ret < 0) 265 goto out; 266 } 267 268 ret = fuel_gauge_read_15bit_word(info, AXP288_FG_CC_MTR1_REG); 269 if (ret < 0) 270 goto out; 271 info->fg_cc_mtr1 = ret; 272 273 ret = fuel_gauge_read_15bit_word(info, AXP288_FG_DES_CAP1_REG); 274 if (ret < 0) 275 goto out; 276 info->fg_des_cap1 = ret; 277 278 out_no_current_sense_res: 279 info->last_updated = jiffies; 280 info->valid = 1; 281 ret = 0; 282 out: 283 iosf_mbi_unblock_punit_i2c_access(); 284 return ret; 285 } 286 287 static void fuel_gauge_get_status(struct axp288_fg_info *info) 288 { 289 int pwr_stat = info->pwr_stat; 290 int fg_res = info->fg_res; 291 int curr = info->d_curr; 292 293 /* Report full if Vbus is valid and the reported capacity is 100% */ 294 if (!(pwr_stat & PS_STAT_VBUS_VALID)) 295 goto not_full; 296 297 if (!(fg_res & FG_REP_CAP_VALID)) 298 goto not_full; 299 300 fg_res &= ~FG_REP_CAP_VALID; 301 if (fg_res == 100) { 302 info->status = POWER_SUPPLY_STATUS_FULL; 303 return; 304 } 305 306 /* 307 * Sometimes the charger turns itself off before fg-res reaches 100%. 308 * When this happens the AXP288 reports a not-charging status and 309 * 0 mA discharge current. 310 */ 311 if (fg_res < 90 || (pwr_stat & PS_STAT_BAT_CHRG_DIR) || no_current_sense_res) 312 goto not_full; 313 314 if (curr == 0) { 315 info->status = POWER_SUPPLY_STATUS_FULL; 316 return; 317 } 318 319 not_full: 320 if (pwr_stat & PS_STAT_BAT_CHRG_DIR) 321 info->status = POWER_SUPPLY_STATUS_CHARGING; 322 else 323 info->status = POWER_SUPPLY_STATUS_DISCHARGING; 324 } 325 326 static int fuel_gauge_battery_health(struct axp288_fg_info *info) 327 { 328 int vocv = VOLTAGE_FROM_ADC(info->ocv); 329 int health = POWER_SUPPLY_HEALTH_UNKNOWN; 330 331 if (vocv > info->max_volt) 332 health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 333 else 334 health = POWER_SUPPLY_HEALTH_GOOD; 335 336 return health; 337 } 338 339 static int fuel_gauge_get_property(struct power_supply *ps, 340 enum power_supply_property prop, 341 union power_supply_propval *val) 342 { 343 struct axp288_fg_info *info = power_supply_get_drvdata(ps); 344 int ret, value; 345 346 mutex_lock(&info->lock); 347 348 ret = fuel_gauge_update_registers(info); 349 if (ret < 0) 350 goto out; 351 352 switch (prop) { 353 case POWER_SUPPLY_PROP_STATUS: 354 fuel_gauge_get_status(info); 355 val->intval = info->status; 356 break; 357 case POWER_SUPPLY_PROP_HEALTH: 358 val->intval = fuel_gauge_battery_health(info); 359 break; 360 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 361 value = VOLTAGE_FROM_ADC(info->bat_volt); 362 val->intval = PROP_VOLT(value); 363 break; 364 case POWER_SUPPLY_PROP_VOLTAGE_OCV: 365 value = VOLTAGE_FROM_ADC(info->ocv); 366 val->intval = PROP_VOLT(value); 367 break; 368 case POWER_SUPPLY_PROP_CURRENT_NOW: 369 if (info->d_curr > 0) 370 value = -1 * info->d_curr; 371 else 372 value = info->c_curr; 373 374 val->intval = PROP_CURR(value); 375 break; 376 case POWER_SUPPLY_PROP_PRESENT: 377 if (info->pwr_op & CHRG_STAT_BAT_PRESENT) 378 val->intval = 1; 379 else 380 val->intval = 0; 381 break; 382 case POWER_SUPPLY_PROP_CAPACITY: 383 if (!(info->fg_res & FG_REP_CAP_VALID)) 384 dev_err(info->dev, "capacity measurement not valid\n"); 385 val->intval = (info->fg_res & FG_REP_CAP_VAL_MASK); 386 break; 387 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN: 388 val->intval = (info->low_cap & 0x0f); 389 break; 390 case POWER_SUPPLY_PROP_TECHNOLOGY: 391 val->intval = POWER_SUPPLY_TECHNOLOGY_LION; 392 break; 393 case POWER_SUPPLY_PROP_CHARGE_NOW: 394 val->intval = info->fg_cc_mtr1 * FG_DES_CAP_RES_LSB; 395 break; 396 case POWER_SUPPLY_PROP_CHARGE_FULL: 397 val->intval = info->fg_des_cap1 * FG_DES_CAP_RES_LSB; 398 break; 399 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: 400 val->intval = PROP_VOLT(info->max_volt); 401 break; 402 default: 403 ret = -EINVAL; 404 } 405 406 out: 407 mutex_unlock(&info->lock); 408 return ret; 409 } 410 411 static int fuel_gauge_set_property(struct power_supply *ps, 412 enum power_supply_property prop, 413 const union power_supply_propval *val) 414 { 415 struct axp288_fg_info *info = power_supply_get_drvdata(ps); 416 int new_low_cap, ret = 0; 417 418 mutex_lock(&info->lock); 419 switch (prop) { 420 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN: 421 if ((val->intval < 0) || (val->intval > 15)) { 422 ret = -EINVAL; 423 break; 424 } 425 new_low_cap = info->low_cap; 426 new_low_cap &= 0xf0; 427 new_low_cap |= (val->intval & 0xf); 428 ret = fuel_gauge_reg_writeb(info, AXP288_FG_LOW_CAP_REG, new_low_cap); 429 if (ret == 0) 430 info->low_cap = new_low_cap; 431 break; 432 default: 433 ret = -EINVAL; 434 break; 435 } 436 437 mutex_unlock(&info->lock); 438 return ret; 439 } 440 441 static int fuel_gauge_property_is_writeable(struct power_supply *psy, 442 enum power_supply_property psp) 443 { 444 int ret; 445 446 switch (psp) { 447 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN: 448 ret = 1; 449 break; 450 default: 451 ret = 0; 452 } 453 454 return ret; 455 } 456 457 static irqreturn_t fuel_gauge_thread_handler(int irq, void *dev) 458 { 459 struct axp288_fg_info *info = dev; 460 int i; 461 462 for (i = 0; i < AXP288_FG_INTR_NUM; i++) { 463 if (info->irq[i] == irq) 464 break; 465 } 466 467 if (i >= AXP288_FG_INTR_NUM) { 468 dev_warn(info->dev, "spurious interrupt!!\n"); 469 return IRQ_NONE; 470 } 471 472 switch (i) { 473 case QWBTU_IRQ: 474 dev_info(info->dev, "Quit Battery under temperature in work mode IRQ (QWBTU)\n"); 475 break; 476 case WBTU_IRQ: 477 dev_info(info->dev, "Battery under temperature in work mode IRQ (WBTU)\n"); 478 break; 479 case QWBTO_IRQ: 480 dev_info(info->dev, "Quit Battery over temperature in work mode IRQ (QWBTO)\n"); 481 break; 482 case WBTO_IRQ: 483 dev_info(info->dev, "Battery over temperature in work mode IRQ (WBTO)\n"); 484 break; 485 case WL2_IRQ: 486 dev_info(info->dev, "Low Batt Warning(2) INTR\n"); 487 break; 488 case WL1_IRQ: 489 dev_info(info->dev, "Low Batt Warning(1) INTR\n"); 490 break; 491 default: 492 dev_warn(info->dev, "Spurious Interrupt!!!\n"); 493 } 494 495 mutex_lock(&info->lock); 496 info->valid = 0; /* Force updating of the cached registers */ 497 mutex_unlock(&info->lock); 498 499 power_supply_changed(info->bat); 500 return IRQ_HANDLED; 501 } 502 503 static void fuel_gauge_external_power_changed(struct power_supply *psy) 504 { 505 struct axp288_fg_info *info = power_supply_get_drvdata(psy); 506 507 mutex_lock(&info->lock); 508 info->valid = 0; /* Force updating of the cached registers */ 509 mutex_unlock(&info->lock); 510 power_supply_changed(psy); 511 } 512 513 static struct power_supply_desc fuel_gauge_desc = { 514 .name = DEV_NAME, 515 .type = POWER_SUPPLY_TYPE_BATTERY, 516 .properties = fuel_gauge_props, 517 .num_properties = ARRAY_SIZE(fuel_gauge_props), 518 .get_property = fuel_gauge_get_property, 519 .set_property = fuel_gauge_set_property, 520 .property_is_writeable = fuel_gauge_property_is_writeable, 521 .external_power_changed = fuel_gauge_external_power_changed, 522 }; 523 524 /* 525 * Some devices have no battery (HDMI sticks) and the axp288 battery's 526 * detection reports one despite it not being there. 527 * Please keep this listed sorted alphabetically. 528 */ 529 static const struct dmi_system_id axp288_quirks[] = { 530 { 531 /* ACEPC T8 Cherry Trail Z8350 mini PC */ 532 .matches = { 533 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."), 534 DMI_EXACT_MATCH(DMI_BOARD_NAME, "Cherry Trail CR"), 535 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "T8"), 536 /* also match on somewhat unique bios-version */ 537 DMI_EXACT_MATCH(DMI_BIOS_VERSION, "1.000"), 538 }, 539 .driver_data = (void *)AXP288_QUIRK_NO_BATTERY, 540 }, 541 { 542 /* ACEPC T11 Cherry Trail Z8350 mini PC */ 543 .matches = { 544 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."), 545 DMI_EXACT_MATCH(DMI_BOARD_NAME, "Cherry Trail CR"), 546 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "T11"), 547 /* also match on somewhat unique bios-version */ 548 DMI_EXACT_MATCH(DMI_BIOS_VERSION, "1.000"), 549 }, 550 .driver_data = (void *)AXP288_QUIRK_NO_BATTERY, 551 }, 552 { 553 /* Intel Bay Trail Compute Stick */ 554 .matches = { 555 DMI_MATCH(DMI_SYS_VENDOR, "Intel"), 556 /* Partial match for STCK1A32WFC STCK1A32FC, STCK1A8LFC variants */ 557 DMI_MATCH(DMI_PRODUCT_NAME, "STCK1A"), 558 }, 559 .driver_data = (void *)AXP288_QUIRK_NO_BATTERY, 560 }, 561 { 562 /* Intel Cherry Trail Compute Stick */ 563 .matches = { 564 DMI_MATCH(DMI_SYS_VENDOR, "Intel"), 565 /* Partial match for STK1AW32SC and STK1A32SC variants */ 566 DMI_MATCH(DMI_PRODUCT_NAME, "STK1A"), 567 }, 568 .driver_data = (void *)AXP288_QUIRK_NO_BATTERY, 569 }, 570 { 571 /* Meegopad T02 */ 572 .matches = { 573 DMI_MATCH(DMI_PRODUCT_NAME, "MEEGOPAD T02"), 574 }, 575 .driver_data = (void *)AXP288_QUIRK_NO_BATTERY, 576 }, 577 { /* Mele PCG03 Mini PC */ 578 .matches = { 579 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Mini PC"), 580 DMI_EXACT_MATCH(DMI_BOARD_NAME, "Mini PC"), 581 }, 582 .driver_data = (void *)AXP288_QUIRK_NO_BATTERY, 583 }, 584 { 585 /* Minix Neo Z83-4 mini PC */ 586 .matches = { 587 DMI_MATCH(DMI_SYS_VENDOR, "MINIX"), 588 DMI_MATCH(DMI_PRODUCT_NAME, "Z83-4"), 589 }, 590 .driver_data = (void *)AXP288_QUIRK_NO_BATTERY, 591 }, 592 { 593 /* 594 * One Mix 1, this uses the "T3 MRD" boardname used by 595 * generic mini PCs, but it is a mini laptop so it does 596 * actually have a battery! 597 */ 598 .matches = { 599 DMI_MATCH(DMI_BOARD_NAME, "T3 MRD"), 600 DMI_MATCH(DMI_BIOS_DATE, "06/14/2018"), 601 }, 602 .driver_data = NULL, 603 }, 604 { 605 /* Radxa ROCK Pi X Single Board Computer */ 606 .matches = { 607 DMI_MATCH(DMI_BOARD_NAME, "ROCK Pi X"), 608 DMI_MATCH(DMI_BOARD_VENDOR, "Radxa"), 609 }, 610 .driver_data = (void *)AXP288_QUIRK_NO_BATTERY, 611 }, 612 { 613 /* 614 * Various Ace PC/Meegopad/MinisForum/Wintel Mini-PCs/HDMI-sticks 615 * This entry must be last because it is generic, this allows 616 * adding more specifuc quirks overriding this generic entry. 617 */ 618 .matches = { 619 DMI_MATCH(DMI_BOARD_NAME, "T3 MRD"), 620 DMI_MATCH(DMI_CHASSIS_TYPE, "3"), 621 DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."), 622 }, 623 .driver_data = (void *)AXP288_QUIRK_NO_BATTERY, 624 }, 625 {} 626 }; 627 628 static int axp288_fuel_gauge_read_initial_regs(struct axp288_fg_info *info) 629 { 630 unsigned int val; 631 int ret; 632 633 /* 634 * On some devices the fuelgauge and charger parts of the axp288 are 635 * not used, check that the fuelgauge is enabled (CC_CTRL != 0). 636 */ 637 ret = regmap_read(info->regmap, AXP20X_CC_CTRL, &val); 638 if (ret < 0) 639 return ret; 640 if (val == 0) 641 return -ENODEV; 642 643 ret = fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP1_REG); 644 if (ret < 0) 645 return ret; 646 647 if (!(ret & FG_DES_CAP1_VALID)) { 648 dev_err(info->dev, "axp288 not configured by firmware\n"); 649 return -ENODEV; 650 } 651 652 ret = fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1); 653 if (ret < 0) 654 return ret; 655 switch ((ret & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS) { 656 case CHRG_CCCV_CV_4100MV: 657 info->max_volt = 4100; 658 break; 659 case CHRG_CCCV_CV_4150MV: 660 info->max_volt = 4150; 661 break; 662 case CHRG_CCCV_CV_4200MV: 663 info->max_volt = 4200; 664 break; 665 case CHRG_CCCV_CV_4350MV: 666 info->max_volt = 4350; 667 break; 668 } 669 670 ret = fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE); 671 if (ret < 0) 672 return ret; 673 info->pwr_op = ret; 674 675 ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG); 676 if (ret < 0) 677 return ret; 678 info->low_cap = ret; 679 680 return 0; 681 } 682 683 static void axp288_fuel_gauge_release_iio_chans(void *data) 684 { 685 struct axp288_fg_info *info = data; 686 int i; 687 688 for (i = 0; i < IIO_CHANNEL_NUM; i++) 689 if (!IS_ERR_OR_NULL(info->iio_channel[i])) 690 iio_channel_release(info->iio_channel[i]); 691 } 692 693 static int axp288_fuel_gauge_probe(struct platform_device *pdev) 694 { 695 struct axp288_fg_info *info; 696 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent); 697 struct power_supply_config psy_cfg = {}; 698 static const char * const iio_chan_name[] = { 699 [BAT_CHRG_CURR] = "axp288-chrg-curr", 700 [BAT_D_CURR] = "axp288-chrg-d-curr", 701 [BAT_VOLT] = "axp288-batt-volt", 702 }; 703 const struct dmi_system_id *dmi_id; 704 struct device *dev = &pdev->dev; 705 unsigned long quirks = 0; 706 int i, pirq, ret; 707 708 /* 709 * Normally the native AXP288 fg/charger drivers are preferred but 710 * on some devices the ACPI drivers should be used instead. 711 */ 712 if (!acpi_quirk_skip_acpi_ac_and_battery()) 713 return -ENODEV; 714 715 dmi_id = dmi_first_match(axp288_quirks); 716 if (dmi_id) 717 quirks = (unsigned long)dmi_id->driver_data; 718 719 if (quirks & AXP288_QUIRK_NO_BATTERY) 720 return -ENODEV; 721 722 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); 723 if (!info) 724 return -ENOMEM; 725 726 info->dev = dev; 727 info->regmap = axp20x->regmap; 728 info->status = POWER_SUPPLY_STATUS_UNKNOWN; 729 info->valid = 0; 730 731 platform_set_drvdata(pdev, info); 732 733 mutex_init(&info->lock); 734 735 for (i = 0; i < AXP288_FG_INTR_NUM; i++) { 736 pirq = platform_get_irq(pdev, i); 737 if (pirq < 0) 738 continue; 739 ret = regmap_irq_get_virq(axp20x->regmap_irqc, pirq); 740 if (ret < 0) 741 return dev_err_probe(dev, ret, "getting vIRQ %d\n", pirq); 742 743 info->irq[i] = ret; 744 } 745 746 for (i = 0; i < IIO_CHANNEL_NUM; i++) { 747 /* 748 * Note cannot use devm_iio_channel_get because x86 systems 749 * lack the device<->channel maps which iio_channel_get will 750 * try to use when passed a non NULL device pointer. 751 */ 752 info->iio_channel[i] = 753 iio_channel_get(NULL, iio_chan_name[i]); 754 if (IS_ERR(info->iio_channel[i])) { 755 ret = PTR_ERR(info->iio_channel[i]); 756 dev_dbg(dev, "error getting iiochan %s: %d\n", iio_chan_name[i], ret); 757 /* Wait for axp288_adc to load */ 758 if (ret == -ENODEV) 759 ret = -EPROBE_DEFER; 760 761 axp288_fuel_gauge_release_iio_chans(info); 762 return ret; 763 } 764 } 765 766 ret = devm_add_action_or_reset(dev, axp288_fuel_gauge_release_iio_chans, info); 767 if (ret) 768 return ret; 769 770 ret = iosf_mbi_block_punit_i2c_access(); 771 if (ret < 0) 772 return ret; 773 774 ret = axp288_fuel_gauge_read_initial_regs(info); 775 iosf_mbi_unblock_punit_i2c_access(); 776 if (ret < 0) 777 return ret; 778 779 psy_cfg.drv_data = info; 780 if (no_current_sense_res) 781 fuel_gauge_desc.num_properties = ARRAY_SIZE(fuel_gauge_props) - 3; 782 info->bat = devm_power_supply_register(dev, &fuel_gauge_desc, &psy_cfg); 783 if (IS_ERR(info->bat)) { 784 ret = PTR_ERR(info->bat); 785 dev_err(dev, "failed to register battery: %d\n", ret); 786 return ret; 787 } 788 789 for (i = 0; i < AXP288_FG_INTR_NUM; i++) { 790 ret = devm_request_threaded_irq(dev, info->irq[i], NULL, 791 fuel_gauge_thread_handler, 792 IRQF_ONESHOT, DEV_NAME, info); 793 if (ret) 794 return dev_err_probe(dev, ret, "requesting IRQ %d\n", info->irq[i]); 795 } 796 797 return 0; 798 } 799 800 static const struct platform_device_id axp288_fg_id_table[] = { 801 { .name = DEV_NAME }, 802 {}, 803 }; 804 MODULE_DEVICE_TABLE(platform, axp288_fg_id_table); 805 806 static struct platform_driver axp288_fuel_gauge_driver = { 807 .probe = axp288_fuel_gauge_probe, 808 .id_table = axp288_fg_id_table, 809 .driver = { 810 .name = DEV_NAME, 811 }, 812 }; 813 814 module_platform_driver(axp288_fuel_gauge_driver); 815 816 MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>"); 817 MODULE_AUTHOR("Todd Brandt <todd.e.brandt@linux.intel.com>"); 818 MODULE_DESCRIPTION("Xpower AXP288 Fuel Gauge Driver"); 819 MODULE_LICENSE("GPL"); 820