blob: 0f96be11008a92dc939488ce8348ac764c4cb82b [file] [log] [blame]
Xiaozhe Shi72a72f22013-12-26 13:54:29 -08001/* Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
Xiaozhe Shi73a65692012-09-18 17:51:57 -070013#define pr_fmt(fmt) "BMS: %s: " fmt, __func__
Xiaozhe Shib19f7032012-08-16 12:14:16 -070014
15#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/init.h>
18#include <linux/slab.h>
19#include <linux/err.h>
20#include <linux/of.h>
21#include <linux/of_device.h>
22#include <linux/power_supply.h>
23#include <linux/spmi.h>
Xiaozhe Shie118c692012-09-24 15:17:43 -070024#include <linux/rtc.h>
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -070025#include <linux/delay.h>
Xiaozhe Shi27375822013-08-22 11:40:15 -070026#include <linux/sched.h>
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070027#include <linux/qpnp/qpnp-adc.h>
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -070028#include <linux/qpnp/power-on.h>
Xiaozhe Shiaf203c22013-06-19 12:01:38 -070029#include <linux/of_batterydata.h>
Xiaozhe Shib19f7032012-08-16 12:14:16 -070030
Xiaozhe Shib19f7032012-08-16 12:14:16 -070031/* BMS Register Offsets */
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -070032#define REVISION1 0x0
33#define REVISION2 0x1
Xiaozhe Shib19f7032012-08-16 12:14:16 -070034#define BMS1_STATUS1 0x8
35#define BMS1_MODE_CTL 0X40
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070036/* Coulomb counter clear registers */
Xiaozhe Shib19f7032012-08-16 12:14:16 -070037#define BMS1_CC_DATA_CTL 0x42
Xiaozhe Shia045a562012-11-28 16:55:39 -080038#define BMS1_CC_CLEAR_CTL 0x43
Xiaozhe Shi20640b52013-01-03 11:49:30 -080039/* BMS Tolerances */
40#define BMS1_TOL_CTL 0X44
Xiaozhe Shib19f7032012-08-16 12:14:16 -070041/* OCV limit registers */
42#define BMS1_OCV_USE_LOW_LIMIT_THR0 0x48
43#define BMS1_OCV_USE_LOW_LIMIT_THR1 0x49
44#define BMS1_OCV_USE_HIGH_LIMIT_THR0 0x4A
45#define BMS1_OCV_USE_HIGH_LIMIT_THR1 0x4B
46#define BMS1_OCV_USE_LIMIT_CTL 0x4C
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -070047/* Delay control */
48#define BMS1_S1_DELAY_CTL 0x5A
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -080049/* OCV interrupt threshold */
50#define BMS1_OCV_THR0 0x50
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -070051#define BMS1_S2_SAMP_AVG_CTL 0x61
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -080052/* SW CC interrupt threshold */
53#define BMS1_SW_CC_THR0 0xA0
Xiaozhe Shib19f7032012-08-16 12:14:16 -070054/* OCV for r registers */
55#define BMS1_OCV_FOR_R_DATA0 0x80
Xiaozhe Shib19f7032012-08-16 12:14:16 -070056#define BMS1_VSENSE_FOR_R_DATA0 0x82
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070057/* Coulomb counter data */
Xiaozhe Shib19f7032012-08-16 12:14:16 -070058#define BMS1_CC_DATA0 0x8A
Xiaozhe Shif3da8622013-06-10 14:50:56 -070059/* Shadow Coulomb counter data */
60#define BMS1_SW_CC_DATA0 0xA8
Xiaozhe Shib19f7032012-08-16 12:14:16 -070061/* OCV for soc data */
62#define BMS1_OCV_FOR_SOC_DATA0 0x90
Xiaozhe Shib19f7032012-08-16 12:14:16 -070063#define BMS1_VSENSE_PON_DATA0 0x94
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070064#define BMS1_VSENSE_AVG_DATA0 0x98
Xiaozhe Shib19f7032012-08-16 12:14:16 -070065#define BMS1_VBAT_AVG_DATA0 0x9E
Xiaozhe Shib19f7032012-08-16 12:14:16 -070066/* Extra bms registers */
Xiaozhe Shi57058942013-03-27 16:54:54 -070067#define SOC_STORAGE_REG 0xB0
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070068#define IAVG_STORAGE_REG 0xB1
Anirudh Ghayale0c02932013-07-08 16:26:35 +053069#define BMS_FCC_COUNT 0xB2
70#define BMS_FCC_BASE_REG 0xB3 /* FCC updates - 0xB3 to 0xB7 */
71#define BMS_CHGCYL_BASE_REG 0xB8 /* FCC chgcyl - 0xB8 to 0xBC */
72#define CHARGE_INCREASE_STORAGE 0xBD
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053073#define CHARGE_CYCLE_STORAGE_LSB 0xBE /* LSB=0xBE, MSB=0xBF */
74
Xiaozhe Shic40b3972012-11-30 14:11:16 -080075/* IADC Channel Select */
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -070076#define IADC1_BMS_REVISION2 0x01
Xiaozhe Shic40b3972012-11-30 14:11:16 -080077#define IADC1_BMS_ADC_CH_SEL_CTL 0x48
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -070078#define IADC1_BMS_ADC_INT_RSNSN_CTL 0x49
79#define IADC1_BMS_FAST_AVG_EN 0x5B
Xiaozhe Shib19f7032012-08-16 12:14:16 -070080
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070081/* Configuration for saving of shutdown soc/iavg */
82#define IGNORE_SOC_TEMP_DECIDEG 50
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -070083#define IAVG_STEP_SIZE_MA 10
Xiaozhe Shif5f966d2013-02-19 14:23:11 -080084#define IAVG_INVALID 0xFF
Xiaozhe Shif9f99242013-08-29 12:27:50 -070085#define SOC_INVALID 0x7E
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070086
Xiaozhe Shie118c692012-09-24 15:17:43 -070087#define IAVG_SAMPLES 16
88
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053089/* FCC learning constants */
Anirudh Ghayale0c02932013-07-08 16:26:35 +053090#define MAX_FCC_CYCLES 5
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053091#define DELTA_FCC_PERCENT 5
92#define VALID_FCC_CHGCYL_RANGE 50
Anirudh Ghayale0c02932013-07-08 16:26:35 +053093#define CHGCYL_RESOLUTION 20
94#define FCC_DEFAULT_TEMP 250
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053095
Xiaozhe Shib19f7032012-08-16 12:14:16 -070096#define QPNP_BMS_DEV_NAME "qcom,qpnp-bms"
97
Xiaozhe Shif3da8622013-06-10 14:50:56 -070098enum {
99 SHDW_CC,
100 CC
101};
102
103enum {
104 NORESET,
105 RESET
106};
107
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700108struct soc_params {
109 int fcc_uah;
110 int cc_uah;
Xiaozhe Shi904f1f72012-12-04 12:47:21 -0800111 int rbatt_mohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700112 int iavg_ua;
113 int uuc_uah;
114 int ocv_charge_uah;
Xiaozhe Shif36d2862013-01-04 10:17:35 -0800115 int delta_time_s;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700116};
117
118struct raw_soc_params {
119 uint16_t last_good_ocv_raw;
120 int64_t cc;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700121 int64_t shdw_cc;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700122 int last_good_ocv_uv;
123};
124
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530125struct fcc_sample {
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530126 int fcc_new;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530127 int chargecycles;
128};
129
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800130struct bms_irq {
131 unsigned int irq;
132 unsigned long disabled;
Fenglin Wuc17e97b2015-01-20 16:49:58 +0800133 unsigned long wake_enabled;
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800134 bool ready;
Fenglin Wuc17e97b2015-01-20 16:49:58 +0800135 bool is_wake;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800136};
137
138struct bms_wakeup_source {
139 struct wakeup_source source;
140 unsigned long disabled;
141};
142
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700143struct qpnp_bms_chip {
144 struct device *dev;
145 struct power_supply bms_psy;
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -0700146 bool bms_psy_registered;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -0700147 struct power_supply *batt_psy;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700148 struct spmi_device *spmi;
Xiaozhe Shi27375822013-08-22 11:40:15 -0700149 wait_queue_head_t bms_wait_queue;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700150 u16 base;
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800151 u16 iadc_base;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -0700152 u16 batt_pres_addr;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -0700153 u16 soc_storage_addr;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700154
155 u8 revision1;
156 u8 revision2;
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -0700157
158 u8 iadc_bms_revision1;
159 u8 iadc_bms_revision2;
160
Xiaozhe Shid5d21412013-02-06 17:14:41 -0800161 int battery_present;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700162 int battery_status;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700163 bool batfet_closed;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800164 bool new_battery;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700165 bool done_charging;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800166 bool last_soc_invalid;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700167 /* platform data */
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -0800168 int r_sense_uohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700169 unsigned int v_cutoff_uv;
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -0800170 int max_voltage_uv;
171 int r_conn_mohm;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700172 int shutdown_soc_valid_limit;
173 int adjust_soc_low_threshold;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700174 int chg_term_ua;
Xiaozhe Shi73a65692012-09-18 17:51:57 -0700175 enum battery_type batt_type;
Xiaozhe Shi976618f2013-04-30 10:49:30 -0700176 unsigned int fcc_mah;
Xiaozhe Shi73a65692012-09-18 17:51:57 -0700177 struct single_row_lut *fcc_temp_lut;
178 struct single_row_lut *fcc_sf_lut;
179 struct pc_temp_ocv_lut *pc_temp_ocv_lut;
180 struct sf_lut *pc_sf_lut;
181 struct sf_lut *rbatt_sf_lut;
182 int default_rbatt_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -0700183 int rbatt_capacitive_mohm;
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -0700184 int rbatt_mohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700185
186 struct delayed_work calculate_soc_delayed_work;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800187 struct work_struct recalc_work;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700188 struct work_struct batfet_open_work;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700189
190 struct mutex bms_output_lock;
191 struct mutex last_ocv_uv_mutex;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700192 struct mutex vbat_monitor_mutex;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700193 struct mutex soc_invalidation_mutex;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700194 struct mutex last_soc_mutex;
Xiaozhe Shibda84992013-09-05 10:39:11 -0700195 struct mutex status_lock;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700196
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800197 bool use_external_rsense;
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800198 bool use_ocv_thresholds;
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800199
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700200 bool ignore_shutdown_soc;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800201 bool shutdown_soc_invalid;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700202 int shutdown_soc;
203 int shutdown_iavg_ma;
204
Xiaozhe Shi4be85782013-02-22 17:33:40 -0800205 struct wake_lock low_voltage_wake_lock;
Xiaozhe Shi4be85782013-02-22 17:33:40 -0800206 int low_voltage_threshold;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700207 int low_soc_calc_threshold;
208 int low_soc_calculate_soc_ms;
Xiaozhe Shicb487b12013-10-14 17:42:07 -0700209 int low_voltage_calculate_soc_ms;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700210 int calculate_soc_ms;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800211 struct bms_wakeup_source soc_wake_source;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700212 struct wake_lock cv_wake_lock;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700213
Xiaozhe Shie118c692012-09-24 15:17:43 -0700214 uint16_t ocv_reading_at_100;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700215 uint16_t prev_last_good_ocv_raw;
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700216 int insertion_ocv_uv;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700217 int last_ocv_uv;
Xiaozhe Shicc48e992013-05-28 16:42:24 -0700218 int charging_adjusted_ocv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800219 int last_ocv_temp;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700220 int last_cc_uah;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700221 unsigned long last_soc_change_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700222 unsigned long tm_sec;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700223 unsigned long report_tm_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700224 bool first_time_calc_soc;
225 bool first_time_calc_uuc;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700226 int64_t software_cc_uah;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700227 int64_t software_shdw_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700228
229 int iavg_samples_ma[IAVG_SAMPLES];
230 int iavg_index;
231 int iavg_num_samples;
232 struct timespec t_soc_queried;
233 int last_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -0700234 int last_soc_est;
Xiaozhe Shicc137262013-03-10 06:21:41 -0700235 int last_soc_unbound;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700236 bool was_charging_at_sleep;
237 int charge_start_tm_sec;
238 int catch_up_time_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700239 struct single_row_lut *adjusted_fcc_temp_lut;
240
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700241 struct qpnp_adc_tm_btm_param vbat_monitor_params;
Xiaozhe Shi535494d2013-04-05 12:27:51 -0700242 struct qpnp_adc_tm_btm_param die_temp_monitor_params;
243 int temperature_margin;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700244 unsigned int vadc_v0625;
245 unsigned int vadc_v1250;
246
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -0700247 int system_load_count;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700248 int prev_uuc_iavg_ma;
249 int prev_pc_unusable;
250 int ibat_at_cv_ua;
251 int soc_at_cv;
252 int prev_chg_soc;
253 int calculated_soc;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -0800254 int prev_voltage_based_soc;
255 bool use_voltage_soc;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700256 bool in_cv_range;
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800257
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800258 int prev_batt_terminal_uv;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -0700259 int high_ocv_correction_limit_uv;
260 int low_ocv_correction_limit_uv;
261 int flat_ocv_threshold_uv;
262 int hold_soc_est;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800263
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800264 int ocv_high_threshold_uv;
265 int ocv_low_threshold_uv;
Xiaozhe Shicdeee312012-12-18 15:10:18 -0800266 unsigned long last_recalc_time;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530267
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530268 struct fcc_sample *fcc_learning_samples;
269 u8 fcc_sample_count;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530270 int enable_fcc_learning;
271 int min_fcc_learning_soc;
272 int min_fcc_ocv_pc;
273 int min_fcc_learning_samples;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530274 int start_soc;
275 int end_soc;
276 int start_pc;
277 int start_cc_uah;
278 int start_real_soc;
279 int end_cc_uah;
280 uint16_t fcc_new_mah;
281 int fcc_new_batt_temp;
282 uint16_t charge_cycles;
283 u8 charge_increase;
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530284 int fcc_resolution;
285 bool battery_removed;
Zhenhua Huang95a05d32014-03-31 18:09:45 +0800286 bool in_taper_charge;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800287 struct bms_irq sw_cc_thr_irq;
288 struct bms_irq ocv_thr_irq;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700289 struct qpnp_vadc_chip *vadc_dev;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700290 struct qpnp_iadc_chip *iadc_dev;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -0700291 struct qpnp_adc_tm_chip *adc_tm_dev;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700292};
293
294static struct of_device_id qpnp_bms_match_table[] = {
295 { .compatible = QPNP_BMS_DEV_NAME },
296 {}
297};
298
299static char *qpnp_bms_supplicants[] = {
300 "battery"
301};
302
303static enum power_supply_property msm_bms_power_props[] = {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700304 POWER_SUPPLY_PROP_CAPACITY,
Xiaozhe Shibda84992013-09-05 10:39:11 -0700305 POWER_SUPPLY_PROP_STATUS,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700306 POWER_SUPPLY_PROP_CURRENT_NOW,
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -0700307 POWER_SUPPLY_PROP_RESISTANCE,
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -0700308 POWER_SUPPLY_PROP_CHARGE_COUNTER,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700309 POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700310 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +0530311 POWER_SUPPLY_PROP_CHARGE_FULL,
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +0530312 POWER_SUPPLY_PROP_CYCLE_COUNT,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700313};
wangxlee3614a2015-02-03 13:13:11 +0800314#ifdef CONFIG_TCMD
315static struct qpnp_bms_chip *bms_chip;
316#endif
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700317
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530318static int discard_backup_fcc_data(struct qpnp_bms_chip *chip);
319static void backup_charge_cycle(struct qpnp_bms_chip *chip);
320
Xiaozhe Shi20640b52013-01-03 11:49:30 -0800321static bool bms_reset;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -0800322
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700323static int qpnp_read_wrapper(struct qpnp_bms_chip *chip, u8 *val,
324 u16 base, int count)
325{
326 int rc;
327 struct spmi_device *spmi = chip->spmi;
328
329 rc = spmi_ext_register_readl(spmi->ctrl, spmi->sid, base, val, count);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700330 if (rc) {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700331 pr_err("SPMI read failed rc=%d\n", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700332 return rc;
333 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700334 return 0;
335}
336
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700337static int qpnp_write_wrapper(struct qpnp_bms_chip *chip, u8 *val,
338 u16 base, int count)
339{
340 int rc;
341 struct spmi_device *spmi = chip->spmi;
342
343 rc = spmi_ext_register_writel(spmi->ctrl, spmi->sid, base, val, count);
344 if (rc) {
345 pr_err("SPMI write failed rc=%d\n", rc);
346 return rc;
347 }
348 return 0;
349}
350
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800351static int qpnp_masked_write_base(struct qpnp_bms_chip *chip, u16 addr,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700352 u8 mask, u8 val)
353{
354 int rc;
355 u8 reg;
356
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800357 rc = qpnp_read_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700358 if (rc) {
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800359 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700360 return rc;
361 }
362 reg &= ~mask;
363 reg |= val & mask;
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800364 rc = qpnp_write_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700365 if (rc) {
366 pr_err("write failed addr = %03X, val = %02x, mask = %02x, reg = %02x, rc = %d\n",
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800367 addr, val, mask, reg, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700368 return rc;
369 }
370 return 0;
371}
372
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800373static int qpnp_masked_write_iadc(struct qpnp_bms_chip *chip, u16 addr,
374 u8 mask, u8 val)
375{
376 return qpnp_masked_write_base(chip, chip->iadc_base + addr, mask, val);
377}
378
379static int qpnp_masked_write(struct qpnp_bms_chip *chip, u16 addr,
380 u8 mask, u8 val)
381{
382 return qpnp_masked_write_base(chip, chip->base + addr, mask, val);
383}
384
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800385static void bms_stay_awake(struct bms_wakeup_source *source)
386{
387 if (__test_and_clear_bit(0, &source->disabled)) {
388 __pm_stay_awake(&source->source);
389 pr_debug("enabled source %s\n", source->source.name);
390 }
391}
392
393static void bms_relax(struct bms_wakeup_source *source)
394{
395 if (!__test_and_set_bit(0, &source->disabled)) {
396 __pm_relax(&source->source);
397 pr_debug("disabled source %s\n", source->source.name);
398 }
399}
400
401static void enable_bms_irq(struct bms_irq *irq)
402{
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800403 if (irq->ready && __test_and_clear_bit(0, &irq->disabled)) {
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800404 enable_irq(irq->irq);
405 pr_debug("enabled irq %d\n", irq->irq);
Fenglin Wuc17e97b2015-01-20 16:49:58 +0800406 if ((irq->is_wake) &&
407 !__test_and_set_bit(0, &irq->wake_enabled))
408 enable_irq_wake(irq->irq);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800409 }
410}
411
412static void disable_bms_irq(struct bms_irq *irq)
413{
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800414 if (irq->ready && !__test_and_set_bit(0, &irq->disabled)) {
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800415 disable_irq(irq->irq);
416 pr_debug("disabled irq %d\n", irq->irq);
Fenglin Wuc17e97b2015-01-20 16:49:58 +0800417 if ((irq->is_wake) &&
418 __test_and_clear_bit(0, &irq->wake_enabled))
419 disable_irq_wake(irq->irq);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800420 }
421}
422
Anirudh Ghayal1166eef2013-12-23 19:05:33 +0530423static void disable_bms_irq_nosync(struct bms_irq *irq)
424{
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800425 if (irq->ready && !__test_and_set_bit(0, &irq->disabled)) {
Anirudh Ghayal1166eef2013-12-23 19:05:33 +0530426 disable_irq_nosync(irq->irq);
427 pr_debug("disabled irq %d\n", irq->irq);
Fenglin Wuc17e97b2015-01-20 16:49:58 +0800428 if ((irq->is_wake) &&
429 __test_and_clear_bit(0, &irq->wake_enabled))
430 disable_irq_wake(irq->irq);
Anirudh Ghayal1166eef2013-12-23 19:05:33 +0530431 }
432}
433
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700434#define HOLD_OREG_DATA BIT(0)
435static int lock_output_data(struct qpnp_bms_chip *chip)
436{
437 int rc;
438
439 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL,
440 HOLD_OREG_DATA, HOLD_OREG_DATA);
441 if (rc) {
442 pr_err("couldnt lock bms output rc = %d\n", rc);
443 return rc;
444 }
Xiaozhe Shied4a5522014-09-05 14:56:13 -0700445 /*
446 * Sleep for at least 60 microseconds here to make sure there has
447 * been at least two cycles of the sleep clock so that the registers
448 * are correctly locked.
449 */
450 usleep_range(60, 2000);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700451 return 0;
452}
453
454static int unlock_output_data(struct qpnp_bms_chip *chip)
455{
456 int rc;
457
458 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL, HOLD_OREG_DATA, 0);
459 if (rc) {
460 pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc);
461 return rc;
462 }
463 return 0;
464}
465
466#define V_PER_BIT_MUL_FACTOR 97656
467#define V_PER_BIT_DIV_FACTOR 1000
468#define VADC_INTRINSIC_OFFSET 0x6000
469
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800470static int vadc_reading_to_uv(int reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700471{
472 if (reading <= VADC_INTRINSIC_OFFSET)
473 return 0;
474
475 return (reading - VADC_INTRINSIC_OFFSET)
476 * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR;
477}
478
479#define VADC_CALIB_UV 625000
480#define VBATT_MUL_FACTOR 3
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800481static int adjust_vbatt_reading(struct qpnp_bms_chip *chip, int reading_uv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700482{
483 s64 numerator, denominator;
484
485 if (reading_uv == 0)
486 return 0;
487
488 /* don't adjust if not calibrated */
489 if (chip->vadc_v0625 == 0 || chip->vadc_v1250 == 0) {
490 pr_debug("No cal yet return %d\n",
491 VBATT_MUL_FACTOR * reading_uv);
492 return VBATT_MUL_FACTOR * reading_uv;
493 }
494
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700495 numerator = ((s64)reading_uv - chip->vadc_v0625) * VADC_CALIB_UV;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700496 denominator = (s64)chip->vadc_v1250 - chip->vadc_v0625;
497 if (denominator == 0)
498 return reading_uv * VBATT_MUL_FACTOR;
499 return (VADC_CALIB_UV + div_s64(numerator, denominator))
500 * VBATT_MUL_FACTOR;
501}
502
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800503static int convert_vbatt_uv_to_raw(struct qpnp_bms_chip *chip,
504 int unadjusted_vbatt)
505{
506 int scaled_vbatt = unadjusted_vbatt / VBATT_MUL_FACTOR;
507
508 if (scaled_vbatt <= 0)
509 return VADC_INTRINSIC_OFFSET;
510 return ((scaled_vbatt * V_PER_BIT_DIV_FACTOR) / V_PER_BIT_MUL_FACTOR)
511 + VADC_INTRINSIC_OFFSET;
512}
513
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700514static inline int convert_vbatt_raw_to_uv(struct qpnp_bms_chip *chip,
Xiaozhe Shi80754222013-10-30 14:11:41 -0700515 uint16_t reading, bool is_pon_ocv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700516{
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700517 int64_t uv;
518 int rc;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700519
520 uv = vadc_reading_to_uv(reading);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700521 pr_debug("%u raw converted into %lld uv\n", reading, uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700522 uv = adjust_vbatt_reading(chip, uv);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700523 pr_debug("adjusted into %lld uv\n", uv);
Xiaozhe Shi80754222013-10-30 14:11:41 -0700524 rc = qpnp_vbat_sns_comp_result(chip->vadc_dev, &uv, is_pon_ocv);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700525 if (rc)
526 pr_debug("could not compensate vbatt\n");
527 pr_debug("compensated into %lld uv\n", uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700528 return uv;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700529}
530
531#define CC_READING_RESOLUTION_N 542535
532#define CC_READING_RESOLUTION_D 100000
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -0700533static s64 cc_reading_to_uv(s64 reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700534{
535 return div_s64(reading * CC_READING_RESOLUTION_N,
536 CC_READING_RESOLUTION_D);
537}
538
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800539#define QPNP_ADC_GAIN_IDEAL 3291LL
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700540static s64 cc_adjust_for_gain(s64 uv, uint16_t gain)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700541{
542 s64 result_uv;
543
544 pr_debug("adjusting_uv = %lld\n", uv);
Xiaozhe Shi820a47a2012-11-27 13:23:27 -0800545 if (gain == 0) {
546 pr_debug("gain is %d, not adjusting\n", gain);
547 return uv;
548 }
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700549 pr_debug("adjusting by factor: %lld/%hu = %lld%%\n",
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800550 QPNP_ADC_GAIN_IDEAL, gain,
551 div_s64(QPNP_ADC_GAIN_IDEAL * 100LL, (s64)gain));
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700552
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800553 result_uv = div_s64(uv * QPNP_ADC_GAIN_IDEAL, (s64)gain);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700554 pr_debug("result_uv = %lld\n", result_uv);
555 return result_uv;
556}
557
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700558static s64 cc_reverse_adjust_for_gain(struct qpnp_bms_chip *chip, s64 uv)
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800559{
560 struct qpnp_iadc_calib calibration;
561 int gain;
562 s64 result_uv;
563
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700564 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800565 gain = (int)calibration.gain_raw - (int)calibration.offset_raw;
566
567 pr_debug("reverse adjusting_uv = %lld\n", uv);
568 if (gain == 0) {
569 pr_debug("gain is %d, not adjusting\n", gain);
570 return uv;
571 }
572 pr_debug("adjusting by factor: %hu/%lld = %lld%%\n",
573 gain, QPNP_ADC_GAIN_IDEAL,
574 div64_s64((s64)gain * 100LL,
575 (s64)QPNP_ADC_GAIN_IDEAL));
576
577 result_uv = div64_s64(uv * (s64)gain, QPNP_ADC_GAIN_IDEAL);
578 pr_debug("result_uv = %lld\n", result_uv);
579 return result_uv;
580}
581
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700582static int convert_vsense_to_uv(struct qpnp_bms_chip *chip,
583 int16_t reading)
584{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700585 struct qpnp_iadc_calib calibration;
586
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700587 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700588 return cc_adjust_for_gain(cc_reading_to_uv(reading),
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800589 calibration.gain_raw - calibration.offset_raw);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700590}
591
592static int read_vsense_avg(struct qpnp_bms_chip *chip, int *result_uv)
593{
594 int rc;
595 int16_t reading;
596
597 rc = qpnp_read_wrapper(chip, (u8 *)&reading,
598 chip->base + BMS1_VSENSE_AVG_DATA0, 2);
599
600 if (rc) {
601 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
602 return rc;
603 }
604
605 *result_uv = convert_vsense_to_uv(chip, reading);
606 return 0;
607}
608
609static int get_battery_current(struct qpnp_bms_chip *chip, int *result_ua)
610{
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700611 int rc, vsense_uv = 0;
612 int64_t temp_current;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700613
Xiaozhe Shid0a79542012-11-06 10:00:38 -0800614 if (chip->r_sense_uohm == 0) {
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700615 pr_err("r_sense is zero\n");
616 return -EINVAL;
617 }
618
619 mutex_lock(&chip->bms_output_lock);
620 lock_output_data(chip);
621 read_vsense_avg(chip, &vsense_uv);
622 unlock_output_data(chip);
623 mutex_unlock(&chip->bms_output_lock);
624
625 pr_debug("vsense_uv=%duV\n", vsense_uv);
626 /* cast for signed division */
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700627 temp_current = div_s64((vsense_uv * 1000000LL),
628 (int)chip->r_sense_uohm);
629
Xiaozhe Shi4c8458a2013-11-26 13:00:56 -0800630 *result_ua = temp_current;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700631 rc = qpnp_iadc_comp_result(chip->iadc_dev, &temp_current);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700632 if (rc)
633 pr_debug("error compensation failed: %d\n", rc);
634
Xiaozhe Shi4c8458a2013-11-26 13:00:56 -0800635 pr_debug("%d uA err compensated ibat=%llduA\n",
636 *result_ua, temp_current);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700637 *result_ua = temp_current;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700638 return 0;
639}
wangxlee3614a2015-02-03 13:13:11 +0800640#ifdef CONFIG_TCMD
641int tcmd_get_battery_current(int *result_ua)
642{
643 int result_ibat_ua;
644 get_battery_current(bms_chip,&result_ibat_ua);
645 *result_ua = -1 * result_ibat_ua;
646 pr_debug("ibat=%duA\n", *result_ua);
647 return 0;
648}
649#endif
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700650static int get_battery_voltage(struct qpnp_bms_chip *chip, int *result_uv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700651{
652 int rc;
653 struct qpnp_vadc_result adc_result;
654
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700655 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &adc_result);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700656 if (rc) {
657 pr_err("error reading adc channel = %d, rc = %d\n",
658 VBAT_SNS, rc);
659 return rc;
660 }
661 pr_debug("mvolts phy = %lld meas = 0x%llx\n", adc_result.physical,
662 adc_result.measurement);
663 *result_uv = (int)adc_result.physical;
664 return 0;
665}
666
Xiaozhe Shie118c692012-09-24 15:17:43 -0700667#define CC_36_BIT_MASK 0xFFFFFFFFFLL
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800668static uint64_t convert_s64_to_s36(int64_t raw64)
669{
670 return (uint64_t) raw64 & CC_36_BIT_MASK;
671}
672
673#define SIGN_EXTEND_36_TO_64_MASK (-1LL ^ CC_36_BIT_MASK)
674static int64_t convert_s36_to_s64(uint64_t raw36)
675{
676 raw36 = raw36 & CC_36_BIT_MASK;
677 /* convert 36 bit signed value into 64 signed value */
678 return (raw36 >> 35) == 0LL ?
679 raw36 : (SIGN_EXTEND_36_TO_64_MASK | raw36);
680}
681
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700682static int read_cc_raw(struct qpnp_bms_chip *chip, int64_t *reading,
683 int cc_type)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700684{
685 int64_t raw_reading;
686 int rc;
687
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700688 if (cc_type == SHDW_CC)
689 rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
690 chip->base + BMS1_SW_CC_DATA0, 5);
691 else
692 rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
693 chip->base + BMS1_CC_DATA0, 5);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700694 if (rc) {
695 pr_err("Error reading cc: rc = %d\n", rc);
696 return -ENXIO;
697 }
698
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800699 *reading = convert_s36_to_s64(raw_reading);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700700
701 return 0;
702}
703
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700704static int calib_vadc(struct qpnp_bms_chip *chip)
705{
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700706 int rc, raw_0625, raw_1250;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700707 struct qpnp_vadc_result result;
708
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700709 rc = qpnp_vadc_read(chip->vadc_dev, REF_625MV, &result);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700710 if (rc) {
711 pr_debug("vadc read failed with rc = %d\n", rc);
712 return rc;
713 }
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700714 raw_0625 = result.adc_code;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700715
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700716 rc = qpnp_vadc_read(chip->vadc_dev, REF_125V, &result);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700717 if (rc) {
718 pr_debug("vadc read failed with rc = %d\n", rc);
719 return rc;
720 }
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700721 raw_1250 = result.adc_code;
722 chip->vadc_v0625 = vadc_reading_to_uv(raw_0625);
723 chip->vadc_v1250 = vadc_reading_to_uv(raw_1250);
724 pr_debug("vadc calib: 0625 = %d raw (%d uv), 1250 = %d raw (%d uv)\n",
725 raw_0625, chip->vadc_v0625,
726 raw_1250, chip->vadc_v1250);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700727 return 0;
728}
729
Xiaozhe Shie118c692012-09-24 15:17:43 -0700730static void convert_and_store_ocv(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800731 struct raw_soc_params *raw,
Xiaozhe Shi80754222013-10-30 14:11:41 -0700732 int batt_temp, bool is_pon_ocv)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700733{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700734 int rc;
735
736 pr_debug("prev_last_good_ocv_raw = %d, last_good_ocv_raw = %d\n",
737 chip->prev_last_good_ocv_raw,
738 raw->last_good_ocv_raw);
739 rc = calib_vadc(chip);
740 if (rc)
741 pr_err("Vadc reference voltage read failed, rc = %d\n", rc);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700742 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
743 raw->last_good_ocv_uv = convert_vbatt_raw_to_uv(chip,
Xiaozhe Shi80754222013-10-30 14:11:41 -0700744 raw->last_good_ocv_raw, is_pon_ocv);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700745 chip->last_ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800746 chip->last_ocv_temp = batt_temp;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700747 chip->software_cc_uah = 0;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700748 pr_debug("last_good_ocv_uv = %d\n", raw->last_good_ocv_uv);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700749}
750
Xiaozhe Shia045a562012-11-28 16:55:39 -0800751#define CLEAR_CC BIT(7)
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700752#define CLEAR_SHDW_CC BIT(6)
Xiaozhe Shia045a562012-11-28 16:55:39 -0800753/**
754 * reset both cc and sw-cc.
755 * note: this should only be ever called from one thread
756 * or there may be a race condition where CC is never enabled
757 * again
758 */
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700759static void reset_cc(struct qpnp_bms_chip *chip, u8 flags)
Xiaozhe Shia045a562012-11-28 16:55:39 -0800760{
761 int rc;
762
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700763 pr_debug("resetting cc manually with flags %hhu\n", flags);
764 mutex_lock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800765 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700766 flags,
767 flags);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800768 if (rc)
769 pr_err("cc reset failed: %d\n", rc);
770
771 /* wait for 100us for cc to reset */
772 udelay(100);
773
774 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700775 flags, 0);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800776 if (rc)
777 pr_err("cc reenable failed: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700778 mutex_unlock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800779}
780
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700781static int get_battery_status(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800782{
783 union power_supply_propval ret = {0,};
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800784 int rc;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800785
786 if (chip->batt_psy == NULL)
787 chip->batt_psy = power_supply_get_by_name("battery");
788 if (chip->batt_psy) {
789 /* if battery has been registered, use the status property */
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800790 rc = chip->batt_psy->get_property(chip->batt_psy,
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800791 POWER_SUPPLY_PROP_STATUS, &ret);
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800792 if (rc) {
793 pr_debug("Battery does not export status: %d\n", rc);
794 return POWER_SUPPLY_STATUS_UNKNOWN;
795 }
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700796 return ret.intval;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800797 }
798
799 /* Default to false if the battery power supply is not registered. */
800 pr_debug("battery power supply is not registered\n");
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700801 return POWER_SUPPLY_STATUS_UNKNOWN;
802}
803
Zhenhua Huang95a05d32014-03-31 18:09:45 +0800804static int get_battery_charge_type(struct qpnp_bms_chip *chip)
805{
806 union power_supply_propval ret = {0,};
807 int rc;
808
809 if (chip->batt_psy == NULL)
810 chip->batt_psy = power_supply_get_by_name("battery");
811 if (chip->batt_psy) {
812 /* if battery has been registered, use the type property */
813 rc = chip->batt_psy->get_property(chip->batt_psy,
814 POWER_SUPPLY_PROP_CHARGE_TYPE, &ret);
815 if (rc) {
816 pr_debug("Battery does not export charge type: %d\n"
817 , rc);
818 return POWER_SUPPLY_CHARGE_TYPE_NONE;
819 }
820 return ret.intval;
821 }
822
823 /* Default to false if the battery power supply is not registered. */
824 pr_debug("battery power supply is not registered\n");
825 return POWER_SUPPLY_CHARGE_TYPE_NONE;
826}
827
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700828static bool is_battery_charging(struct qpnp_bms_chip *chip)
829{
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800830 return get_battery_status(chip) == POWER_SUPPLY_STATUS_CHARGING;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800831}
832
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700833static bool is_battery_full(struct qpnp_bms_chip *chip)
834{
835 return get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL;
836}
837
Xiaozhe Shi4515c762013-11-13 16:36:54 -0800838#define BAT_PRES_BIT BIT(7)
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700839static bool is_battery_present(struct qpnp_bms_chip *chip)
840{
841 union power_supply_propval ret = {0,};
Xiaozhe Shi4515c762013-11-13 16:36:54 -0800842 int rc;
843 u8 batt_pres;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700844
Xiaozhe Shi4515c762013-11-13 16:36:54 -0800845 /* first try to use the batt_pres register if given */
846 if (chip->batt_pres_addr) {
847 rc = qpnp_read_wrapper(chip, &batt_pres,
848 chip->batt_pres_addr, 1);
849 if (!rc && (batt_pres & BAT_PRES_BIT))
850 return true;
851 else
852 return false;
853 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700854 if (chip->batt_psy == NULL)
855 chip->batt_psy = power_supply_get_by_name("battery");
856 if (chip->batt_psy) {
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700857 /* if battery has been registered, use the present property */
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800858 rc = chip->batt_psy->get_property(chip->batt_psy,
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700859 POWER_SUPPLY_PROP_PRESENT, &ret);
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800860 if (rc) {
861 pr_debug("battery does not export present: %d\n", rc);
862 return true;
863 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700864 return ret.intval;
865 }
866
867 /* Default to false if the battery power supply is not registered. */
868 pr_debug("battery power supply is not registered\n");
869 return false;
870}
871
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700872static int get_battery_insertion_ocv_uv(struct qpnp_bms_chip *chip)
873{
874 union power_supply_propval ret = {0,};
875 int rc, vbat;
876
877 if (chip->batt_psy == NULL)
878 chip->batt_psy = power_supply_get_by_name("battery");
879 if (chip->batt_psy) {
880 /* if battery has been registered, use the ocv property */
881 rc = chip->batt_psy->get_property(chip->batt_psy,
882 POWER_SUPPLY_PROP_VOLTAGE_OCV, &ret);
883 if (rc) {
884 /*
885 * Default to vbatt if the battery OCV is not
886 * registered.
887 */
888 pr_debug("Battery psy does not have voltage ocv\n");
889 rc = get_battery_voltage(chip, &vbat);
890 if (rc)
891 return -EINVAL;
892 return vbat;
893 }
894 return ret.intval;
895 }
896
897 pr_debug("battery power supply is not registered\n");
898 return -EINVAL;
899}
900
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700901static bool is_batfet_closed(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800902{
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700903 union power_supply_propval ret = {0,};
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800904 int rc;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700905
906 if (chip->batt_psy == NULL)
907 chip->batt_psy = power_supply_get_by_name("battery");
908 if (chip->batt_psy) {
909 /* if battery has been registered, use the online property */
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800910 rc = chip->batt_psy->get_property(chip->batt_psy,
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700911 POWER_SUPPLY_PROP_ONLINE, &ret);
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800912 if (rc) {
913 pr_debug("Battery does not export online: %d\n", rc);
914 return true;
915 }
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700916 return !!ret.intval;
917 }
918
919 /* Default to true if the battery power supply is not registered. */
920 pr_debug("battery power supply is not registered\n");
921 return true;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800922}
923
924static int get_simultaneous_batt_v_and_i(struct qpnp_bms_chip *chip,
925 int *ibat_ua, int *vbat_uv)
926{
927 struct qpnp_iadc_result i_result;
928 struct qpnp_vadc_result v_result;
929 enum qpnp_iadc_channels iadc_channel;
930 int rc;
931
932 iadc_channel = chip->use_external_rsense ?
933 EXTERNAL_RSENSE : INTERNAL_RSENSE;
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700934 if (is_battery_full(chip)) {
935 rc = get_battery_current(chip, ibat_ua);
936 if (rc) {
937 pr_err("bms current read failed with rc: %d\n", rc);
938 return rc;
939 }
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700940 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &v_result);
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700941 if (rc) {
942 pr_err("vadc read failed with rc: %d\n", rc);
943 return rc;
944 }
945 *vbat_uv = (int)v_result.physical;
946 } else {
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700947 rc = qpnp_iadc_vadc_sync_read(chip->iadc_dev,
948 iadc_channel, &i_result,
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700949 VBAT_SNS, &v_result);
950 if (rc) {
951 pr_err("adc sync read failed with rc: %d\n", rc);
952 return rc;
953 }
954 /*
955 * reverse the current read by the iadc, since the bms uses
956 * flipped battery current polarity.
957 */
958 *ibat_ua = -1 * (int)i_result.result_ua;
959 *vbat_uv = (int)v_result.physical;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800960 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800961
962 return 0;
963}
964
Xiaozhe Shi3ede2ad2014-02-04 13:25:38 -0800965static int get_rbatt(struct qpnp_bms_chip *chip,
966 int soc_rbatt_mohm, int batt_temp)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800967{
Xiaozhe Shi3ede2ad2014-02-04 13:25:38 -0800968 int rbatt_mohm, scalefactor;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800969
Xiaozhe Shi3ede2ad2014-02-04 13:25:38 -0800970 rbatt_mohm = chip->default_rbatt_mohm;
971 if (chip->rbatt_sf_lut == NULL) {
972 pr_debug("RBATT = %d\n", rbatt_mohm);
973 return rbatt_mohm;
974 }
975 /* Convert the batt_temp to DegC from deciDegC */
976 scalefactor = interpolate_scalingfactor(chip->rbatt_sf_lut,
977 batt_temp, soc_rbatt_mohm);
978 rbatt_mohm = (rbatt_mohm * scalefactor) / 100;
979
980 rbatt_mohm += chip->r_conn_mohm;
981 rbatt_mohm += chip->rbatt_capacitive_mohm;
982 return rbatt_mohm;
983}
984
985#define DEFAULT_RBATT_SOC 50
986static int estimate_ocv(struct qpnp_bms_chip *chip, int batt_temp)
987{
988 int ibat_ua, vbat_uv, ocv_est_uv, rbatt_mohm, rc;
989
990 rbatt_mohm = get_rbatt(chip, DEFAULT_RBATT_SOC, batt_temp);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800991 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
992 if (rc) {
993 pr_err("simultaneous failed rc = %d\n", rc);
994 return rc;
995 }
996
997 ocv_est_uv = vbat_uv + (ibat_ua * rbatt_mohm) / 1000;
Xiaozhe Shi3ede2ad2014-02-04 13:25:38 -0800998 pr_debug("estimated pon ocv = %d, vbat_uv = %d ibat_ua = %d rbatt_mohm = %d\n",
999 ocv_est_uv, vbat_uv, ibat_ua, rbatt_mohm);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001000 return ocv_est_uv;
1001}
1002
Xiaozhe Shi2c171172013-12-03 13:27:37 -08001003#define MIN_IAVG_MA 250
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001004static void reset_for_new_battery(struct qpnp_bms_chip *chip, int batt_temp)
1005{
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07001006 chip->last_ocv_uv = chip->insertion_ocv_uv;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001007 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001008 chip->last_soc = -EINVAL;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001009 chip->last_soc_invalid = true;
1010 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001011 chip->soc_at_cv = -EINVAL;
1012 chip->shutdown_soc_invalid = true;
1013 chip->shutdown_soc = 0;
Xiaozhe Shi2c171172013-12-03 13:27:37 -08001014 chip->shutdown_iavg_ma = MIN_IAVG_MA;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001015 chip->prev_pc_unusable = -EINVAL;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001016 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001017 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001018 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001019 chip->last_cc_uah = INT_MIN;
1020 chip->last_ocv_temp = batt_temp;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001021 chip->prev_batt_terminal_uv = 0;
Anirudh Ghayale0c02932013-07-08 16:26:35 +05301022 if (chip->enable_fcc_learning) {
1023 chip->adjusted_fcc_temp_lut = NULL;
1024 chip->fcc_new_mah = -EINVAL;
1025 /* reset the charge-cycle and charge-increase registers */
1026 chip->charge_increase = 0;
1027 chip->charge_cycles = 0;
1028 backup_charge_cycle(chip);
1029 /* discard all the FCC learnt data and reset the local table */
1030 discard_backup_fcc_data(chip);
1031 memset(chip->fcc_learning_samples, 0,
1032 chip->min_fcc_learning_samples *
1033 sizeof(struct fcc_sample));
1034 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001035}
1036
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001037#define SIGN(x) ((x) < 0 ? -1 : 1)
1038#define UV_PER_SPIN 50000
1039static int find_ocv_for_pc(struct qpnp_bms_chip *chip, int batt_temp, int pc)
1040{
1041 int new_pc;
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001042 int ocv_mv;
1043 int delta_mv = 5;
1044 int max_spin_count;
1045 int count = 0;
1046 int sign, new_sign;
1047
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08001048 ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut, batt_temp, pc);
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001049
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08001050 new_pc = interpolate_pc(chip->pc_temp_ocv_lut, batt_temp, ocv_mv);
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001051 pr_debug("test revlookup pc = %d for ocv = %d\n", new_pc, ocv_mv);
1052 max_spin_count = 1 + (chip->max_voltage_uv - chip->v_cutoff_uv)
1053 / UV_PER_SPIN;
1054 sign = SIGN(pc - new_pc);
1055
1056 while (abs(new_pc - pc) != 0 && count < max_spin_count) {
1057 /*
1058 * If the newly interpolated pc is larger than the lookup pc,
1059 * the ocv should be reduced and vice versa
1060 */
1061 new_sign = SIGN(pc - new_pc);
1062 /*
1063 * If the sign has changed, then we have passed the lookup pc.
1064 * reduce the ocv step size to get finer results.
1065 *
1066 * If we have already reduced the ocv step size and still
1067 * passed the lookup pc, just stop and use the current ocv.
1068 * This can only happen if the batterydata profile is
1069 * non-monotonic anyways.
1070 */
1071 if (new_sign != sign) {
1072 if (delta_mv > 1)
1073 delta_mv = 1;
1074 else
1075 break;
1076 }
1077 sign = new_sign;
1078
1079 ocv_mv = ocv_mv + delta_mv * sign;
1080 new_pc = interpolate_pc(chip->pc_temp_ocv_lut,
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08001081 batt_temp, ocv_mv);
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001082 pr_debug("test revlookup pc = %d for ocv = %d\n",
1083 new_pc, ocv_mv);
1084 count++;
1085 }
1086
1087 return ocv_mv * 1000;
1088}
1089
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08001090#define OCV_RAW_UNINITIALIZED 0xFFFF
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001091#define MIN_OCV_UV 2000000
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001092static int read_soc_params_raw(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001093 struct raw_soc_params *raw,
1094 int batt_temp)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001095{
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001096 int warm_reset, rc;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001097
1098 mutex_lock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -08001099
Xiaozhe Shie118c692012-09-24 15:17:43 -07001100 lock_output_data(chip);
1101
1102 rc = qpnp_read_wrapper(chip, (u8 *)&raw->last_good_ocv_raw,
1103 chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
1104 if (rc) {
1105 pr_err("Error reading ocv: rc = %d\n", rc);
Xiaozhe Shiabbd6072013-12-11 14:24:02 -08001106 goto param_err;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001107 }
1108
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001109 rc = read_cc_raw(chip, &raw->cc, CC);
Xiaozhe Shied4a5522014-09-05 14:56:13 -07001110 rc |= read_cc_raw(chip, &raw->shdw_cc, SHDW_CC);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001111 if (rc) {
1112 pr_err("Failed to read raw cc data, rc = %d\n", rc);
Xiaozhe Shiabbd6072013-12-11 14:24:02 -08001113 goto param_err;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001114 }
1115
1116 unlock_output_data(chip);
1117 mutex_unlock(&chip->bms_output_lock);
1118
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08001119 if (chip->prev_last_good_ocv_raw == OCV_RAW_UNINITIALIZED) {
Xiaozhe Shi80754222013-10-30 14:11:41 -07001120 convert_and_store_ocv(chip, raw, batt_temp, true);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001121 pr_debug("PON_OCV_UV = %d, cc = %llx\n",
1122 chip->last_ocv_uv, raw->cc);
1123 warm_reset = qpnp_pon_is_warm_reset();
Xiaozhe Shi3ede2ad2014-02-04 13:25:38 -08001124 if (raw->last_good_ocv_uv < MIN_OCV_UV || warm_reset > 0) {
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001125 pr_debug("OCV is stale or bad, estimating new OCV.\n");
Xiaozhe Shi3ede2ad2014-02-04 13:25:38 -08001126 chip->last_ocv_uv = estimate_ocv(chip, batt_temp);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001127 raw->last_good_ocv_uv = chip->last_ocv_uv;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001128 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001129 pr_debug("New PON_OCV_UV = %d, cc = %llx\n",
1130 chip->last_ocv_uv, raw->cc);
1131 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001132 } else if (chip->new_battery) {
1133 /* if a new battery was inserted, estimate the ocv */
1134 reset_for_new_battery(chip, batt_temp);
1135 raw->cc = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001136 raw->shdw_cc = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001137 raw->last_good_ocv_uv = chip->last_ocv_uv;
1138 chip->new_battery = false;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001139 } else if (chip->done_charging) {
1140 chip->done_charging = false;
1141 /* if we just finished charging, reset CC and fake 100% */
1142 chip->ocv_reading_at_100 = raw->last_good_ocv_raw;
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001143 chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp, 100);
1144 raw->last_good_ocv_uv = chip->last_ocv_uv;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001145 raw->cc = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001146 raw->shdw_cc = 0;
1147 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001148 chip->last_ocv_temp = batt_temp;
1149 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001150 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001151 chip->last_cc_uah = INT_MIN;
1152 pr_debug("EOC Battery full ocv_reading = 0x%x\n",
1153 chip->ocv_reading_at_100);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001154 } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) {
Xiaozhe Shi80754222013-10-30 14:11:41 -07001155 convert_and_store_ocv(chip, raw, batt_temp, false);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001156 /* forget the old cc value upon ocv */
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001157 chip->last_cc_uah = INT_MIN;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001158 } else {
1159 raw->last_good_ocv_uv = chip->last_ocv_uv;
1160 }
1161
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001162 /* stop faking a high OCV if we get a new OCV */
1163 if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw)
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08001164 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001165
Xiaozhe Shie118c692012-09-24 15:17:43 -07001166 pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n",
1167 raw->last_good_ocv_raw, raw->last_good_ocv_uv);
1168 pr_debug("cc_raw= 0x%llx\n", raw->cc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001169 return 0;
Xiaozhe Shiabbd6072013-12-11 14:24:02 -08001170
1171param_err:
1172 unlock_output_data(chip);
1173 mutex_unlock(&chip->bms_output_lock);
1174 return rc;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001175}
1176
Xiaozhe Shie118c692012-09-24 15:17:43 -07001177static int calculate_pc(struct qpnp_bms_chip *chip, int ocv_uv,
1178 int batt_temp)
1179{
1180 int pc;
1181
1182 pc = interpolate_pc(chip->pc_temp_ocv_lut,
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08001183 batt_temp, ocv_uv / 1000);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001184 pr_debug("pc = %u %% for ocv = %d uv batt_temp = %d\n",
1185 pc, ocv_uv, batt_temp);
1186 /* Multiply the initial FCC value by the scale factor. */
1187 return pc;
1188}
1189
1190static int calculate_fcc(struct qpnp_bms_chip *chip, int batt_temp)
1191{
1192 int fcc_uah;
1193
1194 if (chip->adjusted_fcc_temp_lut == NULL) {
1195 /* interpolate_fcc returns a mv value. */
1196 fcc_uah = interpolate_fcc(chip->fcc_temp_lut,
1197 batt_temp) * 1000;
1198 pr_debug("fcc = %d uAh\n", fcc_uah);
1199 return fcc_uah;
1200 } else {
1201 return 1000 * interpolate_fcc(chip->adjusted_fcc_temp_lut,
1202 batt_temp);
1203 }
1204}
1205
1206/* calculate remaining charge at the time of ocv */
1207static int calculate_ocv_charge(struct qpnp_bms_chip *chip,
1208 struct raw_soc_params *raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001209 int fcc_uah)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001210{
1211 int ocv_uv, pc;
1212
1213 ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001214 pc = calculate_pc(chip, ocv_uv, chip->last_ocv_temp);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001215 pr_debug("ocv_uv = %d pc = %d\n", ocv_uv, pc);
1216 return (fcc_uah * pc) / 100;
1217}
1218
Xiaozhe Shie118c692012-09-24 15:17:43 -07001219#define CC_READING_TICKS 56
1220#define SLEEP_CLK_HZ 32764
1221#define SECONDS_PER_HOUR 3600
1222
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001223static s64 cc_uv_to_pvh(s64 cc_uv)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001224{
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001225 /* Note that it is necessary need to multiply by 1000000 to convert
1226 * from uvh to pvh here.
1227 * However, the maximum Coulomb Counter value is 2^35, which can cause
1228 * an over flow.
1229 * Multiply by 100000 first to perserve as much precision as possible
1230 * then multiply by 10 after doing the division in order to avoid
1231 * overflow on the maximum Coulomb Counter value.
1232 */
1233 return div_s64(cc_uv * CC_READING_TICKS * 100000,
1234 SLEEP_CLK_HZ * SECONDS_PER_HOUR) * 10;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001235}
1236
1237/**
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001238 * calculate_cc() - converts a hardware coulomb counter reading into uah
Xiaozhe Shie118c692012-09-24 15:17:43 -07001239 * @chip: the bms chip pointer
1240 * @cc: the cc reading from bms h/w
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001241 * @cc_type: calcualte cc from regular or shadow coulomb counter
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001242 * @clear_cc: whether this function should clear the hardware counter
1243 * after reading
Xiaozhe Shie118c692012-09-24 15:17:43 -07001244 *
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001245 * Converts the 64 bit hardware coulomb counter into microamp-hour by taking
1246 * into account hardware resolution and adc errors.
1247 *
1248 * Return: the coulomb counter based charge in uAh (micro-amp hour)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001249 */
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001250static int calculate_cc(struct qpnp_bms_chip *chip, int64_t cc,
1251 int cc_type, int clear_cc)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001252{
Xiaozhe Shi4e376652012-10-25 12:38:50 -07001253 struct qpnp_iadc_calib calibration;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001254 struct qpnp_vadc_result result;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001255 int64_t cc_voltage_uv, cc_pvh, cc_uah, *software_counter;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001256 int rc;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001257
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001258 software_counter = cc_type == SHDW_CC ?
1259 &chip->software_shdw_cc_uah : &chip->software_cc_uah;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001260 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001261 if (rc) {
1262 pr_err("could not read pmic die temperature: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001263 return *software_counter;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001264 }
1265
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07001266 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001267 pr_debug("%scc = %lld, die_temp = %lld\n",
1268 cc_type == SHDW_CC ? "shdw_" : "",
1269 cc, result.physical);
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -07001270 cc_voltage_uv = cc_reading_to_uv(cc);
Xiaozhe Shi0c484932013-02-05 16:14:10 -08001271 cc_voltage_uv = cc_adjust_for_gain(cc_voltage_uv,
1272 calibration.gain_raw
1273 - calibration.offset_raw);
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001274 cc_pvh = cc_uv_to_pvh(cc_voltage_uv);
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001275 cc_uah = div_s64(cc_pvh, chip->r_sense_uohm);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07001276 rc = qpnp_iadc_comp_result(chip->iadc_dev, &cc_uah);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001277 if (rc)
1278 pr_debug("error compensation failed: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001279 if (clear_cc == RESET) {
1280 pr_debug("software_%scc = %lld, added cc_uah = %lld\n",
1281 cc_type == SHDW_CC ? "sw_" : "",
1282 *software_counter, cc_uah);
1283 *software_counter += cc_uah;
1284 reset_cc(chip, cc_type == SHDW_CC ? CLEAR_SHDW_CC : CLEAR_CC);
1285 return (int)*software_counter;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001286 } else {
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001287 pr_debug("software_%scc = %lld, cc_uah = %lld, total = %lld\n",
1288 cc_type == SHDW_CC ? "shdw_" : "",
1289 *software_counter, cc_uah,
1290 *software_counter + cc_uah);
1291 return *software_counter + cc_uah;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001292 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07001293}
1294
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -07001295#define IAVG_MINIMAL_TIME 2
Xiaozhe Shie118c692012-09-24 15:17:43 -07001296static void calculate_iavg(struct qpnp_bms_chip *chip, int cc_uah,
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001297 int *iavg_ua, int delta_time_s)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001298{
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001299 int delta_cc_uah = 0;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001300
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -07001301 /*
1302 * use the battery current if called too quickly
1303 */
1304 if (delta_time_s < IAVG_MINIMAL_TIME
1305 || chip->last_cc_uah == INT_MIN) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001306 get_battery_current(chip, iavg_ua);
1307 goto out;
1308 }
1309
Xiaozhe Shie118c692012-09-24 15:17:43 -07001310 delta_cc_uah = cc_uah - chip->last_cc_uah;
1311
1312 *iavg_ua = div_s64((s64)delta_cc_uah * 3600, delta_time_s);
1313
Xiaozhe Shie118c692012-09-24 15:17:43 -07001314out:
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001315 pr_debug("delta_cc = %d iavg_ua = %d\n", delta_cc_uah, (int)*iavg_ua);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001316
1317 /* remember cc_uah */
1318 chip->last_cc_uah = cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001319}
1320
1321static int calculate_termination_uuc(struct qpnp_bms_chip *chip,
1322 struct soc_params *params,
1323 int batt_temp, int uuc_iavg_ma,
1324 int *ret_pc_unusable)
1325{
1326 int unusable_uv, pc_unusable, uuc_uah;
1327 int i = 0;
1328 int ocv_mv;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001329 int rbatt_mohm;
1330 int delta_uv;
1331 int prev_delta_uv = 0;
1332 int prev_rbatt_mohm = 0;
1333 int uuc_rbatt_mohm;
1334
1335 for (i = 0; i <= 100; i++) {
1336 ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08001337 batt_temp, i);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001338 rbatt_mohm = get_rbatt(chip, i, batt_temp);
1339 unusable_uv = (rbatt_mohm * uuc_iavg_ma)
1340 + (chip->v_cutoff_uv);
1341 delta_uv = ocv_mv * 1000 - unusable_uv;
1342
Xiaozhe Shie118c692012-09-24 15:17:43 -07001343 if (delta_uv > 0)
1344 break;
1345
1346 prev_delta_uv = delta_uv;
1347 prev_rbatt_mohm = rbatt_mohm;
1348 }
1349
1350 uuc_rbatt_mohm = linear_interpolate(rbatt_mohm, delta_uv,
1351 prev_rbatt_mohm, prev_delta_uv,
1352 0);
1353
1354 unusable_uv = (uuc_rbatt_mohm * uuc_iavg_ma) + (chip->v_cutoff_uv);
1355
1356 pc_unusable = calculate_pc(chip, unusable_uv, batt_temp);
1357 uuc_uah = (params->fcc_uah * pc_unusable) / 100;
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001358 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 -07001359 uuc_iavg_ma,
1360 uuc_rbatt_mohm, unusable_uv,
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001361 pc_unusable, i, uuc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001362 *ret_pc_unusable = pc_unusable;
1363 return uuc_uah;
1364}
1365
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001366#define TIME_PER_PERCENT_UUC 60
Xiaozhe Shie118c692012-09-24 15:17:43 -07001367static int adjust_uuc(struct qpnp_bms_chip *chip,
1368 struct soc_params *params,
1369 int new_pc_unusable,
1370 int new_uuc_uah,
1371 int batt_temp)
1372{
1373 int new_unusable_mv, new_iavg_ma;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001374 int max_percent_change;
1375
1376 max_percent_change = max(params->delta_time_s
1377 / TIME_PER_PERCENT_UUC, 1);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001378
Xiaozhe Shi2c171172013-12-03 13:27:37 -08001379 if (chip->first_time_calc_uuc || chip->prev_pc_unusable == -EINVAL
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001380 || abs(chip->prev_pc_unusable - new_pc_unusable)
1381 <= max_percent_change) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001382 chip->prev_pc_unusable = new_pc_unusable;
1383 return new_uuc_uah;
1384 }
1385
1386 /* the uuc is trying to change more than 1% restrict it */
1387 if (new_pc_unusable > chip->prev_pc_unusable)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001388 chip->prev_pc_unusable += max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001389 else
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001390 chip->prev_pc_unusable -= max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001391
1392 new_uuc_uah = (params->fcc_uah * chip->prev_pc_unusable) / 100;
1393
1394 /* also find update the iavg_ma accordingly */
1395 new_unusable_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08001396 batt_temp, chip->prev_pc_unusable);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001397 if (new_unusable_mv < chip->v_cutoff_uv/1000)
1398 new_unusable_mv = chip->v_cutoff_uv/1000;
1399
1400 new_iavg_ma = (new_unusable_mv * 1000 - chip->v_cutoff_uv)
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001401 / params->rbatt_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001402 if (new_iavg_ma == 0)
1403 new_iavg_ma = 1;
1404 chip->prev_uuc_iavg_ma = new_iavg_ma;
1405 pr_debug("Restricting UUC to %d (%d%%) unusable_mv = %d iavg_ma = %d\n",
1406 new_uuc_uah, chip->prev_pc_unusable,
1407 new_unusable_mv, new_iavg_ma);
1408
1409 return new_uuc_uah;
1410}
1411
Xiaozhe Shie118c692012-09-24 15:17:43 -07001412static int calculate_unusable_charge_uah(struct qpnp_bms_chip *chip,
1413 struct soc_params *params,
1414 int batt_temp)
1415{
1416 int uuc_uah_iavg;
1417 int i;
1418 int uuc_iavg_ma = params->iavg_ua / 1000;
1419 int pc_unusable;
1420
1421 /*
1422 * if called first time, fill all the samples with
1423 * the shutdown_iavg_ma
1424 */
1425 if (chip->first_time_calc_uuc && chip->shutdown_iavg_ma != 0) {
1426 pr_debug("Using shutdown_iavg_ma = %d in all samples\n",
1427 chip->shutdown_iavg_ma);
1428 for (i = 0; i < IAVG_SAMPLES; i++)
1429 chip->iavg_samples_ma[i] = chip->shutdown_iavg_ma;
1430
1431 chip->iavg_index = 0;
1432 chip->iavg_num_samples = IAVG_SAMPLES;
1433 }
1434
Xiaozhe Shi70633922013-09-23 15:50:53 -07001435 if (params->delta_time_s >= IAVG_MINIMAL_TIME) {
1436 /*
1437 * if charging use a nominal avg current to keep
1438 * a reasonable UUC while charging
1439 */
1440 if (uuc_iavg_ma < MIN_IAVG_MA)
1441 uuc_iavg_ma = MIN_IAVG_MA;
1442 chip->iavg_samples_ma[chip->iavg_index] = uuc_iavg_ma;
1443 chip->iavg_index = (chip->iavg_index + 1) % IAVG_SAMPLES;
1444 chip->iavg_num_samples++;
1445 if (chip->iavg_num_samples >= IAVG_SAMPLES)
1446 chip->iavg_num_samples = IAVG_SAMPLES;
1447 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07001448
1449 /* now that this sample is added calcualte the average */
1450 uuc_iavg_ma = 0;
1451 if (chip->iavg_num_samples != 0) {
1452 for (i = 0; i < chip->iavg_num_samples; i++) {
1453 pr_debug("iavg_samples_ma[%d] = %d\n", i,
1454 chip->iavg_samples_ma[i]);
1455 uuc_iavg_ma += chip->iavg_samples_ma[i];
1456 }
1457
1458 uuc_iavg_ma = DIV_ROUND_CLOSEST(uuc_iavg_ma,
1459 chip->iavg_num_samples);
1460 }
1461
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001462 /*
1463 * if we're in bms reset mode, force uuc to be 3% of fcc
1464 */
1465 if (bms_reset)
1466 return (params->fcc_uah * 3) / 100;
1467
Xiaozhe Shi75e5efe2013-02-07 09:51:43 -08001468 uuc_uah_iavg = calculate_termination_uuc(chip, params, batt_temp,
1469 uuc_iavg_ma, &pc_unusable);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001470 pr_debug("uuc_iavg_ma = %d uuc with iavg = %d\n",
1471 uuc_iavg_ma, uuc_uah_iavg);
1472
1473 chip->prev_uuc_iavg_ma = uuc_iavg_ma;
1474 /* restrict the uuc such that it can increase only by one percent */
1475 uuc_uah_iavg = adjust_uuc(chip, params, pc_unusable,
1476 uuc_uah_iavg, batt_temp);
1477
Xiaozhe Shie118c692012-09-24 15:17:43 -07001478 return uuc_uah_iavg;
1479}
1480
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001481static s64 find_ocv_charge_for_soc(struct qpnp_bms_chip *chip,
1482 struct soc_params *params, int soc)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001483{
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001484 return div_s64((s64)soc * (params->fcc_uah - params->uuc_uah),
1485 100) + params->cc_uah + params->uuc_uah;
1486}
Xiaozhe Shie118c692012-09-24 15:17:43 -07001487
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001488static int find_pc_for_soc(struct qpnp_bms_chip *chip,
1489 struct soc_params *params, int soc)
1490{
1491 int ocv_charge_uah = find_ocv_charge_for_soc(chip, params, soc);
1492 int pc;
1493
Xiaozhe Shie118c692012-09-24 15:17:43 -07001494 pc = DIV_ROUND_CLOSEST((int)ocv_charge_uah * 100, params->fcc_uah);
1495 pc = clamp(pc, 0, 100);
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001496 pr_debug("soc = %d, fcc = %d uuc = %d rc = %d pc = %d\n",
1497 soc, params->fcc_uah, params->uuc_uah,
1498 ocv_charge_uah, pc);
1499 return pc;
1500}
Xiaozhe Shie118c692012-09-24 15:17:43 -07001501
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001502static int get_current_time(unsigned long *now_tm_sec)
1503{
1504 struct rtc_time tm;
1505 struct rtc_device *rtc;
1506 int rc;
1507
1508 rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
1509 if (rtc == NULL) {
1510 pr_err("%s: unable to open rtc device (%s)\n",
1511 __FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
Xiaozhe Shi0e01af62013-05-06 12:56:08 -07001512 return -EINVAL;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001513 }
1514
1515 rc = rtc_read_time(rtc, &tm);
1516 if (rc) {
1517 pr_err("Error reading rtc device (%s) : %d\n",
1518 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1519 goto close_time;
1520 }
1521
1522 rc = rtc_valid_tm(&tm);
1523 if (rc) {
1524 pr_err("Invalid RTC time (%s): %d\n",
1525 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1526 goto close_time;
1527 }
1528 rtc_tm_to_time(&tm, now_tm_sec);
1529
1530close_time:
1531 rtc_class_close(rtc);
1532 return rc;
1533}
1534
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08001535/* Returns estimated battery resistance */
1536static int get_prop_bms_batt_resistance(struct qpnp_bms_chip *chip)
1537{
1538 return chip->rbatt_mohm * 1000;
1539}
1540
1541/* Returns instantaneous current in uA */
1542static int get_prop_bms_current_now(struct qpnp_bms_chip *chip)
1543{
1544 int rc, result_ua;
1545
1546 rc = get_battery_current(chip, &result_ua);
1547 if (rc) {
1548 pr_err("failed to get current: %d\n", rc);
1549 return rc;
1550 }
1551 return result_ua;
1552}
1553
1554/* Returns coulomb counter in uAh */
1555static int get_prop_bms_charge_counter(struct qpnp_bms_chip *chip)
1556{
1557 int64_t cc_raw;
1558
1559 mutex_lock(&chip->bms_output_lock);
1560 lock_output_data(chip);
Xiaozhe Shie11c9492013-12-11 14:37:36 -08001561 read_cc_raw(chip, &cc_raw, CC);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08001562 unlock_output_data(chip);
1563 mutex_unlock(&chip->bms_output_lock);
1564
1565 return calculate_cc(chip, cc_raw, CC, NORESET);
1566}
1567
1568/* Returns shadow coulomb counter in uAh */
1569static int get_prop_bms_charge_counter_shadow(struct qpnp_bms_chip *chip)
1570{
1571 int64_t cc_raw;
1572
1573 mutex_lock(&chip->bms_output_lock);
1574 lock_output_data(chip);
Xiaozhe Shie11c9492013-12-11 14:37:36 -08001575 read_cc_raw(chip, &cc_raw, SHDW_CC);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08001576 unlock_output_data(chip);
1577 mutex_unlock(&chip->bms_output_lock);
1578
1579 return calculate_cc(chip, cc_raw, SHDW_CC, NORESET);
1580}
1581
1582/* Returns full charge design in uAh */
1583static int get_prop_bms_charge_full_design(struct qpnp_bms_chip *chip)
1584{
1585 return chip->fcc_mah * 1000;
1586}
1587
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05301588/* Returns the current full charge in uAh */
1589static int get_prop_bms_charge_full(struct qpnp_bms_chip *chip)
1590{
1591 int rc;
1592 struct qpnp_vadc_result result;
1593
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001594 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05301595 if (rc) {
1596 pr_err("Unable to read battery temperature\n");
1597 return rc;
1598 }
1599
1600 return calculate_fcc(chip, (int)result.physical);
1601}
1602
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001603static int calculate_delta_time(unsigned long *time_stamp, int *delta_time_s)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001604{
1605 unsigned long now_tm_sec = 0;
1606
1607 /* default to delta time = 0 if anything fails */
1608 *delta_time_s = 0;
1609
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001610 if (get_current_time(&now_tm_sec)) {
1611 pr_err("RTC read failed\n");
1612 return 0;
1613 }
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001614
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001615 *delta_time_s = (now_tm_sec - *time_stamp);
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001616
1617 /* remember this time */
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001618 *time_stamp = now_tm_sec;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001619 return 0;
1620}
1621
Xiaozhe Shie118c692012-09-24 15:17:43 -07001622static void calculate_soc_params(struct qpnp_bms_chip *chip,
1623 struct raw_soc_params *raw,
1624 struct soc_params *params,
1625 int batt_temp)
1626{
Xiaozhe Shi219cb222013-06-10 15:49:59 -07001627 int soc_rbatt, shdw_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001628
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001629 calculate_delta_time(&chip->tm_sec, &params->delta_time_s);
1630 pr_debug("tm_sec = %ld, delta_s = %d\n",
1631 chip->tm_sec, params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001632 params->fcc_uah = calculate_fcc(chip, batt_temp);
1633 pr_debug("FCC = %uuAh batt_temp = %d\n", params->fcc_uah, batt_temp);
1634
1635 /* calculate remainging charge */
1636 params->ocv_charge_uah = calculate_ocv_charge(
1637 chip, raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001638 params->fcc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001639 pr_debug("ocv_charge_uah = %uuAh\n", params->ocv_charge_uah);
1640
1641 /* calculate cc micro_volt_hour */
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001642 params->cc_uah = calculate_cc(chip, raw->cc, CC, RESET);
Xiaozhe Shi219cb222013-06-10 15:49:59 -07001643 shdw_cc_uah = calculate_cc(chip, raw->shdw_cc, SHDW_CC, RESET);
1644 pr_debug("cc_uah = %duAh raw->cc = %llx, shdw_cc_uah = %duAh raw->shdw_cc = %llx\n",
1645 params->cc_uah, raw->cc,
1646 shdw_cc_uah, raw->shdw_cc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001647
1648 soc_rbatt = ((params->ocv_charge_uah - params->cc_uah) * 100)
1649 / params->fcc_uah;
1650 if (soc_rbatt < 0)
1651 soc_rbatt = 0;
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001652 params->rbatt_mohm = get_rbatt(chip, soc_rbatt, batt_temp);
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001653 pr_debug("rbatt_mohm = %d\n", params->rbatt_mohm);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001654
Xiaozhe Shi1e87cda2013-05-17 10:18:56 -07001655 if (params->rbatt_mohm != chip->rbatt_mohm) {
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07001656 chip->rbatt_mohm = params->rbatt_mohm;
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07001657 if (chip->bms_psy_registered)
Xiaozhe Shi1e87cda2013-05-17 10:18:56 -07001658 power_supply_changed(&chip->bms_psy);
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07001659 }
1660
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001661 calculate_iavg(chip, params->cc_uah, &params->iavg_ua,
1662 params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001663
1664 params->uuc_uah = calculate_unusable_charge_uah(chip, params,
1665 batt_temp);
1666 pr_debug("UUC = %uuAh\n", params->uuc_uah);
1667}
1668
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001669static int bound_soc(int soc)
1670{
1671 soc = max(0, soc);
1672 soc = min(100, soc);
1673 return soc;
1674}
1675
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001676#define IBAT_TOL_MASK 0x0F
1677#define OCV_TOL_MASK 0xF0
1678#define IBAT_TOL_DEFAULT 0x03
1679#define IBAT_TOL_NOCHG 0x0F
1680#define OCV_TOL_DEFAULT 0x20
1681#define OCV_TOL_NO_OCV 0x00
1682static int stop_ocv_updates(struct qpnp_bms_chip *chip)
1683{
1684 pr_debug("stopping ocv updates\n");
1685 return qpnp_masked_write(chip, BMS1_TOL_CTL,
1686 OCV_TOL_MASK, OCV_TOL_NO_OCV);
1687}
1688
1689static int reset_bms_for_test(struct qpnp_bms_chip *chip)
1690{
Xiaozhe Shi95da77f2013-02-20 13:40:06 -08001691 int ibat_ua = 0, vbat_uv = 0, rc;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001692 int ocv_est_uv;
1693
1694 if (!chip) {
1695 pr_err("BMS driver has not been initialized yet!\n");
1696 return -EINVAL;
1697 }
1698
1699 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
1700
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07001701 /*
1702 * Don't include rbatt and rbatt_capacitative since we expect this to
1703 * be used with a fake battery which does not have internal resistances
1704 */
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001705 ocv_est_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
1706 pr_debug("forcing ocv to be %d due to bms reset mode\n", ocv_est_uv);
1707 chip->last_ocv_uv = ocv_est_uv;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001708 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001709 chip->last_soc = -EINVAL;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001710 chip->last_soc_invalid = true;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001711 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001712 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001713 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001714 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001715 chip->last_cc_uah = INT_MIN;
1716 stop_ocv_updates(chip);
1717
1718 pr_debug("bms reset to ocv = %duv vbat_ua = %d ibat_ua = %d\n",
1719 chip->last_ocv_uv, vbat_uv, ibat_ua);
1720
1721 return rc;
1722}
1723
1724static int bms_reset_set(const char *val, const struct kernel_param *kp)
1725{
1726 int rc;
1727
1728 rc = param_set_bool(val, kp);
1729 if (rc) {
1730 pr_err("Unable to set bms_reset: %d\n", rc);
1731 return rc;
1732 }
1733
1734 if (*(bool *)kp->arg) {
1735 struct power_supply *bms_psy = power_supply_get_by_name("bms");
1736 struct qpnp_bms_chip *chip = container_of(bms_psy,
1737 struct qpnp_bms_chip, bms_psy);
1738
1739 rc = reset_bms_for_test(chip);
1740 if (rc) {
1741 pr_err("Unable to modify bms_reset: %d\n", rc);
1742 return rc;
1743 }
1744 }
1745 return 0;
1746}
1747
1748static struct kernel_param_ops bms_reset_ops = {
1749 .set = bms_reset_set,
1750 .get = param_get_bool,
1751};
1752
1753module_param_cb(bms_reset, &bms_reset_ops, &bms_reset, 0644);
1754
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001755#define SOC_STORAGE_MASK 0xFE
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001756static void backup_soc_and_iavg(struct qpnp_bms_chip *chip, int batt_temp,
1757 int soc)
1758{
1759 u8 temp;
1760 int rc;
1761 int iavg_ma = chip->prev_uuc_iavg_ma;
1762
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07001763 if (iavg_ma > MIN_IAVG_MA)
1764 temp = (iavg_ma - MIN_IAVG_MA) / IAVG_STEP_SIZE_MA;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001765 else
1766 temp = 0;
1767
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001768 rc = qpnp_write_wrapper(chip, &temp, chip->base + IAVG_STORAGE_REG, 1);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001769
Xiaozhe Shie945a8a2013-11-11 10:20:14 -08001770 /* store an invalid soc if temperature is below 5degC */
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001771 if (batt_temp > IGNORE_SOC_TEMP_DECIDEG)
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001772 qpnp_masked_write_base(chip, chip->soc_storage_addr,
1773 SOC_STORAGE_MASK, (soc + 1) << 1);
Xiaozhe Shie945a8a2013-11-11 10:20:14 -08001774 else
1775 qpnp_masked_write_base(chip, chip->soc_storage_addr,
1776 SOC_STORAGE_MASK, SOC_STORAGE_MASK);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001777}
1778
1779static int scale_soc_while_chg(struct qpnp_bms_chip *chip, int chg_time_sec,
1780 int catch_up_sec, int new_soc, int prev_soc)
1781{
1782 int scaled_soc;
1783 int numerator;
1784
1785 /*
1786 * Don't report a high value immediately slowly scale the
1787 * value from prev_soc to the new soc based on a charge time
1788 * weighted average
1789 */
1790 pr_debug("cts = %d catch_up_sec = %d\n", chg_time_sec, catch_up_sec);
1791 if (catch_up_sec == 0)
1792 return new_soc;
1793
1794 if (chg_time_sec > catch_up_sec)
1795 return new_soc;
1796
1797 numerator = (catch_up_sec - chg_time_sec) * prev_soc
1798 + chg_time_sec * new_soc;
1799 scaled_soc = numerator / catch_up_sec;
1800
1801 pr_debug("cts = %d new_soc = %d prev_soc = %d scaled_soc = %d\n",
1802 chg_time_sec, new_soc, prev_soc, scaled_soc);
1803
1804 return scaled_soc;
1805}
1806
1807/*
1808 * bms_fake_battery is set in setups where a battery emulator is used instead
1809 * of a real battery. This makes the bms driver report a different/fake value
1810 * regardless of the calculated state of charge.
1811 */
1812static int bms_fake_battery = -EINVAL;
1813module_param(bms_fake_battery, int, 0644);
1814
1815static int report_voltage_based_soc(struct qpnp_bms_chip *chip)
1816{
1817 pr_debug("Reported voltage based soc = %d\n",
1818 chip->prev_voltage_based_soc);
1819 return chip->prev_voltage_based_soc;
1820}
1821
1822#define SOC_CATCHUP_SEC_MAX 600
1823#define SOC_CATCHUP_SEC_PER_PERCENT 60
1824#define MAX_CATCHUP_SOC (SOC_CATCHUP_SEC_MAX / SOC_CATCHUP_SEC_PER_PERCENT)
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07001825#define SOC_CHANGE_PER_SEC 5
Xiaozhe Shi27375822013-08-22 11:40:15 -07001826#define REPORT_SOC_WAIT_MS 10000
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001827static int report_cc_based_soc(struct qpnp_bms_chip *chip)
1828{
1829 int soc, soc_change;
1830 int time_since_last_change_sec, charge_time_sec = 0;
1831 unsigned long last_change_sec;
1832 struct timespec now;
1833 struct qpnp_vadc_result result;
1834 int batt_temp;
1835 int rc;
1836 bool charging, charging_since_last_report;
1837
Xiaozhe Shi27375822013-08-22 11:40:15 -07001838 rc = wait_event_interruptible_timeout(chip->bms_wait_queue,
1839 chip->calculated_soc != -EINVAL,
1840 round_jiffies_relative(msecs_to_jiffies
1841 (REPORT_SOC_WAIT_MS)));
1842
1843 if (rc == 0 && chip->calculated_soc == -EINVAL) {
1844 pr_debug("calculate soc timed out\n");
1845 } else if (rc == -ERESTARTSYS) {
1846 pr_err("Wait for SoC interrupted.\n");
1847 return rc;
1848 }
1849
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001850 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001851
1852 if (rc) {
1853 pr_err("error reading adc channel = %d, rc = %d\n",
1854 LR_MUX1_BATT_THERM, rc);
1855 return rc;
1856 }
1857 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
1858 result.measurement);
1859 batt_temp = (int)result.physical;
1860
1861 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shifa6ea692013-05-31 11:15:13 -07001862 soc = chip->calculated_soc;
1863
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001864 last_change_sec = chip->last_soc_change_sec;
1865 calculate_delta_time(&last_change_sec, &time_since_last_change_sec);
1866
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -08001867 charging = chip->battery_status == POWER_SUPPLY_STATUS_CHARGING;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001868 charging_since_last_report = charging || (chip->last_soc_unbound
1869 && chip->was_charging_at_sleep);
1870 /*
1871 * account for charge time - limit it to SOC_CATCHUP_SEC to
1872 * avoid overflows when charging continues for extended periods
1873 */
1874 if (charging) {
1875 if (chip->charge_start_tm_sec == 0) {
1876 /*
1877 * calculating soc for the first time
1878 * after start of chg. Initialize catchup time
1879 */
1880 if (abs(soc - chip->last_soc) < MAX_CATCHUP_SOC)
1881 chip->catch_up_time_sec =
1882 (soc - chip->last_soc)
1883 * SOC_CATCHUP_SEC_PER_PERCENT;
1884 else
1885 chip->catch_up_time_sec = SOC_CATCHUP_SEC_MAX;
1886
1887 if (chip->catch_up_time_sec < 0)
1888 chip->catch_up_time_sec = 0;
1889 chip->charge_start_tm_sec = last_change_sec;
1890 }
1891
1892 charge_time_sec = min(SOC_CATCHUP_SEC_MAX, (int)last_change_sec
1893 - chip->charge_start_tm_sec);
1894
1895 /* end catchup if calculated soc and last soc are same */
1896 if (chip->last_soc == soc)
1897 chip->catch_up_time_sec = 0;
1898 }
1899
1900 if (chip->last_soc != -EINVAL) {
1901 /*
1902 * last_soc < soc ... if we have not been charging at all
1903 * since the last time this was called, report previous SoC.
1904 * Otherwise, scale and catch up.
1905 */
1906 if (chip->last_soc < soc && !charging_since_last_report)
1907 soc = chip->last_soc;
1908 else if (chip->last_soc < soc && soc != 100)
1909 soc = scale_soc_while_chg(chip, charge_time_sec,
1910 chip->catch_up_time_sec,
1911 soc, chip->last_soc);
1912
Xiaozhe Shibd56b052013-10-21 11:51:30 -07001913 /* if the battery is close to cutoff allow more change */
1914 if (wake_lock_active(&chip->low_voltage_wake_lock))
1915 soc_change = min((int)abs(chip->last_soc - soc),
1916 time_since_last_change_sec);
1917 else
1918 soc_change = min((int)abs(chip->last_soc - soc),
1919 time_since_last_change_sec
1920 / SOC_CHANGE_PER_SEC);
1921
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001922 if (chip->last_soc_unbound) {
1923 chip->last_soc_unbound = false;
1924 } else {
1925 /*
1926 * if soc have not been unbound by resume,
1927 * only change reported SoC by 1.
1928 */
1929 soc_change = min(1, soc_change);
1930 }
1931
1932 if (soc < chip->last_soc && soc != 0)
1933 soc = chip->last_soc - soc_change;
1934 if (soc > chip->last_soc && soc != 100)
1935 soc = chip->last_soc + soc_change;
1936 }
1937
Xiaozhe Shi208b8e52013-05-28 10:16:32 -07001938 if (chip->last_soc != soc && !chip->last_soc_unbound)
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001939 chip->last_soc_change_sec = last_change_sec;
1940
1941 pr_debug("last_soc = %d, calculated_soc = %d, soc = %d, time since last change = %d\n",
1942 chip->last_soc, chip->calculated_soc,
1943 soc, time_since_last_change_sec);
1944 chip->last_soc = bound_soc(soc);
1945 backup_soc_and_iavg(chip, batt_temp, chip->last_soc);
1946 pr_debug("Reported SOC = %d\n", chip->last_soc);
1947 chip->t_soc_queried = now;
1948 mutex_unlock(&chip->last_soc_mutex);
1949
1950 return soc;
1951}
1952
1953static int report_state_of_charge(struct qpnp_bms_chip *chip)
1954{
1955 if (bms_fake_battery != -EINVAL) {
1956 pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery);
1957 return bms_fake_battery;
1958 } else if (chip->use_voltage_soc)
1959 return report_voltage_based_soc(chip);
1960 else
1961 return report_cc_based_soc(chip);
1962}
1963
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07001964#define VDD_MAX_ERR 5000
1965#define VDD_STEP_SIZE 10000
1966#define MAX_COUNT_BEFORE_RESET_TO_CC 3
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001967static int charging_adjustments(struct qpnp_bms_chip *chip,
1968 struct soc_params *params, int soc,
1969 int vbat_uv, int ibat_ua, int batt_temp)
1970{
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001971 int chg_soc, soc_ibat, batt_terminal_uv, weight_ibat, weight_cc;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001972
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001973 batt_terminal_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001974
1975 if (chip->soc_at_cv == -EINVAL) {
Zhenhua Huang95a05d32014-03-31 18:09:45 +08001976 if (batt_terminal_uv >= chip->max_voltage_uv - VDD_MAX_ERR ||
1977 chip->in_taper_charge) {
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001978 chip->soc_at_cv = soc;
1979 chip->prev_chg_soc = soc;
Xiaozhe Shifc7af172013-11-04 14:15:44 -08001980 chip->ibat_at_cv_ua = params->iavg_ua;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001981 pr_debug("CC_TO_CV ibat_ua = %d CHG SOC %d\n",
1982 ibat_ua, soc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001983 } else {
1984 /* In constant current charging return the calc soc */
1985 pr_debug("CC CHG SOC %d\n", soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001986 }
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001987
1988 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07001989 chip->system_load_count = 0;
1990 return soc;
1991 } else if (ibat_ua > 0 && batt_terminal_uv
1992 < chip->max_voltage_uv - (VDD_MAX_ERR * 2)) {
1993 if (chip->system_load_count > MAX_COUNT_BEFORE_RESET_TO_CC) {
1994 chip->soc_at_cv = -EINVAL;
1995 pr_debug("Vbat below CV threshold, resetting CC_TO_CV\n");
1996 chip->system_load_count = 0;
1997 } else {
1998 chip->system_load_count += 1;
1999 pr_debug("Vbat below CV threshold, count: %d\n",
2000 chip->system_load_count);
2001 }
2002 return soc;
2003 } else if (ibat_ua > 0) {
2004 pr_debug("NOT CHARGING SOC %d\n", soc);
2005 chip->system_load_count = 0;
2006 chip->prev_chg_soc = soc;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002007 return soc;
2008 }
2009
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07002010 chip->system_load_count = 0;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002011 /*
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08002012 * battery is in CV phase - begin linear interpolation of soc based on
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002013 * battery charge current
2014 */
2015
2016 /*
2017 * if voltage lessened (possibly because of a system load)
2018 * keep reporting the prev chg soc
2019 */
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07002020 if (batt_terminal_uv <= chip->prev_batt_terminal_uv - VDD_STEP_SIZE) {
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08002021 pr_debug("batt_terminal_uv %d < (max = %d - 10000); CC CHG SOC %d\n",
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08002022 batt_terminal_uv, chip->prev_batt_terminal_uv,
2023 chip->prev_chg_soc);
2024 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002025 return chip->prev_chg_soc;
2026 }
2027
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07002028 soc_ibat = bound_soc(linear_interpolate(chip->soc_at_cv,
2029 chip->ibat_at_cv_ua,
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08002030 100, -1 * chip->chg_term_ua,
Xiaozhe Shifc7af172013-11-04 14:15:44 -08002031 params->iavg_ua));
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07002032 weight_ibat = bound_soc(linear_interpolate(1, chip->soc_at_cv,
2033 100, 100, chip->prev_chg_soc));
2034 weight_cc = 100 - weight_ibat;
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07002035 chg_soc = bound_soc(DIV_ROUND_CLOSEST(soc_ibat * weight_ibat
2036 + weight_cc * soc, 100));
2037
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07002038 pr_debug("weight_ibat = %d, weight_cc = %d, soc_ibat = %d, soc_cc = %d\n",
2039 weight_ibat, weight_cc, soc_ibat, soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002040
2041 /* always report a higher soc */
2042 if (chg_soc > chip->prev_chg_soc) {
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002043 chip->prev_chg_soc = chg_soc;
2044
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002045 chip->charging_adjusted_ocv = find_ocv_for_pc(chip, batt_temp,
2046 find_pc_for_soc(chip, params, chg_soc));
2047 pr_debug("CC CHG ADJ OCV = %d CHG SOC %d\n",
2048 chip->charging_adjusted_ocv,
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002049 chip->prev_chg_soc);
2050 }
2051
2052 pr_debug("Reporting CHG SOC %d\n", chip->prev_chg_soc);
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08002053 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002054 return chip->prev_chg_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002055}
2056
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002057static void very_low_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
2058{
2059 /*
2060 * if battery is very low (v_cutoff voltage + 20mv) hold
2061 * a wakelock untill soc = 0%
2062 */
2063 if (vbat_uv <= chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002064 && !wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002065 pr_debug("voltage = %d low holding wakelock\n", vbat_uv);
2066 wake_lock(&chip->low_voltage_wake_lock);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002067 } else if (vbat_uv > chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002068 && wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002069 pr_debug("voltage = %d releasing wakelock\n", vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002070 wake_unlock(&chip->low_voltage_wake_lock);
2071 }
2072}
2073
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002074#define VBATT_ERROR_MARGIN 20000
2075static void cv_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
2076{
2077 /*
2078 * if battery is very low (v_cutoff voltage + 20mv) hold
2079 * a wakelock untill soc = 0%
2080 */
2081 if (wake_lock_active(&chip->cv_wake_lock)) {
2082 if (chip->soc_at_cv != -EINVAL) {
2083 pr_debug("hit CV, releasing cv wakelock\n");
2084 wake_unlock(&chip->cv_wake_lock);
2085 } else if (!is_battery_charging(chip)) {
2086 pr_debug("charging stopped, releasing cv wakelock\n");
2087 wake_unlock(&chip->cv_wake_lock);
2088 }
2089 } else if (vbat_uv > chip->max_voltage_uv - VBATT_ERROR_MARGIN
2090 && chip->soc_at_cv == -EINVAL
2091 && is_battery_charging(chip)
2092 && !wake_lock_active(&chip->cv_wake_lock)) {
2093 pr_debug("voltage = %d holding cv wakelock\n", vbat_uv);
2094 wake_lock(&chip->cv_wake_lock);
2095 }
2096}
2097
Xiaozhe Shi2b647872013-10-31 14:30:27 -07002098#define NO_ADJUST_HIGH_SOC_THRESHOLD 98
Xiaozhe Shie118c692012-09-24 15:17:43 -07002099static int adjust_soc(struct qpnp_bms_chip *chip, struct soc_params *params,
2100 int soc, int batt_temp)
2101{
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002102 int ibat_ua = 0, vbat_uv = 0;
2103 int ocv_est_uv = 0, soc_est = 0, pc_est = 0, pc = 0;
2104 int delta_ocv_uv = 0;
2105 int n = 0;
2106 int rc_new_uah = 0;
2107 int pc_new = 0;
2108 int soc_new = 0;
2109 int slope = 0;
2110 int rc = 0;
2111 int delta_ocv_uv_limit = 0;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002112 int correction_limit_uv = 0;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002113
2114 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
2115 if (rc < 0) {
2116 pr_err("simultaneous vbat ibat failed err = %d\n", rc);
2117 goto out;
2118 }
2119
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002120 very_low_voltage_check(chip, vbat_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002121 cv_voltage_check(chip, vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002122
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002123 delta_ocv_uv_limit = DIV_ROUND_CLOSEST(ibat_ua, 1000);
2124
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08002125 ocv_est_uv = vbat_uv + (ibat_ua * params->rbatt_mohm)/1000;
2126
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002127 pc_est = calculate_pc(chip, ocv_est_uv, batt_temp);
2128 soc_est = div_s64((s64)params->fcc_uah * pc_est - params->uuc_uah*100,
2129 (s64)params->fcc_uah - params->uuc_uah);
2130 soc_est = bound_soc(soc_est);
2131
Xiaozhe Shi20640b52013-01-03 11:49:30 -08002132 /* never adjust during bms reset mode */
2133 if (bms_reset) {
2134 pr_debug("bms reset mode, SOC adjustment skipped\n");
2135 goto out;
2136 }
2137
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07002138 if (is_battery_charging(chip)) {
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002139 soc = charging_adjustments(chip, params, soc, vbat_uv, ibat_ua,
2140 batt_temp);
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07002141 /* Skip adjustments if we are in CV or ibat is negative */
2142 if (chip->soc_at_cv != -EINVAL || ibat_ua < 0)
2143 goto out;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002144 }
2145
2146 /*
2147 * do not adjust
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07002148 * if soc_est is same as what bms calculated
2149 * OR if soc_est > adjust_soc_low_threshold
2150 * OR if soc is above 90
2151 * because we might pull it low
2152 * and cause a bad user experience
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002153 */
Xiaozhe Shibd56b052013-10-21 11:51:30 -07002154 if (!wake_lock_active(&chip->low_voltage_wake_lock) &&
2155 (soc_est == soc
2156 || soc_est > chip->adjust_soc_low_threshold
2157 || soc >= NO_ADJUST_HIGH_SOC_THRESHOLD))
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002158 goto out;
2159
2160 if (chip->last_soc_est == -EINVAL)
2161 chip->last_soc_est = soc;
2162
2163 n = min(200, max(1 , soc + soc_est + chip->last_soc_est));
2164 chip->last_soc_est = soc_est;
2165
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002166 pc = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002167 if (pc > 0) {
2168 pc_new = calculate_pc(chip,
2169 chip->last_ocv_uv - (++slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002170 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002171 while (pc_new == pc) {
2172 /* start taking 10mV steps */
2173 slope = slope + 10;
2174 pc_new = calculate_pc(chip,
2175 chip->last_ocv_uv - (slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002176 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002177 }
2178 } else {
2179 /*
2180 * pc is already at the lowest point,
2181 * assume 1 millivolt translates to 1% pc
2182 */
2183 pc = 1;
2184 pc_new = 0;
2185 slope = 1;
2186 }
2187
2188 delta_ocv_uv = div_s64((soc - soc_est) * (s64)slope * 1000,
2189 n * (pc - pc_new));
2190
2191 if (abs(delta_ocv_uv) > delta_ocv_uv_limit) {
2192 pr_debug("limiting delta ocv %d limit = %d\n", delta_ocv_uv,
2193 delta_ocv_uv_limit);
2194
2195 if (delta_ocv_uv > 0)
2196 delta_ocv_uv = delta_ocv_uv_limit;
2197 else
2198 delta_ocv_uv = -1 * delta_ocv_uv_limit;
2199 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
2200 }
2201
Xiaozhe Shibd56b052013-10-21 11:51:30 -07002202 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2203 /* when in the cutoff region, do not correct upwards */
2204 delta_ocv_uv = max(0, delta_ocv_uv);
Xiaozhe Shi08d9aa72013-07-31 17:15:12 -07002205 goto skip_limits;
Xiaozhe Shibd56b052013-10-21 11:51:30 -07002206 }
Xiaozhe Shi08d9aa72013-07-31 17:15:12 -07002207
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002208 if (chip->last_ocv_uv > chip->flat_ocv_threshold_uv)
2209 correction_limit_uv = chip->high_ocv_correction_limit_uv;
2210 else
2211 correction_limit_uv = chip->low_ocv_correction_limit_uv;
2212
2213 if (abs(delta_ocv_uv) > correction_limit_uv) {
2214 pr_debug("limiting delta ocv %d limit = %d\n",
2215 delta_ocv_uv, correction_limit_uv);
2216 if (delta_ocv_uv > 0)
2217 delta_ocv_uv = correction_limit_uv;
2218 else
2219 delta_ocv_uv = -correction_limit_uv;
2220 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
2221 }
2222
Xiaozhe Shi08d9aa72013-07-31 17:15:12 -07002223skip_limits:
2224
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002225 chip->last_ocv_uv -= delta_ocv_uv;
2226
2227 if (chip->last_ocv_uv >= chip->max_voltage_uv)
2228 chip->last_ocv_uv = chip->max_voltage_uv;
2229
2230 /* calculate the soc based on this new ocv */
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002231 pc_new = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002232 rc_new_uah = (params->fcc_uah * pc_new) / 100;
2233 soc_new = (rc_new_uah - params->cc_uah - params->uuc_uah)*100
2234 / (params->fcc_uah - params->uuc_uah);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002235
2236 /*
2237 * if soc_new is ZERO force it higher so that phone doesnt report soc=0
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002238 * soc = 0 should happen only when soc_est is above a set value
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002239 */
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002240 if (soc_new == 0 && soc_est >= chip->hold_soc_est)
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002241 soc_new = 1;
2242
2243 soc = soc_new;
2244
2245out:
2246 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",
2247 ibat_ua, vbat_uv, ocv_est_uv, pc_est,
2248 soc_est, n, delta_ocv_uv, chip->last_ocv_uv,
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08002249 pc_new, soc_new, params->rbatt_mohm, slope);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002250
Xiaozhe Shie118c692012-09-24 15:17:43 -07002251 return soc;
2252}
2253
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002254static int clamp_soc_based_on_voltage(struct qpnp_bms_chip *chip, int soc)
2255{
2256 int rc, vbat_uv;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002257
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002258 rc = get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shi36458962013-02-06 16:19:57 -08002259 if (rc < 0) {
2260 pr_err("adc vbat failed err = %d\n", rc);
2261 return soc;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002262 }
Xiaozhe Shi9c14f3d2014-03-18 14:27:43 -07002263
2264 /* only clamp when discharging */
2265 if (is_battery_charging(chip))
2266 return soc;
2267
David Keitele50091e2014-03-05 09:55:57 -08002268 if (soc <= 0 && vbat_uv > chip->v_cutoff_uv) {
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002269 pr_debug("clamping soc to 1, vbat (%d) > cutoff (%d)\n",
2270 vbat_uv, chip->v_cutoff_uv);
2271 return 1;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002272 } else {
2273 pr_debug("not clamping, using soc = %d, vbat = %d and cutoff = %d\n",
2274 soc, vbat_uv, chip->v_cutoff_uv);
2275 return soc;
2276 }
2277}
2278
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002279static int64_t convert_cc_uah_to_raw(struct qpnp_bms_chip *chip, int64_t cc_uah)
2280{
2281 int64_t cc_uv, cc_pvh, cc_raw;
2282
2283 cc_pvh = cc_uah * chip->r_sense_uohm;
2284 cc_uv = div_s64(cc_pvh * SLEEP_CLK_HZ * SECONDS_PER_HOUR,
2285 CC_READING_TICKS * 1000000LL);
2286 cc_raw = div_s64(cc_uv * CC_READING_RESOLUTION_D,
2287 CC_READING_RESOLUTION_N);
2288 return cc_raw;
2289}
2290
2291#define CC_STEP_INCREMENT_UAH 1500
2292#define OCV_STEP_INCREMENT 0x10
2293static void configure_soc_wakeup(struct qpnp_bms_chip *chip,
2294 struct soc_params *params,
2295 int batt_temp, int target_soc)
2296{
2297 int target_ocv_uv;
2298 int64_t target_cc_uah, cc_raw_64, current_shdw_cc_raw_64;
2299 int64_t current_shdw_cc_uah, iadc_comp_factor;
2300 uint64_t cc_raw, current_shdw_cc_raw;
2301 int16_t ocv_raw, current_ocv_raw;
2302
2303 current_shdw_cc_raw = 0;
2304 mutex_lock(&chip->bms_output_lock);
2305 lock_output_data(chip);
2306 qpnp_read_wrapper(chip, (u8 *)&current_ocv_raw,
2307 chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
2308 unlock_output_data(chip);
2309 mutex_unlock(&chip->bms_output_lock);
2310 current_shdw_cc_uah = get_prop_bms_charge_counter_shadow(chip);
2311 current_shdw_cc_raw_64 = convert_cc_uah_to_raw(chip,
2312 current_shdw_cc_uah);
2313
2314 /*
2315 * Calculate the target shadow coulomb counter threshold for when
2316 * the SoC changes.
2317 *
2318 * Since the BMS driver resets the shadow coulomb counter every
2319 * 20 seconds when the device is awake, calculate the threshold as
2320 * a delta from the current shadow coulomb count.
2321 */
2322 target_cc_uah = (100 - target_soc)
2323 * (params->fcc_uah - params->uuc_uah)
2324 / 100 - current_shdw_cc_uah;
2325 if (target_cc_uah < 0) {
2326 /*
2327 * If the target cc is below 0, that means we have already
2328 * passed the point where SoC should have fallen.
2329 * Set a wakeup in a few more mAh and check back again
2330 */
2331 target_cc_uah = CC_STEP_INCREMENT_UAH;
2332 }
2333 iadc_comp_factor = 100000;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002334 qpnp_iadc_comp_result(chip->iadc_dev, &iadc_comp_factor);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002335 target_cc_uah = div64_s64(target_cc_uah * 100000, iadc_comp_factor);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002336 target_cc_uah = cc_reverse_adjust_for_gain(chip, target_cc_uah);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002337 cc_raw_64 = convert_cc_uah_to_raw(chip, target_cc_uah);
2338 cc_raw = convert_s64_to_s36(cc_raw_64);
2339
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002340 target_ocv_uv = find_ocv_for_pc(chip, batt_temp,
2341 find_pc_for_soc(chip, params, target_soc));
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002342 ocv_raw = convert_vbatt_uv_to_raw(chip, target_ocv_uv);
2343
2344 /*
2345 * If the current_ocv_raw was updated since reaching 100% and is lower
2346 * than the calculated target ocv threshold, set the new target
2347 * threshold 1.5mAh lower in order to check if the SoC changed yet.
2348 */
2349 if (current_ocv_raw != chip->ocv_reading_at_100
2350 && current_ocv_raw < ocv_raw)
2351 ocv_raw = current_ocv_raw - OCV_STEP_INCREMENT;
2352
2353 qpnp_write_wrapper(chip, (u8 *)&cc_raw,
2354 chip->base + BMS1_SW_CC_THR0, 5);
2355 qpnp_write_wrapper(chip, (u8 *)&ocv_raw,
2356 chip->base + BMS1_OCV_THR0, 2);
2357
Xiaozhe Shif5d87832013-11-21 17:05:39 -08002358 enable_bms_irq(&chip->ocv_thr_irq);
2359 enable_bms_irq(&chip->sw_cc_thr_irq);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002360 pr_debug("current sw_cc_raw = 0x%llx, current ocv = 0x%hx\n",
2361 current_shdw_cc_raw, (uint16_t)current_ocv_raw);
2362 pr_debug("target_cc_uah = %lld, raw64 = 0x%llx, raw 36 = 0x%llx, ocv_raw = 0x%hx\n",
2363 target_cc_uah,
2364 (uint64_t)cc_raw_64, cc_raw,
2365 (uint16_t)ocv_raw);
2366}
2367
Xiaozhe Shi5e791032013-10-25 11:30:42 -07002368#define BAD_SOC_THRESH -10
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002369static int calculate_raw_soc(struct qpnp_bms_chip *chip,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002370 struct raw_soc_params *raw,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002371 struct soc_params *params,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002372 int batt_temp)
2373{
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002374 int soc, remaining_usable_charge_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002375
Xiaozhe Shie118c692012-09-24 15:17:43 -07002376 /* calculate remaining usable charge */
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002377 remaining_usable_charge_uah = params->ocv_charge_uah
2378 - params->cc_uah
2379 - params->uuc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002380 pr_debug("RUC = %duAh\n", remaining_usable_charge_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002381
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002382 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002383 (params->fcc_uah - params->uuc_uah));
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002384
Xiaozhe Shi5e791032013-10-25 11:30:42 -07002385 if (chip->first_time_calc_soc && soc > BAD_SOC_THRESH && soc < 0) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07002386 /*
2387 * first time calcualtion and the pon ocv is too low resulting
2388 * in a bad soc. Adjust ocv to get 0 soc
2389 */
2390 pr_debug("soc is %d, adjusting pon ocv to make it 0\n", soc);
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002391 chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp,
2392 find_pc_for_soc(chip, params, 0));
2393 params->ocv_charge_uah = find_ocv_charge_for_soc(chip,
2394 params, 0);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002395
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002396 remaining_usable_charge_uah = params->ocv_charge_uah
2397 - params->cc_uah
2398 - params->uuc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002399
2400 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002401 (params->fcc_uah
2402 - params->uuc_uah));
Xiaozhe Shie118c692012-09-24 15:17:43 -07002403 pr_debug("DONE for O soc is %d, pon ocv adjusted to %duV\n",
2404 soc, chip->last_ocv_uv);
2405 }
2406
2407 if (soc > 100)
2408 soc = 100;
2409
Xiaozhe Shi5e791032013-10-25 11:30:42 -07002410 if (soc > BAD_SOC_THRESH && soc < 0) {
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002411 pr_debug("bad rem_usb_chg = %d rem_chg %d, cc_uah %d, unusb_chg %d\n",
Xiaozhe Shie118c692012-09-24 15:17:43 -07002412 remaining_usable_charge_uah,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002413 params->ocv_charge_uah,
2414 params->cc_uah, params->uuc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002415
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002416 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 -07002417 chip->last_ocv_uv, batt_temp,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002418 params->fcc_uah, soc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002419 soc = 0;
2420 }
2421
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002422 return soc;
2423}
2424
2425#define SLEEP_RECALC_INTERVAL 3
2426static int calculate_state_of_charge(struct qpnp_bms_chip *chip,
2427 struct raw_soc_params *raw,
2428 int batt_temp)
2429{
2430 struct soc_params params;
2431 int soc, previous_soc, shutdown_soc, new_calculated_soc;
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002432 int remaining_usable_charge_uah;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002433
2434 calculate_soc_params(chip, raw, &params, batt_temp);
2435 if (!is_battery_present(chip)) {
2436 pr_debug("battery gone, reporting 100\n");
2437 new_calculated_soc = 100;
2438 goto done_calculating;
2439 }
2440
2441 if (params.fcc_uah - params.uuc_uah <= 0) {
2442 pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
2443 params.fcc_uah,
2444 params.uuc_uah);
2445 new_calculated_soc = 0;
2446 goto done_calculating;
2447 }
2448
2449 soc = calculate_raw_soc(chip, raw, &params, batt_temp);
2450
Xiaozhe Shie118c692012-09-24 15:17:43 -07002451 mutex_lock(&chip->soc_invalidation_mutex);
2452 shutdown_soc = chip->shutdown_soc;
2453
2454 if (chip->first_time_calc_soc && soc != shutdown_soc
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002455 && !chip->shutdown_soc_invalid) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07002456 /*
2457 * soc for the first time - use shutdown soc
2458 * to adjust pon ocv since it is a small percent away from
2459 * the real soc
2460 */
2461 pr_debug("soc = %d before forcing shutdown_soc = %d\n",
2462 soc, shutdown_soc);
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002463 chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp,
2464 find_pc_for_soc(chip, &params, shutdown_soc));
2465 params.ocv_charge_uah = find_ocv_charge_for_soc(chip,
2466 &params, shutdown_soc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002467
2468 remaining_usable_charge_uah = params.ocv_charge_uah
2469 - params.cc_uah
2470 - params.uuc_uah;
2471
2472 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2473 (params.fcc_uah
2474 - params.uuc_uah));
2475
2476 pr_debug("DONE for shutdown_soc = %d soc is %d, adjusted ocv to %duV\n",
2477 shutdown_soc, soc, chip->last_ocv_uv);
2478 }
2479 mutex_unlock(&chip->soc_invalidation_mutex);
2480
Xiaozhe Shicbce8042014-02-13 14:08:47 -08002481 if (chip->first_time_calc_soc && !chip->shutdown_soc_invalid) {
2482 pr_debug("Skip adjustment when shutdown SOC has been forced\n");
2483 new_calculated_soc = soc;
2484 } else {
2485 pr_debug("SOC before adjustment = %d\n", soc);
2486 new_calculated_soc = adjust_soc(chip, &params, soc, batt_temp);
2487 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07002488
Xiaozhe Shi445d2492013-03-27 18:10:18 -07002489 /* always clamp soc due to BMS hw/sw immaturities */
2490 new_calculated_soc = clamp_soc_based_on_voltage(chip,
2491 new_calculated_soc);
Xiaozhe Shi422c27d2014-08-06 11:22:30 -07002492
2493 new_calculated_soc = bound_soc(new_calculated_soc);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002494 /*
2495 * If the battery is full, configure the cc threshold so the system
2496 * wakes up after SoC changes
2497 */
Xiaozhe Shif5d87832013-11-21 17:05:39 -08002498 if (is_battery_full(chip)) {
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002499 configure_soc_wakeup(chip, &params,
2500 batt_temp, bound_soc(new_calculated_soc - 1));
Xiaozhe Shif5d87832013-11-21 17:05:39 -08002501 } else {
2502 disable_bms_irq(&chip->ocv_thr_irq);
2503 disable_bms_irq(&chip->sw_cc_thr_irq);
2504 }
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002505done_calculating:
Xiaozhe Shifa6ea692013-05-31 11:15:13 -07002506 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07002507 previous_soc = chip->calculated_soc;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002508 chip->calculated_soc = new_calculated_soc;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002509 pr_debug("CC based calculated SOC = %d\n", chip->calculated_soc);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002510 if (chip->last_soc_invalid) {
2511 chip->last_soc_invalid = false;
2512 chip->last_soc = -EINVAL;
2513 }
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002514 /*
2515 * Check if more than a long time has passed since the last
2516 * calculation (more than n times compared to the soc recalculation
2517 * rate, where n is defined by SLEEP_RECALC_INTERVAL). If this is true,
2518 * then the system must have gone through a long sleep, and SoC can be
2519 * allowed to become unbounded by the last reported SoC
2520 */
2521 if (params.delta_time_s * 1000 >
2522 chip->calculate_soc_ms * SLEEP_RECALC_INTERVAL
2523 && !chip->first_time_calc_soc) {
2524 chip->last_soc_unbound = true;
2525 chip->last_soc_change_sec = chip->last_recalc_time;
2526 pr_debug("last_soc unbound because elapsed time = %d\n",
2527 params.delta_time_s);
2528 }
2529 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi27375822013-08-22 11:40:15 -07002530 wake_up_interruptible(&chip->bms_wait_queue);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07002531
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002532 if (new_calculated_soc != previous_soc && chip->bms_psy_registered) {
Xiaozhe Shi83484e32013-05-16 10:59:59 -07002533 power_supply_changed(&chip->bms_psy);
2534 pr_debug("power supply changed\n");
2535 } else {
2536 /*
2537 * Call report state of charge anyways to periodically update
2538 * reported SoC. This prevents reported SoC from being stuck
2539 * when calculated soc doesn't change.
2540 */
2541 report_state_of_charge(chip);
2542 }
2543
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002544 get_current_time(&chip->last_recalc_time);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002545 chip->first_time_calc_soc = 0;
Xiaozhe Shi70633922013-09-23 15:50:53 -07002546 chip->first_time_calc_uuc = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002547 return chip->calculated_soc;
2548}
2549
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002550static int calculate_soc_from_voltage(struct qpnp_bms_chip *chip)
2551{
2552 int voltage_range_uv, voltage_remaining_uv, voltage_based_soc;
Xiaozhe Shi36458962013-02-06 16:19:57 -08002553 int rc, vbat_uv;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002554
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002555 rc = get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shi36458962013-02-06 16:19:57 -08002556 if (rc < 0) {
2557 pr_err("adc vbat failed err = %d\n", rc);
2558 return rc;
2559 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002560 voltage_range_uv = chip->max_voltage_uv - chip->v_cutoff_uv;
2561 voltage_remaining_uv = vbat_uv - chip->v_cutoff_uv;
2562 voltage_based_soc = voltage_remaining_uv * 100 / voltage_range_uv;
2563
2564 voltage_based_soc = clamp(voltage_based_soc, 0, 100);
2565
2566 if (chip->prev_voltage_based_soc != voltage_based_soc
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002567 && chip->bms_psy_registered) {
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002568 power_supply_changed(&chip->bms_psy);
2569 pr_debug("power supply changed\n");
2570 }
2571 chip->prev_voltage_based_soc = voltage_based_soc;
2572
2573 pr_debug("vbat used = %duv\n", vbat_uv);
2574 pr_debug("Calculated voltage based soc = %d\n", voltage_based_soc);
2575 return voltage_based_soc;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08002576}
2577
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002578static int recalculate_raw_soc(struct qpnp_bms_chip *chip)
2579{
2580 int batt_temp, rc, soc;
2581 struct qpnp_vadc_result result;
2582 struct raw_soc_params raw;
2583 struct soc_params params;
2584
2585 bms_stay_awake(&chip->soc_wake_source);
2586 if (chip->use_voltage_soc) {
2587 soc = calculate_soc_from_voltage(chip);
2588 } else {
2589 if (!chip->batfet_closed)
Xiaozhe Shiffb7cfc2014-01-03 10:47:36 -08002590 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002591 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM,
2592 &result);
2593 if (rc) {
2594 pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n",
2595 LR_MUX1_BATT_THERM, rc);
2596 soc = chip->calculated_soc;
2597 } else {
2598 pr_debug("batt_temp phy = %lld meas = 0x%llx\n",
2599 result.physical,
2600 result.measurement);
2601 batt_temp = (int)result.physical;
2602
2603 mutex_lock(&chip->last_ocv_uv_mutex);
Xiaozhe Shied4a5522014-09-05 14:56:13 -07002604 rc = read_soc_params_raw(chip, &raw, batt_temp);
2605 if (rc) {
2606 pr_err("Unable to read params, rc: %d\n", rc);
2607 soc = 0;
2608 goto done;
2609 }
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002610 calculate_soc_params(chip, &raw, &params, batt_temp);
2611 if (!is_battery_present(chip)) {
2612 pr_debug("battery gone\n");
2613 soc = 0;
2614 } else if (params.fcc_uah - params.uuc_uah <= 0) {
2615 pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
2616 params.fcc_uah,
2617 params.uuc_uah);
2618 soc = 0;
2619 } else {
2620 soc = calculate_raw_soc(chip, &raw,
2621 &params, batt_temp);
2622 }
Xiaozhe Shied4a5522014-09-05 14:56:13 -07002623done:
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002624 mutex_unlock(&chip->last_ocv_uv_mutex);
2625 }
2626 }
2627 bms_relax(&chip->soc_wake_source);
2628 return soc;
2629}
2630
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002631static int recalculate_soc(struct qpnp_bms_chip *chip)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002632{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002633 int batt_temp, rc, soc;
2634 struct qpnp_vadc_result result;
2635 struct raw_soc_params raw;
2636
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002637 bms_stay_awake(&chip->soc_wake_source);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002638 mutex_lock(&chip->vbat_monitor_mutex);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002639 if (chip->vbat_monitor_params.state_request !=
2640 ADC_TM_HIGH_LOW_THR_DISABLE)
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002641 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2642 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002643 mutex_unlock(&chip->vbat_monitor_mutex);
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002644 if (chip->use_voltage_soc) {
2645 soc = calculate_soc_from_voltage(chip);
2646 } else {
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07002647 if (!chip->batfet_closed)
Xiaozhe Shiffb7cfc2014-01-03 10:47:36 -08002648 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002649 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM,
2650 &result);
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002651 if (rc) {
2652 pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n",
2653 LR_MUX1_BATT_THERM, rc);
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002654 soc = chip->calculated_soc;
2655 } else {
2656 pr_debug("batt_temp phy = %lld meas = 0x%llx\n",
2657 result.physical,
2658 result.measurement);
2659 batt_temp = (int)result.physical;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002660
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002661 mutex_lock(&chip->last_ocv_uv_mutex);
Xiaozhe Shied4a5522014-09-05 14:56:13 -07002662 rc = read_soc_params_raw(chip, &raw, batt_temp);
2663 if (rc) {
2664 pr_err("Unable to read params, rc: %d\n", rc);
2665 soc = chip->calculated_soc;
2666 } else {
2667 soc = calculate_state_of_charge(chip,
2668 &raw, batt_temp);
2669 }
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002670 mutex_unlock(&chip->last_ocv_uv_mutex);
2671 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002672 }
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002673 bms_relax(&chip->soc_wake_source);
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002674 return soc;
2675}
2676
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002677static void recalculate_work(struct work_struct *work)
2678{
2679 struct qpnp_bms_chip *chip = container_of(work,
2680 struct qpnp_bms_chip,
2681 recalc_work);
2682
2683 recalculate_soc(chip);
2684}
2685
Xiaozhe Shicb487b12013-10-14 17:42:07 -07002686static int get_calculation_delay_ms(struct qpnp_bms_chip *chip)
2687{
2688 if (wake_lock_active(&chip->low_voltage_wake_lock))
2689 return chip->low_voltage_calculate_soc_ms;
2690 else if (chip->calculated_soc < chip->low_soc_calc_threshold)
2691 return chip->low_soc_calculate_soc_ms;
2692 else
2693 return chip->calculate_soc_ms;
2694}
2695
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002696static void calculate_soc_work(struct work_struct *work)
2697{
2698 struct qpnp_bms_chip *chip = container_of(work,
2699 struct qpnp_bms_chip,
2700 calculate_soc_delayed_work.work);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002701
Xiaozhe Shicb487b12013-10-14 17:42:07 -07002702 recalculate_soc(chip);
2703 schedule_delayed_work(&chip->calculate_soc_delayed_work,
2704 round_jiffies_relative(msecs_to_jiffies
2705 (get_calculation_delay_ms(chip))));
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002706}
2707
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002708static void configure_vbat_monitor_low(struct qpnp_bms_chip *chip)
2709{
2710 mutex_lock(&chip->vbat_monitor_mutex);
2711 if (chip->vbat_monitor_params.state_request
2712 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2713 /*
2714 * Battery is now around or below v_cutoff
2715 */
2716 pr_debug("battery entered cutoff range\n");
2717 if (!wake_lock_active(&chip->low_voltage_wake_lock)) {
2718 pr_debug("voltage low, holding wakelock\n");
2719 wake_lock(&chip->low_voltage_wake_lock);
2720 cancel_delayed_work_sync(
2721 &chip->calculate_soc_delayed_work);
2722 schedule_delayed_work(
2723 &chip->calculate_soc_delayed_work, 0);
2724 }
2725 chip->vbat_monitor_params.state_request =
2726 ADC_TM_HIGH_THR_ENABLE;
2727 chip->vbat_monitor_params.high_thr =
2728 (chip->low_voltage_threshold + VBATT_ERROR_MARGIN);
2729 pr_debug("set low thr to %d and high to %d\n",
2730 chip->vbat_monitor_params.low_thr,
2731 chip->vbat_monitor_params.high_thr);
2732 chip->vbat_monitor_params.low_thr = 0;
2733 } else if (chip->vbat_monitor_params.state_request
2734 == ADC_TM_LOW_THR_ENABLE) {
2735 /*
2736 * Battery is in normal operation range.
2737 */
2738 pr_debug("battery entered normal range\n");
2739 if (wake_lock_active(&chip->cv_wake_lock)) {
2740 wake_unlock(&chip->cv_wake_lock);
2741 pr_debug("releasing cv wake lock\n");
2742 }
2743 chip->in_cv_range = false;
2744 chip->vbat_monitor_params.state_request =
2745 ADC_TM_HIGH_LOW_THR_ENABLE;
2746 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2747 - VBATT_ERROR_MARGIN;
2748 chip->vbat_monitor_params.low_thr =
2749 chip->low_voltage_threshold;
2750 pr_debug("set low thr to %d and high to %d\n",
2751 chip->vbat_monitor_params.low_thr,
2752 chip->vbat_monitor_params.high_thr);
2753 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002754 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2755 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002756 mutex_unlock(&chip->vbat_monitor_mutex);
2757}
2758
2759#define CV_LOW_THRESHOLD_HYST_UV 100000
2760static void configure_vbat_monitor_high(struct qpnp_bms_chip *chip)
2761{
2762 mutex_lock(&chip->vbat_monitor_mutex);
2763 if (chip->vbat_monitor_params.state_request
2764 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2765 /*
2766 * Battery is around vddmax
2767 */
2768 pr_debug("battery entered vddmax range\n");
2769 chip->in_cv_range = true;
2770 if (!wake_lock_active(&chip->cv_wake_lock)) {
2771 wake_lock(&chip->cv_wake_lock);
2772 pr_debug("holding cv wake lock\n");
2773 }
2774 schedule_work(&chip->recalc_work);
2775 chip->vbat_monitor_params.state_request =
2776 ADC_TM_LOW_THR_ENABLE;
2777 chip->vbat_monitor_params.low_thr =
2778 (chip->max_voltage_uv - CV_LOW_THRESHOLD_HYST_UV);
2779 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv * 2;
2780 pr_debug("set low thr to %d and high to %d\n",
2781 chip->vbat_monitor_params.low_thr,
2782 chip->vbat_monitor_params.high_thr);
2783 } else if (chip->vbat_monitor_params.state_request
2784 == ADC_TM_HIGH_THR_ENABLE) {
2785 /*
2786 * Battery is in normal operation range.
2787 */
2788 pr_debug("battery entered normal range\n");
2789 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2790 pr_debug("voltage high, releasing wakelock\n");
2791 wake_unlock(&chip->low_voltage_wake_lock);
2792 }
2793 chip->vbat_monitor_params.state_request =
2794 ADC_TM_HIGH_LOW_THR_ENABLE;
2795 chip->vbat_monitor_params.high_thr =
2796 chip->max_voltage_uv - VBATT_ERROR_MARGIN;
2797 chip->vbat_monitor_params.low_thr =
2798 chip->low_voltage_threshold;
2799 pr_debug("set low thr to %d and high to %d\n",
2800 chip->vbat_monitor_params.low_thr,
2801 chip->vbat_monitor_params.high_thr);
2802 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002803 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2804 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002805 mutex_unlock(&chip->vbat_monitor_mutex);
2806}
2807
2808static void btm_notify_vbat(enum qpnp_tm_state state, void *ctx)
2809{
2810 struct qpnp_bms_chip *chip = ctx;
2811 int vbat_uv;
2812 struct qpnp_vadc_result result;
2813 int rc;
2814
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002815 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &result);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002816 pr_debug("vbat = %lld, raw = 0x%x\n", result.physical, result.adc_code);
2817
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002818 get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002819 pr_debug("vbat is at %d, state is at %d\n", vbat_uv, state);
2820
2821 if (state == ADC_TM_LOW_STATE) {
2822 pr_debug("low voltage btm notification triggered\n");
2823 if (vbat_uv - VBATT_ERROR_MARGIN
2824 < chip->vbat_monitor_params.low_thr) {
2825 configure_vbat_monitor_low(chip);
2826 } else {
2827 pr_debug("faulty btm trigger, discarding\n");
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002828 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002829 &chip->vbat_monitor_params);
2830 }
2831 } else if (state == ADC_TM_HIGH_STATE) {
2832 pr_debug("high voltage btm notification triggered\n");
2833 if (vbat_uv + VBATT_ERROR_MARGIN
2834 > chip->vbat_monitor_params.high_thr) {
2835 configure_vbat_monitor_high(chip);
2836 } else {
2837 pr_debug("faulty btm trigger, discarding\n");
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002838 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002839 &chip->vbat_monitor_params);
2840 }
2841 } else {
2842 pr_debug("unknown voltage notification state: %d\n", state);
2843 }
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002844 if (chip->bms_psy_registered)
Xiaozhe Shifa120db2013-06-06 15:57:19 -07002845 power_supply_changed(&chip->bms_psy);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002846}
2847
2848static int reset_vbat_monitoring(struct qpnp_bms_chip *chip)
2849{
2850 int rc;
2851
2852 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_DISABLE;
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002853
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002854 rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2855 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002856 if (rc) {
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002857 pr_err("tm disable failed: %d\n", rc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002858 return rc;
2859 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002860 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2861 pr_debug("battery removed, releasing wakelock\n");
2862 wake_unlock(&chip->low_voltage_wake_lock);
2863 }
2864 if (chip->in_cv_range) {
2865 pr_debug("battery removed, removing in_cv_range state\n");
2866 chip->in_cv_range = false;
2867 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002868 return 0;
2869}
2870
2871static int setup_vbat_monitoring(struct qpnp_bms_chip *chip)
2872{
2873 int rc;
2874
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002875 chip->vbat_monitor_params.low_thr = chip->low_voltage_threshold;
2876 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2877 - VBATT_ERROR_MARGIN;
2878 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_ENABLE;
2879 chip->vbat_monitor_params.channel = VBAT_SNS;
2880 chip->vbat_monitor_params.btm_ctx = (void *)chip;
2881 chip->vbat_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
2882 chip->vbat_monitor_params.threshold_notification = &btm_notify_vbat;
2883 pr_debug("set low thr to %d and high to %d\n",
2884 chip->vbat_monitor_params.low_thr,
2885 chip->vbat_monitor_params.high_thr);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002886
2887 if (!is_battery_present(chip)) {
2888 pr_debug("no battery inserted, do not enable vbat monitoring\n");
2889 chip->vbat_monitor_params.state_request =
2890 ADC_TM_HIGH_LOW_THR_DISABLE;
2891 } else {
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002892 rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2893 &chip->vbat_monitor_params);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002894 if (rc) {
2895 pr_err("tm setup failed: %d\n", rc);
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002896 return rc;
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002897 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002898 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002899
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002900 pr_debug("setup complete\n");
2901 return 0;
2902}
2903
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302904static void readjust_fcc_table(struct qpnp_bms_chip *chip)
2905{
2906 struct single_row_lut *temp, *old;
2907 int i, fcc, ratio;
2908
2909 if (!chip->enable_fcc_learning)
2910 return;
2911
2912 if (!chip->fcc_temp_lut) {
2913 pr_err("The static fcc lut table is NULL\n");
2914 return;
2915 }
2916
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07002917 temp = devm_kzalloc(chip->dev, sizeof(struct single_row_lut),
2918 GFP_KERNEL);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302919 if (!temp) {
2920 pr_err("Cannot allocate memory for adjusted fcc table\n");
2921 return;
2922 }
2923
2924 fcc = interpolate_fcc(chip->fcc_temp_lut, chip->fcc_new_batt_temp);
2925
2926 temp->cols = chip->fcc_temp_lut->cols;
2927 for (i = 0; i < chip->fcc_temp_lut->cols; i++) {
2928 temp->x[i] = chip->fcc_temp_lut->x[i];
2929 ratio = div_u64(chip->fcc_temp_lut->y[i] * 1000, fcc);
2930 temp->y[i] = (ratio * chip->fcc_new_mah);
2931 temp->y[i] /= 1000;
2932 }
2933
2934 old = chip->adjusted_fcc_temp_lut;
2935 chip->adjusted_fcc_temp_lut = temp;
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07002936 devm_kfree(chip->dev, old);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302937}
2938
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302939static int read_fcc_data_from_backup(struct qpnp_bms_chip *chip)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302940{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302941 int rc, i;
2942 u8 fcc = 0, chgcyl = 0;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302943
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302944 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2945 rc = qpnp_read_wrapper(chip, &fcc,
2946 chip->base + BMS_FCC_BASE_REG + i, 1);
2947 rc |= qpnp_read_wrapper(chip, &chgcyl,
2948 chip->base + BMS_CHGCYL_BASE_REG + i, 1);
2949 if (rc) {
2950 pr_err("Unable to read FCC data\n");
2951 return rc;
2952 }
2953 if (fcc == 0 || (fcc == 0xFF && chgcyl == 0xFF)) {
2954 /* FCC invalid/not present */
2955 chip->fcc_learning_samples[i].fcc_new = 0;
2956 chip->fcc_learning_samples[i].chargecycles = 0;
2957 } else {
2958 /* valid FCC data */
2959 chip->fcc_sample_count++;
2960 chip->fcc_learning_samples[i].fcc_new =
2961 fcc * chip->fcc_resolution;
2962 chip->fcc_learning_samples[i].chargecycles =
2963 chgcyl * CHGCYL_RESOLUTION;
2964 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302965 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302966
2967 return 0;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302968}
2969
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302970static int discard_backup_fcc_data(struct qpnp_bms_chip *chip)
2971{
2972 int rc = 0, i;
2973 u8 temp_u8 = 0;
2974
2975 chip->fcc_sample_count = 0;
2976 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2977 rc = qpnp_write_wrapper(chip, &temp_u8,
2978 chip->base + BMS_FCC_BASE_REG + i, 1);
2979 rc |= qpnp_write_wrapper(chip, &temp_u8,
2980 chip->base + BMS_CHGCYL_BASE_REG + i, 1);
2981 if (rc) {
2982 pr_err("Unable to clear FCC data\n");
2983 return rc;
2984 }
2985 }
2986
2987 return 0;
2988}
2989
2990static void
2991average_fcc_samples_and_readjust_fcc_table(struct qpnp_bms_chip *chip)
2992{
2993 int i, temp_fcc_avg = 0, temp_fcc_delta = 0, new_fcc_avg = 0;
2994 struct fcc_sample *ft;
2995
2996 for (i = 0; i < chip->min_fcc_learning_samples; i++)
2997 temp_fcc_avg += chip->fcc_learning_samples[i].fcc_new;
2998
2999 temp_fcc_avg /= chip->min_fcc_learning_samples;
3000 temp_fcc_delta = div_u64(temp_fcc_avg * DELTA_FCC_PERCENT, 100);
3001
3002 /* fix the fcc if its an outlier i.e. > 5% of the average */
3003 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
3004 ft = &chip->fcc_learning_samples[i];
3005 if (abs(ft->fcc_new - temp_fcc_avg) > temp_fcc_delta)
3006 new_fcc_avg += temp_fcc_avg;
3007 else
3008 new_fcc_avg += ft->fcc_new;
3009 }
3010 new_fcc_avg /= chip->min_fcc_learning_samples;
3011
3012 chip->fcc_new_mah = new_fcc_avg;
3013 chip->fcc_new_batt_temp = FCC_DEFAULT_TEMP;
3014 pr_info("FCC update: New fcc_mah=%d, fcc_batt_temp=%d\n",
3015 new_fcc_avg, FCC_DEFAULT_TEMP);
3016 readjust_fcc_table(chip);
3017}
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303018
3019static void backup_charge_cycle(struct qpnp_bms_chip *chip)
3020{
3021 int rc = 0;
3022
3023 if (chip->charge_increase >= 0) {
3024 rc = qpnp_write_wrapper(chip, &chip->charge_increase,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303025 chip->base + CHARGE_INCREASE_STORAGE, 1);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303026 if (rc)
3027 pr_err("Unable to backup charge_increase\n");
3028 }
3029
3030 if (chip->charge_cycles >= 0) {
3031 rc = qpnp_write_wrapper(chip, (u8 *)&chip->charge_cycles,
3032 chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
3033 if (rc)
3034 pr_err("Unable to backup charge_cycles\n");
3035 }
3036}
3037
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303038static bool chargecycles_in_range(struct qpnp_bms_chip *chip)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303039{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303040 int i, min_cycle, max_cycle, valid_range;
3041
3042 /* find the smallest and largest charge cycle */
3043 max_cycle = min_cycle = chip->fcc_learning_samples[0].chargecycles;
3044 for (i = 1; i < chip->min_fcc_learning_samples; i++) {
3045 if (min_cycle > chip->fcc_learning_samples[i].chargecycles)
3046 min_cycle = chip->fcc_learning_samples[i].chargecycles;
3047 if (max_cycle < chip->fcc_learning_samples[i].chargecycles)
3048 max_cycle = chip->fcc_learning_samples[i].chargecycles;
3049 }
3050
3051 /* check if chargecyles are in range to continue with FCC update */
3052 valid_range = DIV_ROUND_UP(VALID_FCC_CHGCYL_RANGE,
3053 CHGCYL_RESOLUTION) * CHGCYL_RESOLUTION;
3054 if (abs(max_cycle - min_cycle) > valid_range)
3055 return false;
3056
3057 return true;
3058}
3059
3060static int read_chgcycle_data_from_backup(struct qpnp_bms_chip *chip)
3061{
3062 int rc;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303063 uint16_t temp_u16 = 0;
3064 u8 temp_u8 = 0;
3065
3066 rc = qpnp_read_wrapper(chip, &temp_u8,
3067 chip->base + CHARGE_INCREASE_STORAGE, 1);
3068 if (!rc && temp_u8 != 0xFF)
3069 chip->charge_increase = temp_u8;
3070
3071 rc = qpnp_read_wrapper(chip, (u8 *)&temp_u16,
3072 chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
3073 if (!rc && temp_u16 != 0xFFFF)
3074 chip->charge_cycles = temp_u16;
3075
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303076 return rc;
3077}
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303078
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303079static void
3080attempt_learning_new_fcc(struct qpnp_bms_chip *chip)
3081{
3082 pr_debug("Total FCC sample count=%d\n", chip->fcc_sample_count);
3083
3084 /* update FCC if we have the required samples */
3085 if ((chip->fcc_sample_count == chip->min_fcc_learning_samples) &&
3086 chargecycles_in_range(chip))
3087 average_fcc_samples_and_readjust_fcc_table(chip);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303088}
3089
3090static int calculate_real_soc(struct qpnp_bms_chip *chip,
3091 int batt_temp, struct raw_soc_params *raw, int cc_uah)
3092{
3093 int fcc_uah, rc_uah;
3094
3095 fcc_uah = calculate_fcc(chip, batt_temp);
3096 rc_uah = calculate_ocv_charge(chip, raw, fcc_uah);
3097
3098 return ((rc_uah - cc_uah) * 100) / fcc_uah;
3099}
3100
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303101#define MAX_U8_VALUE ((u8)(~0U))
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303102
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303103static int backup_new_fcc(struct qpnp_bms_chip *chip, int fcc_mah,
3104 int chargecycles)
3105{
3106 int rc, min_cycle, i;
3107 u8 fcc_new, chgcyl, pos = 0;
3108 struct fcc_sample *ft;
3109
3110 if ((fcc_mah > (chip->fcc_resolution * MAX_U8_VALUE)) ||
3111 (chargecycles > (CHGCYL_RESOLUTION * MAX_U8_VALUE))) {
3112 pr_warn("FCC/Chgcyl beyond storage limit. FCC=%d, chgcyl=%d\n",
3113 fcc_mah, chargecycles);
3114 return -EINVAL;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303115 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303116
3117 if (chip->fcc_sample_count == chip->min_fcc_learning_samples) {
3118 /* search best location - oldest entry */
3119 min_cycle = chip->fcc_learning_samples[0].chargecycles;
3120 for (i = 1; i < chip->min_fcc_learning_samples; i++) {
3121 if (min_cycle >
Zhenhua Huang82663ea2014-09-26 10:42:31 +08003122 chip->fcc_learning_samples[i].chargecycles) {
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303123 pos = i;
Zhenhua Huang82663ea2014-09-26 10:42:31 +08003124 break;
3125 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303126 }
3127 } else {
3128 /* find an empty location */
3129 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
3130 ft = &chip->fcc_learning_samples[i];
3131 if (ft->fcc_new == 0 || (ft->fcc_new == 0xFF &&
3132 ft->chargecycles == 0xFF)) {
3133 pos = i;
3134 break;
3135 }
3136 }
3137 chip->fcc_sample_count++;
3138 }
3139 chip->fcc_learning_samples[pos].fcc_new = fcc_mah;
3140 chip->fcc_learning_samples[pos].chargecycles = chargecycles;
3141
3142 fcc_new = DIV_ROUND_UP(fcc_mah, chip->fcc_resolution);
3143 rc = qpnp_write_wrapper(chip, (u8 *)&fcc_new,
3144 chip->base + BMS_FCC_BASE_REG + pos, 1);
3145 if (rc)
3146 return rc;
3147
3148 chgcyl = DIV_ROUND_UP(chargecycles, CHGCYL_RESOLUTION);
3149 rc = qpnp_write_wrapper(chip, (u8 *)&chgcyl,
3150 chip->base + BMS_CHGCYL_BASE_REG + pos, 1);
3151 if (rc)
3152 return rc;
3153
3154 pr_debug("Backup new FCC: fcc_new=%d, chargecycle=%d, pos=%d\n",
3155 fcc_new, chgcyl, pos);
3156
3157 return rc;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303158}
3159
3160static void update_fcc_learning_table(struct qpnp_bms_chip *chip,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303161 int new_fcc_uah, int chargecycles, int batt_temp)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303162{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303163 int rc, fcc_default, fcc_temp;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303164
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303165 /* convert the fcc at batt_temp to new fcc at FCC_DEFAULT_TEMP */
3166 fcc_default = calculate_fcc(chip, FCC_DEFAULT_TEMP) / 1000;
3167 fcc_temp = calculate_fcc(chip, batt_temp) / 1000;
3168 new_fcc_uah = (new_fcc_uah / fcc_temp) * fcc_default;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303169
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303170 rc = backup_new_fcc(chip, new_fcc_uah / 1000, chargecycles);
3171 if (rc) {
3172 pr_err("Unable to backup new FCC\n");
3173 return;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303174 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303175 /* check if FCC can be updated */
3176 attempt_learning_new_fcc(chip);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303177}
3178
3179static bool is_new_fcc_valid(int new_fcc_uah, int fcc_uah)
3180{
3181 if ((new_fcc_uah >= (fcc_uah / 2)) &&
3182 ((new_fcc_uah * 100) <= (fcc_uah * 105)))
3183 return true;
3184
3185 pr_debug("FCC rejected - not within valid limit\n");
3186 return false;
3187}
3188
3189static void fcc_learning_config(struct qpnp_bms_chip *chip, bool start)
3190{
3191 int rc, batt_temp;
3192 struct raw_soc_params raw;
3193 struct qpnp_vadc_result result;
3194 int fcc_uah, new_fcc_uah, delta_cc_uah, delta_soc;
3195
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003196 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303197 if (rc) {
3198 pr_err("Unable to read batt_temp\n");
3199 return;
3200 } else {
3201 batt_temp = (int)result.physical;
3202 }
3203
3204 rc = read_soc_params_raw(chip, &raw, batt_temp);
3205 if (rc) {
3206 pr_err("Unable to read CC, cannot update FCC\n");
3207 return;
3208 }
3209
3210 if (start) {
3211 chip->start_pc = interpolate_pc(chip->pc_temp_ocv_lut,
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08003212 batt_temp, raw.last_good_ocv_uv / 1000);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003213 chip->start_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303214 chip->start_real_soc = calculate_real_soc(chip,
3215 batt_temp, &raw, chip->start_cc_uah);
3216 pr_debug("start_pc=%d, start_cc=%d, start_soc=%d real_soc=%d\n",
3217 chip->start_pc, chip->start_cc_uah,
3218 chip->start_soc, chip->start_real_soc);
3219 } else {
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003220 chip->end_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303221 delta_soc = 100 - chip->start_real_soc;
3222 delta_cc_uah = abs(chip->end_cc_uah - chip->start_cc_uah);
3223 new_fcc_uah = div_u64(delta_cc_uah * 100, delta_soc);
3224 fcc_uah = calculate_fcc(chip, batt_temp);
3225 pr_debug("start_soc=%d, start_pc=%d, start_real_soc=%d, start_cc=%d, end_cc=%d, new_fcc=%d\n",
3226 chip->start_soc, chip->start_pc, chip->start_real_soc,
3227 chip->start_cc_uah, chip->end_cc_uah, new_fcc_uah);
3228
3229 if (is_new_fcc_valid(new_fcc_uah, fcc_uah))
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303230 update_fcc_learning_table(chip, new_fcc_uah,
3231 chip->charge_cycles, batt_temp);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303232 }
3233}
3234
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003235#define MAX_CAL_TRIES 200
3236#define MIN_CAL_UA 3000
3237static void batfet_open_work(struct work_struct *work)
3238{
3239 int i;
3240 int rc;
3241 int result_ua;
3242 u8 orig_delay, sample_delay;
3243 struct qpnp_bms_chip *chip = container_of(work,
3244 struct qpnp_bms_chip,
3245 batfet_open_work);
3246
3247 rc = qpnp_read_wrapper(chip, &orig_delay,
3248 chip->base + BMS1_S1_DELAY_CTL, 1);
3249
3250 sample_delay = 0x0;
3251 rc = qpnp_write_wrapper(chip, &sample_delay,
3252 chip->base + BMS1_S1_DELAY_CTL, 1);
3253
3254 /*
3255 * In certain PMICs there is a coupling issue which causes
3256 * bad calibration value that result in a huge battery current
3257 * even when the BATFET is open. Do continious calibrations until
3258 * we hit reasonable cal values which result in low battery current
3259 */
3260
3261 for (i = 0; (!chip->batfet_closed) && i < MAX_CAL_TRIES; i++) {
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003262 rc = qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003263 /*
3264 * Wait 20mS after calibration and before reading battery
3265 * current. The BMS h/w uses calibration values in the
3266 * next sampling of vsense.
3267 */
3268 msleep(20);
3269 rc |= get_battery_current(chip, &result_ua);
3270 if (rc == 0 && abs(result_ua) <= MIN_CAL_UA) {
3271 pr_debug("good cal at %d attempt\n", i);
3272 break;
3273 }
3274 }
3275 pr_debug("batfet_closed = %d i = %d result_ua = %d\n",
3276 chip->batfet_closed, i, result_ua);
3277
3278 rc = qpnp_write_wrapper(chip, &orig_delay,
3279 chip->base + BMS1_S1_DELAY_CTL, 1);
3280}
3281
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003282static void charging_began(struct qpnp_bms_chip *chip)
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003283{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003284 mutex_lock(&chip->last_soc_mutex);
3285 chip->charge_start_tm_sec = 0;
3286 chip->catch_up_time_sec = 0;
3287 mutex_unlock(&chip->last_soc_mutex);
3288
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303289 chip->start_soc = report_state_of_charge(chip);
3290
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003291 mutex_lock(&chip->last_ocv_uv_mutex);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303292 if (chip->enable_fcc_learning)
3293 fcc_learning_config(chip, true);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003294 chip->soc_at_cv = -EINVAL;
3295 chip->prev_chg_soc = -EINVAL;
3296 mutex_unlock(&chip->last_ocv_uv_mutex);
3297}
3298
3299static void charging_ended(struct qpnp_bms_chip *chip)
3300{
3301 mutex_lock(&chip->last_soc_mutex);
3302 chip->charge_start_tm_sec = 0;
3303 chip->catch_up_time_sec = 0;
3304 mutex_unlock(&chip->last_soc_mutex);
3305
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303306 chip->end_soc = report_state_of_charge(chip);
3307
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003308 mutex_lock(&chip->last_ocv_uv_mutex);
3309 chip->soc_at_cv = -EINVAL;
3310 chip->prev_chg_soc = -EINVAL;
Zhenhua Huang95a05d32014-03-31 18:09:45 +08003311 chip->in_taper_charge = false;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303312
3313 /* update the chargecycles */
3314 if (chip->end_soc > chip->start_soc) {
3315 chip->charge_increase += (chip->end_soc - chip->start_soc);
3316 if (chip->charge_increase > 100) {
3317 chip->charge_cycles++;
3318 chip->charge_increase = chip->charge_increase % 100;
3319 }
3320 if (chip->enable_fcc_learning)
3321 backup_charge_cycle(chip);
3322 }
3323
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003324 if (get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL) {
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303325 if (chip->enable_fcc_learning &&
3326 (chip->start_soc <= chip->min_fcc_learning_soc) &&
3327 (chip->start_pc <= chip->min_fcc_ocv_pc))
3328 fcc_learning_config(chip, false);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003329 chip->done_charging = true;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003330 chip->last_soc_invalid = true;
Xiaozhe Shicc48e992013-05-28 16:42:24 -07003331 } else if (chip->charging_adjusted_ocv > 0) {
3332 pr_debug("Charging stopped before full, adjusted OCV = %d\n",
3333 chip->charging_adjusted_ocv);
3334 chip->last_ocv_uv = chip->charging_adjusted_ocv;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003335 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303336
Xiaozhe Shicc48e992013-05-28 16:42:24 -07003337 chip->charging_adjusted_ocv = -EINVAL;
3338
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003339 mutex_unlock(&chip->last_ocv_uv_mutex);
3340}
3341
3342static void battery_status_check(struct qpnp_bms_chip *chip)
3343{
3344 int status = get_battery_status(chip);
3345
Xiaozhe Shibda84992013-09-05 10:39:11 -07003346 mutex_lock(&chip->status_lock);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003347 if (chip->battery_status != status) {
Xiaozhe Shi30e94802013-08-19 16:40:53 -07003348 pr_debug("status = %d, shadow status = %d\n",
3349 status, chip->battery_status);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003350 if (status == POWER_SUPPLY_STATUS_CHARGING) {
3351 pr_debug("charging started\n");
3352 charging_began(chip);
3353 } else if (chip->battery_status
3354 == POWER_SUPPLY_STATUS_CHARGING) {
3355 pr_debug("charging ended\n");
3356 charging_ended(chip);
3357 }
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003358
3359 if (status == POWER_SUPPLY_STATUS_FULL) {
3360 pr_debug("battery full\n");
Xiaozhe Shibda84992013-09-05 10:39:11 -07003361 recalculate_soc(chip);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003362 } else if (chip->battery_status
3363 == POWER_SUPPLY_STATUS_FULL) {
3364 pr_debug("battery not full any more\n");
3365 disable_bms_irq(&chip->ocv_thr_irq);
3366 disable_bms_irq(&chip->sw_cc_thr_irq);
3367 }
3368
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003369 chip->battery_status = status;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003370 /* battery charge status has changed, so force a soc
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003371 * recalculation to update the SoC */
3372 schedule_work(&chip->recalc_work);
3373 }
Xiaozhe Shibda84992013-09-05 10:39:11 -07003374 mutex_unlock(&chip->status_lock);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003375}
3376
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003377#define CALIB_WRKARND_DIG_MAJOR_MAX 0x03
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003378static void batfet_status_check(struct qpnp_bms_chip *chip)
3379{
3380 bool batfet_closed;
3381
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003382 batfet_closed = is_batfet_closed(chip);
3383 if (chip->batfet_closed != batfet_closed) {
3384 chip->batfet_closed = batfet_closed;
Xiaozhe Shiffb7cfc2014-01-03 10:47:36 -08003385 if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX)
3386 return;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003387 if (batfet_closed == false) {
3388 /* batfet opened */
3389 schedule_work(&chip->batfet_open_work);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003390 qpnp_iadc_skip_calibration(chip->iadc_dev);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003391 } else {
3392 /* batfet closed */
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003393 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, true);
3394 qpnp_iadc_resume_calibration(chip->iadc_dev);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003395 }
3396 }
3397}
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003398
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003399static void battery_insertion_check(struct qpnp_bms_chip *chip)
3400{
Xiaozhe Shi90f3a412013-08-21 10:31:35 -07003401 int present = (int)is_battery_present(chip);
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07003402 int insertion_ocv_uv = get_battery_insertion_ocv_uv(chip);
3403 int insertion_ocv_taken = (insertion_ocv_uv > 0);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003404
3405 mutex_lock(&chip->vbat_monitor_mutex);
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07003406 if (chip->battery_present != present
3407 && (present == insertion_ocv_taken
3408 || chip->battery_present == -EINVAL)) {
3409 pr_debug("status = %d, shadow status = %d, insertion_ocv_uv = %d\n",
3410 present, chip->battery_present,
3411 insertion_ocv_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003412 if (chip->battery_present != -EINVAL) {
3413 if (present) {
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07003414 chip->insertion_ocv_uv = insertion_ocv_uv;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003415 setup_vbat_monitoring(chip);
3416 chip->new_battery = true;
3417 } else {
3418 reset_vbat_monitoring(chip);
3419 }
3420 }
3421 chip->battery_present = present;
3422 /* a new battery was inserted or removed, so force a soc
3423 * recalculation to update the SoC */
3424 schedule_work(&chip->recalc_work);
3425 }
3426 mutex_unlock(&chip->vbat_monitor_mutex);
3427}
3428
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003429/* Returns capacity as a SoC percentage between 0 and 100 */
3430static int get_prop_bms_capacity(struct qpnp_bms_chip *chip)
3431{
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08003432 return report_state_of_charge(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003433}
3434
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003435static void qpnp_bms_external_power_changed(struct power_supply *psy)
3436{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003437 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
3438 bms_psy);
3439
3440 battery_insertion_check(chip);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003441 batfet_status_check(chip);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003442 battery_status_check(chip);
Zhenhua Huang95a05d32014-03-31 18:09:45 +08003443
3444 if (POWER_SUPPLY_CHARGE_TYPE_TAPER == get_battery_charge_type(chip))
3445 chip->in_taper_charge = true;
3446 else
3447 chip->in_taper_charge = false;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003448}
3449
3450static int qpnp_bms_power_get_property(struct power_supply *psy,
3451 enum power_supply_property psp,
3452 union power_supply_propval *val)
3453{
3454 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
3455 bms_psy);
3456
3457 switch (psp) {
3458 case POWER_SUPPLY_PROP_CAPACITY:
3459 val->intval = get_prop_bms_capacity(chip);
3460 break;
Xiaozhe Shibda84992013-09-05 10:39:11 -07003461 case POWER_SUPPLY_PROP_STATUS:
3462 val->intval = chip->battery_status;
3463 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003464 case POWER_SUPPLY_PROP_CURRENT_NOW:
3465 val->intval = get_prop_bms_current_now(chip);
3466 break;
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07003467 case POWER_SUPPLY_PROP_RESISTANCE:
3468 val->intval = get_prop_bms_batt_resistance(chip);
3469 break;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07003470 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
3471 val->intval = get_prop_bms_charge_counter(chip);
3472 break;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003473 case POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW:
3474 val->intval = get_prop_bms_charge_counter_shadow(chip);
3475 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003476 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
3477 val->intval = get_prop_bms_charge_full_design(chip);
3478 break;
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05303479 case POWER_SUPPLY_PROP_CHARGE_FULL:
3480 val->intval = get_prop_bms_charge_full(chip);
3481 break;
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05303482 case POWER_SUPPLY_PROP_CYCLE_COUNT:
3483 val->intval = chip->charge_cycles;
3484 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003485 default:
3486 return -EINVAL;
3487 }
3488 return 0;
3489}
3490
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003491#define OCV_USE_LIMIT_EN BIT(7)
3492static int set_ocv_voltage_thresholds(struct qpnp_bms_chip *chip,
3493 int low_voltage_threshold,
3494 int high_voltage_threshold)
3495{
3496 uint16_t low_voltage_raw, high_voltage_raw;
3497 int rc;
3498
3499 low_voltage_raw = convert_vbatt_uv_to_raw(chip,
3500 low_voltage_threshold);
3501 high_voltage_raw = convert_vbatt_uv_to_raw(chip,
3502 high_voltage_threshold);
3503 rc = qpnp_write_wrapper(chip, (u8 *)&low_voltage_raw,
3504 chip->base + BMS1_OCV_USE_LOW_LIMIT_THR0, 2);
3505 if (rc) {
3506 pr_err("Failed to set ocv low voltage threshold: %d\n", rc);
3507 return rc;
3508 }
3509 rc = qpnp_write_wrapper(chip, (u8 *)&high_voltage_raw,
3510 chip->base + BMS1_OCV_USE_HIGH_LIMIT_THR0, 2);
3511 if (rc) {
3512 pr_err("Failed to set ocv high voltage threshold: %d\n", rc);
3513 return rc;
3514 }
3515 rc = qpnp_masked_write(chip, BMS1_OCV_USE_LIMIT_CTL,
3516 OCV_USE_LIMIT_EN, OCV_USE_LIMIT_EN);
3517 if (rc) {
3518 pr_err("Failed to enabled ocv voltage thresholds: %d\n", rc);
3519 return rc;
3520 }
3521 pr_debug("ocv low threshold set to %d uv or 0x%x raw\n",
3522 low_voltage_threshold, low_voltage_raw);
3523 pr_debug("ocv high threshold set to %d uv or 0x%x raw\n",
3524 high_voltage_threshold, high_voltage_raw);
3525 return 0;
3526}
3527
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003528static int read_shutdown_iavg_ma(struct qpnp_bms_chip *chip)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003529{
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003530 u8 iavg;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003531 int rc;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003532
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003533 rc = qpnp_read_wrapper(chip, &iavg, chip->base + IAVG_STORAGE_REG, 1);
3534 if (rc) {
3535 pr_err("failed to read addr = %d %d assuming %d\n",
3536 chip->base + IAVG_STORAGE_REG, rc,
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003537 MIN_IAVG_MA);
3538 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003539 } else if (iavg == IAVG_INVALID) {
3540 pr_err("invalid iavg read from BMS1_DATA_REG_1, using %d\n",
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003541 MIN_IAVG_MA);
3542 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003543 } else {
3544 if (iavg == 0)
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003545 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003546 else
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003547 return MIN_IAVG_MA + IAVG_STEP_SIZE_MA * iavg;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003548 }
3549}
3550
3551static int read_shutdown_soc(struct qpnp_bms_chip *chip)
3552{
3553 u8 stored_soc;
3554 int rc, shutdown_soc;
3555
3556 /*
3557 * The previous SOC is stored in the first 7 bits of the register as
3558 * (Shutdown SOC + 1). This allows for register reset values of both
3559 * 0x00 and 0x7F.
3560 */
3561 rc = qpnp_read_wrapper(chip, &stored_soc, chip->soc_storage_addr, 1);
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003562 if (rc) {
3563 pr_err("failed to read addr = %d %d\n",
3564 chip->soc_storage_addr, rc);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003565 return SOC_INVALID;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003566 }
3567
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003568 if ((stored_soc >> 1) > 0)
3569 shutdown_soc = (stored_soc >> 1) - 1;
3570 else
3571 shutdown_soc = SOC_INVALID;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003572
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003573 pr_debug("stored soc = 0x%02x, shutdown_soc = %d\n",
3574 stored_soc, shutdown_soc);
3575 return shutdown_soc;
3576}
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003577
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003578#define BAT_REMOVED_OFFMODE_BIT BIT(6)
3579static bool is_battery_replaced_in_offmode(struct qpnp_bms_chip *chip)
3580{
3581 u8 batt_pres;
3582 int rc;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003583
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003584 if (chip->batt_pres_addr) {
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003585 rc = qpnp_read_wrapper(chip, &batt_pres,
3586 chip->batt_pres_addr, 1);
3587 pr_debug("offmode removed: %02x\n", batt_pres);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003588 if (!rc && (batt_pres & BAT_REMOVED_OFFMODE_BIT))
3589 return true;
3590 }
3591 return false;
3592}
3593
3594static void load_shutdown_data(struct qpnp_bms_chip *chip)
3595{
3596 int calculated_soc, shutdown_soc;
3597 bool invalid_stored_soc;
3598 bool offmode_battery_replaced;
3599 bool shutdown_soc_out_of_limit;
3600
3601 /*
3602 * Read the saved shutdown SoC from the configured register and
3603 * check if the value has been reset
3604 */
3605 shutdown_soc = read_shutdown_soc(chip);
3606 invalid_stored_soc = (shutdown_soc == SOC_INVALID);
3607
3608 /*
3609 * Do a quick run of SoC calculation to find whether the shutdown soc
3610 * is close enough.
3611 */
Xiaozhe Shi2c171172013-12-03 13:27:37 -08003612 chip->shutdown_iavg_ma = MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003613 calculated_soc = recalculate_raw_soc(chip);
3614 shutdown_soc_out_of_limit = (abs(shutdown_soc - calculated_soc)
3615 > chip->shutdown_soc_valid_limit);
3616 pr_debug("calculated_soc = %d, valid_limit = %d\n",
3617 calculated_soc, chip->shutdown_soc_valid_limit);
3618
3619 /*
3620 * Check if the battery has been replaced while the system was powered
3621 * down.
3622 */
3623 offmode_battery_replaced = is_battery_replaced_in_offmode(chip);
3624
3625 /* Invalidate the shutdown SoC if any of these conditions hold true */
3626 if (chip->ignore_shutdown_soc
3627 || invalid_stored_soc
3628 || offmode_battery_replaced
3629 || shutdown_soc_out_of_limit) {
3630 chip->battery_removed = true;
3631 chip->shutdown_soc_invalid = true;
Xiaozhe Shic92cfd92013-10-25 11:36:42 -07003632 chip->shutdown_iavg_ma = MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003633 pr_debug("Ignoring shutdown SoC: invalid = %d, offmode = %d, out_of_limit = %d\n",
3634 invalid_stored_soc, offmode_battery_replaced,
3635 shutdown_soc_out_of_limit);
3636 } else {
3637 chip->shutdown_iavg_ma = read_shutdown_iavg_ma(chip);
3638 chip->shutdown_soc = shutdown_soc;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07003639 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303640
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003641 pr_debug("raw_soc = %d shutdown_soc = %d shutdown_iavg = %d shutdown_soc_invalid = %d, battery_removed = %d\n",
3642 calculated_soc,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003643 chip->shutdown_soc,
3644 chip->shutdown_iavg_ma,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303645 chip->shutdown_soc_invalid,
3646 chip->battery_removed);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003647}
3648
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003649static irqreturn_t bms_ocv_thr_irq_handler(int irq, void *_chip)
3650{
3651 struct qpnp_bms_chip *chip = _chip;
3652
3653 pr_debug("ocv_thr irq triggered\n");
3654 bms_stay_awake(&chip->soc_wake_source);
3655 schedule_work(&chip->recalc_work);
3656 return IRQ_HANDLED;
3657}
3658
3659static irqreturn_t bms_sw_cc_thr_irq_handler(int irq, void *_chip)
3660{
3661 struct qpnp_bms_chip *chip = _chip;
3662
3663 pr_debug("sw_cc_thr irq triggered\n");
Anirudh Ghayal1166eef2013-12-23 19:05:33 +05303664 disable_bms_irq_nosync(&chip->sw_cc_thr_irq);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003665 bms_stay_awake(&chip->soc_wake_source);
3666 schedule_work(&chip->recalc_work);
3667 return IRQ_HANDLED;
3668}
3669
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003670static int64_t read_battery_id(struct qpnp_bms_chip *chip)
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003671{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003672 int rc;
3673 struct qpnp_vadc_result result;
3674
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003675 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX2_BAT_ID, &result);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003676 if (rc) {
3677 pr_err("error reading batt id channel = %d, rc = %d\n",
3678 LR_MUX2_BAT_ID, rc);
3679 return rc;
3680 }
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003681
3682 return result.physical;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003683}
3684
3685static int set_battery_data(struct qpnp_bms_chip *chip)
3686{
3687 int64_t battery_id;
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003688 int rc = 0, dt_data = false;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003689 struct bms_battery_data *batt_data;
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003690 struct device_node *node;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003691
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003692 if (chip->batt_type == BATT_DESAY) {
3693 batt_data = &desay_5200_data;
3694 } else if (chip->batt_type == BATT_PALLADIUM) {
3695 batt_data = &palladium_1500_data;
3696 } else if (chip->batt_type == BATT_OEM) {
3697 batt_data = &oem_batt_data;
Wu Fenglin2ac88aa2013-04-25 12:43:40 +08003698 } else if (chip->batt_type == BATT_QRD_4V35_2000MAH) {
3699 batt_data = &QRD_4v35_2000mAh_data;
tingtingf50326f2013-06-05 15:07:24 +08003700 } else if (chip->batt_type == BATT_QRD_4V2_1300MAH) {
3701 batt_data = &qrd_4v2_1300mah_data;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003702 } else {
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003703 battery_id = read_battery_id(chip);
3704 if (battery_id < 0) {
3705 pr_err("cannot read battery id err = %lld\n",
3706 battery_id);
3707 return battery_id;
3708 }
3709
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003710 node = of_find_node_by_name(chip->spmi->dev.of_node,
3711 "qcom,battery-data");
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003712 if (!node) {
3713 pr_warn("No available batterydata, using palladium 1500\n");
3714 batt_data = &palladium_1500_data;
3715 goto assign_data;
3716 }
3717 batt_data = devm_kzalloc(chip->dev,
3718 sizeof(struct bms_battery_data), GFP_KERNEL);
3719 if (!batt_data) {
3720 pr_err("Could not alloc battery data\n");
3721 batt_data = &palladium_1500_data;
3722 goto assign_data;
3723 }
3724 batt_data->fcc_temp_lut = devm_kzalloc(chip->dev,
3725 sizeof(struct single_row_lut),
3726 GFP_KERNEL);
3727 batt_data->pc_temp_ocv_lut = devm_kzalloc(chip->dev,
3728 sizeof(struct pc_temp_ocv_lut),
3729 GFP_KERNEL);
3730 batt_data->rbatt_sf_lut = devm_kzalloc(chip->dev,
3731 sizeof(struct sf_lut),
3732 GFP_KERNEL);
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003733
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003734 batt_data->max_voltage_uv = -1;
3735 batt_data->cutoff_uv = -1;
3736 batt_data->iterm_ua = -1;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003737
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003738 /*
3739 * if the alloced luts are 0s, of_batterydata_read_data ignores
3740 * them.
3741 */
3742 rc = of_batterydata_read_data(node, batt_data, battery_id);
3743 if (rc == 0 && batt_data->fcc_temp_lut
3744 && batt_data->pc_temp_ocv_lut
3745 && batt_data->rbatt_sf_lut) {
3746 dt_data = true;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003747 } else {
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003748 pr_err("battery data load failed, using palladium 1500\n");
3749 devm_kfree(chip->dev, batt_data->fcc_temp_lut);
3750 devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut);
3751 devm_kfree(chip->dev, batt_data->rbatt_sf_lut);
3752 devm_kfree(chip->dev, batt_data);
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003753 batt_data = &palladium_1500_data;
3754 }
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003755 }
3756
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003757assign_data:
Xiaozhe Shi976618f2013-04-30 10:49:30 -07003758 chip->fcc_mah = batt_data->fcc;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003759 chip->fcc_temp_lut = batt_data->fcc_temp_lut;
3760 chip->fcc_sf_lut = batt_data->fcc_sf_lut;
3761 chip->pc_temp_ocv_lut = batt_data->pc_temp_ocv_lut;
3762 chip->pc_sf_lut = batt_data->pc_sf_lut;
3763 chip->rbatt_sf_lut = batt_data->rbatt_sf_lut;
3764 chip->default_rbatt_mohm = batt_data->default_rbatt_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07003765 chip->rbatt_capacitive_mohm = batt_data->rbatt_capacitive_mohm;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07003766 chip->flat_ocv_threshold_uv = batt_data->flat_ocv_threshold_uv;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003767
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003768 /* Override battery properties if specified in the battery profile */
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003769 if (batt_data->max_voltage_uv >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003770 chip->max_voltage_uv = batt_data->max_voltage_uv;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003771 if (batt_data->cutoff_uv >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003772 chip->v_cutoff_uv = batt_data->cutoff_uv;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003773 if (batt_data->iterm_ua >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003774 chip->chg_term_ua = batt_data->iterm_ua;
3775
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003776 if (chip->pc_temp_ocv_lut == NULL) {
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003777 pr_err("temp ocv lut table has not been loaded\n");
3778 if (dt_data) {
3779 devm_kfree(chip->dev, batt_data->fcc_temp_lut);
3780 devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut);
3781 devm_kfree(chip->dev, batt_data->rbatt_sf_lut);
3782 devm_kfree(chip->dev, batt_data);
3783 }
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003784 return -EINVAL;
3785 }
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003786
3787 if (dt_data)
3788 devm_kfree(chip->dev, batt_data);
3789
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003790 return 0;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003791}
3792
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07003793static int bms_get_adc(struct qpnp_bms_chip *chip,
3794 struct spmi_device *spmi)
3795{
3796 int rc = 0;
3797
3798 chip->vadc_dev = qpnp_get_vadc(&spmi->dev, "bms");
3799 if (IS_ERR(chip->vadc_dev)) {
3800 rc = PTR_ERR(chip->vadc_dev);
3801 if (rc != -EPROBE_DEFER)
3802 pr_err("vadc property missing, rc=%d\n", rc);
3803 return rc;
3804 }
3805
3806 chip->iadc_dev = qpnp_get_iadc(&spmi->dev, "bms");
3807 if (IS_ERR(chip->iadc_dev)) {
3808 rc = PTR_ERR(chip->iadc_dev);
3809 if (rc != -EPROBE_DEFER)
3810 pr_err("iadc property missing, rc=%d\n", rc);
3811 return rc;
3812 }
3813
3814 chip->adc_tm_dev = qpnp_get_adc_tm(&spmi->dev, "bms");
3815 if (IS_ERR(chip->adc_tm_dev)) {
3816 rc = PTR_ERR(chip->adc_tm_dev);
3817 if (rc != -EPROBE_DEFER)
3818 pr_err("adc-tm not ready, defer probe\n");
3819 return rc;
3820 }
3821
3822 return 0;
3823}
3824
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003825#define SPMI_PROP_READ(chip_prop, qpnp_spmi_property, retval) \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003826do { \
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003827 if (retval) \
3828 break; \
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003829 retval = of_property_read_u32(chip->spmi->dev.of_node, \
Xiaozhe Shi9bd24622013-01-23 15:54:54 -08003830 "qcom," qpnp_spmi_property, \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003831 &chip->chip_prop); \
3832 if (retval) { \
3833 pr_err("Error reading " #qpnp_spmi_property \
3834 " property %d\n", rc); \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003835 } \
3836} while (0)
3837
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303838#define SPMI_PROP_READ_BOOL(chip_prop, qpnp_spmi_property) \
3839do { \
3840 chip->chip_prop = of_property_read_bool(chip->spmi->dev.of_node,\
3841 "qcom," qpnp_spmi_property); \
3842} while (0)
3843
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003844static inline int bms_read_properties(struct qpnp_bms_chip *chip)
3845{
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003846 int rc = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003847
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003848 SPMI_PROP_READ(r_sense_uohm, "r-sense-uohm", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003849 SPMI_PROP_READ(v_cutoff_uv, "v-cutoff-uv", rc);
3850 SPMI_PROP_READ(max_voltage_uv, "max-voltage-uv", rc);
3851 SPMI_PROP_READ(r_conn_mohm, "r-conn-mohm", rc);
3852 SPMI_PROP_READ(chg_term_ua, "chg-term-ua", rc);
3853 SPMI_PROP_READ(shutdown_soc_valid_limit,
3854 "shutdown-soc-valid-limit", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003855 SPMI_PROP_READ(adjust_soc_low_threshold,
3856 "adjust-soc-low-threshold", rc);
3857 SPMI_PROP_READ(batt_type, "batt-type", rc);
3858 SPMI_PROP_READ(low_soc_calc_threshold,
3859 "low-soc-calculate-soc-threshold", rc);
3860 SPMI_PROP_READ(low_soc_calculate_soc_ms,
3861 "low-soc-calculate-soc-ms", rc);
Xiaozhe Shicb487b12013-10-14 17:42:07 -07003862 SPMI_PROP_READ(low_voltage_calculate_soc_ms,
3863 "low-voltage-calculate-soc-ms", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003864 SPMI_PROP_READ(calculate_soc_ms, "calculate-soc-ms", rc);
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07003865 SPMI_PROP_READ(high_ocv_correction_limit_uv,
3866 "high-ocv-correction-limit-uv", rc);
3867 SPMI_PROP_READ(low_ocv_correction_limit_uv,
3868 "low-ocv-correction-limit-uv", rc);
3869 SPMI_PROP_READ(hold_soc_est,
3870 "hold-soc-est", rc);
3871 SPMI_PROP_READ(ocv_high_threshold_uv,
3872 "ocv-voltage-high-threshold-uv", rc);
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003873 SPMI_PROP_READ(ocv_low_threshold_uv,
3874 "ocv-voltage-low-threshold-uv", rc);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08003875 SPMI_PROP_READ(low_voltage_threshold, "low-voltage-threshold", rc);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003876 SPMI_PROP_READ(temperature_margin, "tm-temp-margin", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003877
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003878 chip->use_external_rsense = of_property_read_bool(
3879 chip->spmi->dev.of_node,
3880 "qcom,use-external-rsense");
3881 chip->ignore_shutdown_soc = of_property_read_bool(
3882 chip->spmi->dev.of_node,
3883 "qcom,ignore-shutdown-soc");
3884 chip->use_voltage_soc = of_property_read_bool(chip->spmi->dev.of_node,
3885 "qcom,use-voltage-soc");
3886 chip->use_ocv_thresholds = of_property_read_bool(
3887 chip->spmi->dev.of_node,
3888 "qcom,use-ocv-thresholds");
3889
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003890 if (chip->adjust_soc_low_threshold >= 45)
3891 chip->adjust_soc_low_threshold = 45;
3892
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303893 SPMI_PROP_READ_BOOL(enable_fcc_learning, "enable-fcc-learning");
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303894 if (chip->enable_fcc_learning) {
3895 SPMI_PROP_READ(min_fcc_learning_soc,
3896 "min-fcc-learning-soc", rc);
3897 SPMI_PROP_READ(min_fcc_ocv_pc,
3898 "min-fcc-ocv-pc", rc);
3899 SPMI_PROP_READ(min_fcc_learning_samples,
3900 "min-fcc-learning-samples", rc);
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303901 SPMI_PROP_READ(fcc_resolution,
3902 "fcc-resolution", rc);
3903 if (chip->min_fcc_learning_samples > MAX_FCC_CYCLES)
3904 chip->min_fcc_learning_samples = MAX_FCC_CYCLES;
3905 chip->fcc_learning_samples = devm_kzalloc(&chip->spmi->dev,
3906 (sizeof(struct fcc_sample) *
3907 chip->min_fcc_learning_samples), GFP_KERNEL);
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003908 if (chip->fcc_learning_samples == NULL)
3909 return -ENOMEM;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303910 pr_debug("min-fcc-soc=%d, min-fcc-pc=%d, min-fcc-cycles=%d\n",
3911 chip->min_fcc_learning_soc, chip->min_fcc_ocv_pc,
3912 chip->min_fcc_learning_samples);
3913 }
3914
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003915 if (rc) {
3916 pr_err("Missing required properties.\n");
3917 return rc;
3918 }
3919
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003920 pr_debug("dts data: r_sense_uohm:%d, v_cutoff_uv:%d, max_v:%d\n",
3921 chip->r_sense_uohm, chip->v_cutoff_uv,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003922 chip->max_voltage_uv);
3923 pr_debug("r_conn:%d, shutdown_soc: %d, adjust_soc_low:%d\n",
3924 chip->r_conn_mohm, chip->shutdown_soc_valid_limit,
3925 chip->adjust_soc_low_threshold);
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07003926 pr_debug("chg_term_ua:%d, batt_type:%d\n",
3927 chip->chg_term_ua,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003928 chip->batt_type);
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08003929 pr_debug("ignore_shutdown_soc:%d, use_voltage_soc:%d\n",
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08003930 chip->ignore_shutdown_soc, chip->use_voltage_soc);
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003931 pr_debug("use external rsense: %d\n", chip->use_external_rsense);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003932 return 0;
3933}
3934
3935static inline void bms_initialize_constants(struct qpnp_bms_chip *chip)
3936{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003937 chip->prev_pc_unusable = -EINVAL;
3938 chip->soc_at_cv = -EINVAL;
3939 chip->calculated_soc = -EINVAL;
Xiaozhe Shie118c692012-09-24 15:17:43 -07003940 chip->last_soc = -EINVAL;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07003941 chip->last_soc_est = -EINVAL;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003942 chip->battery_present = -EINVAL;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003943 chip->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08003944 chip->last_cc_uah = INT_MIN;
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08003945 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
3946 chip->prev_last_good_ocv_raw = OCV_RAW_UNINITIALIZED;
Xiaozhe Shie118c692012-09-24 15:17:43 -07003947 chip->first_time_calc_soc = 1;
3948 chip->first_time_calc_uuc = 1;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003949}
3950
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003951#define SPMI_FIND_IRQ(chip, irq_name) \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003952do { \
3953 chip->irq_name##_irq.irq = spmi_get_irq_byname(chip->spmi, \
3954 resource, #irq_name); \
3955 if (chip->irq_name##_irq.irq < 0) { \
3956 pr_err("Unable to get " #irq_name " irq\n"); \
3957 return -ENXIO; \
3958 } \
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003959} while (0)
3960
3961static int bms_find_irqs(struct qpnp_bms_chip *chip,
3962 struct spmi_resource *resource)
3963{
3964 SPMI_FIND_IRQ(chip, sw_cc_thr);
3965 SPMI_FIND_IRQ(chip, ocv_thr);
3966 return 0;
3967}
3968
3969#define SPMI_REQUEST_IRQ(chip, rc, irq_name) \
3970do { \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003971 rc = devm_request_irq(chip->dev, chip->irq_name##_irq.irq, \
3972 bms_##irq_name##_irq_handler, \
3973 IRQF_TRIGGER_RISING, #irq_name, chip); \
3974 if (rc < 0) { \
3975 pr_err("Unable to request " #irq_name " irq: %d\n", rc);\
3976 return -ENXIO; \
3977 } \
Xiaozhe Shif511a6e2014-02-20 14:37:18 -08003978 chip->irq_name##_irq.ready = true; \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003979} while (0)
3980
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003981static int bms_request_irqs(struct qpnp_bms_chip *chip)
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003982{
3983 int rc;
3984
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003985 SPMI_REQUEST_IRQ(chip, rc, sw_cc_thr);
Fenglin Wuc17e97b2015-01-20 16:49:58 +08003986 chip->sw_cc_thr_irq.is_wake = true;
Abhijeet Dharmapurikardce21e62013-08-07 15:42:32 -07003987 disable_bms_irq(&chip->sw_cc_thr_irq);
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003988 SPMI_REQUEST_IRQ(chip, rc, ocv_thr);
Fenglin Wuc17e97b2015-01-20 16:49:58 +08003989 chip->ocv_thr_irq.is_wake = true;
Abhijeet Dharmapurikardce21e62013-08-07 15:42:32 -07003990 disable_bms_irq(&chip->ocv_thr_irq);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003991 return 0;
3992}
3993
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003994#define REG_OFFSET_PERP_TYPE 0x04
3995#define REG_OFFSET_PERP_SUBTYPE 0x05
3996#define BMS_BMS_TYPE 0xD
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003997#define BMS_BMS1_SUBTYPE 0x1
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003998#define BMS_IADC_TYPE 0x8
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003999#define BMS_IADC1_SUBTYPE 0x3
4000#define BMS_IADC2_SUBTYPE 0x5
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004001
4002static int register_spmi(struct qpnp_bms_chip *chip, struct spmi_device *spmi)
4003{
4004 struct spmi_resource *spmi_resource;
4005 struct resource *resource;
4006 int rc;
4007 u8 type, subtype;
4008
4009 chip->dev = &(spmi->dev);
4010 chip->spmi = spmi;
4011
4012 spmi_for_each_container_dev(spmi_resource, spmi) {
4013 if (!spmi_resource) {
4014 pr_err("qpnp_bms: spmi resource absent\n");
4015 return -ENXIO;
4016 }
4017
4018 resource = spmi_get_resource(spmi, spmi_resource,
4019 IORESOURCE_MEM, 0);
4020 if (!(resource && resource->start)) {
4021 pr_err("node %s IO resource absent!\n",
4022 spmi->dev.of_node->full_name);
4023 return -ENXIO;
4024 }
4025
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07004026 pr_debug("Node name = %s\n", spmi_resource->of_node->name);
4027
4028 if (strcmp("qcom,batt-pres-status",
4029 spmi_resource->of_node->name) == 0) {
4030 chip->batt_pres_addr = resource->start;
4031 continue;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07004032 } else if (strcmp("qcom,soc-storage-reg",
4033 spmi_resource->of_node->name) == 0) {
4034 chip->soc_storage_addr = resource->start;
4035 continue;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07004036 }
4037
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004038 rc = qpnp_read_wrapper(chip, &type,
4039 resource->start + REG_OFFSET_PERP_TYPE, 1);
4040 if (rc) {
4041 pr_err("Peripheral type read failed rc=%d\n", rc);
4042 return rc;
4043 }
4044 rc = qpnp_read_wrapper(chip, &subtype,
4045 resource->start + REG_OFFSET_PERP_SUBTYPE, 1);
4046 if (rc) {
4047 pr_err("Peripheral subtype read failed rc=%d\n", rc);
4048 return rc;
4049 }
4050
Xiaozhe Shief6274c2013-03-06 15:23:52 -08004051 if (type == BMS_BMS_TYPE && subtype == BMS_BMS1_SUBTYPE) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004052 chip->base = resource->start;
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07004053 rc = bms_find_irqs(chip, spmi_resource);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004054 if (rc) {
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07004055 pr_err("Could not find irqs\n");
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004056 return rc;
4057 }
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004058 } else if (type == BMS_IADC_TYPE
Xiaozhe Shief6274c2013-03-06 15:23:52 -08004059 && (subtype == BMS_IADC1_SUBTYPE
4060 || subtype == BMS_IADC2_SUBTYPE)) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004061 chip->iadc_base = resource->start;
4062 } else {
4063 pr_err("Invalid peripheral start=0x%x type=0x%x, subtype=0x%x\n",
4064 resource->start, type, subtype);
4065 }
4066 }
4067
4068 if (chip->base == 0) {
4069 dev_err(&spmi->dev, "BMS peripheral was not registered\n");
4070 return -EINVAL;
4071 }
4072 if (chip->iadc_base == 0) {
4073 dev_err(&spmi->dev, "BMS_IADC peripheral was not registered\n");
4074 return -EINVAL;
4075 }
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07004076 if (chip->soc_storage_addr == 0) {
4077 /* default to dvdd backed BMS data reg0 */
4078 chip->soc_storage_addr = chip->base + SOC_STORAGE_REG;
4079 }
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004080
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07004081 pr_debug("bms-base = 0x%04x, iadc-base = 0x%04x, bat-pres-reg = 0x%04x, soc-storage-reg = 0x%04x\n",
4082 chip->base, chip->iadc_base,
4083 chip->batt_pres_addr, chip->soc_storage_addr);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004084 return 0;
4085}
4086
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004087#define ADC_CH_SEL_MASK 0x7
4088#define ADC_INT_RSNSN_CTL_MASK 0x3
4089#define ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE 0x2
4090#define FAST_AVG_EN_MASK 0x80
4091#define FAST_AVG_EN_VALUE_EXT_RSENSE 0x80
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004092static int read_iadc_channel_select(struct qpnp_bms_chip *chip)
4093{
4094 u8 iadc_channel_select;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004095 int32_t rds_rsense_nohm;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004096 int rc;
4097
4098 rc = qpnp_read_wrapper(chip, &iadc_channel_select,
4099 chip->iadc_base + IADC1_BMS_ADC_CH_SEL_CTL, 1);
4100 if (rc) {
4101 pr_err("Error reading bms_iadc channel register %d\n", rc);
4102 return rc;
4103 }
4104
4105 iadc_channel_select &= ADC_CH_SEL_MASK;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004106 if (iadc_channel_select != EXTERNAL_RSENSE
4107 && iadc_channel_select != INTERNAL_RSENSE) {
4108 pr_err("IADC1_BMS_IADC configured incorrectly. Selected channel = %d\n",
4109 iadc_channel_select);
4110 return -EINVAL;
4111 }
4112
4113 if (chip->use_external_rsense) {
4114 pr_debug("External rsense selected\n");
4115 if (iadc_channel_select == INTERNAL_RSENSE) {
4116 pr_debug("Internal rsense detected; Changing rsense to external\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004117 rc = qpnp_masked_write_iadc(chip,
4118 IADC1_BMS_ADC_CH_SEL_CTL,
4119 ADC_CH_SEL_MASK,
4120 EXTERNAL_RSENSE);
4121 if (rc) {
4122 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
4123 IADC1_BMS_ADC_CH_SEL_CTL,
4124 EXTERNAL_RSENSE, rc);
4125 return rc;
4126 }
Xiaozhe Shif3da8622013-06-10 14:50:56 -07004127 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07004128 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07004129 chip->software_shdw_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004130 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004131 } else {
4132 pr_debug("Internal rsense selected\n");
4133 if (iadc_channel_select == EXTERNAL_RSENSE) {
4134 pr_debug("External rsense detected; Changing rsense to internal\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004135 rc = qpnp_masked_write_iadc(chip,
4136 IADC1_BMS_ADC_CH_SEL_CTL,
4137 ADC_CH_SEL_MASK,
4138 INTERNAL_RSENSE);
4139 if (rc) {
4140 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
4141 IADC1_BMS_ADC_CH_SEL_CTL,
4142 INTERNAL_RSENSE, rc);
4143 return rc;
4144 }
Xiaozhe Shif3da8622013-06-10 14:50:56 -07004145 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
4146 chip->software_shdw_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004147 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004148
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07004149 rc = qpnp_iadc_get_rsense(chip->iadc_dev, &rds_rsense_nohm);
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004150 if (rc) {
4151 pr_err("Unable to read RDS resistance value from IADC; rc = %d\n",
4152 rc);
4153 return rc;
4154 }
Xiaozhe Shid0a79542012-11-06 10:00:38 -08004155 chip->r_sense_uohm = rds_rsense_nohm/1000;
4156 pr_debug("rds_rsense = %d nOhm, saved as %d uOhm\n",
4157 rds_rsense_nohm, chip->r_sense_uohm);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004158 }
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004159 /* prevent shorting of leads by IADC_BMS when external Rsense is used */
4160 if (chip->use_external_rsense) {
4161 if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX) {
4162 rc = qpnp_masked_write_iadc(chip,
4163 IADC1_BMS_ADC_INT_RSNSN_CTL,
4164 ADC_INT_RSNSN_CTL_MASK,
4165 ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE);
4166 if (rc) {
4167 pr_err("Unable to set batfet config %x to %x: %d\n",
4168 IADC1_BMS_ADC_INT_RSNSN_CTL,
4169 ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE, rc);
4170 return rc;
4171 }
4172 } else {
4173 /* In older PMICS use FAST_AVG_EN register bit 7 */
4174 rc = qpnp_masked_write_iadc(chip,
4175 IADC1_BMS_FAST_AVG_EN,
4176 FAST_AVG_EN_MASK,
4177 FAST_AVG_EN_VALUE_EXT_RSENSE);
4178 if (rc) {
4179 pr_err("Unable to set batfet config %x to %x: %d\n",
4180 IADC1_BMS_FAST_AVG_EN,
4181 FAST_AVG_EN_VALUE_EXT_RSENSE, rc);
4182 return rc;
4183 }
4184 }
4185 }
4186
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004187 return 0;
4188}
4189
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004190static int refresh_die_temp_monitor(struct qpnp_bms_chip *chip)
4191{
4192 struct qpnp_vadc_result result;
4193 int rc;
4194
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07004195 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004196
4197 pr_debug("low = %lld, high = %lld\n",
4198 result.physical - chip->temperature_margin,
4199 result.physical + chip->temperature_margin);
4200 chip->die_temp_monitor_params.high_temp = result.physical
4201 + chip->temperature_margin;
4202 chip->die_temp_monitor_params.low_temp = result.physical
4203 - chip->temperature_margin;
4204 chip->die_temp_monitor_params.state_request =
4205 ADC_TM_HIGH_LOW_THR_ENABLE;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004206 return qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
4207 &chip->die_temp_monitor_params);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004208}
4209
4210static void btm_notify_die_temp(enum qpnp_tm_state state, void *ctx)
4211{
4212 struct qpnp_bms_chip *chip = ctx;
4213 struct qpnp_vadc_result result;
4214 int rc;
4215
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07004216 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004217
4218 if (state == ADC_TM_LOW_STATE)
4219 pr_debug("low state triggered\n");
4220 else if (state == ADC_TM_HIGH_STATE)
4221 pr_debug("high state triggered\n");
4222 pr_debug("die temp = %lld, raw = 0x%x\n",
4223 result.physical, result.adc_code);
4224 schedule_work(&chip->recalc_work);
4225 refresh_die_temp_monitor(chip);
4226}
4227
4228static int setup_die_temp_monitoring(struct qpnp_bms_chip *chip)
4229{
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004230 int rc;
4231
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004232 chip->die_temp_monitor_params.channel = DIE_TEMP;
4233 chip->die_temp_monitor_params.btm_ctx = (void *)chip;
4234 chip->die_temp_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
4235 chip->die_temp_monitor_params.threshold_notification =
4236 &btm_notify_die_temp;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004237 rc = refresh_die_temp_monitor(chip);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004238 if (rc) {
4239 pr_err("tm setup failed: %d\n", rc);
4240 return rc;
4241 }
4242 pr_debug("setup complete\n");
4243 return 0;
4244}
4245
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004246static int __devinit qpnp_bms_probe(struct spmi_device *spmi)
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004247{
4248 struct qpnp_bms_chip *chip;
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004249 bool warm_reset;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004250 int rc, vbatt;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004251
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07004252 chip = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_bms_chip),
4253 GFP_KERNEL);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004254
4255 if (chip == NULL) {
4256 pr_err("kzalloc() failed.\n");
4257 return -ENOMEM;
4258 }
4259
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004260 rc = bms_get_adc(chip, spmi);
4261 if (rc < 0)
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004262 goto error_read;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004263
Xiaozhe Shi3eaf0b62013-07-11 09:48:08 -07004264 mutex_init(&chip->bms_output_lock);
4265 mutex_init(&chip->last_ocv_uv_mutex);
4266 mutex_init(&chip->vbat_monitor_mutex);
4267 mutex_init(&chip->soc_invalidation_mutex);
4268 mutex_init(&chip->last_soc_mutex);
Xiaozhe Shibda84992013-09-05 10:39:11 -07004269 mutex_init(&chip->status_lock);
Xiaozhe Shi27375822013-08-22 11:40:15 -07004270 init_waitqueue_head(&chip->bms_wait_queue);
Xiaozhe Shi3eaf0b62013-07-11 09:48:08 -07004271
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004272 warm_reset = qpnp_pon_is_warm_reset();
4273 rc = warm_reset;
4274 if (rc < 0)
4275 goto error_read;
4276
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004277 rc = register_spmi(chip, spmi);
4278 if (rc) {
4279 pr_err("error registering spmi resource %d\n", rc);
4280 goto error_resource;
4281 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004282
4283 rc = qpnp_read_wrapper(chip, &chip->revision1,
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004284 chip->base + REVISION1, 1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004285 if (rc) {
4286 pr_err("error reading version register %d\n", rc);
4287 goto error_read;
4288 }
4289
4290 rc = qpnp_read_wrapper(chip, &chip->revision2,
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004291 chip->base + REVISION2, 1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004292 if (rc) {
4293 pr_err("Error reading version register %d\n", rc);
4294 goto error_read;
4295 }
Xiaozhe Shia045a562012-11-28 16:55:39 -08004296 pr_debug("BMS version: %hhu.%hhu\n", chip->revision2, chip->revision1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004297
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004298 rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision2,
4299 chip->iadc_base + REVISION2, 1);
4300 if (rc) {
4301 pr_err("Error reading version register %d\n", rc);
4302 goto error_read;
4303 }
4304
4305 rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision1,
4306 chip->iadc_base + REVISION1, 1);
4307 if (rc) {
4308 pr_err("Error reading version register %d\n", rc);
4309 goto error_read;
4310 }
4311 pr_debug("IADC_BMS version: %hhu.%hhu\n",
4312 chip->iadc_bms_revision2, chip->iadc_bms_revision1);
4313
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004314 rc = bms_read_properties(chip);
4315 if (rc) {
4316 pr_err("Unable to read all bms properties, rc = %d\n", rc);
4317 goto error_read;
4318 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004319
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004320 rc = read_iadc_channel_select(chip);
4321 if (rc) {
4322 pr_err("Unable to get iadc selected channel = %d\n", rc);
4323 goto error_read;
4324 }
4325
Xiaozhe Shibdf14742012-12-05 12:41:48 -08004326 if (chip->use_ocv_thresholds) {
4327 rc = set_ocv_voltage_thresholds(chip,
4328 chip->ocv_low_threshold_uv,
4329 chip->ocv_high_threshold_uv);
4330 if (rc) {
4331 pr_err("Could not set ocv voltage thresholds: %d\n",
4332 rc);
4333 goto error_read;
4334 }
4335 }
4336
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004337 rc = set_battery_data(chip);
4338 if (rc) {
4339 pr_err("Bad battery data %d\n", rc);
4340 goto error_read;
4341 }
4342
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004343 bms_initialize_constants(chip);
4344
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004345 wakeup_source_init(&chip->soc_wake_source.source, "qpnp_soc_wake");
Xiaozhe Shi4be85782013-02-22 17:33:40 -08004346 wake_lock_init(&chip->low_voltage_wake_lock, WAKE_LOCK_SUSPEND,
4347 "qpnp_low_voltage_lock");
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004348 wake_lock_init(&chip->cv_wake_lock, WAKE_LOCK_SUSPEND,
4349 "qpnp_cv_lock");
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004350 INIT_DELAYED_WORK(&chip->calculate_soc_delayed_work,
4351 calculate_soc_work);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08004352 INIT_WORK(&chip->recalc_work, recalculate_work);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07004353 INIT_WORK(&chip->batfet_open_work, batfet_open_work);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004354
Xiaozhe Shif9f99242013-08-29 12:27:50 -07004355 dev_set_drvdata(&spmi->dev, chip);
4356 device_init_wakeup(&spmi->dev, 1);
4357
4358 load_shutdown_data(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004359
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05304360 if (chip->enable_fcc_learning) {
Anirudh Ghayale0c02932013-07-08 16:26:35 +05304361 if (chip->battery_removed) {
4362 rc = discard_backup_fcc_data(chip);
4363 if (rc)
4364 pr_err("Could not discard backed-up FCC data\n");
4365 } else {
4366 rc = read_chgcycle_data_from_backup(chip);
4367 if (rc)
4368 pr_err("Unable to restore charge-cycle data\n");
4369
4370 rc = read_fcc_data_from_backup(chip);
4371 if (rc)
4372 pr_err("Unable to restore FCC-learning data\n");
4373 else
4374 attempt_learning_new_fcc(chip);
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05304375 }
4376 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05304377
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004378 rc = setup_vbat_monitoring(chip);
4379 if (rc < 0) {
4380 pr_err("failed to set up voltage notifications: %d\n", rc);
4381 goto error_setup;
Xiaozhe Shid5d21412013-02-06 17:14:41 -08004382 }
4383
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004384 rc = setup_die_temp_monitoring(chip);
4385 if (rc < 0) {
4386 pr_err("failed to set up die temp notifications: %d\n", rc);
4387 goto error_setup;
4388 }
4389
Xu Kai870f8e82014-01-16 19:21:01 +08004390 rc = bms_request_irqs(chip);
4391 if (rc) {
4392 pr_err("error requesting bms irqs, rc = %d\n", rc);
4393 goto error_setup;
4394 }
4395
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07004396 battery_insertion_check(chip);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07004397 batfet_status_check(chip);
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07004398 battery_status_check(chip);
4399
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004400 calculate_soc_work(&(chip->calculate_soc_delayed_work.work));
4401
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004402 /* setup & register the battery power supply */
4403 chip->bms_psy.name = "bms";
4404 chip->bms_psy.type = POWER_SUPPLY_TYPE_BMS;
4405 chip->bms_psy.properties = msm_bms_power_props;
4406 chip->bms_psy.num_properties = ARRAY_SIZE(msm_bms_power_props);
4407 chip->bms_psy.get_property = qpnp_bms_power_get_property;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004408 chip->bms_psy.external_power_changed =
4409 qpnp_bms_external_power_changed;
4410 chip->bms_psy.supplied_to = qpnp_bms_supplicants;
4411 chip->bms_psy.num_supplicants = ARRAY_SIZE(qpnp_bms_supplicants);
4412
4413 rc = power_supply_register(chip->dev, &chip->bms_psy);
4414
4415 if (rc < 0) {
4416 pr_err("power_supply_register bms failed rc = %d\n", rc);
4417 goto unregister_dc;
4418 }
4419
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07004420 chip->bms_psy_registered = true;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004421 vbatt = 0;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07004422 rc = get_battery_voltage(chip, &vbatt);
Xiaozhe Shi36458962013-02-06 16:19:57 -08004423 if (rc) {
4424 pr_err("error reading vbat_sns adc channel = %d, rc = %d\n",
4425 VBAT_SNS, rc);
4426 goto unregister_dc;
4427 }
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004428
wangxlf0e6b1f2014-12-31 09:51:40 +08004429#ifdef CONFIG_TCMD
4430 bms_chip = chip;
4431#endif
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004432 pr_info("probe success: soc =%d vbatt = %d ocv = %d r_sense_uohm = %u warm_reset = %d\n",
4433 get_prop_bms_capacity(chip), vbatt, chip->last_ocv_uv,
4434 chip->r_sense_uohm, warm_reset);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004435 return 0;
4436
4437unregister_dc:
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07004438 chip->bms_psy_registered = false;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004439 power_supply_unregister(&chip->bms_psy);
4440error_setup:
4441 dev_set_drvdata(&spmi->dev, NULL);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004442 wakeup_source_trash(&chip->soc_wake_source.source);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08004443 wake_lock_destroy(&chip->low_voltage_wake_lock);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004444 wake_lock_destroy(&chip->cv_wake_lock);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004445error_resource:
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004446error_read:
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004447 return rc;
4448}
4449
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07004450static int qpnp_bms_remove(struct spmi_device *spmi)
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004451{
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004452 dev_set_drvdata(&spmi->dev, NULL);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004453 return 0;
4454}
4455
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004456static int bms_suspend(struct device *dev)
4457{
4458 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
4459
4460 cancel_delayed_work_sync(&chip->calculate_soc_delayed_work);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07004461 chip->was_charging_at_sleep = is_battery_charging(chip);
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004462 return 0;
4463}
4464
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004465static int bms_resume(struct device *dev)
4466{
4467 int rc;
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004468 int soc_calc_period;
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004469 int time_until_next_recalc = 0;
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004470 unsigned long time_since_last_recalc;
4471 unsigned long tm_now_sec;
4472 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
4473
4474 rc = get_current_time(&tm_now_sec);
4475 if (rc) {
4476 pr_err("Could not read current time: %d\n", rc);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004477 } else {
Xiaozhe Shicb487b12013-10-14 17:42:07 -07004478 soc_calc_period = get_calculation_delay_ms(chip);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004479 time_since_last_recalc = tm_now_sec - chip->last_recalc_time;
4480 pr_debug("Time since last recalc: %lu\n",
4481 time_since_last_recalc);
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004482 time_until_next_recalc = max(0, soc_calc_period
4483 - (int)(time_since_last_recalc * 1000));
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004484 }
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004485
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004486 if (time_until_next_recalc == 0)
4487 bms_stay_awake(&chip->soc_wake_source);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004488 schedule_delayed_work(&chip->calculate_soc_delayed_work,
4489 round_jiffies_relative(msecs_to_jiffies
4490 (time_until_next_recalc)));
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004491 return 0;
4492}
4493
4494static const struct dev_pm_ops qpnp_bms_pm_ops = {
4495 .resume = bms_resume,
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004496 .suspend = bms_suspend,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004497};
4498
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004499static struct spmi_driver qpnp_bms_driver = {
4500 .probe = qpnp_bms_probe,
4501 .remove = __devexit_p(qpnp_bms_remove),
4502 .driver = {
4503 .name = QPNP_BMS_DEV_NAME,
4504 .owner = THIS_MODULE,
4505 .of_match_table = qpnp_bms_match_table,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004506 .pm = &qpnp_bms_pm_ops,
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004507 },
4508};
4509
4510static int __init qpnp_bms_init(void)
4511{
4512 pr_info("QPNP BMS INIT\n");
4513 return spmi_driver_register(&qpnp_bms_driver);
4514}
4515
4516static void __exit qpnp_bms_exit(void)
4517{
4518 pr_info("QPNP BMS EXIT\n");
4519 return spmi_driver_unregister(&qpnp_bms_driver);
4520}
4521
4522module_init(qpnp_bms_init);
4523module_exit(qpnp_bms_exit);
4524
4525MODULE_DESCRIPTION("QPNP BMS Driver");
4526MODULE_LICENSE("GPL v2");
4527MODULE_ALIAS("platform:" QPNP_BMS_DEV_NAME);