1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Driver for Analog Devices (Linear Technology) 4 * LTC4162-L 35V/3.2A Multi-Cell Lithium-Ion Step-Down Battery Charger 5 * LTC4162-F 35V/3.2A Multi-Cell LiFePO4 Step-Down Battery Charger 6 * LTC4162-S 35V/3.2A Lead-Acid Step-Down Battery Charger 7 * LTC4015 35V/3.2A Multichemistry Buck Battery Charger Controller 8 * Copyright (C) 2020, Topic Embedded Products 9 */ 10 11 #include <linux/bitfield.h> 12 #include <linux/module.h> 13 #include <linux/delay.h> 14 #include <linux/of.h> 15 #include <linux/pm_runtime.h> 16 #include <linux/power_supply.h> 17 #include <linux/i2c.h> 18 #include <linux/regmap.h> 19 20 /* Registers (names based on what datasheet uses) */ 21 #define LTC4162L_EN_LIMIT_ALERTS_REG 0x0D 22 #define LTC4162L_EN_CHARGER_STATE_ALERTS_REG 0x0E 23 #define LTC4162L_EN_CHARGE_STATUS_ALERTS_REG 0x0F 24 #define LTC4162L_CONFIG_BITS_REG 0x14 25 #define LTC4162L_IIN_LIMIT_TARGET 0x15 26 #define LTC4162L_ARM_SHIP_MODE 0x19 27 #define LTC4162L_CHARGE_CURRENT_SETTING 0X1A 28 #define LTC4162L_VCHARGE_SETTING 0X1B 29 #define LTC4162L_C_OVER_X_THRESHOLD 0x1C 30 #define LTC4162L_MAX_CV_TIME 0X1D 31 #define LTC4162L_MAX_CHARGE_TIME 0X1E 32 #define LTC4162L_CHARGER_CONFIG_BITS 0x29 33 #define LTC4162L_CHARGER_STATE 0x34 34 #define LTC4162L_CHARGE_STATUS 0x35 35 #define LTC4162L_LIMIT_ALERTS_REG 0x36 36 #define LTC4162L_CHARGER_STATE_ALERTS_REG 0x37 37 #define LTC4162L_CHARGE_STATUS_ALERTS_REG 0x38 38 #define LTC4162L_SYSTEM_STATUS_REG 0x39 39 #define LTC4162L_VBAT 0x3A 40 #define LTC4162L_VIN 0x3B 41 #define LTC4162L_VOUT 0x3C 42 #define LTC4162L_IBAT 0x3D 43 #define LTC4162L_IIN 0x3E 44 #define LTC4162L_DIE_TEMPERATURE 0x3F 45 #define LTC4162L_THERMISTOR_VOLTAGE 0x40 46 #define LTC4162L_BSR 0x41 47 #define LTC4162L_JEITA_REGION 0x42 48 #define LTC4162L_CHEM_CELLS_REG 0x43 49 #define LTC4162L_ICHARGE_DAC 0x44 50 #define LTC4162L_VCHARGE_DAC 0x45 51 #define LTC4162L_IIN_LIMIT_DAC 0x46 52 #define LTC4162L_VBAT_FILT 0x47 53 #define LTC4162L_INPUT_UNDERVOLTAGE_DAC 0x4B 54 55 #define LTC4162L_CHEM_MASK GENMASK(11, 8) 56 57 enum ltc4162_chem { 58 ltc4162_lad, 59 ltc4162_l42, 60 ltc4162_l41, 61 ltc4162_l40, 62 ltc4162_fad, 63 ltc4162_ffs, 64 ltc4162_fst, 65 ltc4162_sst = 8, 66 ltc4162_sad, 67 }; 68 69 /* Enumeration as in datasheet. Individual bits are mutually exclusive. */ 70 enum ltc4162l_state { 71 battery_detection = 2048, 72 charger_suspended = 256, 73 precharge = 128, /* trickle on low bat voltage */ 74 cc_cv_charge = 64, /* normal charge */ 75 ntc_pause = 32, 76 timer_term = 16, 77 c_over_x_term = 8, /* battery is full */ 78 max_charge_time_fault = 4, 79 bat_missing_fault = 2, 80 bat_short_fault = 1 81 }; 82 83 /* Individual bits are mutually exclusive. Only active in charging states.*/ 84 enum ltc4162l_charge_status { 85 ilim_reg_active = 32, 86 thermal_reg_active = 16, 87 vin_uvcl_active = 8, 88 iin_limit_active = 4, 89 constant_current = 2, 90 constant_voltage = 1, 91 charger_off = 0 92 }; 93 94 /* Magic number to write to ARM_SHIP_MODE register */ 95 #define LTC4162L_ARM_SHIP_MODE_MAGIC 21325 96 97 struct ltc4162l_info; 98 99 struct ltc4162l_chip_info { 100 const char *name; 101 int (*get_vbat)(struct ltc4162l_info *info, unsigned int reg, 102 union power_supply_propval *val); 103 int (*get_vcharge)(struct ltc4162l_info *info, unsigned int reg, 104 union power_supply_propval *val); 105 int (*set_vcharge)(struct ltc4162l_info *info, unsigned int reg, 106 unsigned int value); 107 int (*get_die_temp)(struct ltc4162l_info *info, 108 union power_supply_propval *val); 109 unsigned int ibat_resolution_pv; 110 unsigned int vin_resolution_uv; 111 u8 telemetry_mask; 112 }; 113 114 struct ltc4162l_info { 115 struct i2c_client *client; 116 struct regmap *regmap; 117 struct power_supply *charger; 118 const struct ltc4162l_chip_info *chip_info; 119 u32 rsnsb; /* Series resistor that sets charge current, microOhm */ 120 u32 rsnsi; /* Series resistor to measure input current, microOhm */ 121 u8 cell_count; /* Number of connected cells, 0 while unknown */ 122 }; 123 124 static u8 ltc4162l_get_cell_count(struct ltc4162l_info *info) 125 { 126 int ret; 127 unsigned int val; 128 129 /* Once read successfully */ 130 if (info->cell_count) 131 return info->cell_count; 132 133 ret = regmap_read(info->regmap, LTC4162L_CHEM_CELLS_REG, &val); 134 if (ret) 135 return 0; 136 137 /* Lower 4 bits is the cell count, or 0 if the chip doesn't know yet */ 138 val &= 0x0f; 139 if (!val) 140 return 0; 141 142 /* Once determined, keep the value */ 143 info->cell_count = val; 144 145 return val; 146 }; 147 148 static u8 ltc4162l_get_chem_type(struct ltc4162l_info *info) 149 { 150 int ret; 151 unsigned int val; 152 153 ret = regmap_read(info->regmap, LTC4162L_CHEM_CELLS_REG, &val); 154 if (ret) 155 return ret; 156 157 return FIELD_GET(LTC4162L_CHEM_MASK, val); 158 }; 159 160 /* Convert enum value to POWER_SUPPLY_STATUS value */ 161 static int ltc4162l_state_decode(enum ltc4162l_state value) 162 { 163 switch (value) { 164 case precharge: 165 case cc_cv_charge: 166 return POWER_SUPPLY_STATUS_CHARGING; 167 case c_over_x_term: 168 return POWER_SUPPLY_STATUS_FULL; 169 case bat_missing_fault: 170 case bat_short_fault: 171 return POWER_SUPPLY_STATUS_UNKNOWN; 172 default: 173 return POWER_SUPPLY_STATUS_NOT_CHARGING; 174 } 175 }; 176 177 static int ltc4162l_get_status(struct ltc4162l_info *info, 178 union power_supply_propval *val) 179 { 180 unsigned int regval; 181 int ret; 182 183 ret = regmap_read(info->regmap, LTC4162L_CHARGER_STATE, ®val); 184 if (ret) { 185 dev_err(&info->client->dev, "Failed to read CHARGER_STATE\n"); 186 return ret; 187 } 188 189 val->intval = ltc4162l_state_decode(regval); 190 191 return 0; 192 } 193 194 static int ltc4162l_charge_status_decode(enum ltc4162l_charge_status value) 195 { 196 if (!value) 197 return POWER_SUPPLY_CHARGE_TYPE_NONE; 198 199 /* constant voltage/current and input_current limit are "fast" modes */ 200 if (value <= iin_limit_active) 201 return POWER_SUPPLY_CHARGE_TYPE_FAST; 202 203 /* Anything that's not fast we'll return as trickle */ 204 return POWER_SUPPLY_CHARGE_TYPE_TRICKLE; 205 } 206 207 static int ltc4162l_get_charge_type(struct ltc4162l_info *info, 208 union power_supply_propval *val) 209 { 210 unsigned int regval; 211 int ret; 212 213 ret = regmap_read(info->regmap, LTC4162L_CHARGE_STATUS, ®val); 214 if (ret) 215 return ret; 216 217 val->intval = ltc4162l_charge_status_decode(regval); 218 219 return 0; 220 } 221 222 static int ltc4162l_state_to_health(enum ltc4162l_state value) 223 { 224 switch (value) { 225 case ntc_pause: 226 return POWER_SUPPLY_HEALTH_OVERHEAT; 227 case timer_term: 228 return POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE; 229 case max_charge_time_fault: 230 return POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE; 231 case bat_missing_fault: 232 return POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 233 case bat_short_fault: 234 return POWER_SUPPLY_HEALTH_DEAD; 235 default: 236 return POWER_SUPPLY_HEALTH_GOOD; 237 } 238 } 239 240 static int ltc4162l_get_health(struct ltc4162l_info *info, 241 union power_supply_propval *val) 242 { 243 unsigned int regval; 244 int ret; 245 246 ret = regmap_read(info->regmap, LTC4162L_CHARGER_STATE, ®val); 247 if (ret) 248 return ret; 249 250 val->intval = ltc4162l_state_to_health(regval); 251 252 return 0; 253 } 254 255 static int ltc4162l_get_online(struct ltc4162l_info *info, 256 union power_supply_propval *val) 257 { 258 unsigned int regval; 259 int ret; 260 261 ret = regmap_read(info->regmap, LTC4162L_SYSTEM_STATUS_REG, ®val); 262 if (ret) 263 return ret; 264 265 /* BIT(2) indicates if input voltage is sufficient to charge */ 266 val->intval = !!(regval & BIT(2)); 267 268 return 0; 269 } 270 271 static int ltc4162l_get_vbat(struct ltc4162l_info *info, 272 unsigned int reg, 273 union power_supply_propval *val) 274 { 275 unsigned int regval, chem_type; 276 int ret; 277 278 ret = regmap_read(info->regmap, reg, ®val); 279 if (ret) 280 return ret; 281 282 /* 283 * cell_count × scaling factor 284 * For ltc4162-s, it uses a cell_count value of 2 for each group of 3 285 * physical (2V) cells, thus will return 2, 4, 6, 8 for 6V, 12V, 18V, 286 * and 24V respectively, and has to divide by 2 to multiply the scale 287 * factor by 1, 2, 3, or 4 to represent a 6V, 12V, 18V, or 24V battery 288 * respectively. 289 */ 290 chem_type = ltc4162l_get_chem_type(info); 291 switch (chem_type) { 292 case ltc4162_lad ... ltc4162_fst: 293 regval *= 1924; 294 regval *= ltc4162l_get_cell_count(info); 295 regval /= 10; 296 val->intval = regval; 297 298 return 0; 299 case ltc4162_sst ... ltc4162_sad: 300 regval *= 3848; 301 regval *= ltc4162l_get_cell_count(info) / 2; 302 regval /= 10; 303 val->intval = regval; 304 305 return 0; 306 default: 307 return -EINVAL; 308 } 309 } 310 311 static int ltc4015_get_vbat(struct ltc4162l_info *info, 312 unsigned int reg, 313 union power_supply_propval *val) 314 { 315 unsigned int regval, chem_type; 316 int ret; 317 318 ret = regmap_read(info->regmap, reg, ®val); 319 if (ret) 320 return ret; 321 322 /* 323 * cell count x scaling factor 324 * ltc4015 lead-acid fixed and lead-acid programmable corresponds to 325 * 0x7 and 0x8 chem respectively 326 */ 327 chem_type = ltc4162l_get_chem_type(info); 328 switch (chem_type) { 329 case ltc4162_lad ... ltc4162_fst: 330 regval *= 192264; 331 regval *= ltc4162l_get_cell_count(info); 332 regval /= 1000; 333 val->intval = regval; 334 335 return 0; 336 case ltc4162_sst - 1 ... ltc4162_sad - 1: 337 regval *= 128176; 338 regval *= ltc4162l_get_cell_count(info); 339 regval /= 1000; 340 val->intval = regval; 341 342 return 0; 343 default: 344 return -EINVAL; 345 } 346 } 347 348 static int ltc4162l_get_ibat(struct ltc4162l_info *info, 349 union power_supply_propval *val) 350 { 351 const struct ltc4162l_chip_info *chip_info = info->chip_info; 352 unsigned int regval; 353 int ret; 354 355 ret = regmap_read(info->regmap, LTC4162L_IBAT, ®val); 356 if (ret) 357 return ret; 358 359 ret = (s16)(regval & 0xFFFF); 360 val->intval = mult_frac(ret, chip_info->ibat_resolution_pv, info->rsnsb); 361 362 return 0; 363 } 364 365 366 static int ltc4162l_get_input_voltage(struct ltc4162l_info *info, 367 union power_supply_propval *val) 368 { 369 const struct ltc4162l_chip_info *chip_info = info->chip_info; 370 unsigned int regval; 371 int ret; 372 373 ret = regmap_read(info->regmap, LTC4162L_VIN, ®val); 374 if (ret) 375 return ret; 376 377 val->intval = regval * chip_info->vin_resolution_uv; 378 379 return 0; 380 } 381 382 static int ltc4162l_get_input_current(struct ltc4162l_info *info, 383 union power_supply_propval *val) 384 { 385 const struct ltc4162l_chip_info *chip_info = info->chip_info; 386 unsigned int regval; 387 int ret; 388 389 ret = regmap_read(info->regmap, LTC4162L_IIN, ®val); 390 if (ret) 391 return ret; 392 393 ret = (s16)(regval & 0xFFFF); 394 ret *= chip_info->ibat_resolution_pv; 395 ret /= info->rsnsi; 396 397 val->intval = ret; 398 399 return 0; 400 } 401 402 static int ltc4162l_get_icharge(struct ltc4162l_info *info, 403 unsigned int reg, 404 union power_supply_propval *val) 405 { 406 unsigned int regval; 407 int ret; 408 409 ret = regmap_read(info->regmap, reg, ®val); 410 if (ret) 411 return ret; 412 413 regval &= GENMASK(5, 0); 414 415 /* The charge current servo level: (icharge_dac + 1) × 1mV/RSNSB */ 416 ++regval; 417 val->intval = 10000u * mult_frac(regval, 100000u, info->rsnsb); 418 419 return 0; 420 } 421 422 static int ltc4162l_set_icharge(struct ltc4162l_info *info, 423 unsigned int reg, 424 unsigned int value) 425 { 426 value = mult_frac(value, info->rsnsb, 100000u); 427 value /= 10000u; 428 429 /* Round to lowest possible */ 430 if (value) 431 --value; 432 433 if (value > 31) 434 return -EINVAL; 435 436 return regmap_write(info->regmap, reg, value); 437 } 438 439 440 static int ltc4162l_get_vcharge(struct ltc4162l_info *info, 441 unsigned int reg, 442 union power_supply_propval *val) 443 { 444 unsigned int regval, chem_type; 445 int ret; 446 u32 voltage; 447 448 ret = regmap_read(info->regmap, reg, ®val); 449 if (ret) 450 return ret; 451 452 regval &= GENMASK(5, 0); 453 454 /* 455 * charge voltage setting can be computed from 456 * cell_count × (vcharge_setting × a + b) 457 * where vcharge_setting ranges from 0 to c (d). 458 * for ltc4162l: a = 12.5mV , b = 3.8125V, c = 31, d = 4.2Vmax 459 * for ltc4162f: a = 12.5mV , b = 3.4125V, c = 31, d = 3.8Vmax 460 * 461 * for ltc4162s, the charge voltage setting can be computed from 462 * N x (vcharge_setting x 28.571mV + 6.0V) 463 * where N is 1, 2, 3, or 4 for 6V, 12V, 18V, or 24V battery respectively, 464 * and vcharge_setting ranges from 0 to 31 465 */ 466 chem_type = ltc4162l_get_chem_type(info); 467 switch (chem_type) { 468 case ltc4162_lad ... ltc4162_l40: 469 voltage = 3812500 + (regval * 12500); 470 voltage *= ltc4162l_get_cell_count(info); 471 val->intval = voltage; 472 473 return 0; 474 case ltc4162_fad ... ltc4162_fst: 475 voltage = 3412500 + (regval * 12500); 476 voltage *= ltc4162l_get_cell_count(info); 477 val->intval = voltage; 478 479 return 0; 480 case ltc4162_sst ... ltc4162_sad: 481 voltage = 6000000 + (regval * 28571); 482 voltage *= ltc4162l_get_cell_count(info) / 2; 483 val->intval = voltage; 484 485 return 0; 486 default: 487 return -EINVAL; 488 } 489 } 490 491 static int ltc4015_get_vcharge(struct ltc4162l_info *info, 492 unsigned int reg, 493 union power_supply_propval *val) 494 { 495 unsigned int regval, chem_type; 496 int ret; 497 u32 voltage; 498 499 ret = regmap_read(info->regmap, reg, ®val); 500 if (ret) 501 return ret; 502 503 regval &= GENMASK(5, 0); 504 505 /* 506 * charge voltage setting can be computed from: 507 * cell_count × (vcharge_setting × a + b) 508 * where vcharge_setting ranges from 0 to c (d). 509 * Li-Ion: a = 1/80V, b = 3.8125V, c = 31, d = 4.2Vmax 510 * LiFePO4: a = 1/80V, b = 3.4125V, c = 31, d = 3.8Vmax 511 * Lead Acid: a = 1/105V, b = 2V, c = 35, d = 2.6Vmax 512 */ 513 chem_type = ltc4162l_get_chem_type(info); 514 switch (chem_type) { 515 case ltc4162_lad ... ltc4162_l40: 516 voltage = 3812500 + (regval * 12500); 517 voltage *= ltc4162l_get_cell_count(info); 518 val->intval = voltage; 519 520 return 0; 521 case ltc4162_fad ... ltc4162_fst: 522 voltage = 3412500 + (regval * 12500); 523 voltage *= ltc4162l_get_cell_count(info); 524 val->intval = voltage; 525 526 return 0; 527 case ltc4162_sst - 1 ... ltc4162_sad - 1: 528 voltage = 2000000 + mult_frac(regval, 1000000, 105); 529 voltage *= ltc4162l_get_cell_count(info); 530 val->intval = voltage; 531 532 return 0; 533 default: 534 return -EINVAL; 535 } 536 } 537 538 static int ltc4162l_vcharge(unsigned int base_voltage, 539 unsigned int scale_factor, 540 unsigned int range, 541 unsigned int value, 542 u8 cell_count) 543 { 544 value /= cell_count; 545 546 if (value < base_voltage) 547 return -EINVAL; 548 549 value -= base_voltage; 550 value /= scale_factor; 551 552 if (value > range) 553 return -EINVAL; 554 555 return value; 556 } 557 558 static int ltc4162l_set_vcharge(struct ltc4162l_info *info, 559 unsigned int reg, 560 unsigned int value) 561 { 562 unsigned int chem_type; 563 u8 cell_count; 564 565 chem_type = ltc4162l_get_chem_type(info); 566 switch (chem_type) { 567 case ltc4162_lad ... ltc4162_l40: 568 cell_count = ltc4162l_get_cell_count(info); 569 if (!cell_count) 570 return -EBUSY; 571 572 value = ltc4162l_vcharge(3812500, 12500, 31, value, cell_count); 573 return regmap_write(info->regmap, reg, value); 574 case ltc4162_fad ... ltc4162_fst: 575 cell_count = ltc4162l_get_cell_count(info); 576 if (!cell_count) 577 return -EBUSY; 578 579 value = ltc4162l_vcharge(3412500, 12500, 31, value, cell_count); 580 return regmap_write(info->regmap, reg, value); 581 case ltc4162_sst ... ltc4162_sad: 582 cell_count = ltc4162l_get_cell_count(info) / 2; 583 if (!cell_count) 584 return -EBUSY; 585 586 value = ltc4162l_vcharge(6000000, 28571, 31, value, cell_count); 587 return regmap_write(info->regmap, reg, value); 588 default: 589 return -EINVAL; 590 } 591 } 592 593 static int ltc4015_set_vcharge(struct ltc4162l_info *info, 594 unsigned int reg, 595 unsigned int value) 596 { 597 unsigned int chem_type; 598 u8 cell_count; 599 600 chem_type = ltc4162l_get_chem_type(info); 601 switch (chem_type) { 602 case ltc4162_lad ... ltc4162_l40: 603 cell_count = ltc4162l_get_cell_count(info); 604 if (!cell_count) 605 return -EBUSY; 606 607 value = ltc4162l_vcharge(3812500, 12500, 31, value, cell_count); 608 return regmap_write(info->regmap, reg, value); 609 case ltc4162_fad ... ltc4162_fst: 610 cell_count = ltc4162l_get_cell_count(info); 611 if (!cell_count) 612 return -EBUSY; 613 614 value = ltc4162l_vcharge(3412500, 12500, 31, value, cell_count); 615 return regmap_write(info->regmap, reg, value); 616 case ltc4162_sst - 1 ... ltc4162_sad - 1: 617 cell_count = ltc4162l_get_cell_count(info); 618 if (!cell_count) 619 return -EBUSY; 620 621 value = ltc4162l_vcharge(2000000, 1000000 / 105, 35, 622 value, cell_count); 623 return regmap_write(info->regmap, reg, value); 624 default: 625 return -EINVAL; 626 } 627 } 628 629 static int ltc4162l_get_iin_limit_dac(struct ltc4162l_info *info, 630 union power_supply_propval *val) 631 { 632 unsigned int regval; 633 int ret; 634 635 ret = regmap_read(info->regmap, LTC4162L_IIN_LIMIT_DAC, ®val); 636 if (ret) 637 return ret; 638 639 regval &= GENMASK(5, 0); 640 641 /* (iin_limit_dac + 1) × 500μV / RSNSI */ 642 ++regval; 643 regval *= 5000000u; 644 regval /= info->rsnsi; 645 val->intval = 100u * regval; 646 647 return 0; 648 } 649 650 static int ltc4162l_set_iin_limit(struct ltc4162l_info *info, 651 unsigned int value) 652 { 653 unsigned int regval; 654 655 regval = mult_frac(value, info->rsnsi, 50000u); 656 regval /= 10000u; 657 if (regval) 658 --regval; 659 if (regval > 63) 660 regval = 63; 661 662 return regmap_write(info->regmap, LTC4162L_IIN_LIMIT_TARGET, regval); 663 } 664 665 static int ltc4162l_get_die_temp(struct ltc4162l_info *info, 666 union power_supply_propval *val) 667 { 668 unsigned int regval; 669 int ret; 670 671 ret = regmap_read(info->regmap, LTC4162L_DIE_TEMPERATURE, ®val); 672 if (ret) 673 return ret; 674 675 /* die_temp × 0.0215°C/LSB - 264.4°C */ 676 ret = (s16)(regval & 0xFFFF); 677 ret *= 215; 678 ret /= 100; /* Centidegrees scale */ 679 ret -= 26440; 680 val->intval = ret; 681 682 return 0; 683 } 684 685 static int ltc4015_get_die_temp(struct ltc4162l_info *info, 686 union power_supply_propval *val) 687 { 688 unsigned int regval; 689 int ret; 690 691 ret = regmap_read(info->regmap, LTC4162L_DIE_TEMPERATURE, ®val); 692 if (ret) 693 return ret; 694 695 /* (die_temp - 12010) / 45.6°C */ 696 ret = (s16)(regval & 0xFFFF); 697 ret -= 12010; 698 ret *= 1000; 699 ret /= 456; 700 val->intval = ret; 701 702 return 0; 703 } 704 705 static int ltc4162l_get_term_current(struct ltc4162l_info *info, 706 union power_supply_propval *val) 707 { 708 const struct ltc4162l_chip_info *chip_info = info->chip_info; 709 unsigned int regval; 710 int ret; 711 712 ret = regmap_read(info->regmap, LTC4162L_CHARGER_CONFIG_BITS, ®val); 713 if (ret) 714 return ret; 715 716 /* Check if C_OVER_X_THRESHOLD is enabled */ 717 if (!(regval & BIT(2))) { 718 val->intval = 0; 719 return 0; 720 } 721 722 ret = regmap_read(info->regmap, LTC4162L_C_OVER_X_THRESHOLD, ®val); 723 if (ret) 724 return ret; 725 726 regval *= chip_info->ibat_resolution_pv; 727 regval /= info->rsnsb; 728 val->intval = regval; 729 730 return 0; 731 } 732 733 static int ltc4162l_set_term_current(struct ltc4162l_info *info, 734 unsigned int value) 735 { 736 int ret; 737 unsigned int regval; 738 739 if (!value) { 740 /* Disable en_c_over_x_term when set to zero */ 741 return regmap_update_bits(info->regmap, 742 LTC4162L_CHARGER_CONFIG_BITS, 743 BIT(2), 0); 744 } 745 746 regval = mult_frac(value, info->rsnsb, 14660u); 747 regval /= 100u; 748 749 ret = regmap_write(info->regmap, LTC4162L_C_OVER_X_THRESHOLD, regval); 750 if (ret) 751 return ret; 752 753 /* Set en_c_over_x_term after changing the threshold value */ 754 return regmap_update_bits(info->regmap, LTC4162L_CHARGER_CONFIG_BITS, 755 BIT(2), BIT(2)); 756 } 757 758 /* Custom properties */ 759 static const char * const ltc4162l_charge_status_name[] = { 760 "ilim_reg_active", /* 32 */ 761 "thermal_reg_active", 762 "vin_uvcl_active", 763 "iin_limit_active", 764 "constant_current", 765 "constant_voltage", 766 "charger_off" /* 0 */ 767 }; 768 769 static ssize_t charge_status_show(struct device *dev, 770 struct device_attribute *attr, char *buf) 771 { 772 struct power_supply *psy = to_power_supply(dev); 773 struct ltc4162l_info *info = power_supply_get_drvdata(psy); 774 const char *result = ltc4162l_charge_status_name[ 775 ARRAY_SIZE(ltc4162l_charge_status_name) - 1]; 776 unsigned int regval; 777 unsigned int mask; 778 unsigned int index; 779 int ret; 780 781 ret = regmap_read(info->regmap, LTC4162L_CHARGE_STATUS, ®val); 782 if (ret) 783 return ret; 784 785 /* Only one bit is set according to datasheet, let's be safe here */ 786 for (mask = 32, index = 0; mask != 0; mask >>= 1, ++index) { 787 if (regval & mask) { 788 result = ltc4162l_charge_status_name[index]; 789 break; 790 } 791 } 792 793 return sysfs_emit(buf, "%s\n", result); 794 } 795 static DEVICE_ATTR_RO(charge_status); 796 797 static ssize_t vbat_show(struct device *dev, 798 struct device_attribute *attr, char *buf) 799 { 800 struct power_supply *psy = to_power_supply(dev); 801 struct ltc4162l_info *info = power_supply_get_drvdata(psy); 802 const struct ltc4162l_chip_info *chip_info = info->chip_info; 803 union power_supply_propval val; 804 int ret; 805 806 ret = chip_info->get_vbat(info, LTC4162L_VBAT, &val); 807 if (ret) 808 return ret; 809 810 return sysfs_emit(buf, "%d\n", val.intval); 811 } 812 static DEVICE_ATTR_RO(vbat); 813 814 static ssize_t vbat_avg_show(struct device *dev, 815 struct device_attribute *attr, char *buf) 816 { 817 struct power_supply *psy = to_power_supply(dev); 818 struct ltc4162l_info *info = power_supply_get_drvdata(psy); 819 const struct ltc4162l_chip_info *chip_info = info->chip_info; 820 union power_supply_propval val; 821 int ret; 822 823 ret = chip_info->get_vbat(info, LTC4162L_VBAT_FILT, &val); 824 if (ret) 825 return ret; 826 827 return sysfs_emit(buf, "%d\n", val.intval); 828 } 829 static DEVICE_ATTR_RO(vbat_avg); 830 831 static ssize_t ibat_show(struct device *dev, 832 struct device_attribute *attr, char *buf) 833 { 834 struct power_supply *psy = to_power_supply(dev); 835 struct ltc4162l_info *info = power_supply_get_drvdata(psy); 836 union power_supply_propval val; 837 int ret; 838 839 ret = ltc4162l_get_ibat(info, &val); 840 if (ret) 841 return ret; 842 843 return sysfs_emit(buf, "%d\n", val.intval); 844 } 845 static DEVICE_ATTR_RO(ibat); 846 847 static ssize_t force_telemetry_show(struct device *dev, 848 struct device_attribute *attr, char *buf) 849 { 850 struct power_supply *psy = to_power_supply(dev); 851 struct ltc4162l_info *info = power_supply_get_drvdata(psy); 852 unsigned int regval; 853 int ret; 854 855 ret = regmap_read(info->regmap, LTC4162L_CONFIG_BITS_REG, ®val); 856 if (ret) 857 return ret; 858 859 return sysfs_emit(buf, "%u\n", regval & 860 info->chip_info->telemetry_mask ? 1 : 0); 861 } 862 863 static ssize_t force_telemetry_store(struct device *dev, 864 struct device_attribute *attr, 865 const char *buf, 866 size_t count) 867 { 868 struct power_supply *psy = to_power_supply(dev); 869 struct ltc4162l_info *info = power_supply_get_drvdata(psy); 870 int ret; 871 unsigned int value; 872 873 ret = kstrtouint(buf, 0, &value); 874 if (ret < 0) 875 return ret; 876 877 ret = regmap_update_bits(info->regmap, LTC4162L_CONFIG_BITS_REG, 878 info->chip_info->telemetry_mask, 879 value ? info->chip_info->telemetry_mask : 0); 880 if (ret < 0) 881 return ret; 882 883 return count; 884 } 885 886 static DEVICE_ATTR_RW(force_telemetry); 887 888 static ssize_t arm_ship_mode_show(struct device *dev, 889 struct device_attribute *attr, char *buf) 890 { 891 struct power_supply *psy = to_power_supply(dev); 892 struct ltc4162l_info *info = power_supply_get_drvdata(psy); 893 unsigned int regval; 894 int ret; 895 896 ret = regmap_read(info->regmap, LTC4162L_ARM_SHIP_MODE, ®val); 897 if (ret) 898 return ret; 899 900 return sysfs_emit(buf, "%u\n", 901 regval == LTC4162L_ARM_SHIP_MODE_MAGIC ? 1 : 0); 902 } 903 904 static ssize_t arm_ship_mode_store(struct device *dev, 905 struct device_attribute *attr, 906 const char *buf, 907 size_t count) 908 { 909 struct power_supply *psy = to_power_supply(dev); 910 struct ltc4162l_info *info = power_supply_get_drvdata(psy); 911 int ret; 912 unsigned int value; 913 914 ret = kstrtouint(buf, 0, &value); 915 if (ret < 0) 916 return ret; 917 918 ret = regmap_write(info->regmap, LTC4162L_ARM_SHIP_MODE, 919 value ? LTC4162L_ARM_SHIP_MODE_MAGIC : 0); 920 if (ret < 0) 921 return ret; 922 923 return count; 924 } 925 926 static DEVICE_ATTR_RW(arm_ship_mode); 927 928 static struct attribute *ltc4162l_sysfs_entries[] = { 929 &dev_attr_charge_status.attr, 930 &dev_attr_ibat.attr, 931 &dev_attr_vbat.attr, 932 &dev_attr_vbat_avg.attr, 933 &dev_attr_force_telemetry.attr, 934 &dev_attr_arm_ship_mode.attr, 935 NULL, 936 }; 937 938 static const struct attribute_group ltc4162l_attr_group = { 939 .name = NULL, /* put in device directory */ 940 .attrs = ltc4162l_sysfs_entries, 941 }; 942 943 static const struct attribute_group *ltc4162l_attr_groups[] = { 944 <c4162l_attr_group, 945 NULL, 946 }; 947 948 static int ltc4162l_get_property(struct power_supply *psy, 949 enum power_supply_property psp, 950 union power_supply_propval *val) 951 { 952 struct ltc4162l_info *info = power_supply_get_drvdata(psy); 953 const struct ltc4162l_chip_info *chip_info = info->chip_info; 954 955 switch (psp) { 956 case POWER_SUPPLY_PROP_STATUS: 957 return ltc4162l_get_status(info, val); 958 case POWER_SUPPLY_PROP_CHARGE_TYPE: 959 return ltc4162l_get_charge_type(info, val); 960 case POWER_SUPPLY_PROP_HEALTH: 961 return ltc4162l_get_health(info, val); 962 case POWER_SUPPLY_PROP_ONLINE: 963 return ltc4162l_get_online(info, val); 964 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 965 return ltc4162l_get_input_voltage(info, val); 966 case POWER_SUPPLY_PROP_CURRENT_NOW: 967 return ltc4162l_get_input_current(info, val); 968 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 969 return ltc4162l_get_icharge(info, 970 LTC4162L_ICHARGE_DAC, val); 971 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: 972 return ltc4162l_get_icharge(info, 973 LTC4162L_CHARGE_CURRENT_SETTING, val); 974 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 975 return chip_info->get_vcharge(info, LTC4162L_VCHARGE_DAC, val); 976 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: 977 return chip_info->get_vcharge(info, LTC4162L_VCHARGE_SETTING, val); 978 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 979 return ltc4162l_get_iin_limit_dac(info, val); 980 case POWER_SUPPLY_PROP_TEMP: 981 return chip_info->get_die_temp(info, val); 982 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: 983 return ltc4162l_get_term_current(info, val); 984 default: 985 return -EINVAL; 986 } 987 } 988 989 static int ltc4162l_set_property(struct power_supply *psy, 990 enum power_supply_property psp, 991 const union power_supply_propval *val) 992 { 993 struct ltc4162l_info *info = power_supply_get_drvdata(psy); 994 995 switch (psp) { 996 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: 997 return ltc4162l_set_icharge(info, 998 LTC4162L_CHARGE_CURRENT_SETTING, val->intval); 999 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: 1000 return ltc4162l_set_vcharge(info, 1001 LTC4162L_VCHARGE_SETTING, val->intval); 1002 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 1003 return ltc4162l_set_iin_limit(info, val->intval); 1004 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: 1005 return ltc4162l_set_term_current(info, val->intval); 1006 default: 1007 return -EINVAL; 1008 } 1009 } 1010 1011 static int ltc4162l_property_is_writeable(struct power_supply *psy, 1012 enum power_supply_property psp) 1013 { 1014 switch (psp) { 1015 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: 1016 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: 1017 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 1018 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: 1019 return 1; 1020 default: 1021 return 0; 1022 } 1023 } 1024 1025 /* Charger power supply property routines */ 1026 static enum power_supply_property ltc4162l_properties[] = { 1027 POWER_SUPPLY_PROP_STATUS, 1028 POWER_SUPPLY_PROP_CHARGE_TYPE, 1029 POWER_SUPPLY_PROP_HEALTH, 1030 POWER_SUPPLY_PROP_ONLINE, 1031 POWER_SUPPLY_PROP_VOLTAGE_NOW, 1032 POWER_SUPPLY_PROP_CURRENT_NOW, 1033 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, 1034 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 1035 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, 1036 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 1037 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, 1038 POWER_SUPPLY_PROP_TEMP, 1039 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT, 1040 }; 1041 1042 static const struct power_supply_desc ltc4162l_desc = { 1043 .type = POWER_SUPPLY_TYPE_MAINS, 1044 .properties = ltc4162l_properties, 1045 .num_properties = ARRAY_SIZE(ltc4162l_properties), 1046 .get_property = ltc4162l_get_property, 1047 .set_property = ltc4162l_set_property, 1048 .property_is_writeable = ltc4162l_property_is_writeable, 1049 }; 1050 1051 static const struct ltc4162l_chip_info ltc4162l_chip_info = { 1052 .name = "ltc4162-l", 1053 .get_vbat = ltc4162l_get_vbat, 1054 .get_vcharge = ltc4162l_get_vcharge, 1055 .set_vcharge = ltc4162l_set_vcharge, 1056 .get_die_temp = ltc4162l_get_die_temp, 1057 .ibat_resolution_pv = 1466000, 1058 .vin_resolution_uv = 1649, 1059 .telemetry_mask = BIT(2), 1060 }; 1061 1062 static const struct ltc4162l_chip_info ltc4162f_chip_info = { 1063 .name = "ltc4162-f", 1064 .get_vbat = ltc4162l_get_vbat, 1065 .get_vcharge = ltc4162l_get_vcharge, 1066 .set_vcharge = ltc4162l_set_vcharge, 1067 .get_die_temp = ltc4162l_get_die_temp, 1068 .ibat_resolution_pv = 1466000, 1069 .vin_resolution_uv = 1649, 1070 .telemetry_mask = BIT(2), 1071 }; 1072 1073 static const struct ltc4162l_chip_info ltc4162s_chip_info = { 1074 .name = "ltc4162-s", 1075 .get_vbat = ltc4162l_get_vbat, 1076 .get_vcharge = ltc4162l_get_vcharge, 1077 .set_vcharge = ltc4162l_set_vcharge, 1078 .get_die_temp = ltc4162l_get_die_temp, 1079 .ibat_resolution_pv = 1466000, 1080 .vin_resolution_uv = 1649, 1081 .telemetry_mask = BIT(2), 1082 }; 1083 1084 static const struct ltc4162l_chip_info ltc4015_chip_info = { 1085 .name = "ltc4015", 1086 .get_vbat = ltc4015_get_vbat, 1087 .get_vcharge = ltc4015_get_vcharge, 1088 .set_vcharge = ltc4015_set_vcharge, 1089 .get_die_temp = ltc4015_get_die_temp, 1090 .ibat_resolution_pv = 1464870, 1091 .vin_resolution_uv = 1648, 1092 .telemetry_mask = BIT(4), 1093 }; 1094 1095 static bool ltc4162l_is_writeable_reg(struct device *dev, unsigned int reg) 1096 { 1097 /* all registers up to this one are writeable */ 1098 if (reg <= LTC4162L_CHARGER_CONFIG_BITS) 1099 return true; 1100 1101 /* The ALERTS registers can be written to clear alerts */ 1102 if (reg >= LTC4162L_LIMIT_ALERTS_REG && 1103 reg <= LTC4162L_CHARGE_STATUS_ALERTS_REG) 1104 return true; 1105 1106 return false; 1107 } 1108 1109 static bool ltc4162l_is_volatile_reg(struct device *dev, unsigned int reg) 1110 { 1111 /* all registers after this one are read-only status registers */ 1112 return reg > LTC4162L_CHARGER_CONFIG_BITS; 1113 } 1114 1115 static const struct regmap_config ltc4162l_regmap_config = { 1116 .reg_bits = 8, 1117 .val_bits = 16, 1118 .val_format_endian = REGMAP_ENDIAN_LITTLE, 1119 .writeable_reg = ltc4162l_is_writeable_reg, 1120 .volatile_reg = ltc4162l_is_volatile_reg, 1121 .max_register = LTC4162L_INPUT_UNDERVOLTAGE_DAC, 1122 .cache_type = REGCACHE_RBTREE, 1123 }; 1124 1125 static void ltc4162l_clear_interrupts(struct ltc4162l_info *info) 1126 { 1127 /* Acknowledge interrupt to chip by clearing all events */ 1128 regmap_write(info->regmap, LTC4162L_LIMIT_ALERTS_REG, 0); 1129 regmap_write(info->regmap, LTC4162L_CHARGER_STATE_ALERTS_REG, 0); 1130 regmap_write(info->regmap, LTC4162L_CHARGE_STATUS_ALERTS_REG, 0); 1131 } 1132 1133 static int ltc4162l_probe(struct i2c_client *client) 1134 { 1135 struct i2c_adapter *adapter = client->adapter; 1136 struct device *dev = &client->dev; 1137 struct ltc4162l_info *info; 1138 struct power_supply_config ltc4162l_config = {}; 1139 struct power_supply_desc *desc; 1140 const struct ltc4162l_chip_info *chip_info; 1141 u32 value; 1142 int ret; 1143 1144 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { 1145 dev_err(dev, "No support for SMBUS_WORD_DATA\n"); 1146 return -ENODEV; 1147 } 1148 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); 1149 if (!info) 1150 return -ENOMEM; 1151 1152 info->client = client; 1153 i2c_set_clientdata(client, info); 1154 1155 chip_info = i2c_get_match_data(client); 1156 if (!chip_info) 1157 return -ENODEV; 1158 1159 info->chip_info = chip_info; 1160 1161 info->regmap = devm_regmap_init_i2c(client, <c4162l_regmap_config); 1162 if (IS_ERR(info->regmap)) { 1163 dev_err(dev, "Failed to initialize register map\n"); 1164 return PTR_ERR(info->regmap); 1165 } 1166 1167 ret = device_property_read_u32(dev, "lltc,rsnsb-micro-ohms", 1168 &info->rsnsb); 1169 if (ret) { 1170 dev_err(dev, "Missing lltc,rsnsb-micro-ohms property\n"); 1171 return ret; 1172 } 1173 if (!info->rsnsb) 1174 return -EINVAL; 1175 1176 ret = device_property_read_u32(dev, "lltc,rsnsi-micro-ohms", 1177 &info->rsnsi); 1178 if (ret) { 1179 dev_err(dev, "Missing lltc,rsnsi-micro-ohms property\n"); 1180 return ret; 1181 } 1182 if (!info->rsnsi) 1183 return -EINVAL; 1184 1185 if (!device_property_read_u32(dev, "lltc,cell-count", &value)) 1186 info->cell_count = value; 1187 1188 ltc4162l_config.of_node = dev->of_node; 1189 ltc4162l_config.drv_data = info; 1190 ltc4162l_config.attr_grp = ltc4162l_attr_groups; 1191 1192 /* Duplicate the default descriptor to set name based on chip_info. */ 1193 desc = devm_kmemdup(dev, <c4162l_desc, 1194 sizeof(struct power_supply_desc), GFP_KERNEL); 1195 if (!desc) 1196 return -ENOMEM; 1197 1198 desc->name = chip_info->name; 1199 1200 info->charger = devm_power_supply_register(dev, desc, <c4162l_config); 1201 if (IS_ERR(info->charger)) { 1202 dev_err(dev, "Failed to register charger\n"); 1203 return PTR_ERR(info->charger); 1204 } 1205 1206 /* Disable the threshold alerts, we're not using them */ 1207 regmap_write(info->regmap, LTC4162L_EN_LIMIT_ALERTS_REG, 0); 1208 1209 /* Enable interrupts on all status changes */ 1210 regmap_write(info->regmap, LTC4162L_EN_CHARGER_STATE_ALERTS_REG, 1211 0x1fff); 1212 regmap_write(info->regmap, LTC4162L_EN_CHARGE_STATUS_ALERTS_REG, 0x1f); 1213 1214 ltc4162l_clear_interrupts(info); 1215 1216 return 0; 1217 } 1218 1219 static void ltc4162l_alert(struct i2c_client *client, 1220 enum i2c_alert_protocol type, unsigned int flag) 1221 { 1222 struct ltc4162l_info *info = i2c_get_clientdata(client); 1223 1224 if (type != I2C_PROTOCOL_SMBUS_ALERT) 1225 return; 1226 1227 ltc4162l_clear_interrupts(info); 1228 power_supply_changed(info->charger); 1229 } 1230 1231 static const struct i2c_device_id ltc4162l_i2c_id_table[] = { 1232 { "ltc4015", (kernel_ulong_t)<c4015_chip_info }, 1233 { "ltc4162-f", (kernel_ulong_t)<c4162f_chip_info }, 1234 { "ltc4162-l", (kernel_ulong_t)<c4162l_chip_info }, 1235 { "ltc4162-s", (kernel_ulong_t)<c4162s_chip_info }, 1236 { } 1237 }; 1238 MODULE_DEVICE_TABLE(i2c, ltc4162l_i2c_id_table); 1239 1240 static const struct of_device_id ltc4162l_of_match[] __maybe_unused = { 1241 { .compatible = "lltc,ltc4015", .data = <c4015_chip_info }, 1242 { .compatible = "lltc,ltc4162-f", .data = <c4162f_chip_info }, 1243 { .compatible = "lltc,ltc4162-l", .data = <c4162l_chip_info }, 1244 { .compatible = "lltc,ltc4162-s", .data = <c4162s_chip_info }, 1245 { } 1246 }; 1247 MODULE_DEVICE_TABLE(of, ltc4162l_of_match); 1248 1249 static struct i2c_driver ltc4162l_driver = { 1250 .probe = ltc4162l_probe, 1251 .alert = ltc4162l_alert, 1252 .id_table = ltc4162l_i2c_id_table, 1253 .driver = { 1254 .name = "ltc4162-l-charger", 1255 .of_match_table = of_match_ptr(ltc4162l_of_match), 1256 }, 1257 }; 1258 module_i2c_driver(ltc4162l_driver); 1259 1260 MODULE_LICENSE("GPL"); 1261 MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>"); 1262 MODULE_DESCRIPTION("LTC4162-L charger driver"); 1263