18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Fuel gauge driver for CellWise 2013 / 2015 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2012, RockChip 68c2ecf20Sopenharmony_ci * Copyright (C) 2020, Tobias Schramm 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Authors: xuhuicong <xhc@rock-chips.com> 98c2ecf20Sopenharmony_ci * Authors: Tobias Schramm <t.schramm@manjaro.org> 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/bits.h> 138c2ecf20Sopenharmony_ci#include <linux/delay.h> 148c2ecf20Sopenharmony_ci#include <linux/i2c.h> 158c2ecf20Sopenharmony_ci#include <linux/gfp.h> 168c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h> 178c2ecf20Sopenharmony_ci#include <linux/kernel.h> 188c2ecf20Sopenharmony_ci#include <linux/module.h> 198c2ecf20Sopenharmony_ci#include <linux/power_supply.h> 208c2ecf20Sopenharmony_ci#include <linux/property.h> 218c2ecf20Sopenharmony_ci#include <linux/regmap.h> 228c2ecf20Sopenharmony_ci#include <linux/time.h> 238c2ecf20Sopenharmony_ci#include <linux/workqueue.h> 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#define CW2015_SIZE_BATINFO 64 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#define CW2015_RESET_TRIES 5 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci#define CW2015_REG_VERSION 0x00 308c2ecf20Sopenharmony_ci#define CW2015_REG_VCELL 0x02 318c2ecf20Sopenharmony_ci#define CW2015_REG_SOC 0x04 328c2ecf20Sopenharmony_ci#define CW2015_REG_RRT_ALERT 0x06 338c2ecf20Sopenharmony_ci#define CW2015_REG_CONFIG 0x08 348c2ecf20Sopenharmony_ci#define CW2015_REG_MODE 0x0A 358c2ecf20Sopenharmony_ci#define CW2015_REG_BATINFO 0x10 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci#define CW2015_MODE_SLEEP_MASK GENMASK(7, 6) 388c2ecf20Sopenharmony_ci#define CW2015_MODE_SLEEP (0x03 << 6) 398c2ecf20Sopenharmony_ci#define CW2015_MODE_NORMAL (0x00 << 6) 408c2ecf20Sopenharmony_ci#define CW2015_MODE_QUICK_START (0x03 << 4) 418c2ecf20Sopenharmony_ci#define CW2015_MODE_RESTART (0x0f << 0) 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci#define CW2015_CONFIG_UPDATE_FLG (0x01 << 1) 448c2ecf20Sopenharmony_ci#define CW2015_ATHD(x) ((x) << 3) 458c2ecf20Sopenharmony_ci#define CW2015_MASK_ATHD GENMASK(7, 3) 468c2ecf20Sopenharmony_ci#define CW2015_MASK_SOC GENMASK(12, 0) 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci/* reset gauge of no valid state of charge could be polled for 40s */ 498c2ecf20Sopenharmony_ci#define CW2015_BAT_SOC_ERROR_MS (40 * MSEC_PER_SEC) 508c2ecf20Sopenharmony_ci/* reset gauge if state of charge stuck for half an hour during charging */ 518c2ecf20Sopenharmony_ci#define CW2015_BAT_CHARGING_STUCK_MS (1800 * MSEC_PER_SEC) 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci/* poll interval from CellWise GPL Android driver example */ 548c2ecf20Sopenharmony_ci#define CW2015_DEFAULT_POLL_INTERVAL_MS 8000 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci#define CW2015_AVERAGING_SAMPLES 3 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cistruct cw_battery { 598c2ecf20Sopenharmony_ci struct device *dev; 608c2ecf20Sopenharmony_ci struct workqueue_struct *battery_workqueue; 618c2ecf20Sopenharmony_ci struct delayed_work battery_delay_work; 628c2ecf20Sopenharmony_ci struct regmap *regmap; 638c2ecf20Sopenharmony_ci struct power_supply *rk_bat; 648c2ecf20Sopenharmony_ci struct power_supply_battery_info battery; 658c2ecf20Sopenharmony_ci u8 *bat_profile; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci bool charger_attached; 688c2ecf20Sopenharmony_ci bool battery_changed; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci int soc; 718c2ecf20Sopenharmony_ci int voltage_mv; 728c2ecf20Sopenharmony_ci int status; 738c2ecf20Sopenharmony_ci int time_to_empty; 748c2ecf20Sopenharmony_ci int charge_count; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci u32 poll_interval_ms; 778c2ecf20Sopenharmony_ci u8 alert_level; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci unsigned int read_errors; 808c2ecf20Sopenharmony_ci unsigned int charge_stuck_cnt; 818c2ecf20Sopenharmony_ci}; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_cistatic int cw_read_word(struct cw_battery *cw_bat, u8 reg, u16 *val) 848c2ecf20Sopenharmony_ci{ 858c2ecf20Sopenharmony_ci __be16 value; 868c2ecf20Sopenharmony_ci int ret; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci ret = regmap_bulk_read(cw_bat->regmap, reg, &value, sizeof(value)); 898c2ecf20Sopenharmony_ci if (ret) 908c2ecf20Sopenharmony_ci return ret; 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci *val = be16_to_cpu(value); 938c2ecf20Sopenharmony_ci return 0; 948c2ecf20Sopenharmony_ci} 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_cistatic int cw_update_profile(struct cw_battery *cw_bat) 978c2ecf20Sopenharmony_ci{ 988c2ecf20Sopenharmony_ci int ret; 998c2ecf20Sopenharmony_ci unsigned int reg_val; 1008c2ecf20Sopenharmony_ci u8 reset_val; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci /* make sure gauge is not in sleep mode */ 1038c2ecf20Sopenharmony_ci ret = regmap_read(cw_bat->regmap, CW2015_REG_MODE, ®_val); 1048c2ecf20Sopenharmony_ci if (ret) 1058c2ecf20Sopenharmony_ci return ret; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci reset_val = reg_val; 1088c2ecf20Sopenharmony_ci if ((reg_val & CW2015_MODE_SLEEP_MASK) == CW2015_MODE_SLEEP) { 1098c2ecf20Sopenharmony_ci dev_err(cw_bat->dev, 1108c2ecf20Sopenharmony_ci "Gauge is in sleep mode, can't update battery info\n"); 1118c2ecf20Sopenharmony_ci return -EINVAL; 1128c2ecf20Sopenharmony_ci } 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci /* write new battery info */ 1158c2ecf20Sopenharmony_ci ret = regmap_raw_write(cw_bat->regmap, CW2015_REG_BATINFO, 1168c2ecf20Sopenharmony_ci cw_bat->bat_profile, 1178c2ecf20Sopenharmony_ci CW2015_SIZE_BATINFO); 1188c2ecf20Sopenharmony_ci if (ret) 1198c2ecf20Sopenharmony_ci return ret; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci /* set config update flag */ 1228c2ecf20Sopenharmony_ci reg_val |= CW2015_CONFIG_UPDATE_FLG; 1238c2ecf20Sopenharmony_ci reg_val &= ~CW2015_MASK_ATHD; 1248c2ecf20Sopenharmony_ci reg_val |= CW2015_ATHD(cw_bat->alert_level); 1258c2ecf20Sopenharmony_ci ret = regmap_write(cw_bat->regmap, CW2015_REG_CONFIG, reg_val); 1268c2ecf20Sopenharmony_ci if (ret) 1278c2ecf20Sopenharmony_ci return ret; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci /* reset gauge to apply new battery profile */ 1308c2ecf20Sopenharmony_ci reset_val &= ~CW2015_MODE_RESTART; 1318c2ecf20Sopenharmony_ci reg_val = reset_val | CW2015_MODE_RESTART; 1328c2ecf20Sopenharmony_ci ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reg_val); 1338c2ecf20Sopenharmony_ci if (ret) 1348c2ecf20Sopenharmony_ci return ret; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci /* wait for gauge to reset */ 1378c2ecf20Sopenharmony_ci msleep(20); 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci /* clear reset flag */ 1408c2ecf20Sopenharmony_ci ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val); 1418c2ecf20Sopenharmony_ci if (ret) 1428c2ecf20Sopenharmony_ci return ret; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci /* wait for gauge to become ready */ 1458c2ecf20Sopenharmony_ci ret = regmap_read_poll_timeout(cw_bat->regmap, CW2015_REG_SOC, 1468c2ecf20Sopenharmony_ci reg_val, reg_val <= 100, 1478c2ecf20Sopenharmony_ci 10 * USEC_PER_MSEC, 10 * USEC_PER_SEC); 1488c2ecf20Sopenharmony_ci if (ret) 1498c2ecf20Sopenharmony_ci dev_err(cw_bat->dev, 1508c2ecf20Sopenharmony_ci "Gauge did not become ready after profile upload\n"); 1518c2ecf20Sopenharmony_ci else 1528c2ecf20Sopenharmony_ci dev_dbg(cw_bat->dev, "Battery profile updated\n"); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci return ret; 1558c2ecf20Sopenharmony_ci} 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_cistatic int cw_init(struct cw_battery *cw_bat) 1588c2ecf20Sopenharmony_ci{ 1598c2ecf20Sopenharmony_ci int ret; 1608c2ecf20Sopenharmony_ci unsigned int reg_val = CW2015_MODE_SLEEP; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci if ((reg_val & CW2015_MODE_SLEEP_MASK) == CW2015_MODE_SLEEP) { 1638c2ecf20Sopenharmony_ci reg_val = CW2015_MODE_NORMAL; 1648c2ecf20Sopenharmony_ci ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reg_val); 1658c2ecf20Sopenharmony_ci if (ret) 1668c2ecf20Sopenharmony_ci return ret; 1678c2ecf20Sopenharmony_ci } 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci ret = regmap_read(cw_bat->regmap, CW2015_REG_CONFIG, ®_val); 1708c2ecf20Sopenharmony_ci if (ret) 1718c2ecf20Sopenharmony_ci return ret; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci if ((reg_val & CW2015_MASK_ATHD) != CW2015_ATHD(cw_bat->alert_level)) { 1748c2ecf20Sopenharmony_ci dev_dbg(cw_bat->dev, "Setting new alert level\n"); 1758c2ecf20Sopenharmony_ci reg_val &= ~CW2015_MASK_ATHD; 1768c2ecf20Sopenharmony_ci reg_val |= ~CW2015_ATHD(cw_bat->alert_level); 1778c2ecf20Sopenharmony_ci ret = regmap_write(cw_bat->regmap, CW2015_REG_CONFIG, reg_val); 1788c2ecf20Sopenharmony_ci if (ret) 1798c2ecf20Sopenharmony_ci return ret; 1808c2ecf20Sopenharmony_ci } 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci ret = regmap_read(cw_bat->regmap, CW2015_REG_CONFIG, ®_val); 1838c2ecf20Sopenharmony_ci if (ret) 1848c2ecf20Sopenharmony_ci return ret; 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci if (!(reg_val & CW2015_CONFIG_UPDATE_FLG)) { 1878c2ecf20Sopenharmony_ci dev_dbg(cw_bat->dev, 1888c2ecf20Sopenharmony_ci "Battery profile not present, uploading battery profile\n"); 1898c2ecf20Sopenharmony_ci if (cw_bat->bat_profile) { 1908c2ecf20Sopenharmony_ci ret = cw_update_profile(cw_bat); 1918c2ecf20Sopenharmony_ci if (ret) { 1928c2ecf20Sopenharmony_ci dev_err(cw_bat->dev, 1938c2ecf20Sopenharmony_ci "Failed to upload battery profile\n"); 1948c2ecf20Sopenharmony_ci return ret; 1958c2ecf20Sopenharmony_ci } 1968c2ecf20Sopenharmony_ci } else { 1978c2ecf20Sopenharmony_ci dev_warn(cw_bat->dev, 1988c2ecf20Sopenharmony_ci "No profile specified, continuing without profile\n"); 1998c2ecf20Sopenharmony_ci } 2008c2ecf20Sopenharmony_ci } else if (cw_bat->bat_profile) { 2018c2ecf20Sopenharmony_ci u8 bat_info[CW2015_SIZE_BATINFO]; 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci ret = regmap_raw_read(cw_bat->regmap, CW2015_REG_BATINFO, 2048c2ecf20Sopenharmony_ci bat_info, CW2015_SIZE_BATINFO); 2058c2ecf20Sopenharmony_ci if (ret) { 2068c2ecf20Sopenharmony_ci dev_err(cw_bat->dev, 2078c2ecf20Sopenharmony_ci "Failed to read stored battery profile\n"); 2088c2ecf20Sopenharmony_ci return ret; 2098c2ecf20Sopenharmony_ci } 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci if (memcmp(bat_info, cw_bat->bat_profile, CW2015_SIZE_BATINFO)) { 2128c2ecf20Sopenharmony_ci dev_warn(cw_bat->dev, "Replacing stored battery profile\n"); 2138c2ecf20Sopenharmony_ci ret = cw_update_profile(cw_bat); 2148c2ecf20Sopenharmony_ci if (ret) 2158c2ecf20Sopenharmony_ci return ret; 2168c2ecf20Sopenharmony_ci } 2178c2ecf20Sopenharmony_ci } else { 2188c2ecf20Sopenharmony_ci dev_warn(cw_bat->dev, 2198c2ecf20Sopenharmony_ci "Can't check current battery profile, no profile provided\n"); 2208c2ecf20Sopenharmony_ci } 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci dev_dbg(cw_bat->dev, "Battery profile configured\n"); 2238c2ecf20Sopenharmony_ci return 0; 2248c2ecf20Sopenharmony_ci} 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_cistatic int cw_power_on_reset(struct cw_battery *cw_bat) 2278c2ecf20Sopenharmony_ci{ 2288c2ecf20Sopenharmony_ci int ret; 2298c2ecf20Sopenharmony_ci unsigned char reset_val; 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci reset_val = CW2015_MODE_SLEEP; 2328c2ecf20Sopenharmony_ci ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val); 2338c2ecf20Sopenharmony_ci if (ret) 2348c2ecf20Sopenharmony_ci return ret; 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci /* wait for gauge to enter sleep */ 2378c2ecf20Sopenharmony_ci msleep(20); 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci reset_val = CW2015_MODE_NORMAL; 2408c2ecf20Sopenharmony_ci ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val); 2418c2ecf20Sopenharmony_ci if (ret) 2428c2ecf20Sopenharmony_ci return ret; 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci ret = cw_init(cw_bat); 2458c2ecf20Sopenharmony_ci if (ret) 2468c2ecf20Sopenharmony_ci return ret; 2478c2ecf20Sopenharmony_ci return 0; 2488c2ecf20Sopenharmony_ci} 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci#define HYSTERESIS(current, previous, up, down) \ 2518c2ecf20Sopenharmony_ci (((current) < (previous) + (up)) && ((current) > (previous) - (down))) 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_cistatic int cw_get_soc(struct cw_battery *cw_bat) 2548c2ecf20Sopenharmony_ci{ 2558c2ecf20Sopenharmony_ci unsigned int soc; 2568c2ecf20Sopenharmony_ci int ret; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci ret = regmap_read(cw_bat->regmap, CW2015_REG_SOC, &soc); 2598c2ecf20Sopenharmony_ci if (ret) 2608c2ecf20Sopenharmony_ci return ret; 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci if (soc > 100) { 2638c2ecf20Sopenharmony_ci int max_error_cycles = 2648c2ecf20Sopenharmony_ci CW2015_BAT_SOC_ERROR_MS / cw_bat->poll_interval_ms; 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci dev_err(cw_bat->dev, "Invalid SoC %d%%\n", soc); 2678c2ecf20Sopenharmony_ci cw_bat->read_errors++; 2688c2ecf20Sopenharmony_ci if (cw_bat->read_errors > max_error_cycles) { 2698c2ecf20Sopenharmony_ci dev_warn(cw_bat->dev, 2708c2ecf20Sopenharmony_ci "Too many invalid SoC reports, resetting gauge\n"); 2718c2ecf20Sopenharmony_ci cw_power_on_reset(cw_bat); 2728c2ecf20Sopenharmony_ci cw_bat->read_errors = 0; 2738c2ecf20Sopenharmony_ci } 2748c2ecf20Sopenharmony_ci return cw_bat->soc; 2758c2ecf20Sopenharmony_ci } 2768c2ecf20Sopenharmony_ci cw_bat->read_errors = 0; 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci /* Reset gauge if stuck while charging */ 2798c2ecf20Sopenharmony_ci if (cw_bat->status == POWER_SUPPLY_STATUS_CHARGING && soc == cw_bat->soc) { 2808c2ecf20Sopenharmony_ci int max_stuck_cycles = 2818c2ecf20Sopenharmony_ci CW2015_BAT_CHARGING_STUCK_MS / cw_bat->poll_interval_ms; 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci cw_bat->charge_stuck_cnt++; 2848c2ecf20Sopenharmony_ci if (cw_bat->charge_stuck_cnt > max_stuck_cycles) { 2858c2ecf20Sopenharmony_ci dev_warn(cw_bat->dev, 2868c2ecf20Sopenharmony_ci "SoC stuck @%u%%, resetting gauge\n", soc); 2878c2ecf20Sopenharmony_ci cw_power_on_reset(cw_bat); 2888c2ecf20Sopenharmony_ci cw_bat->charge_stuck_cnt = 0; 2898c2ecf20Sopenharmony_ci } 2908c2ecf20Sopenharmony_ci } else { 2918c2ecf20Sopenharmony_ci cw_bat->charge_stuck_cnt = 0; 2928c2ecf20Sopenharmony_ci } 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_ci /* Ignore voltage dips during charge */ 2958c2ecf20Sopenharmony_ci if (cw_bat->charger_attached && HYSTERESIS(soc, cw_bat->soc, 0, 3)) 2968c2ecf20Sopenharmony_ci soc = cw_bat->soc; 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci /* Ignore voltage spikes during discharge */ 2998c2ecf20Sopenharmony_ci if (!cw_bat->charger_attached && HYSTERESIS(soc, cw_bat->soc, 3, 0)) 3008c2ecf20Sopenharmony_ci soc = cw_bat->soc; 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci return soc; 3038c2ecf20Sopenharmony_ci} 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_cistatic int cw_get_voltage(struct cw_battery *cw_bat) 3068c2ecf20Sopenharmony_ci{ 3078c2ecf20Sopenharmony_ci int ret, i, voltage_mv; 3088c2ecf20Sopenharmony_ci u16 reg_val; 3098c2ecf20Sopenharmony_ci u32 avg = 0; 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci for (i = 0; i < CW2015_AVERAGING_SAMPLES; i++) { 3128c2ecf20Sopenharmony_ci ret = cw_read_word(cw_bat, CW2015_REG_VCELL, ®_val); 3138c2ecf20Sopenharmony_ci if (ret) 3148c2ecf20Sopenharmony_ci return ret; 3158c2ecf20Sopenharmony_ci 3168c2ecf20Sopenharmony_ci avg += reg_val; 3178c2ecf20Sopenharmony_ci } 3188c2ecf20Sopenharmony_ci avg /= CW2015_AVERAGING_SAMPLES; 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci /* 3218c2ecf20Sopenharmony_ci * 305 uV per ADC step 3228c2ecf20Sopenharmony_ci * Use 312 / 1024 as efficient approximation of 305 / 1000 3238c2ecf20Sopenharmony_ci * Negligible error of 0.1% 3248c2ecf20Sopenharmony_ci */ 3258c2ecf20Sopenharmony_ci voltage_mv = avg * 312 / 1024; 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_ci dev_dbg(cw_bat->dev, "Read voltage: %d mV, raw=0x%04x\n", 3288c2ecf20Sopenharmony_ci voltage_mv, reg_val); 3298c2ecf20Sopenharmony_ci return voltage_mv; 3308c2ecf20Sopenharmony_ci} 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_cistatic int cw_get_time_to_empty(struct cw_battery *cw_bat) 3338c2ecf20Sopenharmony_ci{ 3348c2ecf20Sopenharmony_ci int ret; 3358c2ecf20Sopenharmony_ci u16 value16; 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_ci ret = cw_read_word(cw_bat, CW2015_REG_RRT_ALERT, &value16); 3388c2ecf20Sopenharmony_ci if (ret) 3398c2ecf20Sopenharmony_ci return ret; 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci return value16 & CW2015_MASK_SOC; 3428c2ecf20Sopenharmony_ci} 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_cistatic void cw_update_charge_status(struct cw_battery *cw_bat) 3458c2ecf20Sopenharmony_ci{ 3468c2ecf20Sopenharmony_ci int ret; 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ci ret = power_supply_am_i_supplied(cw_bat->rk_bat); 3498c2ecf20Sopenharmony_ci if (ret < 0) { 3508c2ecf20Sopenharmony_ci dev_warn(cw_bat->dev, "Failed to get supply state: %d\n", ret); 3518c2ecf20Sopenharmony_ci } else { 3528c2ecf20Sopenharmony_ci bool charger_attached; 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_ci charger_attached = !!ret; 3558c2ecf20Sopenharmony_ci if (cw_bat->charger_attached != charger_attached) { 3568c2ecf20Sopenharmony_ci cw_bat->battery_changed = true; 3578c2ecf20Sopenharmony_ci if (charger_attached) 3588c2ecf20Sopenharmony_ci cw_bat->charge_count++; 3598c2ecf20Sopenharmony_ci } 3608c2ecf20Sopenharmony_ci cw_bat->charger_attached = charger_attached; 3618c2ecf20Sopenharmony_ci } 3628c2ecf20Sopenharmony_ci} 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_cistatic void cw_update_soc(struct cw_battery *cw_bat) 3658c2ecf20Sopenharmony_ci{ 3668c2ecf20Sopenharmony_ci int soc; 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci soc = cw_get_soc(cw_bat); 3698c2ecf20Sopenharmony_ci if (soc < 0) 3708c2ecf20Sopenharmony_ci dev_err(cw_bat->dev, "Failed to get SoC from gauge: %d\n", soc); 3718c2ecf20Sopenharmony_ci else if (cw_bat->soc != soc) { 3728c2ecf20Sopenharmony_ci cw_bat->soc = soc; 3738c2ecf20Sopenharmony_ci cw_bat->battery_changed = true; 3748c2ecf20Sopenharmony_ci } 3758c2ecf20Sopenharmony_ci} 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_cistatic void cw_update_voltage(struct cw_battery *cw_bat) 3788c2ecf20Sopenharmony_ci{ 3798c2ecf20Sopenharmony_ci int voltage_mv; 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_ci voltage_mv = cw_get_voltage(cw_bat); 3828c2ecf20Sopenharmony_ci if (voltage_mv < 0) 3838c2ecf20Sopenharmony_ci dev_err(cw_bat->dev, "Failed to get voltage from gauge: %d\n", 3848c2ecf20Sopenharmony_ci voltage_mv); 3858c2ecf20Sopenharmony_ci else 3868c2ecf20Sopenharmony_ci cw_bat->voltage_mv = voltage_mv; 3878c2ecf20Sopenharmony_ci} 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_cistatic void cw_update_status(struct cw_battery *cw_bat) 3908c2ecf20Sopenharmony_ci{ 3918c2ecf20Sopenharmony_ci int status = POWER_SUPPLY_STATUS_DISCHARGING; 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci if (cw_bat->charger_attached) { 3948c2ecf20Sopenharmony_ci if (cw_bat->soc >= 100) 3958c2ecf20Sopenharmony_ci status = POWER_SUPPLY_STATUS_FULL; 3968c2ecf20Sopenharmony_ci else 3978c2ecf20Sopenharmony_ci status = POWER_SUPPLY_STATUS_CHARGING; 3988c2ecf20Sopenharmony_ci } 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_ci if (cw_bat->status != status) 4018c2ecf20Sopenharmony_ci cw_bat->battery_changed = true; 4028c2ecf20Sopenharmony_ci cw_bat->status = status; 4038c2ecf20Sopenharmony_ci} 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_cistatic void cw_update_time_to_empty(struct cw_battery *cw_bat) 4068c2ecf20Sopenharmony_ci{ 4078c2ecf20Sopenharmony_ci int time_to_empty; 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci time_to_empty = cw_get_time_to_empty(cw_bat); 4108c2ecf20Sopenharmony_ci if (time_to_empty < 0) 4118c2ecf20Sopenharmony_ci dev_err(cw_bat->dev, "Failed to get time to empty from gauge: %d\n", 4128c2ecf20Sopenharmony_ci time_to_empty); 4138c2ecf20Sopenharmony_ci else if (cw_bat->time_to_empty != time_to_empty) { 4148c2ecf20Sopenharmony_ci cw_bat->time_to_empty = time_to_empty; 4158c2ecf20Sopenharmony_ci cw_bat->battery_changed = true; 4168c2ecf20Sopenharmony_ci } 4178c2ecf20Sopenharmony_ci} 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_cistatic void cw_bat_work(struct work_struct *work) 4208c2ecf20Sopenharmony_ci{ 4218c2ecf20Sopenharmony_ci struct delayed_work *delay_work; 4228c2ecf20Sopenharmony_ci struct cw_battery *cw_bat; 4238c2ecf20Sopenharmony_ci int ret; 4248c2ecf20Sopenharmony_ci unsigned int reg_val; 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_ci delay_work = to_delayed_work(work); 4278c2ecf20Sopenharmony_ci cw_bat = container_of(delay_work, struct cw_battery, battery_delay_work); 4288c2ecf20Sopenharmony_ci ret = regmap_read(cw_bat->regmap, CW2015_REG_MODE, ®_val); 4298c2ecf20Sopenharmony_ci if (ret) { 4308c2ecf20Sopenharmony_ci dev_err(cw_bat->dev, "Failed to read mode from gauge: %d\n", ret); 4318c2ecf20Sopenharmony_ci } else { 4328c2ecf20Sopenharmony_ci if ((reg_val & CW2015_MODE_SLEEP_MASK) == CW2015_MODE_SLEEP) { 4338c2ecf20Sopenharmony_ci int i; 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_ci for (i = 0; i < CW2015_RESET_TRIES; i++) { 4368c2ecf20Sopenharmony_ci if (!cw_power_on_reset(cw_bat)) 4378c2ecf20Sopenharmony_ci break; 4388c2ecf20Sopenharmony_ci } 4398c2ecf20Sopenharmony_ci } 4408c2ecf20Sopenharmony_ci cw_update_soc(cw_bat); 4418c2ecf20Sopenharmony_ci cw_update_voltage(cw_bat); 4428c2ecf20Sopenharmony_ci cw_update_charge_status(cw_bat); 4438c2ecf20Sopenharmony_ci cw_update_status(cw_bat); 4448c2ecf20Sopenharmony_ci cw_update_time_to_empty(cw_bat); 4458c2ecf20Sopenharmony_ci } 4468c2ecf20Sopenharmony_ci dev_dbg(cw_bat->dev, "charger_attached = %d\n", cw_bat->charger_attached); 4478c2ecf20Sopenharmony_ci dev_dbg(cw_bat->dev, "status = %d\n", cw_bat->status); 4488c2ecf20Sopenharmony_ci dev_dbg(cw_bat->dev, "soc = %d%%\n", cw_bat->soc); 4498c2ecf20Sopenharmony_ci dev_dbg(cw_bat->dev, "voltage = %dmV\n", cw_bat->voltage_mv); 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ci if (cw_bat->battery_changed) 4528c2ecf20Sopenharmony_ci power_supply_changed(cw_bat->rk_bat); 4538c2ecf20Sopenharmony_ci cw_bat->battery_changed = false; 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci queue_delayed_work(cw_bat->battery_workqueue, 4568c2ecf20Sopenharmony_ci &cw_bat->battery_delay_work, 4578c2ecf20Sopenharmony_ci msecs_to_jiffies(cw_bat->poll_interval_ms)); 4588c2ecf20Sopenharmony_ci} 4598c2ecf20Sopenharmony_ci 4608c2ecf20Sopenharmony_cistatic bool cw_battery_valid_time_to_empty(struct cw_battery *cw_bat) 4618c2ecf20Sopenharmony_ci{ 4628c2ecf20Sopenharmony_ci return cw_bat->time_to_empty > 0 && 4638c2ecf20Sopenharmony_ci cw_bat->time_to_empty < CW2015_MASK_SOC && 4648c2ecf20Sopenharmony_ci cw_bat->status == POWER_SUPPLY_STATUS_DISCHARGING; 4658c2ecf20Sopenharmony_ci} 4668c2ecf20Sopenharmony_ci 4678c2ecf20Sopenharmony_cistatic int cw_battery_get_property(struct power_supply *psy, 4688c2ecf20Sopenharmony_ci enum power_supply_property psp, 4698c2ecf20Sopenharmony_ci union power_supply_propval *val) 4708c2ecf20Sopenharmony_ci{ 4718c2ecf20Sopenharmony_ci struct cw_battery *cw_bat; 4728c2ecf20Sopenharmony_ci 4738c2ecf20Sopenharmony_ci cw_bat = power_supply_get_drvdata(psy); 4748c2ecf20Sopenharmony_ci switch (psp) { 4758c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CAPACITY: 4768c2ecf20Sopenharmony_ci val->intval = cw_bat->soc; 4778c2ecf20Sopenharmony_ci break; 4788c2ecf20Sopenharmony_ci 4798c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_STATUS: 4808c2ecf20Sopenharmony_ci val->intval = cw_bat->status; 4818c2ecf20Sopenharmony_ci break; 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_PRESENT: 4848c2ecf20Sopenharmony_ci val->intval = !!cw_bat->voltage_mv; 4858c2ecf20Sopenharmony_ci break; 4868c2ecf20Sopenharmony_ci 4878c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_VOLTAGE_NOW: 4888c2ecf20Sopenharmony_ci val->intval = cw_bat->voltage_mv * 1000; 4898c2ecf20Sopenharmony_ci break; 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: 4928c2ecf20Sopenharmony_ci if (cw_battery_valid_time_to_empty(cw_bat)) 4938c2ecf20Sopenharmony_ci val->intval = cw_bat->time_to_empty * 60; 4948c2ecf20Sopenharmony_ci else 4958c2ecf20Sopenharmony_ci val->intval = 0; 4968c2ecf20Sopenharmony_ci break; 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_TECHNOLOGY: 4998c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_TECHNOLOGY_LION; 5008c2ecf20Sopenharmony_ci break; 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CHARGE_COUNTER: 5038c2ecf20Sopenharmony_ci val->intval = cw_bat->charge_count; 5048c2ecf20Sopenharmony_ci break; 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CHARGE_FULL: 5078c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 5088c2ecf20Sopenharmony_ci if (cw_bat->battery.charge_full_design_uah > 0) 5098c2ecf20Sopenharmony_ci val->intval = cw_bat->battery.charge_full_design_uah; 5108c2ecf20Sopenharmony_ci else 5118c2ecf20Sopenharmony_ci val->intval = 0; 5128c2ecf20Sopenharmony_ci break; 5138c2ecf20Sopenharmony_ci 5148c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CURRENT_NOW: 5158c2ecf20Sopenharmony_ci if (cw_battery_valid_time_to_empty(cw_bat) && 5168c2ecf20Sopenharmony_ci cw_bat->battery.charge_full_design_uah > 0) { 5178c2ecf20Sopenharmony_ci /* calculate remaining capacity */ 5188c2ecf20Sopenharmony_ci val->intval = cw_bat->battery.charge_full_design_uah; 5198c2ecf20Sopenharmony_ci val->intval = val->intval * cw_bat->soc / 100; 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_ci /* estimate current based on time to empty */ 5228c2ecf20Sopenharmony_ci val->intval = 60 * val->intval / cw_bat->time_to_empty; 5238c2ecf20Sopenharmony_ci } else { 5248c2ecf20Sopenharmony_ci val->intval = 0; 5258c2ecf20Sopenharmony_ci } 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_ci break; 5288c2ecf20Sopenharmony_ci 5298c2ecf20Sopenharmony_ci default: 5308c2ecf20Sopenharmony_ci break; 5318c2ecf20Sopenharmony_ci } 5328c2ecf20Sopenharmony_ci return 0; 5338c2ecf20Sopenharmony_ci} 5348c2ecf20Sopenharmony_ci 5358c2ecf20Sopenharmony_cistatic enum power_supply_property cw_battery_properties[] = { 5368c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CAPACITY, 5378c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_STATUS, 5388c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_PRESENT, 5398c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_VOLTAGE_NOW, 5408c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 5418c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_TECHNOLOGY, 5428c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CHARGE_COUNTER, 5438c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CHARGE_FULL, 5448c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 5458c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CURRENT_NOW, 5468c2ecf20Sopenharmony_ci}; 5478c2ecf20Sopenharmony_ci 5488c2ecf20Sopenharmony_cistatic const struct power_supply_desc cw2015_bat_desc = { 5498c2ecf20Sopenharmony_ci .name = "cw2015-battery", 5508c2ecf20Sopenharmony_ci .type = POWER_SUPPLY_TYPE_BATTERY, 5518c2ecf20Sopenharmony_ci .properties = cw_battery_properties, 5528c2ecf20Sopenharmony_ci .num_properties = ARRAY_SIZE(cw_battery_properties), 5538c2ecf20Sopenharmony_ci .get_property = cw_battery_get_property, 5548c2ecf20Sopenharmony_ci}; 5558c2ecf20Sopenharmony_ci 5568c2ecf20Sopenharmony_cistatic int cw2015_parse_properties(struct cw_battery *cw_bat) 5578c2ecf20Sopenharmony_ci{ 5588c2ecf20Sopenharmony_ci struct device *dev = cw_bat->dev; 5598c2ecf20Sopenharmony_ci int length; 5608c2ecf20Sopenharmony_ci int ret; 5618c2ecf20Sopenharmony_ci 5628c2ecf20Sopenharmony_ci length = device_property_count_u8(dev, "cellwise,battery-profile"); 5638c2ecf20Sopenharmony_ci if (length < 0) { 5648c2ecf20Sopenharmony_ci dev_warn(cw_bat->dev, 5658c2ecf20Sopenharmony_ci "No battery-profile found, using current flash contents\n"); 5668c2ecf20Sopenharmony_ci } else if (length != CW2015_SIZE_BATINFO) { 5678c2ecf20Sopenharmony_ci dev_err(cw_bat->dev, "battery-profile must be %d bytes\n", 5688c2ecf20Sopenharmony_ci CW2015_SIZE_BATINFO); 5698c2ecf20Sopenharmony_ci return -EINVAL; 5708c2ecf20Sopenharmony_ci } else { 5718c2ecf20Sopenharmony_ci cw_bat->bat_profile = devm_kzalloc(dev, length, GFP_KERNEL); 5728c2ecf20Sopenharmony_ci if (!cw_bat->bat_profile) 5738c2ecf20Sopenharmony_ci return -ENOMEM; 5748c2ecf20Sopenharmony_ci 5758c2ecf20Sopenharmony_ci ret = device_property_read_u8_array(dev, 5768c2ecf20Sopenharmony_ci "cellwise,battery-profile", 5778c2ecf20Sopenharmony_ci cw_bat->bat_profile, 5788c2ecf20Sopenharmony_ci length); 5798c2ecf20Sopenharmony_ci if (ret) 5808c2ecf20Sopenharmony_ci return ret; 5818c2ecf20Sopenharmony_ci } 5828c2ecf20Sopenharmony_ci 5838c2ecf20Sopenharmony_ci ret = device_property_read_u32(dev, "cellwise,monitor-interval-ms", 5848c2ecf20Sopenharmony_ci &cw_bat->poll_interval_ms); 5858c2ecf20Sopenharmony_ci if (ret) { 5868c2ecf20Sopenharmony_ci dev_dbg(cw_bat->dev, "Using default poll interval\n"); 5878c2ecf20Sopenharmony_ci cw_bat->poll_interval_ms = CW2015_DEFAULT_POLL_INTERVAL_MS; 5888c2ecf20Sopenharmony_ci } 5898c2ecf20Sopenharmony_ci 5908c2ecf20Sopenharmony_ci return 0; 5918c2ecf20Sopenharmony_ci} 5928c2ecf20Sopenharmony_ci 5938c2ecf20Sopenharmony_cistatic const struct regmap_range regmap_ranges_rd_yes[] = { 5948c2ecf20Sopenharmony_ci regmap_reg_range(CW2015_REG_VERSION, CW2015_REG_VERSION), 5958c2ecf20Sopenharmony_ci regmap_reg_range(CW2015_REG_VCELL, CW2015_REG_CONFIG), 5968c2ecf20Sopenharmony_ci regmap_reg_range(CW2015_REG_MODE, CW2015_REG_MODE), 5978c2ecf20Sopenharmony_ci regmap_reg_range(CW2015_REG_BATINFO, 5988c2ecf20Sopenharmony_ci CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1), 5998c2ecf20Sopenharmony_ci}; 6008c2ecf20Sopenharmony_ci 6018c2ecf20Sopenharmony_cistatic const struct regmap_access_table regmap_rd_table = { 6028c2ecf20Sopenharmony_ci .yes_ranges = regmap_ranges_rd_yes, 6038c2ecf20Sopenharmony_ci .n_yes_ranges = 4, 6048c2ecf20Sopenharmony_ci}; 6058c2ecf20Sopenharmony_ci 6068c2ecf20Sopenharmony_cistatic const struct regmap_range regmap_ranges_wr_yes[] = { 6078c2ecf20Sopenharmony_ci regmap_reg_range(CW2015_REG_RRT_ALERT, CW2015_REG_CONFIG), 6088c2ecf20Sopenharmony_ci regmap_reg_range(CW2015_REG_MODE, CW2015_REG_MODE), 6098c2ecf20Sopenharmony_ci regmap_reg_range(CW2015_REG_BATINFO, 6108c2ecf20Sopenharmony_ci CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1), 6118c2ecf20Sopenharmony_ci}; 6128c2ecf20Sopenharmony_ci 6138c2ecf20Sopenharmony_cistatic const struct regmap_access_table regmap_wr_table = { 6148c2ecf20Sopenharmony_ci .yes_ranges = regmap_ranges_wr_yes, 6158c2ecf20Sopenharmony_ci .n_yes_ranges = 3, 6168c2ecf20Sopenharmony_ci}; 6178c2ecf20Sopenharmony_ci 6188c2ecf20Sopenharmony_cistatic const struct regmap_range regmap_ranges_vol_yes[] = { 6198c2ecf20Sopenharmony_ci regmap_reg_range(CW2015_REG_VCELL, CW2015_REG_SOC + 1), 6208c2ecf20Sopenharmony_ci}; 6218c2ecf20Sopenharmony_ci 6228c2ecf20Sopenharmony_cistatic const struct regmap_access_table regmap_vol_table = { 6238c2ecf20Sopenharmony_ci .yes_ranges = regmap_ranges_vol_yes, 6248c2ecf20Sopenharmony_ci .n_yes_ranges = 1, 6258c2ecf20Sopenharmony_ci}; 6268c2ecf20Sopenharmony_ci 6278c2ecf20Sopenharmony_cistatic const struct regmap_config cw2015_regmap_config = { 6288c2ecf20Sopenharmony_ci .reg_bits = 8, 6298c2ecf20Sopenharmony_ci .val_bits = 8, 6308c2ecf20Sopenharmony_ci .rd_table = ®map_rd_table, 6318c2ecf20Sopenharmony_ci .wr_table = ®map_wr_table, 6328c2ecf20Sopenharmony_ci .volatile_table = ®map_vol_table, 6338c2ecf20Sopenharmony_ci .max_register = CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1, 6348c2ecf20Sopenharmony_ci}; 6358c2ecf20Sopenharmony_ci 6368c2ecf20Sopenharmony_cistatic int cw_bat_probe(struct i2c_client *client) 6378c2ecf20Sopenharmony_ci{ 6388c2ecf20Sopenharmony_ci int ret; 6398c2ecf20Sopenharmony_ci struct cw_battery *cw_bat; 6408c2ecf20Sopenharmony_ci struct power_supply_config psy_cfg = { 0 }; 6418c2ecf20Sopenharmony_ci 6428c2ecf20Sopenharmony_ci cw_bat = devm_kzalloc(&client->dev, sizeof(*cw_bat), GFP_KERNEL); 6438c2ecf20Sopenharmony_ci if (!cw_bat) 6448c2ecf20Sopenharmony_ci return -ENOMEM; 6458c2ecf20Sopenharmony_ci 6468c2ecf20Sopenharmony_ci i2c_set_clientdata(client, cw_bat); 6478c2ecf20Sopenharmony_ci cw_bat->dev = &client->dev; 6488c2ecf20Sopenharmony_ci cw_bat->soc = 1; 6498c2ecf20Sopenharmony_ci 6508c2ecf20Sopenharmony_ci ret = cw2015_parse_properties(cw_bat); 6518c2ecf20Sopenharmony_ci if (ret) { 6528c2ecf20Sopenharmony_ci dev_err(cw_bat->dev, "Failed to parse cw2015 properties\n"); 6538c2ecf20Sopenharmony_ci return ret; 6548c2ecf20Sopenharmony_ci } 6558c2ecf20Sopenharmony_ci 6568c2ecf20Sopenharmony_ci cw_bat->regmap = devm_regmap_init_i2c(client, &cw2015_regmap_config); 6578c2ecf20Sopenharmony_ci if (IS_ERR(cw_bat->regmap)) { 6588c2ecf20Sopenharmony_ci dev_err(cw_bat->dev, "Failed to allocate regmap: %ld\n", 6598c2ecf20Sopenharmony_ci PTR_ERR(cw_bat->regmap)); 6608c2ecf20Sopenharmony_ci return PTR_ERR(cw_bat->regmap); 6618c2ecf20Sopenharmony_ci } 6628c2ecf20Sopenharmony_ci 6638c2ecf20Sopenharmony_ci ret = cw_init(cw_bat); 6648c2ecf20Sopenharmony_ci if (ret) { 6658c2ecf20Sopenharmony_ci dev_err(cw_bat->dev, "Init failed: %d\n", ret); 6668c2ecf20Sopenharmony_ci return ret; 6678c2ecf20Sopenharmony_ci } 6688c2ecf20Sopenharmony_ci 6698c2ecf20Sopenharmony_ci psy_cfg.drv_data = cw_bat; 6708c2ecf20Sopenharmony_ci psy_cfg.fwnode = dev_fwnode(cw_bat->dev); 6718c2ecf20Sopenharmony_ci 6728c2ecf20Sopenharmony_ci cw_bat->rk_bat = devm_power_supply_register(&client->dev, 6738c2ecf20Sopenharmony_ci &cw2015_bat_desc, 6748c2ecf20Sopenharmony_ci &psy_cfg); 6758c2ecf20Sopenharmony_ci if (IS_ERR(cw_bat->rk_bat)) { 6768c2ecf20Sopenharmony_ci /* try again if this happens */ 6778c2ecf20Sopenharmony_ci dev_err_probe(&client->dev, PTR_ERR(cw_bat->rk_bat), 6788c2ecf20Sopenharmony_ci "Failed to register power supply\n"); 6798c2ecf20Sopenharmony_ci return PTR_ERR(cw_bat->rk_bat); 6808c2ecf20Sopenharmony_ci } 6818c2ecf20Sopenharmony_ci 6828c2ecf20Sopenharmony_ci ret = power_supply_get_battery_info(cw_bat->rk_bat, &cw_bat->battery); 6838c2ecf20Sopenharmony_ci if (ret) { 6848c2ecf20Sopenharmony_ci dev_warn(cw_bat->dev, 6858c2ecf20Sopenharmony_ci "No monitored battery, some properties will be missing\n"); 6868c2ecf20Sopenharmony_ci } 6878c2ecf20Sopenharmony_ci 6888c2ecf20Sopenharmony_ci cw_bat->battery_workqueue = create_singlethread_workqueue("rk_battery"); 6898c2ecf20Sopenharmony_ci INIT_DELAYED_WORK(&cw_bat->battery_delay_work, cw_bat_work); 6908c2ecf20Sopenharmony_ci queue_delayed_work(cw_bat->battery_workqueue, 6918c2ecf20Sopenharmony_ci &cw_bat->battery_delay_work, msecs_to_jiffies(10)); 6928c2ecf20Sopenharmony_ci return 0; 6938c2ecf20Sopenharmony_ci} 6948c2ecf20Sopenharmony_ci 6958c2ecf20Sopenharmony_cistatic int __maybe_unused cw_bat_suspend(struct device *dev) 6968c2ecf20Sopenharmony_ci{ 6978c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 6988c2ecf20Sopenharmony_ci struct cw_battery *cw_bat = i2c_get_clientdata(client); 6998c2ecf20Sopenharmony_ci 7008c2ecf20Sopenharmony_ci cancel_delayed_work_sync(&cw_bat->battery_delay_work); 7018c2ecf20Sopenharmony_ci return 0; 7028c2ecf20Sopenharmony_ci} 7038c2ecf20Sopenharmony_ci 7048c2ecf20Sopenharmony_cistatic int __maybe_unused cw_bat_resume(struct device *dev) 7058c2ecf20Sopenharmony_ci{ 7068c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 7078c2ecf20Sopenharmony_ci struct cw_battery *cw_bat = i2c_get_clientdata(client); 7088c2ecf20Sopenharmony_ci 7098c2ecf20Sopenharmony_ci queue_delayed_work(cw_bat->battery_workqueue, 7108c2ecf20Sopenharmony_ci &cw_bat->battery_delay_work, 0); 7118c2ecf20Sopenharmony_ci return 0; 7128c2ecf20Sopenharmony_ci} 7138c2ecf20Sopenharmony_ci 7148c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(cw_bat_pm_ops, cw_bat_suspend, cw_bat_resume); 7158c2ecf20Sopenharmony_ci 7168c2ecf20Sopenharmony_cistatic int cw_bat_remove(struct i2c_client *client) 7178c2ecf20Sopenharmony_ci{ 7188c2ecf20Sopenharmony_ci struct cw_battery *cw_bat = i2c_get_clientdata(client); 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_ci cancel_delayed_work_sync(&cw_bat->battery_delay_work); 7218c2ecf20Sopenharmony_ci power_supply_put_battery_info(cw_bat->rk_bat, &cw_bat->battery); 7228c2ecf20Sopenharmony_ci return 0; 7238c2ecf20Sopenharmony_ci} 7248c2ecf20Sopenharmony_ci 7258c2ecf20Sopenharmony_cistatic const struct i2c_device_id cw_bat_id_table[] = { 7268c2ecf20Sopenharmony_ci { "cw2015", 0 }, 7278c2ecf20Sopenharmony_ci { } 7288c2ecf20Sopenharmony_ci}; 7298c2ecf20Sopenharmony_ci 7308c2ecf20Sopenharmony_cistatic const struct of_device_id cw2015_of_match[] = { 7318c2ecf20Sopenharmony_ci { .compatible = "cellwise,cw2015" }, 7328c2ecf20Sopenharmony_ci { } 7338c2ecf20Sopenharmony_ci}; 7348c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, cw2015_of_match); 7358c2ecf20Sopenharmony_ci 7368c2ecf20Sopenharmony_cistatic struct i2c_driver cw_bat_driver = { 7378c2ecf20Sopenharmony_ci .driver = { 7388c2ecf20Sopenharmony_ci .name = "cw2015", 7398c2ecf20Sopenharmony_ci .of_match_table = cw2015_of_match, 7408c2ecf20Sopenharmony_ci .pm = &cw_bat_pm_ops, 7418c2ecf20Sopenharmony_ci }, 7428c2ecf20Sopenharmony_ci .probe_new = cw_bat_probe, 7438c2ecf20Sopenharmony_ci .remove = cw_bat_remove, 7448c2ecf20Sopenharmony_ci .id_table = cw_bat_id_table, 7458c2ecf20Sopenharmony_ci}; 7468c2ecf20Sopenharmony_ci 7478c2ecf20Sopenharmony_cimodule_i2c_driver(cw_bat_driver); 7488c2ecf20Sopenharmony_ci 7498c2ecf20Sopenharmony_ciMODULE_AUTHOR("xhc<xhc@rock-chips.com>"); 7508c2ecf20Sopenharmony_ciMODULE_AUTHOR("Tobias Schramm <t.schramm@manjaro.org>"); 7518c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("cw2015/cw2013 battery driver"); 7528c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 753