blob: de721b2ebd6ed55fed6de3080747319d8cacc73e [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>
Todd Poynorac2a0462012-06-19 18:04:30 -070030#include <linux/wakelock.h>
Xiaozhe Shib19f7032012-08-16 12:14:16 -070031
Xiaozhe Shib19f7032012-08-16 12:14:16 -070032/* BMS Register Offsets */
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -070033#define REVISION1 0x0
34#define REVISION2 0x1
Xiaozhe Shib19f7032012-08-16 12:14:16 -070035#define BMS1_STATUS1 0x8
36#define BMS1_MODE_CTL 0X40
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070037/* Coulomb counter clear registers */
Xiaozhe Shib19f7032012-08-16 12:14:16 -070038#define BMS1_CC_DATA_CTL 0x42
Xiaozhe Shia045a562012-11-28 16:55:39 -080039#define BMS1_CC_CLEAR_CTL 0x43
Xiaozhe Shi20640b52013-01-03 11:49:30 -080040/* BMS Tolerances */
41#define BMS1_TOL_CTL 0X44
Xiaozhe Shib19f7032012-08-16 12:14:16 -070042/* OCV limit registers */
43#define BMS1_OCV_USE_LOW_LIMIT_THR0 0x48
44#define BMS1_OCV_USE_LOW_LIMIT_THR1 0x49
45#define BMS1_OCV_USE_HIGH_LIMIT_THR0 0x4A
46#define BMS1_OCV_USE_HIGH_LIMIT_THR1 0x4B
47#define BMS1_OCV_USE_LIMIT_CTL 0x4C
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -070048/* Delay control */
49#define BMS1_S1_DELAY_CTL 0x5A
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -080050/* OCV interrupt threshold */
51#define BMS1_OCV_THR0 0x50
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -070052#define BMS1_S2_SAMP_AVG_CTL 0x61
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -080053/* SW CC interrupt threshold */
54#define BMS1_SW_CC_THR0 0xA0
Xiaozhe Shib19f7032012-08-16 12:14:16 -070055/* OCV for r registers */
56#define BMS1_OCV_FOR_R_DATA0 0x80
Xiaozhe Shib19f7032012-08-16 12:14:16 -070057#define BMS1_VSENSE_FOR_R_DATA0 0x82
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070058/* Coulomb counter data */
Xiaozhe Shib19f7032012-08-16 12:14:16 -070059#define BMS1_CC_DATA0 0x8A
Xiaozhe Shif3da8622013-06-10 14:50:56 -070060/* Shadow Coulomb counter data */
61#define BMS1_SW_CC_DATA0 0xA8
Xiaozhe Shib19f7032012-08-16 12:14:16 -070062/* OCV for soc data */
63#define BMS1_OCV_FOR_SOC_DATA0 0x90
Xiaozhe Shib19f7032012-08-16 12:14:16 -070064#define BMS1_VSENSE_PON_DATA0 0x94
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070065#define BMS1_VSENSE_AVG_DATA0 0x98
Xiaozhe Shib19f7032012-08-16 12:14:16 -070066#define BMS1_VBAT_AVG_DATA0 0x9E
Xiaozhe Shib19f7032012-08-16 12:14:16 -070067/* Extra bms registers */
Xiaozhe Shi57058942013-03-27 16:54:54 -070068#define SOC_STORAGE_REG 0xB0
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070069#define IAVG_STORAGE_REG 0xB1
Anirudh Ghayale0c02932013-07-08 16:26:35 +053070#define BMS_FCC_COUNT 0xB2
71#define BMS_FCC_BASE_REG 0xB3 /* FCC updates - 0xB3 to 0xB7 */
72#define BMS_CHGCYL_BASE_REG 0xB8 /* FCC chgcyl - 0xB8 to 0xBC */
73#define CHARGE_INCREASE_STORAGE 0xBD
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053074#define CHARGE_CYCLE_STORAGE_LSB 0xBE /* LSB=0xBE, MSB=0xBF */
75
Xiaozhe Shic40b3972012-11-30 14:11:16 -080076/* IADC Channel Select */
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -070077#define IADC1_BMS_REVISION2 0x01
Xiaozhe Shic40b3972012-11-30 14:11:16 -080078#define IADC1_BMS_ADC_CH_SEL_CTL 0x48
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -070079#define IADC1_BMS_ADC_INT_RSNSN_CTL 0x49
80#define IADC1_BMS_FAST_AVG_EN 0x5B
Xiaozhe Shib19f7032012-08-16 12:14:16 -070081
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070082/* Configuration for saving of shutdown soc/iavg */
83#define IGNORE_SOC_TEMP_DECIDEG 50
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -070084#define IAVG_STEP_SIZE_MA 10
Xiaozhe Shif5f966d2013-02-19 14:23:11 -080085#define IAVG_INVALID 0xFF
Xiaozhe Shif9f99242013-08-29 12:27:50 -070086#define SOC_INVALID 0x7E
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070087
Xiaozhe Shie118c692012-09-24 15:17:43 -070088#define IAVG_SAMPLES 16
89
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053090/* FCC learning constants */
Anirudh Ghayale0c02932013-07-08 16:26:35 +053091#define MAX_FCC_CYCLES 5
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053092#define DELTA_FCC_PERCENT 5
93#define VALID_FCC_CHGCYL_RANGE 50
Anirudh Ghayale0c02932013-07-08 16:26:35 +053094#define CHGCYL_RESOLUTION 20
95#define FCC_DEFAULT_TEMP 250
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053096
Xiaozhe Shib19f7032012-08-16 12:14:16 -070097#define QPNP_BMS_DEV_NAME "qcom,qpnp-bms"
98
Xiaozhe Shif3da8622013-06-10 14:50:56 -070099enum {
100 SHDW_CC,
101 CC
102};
103
104enum {
105 NORESET,
106 RESET
107};
108
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700109struct soc_params {
110 int fcc_uah;
111 int cc_uah;
Xiaozhe Shi904f1f72012-12-04 12:47:21 -0800112 int rbatt_mohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700113 int iavg_ua;
114 int uuc_uah;
115 int ocv_charge_uah;
Xiaozhe Shif36d2862013-01-04 10:17:35 -0800116 int delta_time_s;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700117};
118
119struct raw_soc_params {
120 uint16_t last_good_ocv_raw;
121 int64_t cc;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700122 int64_t shdw_cc;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700123 int last_good_ocv_uv;
124};
125
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530126struct fcc_sample {
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530127 int fcc_new;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530128 int chargecycles;
129};
130
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800131struct bms_irq {
132 unsigned int irq;
133 unsigned long disabled;
Fenglin Wuc17e97b2015-01-20 16:49:58 +0800134 unsigned long wake_enabled;
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800135 bool ready;
Fenglin Wuc17e97b2015-01-20 16:49:58 +0800136 bool is_wake;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800137};
138
139struct bms_wakeup_source {
140 struct wakeup_source source;
141 unsigned long disabled;
142};
143
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700144struct qpnp_bms_chip {
145 struct device *dev;
146 struct power_supply bms_psy;
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -0700147 bool bms_psy_registered;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -0700148 struct power_supply *batt_psy;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700149 struct spmi_device *spmi;
Xiaozhe Shi27375822013-08-22 11:40:15 -0700150 wait_queue_head_t bms_wait_queue;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700151 u16 base;
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800152 u16 iadc_base;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -0700153 u16 batt_pres_addr;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -0700154 u16 soc_storage_addr;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700155
156 u8 revision1;
157 u8 revision2;
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -0700158
159 u8 iadc_bms_revision1;
160 u8 iadc_bms_revision2;
161
Xiaozhe Shid5d21412013-02-06 17:14:41 -0800162 int battery_present;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700163 int battery_status;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700164 bool batfet_closed;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800165 bool new_battery;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700166 bool done_charging;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800167 bool last_soc_invalid;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700168 /* platform data */
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -0800169 int r_sense_uohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700170 unsigned int v_cutoff_uv;
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -0800171 int max_voltage_uv;
172 int r_conn_mohm;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700173 int shutdown_soc_valid_limit;
174 int adjust_soc_low_threshold;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700175 int chg_term_ua;
Xiaozhe Shi73a65692012-09-18 17:51:57 -0700176 enum battery_type batt_type;
Xiaozhe Shi976618f2013-04-30 10:49:30 -0700177 unsigned int fcc_mah;
Xiaozhe Shi73a65692012-09-18 17:51:57 -0700178 struct single_row_lut *fcc_temp_lut;
179 struct single_row_lut *fcc_sf_lut;
180 struct pc_temp_ocv_lut *pc_temp_ocv_lut;
181 struct sf_lut *pc_sf_lut;
182 struct sf_lut *rbatt_sf_lut;
183 int default_rbatt_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -0700184 int rbatt_capacitive_mohm;
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -0700185 int rbatt_mohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700186
187 struct delayed_work calculate_soc_delayed_work;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800188 struct work_struct recalc_work;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700189 struct work_struct batfet_open_work;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700190
191 struct mutex bms_output_lock;
192 struct mutex last_ocv_uv_mutex;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700193 struct mutex vbat_monitor_mutex;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700194 struct mutex soc_invalidation_mutex;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700195 struct mutex last_soc_mutex;
Xiaozhe Shibda84992013-09-05 10:39:11 -0700196 struct mutex status_lock;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700197
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800198 bool use_external_rsense;
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800199 bool use_ocv_thresholds;
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800200
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700201 bool ignore_shutdown_soc;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800202 bool shutdown_soc_invalid;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700203 int shutdown_soc;
204 int shutdown_iavg_ma;
205
Xiaozhe Shi4be85782013-02-22 17:33:40 -0800206 struct wake_lock low_voltage_wake_lock;
Xiaozhe Shi4be85782013-02-22 17:33:40 -0800207 int low_voltage_threshold;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700208 int low_soc_calc_threshold;
209 int low_soc_calculate_soc_ms;
Xiaozhe Shicb487b12013-10-14 17:42:07 -0700210 int low_voltage_calculate_soc_ms;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700211 int calculate_soc_ms;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800212 struct bms_wakeup_source soc_wake_source;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700213 struct wake_lock cv_wake_lock;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700214
Xiaozhe Shie118c692012-09-24 15:17:43 -0700215 uint16_t ocv_reading_at_100;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700216 uint16_t prev_last_good_ocv_raw;
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700217 int insertion_ocv_uv;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700218 int last_ocv_uv;
Xiaozhe Shicc48e992013-05-28 16:42:24 -0700219 int charging_adjusted_ocv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800220 int last_ocv_temp;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700221 int last_cc_uah;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700222 unsigned long last_soc_change_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700223 unsigned long tm_sec;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700224 unsigned long report_tm_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700225 bool first_time_calc_soc;
226 bool first_time_calc_uuc;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700227 int64_t software_cc_uah;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700228 int64_t software_shdw_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700229
230 int iavg_samples_ma[IAVG_SAMPLES];
231 int iavg_index;
232 int iavg_num_samples;
233 struct timespec t_soc_queried;
234 int last_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -0700235 int last_soc_est;
Xiaozhe Shicc137262013-03-10 06:21:41 -0700236 int last_soc_unbound;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700237 bool was_charging_at_sleep;
238 int charge_start_tm_sec;
239 int catch_up_time_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700240 struct single_row_lut *adjusted_fcc_temp_lut;
241
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700242 struct qpnp_adc_tm_btm_param vbat_monitor_params;
Xiaozhe Shi535494d2013-04-05 12:27:51 -0700243 struct qpnp_adc_tm_btm_param die_temp_monitor_params;
244 int temperature_margin;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700245 unsigned int vadc_v0625;
246 unsigned int vadc_v1250;
247
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -0700248 int system_load_count;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700249 int prev_uuc_iavg_ma;
250 int prev_pc_unusable;
251 int ibat_at_cv_ua;
252 int soc_at_cv;
253 int prev_chg_soc;
254 int calculated_soc;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -0800255 int prev_voltage_based_soc;
256 bool use_voltage_soc;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700257 bool in_cv_range;
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800258
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800259 int prev_batt_terminal_uv;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -0700260 int high_ocv_correction_limit_uv;
261 int low_ocv_correction_limit_uv;
262 int flat_ocv_threshold_uv;
263 int hold_soc_est;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800264
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800265 int ocv_high_threshold_uv;
266 int ocv_low_threshold_uv;
Xiaozhe Shicdeee312012-12-18 15:10:18 -0800267 unsigned long last_recalc_time;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530268
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530269 struct fcc_sample *fcc_learning_samples;
270 u8 fcc_sample_count;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530271 int enable_fcc_learning;
272 int min_fcc_learning_soc;
273 int min_fcc_ocv_pc;
274 int min_fcc_learning_samples;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530275 int start_soc;
276 int end_soc;
277 int start_pc;
278 int start_cc_uah;
279 int start_real_soc;
280 int end_cc_uah;
281 uint16_t fcc_new_mah;
282 int fcc_new_batt_temp;
283 uint16_t charge_cycles;
284 u8 charge_increase;
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530285 int fcc_resolution;
286 bool battery_removed;
Zhenhua Huang95a05d32014-03-31 18:09:45 +0800287 bool in_taper_charge;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800288 struct bms_irq sw_cc_thr_irq;
289 struct bms_irq ocv_thr_irq;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700290 struct qpnp_vadc_chip *vadc_dev;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700291 struct qpnp_iadc_chip *iadc_dev;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -0700292 struct qpnp_adc_tm_chip *adc_tm_dev;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700293};
294
295static struct of_device_id qpnp_bms_match_table[] = {
296 { .compatible = QPNP_BMS_DEV_NAME },
297 {}
298};
299
300static char *qpnp_bms_supplicants[] = {
301 "battery"
302};
303
304static enum power_supply_property msm_bms_power_props[] = {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700305 POWER_SUPPLY_PROP_CAPACITY,
Xiaozhe Shibda84992013-09-05 10:39:11 -0700306 POWER_SUPPLY_PROP_STATUS,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700307 POWER_SUPPLY_PROP_CURRENT_NOW,
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -0700308 POWER_SUPPLY_PROP_RESISTANCE,
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -0700309 POWER_SUPPLY_PROP_CHARGE_COUNTER,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700310 POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700311 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +0530312 POWER_SUPPLY_PROP_CHARGE_FULL,
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +0530313 POWER_SUPPLY_PROP_CYCLE_COUNT,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700314};
wangxlee3614a2015-02-03 13:13:11 +0800315#ifdef CONFIG_TCMD
316static struct qpnp_bms_chip *bms_chip;
317#endif
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700318
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530319static int discard_backup_fcc_data(struct qpnp_bms_chip *chip);
320static void backup_charge_cycle(struct qpnp_bms_chip *chip);
321
Xiaozhe Shi20640b52013-01-03 11:49:30 -0800322static bool bms_reset;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -0800323
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700324static int qpnp_read_wrapper(struct qpnp_bms_chip *chip, u8 *val,
325 u16 base, int count)
326{
327 int rc;
328 struct spmi_device *spmi = chip->spmi;
329
330 rc = spmi_ext_register_readl(spmi->ctrl, spmi->sid, base, val, count);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700331 if (rc) {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700332 pr_err("SPMI read failed rc=%d\n", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700333 return rc;
334 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700335 return 0;
336}
337
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700338static int qpnp_write_wrapper(struct qpnp_bms_chip *chip, u8 *val,
339 u16 base, int count)
340{
341 int rc;
342 struct spmi_device *spmi = chip->spmi;
343
344 rc = spmi_ext_register_writel(spmi->ctrl, spmi->sid, base, val, count);
345 if (rc) {
346 pr_err("SPMI write failed rc=%d\n", rc);
347 return rc;
348 }
349 return 0;
350}
351
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800352static int qpnp_masked_write_base(struct qpnp_bms_chip *chip, u16 addr,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700353 u8 mask, u8 val)
354{
355 int rc;
356 u8 reg;
357
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800358 rc = qpnp_read_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700359 if (rc) {
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800360 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700361 return rc;
362 }
363 reg &= ~mask;
364 reg |= val & mask;
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800365 rc = qpnp_write_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700366 if (rc) {
367 pr_err("write failed addr = %03X, val = %02x, mask = %02x, reg = %02x, rc = %d\n",
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800368 addr, val, mask, reg, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700369 return rc;
370 }
371 return 0;
372}
373
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800374static int qpnp_masked_write_iadc(struct qpnp_bms_chip *chip, u16 addr,
375 u8 mask, u8 val)
376{
377 return qpnp_masked_write_base(chip, chip->iadc_base + addr, mask, val);
378}
379
380static int qpnp_masked_write(struct qpnp_bms_chip *chip, u16 addr,
381 u8 mask, u8 val)
382{
383 return qpnp_masked_write_base(chip, chip->base + addr, mask, val);
384}
385
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800386static void bms_stay_awake(struct bms_wakeup_source *source)
387{
388 if (__test_and_clear_bit(0, &source->disabled)) {
389 __pm_stay_awake(&source->source);
390 pr_debug("enabled source %s\n", source->source.name);
391 }
392}
393
394static void bms_relax(struct bms_wakeup_source *source)
395{
396 if (!__test_and_set_bit(0, &source->disabled)) {
397 __pm_relax(&source->source);
398 pr_debug("disabled source %s\n", source->source.name);
399 }
400}
401
402static void enable_bms_irq(struct bms_irq *irq)
403{
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800404 if (irq->ready && __test_and_clear_bit(0, &irq->disabled)) {
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800405 enable_irq(irq->irq);
406 pr_debug("enabled irq %d\n", irq->irq);
Fenglin Wuc17e97b2015-01-20 16:49:58 +0800407 if ((irq->is_wake) &&
408 !__test_and_set_bit(0, &irq->wake_enabled))
409 enable_irq_wake(irq->irq);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800410 }
411}
412
413static void disable_bms_irq(struct bms_irq *irq)
414{
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800415 if (irq->ready && !__test_and_set_bit(0, &irq->disabled)) {
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800416 disable_irq(irq->irq);
417 pr_debug("disabled irq %d\n", irq->irq);
Fenglin Wuc17e97b2015-01-20 16:49:58 +0800418 if ((irq->is_wake) &&
419 __test_and_clear_bit(0, &irq->wake_enabled))
420 disable_irq_wake(irq->irq);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800421 }
422}
423
Anirudh Ghayal1166eef2013-12-23 19:05:33 +0530424static void disable_bms_irq_nosync(struct bms_irq *irq)
425{
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800426 if (irq->ready && !__test_and_set_bit(0, &irq->disabled)) {
Anirudh Ghayal1166eef2013-12-23 19:05:33 +0530427 disable_irq_nosync(irq->irq);
428 pr_debug("disabled irq %d\n", irq->irq);
Fenglin Wuc17e97b2015-01-20 16:49:58 +0800429 if ((irq->is_wake) &&
430 __test_and_clear_bit(0, &irq->wake_enabled))
431 disable_irq_wake(irq->irq);
Anirudh Ghayal1166eef2013-12-23 19:05:33 +0530432 }
433}
434
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700435#define HOLD_OREG_DATA BIT(0)
436static int lock_output_data(struct qpnp_bms_chip *chip)
437{
438 int rc;
439
440 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL,
441 HOLD_OREG_DATA, HOLD_OREG_DATA);
442 if (rc) {
443 pr_err("couldnt lock bms output rc = %d\n", rc);
444 return rc;
445 }
Xiaozhe Shied4a5522014-09-05 14:56:13 -0700446 /*
447 * Sleep for at least 60 microseconds here to make sure there has
448 * been at least two cycles of the sleep clock so that the registers
449 * are correctly locked.
450 */
451 usleep_range(60, 2000);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700452 return 0;
453}
454
455static int unlock_output_data(struct qpnp_bms_chip *chip)
456{
457 int rc;
458
459 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL, HOLD_OREG_DATA, 0);
460 if (rc) {
461 pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc);
462 return rc;
463 }
464 return 0;
465}
466
467#define V_PER_BIT_MUL_FACTOR 97656
468#define V_PER_BIT_DIV_FACTOR 1000
469#define VADC_INTRINSIC_OFFSET 0x6000
470
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800471static int vadc_reading_to_uv(int reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700472{
473 if (reading <= VADC_INTRINSIC_OFFSET)
474 return 0;
475
476 return (reading - VADC_INTRINSIC_OFFSET)
477 * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR;
478}
479
480#define VADC_CALIB_UV 625000
481#define VBATT_MUL_FACTOR 3
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800482static int adjust_vbatt_reading(struct qpnp_bms_chip *chip, int reading_uv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700483{
484 s64 numerator, denominator;
485
486 if (reading_uv == 0)
487 return 0;
488
489 /* don't adjust if not calibrated */
490 if (chip->vadc_v0625 == 0 || chip->vadc_v1250 == 0) {
491 pr_debug("No cal yet return %d\n",
492 VBATT_MUL_FACTOR * reading_uv);
493 return VBATT_MUL_FACTOR * reading_uv;
494 }
495
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700496 numerator = ((s64)reading_uv - chip->vadc_v0625) * VADC_CALIB_UV;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700497 denominator = (s64)chip->vadc_v1250 - chip->vadc_v0625;
498 if (denominator == 0)
499 return reading_uv * VBATT_MUL_FACTOR;
500 return (VADC_CALIB_UV + div_s64(numerator, denominator))
501 * VBATT_MUL_FACTOR;
502}
503
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800504static int convert_vbatt_uv_to_raw(struct qpnp_bms_chip *chip,
505 int unadjusted_vbatt)
506{
507 int scaled_vbatt = unadjusted_vbatt / VBATT_MUL_FACTOR;
508
509 if (scaled_vbatt <= 0)
510 return VADC_INTRINSIC_OFFSET;
511 return ((scaled_vbatt * V_PER_BIT_DIV_FACTOR) / V_PER_BIT_MUL_FACTOR)
512 + VADC_INTRINSIC_OFFSET;
513}
514
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700515static inline int convert_vbatt_raw_to_uv(struct qpnp_bms_chip *chip,
Xiaozhe Shi80754222013-10-30 14:11:41 -0700516 uint16_t reading, bool is_pon_ocv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700517{
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700518 int64_t uv;
519 int rc;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700520
521 uv = vadc_reading_to_uv(reading);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700522 pr_debug("%u raw converted into %lld uv\n", reading, uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700523 uv = adjust_vbatt_reading(chip, uv);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700524 pr_debug("adjusted into %lld uv\n", uv);
Xiaozhe Shi80754222013-10-30 14:11:41 -0700525 rc = qpnp_vbat_sns_comp_result(chip->vadc_dev, &uv, is_pon_ocv);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700526 if (rc)
527 pr_debug("could not compensate vbatt\n");
528 pr_debug("compensated into %lld uv\n", uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700529 return uv;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700530}
531
532#define CC_READING_RESOLUTION_N 542535
533#define CC_READING_RESOLUTION_D 100000
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -0700534static s64 cc_reading_to_uv(s64 reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700535{
536 return div_s64(reading * CC_READING_RESOLUTION_N,
537 CC_READING_RESOLUTION_D);
538}
539
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800540#define QPNP_ADC_GAIN_IDEAL 3291LL
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700541static s64 cc_adjust_for_gain(s64 uv, uint16_t gain)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700542{
543 s64 result_uv;
544
545 pr_debug("adjusting_uv = %lld\n", uv);
Xiaozhe Shi820a47a2012-11-27 13:23:27 -0800546 if (gain == 0) {
547 pr_debug("gain is %d, not adjusting\n", gain);
548 return uv;
549 }
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700550 pr_debug("adjusting by factor: %lld/%hu = %lld%%\n",
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800551 QPNP_ADC_GAIN_IDEAL, gain,
552 div_s64(QPNP_ADC_GAIN_IDEAL * 100LL, (s64)gain));
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700553
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800554 result_uv = div_s64(uv * QPNP_ADC_GAIN_IDEAL, (s64)gain);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700555 pr_debug("result_uv = %lld\n", result_uv);
556 return result_uv;
557}
558
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700559static s64 cc_reverse_adjust_for_gain(struct qpnp_bms_chip *chip, s64 uv)
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800560{
561 struct qpnp_iadc_calib calibration;
562 int gain;
563 s64 result_uv;
564
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700565 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800566 gain = (int)calibration.gain_raw - (int)calibration.offset_raw;
567
568 pr_debug("reverse adjusting_uv = %lld\n", uv);
569 if (gain == 0) {
570 pr_debug("gain is %d, not adjusting\n", gain);
571 return uv;
572 }
573 pr_debug("adjusting by factor: %hu/%lld = %lld%%\n",
574 gain, QPNP_ADC_GAIN_IDEAL,
575 div64_s64((s64)gain * 100LL,
576 (s64)QPNP_ADC_GAIN_IDEAL));
577
578 result_uv = div64_s64(uv * (s64)gain, QPNP_ADC_GAIN_IDEAL);
579 pr_debug("result_uv = %lld\n", result_uv);
580 return result_uv;
581}
582
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700583static int convert_vsense_to_uv(struct qpnp_bms_chip *chip,
584 int16_t reading)
585{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700586 struct qpnp_iadc_calib calibration;
587
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700588 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700589 return cc_adjust_for_gain(cc_reading_to_uv(reading),
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800590 calibration.gain_raw - calibration.offset_raw);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700591}
592
593static int read_vsense_avg(struct qpnp_bms_chip *chip, int *result_uv)
594{
595 int rc;
596 int16_t reading;
597
598 rc = qpnp_read_wrapper(chip, (u8 *)&reading,
599 chip->base + BMS1_VSENSE_AVG_DATA0, 2);
600
601 if (rc) {
602 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
603 return rc;
604 }
605
606 *result_uv = convert_vsense_to_uv(chip, reading);
607 return 0;
608}
609
610static int get_battery_current(struct qpnp_bms_chip *chip, int *result_ua)
611{
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700612 int rc, vsense_uv = 0;
613 int64_t temp_current;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700614
Xiaozhe Shid0a79542012-11-06 10:00:38 -0800615 if (chip->r_sense_uohm == 0) {
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700616 pr_err("r_sense is zero\n");
617 return -EINVAL;
618 }
619
620 mutex_lock(&chip->bms_output_lock);
621 lock_output_data(chip);
622 read_vsense_avg(chip, &vsense_uv);
623 unlock_output_data(chip);
624 mutex_unlock(&chip->bms_output_lock);
625
626 pr_debug("vsense_uv=%duV\n", vsense_uv);
627 /* cast for signed division */
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700628 temp_current = div_s64((vsense_uv * 1000000LL),
629 (int)chip->r_sense_uohm);
630
Xiaozhe Shi4c8458a2013-11-26 13:00:56 -0800631 *result_ua = temp_current;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700632 rc = qpnp_iadc_comp_result(chip->iadc_dev, &temp_current);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700633 if (rc)
634 pr_debug("error compensation failed: %d\n", rc);
635
Xiaozhe Shi4c8458a2013-11-26 13:00:56 -0800636 pr_debug("%d uA err compensated ibat=%llduA\n",
637 *result_ua, temp_current);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700638 *result_ua = temp_current;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700639 return 0;
640}
wangxlee3614a2015-02-03 13:13:11 +0800641#ifdef CONFIG_TCMD
642int tcmd_get_battery_current(int *result_ua)
643{
644 int result_ibat_ua;
645 get_battery_current(bms_chip,&result_ibat_ua);
646 *result_ua = -1 * result_ibat_ua;
647 pr_debug("ibat=%duA\n", *result_ua);
648 return 0;
649}
650#endif
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700651static int get_battery_voltage(struct qpnp_bms_chip *chip, int *result_uv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700652{
653 int rc;
654 struct qpnp_vadc_result adc_result;
655
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700656 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &adc_result);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700657 if (rc) {
658 pr_err("error reading adc channel = %d, rc = %d\n",
659 VBAT_SNS, rc);
660 return rc;
661 }
662 pr_debug("mvolts phy = %lld meas = 0x%llx\n", adc_result.physical,
663 adc_result.measurement);
664 *result_uv = (int)adc_result.physical;
665 return 0;
666}
667
Xiaozhe Shie118c692012-09-24 15:17:43 -0700668#define CC_36_BIT_MASK 0xFFFFFFFFFLL
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800669static uint64_t convert_s64_to_s36(int64_t raw64)
670{
671 return (uint64_t) raw64 & CC_36_BIT_MASK;
672}
673
674#define SIGN_EXTEND_36_TO_64_MASK (-1LL ^ CC_36_BIT_MASK)
675static int64_t convert_s36_to_s64(uint64_t raw36)
676{
677 raw36 = raw36 & CC_36_BIT_MASK;
678 /* convert 36 bit signed value into 64 signed value */
679 return (raw36 >> 35) == 0LL ?
680 raw36 : (SIGN_EXTEND_36_TO_64_MASK | raw36);
681}
682
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700683static int read_cc_raw(struct qpnp_bms_chip *chip, int64_t *reading,
684 int cc_type)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700685{
686 int64_t raw_reading;
687 int rc;
688
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700689 if (cc_type == SHDW_CC)
690 rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
691 chip->base + BMS1_SW_CC_DATA0, 5);
692 else
693 rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
694 chip->base + BMS1_CC_DATA0, 5);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700695 if (rc) {
696 pr_err("Error reading cc: rc = %d\n", rc);
697 return -ENXIO;
698 }
699
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800700 *reading = convert_s36_to_s64(raw_reading);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700701
702 return 0;
703}
704
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700705static int calib_vadc(struct qpnp_bms_chip *chip)
706{
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700707 int rc, raw_0625, raw_1250;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700708 struct qpnp_vadc_result result;
709
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700710 rc = qpnp_vadc_read(chip->vadc_dev, REF_625MV, &result);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700711 if (rc) {
712 pr_debug("vadc read failed with rc = %d\n", rc);
713 return rc;
714 }
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700715 raw_0625 = result.adc_code;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700716
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700717 rc = qpnp_vadc_read(chip->vadc_dev, REF_125V, &result);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700718 if (rc) {
719 pr_debug("vadc read failed with rc = %d\n", rc);
720 return rc;
721 }
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700722 raw_1250 = result.adc_code;
723 chip->vadc_v0625 = vadc_reading_to_uv(raw_0625);
724 chip->vadc_v1250 = vadc_reading_to_uv(raw_1250);
725 pr_debug("vadc calib: 0625 = %d raw (%d uv), 1250 = %d raw (%d uv)\n",
726 raw_0625, chip->vadc_v0625,
727 raw_1250, chip->vadc_v1250);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700728 return 0;
729}
730
Xiaozhe Shie118c692012-09-24 15:17:43 -0700731static void convert_and_store_ocv(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800732 struct raw_soc_params *raw,
Xiaozhe Shi80754222013-10-30 14:11:41 -0700733 int batt_temp, bool is_pon_ocv)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700734{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700735 int rc;
736
737 pr_debug("prev_last_good_ocv_raw = %d, last_good_ocv_raw = %d\n",
738 chip->prev_last_good_ocv_raw,
739 raw->last_good_ocv_raw);
740 rc = calib_vadc(chip);
741 if (rc)
742 pr_err("Vadc reference voltage read failed, rc = %d\n", rc);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700743 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
744 raw->last_good_ocv_uv = convert_vbatt_raw_to_uv(chip,
Xiaozhe Shi80754222013-10-30 14:11:41 -0700745 raw->last_good_ocv_raw, is_pon_ocv);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700746 chip->last_ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800747 chip->last_ocv_temp = batt_temp;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700748 chip->software_cc_uah = 0;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700749 pr_debug("last_good_ocv_uv = %d\n", raw->last_good_ocv_uv);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700750}
751
Xiaozhe Shia045a562012-11-28 16:55:39 -0800752#define CLEAR_CC BIT(7)
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700753#define CLEAR_SHDW_CC BIT(6)
Xiaozhe Shia045a562012-11-28 16:55:39 -0800754/**
755 * reset both cc and sw-cc.
756 * note: this should only be ever called from one thread
757 * or there may be a race condition where CC is never enabled
758 * again
759 */
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700760static void reset_cc(struct qpnp_bms_chip *chip, u8 flags)
Xiaozhe Shia045a562012-11-28 16:55:39 -0800761{
762 int rc;
763
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700764 pr_debug("resetting cc manually with flags %hhu\n", flags);
765 mutex_lock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800766 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700767 flags,
768 flags);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800769 if (rc)
770 pr_err("cc reset failed: %d\n", rc);
771
772 /* wait for 100us for cc to reset */
773 udelay(100);
774
775 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700776 flags, 0);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800777 if (rc)
778 pr_err("cc reenable failed: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700779 mutex_unlock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800780}
781
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700782static int get_battery_status(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800783{
784 union power_supply_propval ret = {0,};
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800785 int rc;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800786
787 if (chip->batt_psy == NULL)
788 chip->batt_psy = power_supply_get_by_name("battery");
789 if (chip->batt_psy) {
790 /* if battery has been registered, use the status property */
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800791 rc = chip->batt_psy->get_property(chip->batt_psy,
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800792 POWER_SUPPLY_PROP_STATUS, &ret);
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800793 if (rc) {
794 pr_debug("Battery does not export status: %d\n", rc);
795 return POWER_SUPPLY_STATUS_UNKNOWN;
796 }
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700797 return ret.intval;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800798 }
799
800 /* Default to false if the battery power supply is not registered. */
801 pr_debug("battery power supply is not registered\n");
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700802 return POWER_SUPPLY_STATUS_UNKNOWN;
803}
804
Zhenhua Huang95a05d32014-03-31 18:09:45 +0800805static int get_battery_charge_type(struct qpnp_bms_chip *chip)
806{
807 union power_supply_propval ret = {0,};
808 int rc;
809
810 if (chip->batt_psy == NULL)
811 chip->batt_psy = power_supply_get_by_name("battery");
812 if (chip->batt_psy) {
813 /* if battery has been registered, use the type property */
814 rc = chip->batt_psy->get_property(chip->batt_psy,
815 POWER_SUPPLY_PROP_CHARGE_TYPE, &ret);
816 if (rc) {
817 pr_debug("Battery does not export charge type: %d\n"
818 , rc);
819 return POWER_SUPPLY_CHARGE_TYPE_NONE;
820 }
821 return ret.intval;
822 }
823
824 /* Default to false if the battery power supply is not registered. */
825 pr_debug("battery power supply is not registered\n");
826 return POWER_SUPPLY_CHARGE_TYPE_NONE;
827}
828
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700829static bool is_battery_charging(struct qpnp_bms_chip *chip)
830{
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800831 return get_battery_status(chip) == POWER_SUPPLY_STATUS_CHARGING;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800832}
833
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700834static bool is_battery_full(struct qpnp_bms_chip *chip)
835{
836 return get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL;
837}
838
Xiaozhe Shi4515c762013-11-13 16:36:54 -0800839#define BAT_PRES_BIT BIT(7)
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700840static bool is_battery_present(struct qpnp_bms_chip *chip)
841{
842 union power_supply_propval ret = {0,};
Xiaozhe Shi4515c762013-11-13 16:36:54 -0800843 int rc;
844 u8 batt_pres;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700845
Xiaozhe Shi4515c762013-11-13 16:36:54 -0800846 /* first try to use the batt_pres register if given */
847 if (chip->batt_pres_addr) {
848 rc = qpnp_read_wrapper(chip, &batt_pres,
849 chip->batt_pres_addr, 1);
850 if (!rc && (batt_pres & BAT_PRES_BIT))
851 return true;
852 else
853 return false;
854 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700855 if (chip->batt_psy == NULL)
856 chip->batt_psy = power_supply_get_by_name("battery");
857 if (chip->batt_psy) {
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700858 /* if battery has been registered, use the present property */
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800859 rc = chip->batt_psy->get_property(chip->batt_psy,
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700860 POWER_SUPPLY_PROP_PRESENT, &ret);
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800861 if (rc) {
862 pr_debug("battery does not export present: %d\n", rc);
863 return true;
864 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700865 return ret.intval;
866 }
867
868 /* Default to false if the battery power supply is not registered. */
869 pr_debug("battery power supply is not registered\n");
870 return false;
871}
872
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700873static int get_battery_insertion_ocv_uv(struct qpnp_bms_chip *chip)
874{
875 union power_supply_propval ret = {0,};
876 int rc, vbat;
877
878 if (chip->batt_psy == NULL)
879 chip->batt_psy = power_supply_get_by_name("battery");
880 if (chip->batt_psy) {
881 /* if battery has been registered, use the ocv property */
882 rc = chip->batt_psy->get_property(chip->batt_psy,
883 POWER_SUPPLY_PROP_VOLTAGE_OCV, &ret);
884 if (rc) {
885 /*
886 * Default to vbatt if the battery OCV is not
887 * registered.
888 */
889 pr_debug("Battery psy does not have voltage ocv\n");
890 rc = get_battery_voltage(chip, &vbat);
891 if (rc)
892 return -EINVAL;
893 return vbat;
894 }
895 return ret.intval;
896 }
897
898 pr_debug("battery power supply is not registered\n");
899 return -EINVAL;
900}
901
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700902static bool is_batfet_closed(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800903{
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700904 union power_supply_propval ret = {0,};
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800905 int rc;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700906
907 if (chip->batt_psy == NULL)
908 chip->batt_psy = power_supply_get_by_name("battery");
909 if (chip->batt_psy) {
910 /* if battery has been registered, use the online property */
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800911 rc = chip->batt_psy->get_property(chip->batt_psy,
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700912 POWER_SUPPLY_PROP_ONLINE, &ret);
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800913 if (rc) {
914 pr_debug("Battery does not export online: %d\n", rc);
915 return true;
916 }
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700917 return !!ret.intval;
918 }
919
920 /* Default to true if the battery power supply is not registered. */
921 pr_debug("battery power supply is not registered\n");
922 return true;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800923}
924
925static int get_simultaneous_batt_v_and_i(struct qpnp_bms_chip *chip,
926 int *ibat_ua, int *vbat_uv)
927{
928 struct qpnp_iadc_result i_result;
929 struct qpnp_vadc_result v_result;
930 enum qpnp_iadc_channels iadc_channel;
931 int rc;
932
933 iadc_channel = chip->use_external_rsense ?
934 EXTERNAL_RSENSE : INTERNAL_RSENSE;
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700935 if (is_battery_full(chip)) {
936 rc = get_battery_current(chip, ibat_ua);
937 if (rc) {
938 pr_err("bms current read failed with rc: %d\n", rc);
939 return rc;
940 }
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700941 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &v_result);
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700942 if (rc) {
943 pr_err("vadc read failed with rc: %d\n", rc);
944 return rc;
945 }
946 *vbat_uv = (int)v_result.physical;
947 } else {
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700948 rc = qpnp_iadc_vadc_sync_read(chip->iadc_dev,
949 iadc_channel, &i_result,
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700950 VBAT_SNS, &v_result);
951 if (rc) {
952 pr_err("adc sync read failed with rc: %d\n", rc);
953 return rc;
954 }
955 /*
956 * reverse the current read by the iadc, since the bms uses
957 * flipped battery current polarity.
958 */
959 *ibat_ua = -1 * (int)i_result.result_ua;
960 *vbat_uv = (int)v_result.physical;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800961 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800962
963 return 0;
964}
965
Xiaozhe Shi3ede2ad2014-02-04 13:25:38 -0800966static int get_rbatt(struct qpnp_bms_chip *chip,
967 int soc_rbatt_mohm, int batt_temp)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800968{
Xiaozhe Shi3ede2ad2014-02-04 13:25:38 -0800969 int rbatt_mohm, scalefactor;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800970
Xiaozhe Shi3ede2ad2014-02-04 13:25:38 -0800971 rbatt_mohm = chip->default_rbatt_mohm;
972 if (chip->rbatt_sf_lut == NULL) {
973 pr_debug("RBATT = %d\n", rbatt_mohm);
974 return rbatt_mohm;
975 }
976 /* Convert the batt_temp to DegC from deciDegC */
977 scalefactor = interpolate_scalingfactor(chip->rbatt_sf_lut,
978 batt_temp, soc_rbatt_mohm);
979 rbatt_mohm = (rbatt_mohm * scalefactor) / 100;
980
981 rbatt_mohm += chip->r_conn_mohm;
982 rbatt_mohm += chip->rbatt_capacitive_mohm;
983 return rbatt_mohm;
984}
985
986#define DEFAULT_RBATT_SOC 50
987static int estimate_ocv(struct qpnp_bms_chip *chip, int batt_temp)
988{
989 int ibat_ua, vbat_uv, ocv_est_uv, rbatt_mohm, rc;
990
991 rbatt_mohm = get_rbatt(chip, DEFAULT_RBATT_SOC, batt_temp);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800992 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
993 if (rc) {
994 pr_err("simultaneous failed rc = %d\n", rc);
995 return rc;
996 }
997
998 ocv_est_uv = vbat_uv + (ibat_ua * rbatt_mohm) / 1000;
Xiaozhe Shi3ede2ad2014-02-04 13:25:38 -0800999 pr_debug("estimated pon ocv = %d, vbat_uv = %d ibat_ua = %d rbatt_mohm = %d\n",
1000 ocv_est_uv, vbat_uv, ibat_ua, rbatt_mohm);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001001 return ocv_est_uv;
1002}
1003
Xiaozhe Shi2c171172013-12-03 13:27:37 -08001004#define MIN_IAVG_MA 250
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001005static void reset_for_new_battery(struct qpnp_bms_chip *chip, int batt_temp)
1006{
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07001007 chip->last_ocv_uv = chip->insertion_ocv_uv;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001008 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001009 chip->last_soc = -EINVAL;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001010 chip->last_soc_invalid = true;
1011 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001012 chip->soc_at_cv = -EINVAL;
1013 chip->shutdown_soc_invalid = true;
1014 chip->shutdown_soc = 0;
Xiaozhe Shi2c171172013-12-03 13:27:37 -08001015 chip->shutdown_iavg_ma = MIN_IAVG_MA;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001016 chip->prev_pc_unusable = -EINVAL;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001017 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001018 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001019 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001020 chip->last_cc_uah = INT_MIN;
1021 chip->last_ocv_temp = batt_temp;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001022 chip->prev_batt_terminal_uv = 0;
Anirudh Ghayale0c02932013-07-08 16:26:35 +05301023 if (chip->enable_fcc_learning) {
1024 chip->adjusted_fcc_temp_lut = NULL;
1025 chip->fcc_new_mah = -EINVAL;
1026 /* reset the charge-cycle and charge-increase registers */
1027 chip->charge_increase = 0;
1028 chip->charge_cycles = 0;
1029 backup_charge_cycle(chip);
1030 /* discard all the FCC learnt data and reset the local table */
1031 discard_backup_fcc_data(chip);
1032 memset(chip->fcc_learning_samples, 0,
1033 chip->min_fcc_learning_samples *
1034 sizeof(struct fcc_sample));
1035 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001036}
1037
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001038#define SIGN(x) ((x) < 0 ? -1 : 1)
1039#define UV_PER_SPIN 50000
1040static int find_ocv_for_pc(struct qpnp_bms_chip *chip, int batt_temp, int pc)
1041{
1042 int new_pc;
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001043 int ocv_mv;
1044 int delta_mv = 5;
1045 int max_spin_count;
1046 int count = 0;
1047 int sign, new_sign;
1048
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08001049 ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut, batt_temp, pc);
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001050
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08001051 new_pc = interpolate_pc(chip->pc_temp_ocv_lut, batt_temp, ocv_mv);
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001052 pr_debug("test revlookup pc = %d for ocv = %d\n", new_pc, ocv_mv);
1053 max_spin_count = 1 + (chip->max_voltage_uv - chip->v_cutoff_uv)
1054 / UV_PER_SPIN;
1055 sign = SIGN(pc - new_pc);
1056
1057 while (abs(new_pc - pc) != 0 && count < max_spin_count) {
1058 /*
1059 * If the newly interpolated pc is larger than the lookup pc,
1060 * the ocv should be reduced and vice versa
1061 */
1062 new_sign = SIGN(pc - new_pc);
1063 /*
1064 * If the sign has changed, then we have passed the lookup pc.
1065 * reduce the ocv step size to get finer results.
1066 *
1067 * If we have already reduced the ocv step size and still
1068 * passed the lookup pc, just stop and use the current ocv.
1069 * This can only happen if the batterydata profile is
1070 * non-monotonic anyways.
1071 */
1072 if (new_sign != sign) {
1073 if (delta_mv > 1)
1074 delta_mv = 1;
1075 else
1076 break;
1077 }
1078 sign = new_sign;
1079
1080 ocv_mv = ocv_mv + delta_mv * sign;
1081 new_pc = interpolate_pc(chip->pc_temp_ocv_lut,
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08001082 batt_temp, ocv_mv);
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001083 pr_debug("test revlookup pc = %d for ocv = %d\n",
1084 new_pc, ocv_mv);
1085 count++;
1086 }
1087
1088 return ocv_mv * 1000;
1089}
1090
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08001091#define OCV_RAW_UNINITIALIZED 0xFFFF
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001092#define MIN_OCV_UV 2000000
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001093static int read_soc_params_raw(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001094 struct raw_soc_params *raw,
1095 int batt_temp)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001096{
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001097 int warm_reset, rc;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001098
1099 mutex_lock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -08001100
Xiaozhe Shie118c692012-09-24 15:17:43 -07001101 lock_output_data(chip);
1102
1103 rc = qpnp_read_wrapper(chip, (u8 *)&raw->last_good_ocv_raw,
1104 chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
1105 if (rc) {
1106 pr_err("Error reading ocv: rc = %d\n", rc);
Xiaozhe Shiabbd6072013-12-11 14:24:02 -08001107 goto param_err;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001108 }
1109
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001110 rc = read_cc_raw(chip, &raw->cc, CC);
Xiaozhe Shied4a5522014-09-05 14:56:13 -07001111 rc |= read_cc_raw(chip, &raw->shdw_cc, SHDW_CC);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001112 if (rc) {
1113 pr_err("Failed to read raw cc data, rc = %d\n", rc);
Xiaozhe Shiabbd6072013-12-11 14:24:02 -08001114 goto param_err;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001115 }
1116
1117 unlock_output_data(chip);
1118 mutex_unlock(&chip->bms_output_lock);
1119
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08001120 if (chip->prev_last_good_ocv_raw == OCV_RAW_UNINITIALIZED) {
Xiaozhe Shi80754222013-10-30 14:11:41 -07001121 convert_and_store_ocv(chip, raw, batt_temp, true);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001122 pr_debug("PON_OCV_UV = %d, cc = %llx\n",
1123 chip->last_ocv_uv, raw->cc);
1124 warm_reset = qpnp_pon_is_warm_reset();
Xiaozhe Shi3ede2ad2014-02-04 13:25:38 -08001125 if (raw->last_good_ocv_uv < MIN_OCV_UV || warm_reset > 0) {
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001126 pr_debug("OCV is stale or bad, estimating new OCV.\n");
Xiaozhe Shi3ede2ad2014-02-04 13:25:38 -08001127 chip->last_ocv_uv = estimate_ocv(chip, batt_temp);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001128 raw->last_good_ocv_uv = chip->last_ocv_uv;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001129 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001130 pr_debug("New PON_OCV_UV = %d, cc = %llx\n",
1131 chip->last_ocv_uv, raw->cc);
1132 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001133 } else if (chip->new_battery) {
1134 /* if a new battery was inserted, estimate the ocv */
1135 reset_for_new_battery(chip, batt_temp);
1136 raw->cc = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001137 raw->shdw_cc = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001138 raw->last_good_ocv_uv = chip->last_ocv_uv;
1139 chip->new_battery = false;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001140 } else if (chip->done_charging) {
1141 chip->done_charging = false;
1142 /* if we just finished charging, reset CC and fake 100% */
1143 chip->ocv_reading_at_100 = raw->last_good_ocv_raw;
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001144 chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp, 100);
1145 raw->last_good_ocv_uv = chip->last_ocv_uv;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001146 raw->cc = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001147 raw->shdw_cc = 0;
1148 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001149 chip->last_ocv_temp = batt_temp;
1150 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001151 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001152 chip->last_cc_uah = INT_MIN;
1153 pr_debug("EOC Battery full ocv_reading = 0x%x\n",
1154 chip->ocv_reading_at_100);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001155 } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) {
Xiaozhe Shi80754222013-10-30 14:11:41 -07001156 convert_and_store_ocv(chip, raw, batt_temp, false);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001157 /* forget the old cc value upon ocv */
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001158 chip->last_cc_uah = INT_MIN;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001159 } else {
1160 raw->last_good_ocv_uv = chip->last_ocv_uv;
1161 }
1162
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001163 /* stop faking a high OCV if we get a new OCV */
1164 if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw)
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08001165 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001166
Xiaozhe Shie118c692012-09-24 15:17:43 -07001167 pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n",
1168 raw->last_good_ocv_raw, raw->last_good_ocv_uv);
1169 pr_debug("cc_raw= 0x%llx\n", raw->cc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001170 return 0;
Xiaozhe Shiabbd6072013-12-11 14:24:02 -08001171
1172param_err:
1173 unlock_output_data(chip);
1174 mutex_unlock(&chip->bms_output_lock);
1175 return rc;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001176}
1177
Xiaozhe Shie118c692012-09-24 15:17:43 -07001178static int calculate_pc(struct qpnp_bms_chip *chip, int ocv_uv,
1179 int batt_temp)
1180{
1181 int pc;
1182
1183 pc = interpolate_pc(chip->pc_temp_ocv_lut,
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08001184 batt_temp, ocv_uv / 1000);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001185 pr_debug("pc = %u %% for ocv = %d uv batt_temp = %d\n",
1186 pc, ocv_uv, batt_temp);
1187 /* Multiply the initial FCC value by the scale factor. */
1188 return pc;
1189}
1190
1191static int calculate_fcc(struct qpnp_bms_chip *chip, int batt_temp)
1192{
1193 int fcc_uah;
1194
1195 if (chip->adjusted_fcc_temp_lut == NULL) {
1196 /* interpolate_fcc returns a mv value. */
1197 fcc_uah = interpolate_fcc(chip->fcc_temp_lut,
1198 batt_temp) * 1000;
1199 pr_debug("fcc = %d uAh\n", fcc_uah);
1200 return fcc_uah;
1201 } else {
1202 return 1000 * interpolate_fcc(chip->adjusted_fcc_temp_lut,
1203 batt_temp);
1204 }
1205}
1206
1207/* calculate remaining charge at the time of ocv */
1208static int calculate_ocv_charge(struct qpnp_bms_chip *chip,
1209 struct raw_soc_params *raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001210 int fcc_uah)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001211{
1212 int ocv_uv, pc;
1213
1214 ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001215 pc = calculate_pc(chip, ocv_uv, chip->last_ocv_temp);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001216 pr_debug("ocv_uv = %d pc = %d\n", ocv_uv, pc);
1217 return (fcc_uah * pc) / 100;
1218}
1219
Xiaozhe Shie118c692012-09-24 15:17:43 -07001220#define CC_READING_TICKS 56
1221#define SLEEP_CLK_HZ 32764
1222#define SECONDS_PER_HOUR 3600
1223
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001224static s64 cc_uv_to_pvh(s64 cc_uv)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001225{
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001226 /* Note that it is necessary need to multiply by 1000000 to convert
1227 * from uvh to pvh here.
1228 * However, the maximum Coulomb Counter value is 2^35, which can cause
1229 * an over flow.
1230 * Multiply by 100000 first to perserve as much precision as possible
1231 * then multiply by 10 after doing the division in order to avoid
1232 * overflow on the maximum Coulomb Counter value.
1233 */
1234 return div_s64(cc_uv * CC_READING_TICKS * 100000,
1235 SLEEP_CLK_HZ * SECONDS_PER_HOUR) * 10;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001236}
1237
1238/**
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001239 * calculate_cc() - converts a hardware coulomb counter reading into uah
Xiaozhe Shie118c692012-09-24 15:17:43 -07001240 * @chip: the bms chip pointer
1241 * @cc: the cc reading from bms h/w
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001242 * @cc_type: calcualte cc from regular or shadow coulomb counter
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001243 * @clear_cc: whether this function should clear the hardware counter
1244 * after reading
Xiaozhe Shie118c692012-09-24 15:17:43 -07001245 *
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001246 * Converts the 64 bit hardware coulomb counter into microamp-hour by taking
1247 * into account hardware resolution and adc errors.
1248 *
1249 * Return: the coulomb counter based charge in uAh (micro-amp hour)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001250 */
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001251static int calculate_cc(struct qpnp_bms_chip *chip, int64_t cc,
1252 int cc_type, int clear_cc)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001253{
Xiaozhe Shi4e376652012-10-25 12:38:50 -07001254 struct qpnp_iadc_calib calibration;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001255 struct qpnp_vadc_result result;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001256 int64_t cc_voltage_uv, cc_pvh, cc_uah, *software_counter;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001257 int rc;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001258
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001259 software_counter = cc_type == SHDW_CC ?
1260 &chip->software_shdw_cc_uah : &chip->software_cc_uah;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001261 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001262 if (rc) {
1263 pr_err("could not read pmic die temperature: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001264 return *software_counter;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001265 }
1266
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07001267 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001268 pr_debug("%scc = %lld, die_temp = %lld\n",
1269 cc_type == SHDW_CC ? "shdw_" : "",
1270 cc, result.physical);
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -07001271 cc_voltage_uv = cc_reading_to_uv(cc);
Xiaozhe Shi0c484932013-02-05 16:14:10 -08001272 cc_voltage_uv = cc_adjust_for_gain(cc_voltage_uv,
1273 calibration.gain_raw
1274 - calibration.offset_raw);
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001275 cc_pvh = cc_uv_to_pvh(cc_voltage_uv);
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001276 cc_uah = div_s64(cc_pvh, chip->r_sense_uohm);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07001277 rc = qpnp_iadc_comp_result(chip->iadc_dev, &cc_uah);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001278 if (rc)
1279 pr_debug("error compensation failed: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001280 if (clear_cc == RESET) {
1281 pr_debug("software_%scc = %lld, added cc_uah = %lld\n",
1282 cc_type == SHDW_CC ? "sw_" : "",
1283 *software_counter, cc_uah);
1284 *software_counter += cc_uah;
1285 reset_cc(chip, cc_type == SHDW_CC ? CLEAR_SHDW_CC : CLEAR_CC);
1286 return (int)*software_counter;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001287 } else {
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001288 pr_debug("software_%scc = %lld, cc_uah = %lld, total = %lld\n",
1289 cc_type == SHDW_CC ? "shdw_" : "",
1290 *software_counter, cc_uah,
1291 *software_counter + cc_uah);
1292 return *software_counter + cc_uah;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001293 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07001294}
1295
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -07001296#define IAVG_MINIMAL_TIME 2
Xiaozhe Shie118c692012-09-24 15:17:43 -07001297static void calculate_iavg(struct qpnp_bms_chip *chip, int cc_uah,
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001298 int *iavg_ua, int delta_time_s)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001299{
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001300 int delta_cc_uah = 0;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001301
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -07001302 /*
1303 * use the battery current if called too quickly
1304 */
1305 if (delta_time_s < IAVG_MINIMAL_TIME
1306 || chip->last_cc_uah == INT_MIN) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001307 get_battery_current(chip, iavg_ua);
1308 goto out;
1309 }
1310
Xiaozhe Shie118c692012-09-24 15:17:43 -07001311 delta_cc_uah = cc_uah - chip->last_cc_uah;
1312
1313 *iavg_ua = div_s64((s64)delta_cc_uah * 3600, delta_time_s);
1314
Xiaozhe Shie118c692012-09-24 15:17:43 -07001315out:
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001316 pr_debug("delta_cc = %d iavg_ua = %d\n", delta_cc_uah, (int)*iavg_ua);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001317
1318 /* remember cc_uah */
1319 chip->last_cc_uah = cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001320}
1321
1322static int calculate_termination_uuc(struct qpnp_bms_chip *chip,
1323 struct soc_params *params,
1324 int batt_temp, int uuc_iavg_ma,
1325 int *ret_pc_unusable)
1326{
1327 int unusable_uv, pc_unusable, uuc_uah;
1328 int i = 0;
1329 int ocv_mv;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001330 int rbatt_mohm;
1331 int delta_uv;
1332 int prev_delta_uv = 0;
1333 int prev_rbatt_mohm = 0;
1334 int uuc_rbatt_mohm;
1335
1336 for (i = 0; i <= 100; i++) {
1337 ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08001338 batt_temp, i);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001339 rbatt_mohm = get_rbatt(chip, i, batt_temp);
1340 unusable_uv = (rbatt_mohm * uuc_iavg_ma)
1341 + (chip->v_cutoff_uv);
1342 delta_uv = ocv_mv * 1000 - unusable_uv;
1343
Xiaozhe Shie118c692012-09-24 15:17:43 -07001344 if (delta_uv > 0)
1345 break;
1346
1347 prev_delta_uv = delta_uv;
1348 prev_rbatt_mohm = rbatt_mohm;
1349 }
1350
1351 uuc_rbatt_mohm = linear_interpolate(rbatt_mohm, delta_uv,
1352 prev_rbatt_mohm, prev_delta_uv,
1353 0);
1354
1355 unusable_uv = (uuc_rbatt_mohm * uuc_iavg_ma) + (chip->v_cutoff_uv);
1356
1357 pc_unusable = calculate_pc(chip, unusable_uv, batt_temp);
1358 uuc_uah = (params->fcc_uah * pc_unusable) / 100;
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001359 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 -07001360 uuc_iavg_ma,
1361 uuc_rbatt_mohm, unusable_uv,
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001362 pc_unusable, i, uuc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001363 *ret_pc_unusable = pc_unusable;
1364 return uuc_uah;
1365}
1366
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001367#define TIME_PER_PERCENT_UUC 60
Xiaozhe Shie118c692012-09-24 15:17:43 -07001368static int adjust_uuc(struct qpnp_bms_chip *chip,
1369 struct soc_params *params,
1370 int new_pc_unusable,
1371 int new_uuc_uah,
1372 int batt_temp)
1373{
1374 int new_unusable_mv, new_iavg_ma;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001375 int max_percent_change;
1376
1377 max_percent_change = max(params->delta_time_s
1378 / TIME_PER_PERCENT_UUC, 1);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001379
Xiaozhe Shi2c171172013-12-03 13:27:37 -08001380 if (chip->first_time_calc_uuc || chip->prev_pc_unusable == -EINVAL
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001381 || abs(chip->prev_pc_unusable - new_pc_unusable)
1382 <= max_percent_change) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001383 chip->prev_pc_unusable = new_pc_unusable;
1384 return new_uuc_uah;
1385 }
1386
1387 /* the uuc is trying to change more than 1% restrict it */
1388 if (new_pc_unusable > chip->prev_pc_unusable)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001389 chip->prev_pc_unusable += max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001390 else
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001391 chip->prev_pc_unusable -= max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001392
1393 new_uuc_uah = (params->fcc_uah * chip->prev_pc_unusable) / 100;
1394
1395 /* also find update the iavg_ma accordingly */
1396 new_unusable_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08001397 batt_temp, chip->prev_pc_unusable);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001398 if (new_unusable_mv < chip->v_cutoff_uv/1000)
1399 new_unusable_mv = chip->v_cutoff_uv/1000;
1400
1401 new_iavg_ma = (new_unusable_mv * 1000 - chip->v_cutoff_uv)
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001402 / params->rbatt_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001403 if (new_iavg_ma == 0)
1404 new_iavg_ma = 1;
1405 chip->prev_uuc_iavg_ma = new_iavg_ma;
1406 pr_debug("Restricting UUC to %d (%d%%) unusable_mv = %d iavg_ma = %d\n",
1407 new_uuc_uah, chip->prev_pc_unusable,
1408 new_unusable_mv, new_iavg_ma);
1409
1410 return new_uuc_uah;
1411}
1412
Xiaozhe Shie118c692012-09-24 15:17:43 -07001413static int calculate_unusable_charge_uah(struct qpnp_bms_chip *chip,
1414 struct soc_params *params,
1415 int batt_temp)
1416{
1417 int uuc_uah_iavg;
1418 int i;
1419 int uuc_iavg_ma = params->iavg_ua / 1000;
1420 int pc_unusable;
1421
1422 /*
1423 * if called first time, fill all the samples with
1424 * the shutdown_iavg_ma
1425 */
1426 if (chip->first_time_calc_uuc && chip->shutdown_iavg_ma != 0) {
1427 pr_debug("Using shutdown_iavg_ma = %d in all samples\n",
1428 chip->shutdown_iavg_ma);
1429 for (i = 0; i < IAVG_SAMPLES; i++)
1430 chip->iavg_samples_ma[i] = chip->shutdown_iavg_ma;
1431
1432 chip->iavg_index = 0;
1433 chip->iavg_num_samples = IAVG_SAMPLES;
1434 }
1435
Xiaozhe Shi70633922013-09-23 15:50:53 -07001436 if (params->delta_time_s >= IAVG_MINIMAL_TIME) {
1437 /*
1438 * if charging use a nominal avg current to keep
1439 * a reasonable UUC while charging
1440 */
1441 if (uuc_iavg_ma < MIN_IAVG_MA)
1442 uuc_iavg_ma = MIN_IAVG_MA;
1443 chip->iavg_samples_ma[chip->iavg_index] = uuc_iavg_ma;
1444 chip->iavg_index = (chip->iavg_index + 1) % IAVG_SAMPLES;
1445 chip->iavg_num_samples++;
1446 if (chip->iavg_num_samples >= IAVG_SAMPLES)
1447 chip->iavg_num_samples = IAVG_SAMPLES;
1448 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07001449
1450 /* now that this sample is added calcualte the average */
1451 uuc_iavg_ma = 0;
1452 if (chip->iavg_num_samples != 0) {
1453 for (i = 0; i < chip->iavg_num_samples; i++) {
1454 pr_debug("iavg_samples_ma[%d] = %d\n", i,
1455 chip->iavg_samples_ma[i]);
1456 uuc_iavg_ma += chip->iavg_samples_ma[i];
1457 }
1458
1459 uuc_iavg_ma = DIV_ROUND_CLOSEST(uuc_iavg_ma,
1460 chip->iavg_num_samples);
1461 }
1462
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001463 /*
1464 * if we're in bms reset mode, force uuc to be 3% of fcc
1465 */
1466 if (bms_reset)
1467 return (params->fcc_uah * 3) / 100;
1468
Xiaozhe Shi75e5efe2013-02-07 09:51:43 -08001469 uuc_uah_iavg = calculate_termination_uuc(chip, params, batt_temp,
1470 uuc_iavg_ma, &pc_unusable);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001471 pr_debug("uuc_iavg_ma = %d uuc with iavg = %d\n",
1472 uuc_iavg_ma, uuc_uah_iavg);
1473
1474 chip->prev_uuc_iavg_ma = uuc_iavg_ma;
1475 /* restrict the uuc such that it can increase only by one percent */
1476 uuc_uah_iavg = adjust_uuc(chip, params, pc_unusable,
1477 uuc_uah_iavg, batt_temp);
1478
Xiaozhe Shie118c692012-09-24 15:17:43 -07001479 return uuc_uah_iavg;
1480}
1481
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001482static s64 find_ocv_charge_for_soc(struct qpnp_bms_chip *chip,
1483 struct soc_params *params, int soc)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001484{
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001485 return div_s64((s64)soc * (params->fcc_uah - params->uuc_uah),
1486 100) + params->cc_uah + params->uuc_uah;
1487}
Xiaozhe Shie118c692012-09-24 15:17:43 -07001488
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001489static int find_pc_for_soc(struct qpnp_bms_chip *chip,
1490 struct soc_params *params, int soc)
1491{
1492 int ocv_charge_uah = find_ocv_charge_for_soc(chip, params, soc);
1493 int pc;
1494
Xiaozhe Shie118c692012-09-24 15:17:43 -07001495 pc = DIV_ROUND_CLOSEST((int)ocv_charge_uah * 100, params->fcc_uah);
1496 pc = clamp(pc, 0, 100);
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001497 pr_debug("soc = %d, fcc = %d uuc = %d rc = %d pc = %d\n",
1498 soc, params->fcc_uah, params->uuc_uah,
1499 ocv_charge_uah, pc);
1500 return pc;
1501}
Xiaozhe Shie118c692012-09-24 15:17:43 -07001502
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001503static int get_current_time(unsigned long *now_tm_sec)
1504{
1505 struct rtc_time tm;
1506 struct rtc_device *rtc;
1507 int rc;
1508
1509 rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
1510 if (rtc == NULL) {
1511 pr_err("%s: unable to open rtc device (%s)\n",
1512 __FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
Xiaozhe Shi0e01af62013-05-06 12:56:08 -07001513 return -EINVAL;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001514 }
1515
1516 rc = rtc_read_time(rtc, &tm);
1517 if (rc) {
1518 pr_err("Error reading rtc device (%s) : %d\n",
1519 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1520 goto close_time;
1521 }
1522
1523 rc = rtc_valid_tm(&tm);
1524 if (rc) {
1525 pr_err("Invalid RTC time (%s): %d\n",
1526 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1527 goto close_time;
1528 }
1529 rtc_tm_to_time(&tm, now_tm_sec);
1530
1531close_time:
1532 rtc_class_close(rtc);
1533 return rc;
1534}
1535
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08001536/* Returns estimated battery resistance */
1537static int get_prop_bms_batt_resistance(struct qpnp_bms_chip *chip)
1538{
1539 return chip->rbatt_mohm * 1000;
1540}
1541
1542/* Returns instantaneous current in uA */
1543static int get_prop_bms_current_now(struct qpnp_bms_chip *chip)
1544{
1545 int rc, result_ua;
1546
1547 rc = get_battery_current(chip, &result_ua);
1548 if (rc) {
1549 pr_err("failed to get current: %d\n", rc);
1550 return rc;
1551 }
1552 return result_ua;
1553}
1554
1555/* Returns coulomb counter in uAh */
1556static int get_prop_bms_charge_counter(struct qpnp_bms_chip *chip)
1557{
1558 int64_t cc_raw;
1559
1560 mutex_lock(&chip->bms_output_lock);
1561 lock_output_data(chip);
Xiaozhe Shie11c9492013-12-11 14:37:36 -08001562 read_cc_raw(chip, &cc_raw, CC);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08001563 unlock_output_data(chip);
1564 mutex_unlock(&chip->bms_output_lock);
1565
1566 return calculate_cc(chip, cc_raw, CC, NORESET);
1567}
1568
1569/* Returns shadow coulomb counter in uAh */
1570static int get_prop_bms_charge_counter_shadow(struct qpnp_bms_chip *chip)
1571{
1572 int64_t cc_raw;
1573
1574 mutex_lock(&chip->bms_output_lock);
1575 lock_output_data(chip);
Xiaozhe Shie11c9492013-12-11 14:37:36 -08001576 read_cc_raw(chip, &cc_raw, SHDW_CC);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08001577 unlock_output_data(chip);
1578 mutex_unlock(&chip->bms_output_lock);
1579
1580 return calculate_cc(chip, cc_raw, SHDW_CC, NORESET);
1581}
1582
1583/* Returns full charge design in uAh */
1584static int get_prop_bms_charge_full_design(struct qpnp_bms_chip *chip)
1585{
1586 return chip->fcc_mah * 1000;
1587}
1588
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05301589/* Returns the current full charge in uAh */
1590static int get_prop_bms_charge_full(struct qpnp_bms_chip *chip)
1591{
1592 int rc;
1593 struct qpnp_vadc_result result;
1594
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001595 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05301596 if (rc) {
1597 pr_err("Unable to read battery temperature\n");
1598 return rc;
1599 }
1600
1601 return calculate_fcc(chip, (int)result.physical);
1602}
1603
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001604static int calculate_delta_time(unsigned long *time_stamp, int *delta_time_s)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001605{
1606 unsigned long now_tm_sec = 0;
1607
1608 /* default to delta time = 0 if anything fails */
1609 *delta_time_s = 0;
1610
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001611 if (get_current_time(&now_tm_sec)) {
1612 pr_err("RTC read failed\n");
1613 return 0;
1614 }
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001615
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001616 *delta_time_s = (now_tm_sec - *time_stamp);
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001617
1618 /* remember this time */
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001619 *time_stamp = now_tm_sec;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001620 return 0;
1621}
1622
Xiaozhe Shie118c692012-09-24 15:17:43 -07001623static void calculate_soc_params(struct qpnp_bms_chip *chip,
1624 struct raw_soc_params *raw,
1625 struct soc_params *params,
1626 int batt_temp)
1627{
Xiaozhe Shi219cb222013-06-10 15:49:59 -07001628 int soc_rbatt, shdw_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001629
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001630 calculate_delta_time(&chip->tm_sec, &params->delta_time_s);
1631 pr_debug("tm_sec = %ld, delta_s = %d\n",
1632 chip->tm_sec, params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001633 params->fcc_uah = calculate_fcc(chip, batt_temp);
1634 pr_debug("FCC = %uuAh batt_temp = %d\n", params->fcc_uah, batt_temp);
1635
1636 /* calculate remainging charge */
1637 params->ocv_charge_uah = calculate_ocv_charge(
1638 chip, raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001639 params->fcc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001640 pr_debug("ocv_charge_uah = %uuAh\n", params->ocv_charge_uah);
1641
1642 /* calculate cc micro_volt_hour */
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001643 params->cc_uah = calculate_cc(chip, raw->cc, CC, RESET);
Xiaozhe Shi219cb222013-06-10 15:49:59 -07001644 shdw_cc_uah = calculate_cc(chip, raw->shdw_cc, SHDW_CC, RESET);
1645 pr_debug("cc_uah = %duAh raw->cc = %llx, shdw_cc_uah = %duAh raw->shdw_cc = %llx\n",
1646 params->cc_uah, raw->cc,
1647 shdw_cc_uah, raw->shdw_cc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001648
1649 soc_rbatt = ((params->ocv_charge_uah - params->cc_uah) * 100)
1650 / params->fcc_uah;
1651 if (soc_rbatt < 0)
1652 soc_rbatt = 0;
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001653 params->rbatt_mohm = get_rbatt(chip, soc_rbatt, batt_temp);
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001654 pr_debug("rbatt_mohm = %d\n", params->rbatt_mohm);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001655
Xiaozhe Shi1e87cda2013-05-17 10:18:56 -07001656 if (params->rbatt_mohm != chip->rbatt_mohm) {
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07001657 chip->rbatt_mohm = params->rbatt_mohm;
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07001658 if (chip->bms_psy_registered)
Xiaozhe Shi1e87cda2013-05-17 10:18:56 -07001659 power_supply_changed(&chip->bms_psy);
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07001660 }
1661
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001662 calculate_iavg(chip, params->cc_uah, &params->iavg_ua,
1663 params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001664
1665 params->uuc_uah = calculate_unusable_charge_uah(chip, params,
1666 batt_temp);
1667 pr_debug("UUC = %uuAh\n", params->uuc_uah);
1668}
1669
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001670static int bound_soc(int soc)
1671{
1672 soc = max(0, soc);
1673 soc = min(100, soc);
1674 return soc;
1675}
1676
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001677#define IBAT_TOL_MASK 0x0F
1678#define OCV_TOL_MASK 0xF0
1679#define IBAT_TOL_DEFAULT 0x03
1680#define IBAT_TOL_NOCHG 0x0F
1681#define OCV_TOL_DEFAULT 0x20
1682#define OCV_TOL_NO_OCV 0x00
1683static int stop_ocv_updates(struct qpnp_bms_chip *chip)
1684{
1685 pr_debug("stopping ocv updates\n");
1686 return qpnp_masked_write(chip, BMS1_TOL_CTL,
1687 OCV_TOL_MASK, OCV_TOL_NO_OCV);
1688}
1689
1690static int reset_bms_for_test(struct qpnp_bms_chip *chip)
1691{
Xiaozhe Shi95da77f2013-02-20 13:40:06 -08001692 int ibat_ua = 0, vbat_uv = 0, rc;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001693 int ocv_est_uv;
1694
1695 if (!chip) {
1696 pr_err("BMS driver has not been initialized yet!\n");
1697 return -EINVAL;
1698 }
1699
1700 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
1701
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07001702 /*
1703 * Don't include rbatt and rbatt_capacitative since we expect this to
1704 * be used with a fake battery which does not have internal resistances
1705 */
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001706 ocv_est_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
1707 pr_debug("forcing ocv to be %d due to bms reset mode\n", ocv_est_uv);
1708 chip->last_ocv_uv = ocv_est_uv;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001709 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001710 chip->last_soc = -EINVAL;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001711 chip->last_soc_invalid = true;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001712 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001713 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001714 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001715 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001716 chip->last_cc_uah = INT_MIN;
1717 stop_ocv_updates(chip);
1718
1719 pr_debug("bms reset to ocv = %duv vbat_ua = %d ibat_ua = %d\n",
1720 chip->last_ocv_uv, vbat_uv, ibat_ua);
1721
1722 return rc;
1723}
1724
1725static int bms_reset_set(const char *val, const struct kernel_param *kp)
1726{
1727 int rc;
1728
1729 rc = param_set_bool(val, kp);
1730 if (rc) {
1731 pr_err("Unable to set bms_reset: %d\n", rc);
1732 return rc;
1733 }
1734
1735 if (*(bool *)kp->arg) {
1736 struct power_supply *bms_psy = power_supply_get_by_name("bms");
1737 struct qpnp_bms_chip *chip = container_of(bms_psy,
1738 struct qpnp_bms_chip, bms_psy);
1739
1740 rc = reset_bms_for_test(chip);
1741 if (rc) {
1742 pr_err("Unable to modify bms_reset: %d\n", rc);
1743 return rc;
1744 }
1745 }
1746 return 0;
1747}
1748
1749static struct kernel_param_ops bms_reset_ops = {
1750 .set = bms_reset_set,
1751 .get = param_get_bool,
1752};
1753
1754module_param_cb(bms_reset, &bms_reset_ops, &bms_reset, 0644);
1755
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001756#define SOC_STORAGE_MASK 0xFE
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001757static void backup_soc_and_iavg(struct qpnp_bms_chip *chip, int batt_temp,
1758 int soc)
1759{
1760 u8 temp;
1761 int rc;
1762 int iavg_ma = chip->prev_uuc_iavg_ma;
1763
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07001764 if (iavg_ma > MIN_IAVG_MA)
1765 temp = (iavg_ma - MIN_IAVG_MA) / IAVG_STEP_SIZE_MA;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001766 else
1767 temp = 0;
1768
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001769 rc = qpnp_write_wrapper(chip, &temp, chip->base + IAVG_STORAGE_REG, 1);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001770
Xiaozhe Shie945a8a2013-11-11 10:20:14 -08001771 /* store an invalid soc if temperature is below 5degC */
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001772 if (batt_temp > IGNORE_SOC_TEMP_DECIDEG)
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001773 qpnp_masked_write_base(chip, chip->soc_storage_addr,
1774 SOC_STORAGE_MASK, (soc + 1) << 1);
Xiaozhe Shie945a8a2013-11-11 10:20:14 -08001775 else
1776 qpnp_masked_write_base(chip, chip->soc_storage_addr,
1777 SOC_STORAGE_MASK, SOC_STORAGE_MASK);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001778}
1779
1780static int scale_soc_while_chg(struct qpnp_bms_chip *chip, int chg_time_sec,
1781 int catch_up_sec, int new_soc, int prev_soc)
1782{
1783 int scaled_soc;
1784 int numerator;
1785
1786 /*
1787 * Don't report a high value immediately slowly scale the
1788 * value from prev_soc to the new soc based on a charge time
1789 * weighted average
1790 */
1791 pr_debug("cts = %d catch_up_sec = %d\n", chg_time_sec, catch_up_sec);
1792 if (catch_up_sec == 0)
1793 return new_soc;
1794
1795 if (chg_time_sec > catch_up_sec)
1796 return new_soc;
1797
1798 numerator = (catch_up_sec - chg_time_sec) * prev_soc
1799 + chg_time_sec * new_soc;
1800 scaled_soc = numerator / catch_up_sec;
1801
1802 pr_debug("cts = %d new_soc = %d prev_soc = %d scaled_soc = %d\n",
1803 chg_time_sec, new_soc, prev_soc, scaled_soc);
1804
1805 return scaled_soc;
1806}
1807
1808/*
1809 * bms_fake_battery is set in setups where a battery emulator is used instead
1810 * of a real battery. This makes the bms driver report a different/fake value
1811 * regardless of the calculated state of charge.
1812 */
1813static int bms_fake_battery = -EINVAL;
1814module_param(bms_fake_battery, int, 0644);
1815
1816static int report_voltage_based_soc(struct qpnp_bms_chip *chip)
1817{
1818 pr_debug("Reported voltage based soc = %d\n",
1819 chip->prev_voltage_based_soc);
1820 return chip->prev_voltage_based_soc;
1821}
1822
1823#define SOC_CATCHUP_SEC_MAX 600
1824#define SOC_CATCHUP_SEC_PER_PERCENT 60
1825#define MAX_CATCHUP_SOC (SOC_CATCHUP_SEC_MAX / SOC_CATCHUP_SEC_PER_PERCENT)
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07001826#define SOC_CHANGE_PER_SEC 5
Xiaozhe Shi27375822013-08-22 11:40:15 -07001827#define REPORT_SOC_WAIT_MS 10000
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001828static int report_cc_based_soc(struct qpnp_bms_chip *chip)
1829{
1830 int soc, soc_change;
1831 int time_since_last_change_sec, charge_time_sec = 0;
1832 unsigned long last_change_sec;
1833 struct timespec now;
1834 struct qpnp_vadc_result result;
1835 int batt_temp;
1836 int rc;
1837 bool charging, charging_since_last_report;
1838
Xiaozhe Shi27375822013-08-22 11:40:15 -07001839 rc = wait_event_interruptible_timeout(chip->bms_wait_queue,
1840 chip->calculated_soc != -EINVAL,
1841 round_jiffies_relative(msecs_to_jiffies
1842 (REPORT_SOC_WAIT_MS)));
1843
1844 if (rc == 0 && chip->calculated_soc == -EINVAL) {
1845 pr_debug("calculate soc timed out\n");
1846 } else if (rc == -ERESTARTSYS) {
1847 pr_err("Wait for SoC interrupted.\n");
1848 return rc;
1849 }
1850
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001851 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001852
1853 if (rc) {
1854 pr_err("error reading adc channel = %d, rc = %d\n",
1855 LR_MUX1_BATT_THERM, rc);
1856 return rc;
1857 }
1858 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
1859 result.measurement);
1860 batt_temp = (int)result.physical;
1861
1862 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shifa6ea692013-05-31 11:15:13 -07001863 soc = chip->calculated_soc;
1864
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001865 last_change_sec = chip->last_soc_change_sec;
1866 calculate_delta_time(&last_change_sec, &time_since_last_change_sec);
1867
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -08001868 charging = chip->battery_status == POWER_SUPPLY_STATUS_CHARGING;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001869 charging_since_last_report = charging || (chip->last_soc_unbound
1870 && chip->was_charging_at_sleep);
1871 /*
1872 * account for charge time - limit it to SOC_CATCHUP_SEC to
1873 * avoid overflows when charging continues for extended periods
1874 */
1875 if (charging) {
1876 if (chip->charge_start_tm_sec == 0) {
1877 /*
1878 * calculating soc for the first time
1879 * after start of chg. Initialize catchup time
1880 */
1881 if (abs(soc - chip->last_soc) < MAX_CATCHUP_SOC)
1882 chip->catch_up_time_sec =
1883 (soc - chip->last_soc)
1884 * SOC_CATCHUP_SEC_PER_PERCENT;
1885 else
1886 chip->catch_up_time_sec = SOC_CATCHUP_SEC_MAX;
1887
1888 if (chip->catch_up_time_sec < 0)
1889 chip->catch_up_time_sec = 0;
1890 chip->charge_start_tm_sec = last_change_sec;
1891 }
1892
1893 charge_time_sec = min(SOC_CATCHUP_SEC_MAX, (int)last_change_sec
1894 - chip->charge_start_tm_sec);
1895
1896 /* end catchup if calculated soc and last soc are same */
1897 if (chip->last_soc == soc)
1898 chip->catch_up_time_sec = 0;
1899 }
1900
1901 if (chip->last_soc != -EINVAL) {
1902 /*
1903 * last_soc < soc ... if we have not been charging at all
1904 * since the last time this was called, report previous SoC.
1905 * Otherwise, scale and catch up.
1906 */
1907 if (chip->last_soc < soc && !charging_since_last_report)
1908 soc = chip->last_soc;
1909 else if (chip->last_soc < soc && soc != 100)
1910 soc = scale_soc_while_chg(chip, charge_time_sec,
1911 chip->catch_up_time_sec,
1912 soc, chip->last_soc);
1913
Xiaozhe Shibd56b052013-10-21 11:51:30 -07001914 /* if the battery is close to cutoff allow more change */
1915 if (wake_lock_active(&chip->low_voltage_wake_lock))
1916 soc_change = min((int)abs(chip->last_soc - soc),
1917 time_since_last_change_sec);
1918 else
1919 soc_change = min((int)abs(chip->last_soc - soc),
1920 time_since_last_change_sec
1921 / SOC_CHANGE_PER_SEC);
1922
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001923 if (chip->last_soc_unbound) {
1924 chip->last_soc_unbound = false;
1925 } else {
1926 /*
1927 * if soc have not been unbound by resume,
1928 * only change reported SoC by 1.
1929 */
1930 soc_change = min(1, soc_change);
1931 }
1932
1933 if (soc < chip->last_soc && soc != 0)
1934 soc = chip->last_soc - soc_change;
1935 if (soc > chip->last_soc && soc != 100)
1936 soc = chip->last_soc + soc_change;
1937 }
1938
Xiaozhe Shi208b8e52013-05-28 10:16:32 -07001939 if (chip->last_soc != soc && !chip->last_soc_unbound)
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001940 chip->last_soc_change_sec = last_change_sec;
1941
1942 pr_debug("last_soc = %d, calculated_soc = %d, soc = %d, time since last change = %d\n",
1943 chip->last_soc, chip->calculated_soc,
1944 soc, time_since_last_change_sec);
1945 chip->last_soc = bound_soc(soc);
1946 backup_soc_and_iavg(chip, batt_temp, chip->last_soc);
1947 pr_debug("Reported SOC = %d\n", chip->last_soc);
1948 chip->t_soc_queried = now;
1949 mutex_unlock(&chip->last_soc_mutex);
1950
1951 return soc;
1952}
1953
1954static int report_state_of_charge(struct qpnp_bms_chip *chip)
1955{
1956 if (bms_fake_battery != -EINVAL) {
1957 pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery);
1958 return bms_fake_battery;
1959 } else if (chip->use_voltage_soc)
1960 return report_voltage_based_soc(chip);
1961 else
1962 return report_cc_based_soc(chip);
1963}
1964
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07001965#define VDD_MAX_ERR 5000
1966#define VDD_STEP_SIZE 10000
1967#define MAX_COUNT_BEFORE_RESET_TO_CC 3
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001968static int charging_adjustments(struct qpnp_bms_chip *chip,
1969 struct soc_params *params, int soc,
1970 int vbat_uv, int ibat_ua, int batt_temp)
1971{
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001972 int chg_soc, soc_ibat, batt_terminal_uv, weight_ibat, weight_cc;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001973
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001974 batt_terminal_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001975
1976 if (chip->soc_at_cv == -EINVAL) {
Zhenhua Huang95a05d32014-03-31 18:09:45 +08001977 if (batt_terminal_uv >= chip->max_voltage_uv - VDD_MAX_ERR ||
1978 chip->in_taper_charge) {
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001979 chip->soc_at_cv = soc;
1980 chip->prev_chg_soc = soc;
Xiaozhe Shifc7af172013-11-04 14:15:44 -08001981 chip->ibat_at_cv_ua = params->iavg_ua;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001982 pr_debug("CC_TO_CV ibat_ua = %d CHG SOC %d\n",
1983 ibat_ua, soc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001984 } else {
1985 /* In constant current charging return the calc soc */
1986 pr_debug("CC CHG SOC %d\n", soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001987 }
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001988
1989 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07001990 chip->system_load_count = 0;
1991 return soc;
1992 } else if (ibat_ua > 0 && batt_terminal_uv
1993 < chip->max_voltage_uv - (VDD_MAX_ERR * 2)) {
1994 if (chip->system_load_count > MAX_COUNT_BEFORE_RESET_TO_CC) {
1995 chip->soc_at_cv = -EINVAL;
1996 pr_debug("Vbat below CV threshold, resetting CC_TO_CV\n");
1997 chip->system_load_count = 0;
1998 } else {
1999 chip->system_load_count += 1;
2000 pr_debug("Vbat below CV threshold, count: %d\n",
2001 chip->system_load_count);
2002 }
2003 return soc;
2004 } else if (ibat_ua > 0) {
2005 pr_debug("NOT CHARGING SOC %d\n", soc);
2006 chip->system_load_count = 0;
2007 chip->prev_chg_soc = soc;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002008 return soc;
2009 }
2010
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07002011 chip->system_load_count = 0;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002012 /*
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08002013 * battery is in CV phase - begin linear interpolation of soc based on
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002014 * battery charge current
2015 */
2016
2017 /*
2018 * if voltage lessened (possibly because of a system load)
2019 * keep reporting the prev chg soc
2020 */
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07002021 if (batt_terminal_uv <= chip->prev_batt_terminal_uv - VDD_STEP_SIZE) {
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08002022 pr_debug("batt_terminal_uv %d < (max = %d - 10000); CC CHG SOC %d\n",
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08002023 batt_terminal_uv, chip->prev_batt_terminal_uv,
2024 chip->prev_chg_soc);
2025 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002026 return chip->prev_chg_soc;
2027 }
2028
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07002029 soc_ibat = bound_soc(linear_interpolate(chip->soc_at_cv,
2030 chip->ibat_at_cv_ua,
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08002031 100, -1 * chip->chg_term_ua,
Xiaozhe Shifc7af172013-11-04 14:15:44 -08002032 params->iavg_ua));
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07002033 weight_ibat = bound_soc(linear_interpolate(1, chip->soc_at_cv,
2034 100, 100, chip->prev_chg_soc));
2035 weight_cc = 100 - weight_ibat;
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07002036 chg_soc = bound_soc(DIV_ROUND_CLOSEST(soc_ibat * weight_ibat
2037 + weight_cc * soc, 100));
2038
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07002039 pr_debug("weight_ibat = %d, weight_cc = %d, soc_ibat = %d, soc_cc = %d\n",
2040 weight_ibat, weight_cc, soc_ibat, soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002041
2042 /* always report a higher soc */
2043 if (chg_soc > chip->prev_chg_soc) {
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002044 chip->prev_chg_soc = chg_soc;
2045
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002046 chip->charging_adjusted_ocv = find_ocv_for_pc(chip, batt_temp,
2047 find_pc_for_soc(chip, params, chg_soc));
2048 pr_debug("CC CHG ADJ OCV = %d CHG SOC %d\n",
2049 chip->charging_adjusted_ocv,
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002050 chip->prev_chg_soc);
2051 }
2052
2053 pr_debug("Reporting CHG SOC %d\n", chip->prev_chg_soc);
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08002054 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002055 return chip->prev_chg_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002056}
2057
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002058static void very_low_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
2059{
2060 /*
2061 * if battery is very low (v_cutoff voltage + 20mv) hold
2062 * a wakelock untill soc = 0%
2063 */
2064 if (vbat_uv <= chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002065 && !wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002066 pr_debug("voltage = %d low holding wakelock\n", vbat_uv);
2067 wake_lock(&chip->low_voltage_wake_lock);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002068 } else if (vbat_uv > chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002069 && wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002070 pr_debug("voltage = %d releasing wakelock\n", vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002071 wake_unlock(&chip->low_voltage_wake_lock);
2072 }
2073}
2074
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002075#define VBATT_ERROR_MARGIN 20000
2076static void cv_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
2077{
2078 /*
2079 * if battery is very low (v_cutoff voltage + 20mv) hold
2080 * a wakelock untill soc = 0%
2081 */
2082 if (wake_lock_active(&chip->cv_wake_lock)) {
2083 if (chip->soc_at_cv != -EINVAL) {
2084 pr_debug("hit CV, releasing cv wakelock\n");
2085 wake_unlock(&chip->cv_wake_lock);
2086 } else if (!is_battery_charging(chip)) {
2087 pr_debug("charging stopped, releasing cv wakelock\n");
2088 wake_unlock(&chip->cv_wake_lock);
2089 }
2090 } else if (vbat_uv > chip->max_voltage_uv - VBATT_ERROR_MARGIN
2091 && chip->soc_at_cv == -EINVAL
2092 && is_battery_charging(chip)
2093 && !wake_lock_active(&chip->cv_wake_lock)) {
2094 pr_debug("voltage = %d holding cv wakelock\n", vbat_uv);
2095 wake_lock(&chip->cv_wake_lock);
2096 }
2097}
2098
Xiaozhe Shi2b647872013-10-31 14:30:27 -07002099#define NO_ADJUST_HIGH_SOC_THRESHOLD 98
Xiaozhe Shie118c692012-09-24 15:17:43 -07002100static int adjust_soc(struct qpnp_bms_chip *chip, struct soc_params *params,
2101 int soc, int batt_temp)
2102{
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002103 int ibat_ua = 0, vbat_uv = 0;
2104 int ocv_est_uv = 0, soc_est = 0, pc_est = 0, pc = 0;
2105 int delta_ocv_uv = 0;
2106 int n = 0;
2107 int rc_new_uah = 0;
2108 int pc_new = 0;
2109 int soc_new = 0;
2110 int slope = 0;
2111 int rc = 0;
2112 int delta_ocv_uv_limit = 0;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002113 int correction_limit_uv = 0;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002114
2115 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
2116 if (rc < 0) {
2117 pr_err("simultaneous vbat ibat failed err = %d\n", rc);
2118 goto out;
2119 }
2120
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002121 very_low_voltage_check(chip, vbat_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002122 cv_voltage_check(chip, vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002123
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002124 delta_ocv_uv_limit = DIV_ROUND_CLOSEST(ibat_ua, 1000);
2125
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08002126 ocv_est_uv = vbat_uv + (ibat_ua * params->rbatt_mohm)/1000;
2127
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002128 pc_est = calculate_pc(chip, ocv_est_uv, batt_temp);
2129 soc_est = div_s64((s64)params->fcc_uah * pc_est - params->uuc_uah*100,
2130 (s64)params->fcc_uah - params->uuc_uah);
2131 soc_est = bound_soc(soc_est);
2132
Xiaozhe Shi20640b52013-01-03 11:49:30 -08002133 /* never adjust during bms reset mode */
2134 if (bms_reset) {
2135 pr_debug("bms reset mode, SOC adjustment skipped\n");
2136 goto out;
2137 }
2138
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07002139 if (is_battery_charging(chip)) {
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002140 soc = charging_adjustments(chip, params, soc, vbat_uv, ibat_ua,
2141 batt_temp);
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07002142 /* Skip adjustments if we are in CV or ibat is negative */
2143 if (chip->soc_at_cv != -EINVAL || ibat_ua < 0)
2144 goto out;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002145 }
2146
2147 /*
2148 * do not adjust
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07002149 * if soc_est is same as what bms calculated
2150 * OR if soc_est > adjust_soc_low_threshold
2151 * OR if soc is above 90
2152 * because we might pull it low
2153 * and cause a bad user experience
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002154 */
Xiaozhe Shibd56b052013-10-21 11:51:30 -07002155 if (!wake_lock_active(&chip->low_voltage_wake_lock) &&
2156 (soc_est == soc
2157 || soc_est > chip->adjust_soc_low_threshold
2158 || soc >= NO_ADJUST_HIGH_SOC_THRESHOLD))
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002159 goto out;
2160
2161 if (chip->last_soc_est == -EINVAL)
2162 chip->last_soc_est = soc;
2163
2164 n = min(200, max(1 , soc + soc_est + chip->last_soc_est));
2165 chip->last_soc_est = soc_est;
2166
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002167 pc = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002168 if (pc > 0) {
2169 pc_new = calculate_pc(chip,
2170 chip->last_ocv_uv - (++slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002171 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002172 while (pc_new == pc) {
2173 /* start taking 10mV steps */
2174 slope = slope + 10;
2175 pc_new = calculate_pc(chip,
2176 chip->last_ocv_uv - (slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002177 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002178 }
2179 } else {
2180 /*
2181 * pc is already at the lowest point,
2182 * assume 1 millivolt translates to 1% pc
2183 */
2184 pc = 1;
2185 pc_new = 0;
2186 slope = 1;
2187 }
2188
2189 delta_ocv_uv = div_s64((soc - soc_est) * (s64)slope * 1000,
2190 n * (pc - pc_new));
2191
2192 if (abs(delta_ocv_uv) > delta_ocv_uv_limit) {
2193 pr_debug("limiting delta ocv %d limit = %d\n", delta_ocv_uv,
2194 delta_ocv_uv_limit);
2195
2196 if (delta_ocv_uv > 0)
2197 delta_ocv_uv = delta_ocv_uv_limit;
2198 else
2199 delta_ocv_uv = -1 * delta_ocv_uv_limit;
2200 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
2201 }
2202
Xiaozhe Shibd56b052013-10-21 11:51:30 -07002203 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2204 /* when in the cutoff region, do not correct upwards */
2205 delta_ocv_uv = max(0, delta_ocv_uv);
Xiaozhe Shi08d9aa72013-07-31 17:15:12 -07002206 goto skip_limits;
Xiaozhe Shibd56b052013-10-21 11:51:30 -07002207 }
Xiaozhe Shi08d9aa72013-07-31 17:15:12 -07002208
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002209 if (chip->last_ocv_uv > chip->flat_ocv_threshold_uv)
2210 correction_limit_uv = chip->high_ocv_correction_limit_uv;
2211 else
2212 correction_limit_uv = chip->low_ocv_correction_limit_uv;
2213
2214 if (abs(delta_ocv_uv) > correction_limit_uv) {
2215 pr_debug("limiting delta ocv %d limit = %d\n",
2216 delta_ocv_uv, correction_limit_uv);
2217 if (delta_ocv_uv > 0)
2218 delta_ocv_uv = correction_limit_uv;
2219 else
2220 delta_ocv_uv = -correction_limit_uv;
2221 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
2222 }
2223
Xiaozhe Shi08d9aa72013-07-31 17:15:12 -07002224skip_limits:
2225
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002226 chip->last_ocv_uv -= delta_ocv_uv;
2227
2228 if (chip->last_ocv_uv >= chip->max_voltage_uv)
2229 chip->last_ocv_uv = chip->max_voltage_uv;
2230
2231 /* calculate the soc based on this new ocv */
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002232 pc_new = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002233 rc_new_uah = (params->fcc_uah * pc_new) / 100;
2234 soc_new = (rc_new_uah - params->cc_uah - params->uuc_uah)*100
2235 / (params->fcc_uah - params->uuc_uah);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002236
2237 /*
2238 * if soc_new is ZERO force it higher so that phone doesnt report soc=0
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002239 * soc = 0 should happen only when soc_est is above a set value
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002240 */
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002241 if (soc_new == 0 && soc_est >= chip->hold_soc_est)
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002242 soc_new = 1;
2243
2244 soc = soc_new;
2245
2246out:
2247 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",
2248 ibat_ua, vbat_uv, ocv_est_uv, pc_est,
2249 soc_est, n, delta_ocv_uv, chip->last_ocv_uv,
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08002250 pc_new, soc_new, params->rbatt_mohm, slope);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002251
Xiaozhe Shie118c692012-09-24 15:17:43 -07002252 return soc;
2253}
2254
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002255static int clamp_soc_based_on_voltage(struct qpnp_bms_chip *chip, int soc)
2256{
2257 int rc, vbat_uv;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002258
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002259 rc = get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shi36458962013-02-06 16:19:57 -08002260 if (rc < 0) {
2261 pr_err("adc vbat failed err = %d\n", rc);
2262 return soc;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002263 }
Xiaozhe Shi9c14f3d2014-03-18 14:27:43 -07002264
2265 /* only clamp when discharging */
2266 if (is_battery_charging(chip))
2267 return soc;
2268
David Keitele50091e2014-03-05 09:55:57 -08002269 if (soc <= 0 && vbat_uv > chip->v_cutoff_uv) {
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002270 pr_debug("clamping soc to 1, vbat (%d) > cutoff (%d)\n",
2271 vbat_uv, chip->v_cutoff_uv);
2272 return 1;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002273 } else {
2274 pr_debug("not clamping, using soc = %d, vbat = %d and cutoff = %d\n",
2275 soc, vbat_uv, chip->v_cutoff_uv);
2276 return soc;
2277 }
2278}
2279
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002280static int64_t convert_cc_uah_to_raw(struct qpnp_bms_chip *chip, int64_t cc_uah)
2281{
2282 int64_t cc_uv, cc_pvh, cc_raw;
2283
2284 cc_pvh = cc_uah * chip->r_sense_uohm;
2285 cc_uv = div_s64(cc_pvh * SLEEP_CLK_HZ * SECONDS_PER_HOUR,
2286 CC_READING_TICKS * 1000000LL);
2287 cc_raw = div_s64(cc_uv * CC_READING_RESOLUTION_D,
2288 CC_READING_RESOLUTION_N);
2289 return cc_raw;
2290}
2291
2292#define CC_STEP_INCREMENT_UAH 1500
2293#define OCV_STEP_INCREMENT 0x10
2294static void configure_soc_wakeup(struct qpnp_bms_chip *chip,
2295 struct soc_params *params,
2296 int batt_temp, int target_soc)
2297{
2298 int target_ocv_uv;
2299 int64_t target_cc_uah, cc_raw_64, current_shdw_cc_raw_64;
2300 int64_t current_shdw_cc_uah, iadc_comp_factor;
2301 uint64_t cc_raw, current_shdw_cc_raw;
2302 int16_t ocv_raw, current_ocv_raw;
2303
2304 current_shdw_cc_raw = 0;
2305 mutex_lock(&chip->bms_output_lock);
2306 lock_output_data(chip);
2307 qpnp_read_wrapper(chip, (u8 *)&current_ocv_raw,
2308 chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
2309 unlock_output_data(chip);
2310 mutex_unlock(&chip->bms_output_lock);
2311 current_shdw_cc_uah = get_prop_bms_charge_counter_shadow(chip);
2312 current_shdw_cc_raw_64 = convert_cc_uah_to_raw(chip,
2313 current_shdw_cc_uah);
2314
2315 /*
2316 * Calculate the target shadow coulomb counter threshold for when
2317 * the SoC changes.
2318 *
2319 * Since the BMS driver resets the shadow coulomb counter every
2320 * 20 seconds when the device is awake, calculate the threshold as
2321 * a delta from the current shadow coulomb count.
2322 */
2323 target_cc_uah = (100 - target_soc)
2324 * (params->fcc_uah - params->uuc_uah)
2325 / 100 - current_shdw_cc_uah;
2326 if (target_cc_uah < 0) {
2327 /*
2328 * If the target cc is below 0, that means we have already
2329 * passed the point where SoC should have fallen.
2330 * Set a wakeup in a few more mAh and check back again
2331 */
2332 target_cc_uah = CC_STEP_INCREMENT_UAH;
2333 }
2334 iadc_comp_factor = 100000;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002335 qpnp_iadc_comp_result(chip->iadc_dev, &iadc_comp_factor);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002336 target_cc_uah = div64_s64(target_cc_uah * 100000, iadc_comp_factor);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002337 target_cc_uah = cc_reverse_adjust_for_gain(chip, target_cc_uah);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002338 cc_raw_64 = convert_cc_uah_to_raw(chip, target_cc_uah);
2339 cc_raw = convert_s64_to_s36(cc_raw_64);
2340
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002341 target_ocv_uv = find_ocv_for_pc(chip, batt_temp,
2342 find_pc_for_soc(chip, params, target_soc));
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002343 ocv_raw = convert_vbatt_uv_to_raw(chip, target_ocv_uv);
2344
2345 /*
2346 * If the current_ocv_raw was updated since reaching 100% and is lower
2347 * than the calculated target ocv threshold, set the new target
2348 * threshold 1.5mAh lower in order to check if the SoC changed yet.
2349 */
2350 if (current_ocv_raw != chip->ocv_reading_at_100
2351 && current_ocv_raw < ocv_raw)
2352 ocv_raw = current_ocv_raw - OCV_STEP_INCREMENT;
2353
2354 qpnp_write_wrapper(chip, (u8 *)&cc_raw,
2355 chip->base + BMS1_SW_CC_THR0, 5);
2356 qpnp_write_wrapper(chip, (u8 *)&ocv_raw,
2357 chip->base + BMS1_OCV_THR0, 2);
2358
Xiaozhe Shif5d87832013-11-21 17:05:39 -08002359 enable_bms_irq(&chip->ocv_thr_irq);
2360 enable_bms_irq(&chip->sw_cc_thr_irq);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002361 pr_debug("current sw_cc_raw = 0x%llx, current ocv = 0x%hx\n",
2362 current_shdw_cc_raw, (uint16_t)current_ocv_raw);
2363 pr_debug("target_cc_uah = %lld, raw64 = 0x%llx, raw 36 = 0x%llx, ocv_raw = 0x%hx\n",
2364 target_cc_uah,
2365 (uint64_t)cc_raw_64, cc_raw,
2366 (uint16_t)ocv_raw);
2367}
2368
Xiaozhe Shi5e791032013-10-25 11:30:42 -07002369#define BAD_SOC_THRESH -10
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002370static int calculate_raw_soc(struct qpnp_bms_chip *chip,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002371 struct raw_soc_params *raw,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002372 struct soc_params *params,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002373 int batt_temp)
2374{
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002375 int soc, remaining_usable_charge_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002376
Xiaozhe Shie118c692012-09-24 15:17:43 -07002377 /* calculate remaining usable charge */
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002378 remaining_usable_charge_uah = params->ocv_charge_uah
2379 - params->cc_uah
2380 - params->uuc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002381 pr_debug("RUC = %duAh\n", remaining_usable_charge_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002382
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002383 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002384 (params->fcc_uah - params->uuc_uah));
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002385
Xiaozhe Shi5e791032013-10-25 11:30:42 -07002386 if (chip->first_time_calc_soc && soc > BAD_SOC_THRESH && soc < 0) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07002387 /*
2388 * first time calcualtion and the pon ocv is too low resulting
2389 * in a bad soc. Adjust ocv to get 0 soc
2390 */
2391 pr_debug("soc is %d, adjusting pon ocv to make it 0\n", soc);
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002392 chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp,
2393 find_pc_for_soc(chip, params, 0));
2394 params->ocv_charge_uah = find_ocv_charge_for_soc(chip,
2395 params, 0);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002396
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002397 remaining_usable_charge_uah = params->ocv_charge_uah
2398 - params->cc_uah
2399 - params->uuc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002400
2401 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002402 (params->fcc_uah
2403 - params->uuc_uah));
Xiaozhe Shie118c692012-09-24 15:17:43 -07002404 pr_debug("DONE for O soc is %d, pon ocv adjusted to %duV\n",
2405 soc, chip->last_ocv_uv);
2406 }
2407
2408 if (soc > 100)
2409 soc = 100;
2410
Xiaozhe Shi5e791032013-10-25 11:30:42 -07002411 if (soc > BAD_SOC_THRESH && soc < 0) {
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002412 pr_debug("bad rem_usb_chg = %d rem_chg %d, cc_uah %d, unusb_chg %d\n",
Xiaozhe Shie118c692012-09-24 15:17:43 -07002413 remaining_usable_charge_uah,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002414 params->ocv_charge_uah,
2415 params->cc_uah, params->uuc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002416
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002417 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 -07002418 chip->last_ocv_uv, batt_temp,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002419 params->fcc_uah, soc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002420 soc = 0;
2421 }
2422
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002423 return soc;
2424}
2425
2426#define SLEEP_RECALC_INTERVAL 3
2427static int calculate_state_of_charge(struct qpnp_bms_chip *chip,
2428 struct raw_soc_params *raw,
2429 int batt_temp)
2430{
2431 struct soc_params params;
2432 int soc, previous_soc, shutdown_soc, new_calculated_soc;
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002433 int remaining_usable_charge_uah;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002434
2435 calculate_soc_params(chip, raw, &params, batt_temp);
2436 if (!is_battery_present(chip)) {
2437 pr_debug("battery gone, reporting 100\n");
2438 new_calculated_soc = 100;
2439 goto done_calculating;
2440 }
2441
2442 if (params.fcc_uah - params.uuc_uah <= 0) {
2443 pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
2444 params.fcc_uah,
2445 params.uuc_uah);
2446 new_calculated_soc = 0;
2447 goto done_calculating;
2448 }
2449
2450 soc = calculate_raw_soc(chip, raw, &params, batt_temp);
2451
Xiaozhe Shie118c692012-09-24 15:17:43 -07002452 mutex_lock(&chip->soc_invalidation_mutex);
2453 shutdown_soc = chip->shutdown_soc;
2454
2455 if (chip->first_time_calc_soc && soc != shutdown_soc
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002456 && !chip->shutdown_soc_invalid) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07002457 /*
2458 * soc for the first time - use shutdown soc
2459 * to adjust pon ocv since it is a small percent away from
2460 * the real soc
2461 */
2462 pr_debug("soc = %d before forcing shutdown_soc = %d\n",
2463 soc, shutdown_soc);
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002464 chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp,
2465 find_pc_for_soc(chip, &params, shutdown_soc));
2466 params.ocv_charge_uah = find_ocv_charge_for_soc(chip,
2467 &params, shutdown_soc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002468
2469 remaining_usable_charge_uah = params.ocv_charge_uah
2470 - params.cc_uah
2471 - params.uuc_uah;
2472
2473 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2474 (params.fcc_uah
2475 - params.uuc_uah));
2476
2477 pr_debug("DONE for shutdown_soc = %d soc is %d, adjusted ocv to %duV\n",
2478 shutdown_soc, soc, chip->last_ocv_uv);
2479 }
2480 mutex_unlock(&chip->soc_invalidation_mutex);
2481
Xiaozhe Shicbce8042014-02-13 14:08:47 -08002482 if (chip->first_time_calc_soc && !chip->shutdown_soc_invalid) {
2483 pr_debug("Skip adjustment when shutdown SOC has been forced\n");
2484 new_calculated_soc = soc;
2485 } else {
2486 pr_debug("SOC before adjustment = %d\n", soc);
2487 new_calculated_soc = adjust_soc(chip, &params, soc, batt_temp);
2488 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07002489
Xiaozhe Shi445d2492013-03-27 18:10:18 -07002490 /* always clamp soc due to BMS hw/sw immaturities */
2491 new_calculated_soc = clamp_soc_based_on_voltage(chip,
2492 new_calculated_soc);
Xiaozhe Shi422c27d2014-08-06 11:22:30 -07002493
2494 new_calculated_soc = bound_soc(new_calculated_soc);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002495 /*
2496 * If the battery is full, configure the cc threshold so the system
2497 * wakes up after SoC changes
2498 */
Xiaozhe Shif5d87832013-11-21 17:05:39 -08002499 if (is_battery_full(chip)) {
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002500 configure_soc_wakeup(chip, &params,
2501 batt_temp, bound_soc(new_calculated_soc - 1));
Xiaozhe Shif5d87832013-11-21 17:05:39 -08002502 } else {
2503 disable_bms_irq(&chip->ocv_thr_irq);
2504 disable_bms_irq(&chip->sw_cc_thr_irq);
2505 }
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002506done_calculating:
Xiaozhe Shifa6ea692013-05-31 11:15:13 -07002507 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07002508 previous_soc = chip->calculated_soc;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002509 chip->calculated_soc = new_calculated_soc;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002510 pr_debug("CC based calculated SOC = %d\n", chip->calculated_soc);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002511 if (chip->last_soc_invalid) {
2512 chip->last_soc_invalid = false;
2513 chip->last_soc = -EINVAL;
2514 }
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002515 /*
2516 * Check if more than a long time has passed since the last
2517 * calculation (more than n times compared to the soc recalculation
2518 * rate, where n is defined by SLEEP_RECALC_INTERVAL). If this is true,
2519 * then the system must have gone through a long sleep, and SoC can be
2520 * allowed to become unbounded by the last reported SoC
2521 */
2522 if (params.delta_time_s * 1000 >
2523 chip->calculate_soc_ms * SLEEP_RECALC_INTERVAL
2524 && !chip->first_time_calc_soc) {
2525 chip->last_soc_unbound = true;
2526 chip->last_soc_change_sec = chip->last_recalc_time;
2527 pr_debug("last_soc unbound because elapsed time = %d\n",
2528 params.delta_time_s);
2529 }
2530 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi27375822013-08-22 11:40:15 -07002531 wake_up_interruptible(&chip->bms_wait_queue);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07002532
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002533 if (new_calculated_soc != previous_soc && chip->bms_psy_registered) {
Xiaozhe Shi83484e32013-05-16 10:59:59 -07002534 power_supply_changed(&chip->bms_psy);
2535 pr_debug("power supply changed\n");
2536 } else {
2537 /*
2538 * Call report state of charge anyways to periodically update
2539 * reported SoC. This prevents reported SoC from being stuck
2540 * when calculated soc doesn't change.
2541 */
2542 report_state_of_charge(chip);
2543 }
2544
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002545 get_current_time(&chip->last_recalc_time);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002546 chip->first_time_calc_soc = 0;
Xiaozhe Shi70633922013-09-23 15:50:53 -07002547 chip->first_time_calc_uuc = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002548 return chip->calculated_soc;
2549}
2550
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002551static int calculate_soc_from_voltage(struct qpnp_bms_chip *chip)
2552{
2553 int voltage_range_uv, voltage_remaining_uv, voltage_based_soc;
Xiaozhe Shi36458962013-02-06 16:19:57 -08002554 int rc, vbat_uv;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002555
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002556 rc = get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shi36458962013-02-06 16:19:57 -08002557 if (rc < 0) {
2558 pr_err("adc vbat failed err = %d\n", rc);
2559 return rc;
2560 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002561 voltage_range_uv = chip->max_voltage_uv - chip->v_cutoff_uv;
2562 voltage_remaining_uv = vbat_uv - chip->v_cutoff_uv;
2563 voltage_based_soc = voltage_remaining_uv * 100 / voltage_range_uv;
2564
2565 voltage_based_soc = clamp(voltage_based_soc, 0, 100);
2566
2567 if (chip->prev_voltage_based_soc != voltage_based_soc
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002568 && chip->bms_psy_registered) {
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002569 power_supply_changed(&chip->bms_psy);
2570 pr_debug("power supply changed\n");
2571 }
2572 chip->prev_voltage_based_soc = voltage_based_soc;
2573
2574 pr_debug("vbat used = %duv\n", vbat_uv);
2575 pr_debug("Calculated voltage based soc = %d\n", voltage_based_soc);
2576 return voltage_based_soc;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08002577}
2578
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002579static int recalculate_raw_soc(struct qpnp_bms_chip *chip)
2580{
2581 int batt_temp, rc, soc;
2582 struct qpnp_vadc_result result;
2583 struct raw_soc_params raw;
2584 struct soc_params params;
2585
2586 bms_stay_awake(&chip->soc_wake_source);
2587 if (chip->use_voltage_soc) {
2588 soc = calculate_soc_from_voltage(chip);
2589 } else {
2590 if (!chip->batfet_closed)
Xiaozhe Shiffb7cfc2014-01-03 10:47:36 -08002591 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002592 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM,
2593 &result);
2594 if (rc) {
2595 pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n",
2596 LR_MUX1_BATT_THERM, rc);
2597 soc = chip->calculated_soc;
2598 } else {
2599 pr_debug("batt_temp phy = %lld meas = 0x%llx\n",
2600 result.physical,
2601 result.measurement);
2602 batt_temp = (int)result.physical;
2603
2604 mutex_lock(&chip->last_ocv_uv_mutex);
Xiaozhe Shied4a5522014-09-05 14:56:13 -07002605 rc = read_soc_params_raw(chip, &raw, batt_temp);
2606 if (rc) {
2607 pr_err("Unable to read params, rc: %d\n", rc);
2608 soc = 0;
2609 goto done;
2610 }
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002611 calculate_soc_params(chip, &raw, &params, batt_temp);
2612 if (!is_battery_present(chip)) {
2613 pr_debug("battery gone\n");
2614 soc = 0;
2615 } else if (params.fcc_uah - params.uuc_uah <= 0) {
2616 pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
2617 params.fcc_uah,
2618 params.uuc_uah);
2619 soc = 0;
2620 } else {
2621 soc = calculate_raw_soc(chip, &raw,
2622 &params, batt_temp);
2623 }
Xiaozhe Shied4a5522014-09-05 14:56:13 -07002624done:
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002625 mutex_unlock(&chip->last_ocv_uv_mutex);
2626 }
2627 }
2628 bms_relax(&chip->soc_wake_source);
2629 return soc;
2630}
2631
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002632static int recalculate_soc(struct qpnp_bms_chip *chip)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002633{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002634 int batt_temp, rc, soc;
2635 struct qpnp_vadc_result result;
2636 struct raw_soc_params raw;
2637
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002638 bms_stay_awake(&chip->soc_wake_source);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002639 mutex_lock(&chip->vbat_monitor_mutex);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002640 if (chip->vbat_monitor_params.state_request !=
2641 ADC_TM_HIGH_LOW_THR_DISABLE)
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002642 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2643 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002644 mutex_unlock(&chip->vbat_monitor_mutex);
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002645 if (chip->use_voltage_soc) {
2646 soc = calculate_soc_from_voltage(chip);
2647 } else {
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07002648 if (!chip->batfet_closed)
Xiaozhe Shiffb7cfc2014-01-03 10:47:36 -08002649 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002650 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM,
2651 &result);
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002652 if (rc) {
2653 pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n",
2654 LR_MUX1_BATT_THERM, rc);
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002655 soc = chip->calculated_soc;
2656 } else {
2657 pr_debug("batt_temp phy = %lld meas = 0x%llx\n",
2658 result.physical,
2659 result.measurement);
2660 batt_temp = (int)result.physical;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002661
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002662 mutex_lock(&chip->last_ocv_uv_mutex);
Xiaozhe Shied4a5522014-09-05 14:56:13 -07002663 rc = read_soc_params_raw(chip, &raw, batt_temp);
2664 if (rc) {
2665 pr_err("Unable to read params, rc: %d\n", rc);
2666 soc = chip->calculated_soc;
2667 } else {
2668 soc = calculate_state_of_charge(chip,
2669 &raw, batt_temp);
2670 }
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002671 mutex_unlock(&chip->last_ocv_uv_mutex);
2672 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002673 }
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002674 bms_relax(&chip->soc_wake_source);
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002675 return soc;
2676}
2677
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002678static void recalculate_work(struct work_struct *work)
2679{
2680 struct qpnp_bms_chip *chip = container_of(work,
2681 struct qpnp_bms_chip,
2682 recalc_work);
2683
2684 recalculate_soc(chip);
2685}
2686
Xiaozhe Shicb487b12013-10-14 17:42:07 -07002687static int get_calculation_delay_ms(struct qpnp_bms_chip *chip)
2688{
2689 if (wake_lock_active(&chip->low_voltage_wake_lock))
2690 return chip->low_voltage_calculate_soc_ms;
2691 else if (chip->calculated_soc < chip->low_soc_calc_threshold)
2692 return chip->low_soc_calculate_soc_ms;
2693 else
2694 return chip->calculate_soc_ms;
2695}
2696
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002697static void calculate_soc_work(struct work_struct *work)
2698{
2699 struct qpnp_bms_chip *chip = container_of(work,
2700 struct qpnp_bms_chip,
2701 calculate_soc_delayed_work.work);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002702
Xiaozhe Shicb487b12013-10-14 17:42:07 -07002703 recalculate_soc(chip);
2704 schedule_delayed_work(&chip->calculate_soc_delayed_work,
2705 round_jiffies_relative(msecs_to_jiffies
2706 (get_calculation_delay_ms(chip))));
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002707}
2708
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002709static void configure_vbat_monitor_low(struct qpnp_bms_chip *chip)
2710{
2711 mutex_lock(&chip->vbat_monitor_mutex);
2712 if (chip->vbat_monitor_params.state_request
2713 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2714 /*
2715 * Battery is now around or below v_cutoff
2716 */
2717 pr_debug("battery entered cutoff range\n");
2718 if (!wake_lock_active(&chip->low_voltage_wake_lock)) {
2719 pr_debug("voltage low, holding wakelock\n");
2720 wake_lock(&chip->low_voltage_wake_lock);
2721 cancel_delayed_work_sync(
2722 &chip->calculate_soc_delayed_work);
2723 schedule_delayed_work(
2724 &chip->calculate_soc_delayed_work, 0);
2725 }
2726 chip->vbat_monitor_params.state_request =
2727 ADC_TM_HIGH_THR_ENABLE;
2728 chip->vbat_monitor_params.high_thr =
2729 (chip->low_voltage_threshold + VBATT_ERROR_MARGIN);
2730 pr_debug("set low thr to %d and high to %d\n",
2731 chip->vbat_monitor_params.low_thr,
2732 chip->vbat_monitor_params.high_thr);
2733 chip->vbat_monitor_params.low_thr = 0;
2734 } else if (chip->vbat_monitor_params.state_request
2735 == ADC_TM_LOW_THR_ENABLE) {
2736 /*
2737 * Battery is in normal operation range.
2738 */
2739 pr_debug("battery entered normal range\n");
2740 if (wake_lock_active(&chip->cv_wake_lock)) {
2741 wake_unlock(&chip->cv_wake_lock);
2742 pr_debug("releasing cv wake lock\n");
2743 }
2744 chip->in_cv_range = false;
2745 chip->vbat_monitor_params.state_request =
2746 ADC_TM_HIGH_LOW_THR_ENABLE;
2747 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2748 - VBATT_ERROR_MARGIN;
2749 chip->vbat_monitor_params.low_thr =
2750 chip->low_voltage_threshold;
2751 pr_debug("set low thr to %d and high to %d\n",
2752 chip->vbat_monitor_params.low_thr,
2753 chip->vbat_monitor_params.high_thr);
2754 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002755 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2756 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002757 mutex_unlock(&chip->vbat_monitor_mutex);
2758}
2759
2760#define CV_LOW_THRESHOLD_HYST_UV 100000
2761static void configure_vbat_monitor_high(struct qpnp_bms_chip *chip)
2762{
2763 mutex_lock(&chip->vbat_monitor_mutex);
2764 if (chip->vbat_monitor_params.state_request
2765 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2766 /*
2767 * Battery is around vddmax
2768 */
2769 pr_debug("battery entered vddmax range\n");
2770 chip->in_cv_range = true;
2771 if (!wake_lock_active(&chip->cv_wake_lock)) {
2772 wake_lock(&chip->cv_wake_lock);
2773 pr_debug("holding cv wake lock\n");
2774 }
2775 schedule_work(&chip->recalc_work);
2776 chip->vbat_monitor_params.state_request =
2777 ADC_TM_LOW_THR_ENABLE;
2778 chip->vbat_monitor_params.low_thr =
2779 (chip->max_voltage_uv - CV_LOW_THRESHOLD_HYST_UV);
2780 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv * 2;
2781 pr_debug("set low thr to %d and high to %d\n",
2782 chip->vbat_monitor_params.low_thr,
2783 chip->vbat_monitor_params.high_thr);
2784 } else if (chip->vbat_monitor_params.state_request
2785 == ADC_TM_HIGH_THR_ENABLE) {
2786 /*
2787 * Battery is in normal operation range.
2788 */
2789 pr_debug("battery entered normal range\n");
2790 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2791 pr_debug("voltage high, releasing wakelock\n");
2792 wake_unlock(&chip->low_voltage_wake_lock);
2793 }
2794 chip->vbat_monitor_params.state_request =
2795 ADC_TM_HIGH_LOW_THR_ENABLE;
2796 chip->vbat_monitor_params.high_thr =
2797 chip->max_voltage_uv - VBATT_ERROR_MARGIN;
2798 chip->vbat_monitor_params.low_thr =
2799 chip->low_voltage_threshold;
2800 pr_debug("set low thr to %d and high to %d\n",
2801 chip->vbat_monitor_params.low_thr,
2802 chip->vbat_monitor_params.high_thr);
2803 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002804 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2805 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002806 mutex_unlock(&chip->vbat_monitor_mutex);
2807}
2808
2809static void btm_notify_vbat(enum qpnp_tm_state state, void *ctx)
2810{
2811 struct qpnp_bms_chip *chip = ctx;
2812 int vbat_uv;
2813 struct qpnp_vadc_result result;
2814 int rc;
2815
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002816 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &result);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002817 pr_debug("vbat = %lld, raw = 0x%x\n", result.physical, result.adc_code);
2818
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002819 get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002820 pr_debug("vbat is at %d, state is at %d\n", vbat_uv, state);
2821
2822 if (state == ADC_TM_LOW_STATE) {
2823 pr_debug("low voltage btm notification triggered\n");
2824 if (vbat_uv - VBATT_ERROR_MARGIN
2825 < chip->vbat_monitor_params.low_thr) {
2826 configure_vbat_monitor_low(chip);
2827 } else {
2828 pr_debug("faulty btm trigger, discarding\n");
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002829 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002830 &chip->vbat_monitor_params);
2831 }
2832 } else if (state == ADC_TM_HIGH_STATE) {
2833 pr_debug("high voltage btm notification triggered\n");
2834 if (vbat_uv + VBATT_ERROR_MARGIN
2835 > chip->vbat_monitor_params.high_thr) {
2836 configure_vbat_monitor_high(chip);
2837 } else {
2838 pr_debug("faulty btm trigger, discarding\n");
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002839 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002840 &chip->vbat_monitor_params);
2841 }
2842 } else {
2843 pr_debug("unknown voltage notification state: %d\n", state);
2844 }
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002845 if (chip->bms_psy_registered)
Xiaozhe Shifa120db2013-06-06 15:57:19 -07002846 power_supply_changed(&chip->bms_psy);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002847}
2848
2849static int reset_vbat_monitoring(struct qpnp_bms_chip *chip)
2850{
2851 int rc;
2852
2853 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_DISABLE;
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002854
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002855 rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2856 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002857 if (rc) {
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002858 pr_err("tm disable failed: %d\n", rc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002859 return rc;
2860 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002861 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2862 pr_debug("battery removed, releasing wakelock\n");
2863 wake_unlock(&chip->low_voltage_wake_lock);
2864 }
2865 if (chip->in_cv_range) {
2866 pr_debug("battery removed, removing in_cv_range state\n");
2867 chip->in_cv_range = false;
2868 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002869 return 0;
2870}
2871
2872static int setup_vbat_monitoring(struct qpnp_bms_chip *chip)
2873{
2874 int rc;
2875
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002876 chip->vbat_monitor_params.low_thr = chip->low_voltage_threshold;
2877 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2878 - VBATT_ERROR_MARGIN;
2879 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_ENABLE;
2880 chip->vbat_monitor_params.channel = VBAT_SNS;
2881 chip->vbat_monitor_params.btm_ctx = (void *)chip;
2882 chip->vbat_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
2883 chip->vbat_monitor_params.threshold_notification = &btm_notify_vbat;
2884 pr_debug("set low thr to %d and high to %d\n",
2885 chip->vbat_monitor_params.low_thr,
2886 chip->vbat_monitor_params.high_thr);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002887
2888 if (!is_battery_present(chip)) {
2889 pr_debug("no battery inserted, do not enable vbat monitoring\n");
2890 chip->vbat_monitor_params.state_request =
2891 ADC_TM_HIGH_LOW_THR_DISABLE;
2892 } else {
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002893 rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2894 &chip->vbat_monitor_params);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002895 if (rc) {
2896 pr_err("tm setup failed: %d\n", rc);
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002897 return rc;
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002898 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002899 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002900
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002901 pr_debug("setup complete\n");
2902 return 0;
2903}
2904
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302905static void readjust_fcc_table(struct qpnp_bms_chip *chip)
2906{
2907 struct single_row_lut *temp, *old;
2908 int i, fcc, ratio;
2909
2910 if (!chip->enable_fcc_learning)
2911 return;
2912
2913 if (!chip->fcc_temp_lut) {
2914 pr_err("The static fcc lut table is NULL\n");
2915 return;
2916 }
2917
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07002918 temp = devm_kzalloc(chip->dev, sizeof(struct single_row_lut),
2919 GFP_KERNEL);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302920 if (!temp) {
2921 pr_err("Cannot allocate memory for adjusted fcc table\n");
2922 return;
2923 }
2924
2925 fcc = interpolate_fcc(chip->fcc_temp_lut, chip->fcc_new_batt_temp);
2926
2927 temp->cols = chip->fcc_temp_lut->cols;
2928 for (i = 0; i < chip->fcc_temp_lut->cols; i++) {
2929 temp->x[i] = chip->fcc_temp_lut->x[i];
2930 ratio = div_u64(chip->fcc_temp_lut->y[i] * 1000, fcc);
2931 temp->y[i] = (ratio * chip->fcc_new_mah);
2932 temp->y[i] /= 1000;
2933 }
2934
2935 old = chip->adjusted_fcc_temp_lut;
2936 chip->adjusted_fcc_temp_lut = temp;
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07002937 devm_kfree(chip->dev, old);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302938}
2939
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302940static int read_fcc_data_from_backup(struct qpnp_bms_chip *chip)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302941{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302942 int rc, i;
2943 u8 fcc = 0, chgcyl = 0;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302944
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302945 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2946 rc = qpnp_read_wrapper(chip, &fcc,
2947 chip->base + BMS_FCC_BASE_REG + i, 1);
2948 rc |= qpnp_read_wrapper(chip, &chgcyl,
2949 chip->base + BMS_CHGCYL_BASE_REG + i, 1);
2950 if (rc) {
2951 pr_err("Unable to read FCC data\n");
2952 return rc;
2953 }
2954 if (fcc == 0 || (fcc == 0xFF && chgcyl == 0xFF)) {
2955 /* FCC invalid/not present */
2956 chip->fcc_learning_samples[i].fcc_new = 0;
2957 chip->fcc_learning_samples[i].chargecycles = 0;
2958 } else {
2959 /* valid FCC data */
2960 chip->fcc_sample_count++;
2961 chip->fcc_learning_samples[i].fcc_new =
2962 fcc * chip->fcc_resolution;
2963 chip->fcc_learning_samples[i].chargecycles =
2964 chgcyl * CHGCYL_RESOLUTION;
2965 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302966 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302967
2968 return 0;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302969}
2970
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302971static int discard_backup_fcc_data(struct qpnp_bms_chip *chip)
2972{
2973 int rc = 0, i;
2974 u8 temp_u8 = 0;
2975
2976 chip->fcc_sample_count = 0;
2977 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2978 rc = qpnp_write_wrapper(chip, &temp_u8,
2979 chip->base + BMS_FCC_BASE_REG + i, 1);
2980 rc |= qpnp_write_wrapper(chip, &temp_u8,
2981 chip->base + BMS_CHGCYL_BASE_REG + i, 1);
2982 if (rc) {
2983 pr_err("Unable to clear FCC data\n");
2984 return rc;
2985 }
2986 }
2987
2988 return 0;
2989}
2990
2991static void
2992average_fcc_samples_and_readjust_fcc_table(struct qpnp_bms_chip *chip)
2993{
2994 int i, temp_fcc_avg = 0, temp_fcc_delta = 0, new_fcc_avg = 0;
2995 struct fcc_sample *ft;
2996
2997 for (i = 0; i < chip->min_fcc_learning_samples; i++)
2998 temp_fcc_avg += chip->fcc_learning_samples[i].fcc_new;
2999
3000 temp_fcc_avg /= chip->min_fcc_learning_samples;
3001 temp_fcc_delta = div_u64(temp_fcc_avg * DELTA_FCC_PERCENT, 100);
3002
3003 /* fix the fcc if its an outlier i.e. > 5% of the average */
3004 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
3005 ft = &chip->fcc_learning_samples[i];
3006 if (abs(ft->fcc_new - temp_fcc_avg) > temp_fcc_delta)
3007 new_fcc_avg += temp_fcc_avg;
3008 else
3009 new_fcc_avg += ft->fcc_new;
3010 }
3011 new_fcc_avg /= chip->min_fcc_learning_samples;
3012
3013 chip->fcc_new_mah = new_fcc_avg;
3014 chip->fcc_new_batt_temp = FCC_DEFAULT_TEMP;
3015 pr_info("FCC update: New fcc_mah=%d, fcc_batt_temp=%d\n",
3016 new_fcc_avg, FCC_DEFAULT_TEMP);
3017 readjust_fcc_table(chip);
3018}
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303019
3020static void backup_charge_cycle(struct qpnp_bms_chip *chip)
3021{
3022 int rc = 0;
3023
3024 if (chip->charge_increase >= 0) {
3025 rc = qpnp_write_wrapper(chip, &chip->charge_increase,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303026 chip->base + CHARGE_INCREASE_STORAGE, 1);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303027 if (rc)
3028 pr_err("Unable to backup charge_increase\n");
3029 }
3030
3031 if (chip->charge_cycles >= 0) {
3032 rc = qpnp_write_wrapper(chip, (u8 *)&chip->charge_cycles,
3033 chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
3034 if (rc)
3035 pr_err("Unable to backup charge_cycles\n");
3036 }
3037}
3038
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303039static bool chargecycles_in_range(struct qpnp_bms_chip *chip)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303040{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303041 int i, min_cycle, max_cycle, valid_range;
3042
3043 /* find the smallest and largest charge cycle */
3044 max_cycle = min_cycle = chip->fcc_learning_samples[0].chargecycles;
3045 for (i = 1; i < chip->min_fcc_learning_samples; i++) {
3046 if (min_cycle > chip->fcc_learning_samples[i].chargecycles)
3047 min_cycle = chip->fcc_learning_samples[i].chargecycles;
3048 if (max_cycle < chip->fcc_learning_samples[i].chargecycles)
3049 max_cycle = chip->fcc_learning_samples[i].chargecycles;
3050 }
3051
3052 /* check if chargecyles are in range to continue with FCC update */
3053 valid_range = DIV_ROUND_UP(VALID_FCC_CHGCYL_RANGE,
3054 CHGCYL_RESOLUTION) * CHGCYL_RESOLUTION;
3055 if (abs(max_cycle - min_cycle) > valid_range)
3056 return false;
3057
3058 return true;
3059}
3060
3061static int read_chgcycle_data_from_backup(struct qpnp_bms_chip *chip)
3062{
3063 int rc;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303064 uint16_t temp_u16 = 0;
3065 u8 temp_u8 = 0;
3066
3067 rc = qpnp_read_wrapper(chip, &temp_u8,
3068 chip->base + CHARGE_INCREASE_STORAGE, 1);
3069 if (!rc && temp_u8 != 0xFF)
3070 chip->charge_increase = temp_u8;
3071
3072 rc = qpnp_read_wrapper(chip, (u8 *)&temp_u16,
3073 chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
3074 if (!rc && temp_u16 != 0xFFFF)
3075 chip->charge_cycles = temp_u16;
3076
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303077 return rc;
3078}
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303079
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303080static void
3081attempt_learning_new_fcc(struct qpnp_bms_chip *chip)
3082{
3083 pr_debug("Total FCC sample count=%d\n", chip->fcc_sample_count);
3084
3085 /* update FCC if we have the required samples */
3086 if ((chip->fcc_sample_count == chip->min_fcc_learning_samples) &&
3087 chargecycles_in_range(chip))
3088 average_fcc_samples_and_readjust_fcc_table(chip);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303089}
3090
3091static int calculate_real_soc(struct qpnp_bms_chip *chip,
3092 int batt_temp, struct raw_soc_params *raw, int cc_uah)
3093{
3094 int fcc_uah, rc_uah;
3095
3096 fcc_uah = calculate_fcc(chip, batt_temp);
3097 rc_uah = calculate_ocv_charge(chip, raw, fcc_uah);
3098
3099 return ((rc_uah - cc_uah) * 100) / fcc_uah;
3100}
3101
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303102#define MAX_U8_VALUE ((u8)(~0U))
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303103
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303104static int backup_new_fcc(struct qpnp_bms_chip *chip, int fcc_mah,
3105 int chargecycles)
3106{
3107 int rc, min_cycle, i;
3108 u8 fcc_new, chgcyl, pos = 0;
3109 struct fcc_sample *ft;
3110
3111 if ((fcc_mah > (chip->fcc_resolution * MAX_U8_VALUE)) ||
3112 (chargecycles > (CHGCYL_RESOLUTION * MAX_U8_VALUE))) {
3113 pr_warn("FCC/Chgcyl beyond storage limit. FCC=%d, chgcyl=%d\n",
3114 fcc_mah, chargecycles);
3115 return -EINVAL;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303116 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303117
3118 if (chip->fcc_sample_count == chip->min_fcc_learning_samples) {
3119 /* search best location - oldest entry */
3120 min_cycle = chip->fcc_learning_samples[0].chargecycles;
3121 for (i = 1; i < chip->min_fcc_learning_samples; i++) {
3122 if (min_cycle >
Zhenhua Huang82663ea2014-09-26 10:42:31 +08003123 chip->fcc_learning_samples[i].chargecycles) {
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303124 pos = i;
Zhenhua Huang82663ea2014-09-26 10:42:31 +08003125 break;
3126 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303127 }
3128 } else {
3129 /* find an empty location */
3130 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
3131 ft = &chip->fcc_learning_samples[i];
3132 if (ft->fcc_new == 0 || (ft->fcc_new == 0xFF &&
3133 ft->chargecycles == 0xFF)) {
3134 pos = i;
3135 break;
3136 }
3137 }
3138 chip->fcc_sample_count++;
3139 }
3140 chip->fcc_learning_samples[pos].fcc_new = fcc_mah;
3141 chip->fcc_learning_samples[pos].chargecycles = chargecycles;
3142
3143 fcc_new = DIV_ROUND_UP(fcc_mah, chip->fcc_resolution);
3144 rc = qpnp_write_wrapper(chip, (u8 *)&fcc_new,
3145 chip->base + BMS_FCC_BASE_REG + pos, 1);
3146 if (rc)
3147 return rc;
3148
3149 chgcyl = DIV_ROUND_UP(chargecycles, CHGCYL_RESOLUTION);
3150 rc = qpnp_write_wrapper(chip, (u8 *)&chgcyl,
3151 chip->base + BMS_CHGCYL_BASE_REG + pos, 1);
3152 if (rc)
3153 return rc;
3154
3155 pr_debug("Backup new FCC: fcc_new=%d, chargecycle=%d, pos=%d\n",
3156 fcc_new, chgcyl, pos);
3157
3158 return rc;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303159}
3160
3161static void update_fcc_learning_table(struct qpnp_bms_chip *chip,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303162 int new_fcc_uah, int chargecycles, int batt_temp)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303163{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303164 int rc, fcc_default, fcc_temp;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303165
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303166 /* convert the fcc at batt_temp to new fcc at FCC_DEFAULT_TEMP */
3167 fcc_default = calculate_fcc(chip, FCC_DEFAULT_TEMP) / 1000;
3168 fcc_temp = calculate_fcc(chip, batt_temp) / 1000;
3169 new_fcc_uah = (new_fcc_uah / fcc_temp) * fcc_default;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303170
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303171 rc = backup_new_fcc(chip, new_fcc_uah / 1000, chargecycles);
3172 if (rc) {
3173 pr_err("Unable to backup new FCC\n");
3174 return;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303175 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303176 /* check if FCC can be updated */
3177 attempt_learning_new_fcc(chip);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303178}
3179
3180static bool is_new_fcc_valid(int new_fcc_uah, int fcc_uah)
3181{
3182 if ((new_fcc_uah >= (fcc_uah / 2)) &&
3183 ((new_fcc_uah * 100) <= (fcc_uah * 105)))
3184 return true;
3185
3186 pr_debug("FCC rejected - not within valid limit\n");
3187 return false;
3188}
3189
3190static void fcc_learning_config(struct qpnp_bms_chip *chip, bool start)
3191{
3192 int rc, batt_temp;
3193 struct raw_soc_params raw;
3194 struct qpnp_vadc_result result;
3195 int fcc_uah, new_fcc_uah, delta_cc_uah, delta_soc;
3196
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003197 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303198 if (rc) {
3199 pr_err("Unable to read batt_temp\n");
3200 return;
3201 } else {
3202 batt_temp = (int)result.physical;
3203 }
3204
3205 rc = read_soc_params_raw(chip, &raw, batt_temp);
3206 if (rc) {
3207 pr_err("Unable to read CC, cannot update FCC\n");
3208 return;
3209 }
3210
3211 if (start) {
3212 chip->start_pc = interpolate_pc(chip->pc_temp_ocv_lut,
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08003213 batt_temp, raw.last_good_ocv_uv / 1000);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003214 chip->start_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303215 chip->start_real_soc = calculate_real_soc(chip,
3216 batt_temp, &raw, chip->start_cc_uah);
3217 pr_debug("start_pc=%d, start_cc=%d, start_soc=%d real_soc=%d\n",
3218 chip->start_pc, chip->start_cc_uah,
3219 chip->start_soc, chip->start_real_soc);
3220 } else {
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003221 chip->end_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303222 delta_soc = 100 - chip->start_real_soc;
3223 delta_cc_uah = abs(chip->end_cc_uah - chip->start_cc_uah);
3224 new_fcc_uah = div_u64(delta_cc_uah * 100, delta_soc);
3225 fcc_uah = calculate_fcc(chip, batt_temp);
3226 pr_debug("start_soc=%d, start_pc=%d, start_real_soc=%d, start_cc=%d, end_cc=%d, new_fcc=%d\n",
3227 chip->start_soc, chip->start_pc, chip->start_real_soc,
3228 chip->start_cc_uah, chip->end_cc_uah, new_fcc_uah);
3229
3230 if (is_new_fcc_valid(new_fcc_uah, fcc_uah))
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303231 update_fcc_learning_table(chip, new_fcc_uah,
3232 chip->charge_cycles, batt_temp);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303233 }
3234}
3235
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003236#define MAX_CAL_TRIES 200
3237#define MIN_CAL_UA 3000
3238static void batfet_open_work(struct work_struct *work)
3239{
3240 int i;
3241 int rc;
3242 int result_ua;
3243 u8 orig_delay, sample_delay;
3244 struct qpnp_bms_chip *chip = container_of(work,
3245 struct qpnp_bms_chip,
3246 batfet_open_work);
3247
3248 rc = qpnp_read_wrapper(chip, &orig_delay,
3249 chip->base + BMS1_S1_DELAY_CTL, 1);
3250
3251 sample_delay = 0x0;
3252 rc = qpnp_write_wrapper(chip, &sample_delay,
3253 chip->base + BMS1_S1_DELAY_CTL, 1);
3254
3255 /*
3256 * In certain PMICs there is a coupling issue which causes
3257 * bad calibration value that result in a huge battery current
3258 * even when the BATFET is open. Do continious calibrations until
3259 * we hit reasonable cal values which result in low battery current
3260 */
3261
3262 for (i = 0; (!chip->batfet_closed) && i < MAX_CAL_TRIES; i++) {
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003263 rc = qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003264 /*
3265 * Wait 20mS after calibration and before reading battery
3266 * current. The BMS h/w uses calibration values in the
3267 * next sampling of vsense.
3268 */
3269 msleep(20);
3270 rc |= get_battery_current(chip, &result_ua);
3271 if (rc == 0 && abs(result_ua) <= MIN_CAL_UA) {
3272 pr_debug("good cal at %d attempt\n", i);
3273 break;
3274 }
3275 }
3276 pr_debug("batfet_closed = %d i = %d result_ua = %d\n",
3277 chip->batfet_closed, i, result_ua);
3278
3279 rc = qpnp_write_wrapper(chip, &orig_delay,
3280 chip->base + BMS1_S1_DELAY_CTL, 1);
3281}
3282
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003283static void charging_began(struct qpnp_bms_chip *chip)
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003284{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003285 mutex_lock(&chip->last_soc_mutex);
3286 chip->charge_start_tm_sec = 0;
3287 chip->catch_up_time_sec = 0;
3288 mutex_unlock(&chip->last_soc_mutex);
3289
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303290 chip->start_soc = report_state_of_charge(chip);
3291
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003292 mutex_lock(&chip->last_ocv_uv_mutex);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303293 if (chip->enable_fcc_learning)
3294 fcc_learning_config(chip, true);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003295 chip->soc_at_cv = -EINVAL;
3296 chip->prev_chg_soc = -EINVAL;
3297 mutex_unlock(&chip->last_ocv_uv_mutex);
3298}
3299
3300static void charging_ended(struct qpnp_bms_chip *chip)
3301{
3302 mutex_lock(&chip->last_soc_mutex);
3303 chip->charge_start_tm_sec = 0;
3304 chip->catch_up_time_sec = 0;
3305 mutex_unlock(&chip->last_soc_mutex);
3306
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303307 chip->end_soc = report_state_of_charge(chip);
3308
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003309 mutex_lock(&chip->last_ocv_uv_mutex);
3310 chip->soc_at_cv = -EINVAL;
3311 chip->prev_chg_soc = -EINVAL;
Zhenhua Huang95a05d32014-03-31 18:09:45 +08003312 chip->in_taper_charge = false;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303313
3314 /* update the chargecycles */
3315 if (chip->end_soc > chip->start_soc) {
3316 chip->charge_increase += (chip->end_soc - chip->start_soc);
3317 if (chip->charge_increase > 100) {
3318 chip->charge_cycles++;
3319 chip->charge_increase = chip->charge_increase % 100;
3320 }
3321 if (chip->enable_fcc_learning)
3322 backup_charge_cycle(chip);
3323 }
3324
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003325 if (get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL) {
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303326 if (chip->enable_fcc_learning &&
3327 (chip->start_soc <= chip->min_fcc_learning_soc) &&
3328 (chip->start_pc <= chip->min_fcc_ocv_pc))
3329 fcc_learning_config(chip, false);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003330 chip->done_charging = true;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003331 chip->last_soc_invalid = true;
Xiaozhe Shicc48e992013-05-28 16:42:24 -07003332 } else if (chip->charging_adjusted_ocv > 0) {
3333 pr_debug("Charging stopped before full, adjusted OCV = %d\n",
3334 chip->charging_adjusted_ocv);
3335 chip->last_ocv_uv = chip->charging_adjusted_ocv;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003336 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303337
Xiaozhe Shicc48e992013-05-28 16:42:24 -07003338 chip->charging_adjusted_ocv = -EINVAL;
3339
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003340 mutex_unlock(&chip->last_ocv_uv_mutex);
3341}
3342
3343static void battery_status_check(struct qpnp_bms_chip *chip)
3344{
3345 int status = get_battery_status(chip);
3346
Xiaozhe Shibda84992013-09-05 10:39:11 -07003347 mutex_lock(&chip->status_lock);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003348 if (chip->battery_status != status) {
Xiaozhe Shi30e94802013-08-19 16:40:53 -07003349 pr_debug("status = %d, shadow status = %d\n",
3350 status, chip->battery_status);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003351 if (status == POWER_SUPPLY_STATUS_CHARGING) {
3352 pr_debug("charging started\n");
3353 charging_began(chip);
3354 } else if (chip->battery_status
3355 == POWER_SUPPLY_STATUS_CHARGING) {
3356 pr_debug("charging ended\n");
3357 charging_ended(chip);
3358 }
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003359
3360 if (status == POWER_SUPPLY_STATUS_FULL) {
3361 pr_debug("battery full\n");
Xiaozhe Shibda84992013-09-05 10:39:11 -07003362 recalculate_soc(chip);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003363 } else if (chip->battery_status
3364 == POWER_SUPPLY_STATUS_FULL) {
3365 pr_debug("battery not full any more\n");
3366 disable_bms_irq(&chip->ocv_thr_irq);
3367 disable_bms_irq(&chip->sw_cc_thr_irq);
3368 }
3369
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003370 chip->battery_status = status;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003371 /* battery charge status has changed, so force a soc
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003372 * recalculation to update the SoC */
3373 schedule_work(&chip->recalc_work);
3374 }
Xiaozhe Shibda84992013-09-05 10:39:11 -07003375 mutex_unlock(&chip->status_lock);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003376}
3377
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003378#define CALIB_WRKARND_DIG_MAJOR_MAX 0x03
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003379static void batfet_status_check(struct qpnp_bms_chip *chip)
3380{
3381 bool batfet_closed;
3382
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003383 batfet_closed = is_batfet_closed(chip);
3384 if (chip->batfet_closed != batfet_closed) {
3385 chip->batfet_closed = batfet_closed;
Xiaozhe Shiffb7cfc2014-01-03 10:47:36 -08003386 if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX)
3387 return;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003388 if (batfet_closed == false) {
3389 /* batfet opened */
3390 schedule_work(&chip->batfet_open_work);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003391 qpnp_iadc_skip_calibration(chip->iadc_dev);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003392 } else {
3393 /* batfet closed */
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003394 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, true);
3395 qpnp_iadc_resume_calibration(chip->iadc_dev);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003396 }
3397 }
3398}
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003399
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003400static void battery_insertion_check(struct qpnp_bms_chip *chip)
3401{
Xiaozhe Shi90f3a412013-08-21 10:31:35 -07003402 int present = (int)is_battery_present(chip);
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07003403 int insertion_ocv_uv = get_battery_insertion_ocv_uv(chip);
3404 int insertion_ocv_taken = (insertion_ocv_uv > 0);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003405
3406 mutex_lock(&chip->vbat_monitor_mutex);
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07003407 if (chip->battery_present != present
3408 && (present == insertion_ocv_taken
3409 || chip->battery_present == -EINVAL)) {
3410 pr_debug("status = %d, shadow status = %d, insertion_ocv_uv = %d\n",
3411 present, chip->battery_present,
3412 insertion_ocv_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003413 if (chip->battery_present != -EINVAL) {
3414 if (present) {
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07003415 chip->insertion_ocv_uv = insertion_ocv_uv;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003416 setup_vbat_monitoring(chip);
3417 chip->new_battery = true;
3418 } else {
3419 reset_vbat_monitoring(chip);
3420 }
3421 }
3422 chip->battery_present = present;
3423 /* a new battery was inserted or removed, so force a soc
3424 * recalculation to update the SoC */
3425 schedule_work(&chip->recalc_work);
3426 }
3427 mutex_unlock(&chip->vbat_monitor_mutex);
3428}
3429
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003430/* Returns capacity as a SoC percentage between 0 and 100 */
3431static int get_prop_bms_capacity(struct qpnp_bms_chip *chip)
3432{
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08003433 return report_state_of_charge(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003434}
3435
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003436static void qpnp_bms_external_power_changed(struct power_supply *psy)
3437{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003438 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
3439 bms_psy);
3440
3441 battery_insertion_check(chip);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003442 batfet_status_check(chip);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003443 battery_status_check(chip);
Zhenhua Huang95a05d32014-03-31 18:09:45 +08003444
3445 if (POWER_SUPPLY_CHARGE_TYPE_TAPER == get_battery_charge_type(chip))
3446 chip->in_taper_charge = true;
3447 else
3448 chip->in_taper_charge = false;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003449}
3450
3451static int qpnp_bms_power_get_property(struct power_supply *psy,
3452 enum power_supply_property psp,
3453 union power_supply_propval *val)
3454{
3455 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
3456 bms_psy);
3457
3458 switch (psp) {
3459 case POWER_SUPPLY_PROP_CAPACITY:
3460 val->intval = get_prop_bms_capacity(chip);
3461 break;
Xiaozhe Shibda84992013-09-05 10:39:11 -07003462 case POWER_SUPPLY_PROP_STATUS:
3463 val->intval = chip->battery_status;
3464 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003465 case POWER_SUPPLY_PROP_CURRENT_NOW:
3466 val->intval = get_prop_bms_current_now(chip);
3467 break;
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07003468 case POWER_SUPPLY_PROP_RESISTANCE:
3469 val->intval = get_prop_bms_batt_resistance(chip);
3470 break;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07003471 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
3472 val->intval = get_prop_bms_charge_counter(chip);
3473 break;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003474 case POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW:
3475 val->intval = get_prop_bms_charge_counter_shadow(chip);
3476 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003477 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
3478 val->intval = get_prop_bms_charge_full_design(chip);
3479 break;
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05303480 case POWER_SUPPLY_PROP_CHARGE_FULL:
3481 val->intval = get_prop_bms_charge_full(chip);
3482 break;
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05303483 case POWER_SUPPLY_PROP_CYCLE_COUNT:
3484 val->intval = chip->charge_cycles;
3485 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003486 default:
3487 return -EINVAL;
3488 }
3489 return 0;
3490}
3491
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003492#define OCV_USE_LIMIT_EN BIT(7)
3493static int set_ocv_voltage_thresholds(struct qpnp_bms_chip *chip,
3494 int low_voltage_threshold,
3495 int high_voltage_threshold)
3496{
3497 uint16_t low_voltage_raw, high_voltage_raw;
3498 int rc;
3499
3500 low_voltage_raw = convert_vbatt_uv_to_raw(chip,
3501 low_voltage_threshold);
3502 high_voltage_raw = convert_vbatt_uv_to_raw(chip,
3503 high_voltage_threshold);
3504 rc = qpnp_write_wrapper(chip, (u8 *)&low_voltage_raw,
3505 chip->base + BMS1_OCV_USE_LOW_LIMIT_THR0, 2);
3506 if (rc) {
3507 pr_err("Failed to set ocv low voltage threshold: %d\n", rc);
3508 return rc;
3509 }
3510 rc = qpnp_write_wrapper(chip, (u8 *)&high_voltage_raw,
3511 chip->base + BMS1_OCV_USE_HIGH_LIMIT_THR0, 2);
3512 if (rc) {
3513 pr_err("Failed to set ocv high voltage threshold: %d\n", rc);
3514 return rc;
3515 }
3516 rc = qpnp_masked_write(chip, BMS1_OCV_USE_LIMIT_CTL,
3517 OCV_USE_LIMIT_EN, OCV_USE_LIMIT_EN);
3518 if (rc) {
3519 pr_err("Failed to enabled ocv voltage thresholds: %d\n", rc);
3520 return rc;
3521 }
3522 pr_debug("ocv low threshold set to %d uv or 0x%x raw\n",
3523 low_voltage_threshold, low_voltage_raw);
3524 pr_debug("ocv high threshold set to %d uv or 0x%x raw\n",
3525 high_voltage_threshold, high_voltage_raw);
3526 return 0;
3527}
3528
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003529static int read_shutdown_iavg_ma(struct qpnp_bms_chip *chip)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003530{
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003531 u8 iavg;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003532 int rc;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003533
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003534 rc = qpnp_read_wrapper(chip, &iavg, chip->base + IAVG_STORAGE_REG, 1);
3535 if (rc) {
3536 pr_err("failed to read addr = %d %d assuming %d\n",
3537 chip->base + IAVG_STORAGE_REG, rc,
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003538 MIN_IAVG_MA);
3539 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003540 } else if (iavg == IAVG_INVALID) {
3541 pr_err("invalid iavg read from BMS1_DATA_REG_1, using %d\n",
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003542 MIN_IAVG_MA);
3543 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003544 } else {
3545 if (iavg == 0)
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003546 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003547 else
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003548 return MIN_IAVG_MA + IAVG_STEP_SIZE_MA * iavg;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003549 }
3550}
3551
3552static int read_shutdown_soc(struct qpnp_bms_chip *chip)
3553{
3554 u8 stored_soc;
3555 int rc, shutdown_soc;
3556
3557 /*
3558 * The previous SOC is stored in the first 7 bits of the register as
3559 * (Shutdown SOC + 1). This allows for register reset values of both
3560 * 0x00 and 0x7F.
3561 */
3562 rc = qpnp_read_wrapper(chip, &stored_soc, chip->soc_storage_addr, 1);
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003563 if (rc) {
3564 pr_err("failed to read addr = %d %d\n",
3565 chip->soc_storage_addr, rc);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003566 return SOC_INVALID;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003567 }
3568
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003569 if ((stored_soc >> 1) > 0)
3570 shutdown_soc = (stored_soc >> 1) - 1;
3571 else
3572 shutdown_soc = SOC_INVALID;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003573
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003574 pr_debug("stored soc = 0x%02x, shutdown_soc = %d\n",
3575 stored_soc, shutdown_soc);
3576 return shutdown_soc;
3577}
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003578
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003579#define BAT_REMOVED_OFFMODE_BIT BIT(6)
3580static bool is_battery_replaced_in_offmode(struct qpnp_bms_chip *chip)
3581{
3582 u8 batt_pres;
3583 int rc;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003584
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003585 if (chip->batt_pres_addr) {
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003586 rc = qpnp_read_wrapper(chip, &batt_pres,
3587 chip->batt_pres_addr, 1);
3588 pr_debug("offmode removed: %02x\n", batt_pres);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003589 if (!rc && (batt_pres & BAT_REMOVED_OFFMODE_BIT))
3590 return true;
3591 }
3592 return false;
3593}
3594
3595static void load_shutdown_data(struct qpnp_bms_chip *chip)
3596{
3597 int calculated_soc, shutdown_soc;
3598 bool invalid_stored_soc;
3599 bool offmode_battery_replaced;
3600 bool shutdown_soc_out_of_limit;
3601
3602 /*
3603 * Read the saved shutdown SoC from the configured register and
3604 * check if the value has been reset
3605 */
3606 shutdown_soc = read_shutdown_soc(chip);
3607 invalid_stored_soc = (shutdown_soc == SOC_INVALID);
3608
3609 /*
3610 * Do a quick run of SoC calculation to find whether the shutdown soc
3611 * is close enough.
3612 */
Xiaozhe Shi2c171172013-12-03 13:27:37 -08003613 chip->shutdown_iavg_ma = MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003614 calculated_soc = recalculate_raw_soc(chip);
3615 shutdown_soc_out_of_limit = (abs(shutdown_soc - calculated_soc)
3616 > chip->shutdown_soc_valid_limit);
3617 pr_debug("calculated_soc = %d, valid_limit = %d\n",
3618 calculated_soc, chip->shutdown_soc_valid_limit);
3619
3620 /*
3621 * Check if the battery has been replaced while the system was powered
3622 * down.
3623 */
3624 offmode_battery_replaced = is_battery_replaced_in_offmode(chip);
3625
3626 /* Invalidate the shutdown SoC if any of these conditions hold true */
3627 if (chip->ignore_shutdown_soc
3628 || invalid_stored_soc
3629 || offmode_battery_replaced
3630 || shutdown_soc_out_of_limit) {
3631 chip->battery_removed = true;
3632 chip->shutdown_soc_invalid = true;
Xiaozhe Shic92cfd92013-10-25 11:36:42 -07003633 chip->shutdown_iavg_ma = MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003634 pr_debug("Ignoring shutdown SoC: invalid = %d, offmode = %d, out_of_limit = %d\n",
3635 invalid_stored_soc, offmode_battery_replaced,
3636 shutdown_soc_out_of_limit);
3637 } else {
3638 chip->shutdown_iavg_ma = read_shutdown_iavg_ma(chip);
3639 chip->shutdown_soc = shutdown_soc;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07003640 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303641
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003642 pr_debug("raw_soc = %d shutdown_soc = %d shutdown_iavg = %d shutdown_soc_invalid = %d, battery_removed = %d\n",
3643 calculated_soc,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003644 chip->shutdown_soc,
3645 chip->shutdown_iavg_ma,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303646 chip->shutdown_soc_invalid,
3647 chip->battery_removed);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003648}
3649
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003650static irqreturn_t bms_ocv_thr_irq_handler(int irq, void *_chip)
3651{
3652 struct qpnp_bms_chip *chip = _chip;
3653
3654 pr_debug("ocv_thr irq triggered\n");
3655 bms_stay_awake(&chip->soc_wake_source);
3656 schedule_work(&chip->recalc_work);
3657 return IRQ_HANDLED;
3658}
3659
3660static irqreturn_t bms_sw_cc_thr_irq_handler(int irq, void *_chip)
3661{
3662 struct qpnp_bms_chip *chip = _chip;
3663
3664 pr_debug("sw_cc_thr irq triggered\n");
Anirudh Ghayal1166eef2013-12-23 19:05:33 +05303665 disable_bms_irq_nosync(&chip->sw_cc_thr_irq);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003666 bms_stay_awake(&chip->soc_wake_source);
3667 schedule_work(&chip->recalc_work);
3668 return IRQ_HANDLED;
3669}
3670
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003671static int64_t read_battery_id(struct qpnp_bms_chip *chip)
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003672{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003673 int rc;
3674 struct qpnp_vadc_result result;
3675
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003676 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX2_BAT_ID, &result);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003677 if (rc) {
3678 pr_err("error reading batt id channel = %d, rc = %d\n",
3679 LR_MUX2_BAT_ID, rc);
3680 return rc;
3681 }
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003682
3683 return result.physical;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003684}
3685
3686static int set_battery_data(struct qpnp_bms_chip *chip)
3687{
3688 int64_t battery_id;
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003689 int rc = 0, dt_data = false;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003690 struct bms_battery_data *batt_data;
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003691 struct device_node *node;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003692
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003693 if (chip->batt_type == BATT_DESAY) {
3694 batt_data = &desay_5200_data;
3695 } else if (chip->batt_type == BATT_PALLADIUM) {
3696 batt_data = &palladium_1500_data;
3697 } else if (chip->batt_type == BATT_OEM) {
3698 batt_data = &oem_batt_data;
Wu Fenglin2ac88aa2013-04-25 12:43:40 +08003699 } else if (chip->batt_type == BATT_QRD_4V35_2000MAH) {
3700 batt_data = &QRD_4v35_2000mAh_data;
tingtingf50326f2013-06-05 15:07:24 +08003701 } else if (chip->batt_type == BATT_QRD_4V2_1300MAH) {
3702 batt_data = &qrd_4v2_1300mah_data;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003703 } else {
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003704 battery_id = read_battery_id(chip);
3705 if (battery_id < 0) {
3706 pr_err("cannot read battery id err = %lld\n",
3707 battery_id);
3708 return battery_id;
3709 }
3710
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003711 node = of_find_node_by_name(chip->spmi->dev.of_node,
3712 "qcom,battery-data");
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003713 if (!node) {
3714 pr_warn("No available batterydata, using palladium 1500\n");
3715 batt_data = &palladium_1500_data;
3716 goto assign_data;
3717 }
3718 batt_data = devm_kzalloc(chip->dev,
3719 sizeof(struct bms_battery_data), GFP_KERNEL);
3720 if (!batt_data) {
3721 pr_err("Could not alloc battery data\n");
3722 batt_data = &palladium_1500_data;
3723 goto assign_data;
3724 }
3725 batt_data->fcc_temp_lut = devm_kzalloc(chip->dev,
3726 sizeof(struct single_row_lut),
3727 GFP_KERNEL);
3728 batt_data->pc_temp_ocv_lut = devm_kzalloc(chip->dev,
3729 sizeof(struct pc_temp_ocv_lut),
3730 GFP_KERNEL);
3731 batt_data->rbatt_sf_lut = devm_kzalloc(chip->dev,
3732 sizeof(struct sf_lut),
3733 GFP_KERNEL);
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003734
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003735 batt_data->max_voltage_uv = -1;
3736 batt_data->cutoff_uv = -1;
3737 batt_data->iterm_ua = -1;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003738
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003739 /*
3740 * if the alloced luts are 0s, of_batterydata_read_data ignores
3741 * them.
3742 */
3743 rc = of_batterydata_read_data(node, batt_data, battery_id);
3744 if (rc == 0 && batt_data->fcc_temp_lut
3745 && batt_data->pc_temp_ocv_lut
3746 && batt_data->rbatt_sf_lut) {
3747 dt_data = true;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003748 } else {
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003749 pr_err("battery data load failed, using palladium 1500\n");
3750 devm_kfree(chip->dev, batt_data->fcc_temp_lut);
3751 devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut);
3752 devm_kfree(chip->dev, batt_data->rbatt_sf_lut);
3753 devm_kfree(chip->dev, batt_data);
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003754 batt_data = &palladium_1500_data;
3755 }
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003756 }
3757
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003758assign_data:
Xiaozhe Shi976618f2013-04-30 10:49:30 -07003759 chip->fcc_mah = batt_data->fcc;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003760 chip->fcc_temp_lut = batt_data->fcc_temp_lut;
3761 chip->fcc_sf_lut = batt_data->fcc_sf_lut;
3762 chip->pc_temp_ocv_lut = batt_data->pc_temp_ocv_lut;
3763 chip->pc_sf_lut = batt_data->pc_sf_lut;
3764 chip->rbatt_sf_lut = batt_data->rbatt_sf_lut;
3765 chip->default_rbatt_mohm = batt_data->default_rbatt_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07003766 chip->rbatt_capacitive_mohm = batt_data->rbatt_capacitive_mohm;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07003767 chip->flat_ocv_threshold_uv = batt_data->flat_ocv_threshold_uv;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003768
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003769 /* Override battery properties if specified in the battery profile */
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003770 if (batt_data->max_voltage_uv >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003771 chip->max_voltage_uv = batt_data->max_voltage_uv;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003772 if (batt_data->cutoff_uv >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003773 chip->v_cutoff_uv = batt_data->cutoff_uv;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003774 if (batt_data->iterm_ua >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003775 chip->chg_term_ua = batt_data->iterm_ua;
3776
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003777 if (chip->pc_temp_ocv_lut == NULL) {
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003778 pr_err("temp ocv lut table has not been loaded\n");
3779 if (dt_data) {
3780 devm_kfree(chip->dev, batt_data->fcc_temp_lut);
3781 devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut);
3782 devm_kfree(chip->dev, batt_data->rbatt_sf_lut);
3783 devm_kfree(chip->dev, batt_data);
3784 }
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003785 return -EINVAL;
3786 }
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003787
3788 if (dt_data)
3789 devm_kfree(chip->dev, batt_data);
3790
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003791 return 0;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003792}
3793
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07003794static int bms_get_adc(struct qpnp_bms_chip *chip,
3795 struct spmi_device *spmi)
3796{
3797 int rc = 0;
3798
3799 chip->vadc_dev = qpnp_get_vadc(&spmi->dev, "bms");
3800 if (IS_ERR(chip->vadc_dev)) {
3801 rc = PTR_ERR(chip->vadc_dev);
3802 if (rc != -EPROBE_DEFER)
3803 pr_err("vadc property missing, rc=%d\n", rc);
3804 return rc;
3805 }
3806
3807 chip->iadc_dev = qpnp_get_iadc(&spmi->dev, "bms");
3808 if (IS_ERR(chip->iadc_dev)) {
3809 rc = PTR_ERR(chip->iadc_dev);
3810 if (rc != -EPROBE_DEFER)
3811 pr_err("iadc property missing, rc=%d\n", rc);
3812 return rc;
3813 }
3814
3815 chip->adc_tm_dev = qpnp_get_adc_tm(&spmi->dev, "bms");
3816 if (IS_ERR(chip->adc_tm_dev)) {
3817 rc = PTR_ERR(chip->adc_tm_dev);
3818 if (rc != -EPROBE_DEFER)
3819 pr_err("adc-tm not ready, defer probe\n");
3820 return rc;
3821 }
3822
3823 return 0;
3824}
3825
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003826#define SPMI_PROP_READ(chip_prop, qpnp_spmi_property, retval) \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003827do { \
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003828 if (retval) \
3829 break; \
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003830 retval = of_property_read_u32(chip->spmi->dev.of_node, \
Xiaozhe Shi9bd24622013-01-23 15:54:54 -08003831 "qcom," qpnp_spmi_property, \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003832 &chip->chip_prop); \
3833 if (retval) { \
3834 pr_err("Error reading " #qpnp_spmi_property \
3835 " property %d\n", rc); \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003836 } \
3837} while (0)
3838
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303839#define SPMI_PROP_READ_BOOL(chip_prop, qpnp_spmi_property) \
3840do { \
3841 chip->chip_prop = of_property_read_bool(chip->spmi->dev.of_node,\
3842 "qcom," qpnp_spmi_property); \
3843} while (0)
3844
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003845static inline int bms_read_properties(struct qpnp_bms_chip *chip)
3846{
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003847 int rc = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003848
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003849 SPMI_PROP_READ(r_sense_uohm, "r-sense-uohm", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003850 SPMI_PROP_READ(v_cutoff_uv, "v-cutoff-uv", rc);
3851 SPMI_PROP_READ(max_voltage_uv, "max-voltage-uv", rc);
3852 SPMI_PROP_READ(r_conn_mohm, "r-conn-mohm", rc);
3853 SPMI_PROP_READ(chg_term_ua, "chg-term-ua", rc);
3854 SPMI_PROP_READ(shutdown_soc_valid_limit,
3855 "shutdown-soc-valid-limit", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003856 SPMI_PROP_READ(adjust_soc_low_threshold,
3857 "adjust-soc-low-threshold", rc);
3858 SPMI_PROP_READ(batt_type, "batt-type", rc);
3859 SPMI_PROP_READ(low_soc_calc_threshold,
3860 "low-soc-calculate-soc-threshold", rc);
3861 SPMI_PROP_READ(low_soc_calculate_soc_ms,
3862 "low-soc-calculate-soc-ms", rc);
Xiaozhe Shicb487b12013-10-14 17:42:07 -07003863 SPMI_PROP_READ(low_voltage_calculate_soc_ms,
3864 "low-voltage-calculate-soc-ms", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003865 SPMI_PROP_READ(calculate_soc_ms, "calculate-soc-ms", rc);
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07003866 SPMI_PROP_READ(high_ocv_correction_limit_uv,
3867 "high-ocv-correction-limit-uv", rc);
3868 SPMI_PROP_READ(low_ocv_correction_limit_uv,
3869 "low-ocv-correction-limit-uv", rc);
3870 SPMI_PROP_READ(hold_soc_est,
3871 "hold-soc-est", rc);
3872 SPMI_PROP_READ(ocv_high_threshold_uv,
3873 "ocv-voltage-high-threshold-uv", rc);
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003874 SPMI_PROP_READ(ocv_low_threshold_uv,
3875 "ocv-voltage-low-threshold-uv", rc);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08003876 SPMI_PROP_READ(low_voltage_threshold, "low-voltage-threshold", rc);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003877 SPMI_PROP_READ(temperature_margin, "tm-temp-margin", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003878
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003879 chip->use_external_rsense = of_property_read_bool(
3880 chip->spmi->dev.of_node,
3881 "qcom,use-external-rsense");
3882 chip->ignore_shutdown_soc = of_property_read_bool(
3883 chip->spmi->dev.of_node,
3884 "qcom,ignore-shutdown-soc");
3885 chip->use_voltage_soc = of_property_read_bool(chip->spmi->dev.of_node,
3886 "qcom,use-voltage-soc");
3887 chip->use_ocv_thresholds = of_property_read_bool(
3888 chip->spmi->dev.of_node,
3889 "qcom,use-ocv-thresholds");
3890
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003891 if (chip->adjust_soc_low_threshold >= 45)
3892 chip->adjust_soc_low_threshold = 45;
3893
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303894 SPMI_PROP_READ_BOOL(enable_fcc_learning, "enable-fcc-learning");
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303895 if (chip->enable_fcc_learning) {
3896 SPMI_PROP_READ(min_fcc_learning_soc,
3897 "min-fcc-learning-soc", rc);
3898 SPMI_PROP_READ(min_fcc_ocv_pc,
3899 "min-fcc-ocv-pc", rc);
3900 SPMI_PROP_READ(min_fcc_learning_samples,
3901 "min-fcc-learning-samples", rc);
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303902 SPMI_PROP_READ(fcc_resolution,
3903 "fcc-resolution", rc);
3904 if (chip->min_fcc_learning_samples > MAX_FCC_CYCLES)
3905 chip->min_fcc_learning_samples = MAX_FCC_CYCLES;
3906 chip->fcc_learning_samples = devm_kzalloc(&chip->spmi->dev,
3907 (sizeof(struct fcc_sample) *
3908 chip->min_fcc_learning_samples), GFP_KERNEL);
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003909 if (chip->fcc_learning_samples == NULL)
3910 return -ENOMEM;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303911 pr_debug("min-fcc-soc=%d, min-fcc-pc=%d, min-fcc-cycles=%d\n",
3912 chip->min_fcc_learning_soc, chip->min_fcc_ocv_pc,
3913 chip->min_fcc_learning_samples);
3914 }
3915
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003916 if (rc) {
3917 pr_err("Missing required properties.\n");
3918 return rc;
3919 }
3920
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003921 pr_debug("dts data: r_sense_uohm:%d, v_cutoff_uv:%d, max_v:%d\n",
3922 chip->r_sense_uohm, chip->v_cutoff_uv,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003923 chip->max_voltage_uv);
3924 pr_debug("r_conn:%d, shutdown_soc: %d, adjust_soc_low:%d\n",
3925 chip->r_conn_mohm, chip->shutdown_soc_valid_limit,
3926 chip->adjust_soc_low_threshold);
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07003927 pr_debug("chg_term_ua:%d, batt_type:%d\n",
3928 chip->chg_term_ua,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003929 chip->batt_type);
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08003930 pr_debug("ignore_shutdown_soc:%d, use_voltage_soc:%d\n",
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08003931 chip->ignore_shutdown_soc, chip->use_voltage_soc);
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003932 pr_debug("use external rsense: %d\n", chip->use_external_rsense);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003933 return 0;
3934}
3935
3936static inline void bms_initialize_constants(struct qpnp_bms_chip *chip)
3937{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003938 chip->prev_pc_unusable = -EINVAL;
3939 chip->soc_at_cv = -EINVAL;
3940 chip->calculated_soc = -EINVAL;
Xiaozhe Shie118c692012-09-24 15:17:43 -07003941 chip->last_soc = -EINVAL;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07003942 chip->last_soc_est = -EINVAL;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003943 chip->battery_present = -EINVAL;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003944 chip->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08003945 chip->last_cc_uah = INT_MIN;
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08003946 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
3947 chip->prev_last_good_ocv_raw = OCV_RAW_UNINITIALIZED;
Xiaozhe Shie118c692012-09-24 15:17:43 -07003948 chip->first_time_calc_soc = 1;
3949 chip->first_time_calc_uuc = 1;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003950}
3951
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003952#define SPMI_FIND_IRQ(chip, irq_name) \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003953do { \
3954 chip->irq_name##_irq.irq = spmi_get_irq_byname(chip->spmi, \
3955 resource, #irq_name); \
3956 if (chip->irq_name##_irq.irq < 0) { \
3957 pr_err("Unable to get " #irq_name " irq\n"); \
3958 return -ENXIO; \
3959 } \
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003960} while (0)
3961
3962static int bms_find_irqs(struct qpnp_bms_chip *chip,
3963 struct spmi_resource *resource)
3964{
3965 SPMI_FIND_IRQ(chip, sw_cc_thr);
3966 SPMI_FIND_IRQ(chip, ocv_thr);
3967 return 0;
3968}
3969
3970#define SPMI_REQUEST_IRQ(chip, rc, irq_name) \
3971do { \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003972 rc = devm_request_irq(chip->dev, chip->irq_name##_irq.irq, \
3973 bms_##irq_name##_irq_handler, \
3974 IRQF_TRIGGER_RISING, #irq_name, chip); \
3975 if (rc < 0) { \
3976 pr_err("Unable to request " #irq_name " irq: %d\n", rc);\
3977 return -ENXIO; \
3978 } \
Xiaozhe Shif511a6e2014-02-20 14:37:18 -08003979 chip->irq_name##_irq.ready = true; \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003980} while (0)
3981
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003982static int bms_request_irqs(struct qpnp_bms_chip *chip)
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003983{
3984 int rc;
3985
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003986 SPMI_REQUEST_IRQ(chip, rc, sw_cc_thr);
Fenglin Wuc17e97b2015-01-20 16:49:58 +08003987 chip->sw_cc_thr_irq.is_wake = true;
Abhijeet Dharmapurikardce21e62013-08-07 15:42:32 -07003988 disable_bms_irq(&chip->sw_cc_thr_irq);
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003989 SPMI_REQUEST_IRQ(chip, rc, ocv_thr);
Fenglin Wuc17e97b2015-01-20 16:49:58 +08003990 chip->ocv_thr_irq.is_wake = true;
Abhijeet Dharmapurikardce21e62013-08-07 15:42:32 -07003991 disable_bms_irq(&chip->ocv_thr_irq);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003992 return 0;
3993}
3994
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003995#define REG_OFFSET_PERP_TYPE 0x04
3996#define REG_OFFSET_PERP_SUBTYPE 0x05
3997#define BMS_BMS_TYPE 0xD
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003998#define BMS_BMS1_SUBTYPE 0x1
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003999#define BMS_IADC_TYPE 0x8
Xiaozhe Shief6274c2013-03-06 15:23:52 -08004000#define BMS_IADC1_SUBTYPE 0x3
4001#define BMS_IADC2_SUBTYPE 0x5
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004002
4003static int register_spmi(struct qpnp_bms_chip *chip, struct spmi_device *spmi)
4004{
4005 struct spmi_resource *spmi_resource;
4006 struct resource *resource;
4007 int rc;
4008 u8 type, subtype;
4009
4010 chip->dev = &(spmi->dev);
4011 chip->spmi = spmi;
4012
4013 spmi_for_each_container_dev(spmi_resource, spmi) {
4014 if (!spmi_resource) {
4015 pr_err("qpnp_bms: spmi resource absent\n");
4016 return -ENXIO;
4017 }
4018
4019 resource = spmi_get_resource(spmi, spmi_resource,
4020 IORESOURCE_MEM, 0);
4021 if (!(resource && resource->start)) {
4022 pr_err("node %s IO resource absent!\n",
4023 spmi->dev.of_node->full_name);
4024 return -ENXIO;
4025 }
4026
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07004027 pr_debug("Node name = %s\n", spmi_resource->of_node->name);
4028
4029 if (strcmp("qcom,batt-pres-status",
4030 spmi_resource->of_node->name) == 0) {
4031 chip->batt_pres_addr = resource->start;
4032 continue;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07004033 } else if (strcmp("qcom,soc-storage-reg",
4034 spmi_resource->of_node->name) == 0) {
4035 chip->soc_storage_addr = resource->start;
4036 continue;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07004037 }
4038
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004039 rc = qpnp_read_wrapper(chip, &type,
4040 resource->start + REG_OFFSET_PERP_TYPE, 1);
4041 if (rc) {
4042 pr_err("Peripheral type read failed rc=%d\n", rc);
4043 return rc;
4044 }
4045 rc = qpnp_read_wrapper(chip, &subtype,
4046 resource->start + REG_OFFSET_PERP_SUBTYPE, 1);
4047 if (rc) {
4048 pr_err("Peripheral subtype read failed rc=%d\n", rc);
4049 return rc;
4050 }
4051
Xiaozhe Shief6274c2013-03-06 15:23:52 -08004052 if (type == BMS_BMS_TYPE && subtype == BMS_BMS1_SUBTYPE) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004053 chip->base = resource->start;
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07004054 rc = bms_find_irqs(chip, spmi_resource);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004055 if (rc) {
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07004056 pr_err("Could not find irqs\n");
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004057 return rc;
4058 }
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004059 } else if (type == BMS_IADC_TYPE
Xiaozhe Shief6274c2013-03-06 15:23:52 -08004060 && (subtype == BMS_IADC1_SUBTYPE
4061 || subtype == BMS_IADC2_SUBTYPE)) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004062 chip->iadc_base = resource->start;
4063 } else {
4064 pr_err("Invalid peripheral start=0x%x type=0x%x, subtype=0x%x\n",
4065 resource->start, type, subtype);
4066 }
4067 }
4068
4069 if (chip->base == 0) {
4070 dev_err(&spmi->dev, "BMS peripheral was not registered\n");
4071 return -EINVAL;
4072 }
4073 if (chip->iadc_base == 0) {
4074 dev_err(&spmi->dev, "BMS_IADC peripheral was not registered\n");
4075 return -EINVAL;
4076 }
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07004077 if (chip->soc_storage_addr == 0) {
4078 /* default to dvdd backed BMS data reg0 */
4079 chip->soc_storage_addr = chip->base + SOC_STORAGE_REG;
4080 }
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004081
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07004082 pr_debug("bms-base = 0x%04x, iadc-base = 0x%04x, bat-pres-reg = 0x%04x, soc-storage-reg = 0x%04x\n",
4083 chip->base, chip->iadc_base,
4084 chip->batt_pres_addr, chip->soc_storage_addr);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004085 return 0;
4086}
4087
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004088#define ADC_CH_SEL_MASK 0x7
4089#define ADC_INT_RSNSN_CTL_MASK 0x3
4090#define ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE 0x2
4091#define FAST_AVG_EN_MASK 0x80
4092#define FAST_AVG_EN_VALUE_EXT_RSENSE 0x80
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004093static int read_iadc_channel_select(struct qpnp_bms_chip *chip)
4094{
4095 u8 iadc_channel_select;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004096 int32_t rds_rsense_nohm;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004097 int rc;
4098
4099 rc = qpnp_read_wrapper(chip, &iadc_channel_select,
4100 chip->iadc_base + IADC1_BMS_ADC_CH_SEL_CTL, 1);
4101 if (rc) {
4102 pr_err("Error reading bms_iadc channel register %d\n", rc);
4103 return rc;
4104 }
4105
4106 iadc_channel_select &= ADC_CH_SEL_MASK;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004107 if (iadc_channel_select != EXTERNAL_RSENSE
4108 && iadc_channel_select != INTERNAL_RSENSE) {
4109 pr_err("IADC1_BMS_IADC configured incorrectly. Selected channel = %d\n",
4110 iadc_channel_select);
4111 return -EINVAL;
4112 }
4113
4114 if (chip->use_external_rsense) {
4115 pr_debug("External rsense selected\n");
4116 if (iadc_channel_select == INTERNAL_RSENSE) {
4117 pr_debug("Internal rsense detected; Changing rsense to external\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004118 rc = qpnp_masked_write_iadc(chip,
4119 IADC1_BMS_ADC_CH_SEL_CTL,
4120 ADC_CH_SEL_MASK,
4121 EXTERNAL_RSENSE);
4122 if (rc) {
4123 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
4124 IADC1_BMS_ADC_CH_SEL_CTL,
4125 EXTERNAL_RSENSE, rc);
4126 return rc;
4127 }
Xiaozhe Shif3da8622013-06-10 14:50:56 -07004128 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07004129 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07004130 chip->software_shdw_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004131 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004132 } else {
4133 pr_debug("Internal rsense selected\n");
4134 if (iadc_channel_select == EXTERNAL_RSENSE) {
4135 pr_debug("External rsense detected; Changing rsense to internal\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004136 rc = qpnp_masked_write_iadc(chip,
4137 IADC1_BMS_ADC_CH_SEL_CTL,
4138 ADC_CH_SEL_MASK,
4139 INTERNAL_RSENSE);
4140 if (rc) {
4141 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
4142 IADC1_BMS_ADC_CH_SEL_CTL,
4143 INTERNAL_RSENSE, rc);
4144 return rc;
4145 }
Xiaozhe Shif3da8622013-06-10 14:50:56 -07004146 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
4147 chip->software_shdw_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004148 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004149
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07004150 rc = qpnp_iadc_get_rsense(chip->iadc_dev, &rds_rsense_nohm);
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004151 if (rc) {
4152 pr_err("Unable to read RDS resistance value from IADC; rc = %d\n",
4153 rc);
4154 return rc;
4155 }
Xiaozhe Shid0a79542012-11-06 10:00:38 -08004156 chip->r_sense_uohm = rds_rsense_nohm/1000;
4157 pr_debug("rds_rsense = %d nOhm, saved as %d uOhm\n",
4158 rds_rsense_nohm, chip->r_sense_uohm);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004159 }
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004160 /* prevent shorting of leads by IADC_BMS when external Rsense is used */
4161 if (chip->use_external_rsense) {
4162 if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX) {
4163 rc = qpnp_masked_write_iadc(chip,
4164 IADC1_BMS_ADC_INT_RSNSN_CTL,
4165 ADC_INT_RSNSN_CTL_MASK,
4166 ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE);
4167 if (rc) {
4168 pr_err("Unable to set batfet config %x to %x: %d\n",
4169 IADC1_BMS_ADC_INT_RSNSN_CTL,
4170 ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE, rc);
4171 return rc;
4172 }
4173 } else {
4174 /* In older PMICS use FAST_AVG_EN register bit 7 */
4175 rc = qpnp_masked_write_iadc(chip,
4176 IADC1_BMS_FAST_AVG_EN,
4177 FAST_AVG_EN_MASK,
4178 FAST_AVG_EN_VALUE_EXT_RSENSE);
4179 if (rc) {
4180 pr_err("Unable to set batfet config %x to %x: %d\n",
4181 IADC1_BMS_FAST_AVG_EN,
4182 FAST_AVG_EN_VALUE_EXT_RSENSE, rc);
4183 return rc;
4184 }
4185 }
4186 }
4187
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004188 return 0;
4189}
4190
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004191static int refresh_die_temp_monitor(struct qpnp_bms_chip *chip)
4192{
4193 struct qpnp_vadc_result result;
4194 int rc;
4195
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07004196 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004197
4198 pr_debug("low = %lld, high = %lld\n",
4199 result.physical - chip->temperature_margin,
4200 result.physical + chip->temperature_margin);
4201 chip->die_temp_monitor_params.high_temp = result.physical
4202 + chip->temperature_margin;
4203 chip->die_temp_monitor_params.low_temp = result.physical
4204 - chip->temperature_margin;
4205 chip->die_temp_monitor_params.state_request =
4206 ADC_TM_HIGH_LOW_THR_ENABLE;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004207 return qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
4208 &chip->die_temp_monitor_params);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004209}
4210
4211static void btm_notify_die_temp(enum qpnp_tm_state state, void *ctx)
4212{
4213 struct qpnp_bms_chip *chip = ctx;
4214 struct qpnp_vadc_result result;
4215 int rc;
4216
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07004217 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004218
4219 if (state == ADC_TM_LOW_STATE)
4220 pr_debug("low state triggered\n");
4221 else if (state == ADC_TM_HIGH_STATE)
4222 pr_debug("high state triggered\n");
4223 pr_debug("die temp = %lld, raw = 0x%x\n",
4224 result.physical, result.adc_code);
4225 schedule_work(&chip->recalc_work);
4226 refresh_die_temp_monitor(chip);
4227}
4228
4229static int setup_die_temp_monitoring(struct qpnp_bms_chip *chip)
4230{
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004231 int rc;
4232
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004233 chip->die_temp_monitor_params.channel = DIE_TEMP;
4234 chip->die_temp_monitor_params.btm_ctx = (void *)chip;
4235 chip->die_temp_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
4236 chip->die_temp_monitor_params.threshold_notification =
4237 &btm_notify_die_temp;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004238 rc = refresh_die_temp_monitor(chip);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004239 if (rc) {
4240 pr_err("tm setup failed: %d\n", rc);
4241 return rc;
4242 }
4243 pr_debug("setup complete\n");
4244 return 0;
4245}
4246
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004247static int __devinit qpnp_bms_probe(struct spmi_device *spmi)
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004248{
4249 struct qpnp_bms_chip *chip;
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004250 bool warm_reset;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004251 int rc, vbatt;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004252
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07004253 chip = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_bms_chip),
4254 GFP_KERNEL);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004255
4256 if (chip == NULL) {
4257 pr_err("kzalloc() failed.\n");
4258 return -ENOMEM;
4259 }
4260
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004261 rc = bms_get_adc(chip, spmi);
4262 if (rc < 0)
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004263 goto error_read;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004264
Xiaozhe Shi3eaf0b62013-07-11 09:48:08 -07004265 mutex_init(&chip->bms_output_lock);
4266 mutex_init(&chip->last_ocv_uv_mutex);
4267 mutex_init(&chip->vbat_monitor_mutex);
4268 mutex_init(&chip->soc_invalidation_mutex);
4269 mutex_init(&chip->last_soc_mutex);
Xiaozhe Shibda84992013-09-05 10:39:11 -07004270 mutex_init(&chip->status_lock);
Xiaozhe Shi27375822013-08-22 11:40:15 -07004271 init_waitqueue_head(&chip->bms_wait_queue);
Xiaozhe Shi3eaf0b62013-07-11 09:48:08 -07004272
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004273 warm_reset = qpnp_pon_is_warm_reset();
4274 rc = warm_reset;
4275 if (rc < 0)
4276 goto error_read;
4277
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004278 rc = register_spmi(chip, spmi);
4279 if (rc) {
4280 pr_err("error registering spmi resource %d\n", rc);
4281 goto error_resource;
4282 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004283
4284 rc = qpnp_read_wrapper(chip, &chip->revision1,
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004285 chip->base + REVISION1, 1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004286 if (rc) {
4287 pr_err("error reading version register %d\n", rc);
4288 goto error_read;
4289 }
4290
4291 rc = qpnp_read_wrapper(chip, &chip->revision2,
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004292 chip->base + REVISION2, 1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004293 if (rc) {
4294 pr_err("Error reading version register %d\n", rc);
4295 goto error_read;
4296 }
Xiaozhe Shia045a562012-11-28 16:55:39 -08004297 pr_debug("BMS version: %hhu.%hhu\n", chip->revision2, chip->revision1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004298
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004299 rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision2,
4300 chip->iadc_base + REVISION2, 1);
4301 if (rc) {
4302 pr_err("Error reading version register %d\n", rc);
4303 goto error_read;
4304 }
4305
4306 rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision1,
4307 chip->iadc_base + REVISION1, 1);
4308 if (rc) {
4309 pr_err("Error reading version register %d\n", rc);
4310 goto error_read;
4311 }
4312 pr_debug("IADC_BMS version: %hhu.%hhu\n",
4313 chip->iadc_bms_revision2, chip->iadc_bms_revision1);
4314
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004315 rc = bms_read_properties(chip);
4316 if (rc) {
4317 pr_err("Unable to read all bms properties, rc = %d\n", rc);
4318 goto error_read;
4319 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004320
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004321 rc = read_iadc_channel_select(chip);
4322 if (rc) {
4323 pr_err("Unable to get iadc selected channel = %d\n", rc);
4324 goto error_read;
4325 }
4326
Xiaozhe Shibdf14742012-12-05 12:41:48 -08004327 if (chip->use_ocv_thresholds) {
4328 rc = set_ocv_voltage_thresholds(chip,
4329 chip->ocv_low_threshold_uv,
4330 chip->ocv_high_threshold_uv);
4331 if (rc) {
4332 pr_err("Could not set ocv voltage thresholds: %d\n",
4333 rc);
4334 goto error_read;
4335 }
4336 }
4337
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004338 rc = set_battery_data(chip);
4339 if (rc) {
4340 pr_err("Bad battery data %d\n", rc);
4341 goto error_read;
4342 }
4343
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004344 bms_initialize_constants(chip);
4345
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004346 wakeup_source_init(&chip->soc_wake_source.source, "qpnp_soc_wake");
Xiaozhe Shi4be85782013-02-22 17:33:40 -08004347 wake_lock_init(&chip->low_voltage_wake_lock, WAKE_LOCK_SUSPEND,
4348 "qpnp_low_voltage_lock");
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004349 wake_lock_init(&chip->cv_wake_lock, WAKE_LOCK_SUSPEND,
4350 "qpnp_cv_lock");
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004351 INIT_DELAYED_WORK(&chip->calculate_soc_delayed_work,
4352 calculate_soc_work);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08004353 INIT_WORK(&chip->recalc_work, recalculate_work);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07004354 INIT_WORK(&chip->batfet_open_work, batfet_open_work);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004355
Xiaozhe Shif9f99242013-08-29 12:27:50 -07004356 dev_set_drvdata(&spmi->dev, chip);
4357 device_init_wakeup(&spmi->dev, 1);
4358
4359 load_shutdown_data(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004360
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05304361 if (chip->enable_fcc_learning) {
Anirudh Ghayale0c02932013-07-08 16:26:35 +05304362 if (chip->battery_removed) {
4363 rc = discard_backup_fcc_data(chip);
4364 if (rc)
4365 pr_err("Could not discard backed-up FCC data\n");
4366 } else {
4367 rc = read_chgcycle_data_from_backup(chip);
4368 if (rc)
4369 pr_err("Unable to restore charge-cycle data\n");
4370
4371 rc = read_fcc_data_from_backup(chip);
4372 if (rc)
4373 pr_err("Unable to restore FCC-learning data\n");
4374 else
4375 attempt_learning_new_fcc(chip);
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05304376 }
4377 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05304378
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004379 rc = setup_vbat_monitoring(chip);
4380 if (rc < 0) {
4381 pr_err("failed to set up voltage notifications: %d\n", rc);
4382 goto error_setup;
Xiaozhe Shid5d21412013-02-06 17:14:41 -08004383 }
4384
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004385 rc = setup_die_temp_monitoring(chip);
4386 if (rc < 0) {
4387 pr_err("failed to set up die temp notifications: %d\n", rc);
4388 goto error_setup;
4389 }
4390
Xu Kai870f8e82014-01-16 19:21:01 +08004391 rc = bms_request_irqs(chip);
4392 if (rc) {
4393 pr_err("error requesting bms irqs, rc = %d\n", rc);
4394 goto error_setup;
4395 }
4396
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07004397 battery_insertion_check(chip);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07004398 batfet_status_check(chip);
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07004399 battery_status_check(chip);
4400
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004401 calculate_soc_work(&(chip->calculate_soc_delayed_work.work));
4402
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004403 /* setup & register the battery power supply */
4404 chip->bms_psy.name = "bms";
4405 chip->bms_psy.type = POWER_SUPPLY_TYPE_BMS;
4406 chip->bms_psy.properties = msm_bms_power_props;
4407 chip->bms_psy.num_properties = ARRAY_SIZE(msm_bms_power_props);
4408 chip->bms_psy.get_property = qpnp_bms_power_get_property;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004409 chip->bms_psy.external_power_changed =
4410 qpnp_bms_external_power_changed;
4411 chip->bms_psy.supplied_to = qpnp_bms_supplicants;
4412 chip->bms_psy.num_supplicants = ARRAY_SIZE(qpnp_bms_supplicants);
4413
4414 rc = power_supply_register(chip->dev, &chip->bms_psy);
4415
4416 if (rc < 0) {
4417 pr_err("power_supply_register bms failed rc = %d\n", rc);
4418 goto unregister_dc;
4419 }
4420
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07004421 chip->bms_psy_registered = true;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004422 vbatt = 0;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07004423 rc = get_battery_voltage(chip, &vbatt);
Xiaozhe Shi36458962013-02-06 16:19:57 -08004424 if (rc) {
4425 pr_err("error reading vbat_sns adc channel = %d, rc = %d\n",
4426 VBAT_SNS, rc);
4427 goto unregister_dc;
4428 }
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004429
wangxlf0e6b1f2014-12-31 09:51:40 +08004430#ifdef CONFIG_TCMD
4431 bms_chip = chip;
4432#endif
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004433 pr_info("probe success: soc =%d vbatt = %d ocv = %d r_sense_uohm = %u warm_reset = %d\n",
4434 get_prop_bms_capacity(chip), vbatt, chip->last_ocv_uv,
4435 chip->r_sense_uohm, warm_reset);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004436 return 0;
4437
4438unregister_dc:
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07004439 chip->bms_psy_registered = false;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004440 power_supply_unregister(&chip->bms_psy);
4441error_setup:
4442 dev_set_drvdata(&spmi->dev, NULL);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004443 wakeup_source_trash(&chip->soc_wake_source.source);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08004444 wake_lock_destroy(&chip->low_voltage_wake_lock);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004445 wake_lock_destroy(&chip->cv_wake_lock);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004446error_resource:
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004447error_read:
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004448 return rc;
4449}
4450
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07004451static int qpnp_bms_remove(struct spmi_device *spmi)
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004452{
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004453 dev_set_drvdata(&spmi->dev, NULL);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004454 return 0;
4455}
4456
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004457static int bms_suspend(struct device *dev)
4458{
4459 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
4460
4461 cancel_delayed_work_sync(&chip->calculate_soc_delayed_work);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07004462 chip->was_charging_at_sleep = is_battery_charging(chip);
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004463 return 0;
4464}
4465
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004466static int bms_resume(struct device *dev)
4467{
4468 int rc;
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004469 int soc_calc_period;
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004470 int time_until_next_recalc = 0;
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004471 unsigned long time_since_last_recalc;
4472 unsigned long tm_now_sec;
4473 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
4474
4475 rc = get_current_time(&tm_now_sec);
4476 if (rc) {
4477 pr_err("Could not read current time: %d\n", rc);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004478 } else {
Xiaozhe Shicb487b12013-10-14 17:42:07 -07004479 soc_calc_period = get_calculation_delay_ms(chip);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004480 time_since_last_recalc = tm_now_sec - chip->last_recalc_time;
4481 pr_debug("Time since last recalc: %lu\n",
4482 time_since_last_recalc);
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004483 time_until_next_recalc = max(0, soc_calc_period
4484 - (int)(time_since_last_recalc * 1000));
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004485 }
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004486
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004487 if (time_until_next_recalc == 0)
4488 bms_stay_awake(&chip->soc_wake_source);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004489 schedule_delayed_work(&chip->calculate_soc_delayed_work,
4490 round_jiffies_relative(msecs_to_jiffies
4491 (time_until_next_recalc)));
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004492 return 0;
4493}
4494
4495static const struct dev_pm_ops qpnp_bms_pm_ops = {
4496 .resume = bms_resume,
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004497 .suspend = bms_suspend,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004498};
4499
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004500static struct spmi_driver qpnp_bms_driver = {
4501 .probe = qpnp_bms_probe,
4502 .remove = __devexit_p(qpnp_bms_remove),
4503 .driver = {
4504 .name = QPNP_BMS_DEV_NAME,
4505 .owner = THIS_MODULE,
4506 .of_match_table = qpnp_bms_match_table,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004507 .pm = &qpnp_bms_pm_ops,
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004508 },
4509};
4510
4511static int __init qpnp_bms_init(void)
4512{
4513 pr_info("QPNP BMS INIT\n");
4514 return spmi_driver_register(&qpnp_bms_driver);
4515}
4516
4517static void __exit qpnp_bms_exit(void)
4518{
4519 pr_info("QPNP BMS EXIT\n");
4520 return spmi_driver_unregister(&qpnp_bms_driver);
4521}
4522
4523module_init(qpnp_bms_init);
4524module_exit(qpnp_bms_exit);
4525
4526MODULE_DESCRIPTION("QPNP BMS Driver");
4527MODULE_LICENSE("GPL v2");
4528MODULE_ALIAS("platform:" QPNP_BMS_DEV_NAME);