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