1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Gas Gauge driver for SBS Compliant Batteries 4 * 5 * Copyright (c) 2010, NVIDIA Corporation. 6 */ 7 8 #include <linux/bits.h> 9 #include <linux/delay.h> 10 #include <linux/err.h> 11 #include <linux/gpio/consumer.h> 12 #include <linux/i2c.h> 13 #include <linux/init.h> 14 #include <linux/interrupt.h> 15 #include <linux/kernel.h> 16 #include <linux/module.h> 17 #include <linux/property.h> 18 #include <linux/of_device.h> 19 #include <linux/power/sbs-battery.h> 20 #include <linux/power_supply.h> 21 #include <linux/slab.h> 22 #include <linux/stat.h> 23 24 enum { 25 REG_MANUFACTURER_DATA, 26 REG_BATTERY_MODE, 27 REG_TEMPERATURE, 28 REG_VOLTAGE, 29 REG_CURRENT_NOW, 30 REG_CURRENT_AVG, 31 REG_MAX_ERR, 32 REG_CAPACITY, 33 REG_TIME_TO_EMPTY, 34 REG_TIME_TO_FULL, 35 REG_STATUS, 36 REG_CAPACITY_LEVEL, 37 REG_CYCLE_COUNT, 38 REG_SERIAL_NUMBER, 39 REG_REMAINING_CAPACITY, 40 REG_REMAINING_CAPACITY_CHARGE, 41 REG_FULL_CHARGE_CAPACITY, 42 REG_FULL_CHARGE_CAPACITY_CHARGE, 43 REG_DESIGN_CAPACITY, 44 REG_DESIGN_CAPACITY_CHARGE, 45 REG_DESIGN_VOLTAGE_MIN, 46 REG_DESIGN_VOLTAGE_MAX, 47 REG_CHEMISTRY, 48 REG_MANUFACTURER, 49 REG_MODEL_NAME, 50 REG_CHARGE_CURRENT, 51 REG_CHARGE_VOLTAGE, 52 }; 53 54 #define REG_ADDR_MANUFACTURE_DATE 0x1B 55 56 /* Battery Mode defines */ 57 #define BATTERY_MODE_OFFSET 0x03 58 #define BATTERY_MODE_CAPACITY_MASK BIT(15) 59 enum sbs_capacity_mode { 60 CAPACITY_MODE_AMPS = 0, 61 CAPACITY_MODE_WATTS = BATTERY_MODE_CAPACITY_MASK 62 }; 63 #define BATTERY_MODE_CHARGER_MASK (1<<14) 64 65 /* manufacturer access defines */ 66 #define MANUFACTURER_ACCESS_STATUS 0x0006 67 #define MANUFACTURER_ACCESS_SLEEP 0x0011 68 69 /* battery status value bits */ 70 #define BATTERY_INITIALIZED 0x80 71 #define BATTERY_DISCHARGING 0x40 72 #define BATTERY_FULL_CHARGED 0x20 73 #define BATTERY_FULL_DISCHARGED 0x10 74 75 /* min_value and max_value are only valid for numerical data */ 76 #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \ 77 .psp = _psp, \ 78 .addr = _addr, \ 79 .min_value = _min_value, \ 80 .max_value = _max_value, \ 81 } 82 83 static const struct chip_data { 84 enum power_supply_property psp; 85 u8 addr; 86 int min_value; 87 int max_value; 88 } sbs_data[] = { 89 [REG_MANUFACTURER_DATA] = 90 SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535), 91 [REG_BATTERY_MODE] = 92 SBS_DATA(-1, 0x03, 0, 65535), 93 [REG_TEMPERATURE] = 94 SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535), 95 [REG_VOLTAGE] = 96 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000), 97 [REG_CURRENT_NOW] = 98 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767), 99 [REG_CURRENT_AVG] = 100 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_AVG, 0x0B, -32768, 32767), 101 [REG_MAX_ERR] = 102 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN, 0x0c, 0, 100), 103 [REG_CAPACITY] = 104 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100), 105 [REG_REMAINING_CAPACITY] = 106 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535), 107 [REG_REMAINING_CAPACITY_CHARGE] = 108 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535), 109 [REG_FULL_CHARGE_CAPACITY] = 110 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535), 111 [REG_FULL_CHARGE_CAPACITY_CHARGE] = 112 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535), 113 [REG_TIME_TO_EMPTY] = 114 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535), 115 [REG_TIME_TO_FULL] = 116 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535), 117 [REG_CHARGE_CURRENT] = 118 SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 0x14, 0, 65535), 119 [REG_CHARGE_VOLTAGE] = 120 SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 0x15, 0, 65535), 121 [REG_STATUS] = 122 SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535), 123 [REG_CAPACITY_LEVEL] = 124 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535), 125 [REG_CYCLE_COUNT] = 126 SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535), 127 [REG_DESIGN_CAPACITY] = 128 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535), 129 [REG_DESIGN_CAPACITY_CHARGE] = 130 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535), 131 [REG_DESIGN_VOLTAGE_MIN] = 132 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535), 133 [REG_DESIGN_VOLTAGE_MAX] = 134 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535), 135 [REG_SERIAL_NUMBER] = 136 SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535), 137 /* Properties of type `const char *' */ 138 [REG_MANUFACTURER] = 139 SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535), 140 [REG_MODEL_NAME] = 141 SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535), 142 [REG_CHEMISTRY] = 143 SBS_DATA(POWER_SUPPLY_PROP_TECHNOLOGY, 0x22, 0, 65535) 144 }; 145 146 static const enum power_supply_property sbs_properties[] = { 147 POWER_SUPPLY_PROP_STATUS, 148 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 149 POWER_SUPPLY_PROP_HEALTH, 150 POWER_SUPPLY_PROP_PRESENT, 151 POWER_SUPPLY_PROP_TECHNOLOGY, 152 POWER_SUPPLY_PROP_CYCLE_COUNT, 153 POWER_SUPPLY_PROP_VOLTAGE_NOW, 154 POWER_SUPPLY_PROP_CURRENT_NOW, 155 POWER_SUPPLY_PROP_CURRENT_AVG, 156 POWER_SUPPLY_PROP_CAPACITY, 157 POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN, 158 POWER_SUPPLY_PROP_TEMP, 159 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 160 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 161 POWER_SUPPLY_PROP_SERIAL_NUMBER, 162 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 163 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 164 POWER_SUPPLY_PROP_ENERGY_NOW, 165 POWER_SUPPLY_PROP_ENERGY_FULL, 166 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 167 POWER_SUPPLY_PROP_CHARGE_NOW, 168 POWER_SUPPLY_PROP_CHARGE_FULL, 169 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 170 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 171 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 172 POWER_SUPPLY_PROP_MANUFACTURE_YEAR, 173 POWER_SUPPLY_PROP_MANUFACTURE_MONTH, 174 POWER_SUPPLY_PROP_MANUFACTURE_DAY, 175 /* Properties of type `const char *' */ 176 POWER_SUPPLY_PROP_MANUFACTURER, 177 POWER_SUPPLY_PROP_MODEL_NAME 178 }; 179 180 /* Supports special manufacturer commands from TI BQ20Z65 and BQ20Z75 IC. */ 181 #define SBS_FLAGS_TI_BQ20ZX5 BIT(0) 182 183 struct sbs_info { 184 struct i2c_client *client; 185 struct power_supply *power_supply; 186 bool is_present; 187 struct gpio_desc *gpio_detect; 188 bool enable_detection; 189 bool charger_broadcasts; 190 int last_state; 191 int poll_time; 192 u32 i2c_retry_count; 193 u32 poll_retry_count; 194 struct delayed_work work; 195 struct mutex mode_lock; 196 u32 flags; 197 }; 198 199 static char model_name[I2C_SMBUS_BLOCK_MAX + 1]; 200 static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1]; 201 static char chemistry[I2C_SMBUS_BLOCK_MAX + 1]; 202 static bool force_load; 203 204 static int sbs_read_word_data(struct i2c_client *client, u8 address); 205 static int sbs_write_word_data(struct i2c_client *client, u8 address, u16 value); 206 207 static void sbs_disable_charger_broadcasts(struct sbs_info *chip) 208 { 209 int val = sbs_read_word_data(chip->client, BATTERY_MODE_OFFSET); 210 if (val < 0) 211 goto exit; 212 213 val |= BATTERY_MODE_CHARGER_MASK; 214 215 val = sbs_write_word_data(chip->client, BATTERY_MODE_OFFSET, val); 216 217 exit: 218 if (val < 0) 219 dev_err(&chip->client->dev, 220 "Failed to disable charger broadcasting: %d\n", val); 221 else 222 dev_dbg(&chip->client->dev, "%s\n", __func__); 223 } 224 225 static int sbs_update_presence(struct sbs_info *chip, bool is_present) 226 { 227 if (chip->is_present == is_present) 228 return 0; 229 230 if (!is_present) { 231 chip->is_present = false; 232 return 0; 233 } 234 235 if (!chip->is_present && is_present && !chip->charger_broadcasts) 236 sbs_disable_charger_broadcasts(chip); 237 238 chip->is_present = true; 239 240 return 0; 241 } 242 243 static int sbs_read_word_data(struct i2c_client *client, u8 address) 244 { 245 struct sbs_info *chip = i2c_get_clientdata(client); 246 int retries = chip->i2c_retry_count; 247 s32 ret = 0; 248 249 while (retries > 0) { 250 ret = i2c_smbus_read_word_data(client, address); 251 if (ret >= 0) 252 break; 253 retries--; 254 } 255 256 if (ret < 0) { 257 dev_dbg(&client->dev, 258 "%s: i2c read at address 0x%x failed\n", 259 __func__, address); 260 return ret; 261 } 262 263 return ret; 264 } 265 266 static int sbs_read_string_data(struct i2c_client *client, u8 address, 267 char *values) 268 { 269 struct sbs_info *chip = i2c_get_clientdata(client); 270 s32 ret = 0, block_length = 0; 271 int retries_length, retries_block; 272 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1]; 273 274 retries_length = chip->i2c_retry_count; 275 retries_block = chip->i2c_retry_count; 276 277 /* Adapter needs to support these two functions */ 278 if (!i2c_check_functionality(client->adapter, 279 I2C_FUNC_SMBUS_BYTE_DATA | 280 I2C_FUNC_SMBUS_I2C_BLOCK)){ 281 return -ENODEV; 282 } 283 284 /* Get the length of block data */ 285 while (retries_length > 0) { 286 ret = i2c_smbus_read_byte_data(client, address); 287 if (ret >= 0) 288 break; 289 retries_length--; 290 } 291 292 if (ret < 0) { 293 dev_dbg(&client->dev, 294 "%s: i2c read at address 0x%x failed\n", 295 __func__, address); 296 return ret; 297 } 298 299 /* block_length does not include NULL terminator */ 300 block_length = ret; 301 if (block_length > I2C_SMBUS_BLOCK_MAX) { 302 dev_err(&client->dev, 303 "%s: Returned block_length is longer than 0x%x\n", 304 __func__, I2C_SMBUS_BLOCK_MAX); 305 return -EINVAL; 306 } 307 308 /* Get the block data */ 309 while (retries_block > 0) { 310 ret = i2c_smbus_read_i2c_block_data( 311 client, address, 312 block_length + 1, block_buffer); 313 if (ret >= 0) 314 break; 315 retries_block--; 316 } 317 318 if (ret < 0) { 319 dev_dbg(&client->dev, 320 "%s: i2c read at address 0x%x failed\n", 321 __func__, address); 322 return ret; 323 } 324 325 /* block_buffer[0] == block_length */ 326 memcpy(values, block_buffer + 1, block_length); 327 values[block_length] = '\0'; 328 329 return ret; 330 } 331 332 static int sbs_write_word_data(struct i2c_client *client, u8 address, 333 u16 value) 334 { 335 struct sbs_info *chip = i2c_get_clientdata(client); 336 int retries = chip->i2c_retry_count; 337 s32 ret = 0; 338 339 while (retries > 0) { 340 ret = i2c_smbus_write_word_data(client, address, value); 341 if (ret >= 0) 342 break; 343 retries--; 344 } 345 346 if (ret < 0) { 347 dev_dbg(&client->dev, 348 "%s: i2c write to address 0x%x failed\n", 349 __func__, address); 350 return ret; 351 } 352 353 return 0; 354 } 355 356 static int sbs_status_correct(struct i2c_client *client, int *intval) 357 { 358 int ret; 359 360 ret = sbs_read_word_data(client, sbs_data[REG_CURRENT_NOW].addr); 361 if (ret < 0) 362 return ret; 363 364 ret = (s16)ret; 365 366 /* Not drawing current -> not charging (i.e. idle) */ 367 if (*intval != POWER_SUPPLY_STATUS_FULL && ret == 0) 368 *intval = POWER_SUPPLY_STATUS_NOT_CHARGING; 369 370 if (*intval == POWER_SUPPLY_STATUS_FULL) { 371 /* Drawing or providing current when full */ 372 if (ret > 0) 373 *intval = POWER_SUPPLY_STATUS_CHARGING; 374 else if (ret < 0) 375 *intval = POWER_SUPPLY_STATUS_DISCHARGING; 376 } 377 378 return 0; 379 } 380 381 static bool sbs_bat_needs_calibration(struct i2c_client *client) 382 { 383 int ret; 384 385 ret = sbs_read_word_data(client, sbs_data[REG_BATTERY_MODE].addr); 386 if (ret < 0) 387 return false; 388 389 return !!(ret & BIT(7)); 390 } 391 392 static int sbs_get_battery_presence_and_health( 393 struct i2c_client *client, enum power_supply_property psp, 394 union power_supply_propval *val) 395 { 396 int ret; 397 398 /* Dummy command; if it succeeds, battery is present. */ 399 ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr); 400 401 if (ret < 0) { /* battery not present*/ 402 if (psp == POWER_SUPPLY_PROP_PRESENT) { 403 val->intval = 0; 404 return 0; 405 } 406 return ret; 407 } 408 409 if (psp == POWER_SUPPLY_PROP_PRESENT) 410 val->intval = 1; /* battery present */ 411 else { /* POWER_SUPPLY_PROP_HEALTH */ 412 if (sbs_bat_needs_calibration(client)) { 413 val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED; 414 } else { 415 /* SBS spec doesn't have a general health command. */ 416 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN; 417 } 418 } 419 420 return 0; 421 } 422 423 static int sbs_get_ti_battery_presence_and_health( 424 struct i2c_client *client, enum power_supply_property psp, 425 union power_supply_propval *val) 426 { 427 s32 ret; 428 429 /* 430 * Write to ManufacturerAccess with ManufacturerAccess command 431 * and then read the status. 432 */ 433 ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr, 434 MANUFACTURER_ACCESS_STATUS); 435 if (ret < 0) { 436 if (psp == POWER_SUPPLY_PROP_PRESENT) 437 val->intval = 0; /* battery removed */ 438 return ret; 439 } 440 441 ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr); 442 if (ret < 0) { 443 if (psp == POWER_SUPPLY_PROP_PRESENT) 444 val->intval = 0; /* battery removed */ 445 return ret; 446 } 447 448 if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value || 449 ret > sbs_data[REG_MANUFACTURER_DATA].max_value) { 450 val->intval = 0; 451 return 0; 452 } 453 454 /* Mask the upper nibble of 2nd byte and 455 * lower byte of response then 456 * shift the result by 8 to get status*/ 457 ret &= 0x0F00; 458 ret >>= 8; 459 if (psp == POWER_SUPPLY_PROP_PRESENT) { 460 if (ret == 0x0F) 461 /* battery removed */ 462 val->intval = 0; 463 else 464 val->intval = 1; 465 } else if (psp == POWER_SUPPLY_PROP_HEALTH) { 466 if (ret == 0x09) 467 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 468 else if (ret == 0x0B) 469 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; 470 else if (ret == 0x0C) 471 val->intval = POWER_SUPPLY_HEALTH_DEAD; 472 else if (sbs_bat_needs_calibration(client)) 473 val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED; 474 else 475 val->intval = POWER_SUPPLY_HEALTH_GOOD; 476 } 477 478 return 0; 479 } 480 481 static int sbs_get_battery_property(struct i2c_client *client, 482 int reg_offset, enum power_supply_property psp, 483 union power_supply_propval *val) 484 { 485 struct sbs_info *chip = i2c_get_clientdata(client); 486 s32 ret; 487 488 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr); 489 if (ret < 0) 490 return ret; 491 492 /* returned values are 16 bit */ 493 if (sbs_data[reg_offset].min_value < 0) 494 ret = (s16)ret; 495 496 if (ret >= sbs_data[reg_offset].min_value && 497 ret <= sbs_data[reg_offset].max_value) { 498 val->intval = ret; 499 if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) { 500 if (!(ret & BATTERY_INITIALIZED)) 501 val->intval = 502 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; 503 else if (ret & BATTERY_FULL_CHARGED) 504 val->intval = 505 POWER_SUPPLY_CAPACITY_LEVEL_FULL; 506 else if (ret & BATTERY_FULL_DISCHARGED) 507 val->intval = 508 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; 509 else 510 val->intval = 511 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; 512 return 0; 513 } else if (psp != POWER_SUPPLY_PROP_STATUS) { 514 return 0; 515 } 516 517 if (ret & BATTERY_FULL_CHARGED) 518 val->intval = POWER_SUPPLY_STATUS_FULL; 519 else if (ret & BATTERY_DISCHARGING) 520 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 521 else 522 val->intval = POWER_SUPPLY_STATUS_CHARGING; 523 524 sbs_status_correct(client, &val->intval); 525 526 if (chip->poll_time == 0) 527 chip->last_state = val->intval; 528 else if (chip->last_state != val->intval) { 529 cancel_delayed_work_sync(&chip->work); 530 power_supply_changed(chip->power_supply); 531 chip->poll_time = 0; 532 } 533 } else { 534 if (psp == POWER_SUPPLY_PROP_STATUS) 535 val->intval = POWER_SUPPLY_STATUS_UNKNOWN; 536 else if (psp == POWER_SUPPLY_PROP_CAPACITY) 537 /* sbs spec says that this can be >100 % 538 * even if max value is 100 % 539 */ 540 val->intval = min(ret, 100); 541 else 542 val->intval = 0; 543 } 544 545 return 0; 546 } 547 548 static int sbs_get_battery_string_property(struct i2c_client *client, 549 int reg_offset, enum power_supply_property psp, char *val) 550 { 551 s32 ret; 552 553 ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val); 554 555 if (ret < 0) 556 return ret; 557 558 return 0; 559 } 560 561 static void sbs_unit_adjustment(struct i2c_client *client, 562 enum power_supply_property psp, union power_supply_propval *val) 563 { 564 #define BASE_UNIT_CONVERSION 1000 565 #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION) 566 #define TIME_UNIT_CONVERSION 60 567 #define TEMP_KELVIN_TO_CELSIUS 2731 568 switch (psp) { 569 case POWER_SUPPLY_PROP_ENERGY_NOW: 570 case POWER_SUPPLY_PROP_ENERGY_FULL: 571 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: 572 /* sbs provides energy in units of 10mWh. 573 * Convert to µWh 574 */ 575 val->intval *= BATTERY_MODE_CAP_MULT_WATT; 576 break; 577 578 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 579 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: 580 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: 581 case POWER_SUPPLY_PROP_CURRENT_NOW: 582 case POWER_SUPPLY_PROP_CURRENT_AVG: 583 case POWER_SUPPLY_PROP_CHARGE_NOW: 584 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: 585 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: 586 case POWER_SUPPLY_PROP_CHARGE_FULL: 587 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 588 val->intval *= BASE_UNIT_CONVERSION; 589 break; 590 591 case POWER_SUPPLY_PROP_TEMP: 592 /* sbs provides battery temperature in 0.1K 593 * so convert it to 0.1°C 594 */ 595 val->intval -= TEMP_KELVIN_TO_CELSIUS; 596 break; 597 598 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: 599 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: 600 /* sbs provides time to empty and time to full in minutes. 601 * Convert to seconds 602 */ 603 val->intval *= TIME_UNIT_CONVERSION; 604 break; 605 606 default: 607 dev_dbg(&client->dev, 608 "%s: no need for unit conversion %d\n", __func__, psp); 609 } 610 } 611 612 static enum sbs_capacity_mode sbs_set_capacity_mode(struct i2c_client *client, 613 enum sbs_capacity_mode mode) 614 { 615 int ret, original_val; 616 617 original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET); 618 if (original_val < 0) 619 return original_val; 620 621 if ((original_val & BATTERY_MODE_CAPACITY_MASK) == mode) 622 return mode; 623 624 if (mode == CAPACITY_MODE_AMPS) 625 ret = original_val & ~BATTERY_MODE_CAPACITY_MASK; 626 else 627 ret = original_val | BATTERY_MODE_CAPACITY_MASK; 628 629 ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret); 630 if (ret < 0) 631 return ret; 632 633 usleep_range(1000, 2000); 634 635 return original_val & BATTERY_MODE_CAPACITY_MASK; 636 } 637 638 static int sbs_get_battery_capacity(struct i2c_client *client, 639 int reg_offset, enum power_supply_property psp, 640 union power_supply_propval *val) 641 { 642 s32 ret; 643 enum sbs_capacity_mode mode = CAPACITY_MODE_WATTS; 644 645 if (power_supply_is_amp_property(psp)) 646 mode = CAPACITY_MODE_AMPS; 647 648 mode = sbs_set_capacity_mode(client, mode); 649 if ((int)mode < 0) 650 return mode; 651 652 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr); 653 if (ret < 0) 654 return ret; 655 656 val->intval = ret; 657 658 ret = sbs_set_capacity_mode(client, mode); 659 if (ret < 0) 660 return ret; 661 662 return 0; 663 } 664 665 static char sbs_serial[5]; 666 static int sbs_get_battery_serial_number(struct i2c_client *client, 667 union power_supply_propval *val) 668 { 669 int ret; 670 671 ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr); 672 if (ret < 0) 673 return ret; 674 675 sprintf(sbs_serial, "%04x", ret); 676 val->strval = sbs_serial; 677 678 return 0; 679 } 680 681 static int sbs_get_property_index(struct i2c_client *client, 682 enum power_supply_property psp) 683 { 684 int count; 685 for (count = 0; count < ARRAY_SIZE(sbs_data); count++) 686 if (psp == sbs_data[count].psp) 687 return count; 688 689 dev_warn(&client->dev, 690 "%s: Invalid Property - %d\n", __func__, psp); 691 692 return -EINVAL; 693 } 694 695 static int sbs_get_chemistry(struct i2c_client *client, 696 union power_supply_propval *val) 697 { 698 enum power_supply_property psp = POWER_SUPPLY_PROP_TECHNOLOGY; 699 int ret; 700 701 ret = sbs_get_property_index(client, psp); 702 if (ret < 0) 703 return ret; 704 705 ret = sbs_get_battery_string_property(client, ret, psp, 706 chemistry); 707 if (ret < 0) 708 return ret; 709 710 if (!strncasecmp(chemistry, "LION", 4)) 711 val->intval = POWER_SUPPLY_TECHNOLOGY_LION; 712 else if (!strncasecmp(chemistry, "LiP", 3)) 713 val->intval = POWER_SUPPLY_TECHNOLOGY_LIPO; 714 else if (!strncasecmp(chemistry, "NiCd", 4)) 715 val->intval = POWER_SUPPLY_TECHNOLOGY_NiCd; 716 else if (!strncasecmp(chemistry, "NiMH", 4)) 717 val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH; 718 else 719 val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN; 720 721 if (val->intval == POWER_SUPPLY_TECHNOLOGY_UNKNOWN) 722 dev_warn(&client->dev, "Unknown chemistry: %s\n", chemistry); 723 724 return 0; 725 } 726 727 static int sbs_get_battery_manufacture_date(struct i2c_client *client, 728 enum power_supply_property psp, 729 union power_supply_propval *val) 730 { 731 int ret; 732 u16 day, month, year; 733 734 ret = sbs_read_word_data(client, REG_ADDR_MANUFACTURE_DATE); 735 if (ret < 0) 736 return ret; 737 738 day = ret & GENMASK(4, 0); 739 month = (ret & GENMASK(8, 5)) >> 5; 740 year = ((ret & GENMASK(15, 9)) >> 9) + 1980; 741 742 switch (psp) { 743 case POWER_SUPPLY_PROP_MANUFACTURE_YEAR: 744 val->intval = year; 745 break; 746 case POWER_SUPPLY_PROP_MANUFACTURE_MONTH: 747 val->intval = month; 748 break; 749 case POWER_SUPPLY_PROP_MANUFACTURE_DAY: 750 val->intval = day; 751 break; 752 default: 753 return -EINVAL; 754 } 755 756 return 0; 757 } 758 759 static int sbs_get_property(struct power_supply *psy, 760 enum power_supply_property psp, 761 union power_supply_propval *val) 762 { 763 int ret = 0; 764 struct sbs_info *chip = power_supply_get_drvdata(psy); 765 struct i2c_client *client = chip->client; 766 767 if (chip->gpio_detect) { 768 ret = gpiod_get_value_cansleep(chip->gpio_detect); 769 if (ret < 0) 770 return ret; 771 if (psp == POWER_SUPPLY_PROP_PRESENT) { 772 val->intval = ret; 773 sbs_update_presence(chip, ret); 774 return 0; 775 } 776 if (ret == 0) 777 return -ENODATA; 778 } 779 780 switch (psp) { 781 case POWER_SUPPLY_PROP_PRESENT: 782 case POWER_SUPPLY_PROP_HEALTH: 783 if (chip->flags & SBS_FLAGS_TI_BQ20ZX5) 784 ret = sbs_get_ti_battery_presence_and_health(client, 785 psp, val); 786 else 787 ret = sbs_get_battery_presence_and_health(client, psp, 788 val); 789 790 /* this can only be true if no gpio is used */ 791 if (psp == POWER_SUPPLY_PROP_PRESENT) 792 return 0; 793 break; 794 795 case POWER_SUPPLY_PROP_TECHNOLOGY: 796 ret = sbs_get_chemistry(client, val); 797 if (ret < 0) 798 break; 799 800 goto done; /* don't trigger power_supply_changed()! */ 801 802 case POWER_SUPPLY_PROP_ENERGY_NOW: 803 case POWER_SUPPLY_PROP_ENERGY_FULL: 804 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: 805 case POWER_SUPPLY_PROP_CHARGE_NOW: 806 case POWER_SUPPLY_PROP_CHARGE_FULL: 807 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 808 ret = sbs_get_property_index(client, psp); 809 if (ret < 0) 810 break; 811 812 /* sbs_get_battery_capacity() will change the battery mode 813 * temporarily to read the requested attribute. Ensure we stay 814 * in the desired mode for the duration of the attribute read. 815 */ 816 mutex_lock(&chip->mode_lock); 817 ret = sbs_get_battery_capacity(client, ret, psp, val); 818 mutex_unlock(&chip->mode_lock); 819 break; 820 821 case POWER_SUPPLY_PROP_SERIAL_NUMBER: 822 ret = sbs_get_battery_serial_number(client, val); 823 break; 824 825 case POWER_SUPPLY_PROP_STATUS: 826 case POWER_SUPPLY_PROP_CAPACITY_LEVEL: 827 case POWER_SUPPLY_PROP_CYCLE_COUNT: 828 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 829 case POWER_SUPPLY_PROP_CURRENT_NOW: 830 case POWER_SUPPLY_PROP_CURRENT_AVG: 831 case POWER_SUPPLY_PROP_TEMP: 832 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: 833 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: 834 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: 835 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: 836 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: 837 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: 838 case POWER_SUPPLY_PROP_CAPACITY: 839 case POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN: 840 ret = sbs_get_property_index(client, psp); 841 if (ret < 0) 842 break; 843 844 ret = sbs_get_battery_property(client, ret, psp, val); 845 break; 846 847 case POWER_SUPPLY_PROP_MODEL_NAME: 848 ret = sbs_get_property_index(client, psp); 849 if (ret < 0) 850 break; 851 852 ret = sbs_get_battery_string_property(client, ret, psp, 853 model_name); 854 val->strval = model_name; 855 break; 856 857 case POWER_SUPPLY_PROP_MANUFACTURER: 858 ret = sbs_get_property_index(client, psp); 859 if (ret < 0) 860 break; 861 862 ret = sbs_get_battery_string_property(client, ret, psp, 863 manufacturer); 864 val->strval = manufacturer; 865 break; 866 867 case POWER_SUPPLY_PROP_MANUFACTURE_YEAR: 868 case POWER_SUPPLY_PROP_MANUFACTURE_MONTH: 869 case POWER_SUPPLY_PROP_MANUFACTURE_DAY: 870 ret = sbs_get_battery_manufacture_date(client, psp, val); 871 break; 872 873 default: 874 dev_err(&client->dev, 875 "%s: INVALID property\n", __func__); 876 return -EINVAL; 877 } 878 879 if (!chip->enable_detection) 880 goto done; 881 882 if (!chip->gpio_detect && 883 chip->is_present != (ret >= 0)) { 884 sbs_update_presence(chip, (ret >= 0)); 885 power_supply_changed(chip->power_supply); 886 } 887 888 done: 889 if (!ret) { 890 /* Convert units to match requirements for power supply class */ 891 sbs_unit_adjustment(client, psp, val); 892 } 893 894 dev_dbg(&client->dev, 895 "%s: property = %d, value = %x\n", __func__, psp, val->intval); 896 897 if (ret && chip->is_present) 898 return ret; 899 900 /* battery not present, so return NODATA for properties */ 901 if (ret) 902 return -ENODATA; 903 904 return 0; 905 } 906 907 static void sbs_supply_changed(struct sbs_info *chip) 908 { 909 struct power_supply *battery = chip->power_supply; 910 int ret; 911 912 ret = gpiod_get_value_cansleep(chip->gpio_detect); 913 if (ret < 0) 914 return; 915 sbs_update_presence(chip, ret); 916 power_supply_changed(battery); 917 } 918 919 static irqreturn_t sbs_irq(int irq, void *devid) 920 { 921 sbs_supply_changed(devid); 922 return IRQ_HANDLED; 923 } 924 925 static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot, 926 unsigned int data) 927 { 928 sbs_supply_changed(i2c_get_clientdata(client)); 929 } 930 931 static void sbs_external_power_changed(struct power_supply *psy) 932 { 933 struct sbs_info *chip = power_supply_get_drvdata(psy); 934 935 /* cancel outstanding work */ 936 cancel_delayed_work_sync(&chip->work); 937 938 schedule_delayed_work(&chip->work, HZ); 939 chip->poll_time = chip->poll_retry_count; 940 } 941 942 static void sbs_delayed_work(struct work_struct *work) 943 { 944 struct sbs_info *chip; 945 s32 ret; 946 947 chip = container_of(work, struct sbs_info, work.work); 948 949 ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr); 950 /* if the read failed, give up on this work */ 951 if (ret < 0) { 952 chip->poll_time = 0; 953 return; 954 } 955 956 if (ret & BATTERY_FULL_CHARGED) 957 ret = POWER_SUPPLY_STATUS_FULL; 958 else if (ret & BATTERY_DISCHARGING) 959 ret = POWER_SUPPLY_STATUS_DISCHARGING; 960 else 961 ret = POWER_SUPPLY_STATUS_CHARGING; 962 963 sbs_status_correct(chip->client, &ret); 964 965 if (chip->last_state != ret) { 966 chip->poll_time = 0; 967 power_supply_changed(chip->power_supply); 968 return; 969 } 970 if (chip->poll_time > 0) { 971 schedule_delayed_work(&chip->work, HZ); 972 chip->poll_time--; 973 return; 974 } 975 } 976 977 static const struct power_supply_desc sbs_default_desc = { 978 .type = POWER_SUPPLY_TYPE_BATTERY, 979 .properties = sbs_properties, 980 .num_properties = ARRAY_SIZE(sbs_properties), 981 .get_property = sbs_get_property, 982 .external_power_changed = sbs_external_power_changed, 983 }; 984 985 static int sbs_probe(struct i2c_client *client) 986 { 987 struct sbs_info *chip; 988 struct power_supply_desc *sbs_desc; 989 struct sbs_platform_data *pdata = client->dev.platform_data; 990 struct power_supply_config psy_cfg = {}; 991 int rc; 992 int irq; 993 994 sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc, 995 sizeof(*sbs_desc), GFP_KERNEL); 996 if (!sbs_desc) 997 return -ENOMEM; 998 999 sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s", 1000 dev_name(&client->dev)); 1001 if (!sbs_desc->name) 1002 return -ENOMEM; 1003 1004 chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL); 1005 if (!chip) 1006 return -ENOMEM; 1007 1008 chip->flags = (u32)(uintptr_t)device_get_match_data(&client->dev); 1009 chip->client = client; 1010 chip->enable_detection = false; 1011 psy_cfg.of_node = client->dev.of_node; 1012 psy_cfg.drv_data = chip; 1013 chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN; 1014 mutex_init(&chip->mode_lock); 1015 1016 /* use pdata if available, fall back to DT properties, 1017 * or hardcoded defaults if not 1018 */ 1019 rc = device_property_read_u32(&client->dev, "sbs,i2c-retry-count", 1020 &chip->i2c_retry_count); 1021 if (rc) 1022 chip->i2c_retry_count = 0; 1023 1024 rc = device_property_read_u32(&client->dev, "sbs,poll-retry-count", 1025 &chip->poll_retry_count); 1026 if (rc) 1027 chip->poll_retry_count = 0; 1028 1029 if (pdata) { 1030 chip->poll_retry_count = pdata->poll_retry_count; 1031 chip->i2c_retry_count = pdata->i2c_retry_count; 1032 } 1033 chip->i2c_retry_count = chip->i2c_retry_count + 1; 1034 1035 chip->charger_broadcasts = !device_property_read_bool(&client->dev, 1036 "sbs,disable-charger-broadcasts"); 1037 1038 chip->gpio_detect = devm_gpiod_get_optional(&client->dev, 1039 "sbs,battery-detect", GPIOD_IN); 1040 if (IS_ERR(chip->gpio_detect)) { 1041 dev_err(&client->dev, "Failed to get gpio: %ld\n", 1042 PTR_ERR(chip->gpio_detect)); 1043 return PTR_ERR(chip->gpio_detect); 1044 } 1045 1046 i2c_set_clientdata(client, chip); 1047 1048 if (!chip->gpio_detect) 1049 goto skip_gpio; 1050 1051 irq = gpiod_to_irq(chip->gpio_detect); 1052 if (irq <= 0) { 1053 dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq); 1054 goto skip_gpio; 1055 } 1056 1057 rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq, 1058 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 1059 dev_name(&client->dev), chip); 1060 if (rc) { 1061 dev_warn(&client->dev, "Failed to request irq: %d\n", rc); 1062 goto skip_gpio; 1063 } 1064 1065 skip_gpio: 1066 /* 1067 * Before we register, we might need to make sure we can actually talk 1068 * to the battery. 1069 */ 1070 if (!(force_load || chip->gpio_detect)) { 1071 rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr); 1072 1073 if (rc < 0) { 1074 dev_err(&client->dev, "%s: Failed to get device status\n", 1075 __func__); 1076 goto exit_psupply; 1077 } 1078 } 1079 1080 chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc, 1081 &psy_cfg); 1082 if (IS_ERR(chip->power_supply)) { 1083 dev_err(&client->dev, 1084 "%s: Failed to register power supply\n", __func__); 1085 rc = PTR_ERR(chip->power_supply); 1086 goto exit_psupply; 1087 } 1088 1089 dev_info(&client->dev, 1090 "%s: battery gas gauge device registered\n", client->name); 1091 1092 INIT_DELAYED_WORK(&chip->work, sbs_delayed_work); 1093 1094 chip->enable_detection = true; 1095 1096 return 0; 1097 1098 exit_psupply: 1099 return rc; 1100 } 1101 1102 static int sbs_remove(struct i2c_client *client) 1103 { 1104 struct sbs_info *chip = i2c_get_clientdata(client); 1105 1106 cancel_delayed_work_sync(&chip->work); 1107 1108 return 0; 1109 } 1110 1111 #if defined CONFIG_PM_SLEEP 1112 1113 static int sbs_suspend(struct device *dev) 1114 { 1115 struct i2c_client *client = to_i2c_client(dev); 1116 struct sbs_info *chip = i2c_get_clientdata(client); 1117 int ret; 1118 1119 if (chip->poll_time > 0) 1120 cancel_delayed_work_sync(&chip->work); 1121 1122 if (chip->flags & SBS_FLAGS_TI_BQ20ZX5) { 1123 /* Write to manufacturer access with sleep command. */ 1124 ret = sbs_write_word_data(client, 1125 sbs_data[REG_MANUFACTURER_DATA].addr, 1126 MANUFACTURER_ACCESS_SLEEP); 1127 if (chip->is_present && ret < 0) 1128 return ret; 1129 } 1130 1131 return 0; 1132 } 1133 1134 static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL); 1135 #define SBS_PM_OPS (&sbs_pm_ops) 1136 1137 #else 1138 #define SBS_PM_OPS NULL 1139 #endif 1140 1141 static const struct i2c_device_id sbs_id[] = { 1142 { "bq20z65", 0 }, 1143 { "bq20z75", 0 }, 1144 { "sbs-battery", 1 }, 1145 {} 1146 }; 1147 MODULE_DEVICE_TABLE(i2c, sbs_id); 1148 1149 static const struct of_device_id sbs_dt_ids[] = { 1150 { .compatible = "sbs,sbs-battery" }, 1151 { 1152 .compatible = "ti,bq20z65", 1153 .data = (void *)SBS_FLAGS_TI_BQ20ZX5, 1154 }, 1155 { 1156 .compatible = "ti,bq20z75", 1157 .data = (void *)SBS_FLAGS_TI_BQ20ZX5, 1158 }, 1159 { } 1160 }; 1161 MODULE_DEVICE_TABLE(of, sbs_dt_ids); 1162 1163 static struct i2c_driver sbs_battery_driver = { 1164 .probe_new = sbs_probe, 1165 .remove = sbs_remove, 1166 .alert = sbs_alert, 1167 .id_table = sbs_id, 1168 .driver = { 1169 .name = "sbs-battery", 1170 .of_match_table = sbs_dt_ids, 1171 .pm = SBS_PM_OPS, 1172 }, 1173 }; 1174 module_i2c_driver(sbs_battery_driver); 1175 1176 MODULE_DESCRIPTION("SBS battery monitor driver"); 1177 MODULE_LICENSE("GPL"); 1178 1179 module_param(force_load, bool, 0444); 1180 MODULE_PARM_DESC(force_load, 1181 "Attempt to load the driver even if no battery is connected"); 1182