1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * TWL6030 charger 4 * 5 * Copyright (C) 2024 Andreas Kemnade <andreas@kemnade.info> 6 * 7 * based on older 6030 driver found in a v3.0 vendor kernel 8 * 9 * based on twl4030_bci_battery.c by TI 10 * Copyright (C) 2008 Texas Instruments, Inc. 11 */ 12 13 #include <linux/init.h> 14 #include <linux/module.h> 15 #include <linux/slab.h> 16 #include <linux/err.h> 17 #include <linux/of.h> 18 #include <linux/bits.h> 19 #include <linux/platform_device.h> 20 #include <linux/interrupt.h> 21 #include <linux/mfd/twl.h> 22 #include <linux/power_supply.h> 23 #include <linux/notifier.h> 24 #include <linux/usb/otg.h> 25 #include <linux/iio/consumer.h> 26 #include <linux/devm-helpers.h> 27 28 #define CONTROLLER_INT_MASK 0x00 29 #define CONTROLLER_CTRL1 0x01 30 #define CONTROLLER_WDG 0x02 31 #define CONTROLLER_STAT1 0x03 32 #define CHARGERUSB_INT_STATUS 0x04 33 #define CHARGERUSB_INT_MASK 0x05 34 #define CHARGERUSB_STATUS_INT1 0x06 35 #define CHARGERUSB_STATUS_INT2 0x07 36 #define CHARGERUSB_CTRL1 0x08 37 #define CHARGERUSB_CTRL2 0x09 38 #define CHARGERUSB_CTRL3 0x0A 39 #define CHARGERUSB_STAT1 0x0B 40 #define CHARGERUSB_VOREG 0x0C 41 #define CHARGERUSB_VICHRG 0x0D 42 #define CHARGERUSB_CINLIMIT 0x0E 43 #define CHARGERUSB_CTRLLIMIT1 0x0F 44 #define CHARGERUSB_CTRLLIMIT2 0x10 45 #define ANTICOLLAPSE_CTRL1 0x11 46 #define ANTICOLLAPSE_CTRL2 0x12 47 48 /* TWL6032 registers 0xDA to 0xDE - TWL6032_MODULE_CHARGER */ 49 #define CONTROLLER_CTRL2 0x00 50 #define CONTROLLER_VSEL_COMP 0x01 51 #define CHARGERUSB_VSYSREG 0x02 52 #define CHARGERUSB_VICHRG_PC 0x03 53 #define LINEAR_CHRG_STS 0x04 54 55 #define LINEAR_CHRG_STS_CRYSTL_OSC_OK 0x40 56 #define LINEAR_CHRG_STS_END_OF_CHARGE 0x20 57 #define LINEAR_CHRG_STS_VBATOV 0x10 58 #define LINEAR_CHRG_STS_VSYSOV 0x08 59 #define LINEAR_CHRG_STS_DPPM_STS 0x04 60 #define LINEAR_CHRG_STS_CV_STS 0x02 61 #define LINEAR_CHRG_STS_CC_STS 0x01 62 63 #define FG_REG_00 0x00 64 #define FG_REG_01 0x01 65 #define FG_REG_02 0x02 66 #define FG_REG_03 0x03 67 #define FG_REG_04 0x04 68 #define FG_REG_05 0x05 69 #define FG_REG_06 0x06 70 #define FG_REG_07 0x07 71 #define FG_REG_08 0x08 72 #define FG_REG_09 0x09 73 #define FG_REG_10 0x0A 74 #define FG_REG_11 0x0B 75 76 /* CONTROLLER_INT_MASK */ 77 #define MVAC_FAULT BIT(7) 78 #define MAC_EOC BIT(6) 79 #define LINCH_GATED BIT(5) 80 #define MBAT_REMOVED BIT(4) 81 #define MFAULT_WDG BIT(3) 82 #define MBAT_TEMP BIT(2) 83 #define MVBUS_DET BIT(1) 84 #define MVAC_DET BIT(0) 85 86 /* CONTROLLER_CTRL1 */ 87 #define CONTROLLER_CTRL1_EN_LINCH BIT(5) 88 #define CONTROLLER_CTRL1_EN_CHARGER BIT(4) 89 #define CONTROLLER_CTRL1_SEL_CHARGER BIT(3) 90 91 /* CONTROLLER_STAT1 */ 92 #define CONTROLLER_STAT1_EXTCHRG_STATZ BIT(7) 93 #define CONTROLLER_STAT1_LINCH_GATED BIT(6) 94 #define CONTROLLER_STAT1_CHRG_DET_N BIT(5) 95 #define CONTROLLER_STAT1_FAULT_WDG BIT(4) 96 #define CONTROLLER_STAT1_VAC_DET BIT(3) 97 #define VAC_DET BIT(3) 98 #define CONTROLLER_STAT1_VBUS_DET BIT(2) 99 #define VBUS_DET BIT(2) 100 #define CONTROLLER_STAT1_BAT_REMOVED BIT(1) 101 #define CONTROLLER_STAT1_BAT_TEMP_OVRANGE BIT(0) 102 103 /* CHARGERUSB_INT_STATUS */ 104 #define EN_LINCH BIT(4) 105 #define CURRENT_TERM_INT BIT(3) 106 #define CHARGERUSB_STAT BIT(2) 107 #define CHARGERUSB_THMREG BIT(1) 108 #define CHARGERUSB_FAULT BIT(0) 109 110 /* CHARGERUSB_INT_MASK */ 111 #define MASK_MCURRENT_TERM BIT(3) 112 #define MASK_MCHARGERUSB_STAT BIT(2) 113 #define MASK_MCHARGERUSB_THMREG BIT(1) 114 #define MASK_MCHARGERUSB_FAULT BIT(0) 115 116 /* CHARGERUSB_STATUS_INT1 */ 117 #define CHARGERUSB_STATUS_INT1_TMREG BIT(7) 118 #define CHARGERUSB_STATUS_INT1_NO_BAT BIT(6) 119 #define CHARGERUSB_STATUS_INT1_BST_OCP BIT(5) 120 #define CHARGERUSB_STATUS_INT1_TH_SHUTD BIT(4) 121 #define CHARGERUSB_STATUS_INT1_BAT_OVP BIT(3) 122 #define CHARGERUSB_STATUS_INT1_POOR_SRC BIT(2) 123 #define CHARGERUSB_STATUS_INT1_SLP_MODE BIT(1) 124 #define CHARGERUSB_STATUS_INT1_VBUS_OVP BIT(0) 125 126 /* CHARGERUSB_STATUS_INT2 */ 127 #define ICCLOOP BIT(3) 128 #define CURRENT_TERM BIT(2) 129 #define CHARGE_DONE BIT(1) 130 #define ANTICOLLAPSE BIT(0) 131 132 /* CHARGERUSB_CTRL1 */ 133 #define SUSPEND_BOOT BIT(7) 134 #define OPA_MODE BIT(6) 135 #define HZ_MODE BIT(5) 136 #define TERM BIT(4) 137 138 /* CHARGERUSB_CTRL2 */ 139 #define UA_TO_VITERM(x) (((x) / 50000 - 1) << 5) 140 141 /* CHARGERUSB_CTRL3 */ 142 #define VBUSCHRG_LDO_OVRD BIT(7) 143 #define CHARGE_ONCE BIT(6) 144 #define BST_HW_PR_DIS BIT(5) 145 #define AUTOSUPPLY BIT(3) 146 #define BUCK_HSILIM BIT(0) 147 148 /* CHARGERUSB_VOREG */ 149 #define UV_TO_VOREG(x) (((x) - 3500000) / 20000) 150 #define VOREG_TO_UV(x) (((x) & 0x3F) * 20000 + 3500000) 151 #define CHARGERUSB_VOREG_3P52 0x01 152 #define CHARGERUSB_VOREG_4P0 0x19 153 #define CHARGERUSB_VOREG_4P2 0x23 154 #define CHARGERUSB_VOREG_4P76 0x3F 155 156 /* CHARGERUSB_VICHRG */ 157 /* 158 * might be inaccurate for < 500 mA, diffent scale might apply, 159 * either starting from 100 mA or 300 mA 160 */ 161 #define UA_TO_VICHRG(x) (((x) / 100000) - 1) 162 #define VICHRG_TO_UA(x) (((x) & 0xf) * 100000 + 100000) 163 164 /* CHARGERUSB_CINLIMIT */ 165 #define CHARGERUSB_CIN_LIMIT_100 0x1 166 #define CHARGERUSB_CIN_LIMIT_300 0x5 167 #define CHARGERUSB_CIN_LIMIT_500 0x9 168 #define CHARGERUSB_CIN_LIMIT_NONE 0xF 169 170 /* CHARGERUSB_CTRLLIMIT2 */ 171 #define CHARGERUSB_CTRLLIMIT2_1500 0x0E 172 #define LOCK_LIMIT BIT(4) 173 174 /* ANTICOLLAPSE_CTRL2 */ 175 #define BUCK_VTH_SHIFT 5 176 177 /* FG_REG_00 */ 178 #define CC_ACTIVE_MODE_SHIFT 6 179 #define CC_AUTOCLEAR BIT(2) 180 #define CC_CAL_EN BIT(1) 181 #define CC_PAUSE BIT(0) 182 183 #define REG_TOGGLE1 0x90 184 #define REG_PWDNSTATUS1 0x93 185 #define FGDITHS BIT(7) 186 #define FGDITHR BIT(6) 187 #define FGS BIT(5) 188 #define FGR BIT(4) 189 #define BBSPOR_CFG 0xE6 190 #define BB_CHG_EN BIT(3) 191 192 struct twl6030_charger_info { 193 struct device *dev; 194 struct power_supply *usb; 195 struct power_supply_battery_info *binfo; 196 struct work_struct work; 197 int irq_chg; 198 int input_current_limit; 199 struct iio_channel *channel_vusb; 200 struct delayed_work charger_monitor; 201 bool extended_current_range; 202 }; 203 204 struct twl6030_charger_chip_data { 205 bool extended_current_range; 206 }; 207 208 static int twl6030_charger_read(u8 reg, u8 *val) 209 { 210 return twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, val, reg); 211 } 212 213 static int twl6030_charger_write(u8 reg, u8 val) 214 { 215 return twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, val, reg); 216 } 217 218 static int twl6030_config_cinlimit_reg(struct twl6030_charger_info *charger, 219 unsigned int ua) 220 { 221 if (ua >= 50000 && ua <= 750000) { 222 ua = (ua - 50000) / 50000; 223 } else if ((ua > 750000) && (ua <= 1500000) && charger->extended_current_range) { 224 ua = ((ua % 100000) ? 0x30 : 0x20) + ((ua - 100000) / 100000); 225 } else { 226 if (ua < 50000) { 227 dev_err(charger->dev, "invalid input current limit\n"); 228 return -EINVAL; 229 } 230 /* This is no current limit */ 231 ua = 0x0F; 232 } 233 234 return twl6030_charger_write(CHARGERUSB_CINLIMIT, ua); 235 } 236 237 /* 238 * rewriting all stuff here, resets to extremely conservative defaults were 239 * seen under some circumstances, like charge voltage to 3.5V 240 */ 241 static int twl6030_enable_usb(struct twl6030_charger_info *charger) 242 { 243 int ret; 244 245 ret = twl6030_charger_write(CHARGERUSB_VICHRG, 246 UA_TO_VICHRG(charger->binfo->constant_charge_current_max_ua)); 247 if (ret < 0) 248 return ret; 249 250 ret = twl6030_charger_write(CONTROLLER_WDG, 0xff); 251 if (ret < 0) 252 return ret; 253 254 charger->input_current_limit = 500000; 255 ret = twl6030_config_cinlimit_reg(charger, charger->input_current_limit); 256 if (ret < 0) 257 return ret; 258 259 ret = twl6030_charger_write(CHARGERUSB_CINLIMIT, CHARGERUSB_CIN_LIMIT_500); 260 if (ret < 0) 261 return ret; 262 263 ret = twl6030_charger_write(CHARGERUSB_VOREG, 264 UV_TO_VOREG(charger->binfo->constant_charge_voltage_max_uv)); 265 if (ret < 0) 266 return ret; 267 268 ret = twl6030_charger_write(CHARGERUSB_CTRL1, TERM); 269 if (ret < 0) 270 return ret; 271 272 if (charger->binfo->charge_term_current_ua != -EINVAL) { 273 ret = twl6030_charger_write(CHARGERUSB_CTRL2, 274 UA_TO_VITERM(charger->binfo->charge_term_current_ua)); 275 if (ret < 0) 276 return ret; 277 } 278 279 return twl6030_charger_write(CONTROLLER_CTRL1, CONTROLLER_CTRL1_EN_CHARGER); 280 } 281 282 static void twl6030_charger_wdg(struct work_struct *data) 283 { 284 struct twl6030_charger_info *charger = 285 container_of(data, struct twl6030_charger_info, 286 charger_monitor.work); 287 288 u8 val; 289 u8 int_stat; 290 u8 stat_int1; 291 u8 stat_int2; 292 293 twl6030_charger_read(CONTROLLER_STAT1, &val); 294 twl6030_charger_read(CHARGERUSB_INT_STATUS, &int_stat); 295 twl6030_charger_read(CHARGERUSB_STATUS_INT1, &stat_int1); 296 twl6030_charger_read(CHARGERUSB_STATUS_INT2, &stat_int2); 297 dev_dbg(charger->dev, 298 "wdg: stat1: %02x %s INT_STATUS %02x STATUS_INT1 %02x STATUS_INT2 %02x\n", 299 val, (val & VBUS_DET) ? "usb online" : "usb offline", 300 int_stat, stat_int1, stat_int2); 301 302 twl6030_charger_write(CONTROLLER_WDG, 0xff); 303 schedule_delayed_work(&charger->charger_monitor, 304 msecs_to_jiffies(10000)); 305 } 306 307 static irqreturn_t twl6030_charger_interrupt(int irq, void *arg) 308 { 309 struct twl6030_charger_info *charger = arg; 310 u8 val; 311 u8 int_stat; 312 u8 stat_int1; 313 u8 stat_int2; 314 315 if (twl6030_charger_read(CONTROLLER_STAT1, &val) < 0) 316 return IRQ_HANDLED; 317 318 if (twl6030_charger_read(CHARGERUSB_INT_STATUS, &int_stat) < 0) 319 return IRQ_HANDLED; 320 321 if (twl6030_charger_read(CHARGERUSB_STATUS_INT1, &stat_int1) < 0) 322 return IRQ_HANDLED; 323 324 if (twl6030_charger_read(CHARGERUSB_STATUS_INT2, &stat_int2) < 0) 325 return IRQ_HANDLED; 326 327 dev_dbg(charger->dev, 328 "charger irq: stat1: %02x %s INT_STATUS %02x STATUS_INT1 %02x STATUS_INT2 %02x\n", 329 val, (val & VBUS_DET) ? "usb online" : "usb offline", 330 int_stat, stat_int1, stat_int2); 331 power_supply_changed(charger->usb); 332 333 if (val & VBUS_DET) { 334 if (twl6030_charger_read(CONTROLLER_CTRL1, &val) < 0) 335 return IRQ_HANDLED; 336 337 if (!(val & CONTROLLER_CTRL1_EN_CHARGER)) { 338 if (twl6030_enable_usb(charger) < 0) 339 return IRQ_HANDLED; 340 341 schedule_delayed_work(&charger->charger_monitor, 342 msecs_to_jiffies(10000)); 343 } 344 } else { 345 cancel_delayed_work(&charger->charger_monitor); 346 } 347 return IRQ_HANDLED; 348 } 349 350 static int twl6030_charger_usb_get_property(struct power_supply *psy, 351 enum power_supply_property psp, 352 union power_supply_propval *val) 353 { 354 struct twl6030_charger_info *charger = power_supply_get_drvdata(psy); 355 int ret; 356 u8 stat1; 357 u8 intstat; 358 359 ret = twl6030_charger_read(CONTROLLER_STAT1, &stat1); 360 if (ret) 361 return ret; 362 363 switch (psp) { 364 case POWER_SUPPLY_PROP_STATUS: 365 if (!(stat1 & VBUS_DET)) { 366 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; 367 break; 368 } 369 ret = twl6030_charger_read(CHARGERUSB_STATUS_INT2, &intstat); 370 if (ret) 371 return ret; 372 373 if (intstat & CHARGE_DONE) 374 val->intval = POWER_SUPPLY_STATUS_FULL; 375 else if (intstat & CURRENT_TERM) 376 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; 377 else 378 val->intval = POWER_SUPPLY_STATUS_CHARGING; 379 break; 380 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 381 if (!charger->channel_vusb) 382 return -ENODATA; 383 384 ret = iio_read_channel_processed_scale(charger->channel_vusb, &val->intval, 1000); 385 if (ret < 0) 386 return ret; 387 388 break; 389 case POWER_SUPPLY_PROP_ONLINE: 390 val->intval = !!(stat1 & VBUS_DET); 391 break; 392 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 393 val->intval = charger->input_current_limit; 394 break; 395 default: 396 return -EINVAL; 397 } 398 399 return 0; 400 } 401 402 static int twl6030_charger_usb_set_property(struct power_supply *psy, 403 enum power_supply_property psp, 404 const union power_supply_propval *val) 405 { 406 struct twl6030_charger_info *charger = power_supply_get_drvdata(psy); 407 408 switch (psp) { 409 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 410 charger->input_current_limit = val->intval; 411 return twl6030_config_cinlimit_reg(charger, charger->input_current_limit); 412 default: 413 return -EINVAL; 414 } 415 416 return 0; 417 } 418 419 static int twl6030_charger_usb_property_is_writeable(struct power_supply *psy, 420 enum power_supply_property psp) 421 { 422 dev_info(&psy->dev, "is %d writeable?\n", (int)psp); 423 switch (psp) { 424 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 425 return true; 426 default: 427 return false; 428 } 429 } 430 431 static enum power_supply_property twl6030_charger_props[] = { 432 POWER_SUPPLY_PROP_STATUS, 433 POWER_SUPPLY_PROP_ONLINE, 434 POWER_SUPPLY_PROP_VOLTAGE_NOW, 435 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, 436 }; 437 438 static const struct power_supply_desc twl6030_charger_usb_desc = { 439 .name = "twl6030_usb", 440 .type = POWER_SUPPLY_TYPE_USB, 441 .properties = twl6030_charger_props, 442 .num_properties = ARRAY_SIZE(twl6030_charger_props), 443 .get_property = twl6030_charger_usb_get_property, 444 .set_property = twl6030_charger_usb_set_property, 445 .property_is_writeable = twl6030_charger_usb_property_is_writeable, 446 }; 447 448 static int twl6030_charger_probe(struct platform_device *pdev) 449 { 450 struct twl6030_charger_info *charger; 451 const struct twl6030_charger_chip_data *chip_data; 452 struct power_supply_config psy_cfg = {}; 453 int ret; 454 u8 val; 455 456 charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL); 457 if (!charger) 458 return -ENOMEM; 459 460 charger->dev = &pdev->dev; 461 charger->irq_chg = platform_get_irq(pdev, 0); 462 463 chip_data = device_get_match_data(&pdev->dev); 464 if (!chip_data) 465 return dev_err_probe(&pdev->dev, -EINVAL, "missing chip data\n"); 466 467 charger->extended_current_range = chip_data->extended_current_range; 468 platform_set_drvdata(pdev, charger); 469 psy_cfg.drv_data = charger; 470 psy_cfg.fwnode = dev_fwnode(&pdev->dev); 471 472 charger->channel_vusb = devm_iio_channel_get(&pdev->dev, "vusb"); 473 if (IS_ERR(charger->channel_vusb)) { 474 ret = PTR_ERR(charger->channel_vusb); 475 if (ret == -EPROBE_DEFER) 476 return ret; /* iio not ready */ 477 dev_warn(&pdev->dev, "could not request vusb iio channel (%d)", 478 ret); 479 charger->channel_vusb = NULL; 480 } 481 482 charger->usb = devm_power_supply_register(&pdev->dev, 483 &twl6030_charger_usb_desc, 484 &psy_cfg); 485 if (IS_ERR(charger->usb)) 486 return dev_err_probe(&pdev->dev, PTR_ERR(charger->usb), 487 "Failed to register usb\n"); 488 489 ret = power_supply_get_battery_info(charger->usb, &charger->binfo); 490 if (ret < 0) 491 return dev_err_probe(&pdev->dev, ret, 492 "Failed to get battery info\n"); 493 494 dev_info(&pdev->dev, "battery with vmax %d imax: %d\n", 495 charger->binfo->constant_charge_voltage_max_uv, 496 charger->binfo->constant_charge_current_max_ua); 497 498 if (charger->binfo->constant_charge_voltage_max_uv == -EINVAL) { 499 ret = twl6030_charger_read(CHARGERUSB_CTRLLIMIT1, &val); 500 if (ret < 0) 501 return ret; 502 503 charger->binfo->constant_charge_voltage_max_uv = 504 VOREG_TO_UV(val); 505 } 506 507 if (charger->binfo->constant_charge_voltage_max_uv > 4760000 || 508 charger->binfo->constant_charge_voltage_max_uv < 350000) 509 return dev_err_probe(&pdev->dev, -EINVAL, 510 "Invalid charge voltage\n"); 511 512 if (charger->binfo->constant_charge_current_max_ua == -EINVAL) { 513 ret = twl6030_charger_read(CHARGERUSB_CTRLLIMIT2, &val); 514 if (ret < 0) 515 return ret; 516 517 charger->binfo->constant_charge_current_max_ua = VICHRG_TO_UA(val); 518 } 519 520 if (charger->binfo->constant_charge_current_max_ua < 100000 || 521 charger->binfo->constant_charge_current_max_ua > 1500000) { 522 return dev_err_probe(&pdev->dev, -EINVAL, 523 "Invalid charge current\n"); 524 } 525 526 if ((charger->binfo->charge_term_current_ua != -EINVAL) && 527 (charger->binfo->charge_term_current_ua > 400000 || 528 charger->binfo->charge_term_current_ua < 50000)) { 529 return dev_err_probe(&pdev->dev, -EINVAL, 530 "Invalid charge termination current\n"); 531 } 532 533 ret = devm_delayed_work_autocancel(&pdev->dev, 534 &charger->charger_monitor, 535 twl6030_charger_wdg); 536 if (ret < 0) 537 return dev_err_probe(&pdev->dev, ret, 538 "Failed to register delayed work\n"); 539 540 ret = devm_request_threaded_irq(&pdev->dev, charger->irq_chg, NULL, 541 twl6030_charger_interrupt, 542 IRQF_ONESHOT, pdev->name, 543 charger); 544 if (ret < 0) 545 return dev_err_probe(&pdev->dev, ret, 546 "could not request irq %d\n", 547 charger->irq_chg); 548 549 /* turing to charging to configure things */ 550 twl6030_charger_write(CONTROLLER_CTRL1, 0); 551 twl6030_charger_interrupt(0, charger); 552 553 return 0; 554 } 555 556 static const struct twl6030_charger_chip_data twl6030_data = { 557 .extended_current_range = false, 558 }; 559 560 static const struct twl6030_charger_chip_data twl6032_data = { 561 .extended_current_range = true, 562 }; 563 564 static const struct of_device_id twl_charger_of_match[] = { 565 {.compatible = "ti,twl6030-charger", .data = &twl6030_data}, 566 {.compatible = "ti,twl6032-charger", .data = &twl6032_data}, 567 { } 568 }; 569 MODULE_DEVICE_TABLE(of, twl_charger_of_match); 570 571 static struct platform_driver twl6030_charger_driver = { 572 .probe = twl6030_charger_probe, 573 .driver = { 574 .name = "twl6030_charger", 575 .of_match_table = twl_charger_of_match, 576 }, 577 }; 578 module_platform_driver(twl6030_charger_driver); 579 580 MODULE_DESCRIPTION("TWL6030 Battery Charger Interface driver"); 581 MODULE_LICENSE("GPL"); 582