1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Driver for Richtek RT9455WSC battery charger. 4 * 5 * Copyright (C) 2015 Intel Corporation 6 */ 7 8 #include <linux/module.h> 9 #include <linux/interrupt.h> 10 #include <linux/delay.h> 11 #include <linux/of.h> 12 #include <linux/pm_runtime.h> 13 #include <linux/power_supply.h> 14 #include <linux/i2c.h> 15 #include <linux/acpi.h> 16 #include <linux/usb/phy.h> 17 #include <linux/regmap.h> 18 19 #define RT9455_MANUFACTURER "Richtek" 20 #define RT9455_MODEL_NAME "RT9455" 21 #define RT9455_DRIVER_NAME "rt9455-charger" 22 23 #define RT9455_IRQ_NAME "interrupt" 24 25 #define RT9455_PWR_RDY_DELAY 1 /* 1 second */ 26 #define RT9455_MAX_CHARGING_TIME 21600 /* 6 hrs */ 27 #define RT9455_BATT_PRESENCE_DELAY 60 /* 60 seconds */ 28 29 #define RT9455_CHARGE_MODE 0x00 30 #define RT9455_BOOST_MODE 0x01 31 32 #define RT9455_FAULT 0x03 33 34 #define RT9455_IAICR_100MA 0x00 35 #define RT9455_IAICR_500MA 0x01 36 #define RT9455_IAICR_NO_LIMIT 0x03 37 38 #define RT9455_CHARGE_DISABLE 0x00 39 #define RT9455_CHARGE_ENABLE 0x01 40 41 #define RT9455_PWR_FAULT 0x00 42 #define RT9455_PWR_GOOD 0x01 43 44 #define RT9455_REG_CTRL1 0x00 /* CTRL1 reg address */ 45 #define RT9455_REG_CTRL2 0x01 /* CTRL2 reg address */ 46 #define RT9455_REG_CTRL3 0x02 /* CTRL3 reg address */ 47 #define RT9455_REG_DEV_ID 0x03 /* DEV_ID reg address */ 48 #define RT9455_REG_CTRL4 0x04 /* CTRL4 reg address */ 49 #define RT9455_REG_CTRL5 0x05 /* CTRL5 reg address */ 50 #define RT9455_REG_CTRL6 0x06 /* CTRL6 reg address */ 51 #define RT9455_REG_CTRL7 0x07 /* CTRL7 reg address */ 52 #define RT9455_REG_IRQ1 0x08 /* IRQ1 reg address */ 53 #define RT9455_REG_IRQ2 0x09 /* IRQ2 reg address */ 54 #define RT9455_REG_IRQ3 0x0A /* IRQ3 reg address */ 55 #define RT9455_REG_MASK1 0x0B /* MASK1 reg address */ 56 #define RT9455_REG_MASK2 0x0C /* MASK2 reg address */ 57 #define RT9455_REG_MASK3 0x0D /* MASK3 reg address */ 58 59 enum rt9455_fields { 60 F_STAT, F_BOOST, F_PWR_RDY, F_OTG_PIN_POLARITY, /* CTRL1 reg fields */ 61 62 F_IAICR, F_TE_SHDN_EN, F_HIGHER_OCP, F_TE, F_IAICR_INT, F_HIZ, 63 F_OPA_MODE, /* CTRL2 reg fields */ 64 65 F_VOREG, F_OTG_PL, F_OTG_EN, /* CTRL3 reg fields */ 66 67 F_VENDOR_ID, F_CHIP_REV, /* DEV_ID reg fields */ 68 69 F_RST, /* CTRL4 reg fields */ 70 71 F_TMR_EN, F_MIVR, F_IPREC, F_IEOC_PERCENTAGE, /* CTRL5 reg fields*/ 72 73 F_IAICR_SEL, F_ICHRG, F_VPREC, /* CTRL6 reg fields */ 74 75 F_BATD_EN, F_CHG_EN, F_VMREG, /* CTRL7 reg fields */ 76 77 F_TSDI, F_VINOVPI, F_BATAB, /* IRQ1 reg fields */ 78 79 F_CHRVPI, F_CHBATOVI, F_CHTERMI, F_CHRCHGI, F_CH32MI, F_CHTREGI, 80 F_CHMIVRI, /* IRQ2 reg fields */ 81 82 F_BSTBUSOVI, F_BSTOLI, F_BSTLOWVI, F_BST32SI, /* IRQ3 reg fields */ 83 84 F_TSDM, F_VINOVPIM, F_BATABM, /* MASK1 reg fields */ 85 86 F_CHRVPIM, F_CHBATOVIM, F_CHTERMIM, F_CHRCHGIM, F_CH32MIM, F_CHTREGIM, 87 F_CHMIVRIM, /* MASK2 reg fields */ 88 89 F_BSTVINOVIM, F_BSTOLIM, F_BSTLOWVIM, F_BST32SIM, /* MASK3 reg fields */ 90 91 F_MAX_FIELDS 92 }; 93 94 static const struct reg_field rt9455_reg_fields[] = { 95 [F_STAT] = REG_FIELD(RT9455_REG_CTRL1, 4, 5), 96 [F_BOOST] = REG_FIELD(RT9455_REG_CTRL1, 3, 3), 97 [F_PWR_RDY] = REG_FIELD(RT9455_REG_CTRL1, 2, 2), 98 [F_OTG_PIN_POLARITY] = REG_FIELD(RT9455_REG_CTRL1, 1, 1), 99 100 [F_IAICR] = REG_FIELD(RT9455_REG_CTRL2, 6, 7), 101 [F_TE_SHDN_EN] = REG_FIELD(RT9455_REG_CTRL2, 5, 5), 102 [F_HIGHER_OCP] = REG_FIELD(RT9455_REG_CTRL2, 4, 4), 103 [F_TE] = REG_FIELD(RT9455_REG_CTRL2, 3, 3), 104 [F_IAICR_INT] = REG_FIELD(RT9455_REG_CTRL2, 2, 2), 105 [F_HIZ] = REG_FIELD(RT9455_REG_CTRL2, 1, 1), 106 [F_OPA_MODE] = REG_FIELD(RT9455_REG_CTRL2, 0, 0), 107 108 [F_VOREG] = REG_FIELD(RT9455_REG_CTRL3, 2, 7), 109 [F_OTG_PL] = REG_FIELD(RT9455_REG_CTRL3, 1, 1), 110 [F_OTG_EN] = REG_FIELD(RT9455_REG_CTRL3, 0, 0), 111 112 [F_VENDOR_ID] = REG_FIELD(RT9455_REG_DEV_ID, 4, 7), 113 [F_CHIP_REV] = REG_FIELD(RT9455_REG_DEV_ID, 0, 3), 114 115 [F_RST] = REG_FIELD(RT9455_REG_CTRL4, 7, 7), 116 117 [F_TMR_EN] = REG_FIELD(RT9455_REG_CTRL5, 7, 7), 118 [F_MIVR] = REG_FIELD(RT9455_REG_CTRL5, 4, 5), 119 [F_IPREC] = REG_FIELD(RT9455_REG_CTRL5, 2, 3), 120 [F_IEOC_PERCENTAGE] = REG_FIELD(RT9455_REG_CTRL5, 0, 1), 121 122 [F_IAICR_SEL] = REG_FIELD(RT9455_REG_CTRL6, 7, 7), 123 [F_ICHRG] = REG_FIELD(RT9455_REG_CTRL6, 4, 6), 124 [F_VPREC] = REG_FIELD(RT9455_REG_CTRL6, 0, 2), 125 126 [F_BATD_EN] = REG_FIELD(RT9455_REG_CTRL7, 6, 6), 127 [F_CHG_EN] = REG_FIELD(RT9455_REG_CTRL7, 4, 4), 128 [F_VMREG] = REG_FIELD(RT9455_REG_CTRL7, 0, 3), 129 130 [F_TSDI] = REG_FIELD(RT9455_REG_IRQ1, 7, 7), 131 [F_VINOVPI] = REG_FIELD(RT9455_REG_IRQ1, 6, 6), 132 [F_BATAB] = REG_FIELD(RT9455_REG_IRQ1, 0, 0), 133 134 [F_CHRVPI] = REG_FIELD(RT9455_REG_IRQ2, 7, 7), 135 [F_CHBATOVI] = REG_FIELD(RT9455_REG_IRQ2, 5, 5), 136 [F_CHTERMI] = REG_FIELD(RT9455_REG_IRQ2, 4, 4), 137 [F_CHRCHGI] = REG_FIELD(RT9455_REG_IRQ2, 3, 3), 138 [F_CH32MI] = REG_FIELD(RT9455_REG_IRQ2, 2, 2), 139 [F_CHTREGI] = REG_FIELD(RT9455_REG_IRQ2, 1, 1), 140 [F_CHMIVRI] = REG_FIELD(RT9455_REG_IRQ2, 0, 0), 141 142 [F_BSTBUSOVI] = REG_FIELD(RT9455_REG_IRQ3, 7, 7), 143 [F_BSTOLI] = REG_FIELD(RT9455_REG_IRQ3, 6, 6), 144 [F_BSTLOWVI] = REG_FIELD(RT9455_REG_IRQ3, 5, 5), 145 [F_BST32SI] = REG_FIELD(RT9455_REG_IRQ3, 3, 3), 146 147 [F_TSDM] = REG_FIELD(RT9455_REG_MASK1, 7, 7), 148 [F_VINOVPIM] = REG_FIELD(RT9455_REG_MASK1, 6, 6), 149 [F_BATABM] = REG_FIELD(RT9455_REG_MASK1, 0, 0), 150 151 [F_CHRVPIM] = REG_FIELD(RT9455_REG_MASK2, 7, 7), 152 [F_CHBATOVIM] = REG_FIELD(RT9455_REG_MASK2, 5, 5), 153 [F_CHTERMIM] = REG_FIELD(RT9455_REG_MASK2, 4, 4), 154 [F_CHRCHGIM] = REG_FIELD(RT9455_REG_MASK2, 3, 3), 155 [F_CH32MIM] = REG_FIELD(RT9455_REG_MASK2, 2, 2), 156 [F_CHTREGIM] = REG_FIELD(RT9455_REG_MASK2, 1, 1), 157 [F_CHMIVRIM] = REG_FIELD(RT9455_REG_MASK2, 0, 0), 158 159 [F_BSTVINOVIM] = REG_FIELD(RT9455_REG_MASK3, 7, 7), 160 [F_BSTOLIM] = REG_FIELD(RT9455_REG_MASK3, 6, 6), 161 [F_BSTLOWVIM] = REG_FIELD(RT9455_REG_MASK3, 5, 5), 162 [F_BST32SIM] = REG_FIELD(RT9455_REG_MASK3, 3, 3), 163 }; 164 165 #define GET_MASK(fid) (BIT(rt9455_reg_fields[fid].msb + 1) - \ 166 BIT(rt9455_reg_fields[fid].lsb)) 167 168 /* 169 * Each array initialised below shows the possible real-world values for a 170 * group of bits belonging to RT9455 registers. The arrays are sorted in 171 * ascending order. The index of each real-world value represents the value 172 * that is encoded in the group of bits belonging to RT9455 registers. 173 */ 174 /* REG06[6:4] (ICHRG) in uAh */ 175 static const int rt9455_ichrg_values[] = { 176 500000, 650000, 800000, 950000, 1100000, 1250000, 1400000, 1550000 177 }; 178 179 /* 180 * When the charger is in charge mode, REG02[7:2] represent battery regulation 181 * voltage. 182 */ 183 /* REG02[7:2] (VOREG) in uV */ 184 static const int rt9455_voreg_values[] = { 185 3500000, 3520000, 3540000, 3560000, 3580000, 3600000, 3620000, 3640000, 186 3660000, 3680000, 3700000, 3720000, 3740000, 3760000, 3780000, 3800000, 187 3820000, 3840000, 3860000, 3880000, 3900000, 3920000, 3940000, 3960000, 188 3980000, 4000000, 4020000, 4040000, 4060000, 4080000, 4100000, 4120000, 189 4140000, 4160000, 4180000, 4200000, 4220000, 4240000, 4260000, 4280000, 190 4300000, 4330000, 4350000, 4370000, 4390000, 4410000, 4430000, 4450000, 191 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 192 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000 193 }; 194 195 /* 196 * When the charger is in boost mode, REG02[7:2] represent boost output 197 * voltage. 198 */ 199 /* REG02[7:2] (Boost output voltage) in uV */ 200 static const int rt9455_boost_voltage_values[] = { 201 4425000, 4450000, 4475000, 4500000, 4525000, 4550000, 4575000, 4600000, 202 4625000, 4650000, 4675000, 4700000, 4725000, 4750000, 4775000, 4800000, 203 4825000, 4850000, 4875000, 4900000, 4925000, 4950000, 4975000, 5000000, 204 5025000, 5050000, 5075000, 5100000, 5125000, 5150000, 5175000, 5200000, 205 5225000, 5250000, 5275000, 5300000, 5325000, 5350000, 5375000, 5400000, 206 5425000, 5450000, 5475000, 5500000, 5525000, 5550000, 5575000, 5600000, 207 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 208 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 209 }; 210 211 /* REG07[3:0] (VMREG) in uV */ 212 static const int rt9455_vmreg_values[] = { 213 4200000, 4220000, 4240000, 4260000, 4280000, 4300000, 4320000, 4340000, 214 4360000, 4380000, 4400000, 4430000, 4450000, 4450000, 4450000, 4450000 215 }; 216 217 /* REG05[5:4] (IEOC_PERCENTAGE) */ 218 static const int rt9455_ieoc_percentage_values[] = { 219 10, 30, 20, 30 220 }; 221 222 /* REG05[1:0] (MIVR) in uV */ 223 static const int rt9455_mivr_values[] = { 224 4000000, 4250000, 4500000, 5000000 225 }; 226 227 /* REG05[1:0] (IAICR) in uA */ 228 static const int rt9455_iaicr_values[] = { 229 100000, 500000, 1000000, 2000000 230 }; 231 232 struct rt9455_info { 233 struct i2c_client *client; 234 struct regmap *regmap; 235 struct regmap_field *regmap_fields[F_MAX_FIELDS]; 236 struct power_supply *charger; 237 #if IS_ENABLED(CONFIG_USB_PHY) 238 struct usb_phy *usb_phy; 239 struct notifier_block nb; 240 #endif 241 struct delayed_work pwr_rdy_work; 242 struct delayed_work max_charging_time_work; 243 struct delayed_work batt_presence_work; 244 u32 voreg; 245 u32 boost_voltage; 246 }; 247 248 /* 249 * Iterate through each element of the 'tbl' array until an element whose value 250 * is greater than v is found. Return the index of the respective element, 251 * or the index of the last element in the array, if no such element is found. 252 */ 253 static unsigned int rt9455_find_idx(const int tbl[], int tbl_size, int v) 254 { 255 int i; 256 257 /* 258 * No need to iterate until the last index in the table because 259 * if no element greater than v is found in the table, 260 * or if only the last element is greater than v, 261 * function returns the index of the last element. 262 */ 263 for (i = 0; i < tbl_size - 1; i++) 264 if (v <= tbl[i]) 265 return i; 266 267 return (tbl_size - 1); 268 } 269 270 static int rt9455_get_field_val(struct rt9455_info *info, 271 enum rt9455_fields field, 272 const int tbl[], int tbl_size, int *val) 273 { 274 unsigned int v; 275 int ret; 276 277 ret = regmap_field_read(info->regmap_fields[field], &v); 278 if (ret) 279 return ret; 280 281 v = (v >= tbl_size) ? (tbl_size - 1) : v; 282 *val = tbl[v]; 283 284 return 0; 285 } 286 287 static int rt9455_set_field_val(struct rt9455_info *info, 288 enum rt9455_fields field, 289 const int tbl[], int tbl_size, int val) 290 { 291 unsigned int idx = rt9455_find_idx(tbl, tbl_size, val); 292 293 return regmap_field_write(info->regmap_fields[field], idx); 294 } 295 296 static int rt9455_register_reset(struct rt9455_info *info) 297 { 298 struct device *dev = &info->client->dev; 299 unsigned int v; 300 int ret, limit = 100; 301 302 ret = regmap_field_write(info->regmap_fields[F_RST], 0x01); 303 if (ret) { 304 dev_err(dev, "Failed to set RST bit\n"); 305 return ret; 306 } 307 308 /* 309 * To make sure that reset operation has finished, loop until RST bit 310 * is set to 0. 311 */ 312 do { 313 ret = regmap_field_read(info->regmap_fields[F_RST], &v); 314 if (ret) { 315 dev_err(dev, "Failed to read RST bit\n"); 316 return ret; 317 } 318 319 if (!v) 320 break; 321 322 usleep_range(10, 100); 323 } while (--limit); 324 325 if (!limit) 326 return -EIO; 327 328 return 0; 329 } 330 331 /* Charger power supply property routines */ 332 static enum power_supply_property rt9455_charger_properties[] = { 333 POWER_SUPPLY_PROP_STATUS, 334 POWER_SUPPLY_PROP_HEALTH, 335 POWER_SUPPLY_PROP_PRESENT, 336 POWER_SUPPLY_PROP_ONLINE, 337 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, 338 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 339 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, 340 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 341 POWER_SUPPLY_PROP_SCOPE, 342 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT, 343 POWER_SUPPLY_PROP_MODEL_NAME, 344 POWER_SUPPLY_PROP_MANUFACTURER, 345 }; 346 347 static char *rt9455_charger_supplied_to[] = { 348 "main-battery", 349 }; 350 351 static int rt9455_charger_get_status(struct rt9455_info *info, 352 union power_supply_propval *val) 353 { 354 unsigned int v, pwr_rdy; 355 int ret; 356 357 ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], 358 &pwr_rdy); 359 if (ret) { 360 dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n"); 361 return ret; 362 } 363 364 /* 365 * If PWR_RDY bit is unset, the battery is discharging. Otherwise, 366 * STAT bits value must be checked. 367 */ 368 if (!pwr_rdy) { 369 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 370 return 0; 371 } 372 373 ret = regmap_field_read(info->regmap_fields[F_STAT], &v); 374 if (ret) { 375 dev_err(&info->client->dev, "Failed to read STAT bits\n"); 376 return ret; 377 } 378 379 switch (v) { 380 case 0: 381 /* 382 * If PWR_RDY bit is set, but STAT bits value is 0, the charger 383 * may be in one of the following cases: 384 * 1. CHG_EN bit is 0. 385 * 2. CHG_EN bit is 1 but the battery is not connected. 386 * In any of these cases, POWER_SUPPLY_STATUS_NOT_CHARGING is 387 * returned. 388 */ 389 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; 390 return 0; 391 case 1: 392 val->intval = POWER_SUPPLY_STATUS_CHARGING; 393 return 0; 394 case 2: 395 val->intval = POWER_SUPPLY_STATUS_FULL; 396 return 0; 397 default: 398 val->intval = POWER_SUPPLY_STATUS_UNKNOWN; 399 return 0; 400 } 401 } 402 403 static int rt9455_charger_get_health(struct rt9455_info *info, 404 union power_supply_propval *val) 405 { 406 struct device *dev = &info->client->dev; 407 unsigned int v; 408 int ret; 409 410 val->intval = POWER_SUPPLY_HEALTH_GOOD; 411 412 ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &v); 413 if (ret) { 414 dev_err(dev, "Failed to read IRQ1 register\n"); 415 return ret; 416 } 417 418 if (v & GET_MASK(F_TSDI)) { 419 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; 420 return 0; 421 } 422 if (v & GET_MASK(F_VINOVPI)) { 423 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 424 return 0; 425 } 426 if (v & GET_MASK(F_BATAB)) { 427 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 428 return 0; 429 } 430 431 ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &v); 432 if (ret) { 433 dev_err(dev, "Failed to read IRQ2 register\n"); 434 return ret; 435 } 436 437 if (v & GET_MASK(F_CHBATOVI)) { 438 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 439 return 0; 440 } 441 if (v & GET_MASK(F_CH32MI)) { 442 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE; 443 return 0; 444 } 445 446 ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &v); 447 if (ret) { 448 dev_err(dev, "Failed to read IRQ3 register\n"); 449 return ret; 450 } 451 452 if (v & GET_MASK(F_BSTBUSOVI)) { 453 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 454 return 0; 455 } 456 if (v & GET_MASK(F_BSTOLI)) { 457 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 458 return 0; 459 } 460 if (v & GET_MASK(F_BSTLOWVI)) { 461 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 462 return 0; 463 } 464 if (v & GET_MASK(F_BST32SI)) { 465 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE; 466 return 0; 467 } 468 469 ret = regmap_field_read(info->regmap_fields[F_STAT], &v); 470 if (ret) { 471 dev_err(dev, "Failed to read STAT bits\n"); 472 return ret; 473 } 474 475 if (v == RT9455_FAULT) { 476 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 477 return 0; 478 } 479 480 return 0; 481 } 482 483 static int rt9455_charger_get_battery_presence(struct rt9455_info *info, 484 union power_supply_propval *val) 485 { 486 unsigned int v; 487 int ret; 488 489 ret = regmap_field_read(info->regmap_fields[F_BATAB], &v); 490 if (ret) { 491 dev_err(&info->client->dev, "Failed to read BATAB bit\n"); 492 return ret; 493 } 494 495 /* 496 * Since BATAB is 1 when battery is NOT present and 0 otherwise, 497 * !BATAB is returned. 498 */ 499 val->intval = !v; 500 501 return 0; 502 } 503 504 static int rt9455_charger_get_online(struct rt9455_info *info, 505 union power_supply_propval *val) 506 { 507 unsigned int v; 508 int ret; 509 510 ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &v); 511 if (ret) { 512 dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n"); 513 return ret; 514 } 515 516 val->intval = (int)v; 517 518 return 0; 519 } 520 521 static int rt9455_charger_get_current(struct rt9455_info *info, 522 union power_supply_propval *val) 523 { 524 int curr; 525 int ret; 526 527 ret = rt9455_get_field_val(info, F_ICHRG, 528 rt9455_ichrg_values, 529 ARRAY_SIZE(rt9455_ichrg_values), 530 &curr); 531 if (ret) { 532 dev_err(&info->client->dev, "Failed to read ICHRG value\n"); 533 return ret; 534 } 535 536 val->intval = curr; 537 538 return 0; 539 } 540 541 static int rt9455_charger_get_current_max(struct rt9455_info *info, 542 union power_supply_propval *val) 543 { 544 int idx = ARRAY_SIZE(rt9455_ichrg_values) - 1; 545 546 val->intval = rt9455_ichrg_values[idx]; 547 548 return 0; 549 } 550 551 static int rt9455_charger_get_voltage(struct rt9455_info *info, 552 union power_supply_propval *val) 553 { 554 int voltage; 555 int ret; 556 557 ret = rt9455_get_field_val(info, F_VOREG, 558 rt9455_voreg_values, 559 ARRAY_SIZE(rt9455_voreg_values), 560 &voltage); 561 if (ret) { 562 dev_err(&info->client->dev, "Failed to read VOREG value\n"); 563 return ret; 564 } 565 566 val->intval = voltage; 567 568 return 0; 569 } 570 571 static int rt9455_charger_get_voltage_max(struct rt9455_info *info, 572 union power_supply_propval *val) 573 { 574 int idx = ARRAY_SIZE(rt9455_vmreg_values) - 1; 575 576 val->intval = rt9455_vmreg_values[idx]; 577 578 return 0; 579 } 580 581 static int rt9455_charger_get_term_current(struct rt9455_info *info, 582 union power_supply_propval *val) 583 { 584 struct device *dev = &info->client->dev; 585 int ichrg, ieoc_percentage, ret; 586 587 ret = rt9455_get_field_val(info, F_ICHRG, 588 rt9455_ichrg_values, 589 ARRAY_SIZE(rt9455_ichrg_values), 590 &ichrg); 591 if (ret) { 592 dev_err(dev, "Failed to read ICHRG value\n"); 593 return ret; 594 } 595 596 ret = rt9455_get_field_val(info, F_IEOC_PERCENTAGE, 597 rt9455_ieoc_percentage_values, 598 ARRAY_SIZE(rt9455_ieoc_percentage_values), 599 &ieoc_percentage); 600 if (ret) { 601 dev_err(dev, "Failed to read IEOC value\n"); 602 return ret; 603 } 604 605 val->intval = ichrg * ieoc_percentage / 100; 606 607 return 0; 608 } 609 610 static int rt9455_charger_get_property(struct power_supply *psy, 611 enum power_supply_property psp, 612 union power_supply_propval *val) 613 { 614 struct rt9455_info *info = power_supply_get_drvdata(psy); 615 616 switch (psp) { 617 case POWER_SUPPLY_PROP_STATUS: 618 return rt9455_charger_get_status(info, val); 619 case POWER_SUPPLY_PROP_HEALTH: 620 return rt9455_charger_get_health(info, val); 621 case POWER_SUPPLY_PROP_PRESENT: 622 return rt9455_charger_get_battery_presence(info, val); 623 case POWER_SUPPLY_PROP_ONLINE: 624 return rt9455_charger_get_online(info, val); 625 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 626 return rt9455_charger_get_current(info, val); 627 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: 628 return rt9455_charger_get_current_max(info, val); 629 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 630 return rt9455_charger_get_voltage(info, val); 631 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: 632 return rt9455_charger_get_voltage_max(info, val); 633 case POWER_SUPPLY_PROP_SCOPE: 634 val->intval = POWER_SUPPLY_SCOPE_SYSTEM; 635 return 0; 636 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: 637 return rt9455_charger_get_term_current(info, val); 638 case POWER_SUPPLY_PROP_MODEL_NAME: 639 val->strval = RT9455_MODEL_NAME; 640 return 0; 641 case POWER_SUPPLY_PROP_MANUFACTURER: 642 val->strval = RT9455_MANUFACTURER; 643 return 0; 644 default: 645 return -ENODATA; 646 } 647 } 648 649 static int rt9455_hw_init(struct rt9455_info *info, u32 ichrg, 650 u32 ieoc_percentage, 651 u32 mivr, u32 iaicr) 652 { 653 struct device *dev = &info->client->dev; 654 int idx, ret; 655 656 ret = rt9455_register_reset(info); 657 if (ret) { 658 dev_err(dev, "Power On Reset failed\n"); 659 return ret; 660 } 661 662 /* Set TE bit in order to enable end of charge detection */ 663 ret = regmap_field_write(info->regmap_fields[F_TE], 1); 664 if (ret) { 665 dev_err(dev, "Failed to set TE bit\n"); 666 return ret; 667 } 668 669 /* Set TE_SHDN_EN bit in order to enable end of charge detection */ 670 ret = regmap_field_write(info->regmap_fields[F_TE_SHDN_EN], 1); 671 if (ret) { 672 dev_err(dev, "Failed to set TE_SHDN_EN bit\n"); 673 return ret; 674 } 675 676 /* 677 * Set BATD_EN bit in order to enable battery detection 678 * when charging is done 679 */ 680 ret = regmap_field_write(info->regmap_fields[F_BATD_EN], 1); 681 if (ret) { 682 dev_err(dev, "Failed to set BATD_EN bit\n"); 683 return ret; 684 } 685 686 /* 687 * Disable Safety Timer. In charge mode, this timer terminates charging 688 * if no read or write via I2C is done within 32 minutes. This timer 689 * avoids overcharging the baterry when the OS is not loaded and the 690 * charger is connected to a power source. 691 * In boost mode, this timer triggers BST32SI interrupt if no read or 692 * write via I2C is done within 32 seconds. 693 * When the OS is loaded and the charger driver is inserted, it is used 694 * delayed_work, named max_charging_time_work, to avoid overcharging 695 * the battery. 696 */ 697 ret = regmap_field_write(info->regmap_fields[F_TMR_EN], 0x00); 698 if (ret) { 699 dev_err(dev, "Failed to disable Safety Timer\n"); 700 return ret; 701 } 702 703 /* Set ICHRG to value retrieved from device-specific data */ 704 ret = rt9455_set_field_val(info, F_ICHRG, 705 rt9455_ichrg_values, 706 ARRAY_SIZE(rt9455_ichrg_values), ichrg); 707 if (ret) { 708 dev_err(dev, "Failed to set ICHRG value\n"); 709 return ret; 710 } 711 712 /* Set IEOC Percentage to value retrieved from device-specific data */ 713 ret = rt9455_set_field_val(info, F_IEOC_PERCENTAGE, 714 rt9455_ieoc_percentage_values, 715 ARRAY_SIZE(rt9455_ieoc_percentage_values), 716 ieoc_percentage); 717 if (ret) { 718 dev_err(dev, "Failed to set IEOC Percentage value\n"); 719 return ret; 720 } 721 722 /* Set VOREG to value retrieved from device-specific data */ 723 ret = rt9455_set_field_val(info, F_VOREG, 724 rt9455_voreg_values, 725 ARRAY_SIZE(rt9455_voreg_values), 726 info->voreg); 727 if (ret) { 728 dev_err(dev, "Failed to set VOREG value\n"); 729 return ret; 730 } 731 732 /* Set VMREG value to maximum (4.45V). */ 733 idx = ARRAY_SIZE(rt9455_vmreg_values) - 1; 734 ret = rt9455_set_field_val(info, F_VMREG, 735 rt9455_vmreg_values, 736 ARRAY_SIZE(rt9455_vmreg_values), 737 rt9455_vmreg_values[idx]); 738 if (ret) { 739 dev_err(dev, "Failed to set VMREG value\n"); 740 return ret; 741 } 742 743 /* 744 * Set MIVR to value retrieved from device-specific data. 745 * If no value is specified, default value for MIVR is 4.5V. 746 */ 747 if (mivr == -1) 748 mivr = 4500000; 749 750 ret = rt9455_set_field_val(info, F_MIVR, 751 rt9455_mivr_values, 752 ARRAY_SIZE(rt9455_mivr_values), mivr); 753 if (ret) { 754 dev_err(dev, "Failed to set MIVR value\n"); 755 return ret; 756 } 757 758 /* 759 * Set IAICR to value retrieved from device-specific data. 760 * If no value is specified, default value for IAICR is 500 mA. 761 */ 762 if (iaicr == -1) 763 iaicr = 500000; 764 765 ret = rt9455_set_field_val(info, F_IAICR, 766 rt9455_iaicr_values, 767 ARRAY_SIZE(rt9455_iaicr_values), iaicr); 768 if (ret) { 769 dev_err(dev, "Failed to set IAICR value\n"); 770 return ret; 771 } 772 773 /* 774 * Set IAICR_INT bit so that IAICR value is determined by IAICR bits 775 * and not by OTG pin. 776 */ 777 ret = regmap_field_write(info->regmap_fields[F_IAICR_INT], 0x01); 778 if (ret) { 779 dev_err(dev, "Failed to set IAICR_INT bit\n"); 780 return ret; 781 } 782 783 /* 784 * Disable CHMIVRI interrupt. Because the driver sets MIVR value, 785 * CHMIVRI is triggered, but there is no action to be taken by the 786 * driver when CHMIVRI is triggered. 787 */ 788 ret = regmap_field_write(info->regmap_fields[F_CHMIVRIM], 0x01); 789 if (ret) { 790 dev_err(dev, "Failed to mask CHMIVRI interrupt\n"); 791 return ret; 792 } 793 794 return 0; 795 } 796 797 #if IS_ENABLED(CONFIG_USB_PHY) 798 /* 799 * Before setting the charger into boost mode, boost output voltage is 800 * set. This is needed because boost output voltage may differ from battery 801 * regulation voltage. F_VOREG bits represent either battery regulation voltage 802 * or boost output voltage, depending on the mode the charger is. Both battery 803 * regulation voltage and boost output voltage are read from DT/ACPI during 804 * probe. 805 */ 806 static int rt9455_set_boost_voltage_before_boost_mode(struct rt9455_info *info) 807 { 808 struct device *dev = &info->client->dev; 809 int ret; 810 811 ret = rt9455_set_field_val(info, F_VOREG, 812 rt9455_boost_voltage_values, 813 ARRAY_SIZE(rt9455_boost_voltage_values), 814 info->boost_voltage); 815 if (ret) { 816 dev_err(dev, "Failed to set boost output voltage value\n"); 817 return ret; 818 } 819 820 return 0; 821 } 822 #endif 823 824 /* 825 * Before setting the charger into charge mode, battery regulation voltage is 826 * set. This is needed because boost output voltage may differ from battery 827 * regulation voltage. F_VOREG bits represent either battery regulation voltage 828 * or boost output voltage, depending on the mode the charger is. Both battery 829 * regulation voltage and boost output voltage are read from DT/ACPI during 830 * probe. 831 */ 832 static int rt9455_set_voreg_before_charge_mode(struct rt9455_info *info) 833 { 834 struct device *dev = &info->client->dev; 835 int ret; 836 837 ret = rt9455_set_field_val(info, F_VOREG, 838 rt9455_voreg_values, 839 ARRAY_SIZE(rt9455_voreg_values), 840 info->voreg); 841 if (ret) { 842 dev_err(dev, "Failed to set VOREG value\n"); 843 return ret; 844 } 845 846 return 0; 847 } 848 849 static int rt9455_irq_handler_check_irq1_register(struct rt9455_info *info, 850 bool *_is_battery_absent, 851 bool *_alert_userspace) 852 { 853 unsigned int irq1, mask1, mask2; 854 struct device *dev = &info->client->dev; 855 bool is_battery_absent = false; 856 bool alert_userspace = false; 857 int ret; 858 859 ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1); 860 if (ret) { 861 dev_err(dev, "Failed to read IRQ1 register\n"); 862 return ret; 863 } 864 865 ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1); 866 if (ret) { 867 dev_err(dev, "Failed to read MASK1 register\n"); 868 return ret; 869 } 870 871 if (irq1 & GET_MASK(F_TSDI)) { 872 dev_err(dev, "Thermal shutdown fault occurred\n"); 873 alert_userspace = true; 874 } 875 876 if (irq1 & GET_MASK(F_VINOVPI)) { 877 dev_err(dev, "Overvoltage input occurred\n"); 878 alert_userspace = true; 879 } 880 881 if (irq1 & GET_MASK(F_BATAB)) { 882 dev_err(dev, "Battery absence occurred\n"); 883 is_battery_absent = true; 884 alert_userspace = true; 885 886 if ((mask1 & GET_MASK(F_BATABM)) == 0) { 887 ret = regmap_field_write(info->regmap_fields[F_BATABM], 888 0x01); 889 if (ret) { 890 dev_err(dev, "Failed to mask BATAB interrupt\n"); 891 return ret; 892 } 893 } 894 895 ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2); 896 if (ret) { 897 dev_err(dev, "Failed to read MASK2 register\n"); 898 return ret; 899 } 900 901 if (mask2 & GET_MASK(F_CHTERMIM)) { 902 ret = regmap_field_write( 903 info->regmap_fields[F_CHTERMIM], 0x00); 904 if (ret) { 905 dev_err(dev, "Failed to unmask CHTERMI interrupt\n"); 906 return ret; 907 } 908 } 909 910 if (mask2 & GET_MASK(F_CHRCHGIM)) { 911 ret = regmap_field_write( 912 info->regmap_fields[F_CHRCHGIM], 0x00); 913 if (ret) { 914 dev_err(dev, "Failed to unmask CHRCHGI interrupt\n"); 915 return ret; 916 } 917 } 918 919 /* 920 * When the battery is absent, max_charging_time_work is 921 * cancelled, since no charging is done. 922 */ 923 cancel_delayed_work_sync(&info->max_charging_time_work); 924 /* 925 * Since no interrupt is triggered when the battery is 926 * reconnected, max_charging_time_work is not rescheduled. 927 * Therefore, batt_presence_work is scheduled to check whether 928 * the battery is still absent or not. 929 */ 930 queue_delayed_work(system_power_efficient_wq, 931 &info->batt_presence_work, 932 RT9455_BATT_PRESENCE_DELAY * HZ); 933 } 934 935 *_is_battery_absent = is_battery_absent; 936 937 if (alert_userspace) 938 *_alert_userspace = alert_userspace; 939 940 return 0; 941 } 942 943 static int rt9455_irq_handler_check_irq2_register(struct rt9455_info *info, 944 bool is_battery_absent, 945 bool *_alert_userspace) 946 { 947 unsigned int irq2, mask2; 948 struct device *dev = &info->client->dev; 949 bool alert_userspace = false; 950 int ret; 951 952 ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &irq2); 953 if (ret) { 954 dev_err(dev, "Failed to read IRQ2 register\n"); 955 return ret; 956 } 957 958 ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2); 959 if (ret) { 960 dev_err(dev, "Failed to read MASK2 register\n"); 961 return ret; 962 } 963 964 if (irq2 & GET_MASK(F_CHRVPI)) { 965 dev_dbg(dev, "Charger fault occurred\n"); 966 /* 967 * CHRVPI bit is set in 2 cases: 968 * 1. when the power source is connected to the charger. 969 * 2. when the power source is disconnected from the charger. 970 * To identify the case, PWR_RDY bit is checked. Because 971 * PWR_RDY bit is set / cleared after CHRVPI interrupt is 972 * triggered, it is used delayed_work to later read PWR_RDY bit. 973 * Also, do not set to true alert_userspace, because there is no 974 * need to notify userspace when CHRVPI interrupt has occurred. 975 * Userspace will be notified after PWR_RDY bit is read. 976 */ 977 queue_delayed_work(system_power_efficient_wq, 978 &info->pwr_rdy_work, 979 RT9455_PWR_RDY_DELAY * HZ); 980 } 981 if (irq2 & GET_MASK(F_CHBATOVI)) { 982 dev_err(dev, "Battery OVP occurred\n"); 983 alert_userspace = true; 984 } 985 if (irq2 & GET_MASK(F_CHTERMI)) { 986 dev_dbg(dev, "Charge terminated\n"); 987 if (!is_battery_absent) { 988 if ((mask2 & GET_MASK(F_CHTERMIM)) == 0) { 989 ret = regmap_field_write( 990 info->regmap_fields[F_CHTERMIM], 0x01); 991 if (ret) { 992 dev_err(dev, "Failed to mask CHTERMI interrupt\n"); 993 return ret; 994 } 995 /* 996 * Update MASK2 value, since CHTERMIM bit is 997 * set. 998 */ 999 mask2 = mask2 | GET_MASK(F_CHTERMIM); 1000 } 1001 cancel_delayed_work_sync(&info->max_charging_time_work); 1002 alert_userspace = true; 1003 } 1004 } 1005 if (irq2 & GET_MASK(F_CHRCHGI)) { 1006 dev_dbg(dev, "Recharge request\n"); 1007 ret = regmap_field_write(info->regmap_fields[F_CHG_EN], 1008 RT9455_CHARGE_ENABLE); 1009 if (ret) { 1010 dev_err(dev, "Failed to enable charging\n"); 1011 return ret; 1012 } 1013 if (mask2 & GET_MASK(F_CHTERMIM)) { 1014 ret = regmap_field_write( 1015 info->regmap_fields[F_CHTERMIM], 0x00); 1016 if (ret) { 1017 dev_err(dev, "Failed to unmask CHTERMI interrupt\n"); 1018 return ret; 1019 } 1020 /* Update MASK2 value, since CHTERMIM bit is cleared. */ 1021 mask2 = mask2 & ~GET_MASK(F_CHTERMIM); 1022 } 1023 if (!is_battery_absent) { 1024 /* 1025 * No need to check whether the charger is connected to 1026 * power source when CHRCHGI is received, since CHRCHGI 1027 * is not triggered if the charger is not connected to 1028 * the power source. 1029 */ 1030 queue_delayed_work(system_power_efficient_wq, 1031 &info->max_charging_time_work, 1032 RT9455_MAX_CHARGING_TIME * HZ); 1033 alert_userspace = true; 1034 } 1035 } 1036 if (irq2 & GET_MASK(F_CH32MI)) { 1037 dev_err(dev, "Charger fault. 32 mins timeout occurred\n"); 1038 alert_userspace = true; 1039 } 1040 if (irq2 & GET_MASK(F_CHTREGI)) { 1041 dev_warn(dev, 1042 "Charger warning. Thermal regulation loop active\n"); 1043 alert_userspace = true; 1044 } 1045 if (irq2 & GET_MASK(F_CHMIVRI)) { 1046 dev_dbg(dev, 1047 "Charger warning. Input voltage MIVR loop active\n"); 1048 } 1049 1050 if (alert_userspace) 1051 *_alert_userspace = alert_userspace; 1052 1053 return 0; 1054 } 1055 1056 static int rt9455_irq_handler_check_irq3_register(struct rt9455_info *info, 1057 bool *_alert_userspace) 1058 { 1059 unsigned int irq3, mask3; 1060 struct device *dev = &info->client->dev; 1061 bool alert_userspace = false; 1062 int ret; 1063 1064 ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &irq3); 1065 if (ret) { 1066 dev_err(dev, "Failed to read IRQ3 register\n"); 1067 return ret; 1068 } 1069 1070 ret = regmap_read(info->regmap, RT9455_REG_MASK3, &mask3); 1071 if (ret) { 1072 dev_err(dev, "Failed to read MASK3 register\n"); 1073 return ret; 1074 } 1075 1076 if (irq3 & GET_MASK(F_BSTBUSOVI)) { 1077 dev_err(dev, "Boost fault. Overvoltage input occurred\n"); 1078 alert_userspace = true; 1079 } 1080 if (irq3 & GET_MASK(F_BSTOLI)) { 1081 dev_err(dev, "Boost fault. Overload\n"); 1082 alert_userspace = true; 1083 } 1084 if (irq3 & GET_MASK(F_BSTLOWVI)) { 1085 dev_err(dev, "Boost fault. Battery voltage too low\n"); 1086 alert_userspace = true; 1087 } 1088 if (irq3 & GET_MASK(F_BST32SI)) { 1089 dev_err(dev, "Boost fault. 32 seconds timeout occurred.\n"); 1090 alert_userspace = true; 1091 } 1092 1093 if (alert_userspace) { 1094 dev_info(dev, "Boost fault occurred, therefore the charger goes into charge mode\n"); 1095 ret = rt9455_set_voreg_before_charge_mode(info); 1096 if (ret) { 1097 dev_err(dev, "Failed to set VOREG before entering charge mode\n"); 1098 return ret; 1099 } 1100 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE], 1101 RT9455_CHARGE_MODE); 1102 if (ret) { 1103 dev_err(dev, "Failed to set charger in charge mode\n"); 1104 return ret; 1105 } 1106 *_alert_userspace = alert_userspace; 1107 } 1108 1109 return 0; 1110 } 1111 1112 static irqreturn_t rt9455_irq_handler_thread(int irq, void *data) 1113 { 1114 struct rt9455_info *info = data; 1115 struct device *dev; 1116 bool alert_userspace = false; 1117 bool is_battery_absent = false; 1118 unsigned int status; 1119 int ret; 1120 1121 if (!info) 1122 return IRQ_NONE; 1123 1124 dev = &info->client->dev; 1125 1126 if (irq != info->client->irq) { 1127 dev_err(dev, "Interrupt is not for RT9455 charger\n"); 1128 return IRQ_NONE; 1129 } 1130 1131 ret = regmap_field_read(info->regmap_fields[F_STAT], &status); 1132 if (ret) { 1133 dev_err(dev, "Failed to read STAT bits\n"); 1134 return IRQ_HANDLED; 1135 } 1136 dev_dbg(dev, "Charger status is %d\n", status); 1137 1138 /* 1139 * Each function that processes an IRQ register receives as output 1140 * parameter alert_userspace pointer. alert_userspace is set to true 1141 * in such a function only if an interrupt has occurred in the 1142 * respective interrupt register. This way, it is avoided the following 1143 * case: interrupt occurs only in IRQ1 register, 1144 * rt9455_irq_handler_check_irq1_register() function sets to true 1145 * alert_userspace, but rt9455_irq_handler_check_irq2_register() 1146 * and rt9455_irq_handler_check_irq3_register() functions set to false 1147 * alert_userspace and power_supply_changed() is never called. 1148 */ 1149 ret = rt9455_irq_handler_check_irq1_register(info, &is_battery_absent, 1150 &alert_userspace); 1151 if (ret) { 1152 dev_err(dev, "Failed to handle IRQ1 register\n"); 1153 return IRQ_HANDLED; 1154 } 1155 1156 ret = rt9455_irq_handler_check_irq2_register(info, is_battery_absent, 1157 &alert_userspace); 1158 if (ret) { 1159 dev_err(dev, "Failed to handle IRQ2 register\n"); 1160 return IRQ_HANDLED; 1161 } 1162 1163 ret = rt9455_irq_handler_check_irq3_register(info, &alert_userspace); 1164 if (ret) { 1165 dev_err(dev, "Failed to handle IRQ3 register\n"); 1166 return IRQ_HANDLED; 1167 } 1168 1169 if (alert_userspace) { 1170 /* 1171 * Sometimes, an interrupt occurs while rt9455_probe() function 1172 * is executing and power_supply_register() is not yet called. 1173 * Do not call power_supply_changed() in this case. 1174 */ 1175 if (info->charger) 1176 power_supply_changed(info->charger); 1177 } 1178 1179 return IRQ_HANDLED; 1180 } 1181 1182 static int rt9455_discover_charger(struct rt9455_info *info, u32 *ichrg, 1183 u32 *ieoc_percentage, 1184 u32 *mivr, u32 *iaicr) 1185 { 1186 struct device *dev = &info->client->dev; 1187 int ret; 1188 1189 if (!dev->of_node && !ACPI_HANDLE(dev)) { 1190 dev_err(dev, "No support for either device tree or ACPI\n"); 1191 return -EINVAL; 1192 } 1193 /* 1194 * ICHRG, IEOC_PERCENTAGE, VOREG and boost output voltage are mandatory 1195 * parameters. 1196 */ 1197 ret = device_property_read_u32(dev, "richtek,output-charge-current", 1198 ichrg); 1199 if (ret) { 1200 dev_err(dev, "Error: missing \"output-charge-current\" property\n"); 1201 return ret; 1202 } 1203 1204 ret = device_property_read_u32(dev, "richtek,end-of-charge-percentage", 1205 ieoc_percentage); 1206 if (ret) { 1207 dev_err(dev, "Error: missing \"end-of-charge-percentage\" property\n"); 1208 return ret; 1209 } 1210 1211 ret = device_property_read_u32(dev, 1212 "richtek,battery-regulation-voltage", 1213 &info->voreg); 1214 if (ret) { 1215 dev_err(dev, "Error: missing \"battery-regulation-voltage\" property\n"); 1216 return ret; 1217 } 1218 1219 ret = device_property_read_u32(dev, "richtek,boost-output-voltage", 1220 &info->boost_voltage); 1221 if (ret) { 1222 dev_err(dev, "Error: missing \"boost-output-voltage\" property\n"); 1223 return ret; 1224 } 1225 1226 /* 1227 * MIVR and IAICR are optional parameters. Do not return error if one of 1228 * them is not present in ACPI table or device tree specification. 1229 */ 1230 device_property_read_u32(dev, "richtek,min-input-voltage-regulation", 1231 mivr); 1232 device_property_read_u32(dev, "richtek,avg-input-current-regulation", 1233 iaicr); 1234 1235 return 0; 1236 } 1237 1238 #if IS_ENABLED(CONFIG_USB_PHY) 1239 static int rt9455_usb_event_none(struct rt9455_info *info, 1240 u8 opa_mode, u8 iaicr) 1241 { 1242 struct device *dev = &info->client->dev; 1243 int ret; 1244 1245 if (opa_mode == RT9455_BOOST_MODE) { 1246 ret = rt9455_set_voreg_before_charge_mode(info); 1247 if (ret) { 1248 dev_err(dev, "Failed to set VOREG before entering charge mode\n"); 1249 return ret; 1250 } 1251 /* 1252 * If the charger is in boost mode, and it has received 1253 * USB_EVENT_NONE, this means the consumer device powered by the 1254 * charger is not connected anymore. 1255 * In this case, the charger goes into charge mode. 1256 */ 1257 dev_dbg(dev, "USB_EVENT_NONE received, therefore the charger goes into charge mode\n"); 1258 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE], 1259 RT9455_CHARGE_MODE); 1260 if (ret) { 1261 dev_err(dev, "Failed to set charger in charge mode\n"); 1262 return NOTIFY_DONE; 1263 } 1264 } 1265 1266 dev_dbg(dev, "USB_EVENT_NONE received, therefore IAICR is set to its minimum value\n"); 1267 if (iaicr != RT9455_IAICR_100MA) { 1268 ret = regmap_field_write(info->regmap_fields[F_IAICR], 1269 RT9455_IAICR_100MA); 1270 if (ret) { 1271 dev_err(dev, "Failed to set IAICR value\n"); 1272 return NOTIFY_DONE; 1273 } 1274 } 1275 1276 return NOTIFY_OK; 1277 } 1278 1279 static int rt9455_usb_event_vbus(struct rt9455_info *info, 1280 u8 opa_mode, u8 iaicr) 1281 { 1282 struct device *dev = &info->client->dev; 1283 int ret; 1284 1285 if (opa_mode == RT9455_BOOST_MODE) { 1286 ret = rt9455_set_voreg_before_charge_mode(info); 1287 if (ret) { 1288 dev_err(dev, "Failed to set VOREG before entering charge mode\n"); 1289 return ret; 1290 } 1291 /* 1292 * If the charger is in boost mode, and it has received 1293 * USB_EVENT_VBUS, this means the consumer device powered by the 1294 * charger is not connected anymore. 1295 * In this case, the charger goes into charge mode. 1296 */ 1297 dev_dbg(dev, "USB_EVENT_VBUS received, therefore the charger goes into charge mode\n"); 1298 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE], 1299 RT9455_CHARGE_MODE); 1300 if (ret) { 1301 dev_err(dev, "Failed to set charger in charge mode\n"); 1302 return NOTIFY_DONE; 1303 } 1304 } 1305 1306 dev_dbg(dev, "USB_EVENT_VBUS received, therefore IAICR is set to 500 mA\n"); 1307 if (iaicr != RT9455_IAICR_500MA) { 1308 ret = regmap_field_write(info->regmap_fields[F_IAICR], 1309 RT9455_IAICR_500MA); 1310 if (ret) { 1311 dev_err(dev, "Failed to set IAICR value\n"); 1312 return NOTIFY_DONE; 1313 } 1314 } 1315 1316 return NOTIFY_OK; 1317 } 1318 1319 static int rt9455_usb_event_id(struct rt9455_info *info, 1320 u8 opa_mode, u8 iaicr) 1321 { 1322 struct device *dev = &info->client->dev; 1323 int ret; 1324 1325 if (opa_mode == RT9455_CHARGE_MODE) { 1326 ret = rt9455_set_boost_voltage_before_boost_mode(info); 1327 if (ret) { 1328 dev_err(dev, "Failed to set boost output voltage before entering boost mode\n"); 1329 return ret; 1330 } 1331 /* 1332 * If the charger is in charge mode, and it has received 1333 * USB_EVENT_ID, this means a consumer device is connected and 1334 * it should be powered by the charger. 1335 * In this case, the charger goes into boost mode. 1336 */ 1337 dev_dbg(dev, "USB_EVENT_ID received, therefore the charger goes into boost mode\n"); 1338 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE], 1339 RT9455_BOOST_MODE); 1340 if (ret) { 1341 dev_err(dev, "Failed to set charger in boost mode\n"); 1342 return NOTIFY_DONE; 1343 } 1344 } 1345 1346 dev_dbg(dev, "USB_EVENT_ID received, therefore IAICR is set to its minimum value\n"); 1347 if (iaicr != RT9455_IAICR_100MA) { 1348 ret = regmap_field_write(info->regmap_fields[F_IAICR], 1349 RT9455_IAICR_100MA); 1350 if (ret) { 1351 dev_err(dev, "Failed to set IAICR value\n"); 1352 return NOTIFY_DONE; 1353 } 1354 } 1355 1356 return NOTIFY_OK; 1357 } 1358 1359 static int rt9455_usb_event_charger(struct rt9455_info *info, 1360 u8 opa_mode, u8 iaicr) 1361 { 1362 struct device *dev = &info->client->dev; 1363 int ret; 1364 1365 if (opa_mode == RT9455_BOOST_MODE) { 1366 ret = rt9455_set_voreg_before_charge_mode(info); 1367 if (ret) { 1368 dev_err(dev, "Failed to set VOREG before entering charge mode\n"); 1369 return ret; 1370 } 1371 /* 1372 * If the charger is in boost mode, and it has received 1373 * USB_EVENT_CHARGER, this means the consumer device powered by 1374 * the charger is not connected anymore. 1375 * In this case, the charger goes into charge mode. 1376 */ 1377 dev_dbg(dev, "USB_EVENT_CHARGER received, therefore the charger goes into charge mode\n"); 1378 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE], 1379 RT9455_CHARGE_MODE); 1380 if (ret) { 1381 dev_err(dev, "Failed to set charger in charge mode\n"); 1382 return NOTIFY_DONE; 1383 } 1384 } 1385 1386 dev_dbg(dev, "USB_EVENT_CHARGER received, therefore IAICR is set to no current limit\n"); 1387 if (iaicr != RT9455_IAICR_NO_LIMIT) { 1388 ret = regmap_field_write(info->regmap_fields[F_IAICR], 1389 RT9455_IAICR_NO_LIMIT); 1390 if (ret) { 1391 dev_err(dev, "Failed to set IAICR value\n"); 1392 return NOTIFY_DONE; 1393 } 1394 } 1395 1396 return NOTIFY_OK; 1397 } 1398 1399 static int rt9455_usb_event(struct notifier_block *nb, 1400 unsigned long event, void *power) 1401 { 1402 struct rt9455_info *info = container_of(nb, struct rt9455_info, nb); 1403 struct device *dev = &info->client->dev; 1404 unsigned int opa_mode, iaicr; 1405 int ret; 1406 1407 /* 1408 * Determine whether the charger is in charge mode 1409 * or in boost mode. 1410 */ 1411 ret = regmap_field_read(info->regmap_fields[F_OPA_MODE], 1412 &opa_mode); 1413 if (ret) { 1414 dev_err(dev, "Failed to read OPA_MODE value\n"); 1415 return NOTIFY_DONE; 1416 } 1417 1418 ret = regmap_field_read(info->regmap_fields[F_IAICR], 1419 &iaicr); 1420 if (ret) { 1421 dev_err(dev, "Failed to read IAICR value\n"); 1422 return NOTIFY_DONE; 1423 } 1424 1425 dev_dbg(dev, "Received USB event %lu\n", event); 1426 switch (event) { 1427 case USB_EVENT_NONE: 1428 return rt9455_usb_event_none(info, opa_mode, iaicr); 1429 case USB_EVENT_VBUS: 1430 return rt9455_usb_event_vbus(info, opa_mode, iaicr); 1431 case USB_EVENT_ID: 1432 return rt9455_usb_event_id(info, opa_mode, iaicr); 1433 case USB_EVENT_CHARGER: 1434 return rt9455_usb_event_charger(info, opa_mode, iaicr); 1435 default: 1436 dev_err(dev, "Unknown USB event\n"); 1437 } 1438 return NOTIFY_DONE; 1439 } 1440 #endif 1441 1442 static void rt9455_pwr_rdy_work_callback(struct work_struct *work) 1443 { 1444 struct rt9455_info *info = container_of(work, struct rt9455_info, 1445 pwr_rdy_work.work); 1446 struct device *dev = &info->client->dev; 1447 unsigned int pwr_rdy; 1448 int ret; 1449 1450 ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &pwr_rdy); 1451 if (ret) { 1452 dev_err(dev, "Failed to read PWR_RDY bit\n"); 1453 return; 1454 } 1455 switch (pwr_rdy) { 1456 case RT9455_PWR_FAULT: 1457 dev_dbg(dev, "Charger disconnected from power source\n"); 1458 cancel_delayed_work_sync(&info->max_charging_time_work); 1459 break; 1460 case RT9455_PWR_GOOD: 1461 dev_dbg(dev, "Charger connected to power source\n"); 1462 ret = regmap_field_write(info->regmap_fields[F_CHG_EN], 1463 RT9455_CHARGE_ENABLE); 1464 if (ret) { 1465 dev_err(dev, "Failed to enable charging\n"); 1466 return; 1467 } 1468 queue_delayed_work(system_power_efficient_wq, 1469 &info->max_charging_time_work, 1470 RT9455_MAX_CHARGING_TIME * HZ); 1471 break; 1472 } 1473 /* 1474 * Notify userspace that the charger has been either connected to or 1475 * disconnected from the power source. 1476 */ 1477 power_supply_changed(info->charger); 1478 } 1479 1480 static void rt9455_max_charging_time_work_callback(struct work_struct *work) 1481 { 1482 struct rt9455_info *info = container_of(work, struct rt9455_info, 1483 max_charging_time_work.work); 1484 struct device *dev = &info->client->dev; 1485 int ret; 1486 1487 dev_err(dev, "Battery has been charging for at least 6 hours and is not yet fully charged. Battery is dead, therefore charging is disabled.\n"); 1488 ret = regmap_field_write(info->regmap_fields[F_CHG_EN], 1489 RT9455_CHARGE_DISABLE); 1490 if (ret) 1491 dev_err(dev, "Failed to disable charging\n"); 1492 } 1493 1494 static void rt9455_batt_presence_work_callback(struct work_struct *work) 1495 { 1496 struct rt9455_info *info = container_of(work, struct rt9455_info, 1497 batt_presence_work.work); 1498 struct device *dev = &info->client->dev; 1499 unsigned int irq1, mask1; 1500 int ret; 1501 1502 ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1); 1503 if (ret) { 1504 dev_err(dev, "Failed to read IRQ1 register\n"); 1505 return; 1506 } 1507 1508 /* 1509 * If the battery is still absent, batt_presence_work is rescheduled. 1510 * Otherwise, max_charging_time is scheduled. 1511 */ 1512 if (irq1 & GET_MASK(F_BATAB)) { 1513 queue_delayed_work(system_power_efficient_wq, 1514 &info->batt_presence_work, 1515 RT9455_BATT_PRESENCE_DELAY * HZ); 1516 } else { 1517 queue_delayed_work(system_power_efficient_wq, 1518 &info->max_charging_time_work, 1519 RT9455_MAX_CHARGING_TIME * HZ); 1520 1521 ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1); 1522 if (ret) { 1523 dev_err(dev, "Failed to read MASK1 register\n"); 1524 return; 1525 } 1526 1527 if (mask1 & GET_MASK(F_BATABM)) { 1528 ret = regmap_field_write(info->regmap_fields[F_BATABM], 1529 0x00); 1530 if (ret) 1531 dev_err(dev, "Failed to unmask BATAB interrupt\n"); 1532 } 1533 /* 1534 * Notify userspace that the battery is now connected to the 1535 * charger. 1536 */ 1537 power_supply_changed(info->charger); 1538 } 1539 } 1540 1541 static const struct power_supply_desc rt9455_charger_desc = { 1542 .name = RT9455_DRIVER_NAME, 1543 .type = POWER_SUPPLY_TYPE_USB, 1544 .properties = rt9455_charger_properties, 1545 .num_properties = ARRAY_SIZE(rt9455_charger_properties), 1546 .get_property = rt9455_charger_get_property, 1547 }; 1548 1549 static bool rt9455_is_writeable_reg(struct device *dev, unsigned int reg) 1550 { 1551 switch (reg) { 1552 case RT9455_REG_DEV_ID: 1553 case RT9455_REG_IRQ1: 1554 case RT9455_REG_IRQ2: 1555 case RT9455_REG_IRQ3: 1556 return false; 1557 default: 1558 return true; 1559 } 1560 } 1561 1562 static bool rt9455_is_volatile_reg(struct device *dev, unsigned int reg) 1563 { 1564 switch (reg) { 1565 case RT9455_REG_DEV_ID: 1566 case RT9455_REG_CTRL5: 1567 case RT9455_REG_CTRL6: 1568 return false; 1569 default: 1570 return true; 1571 } 1572 } 1573 1574 static const struct regmap_config rt9455_regmap_config = { 1575 .reg_bits = 8, 1576 .val_bits = 8, 1577 .writeable_reg = rt9455_is_writeable_reg, 1578 .volatile_reg = rt9455_is_volatile_reg, 1579 .max_register = RT9455_REG_MASK3, 1580 .cache_type = REGCACHE_RBTREE, 1581 }; 1582 1583 static int rt9455_probe(struct i2c_client *client) 1584 { 1585 struct i2c_adapter *adapter = client->adapter; 1586 struct device *dev = &client->dev; 1587 struct rt9455_info *info; 1588 struct power_supply_config rt9455_charger_config = {}; 1589 /* 1590 * Mandatory device-specific data values. Also, VOREG and boost output 1591 * voltage are mandatory values, but they are stored in rt9455_info 1592 * structure. 1593 */ 1594 u32 ichrg, ieoc_percentage; 1595 /* Optional device-specific data values. */ 1596 u32 mivr = -1, iaicr = -1; 1597 int i, ret; 1598 1599 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { 1600 dev_err(dev, "No support for SMBUS_BYTE_DATA\n"); 1601 return -ENODEV; 1602 } 1603 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); 1604 if (!info) 1605 return -ENOMEM; 1606 1607 info->client = client; 1608 i2c_set_clientdata(client, info); 1609 1610 info->regmap = devm_regmap_init_i2c(client, 1611 &rt9455_regmap_config); 1612 if (IS_ERR(info->regmap)) { 1613 dev_err(dev, "Failed to initialize register map\n"); 1614 return -EINVAL; 1615 } 1616 1617 for (i = 0; i < F_MAX_FIELDS; i++) { 1618 info->regmap_fields[i] = 1619 devm_regmap_field_alloc(dev, info->regmap, 1620 rt9455_reg_fields[i]); 1621 if (IS_ERR(info->regmap_fields[i])) { 1622 dev_err(dev, 1623 "Failed to allocate regmap field = %d\n", i); 1624 return PTR_ERR(info->regmap_fields[i]); 1625 } 1626 } 1627 1628 ret = rt9455_discover_charger(info, &ichrg, &ieoc_percentage, 1629 &mivr, &iaicr); 1630 if (ret) { 1631 dev_err(dev, "Failed to discover charger\n"); 1632 return ret; 1633 } 1634 1635 #if IS_ENABLED(CONFIG_USB_PHY) 1636 info->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); 1637 if (IS_ERR(info->usb_phy)) { 1638 dev_err(dev, "Failed to get USB transceiver\n"); 1639 } else { 1640 info->nb.notifier_call = rt9455_usb_event; 1641 ret = usb_register_notifier(info->usb_phy, &info->nb); 1642 if (ret) { 1643 dev_err(dev, "Failed to register USB notifier\n"); 1644 /* 1645 * If usb_register_notifier() fails, set notifier_call 1646 * to NULL, to avoid calling usb_unregister_notifier(). 1647 */ 1648 info->nb.notifier_call = NULL; 1649 } 1650 } 1651 #endif 1652 1653 INIT_DEFERRABLE_WORK(&info->pwr_rdy_work, rt9455_pwr_rdy_work_callback); 1654 INIT_DEFERRABLE_WORK(&info->max_charging_time_work, 1655 rt9455_max_charging_time_work_callback); 1656 INIT_DEFERRABLE_WORK(&info->batt_presence_work, 1657 rt9455_batt_presence_work_callback); 1658 1659 rt9455_charger_config.of_node = dev->of_node; 1660 rt9455_charger_config.drv_data = info; 1661 rt9455_charger_config.supplied_to = rt9455_charger_supplied_to; 1662 rt9455_charger_config.num_supplicants = 1663 ARRAY_SIZE(rt9455_charger_supplied_to); 1664 ret = devm_request_threaded_irq(dev, client->irq, NULL, 1665 rt9455_irq_handler_thread, 1666 IRQF_TRIGGER_LOW | IRQF_ONESHOT, 1667 RT9455_DRIVER_NAME, info); 1668 if (ret) { 1669 dev_err(dev, "Failed to register IRQ handler\n"); 1670 goto put_usb_notifier; 1671 } 1672 1673 ret = rt9455_hw_init(info, ichrg, ieoc_percentage, mivr, iaicr); 1674 if (ret) { 1675 dev_err(dev, "Failed to set charger to its default values\n"); 1676 goto put_usb_notifier; 1677 } 1678 1679 info->charger = devm_power_supply_register(dev, &rt9455_charger_desc, 1680 &rt9455_charger_config); 1681 if (IS_ERR(info->charger)) { 1682 dev_err(dev, "Failed to register charger\n"); 1683 ret = PTR_ERR(info->charger); 1684 goto put_usb_notifier; 1685 } 1686 1687 return 0; 1688 1689 put_usb_notifier: 1690 #if IS_ENABLED(CONFIG_USB_PHY) 1691 if (info->nb.notifier_call) { 1692 usb_unregister_notifier(info->usb_phy, &info->nb); 1693 info->nb.notifier_call = NULL; 1694 } 1695 #endif 1696 return ret; 1697 } 1698 1699 static void rt9455_remove(struct i2c_client *client) 1700 { 1701 int ret; 1702 struct rt9455_info *info = i2c_get_clientdata(client); 1703 1704 ret = rt9455_register_reset(info); 1705 if (ret) 1706 dev_err(&info->client->dev, "Failed to set charger to its default values\n"); 1707 1708 #if IS_ENABLED(CONFIG_USB_PHY) 1709 if (info->nb.notifier_call) 1710 usb_unregister_notifier(info->usb_phy, &info->nb); 1711 #endif 1712 1713 cancel_delayed_work_sync(&info->pwr_rdy_work); 1714 cancel_delayed_work_sync(&info->max_charging_time_work); 1715 cancel_delayed_work_sync(&info->batt_presence_work); 1716 } 1717 1718 static const struct i2c_device_id rt9455_i2c_id_table[] = { 1719 { RT9455_DRIVER_NAME, 0 }, 1720 { }, 1721 }; 1722 MODULE_DEVICE_TABLE(i2c, rt9455_i2c_id_table); 1723 1724 static const struct of_device_id rt9455_of_match[] __maybe_unused = { 1725 { .compatible = "richtek,rt9455", }, 1726 { }, 1727 }; 1728 MODULE_DEVICE_TABLE(of, rt9455_of_match); 1729 1730 #ifdef CONFIG_ACPI 1731 static const struct acpi_device_id rt9455_i2c_acpi_match[] = { 1732 { "RT945500", 0 }, 1733 { } 1734 }; 1735 MODULE_DEVICE_TABLE(acpi, rt9455_i2c_acpi_match); 1736 #endif 1737 1738 static struct i2c_driver rt9455_driver = { 1739 .probe = rt9455_probe, 1740 .remove = rt9455_remove, 1741 .id_table = rt9455_i2c_id_table, 1742 .driver = { 1743 .name = RT9455_DRIVER_NAME, 1744 .of_match_table = of_match_ptr(rt9455_of_match), 1745 .acpi_match_table = ACPI_PTR(rt9455_i2c_acpi_match), 1746 }, 1747 }; 1748 module_i2c_driver(rt9455_driver); 1749 1750 MODULE_LICENSE("GPL"); 1751 MODULE_AUTHOR("Anda-Maria Nicolae <anda-maria.nicolae@intel.com>"); 1752 MODULE_DESCRIPTION("Richtek RT9455 Charger Driver"); 1753