blob: bfe1f43ee57a4240f3a989407a85a6b20761fec8 [file] [log] [blame]
Xiaozhe Shi72a72f22013-12-26 13:54:29 -08001/* Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
Xiaozhe Shi73a65692012-09-18 17:51:57 -070013#define pr_fmt(fmt) "BMS: %s: " fmt, __func__
Xiaozhe Shib19f7032012-08-16 12:14:16 -070014
15#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/init.h>
18#include <linux/slab.h>
19#include <linux/err.h>
20#include <linux/of.h>
21#include <linux/of_device.h>
22#include <linux/power_supply.h>
23#include <linux/spmi.h>
Xiaozhe Shie118c692012-09-24 15:17:43 -070024#include <linux/rtc.h>
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -070025#include <linux/delay.h>
Xiaozhe Shi27375822013-08-22 11:40:15 -070026#include <linux/sched.h>
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070027#include <linux/qpnp/qpnp-adc.h>
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -070028#include <linux/qpnp/power-on.h>
Xiaozhe Shiaf203c22013-06-19 12:01:38 -070029#include <linux/of_batterydata.h>
Xiaozhe Shib19f7032012-08-16 12:14:16 -070030
Xiaozhe Shib19f7032012-08-16 12:14:16 -070031/* BMS Register Offsets */
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -070032#define REVISION1 0x0
33#define REVISION2 0x1
Xiaozhe Shib19f7032012-08-16 12:14:16 -070034#define BMS1_STATUS1 0x8
35#define BMS1_MODE_CTL 0X40
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070036/* Coulomb counter clear registers */
Xiaozhe Shib19f7032012-08-16 12:14:16 -070037#define BMS1_CC_DATA_CTL 0x42
Xiaozhe Shia045a562012-11-28 16:55:39 -080038#define BMS1_CC_CLEAR_CTL 0x43
Xiaozhe Shi20640b52013-01-03 11:49:30 -080039/* BMS Tolerances */
40#define BMS1_TOL_CTL 0X44
Xiaozhe Shib19f7032012-08-16 12:14:16 -070041/* OCV limit registers */
42#define BMS1_OCV_USE_LOW_LIMIT_THR0 0x48
43#define BMS1_OCV_USE_LOW_LIMIT_THR1 0x49
44#define BMS1_OCV_USE_HIGH_LIMIT_THR0 0x4A
45#define BMS1_OCV_USE_HIGH_LIMIT_THR1 0x4B
46#define BMS1_OCV_USE_LIMIT_CTL 0x4C
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -070047/* Delay control */
48#define BMS1_S1_DELAY_CTL 0x5A
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -080049/* OCV interrupt threshold */
50#define BMS1_OCV_THR0 0x50
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -070051#define BMS1_S2_SAMP_AVG_CTL 0x61
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -080052/* SW CC interrupt threshold */
53#define BMS1_SW_CC_THR0 0xA0
Xiaozhe Shib19f7032012-08-16 12:14:16 -070054/* OCV for r registers */
55#define BMS1_OCV_FOR_R_DATA0 0x80
Xiaozhe Shib19f7032012-08-16 12:14:16 -070056#define BMS1_VSENSE_FOR_R_DATA0 0x82
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070057/* Coulomb counter data */
Xiaozhe Shib19f7032012-08-16 12:14:16 -070058#define BMS1_CC_DATA0 0x8A
Xiaozhe Shif3da8622013-06-10 14:50:56 -070059/* Shadow Coulomb counter data */
60#define BMS1_SW_CC_DATA0 0xA8
Xiaozhe Shib19f7032012-08-16 12:14:16 -070061/* OCV for soc data */
62#define BMS1_OCV_FOR_SOC_DATA0 0x90
Xiaozhe Shib19f7032012-08-16 12:14:16 -070063#define BMS1_VSENSE_PON_DATA0 0x94
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070064#define BMS1_VSENSE_AVG_DATA0 0x98
Xiaozhe Shib19f7032012-08-16 12:14:16 -070065#define BMS1_VBAT_AVG_DATA0 0x9E
Xiaozhe Shib19f7032012-08-16 12:14:16 -070066/* Extra bms registers */
Xiaozhe Shi57058942013-03-27 16:54:54 -070067#define SOC_STORAGE_REG 0xB0
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070068#define IAVG_STORAGE_REG 0xB1
Anirudh Ghayale0c02932013-07-08 16:26:35 +053069#define BMS_FCC_COUNT 0xB2
70#define BMS_FCC_BASE_REG 0xB3 /* FCC updates - 0xB3 to 0xB7 */
71#define BMS_CHGCYL_BASE_REG 0xB8 /* FCC chgcyl - 0xB8 to 0xBC */
72#define CHARGE_INCREASE_STORAGE 0xBD
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053073#define CHARGE_CYCLE_STORAGE_LSB 0xBE /* LSB=0xBE, MSB=0xBF */
74
Xiaozhe Shic40b3972012-11-30 14:11:16 -080075/* IADC Channel Select */
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -070076#define IADC1_BMS_REVISION2 0x01
Xiaozhe Shic40b3972012-11-30 14:11:16 -080077#define IADC1_BMS_ADC_CH_SEL_CTL 0x48
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -070078#define IADC1_BMS_ADC_INT_RSNSN_CTL 0x49
79#define IADC1_BMS_FAST_AVG_EN 0x5B
Xiaozhe Shib19f7032012-08-16 12:14:16 -070080
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070081/* Configuration for saving of shutdown soc/iavg */
82#define IGNORE_SOC_TEMP_DECIDEG 50
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -070083#define IAVG_STEP_SIZE_MA 10
Xiaozhe Shif5f966d2013-02-19 14:23:11 -080084#define IAVG_INVALID 0xFF
Xiaozhe Shif9f99242013-08-29 12:27:50 -070085#define SOC_INVALID 0x7E
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070086
Xiaozhe Shie118c692012-09-24 15:17:43 -070087#define IAVG_SAMPLES 16
88
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053089/* FCC learning constants */
Anirudh Ghayale0c02932013-07-08 16:26:35 +053090#define MAX_FCC_CYCLES 5
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053091#define DELTA_FCC_PERCENT 5
92#define VALID_FCC_CHGCYL_RANGE 50
Anirudh Ghayale0c02932013-07-08 16:26:35 +053093#define CHGCYL_RESOLUTION 20
94#define FCC_DEFAULT_TEMP 250
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053095
Xiaozhe Shib19f7032012-08-16 12:14:16 -070096#define QPNP_BMS_DEV_NAME "qcom,qpnp-bms"
97
Xiaozhe Shif3da8622013-06-10 14:50:56 -070098enum {
99 SHDW_CC,
100 CC
101};
102
103enum {
104 NORESET,
105 RESET
106};
107
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700108struct soc_params {
109 int fcc_uah;
110 int cc_uah;
Xiaozhe Shi904f1f72012-12-04 12:47:21 -0800111 int rbatt_mohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700112 int iavg_ua;
113 int uuc_uah;
114 int ocv_charge_uah;
Xiaozhe Shif36d2862013-01-04 10:17:35 -0800115 int delta_time_s;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700116};
117
118struct raw_soc_params {
119 uint16_t last_good_ocv_raw;
120 int64_t cc;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700121 int64_t shdw_cc;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700122 int last_good_ocv_uv;
123};
124
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530125struct fcc_sample {
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530126 int fcc_new;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530127 int chargecycles;
128};
129
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800130struct bms_irq {
131 unsigned int irq;
132 unsigned long disabled;
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800133 bool ready;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800134};
135
136struct bms_wakeup_source {
137 struct wakeup_source source;
138 unsigned long disabled;
139};
140
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700141struct qpnp_bms_chip {
142 struct device *dev;
143 struct power_supply bms_psy;
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -0700144 bool bms_psy_registered;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -0700145 struct power_supply *batt_psy;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700146 struct spmi_device *spmi;
Xiaozhe Shi27375822013-08-22 11:40:15 -0700147 wait_queue_head_t bms_wait_queue;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700148 u16 base;
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800149 u16 iadc_base;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -0700150 u16 batt_pres_addr;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -0700151 u16 soc_storage_addr;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700152
153 u8 revision1;
154 u8 revision2;
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -0700155
156 u8 iadc_bms_revision1;
157 u8 iadc_bms_revision2;
158
Xiaozhe Shid5d21412013-02-06 17:14:41 -0800159 int battery_present;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700160 int battery_status;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700161 bool batfet_closed;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800162 bool new_battery;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700163 bool done_charging;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800164 bool last_soc_invalid;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700165 /* platform data */
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -0800166 int r_sense_uohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700167 unsigned int v_cutoff_uv;
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -0800168 int max_voltage_uv;
169 int r_conn_mohm;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700170 int shutdown_soc_valid_limit;
171 int adjust_soc_low_threshold;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700172 int chg_term_ua;
Xiaozhe Shi73a65692012-09-18 17:51:57 -0700173 enum battery_type batt_type;
Xiaozhe Shi976618f2013-04-30 10:49:30 -0700174 unsigned int fcc_mah;
Xiaozhe Shi73a65692012-09-18 17:51:57 -0700175 struct single_row_lut *fcc_temp_lut;
176 struct single_row_lut *fcc_sf_lut;
177 struct pc_temp_ocv_lut *pc_temp_ocv_lut;
178 struct sf_lut *pc_sf_lut;
179 struct sf_lut *rbatt_sf_lut;
180 int default_rbatt_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -0700181 int rbatt_capacitive_mohm;
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -0700182 int rbatt_mohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700183
184 struct delayed_work calculate_soc_delayed_work;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800185 struct work_struct recalc_work;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700186 struct work_struct batfet_open_work;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700187
188 struct mutex bms_output_lock;
189 struct mutex last_ocv_uv_mutex;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700190 struct mutex vbat_monitor_mutex;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700191 struct mutex soc_invalidation_mutex;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700192 struct mutex last_soc_mutex;
Xiaozhe Shibda84992013-09-05 10:39:11 -0700193 struct mutex status_lock;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700194
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800195 bool use_external_rsense;
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800196 bool use_ocv_thresholds;
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800197
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700198 bool ignore_shutdown_soc;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800199 bool shutdown_soc_invalid;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700200 int shutdown_soc;
201 int shutdown_iavg_ma;
202
Xiaozhe Shi4be85782013-02-22 17:33:40 -0800203 struct wake_lock low_voltage_wake_lock;
Xiaozhe Shi4be85782013-02-22 17:33:40 -0800204 int low_voltage_threshold;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700205 int low_soc_calc_threshold;
206 int low_soc_calculate_soc_ms;
Xiaozhe Shicb487b12013-10-14 17:42:07 -0700207 int low_voltage_calculate_soc_ms;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700208 int calculate_soc_ms;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800209 struct bms_wakeup_source soc_wake_source;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700210 struct wake_lock cv_wake_lock;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700211
Xiaozhe Shie118c692012-09-24 15:17:43 -0700212 uint16_t ocv_reading_at_100;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700213 uint16_t prev_last_good_ocv_raw;
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700214 int insertion_ocv_uv;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700215 int last_ocv_uv;
Xiaozhe Shicc48e992013-05-28 16:42:24 -0700216 int charging_adjusted_ocv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800217 int last_ocv_temp;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700218 int last_cc_uah;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700219 unsigned long last_soc_change_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700220 unsigned long tm_sec;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700221 unsigned long report_tm_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700222 bool first_time_calc_soc;
223 bool first_time_calc_uuc;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700224 int64_t software_cc_uah;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700225 int64_t software_shdw_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700226
227 int iavg_samples_ma[IAVG_SAMPLES];
228 int iavg_index;
229 int iavg_num_samples;
230 struct timespec t_soc_queried;
231 int last_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -0700232 int last_soc_est;
Xiaozhe Shicc137262013-03-10 06:21:41 -0700233 int last_soc_unbound;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700234 bool was_charging_at_sleep;
235 int charge_start_tm_sec;
236 int catch_up_time_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700237 struct single_row_lut *adjusted_fcc_temp_lut;
238
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700239 struct qpnp_adc_tm_btm_param vbat_monitor_params;
Xiaozhe Shi535494d2013-04-05 12:27:51 -0700240 struct qpnp_adc_tm_btm_param die_temp_monitor_params;
241 int temperature_margin;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700242 unsigned int vadc_v0625;
243 unsigned int vadc_v1250;
244
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -0700245 int system_load_count;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700246 int prev_uuc_iavg_ma;
247 int prev_pc_unusable;
248 int ibat_at_cv_ua;
249 int soc_at_cv;
250 int prev_chg_soc;
251 int calculated_soc;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -0800252 int prev_voltage_based_soc;
253 bool use_voltage_soc;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700254 bool in_cv_range;
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800255
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800256 int prev_batt_terminal_uv;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -0700257 int high_ocv_correction_limit_uv;
258 int low_ocv_correction_limit_uv;
259 int flat_ocv_threshold_uv;
260 int hold_soc_est;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800261
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800262 int ocv_high_threshold_uv;
263 int ocv_low_threshold_uv;
Xiaozhe Shicdeee312012-12-18 15:10:18 -0800264 unsigned long last_recalc_time;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530265
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530266 struct fcc_sample *fcc_learning_samples;
267 u8 fcc_sample_count;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530268 int enable_fcc_learning;
269 int min_fcc_learning_soc;
270 int min_fcc_ocv_pc;
271 int min_fcc_learning_samples;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530272 int start_soc;
273 int end_soc;
274 int start_pc;
275 int start_cc_uah;
276 int start_real_soc;
277 int end_cc_uah;
278 uint16_t fcc_new_mah;
279 int fcc_new_batt_temp;
280 uint16_t charge_cycles;
281 u8 charge_increase;
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530282 int fcc_resolution;
283 bool battery_removed;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800284 struct bms_irq sw_cc_thr_irq;
285 struct bms_irq ocv_thr_irq;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700286 struct qpnp_vadc_chip *vadc_dev;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700287 struct qpnp_iadc_chip *iadc_dev;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -0700288 struct qpnp_adc_tm_chip *adc_tm_dev;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700289};
290
291static struct of_device_id qpnp_bms_match_table[] = {
292 { .compatible = QPNP_BMS_DEV_NAME },
293 {}
294};
295
296static char *qpnp_bms_supplicants[] = {
297 "battery"
298};
299
300static enum power_supply_property msm_bms_power_props[] = {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700301 POWER_SUPPLY_PROP_CAPACITY,
Xiaozhe Shibda84992013-09-05 10:39:11 -0700302 POWER_SUPPLY_PROP_STATUS,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700303 POWER_SUPPLY_PROP_CURRENT_NOW,
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -0700304 POWER_SUPPLY_PROP_RESISTANCE,
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -0700305 POWER_SUPPLY_PROP_CHARGE_COUNTER,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700306 POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700307 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +0530308 POWER_SUPPLY_PROP_CHARGE_FULL,
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +0530309 POWER_SUPPLY_PROP_CYCLE_COUNT,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700310};
311
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530312static int discard_backup_fcc_data(struct qpnp_bms_chip *chip);
313static void backup_charge_cycle(struct qpnp_bms_chip *chip);
314
Xiaozhe Shi20640b52013-01-03 11:49:30 -0800315static bool bms_reset;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -0800316
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700317static int qpnp_read_wrapper(struct qpnp_bms_chip *chip, u8 *val,
318 u16 base, int count)
319{
320 int rc;
321 struct spmi_device *spmi = chip->spmi;
322
323 rc = spmi_ext_register_readl(spmi->ctrl, spmi->sid, base, val, count);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700324 if (rc) {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700325 pr_err("SPMI read failed rc=%d\n", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700326 return rc;
327 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700328 return 0;
329}
330
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700331static int qpnp_write_wrapper(struct qpnp_bms_chip *chip, u8 *val,
332 u16 base, int count)
333{
334 int rc;
335 struct spmi_device *spmi = chip->spmi;
336
337 rc = spmi_ext_register_writel(spmi->ctrl, spmi->sid, base, val, count);
338 if (rc) {
339 pr_err("SPMI write failed rc=%d\n", rc);
340 return rc;
341 }
342 return 0;
343}
344
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800345static int qpnp_masked_write_base(struct qpnp_bms_chip *chip, u16 addr,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700346 u8 mask, u8 val)
347{
348 int rc;
349 u8 reg;
350
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800351 rc = qpnp_read_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700352 if (rc) {
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800353 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700354 return rc;
355 }
356 reg &= ~mask;
357 reg |= val & mask;
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800358 rc = qpnp_write_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700359 if (rc) {
360 pr_err("write failed addr = %03X, val = %02x, mask = %02x, reg = %02x, rc = %d\n",
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800361 addr, val, mask, reg, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700362 return rc;
363 }
364 return 0;
365}
366
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800367static int qpnp_masked_write_iadc(struct qpnp_bms_chip *chip, u16 addr,
368 u8 mask, u8 val)
369{
370 return qpnp_masked_write_base(chip, chip->iadc_base + addr, mask, val);
371}
372
373static int qpnp_masked_write(struct qpnp_bms_chip *chip, u16 addr,
374 u8 mask, u8 val)
375{
376 return qpnp_masked_write_base(chip, chip->base + addr, mask, val);
377}
378
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800379static void bms_stay_awake(struct bms_wakeup_source *source)
380{
381 if (__test_and_clear_bit(0, &source->disabled)) {
382 __pm_stay_awake(&source->source);
383 pr_debug("enabled source %s\n", source->source.name);
384 }
385}
386
387static void bms_relax(struct bms_wakeup_source *source)
388{
389 if (!__test_and_set_bit(0, &source->disabled)) {
390 __pm_relax(&source->source);
391 pr_debug("disabled source %s\n", source->source.name);
392 }
393}
394
395static void enable_bms_irq(struct bms_irq *irq)
396{
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800397 if (irq->ready && __test_and_clear_bit(0, &irq->disabled)) {
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800398 enable_irq(irq->irq);
399 pr_debug("enabled irq %d\n", irq->irq);
400 }
401}
402
403static void disable_bms_irq(struct bms_irq *irq)
404{
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800405 if (irq->ready && !__test_and_set_bit(0, &irq->disabled)) {
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800406 disable_irq(irq->irq);
407 pr_debug("disabled irq %d\n", irq->irq);
408 }
409}
410
Anirudh Ghayal1166eef2013-12-23 19:05:33 +0530411static void disable_bms_irq_nosync(struct bms_irq *irq)
412{
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800413 if (irq->ready && !__test_and_set_bit(0, &irq->disabled)) {
Anirudh Ghayal1166eef2013-12-23 19:05:33 +0530414 disable_irq_nosync(irq->irq);
415 pr_debug("disabled irq %d\n", irq->irq);
416 }
417}
418
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700419#define HOLD_OREG_DATA BIT(0)
420static int lock_output_data(struct qpnp_bms_chip *chip)
421{
422 int rc;
423
424 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL,
425 HOLD_OREG_DATA, HOLD_OREG_DATA);
426 if (rc) {
427 pr_err("couldnt lock bms output rc = %d\n", rc);
428 return rc;
429 }
430 return 0;
431}
432
433static int unlock_output_data(struct qpnp_bms_chip *chip)
434{
435 int rc;
436
437 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL, HOLD_OREG_DATA, 0);
438 if (rc) {
439 pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc);
440 return rc;
441 }
442 return 0;
443}
444
445#define V_PER_BIT_MUL_FACTOR 97656
446#define V_PER_BIT_DIV_FACTOR 1000
447#define VADC_INTRINSIC_OFFSET 0x6000
448
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800449static int vadc_reading_to_uv(int reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700450{
451 if (reading <= VADC_INTRINSIC_OFFSET)
452 return 0;
453
454 return (reading - VADC_INTRINSIC_OFFSET)
455 * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR;
456}
457
458#define VADC_CALIB_UV 625000
459#define VBATT_MUL_FACTOR 3
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800460static int adjust_vbatt_reading(struct qpnp_bms_chip *chip, int reading_uv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700461{
462 s64 numerator, denominator;
463
464 if (reading_uv == 0)
465 return 0;
466
467 /* don't adjust if not calibrated */
468 if (chip->vadc_v0625 == 0 || chip->vadc_v1250 == 0) {
469 pr_debug("No cal yet return %d\n",
470 VBATT_MUL_FACTOR * reading_uv);
471 return VBATT_MUL_FACTOR * reading_uv;
472 }
473
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700474 numerator = ((s64)reading_uv - chip->vadc_v0625) * VADC_CALIB_UV;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700475 denominator = (s64)chip->vadc_v1250 - chip->vadc_v0625;
476 if (denominator == 0)
477 return reading_uv * VBATT_MUL_FACTOR;
478 return (VADC_CALIB_UV + div_s64(numerator, denominator))
479 * VBATT_MUL_FACTOR;
480}
481
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800482static int convert_vbatt_uv_to_raw(struct qpnp_bms_chip *chip,
483 int unadjusted_vbatt)
484{
485 int scaled_vbatt = unadjusted_vbatt / VBATT_MUL_FACTOR;
486
487 if (scaled_vbatt <= 0)
488 return VADC_INTRINSIC_OFFSET;
489 return ((scaled_vbatt * V_PER_BIT_DIV_FACTOR) / V_PER_BIT_MUL_FACTOR)
490 + VADC_INTRINSIC_OFFSET;
491}
492
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700493static inline int convert_vbatt_raw_to_uv(struct qpnp_bms_chip *chip,
494 uint16_t reading)
495{
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700496 int64_t uv;
497 int rc;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700498
499 uv = vadc_reading_to_uv(reading);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700500 pr_debug("%u raw converted into %lld uv\n", reading, uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700501 uv = adjust_vbatt_reading(chip, uv);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700502 pr_debug("adjusted into %lld uv\n", uv);
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700503 rc = qpnp_vbat_sns_comp_result(chip->vadc_dev, &uv);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700504 if (rc)
505 pr_debug("could not compensate vbatt\n");
506 pr_debug("compensated into %lld uv\n", uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700507 return uv;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700508}
509
510#define CC_READING_RESOLUTION_N 542535
511#define CC_READING_RESOLUTION_D 100000
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -0700512static s64 cc_reading_to_uv(s64 reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700513{
514 return div_s64(reading * CC_READING_RESOLUTION_N,
515 CC_READING_RESOLUTION_D);
516}
517
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800518#define QPNP_ADC_GAIN_IDEAL 3291LL
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700519static s64 cc_adjust_for_gain(s64 uv, uint16_t gain)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700520{
521 s64 result_uv;
522
523 pr_debug("adjusting_uv = %lld\n", uv);
Xiaozhe Shi820a47a2012-11-27 13:23:27 -0800524 if (gain == 0) {
525 pr_debug("gain is %d, not adjusting\n", gain);
526 return uv;
527 }
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700528 pr_debug("adjusting by factor: %lld/%hu = %lld%%\n",
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800529 QPNP_ADC_GAIN_IDEAL, gain,
530 div_s64(QPNP_ADC_GAIN_IDEAL * 100LL, (s64)gain));
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700531
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800532 result_uv = div_s64(uv * QPNP_ADC_GAIN_IDEAL, (s64)gain);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700533 pr_debug("result_uv = %lld\n", result_uv);
534 return result_uv;
535}
536
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700537static s64 cc_reverse_adjust_for_gain(struct qpnp_bms_chip *chip, s64 uv)
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800538{
539 struct qpnp_iadc_calib calibration;
540 int gain;
541 s64 result_uv;
542
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700543 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800544 gain = (int)calibration.gain_raw - (int)calibration.offset_raw;
545
546 pr_debug("reverse adjusting_uv = %lld\n", uv);
547 if (gain == 0) {
548 pr_debug("gain is %d, not adjusting\n", gain);
549 return uv;
550 }
551 pr_debug("adjusting by factor: %hu/%lld = %lld%%\n",
552 gain, QPNP_ADC_GAIN_IDEAL,
553 div64_s64((s64)gain * 100LL,
554 (s64)QPNP_ADC_GAIN_IDEAL));
555
556 result_uv = div64_s64(uv * (s64)gain, QPNP_ADC_GAIN_IDEAL);
557 pr_debug("result_uv = %lld\n", result_uv);
558 return result_uv;
559}
560
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700561static int convert_vsense_to_uv(struct qpnp_bms_chip *chip,
562 int16_t reading)
563{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700564 struct qpnp_iadc_calib calibration;
565
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700566 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700567 return cc_adjust_for_gain(cc_reading_to_uv(reading),
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800568 calibration.gain_raw - calibration.offset_raw);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700569}
570
571static int read_vsense_avg(struct qpnp_bms_chip *chip, int *result_uv)
572{
573 int rc;
574 int16_t reading;
575
576 rc = qpnp_read_wrapper(chip, (u8 *)&reading,
577 chip->base + BMS1_VSENSE_AVG_DATA0, 2);
578
579 if (rc) {
580 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
581 return rc;
582 }
583
584 *result_uv = convert_vsense_to_uv(chip, reading);
585 return 0;
586}
587
588static int get_battery_current(struct qpnp_bms_chip *chip, int *result_ua)
589{
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700590 int rc, vsense_uv = 0;
591 int64_t temp_current;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700592
Xiaozhe Shid0a79542012-11-06 10:00:38 -0800593 if (chip->r_sense_uohm == 0) {
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700594 pr_err("r_sense is zero\n");
595 return -EINVAL;
596 }
597
598 mutex_lock(&chip->bms_output_lock);
599 lock_output_data(chip);
600 read_vsense_avg(chip, &vsense_uv);
601 unlock_output_data(chip);
602 mutex_unlock(&chip->bms_output_lock);
603
604 pr_debug("vsense_uv=%duV\n", vsense_uv);
605 /* cast for signed division */
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700606 temp_current = div_s64((vsense_uv * 1000000LL),
607 (int)chip->r_sense_uohm);
608
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700609 rc = qpnp_iadc_comp_result(chip->iadc_dev, &temp_current);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700610 if (rc)
611 pr_debug("error compensation failed: %d\n", rc);
612
613 *result_ua = temp_current;
614 pr_debug("err compensated ibat=%duA\n", *result_ua);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700615 return 0;
616}
617
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700618static int get_battery_voltage(struct qpnp_bms_chip *chip, int *result_uv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700619{
620 int rc;
621 struct qpnp_vadc_result adc_result;
622
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700623 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &adc_result);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700624 if (rc) {
625 pr_err("error reading adc channel = %d, rc = %d\n",
626 VBAT_SNS, rc);
627 return rc;
628 }
629 pr_debug("mvolts phy = %lld meas = 0x%llx\n", adc_result.physical,
630 adc_result.measurement);
631 *result_uv = (int)adc_result.physical;
632 return 0;
633}
634
Xiaozhe Shie118c692012-09-24 15:17:43 -0700635#define CC_36_BIT_MASK 0xFFFFFFFFFLL
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800636static uint64_t convert_s64_to_s36(int64_t raw64)
637{
638 return (uint64_t) raw64 & CC_36_BIT_MASK;
639}
640
641#define SIGN_EXTEND_36_TO_64_MASK (-1LL ^ CC_36_BIT_MASK)
642static int64_t convert_s36_to_s64(uint64_t raw36)
643{
644 raw36 = raw36 & CC_36_BIT_MASK;
645 /* convert 36 bit signed value into 64 signed value */
646 return (raw36 >> 35) == 0LL ?
647 raw36 : (SIGN_EXTEND_36_TO_64_MASK | raw36);
648}
649
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700650static int read_cc_raw(struct qpnp_bms_chip *chip, int64_t *reading,
651 int cc_type)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700652{
653 int64_t raw_reading;
654 int rc;
655
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700656 if (cc_type == SHDW_CC)
657 rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
658 chip->base + BMS1_SW_CC_DATA0, 5);
659 else
660 rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
661 chip->base + BMS1_CC_DATA0, 5);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700662 if (rc) {
663 pr_err("Error reading cc: rc = %d\n", rc);
664 return -ENXIO;
665 }
666
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800667 *reading = convert_s36_to_s64(raw_reading);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700668
669 return 0;
670}
671
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700672static int calib_vadc(struct qpnp_bms_chip *chip)
673{
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700674 int rc, raw_0625, raw_1250;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700675 struct qpnp_vadc_result result;
676
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700677 rc = qpnp_vadc_read(chip->vadc_dev, REF_625MV, &result);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700678 if (rc) {
679 pr_debug("vadc read failed with rc = %d\n", rc);
680 return rc;
681 }
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700682 raw_0625 = result.adc_code;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700683
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700684 rc = qpnp_vadc_read(chip->vadc_dev, REF_125V, &result);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700685 if (rc) {
686 pr_debug("vadc read failed with rc = %d\n", rc);
687 return rc;
688 }
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700689 raw_1250 = result.adc_code;
690 chip->vadc_v0625 = vadc_reading_to_uv(raw_0625);
691 chip->vadc_v1250 = vadc_reading_to_uv(raw_1250);
692 pr_debug("vadc calib: 0625 = %d raw (%d uv), 1250 = %d raw (%d uv)\n",
693 raw_0625, chip->vadc_v0625,
694 raw_1250, chip->vadc_v1250);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700695 return 0;
696}
697
Xiaozhe Shie118c692012-09-24 15:17:43 -0700698static void convert_and_store_ocv(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800699 struct raw_soc_params *raw,
700 int batt_temp)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700701{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700702 int rc;
703
704 pr_debug("prev_last_good_ocv_raw = %d, last_good_ocv_raw = %d\n",
705 chip->prev_last_good_ocv_raw,
706 raw->last_good_ocv_raw);
707 rc = calib_vadc(chip);
708 if (rc)
709 pr_err("Vadc reference voltage read failed, rc = %d\n", rc);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700710 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
711 raw->last_good_ocv_uv = convert_vbatt_raw_to_uv(chip,
712 raw->last_good_ocv_raw);
713 chip->last_ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800714 chip->last_ocv_temp = batt_temp;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700715 chip->software_cc_uah = 0;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700716 pr_debug("last_good_ocv_uv = %d\n", raw->last_good_ocv_uv);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700717}
718
Xiaozhe Shia045a562012-11-28 16:55:39 -0800719#define CLEAR_CC BIT(7)
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700720#define CLEAR_SHDW_CC BIT(6)
Xiaozhe Shia045a562012-11-28 16:55:39 -0800721/**
722 * reset both cc and sw-cc.
723 * note: this should only be ever called from one thread
724 * or there may be a race condition where CC is never enabled
725 * again
726 */
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700727static void reset_cc(struct qpnp_bms_chip *chip, u8 flags)
Xiaozhe Shia045a562012-11-28 16:55:39 -0800728{
729 int rc;
730
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700731 pr_debug("resetting cc manually with flags %hhu\n", flags);
732 mutex_lock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800733 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700734 flags,
735 flags);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800736 if (rc)
737 pr_err("cc reset failed: %d\n", rc);
738
739 /* wait for 100us for cc to reset */
740 udelay(100);
741
742 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700743 flags, 0);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800744 if (rc)
745 pr_err("cc reenable failed: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700746 mutex_unlock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800747}
748
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700749static int get_battery_status(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800750{
751 union power_supply_propval ret = {0,};
752
753 if (chip->batt_psy == NULL)
754 chip->batt_psy = power_supply_get_by_name("battery");
755 if (chip->batt_psy) {
756 /* if battery has been registered, use the status property */
757 chip->batt_psy->get_property(chip->batt_psy,
758 POWER_SUPPLY_PROP_STATUS, &ret);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700759 return ret.intval;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800760 }
761
762 /* Default to false if the battery power supply is not registered. */
763 pr_debug("battery power supply is not registered\n");
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700764 return POWER_SUPPLY_STATUS_UNKNOWN;
765}
766
767static bool is_battery_charging(struct qpnp_bms_chip *chip)
768{
Xiaozhe Shi72a72f22013-12-26 13:54:29 -0800769 union power_supply_propval ret = {0,};
770
771 if (chip->batt_psy == NULL)
772 chip->batt_psy = power_supply_get_by_name("battery");
773 if (chip->batt_psy) {
774 /* if battery has been registered, use the status property */
775 chip->batt_psy->get_property(chip->batt_psy,
776 POWER_SUPPLY_PROP_CHARGE_TYPE, &ret);
777 return ret.intval != POWER_SUPPLY_CHARGE_TYPE_NONE;
778 }
779
780 /* Default to false if the battery power supply is not registered. */
781 pr_debug("battery power supply is not registered\n");
782 return false;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800783}
784
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700785static bool is_battery_full(struct qpnp_bms_chip *chip)
786{
787 return get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL;
788}
789
Xiaozhe Shi4515c762013-11-13 16:36:54 -0800790#define BAT_PRES_BIT BIT(7)
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700791static bool is_battery_present(struct qpnp_bms_chip *chip)
792{
793 union power_supply_propval ret = {0,};
Xiaozhe Shi4515c762013-11-13 16:36:54 -0800794 int rc;
795 u8 batt_pres;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700796
Xiaozhe Shi4515c762013-11-13 16:36:54 -0800797 /* first try to use the batt_pres register if given */
798 if (chip->batt_pres_addr) {
799 rc = qpnp_read_wrapper(chip, &batt_pres,
800 chip->batt_pres_addr, 1);
801 if (!rc && (batt_pres & BAT_PRES_BIT))
802 return true;
803 else
804 return false;
805 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700806 if (chip->batt_psy == NULL)
807 chip->batt_psy = power_supply_get_by_name("battery");
808 if (chip->batt_psy) {
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700809 /* if battery has been registered, use the present property */
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700810 chip->batt_psy->get_property(chip->batt_psy,
811 POWER_SUPPLY_PROP_PRESENT, &ret);
812 return ret.intval;
813 }
814
815 /* Default to false if the battery power supply is not registered. */
816 pr_debug("battery power supply is not registered\n");
817 return false;
818}
819
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700820static int get_battery_insertion_ocv_uv(struct qpnp_bms_chip *chip)
821{
822 union power_supply_propval ret = {0,};
823 int rc, vbat;
824
825 if (chip->batt_psy == NULL)
826 chip->batt_psy = power_supply_get_by_name("battery");
827 if (chip->batt_psy) {
828 /* if battery has been registered, use the ocv property */
829 rc = chip->batt_psy->get_property(chip->batt_psy,
830 POWER_SUPPLY_PROP_VOLTAGE_OCV, &ret);
831 if (rc) {
832 /*
833 * Default to vbatt if the battery OCV is not
834 * registered.
835 */
836 pr_debug("Battery psy does not have voltage ocv\n");
837 rc = get_battery_voltage(chip, &vbat);
838 if (rc)
839 return -EINVAL;
840 return vbat;
841 }
842 return ret.intval;
843 }
844
845 pr_debug("battery power supply is not registered\n");
846 return -EINVAL;
847}
848
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700849static bool is_batfet_closed(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800850{
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700851 union power_supply_propval ret = {0,};
852
853 if (chip->batt_psy == NULL)
854 chip->batt_psy = power_supply_get_by_name("battery");
855 if (chip->batt_psy) {
856 /* if battery has been registered, use the online property */
857 chip->batt_psy->get_property(chip->batt_psy,
858 POWER_SUPPLY_PROP_ONLINE, &ret);
859 return !!ret.intval;
860 }
861
862 /* Default to true if the battery power supply is not registered. */
863 pr_debug("battery power supply is not registered\n");
864 return true;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800865}
866
867static int get_simultaneous_batt_v_and_i(struct qpnp_bms_chip *chip,
868 int *ibat_ua, int *vbat_uv)
869{
870 struct qpnp_iadc_result i_result;
871 struct qpnp_vadc_result v_result;
872 enum qpnp_iadc_channels iadc_channel;
873 int rc;
874
875 iadc_channel = chip->use_external_rsense ?
876 EXTERNAL_RSENSE : INTERNAL_RSENSE;
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700877 if (is_battery_full(chip)) {
878 rc = get_battery_current(chip, ibat_ua);
879 if (rc) {
880 pr_err("bms current read failed with rc: %d\n", rc);
881 return rc;
882 }
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700883 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &v_result);
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700884 if (rc) {
885 pr_err("vadc read failed with rc: %d\n", rc);
886 return rc;
887 }
888 *vbat_uv = (int)v_result.physical;
889 } else {
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700890 rc = qpnp_iadc_vadc_sync_read(chip->iadc_dev,
891 iadc_channel, &i_result,
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700892 VBAT_SNS, &v_result);
893 if (rc) {
894 pr_err("adc sync read failed with rc: %d\n", rc);
895 return rc;
896 }
897 /*
898 * reverse the current read by the iadc, since the bms uses
899 * flipped battery current polarity.
900 */
901 *ibat_ua = -1 * (int)i_result.result_ua;
902 *vbat_uv = (int)v_result.physical;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800903 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800904
905 return 0;
906}
907
908static int estimate_ocv(struct qpnp_bms_chip *chip)
909{
910 int ibat_ua, vbat_uv, ocv_est_uv;
911 int rc;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -0700912 int rbatt_mohm = chip->default_rbatt_mohm + chip->r_conn_mohm
913 + chip->rbatt_capacitive_mohm;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800914
915 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
916 if (rc) {
917 pr_err("simultaneous failed rc = %d\n", rc);
918 return rc;
919 }
920
921 ocv_est_uv = vbat_uv + (ibat_ua * rbatt_mohm) / 1000;
922 pr_debug("estimated pon ocv = %d\n", ocv_est_uv);
923 return ocv_est_uv;
924}
925
926static void reset_for_new_battery(struct qpnp_bms_chip *chip, int batt_temp)
927{
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700928 chip->last_ocv_uv = chip->insertion_ocv_uv;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700929 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800930 chip->last_soc = -EINVAL;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700931 chip->last_soc_invalid = true;
932 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800933 chip->soc_at_cv = -EINVAL;
934 chip->shutdown_soc_invalid = true;
935 chip->shutdown_soc = 0;
936 chip->shutdown_iavg_ma = 0;
937 chip->prev_pc_unusable = -EINVAL;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700938 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700939 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700940 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800941 chip->last_cc_uah = INT_MIN;
942 chip->last_ocv_temp = batt_temp;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800943 chip->prev_batt_terminal_uv = 0;
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530944 if (chip->enable_fcc_learning) {
945 chip->adjusted_fcc_temp_lut = NULL;
946 chip->fcc_new_mah = -EINVAL;
947 /* reset the charge-cycle and charge-increase registers */
948 chip->charge_increase = 0;
949 chip->charge_cycles = 0;
950 backup_charge_cycle(chip);
951 /* discard all the FCC learnt data and reset the local table */
952 discard_backup_fcc_data(chip);
953 memset(chip->fcc_learning_samples, 0,
954 chip->min_fcc_learning_samples *
955 sizeof(struct fcc_sample));
956 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800957}
958
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -0700959#define SIGN(x) ((x) < 0 ? -1 : 1)
960#define UV_PER_SPIN 50000
961static int find_ocv_for_pc(struct qpnp_bms_chip *chip, int batt_temp, int pc)
962{
963 int new_pc;
964 int batt_temp_degc = batt_temp / 10;
965 int ocv_mv;
966 int delta_mv = 5;
967 int max_spin_count;
968 int count = 0;
969 int sign, new_sign;
970
971 ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut, batt_temp_degc, pc);
972
973 new_pc = interpolate_pc(chip->pc_temp_ocv_lut, batt_temp_degc, ocv_mv);
974 pr_debug("test revlookup pc = %d for ocv = %d\n", new_pc, ocv_mv);
975 max_spin_count = 1 + (chip->max_voltage_uv - chip->v_cutoff_uv)
976 / UV_PER_SPIN;
977 sign = SIGN(pc - new_pc);
978
979 while (abs(new_pc - pc) != 0 && count < max_spin_count) {
980 /*
981 * If the newly interpolated pc is larger than the lookup pc,
982 * the ocv should be reduced and vice versa
983 */
984 new_sign = SIGN(pc - new_pc);
985 /*
986 * If the sign has changed, then we have passed the lookup pc.
987 * reduce the ocv step size to get finer results.
988 *
989 * If we have already reduced the ocv step size and still
990 * passed the lookup pc, just stop and use the current ocv.
991 * This can only happen if the batterydata profile is
992 * non-monotonic anyways.
993 */
994 if (new_sign != sign) {
995 if (delta_mv > 1)
996 delta_mv = 1;
997 else
998 break;
999 }
1000 sign = new_sign;
1001
1002 ocv_mv = ocv_mv + delta_mv * sign;
1003 new_pc = interpolate_pc(chip->pc_temp_ocv_lut,
1004 batt_temp_degc, ocv_mv);
1005 pr_debug("test revlookup pc = %d for ocv = %d\n",
1006 new_pc, ocv_mv);
1007 count++;
1008 }
1009
1010 return ocv_mv * 1000;
1011}
1012
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08001013#define OCV_RAW_UNINITIALIZED 0xFFFF
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001014#define MIN_OCV_UV 2000000
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001015static int read_soc_params_raw(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001016 struct raw_soc_params *raw,
1017 int batt_temp)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001018{
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001019 int warm_reset, rc;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001020
1021 mutex_lock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -08001022
Xiaozhe Shie118c692012-09-24 15:17:43 -07001023 lock_output_data(chip);
1024
1025 rc = qpnp_read_wrapper(chip, (u8 *)&raw->last_good_ocv_raw,
1026 chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
1027 if (rc) {
1028 pr_err("Error reading ocv: rc = %d\n", rc);
1029 return -ENXIO;
1030 }
1031
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001032 rc = read_cc_raw(chip, &raw->cc, CC);
1033 rc = read_cc_raw(chip, &raw->shdw_cc, SHDW_CC);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001034 if (rc) {
1035 pr_err("Failed to read raw cc data, rc = %d\n", rc);
1036 return rc;
1037 }
1038
1039 unlock_output_data(chip);
1040 mutex_unlock(&chip->bms_output_lock);
1041
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08001042 if (chip->prev_last_good_ocv_raw == OCV_RAW_UNINITIALIZED) {
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001043 convert_and_store_ocv(chip, raw, batt_temp);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001044 pr_debug("PON_OCV_UV = %d, cc = %llx\n",
1045 chip->last_ocv_uv, raw->cc);
1046 warm_reset = qpnp_pon_is_warm_reset();
1047 if (raw->last_good_ocv_uv < MIN_OCV_UV
1048 || warm_reset > 0) {
1049 pr_debug("OCV is stale or bad, estimating new OCV.\n");
1050 chip->last_ocv_uv = estimate_ocv(chip);
1051 raw->last_good_ocv_uv = chip->last_ocv_uv;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001052 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001053 pr_debug("New PON_OCV_UV = %d, cc = %llx\n",
1054 chip->last_ocv_uv, raw->cc);
1055 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001056 } else if (chip->new_battery) {
1057 /* if a new battery was inserted, estimate the ocv */
1058 reset_for_new_battery(chip, batt_temp);
1059 raw->cc = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001060 raw->shdw_cc = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001061 raw->last_good_ocv_uv = chip->last_ocv_uv;
1062 chip->new_battery = false;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001063 } else if (chip->done_charging) {
1064 chip->done_charging = false;
1065 /* if we just finished charging, reset CC and fake 100% */
1066 chip->ocv_reading_at_100 = raw->last_good_ocv_raw;
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001067 chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp, 100);
1068 raw->last_good_ocv_uv = chip->last_ocv_uv;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001069 raw->cc = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001070 raw->shdw_cc = 0;
1071 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001072 chip->last_ocv_temp = batt_temp;
1073 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001074 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001075 chip->last_cc_uah = INT_MIN;
1076 pr_debug("EOC Battery full ocv_reading = 0x%x\n",
1077 chip->ocv_reading_at_100);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001078 } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) {
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001079 convert_and_store_ocv(chip, raw, batt_temp);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001080 /* forget the old cc value upon ocv */
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001081 chip->last_cc_uah = INT_MIN;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001082 } else {
1083 raw->last_good_ocv_uv = chip->last_ocv_uv;
1084 }
1085
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001086 /* stop faking a high OCV if we get a new OCV */
1087 if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw)
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08001088 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001089
Xiaozhe Shie118c692012-09-24 15:17:43 -07001090 pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n",
1091 raw->last_good_ocv_raw, raw->last_good_ocv_uv);
1092 pr_debug("cc_raw= 0x%llx\n", raw->cc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001093 return 0;
1094}
1095
Xiaozhe Shie118c692012-09-24 15:17:43 -07001096static int calculate_pc(struct qpnp_bms_chip *chip, int ocv_uv,
1097 int batt_temp)
1098{
1099 int pc;
1100
1101 pc = interpolate_pc(chip->pc_temp_ocv_lut,
1102 batt_temp / 10, ocv_uv / 1000);
1103 pr_debug("pc = %u %% for ocv = %d uv batt_temp = %d\n",
1104 pc, ocv_uv, batt_temp);
1105 /* Multiply the initial FCC value by the scale factor. */
1106 return pc;
1107}
1108
1109static int calculate_fcc(struct qpnp_bms_chip *chip, int batt_temp)
1110{
1111 int fcc_uah;
1112
1113 if (chip->adjusted_fcc_temp_lut == NULL) {
1114 /* interpolate_fcc returns a mv value. */
1115 fcc_uah = interpolate_fcc(chip->fcc_temp_lut,
1116 batt_temp) * 1000;
1117 pr_debug("fcc = %d uAh\n", fcc_uah);
1118 return fcc_uah;
1119 } else {
1120 return 1000 * interpolate_fcc(chip->adjusted_fcc_temp_lut,
1121 batt_temp);
1122 }
1123}
1124
1125/* calculate remaining charge at the time of ocv */
1126static int calculate_ocv_charge(struct qpnp_bms_chip *chip,
1127 struct raw_soc_params *raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001128 int fcc_uah)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001129{
1130 int ocv_uv, pc;
1131
1132 ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001133 pc = calculate_pc(chip, ocv_uv, chip->last_ocv_temp);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001134 pr_debug("ocv_uv = %d pc = %d\n", ocv_uv, pc);
1135 return (fcc_uah * pc) / 100;
1136}
1137
Xiaozhe Shie118c692012-09-24 15:17:43 -07001138#define CC_READING_TICKS 56
1139#define SLEEP_CLK_HZ 32764
1140#define SECONDS_PER_HOUR 3600
1141
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001142static s64 cc_uv_to_pvh(s64 cc_uv)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001143{
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001144 /* Note that it is necessary need to multiply by 1000000 to convert
1145 * from uvh to pvh here.
1146 * However, the maximum Coulomb Counter value is 2^35, which can cause
1147 * an over flow.
1148 * Multiply by 100000 first to perserve as much precision as possible
1149 * then multiply by 10 after doing the division in order to avoid
1150 * overflow on the maximum Coulomb Counter value.
1151 */
1152 return div_s64(cc_uv * CC_READING_TICKS * 100000,
1153 SLEEP_CLK_HZ * SECONDS_PER_HOUR) * 10;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001154}
1155
1156/**
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001157 * calculate_cc() - converts a hardware coulomb counter reading into uah
Xiaozhe Shie118c692012-09-24 15:17:43 -07001158 * @chip: the bms chip pointer
1159 * @cc: the cc reading from bms h/w
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001160 * @cc_type: calcualte cc from regular or shadow coulomb counter
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001161 * @clear_cc: whether this function should clear the hardware counter
1162 * after reading
Xiaozhe Shie118c692012-09-24 15:17:43 -07001163 *
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001164 * Converts the 64 bit hardware coulomb counter into microamp-hour by taking
1165 * into account hardware resolution and adc errors.
1166 *
1167 * Return: the coulomb counter based charge in uAh (micro-amp hour)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001168 */
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001169static int calculate_cc(struct qpnp_bms_chip *chip, int64_t cc,
1170 int cc_type, int clear_cc)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001171{
Xiaozhe Shi4e376652012-10-25 12:38:50 -07001172 struct qpnp_iadc_calib calibration;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001173 struct qpnp_vadc_result result;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001174 int64_t cc_voltage_uv, cc_pvh, cc_uah, *software_counter;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001175 int rc;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001176
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001177 software_counter = cc_type == SHDW_CC ?
1178 &chip->software_shdw_cc_uah : &chip->software_cc_uah;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001179 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001180 if (rc) {
1181 pr_err("could not read pmic die temperature: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001182 return *software_counter;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001183 }
1184
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07001185 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001186 pr_debug("%scc = %lld, die_temp = %lld\n",
1187 cc_type == SHDW_CC ? "shdw_" : "",
1188 cc, result.physical);
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -07001189 cc_voltage_uv = cc_reading_to_uv(cc);
Xiaozhe Shi0c484932013-02-05 16:14:10 -08001190 cc_voltage_uv = cc_adjust_for_gain(cc_voltage_uv,
1191 calibration.gain_raw
1192 - calibration.offset_raw);
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001193 cc_pvh = cc_uv_to_pvh(cc_voltage_uv);
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001194 cc_uah = div_s64(cc_pvh, chip->r_sense_uohm);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07001195 rc = qpnp_iadc_comp_result(chip->iadc_dev, &cc_uah);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001196 if (rc)
1197 pr_debug("error compensation failed: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001198 if (clear_cc == RESET) {
1199 pr_debug("software_%scc = %lld, added cc_uah = %lld\n",
1200 cc_type == SHDW_CC ? "sw_" : "",
1201 *software_counter, cc_uah);
1202 *software_counter += cc_uah;
1203 reset_cc(chip, cc_type == SHDW_CC ? CLEAR_SHDW_CC : CLEAR_CC);
1204 return (int)*software_counter;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001205 } else {
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001206 pr_debug("software_%scc = %lld, cc_uah = %lld, total = %lld\n",
1207 cc_type == SHDW_CC ? "shdw_" : "",
1208 *software_counter, cc_uah,
1209 *software_counter + cc_uah);
1210 return *software_counter + cc_uah;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001211 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07001212}
1213
1214static int get_rbatt(struct qpnp_bms_chip *chip,
1215 int soc_rbatt_mohm, int batt_temp)
1216{
1217 int rbatt_mohm, scalefactor;
1218
1219 rbatt_mohm = chip->default_rbatt_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001220 if (chip->rbatt_sf_lut == NULL) {
1221 pr_debug("RBATT = %d\n", rbatt_mohm);
1222 return rbatt_mohm;
1223 }
1224 /* Convert the batt_temp to DegC from deciDegC */
1225 batt_temp = batt_temp / 10;
1226 scalefactor = interpolate_scalingfactor(chip->rbatt_sf_lut,
1227 batt_temp, soc_rbatt_mohm);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001228 rbatt_mohm = (rbatt_mohm * scalefactor) / 100;
1229
1230 rbatt_mohm += chip->r_conn_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07001231 rbatt_mohm += chip->rbatt_capacitive_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001232 return rbatt_mohm;
1233}
1234
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -07001235#define IAVG_MINIMAL_TIME 2
Xiaozhe Shie118c692012-09-24 15:17:43 -07001236static void calculate_iavg(struct qpnp_bms_chip *chip, int cc_uah,
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001237 int *iavg_ua, int delta_time_s)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001238{
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001239 int delta_cc_uah = 0;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001240
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -07001241 /*
1242 * use the battery current if called too quickly
1243 */
1244 if (delta_time_s < IAVG_MINIMAL_TIME
1245 || chip->last_cc_uah == INT_MIN) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001246 get_battery_current(chip, iavg_ua);
1247 goto out;
1248 }
1249
Xiaozhe Shie118c692012-09-24 15:17:43 -07001250 delta_cc_uah = cc_uah - chip->last_cc_uah;
1251
1252 *iavg_ua = div_s64((s64)delta_cc_uah * 3600, delta_time_s);
1253
Xiaozhe Shie118c692012-09-24 15:17:43 -07001254out:
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001255 pr_debug("delta_cc = %d iavg_ua = %d\n", delta_cc_uah, (int)*iavg_ua);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001256
1257 /* remember cc_uah */
1258 chip->last_cc_uah = cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001259}
1260
1261static int calculate_termination_uuc(struct qpnp_bms_chip *chip,
1262 struct soc_params *params,
1263 int batt_temp, int uuc_iavg_ma,
1264 int *ret_pc_unusable)
1265{
1266 int unusable_uv, pc_unusable, uuc_uah;
1267 int i = 0;
1268 int ocv_mv;
1269 int batt_temp_degc = batt_temp / 10;
1270 int rbatt_mohm;
1271 int delta_uv;
1272 int prev_delta_uv = 0;
1273 int prev_rbatt_mohm = 0;
1274 int uuc_rbatt_mohm;
1275
1276 for (i = 0; i <= 100; i++) {
1277 ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
1278 batt_temp_degc, i);
1279 rbatt_mohm = get_rbatt(chip, i, batt_temp);
1280 unusable_uv = (rbatt_mohm * uuc_iavg_ma)
1281 + (chip->v_cutoff_uv);
1282 delta_uv = ocv_mv * 1000 - unusable_uv;
1283
Xiaozhe Shie118c692012-09-24 15:17:43 -07001284 if (delta_uv > 0)
1285 break;
1286
1287 prev_delta_uv = delta_uv;
1288 prev_rbatt_mohm = rbatt_mohm;
1289 }
1290
1291 uuc_rbatt_mohm = linear_interpolate(rbatt_mohm, delta_uv,
1292 prev_rbatt_mohm, prev_delta_uv,
1293 0);
1294
1295 unusable_uv = (uuc_rbatt_mohm * uuc_iavg_ma) + (chip->v_cutoff_uv);
1296
1297 pc_unusable = calculate_pc(chip, unusable_uv, batt_temp);
1298 uuc_uah = (params->fcc_uah * pc_unusable) / 100;
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001299 pr_debug("For uuc_iavg_ma = %d, unusable_rbatt = %d unusable_uv = %d unusable_pc = %d rbatt_pc = %d uuc = %d\n",
Xiaozhe Shie118c692012-09-24 15:17:43 -07001300 uuc_iavg_ma,
1301 uuc_rbatt_mohm, unusable_uv,
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001302 pc_unusable, i, uuc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001303 *ret_pc_unusable = pc_unusable;
1304 return uuc_uah;
1305}
1306
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001307#define TIME_PER_PERCENT_UUC 60
Xiaozhe Shie118c692012-09-24 15:17:43 -07001308static int adjust_uuc(struct qpnp_bms_chip *chip,
1309 struct soc_params *params,
1310 int new_pc_unusable,
1311 int new_uuc_uah,
1312 int batt_temp)
1313{
1314 int new_unusable_mv, new_iavg_ma;
1315 int batt_temp_degc = batt_temp / 10;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001316 int max_percent_change;
1317
1318 max_percent_change = max(params->delta_time_s
1319 / TIME_PER_PERCENT_UUC, 1);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001320
1321 if (chip->prev_pc_unusable == -EINVAL
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001322 || abs(chip->prev_pc_unusable - new_pc_unusable)
1323 <= max_percent_change) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001324 chip->prev_pc_unusable = new_pc_unusable;
1325 return new_uuc_uah;
1326 }
1327
1328 /* the uuc is trying to change more than 1% restrict it */
1329 if (new_pc_unusable > chip->prev_pc_unusable)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001330 chip->prev_pc_unusable += max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001331 else
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001332 chip->prev_pc_unusable -= max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001333
1334 new_uuc_uah = (params->fcc_uah * chip->prev_pc_unusable) / 100;
1335
1336 /* also find update the iavg_ma accordingly */
1337 new_unusable_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
1338 batt_temp_degc, chip->prev_pc_unusable);
1339 if (new_unusable_mv < chip->v_cutoff_uv/1000)
1340 new_unusable_mv = chip->v_cutoff_uv/1000;
1341
1342 new_iavg_ma = (new_unusable_mv * 1000 - chip->v_cutoff_uv)
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001343 / params->rbatt_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001344 if (new_iavg_ma == 0)
1345 new_iavg_ma = 1;
1346 chip->prev_uuc_iavg_ma = new_iavg_ma;
1347 pr_debug("Restricting UUC to %d (%d%%) unusable_mv = %d iavg_ma = %d\n",
1348 new_uuc_uah, chip->prev_pc_unusable,
1349 new_unusable_mv, new_iavg_ma);
1350
1351 return new_uuc_uah;
1352}
1353
Abhijeet Dharmapurikarbdf8ba82012-12-20 18:33:56 -08001354#define MIN_IAVG_MA 250
Xiaozhe Shie118c692012-09-24 15:17:43 -07001355static int calculate_unusable_charge_uah(struct qpnp_bms_chip *chip,
1356 struct soc_params *params,
1357 int batt_temp)
1358{
1359 int uuc_uah_iavg;
1360 int i;
1361 int uuc_iavg_ma = params->iavg_ua / 1000;
1362 int pc_unusable;
1363
1364 /*
1365 * if called first time, fill all the samples with
1366 * the shutdown_iavg_ma
1367 */
1368 if (chip->first_time_calc_uuc && chip->shutdown_iavg_ma != 0) {
1369 pr_debug("Using shutdown_iavg_ma = %d in all samples\n",
1370 chip->shutdown_iavg_ma);
1371 for (i = 0; i < IAVG_SAMPLES; i++)
1372 chip->iavg_samples_ma[i] = chip->shutdown_iavg_ma;
1373
1374 chip->iavg_index = 0;
1375 chip->iavg_num_samples = IAVG_SAMPLES;
1376 }
1377
Xiaozhe Shi70633922013-09-23 15:50:53 -07001378 if (params->delta_time_s >= IAVG_MINIMAL_TIME) {
1379 /*
1380 * if charging use a nominal avg current to keep
1381 * a reasonable UUC while charging
1382 */
1383 if (uuc_iavg_ma < MIN_IAVG_MA)
1384 uuc_iavg_ma = MIN_IAVG_MA;
1385 chip->iavg_samples_ma[chip->iavg_index] = uuc_iavg_ma;
1386 chip->iavg_index = (chip->iavg_index + 1) % IAVG_SAMPLES;
1387 chip->iavg_num_samples++;
1388 if (chip->iavg_num_samples >= IAVG_SAMPLES)
1389 chip->iavg_num_samples = IAVG_SAMPLES;
1390 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07001391
1392 /* now that this sample is added calcualte the average */
1393 uuc_iavg_ma = 0;
1394 if (chip->iavg_num_samples != 0) {
1395 for (i = 0; i < chip->iavg_num_samples; i++) {
1396 pr_debug("iavg_samples_ma[%d] = %d\n", i,
1397 chip->iavg_samples_ma[i]);
1398 uuc_iavg_ma += chip->iavg_samples_ma[i];
1399 }
1400
1401 uuc_iavg_ma = DIV_ROUND_CLOSEST(uuc_iavg_ma,
1402 chip->iavg_num_samples);
1403 }
1404
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001405 /*
1406 * if we're in bms reset mode, force uuc to be 3% of fcc
1407 */
1408 if (bms_reset)
1409 return (params->fcc_uah * 3) / 100;
1410
Xiaozhe Shi75e5efe2013-02-07 09:51:43 -08001411 uuc_uah_iavg = calculate_termination_uuc(chip, params, batt_temp,
1412 uuc_iavg_ma, &pc_unusable);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001413 pr_debug("uuc_iavg_ma = %d uuc with iavg = %d\n",
1414 uuc_iavg_ma, uuc_uah_iavg);
1415
1416 chip->prev_uuc_iavg_ma = uuc_iavg_ma;
1417 /* restrict the uuc such that it can increase only by one percent */
1418 uuc_uah_iavg = adjust_uuc(chip, params, pc_unusable,
1419 uuc_uah_iavg, batt_temp);
1420
Xiaozhe Shie118c692012-09-24 15:17:43 -07001421 return uuc_uah_iavg;
1422}
1423
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001424static s64 find_ocv_charge_for_soc(struct qpnp_bms_chip *chip,
1425 struct soc_params *params, int soc)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001426{
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001427 return div_s64((s64)soc * (params->fcc_uah - params->uuc_uah),
1428 100) + params->cc_uah + params->uuc_uah;
1429}
Xiaozhe Shie118c692012-09-24 15:17:43 -07001430
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001431static int find_pc_for_soc(struct qpnp_bms_chip *chip,
1432 struct soc_params *params, int soc)
1433{
1434 int ocv_charge_uah = find_ocv_charge_for_soc(chip, params, soc);
1435 int pc;
1436
Xiaozhe Shie118c692012-09-24 15:17:43 -07001437 pc = DIV_ROUND_CLOSEST((int)ocv_charge_uah * 100, params->fcc_uah);
1438 pc = clamp(pc, 0, 100);
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001439 pr_debug("soc = %d, fcc = %d uuc = %d rc = %d pc = %d\n",
1440 soc, params->fcc_uah, params->uuc_uah,
1441 ocv_charge_uah, pc);
1442 return pc;
1443}
Xiaozhe Shie118c692012-09-24 15:17:43 -07001444
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001445static int get_current_time(unsigned long *now_tm_sec)
1446{
1447 struct rtc_time tm;
1448 struct rtc_device *rtc;
1449 int rc;
1450
1451 rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
1452 if (rtc == NULL) {
1453 pr_err("%s: unable to open rtc device (%s)\n",
1454 __FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
Xiaozhe Shi0e01af62013-05-06 12:56:08 -07001455 return -EINVAL;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001456 }
1457
1458 rc = rtc_read_time(rtc, &tm);
1459 if (rc) {
1460 pr_err("Error reading rtc device (%s) : %d\n",
1461 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1462 goto close_time;
1463 }
1464
1465 rc = rtc_valid_tm(&tm);
1466 if (rc) {
1467 pr_err("Invalid RTC time (%s): %d\n",
1468 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1469 goto close_time;
1470 }
1471 rtc_tm_to_time(&tm, now_tm_sec);
1472
1473close_time:
1474 rtc_class_close(rtc);
1475 return rc;
1476}
1477
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08001478/* Returns estimated battery resistance */
1479static int get_prop_bms_batt_resistance(struct qpnp_bms_chip *chip)
1480{
1481 return chip->rbatt_mohm * 1000;
1482}
1483
1484/* Returns instantaneous current in uA */
1485static int get_prop_bms_current_now(struct qpnp_bms_chip *chip)
1486{
1487 int rc, result_ua;
1488
1489 rc = get_battery_current(chip, &result_ua);
1490 if (rc) {
1491 pr_err("failed to get current: %d\n", rc);
1492 return rc;
1493 }
1494 return result_ua;
1495}
1496
1497/* Returns coulomb counter in uAh */
1498static int get_prop_bms_charge_counter(struct qpnp_bms_chip *chip)
1499{
1500 int64_t cc_raw;
1501
1502 mutex_lock(&chip->bms_output_lock);
1503 lock_output_data(chip);
1504 read_cc_raw(chip, &cc_raw, false);
1505 unlock_output_data(chip);
1506 mutex_unlock(&chip->bms_output_lock);
1507
1508 return calculate_cc(chip, cc_raw, CC, NORESET);
1509}
1510
1511/* Returns shadow coulomb counter in uAh */
1512static int get_prop_bms_charge_counter_shadow(struct qpnp_bms_chip *chip)
1513{
1514 int64_t cc_raw;
1515
1516 mutex_lock(&chip->bms_output_lock);
1517 lock_output_data(chip);
1518 read_cc_raw(chip, &cc_raw, true);
1519 unlock_output_data(chip);
1520 mutex_unlock(&chip->bms_output_lock);
1521
1522 return calculate_cc(chip, cc_raw, SHDW_CC, NORESET);
1523}
1524
1525/* Returns full charge design in uAh */
1526static int get_prop_bms_charge_full_design(struct qpnp_bms_chip *chip)
1527{
1528 return chip->fcc_mah * 1000;
1529}
1530
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05301531/* Returns the current full charge in uAh */
1532static int get_prop_bms_charge_full(struct qpnp_bms_chip *chip)
1533{
1534 int rc;
1535 struct qpnp_vadc_result result;
1536
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001537 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05301538 if (rc) {
1539 pr_err("Unable to read battery temperature\n");
1540 return rc;
1541 }
1542
1543 return calculate_fcc(chip, (int)result.physical);
1544}
1545
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001546static int calculate_delta_time(unsigned long *time_stamp, int *delta_time_s)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001547{
1548 unsigned long now_tm_sec = 0;
1549
1550 /* default to delta time = 0 if anything fails */
1551 *delta_time_s = 0;
1552
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001553 if (get_current_time(&now_tm_sec)) {
1554 pr_err("RTC read failed\n");
1555 return 0;
1556 }
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001557
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001558 *delta_time_s = (now_tm_sec - *time_stamp);
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001559
1560 /* remember this time */
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001561 *time_stamp = now_tm_sec;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001562 return 0;
1563}
1564
Xiaozhe Shie118c692012-09-24 15:17:43 -07001565static void calculate_soc_params(struct qpnp_bms_chip *chip,
1566 struct raw_soc_params *raw,
1567 struct soc_params *params,
1568 int batt_temp)
1569{
Xiaozhe Shi219cb222013-06-10 15:49:59 -07001570 int soc_rbatt, shdw_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001571
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001572 calculate_delta_time(&chip->tm_sec, &params->delta_time_s);
1573 pr_debug("tm_sec = %ld, delta_s = %d\n",
1574 chip->tm_sec, params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001575 params->fcc_uah = calculate_fcc(chip, batt_temp);
1576 pr_debug("FCC = %uuAh batt_temp = %d\n", params->fcc_uah, batt_temp);
1577
1578 /* calculate remainging charge */
1579 params->ocv_charge_uah = calculate_ocv_charge(
1580 chip, raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001581 params->fcc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001582 pr_debug("ocv_charge_uah = %uuAh\n", params->ocv_charge_uah);
1583
1584 /* calculate cc micro_volt_hour */
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001585 params->cc_uah = calculate_cc(chip, raw->cc, CC, RESET);
Xiaozhe Shi219cb222013-06-10 15:49:59 -07001586 shdw_cc_uah = calculate_cc(chip, raw->shdw_cc, SHDW_CC, RESET);
1587 pr_debug("cc_uah = %duAh raw->cc = %llx, shdw_cc_uah = %duAh raw->shdw_cc = %llx\n",
1588 params->cc_uah, raw->cc,
1589 shdw_cc_uah, raw->shdw_cc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001590
1591 soc_rbatt = ((params->ocv_charge_uah - params->cc_uah) * 100)
1592 / params->fcc_uah;
1593 if (soc_rbatt < 0)
1594 soc_rbatt = 0;
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001595 params->rbatt_mohm = get_rbatt(chip, soc_rbatt, batt_temp);
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001596 pr_debug("rbatt_mohm = %d\n", params->rbatt_mohm);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001597
Xiaozhe Shi1e87cda2013-05-17 10:18:56 -07001598 if (params->rbatt_mohm != chip->rbatt_mohm) {
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07001599 chip->rbatt_mohm = params->rbatt_mohm;
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07001600 if (chip->bms_psy_registered)
Xiaozhe Shi1e87cda2013-05-17 10:18:56 -07001601 power_supply_changed(&chip->bms_psy);
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07001602 }
1603
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001604 calculate_iavg(chip, params->cc_uah, &params->iavg_ua,
1605 params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001606
1607 params->uuc_uah = calculate_unusable_charge_uah(chip, params,
1608 batt_temp);
1609 pr_debug("UUC = %uuAh\n", params->uuc_uah);
1610}
1611
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001612static int bound_soc(int soc)
1613{
1614 soc = max(0, soc);
1615 soc = min(100, soc);
1616 return soc;
1617}
1618
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001619#define IBAT_TOL_MASK 0x0F
1620#define OCV_TOL_MASK 0xF0
1621#define IBAT_TOL_DEFAULT 0x03
1622#define IBAT_TOL_NOCHG 0x0F
1623#define OCV_TOL_DEFAULT 0x20
1624#define OCV_TOL_NO_OCV 0x00
1625static int stop_ocv_updates(struct qpnp_bms_chip *chip)
1626{
1627 pr_debug("stopping ocv updates\n");
1628 return qpnp_masked_write(chip, BMS1_TOL_CTL,
1629 OCV_TOL_MASK, OCV_TOL_NO_OCV);
1630}
1631
1632static int reset_bms_for_test(struct qpnp_bms_chip *chip)
1633{
Xiaozhe Shi95da77f2013-02-20 13:40:06 -08001634 int ibat_ua = 0, vbat_uv = 0, rc;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001635 int ocv_est_uv;
1636
1637 if (!chip) {
1638 pr_err("BMS driver has not been initialized yet!\n");
1639 return -EINVAL;
1640 }
1641
1642 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
1643
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07001644 /*
1645 * Don't include rbatt and rbatt_capacitative since we expect this to
1646 * be used with a fake battery which does not have internal resistances
1647 */
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001648 ocv_est_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
1649 pr_debug("forcing ocv to be %d due to bms reset mode\n", ocv_est_uv);
1650 chip->last_ocv_uv = ocv_est_uv;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001651 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001652 chip->last_soc = -EINVAL;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001653 chip->last_soc_invalid = true;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001654 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001655 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001656 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001657 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001658 chip->last_cc_uah = INT_MIN;
1659 stop_ocv_updates(chip);
1660
1661 pr_debug("bms reset to ocv = %duv vbat_ua = %d ibat_ua = %d\n",
1662 chip->last_ocv_uv, vbat_uv, ibat_ua);
1663
1664 return rc;
1665}
1666
1667static int bms_reset_set(const char *val, const struct kernel_param *kp)
1668{
1669 int rc;
1670
1671 rc = param_set_bool(val, kp);
1672 if (rc) {
1673 pr_err("Unable to set bms_reset: %d\n", rc);
1674 return rc;
1675 }
1676
1677 if (*(bool *)kp->arg) {
1678 struct power_supply *bms_psy = power_supply_get_by_name("bms");
1679 struct qpnp_bms_chip *chip = container_of(bms_psy,
1680 struct qpnp_bms_chip, bms_psy);
1681
1682 rc = reset_bms_for_test(chip);
1683 if (rc) {
1684 pr_err("Unable to modify bms_reset: %d\n", rc);
1685 return rc;
1686 }
1687 }
1688 return 0;
1689}
1690
1691static struct kernel_param_ops bms_reset_ops = {
1692 .set = bms_reset_set,
1693 .get = param_get_bool,
1694};
1695
1696module_param_cb(bms_reset, &bms_reset_ops, &bms_reset, 0644);
1697
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001698#define SOC_STORAGE_MASK 0xFE
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001699static void backup_soc_and_iavg(struct qpnp_bms_chip *chip, int batt_temp,
1700 int soc)
1701{
1702 u8 temp;
1703 int rc;
1704 int iavg_ma = chip->prev_uuc_iavg_ma;
1705
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07001706 if (iavg_ma > MIN_IAVG_MA)
1707 temp = (iavg_ma - MIN_IAVG_MA) / IAVG_STEP_SIZE_MA;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001708 else
1709 temp = 0;
1710
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001711 rc = qpnp_write_wrapper(chip, &temp, chip->base + IAVG_STORAGE_REG, 1);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001712
Xiaozhe Shie945a8a2013-11-11 10:20:14 -08001713 /* store an invalid soc if temperature is below 5degC */
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001714 if (batt_temp > IGNORE_SOC_TEMP_DECIDEG)
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001715 qpnp_masked_write_base(chip, chip->soc_storage_addr,
1716 SOC_STORAGE_MASK, (soc + 1) << 1);
Xiaozhe Shie945a8a2013-11-11 10:20:14 -08001717 else
1718 qpnp_masked_write_base(chip, chip->soc_storage_addr,
1719 SOC_STORAGE_MASK, SOC_STORAGE_MASK);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001720}
1721
1722static int scale_soc_while_chg(struct qpnp_bms_chip *chip, int chg_time_sec,
1723 int catch_up_sec, int new_soc, int prev_soc)
1724{
1725 int scaled_soc;
1726 int numerator;
1727
1728 /*
1729 * Don't report a high value immediately slowly scale the
1730 * value from prev_soc to the new soc based on a charge time
1731 * weighted average
1732 */
1733 pr_debug("cts = %d catch_up_sec = %d\n", chg_time_sec, catch_up_sec);
1734 if (catch_up_sec == 0)
1735 return new_soc;
1736
1737 if (chg_time_sec > catch_up_sec)
1738 return new_soc;
1739
1740 numerator = (catch_up_sec - chg_time_sec) * prev_soc
1741 + chg_time_sec * new_soc;
1742 scaled_soc = numerator / catch_up_sec;
1743
1744 pr_debug("cts = %d new_soc = %d prev_soc = %d scaled_soc = %d\n",
1745 chg_time_sec, new_soc, prev_soc, scaled_soc);
1746
1747 return scaled_soc;
1748}
1749
1750/*
1751 * bms_fake_battery is set in setups where a battery emulator is used instead
1752 * of a real battery. This makes the bms driver report a different/fake value
1753 * regardless of the calculated state of charge.
1754 */
1755static int bms_fake_battery = -EINVAL;
1756module_param(bms_fake_battery, int, 0644);
1757
1758static int report_voltage_based_soc(struct qpnp_bms_chip *chip)
1759{
1760 pr_debug("Reported voltage based soc = %d\n",
1761 chip->prev_voltage_based_soc);
1762 return chip->prev_voltage_based_soc;
1763}
1764
1765#define SOC_CATCHUP_SEC_MAX 600
1766#define SOC_CATCHUP_SEC_PER_PERCENT 60
1767#define MAX_CATCHUP_SOC (SOC_CATCHUP_SEC_MAX / SOC_CATCHUP_SEC_PER_PERCENT)
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07001768#define SOC_CHANGE_PER_SEC 5
Xiaozhe Shi27375822013-08-22 11:40:15 -07001769#define REPORT_SOC_WAIT_MS 10000
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001770static int report_cc_based_soc(struct qpnp_bms_chip *chip)
1771{
1772 int soc, soc_change;
1773 int time_since_last_change_sec, charge_time_sec = 0;
1774 unsigned long last_change_sec;
1775 struct timespec now;
1776 struct qpnp_vadc_result result;
1777 int batt_temp;
1778 int rc;
1779 bool charging, charging_since_last_report;
1780
Xiaozhe Shi27375822013-08-22 11:40:15 -07001781 rc = wait_event_interruptible_timeout(chip->bms_wait_queue,
1782 chip->calculated_soc != -EINVAL,
1783 round_jiffies_relative(msecs_to_jiffies
1784 (REPORT_SOC_WAIT_MS)));
1785
1786 if (rc == 0 && chip->calculated_soc == -EINVAL) {
1787 pr_debug("calculate soc timed out\n");
1788 } else if (rc == -ERESTARTSYS) {
1789 pr_err("Wait for SoC interrupted.\n");
1790 return rc;
1791 }
1792
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001793 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001794
1795 if (rc) {
1796 pr_err("error reading adc channel = %d, rc = %d\n",
1797 LR_MUX1_BATT_THERM, rc);
1798 return rc;
1799 }
1800 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
1801 result.measurement);
1802 batt_temp = (int)result.physical;
1803
1804 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shifa6ea692013-05-31 11:15:13 -07001805 soc = chip->calculated_soc;
1806
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001807 last_change_sec = chip->last_soc_change_sec;
1808 calculate_delta_time(&last_change_sec, &time_since_last_change_sec);
1809
1810 charging = is_battery_charging(chip);
1811 charging_since_last_report = charging || (chip->last_soc_unbound
1812 && chip->was_charging_at_sleep);
1813 /*
1814 * account for charge time - limit it to SOC_CATCHUP_SEC to
1815 * avoid overflows when charging continues for extended periods
1816 */
1817 if (charging) {
1818 if (chip->charge_start_tm_sec == 0) {
1819 /*
1820 * calculating soc for the first time
1821 * after start of chg. Initialize catchup time
1822 */
1823 if (abs(soc - chip->last_soc) < MAX_CATCHUP_SOC)
1824 chip->catch_up_time_sec =
1825 (soc - chip->last_soc)
1826 * SOC_CATCHUP_SEC_PER_PERCENT;
1827 else
1828 chip->catch_up_time_sec = SOC_CATCHUP_SEC_MAX;
1829
1830 if (chip->catch_up_time_sec < 0)
1831 chip->catch_up_time_sec = 0;
1832 chip->charge_start_tm_sec = last_change_sec;
1833 }
1834
1835 charge_time_sec = min(SOC_CATCHUP_SEC_MAX, (int)last_change_sec
1836 - chip->charge_start_tm_sec);
1837
1838 /* end catchup if calculated soc and last soc are same */
1839 if (chip->last_soc == soc)
1840 chip->catch_up_time_sec = 0;
1841 }
1842
1843 if (chip->last_soc != -EINVAL) {
1844 /*
1845 * last_soc < soc ... if we have not been charging at all
1846 * since the last time this was called, report previous SoC.
1847 * Otherwise, scale and catch up.
1848 */
1849 if (chip->last_soc < soc && !charging_since_last_report)
1850 soc = chip->last_soc;
1851 else if (chip->last_soc < soc && soc != 100)
1852 soc = scale_soc_while_chg(chip, charge_time_sec,
1853 chip->catch_up_time_sec,
1854 soc, chip->last_soc);
1855
Xiaozhe Shibd56b052013-10-21 11:51:30 -07001856 /* if the battery is close to cutoff allow more change */
1857 if (wake_lock_active(&chip->low_voltage_wake_lock))
1858 soc_change = min((int)abs(chip->last_soc - soc),
1859 time_since_last_change_sec);
1860 else
1861 soc_change = min((int)abs(chip->last_soc - soc),
1862 time_since_last_change_sec
1863 / SOC_CHANGE_PER_SEC);
1864
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001865 if (chip->last_soc_unbound) {
1866 chip->last_soc_unbound = false;
1867 } else {
1868 /*
1869 * if soc have not been unbound by resume,
1870 * only change reported SoC by 1.
1871 */
1872 soc_change = min(1, soc_change);
1873 }
1874
1875 if (soc < chip->last_soc && soc != 0)
1876 soc = chip->last_soc - soc_change;
1877 if (soc > chip->last_soc && soc != 100)
1878 soc = chip->last_soc + soc_change;
1879 }
1880
Xiaozhe Shi208b8e52013-05-28 10:16:32 -07001881 if (chip->last_soc != soc && !chip->last_soc_unbound)
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001882 chip->last_soc_change_sec = last_change_sec;
1883
1884 pr_debug("last_soc = %d, calculated_soc = %d, soc = %d, time since last change = %d\n",
1885 chip->last_soc, chip->calculated_soc,
1886 soc, time_since_last_change_sec);
1887 chip->last_soc = bound_soc(soc);
1888 backup_soc_and_iavg(chip, batt_temp, chip->last_soc);
1889 pr_debug("Reported SOC = %d\n", chip->last_soc);
1890 chip->t_soc_queried = now;
1891 mutex_unlock(&chip->last_soc_mutex);
1892
1893 return soc;
1894}
1895
1896static int report_state_of_charge(struct qpnp_bms_chip *chip)
1897{
1898 if (bms_fake_battery != -EINVAL) {
1899 pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery);
1900 return bms_fake_battery;
1901 } else if (chip->use_voltage_soc)
1902 return report_voltage_based_soc(chip);
1903 else
1904 return report_cc_based_soc(chip);
1905}
1906
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07001907#define VDD_MAX_ERR 5000
1908#define VDD_STEP_SIZE 10000
1909#define MAX_COUNT_BEFORE_RESET_TO_CC 3
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001910static int charging_adjustments(struct qpnp_bms_chip *chip,
1911 struct soc_params *params, int soc,
1912 int vbat_uv, int ibat_ua, int batt_temp)
1913{
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001914 int chg_soc, soc_ibat, batt_terminal_uv, weight_ibat, weight_cc;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001915
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001916 batt_terminal_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001917
1918 if (chip->soc_at_cv == -EINVAL) {
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001919 if (batt_terminal_uv >= chip->max_voltage_uv - VDD_MAX_ERR) {
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001920 chip->soc_at_cv = soc;
1921 chip->prev_chg_soc = soc;
Xiaozhe Shifc7af172013-11-04 14:15:44 -08001922 chip->ibat_at_cv_ua = params->iavg_ua;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001923 pr_debug("CC_TO_CV ibat_ua = %d CHG SOC %d\n",
1924 ibat_ua, soc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001925 } else {
1926 /* In constant current charging return the calc soc */
1927 pr_debug("CC CHG SOC %d\n", soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001928 }
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001929
1930 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07001931 chip->system_load_count = 0;
1932 return soc;
1933 } else if (ibat_ua > 0 && batt_terminal_uv
1934 < chip->max_voltage_uv - (VDD_MAX_ERR * 2)) {
1935 if (chip->system_load_count > MAX_COUNT_BEFORE_RESET_TO_CC) {
1936 chip->soc_at_cv = -EINVAL;
1937 pr_debug("Vbat below CV threshold, resetting CC_TO_CV\n");
1938 chip->system_load_count = 0;
1939 } else {
1940 chip->system_load_count += 1;
1941 pr_debug("Vbat below CV threshold, count: %d\n",
1942 chip->system_load_count);
1943 }
1944 return soc;
1945 } else if (ibat_ua > 0) {
1946 pr_debug("NOT CHARGING SOC %d\n", soc);
1947 chip->system_load_count = 0;
1948 chip->prev_chg_soc = soc;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001949 return soc;
1950 }
1951
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07001952 chip->system_load_count = 0;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001953 /*
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001954 * battery is in CV phase - begin linear interpolation of soc based on
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001955 * battery charge current
1956 */
1957
1958 /*
1959 * if voltage lessened (possibly because of a system load)
1960 * keep reporting the prev chg soc
1961 */
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001962 if (batt_terminal_uv <= chip->prev_batt_terminal_uv - VDD_STEP_SIZE) {
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08001963 pr_debug("batt_terminal_uv %d < (max = %d - 10000); CC CHG SOC %d\n",
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001964 batt_terminal_uv, chip->prev_batt_terminal_uv,
1965 chip->prev_chg_soc);
1966 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001967 return chip->prev_chg_soc;
1968 }
1969
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001970 soc_ibat = bound_soc(linear_interpolate(chip->soc_at_cv,
1971 chip->ibat_at_cv_ua,
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08001972 100, -1 * chip->chg_term_ua,
Xiaozhe Shifc7af172013-11-04 14:15:44 -08001973 params->iavg_ua));
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001974 weight_ibat = bound_soc(linear_interpolate(1, chip->soc_at_cv,
1975 100, 100, chip->prev_chg_soc));
1976 weight_cc = 100 - weight_ibat;
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001977 chg_soc = bound_soc(DIV_ROUND_CLOSEST(soc_ibat * weight_ibat
1978 + weight_cc * soc, 100));
1979
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001980 pr_debug("weight_ibat = %d, weight_cc = %d, soc_ibat = %d, soc_cc = %d\n",
1981 weight_ibat, weight_cc, soc_ibat, soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001982
1983 /* always report a higher soc */
1984 if (chg_soc > chip->prev_chg_soc) {
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001985 chip->prev_chg_soc = chg_soc;
1986
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001987 chip->charging_adjusted_ocv = find_ocv_for_pc(chip, batt_temp,
1988 find_pc_for_soc(chip, params, chg_soc));
1989 pr_debug("CC CHG ADJ OCV = %d CHG SOC %d\n",
1990 chip->charging_adjusted_ocv,
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001991 chip->prev_chg_soc);
1992 }
1993
1994 pr_debug("Reporting CHG SOC %d\n", chip->prev_chg_soc);
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001995 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001996 return chip->prev_chg_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001997}
1998
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001999static void very_low_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
2000{
2001 /*
2002 * if battery is very low (v_cutoff voltage + 20mv) hold
2003 * a wakelock untill soc = 0%
2004 */
2005 if (vbat_uv <= chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002006 && !wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002007 pr_debug("voltage = %d low holding wakelock\n", vbat_uv);
2008 wake_lock(&chip->low_voltage_wake_lock);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002009 } else if (vbat_uv > chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002010 && wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002011 pr_debug("voltage = %d releasing wakelock\n", vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002012 wake_unlock(&chip->low_voltage_wake_lock);
2013 }
2014}
2015
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002016#define VBATT_ERROR_MARGIN 20000
2017static void cv_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
2018{
2019 /*
2020 * if battery is very low (v_cutoff voltage + 20mv) hold
2021 * a wakelock untill soc = 0%
2022 */
2023 if (wake_lock_active(&chip->cv_wake_lock)) {
2024 if (chip->soc_at_cv != -EINVAL) {
2025 pr_debug("hit CV, releasing cv wakelock\n");
2026 wake_unlock(&chip->cv_wake_lock);
2027 } else if (!is_battery_charging(chip)) {
2028 pr_debug("charging stopped, releasing cv wakelock\n");
2029 wake_unlock(&chip->cv_wake_lock);
2030 }
2031 } else if (vbat_uv > chip->max_voltage_uv - VBATT_ERROR_MARGIN
2032 && chip->soc_at_cv == -EINVAL
2033 && is_battery_charging(chip)
2034 && !wake_lock_active(&chip->cv_wake_lock)) {
2035 pr_debug("voltage = %d holding cv wakelock\n", vbat_uv);
2036 wake_lock(&chip->cv_wake_lock);
2037 }
2038}
2039
Xiaozhe Shi2b647872013-10-31 14:30:27 -07002040#define NO_ADJUST_HIGH_SOC_THRESHOLD 98
Xiaozhe Shie118c692012-09-24 15:17:43 -07002041static int adjust_soc(struct qpnp_bms_chip *chip, struct soc_params *params,
2042 int soc, int batt_temp)
2043{
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002044 int ibat_ua = 0, vbat_uv = 0;
2045 int ocv_est_uv = 0, soc_est = 0, pc_est = 0, pc = 0;
2046 int delta_ocv_uv = 0;
2047 int n = 0;
2048 int rc_new_uah = 0;
2049 int pc_new = 0;
2050 int soc_new = 0;
2051 int slope = 0;
2052 int rc = 0;
2053 int delta_ocv_uv_limit = 0;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002054 int correction_limit_uv = 0;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002055
2056 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
2057 if (rc < 0) {
2058 pr_err("simultaneous vbat ibat failed err = %d\n", rc);
2059 goto out;
2060 }
2061
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002062 very_low_voltage_check(chip, vbat_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002063 cv_voltage_check(chip, vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002064
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002065 delta_ocv_uv_limit = DIV_ROUND_CLOSEST(ibat_ua, 1000);
2066
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08002067 ocv_est_uv = vbat_uv + (ibat_ua * params->rbatt_mohm)/1000;
2068
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002069 pc_est = calculate_pc(chip, ocv_est_uv, batt_temp);
2070 soc_est = div_s64((s64)params->fcc_uah * pc_est - params->uuc_uah*100,
2071 (s64)params->fcc_uah - params->uuc_uah);
2072 soc_est = bound_soc(soc_est);
2073
Xiaozhe Shi20640b52013-01-03 11:49:30 -08002074 /* never adjust during bms reset mode */
2075 if (bms_reset) {
2076 pr_debug("bms reset mode, SOC adjustment skipped\n");
2077 goto out;
2078 }
2079
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07002080 if (is_battery_charging(chip)) {
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002081 soc = charging_adjustments(chip, params, soc, vbat_uv, ibat_ua,
2082 batt_temp);
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07002083 /* Skip adjustments if we are in CV or ibat is negative */
2084 if (chip->soc_at_cv != -EINVAL || ibat_ua < 0)
2085 goto out;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002086 }
2087
2088 /*
2089 * do not adjust
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07002090 * if soc_est is same as what bms calculated
2091 * OR if soc_est > adjust_soc_low_threshold
2092 * OR if soc is above 90
2093 * because we might pull it low
2094 * and cause a bad user experience
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002095 */
Xiaozhe Shibd56b052013-10-21 11:51:30 -07002096 if (!wake_lock_active(&chip->low_voltage_wake_lock) &&
2097 (soc_est == soc
2098 || soc_est > chip->adjust_soc_low_threshold
2099 || soc >= NO_ADJUST_HIGH_SOC_THRESHOLD))
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002100 goto out;
2101
2102 if (chip->last_soc_est == -EINVAL)
2103 chip->last_soc_est = soc;
2104
2105 n = min(200, max(1 , soc + soc_est + chip->last_soc_est));
2106 chip->last_soc_est = soc_est;
2107
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002108 pc = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002109 if (pc > 0) {
2110 pc_new = calculate_pc(chip,
2111 chip->last_ocv_uv - (++slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002112 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002113 while (pc_new == pc) {
2114 /* start taking 10mV steps */
2115 slope = slope + 10;
2116 pc_new = calculate_pc(chip,
2117 chip->last_ocv_uv - (slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002118 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002119 }
2120 } else {
2121 /*
2122 * pc is already at the lowest point,
2123 * assume 1 millivolt translates to 1% pc
2124 */
2125 pc = 1;
2126 pc_new = 0;
2127 slope = 1;
2128 }
2129
2130 delta_ocv_uv = div_s64((soc - soc_est) * (s64)slope * 1000,
2131 n * (pc - pc_new));
2132
2133 if (abs(delta_ocv_uv) > delta_ocv_uv_limit) {
2134 pr_debug("limiting delta ocv %d limit = %d\n", delta_ocv_uv,
2135 delta_ocv_uv_limit);
2136
2137 if (delta_ocv_uv > 0)
2138 delta_ocv_uv = delta_ocv_uv_limit;
2139 else
2140 delta_ocv_uv = -1 * delta_ocv_uv_limit;
2141 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
2142 }
2143
Xiaozhe Shibd56b052013-10-21 11:51:30 -07002144 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2145 /* when in the cutoff region, do not correct upwards */
2146 delta_ocv_uv = max(0, delta_ocv_uv);
Xiaozhe Shi08d9aa72013-07-31 17:15:12 -07002147 goto skip_limits;
Xiaozhe Shibd56b052013-10-21 11:51:30 -07002148 }
Xiaozhe Shi08d9aa72013-07-31 17:15:12 -07002149
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002150 if (chip->last_ocv_uv > chip->flat_ocv_threshold_uv)
2151 correction_limit_uv = chip->high_ocv_correction_limit_uv;
2152 else
2153 correction_limit_uv = chip->low_ocv_correction_limit_uv;
2154
2155 if (abs(delta_ocv_uv) > correction_limit_uv) {
2156 pr_debug("limiting delta ocv %d limit = %d\n",
2157 delta_ocv_uv, correction_limit_uv);
2158 if (delta_ocv_uv > 0)
2159 delta_ocv_uv = correction_limit_uv;
2160 else
2161 delta_ocv_uv = -correction_limit_uv;
2162 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
2163 }
2164
Xiaozhe Shi08d9aa72013-07-31 17:15:12 -07002165skip_limits:
2166
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002167 chip->last_ocv_uv -= delta_ocv_uv;
2168
2169 if (chip->last_ocv_uv >= chip->max_voltage_uv)
2170 chip->last_ocv_uv = chip->max_voltage_uv;
2171
2172 /* calculate the soc based on this new ocv */
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002173 pc_new = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002174 rc_new_uah = (params->fcc_uah * pc_new) / 100;
2175 soc_new = (rc_new_uah - params->cc_uah - params->uuc_uah)*100
2176 / (params->fcc_uah - params->uuc_uah);
2177 soc_new = bound_soc(soc_new);
2178
2179 /*
2180 * if soc_new is ZERO force it higher so that phone doesnt report soc=0
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002181 * soc = 0 should happen only when soc_est is above a set value
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002182 */
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002183 if (soc_new == 0 && soc_est >= chip->hold_soc_est)
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002184 soc_new = 1;
2185
2186 soc = soc_new;
2187
2188out:
2189 pr_debug("ibat_ua = %d, vbat_uv = %d, ocv_est_uv = %d, pc_est = %d, soc_est = %d, n = %d, delta_ocv_uv = %d, last_ocv_uv = %d, pc_new = %d, soc_new = %d, rbatt = %d, slope = %d\n",
2190 ibat_ua, vbat_uv, ocv_est_uv, pc_est,
2191 soc_est, n, delta_ocv_uv, chip->last_ocv_uv,
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08002192 pc_new, soc_new, params->rbatt_mohm, slope);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002193
Xiaozhe Shie118c692012-09-24 15:17:43 -07002194 return soc;
2195}
2196
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002197static int clamp_soc_based_on_voltage(struct qpnp_bms_chip *chip, int soc)
2198{
2199 int rc, vbat_uv;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002200
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002201 rc = get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shi36458962013-02-06 16:19:57 -08002202 if (rc < 0) {
2203 pr_err("adc vbat failed err = %d\n", rc);
2204 return soc;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002205 }
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002206 if (soc == 0 && vbat_uv > chip->v_cutoff_uv) {
2207 pr_debug("clamping soc to 1, vbat (%d) > cutoff (%d)\n",
2208 vbat_uv, chip->v_cutoff_uv);
2209 return 1;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002210 } else {
2211 pr_debug("not clamping, using soc = %d, vbat = %d and cutoff = %d\n",
2212 soc, vbat_uv, chip->v_cutoff_uv);
2213 return soc;
2214 }
2215}
2216
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002217static int64_t convert_cc_uah_to_raw(struct qpnp_bms_chip *chip, int64_t cc_uah)
2218{
2219 int64_t cc_uv, cc_pvh, cc_raw;
2220
2221 cc_pvh = cc_uah * chip->r_sense_uohm;
2222 cc_uv = div_s64(cc_pvh * SLEEP_CLK_HZ * SECONDS_PER_HOUR,
2223 CC_READING_TICKS * 1000000LL);
2224 cc_raw = div_s64(cc_uv * CC_READING_RESOLUTION_D,
2225 CC_READING_RESOLUTION_N);
2226 return cc_raw;
2227}
2228
2229#define CC_STEP_INCREMENT_UAH 1500
2230#define OCV_STEP_INCREMENT 0x10
2231static void configure_soc_wakeup(struct qpnp_bms_chip *chip,
2232 struct soc_params *params,
2233 int batt_temp, int target_soc)
2234{
2235 int target_ocv_uv;
2236 int64_t target_cc_uah, cc_raw_64, current_shdw_cc_raw_64;
2237 int64_t current_shdw_cc_uah, iadc_comp_factor;
2238 uint64_t cc_raw, current_shdw_cc_raw;
2239 int16_t ocv_raw, current_ocv_raw;
2240
2241 current_shdw_cc_raw = 0;
2242 mutex_lock(&chip->bms_output_lock);
2243 lock_output_data(chip);
2244 qpnp_read_wrapper(chip, (u8 *)&current_ocv_raw,
2245 chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
2246 unlock_output_data(chip);
2247 mutex_unlock(&chip->bms_output_lock);
2248 current_shdw_cc_uah = get_prop_bms_charge_counter_shadow(chip);
2249 current_shdw_cc_raw_64 = convert_cc_uah_to_raw(chip,
2250 current_shdw_cc_uah);
2251
2252 /*
2253 * Calculate the target shadow coulomb counter threshold for when
2254 * the SoC changes.
2255 *
2256 * Since the BMS driver resets the shadow coulomb counter every
2257 * 20 seconds when the device is awake, calculate the threshold as
2258 * a delta from the current shadow coulomb count.
2259 */
2260 target_cc_uah = (100 - target_soc)
2261 * (params->fcc_uah - params->uuc_uah)
2262 / 100 - current_shdw_cc_uah;
2263 if (target_cc_uah < 0) {
2264 /*
2265 * If the target cc is below 0, that means we have already
2266 * passed the point where SoC should have fallen.
2267 * Set a wakeup in a few more mAh and check back again
2268 */
2269 target_cc_uah = CC_STEP_INCREMENT_UAH;
2270 }
2271 iadc_comp_factor = 100000;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002272 qpnp_iadc_comp_result(chip->iadc_dev, &iadc_comp_factor);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002273 target_cc_uah = div64_s64(target_cc_uah * 100000, iadc_comp_factor);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002274 target_cc_uah = cc_reverse_adjust_for_gain(chip, target_cc_uah);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002275 cc_raw_64 = convert_cc_uah_to_raw(chip, target_cc_uah);
2276 cc_raw = convert_s64_to_s36(cc_raw_64);
2277
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002278 target_ocv_uv = find_ocv_for_pc(chip, batt_temp,
2279 find_pc_for_soc(chip, params, target_soc));
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002280 ocv_raw = convert_vbatt_uv_to_raw(chip, target_ocv_uv);
2281
2282 /*
2283 * If the current_ocv_raw was updated since reaching 100% and is lower
2284 * than the calculated target ocv threshold, set the new target
2285 * threshold 1.5mAh lower in order to check if the SoC changed yet.
2286 */
2287 if (current_ocv_raw != chip->ocv_reading_at_100
2288 && current_ocv_raw < ocv_raw)
2289 ocv_raw = current_ocv_raw - OCV_STEP_INCREMENT;
2290
2291 qpnp_write_wrapper(chip, (u8 *)&cc_raw,
2292 chip->base + BMS1_SW_CC_THR0, 5);
2293 qpnp_write_wrapper(chip, (u8 *)&ocv_raw,
2294 chip->base + BMS1_OCV_THR0, 2);
2295
Xiaozhe Shif5d87832013-11-21 17:05:39 -08002296 enable_bms_irq(&chip->ocv_thr_irq);
2297 enable_bms_irq(&chip->sw_cc_thr_irq);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002298 pr_debug("current sw_cc_raw = 0x%llx, current ocv = 0x%hx\n",
2299 current_shdw_cc_raw, (uint16_t)current_ocv_raw);
2300 pr_debug("target_cc_uah = %lld, raw64 = 0x%llx, raw 36 = 0x%llx, ocv_raw = 0x%hx\n",
2301 target_cc_uah,
2302 (uint64_t)cc_raw_64, cc_raw,
2303 (uint16_t)ocv_raw);
2304}
2305
Xiaozhe Shi5e791032013-10-25 11:30:42 -07002306#define BAD_SOC_THRESH -10
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002307static int calculate_raw_soc(struct qpnp_bms_chip *chip,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002308 struct raw_soc_params *raw,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002309 struct soc_params *params,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002310 int batt_temp)
2311{
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002312 int soc, remaining_usable_charge_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002313
Xiaozhe Shie118c692012-09-24 15:17:43 -07002314 /* calculate remaining usable charge */
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002315 remaining_usable_charge_uah = params->ocv_charge_uah
2316 - params->cc_uah
2317 - params->uuc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002318 pr_debug("RUC = %duAh\n", remaining_usable_charge_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002319
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002320 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002321 (params->fcc_uah - params->uuc_uah));
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002322
Xiaozhe Shi5e791032013-10-25 11:30:42 -07002323 if (chip->first_time_calc_soc && soc > BAD_SOC_THRESH && soc < 0) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07002324 /*
2325 * first time calcualtion and the pon ocv is too low resulting
2326 * in a bad soc. Adjust ocv to get 0 soc
2327 */
2328 pr_debug("soc is %d, adjusting pon ocv to make it 0\n", soc);
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002329 chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp,
2330 find_pc_for_soc(chip, params, 0));
2331 params->ocv_charge_uah = find_ocv_charge_for_soc(chip,
2332 params, 0);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002333
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002334 remaining_usable_charge_uah = params->ocv_charge_uah
2335 - params->cc_uah
2336 - params->uuc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002337
2338 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002339 (params->fcc_uah
2340 - params->uuc_uah));
Xiaozhe Shie118c692012-09-24 15:17:43 -07002341 pr_debug("DONE for O soc is %d, pon ocv adjusted to %duV\n",
2342 soc, chip->last_ocv_uv);
2343 }
2344
2345 if (soc > 100)
2346 soc = 100;
2347
Xiaozhe Shi5e791032013-10-25 11:30:42 -07002348 if (soc > BAD_SOC_THRESH && soc < 0) {
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002349 pr_debug("bad rem_usb_chg = %d rem_chg %d, cc_uah %d, unusb_chg %d\n",
Xiaozhe Shie118c692012-09-24 15:17:43 -07002350 remaining_usable_charge_uah,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002351 params->ocv_charge_uah,
2352 params->cc_uah, params->uuc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002353
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002354 pr_debug("for bad rem_usb_chg last_ocv_uv = %d batt_temp = %d fcc = %d soc =%d\n",
Xiaozhe Shie118c692012-09-24 15:17:43 -07002355 chip->last_ocv_uv, batt_temp,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002356 params->fcc_uah, soc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002357 soc = 0;
2358 }
2359
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002360 return soc;
2361}
2362
2363#define SLEEP_RECALC_INTERVAL 3
2364static int calculate_state_of_charge(struct qpnp_bms_chip *chip,
2365 struct raw_soc_params *raw,
2366 int batt_temp)
2367{
2368 struct soc_params params;
2369 int soc, previous_soc, shutdown_soc, new_calculated_soc;
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002370 int remaining_usable_charge_uah;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002371
2372 calculate_soc_params(chip, raw, &params, batt_temp);
2373 if (!is_battery_present(chip)) {
2374 pr_debug("battery gone, reporting 100\n");
2375 new_calculated_soc = 100;
2376 goto done_calculating;
2377 }
2378
2379 if (params.fcc_uah - params.uuc_uah <= 0) {
2380 pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
2381 params.fcc_uah,
2382 params.uuc_uah);
2383 new_calculated_soc = 0;
2384 goto done_calculating;
2385 }
2386
2387 soc = calculate_raw_soc(chip, raw, &params, batt_temp);
2388
Xiaozhe Shie118c692012-09-24 15:17:43 -07002389 mutex_lock(&chip->soc_invalidation_mutex);
2390 shutdown_soc = chip->shutdown_soc;
2391
2392 if (chip->first_time_calc_soc && soc != shutdown_soc
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002393 && !chip->shutdown_soc_invalid) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07002394 /*
2395 * soc for the first time - use shutdown soc
2396 * to adjust pon ocv since it is a small percent away from
2397 * the real soc
2398 */
2399 pr_debug("soc = %d before forcing shutdown_soc = %d\n",
2400 soc, shutdown_soc);
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002401 chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp,
2402 find_pc_for_soc(chip, &params, shutdown_soc));
2403 params.ocv_charge_uah = find_ocv_charge_for_soc(chip,
2404 &params, shutdown_soc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002405
2406 remaining_usable_charge_uah = params.ocv_charge_uah
2407 - params.cc_uah
2408 - params.uuc_uah;
2409
2410 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2411 (params.fcc_uah
2412 - params.uuc_uah));
2413
2414 pr_debug("DONE for shutdown_soc = %d soc is %d, adjusted ocv to %duV\n",
2415 shutdown_soc, soc, chip->last_ocv_uv);
2416 }
2417 mutex_unlock(&chip->soc_invalidation_mutex);
2418
2419 pr_debug("SOC before adjustment = %d\n", soc);
2420 new_calculated_soc = adjust_soc(chip, &params, soc, batt_temp);
2421
Xiaozhe Shi445d2492013-03-27 18:10:18 -07002422 /* always clamp soc due to BMS hw/sw immaturities */
2423 new_calculated_soc = clamp_soc_based_on_voltage(chip,
2424 new_calculated_soc);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002425 /*
2426 * If the battery is full, configure the cc threshold so the system
2427 * wakes up after SoC changes
2428 */
Xiaozhe Shif5d87832013-11-21 17:05:39 -08002429 if (is_battery_full(chip)) {
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002430 configure_soc_wakeup(chip, &params,
2431 batt_temp, bound_soc(new_calculated_soc - 1));
Xiaozhe Shif5d87832013-11-21 17:05:39 -08002432 } else {
2433 disable_bms_irq(&chip->ocv_thr_irq);
2434 disable_bms_irq(&chip->sw_cc_thr_irq);
2435 }
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002436done_calculating:
Xiaozhe Shifa6ea692013-05-31 11:15:13 -07002437 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07002438 previous_soc = chip->calculated_soc;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002439 chip->calculated_soc = new_calculated_soc;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002440 pr_debug("CC based calculated SOC = %d\n", chip->calculated_soc);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002441 if (chip->last_soc_invalid) {
2442 chip->last_soc_invalid = false;
2443 chip->last_soc = -EINVAL;
2444 }
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002445 /*
2446 * Check if more than a long time has passed since the last
2447 * calculation (more than n times compared to the soc recalculation
2448 * rate, where n is defined by SLEEP_RECALC_INTERVAL). If this is true,
2449 * then the system must have gone through a long sleep, and SoC can be
2450 * allowed to become unbounded by the last reported SoC
2451 */
2452 if (params.delta_time_s * 1000 >
2453 chip->calculate_soc_ms * SLEEP_RECALC_INTERVAL
2454 && !chip->first_time_calc_soc) {
2455 chip->last_soc_unbound = true;
2456 chip->last_soc_change_sec = chip->last_recalc_time;
2457 pr_debug("last_soc unbound because elapsed time = %d\n",
2458 params.delta_time_s);
2459 }
2460 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi27375822013-08-22 11:40:15 -07002461 wake_up_interruptible(&chip->bms_wait_queue);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07002462
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002463 if (new_calculated_soc != previous_soc && chip->bms_psy_registered) {
Xiaozhe Shi83484e32013-05-16 10:59:59 -07002464 power_supply_changed(&chip->bms_psy);
2465 pr_debug("power supply changed\n");
2466 } else {
2467 /*
2468 * Call report state of charge anyways to periodically update
2469 * reported SoC. This prevents reported SoC from being stuck
2470 * when calculated soc doesn't change.
2471 */
2472 report_state_of_charge(chip);
2473 }
2474
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002475 get_current_time(&chip->last_recalc_time);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002476 chip->first_time_calc_soc = 0;
Xiaozhe Shi70633922013-09-23 15:50:53 -07002477 chip->first_time_calc_uuc = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002478 return chip->calculated_soc;
2479}
2480
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002481static int calculate_soc_from_voltage(struct qpnp_bms_chip *chip)
2482{
2483 int voltage_range_uv, voltage_remaining_uv, voltage_based_soc;
Xiaozhe Shi36458962013-02-06 16:19:57 -08002484 int rc, vbat_uv;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002485
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002486 rc = get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shi36458962013-02-06 16:19:57 -08002487 if (rc < 0) {
2488 pr_err("adc vbat failed err = %d\n", rc);
2489 return rc;
2490 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002491 voltage_range_uv = chip->max_voltage_uv - chip->v_cutoff_uv;
2492 voltage_remaining_uv = vbat_uv - chip->v_cutoff_uv;
2493 voltage_based_soc = voltage_remaining_uv * 100 / voltage_range_uv;
2494
2495 voltage_based_soc = clamp(voltage_based_soc, 0, 100);
2496
2497 if (chip->prev_voltage_based_soc != voltage_based_soc
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002498 && chip->bms_psy_registered) {
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002499 power_supply_changed(&chip->bms_psy);
2500 pr_debug("power supply changed\n");
2501 }
2502 chip->prev_voltage_based_soc = voltage_based_soc;
2503
2504 pr_debug("vbat used = %duv\n", vbat_uv);
2505 pr_debug("Calculated voltage based soc = %d\n", voltage_based_soc);
2506 return voltage_based_soc;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08002507}
2508
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002509static int recalculate_raw_soc(struct qpnp_bms_chip *chip)
2510{
2511 int batt_temp, rc, soc;
2512 struct qpnp_vadc_result result;
2513 struct raw_soc_params raw;
2514 struct soc_params params;
2515
2516 bms_stay_awake(&chip->soc_wake_source);
2517 if (chip->use_voltage_soc) {
2518 soc = calculate_soc_from_voltage(chip);
2519 } else {
2520 if (!chip->batfet_closed)
Xiaozhe Shiffb7cfc2014-01-03 10:47:36 -08002521 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002522 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM,
2523 &result);
2524 if (rc) {
2525 pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n",
2526 LR_MUX1_BATT_THERM, rc);
2527 soc = chip->calculated_soc;
2528 } else {
2529 pr_debug("batt_temp phy = %lld meas = 0x%llx\n",
2530 result.physical,
2531 result.measurement);
2532 batt_temp = (int)result.physical;
2533
2534 mutex_lock(&chip->last_ocv_uv_mutex);
2535 read_soc_params_raw(chip, &raw, batt_temp);
2536 calculate_soc_params(chip, &raw, &params, batt_temp);
2537 if (!is_battery_present(chip)) {
2538 pr_debug("battery gone\n");
2539 soc = 0;
2540 } else if (params.fcc_uah - params.uuc_uah <= 0) {
2541 pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
2542 params.fcc_uah,
2543 params.uuc_uah);
2544 soc = 0;
2545 } else {
2546 soc = calculate_raw_soc(chip, &raw,
2547 &params, batt_temp);
2548 }
2549 mutex_unlock(&chip->last_ocv_uv_mutex);
2550 }
2551 }
2552 bms_relax(&chip->soc_wake_source);
2553 return soc;
2554}
2555
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002556static int recalculate_soc(struct qpnp_bms_chip *chip)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002557{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002558 int batt_temp, rc, soc;
2559 struct qpnp_vadc_result result;
2560 struct raw_soc_params raw;
2561
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002562 bms_stay_awake(&chip->soc_wake_source);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002563 mutex_lock(&chip->vbat_monitor_mutex);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002564 if (chip->vbat_monitor_params.state_request !=
2565 ADC_TM_HIGH_LOW_THR_DISABLE)
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002566 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2567 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002568 mutex_unlock(&chip->vbat_monitor_mutex);
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002569 if (chip->use_voltage_soc) {
2570 soc = calculate_soc_from_voltage(chip);
2571 } else {
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07002572 if (!chip->batfet_closed)
Xiaozhe Shiffb7cfc2014-01-03 10:47:36 -08002573 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002574 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM,
2575 &result);
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002576 if (rc) {
2577 pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n",
2578 LR_MUX1_BATT_THERM, rc);
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002579 soc = chip->calculated_soc;
2580 } else {
2581 pr_debug("batt_temp phy = %lld meas = 0x%llx\n",
2582 result.physical,
2583 result.measurement);
2584 batt_temp = (int)result.physical;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002585
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002586 mutex_lock(&chip->last_ocv_uv_mutex);
2587 read_soc_params_raw(chip, &raw, batt_temp);
2588 soc = calculate_state_of_charge(chip, &raw, batt_temp);
2589 mutex_unlock(&chip->last_ocv_uv_mutex);
2590 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002591 }
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002592 bms_relax(&chip->soc_wake_source);
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002593 return soc;
2594}
2595
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002596static void recalculate_work(struct work_struct *work)
2597{
2598 struct qpnp_bms_chip *chip = container_of(work,
2599 struct qpnp_bms_chip,
2600 recalc_work);
2601
2602 recalculate_soc(chip);
2603}
2604
Xiaozhe Shicb487b12013-10-14 17:42:07 -07002605static int get_calculation_delay_ms(struct qpnp_bms_chip *chip)
2606{
2607 if (wake_lock_active(&chip->low_voltage_wake_lock))
2608 return chip->low_voltage_calculate_soc_ms;
2609 else if (chip->calculated_soc < chip->low_soc_calc_threshold)
2610 return chip->low_soc_calculate_soc_ms;
2611 else
2612 return chip->calculate_soc_ms;
2613}
2614
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002615static void calculate_soc_work(struct work_struct *work)
2616{
2617 struct qpnp_bms_chip *chip = container_of(work,
2618 struct qpnp_bms_chip,
2619 calculate_soc_delayed_work.work);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002620
Xiaozhe Shicb487b12013-10-14 17:42:07 -07002621 recalculate_soc(chip);
2622 schedule_delayed_work(&chip->calculate_soc_delayed_work,
2623 round_jiffies_relative(msecs_to_jiffies
2624 (get_calculation_delay_ms(chip))));
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002625}
2626
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002627static void configure_vbat_monitor_low(struct qpnp_bms_chip *chip)
2628{
2629 mutex_lock(&chip->vbat_monitor_mutex);
2630 if (chip->vbat_monitor_params.state_request
2631 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2632 /*
2633 * Battery is now around or below v_cutoff
2634 */
2635 pr_debug("battery entered cutoff range\n");
2636 if (!wake_lock_active(&chip->low_voltage_wake_lock)) {
2637 pr_debug("voltage low, holding wakelock\n");
2638 wake_lock(&chip->low_voltage_wake_lock);
2639 cancel_delayed_work_sync(
2640 &chip->calculate_soc_delayed_work);
2641 schedule_delayed_work(
2642 &chip->calculate_soc_delayed_work, 0);
2643 }
2644 chip->vbat_monitor_params.state_request =
2645 ADC_TM_HIGH_THR_ENABLE;
2646 chip->vbat_monitor_params.high_thr =
2647 (chip->low_voltage_threshold + VBATT_ERROR_MARGIN);
2648 pr_debug("set low thr to %d and high to %d\n",
2649 chip->vbat_monitor_params.low_thr,
2650 chip->vbat_monitor_params.high_thr);
2651 chip->vbat_monitor_params.low_thr = 0;
2652 } else if (chip->vbat_monitor_params.state_request
2653 == ADC_TM_LOW_THR_ENABLE) {
2654 /*
2655 * Battery is in normal operation range.
2656 */
2657 pr_debug("battery entered normal range\n");
2658 if (wake_lock_active(&chip->cv_wake_lock)) {
2659 wake_unlock(&chip->cv_wake_lock);
2660 pr_debug("releasing cv wake lock\n");
2661 }
2662 chip->in_cv_range = false;
2663 chip->vbat_monitor_params.state_request =
2664 ADC_TM_HIGH_LOW_THR_ENABLE;
2665 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2666 - VBATT_ERROR_MARGIN;
2667 chip->vbat_monitor_params.low_thr =
2668 chip->low_voltage_threshold;
2669 pr_debug("set low thr to %d and high to %d\n",
2670 chip->vbat_monitor_params.low_thr,
2671 chip->vbat_monitor_params.high_thr);
2672 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002673 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2674 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002675 mutex_unlock(&chip->vbat_monitor_mutex);
2676}
2677
2678#define CV_LOW_THRESHOLD_HYST_UV 100000
2679static void configure_vbat_monitor_high(struct qpnp_bms_chip *chip)
2680{
2681 mutex_lock(&chip->vbat_monitor_mutex);
2682 if (chip->vbat_monitor_params.state_request
2683 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2684 /*
2685 * Battery is around vddmax
2686 */
2687 pr_debug("battery entered vddmax range\n");
2688 chip->in_cv_range = true;
2689 if (!wake_lock_active(&chip->cv_wake_lock)) {
2690 wake_lock(&chip->cv_wake_lock);
2691 pr_debug("holding cv wake lock\n");
2692 }
2693 schedule_work(&chip->recalc_work);
2694 chip->vbat_monitor_params.state_request =
2695 ADC_TM_LOW_THR_ENABLE;
2696 chip->vbat_monitor_params.low_thr =
2697 (chip->max_voltage_uv - CV_LOW_THRESHOLD_HYST_UV);
2698 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv * 2;
2699 pr_debug("set low thr to %d and high to %d\n",
2700 chip->vbat_monitor_params.low_thr,
2701 chip->vbat_monitor_params.high_thr);
2702 } else if (chip->vbat_monitor_params.state_request
2703 == ADC_TM_HIGH_THR_ENABLE) {
2704 /*
2705 * Battery is in normal operation range.
2706 */
2707 pr_debug("battery entered normal range\n");
2708 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2709 pr_debug("voltage high, releasing wakelock\n");
2710 wake_unlock(&chip->low_voltage_wake_lock);
2711 }
2712 chip->vbat_monitor_params.state_request =
2713 ADC_TM_HIGH_LOW_THR_ENABLE;
2714 chip->vbat_monitor_params.high_thr =
2715 chip->max_voltage_uv - VBATT_ERROR_MARGIN;
2716 chip->vbat_monitor_params.low_thr =
2717 chip->low_voltage_threshold;
2718 pr_debug("set low thr to %d and high to %d\n",
2719 chip->vbat_monitor_params.low_thr,
2720 chip->vbat_monitor_params.high_thr);
2721 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002722 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2723 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002724 mutex_unlock(&chip->vbat_monitor_mutex);
2725}
2726
2727static void btm_notify_vbat(enum qpnp_tm_state state, void *ctx)
2728{
2729 struct qpnp_bms_chip *chip = ctx;
2730 int vbat_uv;
2731 struct qpnp_vadc_result result;
2732 int rc;
2733
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002734 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &result);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002735 pr_debug("vbat = %lld, raw = 0x%x\n", result.physical, result.adc_code);
2736
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002737 get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002738 pr_debug("vbat is at %d, state is at %d\n", vbat_uv, state);
2739
2740 if (state == ADC_TM_LOW_STATE) {
2741 pr_debug("low voltage btm notification triggered\n");
2742 if (vbat_uv - VBATT_ERROR_MARGIN
2743 < chip->vbat_monitor_params.low_thr) {
2744 configure_vbat_monitor_low(chip);
2745 } else {
2746 pr_debug("faulty btm trigger, discarding\n");
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002747 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002748 &chip->vbat_monitor_params);
2749 }
2750 } else if (state == ADC_TM_HIGH_STATE) {
2751 pr_debug("high voltage btm notification triggered\n");
2752 if (vbat_uv + VBATT_ERROR_MARGIN
2753 > chip->vbat_monitor_params.high_thr) {
2754 configure_vbat_monitor_high(chip);
2755 } else {
2756 pr_debug("faulty btm trigger, discarding\n");
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002757 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002758 &chip->vbat_monitor_params);
2759 }
2760 } else {
2761 pr_debug("unknown voltage notification state: %d\n", state);
2762 }
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002763 if (chip->bms_psy_registered)
Xiaozhe Shifa120db2013-06-06 15:57:19 -07002764 power_supply_changed(&chip->bms_psy);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002765}
2766
2767static int reset_vbat_monitoring(struct qpnp_bms_chip *chip)
2768{
2769 int rc;
2770
2771 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_DISABLE;
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002772
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002773 rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2774 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002775 if (rc) {
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002776 pr_err("tm disable failed: %d\n", rc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002777 return rc;
2778 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002779 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2780 pr_debug("battery removed, releasing wakelock\n");
2781 wake_unlock(&chip->low_voltage_wake_lock);
2782 }
2783 if (chip->in_cv_range) {
2784 pr_debug("battery removed, removing in_cv_range state\n");
2785 chip->in_cv_range = false;
2786 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002787 return 0;
2788}
2789
2790static int setup_vbat_monitoring(struct qpnp_bms_chip *chip)
2791{
2792 int rc;
2793
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002794 chip->vbat_monitor_params.low_thr = chip->low_voltage_threshold;
2795 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2796 - VBATT_ERROR_MARGIN;
2797 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_ENABLE;
2798 chip->vbat_monitor_params.channel = VBAT_SNS;
2799 chip->vbat_monitor_params.btm_ctx = (void *)chip;
2800 chip->vbat_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
2801 chip->vbat_monitor_params.threshold_notification = &btm_notify_vbat;
2802 pr_debug("set low thr to %d and high to %d\n",
2803 chip->vbat_monitor_params.low_thr,
2804 chip->vbat_monitor_params.high_thr);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002805
2806 if (!is_battery_present(chip)) {
2807 pr_debug("no battery inserted, do not enable vbat monitoring\n");
2808 chip->vbat_monitor_params.state_request =
2809 ADC_TM_HIGH_LOW_THR_DISABLE;
2810 } else {
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002811 rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2812 &chip->vbat_monitor_params);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002813 if (rc) {
2814 pr_err("tm setup failed: %d\n", rc);
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002815 return rc;
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002816 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002817 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002818
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002819 pr_debug("setup complete\n");
2820 return 0;
2821}
2822
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302823static void readjust_fcc_table(struct qpnp_bms_chip *chip)
2824{
2825 struct single_row_lut *temp, *old;
2826 int i, fcc, ratio;
2827
2828 if (!chip->enable_fcc_learning)
2829 return;
2830
2831 if (!chip->fcc_temp_lut) {
2832 pr_err("The static fcc lut table is NULL\n");
2833 return;
2834 }
2835
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07002836 temp = devm_kzalloc(chip->dev, sizeof(struct single_row_lut),
2837 GFP_KERNEL);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302838 if (!temp) {
2839 pr_err("Cannot allocate memory for adjusted fcc table\n");
2840 return;
2841 }
2842
2843 fcc = interpolate_fcc(chip->fcc_temp_lut, chip->fcc_new_batt_temp);
2844
2845 temp->cols = chip->fcc_temp_lut->cols;
2846 for (i = 0; i < chip->fcc_temp_lut->cols; i++) {
2847 temp->x[i] = chip->fcc_temp_lut->x[i];
2848 ratio = div_u64(chip->fcc_temp_lut->y[i] * 1000, fcc);
2849 temp->y[i] = (ratio * chip->fcc_new_mah);
2850 temp->y[i] /= 1000;
2851 }
2852
2853 old = chip->adjusted_fcc_temp_lut;
2854 chip->adjusted_fcc_temp_lut = temp;
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07002855 devm_kfree(chip->dev, old);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302856}
2857
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302858static int read_fcc_data_from_backup(struct qpnp_bms_chip *chip)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302859{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302860 int rc, i;
2861 u8 fcc = 0, chgcyl = 0;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302862
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302863 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2864 rc = qpnp_read_wrapper(chip, &fcc,
2865 chip->base + BMS_FCC_BASE_REG + i, 1);
2866 rc |= qpnp_read_wrapper(chip, &chgcyl,
2867 chip->base + BMS_CHGCYL_BASE_REG + i, 1);
2868 if (rc) {
2869 pr_err("Unable to read FCC data\n");
2870 return rc;
2871 }
2872 if (fcc == 0 || (fcc == 0xFF && chgcyl == 0xFF)) {
2873 /* FCC invalid/not present */
2874 chip->fcc_learning_samples[i].fcc_new = 0;
2875 chip->fcc_learning_samples[i].chargecycles = 0;
2876 } else {
2877 /* valid FCC data */
2878 chip->fcc_sample_count++;
2879 chip->fcc_learning_samples[i].fcc_new =
2880 fcc * chip->fcc_resolution;
2881 chip->fcc_learning_samples[i].chargecycles =
2882 chgcyl * CHGCYL_RESOLUTION;
2883 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302884 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302885
2886 return 0;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302887}
2888
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302889static int discard_backup_fcc_data(struct qpnp_bms_chip *chip)
2890{
2891 int rc = 0, i;
2892 u8 temp_u8 = 0;
2893
2894 chip->fcc_sample_count = 0;
2895 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2896 rc = qpnp_write_wrapper(chip, &temp_u8,
2897 chip->base + BMS_FCC_BASE_REG + i, 1);
2898 rc |= qpnp_write_wrapper(chip, &temp_u8,
2899 chip->base + BMS_CHGCYL_BASE_REG + i, 1);
2900 if (rc) {
2901 pr_err("Unable to clear FCC data\n");
2902 return rc;
2903 }
2904 }
2905
2906 return 0;
2907}
2908
2909static void
2910average_fcc_samples_and_readjust_fcc_table(struct qpnp_bms_chip *chip)
2911{
2912 int i, temp_fcc_avg = 0, temp_fcc_delta = 0, new_fcc_avg = 0;
2913 struct fcc_sample *ft;
2914
2915 for (i = 0; i < chip->min_fcc_learning_samples; i++)
2916 temp_fcc_avg += chip->fcc_learning_samples[i].fcc_new;
2917
2918 temp_fcc_avg /= chip->min_fcc_learning_samples;
2919 temp_fcc_delta = div_u64(temp_fcc_avg * DELTA_FCC_PERCENT, 100);
2920
2921 /* fix the fcc if its an outlier i.e. > 5% of the average */
2922 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2923 ft = &chip->fcc_learning_samples[i];
2924 if (abs(ft->fcc_new - temp_fcc_avg) > temp_fcc_delta)
2925 new_fcc_avg += temp_fcc_avg;
2926 else
2927 new_fcc_avg += ft->fcc_new;
2928 }
2929 new_fcc_avg /= chip->min_fcc_learning_samples;
2930
2931 chip->fcc_new_mah = new_fcc_avg;
2932 chip->fcc_new_batt_temp = FCC_DEFAULT_TEMP;
2933 pr_info("FCC update: New fcc_mah=%d, fcc_batt_temp=%d\n",
2934 new_fcc_avg, FCC_DEFAULT_TEMP);
2935 readjust_fcc_table(chip);
2936}
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302937
2938static void backup_charge_cycle(struct qpnp_bms_chip *chip)
2939{
2940 int rc = 0;
2941
2942 if (chip->charge_increase >= 0) {
2943 rc = qpnp_write_wrapper(chip, &chip->charge_increase,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302944 chip->base + CHARGE_INCREASE_STORAGE, 1);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302945 if (rc)
2946 pr_err("Unable to backup charge_increase\n");
2947 }
2948
2949 if (chip->charge_cycles >= 0) {
2950 rc = qpnp_write_wrapper(chip, (u8 *)&chip->charge_cycles,
2951 chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
2952 if (rc)
2953 pr_err("Unable to backup charge_cycles\n");
2954 }
2955}
2956
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302957static bool chargecycles_in_range(struct qpnp_bms_chip *chip)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302958{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302959 int i, min_cycle, max_cycle, valid_range;
2960
2961 /* find the smallest and largest charge cycle */
2962 max_cycle = min_cycle = chip->fcc_learning_samples[0].chargecycles;
2963 for (i = 1; i < chip->min_fcc_learning_samples; i++) {
2964 if (min_cycle > chip->fcc_learning_samples[i].chargecycles)
2965 min_cycle = chip->fcc_learning_samples[i].chargecycles;
2966 if (max_cycle < chip->fcc_learning_samples[i].chargecycles)
2967 max_cycle = chip->fcc_learning_samples[i].chargecycles;
2968 }
2969
2970 /* check if chargecyles are in range to continue with FCC update */
2971 valid_range = DIV_ROUND_UP(VALID_FCC_CHGCYL_RANGE,
2972 CHGCYL_RESOLUTION) * CHGCYL_RESOLUTION;
2973 if (abs(max_cycle - min_cycle) > valid_range)
2974 return false;
2975
2976 return true;
2977}
2978
2979static int read_chgcycle_data_from_backup(struct qpnp_bms_chip *chip)
2980{
2981 int rc;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302982 uint16_t temp_u16 = 0;
2983 u8 temp_u8 = 0;
2984
2985 rc = qpnp_read_wrapper(chip, &temp_u8,
2986 chip->base + CHARGE_INCREASE_STORAGE, 1);
2987 if (!rc && temp_u8 != 0xFF)
2988 chip->charge_increase = temp_u8;
2989
2990 rc = qpnp_read_wrapper(chip, (u8 *)&temp_u16,
2991 chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
2992 if (!rc && temp_u16 != 0xFFFF)
2993 chip->charge_cycles = temp_u16;
2994
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302995 return rc;
2996}
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302997
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302998static void
2999attempt_learning_new_fcc(struct qpnp_bms_chip *chip)
3000{
3001 pr_debug("Total FCC sample count=%d\n", chip->fcc_sample_count);
3002
3003 /* update FCC if we have the required samples */
3004 if ((chip->fcc_sample_count == chip->min_fcc_learning_samples) &&
3005 chargecycles_in_range(chip))
3006 average_fcc_samples_and_readjust_fcc_table(chip);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303007}
3008
3009static int calculate_real_soc(struct qpnp_bms_chip *chip,
3010 int batt_temp, struct raw_soc_params *raw, int cc_uah)
3011{
3012 int fcc_uah, rc_uah;
3013
3014 fcc_uah = calculate_fcc(chip, batt_temp);
3015 rc_uah = calculate_ocv_charge(chip, raw, fcc_uah);
3016
3017 return ((rc_uah - cc_uah) * 100) / fcc_uah;
3018}
3019
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303020#define MAX_U8_VALUE ((u8)(~0U))
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303021
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303022static int backup_new_fcc(struct qpnp_bms_chip *chip, int fcc_mah,
3023 int chargecycles)
3024{
3025 int rc, min_cycle, i;
3026 u8 fcc_new, chgcyl, pos = 0;
3027 struct fcc_sample *ft;
3028
3029 if ((fcc_mah > (chip->fcc_resolution * MAX_U8_VALUE)) ||
3030 (chargecycles > (CHGCYL_RESOLUTION * MAX_U8_VALUE))) {
3031 pr_warn("FCC/Chgcyl beyond storage limit. FCC=%d, chgcyl=%d\n",
3032 fcc_mah, chargecycles);
3033 return -EINVAL;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303034 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303035
3036 if (chip->fcc_sample_count == chip->min_fcc_learning_samples) {
3037 /* search best location - oldest entry */
3038 min_cycle = chip->fcc_learning_samples[0].chargecycles;
3039 for (i = 1; i < chip->min_fcc_learning_samples; i++) {
3040 if (min_cycle >
3041 chip->fcc_learning_samples[i].chargecycles)
3042 pos = i;
3043 }
3044 } else {
3045 /* find an empty location */
3046 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
3047 ft = &chip->fcc_learning_samples[i];
3048 if (ft->fcc_new == 0 || (ft->fcc_new == 0xFF &&
3049 ft->chargecycles == 0xFF)) {
3050 pos = i;
3051 break;
3052 }
3053 }
3054 chip->fcc_sample_count++;
3055 }
3056 chip->fcc_learning_samples[pos].fcc_new = fcc_mah;
3057 chip->fcc_learning_samples[pos].chargecycles = chargecycles;
3058
3059 fcc_new = DIV_ROUND_UP(fcc_mah, chip->fcc_resolution);
3060 rc = qpnp_write_wrapper(chip, (u8 *)&fcc_new,
3061 chip->base + BMS_FCC_BASE_REG + pos, 1);
3062 if (rc)
3063 return rc;
3064
3065 chgcyl = DIV_ROUND_UP(chargecycles, CHGCYL_RESOLUTION);
3066 rc = qpnp_write_wrapper(chip, (u8 *)&chgcyl,
3067 chip->base + BMS_CHGCYL_BASE_REG + pos, 1);
3068 if (rc)
3069 return rc;
3070
3071 pr_debug("Backup new FCC: fcc_new=%d, chargecycle=%d, pos=%d\n",
3072 fcc_new, chgcyl, pos);
3073
3074 return rc;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303075}
3076
3077static void update_fcc_learning_table(struct qpnp_bms_chip *chip,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303078 int new_fcc_uah, int chargecycles, int batt_temp)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303079{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303080 int rc, fcc_default, fcc_temp;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303081
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303082 /* convert the fcc at batt_temp to new fcc at FCC_DEFAULT_TEMP */
3083 fcc_default = calculate_fcc(chip, FCC_DEFAULT_TEMP) / 1000;
3084 fcc_temp = calculate_fcc(chip, batt_temp) / 1000;
3085 new_fcc_uah = (new_fcc_uah / fcc_temp) * fcc_default;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303086
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303087 rc = backup_new_fcc(chip, new_fcc_uah / 1000, chargecycles);
3088 if (rc) {
3089 pr_err("Unable to backup new FCC\n");
3090 return;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303091 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303092 /* check if FCC can be updated */
3093 attempt_learning_new_fcc(chip);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303094}
3095
3096static bool is_new_fcc_valid(int new_fcc_uah, int fcc_uah)
3097{
3098 if ((new_fcc_uah >= (fcc_uah / 2)) &&
3099 ((new_fcc_uah * 100) <= (fcc_uah * 105)))
3100 return true;
3101
3102 pr_debug("FCC rejected - not within valid limit\n");
3103 return false;
3104}
3105
3106static void fcc_learning_config(struct qpnp_bms_chip *chip, bool start)
3107{
3108 int rc, batt_temp;
3109 struct raw_soc_params raw;
3110 struct qpnp_vadc_result result;
3111 int fcc_uah, new_fcc_uah, delta_cc_uah, delta_soc;
3112
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003113 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303114 if (rc) {
3115 pr_err("Unable to read batt_temp\n");
3116 return;
3117 } else {
3118 batt_temp = (int)result.physical;
3119 }
3120
3121 rc = read_soc_params_raw(chip, &raw, batt_temp);
3122 if (rc) {
3123 pr_err("Unable to read CC, cannot update FCC\n");
3124 return;
3125 }
3126
3127 if (start) {
3128 chip->start_pc = interpolate_pc(chip->pc_temp_ocv_lut,
3129 batt_temp / 10, raw.last_good_ocv_uv / 1000);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003130 chip->start_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303131 chip->start_real_soc = calculate_real_soc(chip,
3132 batt_temp, &raw, chip->start_cc_uah);
3133 pr_debug("start_pc=%d, start_cc=%d, start_soc=%d real_soc=%d\n",
3134 chip->start_pc, chip->start_cc_uah,
3135 chip->start_soc, chip->start_real_soc);
3136 } else {
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003137 chip->end_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303138 delta_soc = 100 - chip->start_real_soc;
3139 delta_cc_uah = abs(chip->end_cc_uah - chip->start_cc_uah);
3140 new_fcc_uah = div_u64(delta_cc_uah * 100, delta_soc);
3141 fcc_uah = calculate_fcc(chip, batt_temp);
3142 pr_debug("start_soc=%d, start_pc=%d, start_real_soc=%d, start_cc=%d, end_cc=%d, new_fcc=%d\n",
3143 chip->start_soc, chip->start_pc, chip->start_real_soc,
3144 chip->start_cc_uah, chip->end_cc_uah, new_fcc_uah);
3145
3146 if (is_new_fcc_valid(new_fcc_uah, fcc_uah))
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303147 update_fcc_learning_table(chip, new_fcc_uah,
3148 chip->charge_cycles, batt_temp);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303149 }
3150}
3151
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003152#define MAX_CAL_TRIES 200
3153#define MIN_CAL_UA 3000
3154static void batfet_open_work(struct work_struct *work)
3155{
3156 int i;
3157 int rc;
3158 int result_ua;
3159 u8 orig_delay, sample_delay;
3160 struct qpnp_bms_chip *chip = container_of(work,
3161 struct qpnp_bms_chip,
3162 batfet_open_work);
3163
3164 rc = qpnp_read_wrapper(chip, &orig_delay,
3165 chip->base + BMS1_S1_DELAY_CTL, 1);
3166
3167 sample_delay = 0x0;
3168 rc = qpnp_write_wrapper(chip, &sample_delay,
3169 chip->base + BMS1_S1_DELAY_CTL, 1);
3170
3171 /*
3172 * In certain PMICs there is a coupling issue which causes
3173 * bad calibration value that result in a huge battery current
3174 * even when the BATFET is open. Do continious calibrations until
3175 * we hit reasonable cal values which result in low battery current
3176 */
3177
3178 for (i = 0; (!chip->batfet_closed) && i < MAX_CAL_TRIES; i++) {
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003179 rc = qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003180 /*
3181 * Wait 20mS after calibration and before reading battery
3182 * current. The BMS h/w uses calibration values in the
3183 * next sampling of vsense.
3184 */
3185 msleep(20);
3186 rc |= get_battery_current(chip, &result_ua);
3187 if (rc == 0 && abs(result_ua) <= MIN_CAL_UA) {
3188 pr_debug("good cal at %d attempt\n", i);
3189 break;
3190 }
3191 }
3192 pr_debug("batfet_closed = %d i = %d result_ua = %d\n",
3193 chip->batfet_closed, i, result_ua);
3194
3195 rc = qpnp_write_wrapper(chip, &orig_delay,
3196 chip->base + BMS1_S1_DELAY_CTL, 1);
3197}
3198
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003199static void charging_began(struct qpnp_bms_chip *chip)
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003200{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003201 mutex_lock(&chip->last_soc_mutex);
3202 chip->charge_start_tm_sec = 0;
3203 chip->catch_up_time_sec = 0;
3204 mutex_unlock(&chip->last_soc_mutex);
3205
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303206 chip->start_soc = report_state_of_charge(chip);
3207
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003208 mutex_lock(&chip->last_ocv_uv_mutex);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303209 if (chip->enable_fcc_learning)
3210 fcc_learning_config(chip, true);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003211 chip->soc_at_cv = -EINVAL;
3212 chip->prev_chg_soc = -EINVAL;
3213 mutex_unlock(&chip->last_ocv_uv_mutex);
3214}
3215
3216static void charging_ended(struct qpnp_bms_chip *chip)
3217{
3218 mutex_lock(&chip->last_soc_mutex);
3219 chip->charge_start_tm_sec = 0;
3220 chip->catch_up_time_sec = 0;
3221 mutex_unlock(&chip->last_soc_mutex);
3222
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303223 chip->end_soc = report_state_of_charge(chip);
3224
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003225 mutex_lock(&chip->last_ocv_uv_mutex);
3226 chip->soc_at_cv = -EINVAL;
3227 chip->prev_chg_soc = -EINVAL;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303228
3229 /* update the chargecycles */
3230 if (chip->end_soc > chip->start_soc) {
3231 chip->charge_increase += (chip->end_soc - chip->start_soc);
3232 if (chip->charge_increase > 100) {
3233 chip->charge_cycles++;
3234 chip->charge_increase = chip->charge_increase % 100;
3235 }
3236 if (chip->enable_fcc_learning)
3237 backup_charge_cycle(chip);
3238 }
3239
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003240 if (get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL) {
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303241 if (chip->enable_fcc_learning &&
3242 (chip->start_soc <= chip->min_fcc_learning_soc) &&
3243 (chip->start_pc <= chip->min_fcc_ocv_pc))
3244 fcc_learning_config(chip, false);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003245 chip->done_charging = true;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003246 chip->last_soc_invalid = true;
Xiaozhe Shicc48e992013-05-28 16:42:24 -07003247 } else if (chip->charging_adjusted_ocv > 0) {
3248 pr_debug("Charging stopped before full, adjusted OCV = %d\n",
3249 chip->charging_adjusted_ocv);
3250 chip->last_ocv_uv = chip->charging_adjusted_ocv;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003251 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303252
Xiaozhe Shicc48e992013-05-28 16:42:24 -07003253 chip->charging_adjusted_ocv = -EINVAL;
3254
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003255 mutex_unlock(&chip->last_ocv_uv_mutex);
3256}
3257
3258static void battery_status_check(struct qpnp_bms_chip *chip)
3259{
3260 int status = get_battery_status(chip);
3261
Xiaozhe Shibda84992013-09-05 10:39:11 -07003262 mutex_lock(&chip->status_lock);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003263 if (chip->battery_status != status) {
Xiaozhe Shi30e94802013-08-19 16:40:53 -07003264 pr_debug("status = %d, shadow status = %d\n",
3265 status, chip->battery_status);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003266 if (status == POWER_SUPPLY_STATUS_CHARGING) {
3267 pr_debug("charging started\n");
3268 charging_began(chip);
3269 } else if (chip->battery_status
3270 == POWER_SUPPLY_STATUS_CHARGING) {
3271 pr_debug("charging ended\n");
3272 charging_ended(chip);
3273 }
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003274
3275 if (status == POWER_SUPPLY_STATUS_FULL) {
3276 pr_debug("battery full\n");
Xiaozhe Shibda84992013-09-05 10:39:11 -07003277 recalculate_soc(chip);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003278 } else if (chip->battery_status
3279 == POWER_SUPPLY_STATUS_FULL) {
3280 pr_debug("battery not full any more\n");
3281 disable_bms_irq(&chip->ocv_thr_irq);
3282 disable_bms_irq(&chip->sw_cc_thr_irq);
3283 }
3284
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003285 chip->battery_status = status;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003286 /* battery charge status has changed, so force a soc
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003287 * recalculation to update the SoC */
3288 schedule_work(&chip->recalc_work);
3289 }
Xiaozhe Shibda84992013-09-05 10:39:11 -07003290 mutex_unlock(&chip->status_lock);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003291}
3292
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003293#define CALIB_WRKARND_DIG_MAJOR_MAX 0x03
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003294static void batfet_status_check(struct qpnp_bms_chip *chip)
3295{
3296 bool batfet_closed;
3297
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003298 batfet_closed = is_batfet_closed(chip);
3299 if (chip->batfet_closed != batfet_closed) {
3300 chip->batfet_closed = batfet_closed;
Xiaozhe Shiffb7cfc2014-01-03 10:47:36 -08003301 if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX)
3302 return;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003303 if (batfet_closed == false) {
3304 /* batfet opened */
3305 schedule_work(&chip->batfet_open_work);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003306 qpnp_iadc_skip_calibration(chip->iadc_dev);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003307 } else {
3308 /* batfet closed */
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003309 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, true);
3310 qpnp_iadc_resume_calibration(chip->iadc_dev);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003311 }
3312 }
3313}
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003314
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003315static void battery_insertion_check(struct qpnp_bms_chip *chip)
3316{
Xiaozhe Shi90f3a412013-08-21 10:31:35 -07003317 int present = (int)is_battery_present(chip);
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07003318 int insertion_ocv_uv = get_battery_insertion_ocv_uv(chip);
3319 int insertion_ocv_taken = (insertion_ocv_uv > 0);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003320
3321 mutex_lock(&chip->vbat_monitor_mutex);
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07003322 if (chip->battery_present != present
3323 && (present == insertion_ocv_taken
3324 || chip->battery_present == -EINVAL)) {
3325 pr_debug("status = %d, shadow status = %d, insertion_ocv_uv = %d\n",
3326 present, chip->battery_present,
3327 insertion_ocv_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003328 if (chip->battery_present != -EINVAL) {
3329 if (present) {
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07003330 chip->insertion_ocv_uv = insertion_ocv_uv;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003331 setup_vbat_monitoring(chip);
3332 chip->new_battery = true;
3333 } else {
3334 reset_vbat_monitoring(chip);
3335 }
3336 }
3337 chip->battery_present = present;
3338 /* a new battery was inserted or removed, so force a soc
3339 * recalculation to update the SoC */
3340 schedule_work(&chip->recalc_work);
3341 }
3342 mutex_unlock(&chip->vbat_monitor_mutex);
3343}
3344
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003345/* Returns capacity as a SoC percentage between 0 and 100 */
3346static int get_prop_bms_capacity(struct qpnp_bms_chip *chip)
3347{
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08003348 return report_state_of_charge(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003349}
3350
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003351static void qpnp_bms_external_power_changed(struct power_supply *psy)
3352{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003353 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
3354 bms_psy);
3355
3356 battery_insertion_check(chip);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003357 batfet_status_check(chip);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003358 battery_status_check(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003359}
3360
3361static int qpnp_bms_power_get_property(struct power_supply *psy,
3362 enum power_supply_property psp,
3363 union power_supply_propval *val)
3364{
3365 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
3366 bms_psy);
3367
3368 switch (psp) {
3369 case POWER_SUPPLY_PROP_CAPACITY:
3370 val->intval = get_prop_bms_capacity(chip);
3371 break;
Xiaozhe Shibda84992013-09-05 10:39:11 -07003372 case POWER_SUPPLY_PROP_STATUS:
3373 val->intval = chip->battery_status;
3374 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003375 case POWER_SUPPLY_PROP_CURRENT_NOW:
3376 val->intval = get_prop_bms_current_now(chip);
3377 break;
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07003378 case POWER_SUPPLY_PROP_RESISTANCE:
3379 val->intval = get_prop_bms_batt_resistance(chip);
3380 break;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07003381 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
3382 val->intval = get_prop_bms_charge_counter(chip);
3383 break;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003384 case POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW:
3385 val->intval = get_prop_bms_charge_counter_shadow(chip);
3386 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003387 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
3388 val->intval = get_prop_bms_charge_full_design(chip);
3389 break;
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05303390 case POWER_SUPPLY_PROP_CHARGE_FULL:
3391 val->intval = get_prop_bms_charge_full(chip);
3392 break;
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05303393 case POWER_SUPPLY_PROP_CYCLE_COUNT:
3394 val->intval = chip->charge_cycles;
3395 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003396 default:
3397 return -EINVAL;
3398 }
3399 return 0;
3400}
3401
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003402#define OCV_USE_LIMIT_EN BIT(7)
3403static int set_ocv_voltage_thresholds(struct qpnp_bms_chip *chip,
3404 int low_voltage_threshold,
3405 int high_voltage_threshold)
3406{
3407 uint16_t low_voltage_raw, high_voltage_raw;
3408 int rc;
3409
3410 low_voltage_raw = convert_vbatt_uv_to_raw(chip,
3411 low_voltage_threshold);
3412 high_voltage_raw = convert_vbatt_uv_to_raw(chip,
3413 high_voltage_threshold);
3414 rc = qpnp_write_wrapper(chip, (u8 *)&low_voltage_raw,
3415 chip->base + BMS1_OCV_USE_LOW_LIMIT_THR0, 2);
3416 if (rc) {
3417 pr_err("Failed to set ocv low voltage threshold: %d\n", rc);
3418 return rc;
3419 }
3420 rc = qpnp_write_wrapper(chip, (u8 *)&high_voltage_raw,
3421 chip->base + BMS1_OCV_USE_HIGH_LIMIT_THR0, 2);
3422 if (rc) {
3423 pr_err("Failed to set ocv high voltage threshold: %d\n", rc);
3424 return rc;
3425 }
3426 rc = qpnp_masked_write(chip, BMS1_OCV_USE_LIMIT_CTL,
3427 OCV_USE_LIMIT_EN, OCV_USE_LIMIT_EN);
3428 if (rc) {
3429 pr_err("Failed to enabled ocv voltage thresholds: %d\n", rc);
3430 return rc;
3431 }
3432 pr_debug("ocv low threshold set to %d uv or 0x%x raw\n",
3433 low_voltage_threshold, low_voltage_raw);
3434 pr_debug("ocv high threshold set to %d uv or 0x%x raw\n",
3435 high_voltage_threshold, high_voltage_raw);
3436 return 0;
3437}
3438
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003439static int read_shutdown_iavg_ma(struct qpnp_bms_chip *chip)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003440{
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003441 u8 iavg;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003442 int rc;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003443
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003444 rc = qpnp_read_wrapper(chip, &iavg, chip->base + IAVG_STORAGE_REG, 1);
3445 if (rc) {
3446 pr_err("failed to read addr = %d %d assuming %d\n",
3447 chip->base + IAVG_STORAGE_REG, rc,
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003448 MIN_IAVG_MA);
3449 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003450 } else if (iavg == IAVG_INVALID) {
3451 pr_err("invalid iavg read from BMS1_DATA_REG_1, using %d\n",
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003452 MIN_IAVG_MA);
3453 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003454 } else {
3455 if (iavg == 0)
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003456 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003457 else
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003458 return MIN_IAVG_MA + IAVG_STEP_SIZE_MA * iavg;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003459 }
3460}
3461
3462static int read_shutdown_soc(struct qpnp_bms_chip *chip)
3463{
3464 u8 stored_soc;
3465 int rc, shutdown_soc;
3466
3467 /*
3468 * The previous SOC is stored in the first 7 bits of the register as
3469 * (Shutdown SOC + 1). This allows for register reset values of both
3470 * 0x00 and 0x7F.
3471 */
3472 rc = qpnp_read_wrapper(chip, &stored_soc, chip->soc_storage_addr, 1);
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003473 if (rc) {
3474 pr_err("failed to read addr = %d %d\n",
3475 chip->soc_storage_addr, rc);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003476 return SOC_INVALID;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003477 }
3478
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003479 if ((stored_soc >> 1) > 0)
3480 shutdown_soc = (stored_soc >> 1) - 1;
3481 else
3482 shutdown_soc = SOC_INVALID;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003483
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003484 pr_debug("stored soc = 0x%02x, shutdown_soc = %d\n",
3485 stored_soc, shutdown_soc);
3486 return shutdown_soc;
3487}
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003488
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003489#define BAT_REMOVED_OFFMODE_BIT BIT(6)
3490static bool is_battery_replaced_in_offmode(struct qpnp_bms_chip *chip)
3491{
3492 u8 batt_pres;
3493 int rc;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003494
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003495 if (chip->batt_pres_addr) {
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003496 rc = qpnp_read_wrapper(chip, &batt_pres,
3497 chip->batt_pres_addr, 1);
3498 pr_debug("offmode removed: %02x\n", batt_pres);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003499 if (!rc && (batt_pres & BAT_REMOVED_OFFMODE_BIT))
3500 return true;
3501 }
3502 return false;
3503}
3504
3505static void load_shutdown_data(struct qpnp_bms_chip *chip)
3506{
3507 int calculated_soc, shutdown_soc;
3508 bool invalid_stored_soc;
3509 bool offmode_battery_replaced;
3510 bool shutdown_soc_out_of_limit;
3511
3512 /*
3513 * Read the saved shutdown SoC from the configured register and
3514 * check if the value has been reset
3515 */
3516 shutdown_soc = read_shutdown_soc(chip);
3517 invalid_stored_soc = (shutdown_soc == SOC_INVALID);
3518
3519 /*
3520 * Do a quick run of SoC calculation to find whether the shutdown soc
3521 * is close enough.
3522 */
3523 calculated_soc = recalculate_raw_soc(chip);
3524 shutdown_soc_out_of_limit = (abs(shutdown_soc - calculated_soc)
3525 > chip->shutdown_soc_valid_limit);
3526 pr_debug("calculated_soc = %d, valid_limit = %d\n",
3527 calculated_soc, chip->shutdown_soc_valid_limit);
3528
3529 /*
3530 * Check if the battery has been replaced while the system was powered
3531 * down.
3532 */
3533 offmode_battery_replaced = is_battery_replaced_in_offmode(chip);
3534
3535 /* Invalidate the shutdown SoC if any of these conditions hold true */
3536 if (chip->ignore_shutdown_soc
3537 || invalid_stored_soc
3538 || offmode_battery_replaced
3539 || shutdown_soc_out_of_limit) {
3540 chip->battery_removed = true;
3541 chip->shutdown_soc_invalid = true;
Xiaozhe Shic92cfd92013-10-25 11:36:42 -07003542 chip->shutdown_iavg_ma = MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003543 pr_debug("Ignoring shutdown SoC: invalid = %d, offmode = %d, out_of_limit = %d\n",
3544 invalid_stored_soc, offmode_battery_replaced,
3545 shutdown_soc_out_of_limit);
3546 } else {
3547 chip->shutdown_iavg_ma = read_shutdown_iavg_ma(chip);
3548 chip->shutdown_soc = shutdown_soc;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07003549 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303550
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003551 pr_debug("raw_soc = %d shutdown_soc = %d shutdown_iavg = %d shutdown_soc_invalid = %d, battery_removed = %d\n",
3552 calculated_soc,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003553 chip->shutdown_soc,
3554 chip->shutdown_iavg_ma,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303555 chip->shutdown_soc_invalid,
3556 chip->battery_removed);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003557}
3558
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003559static irqreturn_t bms_ocv_thr_irq_handler(int irq, void *_chip)
3560{
3561 struct qpnp_bms_chip *chip = _chip;
3562
3563 pr_debug("ocv_thr irq triggered\n");
3564 bms_stay_awake(&chip->soc_wake_source);
3565 schedule_work(&chip->recalc_work);
3566 return IRQ_HANDLED;
3567}
3568
3569static irqreturn_t bms_sw_cc_thr_irq_handler(int irq, void *_chip)
3570{
3571 struct qpnp_bms_chip *chip = _chip;
3572
3573 pr_debug("sw_cc_thr irq triggered\n");
Anirudh Ghayal1166eef2013-12-23 19:05:33 +05303574 disable_bms_irq_nosync(&chip->sw_cc_thr_irq);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003575 bms_stay_awake(&chip->soc_wake_source);
3576 schedule_work(&chip->recalc_work);
3577 return IRQ_HANDLED;
3578}
3579
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003580static int64_t read_battery_id(struct qpnp_bms_chip *chip)
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003581{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003582 int rc;
3583 struct qpnp_vadc_result result;
3584
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003585 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX2_BAT_ID, &result);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003586 if (rc) {
3587 pr_err("error reading batt id channel = %d, rc = %d\n",
3588 LR_MUX2_BAT_ID, rc);
3589 return rc;
3590 }
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003591
3592 return result.physical;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003593}
3594
3595static int set_battery_data(struct qpnp_bms_chip *chip)
3596{
3597 int64_t battery_id;
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003598 int rc = 0, dt_data = false;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003599 struct bms_battery_data *batt_data;
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003600 struct device_node *node;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003601
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003602 if (chip->batt_type == BATT_DESAY) {
3603 batt_data = &desay_5200_data;
3604 } else if (chip->batt_type == BATT_PALLADIUM) {
3605 batt_data = &palladium_1500_data;
3606 } else if (chip->batt_type == BATT_OEM) {
3607 batt_data = &oem_batt_data;
Wu Fenglin2ac88aa2013-04-25 12:43:40 +08003608 } else if (chip->batt_type == BATT_QRD_4V35_2000MAH) {
3609 batt_data = &QRD_4v35_2000mAh_data;
tingtingf50326f2013-06-05 15:07:24 +08003610 } else if (chip->batt_type == BATT_QRD_4V2_1300MAH) {
3611 batt_data = &qrd_4v2_1300mah_data;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003612 } else {
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003613 battery_id = read_battery_id(chip);
3614 if (battery_id < 0) {
3615 pr_err("cannot read battery id err = %lld\n",
3616 battery_id);
3617 return battery_id;
3618 }
3619
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003620 node = of_find_node_by_name(chip->spmi->dev.of_node,
3621 "qcom,battery-data");
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003622 if (!node) {
3623 pr_warn("No available batterydata, using palladium 1500\n");
3624 batt_data = &palladium_1500_data;
3625 goto assign_data;
3626 }
3627 batt_data = devm_kzalloc(chip->dev,
3628 sizeof(struct bms_battery_data), GFP_KERNEL);
3629 if (!batt_data) {
3630 pr_err("Could not alloc battery data\n");
3631 batt_data = &palladium_1500_data;
3632 goto assign_data;
3633 }
3634 batt_data->fcc_temp_lut = devm_kzalloc(chip->dev,
3635 sizeof(struct single_row_lut),
3636 GFP_KERNEL);
3637 batt_data->pc_temp_ocv_lut = devm_kzalloc(chip->dev,
3638 sizeof(struct pc_temp_ocv_lut),
3639 GFP_KERNEL);
3640 batt_data->rbatt_sf_lut = devm_kzalloc(chip->dev,
3641 sizeof(struct sf_lut),
3642 GFP_KERNEL);
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003643
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003644 batt_data->max_voltage_uv = -1;
3645 batt_data->cutoff_uv = -1;
3646 batt_data->iterm_ua = -1;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003647
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003648 /*
3649 * if the alloced luts are 0s, of_batterydata_read_data ignores
3650 * them.
3651 */
3652 rc = of_batterydata_read_data(node, batt_data, battery_id);
3653 if (rc == 0 && batt_data->fcc_temp_lut
3654 && batt_data->pc_temp_ocv_lut
3655 && batt_data->rbatt_sf_lut) {
3656 dt_data = true;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003657 } else {
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003658 pr_err("battery data load failed, using palladium 1500\n");
3659 devm_kfree(chip->dev, batt_data->fcc_temp_lut);
3660 devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut);
3661 devm_kfree(chip->dev, batt_data->rbatt_sf_lut);
3662 devm_kfree(chip->dev, batt_data);
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003663 batt_data = &palladium_1500_data;
3664 }
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003665 }
3666
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003667assign_data:
Xiaozhe Shi976618f2013-04-30 10:49:30 -07003668 chip->fcc_mah = batt_data->fcc;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003669 chip->fcc_temp_lut = batt_data->fcc_temp_lut;
3670 chip->fcc_sf_lut = batt_data->fcc_sf_lut;
3671 chip->pc_temp_ocv_lut = batt_data->pc_temp_ocv_lut;
3672 chip->pc_sf_lut = batt_data->pc_sf_lut;
3673 chip->rbatt_sf_lut = batt_data->rbatt_sf_lut;
3674 chip->default_rbatt_mohm = batt_data->default_rbatt_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07003675 chip->rbatt_capacitive_mohm = batt_data->rbatt_capacitive_mohm;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07003676 chip->flat_ocv_threshold_uv = batt_data->flat_ocv_threshold_uv;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003677
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003678 /* Override battery properties if specified in the battery profile */
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003679 if (batt_data->max_voltage_uv >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003680 chip->max_voltage_uv = batt_data->max_voltage_uv;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003681 if (batt_data->cutoff_uv >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003682 chip->v_cutoff_uv = batt_data->cutoff_uv;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003683 if (batt_data->iterm_ua >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003684 chip->chg_term_ua = batt_data->iterm_ua;
3685
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003686 if (chip->pc_temp_ocv_lut == NULL) {
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003687 pr_err("temp ocv lut table has not been loaded\n");
3688 if (dt_data) {
3689 devm_kfree(chip->dev, batt_data->fcc_temp_lut);
3690 devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut);
3691 devm_kfree(chip->dev, batt_data->rbatt_sf_lut);
3692 devm_kfree(chip->dev, batt_data);
3693 }
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003694 return -EINVAL;
3695 }
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003696
3697 if (dt_data)
3698 devm_kfree(chip->dev, batt_data);
3699
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003700 return 0;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003701}
3702
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07003703static int bms_get_adc(struct qpnp_bms_chip *chip,
3704 struct spmi_device *spmi)
3705{
3706 int rc = 0;
3707
3708 chip->vadc_dev = qpnp_get_vadc(&spmi->dev, "bms");
3709 if (IS_ERR(chip->vadc_dev)) {
3710 rc = PTR_ERR(chip->vadc_dev);
3711 if (rc != -EPROBE_DEFER)
3712 pr_err("vadc property missing, rc=%d\n", rc);
3713 return rc;
3714 }
3715
3716 chip->iadc_dev = qpnp_get_iadc(&spmi->dev, "bms");
3717 if (IS_ERR(chip->iadc_dev)) {
3718 rc = PTR_ERR(chip->iadc_dev);
3719 if (rc != -EPROBE_DEFER)
3720 pr_err("iadc property missing, rc=%d\n", rc);
3721 return rc;
3722 }
3723
3724 chip->adc_tm_dev = qpnp_get_adc_tm(&spmi->dev, "bms");
3725 if (IS_ERR(chip->adc_tm_dev)) {
3726 rc = PTR_ERR(chip->adc_tm_dev);
3727 if (rc != -EPROBE_DEFER)
3728 pr_err("adc-tm not ready, defer probe\n");
3729 return rc;
3730 }
3731
3732 return 0;
3733}
3734
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003735#define SPMI_PROP_READ(chip_prop, qpnp_spmi_property, retval) \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003736do { \
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003737 if (retval) \
3738 break; \
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003739 retval = of_property_read_u32(chip->spmi->dev.of_node, \
Xiaozhe Shi9bd24622013-01-23 15:54:54 -08003740 "qcom," qpnp_spmi_property, \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003741 &chip->chip_prop); \
3742 if (retval) { \
3743 pr_err("Error reading " #qpnp_spmi_property \
3744 " property %d\n", rc); \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003745 } \
3746} while (0)
3747
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303748#define SPMI_PROP_READ_BOOL(chip_prop, qpnp_spmi_property) \
3749do { \
3750 chip->chip_prop = of_property_read_bool(chip->spmi->dev.of_node,\
3751 "qcom," qpnp_spmi_property); \
3752} while (0)
3753
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003754static inline int bms_read_properties(struct qpnp_bms_chip *chip)
3755{
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003756 int rc = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003757
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003758 SPMI_PROP_READ(r_sense_uohm, "r-sense-uohm", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003759 SPMI_PROP_READ(v_cutoff_uv, "v-cutoff-uv", rc);
3760 SPMI_PROP_READ(max_voltage_uv, "max-voltage-uv", rc);
3761 SPMI_PROP_READ(r_conn_mohm, "r-conn-mohm", rc);
3762 SPMI_PROP_READ(chg_term_ua, "chg-term-ua", rc);
3763 SPMI_PROP_READ(shutdown_soc_valid_limit,
3764 "shutdown-soc-valid-limit", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003765 SPMI_PROP_READ(adjust_soc_low_threshold,
3766 "adjust-soc-low-threshold", rc);
3767 SPMI_PROP_READ(batt_type, "batt-type", rc);
3768 SPMI_PROP_READ(low_soc_calc_threshold,
3769 "low-soc-calculate-soc-threshold", rc);
3770 SPMI_PROP_READ(low_soc_calculate_soc_ms,
3771 "low-soc-calculate-soc-ms", rc);
Xiaozhe Shicb487b12013-10-14 17:42:07 -07003772 SPMI_PROP_READ(low_voltage_calculate_soc_ms,
3773 "low-voltage-calculate-soc-ms", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003774 SPMI_PROP_READ(calculate_soc_ms, "calculate-soc-ms", rc);
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07003775 SPMI_PROP_READ(high_ocv_correction_limit_uv,
3776 "high-ocv-correction-limit-uv", rc);
3777 SPMI_PROP_READ(low_ocv_correction_limit_uv,
3778 "low-ocv-correction-limit-uv", rc);
3779 SPMI_PROP_READ(hold_soc_est,
3780 "hold-soc-est", rc);
3781 SPMI_PROP_READ(ocv_high_threshold_uv,
3782 "ocv-voltage-high-threshold-uv", rc);
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003783 SPMI_PROP_READ(ocv_low_threshold_uv,
3784 "ocv-voltage-low-threshold-uv", rc);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08003785 SPMI_PROP_READ(low_voltage_threshold, "low-voltage-threshold", rc);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003786 SPMI_PROP_READ(temperature_margin, "tm-temp-margin", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003787
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003788 chip->use_external_rsense = of_property_read_bool(
3789 chip->spmi->dev.of_node,
3790 "qcom,use-external-rsense");
3791 chip->ignore_shutdown_soc = of_property_read_bool(
3792 chip->spmi->dev.of_node,
3793 "qcom,ignore-shutdown-soc");
3794 chip->use_voltage_soc = of_property_read_bool(chip->spmi->dev.of_node,
3795 "qcom,use-voltage-soc");
3796 chip->use_ocv_thresholds = of_property_read_bool(
3797 chip->spmi->dev.of_node,
3798 "qcom,use-ocv-thresholds");
3799
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003800 if (chip->adjust_soc_low_threshold >= 45)
3801 chip->adjust_soc_low_threshold = 45;
3802
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303803 SPMI_PROP_READ_BOOL(enable_fcc_learning, "enable-fcc-learning");
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303804 if (chip->enable_fcc_learning) {
3805 SPMI_PROP_READ(min_fcc_learning_soc,
3806 "min-fcc-learning-soc", rc);
3807 SPMI_PROP_READ(min_fcc_ocv_pc,
3808 "min-fcc-ocv-pc", rc);
3809 SPMI_PROP_READ(min_fcc_learning_samples,
3810 "min-fcc-learning-samples", rc);
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303811 SPMI_PROP_READ(fcc_resolution,
3812 "fcc-resolution", rc);
3813 if (chip->min_fcc_learning_samples > MAX_FCC_CYCLES)
3814 chip->min_fcc_learning_samples = MAX_FCC_CYCLES;
3815 chip->fcc_learning_samples = devm_kzalloc(&chip->spmi->dev,
3816 (sizeof(struct fcc_sample) *
3817 chip->min_fcc_learning_samples), GFP_KERNEL);
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003818 if (chip->fcc_learning_samples == NULL)
3819 return -ENOMEM;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303820 pr_debug("min-fcc-soc=%d, min-fcc-pc=%d, min-fcc-cycles=%d\n",
3821 chip->min_fcc_learning_soc, chip->min_fcc_ocv_pc,
3822 chip->min_fcc_learning_samples);
3823 }
3824
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003825 if (rc) {
3826 pr_err("Missing required properties.\n");
3827 return rc;
3828 }
3829
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003830 pr_debug("dts data: r_sense_uohm:%d, v_cutoff_uv:%d, max_v:%d\n",
3831 chip->r_sense_uohm, chip->v_cutoff_uv,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003832 chip->max_voltage_uv);
3833 pr_debug("r_conn:%d, shutdown_soc: %d, adjust_soc_low:%d\n",
3834 chip->r_conn_mohm, chip->shutdown_soc_valid_limit,
3835 chip->adjust_soc_low_threshold);
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07003836 pr_debug("chg_term_ua:%d, batt_type:%d\n",
3837 chip->chg_term_ua,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003838 chip->batt_type);
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08003839 pr_debug("ignore_shutdown_soc:%d, use_voltage_soc:%d\n",
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08003840 chip->ignore_shutdown_soc, chip->use_voltage_soc);
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003841 pr_debug("use external rsense: %d\n", chip->use_external_rsense);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003842 return 0;
3843}
3844
3845static inline void bms_initialize_constants(struct qpnp_bms_chip *chip)
3846{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003847 chip->prev_pc_unusable = -EINVAL;
3848 chip->soc_at_cv = -EINVAL;
3849 chip->calculated_soc = -EINVAL;
Xiaozhe Shie118c692012-09-24 15:17:43 -07003850 chip->last_soc = -EINVAL;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07003851 chip->last_soc_est = -EINVAL;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003852 chip->battery_present = -EINVAL;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003853 chip->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08003854 chip->last_cc_uah = INT_MIN;
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08003855 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
3856 chip->prev_last_good_ocv_raw = OCV_RAW_UNINITIALIZED;
Xiaozhe Shie118c692012-09-24 15:17:43 -07003857 chip->first_time_calc_soc = 1;
3858 chip->first_time_calc_uuc = 1;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003859}
3860
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003861#define SPMI_FIND_IRQ(chip, irq_name) \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003862do { \
3863 chip->irq_name##_irq.irq = spmi_get_irq_byname(chip->spmi, \
3864 resource, #irq_name); \
3865 if (chip->irq_name##_irq.irq < 0) { \
3866 pr_err("Unable to get " #irq_name " irq\n"); \
3867 return -ENXIO; \
3868 } \
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003869} while (0)
3870
3871static int bms_find_irqs(struct qpnp_bms_chip *chip,
3872 struct spmi_resource *resource)
3873{
3874 SPMI_FIND_IRQ(chip, sw_cc_thr);
3875 SPMI_FIND_IRQ(chip, ocv_thr);
3876 return 0;
3877}
3878
3879#define SPMI_REQUEST_IRQ(chip, rc, irq_name) \
3880do { \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003881 rc = devm_request_irq(chip->dev, chip->irq_name##_irq.irq, \
3882 bms_##irq_name##_irq_handler, \
3883 IRQF_TRIGGER_RISING, #irq_name, chip); \
3884 if (rc < 0) { \
3885 pr_err("Unable to request " #irq_name " irq: %d\n", rc);\
3886 return -ENXIO; \
3887 } \
Xiaozhe Shif511a6e2014-02-20 14:37:18 -08003888 chip->irq_name##_irq.ready = true; \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003889} while (0)
3890
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003891static int bms_request_irqs(struct qpnp_bms_chip *chip)
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003892{
3893 int rc;
3894
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003895 SPMI_REQUEST_IRQ(chip, rc, sw_cc_thr);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003896 enable_irq_wake(chip->sw_cc_thr_irq.irq);
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003897 SPMI_REQUEST_IRQ(chip, rc, ocv_thr);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003898 enable_irq_wake(chip->ocv_thr_irq.irq);
3899 return 0;
3900}
3901
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003902#define REG_OFFSET_PERP_TYPE 0x04
3903#define REG_OFFSET_PERP_SUBTYPE 0x05
3904#define BMS_BMS_TYPE 0xD
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003905#define BMS_BMS1_SUBTYPE 0x1
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003906#define BMS_IADC_TYPE 0x8
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003907#define BMS_IADC1_SUBTYPE 0x3
3908#define BMS_IADC2_SUBTYPE 0x5
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003909
3910static int register_spmi(struct qpnp_bms_chip *chip, struct spmi_device *spmi)
3911{
3912 struct spmi_resource *spmi_resource;
3913 struct resource *resource;
3914 int rc;
3915 u8 type, subtype;
3916
3917 chip->dev = &(spmi->dev);
3918 chip->spmi = spmi;
3919
3920 spmi_for_each_container_dev(spmi_resource, spmi) {
3921 if (!spmi_resource) {
3922 pr_err("qpnp_bms: spmi resource absent\n");
3923 return -ENXIO;
3924 }
3925
3926 resource = spmi_get_resource(spmi, spmi_resource,
3927 IORESOURCE_MEM, 0);
3928 if (!(resource && resource->start)) {
3929 pr_err("node %s IO resource absent!\n",
3930 spmi->dev.of_node->full_name);
3931 return -ENXIO;
3932 }
3933
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07003934 pr_debug("Node name = %s\n", spmi_resource->of_node->name);
3935
3936 if (strcmp("qcom,batt-pres-status",
3937 spmi_resource->of_node->name) == 0) {
3938 chip->batt_pres_addr = resource->start;
3939 continue;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003940 } else if (strcmp("qcom,soc-storage-reg",
3941 spmi_resource->of_node->name) == 0) {
3942 chip->soc_storage_addr = resource->start;
3943 continue;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07003944 }
3945
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003946 rc = qpnp_read_wrapper(chip, &type,
3947 resource->start + REG_OFFSET_PERP_TYPE, 1);
3948 if (rc) {
3949 pr_err("Peripheral type read failed rc=%d\n", rc);
3950 return rc;
3951 }
3952 rc = qpnp_read_wrapper(chip, &subtype,
3953 resource->start + REG_OFFSET_PERP_SUBTYPE, 1);
3954 if (rc) {
3955 pr_err("Peripheral subtype read failed rc=%d\n", rc);
3956 return rc;
3957 }
3958
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003959 if (type == BMS_BMS_TYPE && subtype == BMS_BMS1_SUBTYPE) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003960 chip->base = resource->start;
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003961 rc = bms_find_irqs(chip, spmi_resource);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003962 if (rc) {
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003963 pr_err("Could not find irqs\n");
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003964 return rc;
3965 }
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003966 } else if (type == BMS_IADC_TYPE
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003967 && (subtype == BMS_IADC1_SUBTYPE
3968 || subtype == BMS_IADC2_SUBTYPE)) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003969 chip->iadc_base = resource->start;
3970 } else {
3971 pr_err("Invalid peripheral start=0x%x type=0x%x, subtype=0x%x\n",
3972 resource->start, type, subtype);
3973 }
3974 }
3975
3976 if (chip->base == 0) {
3977 dev_err(&spmi->dev, "BMS peripheral was not registered\n");
3978 return -EINVAL;
3979 }
3980 if (chip->iadc_base == 0) {
3981 dev_err(&spmi->dev, "BMS_IADC peripheral was not registered\n");
3982 return -EINVAL;
3983 }
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003984 if (chip->soc_storage_addr == 0) {
3985 /* default to dvdd backed BMS data reg0 */
3986 chip->soc_storage_addr = chip->base + SOC_STORAGE_REG;
3987 }
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003988
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003989 pr_debug("bms-base = 0x%04x, iadc-base = 0x%04x, bat-pres-reg = 0x%04x, soc-storage-reg = 0x%04x\n",
3990 chip->base, chip->iadc_base,
3991 chip->batt_pres_addr, chip->soc_storage_addr);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003992 return 0;
3993}
3994
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003995#define ADC_CH_SEL_MASK 0x7
3996#define ADC_INT_RSNSN_CTL_MASK 0x3
3997#define ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE 0x2
3998#define FAST_AVG_EN_MASK 0x80
3999#define FAST_AVG_EN_VALUE_EXT_RSENSE 0x80
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004000static int read_iadc_channel_select(struct qpnp_bms_chip *chip)
4001{
4002 u8 iadc_channel_select;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004003 int32_t rds_rsense_nohm;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004004 int rc;
4005
4006 rc = qpnp_read_wrapper(chip, &iadc_channel_select,
4007 chip->iadc_base + IADC1_BMS_ADC_CH_SEL_CTL, 1);
4008 if (rc) {
4009 pr_err("Error reading bms_iadc channel register %d\n", rc);
4010 return rc;
4011 }
4012
4013 iadc_channel_select &= ADC_CH_SEL_MASK;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004014 if (iadc_channel_select != EXTERNAL_RSENSE
4015 && iadc_channel_select != INTERNAL_RSENSE) {
4016 pr_err("IADC1_BMS_IADC configured incorrectly. Selected channel = %d\n",
4017 iadc_channel_select);
4018 return -EINVAL;
4019 }
4020
4021 if (chip->use_external_rsense) {
4022 pr_debug("External rsense selected\n");
4023 if (iadc_channel_select == INTERNAL_RSENSE) {
4024 pr_debug("Internal rsense detected; Changing rsense to external\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004025 rc = qpnp_masked_write_iadc(chip,
4026 IADC1_BMS_ADC_CH_SEL_CTL,
4027 ADC_CH_SEL_MASK,
4028 EXTERNAL_RSENSE);
4029 if (rc) {
4030 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
4031 IADC1_BMS_ADC_CH_SEL_CTL,
4032 EXTERNAL_RSENSE, rc);
4033 return rc;
4034 }
Xiaozhe Shif3da8622013-06-10 14:50:56 -07004035 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07004036 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07004037 chip->software_shdw_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004038 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004039 } else {
4040 pr_debug("Internal rsense selected\n");
4041 if (iadc_channel_select == EXTERNAL_RSENSE) {
4042 pr_debug("External rsense detected; Changing rsense to internal\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004043 rc = qpnp_masked_write_iadc(chip,
4044 IADC1_BMS_ADC_CH_SEL_CTL,
4045 ADC_CH_SEL_MASK,
4046 INTERNAL_RSENSE);
4047 if (rc) {
4048 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
4049 IADC1_BMS_ADC_CH_SEL_CTL,
4050 INTERNAL_RSENSE, rc);
4051 return rc;
4052 }
Xiaozhe Shif3da8622013-06-10 14:50:56 -07004053 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
4054 chip->software_shdw_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004055 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004056
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07004057 rc = qpnp_iadc_get_rsense(chip->iadc_dev, &rds_rsense_nohm);
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004058 if (rc) {
4059 pr_err("Unable to read RDS resistance value from IADC; rc = %d\n",
4060 rc);
4061 return rc;
4062 }
Xiaozhe Shid0a79542012-11-06 10:00:38 -08004063 chip->r_sense_uohm = rds_rsense_nohm/1000;
4064 pr_debug("rds_rsense = %d nOhm, saved as %d uOhm\n",
4065 rds_rsense_nohm, chip->r_sense_uohm);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004066 }
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004067 /* prevent shorting of leads by IADC_BMS when external Rsense is used */
4068 if (chip->use_external_rsense) {
4069 if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX) {
4070 rc = qpnp_masked_write_iadc(chip,
4071 IADC1_BMS_ADC_INT_RSNSN_CTL,
4072 ADC_INT_RSNSN_CTL_MASK,
4073 ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE);
4074 if (rc) {
4075 pr_err("Unable to set batfet config %x to %x: %d\n",
4076 IADC1_BMS_ADC_INT_RSNSN_CTL,
4077 ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE, rc);
4078 return rc;
4079 }
4080 } else {
4081 /* In older PMICS use FAST_AVG_EN register bit 7 */
4082 rc = qpnp_masked_write_iadc(chip,
4083 IADC1_BMS_FAST_AVG_EN,
4084 FAST_AVG_EN_MASK,
4085 FAST_AVG_EN_VALUE_EXT_RSENSE);
4086 if (rc) {
4087 pr_err("Unable to set batfet config %x to %x: %d\n",
4088 IADC1_BMS_FAST_AVG_EN,
4089 FAST_AVG_EN_VALUE_EXT_RSENSE, rc);
4090 return rc;
4091 }
4092 }
4093 }
4094
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004095 return 0;
4096}
4097
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004098static int refresh_die_temp_monitor(struct qpnp_bms_chip *chip)
4099{
4100 struct qpnp_vadc_result result;
4101 int rc;
4102
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07004103 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004104
4105 pr_debug("low = %lld, high = %lld\n",
4106 result.physical - chip->temperature_margin,
4107 result.physical + chip->temperature_margin);
4108 chip->die_temp_monitor_params.high_temp = result.physical
4109 + chip->temperature_margin;
4110 chip->die_temp_monitor_params.low_temp = result.physical
4111 - chip->temperature_margin;
4112 chip->die_temp_monitor_params.state_request =
4113 ADC_TM_HIGH_LOW_THR_ENABLE;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004114 return qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
4115 &chip->die_temp_monitor_params);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004116}
4117
4118static void btm_notify_die_temp(enum qpnp_tm_state state, void *ctx)
4119{
4120 struct qpnp_bms_chip *chip = ctx;
4121 struct qpnp_vadc_result result;
4122 int rc;
4123
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07004124 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004125
4126 if (state == ADC_TM_LOW_STATE)
4127 pr_debug("low state triggered\n");
4128 else if (state == ADC_TM_HIGH_STATE)
4129 pr_debug("high state triggered\n");
4130 pr_debug("die temp = %lld, raw = 0x%x\n",
4131 result.physical, result.adc_code);
4132 schedule_work(&chip->recalc_work);
4133 refresh_die_temp_monitor(chip);
4134}
4135
4136static int setup_die_temp_monitoring(struct qpnp_bms_chip *chip)
4137{
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004138 int rc;
4139
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004140 chip->die_temp_monitor_params.channel = DIE_TEMP;
4141 chip->die_temp_monitor_params.btm_ctx = (void *)chip;
4142 chip->die_temp_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
4143 chip->die_temp_monitor_params.threshold_notification =
4144 &btm_notify_die_temp;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004145 rc = refresh_die_temp_monitor(chip);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004146 if (rc) {
4147 pr_err("tm setup failed: %d\n", rc);
4148 return rc;
4149 }
4150 pr_debug("setup complete\n");
4151 return 0;
4152}
4153
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004154static int __devinit qpnp_bms_probe(struct spmi_device *spmi)
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004155{
4156 struct qpnp_bms_chip *chip;
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004157 bool warm_reset;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004158 int rc, vbatt;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004159
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07004160 chip = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_bms_chip),
4161 GFP_KERNEL);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004162
4163 if (chip == NULL) {
4164 pr_err("kzalloc() failed.\n");
4165 return -ENOMEM;
4166 }
4167
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004168 rc = bms_get_adc(chip, spmi);
4169 if (rc < 0)
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004170 goto error_read;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004171
Xiaozhe Shi3eaf0b62013-07-11 09:48:08 -07004172 mutex_init(&chip->bms_output_lock);
4173 mutex_init(&chip->last_ocv_uv_mutex);
4174 mutex_init(&chip->vbat_monitor_mutex);
4175 mutex_init(&chip->soc_invalidation_mutex);
4176 mutex_init(&chip->last_soc_mutex);
Xiaozhe Shibda84992013-09-05 10:39:11 -07004177 mutex_init(&chip->status_lock);
Xiaozhe Shi27375822013-08-22 11:40:15 -07004178 init_waitqueue_head(&chip->bms_wait_queue);
Xiaozhe Shi3eaf0b62013-07-11 09:48:08 -07004179
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004180 warm_reset = qpnp_pon_is_warm_reset();
4181 rc = warm_reset;
4182 if (rc < 0)
4183 goto error_read;
4184
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004185 rc = register_spmi(chip, spmi);
4186 if (rc) {
4187 pr_err("error registering spmi resource %d\n", rc);
4188 goto error_resource;
4189 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004190
4191 rc = qpnp_read_wrapper(chip, &chip->revision1,
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004192 chip->base + REVISION1, 1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004193 if (rc) {
4194 pr_err("error reading version register %d\n", rc);
4195 goto error_read;
4196 }
4197
4198 rc = qpnp_read_wrapper(chip, &chip->revision2,
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004199 chip->base + REVISION2, 1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004200 if (rc) {
4201 pr_err("Error reading version register %d\n", rc);
4202 goto error_read;
4203 }
Xiaozhe Shia045a562012-11-28 16:55:39 -08004204 pr_debug("BMS version: %hhu.%hhu\n", chip->revision2, chip->revision1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004205
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004206 rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision2,
4207 chip->iadc_base + REVISION2, 1);
4208 if (rc) {
4209 pr_err("Error reading version register %d\n", rc);
4210 goto error_read;
4211 }
4212
4213 rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision1,
4214 chip->iadc_base + REVISION1, 1);
4215 if (rc) {
4216 pr_err("Error reading version register %d\n", rc);
4217 goto error_read;
4218 }
4219 pr_debug("IADC_BMS version: %hhu.%hhu\n",
4220 chip->iadc_bms_revision2, chip->iadc_bms_revision1);
4221
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004222 rc = bms_read_properties(chip);
4223 if (rc) {
4224 pr_err("Unable to read all bms properties, rc = %d\n", rc);
4225 goto error_read;
4226 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004227
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004228 rc = read_iadc_channel_select(chip);
4229 if (rc) {
4230 pr_err("Unable to get iadc selected channel = %d\n", rc);
4231 goto error_read;
4232 }
4233
Xiaozhe Shibdf14742012-12-05 12:41:48 -08004234 if (chip->use_ocv_thresholds) {
4235 rc = set_ocv_voltage_thresholds(chip,
4236 chip->ocv_low_threshold_uv,
4237 chip->ocv_high_threshold_uv);
4238 if (rc) {
4239 pr_err("Could not set ocv voltage thresholds: %d\n",
4240 rc);
4241 goto error_read;
4242 }
4243 }
4244
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004245 rc = set_battery_data(chip);
4246 if (rc) {
4247 pr_err("Bad battery data %d\n", rc);
4248 goto error_read;
4249 }
4250
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004251 bms_initialize_constants(chip);
4252
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004253 wakeup_source_init(&chip->soc_wake_source.source, "qpnp_soc_wake");
Xiaozhe Shi4be85782013-02-22 17:33:40 -08004254 wake_lock_init(&chip->low_voltage_wake_lock, WAKE_LOCK_SUSPEND,
4255 "qpnp_low_voltage_lock");
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004256 wake_lock_init(&chip->cv_wake_lock, WAKE_LOCK_SUSPEND,
4257 "qpnp_cv_lock");
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004258 INIT_DELAYED_WORK(&chip->calculate_soc_delayed_work,
4259 calculate_soc_work);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08004260 INIT_WORK(&chip->recalc_work, recalculate_work);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07004261 INIT_WORK(&chip->batfet_open_work, batfet_open_work);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004262
Xiaozhe Shif9f99242013-08-29 12:27:50 -07004263 dev_set_drvdata(&spmi->dev, chip);
4264 device_init_wakeup(&spmi->dev, 1);
4265
4266 load_shutdown_data(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004267
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05304268 if (chip->enable_fcc_learning) {
Anirudh Ghayale0c02932013-07-08 16:26:35 +05304269 if (chip->battery_removed) {
4270 rc = discard_backup_fcc_data(chip);
4271 if (rc)
4272 pr_err("Could not discard backed-up FCC data\n");
4273 } else {
4274 rc = read_chgcycle_data_from_backup(chip);
4275 if (rc)
4276 pr_err("Unable to restore charge-cycle data\n");
4277
4278 rc = read_fcc_data_from_backup(chip);
4279 if (rc)
4280 pr_err("Unable to restore FCC-learning data\n");
4281 else
4282 attempt_learning_new_fcc(chip);
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05304283 }
4284 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05304285
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004286 rc = setup_vbat_monitoring(chip);
4287 if (rc < 0) {
4288 pr_err("failed to set up voltage notifications: %d\n", rc);
4289 goto error_setup;
Xiaozhe Shid5d21412013-02-06 17:14:41 -08004290 }
4291
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004292 rc = setup_die_temp_monitoring(chip);
4293 if (rc < 0) {
4294 pr_err("failed to set up die temp notifications: %d\n", rc);
4295 goto error_setup;
4296 }
4297
Xu Kai870f8e82014-01-16 19:21:01 +08004298 rc = bms_request_irqs(chip);
4299 if (rc) {
4300 pr_err("error requesting bms irqs, rc = %d\n", rc);
4301 goto error_setup;
4302 }
4303
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07004304 battery_insertion_check(chip);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07004305 batfet_status_check(chip);
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07004306 battery_status_check(chip);
4307
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004308 calculate_soc_work(&(chip->calculate_soc_delayed_work.work));
4309
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004310 /* setup & register the battery power supply */
4311 chip->bms_psy.name = "bms";
4312 chip->bms_psy.type = POWER_SUPPLY_TYPE_BMS;
4313 chip->bms_psy.properties = msm_bms_power_props;
4314 chip->bms_psy.num_properties = ARRAY_SIZE(msm_bms_power_props);
4315 chip->bms_psy.get_property = qpnp_bms_power_get_property;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004316 chip->bms_psy.external_power_changed =
4317 qpnp_bms_external_power_changed;
4318 chip->bms_psy.supplied_to = qpnp_bms_supplicants;
4319 chip->bms_psy.num_supplicants = ARRAY_SIZE(qpnp_bms_supplicants);
4320
4321 rc = power_supply_register(chip->dev, &chip->bms_psy);
4322
4323 if (rc < 0) {
4324 pr_err("power_supply_register bms failed rc = %d\n", rc);
4325 goto unregister_dc;
4326 }
4327
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07004328 chip->bms_psy_registered = true;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004329 vbatt = 0;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07004330 rc = get_battery_voltage(chip, &vbatt);
Xiaozhe Shi36458962013-02-06 16:19:57 -08004331 if (rc) {
4332 pr_err("error reading vbat_sns adc channel = %d, rc = %d\n",
4333 VBAT_SNS, rc);
4334 goto unregister_dc;
4335 }
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004336
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004337 pr_info("probe success: soc =%d vbatt = %d ocv = %d r_sense_uohm = %u warm_reset = %d\n",
4338 get_prop_bms_capacity(chip), vbatt, chip->last_ocv_uv,
4339 chip->r_sense_uohm, warm_reset);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004340 return 0;
4341
4342unregister_dc:
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07004343 chip->bms_psy_registered = false;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004344 power_supply_unregister(&chip->bms_psy);
4345error_setup:
4346 dev_set_drvdata(&spmi->dev, NULL);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004347 wakeup_source_trash(&chip->soc_wake_source.source);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08004348 wake_lock_destroy(&chip->low_voltage_wake_lock);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004349 wake_lock_destroy(&chip->cv_wake_lock);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004350error_resource:
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004351error_read:
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004352 return rc;
4353}
4354
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07004355static int qpnp_bms_remove(struct spmi_device *spmi)
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004356{
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004357 dev_set_drvdata(&spmi->dev, NULL);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004358 return 0;
4359}
4360
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004361static int bms_suspend(struct device *dev)
4362{
4363 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
4364
4365 cancel_delayed_work_sync(&chip->calculate_soc_delayed_work);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07004366 chip->was_charging_at_sleep = is_battery_charging(chip);
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004367 return 0;
4368}
4369
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004370static int bms_resume(struct device *dev)
4371{
4372 int rc;
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004373 int soc_calc_period;
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004374 int time_until_next_recalc = 0;
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004375 unsigned long time_since_last_recalc;
4376 unsigned long tm_now_sec;
4377 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
4378
4379 rc = get_current_time(&tm_now_sec);
4380 if (rc) {
4381 pr_err("Could not read current time: %d\n", rc);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004382 } else {
Xiaozhe Shicb487b12013-10-14 17:42:07 -07004383 soc_calc_period = get_calculation_delay_ms(chip);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004384 time_since_last_recalc = tm_now_sec - chip->last_recalc_time;
4385 pr_debug("Time since last recalc: %lu\n",
4386 time_since_last_recalc);
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004387 time_until_next_recalc = max(0, soc_calc_period
4388 - (int)(time_since_last_recalc * 1000));
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004389 }
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004390
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004391 if (time_until_next_recalc == 0)
4392 bms_stay_awake(&chip->soc_wake_source);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004393 schedule_delayed_work(&chip->calculate_soc_delayed_work,
4394 round_jiffies_relative(msecs_to_jiffies
4395 (time_until_next_recalc)));
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004396 return 0;
4397}
4398
4399static const struct dev_pm_ops qpnp_bms_pm_ops = {
4400 .resume = bms_resume,
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004401 .suspend = bms_suspend,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004402};
4403
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004404static struct spmi_driver qpnp_bms_driver = {
4405 .probe = qpnp_bms_probe,
4406 .remove = __devexit_p(qpnp_bms_remove),
4407 .driver = {
4408 .name = QPNP_BMS_DEV_NAME,
4409 .owner = THIS_MODULE,
4410 .of_match_table = qpnp_bms_match_table,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004411 .pm = &qpnp_bms_pm_ops,
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004412 },
4413};
4414
4415static int __init qpnp_bms_init(void)
4416{
4417 pr_info("QPNP BMS INIT\n");
4418 return spmi_driver_register(&qpnp_bms_driver);
4419}
4420
4421static void __exit qpnp_bms_exit(void)
4422{
4423 pr_info("QPNP BMS EXIT\n");
4424 return spmi_driver_unregister(&qpnp_bms_driver);
4425}
4426
4427module_init(qpnp_bms_init);
4428module_exit(qpnp_bms_exit);
4429
4430MODULE_DESCRIPTION("QPNP BMS Driver");
4431MODULE_LICENSE("GPL v2");
4432MODULE_ALIAS("platform:" QPNP_BMS_DEV_NAME);