18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Gas Gauge driver for SBS Compliant Batteries 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2010, NVIDIA Corporation. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/bits.h> 98c2ecf20Sopenharmony_ci#include <linux/delay.h> 108c2ecf20Sopenharmony_ci#include <linux/err.h> 118c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h> 128c2ecf20Sopenharmony_ci#include <linux/i2c.h> 138c2ecf20Sopenharmony_ci#include <linux/init.h> 148c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 158c2ecf20Sopenharmony_ci#include <linux/kernel.h> 168c2ecf20Sopenharmony_ci#include <linux/module.h> 178c2ecf20Sopenharmony_ci#include <linux/property.h> 188c2ecf20Sopenharmony_ci#include <linux/of_device.h> 198c2ecf20Sopenharmony_ci#include <linux/power/sbs-battery.h> 208c2ecf20Sopenharmony_ci#include <linux/power_supply.h> 218c2ecf20Sopenharmony_ci#include <linux/slab.h> 228c2ecf20Sopenharmony_ci#include <linux/stat.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cienum { 258c2ecf20Sopenharmony_ci REG_MANUFACTURER_DATA, 268c2ecf20Sopenharmony_ci REG_BATTERY_MODE, 278c2ecf20Sopenharmony_ci REG_TEMPERATURE, 288c2ecf20Sopenharmony_ci REG_VOLTAGE, 298c2ecf20Sopenharmony_ci REG_CURRENT_NOW, 308c2ecf20Sopenharmony_ci REG_CURRENT_AVG, 318c2ecf20Sopenharmony_ci REG_MAX_ERR, 328c2ecf20Sopenharmony_ci REG_CAPACITY, 338c2ecf20Sopenharmony_ci REG_TIME_TO_EMPTY, 348c2ecf20Sopenharmony_ci REG_TIME_TO_FULL, 358c2ecf20Sopenharmony_ci REG_STATUS, 368c2ecf20Sopenharmony_ci REG_CAPACITY_LEVEL, 378c2ecf20Sopenharmony_ci REG_CYCLE_COUNT, 388c2ecf20Sopenharmony_ci REG_SERIAL_NUMBER, 398c2ecf20Sopenharmony_ci REG_REMAINING_CAPACITY, 408c2ecf20Sopenharmony_ci REG_REMAINING_CAPACITY_CHARGE, 418c2ecf20Sopenharmony_ci REG_FULL_CHARGE_CAPACITY, 428c2ecf20Sopenharmony_ci REG_FULL_CHARGE_CAPACITY_CHARGE, 438c2ecf20Sopenharmony_ci REG_DESIGN_CAPACITY, 448c2ecf20Sopenharmony_ci REG_DESIGN_CAPACITY_CHARGE, 458c2ecf20Sopenharmony_ci REG_DESIGN_VOLTAGE_MIN, 468c2ecf20Sopenharmony_ci REG_DESIGN_VOLTAGE_MAX, 478c2ecf20Sopenharmony_ci REG_CHEMISTRY, 488c2ecf20Sopenharmony_ci REG_MANUFACTURER, 498c2ecf20Sopenharmony_ci REG_MODEL_NAME, 508c2ecf20Sopenharmony_ci REG_CHARGE_CURRENT, 518c2ecf20Sopenharmony_ci REG_CHARGE_VOLTAGE, 528c2ecf20Sopenharmony_ci}; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci#define REG_ADDR_SPEC_INFO 0x1A 558c2ecf20Sopenharmony_ci#define SPEC_INFO_VERSION_MASK GENMASK(7, 4) 568c2ecf20Sopenharmony_ci#define SPEC_INFO_VERSION_SHIFT 4 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci#define SBS_VERSION_1_0 1 598c2ecf20Sopenharmony_ci#define SBS_VERSION_1_1 2 608c2ecf20Sopenharmony_ci#define SBS_VERSION_1_1_WITH_PEC 3 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci#define REG_ADDR_MANUFACTURE_DATE 0x1B 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci/* Battery Mode defines */ 658c2ecf20Sopenharmony_ci#define BATTERY_MODE_OFFSET 0x03 668c2ecf20Sopenharmony_ci#define BATTERY_MODE_CAPACITY_MASK BIT(15) 678c2ecf20Sopenharmony_cienum sbs_capacity_mode { 688c2ecf20Sopenharmony_ci CAPACITY_MODE_AMPS = 0, 698c2ecf20Sopenharmony_ci CAPACITY_MODE_WATTS = BATTERY_MODE_CAPACITY_MASK 708c2ecf20Sopenharmony_ci}; 718c2ecf20Sopenharmony_ci#define BATTERY_MODE_CHARGER_MASK (1<<14) 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci/* manufacturer access defines */ 748c2ecf20Sopenharmony_ci#define MANUFACTURER_ACCESS_STATUS 0x0006 758c2ecf20Sopenharmony_ci#define MANUFACTURER_ACCESS_SLEEP 0x0011 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci/* battery status value bits */ 788c2ecf20Sopenharmony_ci#define BATTERY_INITIALIZED 0x80 798c2ecf20Sopenharmony_ci#define BATTERY_DISCHARGING 0x40 808c2ecf20Sopenharmony_ci#define BATTERY_FULL_CHARGED 0x20 818c2ecf20Sopenharmony_ci#define BATTERY_FULL_DISCHARGED 0x10 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci/* min_value and max_value are only valid for numerical data */ 848c2ecf20Sopenharmony_ci#define SBS_DATA(_psp, _addr, _min_value, _max_value) { \ 858c2ecf20Sopenharmony_ci .psp = _psp, \ 868c2ecf20Sopenharmony_ci .addr = _addr, \ 878c2ecf20Sopenharmony_ci .min_value = _min_value, \ 888c2ecf20Sopenharmony_ci .max_value = _max_value, \ 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_cistatic const struct chip_data { 928c2ecf20Sopenharmony_ci enum power_supply_property psp; 938c2ecf20Sopenharmony_ci u8 addr; 948c2ecf20Sopenharmony_ci int min_value; 958c2ecf20Sopenharmony_ci int max_value; 968c2ecf20Sopenharmony_ci} sbs_data[] = { 978c2ecf20Sopenharmony_ci [REG_MANUFACTURER_DATA] = 988c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535), 998c2ecf20Sopenharmony_ci [REG_BATTERY_MODE] = 1008c2ecf20Sopenharmony_ci SBS_DATA(-1, 0x03, 0, 65535), 1018c2ecf20Sopenharmony_ci [REG_TEMPERATURE] = 1028c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535), 1038c2ecf20Sopenharmony_ci [REG_VOLTAGE] = 1048c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000), 1058c2ecf20Sopenharmony_ci [REG_CURRENT_NOW] = 1068c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767), 1078c2ecf20Sopenharmony_ci [REG_CURRENT_AVG] = 1088c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_CURRENT_AVG, 0x0B, -32768, 32767), 1098c2ecf20Sopenharmony_ci [REG_MAX_ERR] = 1108c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN, 0x0c, 0, 100), 1118c2ecf20Sopenharmony_ci [REG_CAPACITY] = 1128c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100), 1138c2ecf20Sopenharmony_ci [REG_REMAINING_CAPACITY] = 1148c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535), 1158c2ecf20Sopenharmony_ci [REG_REMAINING_CAPACITY_CHARGE] = 1168c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535), 1178c2ecf20Sopenharmony_ci [REG_FULL_CHARGE_CAPACITY] = 1188c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535), 1198c2ecf20Sopenharmony_ci [REG_FULL_CHARGE_CAPACITY_CHARGE] = 1208c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535), 1218c2ecf20Sopenharmony_ci [REG_TIME_TO_EMPTY] = 1228c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535), 1238c2ecf20Sopenharmony_ci [REG_TIME_TO_FULL] = 1248c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535), 1258c2ecf20Sopenharmony_ci [REG_CHARGE_CURRENT] = 1268c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 0x14, 0, 65535), 1278c2ecf20Sopenharmony_ci [REG_CHARGE_VOLTAGE] = 1288c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 0x15, 0, 65535), 1298c2ecf20Sopenharmony_ci [REG_STATUS] = 1308c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535), 1318c2ecf20Sopenharmony_ci [REG_CAPACITY_LEVEL] = 1328c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535), 1338c2ecf20Sopenharmony_ci [REG_CYCLE_COUNT] = 1348c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535), 1358c2ecf20Sopenharmony_ci [REG_DESIGN_CAPACITY] = 1368c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535), 1378c2ecf20Sopenharmony_ci [REG_DESIGN_CAPACITY_CHARGE] = 1388c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535), 1398c2ecf20Sopenharmony_ci [REG_DESIGN_VOLTAGE_MIN] = 1408c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535), 1418c2ecf20Sopenharmony_ci [REG_DESIGN_VOLTAGE_MAX] = 1428c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535), 1438c2ecf20Sopenharmony_ci [REG_SERIAL_NUMBER] = 1448c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535), 1458c2ecf20Sopenharmony_ci /* Properties of type `const char *' */ 1468c2ecf20Sopenharmony_ci [REG_MANUFACTURER] = 1478c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535), 1488c2ecf20Sopenharmony_ci [REG_MODEL_NAME] = 1498c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535), 1508c2ecf20Sopenharmony_ci [REG_CHEMISTRY] = 1518c2ecf20Sopenharmony_ci SBS_DATA(POWER_SUPPLY_PROP_TECHNOLOGY, 0x22, 0, 65535) 1528c2ecf20Sopenharmony_ci}; 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_cistatic const enum power_supply_property sbs_properties[] = { 1558c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_STATUS, 1568c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CAPACITY_LEVEL, 1578c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_HEALTH, 1588c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_PRESENT, 1598c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_TECHNOLOGY, 1608c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CYCLE_COUNT, 1618c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_VOLTAGE_NOW, 1628c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CURRENT_NOW, 1638c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CURRENT_AVG, 1648c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CAPACITY, 1658c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN, 1668c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_TEMP, 1678c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 1688c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 1698c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_SERIAL_NUMBER, 1708c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 1718c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 1728c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_ENERGY_NOW, 1738c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_ENERGY_FULL, 1748c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 1758c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CHARGE_NOW, 1768c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CHARGE_FULL, 1778c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 1788c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 1798c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 1808c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_MANUFACTURE_YEAR, 1818c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_MANUFACTURE_MONTH, 1828c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_MANUFACTURE_DAY, 1838c2ecf20Sopenharmony_ci /* Properties of type `const char *' */ 1848c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_MANUFACTURER, 1858c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_MODEL_NAME 1868c2ecf20Sopenharmony_ci}; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci/* Supports special manufacturer commands from TI BQ20Z65 and BQ20Z75 IC. */ 1898c2ecf20Sopenharmony_ci#define SBS_FLAGS_TI_BQ20ZX5 BIT(0) 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_cistruct sbs_info { 1928c2ecf20Sopenharmony_ci struct i2c_client *client; 1938c2ecf20Sopenharmony_ci struct power_supply *power_supply; 1948c2ecf20Sopenharmony_ci bool is_present; 1958c2ecf20Sopenharmony_ci struct gpio_desc *gpio_detect; 1968c2ecf20Sopenharmony_ci bool charger_broadcasts; 1978c2ecf20Sopenharmony_ci int last_state; 1988c2ecf20Sopenharmony_ci int poll_time; 1998c2ecf20Sopenharmony_ci u32 i2c_retry_count; 2008c2ecf20Sopenharmony_ci u32 poll_retry_count; 2018c2ecf20Sopenharmony_ci struct delayed_work work; 2028c2ecf20Sopenharmony_ci struct mutex mode_lock; 2038c2ecf20Sopenharmony_ci u32 flags; 2048c2ecf20Sopenharmony_ci}; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_cistatic char model_name[I2C_SMBUS_BLOCK_MAX + 1]; 2078c2ecf20Sopenharmony_cistatic char manufacturer[I2C_SMBUS_BLOCK_MAX + 1]; 2088c2ecf20Sopenharmony_cistatic char chemistry[I2C_SMBUS_BLOCK_MAX + 1]; 2098c2ecf20Sopenharmony_cistatic bool force_load; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_cistatic int sbs_read_word_data(struct i2c_client *client, u8 address); 2128c2ecf20Sopenharmony_cistatic int sbs_write_word_data(struct i2c_client *client, u8 address, u16 value); 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_cistatic void sbs_disable_charger_broadcasts(struct sbs_info *chip) 2158c2ecf20Sopenharmony_ci{ 2168c2ecf20Sopenharmony_ci int val = sbs_read_word_data(chip->client, BATTERY_MODE_OFFSET); 2178c2ecf20Sopenharmony_ci if (val < 0) 2188c2ecf20Sopenharmony_ci goto exit; 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci val |= BATTERY_MODE_CHARGER_MASK; 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci val = sbs_write_word_data(chip->client, BATTERY_MODE_OFFSET, val); 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ciexit: 2258c2ecf20Sopenharmony_ci if (val < 0) 2268c2ecf20Sopenharmony_ci dev_err(&chip->client->dev, 2278c2ecf20Sopenharmony_ci "Failed to disable charger broadcasting: %d\n", val); 2288c2ecf20Sopenharmony_ci else 2298c2ecf20Sopenharmony_ci dev_dbg(&chip->client->dev, "%s\n", __func__); 2308c2ecf20Sopenharmony_ci} 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_cistatic int sbs_update_presence(struct sbs_info *chip, bool is_present) 2338c2ecf20Sopenharmony_ci{ 2348c2ecf20Sopenharmony_ci struct i2c_client *client = chip->client; 2358c2ecf20Sopenharmony_ci int retries = chip->i2c_retry_count; 2368c2ecf20Sopenharmony_ci s32 ret = 0; 2378c2ecf20Sopenharmony_ci u8 version; 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci if (chip->is_present == is_present) 2408c2ecf20Sopenharmony_ci return 0; 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci if (!is_present) { 2438c2ecf20Sopenharmony_ci chip->is_present = false; 2448c2ecf20Sopenharmony_ci /* Disable PEC when no device is present */ 2458c2ecf20Sopenharmony_ci client->flags &= ~I2C_CLIENT_PEC; 2468c2ecf20Sopenharmony_ci return 0; 2478c2ecf20Sopenharmony_ci } 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci /* Check if device supports packet error checking and use it */ 2508c2ecf20Sopenharmony_ci while (retries > 0) { 2518c2ecf20Sopenharmony_ci ret = i2c_smbus_read_word_data(client, REG_ADDR_SPEC_INFO); 2528c2ecf20Sopenharmony_ci if (ret >= 0) 2538c2ecf20Sopenharmony_ci break; 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci /* 2568c2ecf20Sopenharmony_ci * Some batteries trigger the detection pin before the 2578c2ecf20Sopenharmony_ci * I2C bus is properly connected. This works around the 2588c2ecf20Sopenharmony_ci * issue. 2598c2ecf20Sopenharmony_ci */ 2608c2ecf20Sopenharmony_ci msleep(100); 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci retries--; 2638c2ecf20Sopenharmony_ci } 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci if (ret < 0) { 2668c2ecf20Sopenharmony_ci dev_dbg(&client->dev, "failed to read spec info: %d\n", ret); 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci /* fallback to old behaviour */ 2698c2ecf20Sopenharmony_ci client->flags &= ~I2C_CLIENT_PEC; 2708c2ecf20Sopenharmony_ci chip->is_present = true; 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci return ret; 2738c2ecf20Sopenharmony_ci } 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci version = (ret & SPEC_INFO_VERSION_MASK) >> SPEC_INFO_VERSION_SHIFT; 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci if (version == SBS_VERSION_1_1_WITH_PEC) 2788c2ecf20Sopenharmony_ci client->flags |= I2C_CLIENT_PEC; 2798c2ecf20Sopenharmony_ci else 2808c2ecf20Sopenharmony_ci client->flags &= ~I2C_CLIENT_PEC; 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci if (of_device_is_compatible(client->dev.parent->of_node, "google,cros-ec-i2c-tunnel") 2838c2ecf20Sopenharmony_ci && client->flags & I2C_CLIENT_PEC) { 2848c2ecf20Sopenharmony_ci dev_info(&client->dev, "Disabling PEC because of broken Cros-EC implementation\n"); 2858c2ecf20Sopenharmony_ci client->flags &= ~I2C_CLIENT_PEC; 2868c2ecf20Sopenharmony_ci } 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci dev_dbg(&client->dev, "PEC: %s\n", (client->flags & I2C_CLIENT_PEC) ? 2898c2ecf20Sopenharmony_ci "enabled" : "disabled"); 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci if (!chip->is_present && is_present && !chip->charger_broadcasts) 2928c2ecf20Sopenharmony_ci sbs_disable_charger_broadcasts(chip); 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_ci chip->is_present = true; 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci return 0; 2978c2ecf20Sopenharmony_ci} 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_cistatic int sbs_read_word_data(struct i2c_client *client, u8 address) 3008c2ecf20Sopenharmony_ci{ 3018c2ecf20Sopenharmony_ci struct sbs_info *chip = i2c_get_clientdata(client); 3028c2ecf20Sopenharmony_ci int retries = chip->i2c_retry_count; 3038c2ecf20Sopenharmony_ci s32 ret = 0; 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci while (retries > 0) { 3068c2ecf20Sopenharmony_ci ret = i2c_smbus_read_word_data(client, address); 3078c2ecf20Sopenharmony_ci if (ret >= 0) 3088c2ecf20Sopenharmony_ci break; 3098c2ecf20Sopenharmony_ci retries--; 3108c2ecf20Sopenharmony_ci } 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci if (ret < 0) { 3138c2ecf20Sopenharmony_ci dev_dbg(&client->dev, 3148c2ecf20Sopenharmony_ci "%s: i2c read at address 0x%x failed\n", 3158c2ecf20Sopenharmony_ci __func__, address); 3168c2ecf20Sopenharmony_ci return ret; 3178c2ecf20Sopenharmony_ci } 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci return ret; 3208c2ecf20Sopenharmony_ci} 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_cistatic int sbs_read_string_data_fallback(struct i2c_client *client, u8 address, char *values) 3238c2ecf20Sopenharmony_ci{ 3248c2ecf20Sopenharmony_ci struct sbs_info *chip = i2c_get_clientdata(client); 3258c2ecf20Sopenharmony_ci s32 ret = 0, block_length = 0; 3268c2ecf20Sopenharmony_ci int retries_length, retries_block; 3278c2ecf20Sopenharmony_ci u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1]; 3288c2ecf20Sopenharmony_ci 3298c2ecf20Sopenharmony_ci retries_length = chip->i2c_retry_count; 3308c2ecf20Sopenharmony_ci retries_block = chip->i2c_retry_count; 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci dev_warn_once(&client->dev, "I2C adapter does not support I2C_FUNC_SMBUS_READ_BLOCK_DATA.\n" 3338c2ecf20Sopenharmony_ci "Fallback method does not support PEC.\n"); 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ci /* Adapter needs to support these two functions */ 3368c2ecf20Sopenharmony_ci if (!i2c_check_functionality(client->adapter, 3378c2ecf20Sopenharmony_ci I2C_FUNC_SMBUS_BYTE_DATA | 3388c2ecf20Sopenharmony_ci I2C_FUNC_SMBUS_I2C_BLOCK)){ 3398c2ecf20Sopenharmony_ci return -ENODEV; 3408c2ecf20Sopenharmony_ci } 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci /* Get the length of block data */ 3438c2ecf20Sopenharmony_ci while (retries_length > 0) { 3448c2ecf20Sopenharmony_ci ret = i2c_smbus_read_byte_data(client, address); 3458c2ecf20Sopenharmony_ci if (ret >= 0) 3468c2ecf20Sopenharmony_ci break; 3478c2ecf20Sopenharmony_ci retries_length--; 3488c2ecf20Sopenharmony_ci } 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci if (ret < 0) { 3518c2ecf20Sopenharmony_ci dev_dbg(&client->dev, 3528c2ecf20Sopenharmony_ci "%s: i2c read at address 0x%x failed\n", 3538c2ecf20Sopenharmony_ci __func__, address); 3548c2ecf20Sopenharmony_ci return ret; 3558c2ecf20Sopenharmony_ci } 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_ci /* block_length does not include NULL terminator */ 3588c2ecf20Sopenharmony_ci block_length = ret; 3598c2ecf20Sopenharmony_ci if (block_length > I2C_SMBUS_BLOCK_MAX) { 3608c2ecf20Sopenharmony_ci dev_err(&client->dev, 3618c2ecf20Sopenharmony_ci "%s: Returned block_length is longer than 0x%x\n", 3628c2ecf20Sopenharmony_ci __func__, I2C_SMBUS_BLOCK_MAX); 3638c2ecf20Sopenharmony_ci return -EINVAL; 3648c2ecf20Sopenharmony_ci } 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_ci /* Get the block data */ 3678c2ecf20Sopenharmony_ci while (retries_block > 0) { 3688c2ecf20Sopenharmony_ci ret = i2c_smbus_read_i2c_block_data( 3698c2ecf20Sopenharmony_ci client, address, 3708c2ecf20Sopenharmony_ci block_length + 1, block_buffer); 3718c2ecf20Sopenharmony_ci if (ret >= 0) 3728c2ecf20Sopenharmony_ci break; 3738c2ecf20Sopenharmony_ci retries_block--; 3748c2ecf20Sopenharmony_ci } 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci if (ret < 0) { 3778c2ecf20Sopenharmony_ci dev_dbg(&client->dev, 3788c2ecf20Sopenharmony_ci "%s: i2c read at address 0x%x failed\n", 3798c2ecf20Sopenharmony_ci __func__, address); 3808c2ecf20Sopenharmony_ci return ret; 3818c2ecf20Sopenharmony_ci } 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ci /* block_buffer[0] == block_length */ 3848c2ecf20Sopenharmony_ci memcpy(values, block_buffer + 1, block_length); 3858c2ecf20Sopenharmony_ci values[block_length] = '\0'; 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci return ret; 3888c2ecf20Sopenharmony_ci} 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_cistatic int sbs_read_string_data(struct i2c_client *client, u8 address, char *values) 3918c2ecf20Sopenharmony_ci{ 3928c2ecf20Sopenharmony_ci struct sbs_info *chip = i2c_get_clientdata(client); 3938c2ecf20Sopenharmony_ci int retries = chip->i2c_retry_count; 3948c2ecf20Sopenharmony_ci int ret = 0; 3958c2ecf20Sopenharmony_ci 3968c2ecf20Sopenharmony_ci if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BLOCK_DATA)) { 3978c2ecf20Sopenharmony_ci bool pec = client->flags & I2C_CLIENT_PEC; 3988c2ecf20Sopenharmony_ci client->flags &= ~I2C_CLIENT_PEC; 3998c2ecf20Sopenharmony_ci ret = sbs_read_string_data_fallback(client, address, values); 4008c2ecf20Sopenharmony_ci if (pec) 4018c2ecf20Sopenharmony_ci client->flags |= I2C_CLIENT_PEC; 4028c2ecf20Sopenharmony_ci return ret; 4038c2ecf20Sopenharmony_ci } 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci while (retries > 0) { 4068c2ecf20Sopenharmony_ci ret = i2c_smbus_read_block_data(client, address, values); 4078c2ecf20Sopenharmony_ci if (ret >= 0) 4088c2ecf20Sopenharmony_ci break; 4098c2ecf20Sopenharmony_ci retries--; 4108c2ecf20Sopenharmony_ci } 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_ci if (ret < 0) { 4138c2ecf20Sopenharmony_ci dev_dbg(&client->dev, "failed to read block 0x%x: %d\n", address, ret); 4148c2ecf20Sopenharmony_ci return ret; 4158c2ecf20Sopenharmony_ci } 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci /* add string termination */ 4188c2ecf20Sopenharmony_ci values[ret] = '\0'; 4198c2ecf20Sopenharmony_ci return ret; 4208c2ecf20Sopenharmony_ci} 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_cistatic int sbs_write_word_data(struct i2c_client *client, u8 address, 4238c2ecf20Sopenharmony_ci u16 value) 4248c2ecf20Sopenharmony_ci{ 4258c2ecf20Sopenharmony_ci struct sbs_info *chip = i2c_get_clientdata(client); 4268c2ecf20Sopenharmony_ci int retries = chip->i2c_retry_count; 4278c2ecf20Sopenharmony_ci s32 ret = 0; 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_ci while (retries > 0) { 4308c2ecf20Sopenharmony_ci ret = i2c_smbus_write_word_data(client, address, value); 4318c2ecf20Sopenharmony_ci if (ret >= 0) 4328c2ecf20Sopenharmony_ci break; 4338c2ecf20Sopenharmony_ci retries--; 4348c2ecf20Sopenharmony_ci } 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci if (ret < 0) { 4378c2ecf20Sopenharmony_ci dev_dbg(&client->dev, 4388c2ecf20Sopenharmony_ci "%s: i2c write to address 0x%x failed\n", 4398c2ecf20Sopenharmony_ci __func__, address); 4408c2ecf20Sopenharmony_ci return ret; 4418c2ecf20Sopenharmony_ci } 4428c2ecf20Sopenharmony_ci 4438c2ecf20Sopenharmony_ci return 0; 4448c2ecf20Sopenharmony_ci} 4458c2ecf20Sopenharmony_ci 4468c2ecf20Sopenharmony_cistatic int sbs_status_correct(struct i2c_client *client, int *intval) 4478c2ecf20Sopenharmony_ci{ 4488c2ecf20Sopenharmony_ci int ret; 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_ci ret = sbs_read_word_data(client, sbs_data[REG_CURRENT_NOW].addr); 4518c2ecf20Sopenharmony_ci if (ret < 0) 4528c2ecf20Sopenharmony_ci return ret; 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_ci ret = (s16)ret; 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ci /* Not drawing current -> not charging (i.e. idle) */ 4578c2ecf20Sopenharmony_ci if (*intval != POWER_SUPPLY_STATUS_FULL && ret == 0) 4588c2ecf20Sopenharmony_ci *intval = POWER_SUPPLY_STATUS_NOT_CHARGING; 4598c2ecf20Sopenharmony_ci 4608c2ecf20Sopenharmony_ci if (*intval == POWER_SUPPLY_STATUS_FULL) { 4618c2ecf20Sopenharmony_ci /* Drawing or providing current when full */ 4628c2ecf20Sopenharmony_ci if (ret > 0) 4638c2ecf20Sopenharmony_ci *intval = POWER_SUPPLY_STATUS_CHARGING; 4648c2ecf20Sopenharmony_ci else if (ret < 0) 4658c2ecf20Sopenharmony_ci *intval = POWER_SUPPLY_STATUS_DISCHARGING; 4668c2ecf20Sopenharmony_ci } 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci return 0; 4698c2ecf20Sopenharmony_ci} 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_cistatic bool sbs_bat_needs_calibration(struct i2c_client *client) 4728c2ecf20Sopenharmony_ci{ 4738c2ecf20Sopenharmony_ci int ret; 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci ret = sbs_read_word_data(client, sbs_data[REG_BATTERY_MODE].addr); 4768c2ecf20Sopenharmony_ci if (ret < 0) 4778c2ecf20Sopenharmony_ci return false; 4788c2ecf20Sopenharmony_ci 4798c2ecf20Sopenharmony_ci return !!(ret & BIT(7)); 4808c2ecf20Sopenharmony_ci} 4818c2ecf20Sopenharmony_ci 4828c2ecf20Sopenharmony_cistatic int sbs_get_ti_battery_presence_and_health( 4838c2ecf20Sopenharmony_ci struct i2c_client *client, enum power_supply_property psp, 4848c2ecf20Sopenharmony_ci union power_supply_propval *val) 4858c2ecf20Sopenharmony_ci{ 4868c2ecf20Sopenharmony_ci s32 ret; 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_ci /* 4898c2ecf20Sopenharmony_ci * Write to ManufacturerAccess with ManufacturerAccess command 4908c2ecf20Sopenharmony_ci * and then read the status. 4918c2ecf20Sopenharmony_ci */ 4928c2ecf20Sopenharmony_ci ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr, 4938c2ecf20Sopenharmony_ci MANUFACTURER_ACCESS_STATUS); 4948c2ecf20Sopenharmony_ci if (ret < 0) { 4958c2ecf20Sopenharmony_ci if (psp == POWER_SUPPLY_PROP_PRESENT) 4968c2ecf20Sopenharmony_ci val->intval = 0; /* battery removed */ 4978c2ecf20Sopenharmony_ci return ret; 4988c2ecf20Sopenharmony_ci } 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr); 5018c2ecf20Sopenharmony_ci if (ret < 0) { 5028c2ecf20Sopenharmony_ci if (psp == POWER_SUPPLY_PROP_PRESENT) 5038c2ecf20Sopenharmony_ci val->intval = 0; /* battery removed */ 5048c2ecf20Sopenharmony_ci return ret; 5058c2ecf20Sopenharmony_ci } 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ci if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value || 5088c2ecf20Sopenharmony_ci ret > sbs_data[REG_MANUFACTURER_DATA].max_value) { 5098c2ecf20Sopenharmony_ci val->intval = 0; 5108c2ecf20Sopenharmony_ci return 0; 5118c2ecf20Sopenharmony_ci } 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci /* Mask the upper nibble of 2nd byte and 5148c2ecf20Sopenharmony_ci * lower byte of response then 5158c2ecf20Sopenharmony_ci * shift the result by 8 to get status*/ 5168c2ecf20Sopenharmony_ci ret &= 0x0F00; 5178c2ecf20Sopenharmony_ci ret >>= 8; 5188c2ecf20Sopenharmony_ci if (psp == POWER_SUPPLY_PROP_PRESENT) { 5198c2ecf20Sopenharmony_ci if (ret == 0x0F) 5208c2ecf20Sopenharmony_ci /* battery removed */ 5218c2ecf20Sopenharmony_ci val->intval = 0; 5228c2ecf20Sopenharmony_ci else 5238c2ecf20Sopenharmony_ci val->intval = 1; 5248c2ecf20Sopenharmony_ci } else if (psp == POWER_SUPPLY_PROP_HEALTH) { 5258c2ecf20Sopenharmony_ci if (ret == 0x09) 5268c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 5278c2ecf20Sopenharmony_ci else if (ret == 0x0B) 5288c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; 5298c2ecf20Sopenharmony_ci else if (ret == 0x0C) 5308c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_DEAD; 5318c2ecf20Sopenharmony_ci else if (sbs_bat_needs_calibration(client)) 5328c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED; 5338c2ecf20Sopenharmony_ci else 5348c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_GOOD; 5358c2ecf20Sopenharmony_ci } 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_ci return 0; 5388c2ecf20Sopenharmony_ci} 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_cistatic int sbs_get_battery_presence_and_health( 5418c2ecf20Sopenharmony_ci struct i2c_client *client, enum power_supply_property psp, 5428c2ecf20Sopenharmony_ci union power_supply_propval *val) 5438c2ecf20Sopenharmony_ci{ 5448c2ecf20Sopenharmony_ci struct sbs_info *chip = i2c_get_clientdata(client); 5458c2ecf20Sopenharmony_ci int ret; 5468c2ecf20Sopenharmony_ci 5478c2ecf20Sopenharmony_ci if (chip->flags & SBS_FLAGS_TI_BQ20ZX5) 5488c2ecf20Sopenharmony_ci return sbs_get_ti_battery_presence_and_health(client, psp, val); 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_ci /* Dummy command; if it succeeds, battery is present. */ 5518c2ecf20Sopenharmony_ci ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr); 5528c2ecf20Sopenharmony_ci 5538c2ecf20Sopenharmony_ci if (ret < 0) { /* battery not present*/ 5548c2ecf20Sopenharmony_ci if (psp == POWER_SUPPLY_PROP_PRESENT) { 5558c2ecf20Sopenharmony_ci val->intval = 0; 5568c2ecf20Sopenharmony_ci return 0; 5578c2ecf20Sopenharmony_ci } 5588c2ecf20Sopenharmony_ci return ret; 5598c2ecf20Sopenharmony_ci } 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci if (psp == POWER_SUPPLY_PROP_PRESENT) 5628c2ecf20Sopenharmony_ci val->intval = 1; /* battery present */ 5638c2ecf20Sopenharmony_ci else { /* POWER_SUPPLY_PROP_HEALTH */ 5648c2ecf20Sopenharmony_ci if (sbs_bat_needs_calibration(client)) { 5658c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED; 5668c2ecf20Sopenharmony_ci } else { 5678c2ecf20Sopenharmony_ci /* SBS spec doesn't have a general health command. */ 5688c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_UNKNOWN; 5698c2ecf20Sopenharmony_ci } 5708c2ecf20Sopenharmony_ci } 5718c2ecf20Sopenharmony_ci 5728c2ecf20Sopenharmony_ci return 0; 5738c2ecf20Sopenharmony_ci} 5748c2ecf20Sopenharmony_ci 5758c2ecf20Sopenharmony_cistatic int sbs_get_battery_property(struct i2c_client *client, 5768c2ecf20Sopenharmony_ci int reg_offset, enum power_supply_property psp, 5778c2ecf20Sopenharmony_ci union power_supply_propval *val) 5788c2ecf20Sopenharmony_ci{ 5798c2ecf20Sopenharmony_ci struct sbs_info *chip = i2c_get_clientdata(client); 5808c2ecf20Sopenharmony_ci s32 ret; 5818c2ecf20Sopenharmony_ci 5828c2ecf20Sopenharmony_ci ret = sbs_read_word_data(client, sbs_data[reg_offset].addr); 5838c2ecf20Sopenharmony_ci if (ret < 0) 5848c2ecf20Sopenharmony_ci return ret; 5858c2ecf20Sopenharmony_ci 5868c2ecf20Sopenharmony_ci /* returned values are 16 bit */ 5878c2ecf20Sopenharmony_ci if (sbs_data[reg_offset].min_value < 0) 5888c2ecf20Sopenharmony_ci ret = (s16)ret; 5898c2ecf20Sopenharmony_ci 5908c2ecf20Sopenharmony_ci if (ret >= sbs_data[reg_offset].min_value && 5918c2ecf20Sopenharmony_ci ret <= sbs_data[reg_offset].max_value) { 5928c2ecf20Sopenharmony_ci val->intval = ret; 5938c2ecf20Sopenharmony_ci if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) { 5948c2ecf20Sopenharmony_ci if (!(ret & BATTERY_INITIALIZED)) 5958c2ecf20Sopenharmony_ci val->intval = 5968c2ecf20Sopenharmony_ci POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; 5978c2ecf20Sopenharmony_ci else if (ret & BATTERY_FULL_CHARGED) 5988c2ecf20Sopenharmony_ci val->intval = 5998c2ecf20Sopenharmony_ci POWER_SUPPLY_CAPACITY_LEVEL_FULL; 6008c2ecf20Sopenharmony_ci else if (ret & BATTERY_FULL_DISCHARGED) 6018c2ecf20Sopenharmony_ci val->intval = 6028c2ecf20Sopenharmony_ci POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; 6038c2ecf20Sopenharmony_ci else 6048c2ecf20Sopenharmony_ci val->intval = 6058c2ecf20Sopenharmony_ci POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; 6068c2ecf20Sopenharmony_ci return 0; 6078c2ecf20Sopenharmony_ci } else if (psp != POWER_SUPPLY_PROP_STATUS) { 6088c2ecf20Sopenharmony_ci return 0; 6098c2ecf20Sopenharmony_ci } 6108c2ecf20Sopenharmony_ci 6118c2ecf20Sopenharmony_ci if (ret & BATTERY_FULL_CHARGED) 6128c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_FULL; 6138c2ecf20Sopenharmony_ci else if (ret & BATTERY_DISCHARGING) 6148c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 6158c2ecf20Sopenharmony_ci else 6168c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_CHARGING; 6178c2ecf20Sopenharmony_ci 6188c2ecf20Sopenharmony_ci sbs_status_correct(client, &val->intval); 6198c2ecf20Sopenharmony_ci 6208c2ecf20Sopenharmony_ci if (chip->poll_time == 0) 6218c2ecf20Sopenharmony_ci chip->last_state = val->intval; 6228c2ecf20Sopenharmony_ci else if (chip->last_state != val->intval) { 6238c2ecf20Sopenharmony_ci cancel_delayed_work_sync(&chip->work); 6248c2ecf20Sopenharmony_ci power_supply_changed(chip->power_supply); 6258c2ecf20Sopenharmony_ci chip->poll_time = 0; 6268c2ecf20Sopenharmony_ci } 6278c2ecf20Sopenharmony_ci } else { 6288c2ecf20Sopenharmony_ci if (psp == POWER_SUPPLY_PROP_STATUS) 6298c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_UNKNOWN; 6308c2ecf20Sopenharmony_ci else if (psp == POWER_SUPPLY_PROP_CAPACITY) 6318c2ecf20Sopenharmony_ci /* sbs spec says that this can be >100 % 6328c2ecf20Sopenharmony_ci * even if max value is 100 % 6338c2ecf20Sopenharmony_ci */ 6348c2ecf20Sopenharmony_ci val->intval = min(ret, 100); 6358c2ecf20Sopenharmony_ci else 6368c2ecf20Sopenharmony_ci val->intval = 0; 6378c2ecf20Sopenharmony_ci } 6388c2ecf20Sopenharmony_ci 6398c2ecf20Sopenharmony_ci return 0; 6408c2ecf20Sopenharmony_ci} 6418c2ecf20Sopenharmony_ci 6428c2ecf20Sopenharmony_cistatic int sbs_get_battery_string_property(struct i2c_client *client, 6438c2ecf20Sopenharmony_ci int reg_offset, enum power_supply_property psp, char *val) 6448c2ecf20Sopenharmony_ci{ 6458c2ecf20Sopenharmony_ci s32 ret; 6468c2ecf20Sopenharmony_ci 6478c2ecf20Sopenharmony_ci ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val); 6488c2ecf20Sopenharmony_ci 6498c2ecf20Sopenharmony_ci if (ret < 0) 6508c2ecf20Sopenharmony_ci return ret; 6518c2ecf20Sopenharmony_ci 6528c2ecf20Sopenharmony_ci return 0; 6538c2ecf20Sopenharmony_ci} 6548c2ecf20Sopenharmony_ci 6558c2ecf20Sopenharmony_cistatic void sbs_unit_adjustment(struct i2c_client *client, 6568c2ecf20Sopenharmony_ci enum power_supply_property psp, union power_supply_propval *val) 6578c2ecf20Sopenharmony_ci{ 6588c2ecf20Sopenharmony_ci#define BASE_UNIT_CONVERSION 1000 6598c2ecf20Sopenharmony_ci#define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION) 6608c2ecf20Sopenharmony_ci#define TIME_UNIT_CONVERSION 60 6618c2ecf20Sopenharmony_ci#define TEMP_KELVIN_TO_CELSIUS 2731 6628c2ecf20Sopenharmony_ci switch (psp) { 6638c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_ENERGY_NOW: 6648c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_ENERGY_FULL: 6658c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: 6668c2ecf20Sopenharmony_ci /* sbs provides energy in units of 10mWh. 6678c2ecf20Sopenharmony_ci * Convert to µWh 6688c2ecf20Sopenharmony_ci */ 6698c2ecf20Sopenharmony_ci val->intval *= BATTERY_MODE_CAP_MULT_WATT; 6708c2ecf20Sopenharmony_ci break; 6718c2ecf20Sopenharmony_ci 6728c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_VOLTAGE_NOW: 6738c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: 6748c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: 6758c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CURRENT_NOW: 6768c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CURRENT_AVG: 6778c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CHARGE_NOW: 6788c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: 6798c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: 6808c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CHARGE_FULL: 6818c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 6828c2ecf20Sopenharmony_ci val->intval *= BASE_UNIT_CONVERSION; 6838c2ecf20Sopenharmony_ci break; 6848c2ecf20Sopenharmony_ci 6858c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_TEMP: 6868c2ecf20Sopenharmony_ci /* sbs provides battery temperature in 0.1K 6878c2ecf20Sopenharmony_ci * so convert it to 0.1°C 6888c2ecf20Sopenharmony_ci */ 6898c2ecf20Sopenharmony_ci val->intval -= TEMP_KELVIN_TO_CELSIUS; 6908c2ecf20Sopenharmony_ci break; 6918c2ecf20Sopenharmony_ci 6928c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: 6938c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: 6948c2ecf20Sopenharmony_ci /* sbs provides time to empty and time to full in minutes. 6958c2ecf20Sopenharmony_ci * Convert to seconds 6968c2ecf20Sopenharmony_ci */ 6978c2ecf20Sopenharmony_ci val->intval *= TIME_UNIT_CONVERSION; 6988c2ecf20Sopenharmony_ci break; 6998c2ecf20Sopenharmony_ci 7008c2ecf20Sopenharmony_ci default: 7018c2ecf20Sopenharmony_ci dev_dbg(&client->dev, 7028c2ecf20Sopenharmony_ci "%s: no need for unit conversion %d\n", __func__, psp); 7038c2ecf20Sopenharmony_ci } 7048c2ecf20Sopenharmony_ci} 7058c2ecf20Sopenharmony_ci 7068c2ecf20Sopenharmony_cistatic enum sbs_capacity_mode sbs_set_capacity_mode(struct i2c_client *client, 7078c2ecf20Sopenharmony_ci enum sbs_capacity_mode mode) 7088c2ecf20Sopenharmony_ci{ 7098c2ecf20Sopenharmony_ci int ret, original_val; 7108c2ecf20Sopenharmony_ci 7118c2ecf20Sopenharmony_ci original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET); 7128c2ecf20Sopenharmony_ci if (original_val < 0) 7138c2ecf20Sopenharmony_ci return original_val; 7148c2ecf20Sopenharmony_ci 7158c2ecf20Sopenharmony_ci if ((original_val & BATTERY_MODE_CAPACITY_MASK) == mode) 7168c2ecf20Sopenharmony_ci return mode; 7178c2ecf20Sopenharmony_ci 7188c2ecf20Sopenharmony_ci if (mode == CAPACITY_MODE_AMPS) 7198c2ecf20Sopenharmony_ci ret = original_val & ~BATTERY_MODE_CAPACITY_MASK; 7208c2ecf20Sopenharmony_ci else 7218c2ecf20Sopenharmony_ci ret = original_val | BATTERY_MODE_CAPACITY_MASK; 7228c2ecf20Sopenharmony_ci 7238c2ecf20Sopenharmony_ci ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret); 7248c2ecf20Sopenharmony_ci if (ret < 0) 7258c2ecf20Sopenharmony_ci return ret; 7268c2ecf20Sopenharmony_ci 7278c2ecf20Sopenharmony_ci usleep_range(1000, 2000); 7288c2ecf20Sopenharmony_ci 7298c2ecf20Sopenharmony_ci return original_val & BATTERY_MODE_CAPACITY_MASK; 7308c2ecf20Sopenharmony_ci} 7318c2ecf20Sopenharmony_ci 7328c2ecf20Sopenharmony_cistatic int sbs_get_battery_capacity(struct i2c_client *client, 7338c2ecf20Sopenharmony_ci int reg_offset, enum power_supply_property psp, 7348c2ecf20Sopenharmony_ci union power_supply_propval *val) 7358c2ecf20Sopenharmony_ci{ 7368c2ecf20Sopenharmony_ci s32 ret; 7378c2ecf20Sopenharmony_ci enum sbs_capacity_mode mode = CAPACITY_MODE_WATTS; 7388c2ecf20Sopenharmony_ci 7398c2ecf20Sopenharmony_ci if (power_supply_is_amp_property(psp)) 7408c2ecf20Sopenharmony_ci mode = CAPACITY_MODE_AMPS; 7418c2ecf20Sopenharmony_ci 7428c2ecf20Sopenharmony_ci mode = sbs_set_capacity_mode(client, mode); 7438c2ecf20Sopenharmony_ci if ((int)mode < 0) 7448c2ecf20Sopenharmony_ci return mode; 7458c2ecf20Sopenharmony_ci 7468c2ecf20Sopenharmony_ci ret = sbs_read_word_data(client, sbs_data[reg_offset].addr); 7478c2ecf20Sopenharmony_ci if (ret < 0) 7488c2ecf20Sopenharmony_ci return ret; 7498c2ecf20Sopenharmony_ci 7508c2ecf20Sopenharmony_ci val->intval = ret; 7518c2ecf20Sopenharmony_ci 7528c2ecf20Sopenharmony_ci ret = sbs_set_capacity_mode(client, mode); 7538c2ecf20Sopenharmony_ci if (ret < 0) 7548c2ecf20Sopenharmony_ci return ret; 7558c2ecf20Sopenharmony_ci 7568c2ecf20Sopenharmony_ci return 0; 7578c2ecf20Sopenharmony_ci} 7588c2ecf20Sopenharmony_ci 7598c2ecf20Sopenharmony_cistatic char sbs_serial[5]; 7608c2ecf20Sopenharmony_cistatic int sbs_get_battery_serial_number(struct i2c_client *client, 7618c2ecf20Sopenharmony_ci union power_supply_propval *val) 7628c2ecf20Sopenharmony_ci{ 7638c2ecf20Sopenharmony_ci int ret; 7648c2ecf20Sopenharmony_ci 7658c2ecf20Sopenharmony_ci ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr); 7668c2ecf20Sopenharmony_ci if (ret < 0) 7678c2ecf20Sopenharmony_ci return ret; 7688c2ecf20Sopenharmony_ci 7698c2ecf20Sopenharmony_ci sprintf(sbs_serial, "%04x", ret); 7708c2ecf20Sopenharmony_ci val->strval = sbs_serial; 7718c2ecf20Sopenharmony_ci 7728c2ecf20Sopenharmony_ci return 0; 7738c2ecf20Sopenharmony_ci} 7748c2ecf20Sopenharmony_ci 7758c2ecf20Sopenharmony_cistatic int sbs_get_property_index(struct i2c_client *client, 7768c2ecf20Sopenharmony_ci enum power_supply_property psp) 7778c2ecf20Sopenharmony_ci{ 7788c2ecf20Sopenharmony_ci int count; 7798c2ecf20Sopenharmony_ci for (count = 0; count < ARRAY_SIZE(sbs_data); count++) 7808c2ecf20Sopenharmony_ci if (psp == sbs_data[count].psp) 7818c2ecf20Sopenharmony_ci return count; 7828c2ecf20Sopenharmony_ci 7838c2ecf20Sopenharmony_ci dev_warn(&client->dev, 7848c2ecf20Sopenharmony_ci "%s: Invalid Property - %d\n", __func__, psp); 7858c2ecf20Sopenharmony_ci 7868c2ecf20Sopenharmony_ci return -EINVAL; 7878c2ecf20Sopenharmony_ci} 7888c2ecf20Sopenharmony_ci 7898c2ecf20Sopenharmony_cistatic int sbs_get_chemistry(struct i2c_client *client, 7908c2ecf20Sopenharmony_ci union power_supply_propval *val) 7918c2ecf20Sopenharmony_ci{ 7928c2ecf20Sopenharmony_ci enum power_supply_property psp = POWER_SUPPLY_PROP_TECHNOLOGY; 7938c2ecf20Sopenharmony_ci int ret; 7948c2ecf20Sopenharmony_ci 7958c2ecf20Sopenharmony_ci ret = sbs_get_property_index(client, psp); 7968c2ecf20Sopenharmony_ci if (ret < 0) 7978c2ecf20Sopenharmony_ci return ret; 7988c2ecf20Sopenharmony_ci 7998c2ecf20Sopenharmony_ci ret = sbs_get_battery_string_property(client, ret, psp, 8008c2ecf20Sopenharmony_ci chemistry); 8018c2ecf20Sopenharmony_ci if (ret < 0) 8028c2ecf20Sopenharmony_ci return ret; 8038c2ecf20Sopenharmony_ci 8048c2ecf20Sopenharmony_ci if (!strncasecmp(chemistry, "LION", 4)) 8058c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_TECHNOLOGY_LION; 8068c2ecf20Sopenharmony_ci else if (!strncasecmp(chemistry, "LiP", 3)) 8078c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_TECHNOLOGY_LIPO; 8088c2ecf20Sopenharmony_ci else if (!strncasecmp(chemistry, "NiCd", 4)) 8098c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_TECHNOLOGY_NiCd; 8108c2ecf20Sopenharmony_ci else if (!strncasecmp(chemistry, "NiMH", 4)) 8118c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH; 8128c2ecf20Sopenharmony_ci else 8138c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN; 8148c2ecf20Sopenharmony_ci 8158c2ecf20Sopenharmony_ci if (val->intval == POWER_SUPPLY_TECHNOLOGY_UNKNOWN) 8168c2ecf20Sopenharmony_ci dev_warn(&client->dev, "Unknown chemistry: %s\n", chemistry); 8178c2ecf20Sopenharmony_ci 8188c2ecf20Sopenharmony_ci return 0; 8198c2ecf20Sopenharmony_ci} 8208c2ecf20Sopenharmony_ci 8218c2ecf20Sopenharmony_cistatic int sbs_get_battery_manufacture_date(struct i2c_client *client, 8228c2ecf20Sopenharmony_ci enum power_supply_property psp, 8238c2ecf20Sopenharmony_ci union power_supply_propval *val) 8248c2ecf20Sopenharmony_ci{ 8258c2ecf20Sopenharmony_ci int ret; 8268c2ecf20Sopenharmony_ci u16 day, month, year; 8278c2ecf20Sopenharmony_ci 8288c2ecf20Sopenharmony_ci ret = sbs_read_word_data(client, REG_ADDR_MANUFACTURE_DATE); 8298c2ecf20Sopenharmony_ci if (ret < 0) 8308c2ecf20Sopenharmony_ci return ret; 8318c2ecf20Sopenharmony_ci 8328c2ecf20Sopenharmony_ci day = ret & GENMASK(4, 0); 8338c2ecf20Sopenharmony_ci month = (ret & GENMASK(8, 5)) >> 5; 8348c2ecf20Sopenharmony_ci year = ((ret & GENMASK(15, 9)) >> 9) + 1980; 8358c2ecf20Sopenharmony_ci 8368c2ecf20Sopenharmony_ci switch (psp) { 8378c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_MANUFACTURE_YEAR: 8388c2ecf20Sopenharmony_ci val->intval = year; 8398c2ecf20Sopenharmony_ci break; 8408c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_MANUFACTURE_MONTH: 8418c2ecf20Sopenharmony_ci val->intval = month; 8428c2ecf20Sopenharmony_ci break; 8438c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_MANUFACTURE_DAY: 8448c2ecf20Sopenharmony_ci val->intval = day; 8458c2ecf20Sopenharmony_ci break; 8468c2ecf20Sopenharmony_ci default: 8478c2ecf20Sopenharmony_ci return -EINVAL; 8488c2ecf20Sopenharmony_ci } 8498c2ecf20Sopenharmony_ci 8508c2ecf20Sopenharmony_ci return 0; 8518c2ecf20Sopenharmony_ci} 8528c2ecf20Sopenharmony_ci 8538c2ecf20Sopenharmony_cistatic int sbs_get_property(struct power_supply *psy, 8548c2ecf20Sopenharmony_ci enum power_supply_property psp, 8558c2ecf20Sopenharmony_ci union power_supply_propval *val) 8568c2ecf20Sopenharmony_ci{ 8578c2ecf20Sopenharmony_ci int ret = 0; 8588c2ecf20Sopenharmony_ci struct sbs_info *chip = power_supply_get_drvdata(psy); 8598c2ecf20Sopenharmony_ci struct i2c_client *client = chip->client; 8608c2ecf20Sopenharmony_ci 8618c2ecf20Sopenharmony_ci if (chip->gpio_detect) { 8628c2ecf20Sopenharmony_ci ret = gpiod_get_value_cansleep(chip->gpio_detect); 8638c2ecf20Sopenharmony_ci if (ret < 0) 8648c2ecf20Sopenharmony_ci return ret; 8658c2ecf20Sopenharmony_ci if (psp == POWER_SUPPLY_PROP_PRESENT) { 8668c2ecf20Sopenharmony_ci val->intval = ret; 8678c2ecf20Sopenharmony_ci sbs_update_presence(chip, ret); 8688c2ecf20Sopenharmony_ci return 0; 8698c2ecf20Sopenharmony_ci } 8708c2ecf20Sopenharmony_ci if (ret == 0) 8718c2ecf20Sopenharmony_ci return -ENODATA; 8728c2ecf20Sopenharmony_ci } 8738c2ecf20Sopenharmony_ci 8748c2ecf20Sopenharmony_ci switch (psp) { 8758c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_PRESENT: 8768c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_HEALTH: 8778c2ecf20Sopenharmony_ci ret = sbs_get_battery_presence_and_health(client, psp, val); 8788c2ecf20Sopenharmony_ci 8798c2ecf20Sopenharmony_ci /* this can only be true if no gpio is used */ 8808c2ecf20Sopenharmony_ci if (psp == POWER_SUPPLY_PROP_PRESENT) 8818c2ecf20Sopenharmony_ci return 0; 8828c2ecf20Sopenharmony_ci break; 8838c2ecf20Sopenharmony_ci 8848c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_TECHNOLOGY: 8858c2ecf20Sopenharmony_ci ret = sbs_get_chemistry(client, val); 8868c2ecf20Sopenharmony_ci if (ret < 0) 8878c2ecf20Sopenharmony_ci break; 8888c2ecf20Sopenharmony_ci 8898c2ecf20Sopenharmony_ci goto done; /* don't trigger power_supply_changed()! */ 8908c2ecf20Sopenharmony_ci 8918c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_ENERGY_NOW: 8928c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_ENERGY_FULL: 8938c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: 8948c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CHARGE_NOW: 8958c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CHARGE_FULL: 8968c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 8978c2ecf20Sopenharmony_ci ret = sbs_get_property_index(client, psp); 8988c2ecf20Sopenharmony_ci if (ret < 0) 8998c2ecf20Sopenharmony_ci break; 9008c2ecf20Sopenharmony_ci 9018c2ecf20Sopenharmony_ci /* sbs_get_battery_capacity() will change the battery mode 9028c2ecf20Sopenharmony_ci * temporarily to read the requested attribute. Ensure we stay 9038c2ecf20Sopenharmony_ci * in the desired mode for the duration of the attribute read. 9048c2ecf20Sopenharmony_ci */ 9058c2ecf20Sopenharmony_ci mutex_lock(&chip->mode_lock); 9068c2ecf20Sopenharmony_ci ret = sbs_get_battery_capacity(client, ret, psp, val); 9078c2ecf20Sopenharmony_ci mutex_unlock(&chip->mode_lock); 9088c2ecf20Sopenharmony_ci break; 9098c2ecf20Sopenharmony_ci 9108c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_SERIAL_NUMBER: 9118c2ecf20Sopenharmony_ci ret = sbs_get_battery_serial_number(client, val); 9128c2ecf20Sopenharmony_ci break; 9138c2ecf20Sopenharmony_ci 9148c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_STATUS: 9158c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CAPACITY_LEVEL: 9168c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CYCLE_COUNT: 9178c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_VOLTAGE_NOW: 9188c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CURRENT_NOW: 9198c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CURRENT_AVG: 9208c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_TEMP: 9218c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: 9228c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: 9238c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: 9248c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: 9258c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: 9268c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: 9278c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CAPACITY: 9288c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN: 9298c2ecf20Sopenharmony_ci ret = sbs_get_property_index(client, psp); 9308c2ecf20Sopenharmony_ci if (ret < 0) 9318c2ecf20Sopenharmony_ci break; 9328c2ecf20Sopenharmony_ci 9338c2ecf20Sopenharmony_ci ret = sbs_get_battery_property(client, ret, psp, val); 9348c2ecf20Sopenharmony_ci break; 9358c2ecf20Sopenharmony_ci 9368c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_MODEL_NAME: 9378c2ecf20Sopenharmony_ci ret = sbs_get_property_index(client, psp); 9388c2ecf20Sopenharmony_ci if (ret < 0) 9398c2ecf20Sopenharmony_ci break; 9408c2ecf20Sopenharmony_ci 9418c2ecf20Sopenharmony_ci ret = sbs_get_battery_string_property(client, ret, psp, 9428c2ecf20Sopenharmony_ci model_name); 9438c2ecf20Sopenharmony_ci val->strval = model_name; 9448c2ecf20Sopenharmony_ci break; 9458c2ecf20Sopenharmony_ci 9468c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_MANUFACTURER: 9478c2ecf20Sopenharmony_ci ret = sbs_get_property_index(client, psp); 9488c2ecf20Sopenharmony_ci if (ret < 0) 9498c2ecf20Sopenharmony_ci break; 9508c2ecf20Sopenharmony_ci 9518c2ecf20Sopenharmony_ci ret = sbs_get_battery_string_property(client, ret, psp, 9528c2ecf20Sopenharmony_ci manufacturer); 9538c2ecf20Sopenharmony_ci val->strval = manufacturer; 9548c2ecf20Sopenharmony_ci break; 9558c2ecf20Sopenharmony_ci 9568c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_MANUFACTURE_YEAR: 9578c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_MANUFACTURE_MONTH: 9588c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_MANUFACTURE_DAY: 9598c2ecf20Sopenharmony_ci ret = sbs_get_battery_manufacture_date(client, psp, val); 9608c2ecf20Sopenharmony_ci break; 9618c2ecf20Sopenharmony_ci 9628c2ecf20Sopenharmony_ci default: 9638c2ecf20Sopenharmony_ci dev_err(&client->dev, 9648c2ecf20Sopenharmony_ci "%s: INVALID property\n", __func__); 9658c2ecf20Sopenharmony_ci return -EINVAL; 9668c2ecf20Sopenharmony_ci } 9678c2ecf20Sopenharmony_ci 9688c2ecf20Sopenharmony_ci if (!chip->gpio_detect && chip->is_present != (ret >= 0)) { 9698c2ecf20Sopenharmony_ci bool old_present = chip->is_present; 9708c2ecf20Sopenharmony_ci union power_supply_propval val; 9718c2ecf20Sopenharmony_ci int err = sbs_get_battery_presence_and_health( 9728c2ecf20Sopenharmony_ci client, POWER_SUPPLY_PROP_PRESENT, &val); 9738c2ecf20Sopenharmony_ci 9748c2ecf20Sopenharmony_ci sbs_update_presence(chip, !err && val.intval); 9758c2ecf20Sopenharmony_ci 9768c2ecf20Sopenharmony_ci if (old_present != chip->is_present) 9778c2ecf20Sopenharmony_ci power_supply_changed(chip->power_supply); 9788c2ecf20Sopenharmony_ci } 9798c2ecf20Sopenharmony_ci 9808c2ecf20Sopenharmony_cidone: 9818c2ecf20Sopenharmony_ci if (!ret) { 9828c2ecf20Sopenharmony_ci /* Convert units to match requirements for power supply class */ 9838c2ecf20Sopenharmony_ci sbs_unit_adjustment(client, psp, val); 9848c2ecf20Sopenharmony_ci dev_dbg(&client->dev, 9858c2ecf20Sopenharmony_ci "%s: property = %d, value = %x\n", __func__, 9868c2ecf20Sopenharmony_ci psp, val->intval); 9878c2ecf20Sopenharmony_ci } else if (!chip->is_present) { 9888c2ecf20Sopenharmony_ci /* battery not present, so return NODATA for properties */ 9898c2ecf20Sopenharmony_ci ret = -ENODATA; 9908c2ecf20Sopenharmony_ci } 9918c2ecf20Sopenharmony_ci return ret; 9928c2ecf20Sopenharmony_ci} 9938c2ecf20Sopenharmony_ci 9948c2ecf20Sopenharmony_cistatic void sbs_supply_changed(struct sbs_info *chip) 9958c2ecf20Sopenharmony_ci{ 9968c2ecf20Sopenharmony_ci struct power_supply *battery = chip->power_supply; 9978c2ecf20Sopenharmony_ci int ret; 9988c2ecf20Sopenharmony_ci 9998c2ecf20Sopenharmony_ci ret = gpiod_get_value_cansleep(chip->gpio_detect); 10008c2ecf20Sopenharmony_ci if (ret < 0) 10018c2ecf20Sopenharmony_ci return; 10028c2ecf20Sopenharmony_ci sbs_update_presence(chip, ret); 10038c2ecf20Sopenharmony_ci power_supply_changed(battery); 10048c2ecf20Sopenharmony_ci} 10058c2ecf20Sopenharmony_ci 10068c2ecf20Sopenharmony_cistatic irqreturn_t sbs_irq(int irq, void *devid) 10078c2ecf20Sopenharmony_ci{ 10088c2ecf20Sopenharmony_ci sbs_supply_changed(devid); 10098c2ecf20Sopenharmony_ci return IRQ_HANDLED; 10108c2ecf20Sopenharmony_ci} 10118c2ecf20Sopenharmony_ci 10128c2ecf20Sopenharmony_cistatic void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot, 10138c2ecf20Sopenharmony_ci unsigned int data) 10148c2ecf20Sopenharmony_ci{ 10158c2ecf20Sopenharmony_ci sbs_supply_changed(i2c_get_clientdata(client)); 10168c2ecf20Sopenharmony_ci} 10178c2ecf20Sopenharmony_ci 10188c2ecf20Sopenharmony_cistatic void sbs_external_power_changed(struct power_supply *psy) 10198c2ecf20Sopenharmony_ci{ 10208c2ecf20Sopenharmony_ci struct sbs_info *chip = power_supply_get_drvdata(psy); 10218c2ecf20Sopenharmony_ci 10228c2ecf20Sopenharmony_ci /* cancel outstanding work */ 10238c2ecf20Sopenharmony_ci cancel_delayed_work_sync(&chip->work); 10248c2ecf20Sopenharmony_ci 10258c2ecf20Sopenharmony_ci schedule_delayed_work(&chip->work, HZ); 10268c2ecf20Sopenharmony_ci chip->poll_time = chip->poll_retry_count; 10278c2ecf20Sopenharmony_ci} 10288c2ecf20Sopenharmony_ci 10298c2ecf20Sopenharmony_cistatic void sbs_delayed_work(struct work_struct *work) 10308c2ecf20Sopenharmony_ci{ 10318c2ecf20Sopenharmony_ci struct sbs_info *chip; 10328c2ecf20Sopenharmony_ci s32 ret; 10338c2ecf20Sopenharmony_ci 10348c2ecf20Sopenharmony_ci chip = container_of(work, struct sbs_info, work.work); 10358c2ecf20Sopenharmony_ci 10368c2ecf20Sopenharmony_ci ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr); 10378c2ecf20Sopenharmony_ci /* if the read failed, give up on this work */ 10388c2ecf20Sopenharmony_ci if (ret < 0) { 10398c2ecf20Sopenharmony_ci chip->poll_time = 0; 10408c2ecf20Sopenharmony_ci return; 10418c2ecf20Sopenharmony_ci } 10428c2ecf20Sopenharmony_ci 10438c2ecf20Sopenharmony_ci if (ret & BATTERY_FULL_CHARGED) 10448c2ecf20Sopenharmony_ci ret = POWER_SUPPLY_STATUS_FULL; 10458c2ecf20Sopenharmony_ci else if (ret & BATTERY_DISCHARGING) 10468c2ecf20Sopenharmony_ci ret = POWER_SUPPLY_STATUS_DISCHARGING; 10478c2ecf20Sopenharmony_ci else 10488c2ecf20Sopenharmony_ci ret = POWER_SUPPLY_STATUS_CHARGING; 10498c2ecf20Sopenharmony_ci 10508c2ecf20Sopenharmony_ci sbs_status_correct(chip->client, &ret); 10518c2ecf20Sopenharmony_ci 10528c2ecf20Sopenharmony_ci if (chip->last_state != ret) { 10538c2ecf20Sopenharmony_ci chip->poll_time = 0; 10548c2ecf20Sopenharmony_ci power_supply_changed(chip->power_supply); 10558c2ecf20Sopenharmony_ci return; 10568c2ecf20Sopenharmony_ci } 10578c2ecf20Sopenharmony_ci if (chip->poll_time > 0) { 10588c2ecf20Sopenharmony_ci schedule_delayed_work(&chip->work, HZ); 10598c2ecf20Sopenharmony_ci chip->poll_time--; 10608c2ecf20Sopenharmony_ci return; 10618c2ecf20Sopenharmony_ci } 10628c2ecf20Sopenharmony_ci} 10638c2ecf20Sopenharmony_ci 10648c2ecf20Sopenharmony_cistatic const struct power_supply_desc sbs_default_desc = { 10658c2ecf20Sopenharmony_ci .type = POWER_SUPPLY_TYPE_BATTERY, 10668c2ecf20Sopenharmony_ci .properties = sbs_properties, 10678c2ecf20Sopenharmony_ci .num_properties = ARRAY_SIZE(sbs_properties), 10688c2ecf20Sopenharmony_ci .get_property = sbs_get_property, 10698c2ecf20Sopenharmony_ci .external_power_changed = sbs_external_power_changed, 10708c2ecf20Sopenharmony_ci}; 10718c2ecf20Sopenharmony_ci 10728c2ecf20Sopenharmony_cistatic int sbs_probe(struct i2c_client *client) 10738c2ecf20Sopenharmony_ci{ 10748c2ecf20Sopenharmony_ci struct sbs_info *chip; 10758c2ecf20Sopenharmony_ci struct power_supply_desc *sbs_desc; 10768c2ecf20Sopenharmony_ci struct sbs_platform_data *pdata = client->dev.platform_data; 10778c2ecf20Sopenharmony_ci struct power_supply_config psy_cfg = {}; 10788c2ecf20Sopenharmony_ci int rc; 10798c2ecf20Sopenharmony_ci int irq; 10808c2ecf20Sopenharmony_ci 10818c2ecf20Sopenharmony_ci sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc, 10828c2ecf20Sopenharmony_ci sizeof(*sbs_desc), GFP_KERNEL); 10838c2ecf20Sopenharmony_ci if (!sbs_desc) 10848c2ecf20Sopenharmony_ci return -ENOMEM; 10858c2ecf20Sopenharmony_ci 10868c2ecf20Sopenharmony_ci sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s", 10878c2ecf20Sopenharmony_ci dev_name(&client->dev)); 10888c2ecf20Sopenharmony_ci if (!sbs_desc->name) 10898c2ecf20Sopenharmony_ci return -ENOMEM; 10908c2ecf20Sopenharmony_ci 10918c2ecf20Sopenharmony_ci chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL); 10928c2ecf20Sopenharmony_ci if (!chip) 10938c2ecf20Sopenharmony_ci return -ENOMEM; 10948c2ecf20Sopenharmony_ci 10958c2ecf20Sopenharmony_ci chip->flags = (u32)(uintptr_t)device_get_match_data(&client->dev); 10968c2ecf20Sopenharmony_ci chip->client = client; 10978c2ecf20Sopenharmony_ci psy_cfg.of_node = client->dev.of_node; 10988c2ecf20Sopenharmony_ci psy_cfg.drv_data = chip; 10998c2ecf20Sopenharmony_ci chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN; 11008c2ecf20Sopenharmony_ci mutex_init(&chip->mode_lock); 11018c2ecf20Sopenharmony_ci 11028c2ecf20Sopenharmony_ci /* use pdata if available, fall back to DT properties, 11038c2ecf20Sopenharmony_ci * or hardcoded defaults if not 11048c2ecf20Sopenharmony_ci */ 11058c2ecf20Sopenharmony_ci rc = device_property_read_u32(&client->dev, "sbs,i2c-retry-count", 11068c2ecf20Sopenharmony_ci &chip->i2c_retry_count); 11078c2ecf20Sopenharmony_ci if (rc) 11088c2ecf20Sopenharmony_ci chip->i2c_retry_count = 0; 11098c2ecf20Sopenharmony_ci 11108c2ecf20Sopenharmony_ci rc = device_property_read_u32(&client->dev, "sbs,poll-retry-count", 11118c2ecf20Sopenharmony_ci &chip->poll_retry_count); 11128c2ecf20Sopenharmony_ci if (rc) 11138c2ecf20Sopenharmony_ci chip->poll_retry_count = 0; 11148c2ecf20Sopenharmony_ci 11158c2ecf20Sopenharmony_ci if (pdata) { 11168c2ecf20Sopenharmony_ci chip->poll_retry_count = pdata->poll_retry_count; 11178c2ecf20Sopenharmony_ci chip->i2c_retry_count = pdata->i2c_retry_count; 11188c2ecf20Sopenharmony_ci } 11198c2ecf20Sopenharmony_ci chip->i2c_retry_count = chip->i2c_retry_count + 1; 11208c2ecf20Sopenharmony_ci 11218c2ecf20Sopenharmony_ci chip->charger_broadcasts = !device_property_read_bool(&client->dev, 11228c2ecf20Sopenharmony_ci "sbs,disable-charger-broadcasts"); 11238c2ecf20Sopenharmony_ci 11248c2ecf20Sopenharmony_ci chip->gpio_detect = devm_gpiod_get_optional(&client->dev, 11258c2ecf20Sopenharmony_ci "sbs,battery-detect", GPIOD_IN); 11268c2ecf20Sopenharmony_ci if (IS_ERR(chip->gpio_detect)) { 11278c2ecf20Sopenharmony_ci dev_err(&client->dev, "Failed to get gpio: %ld\n", 11288c2ecf20Sopenharmony_ci PTR_ERR(chip->gpio_detect)); 11298c2ecf20Sopenharmony_ci return PTR_ERR(chip->gpio_detect); 11308c2ecf20Sopenharmony_ci } 11318c2ecf20Sopenharmony_ci 11328c2ecf20Sopenharmony_ci i2c_set_clientdata(client, chip); 11338c2ecf20Sopenharmony_ci 11348c2ecf20Sopenharmony_ci if (!chip->gpio_detect) 11358c2ecf20Sopenharmony_ci goto skip_gpio; 11368c2ecf20Sopenharmony_ci 11378c2ecf20Sopenharmony_ci irq = gpiod_to_irq(chip->gpio_detect); 11388c2ecf20Sopenharmony_ci if (irq <= 0) { 11398c2ecf20Sopenharmony_ci dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq); 11408c2ecf20Sopenharmony_ci goto skip_gpio; 11418c2ecf20Sopenharmony_ci } 11428c2ecf20Sopenharmony_ci 11438c2ecf20Sopenharmony_ci rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq, 11448c2ecf20Sopenharmony_ci IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 11458c2ecf20Sopenharmony_ci dev_name(&client->dev), chip); 11468c2ecf20Sopenharmony_ci if (rc) { 11478c2ecf20Sopenharmony_ci dev_warn(&client->dev, "Failed to request irq: %d\n", rc); 11488c2ecf20Sopenharmony_ci goto skip_gpio; 11498c2ecf20Sopenharmony_ci } 11508c2ecf20Sopenharmony_ci 11518c2ecf20Sopenharmony_ciskip_gpio: 11528c2ecf20Sopenharmony_ci /* 11538c2ecf20Sopenharmony_ci * Before we register, we might need to make sure we can actually talk 11548c2ecf20Sopenharmony_ci * to the battery. 11558c2ecf20Sopenharmony_ci */ 11568c2ecf20Sopenharmony_ci if (!(force_load || chip->gpio_detect)) { 11578c2ecf20Sopenharmony_ci union power_supply_propval val; 11588c2ecf20Sopenharmony_ci 11598c2ecf20Sopenharmony_ci rc = sbs_get_battery_presence_and_health( 11608c2ecf20Sopenharmony_ci client, POWER_SUPPLY_PROP_PRESENT, &val); 11618c2ecf20Sopenharmony_ci if (rc < 0 || !val.intval) { 11628c2ecf20Sopenharmony_ci dev_err(&client->dev, "Failed to get present status\n"); 11638c2ecf20Sopenharmony_ci rc = -ENODEV; 11648c2ecf20Sopenharmony_ci goto exit_psupply; 11658c2ecf20Sopenharmony_ci } 11668c2ecf20Sopenharmony_ci } 11678c2ecf20Sopenharmony_ci 11688c2ecf20Sopenharmony_ci INIT_DELAYED_WORK(&chip->work, sbs_delayed_work); 11698c2ecf20Sopenharmony_ci 11708c2ecf20Sopenharmony_ci chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc, 11718c2ecf20Sopenharmony_ci &psy_cfg); 11728c2ecf20Sopenharmony_ci if (IS_ERR(chip->power_supply)) { 11738c2ecf20Sopenharmony_ci dev_err(&client->dev, 11748c2ecf20Sopenharmony_ci "%s: Failed to register power supply\n", __func__); 11758c2ecf20Sopenharmony_ci rc = PTR_ERR(chip->power_supply); 11768c2ecf20Sopenharmony_ci goto exit_psupply; 11778c2ecf20Sopenharmony_ci } 11788c2ecf20Sopenharmony_ci 11798c2ecf20Sopenharmony_ci dev_info(&client->dev, 11808c2ecf20Sopenharmony_ci "%s: battery gas gauge device registered\n", client->name); 11818c2ecf20Sopenharmony_ci 11828c2ecf20Sopenharmony_ci return 0; 11838c2ecf20Sopenharmony_ci 11848c2ecf20Sopenharmony_ciexit_psupply: 11858c2ecf20Sopenharmony_ci return rc; 11868c2ecf20Sopenharmony_ci} 11878c2ecf20Sopenharmony_ci 11888c2ecf20Sopenharmony_cistatic int sbs_remove(struct i2c_client *client) 11898c2ecf20Sopenharmony_ci{ 11908c2ecf20Sopenharmony_ci struct sbs_info *chip = i2c_get_clientdata(client); 11918c2ecf20Sopenharmony_ci 11928c2ecf20Sopenharmony_ci cancel_delayed_work_sync(&chip->work); 11938c2ecf20Sopenharmony_ci 11948c2ecf20Sopenharmony_ci return 0; 11958c2ecf20Sopenharmony_ci} 11968c2ecf20Sopenharmony_ci 11978c2ecf20Sopenharmony_ci#if defined CONFIG_PM_SLEEP 11988c2ecf20Sopenharmony_ci 11998c2ecf20Sopenharmony_cistatic int sbs_suspend(struct device *dev) 12008c2ecf20Sopenharmony_ci{ 12018c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 12028c2ecf20Sopenharmony_ci struct sbs_info *chip = i2c_get_clientdata(client); 12038c2ecf20Sopenharmony_ci int ret; 12048c2ecf20Sopenharmony_ci 12058c2ecf20Sopenharmony_ci if (chip->poll_time > 0) 12068c2ecf20Sopenharmony_ci cancel_delayed_work_sync(&chip->work); 12078c2ecf20Sopenharmony_ci 12088c2ecf20Sopenharmony_ci if (chip->flags & SBS_FLAGS_TI_BQ20ZX5) { 12098c2ecf20Sopenharmony_ci /* Write to manufacturer access with sleep command. */ 12108c2ecf20Sopenharmony_ci ret = sbs_write_word_data(client, 12118c2ecf20Sopenharmony_ci sbs_data[REG_MANUFACTURER_DATA].addr, 12128c2ecf20Sopenharmony_ci MANUFACTURER_ACCESS_SLEEP); 12138c2ecf20Sopenharmony_ci if (chip->is_present && ret < 0) 12148c2ecf20Sopenharmony_ci return ret; 12158c2ecf20Sopenharmony_ci } 12168c2ecf20Sopenharmony_ci 12178c2ecf20Sopenharmony_ci return 0; 12188c2ecf20Sopenharmony_ci} 12198c2ecf20Sopenharmony_ci 12208c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL); 12218c2ecf20Sopenharmony_ci#define SBS_PM_OPS (&sbs_pm_ops) 12228c2ecf20Sopenharmony_ci 12238c2ecf20Sopenharmony_ci#else 12248c2ecf20Sopenharmony_ci#define SBS_PM_OPS NULL 12258c2ecf20Sopenharmony_ci#endif 12268c2ecf20Sopenharmony_ci 12278c2ecf20Sopenharmony_cistatic const struct i2c_device_id sbs_id[] = { 12288c2ecf20Sopenharmony_ci { "bq20z65", 0 }, 12298c2ecf20Sopenharmony_ci { "bq20z75", 0 }, 12308c2ecf20Sopenharmony_ci { "sbs-battery", 1 }, 12318c2ecf20Sopenharmony_ci {} 12328c2ecf20Sopenharmony_ci}; 12338c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, sbs_id); 12348c2ecf20Sopenharmony_ci 12358c2ecf20Sopenharmony_cistatic const struct of_device_id sbs_dt_ids[] = { 12368c2ecf20Sopenharmony_ci { .compatible = "sbs,sbs-battery" }, 12378c2ecf20Sopenharmony_ci { 12388c2ecf20Sopenharmony_ci .compatible = "ti,bq20z65", 12398c2ecf20Sopenharmony_ci .data = (void *)SBS_FLAGS_TI_BQ20ZX5, 12408c2ecf20Sopenharmony_ci }, 12418c2ecf20Sopenharmony_ci { 12428c2ecf20Sopenharmony_ci .compatible = "ti,bq20z75", 12438c2ecf20Sopenharmony_ci .data = (void *)SBS_FLAGS_TI_BQ20ZX5, 12448c2ecf20Sopenharmony_ci }, 12458c2ecf20Sopenharmony_ci { } 12468c2ecf20Sopenharmony_ci}; 12478c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, sbs_dt_ids); 12488c2ecf20Sopenharmony_ci 12498c2ecf20Sopenharmony_cistatic struct i2c_driver sbs_battery_driver = { 12508c2ecf20Sopenharmony_ci .probe_new = sbs_probe, 12518c2ecf20Sopenharmony_ci .remove = sbs_remove, 12528c2ecf20Sopenharmony_ci .alert = sbs_alert, 12538c2ecf20Sopenharmony_ci .id_table = sbs_id, 12548c2ecf20Sopenharmony_ci .driver = { 12558c2ecf20Sopenharmony_ci .name = "sbs-battery", 12568c2ecf20Sopenharmony_ci .of_match_table = sbs_dt_ids, 12578c2ecf20Sopenharmony_ci .pm = SBS_PM_OPS, 12588c2ecf20Sopenharmony_ci }, 12598c2ecf20Sopenharmony_ci}; 12608c2ecf20Sopenharmony_cimodule_i2c_driver(sbs_battery_driver); 12618c2ecf20Sopenharmony_ci 12628c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("SBS battery monitor driver"); 12638c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 12648c2ecf20Sopenharmony_ci 12658c2ecf20Sopenharmony_cimodule_param(force_load, bool, 0444); 12668c2ecf20Sopenharmony_ciMODULE_PARM_DESC(force_load, 12678c2ecf20Sopenharmony_ci "Attempt to load the driver even if no battery is connected"); 1268