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 struct mutex mode_lock; 175 }; 176 177 static char model_name[I2C_SMBUS_BLOCK_MAX + 1]; 178 static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1]; 179 static bool force_load; 180 181 static int sbs_read_word_data(struct i2c_client *client, u8 address) 182 { 183 struct sbs_info *chip = i2c_get_clientdata(client); 184 s32 ret = 0; 185 int retries = 1; 186 187 retries = chip->i2c_retry_count; 188 189 while (retries > 0) { 190 ret = i2c_smbus_read_word_data(client, address); 191 if (ret >= 0) 192 break; 193 retries--; 194 } 195 196 if (ret < 0) { 197 dev_dbg(&client->dev, 198 "%s: i2c read at address 0x%x failed\n", 199 __func__, address); 200 return ret; 201 } 202 203 return ret; 204 } 205 206 static int sbs_read_string_data(struct i2c_client *client, u8 address, 207 char *values) 208 { 209 struct sbs_info *chip = i2c_get_clientdata(client); 210 s32 ret = 0, block_length = 0; 211 int retries_length = 1, retries_block = 1; 212 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1]; 213 214 retries_length = chip->i2c_retry_count; 215 retries_block = chip->i2c_retry_count; 216 217 /* Adapter needs to support these two functions */ 218 if (!i2c_check_functionality(client->adapter, 219 I2C_FUNC_SMBUS_BYTE_DATA | 220 I2C_FUNC_SMBUS_I2C_BLOCK)){ 221 return -ENODEV; 222 } 223 224 /* Get the length of block data */ 225 while (retries_length > 0) { 226 ret = i2c_smbus_read_byte_data(client, address); 227 if (ret >= 0) 228 break; 229 retries_length--; 230 } 231 232 if (ret < 0) { 233 dev_dbg(&client->dev, 234 "%s: i2c read at address 0x%x failed\n", 235 __func__, address); 236 return ret; 237 } 238 239 /* block_length does not include NULL terminator */ 240 block_length = ret; 241 if (block_length > I2C_SMBUS_BLOCK_MAX) { 242 dev_err(&client->dev, 243 "%s: Returned block_length is longer than 0x%x\n", 244 __func__, I2C_SMBUS_BLOCK_MAX); 245 return -EINVAL; 246 } 247 248 /* Get the block data */ 249 while (retries_block > 0) { 250 ret = i2c_smbus_read_i2c_block_data( 251 client, address, 252 block_length + 1, block_buffer); 253 if (ret >= 0) 254 break; 255 retries_block--; 256 } 257 258 if (ret < 0) { 259 dev_dbg(&client->dev, 260 "%s: i2c read at address 0x%x failed\n", 261 __func__, address); 262 return ret; 263 } 264 265 /* block_buffer[0] == block_length */ 266 memcpy(values, block_buffer + 1, block_length); 267 values[block_length] = '\0'; 268 269 return ret; 270 } 271 272 static int sbs_write_word_data(struct i2c_client *client, u8 address, 273 u16 value) 274 { 275 struct sbs_info *chip = i2c_get_clientdata(client); 276 s32 ret = 0; 277 int retries = 1; 278 279 retries = chip->i2c_retry_count; 280 281 while (retries > 0) { 282 ret = i2c_smbus_write_word_data(client, address, 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 if (psp == POWER_SUPPLY_PROP_CAPACITY) 442 /* sbs spec says that this can be >100 % 443 * even if max value is 100 % 444 */ 445 val->intval = min(ret, 100); 446 else 447 val->intval = 0; 448 } 449 450 return 0; 451 } 452 453 static int sbs_get_battery_string_property(struct i2c_client *client, 454 int reg_offset, enum power_supply_property psp, char *val) 455 { 456 s32 ret; 457 458 ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val); 459 460 if (ret < 0) 461 return ret; 462 463 return 0; 464 } 465 466 static void sbs_unit_adjustment(struct i2c_client *client, 467 enum power_supply_property psp, union power_supply_propval *val) 468 { 469 #define BASE_UNIT_CONVERSION 1000 470 #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION) 471 #define TIME_UNIT_CONVERSION 60 472 #define TEMP_KELVIN_TO_CELSIUS 2731 473 switch (psp) { 474 case POWER_SUPPLY_PROP_ENERGY_NOW: 475 case POWER_SUPPLY_PROP_ENERGY_FULL: 476 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: 477 /* sbs provides energy in units of 10mWh. 478 * Convert to µWh 479 */ 480 val->intval *= BATTERY_MODE_CAP_MULT_WATT; 481 break; 482 483 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 484 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: 485 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: 486 case POWER_SUPPLY_PROP_CURRENT_NOW: 487 case POWER_SUPPLY_PROP_CHARGE_NOW: 488 case POWER_SUPPLY_PROP_CHARGE_FULL: 489 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 490 val->intval *= BASE_UNIT_CONVERSION; 491 break; 492 493 case POWER_SUPPLY_PROP_TEMP: 494 /* sbs provides battery temperature in 0.1K 495 * so convert it to 0.1°C 496 */ 497 val->intval -= TEMP_KELVIN_TO_CELSIUS; 498 break; 499 500 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: 501 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: 502 /* sbs provides time to empty and time to full in minutes. 503 * Convert to seconds 504 */ 505 val->intval *= TIME_UNIT_CONVERSION; 506 break; 507 508 default: 509 dev_dbg(&client->dev, 510 "%s: no need for unit conversion %d\n", __func__, psp); 511 } 512 } 513 514 static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client, 515 enum sbs_battery_mode mode) 516 { 517 int ret, original_val; 518 519 original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET); 520 if (original_val < 0) 521 return original_val; 522 523 if ((original_val & BATTERY_MODE_MASK) == mode) 524 return mode; 525 526 if (mode == BATTERY_MODE_AMPS) 527 ret = original_val & ~BATTERY_MODE_MASK; 528 else 529 ret = original_val | BATTERY_MODE_MASK; 530 531 ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret); 532 if (ret < 0) 533 return ret; 534 535 return original_val & BATTERY_MODE_MASK; 536 } 537 538 static int sbs_get_battery_capacity(struct i2c_client *client, 539 int reg_offset, enum power_supply_property psp, 540 union power_supply_propval *val) 541 { 542 s32 ret; 543 enum sbs_battery_mode mode = BATTERY_MODE_WATTS; 544 545 if (power_supply_is_amp_property(psp)) 546 mode = BATTERY_MODE_AMPS; 547 548 mode = sbs_set_battery_mode(client, mode); 549 if (mode < 0) 550 return mode; 551 552 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr); 553 if (ret < 0) 554 return ret; 555 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 ret = sbs_get_property_index(client, psp); 622 if (ret < 0) 623 break; 624 625 /* sbs_get_battery_capacity() will change the battery mode 626 * temporarily to read the requested attribute. Ensure we stay 627 * in the desired mode for the duration of the attribute read. 628 */ 629 mutex_lock(&chip->mode_lock); 630 ret = sbs_get_battery_capacity(client, ret, psp, val); 631 mutex_unlock(&chip->mode_lock); 632 break; 633 634 case POWER_SUPPLY_PROP_SERIAL_NUMBER: 635 ret = sbs_get_battery_serial_number(client, val); 636 break; 637 638 case POWER_SUPPLY_PROP_STATUS: 639 case POWER_SUPPLY_PROP_CAPACITY_LEVEL: 640 case POWER_SUPPLY_PROP_CYCLE_COUNT: 641 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 642 case POWER_SUPPLY_PROP_CURRENT_NOW: 643 case POWER_SUPPLY_PROP_TEMP: 644 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: 645 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: 646 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: 647 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: 648 case POWER_SUPPLY_PROP_CAPACITY: 649 ret = sbs_get_property_index(client, psp); 650 if (ret < 0) 651 break; 652 653 ret = sbs_get_battery_property(client, ret, psp, val); 654 break; 655 656 case POWER_SUPPLY_PROP_MODEL_NAME: 657 ret = sbs_get_property_index(client, psp); 658 if (ret < 0) 659 break; 660 661 ret = sbs_get_battery_string_property(client, ret, psp, 662 model_name); 663 val->strval = model_name; 664 break; 665 666 case POWER_SUPPLY_PROP_MANUFACTURER: 667 ret = sbs_get_property_index(client, psp); 668 if (ret < 0) 669 break; 670 671 ret = sbs_get_battery_string_property(client, ret, psp, 672 manufacturer); 673 val->strval = manufacturer; 674 break; 675 676 default: 677 dev_err(&client->dev, 678 "%s: INVALID property\n", __func__); 679 return -EINVAL; 680 } 681 682 if (!chip->enable_detection) 683 goto done; 684 685 if (!chip->gpio_detect && 686 chip->is_present != (ret >= 0)) { 687 chip->is_present = (ret >= 0); 688 power_supply_changed(chip->power_supply); 689 } 690 691 done: 692 if (!ret) { 693 /* Convert units to match requirements for power supply class */ 694 sbs_unit_adjustment(client, psp, val); 695 } 696 697 dev_dbg(&client->dev, 698 "%s: property = %d, value = %x\n", __func__, psp, val->intval); 699 700 if (ret && chip->is_present) 701 return ret; 702 703 /* battery not present, so return NODATA for properties */ 704 if (ret) 705 return -ENODATA; 706 707 return 0; 708 } 709 710 static void sbs_supply_changed(struct sbs_info *chip) 711 { 712 struct power_supply *battery = chip->power_supply; 713 int ret; 714 715 ret = gpiod_get_value_cansleep(chip->gpio_detect); 716 if (ret < 0) 717 return; 718 chip->is_present = ret; 719 power_supply_changed(battery); 720 } 721 722 static irqreturn_t sbs_irq(int irq, void *devid) 723 { 724 sbs_supply_changed(devid); 725 return IRQ_HANDLED; 726 } 727 728 static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot, 729 unsigned int data) 730 { 731 sbs_supply_changed(i2c_get_clientdata(client)); 732 } 733 734 static void sbs_external_power_changed(struct power_supply *psy) 735 { 736 struct sbs_info *chip = power_supply_get_drvdata(psy); 737 738 /* cancel outstanding work */ 739 cancel_delayed_work_sync(&chip->work); 740 741 schedule_delayed_work(&chip->work, HZ); 742 chip->poll_time = chip->poll_retry_count; 743 } 744 745 static void sbs_delayed_work(struct work_struct *work) 746 { 747 struct sbs_info *chip; 748 s32 ret; 749 750 chip = container_of(work, struct sbs_info, work.work); 751 752 ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr); 753 /* if the read failed, give up on this work */ 754 if (ret < 0) { 755 chip->poll_time = 0; 756 return; 757 } 758 759 if (ret & BATTERY_FULL_CHARGED) 760 ret = POWER_SUPPLY_STATUS_FULL; 761 else if (ret & BATTERY_DISCHARGING) 762 ret = POWER_SUPPLY_STATUS_DISCHARGING; 763 else 764 ret = POWER_SUPPLY_STATUS_CHARGING; 765 766 sbs_status_correct(chip->client, &ret); 767 768 if (chip->last_state != ret) { 769 chip->poll_time = 0; 770 power_supply_changed(chip->power_supply); 771 return; 772 } 773 if (chip->poll_time > 0) { 774 schedule_delayed_work(&chip->work, HZ); 775 chip->poll_time--; 776 return; 777 } 778 } 779 780 static const struct power_supply_desc sbs_default_desc = { 781 .type = POWER_SUPPLY_TYPE_BATTERY, 782 .properties = sbs_properties, 783 .num_properties = ARRAY_SIZE(sbs_properties), 784 .get_property = sbs_get_property, 785 .external_power_changed = sbs_external_power_changed, 786 }; 787 788 static int sbs_probe(struct i2c_client *client, 789 const struct i2c_device_id *id) 790 { 791 struct sbs_info *chip; 792 struct power_supply_desc *sbs_desc; 793 struct sbs_platform_data *pdata = client->dev.platform_data; 794 struct power_supply_config psy_cfg = {}; 795 int rc; 796 int irq; 797 798 sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc, 799 sizeof(*sbs_desc), GFP_KERNEL); 800 if (!sbs_desc) 801 return -ENOMEM; 802 803 sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s", 804 dev_name(&client->dev)); 805 if (!sbs_desc->name) 806 return -ENOMEM; 807 808 chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL); 809 if (!chip) 810 return -ENOMEM; 811 812 chip->client = client; 813 chip->enable_detection = false; 814 psy_cfg.of_node = client->dev.of_node; 815 psy_cfg.drv_data = chip; 816 chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN; 817 mutex_init(&chip->mode_lock); 818 819 /* use pdata if available, fall back to DT properties, 820 * or hardcoded defaults if not 821 */ 822 rc = of_property_read_u32(client->dev.of_node, "sbs,i2c-retry-count", 823 &chip->i2c_retry_count); 824 if (rc) 825 chip->i2c_retry_count = 0; 826 827 rc = of_property_read_u32(client->dev.of_node, "sbs,poll-retry-count", 828 &chip->poll_retry_count); 829 if (rc) 830 chip->poll_retry_count = 0; 831 832 if (pdata) { 833 chip->poll_retry_count = pdata->poll_retry_count; 834 chip->i2c_retry_count = pdata->i2c_retry_count; 835 } 836 chip->i2c_retry_count = chip->i2c_retry_count + 1; 837 838 chip->gpio_detect = devm_gpiod_get_optional(&client->dev, 839 "sbs,battery-detect", GPIOD_IN); 840 if (IS_ERR(chip->gpio_detect)) { 841 dev_err(&client->dev, "Failed to get gpio: %ld\n", 842 PTR_ERR(chip->gpio_detect)); 843 return PTR_ERR(chip->gpio_detect); 844 } 845 846 i2c_set_clientdata(client, chip); 847 848 if (!chip->gpio_detect) 849 goto skip_gpio; 850 851 irq = gpiod_to_irq(chip->gpio_detect); 852 if (irq <= 0) { 853 dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq); 854 goto skip_gpio; 855 } 856 857 rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq, 858 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 859 dev_name(&client->dev), chip); 860 if (rc) { 861 dev_warn(&client->dev, "Failed to request irq: %d\n", rc); 862 goto skip_gpio; 863 } 864 865 skip_gpio: 866 /* 867 * Before we register, we might need to make sure we can actually talk 868 * to the battery. 869 */ 870 if (!(force_load || chip->gpio_detect)) { 871 rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr); 872 873 if (rc < 0) { 874 dev_err(&client->dev, "%s: Failed to get device status\n", 875 __func__); 876 goto exit_psupply; 877 } 878 } 879 880 chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc, 881 &psy_cfg); 882 if (IS_ERR(chip->power_supply)) { 883 dev_err(&client->dev, 884 "%s: Failed to register power supply\n", __func__); 885 rc = PTR_ERR(chip->power_supply); 886 goto exit_psupply; 887 } 888 889 dev_info(&client->dev, 890 "%s: battery gas gauge device registered\n", client->name); 891 892 INIT_DELAYED_WORK(&chip->work, sbs_delayed_work); 893 894 chip->enable_detection = true; 895 896 return 0; 897 898 exit_psupply: 899 return rc; 900 } 901 902 static int sbs_remove(struct i2c_client *client) 903 { 904 struct sbs_info *chip = i2c_get_clientdata(client); 905 906 cancel_delayed_work_sync(&chip->work); 907 908 return 0; 909 } 910 911 #if defined CONFIG_PM_SLEEP 912 913 static int sbs_suspend(struct device *dev) 914 { 915 struct i2c_client *client = to_i2c_client(dev); 916 struct sbs_info *chip = i2c_get_clientdata(client); 917 918 if (chip->poll_time > 0) 919 cancel_delayed_work_sync(&chip->work); 920 921 /* 922 * Write to manufacturer access with sleep command. 923 * Support is manufacturer dependend, so ignore errors. 924 */ 925 sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr, 926 MANUFACTURER_ACCESS_SLEEP); 927 928 return 0; 929 } 930 931 static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL); 932 #define SBS_PM_OPS (&sbs_pm_ops) 933 934 #else 935 #define SBS_PM_OPS NULL 936 #endif 937 938 static const struct i2c_device_id sbs_id[] = { 939 { "bq20z75", 0 }, 940 { "sbs-battery", 1 }, 941 {} 942 }; 943 MODULE_DEVICE_TABLE(i2c, sbs_id); 944 945 static const struct of_device_id sbs_dt_ids[] = { 946 { .compatible = "sbs,sbs-battery" }, 947 { .compatible = "ti,bq20z75" }, 948 { } 949 }; 950 MODULE_DEVICE_TABLE(of, sbs_dt_ids); 951 952 static struct i2c_driver sbs_battery_driver = { 953 .probe = sbs_probe, 954 .remove = sbs_remove, 955 .alert = sbs_alert, 956 .id_table = sbs_id, 957 .driver = { 958 .name = "sbs-battery", 959 .of_match_table = sbs_dt_ids, 960 .pm = SBS_PM_OPS, 961 }, 962 }; 963 module_i2c_driver(sbs_battery_driver); 964 965 MODULE_DESCRIPTION("SBS battery monitor driver"); 966 MODULE_LICENSE("GPL"); 967 968 module_param(force_load, bool, S_IRUSR | S_IRGRP | S_IROTH); 969 MODULE_PARM_DESC(force_load, 970 "Attempt to load the driver even if no battery is connected"); 971