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