^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Fuel gauge driver for CellWise 2013 / 2015
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2012, RockChip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2020, Tobias Schramm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Authors: xuhuicong <xhc@rock-chips.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Authors: Tobias Schramm <t.schramm@manjaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/bits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/property.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define CW2015_SIZE_BATINFO 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define CW2015_RESET_TRIES 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define CW2015_REG_VERSION 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define CW2015_REG_VCELL 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define CW2015_REG_SOC 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define CW2015_REG_RRT_ALERT 0x06
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define CW2015_REG_CONFIG 0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define CW2015_REG_MODE 0x0A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define CW2015_REG_BATINFO 0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define CW2015_MODE_SLEEP_MASK GENMASK(7, 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define CW2015_MODE_SLEEP (0x03 << 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define CW2015_MODE_NORMAL (0x00 << 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define CW2015_MODE_QUICK_START (0x03 << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define CW2015_MODE_RESTART (0x0f << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define CW2015_CONFIG_UPDATE_FLG (0x01 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define CW2015_ATHD(x) ((x) << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define CW2015_MASK_ATHD GENMASK(7, 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define CW2015_MASK_SOC GENMASK(12, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /* reset gauge of no valid state of charge could be polled for 40s */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define CW2015_BAT_SOC_ERROR_MS (40 * MSEC_PER_SEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* reset gauge if state of charge stuck for half an hour during charging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #define CW2015_BAT_CHARGING_STUCK_MS (1800 * MSEC_PER_SEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* poll interval from CellWise GPL Android driver example */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define CW2015_DEFAULT_POLL_INTERVAL_MS 8000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define CW2015_AVERAGING_SAMPLES 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct cw_battery {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct workqueue_struct *battery_workqueue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct delayed_work battery_delay_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct power_supply *rk_bat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct power_supply_battery_info battery;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) u8 *bat_profile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) bool charger_attached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) bool battery_changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int voltage_mv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) int time_to_empty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int charge_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) u32 poll_interval_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) u8 alert_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) bool dual_cell;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) unsigned int read_errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) unsigned int charge_stuck_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static int cw_read_word(struct cw_battery *cw_bat, u8 reg, u16 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) __be16 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) ret = regmap_bulk_read(cw_bat->regmap, reg, &value, sizeof(value));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) *val = be16_to_cpu(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static int cw_update_profile(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) unsigned int reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) u8 reset_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /* make sure gauge is not in sleep mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ret = regmap_read(cw_bat->regmap, CW2015_REG_MODE, ®_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) reset_val = reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if ((reg_val & CW2015_MODE_SLEEP_MASK) == CW2015_MODE_SLEEP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) dev_err(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) "Gauge is in sleep mode, can't update battery info\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /* write new battery info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) ret = regmap_raw_write(cw_bat->regmap, CW2015_REG_BATINFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) cw_bat->bat_profile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) CW2015_SIZE_BATINFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* set config update flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) reg_val |= CW2015_CONFIG_UPDATE_FLG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) reg_val &= ~CW2015_MASK_ATHD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) reg_val |= CW2015_ATHD(cw_bat->alert_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) ret = regmap_write(cw_bat->regmap, CW2015_REG_CONFIG, reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /* reset gauge to apply new battery profile */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) reset_val &= ~CW2015_MODE_RESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) reg_val = reset_val | CW2015_MODE_RESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /* wait for gauge to reset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) msleep(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* clear reset flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /* wait for gauge to become ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) ret = regmap_read_poll_timeout(cw_bat->regmap, CW2015_REG_SOC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) reg_val, reg_val <= 100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 10 * USEC_PER_MSEC, 10 * USEC_PER_SEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) dev_err(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) "Gauge did not become ready after profile upload\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) dev_dbg(cw_bat->dev, "Battery profile updated\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static int cw_init(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) unsigned int reg_val = CW2015_MODE_SLEEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if ((reg_val & CW2015_MODE_SLEEP_MASK) == CW2015_MODE_SLEEP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) reg_val = CW2015_MODE_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) ret = regmap_read(cw_bat->regmap, CW2015_REG_CONFIG, ®_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if ((reg_val & CW2015_MASK_ATHD) != CW2015_ATHD(cw_bat->alert_level)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) dev_dbg(cw_bat->dev, "Setting new alert level\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) reg_val &= ~CW2015_MASK_ATHD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) reg_val |= ~CW2015_ATHD(cw_bat->alert_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ret = regmap_write(cw_bat->regmap, CW2015_REG_CONFIG, reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) ret = regmap_read(cw_bat->regmap, CW2015_REG_CONFIG, ®_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (!(reg_val & CW2015_CONFIG_UPDATE_FLG)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) dev_dbg(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) "Battery profile not present, uploading battery profile\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (cw_bat->bat_profile) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) ret = cw_update_profile(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) dev_err(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) "Failed to upload battery profile\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) dev_warn(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) "No profile specified, continuing without profile\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) } else if (cw_bat->bat_profile) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) u8 bat_info[CW2015_SIZE_BATINFO];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) ret = regmap_raw_read(cw_bat->regmap, CW2015_REG_BATINFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) bat_info, CW2015_SIZE_BATINFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) dev_err(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) "Failed to read stored battery profile\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (memcmp(bat_info, cw_bat->bat_profile, CW2015_SIZE_BATINFO)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) dev_warn(cw_bat->dev, "Replacing stored battery profile\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) ret = cw_update_profile(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) dev_warn(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) "Can't check current battery profile, no profile provided\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) dev_dbg(cw_bat->dev, "Battery profile configured\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static int cw_power_on_reset(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) unsigned char reset_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) reset_val = CW2015_MODE_SLEEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /* wait for gauge to enter sleep */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) msleep(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) reset_val = CW2015_MODE_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) ret = cw_init(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) #define HYSTERESIS(current, previous, up, down) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) (((current) < (previous) + (up)) && ((current) > (previous) - (down)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static int cw_get_soc(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) unsigned int soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) ret = regmap_read(cw_bat->regmap, CW2015_REG_SOC, &soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (soc > 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) int max_error_cycles =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) CW2015_BAT_SOC_ERROR_MS / cw_bat->poll_interval_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) dev_err(cw_bat->dev, "Invalid SoC %d%%\n", soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) cw_bat->read_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (cw_bat->read_errors > max_error_cycles) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) dev_warn(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) "Too many invalid SoC reports, resetting gauge\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) cw_power_on_reset(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) cw_bat->read_errors = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return cw_bat->soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) cw_bat->read_errors = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) /* Reset gauge if stuck while charging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (cw_bat->status == POWER_SUPPLY_STATUS_CHARGING && soc == cw_bat->soc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) int max_stuck_cycles =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) CW2015_BAT_CHARGING_STUCK_MS / cw_bat->poll_interval_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) cw_bat->charge_stuck_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (cw_bat->charge_stuck_cnt > max_stuck_cycles) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) dev_warn(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) "SoC stuck @%u%%, resetting gauge\n", soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) cw_power_on_reset(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) cw_bat->charge_stuck_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) cw_bat->charge_stuck_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) /* Ignore voltage dips during charge */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (cw_bat->charger_attached && HYSTERESIS(soc, cw_bat->soc, 0, 3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) soc = cw_bat->soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) /* Ignore voltage spikes during discharge */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (!cw_bat->charger_attached && HYSTERESIS(soc, cw_bat->soc, 3, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) soc = cw_bat->soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static int cw_get_voltage(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) int ret, i, voltage_mv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) u16 reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) u32 avg = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) for (i = 0; i < CW2015_AVERAGING_SAMPLES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) ret = cw_read_word(cw_bat, CW2015_REG_VCELL, ®_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) avg += reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) avg /= CW2015_AVERAGING_SAMPLES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * 305 uV per ADC step
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * Use 312 / 1024 as efficient approximation of 305 / 1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * Negligible error of 0.1%
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) voltage_mv = avg * 312 / 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (cw_bat->dual_cell)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) voltage_mv *= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) dev_dbg(cw_bat->dev, "Read voltage: %d mV, raw=0x%04x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) voltage_mv, reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return voltage_mv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static int cw_get_time_to_empty(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) u16 value16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) ret = cw_read_word(cw_bat, CW2015_REG_RRT_ALERT, &value16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return value16 & CW2015_MASK_SOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static void cw_update_charge_status(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) ret = power_supply_am_i_supplied(cw_bat->rk_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) dev_warn(cw_bat->dev, "Failed to get supply state: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) bool charger_attached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) charger_attached = !!ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (cw_bat->charger_attached != charger_attached) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) cw_bat->battery_changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (charger_attached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) cw_bat->charge_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) cw_bat->charger_attached = charger_attached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) static void cw_update_soc(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) int soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) soc = cw_get_soc(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (soc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) dev_err(cw_bat->dev, "Failed to get SoC from gauge: %d\n", soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) else if (cw_bat->soc != soc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) cw_bat->soc = soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) cw_bat->battery_changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static void cw_update_voltage(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) int voltage_mv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) voltage_mv = cw_get_voltage(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (voltage_mv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) dev_err(cw_bat->dev, "Failed to get voltage from gauge: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) voltage_mv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) cw_bat->voltage_mv = voltage_mv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) static void cw_update_status(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) int status = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (cw_bat->charger_attached) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (cw_bat->soc >= 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) status = POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) status = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (cw_bat->status != status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) cw_bat->battery_changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) cw_bat->status = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static void cw_update_time_to_empty(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) int time_to_empty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) time_to_empty = cw_get_time_to_empty(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) if (time_to_empty < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) dev_err(cw_bat->dev, "Failed to get time to empty from gauge: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) time_to_empty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) cw_bat->time_to_empty = time_to_empty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) static void cw_bat_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) struct delayed_work *delay_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) struct cw_battery *cw_bat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) unsigned int reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) delay_work = to_delayed_work(work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) cw_bat = container_of(delay_work, struct cw_battery, battery_delay_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) ret = regmap_read(cw_bat->regmap, CW2015_REG_MODE, ®_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) dev_err(cw_bat->dev, "Failed to read mode from gauge: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if ((reg_val & CW2015_MODE_SLEEP_MASK) == CW2015_MODE_SLEEP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) for (i = 0; i < CW2015_RESET_TRIES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) if (!cw_power_on_reset(cw_bat))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) cw_update_soc(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) cw_update_voltage(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) cw_update_charge_status(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) cw_update_status(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) cw_update_time_to_empty(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) dev_dbg(cw_bat->dev, "charger_attached = %d\n", cw_bat->charger_attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) dev_dbg(cw_bat->dev, "status = %d\n", cw_bat->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) dev_dbg(cw_bat->dev, "soc = %d%%\n", cw_bat->soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) dev_dbg(cw_bat->dev, "voltage = %dmV\n", cw_bat->voltage_mv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (cw_bat->battery_changed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) power_supply_changed(cw_bat->rk_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) cw_bat->battery_changed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) queue_delayed_work(cw_bat->battery_workqueue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) &cw_bat->battery_delay_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) msecs_to_jiffies(cw_bat->poll_interval_ms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) static bool cw_battery_valid_time_to_empty(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) return cw_bat->time_to_empty > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) cw_bat->time_to_empty < CW2015_MASK_SOC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) cw_bat->status == POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) static int cw_get_capacity_leve(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (cw_bat->soc < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) else if (cw_bat->soc <= 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) else if (cw_bat->soc <= 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) else if (cw_bat->soc <= 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) return POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) static int cw_battery_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) struct cw_battery *cw_bat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) cw_bat = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) case POWER_SUPPLY_PROP_CAPACITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) val->intval = cw_bat->soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) val->intval = cw_get_capacity_leve(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) val->intval = cw_bat->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) case POWER_SUPPLY_PROP_PRESENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) val->intval = !!cw_bat->voltage_mv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) val->intval = cw_bat->voltage_mv * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (cw_battery_valid_time_to_empty(cw_bat))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) val->intval = cw_bat->time_to_empty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) val->intval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) case POWER_SUPPLY_PROP_TECHNOLOGY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) case POWER_SUPPLY_PROP_CHARGE_COUNTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) val->intval = cw_bat->charge_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) case POWER_SUPPLY_PROP_CHARGE_FULL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) if (cw_bat->battery.charge_full_design_uah > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) val->intval = cw_bat->battery.charge_full_design_uah;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) val->intval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) case POWER_SUPPLY_PROP_CURRENT_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (cw_battery_valid_time_to_empty(cw_bat) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) cw_bat->battery.charge_full_design_uah > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) /* calculate remaining capacity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) val->intval = cw_bat->battery.charge_full_design_uah;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) val->intval = val->intval * cw_bat->soc / 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) /* estimate current based on time to empty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) val->intval = 60 * val->intval / cw_bat->time_to_empty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) val->intval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) static enum power_supply_property cw_battery_properties[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) POWER_SUPPLY_PROP_CAPACITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) POWER_SUPPLY_PROP_CAPACITY_LEVEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) POWER_SUPPLY_PROP_PRESENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) POWER_SUPPLY_PROP_TECHNOLOGY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) POWER_SUPPLY_PROP_CHARGE_COUNTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) POWER_SUPPLY_PROP_CHARGE_FULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) POWER_SUPPLY_PROP_CURRENT_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) static const struct power_supply_desc cw2015_bat_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) .name = "cw2015-battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) .type = POWER_SUPPLY_TYPE_BATTERY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) .properties = cw_battery_properties,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) .num_properties = ARRAY_SIZE(cw_battery_properties),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) .get_property = cw_battery_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) static int cw2015_parse_properties(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) struct device *dev = cw_bat->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) int length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) length = device_property_count_u8(dev, "cellwise,battery-profile");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) if (length < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) dev_warn(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) "No battery-profile found, using current flash contents\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) } else if (length != CW2015_SIZE_BATINFO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) dev_err(cw_bat->dev, "battery-profile must be %d bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) CW2015_SIZE_BATINFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) cw_bat->bat_profile = devm_kzalloc(dev, length, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) if (!cw_bat->bat_profile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) ret = device_property_read_u8_array(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) "cellwise,battery-profile",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) cw_bat->bat_profile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) cw_bat->dual_cell = device_property_read_bool(dev, "cellwise,dual-cell");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) ret = device_property_read_u32(dev, "cellwise,monitor-interval-ms",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) &cw_bat->poll_interval_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) dev_dbg(cw_bat->dev, "Using default poll interval\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) cw_bat->poll_interval_ms = CW2015_DEFAULT_POLL_INTERVAL_MS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) static const struct regmap_range regmap_ranges_rd_yes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) regmap_reg_range(CW2015_REG_VERSION, CW2015_REG_VERSION),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) regmap_reg_range(CW2015_REG_VCELL, CW2015_REG_CONFIG),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) regmap_reg_range(CW2015_REG_MODE, CW2015_REG_MODE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) regmap_reg_range(CW2015_REG_BATINFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) static const struct regmap_access_table regmap_rd_table = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) .yes_ranges = regmap_ranges_rd_yes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) .n_yes_ranges = 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) static const struct regmap_range regmap_ranges_wr_yes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) regmap_reg_range(CW2015_REG_RRT_ALERT, CW2015_REG_CONFIG),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) regmap_reg_range(CW2015_REG_MODE, CW2015_REG_MODE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) regmap_reg_range(CW2015_REG_BATINFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) static const struct regmap_access_table regmap_wr_table = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) .yes_ranges = regmap_ranges_wr_yes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) .n_yes_ranges = 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) static const struct regmap_range regmap_ranges_vol_yes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) regmap_reg_range(CW2015_REG_VCELL, CW2015_REG_SOC + 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) static const struct regmap_access_table regmap_vol_table = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) .yes_ranges = regmap_ranges_vol_yes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) .n_yes_ranges = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) static const struct regmap_config cw2015_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) .reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) .val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) .rd_table = ®map_rd_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) .wr_table = ®map_wr_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) .volatile_table = ®map_vol_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) .max_register = CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) static int cw_bat_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) struct cw_battery *cw_bat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) struct power_supply_config psy_cfg = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) cw_bat = devm_kzalloc(&client->dev, sizeof(*cw_bat), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) if (!cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) i2c_set_clientdata(client, cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) cw_bat->dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) cw_bat->soc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) ret = cw2015_parse_properties(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) dev_err(cw_bat->dev, "Failed to parse cw2015 properties\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) cw_bat->regmap = devm_regmap_init_i2c(client, &cw2015_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) if (IS_ERR(cw_bat->regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) dev_err(cw_bat->dev, "Failed to allocate regmap: %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) PTR_ERR(cw_bat->regmap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) return PTR_ERR(cw_bat->regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) ret = cw_init(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) dev_err(cw_bat->dev, "Init failed: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) psy_cfg.drv_data = cw_bat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) psy_cfg.fwnode = dev_fwnode(cw_bat->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) cw_bat->rk_bat = devm_power_supply_register(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) &cw2015_bat_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) if (IS_ERR(cw_bat->rk_bat)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) /* try again if this happens */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) dev_err_probe(&client->dev, PTR_ERR(cw_bat->rk_bat),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) "Failed to register power supply\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) return PTR_ERR(cw_bat->rk_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) ret = power_supply_get_battery_info(cw_bat->rk_bat, &cw_bat->battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) dev_warn(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) "No monitored battery, some properties will be missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) cw_bat->battery_workqueue = create_singlethread_workqueue("rk_battery");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) INIT_DELAYED_WORK(&cw_bat->battery_delay_work, cw_bat_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) queue_delayed_work(cw_bat->battery_workqueue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) &cw_bat->battery_delay_work, msecs_to_jiffies(10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) static int __maybe_unused cw_bat_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) struct cw_battery *cw_bat = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) cancel_delayed_work_sync(&cw_bat->battery_delay_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) static int __maybe_unused cw_bat_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) struct cw_battery *cw_bat = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) queue_delayed_work(cw_bat->battery_workqueue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) &cw_bat->battery_delay_work, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) static SIMPLE_DEV_PM_OPS(cw_bat_pm_ops, cw_bat_suspend, cw_bat_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) static int cw_bat_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) struct cw_battery *cw_bat = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) cancel_delayed_work_sync(&cw_bat->battery_delay_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) power_supply_put_battery_info(cw_bat->rk_bat, &cw_bat->battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) static const struct i2c_device_id cw_bat_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) { "cw2015", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) static const struct of_device_id cw2015_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) { .compatible = "cellwise,cw2015" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) MODULE_DEVICE_TABLE(of, cw2015_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) static struct i2c_driver cw_bat_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) .name = "cw2015",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) .of_match_table = cw2015_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) .pm = &cw_bat_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) .probe_new = cw_bat_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) .remove = cw_bat_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) .id_table = cw_bat_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) module_i2c_driver(cw_bat_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) MODULE_AUTHOR("xhc<xhc@rock-chips.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) MODULE_AUTHOR("Tobias Schramm <t.schramm@manjaro.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) MODULE_DESCRIPTION("cw2015/cw2013 battery driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) MODULE_LICENSE("GPL");