^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * rk817 battery driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2018 Rockchip Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * This program is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * it under the terms of the GNU General Public License as published by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * the Free Software Foundation; either version 2 of the License, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * (at your option) any later version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * MERCHANTABILITY or FITNESS FR A PARTICULAR PURPOSE. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define pr_fmt(fmt) "rk817-bat: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/extcon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/fb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/iio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/iio/iio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/mfd/rk808.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/of_gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/power/rk_usbbc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/rk_keys.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <linux/wakelock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static int dbg_enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) module_param_named(dbg_level, dbg_enable, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define DBG(args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (dbg_enable) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) pr_info(args); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define BAT_INFO(fmt, args...) pr_info(fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define DRIVER_VERSION "1.00"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define SFT_SET_KB 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define DIV(x) ((x) ? (x) : 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #define ENABLE 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define DISABLE 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define MAX_INTERPOLATE 1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define MAX_PERCENTAGE 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #define MAX_INT 0x7FFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /* RK818_GGCON */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define OCV_SAMP_MIN_MSK 0x0c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #define OCV_SAMP_8MIN (0x00 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #define ADC_CAL_8MIN 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #define RELAX_VOL12_UPD_MSK (RELAX_VOL1_UPD | RELAX_VOL2_UPD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #define MINUTE(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) ((x) * 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #define ADC_TO_CURRENT(adc_value, samp_res) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) (adc_value * 172 / 1000 / samp_res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define CURRENT_TO_ADC(current, samp_res) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) (current * 1000 * samp_res / 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define ADC_TO_CAPACITY(adc_value, samp_res) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) (adc_value / 1000 * 172 / 3600 / samp_res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define CAPACITY_TO_ADC(capacity, samp_res) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) (capacity * samp_res * 3600 / 172 * 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #define ADC_TO_CAPACITY_UAH(adc_value, samp_res) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) (adc_value / 3600 * 172 / samp_res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #define ADC_TO_CAPACITY_MAH(adc_value, samp_res) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) (adc_value / 1000 * 172 / 3600 / samp_res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* THREAML_REG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #define TEMP_85C (0x00 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #define TEMP_95C (0x01 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #define TEMP_105C (0x02 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #define TEMP_115C (0x03 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #define ZERO_LOAD_LVL1 1400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) #define ZERO_LOAD_LVL2 600
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* zero algorithm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #define PWROFF_THRESD 3400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #define MIN_ZERO_DSOC_ACCURACY 10 /*0.01%*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #define MIN_ZERO_OVERCNT 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #define MIN_ACCURACY 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #define DEF_PWRPATH_RES 50
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #define WAIT_DSOC_DROP_SEC 15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #define WAIT_SHTD_DROP_SEC 30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #define MIN_ZERO_GAP_XSOC1 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #define MIN_ZERO_GAP_XSOC2 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #define MIN_ZERO_GAP_XSOC3 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #define MIN_ZERO_GAP_CALIB 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #define ADC_CALIB_THRESHOLD 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #define ADC_CALIB_LMT_MIN 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) #define ADC_CALIB_CNT 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /* default param */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) #define DEFAULT_BAT_RES 135
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) #define DEFAULT_SLP_ENTER_CUR 300
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) #define DEFAULT_SLP_EXIT_CUR 300
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) #define DEFAULT_SLP_FILTER_CUR 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) #define DEFAULT_PWROFF_VOL_THRESD 3400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #define DEFAULT_MONITOR_SEC 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #define DEFAULT_ALGR_VOL_THRESD1 3850
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) #define DEFAULT_ALGR_VOL_THRESD2 3950
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) #define DEFAULT_CHRG_VOL_SEL CHRG_VOL4200MV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #define DEFAULT_CHRG_CUR_SEL CHRG_CUR1400MA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #define DEFAULT_CHRG_CUR_INPUT INPUT_CUR2000MA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) #define DEFAULT_POFFSET 42
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) #define DEFAULT_MAX_SOC_OFFSET 60
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) #define DEFAULT_FB_TEMP TEMP_115C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #define DEFAULT_ENERGY_MODE 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #define DEFAULT_ZERO_RESERVE_DSOC 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) #define DEFAULT_SAMPLE_RES 20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /* sample resistor and division */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) #define SAMPLE_RES_10MR 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) #define SAMPLE_RES_20MR 20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) #define SAMPLE_RES_DIV1 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) #define SAMPLE_RES_DIV2 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /* sleep */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) #define SLP_CURR_MAX 40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) #define SLP_CURR_MIN 6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) #define LOW_PWR_SLP_CURR_MAX 20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) #define LOW_PWR_SLP_CURR_MIN 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) #define DISCHRG_TIME_STEP1 MINUTE(10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) #define DISCHRG_TIME_STEP2 MINUTE(60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) #define SLP_DSOC_VOL_THRESD 3600
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) #define REBOOT_PERIOD_SEC 180
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #define REBOOT_MAX_CNT 80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) #define TIMER_MS_COUNTS 1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /* fcc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #define MIN_FCC 500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) #define CAP_INVALID 0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* virtual params */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) #define VIRTUAL_CURRENT 1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) #define VIRTUAL_VOLTAGE 3888
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) #define VIRTUAL_SOC 66
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) #define VIRTUAL_PRESET 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) #define VIRTUAL_TEMPERATURE 188
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) #define VIRTUAL_STATUS POWER_SUPPLY_STATUS_CHARGING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) #define FINISH_CHRG_CUR1 1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) #define FINISH_CHRG_CUR2 1500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) #define FINISH_MAX_SOC_DELAY 20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) #define TERM_CHRG_DSOC 88
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) #define TERM_CHRG_CURR 600
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) #define TERM_CHRG_K 650
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) #define SIMULATE_CHRG_INTV 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) #define SIMULATE_CHRG_CURR 400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) #define SIMULATE_CHRG_K 1500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) #define FULL_CHRG_K 400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) enum work_mode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) MODE_ZERO = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) MODE_FINISH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) MODE_SMOOTH_CHRG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) MODE_SMOOTH_DISCHRG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) MODE_SMOOTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) enum charge_status {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) CHRG_OFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) DEAD_CHRG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) TRICKLE_CHRG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) CC_OR_CV_CHRG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) CHARGE_FINISH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) USB_OVER_VOL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) BAT_TMP_ERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) BAT_TIM_ERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) enum bat_mode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) MODE_BATTARY = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) MODE_VIRTUAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) enum rk817_sample_time {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) S_8_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) S_16_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) S_32_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) S_48_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) enum rk817_output_mode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) AVERAGE_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) INSTANT_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) enum rk817_battery_fields {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) ADC_SLP_RATE, BAT_CUR_ADC_EN, BAT_VOL_ADC_EN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) USB_VOL_ADC_EN, TS_ADC_EN, SYS_VOL_ADC_EN, GG_EN, /*ADC_CONFIG0*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) CUR_ADC_DITH_SEL, CUR_ADC_DIH_EN, CUR_ADC_CHOP_EN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) CUR_ADC_CHOP_SEL, CUR_ADC_CHOP_VREF_EN, /*CUR_ADC_CFG0*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) CUR_ADC_VCOM_SEL, CUR_ADC_VCOM_BUF_INC, CUR_ADC_VREF_BUF_INC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) CUR_ADC_BIAS_DEC, CUR_ADC_IBIAS_SEL,/*CUR_ADC_CFG1*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) VOL_ADC_EXT_VREF_EN, VOL_ADC_DITH_SEL, VOL_ADC_DITH_EN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) VOL_ADC_CHOP_EN, VOL_ADC_CHOP_SEL, VOL_ADC_CHOP_VREF_EN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) VOL_ADC_VCOM_SEL, VOL_ADC_VCOM_BUF_INC, VOL_ADC_VREF_BUF_INC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) VOL_ADC_IBIAS_SEL, /*VOL_ADC_CFG1*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) RLX_CUR_FILTER, TS_FUN, VOL_ADC_TSCUR_SEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) VOL_CALIB_UPD, CUR_CALIB_UPD, /*ADC_CONFIG1*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) CUR_OUT_MOD, VOL_OUT_MOD, FRAME_SMP_INTERV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) ADC_OFF_CAL_INTERV, RLX_SPT, /*GG_CON*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) OCV_UPD, RELAX_STS, RELAX_VOL2_UPD, RELAX_VOL1_UPD, BAT_CON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) QMAX_UPD_SOFT, TERM_UPD, OCV_STS, /*GG_STS*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) RELAX_THRE_H, RELAX_THRE_L, /*RELAX_THRE*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) RELAX_VOL1_H, RELAX_VOL1_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) RELAX_VOL2_H, RELAX_VOL2_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) RELAX_CUR1_H, RELAX_CUR1_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) RELAX_CUR2_H, RELAX_CUR2_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) OCV_THRE_VOL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) OCV_VOL_H, OCV_VOL_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) OCV_VOL0_H, OCV_VOL0_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) OCV_CUR_H, OCV_CUR_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) OCV_CUR0_H, OCV_CUR0_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) PWRON_VOL_H, PWRON_VOL_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) PWRON_CUR_H, PWRON_CUR_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) OFF_CNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) Q_INIT_H3, Q_INIT_H2, Q_INIT_L1, Q_INIT_L0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) Q_PRESS_H3, Q_PRESS_H2, Q_PRESS_L1, Q_PRESS_L0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) BAT_VOL_H, BAT_VOL_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) BAT_CUR_H, BAT_CUR_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) BAT_TS_H, BAT_TS_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) USB_VOL_H, USB_VOL_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) SYS_VOL_H, SYS_VOL_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) Q_MAX_H3, Q_MAX_H2, Q_MAX_L1, Q_MAX_L0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) Q_TERM_H3, Q_TERM_H2, Q_TERM_L1, Q_TERM_L0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) Q_OCV_H3, Q_OCV_H2, Q_OCV_L1, Q_OCV_L0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) OCV_CNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) SLEEP_CON_SAMP_CUR_H, SLEEP_CON_SAMP_CUR_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) CAL_OFFSET_H, CAL_OFFSET_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) VCALIB0_H, VCALIB0_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) VCALIB1_H, VCALIB1_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) IOFFSET_H, IOFFSET_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) BAT_R0, SOC_REG0, SOC_REG1, SOC_REG2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) REMAIN_CAP_REG2, REMAIN_CAP_REG1, REMAIN_CAP_REG0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) NEW_FCC_REG2, NEW_FCC_REG1, NEW_FCC_REG0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) RESET_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) FG_INIT, HALT_CNT_REG, CALC_REST_REGL, CALC_REST_REGH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) VOL_ADC_B3, VOL_ADC_B2, VOL_ADC_B1, VOL_ADC_B0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) VOL_ADC_K3, VOL_ADC_K2, VOL_ADC_K1, VOL_ADC_K0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) BAT_EXS, CHG_STS, BAT_OVP_STS, CHRG_IN_CLAMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) CHIP_NAME_H, CHIP_NAME_L,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) PLUG_IN_STS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) F_MAX_FIELDS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static const struct reg_field rk817_battery_reg_fields[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) [ADC_SLP_RATE] = REG_FIELD(0x50, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) [BAT_CUR_ADC_EN] = REG_FIELD(0x50, 2, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) [BAT_VOL_ADC_EN] = REG_FIELD(0x50, 3, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) [USB_VOL_ADC_EN] = REG_FIELD(0x50, 4, 4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) [TS_ADC_EN] = REG_FIELD(0x50, 5, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) [SYS_VOL_ADC_EN] = REG_FIELD(0x50, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) [GG_EN] = REG_FIELD(0x50, 7, 7),/*ADC_CONFIG0*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) [CUR_ADC_DITH_SEL] = REG_FIELD(0x51, 1, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) [CUR_ADC_DIH_EN] = REG_FIELD(0x51, 4, 4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) [CUR_ADC_CHOP_EN] = REG_FIELD(0x51, 5, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) [CUR_ADC_CHOP_SEL] = REG_FIELD(0x51, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) [CUR_ADC_CHOP_VREF_EN] = REG_FIELD(0x51, 7, 7), /*CUR_ADC_COFG0*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) [CUR_ADC_VCOM_SEL] = REG_FIELD(0x52, 0, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) [CUR_ADC_VCOM_BUF_INC] = REG_FIELD(0x52, 2, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) [CUR_ADC_VREF_BUF_INC] = REG_FIELD(0x52, 3, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) [CUR_ADC_BIAS_DEC] = REG_FIELD(0x52, 4, 4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) [CUR_ADC_IBIAS_SEL] = REG_FIELD(0x52, 5, 6), /*CUR_ADC_COFG1*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) [VOL_ADC_EXT_VREF_EN] = REG_FIELD(0x53, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) [VOL_ADC_DITH_SEL] = REG_FIELD(0x53, 1, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) [VOL_ADC_DITH_EN] = REG_FIELD(0x53, 4, 4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) [VOL_ADC_CHOP_EN] = REG_FIELD(0x53, 5, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) [VOL_ADC_CHOP_SEL] = REG_FIELD(0x53, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) [VOL_ADC_CHOP_VREF_EN] = REG_FIELD(0x53, 7, 7),/*VOL_ADC_COFG0*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) [VOL_ADC_VCOM_SEL] = REG_FIELD(0x54, 0, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) [VOL_ADC_VCOM_BUF_INC] = REG_FIELD(0x54, 2, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) [VOL_ADC_VREF_BUF_INC] = REG_FIELD(0x54, 3, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) [VOL_ADC_IBIAS_SEL] = REG_FIELD(0x54, 5, 6), /*VOL_ADC_COFG1*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) [RLX_CUR_FILTER] = REG_FIELD(0x55, 0, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) [TS_FUN] = REG_FIELD(0x55, 3, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) [VOL_ADC_TSCUR_SEL] = REG_FIELD(0x55, 4, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) [VOL_CALIB_UPD] = REG_FIELD(0x55, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) [CUR_CALIB_UPD] = REG_FIELD(0x55, 7, 7), /*ADC_CONFIG1*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) [CUR_OUT_MOD] = REG_FIELD(0x56, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) [VOL_OUT_MOD] = REG_FIELD(0x56, 1, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) [FRAME_SMP_INTERV] = REG_FIELD(0x56, 2, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) [ADC_OFF_CAL_INTERV] = REG_FIELD(0x56, 4, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) [RLX_SPT] = REG_FIELD(0x56, 6, 7), /*GG_CON*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) [OCV_UPD] = REG_FIELD(0x57, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) [RELAX_STS] = REG_FIELD(0x57, 1, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) [RELAX_VOL2_UPD] = REG_FIELD(0x57, 2, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) [RELAX_VOL1_UPD] = REG_FIELD(0x57, 3, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) [BAT_CON] = REG_FIELD(0x57, 4, 4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) [QMAX_UPD_SOFT] = REG_FIELD(0x57, 5, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) [TERM_UPD] = REG_FIELD(0x57, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) [OCV_STS] = REG_FIELD(0x57, 7, 7), /*GG_STS*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) [RELAX_THRE_H] = REG_FIELD(0x58, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) [RELAX_THRE_L] = REG_FIELD(0x59, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) [RELAX_VOL1_H] = REG_FIELD(0x5A, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) [RELAX_VOL1_L] = REG_FIELD(0x5B, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) [RELAX_VOL2_H] = REG_FIELD(0x5C, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) [RELAX_VOL2_L] = REG_FIELD(0x5D, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) [RELAX_CUR1_H] = REG_FIELD(0x5E, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) [RELAX_CUR1_L] = REG_FIELD(0x5F, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) [RELAX_CUR2_H] = REG_FIELD(0x60, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) [RELAX_CUR2_L] = REG_FIELD(0x61, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) [OCV_THRE_VOL] = REG_FIELD(0x62, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) [OCV_VOL_H] = REG_FIELD(0x63, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) [OCV_VOL_L] = REG_FIELD(0x64, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) [OCV_VOL0_H] = REG_FIELD(0x65, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) [OCV_VOL0_L] = REG_FIELD(0x66, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) [OCV_CUR_H] = REG_FIELD(0x67, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) [OCV_CUR_L] = REG_FIELD(0x68, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) [OCV_CUR0_H] = REG_FIELD(0x69, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) [OCV_CUR0_L] = REG_FIELD(0x6A, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) [PWRON_VOL_H] = REG_FIELD(0x6B, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) [PWRON_VOL_L] = REG_FIELD(0x6C, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) [PWRON_CUR_H] = REG_FIELD(0x6D, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) [PWRON_CUR_L] = REG_FIELD(0x6E, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) [OFF_CNT] = REG_FIELD(0x6F, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) [Q_INIT_H3] = REG_FIELD(0x70, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) [Q_INIT_H2] = REG_FIELD(0x71, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) [Q_INIT_L1] = REG_FIELD(0x72, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) [Q_INIT_L0] = REG_FIELD(0x73, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) [Q_PRESS_H3] = REG_FIELD(0x74, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) [Q_PRESS_H2] = REG_FIELD(0x75, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) [Q_PRESS_L1] = REG_FIELD(0x76, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) [Q_PRESS_L0] = REG_FIELD(0x77, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) [BAT_VOL_H] = REG_FIELD(0x78, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) [BAT_VOL_L] = REG_FIELD(0x79, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) [BAT_CUR_H] = REG_FIELD(0x7A, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) [BAT_CUR_L] = REG_FIELD(0x7B, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) [BAT_TS_H] = REG_FIELD(0x7C, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) [BAT_TS_L] = REG_FIELD(0x7D, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) [USB_VOL_H] = REG_FIELD(0x7E, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) [USB_VOL_L] = REG_FIELD(0x7F, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) [SYS_VOL_H] = REG_FIELD(0x80, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) [SYS_VOL_L] = REG_FIELD(0x81, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) [Q_MAX_H3] = REG_FIELD(0x82, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) [Q_MAX_H2] = REG_FIELD(0x83, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) [Q_MAX_L1] = REG_FIELD(0x84, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) [Q_MAX_L0] = REG_FIELD(0x85, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) [Q_TERM_H3] = REG_FIELD(0x86, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) [Q_TERM_H2] = REG_FIELD(0x87, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) [Q_TERM_L1] = REG_FIELD(0x88, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) [Q_TERM_L0] = REG_FIELD(0x89, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) [Q_OCV_H3] = REG_FIELD(0x8A, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) [Q_OCV_H2] = REG_FIELD(0x8B, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) [Q_OCV_L1] = REG_FIELD(0x8C, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) [Q_OCV_L0] = REG_FIELD(0x8D, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) [OCV_CNT] = REG_FIELD(0x8E, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) [SLEEP_CON_SAMP_CUR_H] = REG_FIELD(0x8F, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) [SLEEP_CON_SAMP_CUR_L] = REG_FIELD(0x90, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) [CAL_OFFSET_H] = REG_FIELD(0x91, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) [CAL_OFFSET_L] = REG_FIELD(0x92, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) [VCALIB0_H] = REG_FIELD(0x93, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) [VCALIB0_L] = REG_FIELD(0x94, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) [VCALIB1_H] = REG_FIELD(0x95, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) [VCALIB1_L] = REG_FIELD(0x96, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) [IOFFSET_H] = REG_FIELD(0x97, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) [IOFFSET_L] = REG_FIELD(0x98, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) [BAT_R0] = REG_FIELD(0x99, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) [SOC_REG0] = REG_FIELD(0x9A, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) [SOC_REG1] = REG_FIELD(0x9B, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) [SOC_REG2] = REG_FIELD(0x9C, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) [REMAIN_CAP_REG0] = REG_FIELD(0x9D, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) [REMAIN_CAP_REG1] = REG_FIELD(0x9E, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) [REMAIN_CAP_REG2] = REG_FIELD(0x9F, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) [NEW_FCC_REG0] = REG_FIELD(0xA0, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) [NEW_FCC_REG1] = REG_FIELD(0xA1, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) [NEW_FCC_REG2] = REG_FIELD(0xA2, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) [RESET_MODE] = REG_FIELD(0xA3, 0, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) [FG_INIT] = REG_FIELD(0xA5, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) [HALT_CNT_REG] = REG_FIELD(0xA6, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) [CALC_REST_REGL] = REG_FIELD(0xA7, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) [CALC_REST_REGH] = REG_FIELD(0xA8, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) [VOL_ADC_B3] = REG_FIELD(0xA9, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) [VOL_ADC_B2] = REG_FIELD(0xAA, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) [VOL_ADC_B1] = REG_FIELD(0xAB, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) [VOL_ADC_B0] = REG_FIELD(0xAC, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) [VOL_ADC_K3] = REG_FIELD(0xAD, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) [VOL_ADC_K2] = REG_FIELD(0xAE, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) [VOL_ADC_K1] = REG_FIELD(0xAF, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) [VOL_ADC_K0] = REG_FIELD(0xB0, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) [BAT_EXS] = REG_FIELD(0xEB, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) [CHG_STS] = REG_FIELD(0xEB, 4, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) [BAT_OVP_STS] = REG_FIELD(0xEB, 3, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) [CHRG_IN_CLAMP] = REG_FIELD(0xEB, 2, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) [CHIP_NAME_H] = REG_FIELD(0xED, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) [CHIP_NAME_L] = REG_FIELD(0xEE, 0, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) [PLUG_IN_STS] = REG_FIELD(0xF0, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) struct battery_platform_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) u32 *ocv_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) u32 *zero_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) u32 table_t[4][21];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) int temp_t[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) u32 temp_t_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) u32 *ntc_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) u32 ocv_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) u32 ntc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) int ntc_degree_from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) u32 ntc_factor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) u32 max_input_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) u32 max_chrg_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) u32 max_chrg_voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) u32 lp_input_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) u32 lp_soc_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) u32 lp_soc_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) u32 pwroff_vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) u32 monitor_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) u32 zero_algorithm_vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) u32 zero_reserve_dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) u32 bat_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) u32 design_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) u32 design_qmax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) u32 sleep_enter_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) u32 sleep_exit_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) u32 sleep_filter_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) u32 power_dc2otg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) u32 max_soc_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) u32 bat_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) u32 fb_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) u32 energy_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) u32 cccv_hour;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) u32 dc_det_adc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) int dc_det_pin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) u8 dc_det_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) u32 sample_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) u32 bat_res_up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) u32 bat_res_down;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) u32 design_max_voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) bool extcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) u32 low_pwr_sleep;
^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) struct rk817_battery_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) struct rk808 *rk817;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) struct power_supply *bat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) struct power_supply *chg_psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) struct power_supply *usb_psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) struct power_supply *ac_psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) struct regmap_field *rmap_fields[F_MAX_FIELDS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) struct battery_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) struct workqueue_struct *bat_monitor_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) struct delayed_work bat_delay_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) struct delayed_work calib_delay_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) struct work_struct resume_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) struct wake_lock wake_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) struct timer_list caltimer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) int res_div;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) int bat_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) bool is_first_power_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) int chrg_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) int res_fac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) int over_20mR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) bool is_initialized;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) bool bat_first_power_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) u8 ac_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) u8 usb_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) u8 otg_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) u8 dc_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) u8 prop_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) int cvtlmt_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) int current_avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) int current_relax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) int voltage_usb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) int voltage_sys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) int voltage_avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) int voltage_ocv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) int voltage_relax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) int voltage_k;/* VCALIB0 VCALIB1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) int voltage_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) u32 remain_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) int design_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) int nac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) int fcc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) int lock_fcc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) int qmax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) int dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) int rsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) int poffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) int fake_offline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) int age_ocv_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) bool age_allow_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) int age_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) int age_ocv_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) int pwron_voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) int age_voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) int age_adjust_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) unsigned long age_keep_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) int zero_timeout_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) int zero_remain_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) int zero_dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) int zero_linek;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) u64 zero_drop_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) u64 shtd_drop_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) int powerpatch_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) int zero_voltage_avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) int zero_current_avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) int zero_vsys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) int zero_dead_voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) int zero_dead_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) int zero_dead_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) int zero_batvol_to_ocv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) int zero_batocv_to_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) int zero_batocv_to_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) int zero_xsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) unsigned long finish_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) time64_t rtc_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) int sm_remain_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) int sm_linek;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) int sm_chrg_dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) int sm_dischrg_dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) int smooth_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) int algo_rest_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) int algo_rest_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) int sleep_sum_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) int sleep_remain_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) unsigned long sleep_dischrg_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) unsigned long sleep_sum_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) bool sleep_chrg_online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) u8 sleep_chrg_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) bool adc_allow_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) int fb_blank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) bool s2r; /*suspend to resume*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) u32 work_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) int temperature;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) int chrg_cur_lp_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) int chrg_vol_sel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) int chrg_cur_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) int chrg_cur_sel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) u32 monitor_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) u32 pwroff_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) u32 adc_calib_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) unsigned long chrg_finish_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) unsigned long boot_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) unsigned long flat_match_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) unsigned long plug_in_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) unsigned long plug_out_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) u8 halt_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) bool is_halt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) bool is_max_soc_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) bool is_sw_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) bool is_ocv_calib;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) bool is_first_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) bool is_force_calib;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) int last_dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) u8 cvtlmt_int_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) u8 slp_dcdc_en_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) int ocv_pre_dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) int ocv_new_dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) int max_pre_dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) int max_new_dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) int force_pre_dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) int force_new_dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) int dbg_cap_low0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) int dbg_pwr_dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) int dbg_pwr_rsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) int dbg_pwr_vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) int dbg_chrg_min[10];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) int dbg_meet_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) int dbg_calc_dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) int dbg_calc_rsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) int is_charging;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) unsigned long charge_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) u8 plugin_trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) u8 plugout_trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) int plugin_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) int plugout_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) int chip_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) int is_register_chg_psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) bool change; /* Battery status change, report information */
^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 void rk817_bat_resume_work(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) static u64 get_boot_sec(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) struct timespec64 ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) ktime_get_boottime_ts64(&ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) return ts.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) static unsigned long base2sec(unsigned long x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) if (x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) return (get_boot_sec() > x) ? (get_boot_sec() - x) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) static u32 interpolate(int value, u32 *table, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) u8 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) u16 d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) for (i = 0; i < size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) if (value < table[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) if ((i > 0) && (i < size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) d = (value - table[i - 1]) * (MAX_INTERPOLATE / (size - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) d /= table[i] - table[i - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) d = d + (i - 1) * (MAX_INTERPOLATE / (size - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) d = i * ((MAX_INTERPOLATE + size / 2) / size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) if (d > 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) d = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) return d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) /* (a * b) / c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) static int32_t ab_div_c(u32 a, u32 b, u32 c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) bool sign;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) u32 ans = MAX_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) int tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) sign = ((((a ^ b) ^ c) & 0x80000000) != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) if (c != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) if (sign)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) c = -c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) tmp = (a * b + (c >> 1)) / c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) if (tmp < MAX_INT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) ans = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) if (sign)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) ans = -ans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) return ans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) static int rk817_bat_field_read(struct rk817_battery_device *battery,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) enum rk817_battery_fields field_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) ret = regmap_field_read(battery->rmap_fields[field_id], &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) static int rk817_bat_field_write(struct rk817_battery_device *battery,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) enum rk817_battery_fields field_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) unsigned int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) return regmap_field_write(battery->rmap_fields[field_id], val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) /*cal_offset: current offset value*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) static int rk817_bat_get_coffset(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) int coffset_value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) coffset_value |= rk817_bat_field_read(battery, CAL_OFFSET_H) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) coffset_value |= rk817_bat_field_read(battery, CAL_OFFSET_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) return coffset_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) static void rk817_bat_set_coffset(struct rk817_battery_device *battery, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) u8 buf = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) buf = (val >> 8) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) rk817_bat_field_write(battery, CAL_OFFSET_H, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) buf = (val >> 0) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) rk817_bat_field_write(battery, CAL_OFFSET_L, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) /* current offset value calculated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) static int rk817_bat_get_ioffset(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) int ioffset_value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) ioffset_value |= rk817_bat_field_read(battery, IOFFSET_H) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) ioffset_value |= rk817_bat_field_read(battery, IOFFSET_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) return ioffset_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) static void rk817_bat_current_calibration(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) int pwron_value, ioffset, cal_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) pwron_value = rk817_bat_field_read(battery, PWRON_CUR_H) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) pwron_value |= rk817_bat_field_read(battery, PWRON_CUR_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) ioffset = rk817_bat_get_ioffset(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) DBG("Caloffset: 0x%x\n", rk817_bat_get_coffset(battery));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) DBG("IOFFSET: 0x%x\n", ioffset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) if (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) cal_offset = pwron_value + ioffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) cal_offset = ioffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) rk817_bat_set_coffset(battery, cal_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) DBG("Caloffset: 0x%x\n", rk817_bat_get_coffset(battery));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) static int rk817_bat_get_vaclib0(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) int vcalib_value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) vcalib_value |= rk817_bat_field_read(battery, VCALIB0_H) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) vcalib_value |= rk817_bat_field_read(battery, VCALIB0_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) return vcalib_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) static int rk817_bat_get_vaclib1(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) int vcalib_value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) vcalib_value |= rk817_bat_field_read(battery, VCALIB1_H) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) vcalib_value |= rk817_bat_field_read(battery, VCALIB1_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) return vcalib_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) static void rk817_bat_init_voltage_kb(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) int vcalib0, vcalib1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) vcalib0 = rk817_bat_get_vaclib0(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) vcalib1 = rk817_bat_get_vaclib1(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) if (battery->chip_id == RK809_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) battery->voltage_k = (1050 - 600) * 1000 / DIV(vcalib1 - vcalib0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) battery->voltage_b = 1050 - (battery->voltage_k * vcalib1) / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) battery->voltage_k = (4025 - 2300) * 1000 / DIV(vcalib1 - vcalib0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) battery->voltage_b = 4025 - (battery->voltage_k * vcalib1) / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) static void rk817_bat_restart_relax(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) rk817_bat_field_write(battery, RELAX_VOL1_UPD, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) rk817_bat_field_write(battery, RELAX_VOL2_UPD, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) static bool is_rk817_bat_relax_mode(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) u8 relax_sts, relax_vol1_upd, relax_vol2_upd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) relax_sts = rk817_bat_field_read(battery, RELAX_STS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) relax_vol1_upd = rk817_bat_field_read(battery, RELAX_VOL1_UPD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) relax_vol2_upd = rk817_bat_field_read(battery, RELAX_VOL2_UPD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) DBG("RELAX_STS: %d\n", relax_sts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) DBG("RELAX_VOL1_UPD: %d\n", relax_vol1_upd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) DBG("RELAX_VOL2_UPD: %d\n", relax_vol2_upd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) if (relax_sts && relax_vol1_upd && relax_vol2_upd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) static u16 rk817_bat_get_relax_vol1(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) u16 vol, val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) val = rk817_bat_field_read(battery, RELAX_VOL1_H) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) val |= rk817_bat_field_read(battery, RELAX_VOL1_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) vol = battery->voltage_k * val / 1000 + battery->voltage_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) return vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) static u16 rk817_bat_get_relax_vol2(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) u16 vol, val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) val = rk817_bat_field_read(battery, RELAX_VOL2_H) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) val |= rk817_bat_field_read(battery, RELAX_VOL2_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) vol = battery->voltage_k * val / 1000 + battery->voltage_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) return vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) static u16 rk817_bat_get_relax_voltage(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) u16 relax_vol1, relax_vol2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) if (!is_rk817_bat_relax_mode(battery))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) relax_vol1 = rk817_bat_get_relax_vol1(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) relax_vol2 = rk817_bat_get_relax_vol2(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) return relax_vol1 > relax_vol2 ? relax_vol1 : relax_vol2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) static void rk817_bat_set_relax_sample(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) u8 buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) int enter_thres, filter_thres;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) struct battery_platform_data *pdata = battery->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) filter_thres = pdata->sleep_filter_current * 1000 / 1506;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) enter_thres = CURRENT_TO_ADC(pdata->sleep_enter_current,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) battery->res_div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) filter_thres = CURRENT_TO_ADC(pdata->sleep_filter_current,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) battery->res_div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) /* set relax enter and exit threshold */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) buf = (enter_thres >> 8) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) rk817_bat_field_write(battery, RELAX_THRE_H, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) buf = enter_thres & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) rk817_bat_field_write(battery, RELAX_THRE_L, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) /* set sample current threshold */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) buf = (filter_thres >> 8) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) rk817_bat_field_write(battery, SLEEP_CON_SAMP_CUR_H, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) buf = filter_thres & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) rk817_bat_field_write(battery, SLEEP_CON_SAMP_CUR_L, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) /* reset relax update state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) rk817_bat_restart_relax(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) DBG("<%s>. sleep_enter_current = %d, sleep_exit_current = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) __func__, pdata->sleep_enter_current, pdata->sleep_exit_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) /* runtime OCV voltage, |RLX_VOL2 - RLX_VOL1| < OCV_THRE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) * the OCV reg update every 120s
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) static void rk817_bat_ocv_thre(struct rk817_battery_device *battery, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) rk817_bat_field_write(battery, OCV_THRE_VOL, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) static int rk817_bat_get_ocv_voltage(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) int vol, val = 0, vol_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) val = rk817_bat_field_read(battery, OCV_VOL_H) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) val |= rk817_bat_field_read(battery, OCV_VOL_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) vol = battery->voltage_k * val / 1000 + battery->voltage_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) if (battery->chip_id == RK809_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) vol_temp = vol * battery->pdata->bat_res_up /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) battery->pdata->bat_res_down + vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) vol = vol_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) return vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) static int rk817_bat_get_ocv0_voltage0(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) int vol, val = 0, vol_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) val = rk817_bat_field_read(battery, OCV_VOL0_H) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) val |= rk817_bat_field_read(battery, OCV_VOL0_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) vol = battery->voltage_k * val / 1000 + battery->voltage_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) if (battery->chip_id == RK809_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) vol_temp = vol * battery->pdata->bat_res_up /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) battery->pdata->bat_res_down + vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) vol = vol_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) return vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) /* power on battery voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) static int rk817_bat_get_pwron_voltage(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) int vol, val = 0, vol_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) val = rk817_bat_field_read(battery, PWRON_VOL_H) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) val |= rk817_bat_field_read(battery, PWRON_VOL_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) vol = battery->voltage_k * val / 1000 + battery->voltage_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) if (battery->chip_id == RK809_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) vol_temp = vol * battery->pdata->bat_res_up /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) battery->pdata->bat_res_down + vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) vol = vol_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) return vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) static int rk817_bat_get_battery_voltage(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) int vol, val = 0, vol_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) int vcalib0, vcalib1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) vcalib0 = rk817_bat_get_vaclib0(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) vcalib1 = rk817_bat_get_vaclib1(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) val = rk817_bat_field_read(battery, BAT_VOL_H) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) val |= rk817_bat_field_read(battery, BAT_VOL_L) << 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) vol = battery->voltage_k * val / 1000 + battery->voltage_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) if (battery->chip_id == RK809_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) vol_temp = vol * battery->pdata->bat_res_up /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) battery->pdata->bat_res_down + vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) vol = vol_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) return vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) static int rk817_bat_get_USB_voltage(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) int vol, val = 0, vol_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) rk817_bat_field_write(battery, USB_VOL_ADC_EN, 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) val = rk817_bat_field_read(battery, USB_VOL_H) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) val |= rk817_bat_field_read(battery, USB_VOL_L) << 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) vol = (battery->voltage_k * val / 1000 + battery->voltage_b) * 60 / 46;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) if (battery->chip_id == RK809_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) vol_temp = vol * battery->pdata->bat_res_up /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) battery->pdata->bat_res_down + vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) vol = vol_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) return vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) static int rk817_bat_get_sys_voltage(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) int vol, val = 0, vol_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) val = rk817_bat_field_read(battery, SYS_VOL_H) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) val |= rk817_bat_field_read(battery, SYS_VOL_L) << 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) vol = (battery->voltage_k * val / 1000 + battery->voltage_b) * 60 / 46;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) if (battery->chip_id == RK809_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) vol_temp = vol * battery->pdata->bat_res_up /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) battery->pdata->bat_res_down + vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) vol = vol_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) return vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) static int rk817_bat_get_avg_current(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) int cur, val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) val = rk817_bat_field_read(battery, BAT_CUR_H) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) val |= rk817_bat_field_read(battery, BAT_CUR_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) if (val & 0x8000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) val -= 0x10000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) cur = ADC_TO_CURRENT(val, battery->res_div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) return cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) static int rk817_bat_get_relax_cur1(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) int cur, val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) val = rk817_bat_field_read(battery, RELAX_CUR1_H) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) val |= rk817_bat_field_read(battery, RELAX_CUR1_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) if (val & 0x8000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) val -= 0x10000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) cur = ADC_TO_CURRENT(val, battery->res_div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) return cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) static int rk817_bat_get_relax_cur2(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) int cur, val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) val |= rk817_bat_field_read(battery, RELAX_CUR2_H) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) val = rk817_bat_field_read(battery, RELAX_CUR2_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) if (val & 0x8000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) val -= 0x10000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) cur = ADC_TO_CURRENT(val, battery->res_div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) return cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) static int rk817_bat_get_relax_current(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) int relax_cur1, relax_cur2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) if (!is_rk817_bat_relax_mode(battery))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) relax_cur1 = rk817_bat_get_relax_cur1(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) relax_cur2 = rk817_bat_get_relax_cur2(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) return (relax_cur1 < relax_cur2) ? relax_cur1 : relax_cur2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) static int rk817_bat_get_ocv_current(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) int cur, val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) val = rk817_bat_field_read(battery, OCV_CUR_H) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) val |= rk817_bat_field_read(battery, OCV_CUR_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) if (val & 0x8000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) val -= 0x10000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) cur = ADC_TO_CURRENT(val, battery->res_div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) return cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) static int rk817_bat_get_ocv_current0(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) int cur, val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) val = rk817_bat_field_read(battery, OCV_CUR0_H) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) val |= rk817_bat_field_read(battery, OCV_CUR0_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) if (val & 0x8000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) val -= 0x10000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) cur = ADC_TO_CURRENT(val, battery->res_div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) return cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) static int rk817_bat_get_pwron_current(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) int cur, val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) val = rk817_bat_field_read(battery, PWRON_CUR_H) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) val |= rk817_bat_field_read(battery, PWRON_CUR_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) if (val & 0x8000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) val -= 0x10000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) cur = ADC_TO_CURRENT(val, battery->res_div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) return cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) static bool rk817_bat_remain_cap_is_valid(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) return !(rk817_bat_field_read(battery, Q_PRESS_H3) & CAP_INVALID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) static u32 rk817_bat_get_capacity_uah(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) u32 val = 0, capacity = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) if (rk817_bat_remain_cap_is_valid(battery)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) val = rk817_bat_field_read(battery, Q_PRESS_H3) << 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) val |= rk817_bat_field_read(battery, Q_PRESS_H2) << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) val |= rk817_bat_field_read(battery, Q_PRESS_L1) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) val |= rk817_bat_field_read(battery, Q_PRESS_L0) << 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) capacity = ADC_TO_CAPACITY_UAH(val, battery->res_div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) DBG("xxxxxxxxxxxxx capacity = %d\n", capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) return capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) static u32 rk817_bat_get_capacity_mah(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) u32 val, capacity = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) if (rk817_bat_remain_cap_is_valid(battery)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) val = rk817_bat_field_read(battery, Q_PRESS_H3) << 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) val |= rk817_bat_field_read(battery, Q_PRESS_H2) << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) val |= rk817_bat_field_read(battery, Q_PRESS_L1) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) val |= rk817_bat_field_read(battery, Q_PRESS_L0) << 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) capacity = ADC_TO_CAPACITY(val, battery->res_div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) DBG("Q_PRESS_H3 = 0x%x\n", rk817_bat_field_read(battery, Q_PRESS_H3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) DBG("Q_PRESS_H2 = 0x%x\n", rk817_bat_field_read(battery, Q_PRESS_H2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) DBG("Q_PRESS_H1 = 0x%x\n", rk817_bat_field_read(battery, Q_PRESS_L1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) DBG("Q_PRESS_H0 = 0x%x\n", rk817_bat_field_read(battery, Q_PRESS_L0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) DBG("xxxxxxxxxxxxx capacity = %d\n", capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) return capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) static void fuel_gauge_q_init_info(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) DBG("Q_INIT_H3 = 0x%x\n", rk817_bat_field_read(battery, Q_INIT_H3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) DBG("Q_INIT_H2 = 0x%x\n", rk817_bat_field_read(battery, Q_INIT_H2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) DBG("Q_INIT_L1 = 0x%x\n", rk817_bat_field_read(battery, Q_INIT_L1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) DBG("Q_INIT_L0 = 0x%x\n", rk817_bat_field_read(battery, Q_INIT_L0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) static void rk817_bat_init_coulomb_cap(struct rk817_battery_device *battery,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) u32 capacity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) u8 buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) u32 cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) fuel_gauge_q_init_info(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) cap = CAPACITY_TO_ADC(capacity, battery->res_div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) DBG("new cap: 0x%x\n", cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) buf = (cap >> 24) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) rk817_bat_field_write(battery, Q_INIT_H3, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) buf = (cap >> 16) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) rk817_bat_field_write(battery, Q_INIT_H2, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) buf = (cap >> 8) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) rk817_bat_field_write(battery, Q_INIT_L1, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) buf = (cap >> 0) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) rk817_bat_field_write(battery, Q_INIT_L0, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) battery->rsoc = capacity * 1000 * 100 / DIV(battery->fcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) battery->remain_cap = capacity * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) DBG("new remaincap: %d\n", battery->remain_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) fuel_gauge_q_init_info(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) static void rk817_bat_save_cap(struct rk817_battery_device *battery,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) int capacity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) u8 buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) static u32 old_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) if (capacity >= battery->qmax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) capacity = battery->qmax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) if (capacity <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) capacity = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) if (old_cap == capacity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) old_cap = capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) buf = (capacity >> 16) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) rk817_bat_field_write(battery, REMAIN_CAP_REG2, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) buf = (capacity >> 8) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) rk817_bat_field_write(battery, REMAIN_CAP_REG1, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) buf = (capacity >> 0) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) rk817_bat_field_write(battery, REMAIN_CAP_REG0, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) static void rk817_bat_update_qmax(struct rk817_battery_device *battery,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) u32 capacity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) u8 buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) u32 cap_adc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) cap_adc = CAPACITY_TO_ADC(capacity, battery->res_div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) buf = (cap_adc >> 24) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) rk817_bat_field_write(battery, Q_MAX_H3, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) buf = (cap_adc >> 16) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) rk817_bat_field_write(battery, Q_MAX_H2, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) buf = (cap_adc >> 8) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) rk817_bat_field_write(battery, Q_MAX_L1, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) buf = (cap_adc >> 0) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) rk817_bat_field_write(battery, Q_MAX_L0, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) battery->qmax = capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) static int rk817_bat_get_qmax(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) u32 capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) int val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) val = rk817_bat_field_read(battery, Q_MAX_H3) << 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) val |= rk817_bat_field_read(battery, Q_MAX_H2) << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) val |= rk817_bat_field_read(battery, Q_MAX_L1) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) val |= rk817_bat_field_read(battery, Q_MAX_L0) << 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) capacity = ADC_TO_CAPACITY(val, battery->res_div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) battery->qmax = capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) return capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) static void rk817_bat_save_fcc(struct rk817_battery_device *battery, int fcc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) u8 buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) buf = (fcc >> 16) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) rk817_bat_field_write(battery, NEW_FCC_REG2, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) buf = (fcc >> 8) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) rk817_bat_field_write(battery, NEW_FCC_REG1, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) buf = (fcc >> 0) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) rk817_bat_field_write(battery, NEW_FCC_REG0, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) static int rk817_bat_get_fcc(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) u32 fcc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) fcc |= rk817_bat_field_read(battery, NEW_FCC_REG2) << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) fcc |= rk817_bat_field_read(battery, NEW_FCC_REG1) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) fcc |= rk817_bat_field_read(battery, NEW_FCC_REG0) << 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) if (fcc < MIN_FCC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) DBG("invalid fcc(%d), use design cap", fcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) fcc = battery->pdata->design_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) rk817_bat_save_fcc(battery, fcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) } else if (fcc > battery->pdata->design_qmax) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) DBG("invalid fcc(%d), use qmax", fcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) fcc = battery->pdata->design_qmax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) rk817_bat_save_fcc(battery, fcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) return fcc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) static int rk817_bat_get_rsoc(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) int remain_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) remain_cap = rk817_bat_get_capacity_uah(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) return remain_cap * 100 / DIV(battery->fcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) static int rk817_bat_get_off_count(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) return rk817_bat_field_read(battery, OFF_CNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) static int rk817_bat_get_ocv_count(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) return rk817_bat_field_read(battery, OCV_CNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) static int rk817_bat_vol_to_soc(struct rk817_battery_device *battery,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) int voltage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) u32 *ocv_table, temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) int ocv_size, ocv_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) ocv_table = battery->pdata->ocv_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) ocv_size = battery->pdata->ocv_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) temp = interpolate(voltage, ocv_table, ocv_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) ocv_soc = ab_div_c(temp, MAX_PERCENTAGE, MAX_INTERPOLATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) return ocv_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) static int rk817_bat_vol_to_cap(struct rk817_battery_device *battery,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) int voltage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) u32 *ocv_table, temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) int ocv_size, capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) ocv_table = battery->pdata->ocv_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) ocv_size = battery->pdata->ocv_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) temp = interpolate(voltage, ocv_table, ocv_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) capacity = ab_div_c(temp, battery->fcc, MAX_INTERPOLATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) return capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) static void rk817_bat_save_dsoc(struct rk817_battery_device *battery,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) int save_soc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) static int last_soc = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) if (last_soc != save_soc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) rk817_bat_field_write(battery, SOC_REG0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) save_soc & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) rk817_bat_field_write(battery, SOC_REG1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) (save_soc >> 8) & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) rk817_bat_field_write(battery, SOC_REG2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) (save_soc >> 16) & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) last_soc = save_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) static int rk817_bat_get_prev_dsoc(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) int soc_save;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) soc_save = rk817_bat_field_read(battery, SOC_REG0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) soc_save |= (rk817_bat_field_read(battery, SOC_REG1) << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) soc_save |= (rk817_bat_field_read(battery, SOC_REG2) << 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) return soc_save;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) static bool is_rk817_bat_first_pwron(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) if (rk817_bat_field_read(battery, BAT_CON)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) rk817_bat_field_write(battery, BAT_CON, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) static int rk817_bat_get_charge_status(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) if (battery->chip_id == RK809_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) if ((battery->voltage_avg > battery->pdata->design_max_voltage) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) (battery->current_avg > 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) ((battery->current_avg < 500) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) (battery->rsoc / 1000 == 100)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) return CHARGE_FINISH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) if (battery->plugin_trigger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) return CC_OR_CV_CHRG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) return CHRG_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) status = rk817_bat_field_read(battery, CHG_STS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) if (status == CC_OR_CV_CHRG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) if (battery->rsoc == 100 * 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) DBG("charge to finish\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) status = CHARGE_FINISH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) switch (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) case CHRG_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) DBG("charge off...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) case DEAD_CHRG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) DBG("dead charge...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) case TRICKLE_CHRG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) DBG("trickle charge...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) case CC_OR_CV_CHRG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) DBG("CC or CV charge...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) case CHARGE_FINISH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) DBG("charge finish...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) case USB_OVER_VOL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) DBG("USB over voltage...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) case BAT_TMP_ERR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) DBG("battery temperature error...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) case BAT_TIM_ERR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) DBG("battery timer error..\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) * cccv and finish switch all the time will cause dsoc freeze,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) * if so, do finish chrg, 100ma is less than min finish_ma.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) static bool rk817_bat_fake_finish_mode(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) if ((battery->rsoc == 100) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) (rk817_bat_get_charge_status(battery) == CC_OR_CV_CHRG) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) (abs(battery->current_avg) <= 100))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) static int get_charge_status(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) return rk817_bat_get_charge_status(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) static bool is_rk817_bat_ocv_valid(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) return (!battery->is_initialized && battery->pwroff_min >= 30);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) static void rk817_bat_gas_gaugle_enable(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) rk817_bat_field_write(battery, GG_EN, ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) static void rk817_bat_gg_con_init(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) rk817_bat_field_write(battery, RLX_SPT, S_8_MIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) rk817_bat_field_write(battery, ADC_OFF_CAL_INTERV, S_8_MIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) rk817_bat_field_write(battery, VOL_OUT_MOD, AVERAGE_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) rk817_bat_field_write(battery, CUR_OUT_MOD, AVERAGE_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) static void rk817_bat_adc_init(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) rk817_bat_field_write(battery, SYS_VOL_ADC_EN, ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) rk817_bat_field_write(battery, TS_ADC_EN, ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) rk817_bat_field_write(battery, USB_VOL_ADC_EN, ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) rk817_bat_field_write(battery, BAT_VOL_ADC_EN, ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) rk817_bat_field_write(battery, BAT_CUR_ADC_EN, ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) static void rk817_bat_init_info(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) battery->design_cap = battery->pdata->design_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) battery->qmax = battery->pdata->design_qmax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) battery->bat_res = battery->pdata->bat_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) battery->monitor_ms = battery->pdata->monitor_sec * TIMER_MS_COUNTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) battery->res_div = (battery->pdata->sample_res == SAMPLE_RES_20MR) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) SAMPLE_RES_DIV2 : SAMPLE_RES_DIV1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) DBG("battery->qmax :%d\n", battery->qmax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) static int rk817_bat_get_prev_cap(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) int val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) val = rk817_bat_field_read(battery, REMAIN_CAP_REG2) << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) val |= rk817_bat_field_read(battery, REMAIN_CAP_REG1) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) val |= rk817_bat_field_read(battery, REMAIN_CAP_REG0) << 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) static u8 rk817_bat_get_halt_cnt(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) return rk817_bat_field_read(battery, HALT_CNT_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) static void rk817_bat_inc_halt_cnt(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) u8 cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) cnt = rk817_bat_field_read(battery, HALT_CNT_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) rk817_bat_field_write(battery, HALT_CNT_REG, ++cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) static bool is_rk817_bat_last_halt(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) int pre_cap = rk817_bat_get_prev_cap(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) int now_cap = rk817_bat_get_capacity_mah(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) /* over 10%: system halt last time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) if (abs(now_cap - pre_cap) > (battery->fcc / 10)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) rk817_bat_inc_halt_cnt(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) static u8 is_rk817_bat_initialized(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) u8 val = rk817_bat_field_read(battery, FG_INIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) if (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) rk817_bat_field_write(battery, FG_INIT, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) static void rk817_bat_calc_sm_linek(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) int linek;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) int diff, delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) int current_avg = rk817_bat_get_avg_current(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) delta = abs(battery->dsoc - battery->rsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) diff = delta * 3;/* speed:3/4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) if (current_avg > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) if (battery->dsoc < battery->rsoc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) linek = 1000 * (delta + diff) / DIV(diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) else if (battery->dsoc > battery->rsoc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) linek = 1000 * diff / DIV(delta + diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) linek = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) if (battery->dsoc < battery->rsoc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) linek = -1000 * diff / DIV(delta + diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) else if (battery->dsoc > battery->rsoc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) linek = -1000 * (delta + diff) / DIV(diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) linek = -1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) battery->dbg_meet_soc = (battery->dsoc >= battery->rsoc) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) (battery->dsoc - diff) : (battery->rsoc - diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) battery->sm_linek = linek;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) battery->sm_remain_cap = battery->remain_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) battery->dbg_calc_dsoc = battery->dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) battery->dbg_calc_rsoc = battery->rsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) static void rk817_bat_smooth_algo_prepare(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) battery->smooth_soc = battery->dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) DBG("<%s>. dsoc=%d, dsoc:smooth_soc=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) __func__, battery->dsoc, battery->smooth_soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) rk817_bat_calc_sm_linek(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) static void rk817_bat_finish_algo_prepare(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) battery->finish_base = get_boot_sec();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) if (!battery->finish_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) battery->finish_base = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) static void rk817_bat_init_dsoc_algorithm(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) if (battery->dsoc >= 100 * 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) battery->dsoc = 100 * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) else if (battery->dsoc <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) battery->dsoc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) /* init current mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) battery->voltage_avg = rk817_bat_get_battery_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) battery->current_avg = rk817_bat_get_avg_current(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) if (get_charge_status(battery) == CHARGE_FINISH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) rk817_bat_finish_algo_prepare(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) battery->work_mode = MODE_FINISH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) rk817_bat_smooth_algo_prepare(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) battery->work_mode = MODE_SMOOTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) DBG("%s, sm_remain_cap = %d, smooth_soc = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) __func__, battery->sm_remain_cap, battery->smooth_soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) static void rk817_bat_first_pwron(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) battery->rsoc =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) rk817_bat_vol_to_soc(battery,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) battery->pwron_voltage) * 1000;/* uAH */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) battery->dsoc = battery->rsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) battery->fcc = battery->pdata->design_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) battery->nac = rk817_bat_vol_to_cap(battery, battery->pwron_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) rk817_bat_update_qmax(battery, battery->qmax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) rk817_bat_save_fcc(battery, battery->fcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) DBG("%s, rsoc = %d, dsoc = %d, fcc = %d, nac = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) __func__, battery->rsoc, battery->dsoc, battery->fcc, battery->nac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) static void rk817_bat_not_first_pwron(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) int now_cap, pre_soc, pre_cap, ocv_cap, ocv_soc, ocv_vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) battery->fcc = rk817_bat_get_fcc(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) pre_soc = rk817_bat_get_prev_dsoc(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) pre_cap = rk817_bat_get_prev_cap(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) now_cap = rk817_bat_get_capacity_mah(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) battery->remain_cap = pre_cap * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) battery->is_halt = is_rk817_bat_last_halt(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) battery->halt_cnt = rk817_bat_get_halt_cnt(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) battery->is_initialized = is_rk817_bat_initialized(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) battery->is_ocv_calib = is_rk817_bat_ocv_valid(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) if (battery->is_halt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) BAT_INFO("system halt last time... cap: pre=%d, now=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) pre_cap, now_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) if (now_cap < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) now_cap = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) rk817_bat_init_coulomb_cap(battery, now_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) pre_cap = now_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) pre_soc = battery->rsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) goto finish;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) } else if (battery->is_initialized) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) /* uboot initialized */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) BAT_INFO("initialized yet..\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) goto finish;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) } else if (battery->is_ocv_calib) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) /* not initialized and poweroff_cnt above 30 min */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) ocv_vol = rk817_bat_get_ocv_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) ocv_soc = rk817_bat_vol_to_soc(battery, ocv_vol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) ocv_cap = rk817_bat_vol_to_cap(battery, ocv_vol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) pre_cap = ocv_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) battery->ocv_pre_dsoc = pre_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) battery->ocv_new_dsoc = ocv_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) if (abs(ocv_soc - pre_soc) >= battery->pdata->max_soc_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) battery->ocv_pre_dsoc = pre_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) battery->ocv_new_dsoc = ocv_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) battery->is_max_soc_offset = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) BAT_INFO("trigger max soc offset, dsoc: %d -> %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) pre_soc, ocv_soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) pre_soc = ocv_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) BAT_INFO("OCV calib: cap=%d, rsoc=%d\n", ocv_cap, ocv_soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) } else if (battery->pwroff_min > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) ocv_vol = rk817_bat_get_ocv_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) ocv_soc = rk817_bat_vol_to_soc(battery, ocv_vol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) ocv_cap = rk817_bat_vol_to_cap(battery, ocv_vol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) battery->force_pre_dsoc = pre_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) battery->force_new_dsoc = ocv_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) if (abs(ocv_soc - pre_soc) >= 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) battery->is_force_calib = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) BAT_INFO("dsoc force calib: %d -> %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) pre_soc, ocv_soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) pre_soc = ocv_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) pre_cap = ocv_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) finish:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) battery->dsoc = pre_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) battery->nac = pre_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) if (battery->nac < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) battery->nac = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) DBG("dsoc=%d cap=%d v=%d ov=%d rv=%d min=%d psoc=%d pcap=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) battery->dsoc, battery->nac, rk817_bat_get_battery_voltage(battery),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) rk817_bat_get_ocv_voltage(battery),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) rk817_bat_get_relax_voltage(battery),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) battery->pwroff_min, rk817_bat_get_prev_dsoc(battery),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) rk817_bat_get_prev_cap(battery));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) static void rk817_bat_rsoc_init(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) battery->is_first_power_on = is_rk817_bat_first_pwron(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) battery->pwroff_min = rk817_bat_get_off_count(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) battery->pwron_voltage = rk817_bat_get_pwron_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) DBG("%s, is_first_power_on = %d, pwroff_min = %d, pwron_voltage = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) __func__, battery->is_first_power_on,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) battery->pwroff_min, battery->pwron_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) if (battery->is_first_power_on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) rk817_bat_first_pwron(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) rk817_bat_not_first_pwron(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) rk817_bat_save_dsoc(battery, battery->dsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) static void rk817_bat_caltimer_isr(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) struct rk817_battery_device *battery =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) from_timer(battery, t, caltimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) mod_timer(&battery->caltimer, jiffies + MINUTE(8) * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) queue_delayed_work(battery->bat_monitor_wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) &battery->calib_delay_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) msecs_to_jiffies(10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) static void rk817_bat_internal_calib(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) struct rk817_battery_device *battery = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) struct rk817_battery_device, calib_delay_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) rk817_bat_current_calibration(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) /* calib voltage kb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) rk817_bat_init_voltage_kb(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) DBG("caltimer:coffset=0x%x\n", rk817_bat_get_coffset(battery));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) static void rk817_bat_init_caltimer(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) timer_setup(&battery->caltimer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) rk817_bat_caltimer_isr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) battery->caltimer.expires = jiffies + MINUTE(8) * HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) add_timer(&battery->caltimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) INIT_DELAYED_WORK(&battery->calib_delay_work, rk817_bat_internal_calib);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) static void rk817_bat_init_fg(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) rk817_bat_adc_init(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) rk817_bat_gas_gaugle_enable(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) rk817_bat_gg_con_init(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) rk817_bat_init_voltage_kb(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) rk817_bat_set_relax_sample(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) rk817_bat_ocv_thre(battery, 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) rk817_bat_init_caltimer(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) rk817_bat_rsoc_init(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) rk817_bat_init_coulomb_cap(battery, battery->nac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) DBG("rsoc%d, fcc = %d\n", battery->rsoc, battery->fcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) rk817_bat_init_dsoc_algorithm(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) battery->qmax = rk817_bat_get_qmax(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) battery->voltage_avg = rk817_bat_get_battery_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) battery->voltage_sys = rk817_bat_get_sys_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) battery->voltage_ocv = rk817_bat_get_ocv_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) battery->voltage_relax = rk817_bat_get_relax_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) battery->current_avg = rk817_bat_get_avg_current(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) battery->dbg_pwr_dsoc = battery->dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) battery->dbg_pwr_rsoc = battery->rsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) battery->dbg_pwr_vol = battery->voltage_avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) battery->temperature = VIRTUAL_TEMPERATURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) DBG("probe init: battery->dsoc = %d, rsoc = %d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) "remain_cap = %d\n, battery_vol = %d\n, system_vol = %d, qmax = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) battery->dsoc, battery->rsoc, battery->remain_cap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) battery->voltage_avg, battery->voltage_sys, battery->qmax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) DBG("OCV_THRE_VOL: 0x%x", rk817_bat_field_read(battery, OCV_THRE_VOL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) static int rk817_bat_parse_dt(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) u32 out_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) int length, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) struct battery_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) struct device *dev = battery->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) struct device_node *np = battery->dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) pdata = devm_kzalloc(battery->dev, sizeof(*pdata), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) if (!pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) battery->pdata = pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) /* init default param */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) pdata->bat_res = DEFAULT_BAT_RES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) pdata->monitor_sec = DEFAULT_MONITOR_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) pdata->pwroff_vol = DEFAULT_PWROFF_VOL_THRESD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) pdata->sleep_exit_current = DEFAULT_SLP_EXIT_CUR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) pdata->sleep_enter_current = DEFAULT_SLP_ENTER_CUR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) pdata->sleep_filter_current = DEFAULT_SLP_FILTER_CUR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) pdata->bat_mode = MODE_BATTARY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) pdata->max_soc_offset = DEFAULT_MAX_SOC_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) pdata->fb_temp = DEFAULT_FB_TEMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) pdata->energy_mode = DEFAULT_ENERGY_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) pdata->zero_reserve_dsoc = DEFAULT_ZERO_RESERVE_DSOC * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) pdata->sample_res = DEFAULT_SAMPLE_RES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) /* parse necessary param */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) if (!of_find_property(np, "ocv_table", &length)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) dev_err(dev, "ocv_table not found!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) pdata->ocv_size = length / sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) if (pdata->ocv_size <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) dev_err(dev, "invalid ocv table\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) size = sizeof(*pdata->ocv_table) * pdata->ocv_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) pdata->ocv_table = devm_kzalloc(battery->dev, size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) if (!pdata->ocv_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) ret = of_property_read_u32_array(np, "ocv_table", pdata->ocv_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) pdata->ocv_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) ret = of_property_read_u32(np, "design_capacity", &out_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) dev_err(dev, "design_capacity not found!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) pdata->design_capacity = out_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) ret = of_property_read_u32(np, "design_qmax", &out_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) dev_err(dev, "design_qmax not found!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) pdata->design_qmax = out_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) /* parse unnecessary param */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) ret = of_property_read_u32(np, "sample_res", &pdata->sample_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) dev_err(dev, "sample_res missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) ret = of_property_read_u32(np, "fb_temperature", &pdata->fb_temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) dev_err(dev, "fb_temperature missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) ret = of_property_read_u32(np, "energy_mode", &pdata->energy_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) dev_err(dev, "energy_mode missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) ret = of_property_read_u32(np, "max_soc_offset",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) &pdata->max_soc_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) dev_err(dev, "max_soc_offset missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) ret = of_property_read_u32(np, "monitor_sec", &pdata->monitor_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) dev_err(dev, "monitor_sec missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) ret = of_property_read_u32(np, "zero_algorithm_vol",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) &pdata->zero_algorithm_vol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) dev_err(dev, "zero_algorithm_vol missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) ret = of_property_read_u32(np, "zero_reserve_dsoc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) &pdata->zero_reserve_dsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) dev_err(dev, "zero_reserve_dsoc missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) pdata->zero_reserve_dsoc *= 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) ret = of_property_read_u32(np, "virtual_power", &pdata->bat_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) dev_err(dev, "virtual_power missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) ret = of_property_read_u32(np, "bat_res", &pdata->bat_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) dev_err(dev, "bat_res missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) ret = of_property_read_u32(np, "sleep_enter_current",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) &pdata->sleep_enter_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) dev_err(dev, "sleep_enter_current missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) ret = of_property_read_u32(np, "sleep_exit_current",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) &pdata->sleep_exit_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) dev_err(dev, "sleep_exit_current missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) ret = of_property_read_u32(np, "sleep_filter_current",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) &pdata->sleep_filter_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) dev_err(dev, "sleep_filter_current missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) ret = of_property_read_u32(np, "power_off_thresd", &pdata->pwroff_vol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) dev_err(dev, "power_off_thresd missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) ret = of_property_read_u32(np, "low_power_sleep", &pdata->low_pwr_sleep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) dev_info(dev, "low_power_sleep missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) if (battery->chip_id == RK809_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) ret = of_property_read_u32(np, "bat_res_up",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) &pdata->bat_res_up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) dev_err(dev, "battery res_up missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) ret = of_property_read_u32(np, "bat_res_down",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) &pdata->bat_res_down);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) dev_err(dev, "battery res_down missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) ret = of_property_read_u32(np, "design_max_voltage",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) &pdata->design_max_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) dev_err(dev, "battery design_max_voltage missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) ret = of_property_read_u32(np, "register_chg_psy",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) &battery->is_register_chg_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) if (ret < 0 || !battery->is_register_chg_psy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) dev_err(dev, "not have to register chg psy!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) DBG("the battery dts info dump:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) "bat_res:%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) "res_sample:%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) "design_capacity:%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) "design_qmax :%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) "sleep_enter_current:%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) "sleep_exit_current:%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) "sleep_filter_current:%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) "zero_algorithm_vol:%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) "zero_reserve_dsoc:%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) "monitor_sec:%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) "max_soc_offset:%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) "virtual_power:%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) "pwroff_vol:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) pdata->bat_res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) pdata->sample_res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) pdata->design_capacity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) pdata->design_qmax,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) pdata->sleep_enter_current,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) pdata->sleep_exit_current,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) pdata->sleep_filter_current,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) pdata->zero_algorithm_vol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) pdata->zero_reserve_dsoc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) pdata->monitor_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) pdata->max_soc_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) pdata->bat_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) pdata->pwroff_vol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) static enum power_supply_property rk817_bat_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) POWER_SUPPLY_PROP_CURRENT_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) POWER_SUPPLY_PROP_HEALTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) POWER_SUPPLY_PROP_CAPACITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) POWER_SUPPLY_PROP_CAPACITY_LEVEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) POWER_SUPPLY_PROP_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) POWER_SUPPLY_PROP_CHARGE_COUNTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) POWER_SUPPLY_PROP_CHARGE_FULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) static int rk817_bat_get_usb_psy(struct device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) struct rk817_battery_device *battery = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) struct power_supply *psy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) battery->usb_psy = psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) static int rk817_bat_get_ac_psy(struct device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) struct rk817_battery_device *battery = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) struct power_supply *psy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) if (psy->desc->type == POWER_SUPPLY_TYPE_MAINS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) battery->ac_psy = psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) static void rk817_bat_get_chrg_psy(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) if (!battery->usb_psy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) class_for_each_device(power_supply_class, NULL, (void *)battery,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) rk817_bat_get_usb_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) if (!battery->ac_psy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) class_for_each_device(power_supply_class, NULL, (void *)battery,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) rk817_bat_get_ac_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) static int rk817_bat_get_charge_state(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) union power_supply_propval val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) struct power_supply *psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) if (!battery->usb_psy || !battery->ac_psy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) rk817_bat_get_chrg_psy(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) psy = battery->usb_psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) if (psy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) ret = psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) battery->usb_in = val.intval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) psy = battery->ac_psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) if (psy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) ret = psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) battery->ac_in = val.intval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) DBG("%s: ac_online=%d, usb_online=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) __func__, battery->ac_in, battery->usb_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) return (battery->usb_in || battery->ac_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) static int rk817_get_capacity_leve(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) int dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) if (battery->pdata->bat_mode == MODE_VIRTUAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) dsoc = (battery->dsoc + 500) / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) if (dsoc < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) else if (dsoc <= 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) else if (dsoc <= 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) else if (dsoc <= 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) return POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) static int rk817_battery_time_to_full(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) int time_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) int cap_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) if (battery->pdata->bat_mode == MODE_VIRTUAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) time_sec = 3600;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) } else if (battery->voltage_avg > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) cap_temp = battery->design_cap - (battery->remain_cap / 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) if (cap_temp < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) cap_temp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) time_sec = (3600 * cap_temp) / battery->voltage_avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) time_sec = 3600 * 24; /* One day */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) return time_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) static int rk817_battery_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) struct rk817_battery_device *battery = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) case POWER_SUPPLY_PROP_CURRENT_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) val->intval = battery->current_avg * 1000;/*uA*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) if (battery->pdata->bat_mode == MODE_VIRTUAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) val->intval = VIRTUAL_CURRENT * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) val->intval = battery->voltage_avg * 1000;/*uV*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) if (battery->pdata->bat_mode == MODE_VIRTUAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) val->intval = VIRTUAL_VOLTAGE * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) case POWER_SUPPLY_PROP_CAPACITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) val->intval = (battery->dsoc + 500) / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) if (battery->pdata->bat_mode == MODE_VIRTUAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) val->intval = VIRTUAL_SOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) val->intval = rk817_get_capacity_leve(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) case POWER_SUPPLY_PROP_HEALTH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) val->intval = POWER_SUPPLY_HEALTH_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) case POWER_SUPPLY_PROP_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) val->intval = battery->temperature;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) if (battery->pdata->bat_mode == MODE_VIRTUAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) val->intval = VIRTUAL_TEMPERATURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) if (battery->pdata->bat_mode == MODE_VIRTUAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) val->intval = VIRTUAL_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) else if (battery->dsoc == 100 * 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) val->intval = POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) if ((battery->chip_id != RK809_ID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) rk817_bat_get_charge_state(battery))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) val->intval = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) else if (battery->chip_id == RK809_ID &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) battery->plugin_trigger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) val->intval = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) case POWER_SUPPLY_PROP_CHARGE_COUNTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) val->intval = battery->charge_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) case POWER_SUPPLY_PROP_CHARGE_FULL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) val->intval = battery->pdata->design_capacity * 1000;/* uAh */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) val->intval = rk817_battery_time_to_full(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) case POWER_SUPPLY_PROP_VOLTAGE_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) val->intval = 4500 * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) case POWER_SUPPLY_PROP_CURRENT_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) val->intval = 5000 * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) static const struct power_supply_desc rk817_bat_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) .name = "battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) .type = POWER_SUPPLY_TYPE_BATTERY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) .properties = rk817_bat_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) .num_properties = ARRAY_SIZE(rk817_bat_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) .get_property = rk817_battery_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) static int rk817_bat_init_power_supply(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) struct power_supply_config psy_cfg = { .drv_data = battery, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) battery->bat = devm_power_supply_register(battery->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) &rk817_bat_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) if (IS_ERR(battery->bat)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) dev_err(battery->dev, "register bat power supply fail\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) return PTR_ERR(battery->bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) static enum power_supply_property rk809_chg_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) static int rk809_chg_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) struct rk817_battery_device *battery = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) int online = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) if (battery->plugin_trigger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) online = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) val->intval = online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) dev_dbg(battery->dev, "report online: %d\n", val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) if (online)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) val->intval = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) dev_dbg(battery->dev, "report prop: %d\n", val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) static const struct power_supply_desc rk809_chg_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) .name = "charger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) .type = POWER_SUPPLY_TYPE_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) .properties = rk809_chg_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) .num_properties = ARRAY_SIZE(rk809_chg_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) .get_property = rk809_chg_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) static int rk809_chg_init_power_supply(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) struct power_supply_config psy_cfg = { .drv_data = battery, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) battery->chg_psy =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) devm_power_supply_register(battery->dev, &rk809_chg_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) if (IS_ERR(battery->chg_psy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) dev_err(battery->dev, "register chg psy power supply fail\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) return PTR_ERR(battery->chg_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) static void rk817_bat_power_supply_changed(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) static int old_soc = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) if (battery->dsoc > 100 * 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) battery->dsoc = 100 * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) else if (battery->dsoc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) battery->dsoc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) if (battery->dsoc == old_soc && !battery->change)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) battery->change = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) old_soc = battery->dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) battery->last_dsoc = battery->dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) power_supply_changed(battery->bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) DBG("changed: dsoc=%d, rsoc=%d, v=%d, ov=%d c=%d, cap=%d, f=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) battery->dsoc, battery->rsoc, battery->voltage_avg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) battery->voltage_ocv, battery->current_avg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) battery->remain_cap, battery->fcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) DBG("dl=%d, rl=%d, v=%d, halt=%d, halt_n=%d, max=%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) "init=%d, sw=%d, calib=%d, below0=%d, force=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) battery->dbg_pwr_dsoc, battery->dbg_pwr_rsoc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) battery->dbg_pwr_vol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) battery->is_halt, battery->halt_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) battery->is_max_soc_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) battery->is_initialized, battery->is_sw_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) battery->is_ocv_calib,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) battery->dbg_cap_low0, battery->is_force_calib);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) static void rk817_battery_debug_info(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) rk817_bat_get_battery_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) rk817_bat_get_sys_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) rk817_bat_get_USB_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) rk817_bat_get_pwron_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) rk817_bat_get_ocv_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) rk817_bat_get_ocv0_voltage0(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) rk817_bat_current_calibration(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) rk817_bat_get_avg_current(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) rk817_bat_get_relax_cur1(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) rk817_bat_get_relax_cur2(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) rk817_bat_get_relax_current(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) rk817_bat_get_ocv_current(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) rk817_bat_get_ocv_current0(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) rk817_bat_get_pwron_current(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) rk817_bat_get_ocv_count(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) rk817_bat_save_dsoc(battery, battery->dsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) DBG("capactiy = %d\n", rk817_bat_get_capacity_mah(battery));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) rk817_bat_update_charging_status(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) int is_charging;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) is_charging = rk817_bat_get_charge_state(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) if (is_charging == battery->is_charging)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) battery->change = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) battery->is_charging = is_charging;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) if (is_charging)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) battery->charge_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) static void rk817_bat_update_info(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) battery->voltage_avg = rk817_bat_get_battery_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) battery->voltage_sys = rk817_bat_get_sys_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) battery->current_avg = rk817_bat_get_avg_current(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) battery->voltage_relax = rk817_bat_get_relax_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) battery->rsoc = rk817_bat_get_rsoc(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) battery->remain_cap = rk817_bat_get_capacity_uah(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) battery->voltage_usb = rk817_bat_get_USB_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) battery->chrg_status = get_charge_status(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) rk817_bat_update_charging_status(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) DBG("valtage usb: %d\n", battery->voltage_usb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) DBG("UPDATE: voltage_avg = %d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) "voltage_sys = %d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) "curren_avg = %d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) "rsoc = %d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) "chrg_status = %d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) "PWRON_CUR = %d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) "remain_cap = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) battery->voltage_avg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) battery->voltage_sys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) battery->current_avg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) battery->rsoc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) battery->chrg_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) rk817_bat_get_pwron_current(battery),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) battery->remain_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) /* smooth charge */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) if (battery->remain_cap / 1000 > battery->fcc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) /*battery->sm_remain_cap -=*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) /*(battery->remain_cap - battery->fcc * 1000);*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) battery->sm_remain_cap = battery->fcc * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) DBG("<%s>. cap: remain=%d, sm_remain=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) __func__, battery->remain_cap, battery->sm_remain_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) DBG("fcc: %d\n", battery->fcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) rk817_bat_init_coulomb_cap(battery, battery->fcc + 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) rk817_bat_init_coulomb_cap(battery, battery->fcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) rk817_bat_get_capacity_mah(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) if (battery->chrg_status != CHARGE_FINISH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) battery->finish_base = get_boot_sec();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) static void rk817_bat_save_data(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) rk817_bat_save_dsoc(battery, battery->dsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) rk817_bat_save_cap(battery, battery->remain_cap / 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) /* high load: current < 0 with charger in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) * System will not shutdown while dsoc=0% with charging state(ac_in),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) * which will cause over discharge, so oppose status before report states.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) static void rk817_bat_lowpwr_check(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) static u64 time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) int pwr_off_thresd = battery->pdata->pwroff_vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) if (battery->current_avg < 0 && battery->voltage_avg < pwr_off_thresd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) if (!time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) time = get_boot_sec();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) if ((base2sec(time) > MINUTE(1)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) (battery->voltage_avg <= pwr_off_thresd - 50)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) battery->fake_offline = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) if (battery->voltage_avg <= pwr_off_thresd - 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) battery->dsoc -= 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) DBG("low power, soc=%d, current=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) battery->dsoc, battery->current_avg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) battery->fake_offline = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) DBG("<%s>. t=%lu, dsoc=%d, current=%d, fake_offline=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) __func__, base2sec(time), battery->dsoc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) battery->current_avg, battery->fake_offline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) static void rk817_bat_calc_smooth_dischrg(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) int tmp_soc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) /* check new dsoc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) if (battery->smooth_soc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) battery->smooth_soc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) tmp_soc = battery->smooth_soc / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) if (tmp_soc != battery->dsoc / 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) if (battery->smooth_soc > battery->dsoc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) if (battery->smooth_soc + 1000 > battery->dsoc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) battery->dsoc = battery->smooth_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) battery->dsoc -= 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) if (battery->dsoc <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) battery->dsoc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) static void rk817_bat_smooth_algorithm(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) int ydsoc = 0, delta_cap = 0, old_cap = 0, tmp_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) /*int linek;*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) int diff, delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) /*int current_avg = rk817_bat_get_avg_current(battery);*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) delta = abs(battery->dsoc - battery->rsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) diff = delta * 3;/* speed:3/4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) /* charge and discharge switch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) if ((battery->sm_linek * battery->current_avg <= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) DBG("<%s>. linek mode, retinit sm linek..\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) rk817_bat_calc_sm_linek(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) /*battery->sm_linek = linek;*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) battery->remain_cap = rk817_bat_get_capacity_uah(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) old_cap = battery->sm_remain_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) DBG("smooth: smooth_soc = %d, dsoc = %d, battery->sm_linek = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) battery->smooth_soc, battery->dsoc, battery->sm_linek);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) /* discharge status: sm_remain_cap > remain_cap, delta_cap > 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) /* from charge to discharge:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) * remain_cap may be above sm_remain_cap, delta_cap <= 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) delta_cap = battery->remain_cap - battery->sm_remain_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) DBG("smooth: sm_remain_cap = %d, remain_cap = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) battery->sm_remain_cap, battery->remain_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) DBG("smooth: delta_cap = %d, dsoc = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) delta_cap, battery->dsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) if (delta_cap == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) DBG("<%s>. delta_cap = 0\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) /* discharge: sm_linek < 0, if delate_cap <0, ydsoc > 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) ydsoc = battery->sm_linek * abs(delta_cap / 10) / DIV(battery->fcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) DBG("smooth: ydsoc = %d, fcc = %d\n", ydsoc, battery->fcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) if (ydsoc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) DBG("<%s>. ydsoc = 0\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) battery->sm_remain_cap = battery->remain_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) DBG("<%s>. k=%d, ydsoc=%d; cap:old=%d, new:%d; delta_cap=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) __func__, battery->sm_linek, ydsoc, old_cap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) battery->sm_remain_cap, delta_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) /* discharge mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) /* discharge mode, but ydsoc > 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) * from charge status to dischrage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) battery->smooth_soc += ydsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) if (ydsoc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) rk817_bat_calc_smooth_dischrg(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) if (battery->smooth_soc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) battery->smooth_soc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) tmp_soc = battery->smooth_soc / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) if (tmp_soc != battery->dsoc / 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) if (battery->smooth_soc < battery->dsoc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) battery->dsoc = battery->smooth_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) if (battery->dsoc <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) battery->dsoc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) if (battery->s2r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) battery->s2r = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) rk817_bat_calc_sm_linek(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) DBG("smooth: smooth_soc = %d, dsoc = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) battery->smooth_soc, battery->dsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) DBG("smooth: delta_cap = %d, dsoc = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) delta_cap, battery->dsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) static void rk817_bat_calc_zero_linek(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) int dead_voltage, ocv_voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) int voltage_avg, current_avg, vsys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) int ocv_cap, dead_cap, xsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) int ocv_soc, dead_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) int pwroff_vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) int min_gap_xsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) int powerpatch_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) if ((abs(battery->current_avg) < 400) && (battery->dsoc / 1000 > 5))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) pwroff_vol = battery->pdata->pwroff_vol + 50;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) pwroff_vol = battery->pdata->pwroff_vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) /* calc estimate ocv voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) voltage_avg = rk817_bat_get_battery_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) current_avg = rk817_bat_get_avg_current(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) vsys = voltage_avg + (current_avg * DEF_PWRPATH_RES) / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) powerpatch_res = (voltage_avg - vsys) * 1000 / current_avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) battery->zero_voltage_avg = voltage_avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) battery->zero_current_avg = current_avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) battery->zero_vsys = vsys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) DBG("Zero: voltage_avg = %d, Vsys = %d\n", voltage_avg, vsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) DBG("Zero: powerpatch_res = %d\n", powerpatch_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) DBG("ZERO0: shtd_vol: poweroff_vol(usr) = %d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) "pwroff_vol = %d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) "zero_reserve_dsoc = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) battery->pdata->pwroff_vol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) pwroff_vol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) battery->pdata->zero_reserve_dsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) /* get the dead ocv voltage, pwroff_vol is vsys */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) dead_voltage = pwroff_vol - current_avg *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) (battery->bat_res + DEF_PWRPATH_RES) / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) ocv_voltage = voltage_avg - (current_avg * battery->bat_res) / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) DBG("ZERO0: dead_voltage(shtd) = %d, ocv_voltage(now) = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) dead_voltage, ocv_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) /* calc estimate soc and cap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) dead_soc = rk817_bat_vol_to_soc(battery, dead_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) dead_cap = rk817_bat_vol_to_cap(battery, dead_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) DBG("ZERO0: dead_soc = %d, dead_cap = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) dead_soc, dead_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) ocv_soc = rk817_bat_vol_to_soc(battery, ocv_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) ocv_cap = rk817_bat_vol_to_cap(battery, ocv_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) DBG("ZERO0: ocv_soc = %d, ocv_cap = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) ocv_soc, ocv_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) /* xsoc: available rsoc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) xsoc = ocv_soc - dead_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) battery->zero_dead_voltage = dead_voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) battery->zero_dead_soc = dead_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) battery->zero_dead_cap = dead_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) battery->zero_batvol_to_ocv = ocv_voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) battery->zero_batocv_to_soc = ocv_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) battery->zero_batocv_to_cap = ocv_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) battery->zero_xsoc = xsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) DBG("Zero: xsoc = %d\n", xsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) /* min_gap_xsoc: reserve xsoc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) if (abs(current_avg) > ZERO_LOAD_LVL1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) min_gap_xsoc = MIN_ZERO_GAP_XSOC3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) else if (abs(current_avg) > ZERO_LOAD_LVL2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) min_gap_xsoc = MIN_ZERO_GAP_XSOC2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) min_gap_xsoc = MIN_ZERO_GAP_XSOC1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) if ((xsoc <= 30) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) (battery->dsoc >= battery->pdata->zero_reserve_dsoc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) min_gap_xsoc = min_gap_xsoc + MIN_ZERO_GAP_CALIB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) battery->zero_remain_cap = battery->remain_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) battery->zero_timeout_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) if ((battery->dsoc / 1000 <= 1) && (xsoc > 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) battery->zero_linek = 400;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) battery->zero_drop_sec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) } else if (xsoc >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) battery->zero_drop_sec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) battery->zero_linek =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) (battery->zero_dsoc + xsoc / 2) / DIV(xsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) /* battery energy mode to use up voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) if ((battery->pdata->energy_mode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) (xsoc - battery->dsoc / 1000 >= MIN_ZERO_GAP_XSOC3) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) (battery->dsoc / 1000 <= 10) && (battery->zero_linek < 300)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) battery->zero_linek = 300;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) DBG("ZERO-new: zero_linek adjust step0...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) /* reserve enough power yet, slow down any way */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) } else if ((xsoc - battery->dsoc / 1000 >= min_gap_xsoc) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) ((xsoc - battery->dsoc / 1000 >= MIN_ZERO_GAP_XSOC2) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) (battery->dsoc / 1000 <= 10) && (xsoc > 15))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) if (xsoc <= 20 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) battery->dsoc / 1000 >= battery->pdata->zero_reserve_dsoc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) battery->zero_linek = 1200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) else if (xsoc - battery->dsoc / 1000 >= 2 * min_gap_xsoc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) battery->zero_linek = 400;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) else if (xsoc - battery->dsoc / 1000 >= 3 + min_gap_xsoc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) battery->zero_linek = 600;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) battery->zero_linek = 800;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) DBG("ZERO-new: zero_linek adjust step1...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) /* control zero mode beginning enter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) } else if ((battery->zero_linek > 1800) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) (battery->dsoc / 1000 > 70)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) battery->zero_linek = 1800;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) DBG("ZERO-new: zero_linek adjust step2...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) /* dsoc close to xsoc: it must reserve power */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) } else if ((battery->zero_linek > 1000) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) (battery->zero_linek < 1200)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) battery->zero_linek = 1200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) DBG("ZERO-new: zero_linek adjust step3...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) /* dsoc[5~15], dsoc < xsoc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) } else if ((battery->dsoc / 1000 <= 15 && battery->dsoc > 5) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) (battery->zero_linek <= 1200)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) /* slow down */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) if ((xsoc - battery->dsoc / 1000) >= min_gap_xsoc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) battery->zero_linek = 800;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) /* reserve power */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) battery->zero_linek = 1200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) DBG("ZERO-new: zero_linek adjust step4...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) /* dsoc[5, 100], dsoc < xsoc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) } else if ((battery->zero_linek < 1000) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) (battery->dsoc / 1000 >= 5)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) if ((xsoc - battery->dsoc / 1000) < min_gap_xsoc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) /* reserve power */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) battery->zero_linek = 1200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) if (abs(battery->current_avg) > 500)/* heavy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) battery->zero_linek = 900;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) battery->zero_linek = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) DBG("ZERO-new: zero_linek adjust step5...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) /* dsoc[0~5], dsoc < xsoc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) } else if ((battery->zero_linek < 1000) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) (battery->dsoc / 1000 <= 5)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) if ((xsoc - battery->dsoc / 1000) <= 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) battery->zero_linek = 1200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) battery->zero_linek = 800;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) DBG("ZERO-new: zero_linek adjust step6...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) /* xsoc < 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) battery->zero_linek = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) if (!battery->zero_drop_sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) battery->zero_drop_sec = get_boot_sec();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) if (base2sec(battery->zero_drop_sec) >= WAIT_DSOC_DROP_SEC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) DBG("ZERO0: t=%lu\n", base2sec(battery->zero_drop_sec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) battery->zero_drop_sec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) battery->dsoc -= 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) if (battery->dsoc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) battery->dsoc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) battery->zero_dsoc = battery->dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) if (voltage_avg < pwroff_vol - 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) if (!battery->shtd_drop_sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) battery->shtd_drop_sec = get_boot_sec();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) if (base2sec(battery->shtd_drop_sec) > WAIT_SHTD_DROP_SEC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) DBG("voltage extreme low...soc:%d->0\n", battery->dsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) battery->shtd_drop_sec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) battery->dsoc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) battery->shtd_drop_sec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) DBG("Zero: zero_linek = %d\n", battery->zero_linek);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) static void rk817_bat_zero_algo_prepare(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) int tmp_dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) tmp_dsoc = battery->zero_dsoc / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) if (tmp_dsoc != battery->smooth_soc / 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) battery->zero_dsoc = battery->smooth_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) DBG("zero_smooth: zero_dsoc = %d\n", battery->zero_dsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) rk817_bat_calc_zero_linek(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) static void rk817_bat_calc_zero_algorithm(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) int tmp_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) tmp_soc = battery->zero_dsoc / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) if (tmp_soc == battery->dsoc / 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) if (battery->zero_dsoc > battery->dsoc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) if (battery->zero_dsoc < battery->dsoc - 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) battery->dsoc -= 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) battery->dsoc = battery->zero_dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) static void rk817_bat_zero_algorithm(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) int delta_cap = 0, delta_soc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) battery->zero_timeout_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) delta_cap = battery->zero_remain_cap - battery->remain_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) delta_soc = battery->zero_linek * delta_cap / DIV(battery->fcc) / 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) DBG("zero algorithm start\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) DBG("DEAD: dead_voltage: %d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) "dead_soc: %d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) "dead_cap: %d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) "powoff_vol: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) battery->zero_dead_voltage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) battery->zero_dead_soc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) battery->zero_dead_cap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) battery->pdata->pwroff_vol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) DBG("DEAD: bat_voltage: %d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) "bat_current: %d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) "batvol_to_ocv: %d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) "batocv_to_soc: %d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) "batocv_to_cap: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) battery->zero_voltage_avg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) battery->zero_current_avg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) battery->zero_batvol_to_ocv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) battery->zero_batocv_to_soc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) battery->zero_batocv_to_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) DBG("DEAD: Xsoc: %d, zero_reserve_dsoc: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) battery->zero_xsoc, battery->pdata->zero_reserve_dsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) DBG("CAP: zero_remain_cap = %d, remain_cap = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) battery->zero_remain_cap, battery->remain_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) DBG("Zero: zero_delta_cap = %d, zero_link = %d, delta_soc = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) delta_cap, battery->zero_linek, delta_soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) DBG("zero algorithm end\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) if ((delta_soc >= MIN_ZERO_DSOC_ACCURACY) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) (battery->zero_timeout_cnt > MIN_ZERO_OVERCNT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) (battery->zero_linek == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) DBG("ZERO1:--------- enter calc -----------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) battery->zero_timeout_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) battery->zero_dsoc -= delta_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) rk817_bat_calc_zero_algorithm(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) DBG("Zero: dsoc: %d\n", battery->dsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) rk817_bat_calc_zero_linek(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) if ((battery->rsoc / 1000 < 1) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) (battery->zero_batocv_to_cap > battery->fcc / 100)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) DBG("ZERO2:---------check step1 -----------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) rk817_bat_init_coulomb_cap(battery,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) battery->zero_batocv_to_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) rk817_bat_calc_zero_linek(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) static void rk817_bat_finish_algorithm(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) unsigned long finish_sec, soc_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) int plus_soc, finish_current, rest = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) /* rsoc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) if ((battery->remain_cap != battery->fcc) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) (get_charge_status(battery) == CHARGE_FINISH)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) battery->age_adjust_cap +=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) (battery->fcc * 1000 - battery->remain_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) rk817_bat_init_coulomb_cap(battery, battery->fcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) rk817_bat_get_capacity_mah(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) /* dsoc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) if (battery->dsoc < 100 * 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) if (!battery->finish_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) battery->finish_base = get_boot_sec();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) finish_current = (battery->rsoc - battery->dsoc) > FINISH_MAX_SOC_DELAY ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) FINISH_CHRG_CUR2 : FINISH_CHRG_CUR1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) finish_sec = base2sec(battery->finish_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) soc_sec = battery->fcc * 3600 / 100 / DIV(finish_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) plus_soc = finish_sec / DIV(soc_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) if (finish_sec > soc_sec) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) rest = finish_sec % soc_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) battery->dsoc += plus_soc * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) battery->finish_base = get_boot_sec();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) if (battery->finish_base > rest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) battery->finish_base = get_boot_sec() - rest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) DBG("CHARGE_FINISH:dsoc<100,dsoc=%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) "soc_time=%lu, sec_finish=%lu, plus_soc=%d, rest=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) battery->dsoc, soc_sec, finish_sec, plus_soc, rest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) DBG("battery->age_adjust_cap = %d\n", battery->age_adjust_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) static void rk817_bat_display_smooth(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) /* discharge: reinit "zero & smooth" algorithm to avoid handling dsoc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) if (battery->s2r && !battery->sleep_chrg_online) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) DBG("s2r: discharge, reset algorithm...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) battery->s2r = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) rk817_bat_zero_algo_prepare(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) rk817_bat_smooth_algo_prepare(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) if (battery->work_mode == MODE_FINISH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) DBG("step1: charge finish...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) rk817_bat_finish_algorithm(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) if ((get_charge_status(battery) != CHARGE_FINISH) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) !rk817_bat_fake_finish_mode(battery)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) if ((battery->current_avg < 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) (battery->voltage_avg < battery->pdata->zero_algorithm_vol)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) DBG("step1: change to zero mode...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) rk817_bat_zero_algo_prepare(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) battery->work_mode = MODE_ZERO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) DBG("step1: change to smooth mode...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) rk817_bat_smooth_algo_prepare(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) battery->work_mode = MODE_SMOOTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) } else if (battery->work_mode == MODE_ZERO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) DBG("step2: zero algorithm...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) rk817_bat_zero_algorithm(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) if ((battery->voltage_avg >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) battery->pdata->zero_algorithm_vol + 50) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) (battery->current_avg >= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) DBG("step2: change to smooth mode...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) rk817_bat_smooth_algo_prepare(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) battery->work_mode = MODE_SMOOTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) } else if ((get_charge_status(battery) == CHARGE_FINISH) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) rk817_bat_fake_finish_mode(battery)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) DBG("step2: change to finish mode...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) rk817_bat_finish_algo_prepare(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) battery->work_mode = MODE_FINISH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) DBG("step3: smooth algorithm...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) rk817_bat_smooth_algorithm(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) if ((battery->current_avg < 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) (battery->voltage_avg <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) battery->pdata->zero_algorithm_vol)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) DBG("step3: change to zero mode...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) rk817_bat_zero_algo_prepare(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) battery->work_mode = MODE_ZERO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) } else if ((get_charge_status(battery) == CHARGE_FINISH) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) rk817_bat_fake_finish_mode(battery)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) DBG("step3: change to finish mode...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) rk817_bat_finish_algo_prepare(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) battery->work_mode = MODE_FINISH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) static void rk817_bat_output_info(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) DBG("info start:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) DBG("info: voltage_k = %d\n", battery->voltage_k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) DBG("info: voltage_b = %d\n", battery->voltage_b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) DBG("info: voltage = %d\n", battery->voltage_avg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) DBG("info: voltage_sys = %d\n", battery->voltage_sys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) DBG("info: current = %d\n", battery->current_avg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) DBG("info: FCC = %d\n", battery->fcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) DBG("info: remain_cap = %d\n", battery->remain_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) DBG("info: sm_remain_cap = %d\n", battery->sm_remain_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) DBG("info: sm_link = %d\n", battery->sm_linek);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) DBG("info: smooth_soc = %d\n", battery->smooth_soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) DBG("info: zero_remain_cap = %d\n", battery->zero_remain_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) DBG("info: zero_link = %d\n", battery->zero_linek);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) DBG("info: zero_dsoc = %d\n", battery->zero_dsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) DBG("info: remain_cap = %d\n", battery->remain_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) DBG("info: dsoc = %d, dsoc/1000 = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) battery->dsoc, battery->dsoc / 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) DBG("info: rsoc = %d\n", battery->rsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) DBG("info END.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) static void rk817_battery_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) struct rk817_battery_device *battery =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) struct rk817_battery_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) bat_delay_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) rk817_bat_update_info(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) rk817_bat_lowpwr_check(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) rk817_bat_display_smooth(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) rk817_bat_power_supply_changed(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) rk817_bat_save_data(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) rk817_bat_output_info(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) if (rk817_bat_field_read(battery, CUR_CALIB_UPD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) rk817_bat_current_calibration(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) rk817_bat_init_voltage_kb(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) rk817_bat_field_write(battery, CUR_CALIB_UPD, 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) queue_delayed_work(battery->bat_monitor_wq, &battery->bat_delay_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) msecs_to_jiffies(battery->monitor_ms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) static irqreturn_t rk809_plug_in_isr(int irq, void *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) struct rk817_battery_device *battery;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) battery = (struct rk817_battery_device *)cg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) battery->plugin_trigger = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) battery->plugout_trigger = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) power_supply_changed(battery->bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) if (battery->is_register_chg_psy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) power_supply_changed(battery->chg_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) static irqreturn_t rk809_plug_out_isr(int irq, void *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) struct rk817_battery_device *battery;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) battery = (struct rk817_battery_device *)cg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) battery->plugin_trigger = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) battery->plugout_trigger = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) power_supply_changed(battery->bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) if (battery->is_register_chg_psy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) power_supply_changed(battery->chg_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) static int rk809_charge_init_irqs(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) struct rk808 *rk817 = battery->rk817;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) struct platform_device *pdev = battery->pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) int ret, plug_in_irq, plug_out_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) battery->plugin_trigger = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) battery->plugout_trigger = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) plug_in_irq = regmap_irq_get_virq(rk817->irq_data, RK817_IRQ_PLUG_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) if (plug_in_irq < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) dev_err(battery->dev, "plug_in_irq request failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) return plug_in_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) plug_out_irq = regmap_irq_get_virq(rk817->irq_data, RK817_IRQ_PLUG_OUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) if (plug_out_irq < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) dev_err(battery->dev, "plug_out_irq request failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) return plug_out_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) ret = devm_request_threaded_irq(battery->dev, plug_in_irq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) rk809_plug_in_isr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) IRQF_TRIGGER_RISING | IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) "rk817_plug_in", battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) dev_err(&pdev->dev, "plug_in_irq request failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) ret = devm_request_threaded_irq(battery->dev, plug_out_irq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) rk809_plug_out_isr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) IRQF_TRIGGER_RISING | IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) "rk817_plug_out", battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) dev_err(&pdev->dev, "plug_out_irq request failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) if (rk817_bat_field_read(battery, PLUG_IN_STS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) battery->plugin_trigger = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) battery->plugout_trigger = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) static const struct of_device_id rk817_bat_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) { .compatible = "rk817,battery", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) MODULE_DEVICE_TABLE(of, rk817_bat_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) static const struct of_device_id rk817_bat_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) static int rk817_battery_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) const struct of_device_id *of_id =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) of_match_device(rk817_bat_of_match, &pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) struct rk817_battery_device *battery;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) struct rk808 *rk817 = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) struct i2c_client *client = rk817->i2c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) if (!of_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) dev_err(&pdev->dev, "Failed to find matching dt id\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) battery = devm_kzalloc(&client->dev, sizeof(*battery), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) if (!battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) battery->rk817 = rk817;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) battery->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) battery->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) platform_set_drvdata(pdev, battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) battery->chip_id = rk817->variant;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) battery->regmap = rk817->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) if (IS_ERR(battery->regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) dev_err(battery->dev, "Failed to initialize regmap\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) for (i = 0; i < ARRAY_SIZE(rk817_battery_reg_fields); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) const struct reg_field *reg_fields = rk817_battery_reg_fields;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) battery->rmap_fields[i] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) devm_regmap_field_alloc(battery->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) battery->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) reg_fields[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) if (IS_ERR(battery->rmap_fields[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) dev_err(battery->dev, "cannot allocate regmap field\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) return PTR_ERR(battery->rmap_fields[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) ret = rk817_bat_parse_dt(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) dev_err(battery->dev, "battery parse dt failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) rk817_bat_init_info(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) rk817_bat_init_fg(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) rk817_battery_debug_info(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) rk817_bat_update_info(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) rk817_bat_output_info(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) battery->bat_monitor_wq = alloc_ordered_workqueue("%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) WQ_MEM_RECLAIM | WQ_FREEZABLE, "rk817-bat-monitor-wq");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) INIT_DELAYED_WORK(&battery->bat_delay_work, rk817_battery_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) queue_delayed_work(battery->bat_monitor_wq, &battery->bat_delay_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) msecs_to_jiffies(TIMER_MS_COUNTS * 5));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) INIT_WORK(&battery->resume_work, rk817_bat_resume_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) ret = rk817_bat_init_power_supply(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) dev_err(battery->dev, "rk817 power supply register failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) if (battery->is_register_chg_psy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) ret = rk809_chg_init_power_supply(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) dev_err(battery->dev, "rk809 chg psy init failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) if (battery->chip_id == RK809_ID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) rk809_charge_init_irqs(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) wake_lock_init(&battery->wake_lock, WAKE_LOCK_SUSPEND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) "rk817_bat_lock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) DBG("name: 0x%x", rk817_bat_field_read(battery, CHIP_NAME_H));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) DBG("%x\n", rk817_bat_field_read(battery, CHIP_NAME_L));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) DBG("driver version %s\n", DRIVER_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) static void rk817_battery_shutdown(struct platform_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) static time64_t rk817_get_rtc_sec(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) struct rtc_time tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) err = rtc_read_time(rtc, &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) dev_err(rtc->dev.parent, "read hardware clk failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) err = rtc_valid_tm(&tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) dev_err(rtc->dev.parent, "invalid date time\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) return rtc_tm_to_time64(&tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) static int rk817_bat_pm_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) struct platform_device *pdev = to_platform_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) struct rk817_battery_device *battery = dev_get_drvdata(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) cancel_delayed_work_sync(&battery->bat_delay_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) battery->s2r = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) battery->sleep_chrg_status = get_charge_status(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) battery->current_avg = rk817_bat_get_avg_current(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) if (battery->current_avg > 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) (battery->sleep_chrg_status == CC_OR_CV_CHRG) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) (battery->sleep_chrg_status == CHARGE_FINISH))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) battery->sleep_chrg_online = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) battery->sleep_chrg_online = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) battery->remain_cap = rk817_bat_get_capacity_uah(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) battery->rsoc = rk817_bat_get_rsoc(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) battery->rtc_base = rk817_get_rtc_sec();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) rk817_bat_save_data(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) if (battery->sleep_chrg_status != CHARGE_FINISH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) battery->finish_base = get_boot_sec();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) if ((battery->work_mode == MODE_ZERO) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) (battery->current_avg >= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) DBG("suspend: MODE_ZERO exit...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) /* it need't do prepare for mode finish and smooth, it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) * be done in display_smooth
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) if (battery->sleep_chrg_status == CHARGE_FINISH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) battery->work_mode = MODE_FINISH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) if (!battery->finish_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) battery->finish_base = get_boot_sec();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) battery->work_mode = MODE_SMOOTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) rk817_bat_smooth_algo_prepare(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) DBG("suspend get_boot_sec: %lld\n", get_boot_sec());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) DBG("suspend: dl=%d rl=%d c=%d v=%d cap=%d at=%ld ch=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) battery->dsoc, battery->rsoc, battery->current_avg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) rk817_bat_get_battery_voltage(battery),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) rk817_bat_get_capacity_uah(battery),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) battery->sleep_dischrg_sec, battery->sleep_chrg_online);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) DBG("battery->sleep_chrg_status=%d\n", battery->sleep_chrg_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) static int rk817_bat_rtc_sleep_sec(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) int interval_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) interval_sec = rk817_get_rtc_sec() - battery->rtc_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) return (interval_sec > 0) ? interval_sec : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) static void rk817_bat_relife_age_flag(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) u8 ocv_soc, ocv_cap, soc_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) if (battery->voltage_relax <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) ocv_soc = rk817_bat_vol_to_soc(battery, battery->voltage_relax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) ocv_cap = rk817_bat_vol_to_cap(battery, battery->voltage_relax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) DBG("<%s>. ocv_soc=%d, min=%lu, vol=%d\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) ocv_soc, battery->sleep_dischrg_sec / 60, battery->voltage_relax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) /* sleep enough time and ocv_soc enough low */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) if (!battery->age_allow_update && ocv_soc <= 10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) battery->age_voltage = battery->voltage_relax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) battery->age_ocv_cap = ocv_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) battery->age_ocv_soc = ocv_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) battery->age_adjust_cap = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) if (ocv_soc <= 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) battery->age_level = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) else if (ocv_soc < 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) battery->age_level = 90;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) battery->age_level = 80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) /*soc_level = rk818_bat_get_age_level(battery);*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) soc_level = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) if (soc_level > battery->age_level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) battery->age_allow_update = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) battery->age_allow_update = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) battery->age_keep_sec = get_boot_sec();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) BAT_INFO("resume: age_vol:%d, age_ocv_cap:%d, age_ocv_soc:%d, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) "soc_level:%d, age_allow_update:%d, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) "age_level:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) battery->age_voltage, battery->age_ocv_cap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) ocv_soc, soc_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) battery->age_allow_update, battery->age_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) static void rk817_bat_init_capacity(struct rk817_battery_device *battery,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) u32 cap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) int delta_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) delta_cap = cap - battery->remain_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) if (!delta_cap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) battery->age_adjust_cap += delta_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) rk817_bat_init_coulomb_cap(battery, cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) rk817_bat_smooth_algo_prepare(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) rk817_bat_zero_algo_prepare(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) static void rk817_bat_relax_vol_calib(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) int soc, cap, vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) vol = battery->voltage_relax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) soc = rk817_bat_vol_to_soc(battery, vol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) cap = rk817_bat_vol_to_cap(battery, vol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) rk817_bat_init_capacity(battery, cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) BAT_INFO("sleep ocv calib: rsoc=%d, cap=%d\n", soc, cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) static int rk817_bat_sleep_dischrg(struct rk817_battery_device *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) bool ocv_soc_updated = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) int tgt_dsoc, gap_soc, sleep_soc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) int pwroff_vol = battery->pdata->pwroff_vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) unsigned long sleep_sec = battery->sleep_dischrg_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) int sleep_cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) DBG("<%s>. enter: dsoc=%d, rsoc=%d, rv=%d, v=%d, sleep_min=%lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) __func__, battery->dsoc, battery->rsoc, battery->voltage_relax,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) battery->voltage_avg, sleep_sec / 60);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) if (battery->voltage_relax >= battery->voltage_avg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) rk817_bat_relax_vol_calib(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) rk817_bat_restart_relax(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) rk817_bat_relife_age_flag(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) ocv_soc_updated = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) /* handle dsoc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) if (battery->dsoc <= battery->rsoc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) if (battery->pdata->low_pwr_sleep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) sleep_cur = LOW_PWR_SLP_CURR_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) sleep_cur = SLP_CURR_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) battery->sleep_sum_cap = (sleep_cur * sleep_sec / 3600);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) sleep_soc = battery->sleep_sum_cap * 100 / DIV(battery->fcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) tgt_dsoc = battery->dsoc - sleep_soc * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) if (sleep_soc > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) BAT_INFO("calib0: rl=%d, dl=%d, intval=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) battery->rsoc, battery->dsoc, sleep_soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) if (battery->dsoc / 1000 < 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) battery->dsoc -= 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) } else if ((tgt_dsoc / 1000 < 5) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) (battery->dsoc / 1000 >= 5)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) if (battery->dsoc / 1000 == 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) battery->dsoc -= 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) battery->dsoc = 5 * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) } else if (tgt_dsoc / 1000 > 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) battery->dsoc = tgt_dsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) DBG("%s: dsoc<=rsoc, sum_cap=%d==>sleep_soc=%d, tgt_dsoc=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) __func__, battery->sleep_sum_cap, sleep_soc, tgt_dsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) /* di->dsoc > di->rsoc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) if (battery->pdata->low_pwr_sleep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) sleep_cur = LOW_PWR_SLP_CURR_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) sleep_cur = SLP_CURR_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) battery->sleep_sum_cap = (sleep_cur * sleep_sec / 3600);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) sleep_soc = battery->sleep_sum_cap / DIV(battery->fcc / 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) gap_soc = battery->dsoc - battery->rsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) DBG("calib1: rsoc=%d, dsoc=%d, intval=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) battery->rsoc, battery->dsoc, sleep_soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) if (gap_soc / 1000 > sleep_soc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) if ((gap_soc - 5000) > (sleep_soc * 2 * 1000))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) battery->dsoc -= (sleep_soc * 2 * 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) battery->dsoc -= sleep_soc * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) battery->dsoc = battery->rsoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) DBG("%s: dsoc>rsoc, sum_cap=%d=>sleep_soc=%d, gap_soc=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) __func__, battery->sleep_sum_cap, sleep_soc, gap_soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) if (battery->voltage_avg <= pwroff_vol - 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) battery->dsoc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) DBG("low power sleeping, shutdown... %d\n", battery->dsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) if (ocv_soc_updated && sleep_soc &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) (battery->rsoc - battery->dsoc) < 5000 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) battery->dsoc < 40 * 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) battery->dsoc -= 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) DBG("low power sleeping, reserved... %d\n", battery->dsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) if (battery->dsoc <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) battery->dsoc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) DBG("sleep dsoc is %d...\n", battery->dsoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) DBG("<%s>. out: dsoc=%d, rsoc=%d, sum_cap=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) __func__, battery->dsoc, battery->rsoc, battery->sleep_sum_cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) return sleep_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) static void rk817_bat_resume_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) struct rk817_battery_device *battery = container_of(work, struct rk817_battery_device, resume_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) int interval_sec = 0, time_step = 0, pwroff_vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) battery->s2r = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) battery->current_avg = rk817_bat_get_avg_current(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) battery->voltage_relax = rk817_bat_get_relax_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) battery->voltage_avg = rk817_bat_get_battery_voltage(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) battery->remain_cap = rk817_bat_get_capacity_uah(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) battery->rsoc = rk817_bat_get_rsoc(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) interval_sec = rk817_bat_rtc_sleep_sec(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) battery->sleep_sum_sec += interval_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) pwroff_vol = battery->pdata->pwroff_vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) if (!battery->sleep_chrg_online) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) /* only add up discharge sleep seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) battery->sleep_dischrg_sec += interval_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) if (battery->voltage_avg <= pwroff_vol + 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) time_step = DISCHRG_TIME_STEP1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) time_step = DISCHRG_TIME_STEP2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) DBG("resume: dl=%d rl=%d c=%d v=%d rv=%d "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) "cap=%d dt=%d at=%ld ch=%d, sec = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) battery->dsoc, battery->rsoc, battery->current_avg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) battery->voltage_avg, battery->voltage_relax,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) rk817_bat_get_capacity_uah(battery), interval_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) battery->sleep_dischrg_sec, battery->sleep_chrg_online,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) interval_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) /* sleep: enough time and discharge */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) if ((!battery->sleep_chrg_online) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) (battery->sleep_dischrg_sec > time_step)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) if (rk817_bat_sleep_dischrg(battery))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) battery->sleep_dischrg_sec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) rk817_bat_save_data(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) /* charge/lowpower lock: for battery work to update dsoc and rsoc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) if ((battery->sleep_chrg_online) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) (!battery->sleep_chrg_online &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) battery->voltage_avg < battery->pdata->pwroff_vol))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) wake_lock_timeout(&battery->wake_lock, msecs_to_jiffies(2000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) queue_delayed_work(battery->bat_monitor_wq, &battery->bat_delay_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) msecs_to_jiffies(1000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) static int rk817_bat_pm_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) struct rk817_battery_device *battery = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) queue_work(battery->bat_monitor_wq, &battery->resume_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) static SIMPLE_DEV_PM_OPS(rk817_bat_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) rk817_bat_pm_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) rk817_bat_pm_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) static struct platform_driver rk817_battery_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) .probe = rk817_battery_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) .shutdown = rk817_battery_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) .name = "rk817-battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) .pm = &rk817_bat_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) .of_match_table = of_match_ptr(rk817_bat_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) static int __init rk817_battery_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) return platform_driver_register(&rk817_battery_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) fs_initcall_sync(rk817_battery_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) static void __exit rk817_battery_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) platform_driver_unregister(&rk817_battery_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) module_exit(rk817_battery_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) MODULE_DESCRIPTION("RK817 Battery driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) MODULE_LICENSE("GPL");