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