blob: 73c7e62e99cee02b7a8f69040ccac452a0e5d271 [file] [log] [blame]
Xiaozhe Shi28f5dd52013-01-04 12:19:58 -08001/* Copyright (c) 2011-2013, 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 Shicd7e5302012-10-17 12:29:53 -070026#include <linux/qpnp/qpnp-adc.h>
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -070027#include <linux/qpnp/power-on.h>
Xiaozhe Shiaf203c22013-06-19 12:01:38 -070028#include <linux/of_batterydata.h>
Xiaozhe Shib19f7032012-08-16 12:14:16 -070029
Xiaozhe Shib19f7032012-08-16 12:14:16 -070030/* BMS Register Offsets */
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -070031#define REVISION1 0x0
32#define REVISION2 0x1
Xiaozhe Shib19f7032012-08-16 12:14:16 -070033#define BMS1_STATUS1 0x8
34#define BMS1_MODE_CTL 0X40
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070035/* Coulomb counter clear registers */
Xiaozhe Shib19f7032012-08-16 12:14:16 -070036#define BMS1_CC_DATA_CTL 0x42
Xiaozhe Shia045a562012-11-28 16:55:39 -080037#define BMS1_CC_CLEAR_CTL 0x43
Xiaozhe Shi20640b52013-01-03 11:49:30 -080038/* BMS Tolerances */
39#define BMS1_TOL_CTL 0X44
Xiaozhe Shib19f7032012-08-16 12:14:16 -070040/* OCV limit registers */
41#define BMS1_OCV_USE_LOW_LIMIT_THR0 0x48
42#define BMS1_OCV_USE_LOW_LIMIT_THR1 0x49
43#define BMS1_OCV_USE_HIGH_LIMIT_THR0 0x4A
44#define BMS1_OCV_USE_HIGH_LIMIT_THR1 0x4B
45#define BMS1_OCV_USE_LIMIT_CTL 0x4C
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -070046/* Delay control */
47#define BMS1_S1_DELAY_CTL 0x5A
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -080048/* OCV interrupt threshold */
49#define BMS1_OCV_THR0 0x50
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -070050#define BMS1_S2_SAMP_AVG_CTL 0x61
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -080051/* SW CC interrupt threshold */
52#define BMS1_SW_CC_THR0 0xA0
Xiaozhe Shib19f7032012-08-16 12:14:16 -070053/* OCV for r registers */
54#define BMS1_OCV_FOR_R_DATA0 0x80
Xiaozhe Shib19f7032012-08-16 12:14:16 -070055#define BMS1_VSENSE_FOR_R_DATA0 0x82
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070056/* Coulomb counter data */
Xiaozhe Shib19f7032012-08-16 12:14:16 -070057#define BMS1_CC_DATA0 0x8A
Xiaozhe Shif3da8622013-06-10 14:50:56 -070058/* Shadow Coulomb counter data */
59#define BMS1_SW_CC_DATA0 0xA8
Xiaozhe Shib19f7032012-08-16 12:14:16 -070060/* OCV for soc data */
61#define BMS1_OCV_FOR_SOC_DATA0 0x90
Xiaozhe Shib19f7032012-08-16 12:14:16 -070062#define BMS1_VSENSE_PON_DATA0 0x94
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070063#define BMS1_VSENSE_AVG_DATA0 0x98
Xiaozhe Shib19f7032012-08-16 12:14:16 -070064#define BMS1_VBAT_AVG_DATA0 0x9E
Xiaozhe Shib19f7032012-08-16 12:14:16 -070065/* Extra bms registers */
Xiaozhe Shi57058942013-03-27 16:54:54 -070066#define SOC_STORAGE_REG 0xB0
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070067#define IAVG_STORAGE_REG 0xB1
Anirudh Ghayale0c02932013-07-08 16:26:35 +053068#define BMS_FCC_COUNT 0xB2
69#define BMS_FCC_BASE_REG 0xB3 /* FCC updates - 0xB3 to 0xB7 */
70#define BMS_CHGCYL_BASE_REG 0xB8 /* FCC chgcyl - 0xB8 to 0xBC */
71#define CHARGE_INCREASE_STORAGE 0xBD
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053072#define CHARGE_CYCLE_STORAGE_LSB 0xBE /* LSB=0xBE, MSB=0xBF */
73
Xiaozhe Shic40b3972012-11-30 14:11:16 -080074/* IADC Channel Select */
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -070075#define IADC1_BMS_REVISION2 0x01
Xiaozhe Shic40b3972012-11-30 14:11:16 -080076#define IADC1_BMS_ADC_CH_SEL_CTL 0x48
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -070077#define IADC1_BMS_ADC_INT_RSNSN_CTL 0x49
78#define IADC1_BMS_FAST_AVG_EN 0x5B
Xiaozhe Shib19f7032012-08-16 12:14:16 -070079
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070080/* Configuration for saving of shutdown soc/iavg */
81#define IGNORE_SOC_TEMP_DECIDEG 50
82#define IAVG_STEP_SIZE_MA 50
83#define IAVG_START 600
Xiaozhe Shif5f966d2013-02-19 14:23:11 -080084#define IAVG_INVALID 0xFF
Xiaozhe Shic7cbd052013-03-29 12:03:11 -070085#define SOC_INVALID 0xFF
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;
133};
134
135struct bms_wakeup_source {
136 struct wakeup_source source;
137 unsigned long disabled;
138};
139
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700140struct qpnp_bms_chip {
141 struct device *dev;
142 struct power_supply bms_psy;
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -0700143 bool bms_psy_registered;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -0700144 struct power_supply *batt_psy;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700145 struct spmi_device *spmi;
146 u16 base;
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800147 u16 iadc_base;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700148
149 u8 revision1;
150 u8 revision2;
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -0700151
152 u8 iadc_bms_revision1;
153 u8 iadc_bms_revision2;
154
Xiaozhe Shid5d21412013-02-06 17:14:41 -0800155 int battery_present;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700156 int battery_status;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700157 bool batfet_closed;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800158 bool new_battery;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700159 bool done_charging;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800160 bool last_soc_invalid;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700161 /* platform data */
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -0800162 int r_sense_uohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700163 unsigned int v_cutoff_uv;
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -0800164 int max_voltage_uv;
165 int r_conn_mohm;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700166 int shutdown_soc_valid_limit;
167 int adjust_soc_low_threshold;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700168 int chg_term_ua;
Xiaozhe Shi73a65692012-09-18 17:51:57 -0700169 enum battery_type batt_type;
Xiaozhe Shi976618f2013-04-30 10:49:30 -0700170 unsigned int fcc_mah;
Xiaozhe Shi73a65692012-09-18 17:51:57 -0700171 struct single_row_lut *fcc_temp_lut;
172 struct single_row_lut *fcc_sf_lut;
173 struct pc_temp_ocv_lut *pc_temp_ocv_lut;
174 struct sf_lut *pc_sf_lut;
175 struct sf_lut *rbatt_sf_lut;
176 int default_rbatt_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -0700177 int rbatt_capacitive_mohm;
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -0700178 int rbatt_mohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700179
180 struct delayed_work calculate_soc_delayed_work;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800181 struct work_struct recalc_work;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700182 struct work_struct batfet_open_work;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700183
184 struct mutex bms_output_lock;
185 struct mutex last_ocv_uv_mutex;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700186 struct mutex vbat_monitor_mutex;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700187 struct mutex soc_invalidation_mutex;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700188 struct mutex last_soc_mutex;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700189
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800190 bool use_external_rsense;
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800191 bool use_ocv_thresholds;
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800192
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700193 bool ignore_shutdown_soc;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800194 bool shutdown_soc_invalid;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700195 int shutdown_soc;
196 int shutdown_iavg_ma;
197
Xiaozhe Shi4be85782013-02-22 17:33:40 -0800198 struct wake_lock low_voltage_wake_lock;
Xiaozhe Shi4be85782013-02-22 17:33:40 -0800199 int low_voltage_threshold;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700200 int low_soc_calc_threshold;
201 int low_soc_calculate_soc_ms;
202 int calculate_soc_ms;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800203 struct bms_wakeup_source soc_wake_source;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700204 struct wake_lock cv_wake_lock;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700205
Xiaozhe Shie118c692012-09-24 15:17:43 -0700206 uint16_t ocv_reading_at_100;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700207 uint16_t prev_last_good_ocv_raw;
208 int last_ocv_uv;
Xiaozhe Shicc48e992013-05-28 16:42:24 -0700209 int charging_adjusted_ocv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800210 int last_ocv_temp;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700211 int last_cc_uah;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700212 unsigned long last_soc_change_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700213 unsigned long tm_sec;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700214 unsigned long report_tm_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700215 bool first_time_calc_soc;
216 bool first_time_calc_uuc;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700217 int64_t software_cc_uah;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700218 int64_t software_shdw_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700219
220 int iavg_samples_ma[IAVG_SAMPLES];
221 int iavg_index;
222 int iavg_num_samples;
223 struct timespec t_soc_queried;
224 int last_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -0700225 int last_soc_est;
Xiaozhe Shicc137262013-03-10 06:21:41 -0700226 int last_soc_unbound;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700227 bool was_charging_at_sleep;
228 int charge_start_tm_sec;
229 int catch_up_time_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700230 struct single_row_lut *adjusted_fcc_temp_lut;
231
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700232 struct qpnp_adc_tm_btm_param vbat_monitor_params;
Xiaozhe Shi535494d2013-04-05 12:27:51 -0700233 struct qpnp_adc_tm_btm_param die_temp_monitor_params;
234 int temperature_margin;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700235 unsigned int vadc_v0625;
236 unsigned int vadc_v1250;
237
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700238 int prev_uuc_iavg_ma;
239 int prev_pc_unusable;
240 int ibat_at_cv_ua;
241 int soc_at_cv;
242 int prev_chg_soc;
243 int calculated_soc;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -0800244 int prev_voltage_based_soc;
245 bool use_voltage_soc;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700246 bool in_cv_range;
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800247
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800248 int prev_batt_terminal_uv;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -0700249 int high_ocv_correction_limit_uv;
250 int low_ocv_correction_limit_uv;
251 int flat_ocv_threshold_uv;
252 int hold_soc_est;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800253
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800254 int ocv_high_threshold_uv;
255 int ocv_low_threshold_uv;
Xiaozhe Shicdeee312012-12-18 15:10:18 -0800256 unsigned long last_recalc_time;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530257
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530258 struct fcc_sample *fcc_learning_samples;
259 u8 fcc_sample_count;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530260 int enable_fcc_learning;
261 int min_fcc_learning_soc;
262 int min_fcc_ocv_pc;
263 int min_fcc_learning_samples;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530264 int start_soc;
265 int end_soc;
266 int start_pc;
267 int start_cc_uah;
268 int start_real_soc;
269 int end_cc_uah;
270 uint16_t fcc_new_mah;
271 int fcc_new_batt_temp;
272 uint16_t charge_cycles;
273 u8 charge_increase;
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530274 int fcc_resolution;
275 bool battery_removed;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800276 struct bms_irq sw_cc_thr_irq;
277 struct bms_irq ocv_thr_irq;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700278 struct qpnp_vadc_chip *vadc_dev;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700279 struct qpnp_iadc_chip *iadc_dev;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -0700280 struct qpnp_adc_tm_chip *adc_tm_dev;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700281};
282
283static struct of_device_id qpnp_bms_match_table[] = {
284 { .compatible = QPNP_BMS_DEV_NAME },
285 {}
286};
287
288static char *qpnp_bms_supplicants[] = {
289 "battery"
290};
291
292static enum power_supply_property msm_bms_power_props[] = {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700293 POWER_SUPPLY_PROP_CAPACITY,
294 POWER_SUPPLY_PROP_CURRENT_NOW,
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -0700295 POWER_SUPPLY_PROP_RESISTANCE,
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -0700296 POWER_SUPPLY_PROP_CHARGE_COUNTER,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700297 POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700298 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +0530299 POWER_SUPPLY_PROP_CHARGE_FULL,
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +0530300 POWER_SUPPLY_PROP_CYCLE_COUNT,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700301};
302
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530303static int discard_backup_fcc_data(struct qpnp_bms_chip *chip);
304static void backup_charge_cycle(struct qpnp_bms_chip *chip);
305
Xiaozhe Shi20640b52013-01-03 11:49:30 -0800306static bool bms_reset;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -0800307
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700308static int qpnp_read_wrapper(struct qpnp_bms_chip *chip, u8 *val,
309 u16 base, int count)
310{
311 int rc;
312 struct spmi_device *spmi = chip->spmi;
313
314 rc = spmi_ext_register_readl(spmi->ctrl, spmi->sid, base, val, count);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700315 if (rc) {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700316 pr_err("SPMI read failed rc=%d\n", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700317 return rc;
318 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700319 return 0;
320}
321
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700322static int qpnp_write_wrapper(struct qpnp_bms_chip *chip, u8 *val,
323 u16 base, int count)
324{
325 int rc;
326 struct spmi_device *spmi = chip->spmi;
327
328 rc = spmi_ext_register_writel(spmi->ctrl, spmi->sid, base, val, count);
329 if (rc) {
330 pr_err("SPMI write failed rc=%d\n", rc);
331 return rc;
332 }
333 return 0;
334}
335
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800336static int qpnp_masked_write_base(struct qpnp_bms_chip *chip, u16 addr,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700337 u8 mask, u8 val)
338{
339 int rc;
340 u8 reg;
341
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800342 rc = qpnp_read_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700343 if (rc) {
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800344 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700345 return rc;
346 }
347 reg &= ~mask;
348 reg |= val & mask;
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800349 rc = qpnp_write_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700350 if (rc) {
351 pr_err("write failed addr = %03X, val = %02x, mask = %02x, reg = %02x, rc = %d\n",
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800352 addr, val, mask, reg, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700353 return rc;
354 }
355 return 0;
356}
357
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800358static int qpnp_masked_write_iadc(struct qpnp_bms_chip *chip, u16 addr,
359 u8 mask, u8 val)
360{
361 return qpnp_masked_write_base(chip, chip->iadc_base + addr, mask, val);
362}
363
364static int qpnp_masked_write(struct qpnp_bms_chip *chip, u16 addr,
365 u8 mask, u8 val)
366{
367 return qpnp_masked_write_base(chip, chip->base + addr, mask, val);
368}
369
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800370static void bms_stay_awake(struct bms_wakeup_source *source)
371{
372 if (__test_and_clear_bit(0, &source->disabled)) {
373 __pm_stay_awake(&source->source);
374 pr_debug("enabled source %s\n", source->source.name);
375 }
376}
377
378static void bms_relax(struct bms_wakeup_source *source)
379{
380 if (!__test_and_set_bit(0, &source->disabled)) {
381 __pm_relax(&source->source);
382 pr_debug("disabled source %s\n", source->source.name);
383 }
384}
385
386static void enable_bms_irq(struct bms_irq *irq)
387{
388 if (__test_and_clear_bit(0, &irq->disabled)) {
389 enable_irq(irq->irq);
390 pr_debug("enabled irq %d\n", irq->irq);
391 }
392}
393
394static void disable_bms_irq(struct bms_irq *irq)
395{
396 if (!__test_and_set_bit(0, &irq->disabled)) {
397 disable_irq(irq->irq);
398 pr_debug("disabled irq %d\n", irq->irq);
399 }
400}
401
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700402#define HOLD_OREG_DATA BIT(0)
403static int lock_output_data(struct qpnp_bms_chip *chip)
404{
405 int rc;
406
407 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL,
408 HOLD_OREG_DATA, HOLD_OREG_DATA);
409 if (rc) {
410 pr_err("couldnt lock bms output rc = %d\n", rc);
411 return rc;
412 }
413 return 0;
414}
415
416static int unlock_output_data(struct qpnp_bms_chip *chip)
417{
418 int rc;
419
420 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL, HOLD_OREG_DATA, 0);
421 if (rc) {
422 pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc);
423 return rc;
424 }
425 return 0;
426}
427
428#define V_PER_BIT_MUL_FACTOR 97656
429#define V_PER_BIT_DIV_FACTOR 1000
430#define VADC_INTRINSIC_OFFSET 0x6000
431
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800432static int vadc_reading_to_uv(int reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700433{
434 if (reading <= VADC_INTRINSIC_OFFSET)
435 return 0;
436
437 return (reading - VADC_INTRINSIC_OFFSET)
438 * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR;
439}
440
441#define VADC_CALIB_UV 625000
442#define VBATT_MUL_FACTOR 3
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800443static int adjust_vbatt_reading(struct qpnp_bms_chip *chip, int reading_uv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700444{
445 s64 numerator, denominator;
446
447 if (reading_uv == 0)
448 return 0;
449
450 /* don't adjust if not calibrated */
451 if (chip->vadc_v0625 == 0 || chip->vadc_v1250 == 0) {
452 pr_debug("No cal yet return %d\n",
453 VBATT_MUL_FACTOR * reading_uv);
454 return VBATT_MUL_FACTOR * reading_uv;
455 }
456
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700457 numerator = ((s64)reading_uv - chip->vadc_v0625) * VADC_CALIB_UV;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700458 denominator = (s64)chip->vadc_v1250 - chip->vadc_v0625;
459 if (denominator == 0)
460 return reading_uv * VBATT_MUL_FACTOR;
461 return (VADC_CALIB_UV + div_s64(numerator, denominator))
462 * VBATT_MUL_FACTOR;
463}
464
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800465static int convert_vbatt_uv_to_raw(struct qpnp_bms_chip *chip,
466 int unadjusted_vbatt)
467{
468 int scaled_vbatt = unadjusted_vbatt / VBATT_MUL_FACTOR;
469
470 if (scaled_vbatt <= 0)
471 return VADC_INTRINSIC_OFFSET;
472 return ((scaled_vbatt * V_PER_BIT_DIV_FACTOR) / V_PER_BIT_MUL_FACTOR)
473 + VADC_INTRINSIC_OFFSET;
474}
475
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700476static inline int convert_vbatt_raw_to_uv(struct qpnp_bms_chip *chip,
477 uint16_t reading)
478{
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700479 int64_t uv;
480 int rc;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700481
482 uv = vadc_reading_to_uv(reading);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700483 pr_debug("%u raw converted into %lld uv\n", reading, uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700484 uv = adjust_vbatt_reading(chip, uv);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700485 pr_debug("adjusted into %lld uv\n", uv);
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700486 rc = qpnp_vbat_sns_comp_result(chip->vadc_dev, &uv);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700487 if (rc)
488 pr_debug("could not compensate vbatt\n");
489 pr_debug("compensated into %lld uv\n", uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700490 return uv;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700491}
492
493#define CC_READING_RESOLUTION_N 542535
494#define CC_READING_RESOLUTION_D 100000
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -0700495static s64 cc_reading_to_uv(s64 reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700496{
497 return div_s64(reading * CC_READING_RESOLUTION_N,
498 CC_READING_RESOLUTION_D);
499}
500
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800501#define QPNP_ADC_GAIN_IDEAL 3291LL
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700502static s64 cc_adjust_for_gain(s64 uv, uint16_t gain)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700503{
504 s64 result_uv;
505
506 pr_debug("adjusting_uv = %lld\n", uv);
Xiaozhe Shi820a47a2012-11-27 13:23:27 -0800507 if (gain == 0) {
508 pr_debug("gain is %d, not adjusting\n", gain);
509 return uv;
510 }
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700511 pr_debug("adjusting by factor: %lld/%hu = %lld%%\n",
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800512 QPNP_ADC_GAIN_IDEAL, gain,
513 div_s64(QPNP_ADC_GAIN_IDEAL * 100LL, (s64)gain));
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700514
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800515 result_uv = div_s64(uv * QPNP_ADC_GAIN_IDEAL, (s64)gain);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700516 pr_debug("result_uv = %lld\n", result_uv);
517 return result_uv;
518}
519
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700520static s64 cc_reverse_adjust_for_gain(struct qpnp_bms_chip *chip, s64 uv)
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800521{
522 struct qpnp_iadc_calib calibration;
523 int gain;
524 s64 result_uv;
525
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700526 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800527 gain = (int)calibration.gain_raw - (int)calibration.offset_raw;
528
529 pr_debug("reverse adjusting_uv = %lld\n", uv);
530 if (gain == 0) {
531 pr_debug("gain is %d, not adjusting\n", gain);
532 return uv;
533 }
534 pr_debug("adjusting by factor: %hu/%lld = %lld%%\n",
535 gain, QPNP_ADC_GAIN_IDEAL,
536 div64_s64((s64)gain * 100LL,
537 (s64)QPNP_ADC_GAIN_IDEAL));
538
539 result_uv = div64_s64(uv * (s64)gain, QPNP_ADC_GAIN_IDEAL);
540 pr_debug("result_uv = %lld\n", result_uv);
541 return result_uv;
542}
543
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700544static int convert_vsense_to_uv(struct qpnp_bms_chip *chip,
545 int16_t reading)
546{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700547 struct qpnp_iadc_calib calibration;
548
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700549 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700550 return cc_adjust_for_gain(cc_reading_to_uv(reading),
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800551 calibration.gain_raw - calibration.offset_raw);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700552}
553
554static int read_vsense_avg(struct qpnp_bms_chip *chip, int *result_uv)
555{
556 int rc;
557 int16_t reading;
558
559 rc = qpnp_read_wrapper(chip, (u8 *)&reading,
560 chip->base + BMS1_VSENSE_AVG_DATA0, 2);
561
562 if (rc) {
563 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
564 return rc;
565 }
566
567 *result_uv = convert_vsense_to_uv(chip, reading);
568 return 0;
569}
570
571static int get_battery_current(struct qpnp_bms_chip *chip, int *result_ua)
572{
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700573 int rc, vsense_uv = 0;
574 int64_t temp_current;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700575
Xiaozhe Shid0a79542012-11-06 10:00:38 -0800576 if (chip->r_sense_uohm == 0) {
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700577 pr_err("r_sense is zero\n");
578 return -EINVAL;
579 }
580
581 mutex_lock(&chip->bms_output_lock);
582 lock_output_data(chip);
583 read_vsense_avg(chip, &vsense_uv);
584 unlock_output_data(chip);
585 mutex_unlock(&chip->bms_output_lock);
586
587 pr_debug("vsense_uv=%duV\n", vsense_uv);
588 /* cast for signed division */
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700589 temp_current = div_s64((vsense_uv * 1000000LL),
590 (int)chip->r_sense_uohm);
591
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700592 rc = qpnp_iadc_comp_result(chip->iadc_dev, &temp_current);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700593 if (rc)
594 pr_debug("error compensation failed: %d\n", rc);
595
596 *result_ua = temp_current;
597 pr_debug("err compensated ibat=%duA\n", *result_ua);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700598 return 0;
599}
600
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700601static int get_battery_voltage(struct qpnp_bms_chip *chip, int *result_uv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700602{
603 int rc;
604 struct qpnp_vadc_result adc_result;
605
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700606 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &adc_result);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700607 if (rc) {
608 pr_err("error reading adc channel = %d, rc = %d\n",
609 VBAT_SNS, rc);
610 return rc;
611 }
612 pr_debug("mvolts phy = %lld meas = 0x%llx\n", adc_result.physical,
613 adc_result.measurement);
614 *result_uv = (int)adc_result.physical;
615 return 0;
616}
617
Xiaozhe Shie118c692012-09-24 15:17:43 -0700618#define CC_36_BIT_MASK 0xFFFFFFFFFLL
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800619static uint64_t convert_s64_to_s36(int64_t raw64)
620{
621 return (uint64_t) raw64 & CC_36_BIT_MASK;
622}
623
624#define SIGN_EXTEND_36_TO_64_MASK (-1LL ^ CC_36_BIT_MASK)
625static int64_t convert_s36_to_s64(uint64_t raw36)
626{
627 raw36 = raw36 & CC_36_BIT_MASK;
628 /* convert 36 bit signed value into 64 signed value */
629 return (raw36 >> 35) == 0LL ?
630 raw36 : (SIGN_EXTEND_36_TO_64_MASK | raw36);
631}
632
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700633static int read_cc_raw(struct qpnp_bms_chip *chip, int64_t *reading,
634 int cc_type)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700635{
636 int64_t raw_reading;
637 int rc;
638
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700639 if (cc_type == SHDW_CC)
640 rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
641 chip->base + BMS1_SW_CC_DATA0, 5);
642 else
643 rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
644 chip->base + BMS1_CC_DATA0, 5);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700645 if (rc) {
646 pr_err("Error reading cc: rc = %d\n", rc);
647 return -ENXIO;
648 }
649
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800650 *reading = convert_s36_to_s64(raw_reading);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700651
652 return 0;
653}
654
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700655static int calib_vadc(struct qpnp_bms_chip *chip)
656{
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700657 int rc, raw_0625, raw_1250;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700658 struct qpnp_vadc_result result;
659
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700660 rc = qpnp_vadc_read(chip->vadc_dev, REF_625MV, &result);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700661 if (rc) {
662 pr_debug("vadc read failed with rc = %d\n", rc);
663 return rc;
664 }
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700665 raw_0625 = result.adc_code;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700666
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700667 rc = qpnp_vadc_read(chip->vadc_dev, REF_125V, &result);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700668 if (rc) {
669 pr_debug("vadc read failed with rc = %d\n", rc);
670 return rc;
671 }
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700672 raw_1250 = result.adc_code;
673 chip->vadc_v0625 = vadc_reading_to_uv(raw_0625);
674 chip->vadc_v1250 = vadc_reading_to_uv(raw_1250);
675 pr_debug("vadc calib: 0625 = %d raw (%d uv), 1250 = %d raw (%d uv)\n",
676 raw_0625, chip->vadc_v0625,
677 raw_1250, chip->vadc_v1250);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700678 return 0;
679}
680
Xiaozhe Shie118c692012-09-24 15:17:43 -0700681static void convert_and_store_ocv(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800682 struct raw_soc_params *raw,
683 int batt_temp)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700684{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700685 int rc;
686
687 pr_debug("prev_last_good_ocv_raw = %d, last_good_ocv_raw = %d\n",
688 chip->prev_last_good_ocv_raw,
689 raw->last_good_ocv_raw);
690 rc = calib_vadc(chip);
691 if (rc)
692 pr_err("Vadc reference voltage read failed, rc = %d\n", rc);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700693 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
694 raw->last_good_ocv_uv = convert_vbatt_raw_to_uv(chip,
695 raw->last_good_ocv_raw);
696 chip->last_ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800697 chip->last_ocv_temp = batt_temp;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700698 chip->software_cc_uah = 0;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700699 pr_debug("last_good_ocv_uv = %d\n", raw->last_good_ocv_uv);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700700}
701
Xiaozhe Shia045a562012-11-28 16:55:39 -0800702#define CLEAR_CC BIT(7)
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700703#define CLEAR_SHDW_CC BIT(6)
Xiaozhe Shia045a562012-11-28 16:55:39 -0800704/**
705 * reset both cc and sw-cc.
706 * note: this should only be ever called from one thread
707 * or there may be a race condition where CC is never enabled
708 * again
709 */
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700710static void reset_cc(struct qpnp_bms_chip *chip, u8 flags)
Xiaozhe Shia045a562012-11-28 16:55:39 -0800711{
712 int rc;
713
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700714 pr_debug("resetting cc manually with flags %hhu\n", flags);
715 mutex_lock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800716 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700717 flags,
718 flags);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800719 if (rc)
720 pr_err("cc reset failed: %d\n", rc);
721
722 /* wait for 100us for cc to reset */
723 udelay(100);
724
725 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700726 flags, 0);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800727 if (rc)
728 pr_err("cc reenable failed: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700729 mutex_unlock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800730}
731
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700732static int get_battery_status(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800733{
734 union power_supply_propval ret = {0,};
735
736 if (chip->batt_psy == NULL)
737 chip->batt_psy = power_supply_get_by_name("battery");
738 if (chip->batt_psy) {
739 /* if battery has been registered, use the status property */
740 chip->batt_psy->get_property(chip->batt_psy,
741 POWER_SUPPLY_PROP_STATUS, &ret);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700742 return ret.intval;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800743 }
744
745 /* Default to false if the battery power supply is not registered. */
746 pr_debug("battery power supply is not registered\n");
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700747 return POWER_SUPPLY_STATUS_UNKNOWN;
748}
749
750static bool is_battery_charging(struct qpnp_bms_chip *chip)
751{
752 return get_battery_status(chip) == POWER_SUPPLY_STATUS_CHARGING;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800753}
754
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700755static bool is_battery_full(struct qpnp_bms_chip *chip)
756{
757 return get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL;
758}
759
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700760static bool is_battery_present(struct qpnp_bms_chip *chip)
761{
762 union power_supply_propval ret = {0,};
763
764 if (chip->batt_psy == NULL)
765 chip->batt_psy = power_supply_get_by_name("battery");
766 if (chip->batt_psy) {
767 /* if battery has been registered, use the status property */
768 chip->batt_psy->get_property(chip->batt_psy,
769 POWER_SUPPLY_PROP_PRESENT, &ret);
770 return ret.intval;
771 }
772
773 /* Default to false if the battery power supply is not registered. */
774 pr_debug("battery power supply is not registered\n");
775 return false;
776}
777
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700778static bool is_batfet_closed(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800779{
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700780 union power_supply_propval ret = {0,};
781
782 if (chip->batt_psy == NULL)
783 chip->batt_psy = power_supply_get_by_name("battery");
784 if (chip->batt_psy) {
785 /* if battery has been registered, use the online property */
786 chip->batt_psy->get_property(chip->batt_psy,
787 POWER_SUPPLY_PROP_ONLINE, &ret);
788 return !!ret.intval;
789 }
790
791 /* Default to true if the battery power supply is not registered. */
792 pr_debug("battery power supply is not registered\n");
793 return true;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800794}
795
796static int get_simultaneous_batt_v_and_i(struct qpnp_bms_chip *chip,
797 int *ibat_ua, int *vbat_uv)
798{
799 struct qpnp_iadc_result i_result;
800 struct qpnp_vadc_result v_result;
801 enum qpnp_iadc_channels iadc_channel;
802 int rc;
803
804 iadc_channel = chip->use_external_rsense ?
805 EXTERNAL_RSENSE : INTERNAL_RSENSE;
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700806 if (is_battery_full(chip)) {
807 rc = get_battery_current(chip, ibat_ua);
808 if (rc) {
809 pr_err("bms current read failed with rc: %d\n", rc);
810 return rc;
811 }
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700812 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &v_result);
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700813 if (rc) {
814 pr_err("vadc read failed with rc: %d\n", rc);
815 return rc;
816 }
817 *vbat_uv = (int)v_result.physical;
818 } else {
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700819 rc = qpnp_iadc_vadc_sync_read(chip->iadc_dev,
820 iadc_channel, &i_result,
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700821 VBAT_SNS, &v_result);
822 if (rc) {
823 pr_err("adc sync read failed with rc: %d\n", rc);
824 return rc;
825 }
826 /*
827 * reverse the current read by the iadc, since the bms uses
828 * flipped battery current polarity.
829 */
830 *ibat_ua = -1 * (int)i_result.result_ua;
831 *vbat_uv = (int)v_result.physical;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800832 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800833
834 return 0;
835}
836
837static int estimate_ocv(struct qpnp_bms_chip *chip)
838{
839 int ibat_ua, vbat_uv, ocv_est_uv;
840 int rc;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -0700841 int rbatt_mohm = chip->default_rbatt_mohm + chip->r_conn_mohm
842 + chip->rbatt_capacitive_mohm;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800843
844 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
845 if (rc) {
846 pr_err("simultaneous failed rc = %d\n", rc);
847 return rc;
848 }
849
850 ocv_est_uv = vbat_uv + (ibat_ua * rbatt_mohm) / 1000;
851 pr_debug("estimated pon ocv = %d\n", ocv_est_uv);
852 return ocv_est_uv;
853}
854
855static void reset_for_new_battery(struct qpnp_bms_chip *chip, int batt_temp)
856{
857 chip->last_ocv_uv = estimate_ocv(chip);
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700858 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800859 chip->last_soc = -EINVAL;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700860 chip->last_soc_invalid = true;
861 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800862 chip->soc_at_cv = -EINVAL;
863 chip->shutdown_soc_invalid = true;
864 chip->shutdown_soc = 0;
865 chip->shutdown_iavg_ma = 0;
866 chip->prev_pc_unusable = -EINVAL;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700867 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700868 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700869 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800870 chip->last_cc_uah = INT_MIN;
871 chip->last_ocv_temp = batt_temp;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800872 chip->prev_batt_terminal_uv = 0;
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530873 if (chip->enable_fcc_learning) {
874 chip->adjusted_fcc_temp_lut = NULL;
875 chip->fcc_new_mah = -EINVAL;
876 /* reset the charge-cycle and charge-increase registers */
877 chip->charge_increase = 0;
878 chip->charge_cycles = 0;
879 backup_charge_cycle(chip);
880 /* discard all the FCC learnt data and reset the local table */
881 discard_backup_fcc_data(chip);
882 memset(chip->fcc_learning_samples, 0,
883 chip->min_fcc_learning_samples *
884 sizeof(struct fcc_sample));
885 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800886}
887
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -0800888#define OCV_RAW_UNINITIALIZED 0xFFFF
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -0700889#define MIN_OCV_UV 2000000
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700890static int read_soc_params_raw(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800891 struct raw_soc_params *raw,
892 int batt_temp)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700893{
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -0700894 bool warm_reset = false;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700895 int rc;
896
897 mutex_lock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800898
Xiaozhe Shie118c692012-09-24 15:17:43 -0700899 lock_output_data(chip);
900
901 rc = qpnp_read_wrapper(chip, (u8 *)&raw->last_good_ocv_raw,
902 chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
903 if (rc) {
904 pr_err("Error reading ocv: rc = %d\n", rc);
905 return -ENXIO;
906 }
907
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700908 rc = read_cc_raw(chip, &raw->cc, CC);
909 rc = read_cc_raw(chip, &raw->shdw_cc, SHDW_CC);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700910 if (rc) {
911 pr_err("Failed to read raw cc data, rc = %d\n", rc);
912 return rc;
913 }
914
915 unlock_output_data(chip);
916 mutex_unlock(&chip->bms_output_lock);
917
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -0800918 if (chip->prev_last_good_ocv_raw == OCV_RAW_UNINITIALIZED) {
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800919 convert_and_store_ocv(chip, raw, batt_temp);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -0700920 pr_debug("PON_OCV_UV = %d, cc = %llx\n",
921 chip->last_ocv_uv, raw->cc);
922 warm_reset = qpnp_pon_is_warm_reset();
923 if (raw->last_good_ocv_uv < MIN_OCV_UV
924 || warm_reset > 0) {
925 pr_debug("OCV is stale or bad, estimating new OCV.\n");
926 chip->last_ocv_uv = estimate_ocv(chip);
927 raw->last_good_ocv_uv = chip->last_ocv_uv;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700928 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -0700929 pr_debug("New PON_OCV_UV = %d, cc = %llx\n",
930 chip->last_ocv_uv, raw->cc);
931 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800932 } else if (chip->new_battery) {
933 /* if a new battery was inserted, estimate the ocv */
934 reset_for_new_battery(chip, batt_temp);
935 raw->cc = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700936 raw->shdw_cc = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800937 raw->last_good_ocv_uv = chip->last_ocv_uv;
938 chip->new_battery = false;
Xiaozhe Shi74548b92013-05-02 16:47:08 -0700939 } else if (chip->done_charging) {
940 chip->done_charging = false;
941 /* if we just finished charging, reset CC and fake 100% */
942 chip->ocv_reading_at_100 = raw->last_good_ocv_raw;
943 chip->last_ocv_uv = chip->max_voltage_uv;
944 raw->last_good_ocv_uv = chip->max_voltage_uv;
945 raw->cc = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700946 raw->shdw_cc = 0;
947 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi74548b92013-05-02 16:47:08 -0700948 chip->last_ocv_temp = batt_temp;
949 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700950 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi74548b92013-05-02 16:47:08 -0700951 chip->last_cc_uah = INT_MIN;
952 pr_debug("EOC Battery full ocv_reading = 0x%x\n",
953 chip->ocv_reading_at_100);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700954 } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) {
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800955 convert_and_store_ocv(chip, raw, batt_temp);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700956 /* forget the old cc value upon ocv */
Xiaozhe Shif36d2862013-01-04 10:17:35 -0800957 chip->last_cc_uah = INT_MIN;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700958 } else {
959 raw->last_good_ocv_uv = chip->last_ocv_uv;
960 }
961
Xiaozhe Shi74548b92013-05-02 16:47:08 -0700962 /* stop faking a high OCV if we get a new OCV */
963 if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw)
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -0800964 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
Xiaozhe Shi74548b92013-05-02 16:47:08 -0700965
Xiaozhe Shie118c692012-09-24 15:17:43 -0700966 pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n",
967 raw->last_good_ocv_raw, raw->last_good_ocv_uv);
968 pr_debug("cc_raw= 0x%llx\n", raw->cc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700969 return 0;
970}
971
Xiaozhe Shie118c692012-09-24 15:17:43 -0700972static int calculate_pc(struct qpnp_bms_chip *chip, int ocv_uv,
973 int batt_temp)
974{
975 int pc;
976
977 pc = interpolate_pc(chip->pc_temp_ocv_lut,
978 batt_temp / 10, ocv_uv / 1000);
979 pr_debug("pc = %u %% for ocv = %d uv batt_temp = %d\n",
980 pc, ocv_uv, batt_temp);
981 /* Multiply the initial FCC value by the scale factor. */
982 return pc;
983}
984
985static int calculate_fcc(struct qpnp_bms_chip *chip, int batt_temp)
986{
987 int fcc_uah;
988
989 if (chip->adjusted_fcc_temp_lut == NULL) {
990 /* interpolate_fcc returns a mv value. */
991 fcc_uah = interpolate_fcc(chip->fcc_temp_lut,
992 batt_temp) * 1000;
993 pr_debug("fcc = %d uAh\n", fcc_uah);
994 return fcc_uah;
995 } else {
996 return 1000 * interpolate_fcc(chip->adjusted_fcc_temp_lut,
997 batt_temp);
998 }
999}
1000
1001/* calculate remaining charge at the time of ocv */
1002static int calculate_ocv_charge(struct qpnp_bms_chip *chip,
1003 struct raw_soc_params *raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001004 int fcc_uah)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001005{
1006 int ocv_uv, pc;
1007
1008 ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001009 pc = calculate_pc(chip, ocv_uv, chip->last_ocv_temp);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001010 pr_debug("ocv_uv = %d pc = %d\n", ocv_uv, pc);
1011 return (fcc_uah * pc) / 100;
1012}
1013
Xiaozhe Shie118c692012-09-24 15:17:43 -07001014#define CC_READING_TICKS 56
1015#define SLEEP_CLK_HZ 32764
1016#define SECONDS_PER_HOUR 3600
1017
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001018static s64 cc_uv_to_pvh(s64 cc_uv)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001019{
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001020 /* Note that it is necessary need to multiply by 1000000 to convert
1021 * from uvh to pvh here.
1022 * However, the maximum Coulomb Counter value is 2^35, which can cause
1023 * an over flow.
1024 * Multiply by 100000 first to perserve as much precision as possible
1025 * then multiply by 10 after doing the division in order to avoid
1026 * overflow on the maximum Coulomb Counter value.
1027 */
1028 return div_s64(cc_uv * CC_READING_TICKS * 100000,
1029 SLEEP_CLK_HZ * SECONDS_PER_HOUR) * 10;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001030}
1031
1032/**
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001033 * calculate_cc() - converts a hardware coulomb counter reading into uah
Xiaozhe Shie118c692012-09-24 15:17:43 -07001034 * @chip: the bms chip pointer
1035 * @cc: the cc reading from bms h/w
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001036 * @cc_type: calcualte cc from regular or shadow coulomb counter
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001037 * @clear_cc: whether this function should clear the hardware counter
1038 * after reading
Xiaozhe Shie118c692012-09-24 15:17:43 -07001039 *
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001040 * Converts the 64 bit hardware coulomb counter into microamp-hour by taking
1041 * into account hardware resolution and adc errors.
1042 *
1043 * Return: the coulomb counter based charge in uAh (micro-amp hour)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001044 */
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001045static int calculate_cc(struct qpnp_bms_chip *chip, int64_t cc,
1046 int cc_type, int clear_cc)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001047{
Xiaozhe Shi4e376652012-10-25 12:38:50 -07001048 struct qpnp_iadc_calib calibration;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001049 struct qpnp_vadc_result result;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001050 int64_t cc_voltage_uv, cc_pvh, cc_uah, *software_counter;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001051 int rc;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001052
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001053 software_counter = cc_type == SHDW_CC ?
1054 &chip->software_shdw_cc_uah : &chip->software_cc_uah;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001055 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001056 if (rc) {
1057 pr_err("could not read pmic die temperature: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001058 return *software_counter;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001059 }
1060
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07001061 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001062 pr_debug("%scc = %lld, die_temp = %lld\n",
1063 cc_type == SHDW_CC ? "shdw_" : "",
1064 cc, result.physical);
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -07001065 cc_voltage_uv = cc_reading_to_uv(cc);
Xiaozhe Shi0c484932013-02-05 16:14:10 -08001066 cc_voltage_uv = cc_adjust_for_gain(cc_voltage_uv,
1067 calibration.gain_raw
1068 - calibration.offset_raw);
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001069 cc_pvh = cc_uv_to_pvh(cc_voltage_uv);
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001070 cc_uah = div_s64(cc_pvh, chip->r_sense_uohm);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07001071 rc = qpnp_iadc_comp_result(chip->iadc_dev, &cc_uah);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001072 if (rc)
1073 pr_debug("error compensation failed: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001074 if (clear_cc == RESET) {
1075 pr_debug("software_%scc = %lld, added cc_uah = %lld\n",
1076 cc_type == SHDW_CC ? "sw_" : "",
1077 *software_counter, cc_uah);
1078 *software_counter += cc_uah;
1079 reset_cc(chip, cc_type == SHDW_CC ? CLEAR_SHDW_CC : CLEAR_CC);
1080 return (int)*software_counter;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001081 } else {
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001082 pr_debug("software_%scc = %lld, cc_uah = %lld, total = %lld\n",
1083 cc_type == SHDW_CC ? "shdw_" : "",
1084 *software_counter, cc_uah,
1085 *software_counter + cc_uah);
1086 return *software_counter + cc_uah;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001087 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07001088}
1089
1090static int get_rbatt(struct qpnp_bms_chip *chip,
1091 int soc_rbatt_mohm, int batt_temp)
1092{
1093 int rbatt_mohm, scalefactor;
1094
1095 rbatt_mohm = chip->default_rbatt_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001096 if (chip->rbatt_sf_lut == NULL) {
1097 pr_debug("RBATT = %d\n", rbatt_mohm);
1098 return rbatt_mohm;
1099 }
1100 /* Convert the batt_temp to DegC from deciDegC */
1101 batt_temp = batt_temp / 10;
1102 scalefactor = interpolate_scalingfactor(chip->rbatt_sf_lut,
1103 batt_temp, soc_rbatt_mohm);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001104 rbatt_mohm = (rbatt_mohm * scalefactor) / 100;
1105
1106 rbatt_mohm += chip->r_conn_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07001107 rbatt_mohm += chip->rbatt_capacitive_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001108 return rbatt_mohm;
1109}
1110
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -07001111#define IAVG_MINIMAL_TIME 2
Xiaozhe Shie118c692012-09-24 15:17:43 -07001112static void calculate_iavg(struct qpnp_bms_chip *chip, int cc_uah,
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001113 int *iavg_ua, int delta_time_s)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001114{
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001115 int delta_cc_uah = 0;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001116
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -07001117 /*
1118 * use the battery current if called too quickly
1119 */
1120 if (delta_time_s < IAVG_MINIMAL_TIME
1121 || chip->last_cc_uah == INT_MIN) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001122 get_battery_current(chip, iavg_ua);
1123 goto out;
1124 }
1125
Xiaozhe Shie118c692012-09-24 15:17:43 -07001126 delta_cc_uah = cc_uah - chip->last_cc_uah;
1127
1128 *iavg_ua = div_s64((s64)delta_cc_uah * 3600, delta_time_s);
1129
Xiaozhe Shie118c692012-09-24 15:17:43 -07001130out:
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001131 pr_debug("delta_cc = %d iavg_ua = %d\n", delta_cc_uah, (int)*iavg_ua);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001132
1133 /* remember cc_uah */
1134 chip->last_cc_uah = cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001135}
1136
1137static int calculate_termination_uuc(struct qpnp_bms_chip *chip,
1138 struct soc_params *params,
1139 int batt_temp, int uuc_iavg_ma,
1140 int *ret_pc_unusable)
1141{
1142 int unusable_uv, pc_unusable, uuc_uah;
1143 int i = 0;
1144 int ocv_mv;
1145 int batt_temp_degc = batt_temp / 10;
1146 int rbatt_mohm;
1147 int delta_uv;
1148 int prev_delta_uv = 0;
1149 int prev_rbatt_mohm = 0;
1150 int uuc_rbatt_mohm;
1151
1152 for (i = 0; i <= 100; i++) {
1153 ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
1154 batt_temp_degc, i);
1155 rbatt_mohm = get_rbatt(chip, i, batt_temp);
1156 unusable_uv = (rbatt_mohm * uuc_iavg_ma)
1157 + (chip->v_cutoff_uv);
1158 delta_uv = ocv_mv * 1000 - unusable_uv;
1159
Xiaozhe Shie118c692012-09-24 15:17:43 -07001160 if (delta_uv > 0)
1161 break;
1162
1163 prev_delta_uv = delta_uv;
1164 prev_rbatt_mohm = rbatt_mohm;
1165 }
1166
1167 uuc_rbatt_mohm = linear_interpolate(rbatt_mohm, delta_uv,
1168 prev_rbatt_mohm, prev_delta_uv,
1169 0);
1170
1171 unusable_uv = (uuc_rbatt_mohm * uuc_iavg_ma) + (chip->v_cutoff_uv);
1172
1173 pc_unusable = calculate_pc(chip, unusable_uv, batt_temp);
1174 uuc_uah = (params->fcc_uah * pc_unusable) / 100;
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001175 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 -07001176 uuc_iavg_ma,
1177 uuc_rbatt_mohm, unusable_uv,
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001178 pc_unusable, i, uuc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001179 *ret_pc_unusable = pc_unusable;
1180 return uuc_uah;
1181}
1182
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001183#define TIME_PER_PERCENT_UUC 60
Xiaozhe Shie118c692012-09-24 15:17:43 -07001184static int adjust_uuc(struct qpnp_bms_chip *chip,
1185 struct soc_params *params,
1186 int new_pc_unusable,
1187 int new_uuc_uah,
1188 int batt_temp)
1189{
1190 int new_unusable_mv, new_iavg_ma;
1191 int batt_temp_degc = batt_temp / 10;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001192 int max_percent_change;
1193
1194 max_percent_change = max(params->delta_time_s
1195 / TIME_PER_PERCENT_UUC, 1);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001196
1197 if (chip->prev_pc_unusable == -EINVAL
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001198 || abs(chip->prev_pc_unusable - new_pc_unusable)
1199 <= max_percent_change) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001200 chip->prev_pc_unusable = new_pc_unusable;
1201 return new_uuc_uah;
1202 }
1203
1204 /* the uuc is trying to change more than 1% restrict it */
1205 if (new_pc_unusable > chip->prev_pc_unusable)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001206 chip->prev_pc_unusable += max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001207 else
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001208 chip->prev_pc_unusable -= max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001209
1210 new_uuc_uah = (params->fcc_uah * chip->prev_pc_unusable) / 100;
1211
1212 /* also find update the iavg_ma accordingly */
1213 new_unusable_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
1214 batt_temp_degc, chip->prev_pc_unusable);
1215 if (new_unusable_mv < chip->v_cutoff_uv/1000)
1216 new_unusable_mv = chip->v_cutoff_uv/1000;
1217
1218 new_iavg_ma = (new_unusable_mv * 1000 - chip->v_cutoff_uv)
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001219 / params->rbatt_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001220 if (new_iavg_ma == 0)
1221 new_iavg_ma = 1;
1222 chip->prev_uuc_iavg_ma = new_iavg_ma;
1223 pr_debug("Restricting UUC to %d (%d%%) unusable_mv = %d iavg_ma = %d\n",
1224 new_uuc_uah, chip->prev_pc_unusable,
1225 new_unusable_mv, new_iavg_ma);
1226
1227 return new_uuc_uah;
1228}
1229
Abhijeet Dharmapurikarbdf8ba82012-12-20 18:33:56 -08001230#define MIN_IAVG_MA 250
Xiaozhe Shie118c692012-09-24 15:17:43 -07001231#define MIN_SECONDS_FOR_VALID_SAMPLE 20
1232static int calculate_unusable_charge_uah(struct qpnp_bms_chip *chip,
1233 struct soc_params *params,
1234 int batt_temp)
1235{
1236 int uuc_uah_iavg;
1237 int i;
1238 int uuc_iavg_ma = params->iavg_ua / 1000;
1239 int pc_unusable;
1240
1241 /*
1242 * if called first time, fill all the samples with
1243 * the shutdown_iavg_ma
1244 */
1245 if (chip->first_time_calc_uuc && chip->shutdown_iavg_ma != 0) {
1246 pr_debug("Using shutdown_iavg_ma = %d in all samples\n",
1247 chip->shutdown_iavg_ma);
1248 for (i = 0; i < IAVG_SAMPLES; i++)
1249 chip->iavg_samples_ma[i] = chip->shutdown_iavg_ma;
1250
1251 chip->iavg_index = 0;
1252 chip->iavg_num_samples = IAVG_SAMPLES;
1253 }
1254
1255 /*
1256 * if charging use a nominal avg current to keep
1257 * a reasonable UUC while charging
1258 */
Abhijeet Dharmapurikarbdf8ba82012-12-20 18:33:56 -08001259 if (uuc_iavg_ma < MIN_IAVG_MA)
1260 uuc_iavg_ma = MIN_IAVG_MA;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001261 chip->iavg_samples_ma[chip->iavg_index] = uuc_iavg_ma;
1262 chip->iavg_index = (chip->iavg_index + 1) % IAVG_SAMPLES;
1263 chip->iavg_num_samples++;
1264 if (chip->iavg_num_samples >= IAVG_SAMPLES)
1265 chip->iavg_num_samples = IAVG_SAMPLES;
1266
1267 /* now that this sample is added calcualte the average */
1268 uuc_iavg_ma = 0;
1269 if (chip->iavg_num_samples != 0) {
1270 for (i = 0; i < chip->iavg_num_samples; i++) {
1271 pr_debug("iavg_samples_ma[%d] = %d\n", i,
1272 chip->iavg_samples_ma[i]);
1273 uuc_iavg_ma += chip->iavg_samples_ma[i];
1274 }
1275
1276 uuc_iavg_ma = DIV_ROUND_CLOSEST(uuc_iavg_ma,
1277 chip->iavg_num_samples);
1278 }
1279
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001280 /*
1281 * if we're in bms reset mode, force uuc to be 3% of fcc
1282 */
1283 if (bms_reset)
1284 return (params->fcc_uah * 3) / 100;
1285
Xiaozhe Shi75e5efe2013-02-07 09:51:43 -08001286 uuc_uah_iavg = calculate_termination_uuc(chip, params, batt_temp,
1287 uuc_iavg_ma, &pc_unusable);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001288 pr_debug("uuc_iavg_ma = %d uuc with iavg = %d\n",
1289 uuc_iavg_ma, uuc_uah_iavg);
1290
1291 chip->prev_uuc_iavg_ma = uuc_iavg_ma;
1292 /* restrict the uuc such that it can increase only by one percent */
1293 uuc_uah_iavg = adjust_uuc(chip, params, pc_unusable,
1294 uuc_uah_iavg, batt_temp);
1295
1296 chip->first_time_calc_uuc = 0;
1297 return uuc_uah_iavg;
1298}
1299
1300static void find_ocv_for_soc(struct qpnp_bms_chip *chip,
1301 struct soc_params *params,
1302 int batt_temp,
1303 int shutdown_soc,
1304 int *ret_ocv_uv)
1305{
1306 s64 ocv_charge_uah;
1307 int pc, new_pc;
1308 int batt_temp_degc = batt_temp / 10;
1309 int ocv_uv;
1310
1311 ocv_charge_uah = (s64)shutdown_soc
1312 * (params->fcc_uah - params->uuc_uah);
1313 ocv_charge_uah = div_s64(ocv_charge_uah, 100)
1314 + params->cc_uah + params->uuc_uah;
1315 pc = DIV_ROUND_CLOSEST((int)ocv_charge_uah * 100, params->fcc_uah);
1316 pc = clamp(pc, 0, 100);
1317
1318 ocv_uv = interpolate_ocv(chip->pc_temp_ocv_lut, batt_temp_degc, pc);
1319
1320 pr_debug("s_soc = %d, fcc = %d uuc = %d rc = %d, pc = %d, ocv mv = %d\n",
1321 shutdown_soc, params->fcc_uah,
1322 params->uuc_uah, (int)ocv_charge_uah,
1323 pc, ocv_uv);
1324 new_pc = interpolate_pc(chip->pc_temp_ocv_lut, batt_temp_degc, ocv_uv);
1325 pr_debug("test revlookup pc = %d for ocv = %d\n", new_pc, ocv_uv);
1326
1327 while (abs(new_pc - pc) > 1) {
1328 int delta_mv = 5;
1329
1330 if (new_pc > pc)
1331 delta_mv = -1 * delta_mv;
1332
1333 ocv_uv = ocv_uv + delta_mv;
1334 new_pc = interpolate_pc(chip->pc_temp_ocv_lut,
1335 batt_temp_degc, ocv_uv);
1336 pr_debug("test revlookup pc = %d for ocv = %d\n",
1337 new_pc, ocv_uv);
1338 }
1339
1340 *ret_ocv_uv = ocv_uv * 1000;
1341 params->ocv_charge_uah = (int)ocv_charge_uah;
1342}
1343
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001344static int get_current_time(unsigned long *now_tm_sec)
1345{
1346 struct rtc_time tm;
1347 struct rtc_device *rtc;
1348 int rc;
1349
1350 rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
1351 if (rtc == NULL) {
1352 pr_err("%s: unable to open rtc device (%s)\n",
1353 __FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
Xiaozhe Shi0e01af62013-05-06 12:56:08 -07001354 return -EINVAL;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001355 }
1356
1357 rc = rtc_read_time(rtc, &tm);
1358 if (rc) {
1359 pr_err("Error reading rtc device (%s) : %d\n",
1360 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1361 goto close_time;
1362 }
1363
1364 rc = rtc_valid_tm(&tm);
1365 if (rc) {
1366 pr_err("Invalid RTC time (%s): %d\n",
1367 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1368 goto close_time;
1369 }
1370 rtc_tm_to_time(&tm, now_tm_sec);
1371
1372close_time:
1373 rtc_class_close(rtc);
1374 return rc;
1375}
1376
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08001377/* Returns estimated battery resistance */
1378static int get_prop_bms_batt_resistance(struct qpnp_bms_chip *chip)
1379{
1380 return chip->rbatt_mohm * 1000;
1381}
1382
1383/* Returns instantaneous current in uA */
1384static int get_prop_bms_current_now(struct qpnp_bms_chip *chip)
1385{
1386 int rc, result_ua;
1387
1388 rc = get_battery_current(chip, &result_ua);
1389 if (rc) {
1390 pr_err("failed to get current: %d\n", rc);
1391 return rc;
1392 }
1393 return result_ua;
1394}
1395
1396/* Returns coulomb counter in uAh */
1397static int get_prop_bms_charge_counter(struct qpnp_bms_chip *chip)
1398{
1399 int64_t cc_raw;
1400
1401 mutex_lock(&chip->bms_output_lock);
1402 lock_output_data(chip);
1403 read_cc_raw(chip, &cc_raw, false);
1404 unlock_output_data(chip);
1405 mutex_unlock(&chip->bms_output_lock);
1406
1407 return calculate_cc(chip, cc_raw, CC, NORESET);
1408}
1409
1410/* Returns shadow coulomb counter in uAh */
1411static int get_prop_bms_charge_counter_shadow(struct qpnp_bms_chip *chip)
1412{
1413 int64_t cc_raw;
1414
1415 mutex_lock(&chip->bms_output_lock);
1416 lock_output_data(chip);
1417 read_cc_raw(chip, &cc_raw, true);
1418 unlock_output_data(chip);
1419 mutex_unlock(&chip->bms_output_lock);
1420
1421 return calculate_cc(chip, cc_raw, SHDW_CC, NORESET);
1422}
1423
1424/* Returns full charge design in uAh */
1425static int get_prop_bms_charge_full_design(struct qpnp_bms_chip *chip)
1426{
1427 return chip->fcc_mah * 1000;
1428}
1429
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05301430/* Returns the current full charge in uAh */
1431static int get_prop_bms_charge_full(struct qpnp_bms_chip *chip)
1432{
1433 int rc;
1434 struct qpnp_vadc_result result;
1435
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001436 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05301437 if (rc) {
1438 pr_err("Unable to read battery temperature\n");
1439 return rc;
1440 }
1441
1442 return calculate_fcc(chip, (int)result.physical);
1443}
1444
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001445static int calculate_delta_time(unsigned long *time_stamp, int *delta_time_s)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001446{
1447 unsigned long now_tm_sec = 0;
1448
1449 /* default to delta time = 0 if anything fails */
1450 *delta_time_s = 0;
1451
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001452 if (get_current_time(&now_tm_sec)) {
1453 pr_err("RTC read failed\n");
1454 return 0;
1455 }
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001456
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001457 *delta_time_s = (now_tm_sec - *time_stamp);
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001458
1459 /* remember this time */
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001460 *time_stamp = now_tm_sec;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001461 return 0;
1462}
1463
Xiaozhe Shie118c692012-09-24 15:17:43 -07001464static void calculate_soc_params(struct qpnp_bms_chip *chip,
1465 struct raw_soc_params *raw,
1466 struct soc_params *params,
1467 int batt_temp)
1468{
Xiaozhe Shi219cb222013-06-10 15:49:59 -07001469 int soc_rbatt, shdw_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001470
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001471 calculate_delta_time(&chip->tm_sec, &params->delta_time_s);
1472 pr_debug("tm_sec = %ld, delta_s = %d\n",
1473 chip->tm_sec, params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001474 params->fcc_uah = calculate_fcc(chip, batt_temp);
1475 pr_debug("FCC = %uuAh batt_temp = %d\n", params->fcc_uah, batt_temp);
1476
1477 /* calculate remainging charge */
1478 params->ocv_charge_uah = calculate_ocv_charge(
1479 chip, raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001480 params->fcc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001481 pr_debug("ocv_charge_uah = %uuAh\n", params->ocv_charge_uah);
1482
1483 /* calculate cc micro_volt_hour */
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001484 params->cc_uah = calculate_cc(chip, raw->cc, CC, RESET);
Xiaozhe Shi219cb222013-06-10 15:49:59 -07001485 shdw_cc_uah = calculate_cc(chip, raw->shdw_cc, SHDW_CC, RESET);
1486 pr_debug("cc_uah = %duAh raw->cc = %llx, shdw_cc_uah = %duAh raw->shdw_cc = %llx\n",
1487 params->cc_uah, raw->cc,
1488 shdw_cc_uah, raw->shdw_cc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001489
1490 soc_rbatt = ((params->ocv_charge_uah - params->cc_uah) * 100)
1491 / params->fcc_uah;
1492 if (soc_rbatt < 0)
1493 soc_rbatt = 0;
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001494 params->rbatt_mohm = get_rbatt(chip, soc_rbatt, batt_temp);
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001495 pr_debug("rbatt_mohm = %d\n", params->rbatt_mohm);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001496
Xiaozhe Shi1e87cda2013-05-17 10:18:56 -07001497 if (params->rbatt_mohm != chip->rbatt_mohm) {
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07001498 chip->rbatt_mohm = params->rbatt_mohm;
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07001499 if (chip->bms_psy_registered)
Xiaozhe Shi1e87cda2013-05-17 10:18:56 -07001500 power_supply_changed(&chip->bms_psy);
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07001501 }
1502
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001503 calculate_iavg(chip, params->cc_uah, &params->iavg_ua,
1504 params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001505
1506 params->uuc_uah = calculate_unusable_charge_uah(chip, params,
1507 batt_temp);
1508 pr_debug("UUC = %uuAh\n", params->uuc_uah);
1509}
1510
1511static bool is_shutdown_soc_within_limits(struct qpnp_bms_chip *chip, int soc)
1512{
1513 if (chip->shutdown_soc_invalid) {
1514 pr_debug("NOT forcing shutdown soc = %d\n", chip->shutdown_soc);
1515 return 0;
1516 }
1517
1518 if (abs(chip->shutdown_soc - soc) > chip->shutdown_soc_valid_limit) {
1519 pr_debug("rejecting shutdown soc = %d, soc = %d limit = %d\n",
1520 chip->shutdown_soc, soc,
1521 chip->shutdown_soc_valid_limit);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001522 chip->shutdown_soc_invalid = true;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001523 return 0;
1524 }
1525
1526 return 1;
1527}
1528
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001529static int bound_soc(int soc)
1530{
1531 soc = max(0, soc);
1532 soc = min(100, soc);
1533 return soc;
1534}
1535
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001536#define IBAT_TOL_MASK 0x0F
1537#define OCV_TOL_MASK 0xF0
1538#define IBAT_TOL_DEFAULT 0x03
1539#define IBAT_TOL_NOCHG 0x0F
1540#define OCV_TOL_DEFAULT 0x20
1541#define OCV_TOL_NO_OCV 0x00
1542static int stop_ocv_updates(struct qpnp_bms_chip *chip)
1543{
1544 pr_debug("stopping ocv updates\n");
1545 return qpnp_masked_write(chip, BMS1_TOL_CTL,
1546 OCV_TOL_MASK, OCV_TOL_NO_OCV);
1547}
1548
1549static int reset_bms_for_test(struct qpnp_bms_chip *chip)
1550{
Xiaozhe Shi95da77f2013-02-20 13:40:06 -08001551 int ibat_ua = 0, vbat_uv = 0, rc;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001552 int ocv_est_uv;
1553
1554 if (!chip) {
1555 pr_err("BMS driver has not been initialized yet!\n");
1556 return -EINVAL;
1557 }
1558
1559 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
1560
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07001561 /*
1562 * Don't include rbatt and rbatt_capacitative since we expect this to
1563 * be used with a fake battery which does not have internal resistances
1564 */
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001565 ocv_est_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
1566 pr_debug("forcing ocv to be %d due to bms reset mode\n", ocv_est_uv);
1567 chip->last_ocv_uv = ocv_est_uv;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001568 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001569 chip->last_soc = -EINVAL;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001570 chip->last_soc_invalid = true;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001571 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001572 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001573 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001574 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001575 chip->last_cc_uah = INT_MIN;
1576 stop_ocv_updates(chip);
1577
1578 pr_debug("bms reset to ocv = %duv vbat_ua = %d ibat_ua = %d\n",
1579 chip->last_ocv_uv, vbat_uv, ibat_ua);
1580
1581 return rc;
1582}
1583
1584static int bms_reset_set(const char *val, const struct kernel_param *kp)
1585{
1586 int rc;
1587
1588 rc = param_set_bool(val, kp);
1589 if (rc) {
1590 pr_err("Unable to set bms_reset: %d\n", rc);
1591 return rc;
1592 }
1593
1594 if (*(bool *)kp->arg) {
1595 struct power_supply *bms_psy = power_supply_get_by_name("bms");
1596 struct qpnp_bms_chip *chip = container_of(bms_psy,
1597 struct qpnp_bms_chip, bms_psy);
1598
1599 rc = reset_bms_for_test(chip);
1600 if (rc) {
1601 pr_err("Unable to modify bms_reset: %d\n", rc);
1602 return rc;
1603 }
1604 }
1605 return 0;
1606}
1607
1608static struct kernel_param_ops bms_reset_ops = {
1609 .set = bms_reset_set,
1610 .get = param_get_bool,
1611};
1612
1613module_param_cb(bms_reset, &bms_reset_ops, &bms_reset, 0644);
1614
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001615static void backup_soc_and_iavg(struct qpnp_bms_chip *chip, int batt_temp,
1616 int soc)
1617{
1618 u8 temp;
1619 int rc;
1620 int iavg_ma = chip->prev_uuc_iavg_ma;
1621
1622 if (iavg_ma > IAVG_START)
1623 temp = (iavg_ma - IAVG_START) / IAVG_STEP_SIZE_MA;
1624 else
1625 temp = 0;
1626
1627 rc = qpnp_write_wrapper(chip, &temp,
1628 chip->base + IAVG_STORAGE_REG, 1);
1629
1630 temp = soc;
1631
1632 /* don't store soc if temperature is below 5degC */
1633 if (batt_temp > IGNORE_SOC_TEMP_DECIDEG)
1634 rc = qpnp_write_wrapper(chip, &temp,
1635 chip->base + SOC_STORAGE_REG, 1);
1636}
1637
1638static int scale_soc_while_chg(struct qpnp_bms_chip *chip, int chg_time_sec,
1639 int catch_up_sec, int new_soc, int prev_soc)
1640{
1641 int scaled_soc;
1642 int numerator;
1643
1644 /*
1645 * Don't report a high value immediately slowly scale the
1646 * value from prev_soc to the new soc based on a charge time
1647 * weighted average
1648 */
1649 pr_debug("cts = %d catch_up_sec = %d\n", chg_time_sec, catch_up_sec);
1650 if (catch_up_sec == 0)
1651 return new_soc;
1652
1653 if (chg_time_sec > catch_up_sec)
1654 return new_soc;
1655
1656 numerator = (catch_up_sec - chg_time_sec) * prev_soc
1657 + chg_time_sec * new_soc;
1658 scaled_soc = numerator / catch_up_sec;
1659
1660 pr_debug("cts = %d new_soc = %d prev_soc = %d scaled_soc = %d\n",
1661 chg_time_sec, new_soc, prev_soc, scaled_soc);
1662
1663 return scaled_soc;
1664}
1665
1666/*
1667 * bms_fake_battery is set in setups where a battery emulator is used instead
1668 * of a real battery. This makes the bms driver report a different/fake value
1669 * regardless of the calculated state of charge.
1670 */
1671static int bms_fake_battery = -EINVAL;
1672module_param(bms_fake_battery, int, 0644);
1673
1674static int report_voltage_based_soc(struct qpnp_bms_chip *chip)
1675{
1676 pr_debug("Reported voltage based soc = %d\n",
1677 chip->prev_voltage_based_soc);
1678 return chip->prev_voltage_based_soc;
1679}
1680
1681#define SOC_CATCHUP_SEC_MAX 600
1682#define SOC_CATCHUP_SEC_PER_PERCENT 60
1683#define MAX_CATCHUP_SOC (SOC_CATCHUP_SEC_MAX / SOC_CATCHUP_SEC_PER_PERCENT)
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07001684#define SOC_CHANGE_PER_SEC 5
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001685static int report_cc_based_soc(struct qpnp_bms_chip *chip)
1686{
1687 int soc, soc_change;
1688 int time_since_last_change_sec, charge_time_sec = 0;
1689 unsigned long last_change_sec;
1690 struct timespec now;
1691 struct qpnp_vadc_result result;
1692 int batt_temp;
1693 int rc;
1694 bool charging, charging_since_last_report;
1695
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001696 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001697
1698 if (rc) {
1699 pr_err("error reading adc channel = %d, rc = %d\n",
1700 LR_MUX1_BATT_THERM, rc);
1701 return rc;
1702 }
1703 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
1704 result.measurement);
1705 batt_temp = (int)result.physical;
1706
1707 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shifa6ea692013-05-31 11:15:13 -07001708 soc = chip->calculated_soc;
1709
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001710 last_change_sec = chip->last_soc_change_sec;
1711 calculate_delta_time(&last_change_sec, &time_since_last_change_sec);
1712
1713 charging = is_battery_charging(chip);
1714 charging_since_last_report = charging || (chip->last_soc_unbound
1715 && chip->was_charging_at_sleep);
1716 /*
1717 * account for charge time - limit it to SOC_CATCHUP_SEC to
1718 * avoid overflows when charging continues for extended periods
1719 */
1720 if (charging) {
1721 if (chip->charge_start_tm_sec == 0) {
1722 /*
1723 * calculating soc for the first time
1724 * after start of chg. Initialize catchup time
1725 */
1726 if (abs(soc - chip->last_soc) < MAX_CATCHUP_SOC)
1727 chip->catch_up_time_sec =
1728 (soc - chip->last_soc)
1729 * SOC_CATCHUP_SEC_PER_PERCENT;
1730 else
1731 chip->catch_up_time_sec = SOC_CATCHUP_SEC_MAX;
1732
1733 if (chip->catch_up_time_sec < 0)
1734 chip->catch_up_time_sec = 0;
1735 chip->charge_start_tm_sec = last_change_sec;
1736 }
1737
1738 charge_time_sec = min(SOC_CATCHUP_SEC_MAX, (int)last_change_sec
1739 - chip->charge_start_tm_sec);
1740
1741 /* end catchup if calculated soc and last soc are same */
1742 if (chip->last_soc == soc)
1743 chip->catch_up_time_sec = 0;
1744 }
1745
1746 if (chip->last_soc != -EINVAL) {
1747 /*
1748 * last_soc < soc ... if we have not been charging at all
1749 * since the last time this was called, report previous SoC.
1750 * Otherwise, scale and catch up.
1751 */
1752 if (chip->last_soc < soc && !charging_since_last_report)
1753 soc = chip->last_soc;
1754 else if (chip->last_soc < soc && soc != 100)
1755 soc = scale_soc_while_chg(chip, charge_time_sec,
1756 chip->catch_up_time_sec,
1757 soc, chip->last_soc);
1758
1759 soc_change = min((int)abs(chip->last_soc - soc),
1760 time_since_last_change_sec / SOC_CHANGE_PER_SEC);
1761 if (chip->last_soc_unbound) {
1762 chip->last_soc_unbound = false;
1763 } else {
1764 /*
1765 * if soc have not been unbound by resume,
1766 * only change reported SoC by 1.
1767 */
1768 soc_change = min(1, soc_change);
1769 }
1770
1771 if (soc < chip->last_soc && soc != 0)
1772 soc = chip->last_soc - soc_change;
1773 if (soc > chip->last_soc && soc != 100)
1774 soc = chip->last_soc + soc_change;
1775 }
1776
Xiaozhe Shi208b8e52013-05-28 10:16:32 -07001777 if (chip->last_soc != soc && !chip->last_soc_unbound)
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001778 chip->last_soc_change_sec = last_change_sec;
1779
1780 pr_debug("last_soc = %d, calculated_soc = %d, soc = %d, time since last change = %d\n",
1781 chip->last_soc, chip->calculated_soc,
1782 soc, time_since_last_change_sec);
1783 chip->last_soc = bound_soc(soc);
1784 backup_soc_and_iavg(chip, batt_temp, chip->last_soc);
1785 pr_debug("Reported SOC = %d\n", chip->last_soc);
1786 chip->t_soc_queried = now;
1787 mutex_unlock(&chip->last_soc_mutex);
1788
1789 return soc;
1790}
1791
1792static int report_state_of_charge(struct qpnp_bms_chip *chip)
1793{
1794 if (bms_fake_battery != -EINVAL) {
1795 pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery);
1796 return bms_fake_battery;
1797 } else if (chip->use_voltage_soc)
1798 return report_voltage_based_soc(chip);
1799 else
1800 return report_cc_based_soc(chip);
1801}
1802
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001803#define VDD_MAX_ERR 5000
1804#define VDD_STEP_SIZE 10000
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001805static int charging_adjustments(struct qpnp_bms_chip *chip,
1806 struct soc_params *params, int soc,
1807 int vbat_uv, int ibat_ua, int batt_temp)
1808{
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001809 int chg_soc, soc_ibat, batt_terminal_uv, weight_ibat, weight_cc;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001810
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001811 batt_terminal_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001812
1813 if (chip->soc_at_cv == -EINVAL) {
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001814 if (batt_terminal_uv >= chip->max_voltage_uv - VDD_MAX_ERR) {
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001815 chip->soc_at_cv = soc;
1816 chip->prev_chg_soc = soc;
1817 chip->ibat_at_cv_ua = ibat_ua;
1818 pr_debug("CC_TO_CV ibat_ua = %d CHG SOC %d\n",
1819 ibat_ua, soc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001820 } else {
1821 /* In constant current charging return the calc soc */
1822 pr_debug("CC CHG SOC %d\n", soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001823 }
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001824
1825 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001826 return soc;
1827 }
1828
1829 /*
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001830 * battery is in CV phase - begin linear interpolation of soc based on
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001831 * battery charge current
1832 */
1833
1834 /*
1835 * if voltage lessened (possibly because of a system load)
1836 * keep reporting the prev chg soc
1837 */
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001838 if (batt_terminal_uv <= chip->prev_batt_terminal_uv - VDD_STEP_SIZE) {
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08001839 pr_debug("batt_terminal_uv %d < (max = %d - 10000); CC CHG SOC %d\n",
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001840 batt_terminal_uv, chip->prev_batt_terminal_uv,
1841 chip->prev_chg_soc);
1842 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001843 return chip->prev_chg_soc;
1844 }
1845
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001846 soc_ibat = bound_soc(linear_interpolate(chip->soc_at_cv,
1847 chip->ibat_at_cv_ua,
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08001848 100, -1 * chip->chg_term_ua,
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001849 ibat_ua));
1850 weight_ibat = bound_soc(linear_interpolate(1, chip->soc_at_cv,
1851 100, 100, chip->prev_chg_soc));
1852 weight_cc = 100 - weight_ibat;
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001853 chg_soc = bound_soc(DIV_ROUND_CLOSEST(soc_ibat * weight_ibat
1854 + weight_cc * soc, 100));
1855
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001856 pr_debug("weight_ibat = %d, weight_cc = %d, soc_ibat = %d, soc_cc = %d\n",
1857 weight_ibat, weight_cc, soc_ibat, soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001858
1859 /* always report a higher soc */
1860 if (chg_soc > chip->prev_chg_soc) {
1861 int new_ocv_uv;
1862
1863 chip->prev_chg_soc = chg_soc;
1864
1865 find_ocv_for_soc(chip, params, batt_temp, chg_soc, &new_ocv_uv);
Xiaozhe Shicc48e992013-05-28 16:42:24 -07001866 chip->charging_adjusted_ocv = new_ocv_uv;
1867 pr_debug("CC CHG ADJ OCV = %d CHG SOC %d\n", new_ocv_uv,
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001868 chip->prev_chg_soc);
1869 }
1870
1871 pr_debug("Reporting CHG SOC %d\n", chip->prev_chg_soc);
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001872 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001873 return chip->prev_chg_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001874}
1875
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001876static void very_low_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
1877{
1878 /*
1879 * if battery is very low (v_cutoff voltage + 20mv) hold
1880 * a wakelock untill soc = 0%
1881 */
1882 if (vbat_uv <= chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001883 && !wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001884 pr_debug("voltage = %d low holding wakelock\n", vbat_uv);
1885 wake_lock(&chip->low_voltage_wake_lock);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001886 } else if (vbat_uv > chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001887 && wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001888 pr_debug("voltage = %d releasing wakelock\n", vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001889 wake_unlock(&chip->low_voltage_wake_lock);
1890 }
1891}
1892
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001893#define VBATT_ERROR_MARGIN 20000
1894static void cv_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
1895{
1896 /*
1897 * if battery is very low (v_cutoff voltage + 20mv) hold
1898 * a wakelock untill soc = 0%
1899 */
1900 if (wake_lock_active(&chip->cv_wake_lock)) {
1901 if (chip->soc_at_cv != -EINVAL) {
1902 pr_debug("hit CV, releasing cv wakelock\n");
1903 wake_unlock(&chip->cv_wake_lock);
1904 } else if (!is_battery_charging(chip)) {
1905 pr_debug("charging stopped, releasing cv wakelock\n");
1906 wake_unlock(&chip->cv_wake_lock);
1907 }
1908 } else if (vbat_uv > chip->max_voltage_uv - VBATT_ERROR_MARGIN
1909 && chip->soc_at_cv == -EINVAL
1910 && is_battery_charging(chip)
1911 && !wake_lock_active(&chip->cv_wake_lock)) {
1912 pr_debug("voltage = %d holding cv wakelock\n", vbat_uv);
1913 wake_lock(&chip->cv_wake_lock);
1914 }
1915}
1916
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07001917#define NO_ADJUST_HIGH_SOC_THRESHOLD 90
Xiaozhe Shie118c692012-09-24 15:17:43 -07001918static int adjust_soc(struct qpnp_bms_chip *chip, struct soc_params *params,
1919 int soc, int batt_temp)
1920{
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001921 int ibat_ua = 0, vbat_uv = 0;
1922 int ocv_est_uv = 0, soc_est = 0, pc_est = 0, pc = 0;
1923 int delta_ocv_uv = 0;
1924 int n = 0;
1925 int rc_new_uah = 0;
1926 int pc_new = 0;
1927 int soc_new = 0;
1928 int slope = 0;
1929 int rc = 0;
1930 int delta_ocv_uv_limit = 0;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07001931 int correction_limit_uv = 0;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001932
1933 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
1934 if (rc < 0) {
1935 pr_err("simultaneous vbat ibat failed err = %d\n", rc);
1936 goto out;
1937 }
1938
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001939 very_low_voltage_check(chip, vbat_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001940 cv_voltage_check(chip, vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001941
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001942 delta_ocv_uv_limit = DIV_ROUND_CLOSEST(ibat_ua, 1000);
1943
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001944 ocv_est_uv = vbat_uv + (ibat_ua * params->rbatt_mohm)/1000;
1945
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001946 pc_est = calculate_pc(chip, ocv_est_uv, batt_temp);
1947 soc_est = div_s64((s64)params->fcc_uah * pc_est - params->uuc_uah*100,
1948 (s64)params->fcc_uah - params->uuc_uah);
1949 soc_est = bound_soc(soc_est);
1950
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001951 /* never adjust during bms reset mode */
1952 if (bms_reset) {
1953 pr_debug("bms reset mode, SOC adjustment skipped\n");
1954 goto out;
1955 }
1956
Xiaozhe Shi8658c982013-04-30 11:33:07 -07001957 if (ibat_ua < 0 && !is_battery_full(chip)) {
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001958 soc = charging_adjustments(chip, params, soc, vbat_uv, ibat_ua,
1959 batt_temp);
1960 goto out;
1961 }
1962
1963 /*
1964 * do not adjust
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07001965 * if soc_est is same as what bms calculated
1966 * OR if soc_est > adjust_soc_low_threshold
1967 * OR if soc is above 90
1968 * because we might pull it low
1969 * and cause a bad user experience
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001970 */
1971 if (soc_est == soc
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07001972 || soc_est > chip->adjust_soc_low_threshold
1973 || soc >= NO_ADJUST_HIGH_SOC_THRESHOLD)
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001974 goto out;
1975
1976 if (chip->last_soc_est == -EINVAL)
1977 chip->last_soc_est = soc;
1978
1979 n = min(200, max(1 , soc + soc_est + chip->last_soc_est));
1980 chip->last_soc_est = soc_est;
1981
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001982 pc = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001983 if (pc > 0) {
1984 pc_new = calculate_pc(chip,
1985 chip->last_ocv_uv - (++slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001986 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001987 while (pc_new == pc) {
1988 /* start taking 10mV steps */
1989 slope = slope + 10;
1990 pc_new = calculate_pc(chip,
1991 chip->last_ocv_uv - (slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001992 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001993 }
1994 } else {
1995 /*
1996 * pc is already at the lowest point,
1997 * assume 1 millivolt translates to 1% pc
1998 */
1999 pc = 1;
2000 pc_new = 0;
2001 slope = 1;
2002 }
2003
2004 delta_ocv_uv = div_s64((soc - soc_est) * (s64)slope * 1000,
2005 n * (pc - pc_new));
2006
2007 if (abs(delta_ocv_uv) > delta_ocv_uv_limit) {
2008 pr_debug("limiting delta ocv %d limit = %d\n", delta_ocv_uv,
2009 delta_ocv_uv_limit);
2010
2011 if (delta_ocv_uv > 0)
2012 delta_ocv_uv = delta_ocv_uv_limit;
2013 else
2014 delta_ocv_uv = -1 * delta_ocv_uv_limit;
2015 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
2016 }
2017
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002018 if (chip->last_ocv_uv > chip->flat_ocv_threshold_uv)
2019 correction_limit_uv = chip->high_ocv_correction_limit_uv;
2020 else
2021 correction_limit_uv = chip->low_ocv_correction_limit_uv;
2022
2023 if (abs(delta_ocv_uv) > correction_limit_uv) {
2024 pr_debug("limiting delta ocv %d limit = %d\n",
2025 delta_ocv_uv, correction_limit_uv);
2026 if (delta_ocv_uv > 0)
2027 delta_ocv_uv = correction_limit_uv;
2028 else
2029 delta_ocv_uv = -correction_limit_uv;
2030 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
2031 }
2032
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002033 chip->last_ocv_uv -= delta_ocv_uv;
2034
2035 if (chip->last_ocv_uv >= chip->max_voltage_uv)
2036 chip->last_ocv_uv = chip->max_voltage_uv;
2037
2038 /* calculate the soc based on this new ocv */
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002039 pc_new = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002040 rc_new_uah = (params->fcc_uah * pc_new) / 100;
2041 soc_new = (rc_new_uah - params->cc_uah - params->uuc_uah)*100
2042 / (params->fcc_uah - params->uuc_uah);
2043 soc_new = bound_soc(soc_new);
2044
2045 /*
2046 * if soc_new is ZERO force it higher so that phone doesnt report soc=0
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002047 * soc = 0 should happen only when soc_est is above a set value
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002048 */
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002049 if (soc_new == 0 && soc_est >= chip->hold_soc_est)
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002050 soc_new = 1;
2051
2052 soc = soc_new;
2053
2054out:
2055 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",
2056 ibat_ua, vbat_uv, ocv_est_uv, pc_est,
2057 soc_est, n, delta_ocv_uv, chip->last_ocv_uv,
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08002058 pc_new, soc_new, params->rbatt_mohm, slope);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002059
Xiaozhe Shie118c692012-09-24 15:17:43 -07002060 return soc;
2061}
2062
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002063static int clamp_soc_based_on_voltage(struct qpnp_bms_chip *chip, int soc)
2064{
2065 int rc, vbat_uv;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002066
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002067 rc = get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shi36458962013-02-06 16:19:57 -08002068 if (rc < 0) {
2069 pr_err("adc vbat failed err = %d\n", rc);
2070 return soc;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002071 }
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002072 if (soc == 0 && vbat_uv > chip->v_cutoff_uv) {
2073 pr_debug("clamping soc to 1, vbat (%d) > cutoff (%d)\n",
2074 vbat_uv, chip->v_cutoff_uv);
2075 return 1;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002076 } else {
2077 pr_debug("not clamping, using soc = %d, vbat = %d and cutoff = %d\n",
2078 soc, vbat_uv, chip->v_cutoff_uv);
2079 return soc;
2080 }
2081}
2082
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002083static int64_t convert_cc_uah_to_raw(struct qpnp_bms_chip *chip, int64_t cc_uah)
2084{
2085 int64_t cc_uv, cc_pvh, cc_raw;
2086
2087 cc_pvh = cc_uah * chip->r_sense_uohm;
2088 cc_uv = div_s64(cc_pvh * SLEEP_CLK_HZ * SECONDS_PER_HOUR,
2089 CC_READING_TICKS * 1000000LL);
2090 cc_raw = div_s64(cc_uv * CC_READING_RESOLUTION_D,
2091 CC_READING_RESOLUTION_N);
2092 return cc_raw;
2093}
2094
2095#define CC_STEP_INCREMENT_UAH 1500
2096#define OCV_STEP_INCREMENT 0x10
2097static void configure_soc_wakeup(struct qpnp_bms_chip *chip,
2098 struct soc_params *params,
2099 int batt_temp, int target_soc)
2100{
2101 int target_ocv_uv;
2102 int64_t target_cc_uah, cc_raw_64, current_shdw_cc_raw_64;
2103 int64_t current_shdw_cc_uah, iadc_comp_factor;
2104 uint64_t cc_raw, current_shdw_cc_raw;
2105 int16_t ocv_raw, current_ocv_raw;
2106
2107 current_shdw_cc_raw = 0;
2108 mutex_lock(&chip->bms_output_lock);
2109 lock_output_data(chip);
2110 qpnp_read_wrapper(chip, (u8 *)&current_ocv_raw,
2111 chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
2112 unlock_output_data(chip);
2113 mutex_unlock(&chip->bms_output_lock);
2114 current_shdw_cc_uah = get_prop_bms_charge_counter_shadow(chip);
2115 current_shdw_cc_raw_64 = convert_cc_uah_to_raw(chip,
2116 current_shdw_cc_uah);
2117
2118 /*
2119 * Calculate the target shadow coulomb counter threshold for when
2120 * the SoC changes.
2121 *
2122 * Since the BMS driver resets the shadow coulomb counter every
2123 * 20 seconds when the device is awake, calculate the threshold as
2124 * a delta from the current shadow coulomb count.
2125 */
2126 target_cc_uah = (100 - target_soc)
2127 * (params->fcc_uah - params->uuc_uah)
2128 / 100 - current_shdw_cc_uah;
2129 if (target_cc_uah < 0) {
2130 /*
2131 * If the target cc is below 0, that means we have already
2132 * passed the point where SoC should have fallen.
2133 * Set a wakeup in a few more mAh and check back again
2134 */
2135 target_cc_uah = CC_STEP_INCREMENT_UAH;
2136 }
2137 iadc_comp_factor = 100000;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002138 qpnp_iadc_comp_result(chip->iadc_dev, &iadc_comp_factor);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002139 target_cc_uah = div64_s64(target_cc_uah * 100000, iadc_comp_factor);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002140 target_cc_uah = cc_reverse_adjust_for_gain(chip, target_cc_uah);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002141 cc_raw_64 = convert_cc_uah_to_raw(chip, target_cc_uah);
2142 cc_raw = convert_s64_to_s36(cc_raw_64);
2143
2144 find_ocv_for_soc(chip, params, batt_temp, target_soc, &target_ocv_uv);
2145 ocv_raw = convert_vbatt_uv_to_raw(chip, target_ocv_uv);
2146
2147 /*
2148 * If the current_ocv_raw was updated since reaching 100% and is lower
2149 * than the calculated target ocv threshold, set the new target
2150 * threshold 1.5mAh lower in order to check if the SoC changed yet.
2151 */
2152 if (current_ocv_raw != chip->ocv_reading_at_100
2153 && current_ocv_raw < ocv_raw)
2154 ocv_raw = current_ocv_raw - OCV_STEP_INCREMENT;
2155
2156 qpnp_write_wrapper(chip, (u8 *)&cc_raw,
2157 chip->base + BMS1_SW_CC_THR0, 5);
2158 qpnp_write_wrapper(chip, (u8 *)&ocv_raw,
2159 chip->base + BMS1_OCV_THR0, 2);
2160
2161 pr_debug("current sw_cc_raw = 0x%llx, current ocv = 0x%hx\n",
2162 current_shdw_cc_raw, (uint16_t)current_ocv_raw);
2163 pr_debug("target_cc_uah = %lld, raw64 = 0x%llx, raw 36 = 0x%llx, ocv_raw = 0x%hx\n",
2164 target_cc_uah,
2165 (uint64_t)cc_raw_64, cc_raw,
2166 (uint16_t)ocv_raw);
2167}
2168
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002169#define SLEEP_RECALC_INTERVAL 3
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002170static int calculate_state_of_charge(struct qpnp_bms_chip *chip,
2171 struct raw_soc_params *raw,
2172 int batt_temp)
2173{
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07002174 int soc, new_ocv_uv, previous_soc;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002175 int shutdown_soc, new_calculated_soc, remaining_usable_charge_uah;
2176 struct soc_params params;
2177
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002178 if (!is_battery_present(chip)) {
Xiaozhe Shi026fa9b2013-03-22 17:00:50 -07002179 pr_debug("battery gone, reporting 100\n");
2180 new_calculated_soc = 100;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002181 goto done_calculating;
2182 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07002183 calculate_soc_params(chip, raw, &params, batt_temp);
2184 /* calculate remaining usable charge */
2185 remaining_usable_charge_uah = params.ocv_charge_uah
2186 - params.cc_uah
2187 - params.uuc_uah;
2188
2189 pr_debug("RUC = %duAh\n", remaining_usable_charge_uah);
2190 if (params.fcc_uah - params.uuc_uah <= 0) {
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002191 pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
Xiaozhe Shie118c692012-09-24 15:17:43 -07002192 params.fcc_uah,
2193 params.uuc_uah);
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002194 new_calculated_soc = 0;
2195 goto done_calculating;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002196 }
2197
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002198 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2199 (params.fcc_uah - params.uuc_uah));
2200
Xiaozhe Shie118c692012-09-24 15:17:43 -07002201 if (chip->first_time_calc_soc && soc < 0) {
2202 /*
2203 * first time calcualtion and the pon ocv is too low resulting
2204 * in a bad soc. Adjust ocv to get 0 soc
2205 */
2206 pr_debug("soc is %d, adjusting pon ocv to make it 0\n", soc);
2207 find_ocv_for_soc(chip, &params, batt_temp, 0, &new_ocv_uv);
2208 chip->last_ocv_uv = new_ocv_uv;
2209
2210 remaining_usable_charge_uah = params.ocv_charge_uah
2211 - params.cc_uah
2212 - params.uuc_uah;
2213
2214 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2215 (params.fcc_uah
2216 - params.uuc_uah));
2217 pr_debug("DONE for O soc is %d, pon ocv adjusted to %duV\n",
2218 soc, chip->last_ocv_uv);
2219 }
2220
2221 if (soc > 100)
2222 soc = 100;
2223
2224 if (soc < 0) {
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002225 pr_debug("bad rem_usb_chg = %d rem_chg %d, cc_uah %d, unusb_chg %d\n",
Xiaozhe Shie118c692012-09-24 15:17:43 -07002226 remaining_usable_charge_uah,
2227 params.ocv_charge_uah,
2228 params.cc_uah, params.uuc_uah);
2229
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002230 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 -07002231 chip->last_ocv_uv, batt_temp,
2232 params.fcc_uah, soc);
2233 soc = 0;
2234 }
2235
2236 mutex_lock(&chip->soc_invalidation_mutex);
2237 shutdown_soc = chip->shutdown_soc;
2238
2239 if (chip->first_time_calc_soc && soc != shutdown_soc
2240 && is_shutdown_soc_within_limits(chip, soc)) {
2241 /*
2242 * soc for the first time - use shutdown soc
2243 * to adjust pon ocv since it is a small percent away from
2244 * the real soc
2245 */
2246 pr_debug("soc = %d before forcing shutdown_soc = %d\n",
2247 soc, shutdown_soc);
2248 find_ocv_for_soc(chip, &params, batt_temp,
2249 shutdown_soc, &new_ocv_uv);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002250 chip->last_ocv_uv = new_ocv_uv;
2251
2252 remaining_usable_charge_uah = params.ocv_charge_uah
2253 - params.cc_uah
2254 - params.uuc_uah;
2255
2256 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2257 (params.fcc_uah
2258 - params.uuc_uah));
2259
2260 pr_debug("DONE for shutdown_soc = %d soc is %d, adjusted ocv to %duV\n",
2261 shutdown_soc, soc, chip->last_ocv_uv);
2262 }
2263 mutex_unlock(&chip->soc_invalidation_mutex);
2264
2265 pr_debug("SOC before adjustment = %d\n", soc);
2266 new_calculated_soc = adjust_soc(chip, &params, soc, batt_temp);
2267
Xiaozhe Shi445d2492013-03-27 18:10:18 -07002268 /* always clamp soc due to BMS hw/sw immaturities */
2269 new_calculated_soc = clamp_soc_based_on_voltage(chip,
2270 new_calculated_soc);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002271 /*
2272 * If the battery is full, configure the cc threshold so the system
2273 * wakes up after SoC changes
2274 */
2275 if (is_battery_full(chip))
2276 configure_soc_wakeup(chip, &params,
2277 batt_temp, bound_soc(new_calculated_soc - 1));
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002278done_calculating:
Xiaozhe Shifa6ea692013-05-31 11:15:13 -07002279 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07002280 previous_soc = chip->calculated_soc;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002281 chip->calculated_soc = new_calculated_soc;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002282 pr_debug("CC based calculated SOC = %d\n", chip->calculated_soc);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002283 if (chip->last_soc_invalid) {
2284 chip->last_soc_invalid = false;
2285 chip->last_soc = -EINVAL;
2286 }
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002287 /*
2288 * Check if more than a long time has passed since the last
2289 * calculation (more than n times compared to the soc recalculation
2290 * rate, where n is defined by SLEEP_RECALC_INTERVAL). If this is true,
2291 * then the system must have gone through a long sleep, and SoC can be
2292 * allowed to become unbounded by the last reported SoC
2293 */
2294 if (params.delta_time_s * 1000 >
2295 chip->calculate_soc_ms * SLEEP_RECALC_INTERVAL
2296 && !chip->first_time_calc_soc) {
2297 chip->last_soc_unbound = true;
2298 chip->last_soc_change_sec = chip->last_recalc_time;
2299 pr_debug("last_soc unbound because elapsed time = %d\n",
2300 params.delta_time_s);
2301 }
2302 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07002303
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002304 if (new_calculated_soc != previous_soc && chip->bms_psy_registered) {
Xiaozhe Shi83484e32013-05-16 10:59:59 -07002305 power_supply_changed(&chip->bms_psy);
2306 pr_debug("power supply changed\n");
2307 } else {
2308 /*
2309 * Call report state of charge anyways to periodically update
2310 * reported SoC. This prevents reported SoC from being stuck
2311 * when calculated soc doesn't change.
2312 */
2313 report_state_of_charge(chip);
2314 }
2315
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002316 get_current_time(&chip->last_recalc_time);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002317 chip->first_time_calc_soc = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002318 return chip->calculated_soc;
2319}
2320
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002321static int calculate_soc_from_voltage(struct qpnp_bms_chip *chip)
2322{
2323 int voltage_range_uv, voltage_remaining_uv, voltage_based_soc;
Xiaozhe Shi36458962013-02-06 16:19:57 -08002324 int rc, vbat_uv;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002325
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002326 rc = get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shi36458962013-02-06 16:19:57 -08002327 if (rc < 0) {
2328 pr_err("adc vbat failed err = %d\n", rc);
2329 return rc;
2330 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002331 voltage_range_uv = chip->max_voltage_uv - chip->v_cutoff_uv;
2332 voltage_remaining_uv = vbat_uv - chip->v_cutoff_uv;
2333 voltage_based_soc = voltage_remaining_uv * 100 / voltage_range_uv;
2334
2335 voltage_based_soc = clamp(voltage_based_soc, 0, 100);
2336
2337 if (chip->prev_voltage_based_soc != voltage_based_soc
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002338 && chip->bms_psy_registered) {
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002339 power_supply_changed(&chip->bms_psy);
2340 pr_debug("power supply changed\n");
2341 }
2342 chip->prev_voltage_based_soc = voltage_based_soc;
2343
2344 pr_debug("vbat used = %duv\n", vbat_uv);
2345 pr_debug("Calculated voltage based soc = %d\n", voltage_based_soc);
2346 return voltage_based_soc;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08002347}
2348
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002349static int recalculate_soc(struct qpnp_bms_chip *chip)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002350{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002351 int batt_temp, rc, soc;
2352 struct qpnp_vadc_result result;
2353 struct raw_soc_params raw;
2354
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002355 bms_stay_awake(&chip->soc_wake_source);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002356 mutex_lock(&chip->vbat_monitor_mutex);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002357 if (chip->vbat_monitor_params.state_request !=
2358 ADC_TM_HIGH_LOW_THR_DISABLE)
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002359 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2360 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002361 mutex_unlock(&chip->vbat_monitor_mutex);
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002362 if (chip->use_voltage_soc) {
2363 soc = calculate_soc_from_voltage(chip);
2364 } else {
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07002365 if (!chip->batfet_closed)
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002366 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, true);
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002367 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM,
2368 &result);
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002369 if (rc) {
2370 pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n",
2371 LR_MUX1_BATT_THERM, rc);
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002372 soc = chip->calculated_soc;
2373 } else {
2374 pr_debug("batt_temp phy = %lld meas = 0x%llx\n",
2375 result.physical,
2376 result.measurement);
2377 batt_temp = (int)result.physical;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002378
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002379 mutex_lock(&chip->last_ocv_uv_mutex);
2380 read_soc_params_raw(chip, &raw, batt_temp);
2381 soc = calculate_state_of_charge(chip, &raw, batt_temp);
2382 mutex_unlock(&chip->last_ocv_uv_mutex);
2383 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002384 }
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002385 bms_relax(&chip->soc_wake_source);
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002386 return soc;
2387}
2388
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002389static void recalculate_work(struct work_struct *work)
2390{
2391 struct qpnp_bms_chip *chip = container_of(work,
2392 struct qpnp_bms_chip,
2393 recalc_work);
2394
2395 recalculate_soc(chip);
2396}
2397
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002398static void calculate_soc_work(struct work_struct *work)
2399{
2400 struct qpnp_bms_chip *chip = container_of(work,
2401 struct qpnp_bms_chip,
2402 calculate_soc_delayed_work.work);
2403 int soc = recalculate_soc(chip);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002404
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002405 if (soc < chip->low_soc_calc_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002406 || wake_lock_active(&chip->low_voltage_wake_lock))
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002407 schedule_delayed_work(&chip->calculate_soc_delayed_work,
2408 round_jiffies_relative(msecs_to_jiffies
2409 (chip->low_soc_calculate_soc_ms)));
2410 else
2411 schedule_delayed_work(&chip->calculate_soc_delayed_work,
2412 round_jiffies_relative(msecs_to_jiffies
2413 (chip->calculate_soc_ms)));
2414}
2415
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002416static void configure_vbat_monitor_low(struct qpnp_bms_chip *chip)
2417{
2418 mutex_lock(&chip->vbat_monitor_mutex);
2419 if (chip->vbat_monitor_params.state_request
2420 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2421 /*
2422 * Battery is now around or below v_cutoff
2423 */
2424 pr_debug("battery entered cutoff range\n");
2425 if (!wake_lock_active(&chip->low_voltage_wake_lock)) {
2426 pr_debug("voltage low, holding wakelock\n");
2427 wake_lock(&chip->low_voltage_wake_lock);
2428 cancel_delayed_work_sync(
2429 &chip->calculate_soc_delayed_work);
2430 schedule_delayed_work(
2431 &chip->calculate_soc_delayed_work, 0);
2432 }
2433 chip->vbat_monitor_params.state_request =
2434 ADC_TM_HIGH_THR_ENABLE;
2435 chip->vbat_monitor_params.high_thr =
2436 (chip->low_voltage_threshold + VBATT_ERROR_MARGIN);
2437 pr_debug("set low thr to %d and high to %d\n",
2438 chip->vbat_monitor_params.low_thr,
2439 chip->vbat_monitor_params.high_thr);
2440 chip->vbat_monitor_params.low_thr = 0;
2441 } else if (chip->vbat_monitor_params.state_request
2442 == ADC_TM_LOW_THR_ENABLE) {
2443 /*
2444 * Battery is in normal operation range.
2445 */
2446 pr_debug("battery entered normal range\n");
2447 if (wake_lock_active(&chip->cv_wake_lock)) {
2448 wake_unlock(&chip->cv_wake_lock);
2449 pr_debug("releasing cv wake lock\n");
2450 }
2451 chip->in_cv_range = false;
2452 chip->vbat_monitor_params.state_request =
2453 ADC_TM_HIGH_LOW_THR_ENABLE;
2454 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2455 - VBATT_ERROR_MARGIN;
2456 chip->vbat_monitor_params.low_thr =
2457 chip->low_voltage_threshold;
2458 pr_debug("set low thr to %d and high to %d\n",
2459 chip->vbat_monitor_params.low_thr,
2460 chip->vbat_monitor_params.high_thr);
2461 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002462 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2463 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002464 mutex_unlock(&chip->vbat_monitor_mutex);
2465}
2466
2467#define CV_LOW_THRESHOLD_HYST_UV 100000
2468static void configure_vbat_monitor_high(struct qpnp_bms_chip *chip)
2469{
2470 mutex_lock(&chip->vbat_monitor_mutex);
2471 if (chip->vbat_monitor_params.state_request
2472 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2473 /*
2474 * Battery is around vddmax
2475 */
2476 pr_debug("battery entered vddmax range\n");
2477 chip->in_cv_range = true;
2478 if (!wake_lock_active(&chip->cv_wake_lock)) {
2479 wake_lock(&chip->cv_wake_lock);
2480 pr_debug("holding cv wake lock\n");
2481 }
2482 schedule_work(&chip->recalc_work);
2483 chip->vbat_monitor_params.state_request =
2484 ADC_TM_LOW_THR_ENABLE;
2485 chip->vbat_monitor_params.low_thr =
2486 (chip->max_voltage_uv - CV_LOW_THRESHOLD_HYST_UV);
2487 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv * 2;
2488 pr_debug("set low thr to %d and high to %d\n",
2489 chip->vbat_monitor_params.low_thr,
2490 chip->vbat_monitor_params.high_thr);
2491 } else if (chip->vbat_monitor_params.state_request
2492 == ADC_TM_HIGH_THR_ENABLE) {
2493 /*
2494 * Battery is in normal operation range.
2495 */
2496 pr_debug("battery entered normal range\n");
2497 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2498 pr_debug("voltage high, releasing wakelock\n");
2499 wake_unlock(&chip->low_voltage_wake_lock);
2500 }
2501 chip->vbat_monitor_params.state_request =
2502 ADC_TM_HIGH_LOW_THR_ENABLE;
2503 chip->vbat_monitor_params.high_thr =
2504 chip->max_voltage_uv - VBATT_ERROR_MARGIN;
2505 chip->vbat_monitor_params.low_thr =
2506 chip->low_voltage_threshold;
2507 pr_debug("set low thr to %d and high to %d\n",
2508 chip->vbat_monitor_params.low_thr,
2509 chip->vbat_monitor_params.high_thr);
2510 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002511 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2512 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002513 mutex_unlock(&chip->vbat_monitor_mutex);
2514}
2515
2516static void btm_notify_vbat(enum qpnp_tm_state state, void *ctx)
2517{
2518 struct qpnp_bms_chip *chip = ctx;
2519 int vbat_uv;
2520 struct qpnp_vadc_result result;
2521 int rc;
2522
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002523 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &result);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002524 pr_debug("vbat = %lld, raw = 0x%x\n", result.physical, result.adc_code);
2525
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002526 get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002527 pr_debug("vbat is at %d, state is at %d\n", vbat_uv, state);
2528
2529 if (state == ADC_TM_LOW_STATE) {
2530 pr_debug("low voltage btm notification triggered\n");
2531 if (vbat_uv - VBATT_ERROR_MARGIN
2532 < chip->vbat_monitor_params.low_thr) {
2533 configure_vbat_monitor_low(chip);
2534 } else {
2535 pr_debug("faulty btm trigger, discarding\n");
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002536 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002537 &chip->vbat_monitor_params);
2538 }
2539 } else if (state == ADC_TM_HIGH_STATE) {
2540 pr_debug("high voltage btm notification triggered\n");
2541 if (vbat_uv + VBATT_ERROR_MARGIN
2542 > chip->vbat_monitor_params.high_thr) {
2543 configure_vbat_monitor_high(chip);
2544 } else {
2545 pr_debug("faulty btm trigger, discarding\n");
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002546 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002547 &chip->vbat_monitor_params);
2548 }
2549 } else {
2550 pr_debug("unknown voltage notification state: %d\n", state);
2551 }
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002552 if (chip->bms_psy_registered)
Xiaozhe Shifa120db2013-06-06 15:57:19 -07002553 power_supply_changed(&chip->bms_psy);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002554}
2555
2556static int reset_vbat_monitoring(struct qpnp_bms_chip *chip)
2557{
2558 int rc;
2559
2560 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_DISABLE;
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002561
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002562 rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2563 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002564 if (rc) {
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002565 pr_err("tm disable failed: %d\n", rc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002566 return rc;
2567 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002568 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2569 pr_debug("battery removed, releasing wakelock\n");
2570 wake_unlock(&chip->low_voltage_wake_lock);
2571 }
2572 if (chip->in_cv_range) {
2573 pr_debug("battery removed, removing in_cv_range state\n");
2574 chip->in_cv_range = false;
2575 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002576 return 0;
2577}
2578
2579static int setup_vbat_monitoring(struct qpnp_bms_chip *chip)
2580{
2581 int rc;
2582
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002583 chip->vbat_monitor_params.low_thr = chip->low_voltage_threshold;
2584 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2585 - VBATT_ERROR_MARGIN;
2586 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_ENABLE;
2587 chip->vbat_monitor_params.channel = VBAT_SNS;
2588 chip->vbat_monitor_params.btm_ctx = (void *)chip;
2589 chip->vbat_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
2590 chip->vbat_monitor_params.threshold_notification = &btm_notify_vbat;
2591 pr_debug("set low thr to %d and high to %d\n",
2592 chip->vbat_monitor_params.low_thr,
2593 chip->vbat_monitor_params.high_thr);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002594
2595 if (!is_battery_present(chip)) {
2596 pr_debug("no battery inserted, do not enable vbat monitoring\n");
2597 chip->vbat_monitor_params.state_request =
2598 ADC_TM_HIGH_LOW_THR_DISABLE;
2599 } else {
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002600 rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2601 &chip->vbat_monitor_params);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002602 if (rc) {
2603 pr_err("tm setup failed: %d\n", rc);
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002604 return rc;
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002605 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002606 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002607
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002608 pr_debug("setup complete\n");
2609 return 0;
2610}
2611
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302612static void readjust_fcc_table(struct qpnp_bms_chip *chip)
2613{
2614 struct single_row_lut *temp, *old;
2615 int i, fcc, ratio;
2616
2617 if (!chip->enable_fcc_learning)
2618 return;
2619
2620 if (!chip->fcc_temp_lut) {
2621 pr_err("The static fcc lut table is NULL\n");
2622 return;
2623 }
2624
2625 temp = kzalloc(sizeof(struct single_row_lut), GFP_KERNEL);
2626 if (!temp) {
2627 pr_err("Cannot allocate memory for adjusted fcc table\n");
2628 return;
2629 }
2630
2631 fcc = interpolate_fcc(chip->fcc_temp_lut, chip->fcc_new_batt_temp);
2632
2633 temp->cols = chip->fcc_temp_lut->cols;
2634 for (i = 0; i < chip->fcc_temp_lut->cols; i++) {
2635 temp->x[i] = chip->fcc_temp_lut->x[i];
2636 ratio = div_u64(chip->fcc_temp_lut->y[i] * 1000, fcc);
2637 temp->y[i] = (ratio * chip->fcc_new_mah);
2638 temp->y[i] /= 1000;
2639 }
2640
2641 old = chip->adjusted_fcc_temp_lut;
2642 chip->adjusted_fcc_temp_lut = temp;
2643 kfree(old);
2644}
2645
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302646static int read_fcc_data_from_backup(struct qpnp_bms_chip *chip)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302647{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302648 int rc, i;
2649 u8 fcc = 0, chgcyl = 0;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302650
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302651 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2652 rc = qpnp_read_wrapper(chip, &fcc,
2653 chip->base + BMS_FCC_BASE_REG + i, 1);
2654 rc |= qpnp_read_wrapper(chip, &chgcyl,
2655 chip->base + BMS_CHGCYL_BASE_REG + i, 1);
2656 if (rc) {
2657 pr_err("Unable to read FCC data\n");
2658 return rc;
2659 }
2660 if (fcc == 0 || (fcc == 0xFF && chgcyl == 0xFF)) {
2661 /* FCC invalid/not present */
2662 chip->fcc_learning_samples[i].fcc_new = 0;
2663 chip->fcc_learning_samples[i].chargecycles = 0;
2664 } else {
2665 /* valid FCC data */
2666 chip->fcc_sample_count++;
2667 chip->fcc_learning_samples[i].fcc_new =
2668 fcc * chip->fcc_resolution;
2669 chip->fcc_learning_samples[i].chargecycles =
2670 chgcyl * CHGCYL_RESOLUTION;
2671 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302672 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302673
2674 return 0;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302675}
2676
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302677static int discard_backup_fcc_data(struct qpnp_bms_chip *chip)
2678{
2679 int rc = 0, i;
2680 u8 temp_u8 = 0;
2681
2682 chip->fcc_sample_count = 0;
2683 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2684 rc = qpnp_write_wrapper(chip, &temp_u8,
2685 chip->base + BMS_FCC_BASE_REG + i, 1);
2686 rc |= qpnp_write_wrapper(chip, &temp_u8,
2687 chip->base + BMS_CHGCYL_BASE_REG + i, 1);
2688 if (rc) {
2689 pr_err("Unable to clear FCC data\n");
2690 return rc;
2691 }
2692 }
2693
2694 return 0;
2695}
2696
2697static void
2698average_fcc_samples_and_readjust_fcc_table(struct qpnp_bms_chip *chip)
2699{
2700 int i, temp_fcc_avg = 0, temp_fcc_delta = 0, new_fcc_avg = 0;
2701 struct fcc_sample *ft;
2702
2703 for (i = 0; i < chip->min_fcc_learning_samples; i++)
2704 temp_fcc_avg += chip->fcc_learning_samples[i].fcc_new;
2705
2706 temp_fcc_avg /= chip->min_fcc_learning_samples;
2707 temp_fcc_delta = div_u64(temp_fcc_avg * DELTA_FCC_PERCENT, 100);
2708
2709 /* fix the fcc if its an outlier i.e. > 5% of the average */
2710 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2711 ft = &chip->fcc_learning_samples[i];
2712 if (abs(ft->fcc_new - temp_fcc_avg) > temp_fcc_delta)
2713 new_fcc_avg += temp_fcc_avg;
2714 else
2715 new_fcc_avg += ft->fcc_new;
2716 }
2717 new_fcc_avg /= chip->min_fcc_learning_samples;
2718
2719 chip->fcc_new_mah = new_fcc_avg;
2720 chip->fcc_new_batt_temp = FCC_DEFAULT_TEMP;
2721 pr_info("FCC update: New fcc_mah=%d, fcc_batt_temp=%d\n",
2722 new_fcc_avg, FCC_DEFAULT_TEMP);
2723 readjust_fcc_table(chip);
2724}
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302725
2726static void backup_charge_cycle(struct qpnp_bms_chip *chip)
2727{
2728 int rc = 0;
2729
2730 if (chip->charge_increase >= 0) {
2731 rc = qpnp_write_wrapper(chip, &chip->charge_increase,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302732 chip->base + CHARGE_INCREASE_STORAGE, 1);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302733 if (rc)
2734 pr_err("Unable to backup charge_increase\n");
2735 }
2736
2737 if (chip->charge_cycles >= 0) {
2738 rc = qpnp_write_wrapper(chip, (u8 *)&chip->charge_cycles,
2739 chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
2740 if (rc)
2741 pr_err("Unable to backup charge_cycles\n");
2742 }
2743}
2744
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302745static bool chargecycles_in_range(struct qpnp_bms_chip *chip)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302746{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302747 int i, min_cycle, max_cycle, valid_range;
2748
2749 /* find the smallest and largest charge cycle */
2750 max_cycle = min_cycle = chip->fcc_learning_samples[0].chargecycles;
2751 for (i = 1; i < chip->min_fcc_learning_samples; i++) {
2752 if (min_cycle > chip->fcc_learning_samples[i].chargecycles)
2753 min_cycle = chip->fcc_learning_samples[i].chargecycles;
2754 if (max_cycle < chip->fcc_learning_samples[i].chargecycles)
2755 max_cycle = chip->fcc_learning_samples[i].chargecycles;
2756 }
2757
2758 /* check if chargecyles are in range to continue with FCC update */
2759 valid_range = DIV_ROUND_UP(VALID_FCC_CHGCYL_RANGE,
2760 CHGCYL_RESOLUTION) * CHGCYL_RESOLUTION;
2761 if (abs(max_cycle - min_cycle) > valid_range)
2762 return false;
2763
2764 return true;
2765}
2766
2767static int read_chgcycle_data_from_backup(struct qpnp_bms_chip *chip)
2768{
2769 int rc;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302770 uint16_t temp_u16 = 0;
2771 u8 temp_u8 = 0;
2772
2773 rc = qpnp_read_wrapper(chip, &temp_u8,
2774 chip->base + CHARGE_INCREASE_STORAGE, 1);
2775 if (!rc && temp_u8 != 0xFF)
2776 chip->charge_increase = temp_u8;
2777
2778 rc = qpnp_read_wrapper(chip, (u8 *)&temp_u16,
2779 chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
2780 if (!rc && temp_u16 != 0xFFFF)
2781 chip->charge_cycles = temp_u16;
2782
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302783 return rc;
2784}
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302785
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302786static void
2787attempt_learning_new_fcc(struct qpnp_bms_chip *chip)
2788{
2789 pr_debug("Total FCC sample count=%d\n", chip->fcc_sample_count);
2790
2791 /* update FCC if we have the required samples */
2792 if ((chip->fcc_sample_count == chip->min_fcc_learning_samples) &&
2793 chargecycles_in_range(chip))
2794 average_fcc_samples_and_readjust_fcc_table(chip);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302795}
2796
2797static int calculate_real_soc(struct qpnp_bms_chip *chip,
2798 int batt_temp, struct raw_soc_params *raw, int cc_uah)
2799{
2800 int fcc_uah, rc_uah;
2801
2802 fcc_uah = calculate_fcc(chip, batt_temp);
2803 rc_uah = calculate_ocv_charge(chip, raw, fcc_uah);
2804
2805 return ((rc_uah - cc_uah) * 100) / fcc_uah;
2806}
2807
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302808#define MAX_U8_VALUE ((u8)(~0U))
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302809
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302810static int backup_new_fcc(struct qpnp_bms_chip *chip, int fcc_mah,
2811 int chargecycles)
2812{
2813 int rc, min_cycle, i;
2814 u8 fcc_new, chgcyl, pos = 0;
2815 struct fcc_sample *ft;
2816
2817 if ((fcc_mah > (chip->fcc_resolution * MAX_U8_VALUE)) ||
2818 (chargecycles > (CHGCYL_RESOLUTION * MAX_U8_VALUE))) {
2819 pr_warn("FCC/Chgcyl beyond storage limit. FCC=%d, chgcyl=%d\n",
2820 fcc_mah, chargecycles);
2821 return -EINVAL;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302822 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302823
2824 if (chip->fcc_sample_count == chip->min_fcc_learning_samples) {
2825 /* search best location - oldest entry */
2826 min_cycle = chip->fcc_learning_samples[0].chargecycles;
2827 for (i = 1; i < chip->min_fcc_learning_samples; i++) {
2828 if (min_cycle >
2829 chip->fcc_learning_samples[i].chargecycles)
2830 pos = i;
2831 }
2832 } else {
2833 /* find an empty location */
2834 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2835 ft = &chip->fcc_learning_samples[i];
2836 if (ft->fcc_new == 0 || (ft->fcc_new == 0xFF &&
2837 ft->chargecycles == 0xFF)) {
2838 pos = i;
2839 break;
2840 }
2841 }
2842 chip->fcc_sample_count++;
2843 }
2844 chip->fcc_learning_samples[pos].fcc_new = fcc_mah;
2845 chip->fcc_learning_samples[pos].chargecycles = chargecycles;
2846
2847 fcc_new = DIV_ROUND_UP(fcc_mah, chip->fcc_resolution);
2848 rc = qpnp_write_wrapper(chip, (u8 *)&fcc_new,
2849 chip->base + BMS_FCC_BASE_REG + pos, 1);
2850 if (rc)
2851 return rc;
2852
2853 chgcyl = DIV_ROUND_UP(chargecycles, CHGCYL_RESOLUTION);
2854 rc = qpnp_write_wrapper(chip, (u8 *)&chgcyl,
2855 chip->base + BMS_CHGCYL_BASE_REG + pos, 1);
2856 if (rc)
2857 return rc;
2858
2859 pr_debug("Backup new FCC: fcc_new=%d, chargecycle=%d, pos=%d\n",
2860 fcc_new, chgcyl, pos);
2861
2862 return rc;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302863}
2864
2865static void update_fcc_learning_table(struct qpnp_bms_chip *chip,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302866 int new_fcc_uah, int chargecycles, int batt_temp)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302867{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302868 int rc, fcc_default, fcc_temp;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302869
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302870 /* convert the fcc at batt_temp to new fcc at FCC_DEFAULT_TEMP */
2871 fcc_default = calculate_fcc(chip, FCC_DEFAULT_TEMP) / 1000;
2872 fcc_temp = calculate_fcc(chip, batt_temp) / 1000;
2873 new_fcc_uah = (new_fcc_uah / fcc_temp) * fcc_default;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302874
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302875 rc = backup_new_fcc(chip, new_fcc_uah / 1000, chargecycles);
2876 if (rc) {
2877 pr_err("Unable to backup new FCC\n");
2878 return;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302879 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302880 /* check if FCC can be updated */
2881 attempt_learning_new_fcc(chip);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302882}
2883
2884static bool is_new_fcc_valid(int new_fcc_uah, int fcc_uah)
2885{
2886 if ((new_fcc_uah >= (fcc_uah / 2)) &&
2887 ((new_fcc_uah * 100) <= (fcc_uah * 105)))
2888 return true;
2889
2890 pr_debug("FCC rejected - not within valid limit\n");
2891 return false;
2892}
2893
2894static void fcc_learning_config(struct qpnp_bms_chip *chip, bool start)
2895{
2896 int rc, batt_temp;
2897 struct raw_soc_params raw;
2898 struct qpnp_vadc_result result;
2899 int fcc_uah, new_fcc_uah, delta_cc_uah, delta_soc;
2900
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002901 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302902 if (rc) {
2903 pr_err("Unable to read batt_temp\n");
2904 return;
2905 } else {
2906 batt_temp = (int)result.physical;
2907 }
2908
2909 rc = read_soc_params_raw(chip, &raw, batt_temp);
2910 if (rc) {
2911 pr_err("Unable to read CC, cannot update FCC\n");
2912 return;
2913 }
2914
2915 if (start) {
2916 chip->start_pc = interpolate_pc(chip->pc_temp_ocv_lut,
2917 batt_temp / 10, raw.last_good_ocv_uv / 1000);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07002918 chip->start_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302919 chip->start_real_soc = calculate_real_soc(chip,
2920 batt_temp, &raw, chip->start_cc_uah);
2921 pr_debug("start_pc=%d, start_cc=%d, start_soc=%d real_soc=%d\n",
2922 chip->start_pc, chip->start_cc_uah,
2923 chip->start_soc, chip->start_real_soc);
2924 } else {
Xiaozhe Shif3da8622013-06-10 14:50:56 -07002925 chip->end_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302926 delta_soc = 100 - chip->start_real_soc;
2927 delta_cc_uah = abs(chip->end_cc_uah - chip->start_cc_uah);
2928 new_fcc_uah = div_u64(delta_cc_uah * 100, delta_soc);
2929 fcc_uah = calculate_fcc(chip, batt_temp);
2930 pr_debug("start_soc=%d, start_pc=%d, start_real_soc=%d, start_cc=%d, end_cc=%d, new_fcc=%d\n",
2931 chip->start_soc, chip->start_pc, chip->start_real_soc,
2932 chip->start_cc_uah, chip->end_cc_uah, new_fcc_uah);
2933
2934 if (is_new_fcc_valid(new_fcc_uah, fcc_uah))
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302935 update_fcc_learning_table(chip, new_fcc_uah,
2936 chip->charge_cycles, batt_temp);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302937 }
2938}
2939
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07002940#define MAX_CAL_TRIES 200
2941#define MIN_CAL_UA 3000
2942static void batfet_open_work(struct work_struct *work)
2943{
2944 int i;
2945 int rc;
2946 int result_ua;
2947 u8 orig_delay, sample_delay;
2948 struct qpnp_bms_chip *chip = container_of(work,
2949 struct qpnp_bms_chip,
2950 batfet_open_work);
2951
2952 rc = qpnp_read_wrapper(chip, &orig_delay,
2953 chip->base + BMS1_S1_DELAY_CTL, 1);
2954
2955 sample_delay = 0x0;
2956 rc = qpnp_write_wrapper(chip, &sample_delay,
2957 chip->base + BMS1_S1_DELAY_CTL, 1);
2958
2959 /*
2960 * In certain PMICs there is a coupling issue which causes
2961 * bad calibration value that result in a huge battery current
2962 * even when the BATFET is open. Do continious calibrations until
2963 * we hit reasonable cal values which result in low battery current
2964 */
2965
2966 for (i = 0; (!chip->batfet_closed) && i < MAX_CAL_TRIES; i++) {
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002967 rc = qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07002968 /*
2969 * Wait 20mS after calibration and before reading battery
2970 * current. The BMS h/w uses calibration values in the
2971 * next sampling of vsense.
2972 */
2973 msleep(20);
2974 rc |= get_battery_current(chip, &result_ua);
2975 if (rc == 0 && abs(result_ua) <= MIN_CAL_UA) {
2976 pr_debug("good cal at %d attempt\n", i);
2977 break;
2978 }
2979 }
2980 pr_debug("batfet_closed = %d i = %d result_ua = %d\n",
2981 chip->batfet_closed, i, result_ua);
2982
2983 rc = qpnp_write_wrapper(chip, &orig_delay,
2984 chip->base + BMS1_S1_DELAY_CTL, 1);
2985}
2986
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07002987static void charging_began(struct qpnp_bms_chip *chip)
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002988{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07002989 mutex_lock(&chip->last_soc_mutex);
2990 chip->charge_start_tm_sec = 0;
2991 chip->catch_up_time_sec = 0;
2992 mutex_unlock(&chip->last_soc_mutex);
2993
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302994 chip->start_soc = report_state_of_charge(chip);
2995
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07002996 mutex_lock(&chip->last_ocv_uv_mutex);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302997 if (chip->enable_fcc_learning)
2998 fcc_learning_config(chip, true);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07002999 chip->soc_at_cv = -EINVAL;
3000 chip->prev_chg_soc = -EINVAL;
3001 mutex_unlock(&chip->last_ocv_uv_mutex);
3002}
3003
3004static void charging_ended(struct qpnp_bms_chip *chip)
3005{
3006 mutex_lock(&chip->last_soc_mutex);
3007 chip->charge_start_tm_sec = 0;
3008 chip->catch_up_time_sec = 0;
3009 mutex_unlock(&chip->last_soc_mutex);
3010
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303011 chip->end_soc = report_state_of_charge(chip);
3012
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003013 mutex_lock(&chip->last_ocv_uv_mutex);
3014 chip->soc_at_cv = -EINVAL;
3015 chip->prev_chg_soc = -EINVAL;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303016
3017 /* update the chargecycles */
3018 if (chip->end_soc > chip->start_soc) {
3019 chip->charge_increase += (chip->end_soc - chip->start_soc);
3020 if (chip->charge_increase > 100) {
3021 chip->charge_cycles++;
3022 chip->charge_increase = chip->charge_increase % 100;
3023 }
3024 if (chip->enable_fcc_learning)
3025 backup_charge_cycle(chip);
3026 }
3027
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003028 if (get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL) {
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303029 if (chip->enable_fcc_learning &&
3030 (chip->start_soc <= chip->min_fcc_learning_soc) &&
3031 (chip->start_pc <= chip->min_fcc_ocv_pc))
3032 fcc_learning_config(chip, false);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003033 chip->done_charging = true;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003034 chip->last_soc_invalid = true;
Xiaozhe Shicc48e992013-05-28 16:42:24 -07003035 } else if (chip->charging_adjusted_ocv > 0) {
3036 pr_debug("Charging stopped before full, adjusted OCV = %d\n",
3037 chip->charging_adjusted_ocv);
3038 chip->last_ocv_uv = chip->charging_adjusted_ocv;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003039 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303040
Xiaozhe Shicc48e992013-05-28 16:42:24 -07003041 chip->charging_adjusted_ocv = -EINVAL;
3042
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003043 mutex_unlock(&chip->last_ocv_uv_mutex);
3044}
3045
3046static void battery_status_check(struct qpnp_bms_chip *chip)
3047{
3048 int status = get_battery_status(chip);
3049
3050 if (chip->battery_status != status) {
3051 if (status == POWER_SUPPLY_STATUS_CHARGING) {
3052 pr_debug("charging started\n");
3053 charging_began(chip);
3054 } else if (chip->battery_status
3055 == POWER_SUPPLY_STATUS_CHARGING) {
3056 pr_debug("charging ended\n");
3057 charging_ended(chip);
3058 }
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003059
3060 if (status == POWER_SUPPLY_STATUS_FULL) {
3061 pr_debug("battery full\n");
3062 enable_bms_irq(&chip->ocv_thr_irq);
3063 enable_bms_irq(&chip->sw_cc_thr_irq);
3064 } else if (chip->battery_status
3065 == POWER_SUPPLY_STATUS_FULL) {
3066 pr_debug("battery not full any more\n");
3067 disable_bms_irq(&chip->ocv_thr_irq);
3068 disable_bms_irq(&chip->sw_cc_thr_irq);
3069 }
3070
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003071 chip->battery_status = status;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003072 /* battery charge status has changed, so force a soc
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003073 * recalculation to update the SoC */
3074 schedule_work(&chip->recalc_work);
3075 }
3076}
3077
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003078#define CALIB_WRKARND_DIG_MAJOR_MAX 0x03
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003079static void batfet_status_check(struct qpnp_bms_chip *chip)
3080{
3081 bool batfet_closed;
3082
3083 if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX)
3084 return;
3085
3086 batfet_closed = is_batfet_closed(chip);
3087 if (chip->batfet_closed != batfet_closed) {
3088 chip->batfet_closed = batfet_closed;
3089 if (batfet_closed == false) {
3090 /* batfet opened */
3091 schedule_work(&chip->batfet_open_work);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003092 qpnp_iadc_skip_calibration(chip->iadc_dev);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003093 } else {
3094 /* batfet closed */
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003095 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, true);
3096 qpnp_iadc_resume_calibration(chip->iadc_dev);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003097 }
3098 }
3099}
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003100
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003101static void battery_insertion_check(struct qpnp_bms_chip *chip)
3102{
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003103 bool present = is_battery_present(chip);
3104
3105 mutex_lock(&chip->vbat_monitor_mutex);
3106 if (chip->battery_present != present) {
3107 if (chip->battery_present != -EINVAL) {
3108 if (present) {
3109 setup_vbat_monitoring(chip);
3110 chip->new_battery = true;
3111 } else {
3112 reset_vbat_monitoring(chip);
3113 }
3114 }
3115 chip->battery_present = present;
3116 /* a new battery was inserted or removed, so force a soc
3117 * recalculation to update the SoC */
3118 schedule_work(&chip->recalc_work);
3119 }
3120 mutex_unlock(&chip->vbat_monitor_mutex);
3121}
3122
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003123/* Returns capacity as a SoC percentage between 0 and 100 */
3124static int get_prop_bms_capacity(struct qpnp_bms_chip *chip)
3125{
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08003126 return report_state_of_charge(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003127}
3128
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003129static void qpnp_bms_external_power_changed(struct power_supply *psy)
3130{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003131 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
3132 bms_psy);
3133
3134 battery_insertion_check(chip);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003135 batfet_status_check(chip);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003136 battery_status_check(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003137}
3138
3139static int qpnp_bms_power_get_property(struct power_supply *psy,
3140 enum power_supply_property psp,
3141 union power_supply_propval *val)
3142{
3143 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
3144 bms_psy);
3145
3146 switch (psp) {
3147 case POWER_SUPPLY_PROP_CAPACITY:
3148 val->intval = get_prop_bms_capacity(chip);
3149 break;
3150 case POWER_SUPPLY_PROP_CURRENT_NOW:
3151 val->intval = get_prop_bms_current_now(chip);
3152 break;
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07003153 case POWER_SUPPLY_PROP_RESISTANCE:
3154 val->intval = get_prop_bms_batt_resistance(chip);
3155 break;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07003156 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
3157 val->intval = get_prop_bms_charge_counter(chip);
3158 break;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003159 case POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW:
3160 val->intval = get_prop_bms_charge_counter_shadow(chip);
3161 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003162 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
3163 val->intval = get_prop_bms_charge_full_design(chip);
3164 break;
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05303165 case POWER_SUPPLY_PROP_CHARGE_FULL:
3166 val->intval = get_prop_bms_charge_full(chip);
3167 break;
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05303168 case POWER_SUPPLY_PROP_CYCLE_COUNT:
3169 val->intval = chip->charge_cycles;
3170 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003171 default:
3172 return -EINVAL;
3173 }
3174 return 0;
3175}
3176
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003177#define OCV_USE_LIMIT_EN BIT(7)
3178static int set_ocv_voltage_thresholds(struct qpnp_bms_chip *chip,
3179 int low_voltage_threshold,
3180 int high_voltage_threshold)
3181{
3182 uint16_t low_voltage_raw, high_voltage_raw;
3183 int rc;
3184
3185 low_voltage_raw = convert_vbatt_uv_to_raw(chip,
3186 low_voltage_threshold);
3187 high_voltage_raw = convert_vbatt_uv_to_raw(chip,
3188 high_voltage_threshold);
3189 rc = qpnp_write_wrapper(chip, (u8 *)&low_voltage_raw,
3190 chip->base + BMS1_OCV_USE_LOW_LIMIT_THR0, 2);
3191 if (rc) {
3192 pr_err("Failed to set ocv low voltage threshold: %d\n", rc);
3193 return rc;
3194 }
3195 rc = qpnp_write_wrapper(chip, (u8 *)&high_voltage_raw,
3196 chip->base + BMS1_OCV_USE_HIGH_LIMIT_THR0, 2);
3197 if (rc) {
3198 pr_err("Failed to set ocv high voltage threshold: %d\n", rc);
3199 return rc;
3200 }
3201 rc = qpnp_masked_write(chip, BMS1_OCV_USE_LIMIT_CTL,
3202 OCV_USE_LIMIT_EN, OCV_USE_LIMIT_EN);
3203 if (rc) {
3204 pr_err("Failed to enabled ocv voltage thresholds: %d\n", rc);
3205 return rc;
3206 }
3207 pr_debug("ocv low threshold set to %d uv or 0x%x raw\n",
3208 low_voltage_threshold, low_voltage_raw);
3209 pr_debug("ocv high threshold set to %d uv or 0x%x raw\n",
3210 high_voltage_threshold, high_voltage_raw);
3211 return 0;
3212}
3213
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003214static void read_shutdown_soc_and_iavg(struct qpnp_bms_chip *chip)
3215{
3216 int rc;
3217 u8 temp;
3218
3219 if (chip->ignore_shutdown_soc) {
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08003220 chip->shutdown_soc_invalid = true;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003221 chip->shutdown_soc = 0;
3222 chip->shutdown_iavg_ma = 0;
3223 } else {
3224 rc = qpnp_read_wrapper(chip, &temp,
3225 chip->base + IAVG_STORAGE_REG, 1);
3226 if (rc) {
3227 pr_err("failed to read addr = %d %d assuming %d\n",
3228 chip->base + IAVG_STORAGE_REG, rc,
3229 IAVG_START);
3230 chip->shutdown_iavg_ma = IAVG_START;
Xiaozhe Shif5f966d2013-02-19 14:23:11 -08003231 } else if (temp == IAVG_INVALID) {
3232 pr_err("invalid iavg read from BMS1_DATA_REG_1, using %d\n",
3233 IAVG_START);
3234 chip->shutdown_iavg_ma = IAVG_START;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003235 } else {
3236 if (temp == 0) {
3237 chip->shutdown_iavg_ma = IAVG_START;
3238 } else {
3239 chip->shutdown_iavg_ma = IAVG_START
3240 + IAVG_STEP_SIZE_MA * (temp + 1);
3241 }
3242 }
3243
3244 rc = qpnp_read_wrapper(chip, &temp,
3245 chip->base + SOC_STORAGE_REG, 1);
3246 if (rc) {
3247 pr_err("failed to read addr = %d %d\n",
3248 chip->base + SOC_STORAGE_REG, rc);
3249 } else {
3250 chip->shutdown_soc = temp;
3251
Xiaozhe Shic7cbd052013-03-29 12:03:11 -07003252 if (chip->shutdown_soc == SOC_INVALID) {
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003253 pr_debug("No shutdown soc available\n");
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08003254 chip->shutdown_soc_invalid = true;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003255 chip->shutdown_iavg_ma = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003256 }
3257 }
3258 }
3259
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303260 /* read the SOC storage to determine if there was a battery removal */
3261 rc = qpnp_read_wrapper(chip, &temp, chip->base + SOC_STORAGE_REG, 1);
3262 if (!rc) {
3263 if (temp == SOC_INVALID)
3264 chip->battery_removed = true;
3265 }
3266
3267
3268 pr_debug("shutdown_soc = %d shutdown_iavg = %d shutdown_soc_invalid = %d, battery_removed = %d\n",
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003269 chip->shutdown_soc,
3270 chip->shutdown_iavg_ma,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303271 chip->shutdown_soc_invalid,
3272 chip->battery_removed);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003273}
3274
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003275static irqreturn_t bms_ocv_thr_irq_handler(int irq, void *_chip)
3276{
3277 struct qpnp_bms_chip *chip = _chip;
3278
3279 pr_debug("ocv_thr irq triggered\n");
3280 bms_stay_awake(&chip->soc_wake_source);
3281 schedule_work(&chip->recalc_work);
3282 return IRQ_HANDLED;
3283}
3284
3285static irqreturn_t bms_sw_cc_thr_irq_handler(int irq, void *_chip)
3286{
3287 struct qpnp_bms_chip *chip = _chip;
3288
3289 pr_debug("sw_cc_thr irq triggered\n");
3290 bms_stay_awake(&chip->soc_wake_source);
3291 schedule_work(&chip->recalc_work);
3292 return IRQ_HANDLED;
3293}
3294
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003295static int64_t read_battery_id(struct qpnp_bms_chip *chip)
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003296{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003297 int rc;
3298 struct qpnp_vadc_result result;
3299
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003300 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX2_BAT_ID, &result);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003301 if (rc) {
3302 pr_err("error reading batt id channel = %d, rc = %d\n",
3303 LR_MUX2_BAT_ID, rc);
3304 return rc;
3305 }
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003306
3307 return result.physical;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003308}
3309
3310static int set_battery_data(struct qpnp_bms_chip *chip)
3311{
3312 int64_t battery_id;
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003313 int rc;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003314 struct bms_battery_data *batt_data;
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003315 struct device_node *node;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003316
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003317 if (chip->batt_type == BATT_DESAY) {
3318 batt_data = &desay_5200_data;
3319 } else if (chip->batt_type == BATT_PALLADIUM) {
3320 batt_data = &palladium_1500_data;
3321 } else if (chip->batt_type == BATT_OEM) {
3322 batt_data = &oem_batt_data;
Wu Fenglin2ac88aa2013-04-25 12:43:40 +08003323 } else if (chip->batt_type == BATT_QRD_4V35_2000MAH) {
3324 batt_data = &QRD_4v35_2000mAh_data;
tingtingf50326f2013-06-05 15:07:24 +08003325 } else if (chip->batt_type == BATT_QRD_4V2_1300MAH) {
3326 batt_data = &qrd_4v2_1300mah_data;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003327 } else {
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003328 battery_id = read_battery_id(chip);
3329 if (battery_id < 0) {
3330 pr_err("cannot read battery id err = %lld\n",
3331 battery_id);
3332 return battery_id;
3333 }
3334
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003335 node = of_find_node_by_name(chip->spmi->dev.of_node,
3336 "qcom,battery-data");
3337 if (node) {
3338 batt_data = kzalloc(sizeof(struct bms_battery_data),
3339 GFP_KERNEL);
3340 batt_data->fcc_temp_lut = kzalloc(
3341 sizeof(struct single_row_lut),
3342 GFP_KERNEL);
3343 batt_data->pc_temp_ocv_lut = kzalloc(
3344 sizeof(struct pc_temp_ocv_lut),
3345 GFP_KERNEL);
3346 batt_data->rbatt_sf_lut = kzalloc(
3347 sizeof(struct sf_lut), GFP_KERNEL);
3348
3349 rc = of_batterydata_read_data(node,
3350 batt_data, battery_id);
3351 if (rc) {
3352 pr_err("battery data load failed, using palladium 1500\n");
3353 kfree(batt_data->fcc_temp_lut);
3354 kfree(batt_data->pc_temp_ocv_lut);
3355 kfree(batt_data->rbatt_sf_lut);
3356 kfree(batt_data);
3357 batt_data = &palladium_1500_data;
3358 }
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003359 } else {
3360 pr_warn("invalid battid, palladium 1500 assumed batt_id %llx\n",
3361 battery_id);
3362 batt_data = &palladium_1500_data;
3363 }
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003364 }
3365
Xiaozhe Shi976618f2013-04-30 10:49:30 -07003366 chip->fcc_mah = batt_data->fcc;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003367 chip->fcc_temp_lut = batt_data->fcc_temp_lut;
3368 chip->fcc_sf_lut = batt_data->fcc_sf_lut;
3369 chip->pc_temp_ocv_lut = batt_data->pc_temp_ocv_lut;
3370 chip->pc_sf_lut = batt_data->pc_sf_lut;
3371 chip->rbatt_sf_lut = batt_data->rbatt_sf_lut;
3372 chip->default_rbatt_mohm = batt_data->default_rbatt_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07003373 chip->rbatt_capacitive_mohm = batt_data->rbatt_capacitive_mohm;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07003374 chip->flat_ocv_threshold_uv = batt_data->flat_ocv_threshold_uv;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003375
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003376 /* Override battery properties if specified in the battery profile */
3377 if (batt_data->max_voltage_uv >= 0)
3378 chip->max_voltage_uv = batt_data->max_voltage_uv;
3379 if (batt_data->cutoff_uv >= 0)
3380 chip->v_cutoff_uv = batt_data->cutoff_uv;
3381 if (batt_data->iterm_ua >= 0)
3382 chip->chg_term_ua = batt_data->iterm_ua;
3383
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003384 if (chip->pc_temp_ocv_lut == NULL) {
3385 pr_err("temp ocv lut table is NULL\n");
3386 return -EINVAL;
3387 }
3388 return 0;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003389}
3390
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07003391static int bms_get_adc(struct qpnp_bms_chip *chip,
3392 struct spmi_device *spmi)
3393{
3394 int rc = 0;
3395
3396 chip->vadc_dev = qpnp_get_vadc(&spmi->dev, "bms");
3397 if (IS_ERR(chip->vadc_dev)) {
3398 rc = PTR_ERR(chip->vadc_dev);
3399 if (rc != -EPROBE_DEFER)
3400 pr_err("vadc property missing, rc=%d\n", rc);
3401 return rc;
3402 }
3403
3404 chip->iadc_dev = qpnp_get_iadc(&spmi->dev, "bms");
3405 if (IS_ERR(chip->iadc_dev)) {
3406 rc = PTR_ERR(chip->iadc_dev);
3407 if (rc != -EPROBE_DEFER)
3408 pr_err("iadc property missing, rc=%d\n", rc);
3409 return rc;
3410 }
3411
3412 chip->adc_tm_dev = qpnp_get_adc_tm(&spmi->dev, "bms");
3413 if (IS_ERR(chip->adc_tm_dev)) {
3414 rc = PTR_ERR(chip->adc_tm_dev);
3415 if (rc != -EPROBE_DEFER)
3416 pr_err("adc-tm not ready, defer probe\n");
3417 return rc;
3418 }
3419
3420 return 0;
3421}
3422
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003423#define SPMI_PROP_READ(chip_prop, qpnp_spmi_property, retval) \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003424do { \
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003425 if (retval) \
3426 break; \
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003427 retval = of_property_read_u32(chip->spmi->dev.of_node, \
Xiaozhe Shi9bd24622013-01-23 15:54:54 -08003428 "qcom," qpnp_spmi_property, \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003429 &chip->chip_prop); \
3430 if (retval) { \
3431 pr_err("Error reading " #qpnp_spmi_property \
3432 " property %d\n", rc); \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003433 } \
3434} while (0)
3435
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303436#define SPMI_PROP_READ_BOOL(chip_prop, qpnp_spmi_property) \
3437do { \
3438 chip->chip_prop = of_property_read_bool(chip->spmi->dev.of_node,\
3439 "qcom," qpnp_spmi_property); \
3440} while (0)
3441
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003442static inline int bms_read_properties(struct qpnp_bms_chip *chip)
3443{
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003444 int rc = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003445
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003446 SPMI_PROP_READ(r_sense_uohm, "r-sense-uohm", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003447 SPMI_PROP_READ(v_cutoff_uv, "v-cutoff-uv", rc);
3448 SPMI_PROP_READ(max_voltage_uv, "max-voltage-uv", rc);
3449 SPMI_PROP_READ(r_conn_mohm, "r-conn-mohm", rc);
3450 SPMI_PROP_READ(chg_term_ua, "chg-term-ua", rc);
3451 SPMI_PROP_READ(shutdown_soc_valid_limit,
3452 "shutdown-soc-valid-limit", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003453 SPMI_PROP_READ(adjust_soc_low_threshold,
3454 "adjust-soc-low-threshold", rc);
3455 SPMI_PROP_READ(batt_type, "batt-type", rc);
3456 SPMI_PROP_READ(low_soc_calc_threshold,
3457 "low-soc-calculate-soc-threshold", rc);
3458 SPMI_PROP_READ(low_soc_calculate_soc_ms,
3459 "low-soc-calculate-soc-ms", rc);
3460 SPMI_PROP_READ(calculate_soc_ms, "calculate-soc-ms", rc);
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07003461 SPMI_PROP_READ(high_ocv_correction_limit_uv,
3462 "high-ocv-correction-limit-uv", rc);
3463 SPMI_PROP_READ(low_ocv_correction_limit_uv,
3464 "low-ocv-correction-limit-uv", rc);
3465 SPMI_PROP_READ(hold_soc_est,
3466 "hold-soc-est", rc);
3467 SPMI_PROP_READ(ocv_high_threshold_uv,
3468 "ocv-voltage-high-threshold-uv", rc);
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003469 SPMI_PROP_READ(ocv_low_threshold_uv,
3470 "ocv-voltage-low-threshold-uv", rc);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08003471 SPMI_PROP_READ(low_voltage_threshold, "low-voltage-threshold", rc);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003472 SPMI_PROP_READ(temperature_margin, "tm-temp-margin", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003473
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003474 chip->use_external_rsense = of_property_read_bool(
3475 chip->spmi->dev.of_node,
3476 "qcom,use-external-rsense");
3477 chip->ignore_shutdown_soc = of_property_read_bool(
3478 chip->spmi->dev.of_node,
3479 "qcom,ignore-shutdown-soc");
3480 chip->use_voltage_soc = of_property_read_bool(chip->spmi->dev.of_node,
3481 "qcom,use-voltage-soc");
3482 chip->use_ocv_thresholds = of_property_read_bool(
3483 chip->spmi->dev.of_node,
3484 "qcom,use-ocv-thresholds");
3485
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003486 if (chip->adjust_soc_low_threshold >= 45)
3487 chip->adjust_soc_low_threshold = 45;
3488
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303489 SPMI_PROP_READ_BOOL(enable_fcc_learning, "enable-fcc-learning");
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303490 if (chip->enable_fcc_learning) {
3491 SPMI_PROP_READ(min_fcc_learning_soc,
3492 "min-fcc-learning-soc", rc);
3493 SPMI_PROP_READ(min_fcc_ocv_pc,
3494 "min-fcc-ocv-pc", rc);
3495 SPMI_PROP_READ(min_fcc_learning_samples,
3496 "min-fcc-learning-samples", rc);
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303497 SPMI_PROP_READ(fcc_resolution,
3498 "fcc-resolution", rc);
3499 if (chip->min_fcc_learning_samples > MAX_FCC_CYCLES)
3500 chip->min_fcc_learning_samples = MAX_FCC_CYCLES;
3501 chip->fcc_learning_samples = devm_kzalloc(&chip->spmi->dev,
3502 (sizeof(struct fcc_sample) *
3503 chip->min_fcc_learning_samples), GFP_KERNEL);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303504 pr_debug("min-fcc-soc=%d, min-fcc-pc=%d, min-fcc-cycles=%d\n",
3505 chip->min_fcc_learning_soc, chip->min_fcc_ocv_pc,
3506 chip->min_fcc_learning_samples);
3507 }
3508
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003509 if (rc) {
3510 pr_err("Missing required properties.\n");
3511 return rc;
3512 }
3513
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003514 pr_debug("dts data: r_sense_uohm:%d, v_cutoff_uv:%d, max_v:%d\n",
3515 chip->r_sense_uohm, chip->v_cutoff_uv,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003516 chip->max_voltage_uv);
3517 pr_debug("r_conn:%d, shutdown_soc: %d, adjust_soc_low:%d\n",
3518 chip->r_conn_mohm, chip->shutdown_soc_valid_limit,
3519 chip->adjust_soc_low_threshold);
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07003520 pr_debug("chg_term_ua:%d, batt_type:%d\n",
3521 chip->chg_term_ua,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003522 chip->batt_type);
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08003523 pr_debug("ignore_shutdown_soc:%d, use_voltage_soc:%d\n",
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08003524 chip->ignore_shutdown_soc, chip->use_voltage_soc);
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003525 pr_debug("use external rsense: %d\n", chip->use_external_rsense);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003526 return 0;
3527}
3528
3529static inline void bms_initialize_constants(struct qpnp_bms_chip *chip)
3530{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003531 chip->prev_pc_unusable = -EINVAL;
3532 chip->soc_at_cv = -EINVAL;
3533 chip->calculated_soc = -EINVAL;
Xiaozhe Shie118c692012-09-24 15:17:43 -07003534 chip->last_soc = -EINVAL;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07003535 chip->last_soc_est = -EINVAL;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003536 chip->battery_present = -EINVAL;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003537 chip->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08003538 chip->last_cc_uah = INT_MIN;
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08003539 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
3540 chip->prev_last_good_ocv_raw = OCV_RAW_UNINITIALIZED;
Xiaozhe Shie118c692012-09-24 15:17:43 -07003541 chip->first_time_calc_soc = 1;
3542 chip->first_time_calc_uuc = 1;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003543}
3544
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003545#define SPMI_FIND_IRQ(chip, irq_name) \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003546do { \
3547 chip->irq_name##_irq.irq = spmi_get_irq_byname(chip->spmi, \
3548 resource, #irq_name); \
3549 if (chip->irq_name##_irq.irq < 0) { \
3550 pr_err("Unable to get " #irq_name " irq\n"); \
3551 return -ENXIO; \
3552 } \
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003553} while (0)
3554
3555static int bms_find_irqs(struct qpnp_bms_chip *chip,
3556 struct spmi_resource *resource)
3557{
3558 SPMI_FIND_IRQ(chip, sw_cc_thr);
3559 SPMI_FIND_IRQ(chip, ocv_thr);
3560 return 0;
3561}
3562
3563#define SPMI_REQUEST_IRQ(chip, rc, irq_name) \
3564do { \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003565 rc = devm_request_irq(chip->dev, chip->irq_name##_irq.irq, \
3566 bms_##irq_name##_irq_handler, \
3567 IRQF_TRIGGER_RISING, #irq_name, chip); \
3568 if (rc < 0) { \
3569 pr_err("Unable to request " #irq_name " irq: %d\n", rc);\
3570 return -ENXIO; \
3571 } \
3572} while (0)
3573
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003574static int bms_request_irqs(struct qpnp_bms_chip *chip)
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003575{
3576 int rc;
3577
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003578 SPMI_REQUEST_IRQ(chip, rc, sw_cc_thr);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003579 enable_irq_wake(chip->sw_cc_thr_irq.irq);
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003580 SPMI_REQUEST_IRQ(chip, rc, ocv_thr);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003581 enable_irq_wake(chip->ocv_thr_irq.irq);
3582 return 0;
3583}
3584
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003585#define REG_OFFSET_PERP_TYPE 0x04
3586#define REG_OFFSET_PERP_SUBTYPE 0x05
3587#define BMS_BMS_TYPE 0xD
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003588#define BMS_BMS1_SUBTYPE 0x1
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003589#define BMS_IADC_TYPE 0x8
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003590#define BMS_IADC1_SUBTYPE 0x3
3591#define BMS_IADC2_SUBTYPE 0x5
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003592
3593static int register_spmi(struct qpnp_bms_chip *chip, struct spmi_device *spmi)
3594{
3595 struct spmi_resource *spmi_resource;
3596 struct resource *resource;
3597 int rc;
3598 u8 type, subtype;
3599
3600 chip->dev = &(spmi->dev);
3601 chip->spmi = spmi;
3602
3603 spmi_for_each_container_dev(spmi_resource, spmi) {
3604 if (!spmi_resource) {
3605 pr_err("qpnp_bms: spmi resource absent\n");
3606 return -ENXIO;
3607 }
3608
3609 resource = spmi_get_resource(spmi, spmi_resource,
3610 IORESOURCE_MEM, 0);
3611 if (!(resource && resource->start)) {
3612 pr_err("node %s IO resource absent!\n",
3613 spmi->dev.of_node->full_name);
3614 return -ENXIO;
3615 }
3616
3617 rc = qpnp_read_wrapper(chip, &type,
3618 resource->start + REG_OFFSET_PERP_TYPE, 1);
3619 if (rc) {
3620 pr_err("Peripheral type read failed rc=%d\n", rc);
3621 return rc;
3622 }
3623 rc = qpnp_read_wrapper(chip, &subtype,
3624 resource->start + REG_OFFSET_PERP_SUBTYPE, 1);
3625 if (rc) {
3626 pr_err("Peripheral subtype read failed rc=%d\n", rc);
3627 return rc;
3628 }
3629
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003630 if (type == BMS_BMS_TYPE && subtype == BMS_BMS1_SUBTYPE) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003631 chip->base = resource->start;
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003632 rc = bms_find_irqs(chip, spmi_resource);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003633 if (rc) {
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003634 pr_err("Could not find irqs\n");
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003635 return rc;
3636 }
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003637 } else if (type == BMS_IADC_TYPE
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003638 && (subtype == BMS_IADC1_SUBTYPE
3639 || subtype == BMS_IADC2_SUBTYPE)) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003640 chip->iadc_base = resource->start;
3641 } else {
3642 pr_err("Invalid peripheral start=0x%x type=0x%x, subtype=0x%x\n",
3643 resource->start, type, subtype);
3644 }
3645 }
3646
3647 if (chip->base == 0) {
3648 dev_err(&spmi->dev, "BMS peripheral was not registered\n");
3649 return -EINVAL;
3650 }
3651 if (chip->iadc_base == 0) {
3652 dev_err(&spmi->dev, "BMS_IADC peripheral was not registered\n");
3653 return -EINVAL;
3654 }
3655
3656 return 0;
3657}
3658
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003659#define ADC_CH_SEL_MASK 0x7
3660#define ADC_INT_RSNSN_CTL_MASK 0x3
3661#define ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE 0x2
3662#define FAST_AVG_EN_MASK 0x80
3663#define FAST_AVG_EN_VALUE_EXT_RSENSE 0x80
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003664static int read_iadc_channel_select(struct qpnp_bms_chip *chip)
3665{
3666 u8 iadc_channel_select;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08003667 int32_t rds_rsense_nohm;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003668 int rc;
3669
3670 rc = qpnp_read_wrapper(chip, &iadc_channel_select,
3671 chip->iadc_base + IADC1_BMS_ADC_CH_SEL_CTL, 1);
3672 if (rc) {
3673 pr_err("Error reading bms_iadc channel register %d\n", rc);
3674 return rc;
3675 }
3676
3677 iadc_channel_select &= ADC_CH_SEL_MASK;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08003678 if (iadc_channel_select != EXTERNAL_RSENSE
3679 && iadc_channel_select != INTERNAL_RSENSE) {
3680 pr_err("IADC1_BMS_IADC configured incorrectly. Selected channel = %d\n",
3681 iadc_channel_select);
3682 return -EINVAL;
3683 }
3684
3685 if (chip->use_external_rsense) {
3686 pr_debug("External rsense selected\n");
3687 if (iadc_channel_select == INTERNAL_RSENSE) {
3688 pr_debug("Internal rsense detected; Changing rsense to external\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003689 rc = qpnp_masked_write_iadc(chip,
3690 IADC1_BMS_ADC_CH_SEL_CTL,
3691 ADC_CH_SEL_MASK,
3692 EXTERNAL_RSENSE);
3693 if (rc) {
3694 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
3695 IADC1_BMS_ADC_CH_SEL_CTL,
3696 EXTERNAL_RSENSE, rc);
3697 return rc;
3698 }
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003699 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07003700 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003701 chip->software_shdw_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003702 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08003703 } else {
3704 pr_debug("Internal rsense selected\n");
3705 if (iadc_channel_select == EXTERNAL_RSENSE) {
3706 pr_debug("External rsense detected; Changing rsense to internal\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003707 rc = qpnp_masked_write_iadc(chip,
3708 IADC1_BMS_ADC_CH_SEL_CTL,
3709 ADC_CH_SEL_MASK,
3710 INTERNAL_RSENSE);
3711 if (rc) {
3712 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
3713 IADC1_BMS_ADC_CH_SEL_CTL,
3714 INTERNAL_RSENSE, rc);
3715 return rc;
3716 }
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003717 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
3718 chip->software_shdw_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003719 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08003720
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003721 rc = qpnp_iadc_get_rsense(chip->iadc_dev, &rds_rsense_nohm);
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08003722 if (rc) {
3723 pr_err("Unable to read RDS resistance value from IADC; rc = %d\n",
3724 rc);
3725 return rc;
3726 }
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003727 chip->r_sense_uohm = rds_rsense_nohm/1000;
3728 pr_debug("rds_rsense = %d nOhm, saved as %d uOhm\n",
3729 rds_rsense_nohm, chip->r_sense_uohm);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003730 }
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003731 /* prevent shorting of leads by IADC_BMS when external Rsense is used */
3732 if (chip->use_external_rsense) {
3733 if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX) {
3734 rc = qpnp_masked_write_iadc(chip,
3735 IADC1_BMS_ADC_INT_RSNSN_CTL,
3736 ADC_INT_RSNSN_CTL_MASK,
3737 ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE);
3738 if (rc) {
3739 pr_err("Unable to set batfet config %x to %x: %d\n",
3740 IADC1_BMS_ADC_INT_RSNSN_CTL,
3741 ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE, rc);
3742 return rc;
3743 }
3744 } else {
3745 /* In older PMICS use FAST_AVG_EN register bit 7 */
3746 rc = qpnp_masked_write_iadc(chip,
3747 IADC1_BMS_FAST_AVG_EN,
3748 FAST_AVG_EN_MASK,
3749 FAST_AVG_EN_VALUE_EXT_RSENSE);
3750 if (rc) {
3751 pr_err("Unable to set batfet config %x to %x: %d\n",
3752 IADC1_BMS_FAST_AVG_EN,
3753 FAST_AVG_EN_VALUE_EXT_RSENSE, rc);
3754 return rc;
3755 }
3756 }
3757 }
3758
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003759 return 0;
3760}
3761
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003762static int refresh_die_temp_monitor(struct qpnp_bms_chip *chip)
3763{
3764 struct qpnp_vadc_result result;
3765 int rc;
3766
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003767 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003768
3769 pr_debug("low = %lld, high = %lld\n",
3770 result.physical - chip->temperature_margin,
3771 result.physical + chip->temperature_margin);
3772 chip->die_temp_monitor_params.high_temp = result.physical
3773 + chip->temperature_margin;
3774 chip->die_temp_monitor_params.low_temp = result.physical
3775 - chip->temperature_margin;
3776 chip->die_temp_monitor_params.state_request =
3777 ADC_TM_HIGH_LOW_THR_ENABLE;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07003778 return qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
3779 &chip->die_temp_monitor_params);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003780}
3781
3782static void btm_notify_die_temp(enum qpnp_tm_state state, void *ctx)
3783{
3784 struct qpnp_bms_chip *chip = ctx;
3785 struct qpnp_vadc_result result;
3786 int rc;
3787
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003788 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003789
3790 if (state == ADC_TM_LOW_STATE)
3791 pr_debug("low state triggered\n");
3792 else if (state == ADC_TM_HIGH_STATE)
3793 pr_debug("high state triggered\n");
3794 pr_debug("die temp = %lld, raw = 0x%x\n",
3795 result.physical, result.adc_code);
3796 schedule_work(&chip->recalc_work);
3797 refresh_die_temp_monitor(chip);
3798}
3799
3800static int setup_die_temp_monitoring(struct qpnp_bms_chip *chip)
3801{
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07003802 int rc;
3803
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003804 chip->die_temp_monitor_params.channel = DIE_TEMP;
3805 chip->die_temp_monitor_params.btm_ctx = (void *)chip;
3806 chip->die_temp_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
3807 chip->die_temp_monitor_params.threshold_notification =
3808 &btm_notify_die_temp;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07003809 rc = refresh_die_temp_monitor(chip);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003810 if (rc) {
3811 pr_err("tm setup failed: %d\n", rc);
3812 return rc;
3813 }
3814 pr_debug("setup complete\n");
3815 return 0;
3816}
3817
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003818static int __devinit qpnp_bms_probe(struct spmi_device *spmi)
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003819{
3820 struct qpnp_bms_chip *chip;
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07003821 bool warm_reset;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003822 int rc, vbatt;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003823
3824 chip = kzalloc(sizeof *chip, GFP_KERNEL);
3825
3826 if (chip == NULL) {
3827 pr_err("kzalloc() failed.\n");
3828 return -ENOMEM;
3829 }
3830
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07003831 rc = bms_get_adc(chip, spmi);
3832 if (rc < 0)
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003833 goto error_read;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003834
Xiaozhe Shi3eaf0b62013-07-11 09:48:08 -07003835 mutex_init(&chip->bms_output_lock);
3836 mutex_init(&chip->last_ocv_uv_mutex);
3837 mutex_init(&chip->vbat_monitor_mutex);
3838 mutex_init(&chip->soc_invalidation_mutex);
3839 mutex_init(&chip->last_soc_mutex);
3840
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07003841 warm_reset = qpnp_pon_is_warm_reset();
3842 rc = warm_reset;
3843 if (rc < 0)
3844 goto error_read;
3845
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003846 rc = register_spmi(chip, spmi);
3847 if (rc) {
3848 pr_err("error registering spmi resource %d\n", rc);
3849 goto error_resource;
3850 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003851
3852 rc = qpnp_read_wrapper(chip, &chip->revision1,
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003853 chip->base + REVISION1, 1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003854 if (rc) {
3855 pr_err("error reading version register %d\n", rc);
3856 goto error_read;
3857 }
3858
3859 rc = qpnp_read_wrapper(chip, &chip->revision2,
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003860 chip->base + REVISION2, 1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003861 if (rc) {
3862 pr_err("Error reading version register %d\n", rc);
3863 goto error_read;
3864 }
Xiaozhe Shia045a562012-11-28 16:55:39 -08003865 pr_debug("BMS version: %hhu.%hhu\n", chip->revision2, chip->revision1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003866
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003867 rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision2,
3868 chip->iadc_base + REVISION2, 1);
3869 if (rc) {
3870 pr_err("Error reading version register %d\n", rc);
3871 goto error_read;
3872 }
3873
3874 rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision1,
3875 chip->iadc_base + REVISION1, 1);
3876 if (rc) {
3877 pr_err("Error reading version register %d\n", rc);
3878 goto error_read;
3879 }
3880 pr_debug("IADC_BMS version: %hhu.%hhu\n",
3881 chip->iadc_bms_revision2, chip->iadc_bms_revision1);
3882
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003883 rc = bms_read_properties(chip);
3884 if (rc) {
3885 pr_err("Unable to read all bms properties, rc = %d\n", rc);
3886 goto error_read;
3887 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003888
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003889 rc = read_iadc_channel_select(chip);
3890 if (rc) {
3891 pr_err("Unable to get iadc selected channel = %d\n", rc);
3892 goto error_read;
3893 }
3894
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003895 if (chip->use_ocv_thresholds) {
3896 rc = set_ocv_voltage_thresholds(chip,
3897 chip->ocv_low_threshold_uv,
3898 chip->ocv_high_threshold_uv);
3899 if (rc) {
3900 pr_err("Could not set ocv voltage thresholds: %d\n",
3901 rc);
3902 goto error_read;
3903 }
3904 }
3905
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003906 rc = set_battery_data(chip);
3907 if (rc) {
3908 pr_err("Bad battery data %d\n", rc);
3909 goto error_read;
3910 }
3911
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003912 bms_initialize_constants(chip);
3913
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003914 wakeup_source_init(&chip->soc_wake_source.source, "qpnp_soc_wake");
Xiaozhe Shi4be85782013-02-22 17:33:40 -08003915 wake_lock_init(&chip->low_voltage_wake_lock, WAKE_LOCK_SUSPEND,
3916 "qpnp_low_voltage_lock");
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003917 wake_lock_init(&chip->cv_wake_lock, WAKE_LOCK_SUSPEND,
3918 "qpnp_cv_lock");
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003919 INIT_DELAYED_WORK(&chip->calculate_soc_delayed_work,
3920 calculate_soc_work);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08003921 INIT_WORK(&chip->recalc_work, recalculate_work);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003922 INIT_WORK(&chip->batfet_open_work, batfet_open_work);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003923
3924 read_shutdown_soc_and_iavg(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003925
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05303926 if (chip->enable_fcc_learning) {
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303927 if (chip->battery_removed) {
3928 rc = discard_backup_fcc_data(chip);
3929 if (rc)
3930 pr_err("Could not discard backed-up FCC data\n");
3931 } else {
3932 rc = read_chgcycle_data_from_backup(chip);
3933 if (rc)
3934 pr_err("Unable to restore charge-cycle data\n");
3935
3936 rc = read_fcc_data_from_backup(chip);
3937 if (rc)
3938 pr_err("Unable to restore FCC-learning data\n");
3939 else
3940 attempt_learning_new_fcc(chip);
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05303941 }
3942 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303943
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003944 dev_set_drvdata(&spmi->dev, chip);
3945 device_init_wakeup(&spmi->dev, 1);
3946
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003947 rc = setup_vbat_monitoring(chip);
3948 if (rc < 0) {
3949 pr_err("failed to set up voltage notifications: %d\n", rc);
3950 goto error_setup;
Xiaozhe Shid5d21412013-02-06 17:14:41 -08003951 }
3952
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003953 rc = setup_die_temp_monitoring(chip);
3954 if (rc < 0) {
3955 pr_err("failed to set up die temp notifications: %d\n", rc);
3956 goto error_setup;
3957 }
3958
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07003959 battery_insertion_check(chip);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003960 batfet_status_check(chip);
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07003961 battery_status_check(chip);
3962
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003963 calculate_soc_work(&(chip->calculate_soc_delayed_work.work));
3964
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003965 /* setup & register the battery power supply */
3966 chip->bms_psy.name = "bms";
3967 chip->bms_psy.type = POWER_SUPPLY_TYPE_BMS;
3968 chip->bms_psy.properties = msm_bms_power_props;
3969 chip->bms_psy.num_properties = ARRAY_SIZE(msm_bms_power_props);
3970 chip->bms_psy.get_property = qpnp_bms_power_get_property;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003971 chip->bms_psy.external_power_changed =
3972 qpnp_bms_external_power_changed;
3973 chip->bms_psy.supplied_to = qpnp_bms_supplicants;
3974 chip->bms_psy.num_supplicants = ARRAY_SIZE(qpnp_bms_supplicants);
3975
3976 rc = power_supply_register(chip->dev, &chip->bms_psy);
3977
3978 if (rc < 0) {
3979 pr_err("power_supply_register bms failed rc = %d\n", rc);
3980 goto unregister_dc;
3981 }
3982
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07003983 chip->bms_psy_registered = true;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003984 vbatt = 0;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003985 rc = get_battery_voltage(chip, &vbatt);
Xiaozhe Shi36458962013-02-06 16:19:57 -08003986 if (rc) {
3987 pr_err("error reading vbat_sns adc channel = %d, rc = %d\n",
3988 VBAT_SNS, rc);
3989 goto unregister_dc;
3990 }
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003991
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003992 rc = bms_request_irqs(chip);
3993 if (rc) {
3994 pr_err("error requesting bms irqs, rc = %d\n", rc);
3995 goto unregister_dc;
3996 }
3997
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07003998 pr_info("probe success: soc =%d vbatt = %d ocv = %d r_sense_uohm = %u warm_reset = %d\n",
3999 get_prop_bms_capacity(chip), vbatt, chip->last_ocv_uv,
4000 chip->r_sense_uohm, warm_reset);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004001 return 0;
4002
4003unregister_dc:
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07004004 chip->bms_psy_registered = false;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004005 power_supply_unregister(&chip->bms_psy);
4006error_setup:
4007 dev_set_drvdata(&spmi->dev, NULL);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004008 wakeup_source_trash(&chip->soc_wake_source.source);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08004009 wake_lock_destroy(&chip->low_voltage_wake_lock);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004010 wake_lock_destroy(&chip->cv_wake_lock);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004011error_resource:
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004012error_read:
4013 kfree(chip);
4014 return rc;
4015}
4016
4017static int __devexit
4018qpnp_bms_remove(struct spmi_device *spmi)
4019{
4020 struct qpnp_bms_chip *chip = dev_get_drvdata(&spmi->dev);
4021
4022 dev_set_drvdata(&spmi->dev, NULL);
4023 kfree(chip);
4024 return 0;
4025}
4026
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004027static int bms_suspend(struct device *dev)
4028{
4029 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
4030
4031 cancel_delayed_work_sync(&chip->calculate_soc_delayed_work);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07004032 chip->was_charging_at_sleep = is_battery_charging(chip);
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004033 return 0;
4034}
4035
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004036static int bms_resume(struct device *dev)
4037{
4038 int rc;
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004039 int soc_calc_period;
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004040 int time_until_next_recalc = 0;
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004041 unsigned long time_since_last_recalc;
4042 unsigned long tm_now_sec;
4043 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
4044
4045 rc = get_current_time(&tm_now_sec);
4046 if (rc) {
4047 pr_err("Could not read current time: %d\n", rc);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004048 } else {
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004049 if (chip->calculated_soc < chip->low_soc_calc_threshold)
4050 soc_calc_period = chip->low_soc_calculate_soc_ms;
4051 else
4052 soc_calc_period = chip->calculate_soc_ms;
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004053 time_since_last_recalc = tm_now_sec - chip->last_recalc_time;
4054 pr_debug("Time since last recalc: %lu\n",
4055 time_since_last_recalc);
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004056 time_until_next_recalc = max(0, soc_calc_period
4057 - (int)(time_since_last_recalc * 1000));
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004058 }
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004059
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004060 if (time_until_next_recalc == 0)
4061 bms_stay_awake(&chip->soc_wake_source);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004062 schedule_delayed_work(&chip->calculate_soc_delayed_work,
4063 round_jiffies_relative(msecs_to_jiffies
4064 (time_until_next_recalc)));
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004065 return 0;
4066}
4067
4068static const struct dev_pm_ops qpnp_bms_pm_ops = {
4069 .resume = bms_resume,
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004070 .suspend = bms_suspend,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004071};
4072
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004073static struct spmi_driver qpnp_bms_driver = {
4074 .probe = qpnp_bms_probe,
4075 .remove = __devexit_p(qpnp_bms_remove),
4076 .driver = {
4077 .name = QPNP_BMS_DEV_NAME,
4078 .owner = THIS_MODULE,
4079 .of_match_table = qpnp_bms_match_table,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004080 .pm = &qpnp_bms_pm_ops,
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004081 },
4082};
4083
4084static int __init qpnp_bms_init(void)
4085{
4086 pr_info("QPNP BMS INIT\n");
4087 return spmi_driver_register(&qpnp_bms_driver);
4088}
4089
4090static void __exit qpnp_bms_exit(void)
4091{
4092 pr_info("QPNP BMS EXIT\n");
4093 return spmi_driver_unregister(&qpnp_bms_driver);
4094}
4095
4096module_init(qpnp_bms_init);
4097module_exit(qpnp_bms_exit);
4098
4099MODULE_DESCRIPTION("QPNP BMS Driver");
4100MODULE_LICENSE("GPL v2");
4101MODULE_ALIAS("platform:" QPNP_BMS_DEV_NAME);