1 /* 2 * Gas Gauge driver for SBS Compliant Batteries 3 * 4 * Copyright (c) 2010, NVIDIA Corporation. 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 * more details. 15 * 16 * You should have received a copy of the GNU General Public License along 17 * with this program; if not, write to the Free Software Foundation, Inc., 18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 */ 20 21 #include <linux/init.h> 22 #include <linux/module.h> 23 #include <linux/kernel.h> 24 #include <linux/err.h> 25 #include <linux/power_supply.h> 26 #include <linux/i2c.h> 27 #include <linux/slab.h> 28 #include <linux/interrupt.h> 29 #include <linux/gpio/consumer.h> 30 #include <linux/of.h> 31 #include <linux/stat.h> 32 33 #include <linux/power/sbs-battery.h> 34 35 enum { 36 REG_MANUFACTURER_DATA, 37 REG_TEMPERATURE, 38 REG_VOLTAGE, 39 REG_CURRENT, 40 REG_CAPACITY, 41 REG_TIME_TO_EMPTY, 42 REG_TIME_TO_FULL, 43 REG_STATUS, 44 REG_CAPACITY_LEVEL, 45 REG_CYCLE_COUNT, 46 REG_SERIAL_NUMBER, 47 REG_REMAINING_CAPACITY, 48 REG_REMAINING_CAPACITY_CHARGE, 49 REG_FULL_CHARGE_CAPACITY, 50 REG_FULL_CHARGE_CAPACITY_CHARGE, 51 REG_DESIGN_CAPACITY, 52 REG_DESIGN_CAPACITY_CHARGE, 53 REG_DESIGN_VOLTAGE_MIN, 54 REG_DESIGN_VOLTAGE_MAX, 55 REG_MANUFACTURER, 56 REG_MODEL_NAME, 57 }; 58 59 /* Battery Mode defines */ 60 #define BATTERY_MODE_OFFSET 0x03 61 #define BATTERY_MODE_MASK 0x8000 62 enum sbs_battery_mode { 63 BATTERY_MODE_AMPS, 64 BATTERY_MODE_WATTS 65 }; 66 67 /* manufacturer access defines */ 68 #define MANUFACTURER_ACCESS_STATUS 0x0006 69 #define MANUFACTURER_ACCESS_SLEEP 0x0011 70 71 /* battery status value bits */ 72 #define BATTERY_INITIALIZED 0x80 73 #define BATTERY_DISCHARGING 0x40 74 #define BATTERY_FULL_CHARGED 0x20 75 #define BATTERY_FULL_DISCHARGED 0x10 76 77 /* min_value and max_value are only valid for numerical data */ 78 #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \ 79 .psp = _psp, \ 80 .addr = _addr, \ 81 .min_value = _min_value, \ 82 .max_value = _max_value, \ 83 } 84 85 static const struct chip_data { 86 enum power_supply_property psp; 87 u8 addr; 88 int min_value; 89 int max_value; 90 } sbs_data[] = { 91 [REG_MANUFACTURER_DATA] = 92 SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 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] = 98 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767), 99 [REG_CAPACITY] = 100 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100), 101 [REG_REMAINING_CAPACITY] = 102 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535), 103 [REG_REMAINING_CAPACITY_CHARGE] = 104 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535), 105 [REG_FULL_CHARGE_CAPACITY] = 106 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535), 107 [REG_FULL_CHARGE_CAPACITY_CHARGE] = 108 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535), 109 [REG_TIME_TO_EMPTY] = 110 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535), 111 [REG_TIME_TO_FULL] = 112 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535), 113 [REG_STATUS] = 114 SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535), 115 [REG_CAPACITY_LEVEL] = 116 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535), 117 [REG_CYCLE_COUNT] = 118 SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535), 119 [REG_DESIGN_CAPACITY] = 120 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535), 121 [REG_DESIGN_CAPACITY_CHARGE] = 122 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535), 123 [REG_DESIGN_VOLTAGE_MIN] = 124 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535), 125 [REG_DESIGN_VOLTAGE_MAX] = 126 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535), 127 [REG_SERIAL_NUMBER] = 128 SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535), 129 /* Properties of type `const char *' */ 130 [REG_MANUFACTURER] = 131 SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535), 132 [REG_MODEL_NAME] = 133 SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535) 134 }; 135 136 static enum power_supply_property sbs_properties[] = { 137 POWER_SUPPLY_PROP_STATUS, 138 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 139 POWER_SUPPLY_PROP_HEALTH, 140 POWER_SUPPLY_PROP_PRESENT, 141 POWER_SUPPLY_PROP_TECHNOLOGY, 142 POWER_SUPPLY_PROP_CYCLE_COUNT, 143 POWER_SUPPLY_PROP_VOLTAGE_NOW, 144 POWER_SUPPLY_PROP_CURRENT_NOW, 145 POWER_SUPPLY_PROP_CAPACITY, 146 POWER_SUPPLY_PROP_TEMP, 147 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 148 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 149 POWER_SUPPLY_PROP_SERIAL_NUMBER, 150 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 151 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 152 POWER_SUPPLY_PROP_ENERGY_NOW, 153 POWER_SUPPLY_PROP_ENERGY_FULL, 154 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 155 POWER_SUPPLY_PROP_CHARGE_NOW, 156 POWER_SUPPLY_PROP_CHARGE_FULL, 157 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 158 /* Properties of type `const char *' */ 159 POWER_SUPPLY_PROP_MANUFACTURER, 160 POWER_SUPPLY_PROP_MODEL_NAME 161 }; 162 163 struct sbs_info { 164 struct i2c_client *client; 165 struct power_supply *power_supply; 166 bool is_present; 167 struct gpio_desc *gpio_detect; 168 bool enable_detection; 169 int last_state; 170 int poll_time; 171 u32 i2c_retry_count; 172 u32 poll_retry_count; 173 struct delayed_work work; 174 }; 175 176 static char model_name[I2C_SMBUS_BLOCK_MAX + 1]; 177 static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1]; 178 static bool force_load; 179 180 static int sbs_read_word_data(struct i2c_client *client, u8 address) 181 { 182 struct sbs_info *chip = i2c_get_clientdata(client); 183 s32 ret = 0; 184 int retries = 1; 185 186 retries = chip->i2c_retry_count; 187 188 while (retries > 0) { 189 ret = i2c_smbus_read_word_data(client, address); 190 if (ret >= 0) 191 break; 192 retries--; 193 } 194 195 if (ret < 0) { 196 dev_dbg(&client->dev, 197 "%s: i2c read at address 0x%x failed\n", 198 __func__, address); 199 return ret; 200 } 201 202 return le16_to_cpu(ret); 203 } 204 205 static int sbs_read_string_data(struct i2c_client *client, u8 address, 206 char *values) 207 { 208 struct sbs_info *chip = i2c_get_clientdata(client); 209 s32 ret = 0, block_length = 0; 210 int retries_length = 1, retries_block = 1; 211 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1]; 212 213 retries_length = chip->i2c_retry_count; 214 retries_block = chip->i2c_retry_count; 215 216 /* Adapter needs to support these two functions */ 217 if (!i2c_check_functionality(client->adapter, 218 I2C_FUNC_SMBUS_BYTE_DATA | 219 I2C_FUNC_SMBUS_I2C_BLOCK)){ 220 return -ENODEV; 221 } 222 223 /* Get the length of block data */ 224 while (retries_length > 0) { 225 ret = i2c_smbus_read_byte_data(client, address); 226 if (ret >= 0) 227 break; 228 retries_length--; 229 } 230 231 if (ret < 0) { 232 dev_dbg(&client->dev, 233 "%s: i2c read at address 0x%x failed\n", 234 __func__, address); 235 return ret; 236 } 237 238 /* block_length does not include NULL terminator */ 239 block_length = ret; 240 if (block_length > I2C_SMBUS_BLOCK_MAX) { 241 dev_err(&client->dev, 242 "%s: Returned block_length is longer than 0x%x\n", 243 __func__, I2C_SMBUS_BLOCK_MAX); 244 return -EINVAL; 245 } 246 247 /* Get the block data */ 248 while (retries_block > 0) { 249 ret = i2c_smbus_read_i2c_block_data( 250 client, address, 251 block_length + 1, block_buffer); 252 if (ret >= 0) 253 break; 254 retries_block--; 255 } 256 257 if (ret < 0) { 258 dev_dbg(&client->dev, 259 "%s: i2c read at address 0x%x failed\n", 260 __func__, address); 261 return ret; 262 } 263 264 /* block_buffer[0] == block_length */ 265 memcpy(values, block_buffer + 1, block_length); 266 values[block_length] = '\0'; 267 268 return le16_to_cpu(ret); 269 } 270 271 static int sbs_write_word_data(struct i2c_client *client, u8 address, 272 u16 value) 273 { 274 struct sbs_info *chip = i2c_get_clientdata(client); 275 s32 ret = 0; 276 int retries = 1; 277 278 retries = chip->i2c_retry_count; 279 280 while (retries > 0) { 281 ret = i2c_smbus_write_word_data(client, address, 282 le16_to_cpu(value)); 283 if (ret >= 0) 284 break; 285 retries--; 286 } 287 288 if (ret < 0) { 289 dev_dbg(&client->dev, 290 "%s: i2c write to address 0x%x failed\n", 291 __func__, address); 292 return ret; 293 } 294 295 return 0; 296 } 297 298 static int sbs_status_correct(struct i2c_client *client, int *intval) 299 { 300 int ret; 301 302 ret = sbs_read_word_data(client, sbs_data[REG_CURRENT].addr); 303 if (ret < 0) 304 return ret; 305 306 ret = (s16)ret; 307 308 /* Not drawing current means full (cannot be not charging) */ 309 if (ret == 0) 310 *intval = POWER_SUPPLY_STATUS_FULL; 311 312 if (*intval == POWER_SUPPLY_STATUS_FULL) { 313 /* Drawing or providing current when full */ 314 if (ret > 0) 315 *intval = POWER_SUPPLY_STATUS_CHARGING; 316 else if (ret < 0) 317 *intval = POWER_SUPPLY_STATUS_DISCHARGING; 318 } 319 320 return 0; 321 } 322 323 static int sbs_get_battery_presence_and_health( 324 struct i2c_client *client, enum power_supply_property psp, 325 union power_supply_propval *val) 326 { 327 s32 ret; 328 struct sbs_info *chip = i2c_get_clientdata(client); 329 330 if (psp == POWER_SUPPLY_PROP_PRESENT && chip->gpio_detect) { 331 ret = gpiod_get_value_cansleep(chip->gpio_detect); 332 if (ret < 0) 333 return ret; 334 val->intval = ret; 335 chip->is_present = val->intval; 336 return ret; 337 } 338 339 /* 340 * Write to ManufacturerAccess with ManufacturerAccess command 341 * and then read the status. Do not check for error on the write 342 * since not all batteries implement write access to this command, 343 * while others mandate it. 344 */ 345 sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr, 346 MANUFACTURER_ACCESS_STATUS); 347 348 ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr); 349 if (ret < 0) { 350 if (psp == POWER_SUPPLY_PROP_PRESENT) 351 val->intval = 0; /* battery removed */ 352 return ret; 353 } 354 355 if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value || 356 ret > sbs_data[REG_MANUFACTURER_DATA].max_value) { 357 val->intval = 0; 358 return 0; 359 } 360 361 /* Mask the upper nibble of 2nd byte and 362 * lower byte of response then 363 * shift the result by 8 to get status*/ 364 ret &= 0x0F00; 365 ret >>= 8; 366 if (psp == POWER_SUPPLY_PROP_PRESENT) { 367 if (ret == 0x0F) 368 /* battery removed */ 369 val->intval = 0; 370 else 371 val->intval = 1; 372 } else if (psp == POWER_SUPPLY_PROP_HEALTH) { 373 if (ret == 0x09) 374 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 375 else if (ret == 0x0B) 376 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; 377 else if (ret == 0x0C) 378 val->intval = POWER_SUPPLY_HEALTH_DEAD; 379 else 380 val->intval = POWER_SUPPLY_HEALTH_GOOD; 381 } 382 383 return 0; 384 } 385 386 static int sbs_get_battery_property(struct i2c_client *client, 387 int reg_offset, enum power_supply_property psp, 388 union power_supply_propval *val) 389 { 390 struct sbs_info *chip = i2c_get_clientdata(client); 391 s32 ret; 392 393 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr); 394 if (ret < 0) 395 return ret; 396 397 /* returned values are 16 bit */ 398 if (sbs_data[reg_offset].min_value < 0) 399 ret = (s16)ret; 400 401 if (ret >= sbs_data[reg_offset].min_value && 402 ret <= sbs_data[reg_offset].max_value) { 403 val->intval = ret; 404 if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) { 405 if (!(ret & BATTERY_INITIALIZED)) 406 val->intval = 407 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; 408 else if (ret & BATTERY_FULL_CHARGED) 409 val->intval = 410 POWER_SUPPLY_CAPACITY_LEVEL_FULL; 411 else if (ret & BATTERY_FULL_DISCHARGED) 412 val->intval = 413 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; 414 else 415 val->intval = 416 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; 417 return 0; 418 } else if (psp != POWER_SUPPLY_PROP_STATUS) { 419 return 0; 420 } 421 422 if (ret & BATTERY_FULL_CHARGED) 423 val->intval = POWER_SUPPLY_STATUS_FULL; 424 else if (ret & BATTERY_DISCHARGING) 425 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 426 else 427 val->intval = POWER_SUPPLY_STATUS_CHARGING; 428 429 sbs_status_correct(client, &val->intval); 430 431 if (chip->poll_time == 0) 432 chip->last_state = val->intval; 433 else if (chip->last_state != val->intval) { 434 cancel_delayed_work_sync(&chip->work); 435 power_supply_changed(chip->power_supply); 436 chip->poll_time = 0; 437 } 438 } else { 439 if (psp == POWER_SUPPLY_PROP_STATUS) 440 val->intval = POWER_SUPPLY_STATUS_UNKNOWN; 441 else 442 val->intval = 0; 443 } 444 445 return 0; 446 } 447 448 static int sbs_get_battery_string_property(struct i2c_client *client, 449 int reg_offset, enum power_supply_property psp, char *val) 450 { 451 s32 ret; 452 453 ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val); 454 455 if (ret < 0) 456 return ret; 457 458 return 0; 459 } 460 461 static void sbs_unit_adjustment(struct i2c_client *client, 462 enum power_supply_property psp, union power_supply_propval *val) 463 { 464 #define BASE_UNIT_CONVERSION 1000 465 #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION) 466 #define TIME_UNIT_CONVERSION 60 467 #define TEMP_KELVIN_TO_CELSIUS 2731 468 switch (psp) { 469 case POWER_SUPPLY_PROP_ENERGY_NOW: 470 case POWER_SUPPLY_PROP_ENERGY_FULL: 471 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: 472 /* sbs provides energy in units of 10mWh. 473 * Convert to µWh 474 */ 475 val->intval *= BATTERY_MODE_CAP_MULT_WATT; 476 break; 477 478 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 479 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: 480 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: 481 case POWER_SUPPLY_PROP_CURRENT_NOW: 482 case POWER_SUPPLY_PROP_CHARGE_NOW: 483 case POWER_SUPPLY_PROP_CHARGE_FULL: 484 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 485 val->intval *= BASE_UNIT_CONVERSION; 486 break; 487 488 case POWER_SUPPLY_PROP_TEMP: 489 /* sbs provides battery temperature in 0.1K 490 * so convert it to 0.1°C 491 */ 492 val->intval -= TEMP_KELVIN_TO_CELSIUS; 493 break; 494 495 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: 496 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: 497 /* sbs provides time to empty and time to full in minutes. 498 * Convert to seconds 499 */ 500 val->intval *= TIME_UNIT_CONVERSION; 501 break; 502 503 default: 504 dev_dbg(&client->dev, 505 "%s: no need for unit conversion %d\n", __func__, psp); 506 } 507 } 508 509 static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client, 510 enum sbs_battery_mode mode) 511 { 512 int ret, original_val; 513 514 original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET); 515 if (original_val < 0) 516 return original_val; 517 518 if ((original_val & BATTERY_MODE_MASK) == mode) 519 return mode; 520 521 if (mode == BATTERY_MODE_AMPS) 522 ret = original_val & ~BATTERY_MODE_MASK; 523 else 524 ret = original_val | BATTERY_MODE_MASK; 525 526 ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret); 527 if (ret < 0) 528 return ret; 529 530 return original_val & BATTERY_MODE_MASK; 531 } 532 533 static int sbs_get_battery_capacity(struct i2c_client *client, 534 int reg_offset, enum power_supply_property psp, 535 union power_supply_propval *val) 536 { 537 s32 ret; 538 enum sbs_battery_mode mode = BATTERY_MODE_WATTS; 539 540 if (power_supply_is_amp_property(psp)) 541 mode = BATTERY_MODE_AMPS; 542 543 mode = sbs_set_battery_mode(client, mode); 544 if (mode < 0) 545 return mode; 546 547 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr); 548 if (ret < 0) 549 return ret; 550 551 if (psp == POWER_SUPPLY_PROP_CAPACITY) { 552 /* sbs spec says that this can be >100 % 553 * even if max value is 100 % */ 554 val->intval = min(ret, 100); 555 } else 556 val->intval = ret; 557 558 ret = sbs_set_battery_mode(client, mode); 559 if (ret < 0) 560 return ret; 561 562 return 0; 563 } 564 565 static char sbs_serial[5]; 566 static int sbs_get_battery_serial_number(struct i2c_client *client, 567 union power_supply_propval *val) 568 { 569 int ret; 570 571 ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr); 572 if (ret < 0) 573 return ret; 574 575 ret = sprintf(sbs_serial, "%04x", ret); 576 val->strval = sbs_serial; 577 578 return 0; 579 } 580 581 static int sbs_get_property_index(struct i2c_client *client, 582 enum power_supply_property psp) 583 { 584 int count; 585 for (count = 0; count < ARRAY_SIZE(sbs_data); count++) 586 if (psp == sbs_data[count].psp) 587 return count; 588 589 dev_warn(&client->dev, 590 "%s: Invalid Property - %d\n", __func__, psp); 591 592 return -EINVAL; 593 } 594 595 static int sbs_get_property(struct power_supply *psy, 596 enum power_supply_property psp, 597 union power_supply_propval *val) 598 { 599 int ret = 0; 600 struct sbs_info *chip = power_supply_get_drvdata(psy); 601 struct i2c_client *client = chip->client; 602 603 switch (psp) { 604 case POWER_SUPPLY_PROP_PRESENT: 605 case POWER_SUPPLY_PROP_HEALTH: 606 ret = sbs_get_battery_presence_and_health(client, psp, val); 607 if (psp == POWER_SUPPLY_PROP_PRESENT) 608 return 0; 609 break; 610 611 case POWER_SUPPLY_PROP_TECHNOLOGY: 612 val->intval = POWER_SUPPLY_TECHNOLOGY_LION; 613 goto done; /* don't trigger power_supply_changed()! */ 614 615 case POWER_SUPPLY_PROP_ENERGY_NOW: 616 case POWER_SUPPLY_PROP_ENERGY_FULL: 617 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: 618 case POWER_SUPPLY_PROP_CHARGE_NOW: 619 case POWER_SUPPLY_PROP_CHARGE_FULL: 620 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 621 case POWER_SUPPLY_PROP_CAPACITY: 622 ret = sbs_get_property_index(client, psp); 623 if (ret < 0) 624 break; 625 626 ret = sbs_get_battery_capacity(client, ret, psp, val); 627 break; 628 629 case POWER_SUPPLY_PROP_SERIAL_NUMBER: 630 ret = sbs_get_battery_serial_number(client, val); 631 break; 632 633 case POWER_SUPPLY_PROP_STATUS: 634 case POWER_SUPPLY_PROP_CAPACITY_LEVEL: 635 case POWER_SUPPLY_PROP_CYCLE_COUNT: 636 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 637 case POWER_SUPPLY_PROP_CURRENT_NOW: 638 case POWER_SUPPLY_PROP_TEMP: 639 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: 640 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: 641 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: 642 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: 643 ret = sbs_get_property_index(client, psp); 644 if (ret < 0) 645 break; 646 647 ret = sbs_get_battery_property(client, ret, psp, val); 648 break; 649 650 case POWER_SUPPLY_PROP_MODEL_NAME: 651 ret = sbs_get_property_index(client, psp); 652 if (ret < 0) 653 break; 654 655 ret = sbs_get_battery_string_property(client, ret, psp, 656 model_name); 657 val->strval = model_name; 658 break; 659 660 case POWER_SUPPLY_PROP_MANUFACTURER: 661 ret = sbs_get_property_index(client, psp); 662 if (ret < 0) 663 break; 664 665 ret = sbs_get_battery_string_property(client, ret, psp, 666 manufacturer); 667 val->strval = manufacturer; 668 break; 669 670 default: 671 dev_err(&client->dev, 672 "%s: INVALID property\n", __func__); 673 return -EINVAL; 674 } 675 676 if (!chip->enable_detection) 677 goto done; 678 679 if (!chip->gpio_detect && 680 chip->is_present != (ret >= 0)) { 681 chip->is_present = (ret >= 0); 682 power_supply_changed(chip->power_supply); 683 } 684 685 done: 686 if (!ret) { 687 /* Convert units to match requirements for power supply class */ 688 sbs_unit_adjustment(client, psp, val); 689 } 690 691 dev_dbg(&client->dev, 692 "%s: property = %d, value = %x\n", __func__, psp, val->intval); 693 694 if (ret && chip->is_present) 695 return ret; 696 697 /* battery not present, so return NODATA for properties */ 698 if (ret) 699 return -ENODATA; 700 701 return 0; 702 } 703 704 static void sbs_supply_changed(struct sbs_info *chip) 705 { 706 struct power_supply *battery = chip->power_supply; 707 int ret; 708 709 ret = gpiod_get_value_cansleep(chip->gpio_detect); 710 if (ret < 0) 711 return; 712 chip->is_present = ret; 713 power_supply_changed(battery); 714 } 715 716 static irqreturn_t sbs_irq(int irq, void *devid) 717 { 718 sbs_supply_changed(devid); 719 return IRQ_HANDLED; 720 } 721 722 static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot, 723 unsigned int data) 724 { 725 sbs_supply_changed(i2c_get_clientdata(client)); 726 } 727 728 static void sbs_external_power_changed(struct power_supply *psy) 729 { 730 struct sbs_info *chip = power_supply_get_drvdata(psy); 731 732 /* cancel outstanding work */ 733 cancel_delayed_work_sync(&chip->work); 734 735 schedule_delayed_work(&chip->work, HZ); 736 chip->poll_time = chip->poll_retry_count; 737 } 738 739 static void sbs_delayed_work(struct work_struct *work) 740 { 741 struct sbs_info *chip; 742 s32 ret; 743 744 chip = container_of(work, struct sbs_info, work.work); 745 746 ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr); 747 /* if the read failed, give up on this work */ 748 if (ret < 0) { 749 chip->poll_time = 0; 750 return; 751 } 752 753 if (ret & BATTERY_FULL_CHARGED) 754 ret = POWER_SUPPLY_STATUS_FULL; 755 else if (ret & BATTERY_DISCHARGING) 756 ret = POWER_SUPPLY_STATUS_DISCHARGING; 757 else 758 ret = POWER_SUPPLY_STATUS_CHARGING; 759 760 sbs_status_correct(chip->client, &ret); 761 762 if (chip->last_state != ret) { 763 chip->poll_time = 0; 764 power_supply_changed(chip->power_supply); 765 return; 766 } 767 if (chip->poll_time > 0) { 768 schedule_delayed_work(&chip->work, HZ); 769 chip->poll_time--; 770 return; 771 } 772 } 773 774 static const struct power_supply_desc sbs_default_desc = { 775 .type = POWER_SUPPLY_TYPE_BATTERY, 776 .properties = sbs_properties, 777 .num_properties = ARRAY_SIZE(sbs_properties), 778 .get_property = sbs_get_property, 779 .external_power_changed = sbs_external_power_changed, 780 }; 781 782 static int sbs_probe(struct i2c_client *client, 783 const struct i2c_device_id *id) 784 { 785 struct sbs_info *chip; 786 struct power_supply_desc *sbs_desc; 787 struct sbs_platform_data *pdata = client->dev.platform_data; 788 struct power_supply_config psy_cfg = {}; 789 int rc; 790 int irq; 791 792 sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc, 793 sizeof(*sbs_desc), GFP_KERNEL); 794 if (!sbs_desc) 795 return -ENOMEM; 796 797 sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s", 798 dev_name(&client->dev)); 799 if (!sbs_desc->name) 800 return -ENOMEM; 801 802 chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL); 803 if (!chip) 804 return -ENOMEM; 805 806 chip->client = client; 807 chip->enable_detection = false; 808 psy_cfg.of_node = client->dev.of_node; 809 psy_cfg.drv_data = chip; 810 chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN; 811 812 /* use pdata if available, fall back to DT properties, 813 * or hardcoded defaults if not 814 */ 815 rc = of_property_read_u32(client->dev.of_node, "sbs,i2c-retry-count", 816 &chip->i2c_retry_count); 817 if (rc) 818 chip->i2c_retry_count = 0; 819 820 rc = of_property_read_u32(client->dev.of_node, "sbs,poll-retry-count", 821 &chip->poll_retry_count); 822 if (rc) 823 chip->poll_retry_count = 0; 824 825 if (pdata) { 826 chip->poll_retry_count = pdata->poll_retry_count; 827 chip->i2c_retry_count = pdata->i2c_retry_count; 828 } 829 chip->i2c_retry_count = chip->i2c_retry_count + 1; 830 831 chip->gpio_detect = devm_gpiod_get_optional(&client->dev, 832 "sbs,battery-detect", GPIOD_IN); 833 if (IS_ERR(chip->gpio_detect)) { 834 dev_err(&client->dev, "Failed to get gpio: %ld\n", 835 PTR_ERR(chip->gpio_detect)); 836 return PTR_ERR(chip->gpio_detect); 837 } 838 839 i2c_set_clientdata(client, chip); 840 841 if (!chip->gpio_detect) 842 goto skip_gpio; 843 844 irq = gpiod_to_irq(chip->gpio_detect); 845 if (irq <= 0) { 846 dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq); 847 goto skip_gpio; 848 } 849 850 rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq, 851 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 852 dev_name(&client->dev), chip); 853 if (rc) { 854 dev_warn(&client->dev, "Failed to request irq: %d\n", rc); 855 goto skip_gpio; 856 } 857 858 skip_gpio: 859 /* 860 * Before we register, we might need to make sure we can actually talk 861 * to the battery. 862 */ 863 if (!(force_load || chip->gpio_detect)) { 864 rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr); 865 866 if (rc < 0) { 867 dev_err(&client->dev, "%s: Failed to get device status\n", 868 __func__); 869 goto exit_psupply; 870 } 871 } 872 873 chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc, 874 &psy_cfg); 875 if (IS_ERR(chip->power_supply)) { 876 dev_err(&client->dev, 877 "%s: Failed to register power supply\n", __func__); 878 rc = PTR_ERR(chip->power_supply); 879 goto exit_psupply; 880 } 881 882 dev_info(&client->dev, 883 "%s: battery gas gauge device registered\n", client->name); 884 885 INIT_DELAYED_WORK(&chip->work, sbs_delayed_work); 886 887 chip->enable_detection = true; 888 889 return 0; 890 891 exit_psupply: 892 return rc; 893 } 894 895 static int sbs_remove(struct i2c_client *client) 896 { 897 struct sbs_info *chip = i2c_get_clientdata(client); 898 899 cancel_delayed_work_sync(&chip->work); 900 901 return 0; 902 } 903 904 #if defined CONFIG_PM_SLEEP 905 906 static int sbs_suspend(struct device *dev) 907 { 908 struct i2c_client *client = to_i2c_client(dev); 909 struct sbs_info *chip = i2c_get_clientdata(client); 910 911 if (chip->poll_time > 0) 912 cancel_delayed_work_sync(&chip->work); 913 914 /* 915 * Write to manufacturer access with sleep command. 916 * Support is manufacturer dependend, so ignore errors. 917 */ 918 sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr, 919 MANUFACTURER_ACCESS_SLEEP); 920 921 return 0; 922 } 923 924 static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL); 925 #define SBS_PM_OPS (&sbs_pm_ops) 926 927 #else 928 #define SBS_PM_OPS NULL 929 #endif 930 931 static const struct i2c_device_id sbs_id[] = { 932 { "bq20z75", 0 }, 933 { "sbs-battery", 1 }, 934 {} 935 }; 936 MODULE_DEVICE_TABLE(i2c, sbs_id); 937 938 static const struct of_device_id sbs_dt_ids[] = { 939 { .compatible = "sbs,sbs-battery" }, 940 { .compatible = "ti,bq20z75" }, 941 { } 942 }; 943 MODULE_DEVICE_TABLE(of, sbs_dt_ids); 944 945 static struct i2c_driver sbs_battery_driver = { 946 .probe = sbs_probe, 947 .remove = sbs_remove, 948 .alert = sbs_alert, 949 .id_table = sbs_id, 950 .driver = { 951 .name = "sbs-battery", 952 .of_match_table = sbs_dt_ids, 953 .pm = SBS_PM_OPS, 954 }, 955 }; 956 module_i2c_driver(sbs_battery_driver); 957 958 MODULE_DESCRIPTION("SBS battery monitor driver"); 959 MODULE_LICENSE("GPL"); 960 961 module_param(force_load, bool, S_IRUSR | S_IRGRP | S_IROTH); 962 MODULE_PARM_DESC(force_load, 963 "Attempt to load the driver even if no battery is connected"); 964