blob: 537dc1d20f18cb9ca220a636144a370bfd27588a [file] [log] [blame]
Xiaozhe Shi72a72f22013-12-26 13:54:29 -08001/* Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
Xiaozhe Shi73a65692012-09-18 17:51:57 -070013#define pr_fmt(fmt) "BMS: %s: " fmt, __func__
Xiaozhe Shib19f7032012-08-16 12:14:16 -070014
15#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/init.h>
18#include <linux/slab.h>
19#include <linux/err.h>
20#include <linux/of.h>
21#include <linux/of_device.h>
22#include <linux/power_supply.h>
23#include <linux/spmi.h>
Xiaozhe Shie118c692012-09-24 15:17:43 -070024#include <linux/rtc.h>
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -070025#include <linux/delay.h>
Xiaozhe Shi27375822013-08-22 11:40:15 -070026#include <linux/sched.h>
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070027#include <linux/qpnp/qpnp-adc.h>
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -070028#include <linux/qpnp/power-on.h>
Xiaozhe Shiaf203c22013-06-19 12:01:38 -070029#include <linux/of_batterydata.h>
Xiaozhe Shib19f7032012-08-16 12:14:16 -070030
Xiaozhe Shib19f7032012-08-16 12:14:16 -070031/* BMS Register Offsets */
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -070032#define REVISION1 0x0
33#define REVISION2 0x1
Xiaozhe Shib19f7032012-08-16 12:14:16 -070034#define BMS1_STATUS1 0x8
35#define BMS1_MODE_CTL 0X40
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070036/* Coulomb counter clear registers */
Xiaozhe Shib19f7032012-08-16 12:14:16 -070037#define BMS1_CC_DATA_CTL 0x42
Xiaozhe Shia045a562012-11-28 16:55:39 -080038#define BMS1_CC_CLEAR_CTL 0x43
Xiaozhe Shi20640b52013-01-03 11:49:30 -080039/* BMS Tolerances */
40#define BMS1_TOL_CTL 0X44
Xiaozhe Shib19f7032012-08-16 12:14:16 -070041/* OCV limit registers */
42#define BMS1_OCV_USE_LOW_LIMIT_THR0 0x48
43#define BMS1_OCV_USE_LOW_LIMIT_THR1 0x49
44#define BMS1_OCV_USE_HIGH_LIMIT_THR0 0x4A
45#define BMS1_OCV_USE_HIGH_LIMIT_THR1 0x4B
46#define BMS1_OCV_USE_LIMIT_CTL 0x4C
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -070047/* Delay control */
48#define BMS1_S1_DELAY_CTL 0x5A
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -080049/* OCV interrupt threshold */
50#define BMS1_OCV_THR0 0x50
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -070051#define BMS1_S2_SAMP_AVG_CTL 0x61
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -080052/* SW CC interrupt threshold */
53#define BMS1_SW_CC_THR0 0xA0
Xiaozhe Shib19f7032012-08-16 12:14:16 -070054/* OCV for r registers */
55#define BMS1_OCV_FOR_R_DATA0 0x80
Xiaozhe Shib19f7032012-08-16 12:14:16 -070056#define BMS1_VSENSE_FOR_R_DATA0 0x82
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070057/* Coulomb counter data */
Xiaozhe Shib19f7032012-08-16 12:14:16 -070058#define BMS1_CC_DATA0 0x8A
Xiaozhe Shif3da8622013-06-10 14:50:56 -070059/* Shadow Coulomb counter data */
60#define BMS1_SW_CC_DATA0 0xA8
Xiaozhe Shib19f7032012-08-16 12:14:16 -070061/* OCV for soc data */
62#define BMS1_OCV_FOR_SOC_DATA0 0x90
Xiaozhe Shib19f7032012-08-16 12:14:16 -070063#define BMS1_VSENSE_PON_DATA0 0x94
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070064#define BMS1_VSENSE_AVG_DATA0 0x98
Xiaozhe Shib19f7032012-08-16 12:14:16 -070065#define BMS1_VBAT_AVG_DATA0 0x9E
Xiaozhe Shib19f7032012-08-16 12:14:16 -070066/* Extra bms registers */
Xiaozhe Shi57058942013-03-27 16:54:54 -070067#define SOC_STORAGE_REG 0xB0
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070068#define IAVG_STORAGE_REG 0xB1
Anirudh Ghayale0c02932013-07-08 16:26:35 +053069#define BMS_FCC_COUNT 0xB2
70#define BMS_FCC_BASE_REG 0xB3 /* FCC updates - 0xB3 to 0xB7 */
71#define BMS_CHGCYL_BASE_REG 0xB8 /* FCC chgcyl - 0xB8 to 0xBC */
72#define CHARGE_INCREASE_STORAGE 0xBD
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053073#define CHARGE_CYCLE_STORAGE_LSB 0xBE /* LSB=0xBE, MSB=0xBF */
74
Xiaozhe Shic40b3972012-11-30 14:11:16 -080075/* IADC Channel Select */
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -070076#define IADC1_BMS_REVISION2 0x01
Xiaozhe Shic40b3972012-11-30 14:11:16 -080077#define IADC1_BMS_ADC_CH_SEL_CTL 0x48
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -070078#define IADC1_BMS_ADC_INT_RSNSN_CTL 0x49
79#define IADC1_BMS_FAST_AVG_EN 0x5B
Xiaozhe Shib19f7032012-08-16 12:14:16 -070080
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070081/* Configuration for saving of shutdown soc/iavg */
82#define IGNORE_SOC_TEMP_DECIDEG 50
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -070083#define IAVG_STEP_SIZE_MA 10
Xiaozhe Shif5f966d2013-02-19 14:23:11 -080084#define IAVG_INVALID 0xFF
Xiaozhe Shif9f99242013-08-29 12:27:50 -070085#define SOC_INVALID 0x7E
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070086
Xiaozhe Shie118c692012-09-24 15:17:43 -070087#define IAVG_SAMPLES 16
88
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053089/* FCC learning constants */
Anirudh Ghayale0c02932013-07-08 16:26:35 +053090#define MAX_FCC_CYCLES 5
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053091#define DELTA_FCC_PERCENT 5
92#define VALID_FCC_CHGCYL_RANGE 50
Anirudh Ghayale0c02932013-07-08 16:26:35 +053093#define CHGCYL_RESOLUTION 20
94#define FCC_DEFAULT_TEMP 250
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053095
Xiaozhe Shib19f7032012-08-16 12:14:16 -070096#define QPNP_BMS_DEV_NAME "qcom,qpnp-bms"
97
Xiaozhe Shif3da8622013-06-10 14:50:56 -070098enum {
99 SHDW_CC,
100 CC
101};
102
103enum {
104 NORESET,
105 RESET
106};
107
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700108struct soc_params {
109 int fcc_uah;
110 int cc_uah;
Xiaozhe Shi904f1f72012-12-04 12:47:21 -0800111 int rbatt_mohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700112 int iavg_ua;
113 int uuc_uah;
114 int ocv_charge_uah;
Xiaozhe Shif36d2862013-01-04 10:17:35 -0800115 int delta_time_s;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700116};
117
118struct raw_soc_params {
119 uint16_t last_good_ocv_raw;
120 int64_t cc;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700121 int64_t shdw_cc;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700122 int last_good_ocv_uv;
123};
124
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530125struct fcc_sample {
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530126 int fcc_new;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530127 int chargecycles;
128};
129
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800130struct bms_irq {
131 unsigned int irq;
132 unsigned long disabled;
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800133 bool ready;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800134};
135
136struct bms_wakeup_source {
137 struct wakeup_source source;
138 unsigned long disabled;
139};
140
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700141struct qpnp_bms_chip {
142 struct device *dev;
143 struct power_supply bms_psy;
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -0700144 bool bms_psy_registered;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -0700145 struct power_supply *batt_psy;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700146 struct spmi_device *spmi;
Xiaozhe Shi27375822013-08-22 11:40:15 -0700147 wait_queue_head_t bms_wait_queue;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700148 u16 base;
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800149 u16 iadc_base;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -0700150 u16 batt_pres_addr;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -0700151 u16 soc_storage_addr;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700152
153 u8 revision1;
154 u8 revision2;
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -0700155
156 u8 iadc_bms_revision1;
157 u8 iadc_bms_revision2;
158
Xiaozhe Shid5d21412013-02-06 17:14:41 -0800159 int battery_present;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700160 int battery_status;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700161 bool batfet_closed;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800162 bool new_battery;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700163 bool done_charging;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800164 bool last_soc_invalid;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700165 /* platform data */
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -0800166 int r_sense_uohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700167 unsigned int v_cutoff_uv;
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -0800168 int max_voltage_uv;
169 int r_conn_mohm;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700170 int shutdown_soc_valid_limit;
171 int adjust_soc_low_threshold;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700172 int chg_term_ua;
Xiaozhe Shi73a65692012-09-18 17:51:57 -0700173 enum battery_type batt_type;
Xiaozhe Shi976618f2013-04-30 10:49:30 -0700174 unsigned int fcc_mah;
Xiaozhe Shi73a65692012-09-18 17:51:57 -0700175 struct single_row_lut *fcc_temp_lut;
176 struct single_row_lut *fcc_sf_lut;
177 struct pc_temp_ocv_lut *pc_temp_ocv_lut;
178 struct sf_lut *pc_sf_lut;
179 struct sf_lut *rbatt_sf_lut;
180 int default_rbatt_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -0700181 int rbatt_capacitive_mohm;
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -0700182 int rbatt_mohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700183
184 struct delayed_work calculate_soc_delayed_work;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800185 struct work_struct recalc_work;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700186 struct work_struct batfet_open_work;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700187
188 struct mutex bms_output_lock;
189 struct mutex last_ocv_uv_mutex;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700190 struct mutex vbat_monitor_mutex;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700191 struct mutex soc_invalidation_mutex;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700192 struct mutex last_soc_mutex;
Xiaozhe Shibda84992013-09-05 10:39:11 -0700193 struct mutex status_lock;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700194
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800195 bool use_external_rsense;
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800196 bool use_ocv_thresholds;
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800197
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700198 bool ignore_shutdown_soc;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800199 bool shutdown_soc_invalid;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700200 int shutdown_soc;
201 int shutdown_iavg_ma;
202
Xiaozhe Shi4be85782013-02-22 17:33:40 -0800203 struct wake_lock low_voltage_wake_lock;
Xiaozhe Shi4be85782013-02-22 17:33:40 -0800204 int low_voltage_threshold;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700205 int low_soc_calc_threshold;
206 int low_soc_calculate_soc_ms;
Xiaozhe Shicb487b12013-10-14 17:42:07 -0700207 int low_voltage_calculate_soc_ms;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700208 int calculate_soc_ms;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800209 struct bms_wakeup_source soc_wake_source;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700210 struct wake_lock cv_wake_lock;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700211
Xiaozhe Shie118c692012-09-24 15:17:43 -0700212 uint16_t ocv_reading_at_100;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700213 uint16_t prev_last_good_ocv_raw;
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700214 int insertion_ocv_uv;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700215 int last_ocv_uv;
Xiaozhe Shicc48e992013-05-28 16:42:24 -0700216 int charging_adjusted_ocv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800217 int last_ocv_temp;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700218 int last_cc_uah;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700219 unsigned long last_soc_change_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700220 unsigned long tm_sec;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700221 unsigned long report_tm_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700222 bool first_time_calc_soc;
223 bool first_time_calc_uuc;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700224 int64_t software_cc_uah;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700225 int64_t software_shdw_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700226
227 int iavg_samples_ma[IAVG_SAMPLES];
228 int iavg_index;
229 int iavg_num_samples;
230 struct timespec t_soc_queried;
231 int last_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -0700232 int last_soc_est;
Xiaozhe Shicc137262013-03-10 06:21:41 -0700233 int last_soc_unbound;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700234 bool was_charging_at_sleep;
235 int charge_start_tm_sec;
236 int catch_up_time_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700237 struct single_row_lut *adjusted_fcc_temp_lut;
238
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700239 struct qpnp_adc_tm_btm_param vbat_monitor_params;
Xiaozhe Shi535494d2013-04-05 12:27:51 -0700240 struct qpnp_adc_tm_btm_param die_temp_monitor_params;
241 int temperature_margin;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700242 unsigned int vadc_v0625;
243 unsigned int vadc_v1250;
244
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -0700245 int system_load_count;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700246 int prev_uuc_iavg_ma;
247 int prev_pc_unusable;
248 int ibat_at_cv_ua;
249 int soc_at_cv;
250 int prev_chg_soc;
251 int calculated_soc;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -0800252 int prev_voltage_based_soc;
253 bool use_voltage_soc;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700254 bool in_cv_range;
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800255
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800256 int prev_batt_terminal_uv;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -0700257 int high_ocv_correction_limit_uv;
258 int low_ocv_correction_limit_uv;
259 int flat_ocv_threshold_uv;
260 int hold_soc_est;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800261
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800262 int ocv_high_threshold_uv;
263 int ocv_low_threshold_uv;
Xiaozhe Shicdeee312012-12-18 15:10:18 -0800264 unsigned long last_recalc_time;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530265
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530266 struct fcc_sample *fcc_learning_samples;
267 u8 fcc_sample_count;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530268 int enable_fcc_learning;
269 int min_fcc_learning_soc;
270 int min_fcc_ocv_pc;
271 int min_fcc_learning_samples;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530272 int start_soc;
273 int end_soc;
274 int start_pc;
275 int start_cc_uah;
276 int start_real_soc;
277 int end_cc_uah;
278 uint16_t fcc_new_mah;
279 int fcc_new_batt_temp;
280 uint16_t charge_cycles;
281 u8 charge_increase;
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530282 int fcc_resolution;
283 bool battery_removed;
Zhenhua Huang95a05d32014-03-31 18:09:45 +0800284 bool in_taper_charge;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800285 struct bms_irq sw_cc_thr_irq;
286 struct bms_irq ocv_thr_irq;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700287 struct qpnp_vadc_chip *vadc_dev;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700288 struct qpnp_iadc_chip *iadc_dev;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -0700289 struct qpnp_adc_tm_chip *adc_tm_dev;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700290};
291
292static struct of_device_id qpnp_bms_match_table[] = {
293 { .compatible = QPNP_BMS_DEV_NAME },
294 {}
295};
296
297static char *qpnp_bms_supplicants[] = {
298 "battery"
299};
300
301static enum power_supply_property msm_bms_power_props[] = {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700302 POWER_SUPPLY_PROP_CAPACITY,
Xiaozhe Shibda84992013-09-05 10:39:11 -0700303 POWER_SUPPLY_PROP_STATUS,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700304 POWER_SUPPLY_PROP_CURRENT_NOW,
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -0700305 POWER_SUPPLY_PROP_RESISTANCE,
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -0700306 POWER_SUPPLY_PROP_CHARGE_COUNTER,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700307 POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700308 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +0530309 POWER_SUPPLY_PROP_CHARGE_FULL,
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +0530310 POWER_SUPPLY_PROP_CYCLE_COUNT,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700311};
312
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530313static int discard_backup_fcc_data(struct qpnp_bms_chip *chip);
314static void backup_charge_cycle(struct qpnp_bms_chip *chip);
315
Xiaozhe Shi20640b52013-01-03 11:49:30 -0800316static bool bms_reset;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -0800317
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700318static int qpnp_read_wrapper(struct qpnp_bms_chip *chip, u8 *val,
319 u16 base, int count)
320{
321 int rc;
322 struct spmi_device *spmi = chip->spmi;
323
324 rc = spmi_ext_register_readl(spmi->ctrl, spmi->sid, base, val, count);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700325 if (rc) {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700326 pr_err("SPMI read failed rc=%d\n", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700327 return rc;
328 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700329 return 0;
330}
331
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700332static int qpnp_write_wrapper(struct qpnp_bms_chip *chip, u8 *val,
333 u16 base, int count)
334{
335 int rc;
336 struct spmi_device *spmi = chip->spmi;
337
338 rc = spmi_ext_register_writel(spmi->ctrl, spmi->sid, base, val, count);
339 if (rc) {
340 pr_err("SPMI write failed rc=%d\n", rc);
341 return rc;
342 }
343 return 0;
344}
345
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800346static int qpnp_masked_write_base(struct qpnp_bms_chip *chip, u16 addr,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700347 u8 mask, u8 val)
348{
349 int rc;
350 u8 reg;
351
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800352 rc = qpnp_read_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700353 if (rc) {
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800354 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700355 return rc;
356 }
357 reg &= ~mask;
358 reg |= val & mask;
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800359 rc = qpnp_write_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700360 if (rc) {
361 pr_err("write failed addr = %03X, val = %02x, mask = %02x, reg = %02x, rc = %d\n",
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800362 addr, val, mask, reg, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700363 return rc;
364 }
365 return 0;
366}
367
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800368static int qpnp_masked_write_iadc(struct qpnp_bms_chip *chip, u16 addr,
369 u8 mask, u8 val)
370{
371 return qpnp_masked_write_base(chip, chip->iadc_base + addr, mask, val);
372}
373
374static int qpnp_masked_write(struct qpnp_bms_chip *chip, u16 addr,
375 u8 mask, u8 val)
376{
377 return qpnp_masked_write_base(chip, chip->base + addr, mask, val);
378}
379
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800380static void bms_stay_awake(struct bms_wakeup_source *source)
381{
382 if (__test_and_clear_bit(0, &source->disabled)) {
383 __pm_stay_awake(&source->source);
384 pr_debug("enabled source %s\n", source->source.name);
385 }
386}
387
388static void bms_relax(struct bms_wakeup_source *source)
389{
390 if (!__test_and_set_bit(0, &source->disabled)) {
391 __pm_relax(&source->source);
392 pr_debug("disabled source %s\n", source->source.name);
393 }
394}
395
396static void enable_bms_irq(struct bms_irq *irq)
397{
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800398 if (irq->ready && __test_and_clear_bit(0, &irq->disabled)) {
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800399 enable_irq(irq->irq);
400 pr_debug("enabled irq %d\n", irq->irq);
401 }
402}
403
404static void disable_bms_irq(struct bms_irq *irq)
405{
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800406 if (irq->ready && !__test_and_set_bit(0, &irq->disabled)) {
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800407 disable_irq(irq->irq);
408 pr_debug("disabled irq %d\n", irq->irq);
409 }
410}
411
Anirudh Ghayal1166eef2013-12-23 19:05:33 +0530412static void disable_bms_irq_nosync(struct bms_irq *irq)
413{
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800414 if (irq->ready && !__test_and_set_bit(0, &irq->disabled)) {
Anirudh Ghayal1166eef2013-12-23 19:05:33 +0530415 disable_irq_nosync(irq->irq);
416 pr_debug("disabled irq %d\n", irq->irq);
417 }
418}
419
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700420#define HOLD_OREG_DATA BIT(0)
421static int lock_output_data(struct qpnp_bms_chip *chip)
422{
423 int rc;
424
425 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL,
426 HOLD_OREG_DATA, HOLD_OREG_DATA);
427 if (rc) {
428 pr_err("couldnt lock bms output rc = %d\n", rc);
429 return rc;
430 }
Xiaozhe Shied4a5522014-09-05 14:56:13 -0700431 /*
432 * Sleep for at least 60 microseconds here to make sure there has
433 * been at least two cycles of the sleep clock so that the registers
434 * are correctly locked.
435 */
436 usleep_range(60, 2000);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700437 return 0;
438}
439
440static int unlock_output_data(struct qpnp_bms_chip *chip)
441{
442 int rc;
443
444 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL, HOLD_OREG_DATA, 0);
445 if (rc) {
446 pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc);
447 return rc;
448 }
449 return 0;
450}
451
452#define V_PER_BIT_MUL_FACTOR 97656
453#define V_PER_BIT_DIV_FACTOR 1000
454#define VADC_INTRINSIC_OFFSET 0x6000
455
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800456static int vadc_reading_to_uv(int reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700457{
458 if (reading <= VADC_INTRINSIC_OFFSET)
459 return 0;
460
461 return (reading - VADC_INTRINSIC_OFFSET)
462 * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR;
463}
464
465#define VADC_CALIB_UV 625000
466#define VBATT_MUL_FACTOR 3
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800467static int adjust_vbatt_reading(struct qpnp_bms_chip *chip, int reading_uv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700468{
469 s64 numerator, denominator;
470
471 if (reading_uv == 0)
472 return 0;
473
474 /* don't adjust if not calibrated */
475 if (chip->vadc_v0625 == 0 || chip->vadc_v1250 == 0) {
476 pr_debug("No cal yet return %d\n",
477 VBATT_MUL_FACTOR * reading_uv);
478 return VBATT_MUL_FACTOR * reading_uv;
479 }
480
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700481 numerator = ((s64)reading_uv - chip->vadc_v0625) * VADC_CALIB_UV;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700482 denominator = (s64)chip->vadc_v1250 - chip->vadc_v0625;
483 if (denominator == 0)
484 return reading_uv * VBATT_MUL_FACTOR;
485 return (VADC_CALIB_UV + div_s64(numerator, denominator))
486 * VBATT_MUL_FACTOR;
487}
488
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800489static int convert_vbatt_uv_to_raw(struct qpnp_bms_chip *chip,
490 int unadjusted_vbatt)
491{
492 int scaled_vbatt = unadjusted_vbatt / VBATT_MUL_FACTOR;
493
494 if (scaled_vbatt <= 0)
495 return VADC_INTRINSIC_OFFSET;
496 return ((scaled_vbatt * V_PER_BIT_DIV_FACTOR) / V_PER_BIT_MUL_FACTOR)
497 + VADC_INTRINSIC_OFFSET;
498}
499
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700500static inline int convert_vbatt_raw_to_uv(struct qpnp_bms_chip *chip,
Xiaozhe Shi80754222013-10-30 14:11:41 -0700501 uint16_t reading, bool is_pon_ocv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700502{
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700503 int64_t uv;
504 int rc;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700505
506 uv = vadc_reading_to_uv(reading);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700507 pr_debug("%u raw converted into %lld uv\n", reading, uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700508 uv = adjust_vbatt_reading(chip, uv);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700509 pr_debug("adjusted into %lld uv\n", uv);
Xiaozhe Shi80754222013-10-30 14:11:41 -0700510 rc = qpnp_vbat_sns_comp_result(chip->vadc_dev, &uv, is_pon_ocv);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700511 if (rc)
512 pr_debug("could not compensate vbatt\n");
513 pr_debug("compensated into %lld uv\n", uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700514 return uv;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700515}
516
517#define CC_READING_RESOLUTION_N 542535
518#define CC_READING_RESOLUTION_D 100000
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -0700519static s64 cc_reading_to_uv(s64 reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700520{
521 return div_s64(reading * CC_READING_RESOLUTION_N,
522 CC_READING_RESOLUTION_D);
523}
524
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800525#define QPNP_ADC_GAIN_IDEAL 3291LL
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700526static s64 cc_adjust_for_gain(s64 uv, uint16_t gain)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700527{
528 s64 result_uv;
529
530 pr_debug("adjusting_uv = %lld\n", uv);
Xiaozhe Shi820a47a2012-11-27 13:23:27 -0800531 if (gain == 0) {
532 pr_debug("gain is %d, not adjusting\n", gain);
533 return uv;
534 }
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700535 pr_debug("adjusting by factor: %lld/%hu = %lld%%\n",
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800536 QPNP_ADC_GAIN_IDEAL, gain,
537 div_s64(QPNP_ADC_GAIN_IDEAL * 100LL, (s64)gain));
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700538
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800539 result_uv = div_s64(uv * QPNP_ADC_GAIN_IDEAL, (s64)gain);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700540 pr_debug("result_uv = %lld\n", result_uv);
541 return result_uv;
542}
543
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700544static s64 cc_reverse_adjust_for_gain(struct qpnp_bms_chip *chip, s64 uv)
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800545{
546 struct qpnp_iadc_calib calibration;
547 int gain;
548 s64 result_uv;
549
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700550 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800551 gain = (int)calibration.gain_raw - (int)calibration.offset_raw;
552
553 pr_debug("reverse adjusting_uv = %lld\n", uv);
554 if (gain == 0) {
555 pr_debug("gain is %d, not adjusting\n", gain);
556 return uv;
557 }
558 pr_debug("adjusting by factor: %hu/%lld = %lld%%\n",
559 gain, QPNP_ADC_GAIN_IDEAL,
560 div64_s64((s64)gain * 100LL,
561 (s64)QPNP_ADC_GAIN_IDEAL));
562
563 result_uv = div64_s64(uv * (s64)gain, QPNP_ADC_GAIN_IDEAL);
564 pr_debug("result_uv = %lld\n", result_uv);
565 return result_uv;
566}
567
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700568static int convert_vsense_to_uv(struct qpnp_bms_chip *chip,
569 int16_t reading)
570{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700571 struct qpnp_iadc_calib calibration;
572
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700573 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700574 return cc_adjust_for_gain(cc_reading_to_uv(reading),
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800575 calibration.gain_raw - calibration.offset_raw);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700576}
577
578static int read_vsense_avg(struct qpnp_bms_chip *chip, int *result_uv)
579{
580 int rc;
581 int16_t reading;
582
583 rc = qpnp_read_wrapper(chip, (u8 *)&reading,
584 chip->base + BMS1_VSENSE_AVG_DATA0, 2);
585
586 if (rc) {
587 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
588 return rc;
589 }
590
591 *result_uv = convert_vsense_to_uv(chip, reading);
592 return 0;
593}
594
595static int get_battery_current(struct qpnp_bms_chip *chip, int *result_ua)
596{
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700597 int rc, vsense_uv = 0;
598 int64_t temp_current;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700599
Xiaozhe Shid0a79542012-11-06 10:00:38 -0800600 if (chip->r_sense_uohm == 0) {
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700601 pr_err("r_sense is zero\n");
602 return -EINVAL;
603 }
604
605 mutex_lock(&chip->bms_output_lock);
606 lock_output_data(chip);
607 read_vsense_avg(chip, &vsense_uv);
608 unlock_output_data(chip);
609 mutex_unlock(&chip->bms_output_lock);
610
611 pr_debug("vsense_uv=%duV\n", vsense_uv);
612 /* cast for signed division */
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700613 temp_current = div_s64((vsense_uv * 1000000LL),
614 (int)chip->r_sense_uohm);
615
Xiaozhe Shi4c8458a2013-11-26 13:00:56 -0800616 *result_ua = temp_current;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700617 rc = qpnp_iadc_comp_result(chip->iadc_dev, &temp_current);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700618 if (rc)
619 pr_debug("error compensation failed: %d\n", rc);
620
Xiaozhe Shi4c8458a2013-11-26 13:00:56 -0800621 pr_debug("%d uA err compensated ibat=%llduA\n",
622 *result_ua, temp_current);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700623 *result_ua = temp_current;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700624 return 0;
625}
626
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700627static int get_battery_voltage(struct qpnp_bms_chip *chip, int *result_uv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700628{
629 int rc;
630 struct qpnp_vadc_result adc_result;
631
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700632 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &adc_result);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700633 if (rc) {
634 pr_err("error reading adc channel = %d, rc = %d\n",
635 VBAT_SNS, rc);
636 return rc;
637 }
638 pr_debug("mvolts phy = %lld meas = 0x%llx\n", adc_result.physical,
639 adc_result.measurement);
640 *result_uv = (int)adc_result.physical;
641 return 0;
642}
643
Xiaozhe Shie118c692012-09-24 15:17:43 -0700644#define CC_36_BIT_MASK 0xFFFFFFFFFLL
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800645static uint64_t convert_s64_to_s36(int64_t raw64)
646{
647 return (uint64_t) raw64 & CC_36_BIT_MASK;
648}
649
650#define SIGN_EXTEND_36_TO_64_MASK (-1LL ^ CC_36_BIT_MASK)
651static int64_t convert_s36_to_s64(uint64_t raw36)
652{
653 raw36 = raw36 & CC_36_BIT_MASK;
654 /* convert 36 bit signed value into 64 signed value */
655 return (raw36 >> 35) == 0LL ?
656 raw36 : (SIGN_EXTEND_36_TO_64_MASK | raw36);
657}
658
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700659static int read_cc_raw(struct qpnp_bms_chip *chip, int64_t *reading,
660 int cc_type)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700661{
662 int64_t raw_reading;
663 int rc;
664
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700665 if (cc_type == SHDW_CC)
666 rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
667 chip->base + BMS1_SW_CC_DATA0, 5);
668 else
669 rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
670 chip->base + BMS1_CC_DATA0, 5);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700671 if (rc) {
672 pr_err("Error reading cc: rc = %d\n", rc);
673 return -ENXIO;
674 }
675
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800676 *reading = convert_s36_to_s64(raw_reading);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700677
678 return 0;
679}
680
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700681static int calib_vadc(struct qpnp_bms_chip *chip)
682{
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700683 int rc, raw_0625, raw_1250;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700684 struct qpnp_vadc_result result;
685
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700686 rc = qpnp_vadc_read(chip->vadc_dev, REF_625MV, &result);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700687 if (rc) {
688 pr_debug("vadc read failed with rc = %d\n", rc);
689 return rc;
690 }
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700691 raw_0625 = result.adc_code;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700692
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700693 rc = qpnp_vadc_read(chip->vadc_dev, REF_125V, &result);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700694 if (rc) {
695 pr_debug("vadc read failed with rc = %d\n", rc);
696 return rc;
697 }
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700698 raw_1250 = result.adc_code;
699 chip->vadc_v0625 = vadc_reading_to_uv(raw_0625);
700 chip->vadc_v1250 = vadc_reading_to_uv(raw_1250);
701 pr_debug("vadc calib: 0625 = %d raw (%d uv), 1250 = %d raw (%d uv)\n",
702 raw_0625, chip->vadc_v0625,
703 raw_1250, chip->vadc_v1250);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700704 return 0;
705}
706
Xiaozhe Shie118c692012-09-24 15:17:43 -0700707static void convert_and_store_ocv(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800708 struct raw_soc_params *raw,
Xiaozhe Shi80754222013-10-30 14:11:41 -0700709 int batt_temp, bool is_pon_ocv)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700710{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700711 int rc;
712
713 pr_debug("prev_last_good_ocv_raw = %d, last_good_ocv_raw = %d\n",
714 chip->prev_last_good_ocv_raw,
715 raw->last_good_ocv_raw);
716 rc = calib_vadc(chip);
717 if (rc)
718 pr_err("Vadc reference voltage read failed, rc = %d\n", rc);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700719 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
720 raw->last_good_ocv_uv = convert_vbatt_raw_to_uv(chip,
Xiaozhe Shi80754222013-10-30 14:11:41 -0700721 raw->last_good_ocv_raw, is_pon_ocv);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700722 chip->last_ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800723 chip->last_ocv_temp = batt_temp;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700724 chip->software_cc_uah = 0;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700725 pr_debug("last_good_ocv_uv = %d\n", raw->last_good_ocv_uv);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700726}
727
Xiaozhe Shia045a562012-11-28 16:55:39 -0800728#define CLEAR_CC BIT(7)
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700729#define CLEAR_SHDW_CC BIT(6)
Xiaozhe Shia045a562012-11-28 16:55:39 -0800730/**
731 * reset both cc and sw-cc.
732 * note: this should only be ever called from one thread
733 * or there may be a race condition where CC is never enabled
734 * again
735 */
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700736static void reset_cc(struct qpnp_bms_chip *chip, u8 flags)
Xiaozhe Shia045a562012-11-28 16:55:39 -0800737{
738 int rc;
739
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700740 pr_debug("resetting cc manually with flags %hhu\n", flags);
741 mutex_lock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800742 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700743 flags,
744 flags);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800745 if (rc)
746 pr_err("cc reset failed: %d\n", rc);
747
748 /* wait for 100us for cc to reset */
749 udelay(100);
750
751 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700752 flags, 0);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800753 if (rc)
754 pr_err("cc reenable failed: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700755 mutex_unlock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800756}
757
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700758static int get_battery_status(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800759{
760 union power_supply_propval ret = {0,};
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800761 int rc;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800762
763 if (chip->batt_psy == NULL)
764 chip->batt_psy = power_supply_get_by_name("battery");
765 if (chip->batt_psy) {
766 /* if battery has been registered, use the status property */
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800767 rc = chip->batt_psy->get_property(chip->batt_psy,
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800768 POWER_SUPPLY_PROP_STATUS, &ret);
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800769 if (rc) {
770 pr_debug("Battery does not export status: %d\n", rc);
771 return POWER_SUPPLY_STATUS_UNKNOWN;
772 }
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700773 return ret.intval;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800774 }
775
776 /* Default to false if the battery power supply is not registered. */
777 pr_debug("battery power supply is not registered\n");
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700778 return POWER_SUPPLY_STATUS_UNKNOWN;
779}
780
Zhenhua Huang95a05d32014-03-31 18:09:45 +0800781static int get_battery_charge_type(struct qpnp_bms_chip *chip)
782{
783 union power_supply_propval ret = {0,};
784 int rc;
785
786 if (chip->batt_psy == NULL)
787 chip->batt_psy = power_supply_get_by_name("battery");
788 if (chip->batt_psy) {
789 /* if battery has been registered, use the type property */
790 rc = chip->batt_psy->get_property(chip->batt_psy,
791 POWER_SUPPLY_PROP_CHARGE_TYPE, &ret);
792 if (rc) {
793 pr_debug("Battery does not export charge type: %d\n"
794 , rc);
795 return POWER_SUPPLY_CHARGE_TYPE_NONE;
796 }
797 return ret.intval;
798 }
799
800 /* Default to false if the battery power supply is not registered. */
801 pr_debug("battery power supply is not registered\n");
802 return POWER_SUPPLY_CHARGE_TYPE_NONE;
803}
804
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700805static bool is_battery_charging(struct qpnp_bms_chip *chip)
806{
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800807 return get_battery_status(chip) == POWER_SUPPLY_STATUS_CHARGING;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800808}
809
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700810static bool is_battery_full(struct qpnp_bms_chip *chip)
811{
812 return get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL;
813}
814
Xiaozhe Shi4515c762013-11-13 16:36:54 -0800815#define BAT_PRES_BIT BIT(7)
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700816static bool is_battery_present(struct qpnp_bms_chip *chip)
817{
818 union power_supply_propval ret = {0,};
Xiaozhe Shi4515c762013-11-13 16:36:54 -0800819 int rc;
820 u8 batt_pres;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700821
Xiaozhe Shi4515c762013-11-13 16:36:54 -0800822 /* first try to use the batt_pres register if given */
823 if (chip->batt_pres_addr) {
824 rc = qpnp_read_wrapper(chip, &batt_pres,
825 chip->batt_pres_addr, 1);
826 if (!rc && (batt_pres & BAT_PRES_BIT))
827 return true;
828 else
829 return false;
830 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700831 if (chip->batt_psy == NULL)
832 chip->batt_psy = power_supply_get_by_name("battery");
833 if (chip->batt_psy) {
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700834 /* if battery has been registered, use the present property */
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800835 rc = chip->batt_psy->get_property(chip->batt_psy,
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700836 POWER_SUPPLY_PROP_PRESENT, &ret);
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800837 if (rc) {
838 pr_debug("battery does not export present: %d\n", rc);
839 return true;
840 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700841 return ret.intval;
842 }
843
844 /* Default to false if the battery power supply is not registered. */
845 pr_debug("battery power supply is not registered\n");
846 return false;
847}
848
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700849static int get_battery_insertion_ocv_uv(struct qpnp_bms_chip *chip)
850{
851 union power_supply_propval ret = {0,};
852 int rc, vbat;
853
854 if (chip->batt_psy == NULL)
855 chip->batt_psy = power_supply_get_by_name("battery");
856 if (chip->batt_psy) {
857 /* if battery has been registered, use the ocv property */
858 rc = chip->batt_psy->get_property(chip->batt_psy,
859 POWER_SUPPLY_PROP_VOLTAGE_OCV, &ret);
860 if (rc) {
861 /*
862 * Default to vbatt if the battery OCV is not
863 * registered.
864 */
865 pr_debug("Battery psy does not have voltage ocv\n");
866 rc = get_battery_voltage(chip, &vbat);
867 if (rc)
868 return -EINVAL;
869 return vbat;
870 }
871 return ret.intval;
872 }
873
874 pr_debug("battery power supply is not registered\n");
875 return -EINVAL;
876}
877
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700878static bool is_batfet_closed(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800879{
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700880 union power_supply_propval ret = {0,};
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800881 int rc;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700882
883 if (chip->batt_psy == NULL)
884 chip->batt_psy = power_supply_get_by_name("battery");
885 if (chip->batt_psy) {
886 /* if battery has been registered, use the online property */
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800887 rc = chip->batt_psy->get_property(chip->batt_psy,
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700888 POWER_SUPPLY_PROP_ONLINE, &ret);
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -0800889 if (rc) {
890 pr_debug("Battery does not export online: %d\n", rc);
891 return true;
892 }
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700893 return !!ret.intval;
894 }
895
896 /* Default to true if the battery power supply is not registered. */
897 pr_debug("battery power supply is not registered\n");
898 return true;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800899}
900
901static int get_simultaneous_batt_v_and_i(struct qpnp_bms_chip *chip,
902 int *ibat_ua, int *vbat_uv)
903{
904 struct qpnp_iadc_result i_result;
905 struct qpnp_vadc_result v_result;
906 enum qpnp_iadc_channels iadc_channel;
907 int rc;
908
909 iadc_channel = chip->use_external_rsense ?
910 EXTERNAL_RSENSE : INTERNAL_RSENSE;
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700911 if (is_battery_full(chip)) {
912 rc = get_battery_current(chip, ibat_ua);
913 if (rc) {
914 pr_err("bms current read failed with rc: %d\n", rc);
915 return rc;
916 }
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700917 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &v_result);
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700918 if (rc) {
919 pr_err("vadc read failed with rc: %d\n", rc);
920 return rc;
921 }
922 *vbat_uv = (int)v_result.physical;
923 } else {
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700924 rc = qpnp_iadc_vadc_sync_read(chip->iadc_dev,
925 iadc_channel, &i_result,
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700926 VBAT_SNS, &v_result);
927 if (rc) {
928 pr_err("adc sync read failed with rc: %d\n", rc);
929 return rc;
930 }
931 /*
932 * reverse the current read by the iadc, since the bms uses
933 * flipped battery current polarity.
934 */
935 *ibat_ua = -1 * (int)i_result.result_ua;
936 *vbat_uv = (int)v_result.physical;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800937 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800938
939 return 0;
940}
941
Xiaozhe Shi3ede2ad2014-02-04 13:25:38 -0800942static int get_rbatt(struct qpnp_bms_chip *chip,
943 int soc_rbatt_mohm, int batt_temp)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800944{
Xiaozhe Shi3ede2ad2014-02-04 13:25:38 -0800945 int rbatt_mohm, scalefactor;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800946
Xiaozhe Shi3ede2ad2014-02-04 13:25:38 -0800947 rbatt_mohm = chip->default_rbatt_mohm;
948 if (chip->rbatt_sf_lut == NULL) {
949 pr_debug("RBATT = %d\n", rbatt_mohm);
950 return rbatt_mohm;
951 }
952 /* Convert the batt_temp to DegC from deciDegC */
953 scalefactor = interpolate_scalingfactor(chip->rbatt_sf_lut,
954 batt_temp, soc_rbatt_mohm);
955 rbatt_mohm = (rbatt_mohm * scalefactor) / 100;
956
957 rbatt_mohm += chip->r_conn_mohm;
958 rbatt_mohm += chip->rbatt_capacitive_mohm;
959 return rbatt_mohm;
960}
961
962#define DEFAULT_RBATT_SOC 50
963static int estimate_ocv(struct qpnp_bms_chip *chip, int batt_temp)
964{
965 int ibat_ua, vbat_uv, ocv_est_uv, rbatt_mohm, rc;
966
967 rbatt_mohm = get_rbatt(chip, DEFAULT_RBATT_SOC, batt_temp);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800968 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
969 if (rc) {
970 pr_err("simultaneous failed rc = %d\n", rc);
971 return rc;
972 }
973
974 ocv_est_uv = vbat_uv + (ibat_ua * rbatt_mohm) / 1000;
Xiaozhe Shi3ede2ad2014-02-04 13:25:38 -0800975 pr_debug("estimated pon ocv = %d, vbat_uv = %d ibat_ua = %d rbatt_mohm = %d\n",
976 ocv_est_uv, vbat_uv, ibat_ua, rbatt_mohm);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800977 return ocv_est_uv;
978}
979
Xiaozhe Shi2c171172013-12-03 13:27:37 -0800980#define MIN_IAVG_MA 250
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800981static void reset_for_new_battery(struct qpnp_bms_chip *chip, int batt_temp)
982{
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700983 chip->last_ocv_uv = chip->insertion_ocv_uv;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700984 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800985 chip->last_soc = -EINVAL;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700986 chip->last_soc_invalid = true;
987 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800988 chip->soc_at_cv = -EINVAL;
989 chip->shutdown_soc_invalid = true;
990 chip->shutdown_soc = 0;
Xiaozhe Shi2c171172013-12-03 13:27:37 -0800991 chip->shutdown_iavg_ma = MIN_IAVG_MA;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800992 chip->prev_pc_unusable = -EINVAL;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700993 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700994 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700995 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800996 chip->last_cc_uah = INT_MIN;
997 chip->last_ocv_temp = batt_temp;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800998 chip->prev_batt_terminal_uv = 0;
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530999 if (chip->enable_fcc_learning) {
1000 chip->adjusted_fcc_temp_lut = NULL;
1001 chip->fcc_new_mah = -EINVAL;
1002 /* reset the charge-cycle and charge-increase registers */
1003 chip->charge_increase = 0;
1004 chip->charge_cycles = 0;
1005 backup_charge_cycle(chip);
1006 /* discard all the FCC learnt data and reset the local table */
1007 discard_backup_fcc_data(chip);
1008 memset(chip->fcc_learning_samples, 0,
1009 chip->min_fcc_learning_samples *
1010 sizeof(struct fcc_sample));
1011 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001012}
1013
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001014#define SIGN(x) ((x) < 0 ? -1 : 1)
1015#define UV_PER_SPIN 50000
1016static int find_ocv_for_pc(struct qpnp_bms_chip *chip, int batt_temp, int pc)
1017{
1018 int new_pc;
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001019 int ocv_mv;
1020 int delta_mv = 5;
1021 int max_spin_count;
1022 int count = 0;
1023 int sign, new_sign;
1024
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08001025 ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut, batt_temp, pc);
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001026
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08001027 new_pc = interpolate_pc(chip->pc_temp_ocv_lut, batt_temp, ocv_mv);
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001028 pr_debug("test revlookup pc = %d for ocv = %d\n", new_pc, ocv_mv);
1029 max_spin_count = 1 + (chip->max_voltage_uv - chip->v_cutoff_uv)
1030 / UV_PER_SPIN;
1031 sign = SIGN(pc - new_pc);
1032
1033 while (abs(new_pc - pc) != 0 && count < max_spin_count) {
1034 /*
1035 * If the newly interpolated pc is larger than the lookup pc,
1036 * the ocv should be reduced and vice versa
1037 */
1038 new_sign = SIGN(pc - new_pc);
1039 /*
1040 * If the sign has changed, then we have passed the lookup pc.
1041 * reduce the ocv step size to get finer results.
1042 *
1043 * If we have already reduced the ocv step size and still
1044 * passed the lookup pc, just stop and use the current ocv.
1045 * This can only happen if the batterydata profile is
1046 * non-monotonic anyways.
1047 */
1048 if (new_sign != sign) {
1049 if (delta_mv > 1)
1050 delta_mv = 1;
1051 else
1052 break;
1053 }
1054 sign = new_sign;
1055
1056 ocv_mv = ocv_mv + delta_mv * sign;
1057 new_pc = interpolate_pc(chip->pc_temp_ocv_lut,
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08001058 batt_temp, ocv_mv);
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001059 pr_debug("test revlookup pc = %d for ocv = %d\n",
1060 new_pc, ocv_mv);
1061 count++;
1062 }
1063
1064 return ocv_mv * 1000;
1065}
1066
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08001067#define OCV_RAW_UNINITIALIZED 0xFFFF
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001068#define MIN_OCV_UV 2000000
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001069static int read_soc_params_raw(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001070 struct raw_soc_params *raw,
1071 int batt_temp)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001072{
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001073 int warm_reset, rc;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001074
1075 mutex_lock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -08001076
Xiaozhe Shie118c692012-09-24 15:17:43 -07001077 lock_output_data(chip);
1078
1079 rc = qpnp_read_wrapper(chip, (u8 *)&raw->last_good_ocv_raw,
1080 chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
1081 if (rc) {
1082 pr_err("Error reading ocv: rc = %d\n", rc);
Xiaozhe Shiabbd6072013-12-11 14:24:02 -08001083 goto param_err;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001084 }
1085
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001086 rc = read_cc_raw(chip, &raw->cc, CC);
Xiaozhe Shied4a5522014-09-05 14:56:13 -07001087 rc |= read_cc_raw(chip, &raw->shdw_cc, SHDW_CC);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001088 if (rc) {
1089 pr_err("Failed to read raw cc data, rc = %d\n", rc);
Xiaozhe Shiabbd6072013-12-11 14:24:02 -08001090 goto param_err;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001091 }
1092
1093 unlock_output_data(chip);
1094 mutex_unlock(&chip->bms_output_lock);
1095
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08001096 if (chip->prev_last_good_ocv_raw == OCV_RAW_UNINITIALIZED) {
Xiaozhe Shi80754222013-10-30 14:11:41 -07001097 convert_and_store_ocv(chip, raw, batt_temp, true);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001098 pr_debug("PON_OCV_UV = %d, cc = %llx\n",
1099 chip->last_ocv_uv, raw->cc);
1100 warm_reset = qpnp_pon_is_warm_reset();
Xiaozhe Shi3ede2ad2014-02-04 13:25:38 -08001101 if (raw->last_good_ocv_uv < MIN_OCV_UV || warm_reset > 0) {
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001102 pr_debug("OCV is stale or bad, estimating new OCV.\n");
Xiaozhe Shi3ede2ad2014-02-04 13:25:38 -08001103 chip->last_ocv_uv = estimate_ocv(chip, batt_temp);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001104 raw->last_good_ocv_uv = chip->last_ocv_uv;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001105 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001106 pr_debug("New PON_OCV_UV = %d, cc = %llx\n",
1107 chip->last_ocv_uv, raw->cc);
1108 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001109 } else if (chip->new_battery) {
1110 /* if a new battery was inserted, estimate the ocv */
1111 reset_for_new_battery(chip, batt_temp);
1112 raw->cc = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001113 raw->shdw_cc = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001114 raw->last_good_ocv_uv = chip->last_ocv_uv;
1115 chip->new_battery = false;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001116 } else if (chip->done_charging) {
1117 chip->done_charging = false;
1118 /* if we just finished charging, reset CC and fake 100% */
1119 chip->ocv_reading_at_100 = raw->last_good_ocv_raw;
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001120 chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp, 100);
1121 raw->last_good_ocv_uv = chip->last_ocv_uv;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001122 raw->cc = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001123 raw->shdw_cc = 0;
1124 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001125 chip->last_ocv_temp = batt_temp;
1126 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001127 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001128 chip->last_cc_uah = INT_MIN;
1129 pr_debug("EOC Battery full ocv_reading = 0x%x\n",
1130 chip->ocv_reading_at_100);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001131 } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) {
Xiaozhe Shi80754222013-10-30 14:11:41 -07001132 convert_and_store_ocv(chip, raw, batt_temp, false);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001133 /* forget the old cc value upon ocv */
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001134 chip->last_cc_uah = INT_MIN;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001135 } else {
1136 raw->last_good_ocv_uv = chip->last_ocv_uv;
1137 }
1138
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001139 /* stop faking a high OCV if we get a new OCV */
1140 if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw)
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08001141 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001142
Xiaozhe Shie118c692012-09-24 15:17:43 -07001143 pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n",
1144 raw->last_good_ocv_raw, raw->last_good_ocv_uv);
1145 pr_debug("cc_raw= 0x%llx\n", raw->cc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001146 return 0;
Xiaozhe Shiabbd6072013-12-11 14:24:02 -08001147
1148param_err:
1149 unlock_output_data(chip);
1150 mutex_unlock(&chip->bms_output_lock);
1151 return rc;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001152}
1153
Xiaozhe Shie118c692012-09-24 15:17:43 -07001154static int calculate_pc(struct qpnp_bms_chip *chip, int ocv_uv,
1155 int batt_temp)
1156{
1157 int pc;
1158
1159 pc = interpolate_pc(chip->pc_temp_ocv_lut,
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08001160 batt_temp, ocv_uv / 1000);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001161 pr_debug("pc = %u %% for ocv = %d uv batt_temp = %d\n",
1162 pc, ocv_uv, batt_temp);
1163 /* Multiply the initial FCC value by the scale factor. */
1164 return pc;
1165}
1166
1167static int calculate_fcc(struct qpnp_bms_chip *chip, int batt_temp)
1168{
1169 int fcc_uah;
1170
1171 if (chip->adjusted_fcc_temp_lut == NULL) {
1172 /* interpolate_fcc returns a mv value. */
1173 fcc_uah = interpolate_fcc(chip->fcc_temp_lut,
1174 batt_temp) * 1000;
1175 pr_debug("fcc = %d uAh\n", fcc_uah);
1176 return fcc_uah;
1177 } else {
1178 return 1000 * interpolate_fcc(chip->adjusted_fcc_temp_lut,
1179 batt_temp);
1180 }
1181}
1182
1183/* calculate remaining charge at the time of ocv */
1184static int calculate_ocv_charge(struct qpnp_bms_chip *chip,
1185 struct raw_soc_params *raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001186 int fcc_uah)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001187{
1188 int ocv_uv, pc;
1189
1190 ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001191 pc = calculate_pc(chip, ocv_uv, chip->last_ocv_temp);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001192 pr_debug("ocv_uv = %d pc = %d\n", ocv_uv, pc);
1193 return (fcc_uah * pc) / 100;
1194}
1195
Xiaozhe Shie118c692012-09-24 15:17:43 -07001196#define CC_READING_TICKS 56
1197#define SLEEP_CLK_HZ 32764
1198#define SECONDS_PER_HOUR 3600
1199
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001200static s64 cc_uv_to_pvh(s64 cc_uv)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001201{
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001202 /* Note that it is necessary need to multiply by 1000000 to convert
1203 * from uvh to pvh here.
1204 * However, the maximum Coulomb Counter value is 2^35, which can cause
1205 * an over flow.
1206 * Multiply by 100000 first to perserve as much precision as possible
1207 * then multiply by 10 after doing the division in order to avoid
1208 * overflow on the maximum Coulomb Counter value.
1209 */
1210 return div_s64(cc_uv * CC_READING_TICKS * 100000,
1211 SLEEP_CLK_HZ * SECONDS_PER_HOUR) * 10;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001212}
1213
1214/**
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001215 * calculate_cc() - converts a hardware coulomb counter reading into uah
Xiaozhe Shie118c692012-09-24 15:17:43 -07001216 * @chip: the bms chip pointer
1217 * @cc: the cc reading from bms h/w
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001218 * @cc_type: calcualte cc from regular or shadow coulomb counter
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001219 * @clear_cc: whether this function should clear the hardware counter
1220 * after reading
Xiaozhe Shie118c692012-09-24 15:17:43 -07001221 *
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001222 * Converts the 64 bit hardware coulomb counter into microamp-hour by taking
1223 * into account hardware resolution and adc errors.
1224 *
1225 * Return: the coulomb counter based charge in uAh (micro-amp hour)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001226 */
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001227static int calculate_cc(struct qpnp_bms_chip *chip, int64_t cc,
1228 int cc_type, int clear_cc)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001229{
Xiaozhe Shi4e376652012-10-25 12:38:50 -07001230 struct qpnp_iadc_calib calibration;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001231 struct qpnp_vadc_result result;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001232 int64_t cc_voltage_uv, cc_pvh, cc_uah, *software_counter;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001233 int rc;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001234
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001235 software_counter = cc_type == SHDW_CC ?
1236 &chip->software_shdw_cc_uah : &chip->software_cc_uah;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001237 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001238 if (rc) {
1239 pr_err("could not read pmic die temperature: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001240 return *software_counter;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001241 }
1242
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07001243 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001244 pr_debug("%scc = %lld, die_temp = %lld\n",
1245 cc_type == SHDW_CC ? "shdw_" : "",
1246 cc, result.physical);
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -07001247 cc_voltage_uv = cc_reading_to_uv(cc);
Xiaozhe Shi0c484932013-02-05 16:14:10 -08001248 cc_voltage_uv = cc_adjust_for_gain(cc_voltage_uv,
1249 calibration.gain_raw
1250 - calibration.offset_raw);
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001251 cc_pvh = cc_uv_to_pvh(cc_voltage_uv);
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001252 cc_uah = div_s64(cc_pvh, chip->r_sense_uohm);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07001253 rc = qpnp_iadc_comp_result(chip->iadc_dev, &cc_uah);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001254 if (rc)
1255 pr_debug("error compensation failed: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001256 if (clear_cc == RESET) {
1257 pr_debug("software_%scc = %lld, added cc_uah = %lld\n",
1258 cc_type == SHDW_CC ? "sw_" : "",
1259 *software_counter, cc_uah);
1260 *software_counter += cc_uah;
1261 reset_cc(chip, cc_type == SHDW_CC ? CLEAR_SHDW_CC : CLEAR_CC);
1262 return (int)*software_counter;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001263 } else {
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001264 pr_debug("software_%scc = %lld, cc_uah = %lld, total = %lld\n",
1265 cc_type == SHDW_CC ? "shdw_" : "",
1266 *software_counter, cc_uah,
1267 *software_counter + cc_uah);
1268 return *software_counter + cc_uah;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001269 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07001270}
1271
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -07001272#define IAVG_MINIMAL_TIME 2
Xiaozhe Shie118c692012-09-24 15:17:43 -07001273static void calculate_iavg(struct qpnp_bms_chip *chip, int cc_uah,
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001274 int *iavg_ua, int delta_time_s)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001275{
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001276 int delta_cc_uah = 0;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001277
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -07001278 /*
1279 * use the battery current if called too quickly
1280 */
1281 if (delta_time_s < IAVG_MINIMAL_TIME
1282 || chip->last_cc_uah == INT_MIN) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001283 get_battery_current(chip, iavg_ua);
1284 goto out;
1285 }
1286
Xiaozhe Shie118c692012-09-24 15:17:43 -07001287 delta_cc_uah = cc_uah - chip->last_cc_uah;
1288
1289 *iavg_ua = div_s64((s64)delta_cc_uah * 3600, delta_time_s);
1290
Xiaozhe Shie118c692012-09-24 15:17:43 -07001291out:
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001292 pr_debug("delta_cc = %d iavg_ua = %d\n", delta_cc_uah, (int)*iavg_ua);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001293
1294 /* remember cc_uah */
1295 chip->last_cc_uah = cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001296}
1297
1298static int calculate_termination_uuc(struct qpnp_bms_chip *chip,
1299 struct soc_params *params,
1300 int batt_temp, int uuc_iavg_ma,
1301 int *ret_pc_unusable)
1302{
1303 int unusable_uv, pc_unusable, uuc_uah;
1304 int i = 0;
1305 int ocv_mv;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001306 int rbatt_mohm;
1307 int delta_uv;
1308 int prev_delta_uv = 0;
1309 int prev_rbatt_mohm = 0;
1310 int uuc_rbatt_mohm;
1311
1312 for (i = 0; i <= 100; i++) {
1313 ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08001314 batt_temp, i);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001315 rbatt_mohm = get_rbatt(chip, i, batt_temp);
1316 unusable_uv = (rbatt_mohm * uuc_iavg_ma)
1317 + (chip->v_cutoff_uv);
1318 delta_uv = ocv_mv * 1000 - unusable_uv;
1319
Xiaozhe Shie118c692012-09-24 15:17:43 -07001320 if (delta_uv > 0)
1321 break;
1322
1323 prev_delta_uv = delta_uv;
1324 prev_rbatt_mohm = rbatt_mohm;
1325 }
1326
1327 uuc_rbatt_mohm = linear_interpolate(rbatt_mohm, delta_uv,
1328 prev_rbatt_mohm, prev_delta_uv,
1329 0);
1330
1331 unusable_uv = (uuc_rbatt_mohm * uuc_iavg_ma) + (chip->v_cutoff_uv);
1332
1333 pc_unusable = calculate_pc(chip, unusable_uv, batt_temp);
1334 uuc_uah = (params->fcc_uah * pc_unusable) / 100;
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001335 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 -07001336 uuc_iavg_ma,
1337 uuc_rbatt_mohm, unusable_uv,
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001338 pc_unusable, i, uuc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001339 *ret_pc_unusable = pc_unusable;
1340 return uuc_uah;
1341}
1342
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001343#define TIME_PER_PERCENT_UUC 60
Xiaozhe Shie118c692012-09-24 15:17:43 -07001344static int adjust_uuc(struct qpnp_bms_chip *chip,
1345 struct soc_params *params,
1346 int new_pc_unusable,
1347 int new_uuc_uah,
1348 int batt_temp)
1349{
1350 int new_unusable_mv, new_iavg_ma;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001351 int max_percent_change;
1352
1353 max_percent_change = max(params->delta_time_s
1354 / TIME_PER_PERCENT_UUC, 1);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001355
Xiaozhe Shi2c171172013-12-03 13:27:37 -08001356 if (chip->first_time_calc_uuc || chip->prev_pc_unusable == -EINVAL
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001357 || abs(chip->prev_pc_unusable - new_pc_unusable)
1358 <= max_percent_change) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001359 chip->prev_pc_unusable = new_pc_unusable;
1360 return new_uuc_uah;
1361 }
1362
1363 /* the uuc is trying to change more than 1% restrict it */
1364 if (new_pc_unusable > chip->prev_pc_unusable)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001365 chip->prev_pc_unusable += max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001366 else
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001367 chip->prev_pc_unusable -= max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001368
1369 new_uuc_uah = (params->fcc_uah * chip->prev_pc_unusable) / 100;
1370
1371 /* also find update the iavg_ma accordingly */
1372 new_unusable_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08001373 batt_temp, chip->prev_pc_unusable);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001374 if (new_unusable_mv < chip->v_cutoff_uv/1000)
1375 new_unusable_mv = chip->v_cutoff_uv/1000;
1376
1377 new_iavg_ma = (new_unusable_mv * 1000 - chip->v_cutoff_uv)
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001378 / params->rbatt_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001379 if (new_iavg_ma == 0)
1380 new_iavg_ma = 1;
1381 chip->prev_uuc_iavg_ma = new_iavg_ma;
1382 pr_debug("Restricting UUC to %d (%d%%) unusable_mv = %d iavg_ma = %d\n",
1383 new_uuc_uah, chip->prev_pc_unusable,
1384 new_unusable_mv, new_iavg_ma);
1385
1386 return new_uuc_uah;
1387}
1388
Xiaozhe Shie118c692012-09-24 15:17:43 -07001389static int calculate_unusable_charge_uah(struct qpnp_bms_chip *chip,
1390 struct soc_params *params,
1391 int batt_temp)
1392{
1393 int uuc_uah_iavg;
1394 int i;
1395 int uuc_iavg_ma = params->iavg_ua / 1000;
1396 int pc_unusable;
1397
1398 /*
1399 * if called first time, fill all the samples with
1400 * the shutdown_iavg_ma
1401 */
1402 if (chip->first_time_calc_uuc && chip->shutdown_iavg_ma != 0) {
1403 pr_debug("Using shutdown_iavg_ma = %d in all samples\n",
1404 chip->shutdown_iavg_ma);
1405 for (i = 0; i < IAVG_SAMPLES; i++)
1406 chip->iavg_samples_ma[i] = chip->shutdown_iavg_ma;
1407
1408 chip->iavg_index = 0;
1409 chip->iavg_num_samples = IAVG_SAMPLES;
1410 }
1411
Xiaozhe Shi70633922013-09-23 15:50:53 -07001412 if (params->delta_time_s >= IAVG_MINIMAL_TIME) {
1413 /*
1414 * if charging use a nominal avg current to keep
1415 * a reasonable UUC while charging
1416 */
1417 if (uuc_iavg_ma < MIN_IAVG_MA)
1418 uuc_iavg_ma = MIN_IAVG_MA;
1419 chip->iavg_samples_ma[chip->iavg_index] = uuc_iavg_ma;
1420 chip->iavg_index = (chip->iavg_index + 1) % IAVG_SAMPLES;
1421 chip->iavg_num_samples++;
1422 if (chip->iavg_num_samples >= IAVG_SAMPLES)
1423 chip->iavg_num_samples = IAVG_SAMPLES;
1424 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07001425
1426 /* now that this sample is added calcualte the average */
1427 uuc_iavg_ma = 0;
1428 if (chip->iavg_num_samples != 0) {
1429 for (i = 0; i < chip->iavg_num_samples; i++) {
1430 pr_debug("iavg_samples_ma[%d] = %d\n", i,
1431 chip->iavg_samples_ma[i]);
1432 uuc_iavg_ma += chip->iavg_samples_ma[i];
1433 }
1434
1435 uuc_iavg_ma = DIV_ROUND_CLOSEST(uuc_iavg_ma,
1436 chip->iavg_num_samples);
1437 }
1438
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001439 /*
1440 * if we're in bms reset mode, force uuc to be 3% of fcc
1441 */
1442 if (bms_reset)
1443 return (params->fcc_uah * 3) / 100;
1444
Xiaozhe Shi75e5efe2013-02-07 09:51:43 -08001445 uuc_uah_iavg = calculate_termination_uuc(chip, params, batt_temp,
1446 uuc_iavg_ma, &pc_unusable);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001447 pr_debug("uuc_iavg_ma = %d uuc with iavg = %d\n",
1448 uuc_iavg_ma, uuc_uah_iavg);
1449
1450 chip->prev_uuc_iavg_ma = uuc_iavg_ma;
1451 /* restrict the uuc such that it can increase only by one percent */
1452 uuc_uah_iavg = adjust_uuc(chip, params, pc_unusable,
1453 uuc_uah_iavg, batt_temp);
1454
Xiaozhe Shie118c692012-09-24 15:17:43 -07001455 return uuc_uah_iavg;
1456}
1457
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001458static s64 find_ocv_charge_for_soc(struct qpnp_bms_chip *chip,
1459 struct soc_params *params, int soc)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001460{
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001461 return div_s64((s64)soc * (params->fcc_uah - params->uuc_uah),
1462 100) + params->cc_uah + params->uuc_uah;
1463}
Xiaozhe Shie118c692012-09-24 15:17:43 -07001464
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001465static int find_pc_for_soc(struct qpnp_bms_chip *chip,
1466 struct soc_params *params, int soc)
1467{
1468 int ocv_charge_uah = find_ocv_charge_for_soc(chip, params, soc);
1469 int pc;
1470
Xiaozhe Shie118c692012-09-24 15:17:43 -07001471 pc = DIV_ROUND_CLOSEST((int)ocv_charge_uah * 100, params->fcc_uah);
1472 pc = clamp(pc, 0, 100);
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001473 pr_debug("soc = %d, fcc = %d uuc = %d rc = %d pc = %d\n",
1474 soc, params->fcc_uah, params->uuc_uah,
1475 ocv_charge_uah, pc);
1476 return pc;
1477}
Xiaozhe Shie118c692012-09-24 15:17:43 -07001478
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001479static int get_current_time(unsigned long *now_tm_sec)
1480{
1481 struct rtc_time tm;
1482 struct rtc_device *rtc;
1483 int rc;
1484
1485 rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
1486 if (rtc == NULL) {
1487 pr_err("%s: unable to open rtc device (%s)\n",
1488 __FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
Xiaozhe Shi0e01af62013-05-06 12:56:08 -07001489 return -EINVAL;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001490 }
1491
1492 rc = rtc_read_time(rtc, &tm);
1493 if (rc) {
1494 pr_err("Error reading rtc device (%s) : %d\n",
1495 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1496 goto close_time;
1497 }
1498
1499 rc = rtc_valid_tm(&tm);
1500 if (rc) {
1501 pr_err("Invalid RTC time (%s): %d\n",
1502 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1503 goto close_time;
1504 }
1505 rtc_tm_to_time(&tm, now_tm_sec);
1506
1507close_time:
1508 rtc_class_close(rtc);
1509 return rc;
1510}
1511
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08001512/* Returns estimated battery resistance */
1513static int get_prop_bms_batt_resistance(struct qpnp_bms_chip *chip)
1514{
1515 return chip->rbatt_mohm * 1000;
1516}
1517
1518/* Returns instantaneous current in uA */
1519static int get_prop_bms_current_now(struct qpnp_bms_chip *chip)
1520{
1521 int rc, result_ua;
1522
1523 rc = get_battery_current(chip, &result_ua);
1524 if (rc) {
1525 pr_err("failed to get current: %d\n", rc);
1526 return rc;
1527 }
1528 return result_ua;
1529}
1530
1531/* Returns coulomb counter in uAh */
1532static int get_prop_bms_charge_counter(struct qpnp_bms_chip *chip)
1533{
1534 int64_t cc_raw;
1535
1536 mutex_lock(&chip->bms_output_lock);
1537 lock_output_data(chip);
Xiaozhe Shie11c9492013-12-11 14:37:36 -08001538 read_cc_raw(chip, &cc_raw, CC);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08001539 unlock_output_data(chip);
1540 mutex_unlock(&chip->bms_output_lock);
1541
1542 return calculate_cc(chip, cc_raw, CC, NORESET);
1543}
1544
1545/* Returns shadow coulomb counter in uAh */
1546static int get_prop_bms_charge_counter_shadow(struct qpnp_bms_chip *chip)
1547{
1548 int64_t cc_raw;
1549
1550 mutex_lock(&chip->bms_output_lock);
1551 lock_output_data(chip);
Xiaozhe Shie11c9492013-12-11 14:37:36 -08001552 read_cc_raw(chip, &cc_raw, SHDW_CC);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08001553 unlock_output_data(chip);
1554 mutex_unlock(&chip->bms_output_lock);
1555
1556 return calculate_cc(chip, cc_raw, SHDW_CC, NORESET);
1557}
1558
1559/* Returns full charge design in uAh */
1560static int get_prop_bms_charge_full_design(struct qpnp_bms_chip *chip)
1561{
1562 return chip->fcc_mah * 1000;
1563}
1564
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05301565/* Returns the current full charge in uAh */
1566static int get_prop_bms_charge_full(struct qpnp_bms_chip *chip)
1567{
1568 int rc;
1569 struct qpnp_vadc_result result;
1570
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001571 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05301572 if (rc) {
1573 pr_err("Unable to read battery temperature\n");
1574 return rc;
1575 }
1576
1577 return calculate_fcc(chip, (int)result.physical);
1578}
1579
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001580static int calculate_delta_time(unsigned long *time_stamp, int *delta_time_s)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001581{
1582 unsigned long now_tm_sec = 0;
1583
1584 /* default to delta time = 0 if anything fails */
1585 *delta_time_s = 0;
1586
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001587 if (get_current_time(&now_tm_sec)) {
1588 pr_err("RTC read failed\n");
1589 return 0;
1590 }
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001591
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001592 *delta_time_s = (now_tm_sec - *time_stamp);
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001593
1594 /* remember this time */
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001595 *time_stamp = now_tm_sec;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001596 return 0;
1597}
1598
Xiaozhe Shie118c692012-09-24 15:17:43 -07001599static void calculate_soc_params(struct qpnp_bms_chip *chip,
1600 struct raw_soc_params *raw,
1601 struct soc_params *params,
1602 int batt_temp)
1603{
Xiaozhe Shi219cb222013-06-10 15:49:59 -07001604 int soc_rbatt, shdw_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001605
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001606 calculate_delta_time(&chip->tm_sec, &params->delta_time_s);
1607 pr_debug("tm_sec = %ld, delta_s = %d\n",
1608 chip->tm_sec, params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001609 params->fcc_uah = calculate_fcc(chip, batt_temp);
1610 pr_debug("FCC = %uuAh batt_temp = %d\n", params->fcc_uah, batt_temp);
1611
1612 /* calculate remainging charge */
1613 params->ocv_charge_uah = calculate_ocv_charge(
1614 chip, raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001615 params->fcc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001616 pr_debug("ocv_charge_uah = %uuAh\n", params->ocv_charge_uah);
1617
1618 /* calculate cc micro_volt_hour */
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001619 params->cc_uah = calculate_cc(chip, raw->cc, CC, RESET);
Xiaozhe Shi219cb222013-06-10 15:49:59 -07001620 shdw_cc_uah = calculate_cc(chip, raw->shdw_cc, SHDW_CC, RESET);
1621 pr_debug("cc_uah = %duAh raw->cc = %llx, shdw_cc_uah = %duAh raw->shdw_cc = %llx\n",
1622 params->cc_uah, raw->cc,
1623 shdw_cc_uah, raw->shdw_cc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001624
1625 soc_rbatt = ((params->ocv_charge_uah - params->cc_uah) * 100)
1626 / params->fcc_uah;
1627 if (soc_rbatt < 0)
1628 soc_rbatt = 0;
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001629 params->rbatt_mohm = get_rbatt(chip, soc_rbatt, batt_temp);
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001630 pr_debug("rbatt_mohm = %d\n", params->rbatt_mohm);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001631
Xiaozhe Shi1e87cda2013-05-17 10:18:56 -07001632 if (params->rbatt_mohm != chip->rbatt_mohm) {
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07001633 chip->rbatt_mohm = params->rbatt_mohm;
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07001634 if (chip->bms_psy_registered)
Xiaozhe Shi1e87cda2013-05-17 10:18:56 -07001635 power_supply_changed(&chip->bms_psy);
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07001636 }
1637
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001638 calculate_iavg(chip, params->cc_uah, &params->iavg_ua,
1639 params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001640
1641 params->uuc_uah = calculate_unusable_charge_uah(chip, params,
1642 batt_temp);
1643 pr_debug("UUC = %uuAh\n", params->uuc_uah);
1644}
1645
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001646static int bound_soc(int soc)
1647{
1648 soc = max(0, soc);
1649 soc = min(100, soc);
1650 return soc;
1651}
1652
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001653#define IBAT_TOL_MASK 0x0F
1654#define OCV_TOL_MASK 0xF0
1655#define IBAT_TOL_DEFAULT 0x03
1656#define IBAT_TOL_NOCHG 0x0F
1657#define OCV_TOL_DEFAULT 0x20
1658#define OCV_TOL_NO_OCV 0x00
1659static int stop_ocv_updates(struct qpnp_bms_chip *chip)
1660{
1661 pr_debug("stopping ocv updates\n");
1662 return qpnp_masked_write(chip, BMS1_TOL_CTL,
1663 OCV_TOL_MASK, OCV_TOL_NO_OCV);
1664}
1665
1666static int reset_bms_for_test(struct qpnp_bms_chip *chip)
1667{
Xiaozhe Shi95da77f2013-02-20 13:40:06 -08001668 int ibat_ua = 0, vbat_uv = 0, rc;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001669 int ocv_est_uv;
1670
1671 if (!chip) {
1672 pr_err("BMS driver has not been initialized yet!\n");
1673 return -EINVAL;
1674 }
1675
1676 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
1677
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07001678 /*
1679 * Don't include rbatt and rbatt_capacitative since we expect this to
1680 * be used with a fake battery which does not have internal resistances
1681 */
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001682 ocv_est_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
1683 pr_debug("forcing ocv to be %d due to bms reset mode\n", ocv_est_uv);
1684 chip->last_ocv_uv = ocv_est_uv;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001685 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001686 chip->last_soc = -EINVAL;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001687 chip->last_soc_invalid = true;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001688 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001689 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001690 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001691 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001692 chip->last_cc_uah = INT_MIN;
1693 stop_ocv_updates(chip);
1694
1695 pr_debug("bms reset to ocv = %duv vbat_ua = %d ibat_ua = %d\n",
1696 chip->last_ocv_uv, vbat_uv, ibat_ua);
1697
1698 return rc;
1699}
1700
1701static int bms_reset_set(const char *val, const struct kernel_param *kp)
1702{
1703 int rc;
1704
1705 rc = param_set_bool(val, kp);
1706 if (rc) {
1707 pr_err("Unable to set bms_reset: %d\n", rc);
1708 return rc;
1709 }
1710
1711 if (*(bool *)kp->arg) {
1712 struct power_supply *bms_psy = power_supply_get_by_name("bms");
1713 struct qpnp_bms_chip *chip = container_of(bms_psy,
1714 struct qpnp_bms_chip, bms_psy);
1715
1716 rc = reset_bms_for_test(chip);
1717 if (rc) {
1718 pr_err("Unable to modify bms_reset: %d\n", rc);
1719 return rc;
1720 }
1721 }
1722 return 0;
1723}
1724
1725static struct kernel_param_ops bms_reset_ops = {
1726 .set = bms_reset_set,
1727 .get = param_get_bool,
1728};
1729
1730module_param_cb(bms_reset, &bms_reset_ops, &bms_reset, 0644);
1731
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001732#define SOC_STORAGE_MASK 0xFE
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001733static void backup_soc_and_iavg(struct qpnp_bms_chip *chip, int batt_temp,
1734 int soc)
1735{
1736 u8 temp;
1737 int rc;
1738 int iavg_ma = chip->prev_uuc_iavg_ma;
1739
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07001740 if (iavg_ma > MIN_IAVG_MA)
1741 temp = (iavg_ma - MIN_IAVG_MA) / IAVG_STEP_SIZE_MA;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001742 else
1743 temp = 0;
1744
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001745 rc = qpnp_write_wrapper(chip, &temp, chip->base + IAVG_STORAGE_REG, 1);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001746
Xiaozhe Shie945a8a2013-11-11 10:20:14 -08001747 /* store an invalid soc if temperature is below 5degC */
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001748 if (batt_temp > IGNORE_SOC_TEMP_DECIDEG)
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001749 qpnp_masked_write_base(chip, chip->soc_storage_addr,
1750 SOC_STORAGE_MASK, (soc + 1) << 1);
Xiaozhe Shie945a8a2013-11-11 10:20:14 -08001751 else
1752 qpnp_masked_write_base(chip, chip->soc_storage_addr,
1753 SOC_STORAGE_MASK, SOC_STORAGE_MASK);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001754}
1755
1756static int scale_soc_while_chg(struct qpnp_bms_chip *chip, int chg_time_sec,
1757 int catch_up_sec, int new_soc, int prev_soc)
1758{
1759 int scaled_soc;
1760 int numerator;
1761
1762 /*
1763 * Don't report a high value immediately slowly scale the
1764 * value from prev_soc to the new soc based on a charge time
1765 * weighted average
1766 */
1767 pr_debug("cts = %d catch_up_sec = %d\n", chg_time_sec, catch_up_sec);
1768 if (catch_up_sec == 0)
1769 return new_soc;
1770
1771 if (chg_time_sec > catch_up_sec)
1772 return new_soc;
1773
1774 numerator = (catch_up_sec - chg_time_sec) * prev_soc
1775 + chg_time_sec * new_soc;
1776 scaled_soc = numerator / catch_up_sec;
1777
1778 pr_debug("cts = %d new_soc = %d prev_soc = %d scaled_soc = %d\n",
1779 chg_time_sec, new_soc, prev_soc, scaled_soc);
1780
1781 return scaled_soc;
1782}
1783
1784/*
1785 * bms_fake_battery is set in setups where a battery emulator is used instead
1786 * of a real battery. This makes the bms driver report a different/fake value
1787 * regardless of the calculated state of charge.
1788 */
1789static int bms_fake_battery = -EINVAL;
1790module_param(bms_fake_battery, int, 0644);
1791
1792static int report_voltage_based_soc(struct qpnp_bms_chip *chip)
1793{
1794 pr_debug("Reported voltage based soc = %d\n",
1795 chip->prev_voltage_based_soc);
1796 return chip->prev_voltage_based_soc;
1797}
1798
1799#define SOC_CATCHUP_SEC_MAX 600
1800#define SOC_CATCHUP_SEC_PER_PERCENT 60
1801#define MAX_CATCHUP_SOC (SOC_CATCHUP_SEC_MAX / SOC_CATCHUP_SEC_PER_PERCENT)
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07001802#define SOC_CHANGE_PER_SEC 5
Xiaozhe Shi27375822013-08-22 11:40:15 -07001803#define REPORT_SOC_WAIT_MS 10000
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001804static int report_cc_based_soc(struct qpnp_bms_chip *chip)
1805{
1806 int soc, soc_change;
1807 int time_since_last_change_sec, charge_time_sec = 0;
1808 unsigned long last_change_sec;
1809 struct timespec now;
1810 struct qpnp_vadc_result result;
1811 int batt_temp;
1812 int rc;
1813 bool charging, charging_since_last_report;
1814
Xiaozhe Shi27375822013-08-22 11:40:15 -07001815 rc = wait_event_interruptible_timeout(chip->bms_wait_queue,
1816 chip->calculated_soc != -EINVAL,
1817 round_jiffies_relative(msecs_to_jiffies
1818 (REPORT_SOC_WAIT_MS)));
1819
1820 if (rc == 0 && chip->calculated_soc == -EINVAL) {
1821 pr_debug("calculate soc timed out\n");
1822 } else if (rc == -ERESTARTSYS) {
1823 pr_err("Wait for SoC interrupted.\n");
1824 return rc;
1825 }
1826
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001827 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001828
1829 if (rc) {
1830 pr_err("error reading adc channel = %d, rc = %d\n",
1831 LR_MUX1_BATT_THERM, rc);
1832 return rc;
1833 }
1834 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
1835 result.measurement);
1836 batt_temp = (int)result.physical;
1837
1838 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shifa6ea692013-05-31 11:15:13 -07001839 soc = chip->calculated_soc;
1840
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001841 last_change_sec = chip->last_soc_change_sec;
1842 calculate_delta_time(&last_change_sec, &time_since_last_change_sec);
1843
Xiaozhe Shi9e2422b2014-01-27 15:47:18 -08001844 charging = chip->battery_status == POWER_SUPPLY_STATUS_CHARGING;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001845 charging_since_last_report = charging || (chip->last_soc_unbound
1846 && chip->was_charging_at_sleep);
1847 /*
1848 * account for charge time - limit it to SOC_CATCHUP_SEC to
1849 * avoid overflows when charging continues for extended periods
1850 */
1851 if (charging) {
1852 if (chip->charge_start_tm_sec == 0) {
1853 /*
1854 * calculating soc for the first time
1855 * after start of chg. Initialize catchup time
1856 */
1857 if (abs(soc - chip->last_soc) < MAX_CATCHUP_SOC)
1858 chip->catch_up_time_sec =
1859 (soc - chip->last_soc)
1860 * SOC_CATCHUP_SEC_PER_PERCENT;
1861 else
1862 chip->catch_up_time_sec = SOC_CATCHUP_SEC_MAX;
1863
1864 if (chip->catch_up_time_sec < 0)
1865 chip->catch_up_time_sec = 0;
1866 chip->charge_start_tm_sec = last_change_sec;
1867 }
1868
1869 charge_time_sec = min(SOC_CATCHUP_SEC_MAX, (int)last_change_sec
1870 - chip->charge_start_tm_sec);
1871
1872 /* end catchup if calculated soc and last soc are same */
1873 if (chip->last_soc == soc)
1874 chip->catch_up_time_sec = 0;
1875 }
1876
1877 if (chip->last_soc != -EINVAL) {
1878 /*
1879 * last_soc < soc ... if we have not been charging at all
1880 * since the last time this was called, report previous SoC.
1881 * Otherwise, scale and catch up.
1882 */
1883 if (chip->last_soc < soc && !charging_since_last_report)
1884 soc = chip->last_soc;
1885 else if (chip->last_soc < soc && soc != 100)
1886 soc = scale_soc_while_chg(chip, charge_time_sec,
1887 chip->catch_up_time_sec,
1888 soc, chip->last_soc);
1889
Xiaozhe Shibd56b052013-10-21 11:51:30 -07001890 /* if the battery is close to cutoff allow more change */
1891 if (wake_lock_active(&chip->low_voltage_wake_lock))
1892 soc_change = min((int)abs(chip->last_soc - soc),
1893 time_since_last_change_sec);
1894 else
1895 soc_change = min((int)abs(chip->last_soc - soc),
1896 time_since_last_change_sec
1897 / SOC_CHANGE_PER_SEC);
1898
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001899 if (chip->last_soc_unbound) {
1900 chip->last_soc_unbound = false;
1901 } else {
1902 /*
1903 * if soc have not been unbound by resume,
1904 * only change reported SoC by 1.
1905 */
1906 soc_change = min(1, soc_change);
1907 }
1908
1909 if (soc < chip->last_soc && soc != 0)
1910 soc = chip->last_soc - soc_change;
1911 if (soc > chip->last_soc && soc != 100)
1912 soc = chip->last_soc + soc_change;
1913 }
1914
Xiaozhe Shi208b8e52013-05-28 10:16:32 -07001915 if (chip->last_soc != soc && !chip->last_soc_unbound)
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001916 chip->last_soc_change_sec = last_change_sec;
1917
1918 pr_debug("last_soc = %d, calculated_soc = %d, soc = %d, time since last change = %d\n",
1919 chip->last_soc, chip->calculated_soc,
1920 soc, time_since_last_change_sec);
1921 chip->last_soc = bound_soc(soc);
1922 backup_soc_and_iavg(chip, batt_temp, chip->last_soc);
1923 pr_debug("Reported SOC = %d\n", chip->last_soc);
1924 chip->t_soc_queried = now;
1925 mutex_unlock(&chip->last_soc_mutex);
1926
1927 return soc;
1928}
1929
1930static int report_state_of_charge(struct qpnp_bms_chip *chip)
1931{
1932 if (bms_fake_battery != -EINVAL) {
1933 pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery);
1934 return bms_fake_battery;
1935 } else if (chip->use_voltage_soc)
1936 return report_voltage_based_soc(chip);
1937 else
1938 return report_cc_based_soc(chip);
1939}
1940
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07001941#define VDD_MAX_ERR 5000
1942#define VDD_STEP_SIZE 10000
1943#define MAX_COUNT_BEFORE_RESET_TO_CC 3
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001944static int charging_adjustments(struct qpnp_bms_chip *chip,
1945 struct soc_params *params, int soc,
1946 int vbat_uv, int ibat_ua, int batt_temp)
1947{
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001948 int chg_soc, soc_ibat, batt_terminal_uv, weight_ibat, weight_cc;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001949
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001950 batt_terminal_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001951
1952 if (chip->soc_at_cv == -EINVAL) {
Zhenhua Huang95a05d32014-03-31 18:09:45 +08001953 if (batt_terminal_uv >= chip->max_voltage_uv - VDD_MAX_ERR ||
1954 chip->in_taper_charge) {
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001955 chip->soc_at_cv = soc;
1956 chip->prev_chg_soc = soc;
Xiaozhe Shifc7af172013-11-04 14:15:44 -08001957 chip->ibat_at_cv_ua = params->iavg_ua;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001958 pr_debug("CC_TO_CV ibat_ua = %d CHG SOC %d\n",
1959 ibat_ua, soc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001960 } else {
1961 /* In constant current charging return the calc soc */
1962 pr_debug("CC CHG SOC %d\n", soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001963 }
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001964
1965 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07001966 chip->system_load_count = 0;
1967 return soc;
1968 } else if (ibat_ua > 0 && batt_terminal_uv
1969 < chip->max_voltage_uv - (VDD_MAX_ERR * 2)) {
1970 if (chip->system_load_count > MAX_COUNT_BEFORE_RESET_TO_CC) {
1971 chip->soc_at_cv = -EINVAL;
1972 pr_debug("Vbat below CV threshold, resetting CC_TO_CV\n");
1973 chip->system_load_count = 0;
1974 } else {
1975 chip->system_load_count += 1;
1976 pr_debug("Vbat below CV threshold, count: %d\n",
1977 chip->system_load_count);
1978 }
1979 return soc;
1980 } else if (ibat_ua > 0) {
1981 pr_debug("NOT CHARGING SOC %d\n", soc);
1982 chip->system_load_count = 0;
1983 chip->prev_chg_soc = soc;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001984 return soc;
1985 }
1986
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07001987 chip->system_load_count = 0;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001988 /*
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001989 * battery is in CV phase - begin linear interpolation of soc based on
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001990 * battery charge current
1991 */
1992
1993 /*
1994 * if voltage lessened (possibly because of a system load)
1995 * keep reporting the prev chg soc
1996 */
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001997 if (batt_terminal_uv <= chip->prev_batt_terminal_uv - VDD_STEP_SIZE) {
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08001998 pr_debug("batt_terminal_uv %d < (max = %d - 10000); CC CHG SOC %d\n",
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001999 batt_terminal_uv, chip->prev_batt_terminal_uv,
2000 chip->prev_chg_soc);
2001 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002002 return chip->prev_chg_soc;
2003 }
2004
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07002005 soc_ibat = bound_soc(linear_interpolate(chip->soc_at_cv,
2006 chip->ibat_at_cv_ua,
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08002007 100, -1 * chip->chg_term_ua,
Xiaozhe Shifc7af172013-11-04 14:15:44 -08002008 params->iavg_ua));
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07002009 weight_ibat = bound_soc(linear_interpolate(1, chip->soc_at_cv,
2010 100, 100, chip->prev_chg_soc));
2011 weight_cc = 100 - weight_ibat;
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07002012 chg_soc = bound_soc(DIV_ROUND_CLOSEST(soc_ibat * weight_ibat
2013 + weight_cc * soc, 100));
2014
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07002015 pr_debug("weight_ibat = %d, weight_cc = %d, soc_ibat = %d, soc_cc = %d\n",
2016 weight_ibat, weight_cc, soc_ibat, soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002017
2018 /* always report a higher soc */
2019 if (chg_soc > chip->prev_chg_soc) {
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002020 chip->prev_chg_soc = chg_soc;
2021
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002022 chip->charging_adjusted_ocv = find_ocv_for_pc(chip, batt_temp,
2023 find_pc_for_soc(chip, params, chg_soc));
2024 pr_debug("CC CHG ADJ OCV = %d CHG SOC %d\n",
2025 chip->charging_adjusted_ocv,
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002026 chip->prev_chg_soc);
2027 }
2028
2029 pr_debug("Reporting CHG SOC %d\n", chip->prev_chg_soc);
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08002030 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07002031 return chip->prev_chg_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002032}
2033
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002034static void very_low_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
2035{
2036 /*
2037 * if battery is very low (v_cutoff voltage + 20mv) hold
2038 * a wakelock untill soc = 0%
2039 */
2040 if (vbat_uv <= chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002041 && !wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002042 pr_debug("voltage = %d low holding wakelock\n", vbat_uv);
2043 wake_lock(&chip->low_voltage_wake_lock);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002044 } else if (vbat_uv > chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002045 && wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002046 pr_debug("voltage = %d releasing wakelock\n", vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002047 wake_unlock(&chip->low_voltage_wake_lock);
2048 }
2049}
2050
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002051#define VBATT_ERROR_MARGIN 20000
2052static void cv_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
2053{
2054 /*
2055 * if battery is very low (v_cutoff voltage + 20mv) hold
2056 * a wakelock untill soc = 0%
2057 */
2058 if (wake_lock_active(&chip->cv_wake_lock)) {
2059 if (chip->soc_at_cv != -EINVAL) {
2060 pr_debug("hit CV, releasing cv wakelock\n");
2061 wake_unlock(&chip->cv_wake_lock);
2062 } else if (!is_battery_charging(chip)) {
2063 pr_debug("charging stopped, releasing cv wakelock\n");
2064 wake_unlock(&chip->cv_wake_lock);
2065 }
2066 } else if (vbat_uv > chip->max_voltage_uv - VBATT_ERROR_MARGIN
2067 && chip->soc_at_cv == -EINVAL
2068 && is_battery_charging(chip)
2069 && !wake_lock_active(&chip->cv_wake_lock)) {
2070 pr_debug("voltage = %d holding cv wakelock\n", vbat_uv);
2071 wake_lock(&chip->cv_wake_lock);
2072 }
2073}
2074
Xiaozhe Shi2b647872013-10-31 14:30:27 -07002075#define NO_ADJUST_HIGH_SOC_THRESHOLD 98
Xiaozhe Shie118c692012-09-24 15:17:43 -07002076static int adjust_soc(struct qpnp_bms_chip *chip, struct soc_params *params,
2077 int soc, int batt_temp)
2078{
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002079 int ibat_ua = 0, vbat_uv = 0;
2080 int ocv_est_uv = 0, soc_est = 0, pc_est = 0, pc = 0;
2081 int delta_ocv_uv = 0;
2082 int n = 0;
2083 int rc_new_uah = 0;
2084 int pc_new = 0;
2085 int soc_new = 0;
2086 int slope = 0;
2087 int rc = 0;
2088 int delta_ocv_uv_limit = 0;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002089 int correction_limit_uv = 0;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002090
2091 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
2092 if (rc < 0) {
2093 pr_err("simultaneous vbat ibat failed err = %d\n", rc);
2094 goto out;
2095 }
2096
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002097 very_low_voltage_check(chip, vbat_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002098 cv_voltage_check(chip, vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002099
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002100 delta_ocv_uv_limit = DIV_ROUND_CLOSEST(ibat_ua, 1000);
2101
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08002102 ocv_est_uv = vbat_uv + (ibat_ua * params->rbatt_mohm)/1000;
2103
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002104 pc_est = calculate_pc(chip, ocv_est_uv, batt_temp);
2105 soc_est = div_s64((s64)params->fcc_uah * pc_est - params->uuc_uah*100,
2106 (s64)params->fcc_uah - params->uuc_uah);
2107 soc_est = bound_soc(soc_est);
2108
Xiaozhe Shi20640b52013-01-03 11:49:30 -08002109 /* never adjust during bms reset mode */
2110 if (bms_reset) {
2111 pr_debug("bms reset mode, SOC adjustment skipped\n");
2112 goto out;
2113 }
2114
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07002115 if (is_battery_charging(chip)) {
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002116 soc = charging_adjustments(chip, params, soc, vbat_uv, ibat_ua,
2117 batt_temp);
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07002118 /* Skip adjustments if we are in CV or ibat is negative */
2119 if (chip->soc_at_cv != -EINVAL || ibat_ua < 0)
2120 goto out;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002121 }
2122
2123 /*
2124 * do not adjust
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07002125 * if soc_est is same as what bms calculated
2126 * OR if soc_est > adjust_soc_low_threshold
2127 * OR if soc is above 90
2128 * because we might pull it low
2129 * and cause a bad user experience
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002130 */
Xiaozhe Shibd56b052013-10-21 11:51:30 -07002131 if (!wake_lock_active(&chip->low_voltage_wake_lock) &&
2132 (soc_est == soc
2133 || soc_est > chip->adjust_soc_low_threshold
2134 || soc >= NO_ADJUST_HIGH_SOC_THRESHOLD))
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002135 goto out;
2136
2137 if (chip->last_soc_est == -EINVAL)
2138 chip->last_soc_est = soc;
2139
2140 n = min(200, max(1 , soc + soc_est + chip->last_soc_est));
2141 chip->last_soc_est = soc_est;
2142
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002143 pc = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002144 if (pc > 0) {
2145 pc_new = calculate_pc(chip,
2146 chip->last_ocv_uv - (++slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002147 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002148 while (pc_new == pc) {
2149 /* start taking 10mV steps */
2150 slope = slope + 10;
2151 pc_new = calculate_pc(chip,
2152 chip->last_ocv_uv - (slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002153 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002154 }
2155 } else {
2156 /*
2157 * pc is already at the lowest point,
2158 * assume 1 millivolt translates to 1% pc
2159 */
2160 pc = 1;
2161 pc_new = 0;
2162 slope = 1;
2163 }
2164
2165 delta_ocv_uv = div_s64((soc - soc_est) * (s64)slope * 1000,
2166 n * (pc - pc_new));
2167
2168 if (abs(delta_ocv_uv) > delta_ocv_uv_limit) {
2169 pr_debug("limiting delta ocv %d limit = %d\n", delta_ocv_uv,
2170 delta_ocv_uv_limit);
2171
2172 if (delta_ocv_uv > 0)
2173 delta_ocv_uv = delta_ocv_uv_limit;
2174 else
2175 delta_ocv_uv = -1 * delta_ocv_uv_limit;
2176 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
2177 }
2178
Xiaozhe Shibd56b052013-10-21 11:51:30 -07002179 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2180 /* when in the cutoff region, do not correct upwards */
2181 delta_ocv_uv = max(0, delta_ocv_uv);
Xiaozhe Shi08d9aa72013-07-31 17:15:12 -07002182 goto skip_limits;
Xiaozhe Shibd56b052013-10-21 11:51:30 -07002183 }
Xiaozhe Shi08d9aa72013-07-31 17:15:12 -07002184
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002185 if (chip->last_ocv_uv > chip->flat_ocv_threshold_uv)
2186 correction_limit_uv = chip->high_ocv_correction_limit_uv;
2187 else
2188 correction_limit_uv = chip->low_ocv_correction_limit_uv;
2189
2190 if (abs(delta_ocv_uv) > correction_limit_uv) {
2191 pr_debug("limiting delta ocv %d limit = %d\n",
2192 delta_ocv_uv, correction_limit_uv);
2193 if (delta_ocv_uv > 0)
2194 delta_ocv_uv = correction_limit_uv;
2195 else
2196 delta_ocv_uv = -correction_limit_uv;
2197 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
2198 }
2199
Xiaozhe Shi08d9aa72013-07-31 17:15:12 -07002200skip_limits:
2201
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002202 chip->last_ocv_uv -= delta_ocv_uv;
2203
2204 if (chip->last_ocv_uv >= chip->max_voltage_uv)
2205 chip->last_ocv_uv = chip->max_voltage_uv;
2206
2207 /* calculate the soc based on this new ocv */
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002208 pc_new = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002209 rc_new_uah = (params->fcc_uah * pc_new) / 100;
2210 soc_new = (rc_new_uah - params->cc_uah - params->uuc_uah)*100
2211 / (params->fcc_uah - params->uuc_uah);
2212 soc_new = bound_soc(soc_new);
2213
2214 /*
2215 * if soc_new is ZERO force it higher so that phone doesnt report soc=0
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002216 * soc = 0 should happen only when soc_est is above a set value
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002217 */
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002218 if (soc_new == 0 && soc_est >= chip->hold_soc_est)
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002219 soc_new = 1;
2220
2221 soc = soc_new;
2222
2223out:
2224 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",
2225 ibat_ua, vbat_uv, ocv_est_uv, pc_est,
2226 soc_est, n, delta_ocv_uv, chip->last_ocv_uv,
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08002227 pc_new, soc_new, params->rbatt_mohm, slope);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002228
Xiaozhe Shie118c692012-09-24 15:17:43 -07002229 return soc;
2230}
2231
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002232static int clamp_soc_based_on_voltage(struct qpnp_bms_chip *chip, int soc)
2233{
2234 int rc, vbat_uv;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002235
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002236 rc = get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shi36458962013-02-06 16:19:57 -08002237 if (rc < 0) {
2238 pr_err("adc vbat failed err = %d\n", rc);
2239 return soc;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002240 }
Xiaozhe Shi9c14f3d2014-03-18 14:27:43 -07002241
2242 /* only clamp when discharging */
2243 if (is_battery_charging(chip))
2244 return soc;
2245
David Keitele50091e2014-03-05 09:55:57 -08002246 if (soc <= 0 && vbat_uv > chip->v_cutoff_uv) {
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002247 pr_debug("clamping soc to 1, vbat (%d) > cutoff (%d)\n",
2248 vbat_uv, chip->v_cutoff_uv);
2249 return 1;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002250 } else {
2251 pr_debug("not clamping, using soc = %d, vbat = %d and cutoff = %d\n",
2252 soc, vbat_uv, chip->v_cutoff_uv);
2253 return soc;
2254 }
2255}
2256
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002257static int64_t convert_cc_uah_to_raw(struct qpnp_bms_chip *chip, int64_t cc_uah)
2258{
2259 int64_t cc_uv, cc_pvh, cc_raw;
2260
2261 cc_pvh = cc_uah * chip->r_sense_uohm;
2262 cc_uv = div_s64(cc_pvh * SLEEP_CLK_HZ * SECONDS_PER_HOUR,
2263 CC_READING_TICKS * 1000000LL);
2264 cc_raw = div_s64(cc_uv * CC_READING_RESOLUTION_D,
2265 CC_READING_RESOLUTION_N);
2266 return cc_raw;
2267}
2268
2269#define CC_STEP_INCREMENT_UAH 1500
2270#define OCV_STEP_INCREMENT 0x10
2271static void configure_soc_wakeup(struct qpnp_bms_chip *chip,
2272 struct soc_params *params,
2273 int batt_temp, int target_soc)
2274{
2275 int target_ocv_uv;
2276 int64_t target_cc_uah, cc_raw_64, current_shdw_cc_raw_64;
2277 int64_t current_shdw_cc_uah, iadc_comp_factor;
2278 uint64_t cc_raw, current_shdw_cc_raw;
2279 int16_t ocv_raw, current_ocv_raw;
2280
2281 current_shdw_cc_raw = 0;
2282 mutex_lock(&chip->bms_output_lock);
2283 lock_output_data(chip);
2284 qpnp_read_wrapper(chip, (u8 *)&current_ocv_raw,
2285 chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
2286 unlock_output_data(chip);
2287 mutex_unlock(&chip->bms_output_lock);
2288 current_shdw_cc_uah = get_prop_bms_charge_counter_shadow(chip);
2289 current_shdw_cc_raw_64 = convert_cc_uah_to_raw(chip,
2290 current_shdw_cc_uah);
2291
2292 /*
2293 * Calculate the target shadow coulomb counter threshold for when
2294 * the SoC changes.
2295 *
2296 * Since the BMS driver resets the shadow coulomb counter every
2297 * 20 seconds when the device is awake, calculate the threshold as
2298 * a delta from the current shadow coulomb count.
2299 */
2300 target_cc_uah = (100 - target_soc)
2301 * (params->fcc_uah - params->uuc_uah)
2302 / 100 - current_shdw_cc_uah;
2303 if (target_cc_uah < 0) {
2304 /*
2305 * If the target cc is below 0, that means we have already
2306 * passed the point where SoC should have fallen.
2307 * Set a wakeup in a few more mAh and check back again
2308 */
2309 target_cc_uah = CC_STEP_INCREMENT_UAH;
2310 }
2311 iadc_comp_factor = 100000;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002312 qpnp_iadc_comp_result(chip->iadc_dev, &iadc_comp_factor);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002313 target_cc_uah = div64_s64(target_cc_uah * 100000, iadc_comp_factor);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002314 target_cc_uah = cc_reverse_adjust_for_gain(chip, target_cc_uah);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002315 cc_raw_64 = convert_cc_uah_to_raw(chip, target_cc_uah);
2316 cc_raw = convert_s64_to_s36(cc_raw_64);
2317
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002318 target_ocv_uv = find_ocv_for_pc(chip, batt_temp,
2319 find_pc_for_soc(chip, params, target_soc));
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002320 ocv_raw = convert_vbatt_uv_to_raw(chip, target_ocv_uv);
2321
2322 /*
2323 * If the current_ocv_raw was updated since reaching 100% and is lower
2324 * than the calculated target ocv threshold, set the new target
2325 * threshold 1.5mAh lower in order to check if the SoC changed yet.
2326 */
2327 if (current_ocv_raw != chip->ocv_reading_at_100
2328 && current_ocv_raw < ocv_raw)
2329 ocv_raw = current_ocv_raw - OCV_STEP_INCREMENT;
2330
2331 qpnp_write_wrapper(chip, (u8 *)&cc_raw,
2332 chip->base + BMS1_SW_CC_THR0, 5);
2333 qpnp_write_wrapper(chip, (u8 *)&ocv_raw,
2334 chip->base + BMS1_OCV_THR0, 2);
2335
Xiaozhe Shif5d87832013-11-21 17:05:39 -08002336 enable_bms_irq(&chip->ocv_thr_irq);
2337 enable_bms_irq(&chip->sw_cc_thr_irq);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002338 pr_debug("current sw_cc_raw = 0x%llx, current ocv = 0x%hx\n",
2339 current_shdw_cc_raw, (uint16_t)current_ocv_raw);
2340 pr_debug("target_cc_uah = %lld, raw64 = 0x%llx, raw 36 = 0x%llx, ocv_raw = 0x%hx\n",
2341 target_cc_uah,
2342 (uint64_t)cc_raw_64, cc_raw,
2343 (uint16_t)ocv_raw);
2344}
2345
Xiaozhe Shi5e791032013-10-25 11:30:42 -07002346#define BAD_SOC_THRESH -10
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002347static int calculate_raw_soc(struct qpnp_bms_chip *chip,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002348 struct raw_soc_params *raw,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002349 struct soc_params *params,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002350 int batt_temp)
2351{
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002352 int soc, remaining_usable_charge_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002353
Xiaozhe Shie118c692012-09-24 15:17:43 -07002354 /* calculate remaining usable charge */
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002355 remaining_usable_charge_uah = params->ocv_charge_uah
2356 - params->cc_uah
2357 - params->uuc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002358 pr_debug("RUC = %duAh\n", remaining_usable_charge_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002359
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002360 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002361 (params->fcc_uah - params->uuc_uah));
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002362
Xiaozhe Shi5e791032013-10-25 11:30:42 -07002363 if (chip->first_time_calc_soc && soc > BAD_SOC_THRESH && soc < 0) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07002364 /*
2365 * first time calcualtion and the pon ocv is too low resulting
2366 * in a bad soc. Adjust ocv to get 0 soc
2367 */
2368 pr_debug("soc is %d, adjusting pon ocv to make it 0\n", soc);
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002369 chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp,
2370 find_pc_for_soc(chip, params, 0));
2371 params->ocv_charge_uah = find_ocv_charge_for_soc(chip,
2372 params, 0);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002373
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002374 remaining_usable_charge_uah = params->ocv_charge_uah
2375 - params->cc_uah
2376 - params->uuc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002377
2378 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002379 (params->fcc_uah
2380 - params->uuc_uah));
Xiaozhe Shie118c692012-09-24 15:17:43 -07002381 pr_debug("DONE for O soc is %d, pon ocv adjusted to %duV\n",
2382 soc, chip->last_ocv_uv);
2383 }
2384
2385 if (soc > 100)
2386 soc = 100;
2387
Xiaozhe Shi5e791032013-10-25 11:30:42 -07002388 if (soc > BAD_SOC_THRESH && soc < 0) {
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002389 pr_debug("bad rem_usb_chg = %d rem_chg %d, cc_uah %d, unusb_chg %d\n",
Xiaozhe Shie118c692012-09-24 15:17:43 -07002390 remaining_usable_charge_uah,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002391 params->ocv_charge_uah,
2392 params->cc_uah, params->uuc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002393
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002394 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 -07002395 chip->last_ocv_uv, batt_temp,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002396 params->fcc_uah, soc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002397 soc = 0;
2398 }
2399
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002400 return soc;
2401}
2402
2403#define SLEEP_RECALC_INTERVAL 3
2404static int calculate_state_of_charge(struct qpnp_bms_chip *chip,
2405 struct raw_soc_params *raw,
2406 int batt_temp)
2407{
2408 struct soc_params params;
2409 int soc, previous_soc, shutdown_soc, new_calculated_soc;
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002410 int remaining_usable_charge_uah;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002411
2412 calculate_soc_params(chip, raw, &params, batt_temp);
2413 if (!is_battery_present(chip)) {
2414 pr_debug("battery gone, reporting 100\n");
2415 new_calculated_soc = 100;
2416 goto done_calculating;
2417 }
2418
2419 if (params.fcc_uah - params.uuc_uah <= 0) {
2420 pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
2421 params.fcc_uah,
2422 params.uuc_uah);
2423 new_calculated_soc = 0;
2424 goto done_calculating;
2425 }
2426
2427 soc = calculate_raw_soc(chip, raw, &params, batt_temp);
2428
Xiaozhe Shie118c692012-09-24 15:17:43 -07002429 mutex_lock(&chip->soc_invalidation_mutex);
2430 shutdown_soc = chip->shutdown_soc;
2431
2432 if (chip->first_time_calc_soc && soc != shutdown_soc
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002433 && !chip->shutdown_soc_invalid) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07002434 /*
2435 * soc for the first time - use shutdown soc
2436 * to adjust pon ocv since it is a small percent away from
2437 * the real soc
2438 */
2439 pr_debug("soc = %d before forcing shutdown_soc = %d\n",
2440 soc, shutdown_soc);
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002441 chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp,
2442 find_pc_for_soc(chip, &params, shutdown_soc));
2443 params.ocv_charge_uah = find_ocv_charge_for_soc(chip,
2444 &params, shutdown_soc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002445
2446 remaining_usable_charge_uah = params.ocv_charge_uah
2447 - params.cc_uah
2448 - params.uuc_uah;
2449
2450 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2451 (params.fcc_uah
2452 - params.uuc_uah));
2453
2454 pr_debug("DONE for shutdown_soc = %d soc is %d, adjusted ocv to %duV\n",
2455 shutdown_soc, soc, chip->last_ocv_uv);
2456 }
2457 mutex_unlock(&chip->soc_invalidation_mutex);
2458
Xiaozhe Shicbce8042014-02-13 14:08:47 -08002459 if (chip->first_time_calc_soc && !chip->shutdown_soc_invalid) {
2460 pr_debug("Skip adjustment when shutdown SOC has been forced\n");
2461 new_calculated_soc = soc;
2462 } else {
2463 pr_debug("SOC before adjustment = %d\n", soc);
2464 new_calculated_soc = adjust_soc(chip, &params, soc, batt_temp);
2465 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07002466
Xiaozhe Shi445d2492013-03-27 18:10:18 -07002467 /* always clamp soc due to BMS hw/sw immaturities */
2468 new_calculated_soc = clamp_soc_based_on_voltage(chip,
2469 new_calculated_soc);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002470 /*
2471 * If the battery is full, configure the cc threshold so the system
2472 * wakes up after SoC changes
2473 */
Xiaozhe Shif5d87832013-11-21 17:05:39 -08002474 if (is_battery_full(chip)) {
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002475 configure_soc_wakeup(chip, &params,
2476 batt_temp, bound_soc(new_calculated_soc - 1));
Xiaozhe Shif5d87832013-11-21 17:05:39 -08002477 } else {
2478 disable_bms_irq(&chip->ocv_thr_irq);
2479 disable_bms_irq(&chip->sw_cc_thr_irq);
2480 }
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002481done_calculating:
Xiaozhe Shifa6ea692013-05-31 11:15:13 -07002482 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07002483 previous_soc = chip->calculated_soc;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002484 chip->calculated_soc = new_calculated_soc;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002485 pr_debug("CC based calculated SOC = %d\n", chip->calculated_soc);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002486 if (chip->last_soc_invalid) {
2487 chip->last_soc_invalid = false;
2488 chip->last_soc = -EINVAL;
2489 }
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002490 /*
2491 * Check if more than a long time has passed since the last
2492 * calculation (more than n times compared to the soc recalculation
2493 * rate, where n is defined by SLEEP_RECALC_INTERVAL). If this is true,
2494 * then the system must have gone through a long sleep, and SoC can be
2495 * allowed to become unbounded by the last reported SoC
2496 */
2497 if (params.delta_time_s * 1000 >
2498 chip->calculate_soc_ms * SLEEP_RECALC_INTERVAL
2499 && !chip->first_time_calc_soc) {
2500 chip->last_soc_unbound = true;
2501 chip->last_soc_change_sec = chip->last_recalc_time;
2502 pr_debug("last_soc unbound because elapsed time = %d\n",
2503 params.delta_time_s);
2504 }
2505 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi27375822013-08-22 11:40:15 -07002506 wake_up_interruptible(&chip->bms_wait_queue);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07002507
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002508 if (new_calculated_soc != previous_soc && chip->bms_psy_registered) {
Xiaozhe Shi83484e32013-05-16 10:59:59 -07002509 power_supply_changed(&chip->bms_psy);
2510 pr_debug("power supply changed\n");
2511 } else {
2512 /*
2513 * Call report state of charge anyways to periodically update
2514 * reported SoC. This prevents reported SoC from being stuck
2515 * when calculated soc doesn't change.
2516 */
2517 report_state_of_charge(chip);
2518 }
2519
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002520 get_current_time(&chip->last_recalc_time);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002521 chip->first_time_calc_soc = 0;
Xiaozhe Shi70633922013-09-23 15:50:53 -07002522 chip->first_time_calc_uuc = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002523 return chip->calculated_soc;
2524}
2525
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002526static int calculate_soc_from_voltage(struct qpnp_bms_chip *chip)
2527{
2528 int voltage_range_uv, voltage_remaining_uv, voltage_based_soc;
Xiaozhe Shi36458962013-02-06 16:19:57 -08002529 int rc, vbat_uv;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002530
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002531 rc = get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shi36458962013-02-06 16:19:57 -08002532 if (rc < 0) {
2533 pr_err("adc vbat failed err = %d\n", rc);
2534 return rc;
2535 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002536 voltage_range_uv = chip->max_voltage_uv - chip->v_cutoff_uv;
2537 voltage_remaining_uv = vbat_uv - chip->v_cutoff_uv;
2538 voltage_based_soc = voltage_remaining_uv * 100 / voltage_range_uv;
2539
2540 voltage_based_soc = clamp(voltage_based_soc, 0, 100);
2541
2542 if (chip->prev_voltage_based_soc != voltage_based_soc
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002543 && chip->bms_psy_registered) {
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002544 power_supply_changed(&chip->bms_psy);
2545 pr_debug("power supply changed\n");
2546 }
2547 chip->prev_voltage_based_soc = voltage_based_soc;
2548
2549 pr_debug("vbat used = %duv\n", vbat_uv);
2550 pr_debug("Calculated voltage based soc = %d\n", voltage_based_soc);
2551 return voltage_based_soc;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08002552}
2553
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002554static int recalculate_raw_soc(struct qpnp_bms_chip *chip)
2555{
2556 int batt_temp, rc, soc;
2557 struct qpnp_vadc_result result;
2558 struct raw_soc_params raw;
2559 struct soc_params params;
2560
2561 bms_stay_awake(&chip->soc_wake_source);
2562 if (chip->use_voltage_soc) {
2563 soc = calculate_soc_from_voltage(chip);
2564 } else {
2565 if (!chip->batfet_closed)
Xiaozhe Shiffb7cfc2014-01-03 10:47:36 -08002566 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002567 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM,
2568 &result);
2569 if (rc) {
2570 pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n",
2571 LR_MUX1_BATT_THERM, rc);
2572 soc = chip->calculated_soc;
2573 } else {
2574 pr_debug("batt_temp phy = %lld meas = 0x%llx\n",
2575 result.physical,
2576 result.measurement);
2577 batt_temp = (int)result.physical;
2578
2579 mutex_lock(&chip->last_ocv_uv_mutex);
Xiaozhe Shied4a5522014-09-05 14:56:13 -07002580 rc = read_soc_params_raw(chip, &raw, batt_temp);
2581 if (rc) {
2582 pr_err("Unable to read params, rc: %d\n", rc);
2583 soc = 0;
2584 goto done;
2585 }
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002586 calculate_soc_params(chip, &raw, &params, batt_temp);
2587 if (!is_battery_present(chip)) {
2588 pr_debug("battery gone\n");
2589 soc = 0;
2590 } else if (params.fcc_uah - params.uuc_uah <= 0) {
2591 pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
2592 params.fcc_uah,
2593 params.uuc_uah);
2594 soc = 0;
2595 } else {
2596 soc = calculate_raw_soc(chip, &raw,
2597 &params, batt_temp);
2598 }
Xiaozhe Shied4a5522014-09-05 14:56:13 -07002599done:
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002600 mutex_unlock(&chip->last_ocv_uv_mutex);
2601 }
2602 }
2603 bms_relax(&chip->soc_wake_source);
2604 return soc;
2605}
2606
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002607static int recalculate_soc(struct qpnp_bms_chip *chip)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002608{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002609 int batt_temp, rc, soc;
2610 struct qpnp_vadc_result result;
2611 struct raw_soc_params raw;
2612
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002613 bms_stay_awake(&chip->soc_wake_source);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002614 mutex_lock(&chip->vbat_monitor_mutex);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002615 if (chip->vbat_monitor_params.state_request !=
2616 ADC_TM_HIGH_LOW_THR_DISABLE)
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002617 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2618 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002619 mutex_unlock(&chip->vbat_monitor_mutex);
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002620 if (chip->use_voltage_soc) {
2621 soc = calculate_soc_from_voltage(chip);
2622 } else {
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07002623 if (!chip->batfet_closed)
Xiaozhe Shiffb7cfc2014-01-03 10:47:36 -08002624 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002625 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM,
2626 &result);
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002627 if (rc) {
2628 pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n",
2629 LR_MUX1_BATT_THERM, rc);
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002630 soc = chip->calculated_soc;
2631 } else {
2632 pr_debug("batt_temp phy = %lld meas = 0x%llx\n",
2633 result.physical,
2634 result.measurement);
2635 batt_temp = (int)result.physical;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002636
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002637 mutex_lock(&chip->last_ocv_uv_mutex);
Xiaozhe Shied4a5522014-09-05 14:56:13 -07002638 rc = read_soc_params_raw(chip, &raw, batt_temp);
2639 if (rc) {
2640 pr_err("Unable to read params, rc: %d\n", rc);
2641 soc = chip->calculated_soc;
2642 } else {
2643 soc = calculate_state_of_charge(chip,
2644 &raw, batt_temp);
2645 }
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002646 mutex_unlock(&chip->last_ocv_uv_mutex);
2647 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002648 }
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002649 bms_relax(&chip->soc_wake_source);
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002650 return soc;
2651}
2652
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002653static void recalculate_work(struct work_struct *work)
2654{
2655 struct qpnp_bms_chip *chip = container_of(work,
2656 struct qpnp_bms_chip,
2657 recalc_work);
2658
2659 recalculate_soc(chip);
2660}
2661
Xiaozhe Shicb487b12013-10-14 17:42:07 -07002662static int get_calculation_delay_ms(struct qpnp_bms_chip *chip)
2663{
2664 if (wake_lock_active(&chip->low_voltage_wake_lock))
2665 return chip->low_voltage_calculate_soc_ms;
2666 else if (chip->calculated_soc < chip->low_soc_calc_threshold)
2667 return chip->low_soc_calculate_soc_ms;
2668 else
2669 return chip->calculate_soc_ms;
2670}
2671
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002672static void calculate_soc_work(struct work_struct *work)
2673{
2674 struct qpnp_bms_chip *chip = container_of(work,
2675 struct qpnp_bms_chip,
2676 calculate_soc_delayed_work.work);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002677
Xiaozhe Shicb487b12013-10-14 17:42:07 -07002678 recalculate_soc(chip);
2679 schedule_delayed_work(&chip->calculate_soc_delayed_work,
2680 round_jiffies_relative(msecs_to_jiffies
2681 (get_calculation_delay_ms(chip))));
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002682}
2683
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002684static void configure_vbat_monitor_low(struct qpnp_bms_chip *chip)
2685{
2686 mutex_lock(&chip->vbat_monitor_mutex);
2687 if (chip->vbat_monitor_params.state_request
2688 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2689 /*
2690 * Battery is now around or below v_cutoff
2691 */
2692 pr_debug("battery entered cutoff range\n");
2693 if (!wake_lock_active(&chip->low_voltage_wake_lock)) {
2694 pr_debug("voltage low, holding wakelock\n");
2695 wake_lock(&chip->low_voltage_wake_lock);
2696 cancel_delayed_work_sync(
2697 &chip->calculate_soc_delayed_work);
2698 schedule_delayed_work(
2699 &chip->calculate_soc_delayed_work, 0);
2700 }
2701 chip->vbat_monitor_params.state_request =
2702 ADC_TM_HIGH_THR_ENABLE;
2703 chip->vbat_monitor_params.high_thr =
2704 (chip->low_voltage_threshold + VBATT_ERROR_MARGIN);
2705 pr_debug("set low thr to %d and high to %d\n",
2706 chip->vbat_monitor_params.low_thr,
2707 chip->vbat_monitor_params.high_thr);
2708 chip->vbat_monitor_params.low_thr = 0;
2709 } else if (chip->vbat_monitor_params.state_request
2710 == ADC_TM_LOW_THR_ENABLE) {
2711 /*
2712 * Battery is in normal operation range.
2713 */
2714 pr_debug("battery entered normal range\n");
2715 if (wake_lock_active(&chip->cv_wake_lock)) {
2716 wake_unlock(&chip->cv_wake_lock);
2717 pr_debug("releasing cv wake lock\n");
2718 }
2719 chip->in_cv_range = false;
2720 chip->vbat_monitor_params.state_request =
2721 ADC_TM_HIGH_LOW_THR_ENABLE;
2722 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2723 - VBATT_ERROR_MARGIN;
2724 chip->vbat_monitor_params.low_thr =
2725 chip->low_voltage_threshold;
2726 pr_debug("set low thr to %d and high to %d\n",
2727 chip->vbat_monitor_params.low_thr,
2728 chip->vbat_monitor_params.high_thr);
2729 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002730 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2731 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002732 mutex_unlock(&chip->vbat_monitor_mutex);
2733}
2734
2735#define CV_LOW_THRESHOLD_HYST_UV 100000
2736static void configure_vbat_monitor_high(struct qpnp_bms_chip *chip)
2737{
2738 mutex_lock(&chip->vbat_monitor_mutex);
2739 if (chip->vbat_monitor_params.state_request
2740 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2741 /*
2742 * Battery is around vddmax
2743 */
2744 pr_debug("battery entered vddmax range\n");
2745 chip->in_cv_range = true;
2746 if (!wake_lock_active(&chip->cv_wake_lock)) {
2747 wake_lock(&chip->cv_wake_lock);
2748 pr_debug("holding cv wake lock\n");
2749 }
2750 schedule_work(&chip->recalc_work);
2751 chip->vbat_monitor_params.state_request =
2752 ADC_TM_LOW_THR_ENABLE;
2753 chip->vbat_monitor_params.low_thr =
2754 (chip->max_voltage_uv - CV_LOW_THRESHOLD_HYST_UV);
2755 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv * 2;
2756 pr_debug("set low thr to %d and high to %d\n",
2757 chip->vbat_monitor_params.low_thr,
2758 chip->vbat_monitor_params.high_thr);
2759 } else if (chip->vbat_monitor_params.state_request
2760 == ADC_TM_HIGH_THR_ENABLE) {
2761 /*
2762 * Battery is in normal operation range.
2763 */
2764 pr_debug("battery entered normal range\n");
2765 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2766 pr_debug("voltage high, releasing wakelock\n");
2767 wake_unlock(&chip->low_voltage_wake_lock);
2768 }
2769 chip->vbat_monitor_params.state_request =
2770 ADC_TM_HIGH_LOW_THR_ENABLE;
2771 chip->vbat_monitor_params.high_thr =
2772 chip->max_voltage_uv - VBATT_ERROR_MARGIN;
2773 chip->vbat_monitor_params.low_thr =
2774 chip->low_voltage_threshold;
2775 pr_debug("set low thr to %d and high to %d\n",
2776 chip->vbat_monitor_params.low_thr,
2777 chip->vbat_monitor_params.high_thr);
2778 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002779 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2780 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002781 mutex_unlock(&chip->vbat_monitor_mutex);
2782}
2783
2784static void btm_notify_vbat(enum qpnp_tm_state state, void *ctx)
2785{
2786 struct qpnp_bms_chip *chip = ctx;
2787 int vbat_uv;
2788 struct qpnp_vadc_result result;
2789 int rc;
2790
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002791 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &result);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002792 pr_debug("vbat = %lld, raw = 0x%x\n", result.physical, result.adc_code);
2793
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002794 get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002795 pr_debug("vbat is at %d, state is at %d\n", vbat_uv, state);
2796
2797 if (state == ADC_TM_LOW_STATE) {
2798 pr_debug("low voltage btm notification triggered\n");
2799 if (vbat_uv - VBATT_ERROR_MARGIN
2800 < chip->vbat_monitor_params.low_thr) {
2801 configure_vbat_monitor_low(chip);
2802 } else {
2803 pr_debug("faulty btm trigger, discarding\n");
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002804 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002805 &chip->vbat_monitor_params);
2806 }
2807 } else if (state == ADC_TM_HIGH_STATE) {
2808 pr_debug("high voltage btm notification triggered\n");
2809 if (vbat_uv + VBATT_ERROR_MARGIN
2810 > chip->vbat_monitor_params.high_thr) {
2811 configure_vbat_monitor_high(chip);
2812 } else {
2813 pr_debug("faulty btm trigger, discarding\n");
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002814 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002815 &chip->vbat_monitor_params);
2816 }
2817 } else {
2818 pr_debug("unknown voltage notification state: %d\n", state);
2819 }
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002820 if (chip->bms_psy_registered)
Xiaozhe Shifa120db2013-06-06 15:57:19 -07002821 power_supply_changed(&chip->bms_psy);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002822}
2823
2824static int reset_vbat_monitoring(struct qpnp_bms_chip *chip)
2825{
2826 int rc;
2827
2828 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_DISABLE;
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002829
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002830 rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2831 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002832 if (rc) {
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002833 pr_err("tm disable failed: %d\n", rc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002834 return rc;
2835 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002836 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2837 pr_debug("battery removed, releasing wakelock\n");
2838 wake_unlock(&chip->low_voltage_wake_lock);
2839 }
2840 if (chip->in_cv_range) {
2841 pr_debug("battery removed, removing in_cv_range state\n");
2842 chip->in_cv_range = false;
2843 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002844 return 0;
2845}
2846
2847static int setup_vbat_monitoring(struct qpnp_bms_chip *chip)
2848{
2849 int rc;
2850
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002851 chip->vbat_monitor_params.low_thr = chip->low_voltage_threshold;
2852 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2853 - VBATT_ERROR_MARGIN;
2854 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_ENABLE;
2855 chip->vbat_monitor_params.channel = VBAT_SNS;
2856 chip->vbat_monitor_params.btm_ctx = (void *)chip;
2857 chip->vbat_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
2858 chip->vbat_monitor_params.threshold_notification = &btm_notify_vbat;
2859 pr_debug("set low thr to %d and high to %d\n",
2860 chip->vbat_monitor_params.low_thr,
2861 chip->vbat_monitor_params.high_thr);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002862
2863 if (!is_battery_present(chip)) {
2864 pr_debug("no battery inserted, do not enable vbat monitoring\n");
2865 chip->vbat_monitor_params.state_request =
2866 ADC_TM_HIGH_LOW_THR_DISABLE;
2867 } else {
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002868 rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2869 &chip->vbat_monitor_params);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002870 if (rc) {
2871 pr_err("tm setup failed: %d\n", rc);
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002872 return rc;
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002873 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002874 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002875
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002876 pr_debug("setup complete\n");
2877 return 0;
2878}
2879
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302880static void readjust_fcc_table(struct qpnp_bms_chip *chip)
2881{
2882 struct single_row_lut *temp, *old;
2883 int i, fcc, ratio;
2884
2885 if (!chip->enable_fcc_learning)
2886 return;
2887
2888 if (!chip->fcc_temp_lut) {
2889 pr_err("The static fcc lut table is NULL\n");
2890 return;
2891 }
2892
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07002893 temp = devm_kzalloc(chip->dev, sizeof(struct single_row_lut),
2894 GFP_KERNEL);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302895 if (!temp) {
2896 pr_err("Cannot allocate memory for adjusted fcc table\n");
2897 return;
2898 }
2899
2900 fcc = interpolate_fcc(chip->fcc_temp_lut, chip->fcc_new_batt_temp);
2901
2902 temp->cols = chip->fcc_temp_lut->cols;
2903 for (i = 0; i < chip->fcc_temp_lut->cols; i++) {
2904 temp->x[i] = chip->fcc_temp_lut->x[i];
2905 ratio = div_u64(chip->fcc_temp_lut->y[i] * 1000, fcc);
2906 temp->y[i] = (ratio * chip->fcc_new_mah);
2907 temp->y[i] /= 1000;
2908 }
2909
2910 old = chip->adjusted_fcc_temp_lut;
2911 chip->adjusted_fcc_temp_lut = temp;
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07002912 devm_kfree(chip->dev, old);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302913}
2914
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302915static int read_fcc_data_from_backup(struct qpnp_bms_chip *chip)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302916{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302917 int rc, i;
2918 u8 fcc = 0, chgcyl = 0;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302919
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302920 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2921 rc = qpnp_read_wrapper(chip, &fcc,
2922 chip->base + BMS_FCC_BASE_REG + i, 1);
2923 rc |= qpnp_read_wrapper(chip, &chgcyl,
2924 chip->base + BMS_CHGCYL_BASE_REG + i, 1);
2925 if (rc) {
2926 pr_err("Unable to read FCC data\n");
2927 return rc;
2928 }
2929 if (fcc == 0 || (fcc == 0xFF && chgcyl == 0xFF)) {
2930 /* FCC invalid/not present */
2931 chip->fcc_learning_samples[i].fcc_new = 0;
2932 chip->fcc_learning_samples[i].chargecycles = 0;
2933 } else {
2934 /* valid FCC data */
2935 chip->fcc_sample_count++;
2936 chip->fcc_learning_samples[i].fcc_new =
2937 fcc * chip->fcc_resolution;
2938 chip->fcc_learning_samples[i].chargecycles =
2939 chgcyl * CHGCYL_RESOLUTION;
2940 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302941 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302942
2943 return 0;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302944}
2945
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302946static int discard_backup_fcc_data(struct qpnp_bms_chip *chip)
2947{
2948 int rc = 0, i;
2949 u8 temp_u8 = 0;
2950
2951 chip->fcc_sample_count = 0;
2952 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2953 rc = qpnp_write_wrapper(chip, &temp_u8,
2954 chip->base + BMS_FCC_BASE_REG + i, 1);
2955 rc |= qpnp_write_wrapper(chip, &temp_u8,
2956 chip->base + BMS_CHGCYL_BASE_REG + i, 1);
2957 if (rc) {
2958 pr_err("Unable to clear FCC data\n");
2959 return rc;
2960 }
2961 }
2962
2963 return 0;
2964}
2965
2966static void
2967average_fcc_samples_and_readjust_fcc_table(struct qpnp_bms_chip *chip)
2968{
2969 int i, temp_fcc_avg = 0, temp_fcc_delta = 0, new_fcc_avg = 0;
2970 struct fcc_sample *ft;
2971
2972 for (i = 0; i < chip->min_fcc_learning_samples; i++)
2973 temp_fcc_avg += chip->fcc_learning_samples[i].fcc_new;
2974
2975 temp_fcc_avg /= chip->min_fcc_learning_samples;
2976 temp_fcc_delta = div_u64(temp_fcc_avg * DELTA_FCC_PERCENT, 100);
2977
2978 /* fix the fcc if its an outlier i.e. > 5% of the average */
2979 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2980 ft = &chip->fcc_learning_samples[i];
2981 if (abs(ft->fcc_new - temp_fcc_avg) > temp_fcc_delta)
2982 new_fcc_avg += temp_fcc_avg;
2983 else
2984 new_fcc_avg += ft->fcc_new;
2985 }
2986 new_fcc_avg /= chip->min_fcc_learning_samples;
2987
2988 chip->fcc_new_mah = new_fcc_avg;
2989 chip->fcc_new_batt_temp = FCC_DEFAULT_TEMP;
2990 pr_info("FCC update: New fcc_mah=%d, fcc_batt_temp=%d\n",
2991 new_fcc_avg, FCC_DEFAULT_TEMP);
2992 readjust_fcc_table(chip);
2993}
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302994
2995static void backup_charge_cycle(struct qpnp_bms_chip *chip)
2996{
2997 int rc = 0;
2998
2999 if (chip->charge_increase >= 0) {
3000 rc = qpnp_write_wrapper(chip, &chip->charge_increase,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303001 chip->base + CHARGE_INCREASE_STORAGE, 1);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303002 if (rc)
3003 pr_err("Unable to backup charge_increase\n");
3004 }
3005
3006 if (chip->charge_cycles >= 0) {
3007 rc = qpnp_write_wrapper(chip, (u8 *)&chip->charge_cycles,
3008 chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
3009 if (rc)
3010 pr_err("Unable to backup charge_cycles\n");
3011 }
3012}
3013
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303014static bool chargecycles_in_range(struct qpnp_bms_chip *chip)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303015{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303016 int i, min_cycle, max_cycle, valid_range;
3017
3018 /* find the smallest and largest charge cycle */
3019 max_cycle = min_cycle = chip->fcc_learning_samples[0].chargecycles;
3020 for (i = 1; i < chip->min_fcc_learning_samples; i++) {
3021 if (min_cycle > chip->fcc_learning_samples[i].chargecycles)
3022 min_cycle = chip->fcc_learning_samples[i].chargecycles;
3023 if (max_cycle < chip->fcc_learning_samples[i].chargecycles)
3024 max_cycle = chip->fcc_learning_samples[i].chargecycles;
3025 }
3026
3027 /* check if chargecyles are in range to continue with FCC update */
3028 valid_range = DIV_ROUND_UP(VALID_FCC_CHGCYL_RANGE,
3029 CHGCYL_RESOLUTION) * CHGCYL_RESOLUTION;
3030 if (abs(max_cycle - min_cycle) > valid_range)
3031 return false;
3032
3033 return true;
3034}
3035
3036static int read_chgcycle_data_from_backup(struct qpnp_bms_chip *chip)
3037{
3038 int rc;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303039 uint16_t temp_u16 = 0;
3040 u8 temp_u8 = 0;
3041
3042 rc = qpnp_read_wrapper(chip, &temp_u8,
3043 chip->base + CHARGE_INCREASE_STORAGE, 1);
3044 if (!rc && temp_u8 != 0xFF)
3045 chip->charge_increase = temp_u8;
3046
3047 rc = qpnp_read_wrapper(chip, (u8 *)&temp_u16,
3048 chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
3049 if (!rc && temp_u16 != 0xFFFF)
3050 chip->charge_cycles = temp_u16;
3051
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303052 return rc;
3053}
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303054
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303055static void
3056attempt_learning_new_fcc(struct qpnp_bms_chip *chip)
3057{
3058 pr_debug("Total FCC sample count=%d\n", chip->fcc_sample_count);
3059
3060 /* update FCC if we have the required samples */
3061 if ((chip->fcc_sample_count == chip->min_fcc_learning_samples) &&
3062 chargecycles_in_range(chip))
3063 average_fcc_samples_and_readjust_fcc_table(chip);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303064}
3065
3066static int calculate_real_soc(struct qpnp_bms_chip *chip,
3067 int batt_temp, struct raw_soc_params *raw, int cc_uah)
3068{
3069 int fcc_uah, rc_uah;
3070
3071 fcc_uah = calculate_fcc(chip, batt_temp);
3072 rc_uah = calculate_ocv_charge(chip, raw, fcc_uah);
3073
3074 return ((rc_uah - cc_uah) * 100) / fcc_uah;
3075}
3076
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303077#define MAX_U8_VALUE ((u8)(~0U))
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303078
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303079static int backup_new_fcc(struct qpnp_bms_chip *chip, int fcc_mah,
3080 int chargecycles)
3081{
3082 int rc, min_cycle, i;
3083 u8 fcc_new, chgcyl, pos = 0;
3084 struct fcc_sample *ft;
3085
3086 if ((fcc_mah > (chip->fcc_resolution * MAX_U8_VALUE)) ||
3087 (chargecycles > (CHGCYL_RESOLUTION * MAX_U8_VALUE))) {
3088 pr_warn("FCC/Chgcyl beyond storage limit. FCC=%d, chgcyl=%d\n",
3089 fcc_mah, chargecycles);
3090 return -EINVAL;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303091 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303092
3093 if (chip->fcc_sample_count == chip->min_fcc_learning_samples) {
3094 /* search best location - oldest entry */
3095 min_cycle = chip->fcc_learning_samples[0].chargecycles;
3096 for (i = 1; i < chip->min_fcc_learning_samples; i++) {
3097 if (min_cycle >
Zhenhua Huang82663ea2014-09-26 10:42:31 +08003098 chip->fcc_learning_samples[i].chargecycles) {
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303099 pos = i;
Zhenhua Huang82663ea2014-09-26 10:42:31 +08003100 break;
3101 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303102 }
3103 } else {
3104 /* find an empty location */
3105 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
3106 ft = &chip->fcc_learning_samples[i];
3107 if (ft->fcc_new == 0 || (ft->fcc_new == 0xFF &&
3108 ft->chargecycles == 0xFF)) {
3109 pos = i;
3110 break;
3111 }
3112 }
3113 chip->fcc_sample_count++;
3114 }
3115 chip->fcc_learning_samples[pos].fcc_new = fcc_mah;
3116 chip->fcc_learning_samples[pos].chargecycles = chargecycles;
3117
3118 fcc_new = DIV_ROUND_UP(fcc_mah, chip->fcc_resolution);
3119 rc = qpnp_write_wrapper(chip, (u8 *)&fcc_new,
3120 chip->base + BMS_FCC_BASE_REG + pos, 1);
3121 if (rc)
3122 return rc;
3123
3124 chgcyl = DIV_ROUND_UP(chargecycles, CHGCYL_RESOLUTION);
3125 rc = qpnp_write_wrapper(chip, (u8 *)&chgcyl,
3126 chip->base + BMS_CHGCYL_BASE_REG + pos, 1);
3127 if (rc)
3128 return rc;
3129
3130 pr_debug("Backup new FCC: fcc_new=%d, chargecycle=%d, pos=%d\n",
3131 fcc_new, chgcyl, pos);
3132
3133 return rc;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303134}
3135
3136static void update_fcc_learning_table(struct qpnp_bms_chip *chip,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303137 int new_fcc_uah, int chargecycles, int batt_temp)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303138{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303139 int rc, fcc_default, fcc_temp;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303140
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303141 /* convert the fcc at batt_temp to new fcc at FCC_DEFAULT_TEMP */
3142 fcc_default = calculate_fcc(chip, FCC_DEFAULT_TEMP) / 1000;
3143 fcc_temp = calculate_fcc(chip, batt_temp) / 1000;
3144 new_fcc_uah = (new_fcc_uah / fcc_temp) * fcc_default;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303145
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303146 rc = backup_new_fcc(chip, new_fcc_uah / 1000, chargecycles);
3147 if (rc) {
3148 pr_err("Unable to backup new FCC\n");
3149 return;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303150 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303151 /* check if FCC can be updated */
3152 attempt_learning_new_fcc(chip);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303153}
3154
3155static bool is_new_fcc_valid(int new_fcc_uah, int fcc_uah)
3156{
3157 if ((new_fcc_uah >= (fcc_uah / 2)) &&
3158 ((new_fcc_uah * 100) <= (fcc_uah * 105)))
3159 return true;
3160
3161 pr_debug("FCC rejected - not within valid limit\n");
3162 return false;
3163}
3164
3165static void fcc_learning_config(struct qpnp_bms_chip *chip, bool start)
3166{
3167 int rc, batt_temp;
3168 struct raw_soc_params raw;
3169 struct qpnp_vadc_result result;
3170 int fcc_uah, new_fcc_uah, delta_cc_uah, delta_soc;
3171
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003172 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303173 if (rc) {
3174 pr_err("Unable to read batt_temp\n");
3175 return;
3176 } else {
3177 batt_temp = (int)result.physical;
3178 }
3179
3180 rc = read_soc_params_raw(chip, &raw, batt_temp);
3181 if (rc) {
3182 pr_err("Unable to read CC, cannot update FCC\n");
3183 return;
3184 }
3185
3186 if (start) {
3187 chip->start_pc = interpolate_pc(chip->pc_temp_ocv_lut,
Xiaozhe Shiae4375f2013-11-11 10:55:04 -08003188 batt_temp, raw.last_good_ocv_uv / 1000);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003189 chip->start_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303190 chip->start_real_soc = calculate_real_soc(chip,
3191 batt_temp, &raw, chip->start_cc_uah);
3192 pr_debug("start_pc=%d, start_cc=%d, start_soc=%d real_soc=%d\n",
3193 chip->start_pc, chip->start_cc_uah,
3194 chip->start_soc, chip->start_real_soc);
3195 } else {
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003196 chip->end_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303197 delta_soc = 100 - chip->start_real_soc;
3198 delta_cc_uah = abs(chip->end_cc_uah - chip->start_cc_uah);
3199 new_fcc_uah = div_u64(delta_cc_uah * 100, delta_soc);
3200 fcc_uah = calculate_fcc(chip, batt_temp);
3201 pr_debug("start_soc=%d, start_pc=%d, start_real_soc=%d, start_cc=%d, end_cc=%d, new_fcc=%d\n",
3202 chip->start_soc, chip->start_pc, chip->start_real_soc,
3203 chip->start_cc_uah, chip->end_cc_uah, new_fcc_uah);
3204
3205 if (is_new_fcc_valid(new_fcc_uah, fcc_uah))
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303206 update_fcc_learning_table(chip, new_fcc_uah,
3207 chip->charge_cycles, batt_temp);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303208 }
3209}
3210
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003211#define MAX_CAL_TRIES 200
3212#define MIN_CAL_UA 3000
3213static void batfet_open_work(struct work_struct *work)
3214{
3215 int i;
3216 int rc;
3217 int result_ua;
3218 u8 orig_delay, sample_delay;
3219 struct qpnp_bms_chip *chip = container_of(work,
3220 struct qpnp_bms_chip,
3221 batfet_open_work);
3222
3223 rc = qpnp_read_wrapper(chip, &orig_delay,
3224 chip->base + BMS1_S1_DELAY_CTL, 1);
3225
3226 sample_delay = 0x0;
3227 rc = qpnp_write_wrapper(chip, &sample_delay,
3228 chip->base + BMS1_S1_DELAY_CTL, 1);
3229
3230 /*
3231 * In certain PMICs there is a coupling issue which causes
3232 * bad calibration value that result in a huge battery current
3233 * even when the BATFET is open. Do continious calibrations until
3234 * we hit reasonable cal values which result in low battery current
3235 */
3236
3237 for (i = 0; (!chip->batfet_closed) && i < MAX_CAL_TRIES; i++) {
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003238 rc = qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003239 /*
3240 * Wait 20mS after calibration and before reading battery
3241 * current. The BMS h/w uses calibration values in the
3242 * next sampling of vsense.
3243 */
3244 msleep(20);
3245 rc |= get_battery_current(chip, &result_ua);
3246 if (rc == 0 && abs(result_ua) <= MIN_CAL_UA) {
3247 pr_debug("good cal at %d attempt\n", i);
3248 break;
3249 }
3250 }
3251 pr_debug("batfet_closed = %d i = %d result_ua = %d\n",
3252 chip->batfet_closed, i, result_ua);
3253
3254 rc = qpnp_write_wrapper(chip, &orig_delay,
3255 chip->base + BMS1_S1_DELAY_CTL, 1);
3256}
3257
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003258static void charging_began(struct qpnp_bms_chip *chip)
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003259{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003260 mutex_lock(&chip->last_soc_mutex);
3261 chip->charge_start_tm_sec = 0;
3262 chip->catch_up_time_sec = 0;
3263 mutex_unlock(&chip->last_soc_mutex);
3264
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303265 chip->start_soc = report_state_of_charge(chip);
3266
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003267 mutex_lock(&chip->last_ocv_uv_mutex);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303268 if (chip->enable_fcc_learning)
3269 fcc_learning_config(chip, true);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003270 chip->soc_at_cv = -EINVAL;
3271 chip->prev_chg_soc = -EINVAL;
3272 mutex_unlock(&chip->last_ocv_uv_mutex);
3273}
3274
3275static void charging_ended(struct qpnp_bms_chip *chip)
3276{
3277 mutex_lock(&chip->last_soc_mutex);
3278 chip->charge_start_tm_sec = 0;
3279 chip->catch_up_time_sec = 0;
3280 mutex_unlock(&chip->last_soc_mutex);
3281
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303282 chip->end_soc = report_state_of_charge(chip);
3283
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003284 mutex_lock(&chip->last_ocv_uv_mutex);
3285 chip->soc_at_cv = -EINVAL;
3286 chip->prev_chg_soc = -EINVAL;
Zhenhua Huang95a05d32014-03-31 18:09:45 +08003287 chip->in_taper_charge = false;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303288
3289 /* update the chargecycles */
3290 if (chip->end_soc > chip->start_soc) {
3291 chip->charge_increase += (chip->end_soc - chip->start_soc);
3292 if (chip->charge_increase > 100) {
3293 chip->charge_cycles++;
3294 chip->charge_increase = chip->charge_increase % 100;
3295 }
3296 if (chip->enable_fcc_learning)
3297 backup_charge_cycle(chip);
3298 }
3299
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003300 if (get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL) {
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303301 if (chip->enable_fcc_learning &&
3302 (chip->start_soc <= chip->min_fcc_learning_soc) &&
3303 (chip->start_pc <= chip->min_fcc_ocv_pc))
3304 fcc_learning_config(chip, false);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003305 chip->done_charging = true;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003306 chip->last_soc_invalid = true;
Xiaozhe Shicc48e992013-05-28 16:42:24 -07003307 } else if (chip->charging_adjusted_ocv > 0) {
3308 pr_debug("Charging stopped before full, adjusted OCV = %d\n",
3309 chip->charging_adjusted_ocv);
3310 chip->last_ocv_uv = chip->charging_adjusted_ocv;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003311 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303312
Xiaozhe Shicc48e992013-05-28 16:42:24 -07003313 chip->charging_adjusted_ocv = -EINVAL;
3314
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003315 mutex_unlock(&chip->last_ocv_uv_mutex);
3316}
3317
3318static void battery_status_check(struct qpnp_bms_chip *chip)
3319{
3320 int status = get_battery_status(chip);
3321
Xiaozhe Shibda84992013-09-05 10:39:11 -07003322 mutex_lock(&chip->status_lock);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003323 if (chip->battery_status != status) {
Xiaozhe Shi30e94802013-08-19 16:40:53 -07003324 pr_debug("status = %d, shadow status = %d\n",
3325 status, chip->battery_status);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003326 if (status == POWER_SUPPLY_STATUS_CHARGING) {
3327 pr_debug("charging started\n");
3328 charging_began(chip);
3329 } else if (chip->battery_status
3330 == POWER_SUPPLY_STATUS_CHARGING) {
3331 pr_debug("charging ended\n");
3332 charging_ended(chip);
3333 }
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003334
3335 if (status == POWER_SUPPLY_STATUS_FULL) {
3336 pr_debug("battery full\n");
Xiaozhe Shibda84992013-09-05 10:39:11 -07003337 recalculate_soc(chip);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003338 } else if (chip->battery_status
3339 == POWER_SUPPLY_STATUS_FULL) {
3340 pr_debug("battery not full any more\n");
3341 disable_bms_irq(&chip->ocv_thr_irq);
3342 disable_bms_irq(&chip->sw_cc_thr_irq);
3343 }
3344
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003345 chip->battery_status = status;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003346 /* battery charge status has changed, so force a soc
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003347 * recalculation to update the SoC */
3348 schedule_work(&chip->recalc_work);
3349 }
Xiaozhe Shibda84992013-09-05 10:39:11 -07003350 mutex_unlock(&chip->status_lock);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003351}
3352
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003353#define CALIB_WRKARND_DIG_MAJOR_MAX 0x03
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003354static void batfet_status_check(struct qpnp_bms_chip *chip)
3355{
3356 bool batfet_closed;
3357
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003358 batfet_closed = is_batfet_closed(chip);
3359 if (chip->batfet_closed != batfet_closed) {
3360 chip->batfet_closed = batfet_closed;
Xiaozhe Shiffb7cfc2014-01-03 10:47:36 -08003361 if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX)
3362 return;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003363 if (batfet_closed == false) {
3364 /* batfet opened */
3365 schedule_work(&chip->batfet_open_work);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003366 qpnp_iadc_skip_calibration(chip->iadc_dev);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003367 } else {
3368 /* batfet closed */
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003369 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, true);
3370 qpnp_iadc_resume_calibration(chip->iadc_dev);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003371 }
3372 }
3373}
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003374
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003375static void battery_insertion_check(struct qpnp_bms_chip *chip)
3376{
Xiaozhe Shi90f3a412013-08-21 10:31:35 -07003377 int present = (int)is_battery_present(chip);
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07003378 int insertion_ocv_uv = get_battery_insertion_ocv_uv(chip);
3379 int insertion_ocv_taken = (insertion_ocv_uv > 0);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003380
3381 mutex_lock(&chip->vbat_monitor_mutex);
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07003382 if (chip->battery_present != present
3383 && (present == insertion_ocv_taken
3384 || chip->battery_present == -EINVAL)) {
3385 pr_debug("status = %d, shadow status = %d, insertion_ocv_uv = %d\n",
3386 present, chip->battery_present,
3387 insertion_ocv_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003388 if (chip->battery_present != -EINVAL) {
3389 if (present) {
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07003390 chip->insertion_ocv_uv = insertion_ocv_uv;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003391 setup_vbat_monitoring(chip);
3392 chip->new_battery = true;
3393 } else {
3394 reset_vbat_monitoring(chip);
3395 }
3396 }
3397 chip->battery_present = present;
3398 /* a new battery was inserted or removed, so force a soc
3399 * recalculation to update the SoC */
3400 schedule_work(&chip->recalc_work);
3401 }
3402 mutex_unlock(&chip->vbat_monitor_mutex);
3403}
3404
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003405/* Returns capacity as a SoC percentage between 0 and 100 */
3406static int get_prop_bms_capacity(struct qpnp_bms_chip *chip)
3407{
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08003408 return report_state_of_charge(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003409}
3410
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003411static void qpnp_bms_external_power_changed(struct power_supply *psy)
3412{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003413 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
3414 bms_psy);
3415
3416 battery_insertion_check(chip);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003417 batfet_status_check(chip);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003418 battery_status_check(chip);
Zhenhua Huang95a05d32014-03-31 18:09:45 +08003419
3420 if (POWER_SUPPLY_CHARGE_TYPE_TAPER == get_battery_charge_type(chip))
3421 chip->in_taper_charge = true;
3422 else
3423 chip->in_taper_charge = false;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003424}
3425
3426static int qpnp_bms_power_get_property(struct power_supply *psy,
3427 enum power_supply_property psp,
3428 union power_supply_propval *val)
3429{
3430 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
3431 bms_psy);
3432
3433 switch (psp) {
3434 case POWER_SUPPLY_PROP_CAPACITY:
3435 val->intval = get_prop_bms_capacity(chip);
3436 break;
Xiaozhe Shibda84992013-09-05 10:39:11 -07003437 case POWER_SUPPLY_PROP_STATUS:
3438 val->intval = chip->battery_status;
3439 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003440 case POWER_SUPPLY_PROP_CURRENT_NOW:
3441 val->intval = get_prop_bms_current_now(chip);
3442 break;
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07003443 case POWER_SUPPLY_PROP_RESISTANCE:
3444 val->intval = get_prop_bms_batt_resistance(chip);
3445 break;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07003446 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
3447 val->intval = get_prop_bms_charge_counter(chip);
3448 break;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003449 case POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW:
3450 val->intval = get_prop_bms_charge_counter_shadow(chip);
3451 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003452 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
3453 val->intval = get_prop_bms_charge_full_design(chip);
3454 break;
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05303455 case POWER_SUPPLY_PROP_CHARGE_FULL:
3456 val->intval = get_prop_bms_charge_full(chip);
3457 break;
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05303458 case POWER_SUPPLY_PROP_CYCLE_COUNT:
3459 val->intval = chip->charge_cycles;
3460 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003461 default:
3462 return -EINVAL;
3463 }
3464 return 0;
3465}
3466
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003467#define OCV_USE_LIMIT_EN BIT(7)
3468static int set_ocv_voltage_thresholds(struct qpnp_bms_chip *chip,
3469 int low_voltage_threshold,
3470 int high_voltage_threshold)
3471{
3472 uint16_t low_voltage_raw, high_voltage_raw;
3473 int rc;
3474
3475 low_voltage_raw = convert_vbatt_uv_to_raw(chip,
3476 low_voltage_threshold);
3477 high_voltage_raw = convert_vbatt_uv_to_raw(chip,
3478 high_voltage_threshold);
3479 rc = qpnp_write_wrapper(chip, (u8 *)&low_voltage_raw,
3480 chip->base + BMS1_OCV_USE_LOW_LIMIT_THR0, 2);
3481 if (rc) {
3482 pr_err("Failed to set ocv low voltage threshold: %d\n", rc);
3483 return rc;
3484 }
3485 rc = qpnp_write_wrapper(chip, (u8 *)&high_voltage_raw,
3486 chip->base + BMS1_OCV_USE_HIGH_LIMIT_THR0, 2);
3487 if (rc) {
3488 pr_err("Failed to set ocv high voltage threshold: %d\n", rc);
3489 return rc;
3490 }
3491 rc = qpnp_masked_write(chip, BMS1_OCV_USE_LIMIT_CTL,
3492 OCV_USE_LIMIT_EN, OCV_USE_LIMIT_EN);
3493 if (rc) {
3494 pr_err("Failed to enabled ocv voltage thresholds: %d\n", rc);
3495 return rc;
3496 }
3497 pr_debug("ocv low threshold set to %d uv or 0x%x raw\n",
3498 low_voltage_threshold, low_voltage_raw);
3499 pr_debug("ocv high threshold set to %d uv or 0x%x raw\n",
3500 high_voltage_threshold, high_voltage_raw);
3501 return 0;
3502}
3503
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003504static int read_shutdown_iavg_ma(struct qpnp_bms_chip *chip)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003505{
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003506 u8 iavg;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003507 int rc;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003508
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003509 rc = qpnp_read_wrapper(chip, &iavg, chip->base + IAVG_STORAGE_REG, 1);
3510 if (rc) {
3511 pr_err("failed to read addr = %d %d assuming %d\n",
3512 chip->base + IAVG_STORAGE_REG, rc,
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003513 MIN_IAVG_MA);
3514 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003515 } else if (iavg == IAVG_INVALID) {
3516 pr_err("invalid iavg read from BMS1_DATA_REG_1, using %d\n",
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003517 MIN_IAVG_MA);
3518 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003519 } else {
3520 if (iavg == 0)
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003521 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003522 else
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003523 return MIN_IAVG_MA + IAVG_STEP_SIZE_MA * iavg;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003524 }
3525}
3526
3527static int read_shutdown_soc(struct qpnp_bms_chip *chip)
3528{
3529 u8 stored_soc;
3530 int rc, shutdown_soc;
3531
3532 /*
3533 * The previous SOC is stored in the first 7 bits of the register as
3534 * (Shutdown SOC + 1). This allows for register reset values of both
3535 * 0x00 and 0x7F.
3536 */
3537 rc = qpnp_read_wrapper(chip, &stored_soc, chip->soc_storage_addr, 1);
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003538 if (rc) {
3539 pr_err("failed to read addr = %d %d\n",
3540 chip->soc_storage_addr, rc);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003541 return SOC_INVALID;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003542 }
3543
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003544 if ((stored_soc >> 1) > 0)
3545 shutdown_soc = (stored_soc >> 1) - 1;
3546 else
3547 shutdown_soc = SOC_INVALID;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003548
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003549 pr_debug("stored soc = 0x%02x, shutdown_soc = %d\n",
3550 stored_soc, shutdown_soc);
3551 return shutdown_soc;
3552}
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003553
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003554#define BAT_REMOVED_OFFMODE_BIT BIT(6)
3555static bool is_battery_replaced_in_offmode(struct qpnp_bms_chip *chip)
3556{
3557 u8 batt_pres;
3558 int rc;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003559
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003560 if (chip->batt_pres_addr) {
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003561 rc = qpnp_read_wrapper(chip, &batt_pres,
3562 chip->batt_pres_addr, 1);
3563 pr_debug("offmode removed: %02x\n", batt_pres);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003564 if (!rc && (batt_pres & BAT_REMOVED_OFFMODE_BIT))
3565 return true;
3566 }
3567 return false;
3568}
3569
3570static void load_shutdown_data(struct qpnp_bms_chip *chip)
3571{
3572 int calculated_soc, shutdown_soc;
3573 bool invalid_stored_soc;
3574 bool offmode_battery_replaced;
3575 bool shutdown_soc_out_of_limit;
3576
3577 /*
3578 * Read the saved shutdown SoC from the configured register and
3579 * check if the value has been reset
3580 */
3581 shutdown_soc = read_shutdown_soc(chip);
3582 invalid_stored_soc = (shutdown_soc == SOC_INVALID);
3583
3584 /*
3585 * Do a quick run of SoC calculation to find whether the shutdown soc
3586 * is close enough.
3587 */
Xiaozhe Shi2c171172013-12-03 13:27:37 -08003588 chip->shutdown_iavg_ma = MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003589 calculated_soc = recalculate_raw_soc(chip);
3590 shutdown_soc_out_of_limit = (abs(shutdown_soc - calculated_soc)
3591 > chip->shutdown_soc_valid_limit);
3592 pr_debug("calculated_soc = %d, valid_limit = %d\n",
3593 calculated_soc, chip->shutdown_soc_valid_limit);
3594
3595 /*
3596 * Check if the battery has been replaced while the system was powered
3597 * down.
3598 */
3599 offmode_battery_replaced = is_battery_replaced_in_offmode(chip);
3600
3601 /* Invalidate the shutdown SoC if any of these conditions hold true */
3602 if (chip->ignore_shutdown_soc
3603 || invalid_stored_soc
3604 || offmode_battery_replaced
3605 || shutdown_soc_out_of_limit) {
3606 chip->battery_removed = true;
3607 chip->shutdown_soc_invalid = true;
Xiaozhe Shic92cfd92013-10-25 11:36:42 -07003608 chip->shutdown_iavg_ma = MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003609 pr_debug("Ignoring shutdown SoC: invalid = %d, offmode = %d, out_of_limit = %d\n",
3610 invalid_stored_soc, offmode_battery_replaced,
3611 shutdown_soc_out_of_limit);
3612 } else {
3613 chip->shutdown_iavg_ma = read_shutdown_iavg_ma(chip);
3614 chip->shutdown_soc = shutdown_soc;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07003615 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303616
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003617 pr_debug("raw_soc = %d shutdown_soc = %d shutdown_iavg = %d shutdown_soc_invalid = %d, battery_removed = %d\n",
3618 calculated_soc,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003619 chip->shutdown_soc,
3620 chip->shutdown_iavg_ma,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303621 chip->shutdown_soc_invalid,
3622 chip->battery_removed);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003623}
3624
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003625static irqreturn_t bms_ocv_thr_irq_handler(int irq, void *_chip)
3626{
3627 struct qpnp_bms_chip *chip = _chip;
3628
3629 pr_debug("ocv_thr irq triggered\n");
3630 bms_stay_awake(&chip->soc_wake_source);
3631 schedule_work(&chip->recalc_work);
3632 return IRQ_HANDLED;
3633}
3634
3635static irqreturn_t bms_sw_cc_thr_irq_handler(int irq, void *_chip)
3636{
3637 struct qpnp_bms_chip *chip = _chip;
3638
3639 pr_debug("sw_cc_thr irq triggered\n");
Anirudh Ghayal1166eef2013-12-23 19:05:33 +05303640 disable_bms_irq_nosync(&chip->sw_cc_thr_irq);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003641 bms_stay_awake(&chip->soc_wake_source);
3642 schedule_work(&chip->recalc_work);
3643 return IRQ_HANDLED;
3644}
3645
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003646static int64_t read_battery_id(struct qpnp_bms_chip *chip)
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003647{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003648 int rc;
3649 struct qpnp_vadc_result result;
3650
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003651 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX2_BAT_ID, &result);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003652 if (rc) {
3653 pr_err("error reading batt id channel = %d, rc = %d\n",
3654 LR_MUX2_BAT_ID, rc);
3655 return rc;
3656 }
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003657
3658 return result.physical;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003659}
3660
3661static int set_battery_data(struct qpnp_bms_chip *chip)
3662{
3663 int64_t battery_id;
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003664 int rc = 0, dt_data = false;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003665 struct bms_battery_data *batt_data;
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003666 struct device_node *node;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003667
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003668 if (chip->batt_type == BATT_DESAY) {
3669 batt_data = &desay_5200_data;
3670 } else if (chip->batt_type == BATT_PALLADIUM) {
3671 batt_data = &palladium_1500_data;
3672 } else if (chip->batt_type == BATT_OEM) {
3673 batt_data = &oem_batt_data;
Wu Fenglin2ac88aa2013-04-25 12:43:40 +08003674 } else if (chip->batt_type == BATT_QRD_4V35_2000MAH) {
3675 batt_data = &QRD_4v35_2000mAh_data;
tingtingf50326f2013-06-05 15:07:24 +08003676 } else if (chip->batt_type == BATT_QRD_4V2_1300MAH) {
3677 batt_data = &qrd_4v2_1300mah_data;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003678 } else {
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003679 battery_id = read_battery_id(chip);
3680 if (battery_id < 0) {
3681 pr_err("cannot read battery id err = %lld\n",
3682 battery_id);
3683 return battery_id;
3684 }
3685
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003686 node = of_find_node_by_name(chip->spmi->dev.of_node,
3687 "qcom,battery-data");
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003688 if (!node) {
3689 pr_warn("No available batterydata, using palladium 1500\n");
3690 batt_data = &palladium_1500_data;
3691 goto assign_data;
3692 }
3693 batt_data = devm_kzalloc(chip->dev,
3694 sizeof(struct bms_battery_data), GFP_KERNEL);
3695 if (!batt_data) {
3696 pr_err("Could not alloc battery data\n");
3697 batt_data = &palladium_1500_data;
3698 goto assign_data;
3699 }
3700 batt_data->fcc_temp_lut = devm_kzalloc(chip->dev,
3701 sizeof(struct single_row_lut),
3702 GFP_KERNEL);
3703 batt_data->pc_temp_ocv_lut = devm_kzalloc(chip->dev,
3704 sizeof(struct pc_temp_ocv_lut),
3705 GFP_KERNEL);
3706 batt_data->rbatt_sf_lut = devm_kzalloc(chip->dev,
3707 sizeof(struct sf_lut),
3708 GFP_KERNEL);
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003709
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003710 batt_data->max_voltage_uv = -1;
3711 batt_data->cutoff_uv = -1;
3712 batt_data->iterm_ua = -1;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003713
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003714 /*
3715 * if the alloced luts are 0s, of_batterydata_read_data ignores
3716 * them.
3717 */
3718 rc = of_batterydata_read_data(node, batt_data, battery_id);
3719 if (rc == 0 && batt_data->fcc_temp_lut
3720 && batt_data->pc_temp_ocv_lut
3721 && batt_data->rbatt_sf_lut) {
3722 dt_data = true;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003723 } else {
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003724 pr_err("battery data load failed, using palladium 1500\n");
3725 devm_kfree(chip->dev, batt_data->fcc_temp_lut);
3726 devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut);
3727 devm_kfree(chip->dev, batt_data->rbatt_sf_lut);
3728 devm_kfree(chip->dev, batt_data);
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003729 batt_data = &palladium_1500_data;
3730 }
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003731 }
3732
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003733assign_data:
Xiaozhe Shi976618f2013-04-30 10:49:30 -07003734 chip->fcc_mah = batt_data->fcc;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003735 chip->fcc_temp_lut = batt_data->fcc_temp_lut;
3736 chip->fcc_sf_lut = batt_data->fcc_sf_lut;
3737 chip->pc_temp_ocv_lut = batt_data->pc_temp_ocv_lut;
3738 chip->pc_sf_lut = batt_data->pc_sf_lut;
3739 chip->rbatt_sf_lut = batt_data->rbatt_sf_lut;
3740 chip->default_rbatt_mohm = batt_data->default_rbatt_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07003741 chip->rbatt_capacitive_mohm = batt_data->rbatt_capacitive_mohm;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07003742 chip->flat_ocv_threshold_uv = batt_data->flat_ocv_threshold_uv;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003743
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003744 /* Override battery properties if specified in the battery profile */
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003745 if (batt_data->max_voltage_uv >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003746 chip->max_voltage_uv = batt_data->max_voltage_uv;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003747 if (batt_data->cutoff_uv >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003748 chip->v_cutoff_uv = batt_data->cutoff_uv;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003749 if (batt_data->iterm_ua >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003750 chip->chg_term_ua = batt_data->iterm_ua;
3751
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003752 if (chip->pc_temp_ocv_lut == NULL) {
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003753 pr_err("temp ocv lut table has not been loaded\n");
3754 if (dt_data) {
3755 devm_kfree(chip->dev, batt_data->fcc_temp_lut);
3756 devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut);
3757 devm_kfree(chip->dev, batt_data->rbatt_sf_lut);
3758 devm_kfree(chip->dev, batt_data);
3759 }
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003760 return -EINVAL;
3761 }
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003762
3763 if (dt_data)
3764 devm_kfree(chip->dev, batt_data);
3765
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003766 return 0;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003767}
3768
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07003769static int bms_get_adc(struct qpnp_bms_chip *chip,
3770 struct spmi_device *spmi)
3771{
3772 int rc = 0;
3773
3774 chip->vadc_dev = qpnp_get_vadc(&spmi->dev, "bms");
3775 if (IS_ERR(chip->vadc_dev)) {
3776 rc = PTR_ERR(chip->vadc_dev);
3777 if (rc != -EPROBE_DEFER)
3778 pr_err("vadc property missing, rc=%d\n", rc);
3779 return rc;
3780 }
3781
3782 chip->iadc_dev = qpnp_get_iadc(&spmi->dev, "bms");
3783 if (IS_ERR(chip->iadc_dev)) {
3784 rc = PTR_ERR(chip->iadc_dev);
3785 if (rc != -EPROBE_DEFER)
3786 pr_err("iadc property missing, rc=%d\n", rc);
3787 return rc;
3788 }
3789
3790 chip->adc_tm_dev = qpnp_get_adc_tm(&spmi->dev, "bms");
3791 if (IS_ERR(chip->adc_tm_dev)) {
3792 rc = PTR_ERR(chip->adc_tm_dev);
3793 if (rc != -EPROBE_DEFER)
3794 pr_err("adc-tm not ready, defer probe\n");
3795 return rc;
3796 }
3797
3798 return 0;
3799}
3800
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003801#define SPMI_PROP_READ(chip_prop, qpnp_spmi_property, retval) \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003802do { \
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003803 if (retval) \
3804 break; \
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003805 retval = of_property_read_u32(chip->spmi->dev.of_node, \
Xiaozhe Shi9bd24622013-01-23 15:54:54 -08003806 "qcom," qpnp_spmi_property, \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003807 &chip->chip_prop); \
3808 if (retval) { \
3809 pr_err("Error reading " #qpnp_spmi_property \
3810 " property %d\n", rc); \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003811 } \
3812} while (0)
3813
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303814#define SPMI_PROP_READ_BOOL(chip_prop, qpnp_spmi_property) \
3815do { \
3816 chip->chip_prop = of_property_read_bool(chip->spmi->dev.of_node,\
3817 "qcom," qpnp_spmi_property); \
3818} while (0)
3819
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003820static inline int bms_read_properties(struct qpnp_bms_chip *chip)
3821{
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003822 int rc = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003823
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003824 SPMI_PROP_READ(r_sense_uohm, "r-sense-uohm", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003825 SPMI_PROP_READ(v_cutoff_uv, "v-cutoff-uv", rc);
3826 SPMI_PROP_READ(max_voltage_uv, "max-voltage-uv", rc);
3827 SPMI_PROP_READ(r_conn_mohm, "r-conn-mohm", rc);
3828 SPMI_PROP_READ(chg_term_ua, "chg-term-ua", rc);
3829 SPMI_PROP_READ(shutdown_soc_valid_limit,
3830 "shutdown-soc-valid-limit", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003831 SPMI_PROP_READ(adjust_soc_low_threshold,
3832 "adjust-soc-low-threshold", rc);
3833 SPMI_PROP_READ(batt_type, "batt-type", rc);
3834 SPMI_PROP_READ(low_soc_calc_threshold,
3835 "low-soc-calculate-soc-threshold", rc);
3836 SPMI_PROP_READ(low_soc_calculate_soc_ms,
3837 "low-soc-calculate-soc-ms", rc);
Xiaozhe Shicb487b12013-10-14 17:42:07 -07003838 SPMI_PROP_READ(low_voltage_calculate_soc_ms,
3839 "low-voltage-calculate-soc-ms", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003840 SPMI_PROP_READ(calculate_soc_ms, "calculate-soc-ms", rc);
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07003841 SPMI_PROP_READ(high_ocv_correction_limit_uv,
3842 "high-ocv-correction-limit-uv", rc);
3843 SPMI_PROP_READ(low_ocv_correction_limit_uv,
3844 "low-ocv-correction-limit-uv", rc);
3845 SPMI_PROP_READ(hold_soc_est,
3846 "hold-soc-est", rc);
3847 SPMI_PROP_READ(ocv_high_threshold_uv,
3848 "ocv-voltage-high-threshold-uv", rc);
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003849 SPMI_PROP_READ(ocv_low_threshold_uv,
3850 "ocv-voltage-low-threshold-uv", rc);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08003851 SPMI_PROP_READ(low_voltage_threshold, "low-voltage-threshold", rc);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003852 SPMI_PROP_READ(temperature_margin, "tm-temp-margin", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003853
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003854 chip->use_external_rsense = of_property_read_bool(
3855 chip->spmi->dev.of_node,
3856 "qcom,use-external-rsense");
3857 chip->ignore_shutdown_soc = of_property_read_bool(
3858 chip->spmi->dev.of_node,
3859 "qcom,ignore-shutdown-soc");
3860 chip->use_voltage_soc = of_property_read_bool(chip->spmi->dev.of_node,
3861 "qcom,use-voltage-soc");
3862 chip->use_ocv_thresholds = of_property_read_bool(
3863 chip->spmi->dev.of_node,
3864 "qcom,use-ocv-thresholds");
3865
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003866 if (chip->adjust_soc_low_threshold >= 45)
3867 chip->adjust_soc_low_threshold = 45;
3868
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303869 SPMI_PROP_READ_BOOL(enable_fcc_learning, "enable-fcc-learning");
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303870 if (chip->enable_fcc_learning) {
3871 SPMI_PROP_READ(min_fcc_learning_soc,
3872 "min-fcc-learning-soc", rc);
3873 SPMI_PROP_READ(min_fcc_ocv_pc,
3874 "min-fcc-ocv-pc", rc);
3875 SPMI_PROP_READ(min_fcc_learning_samples,
3876 "min-fcc-learning-samples", rc);
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303877 SPMI_PROP_READ(fcc_resolution,
3878 "fcc-resolution", rc);
3879 if (chip->min_fcc_learning_samples > MAX_FCC_CYCLES)
3880 chip->min_fcc_learning_samples = MAX_FCC_CYCLES;
3881 chip->fcc_learning_samples = devm_kzalloc(&chip->spmi->dev,
3882 (sizeof(struct fcc_sample) *
3883 chip->min_fcc_learning_samples), GFP_KERNEL);
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003884 if (chip->fcc_learning_samples == NULL)
3885 return -ENOMEM;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303886 pr_debug("min-fcc-soc=%d, min-fcc-pc=%d, min-fcc-cycles=%d\n",
3887 chip->min_fcc_learning_soc, chip->min_fcc_ocv_pc,
3888 chip->min_fcc_learning_samples);
3889 }
3890
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003891 if (rc) {
3892 pr_err("Missing required properties.\n");
3893 return rc;
3894 }
3895
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003896 pr_debug("dts data: r_sense_uohm:%d, v_cutoff_uv:%d, max_v:%d\n",
3897 chip->r_sense_uohm, chip->v_cutoff_uv,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003898 chip->max_voltage_uv);
3899 pr_debug("r_conn:%d, shutdown_soc: %d, adjust_soc_low:%d\n",
3900 chip->r_conn_mohm, chip->shutdown_soc_valid_limit,
3901 chip->adjust_soc_low_threshold);
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07003902 pr_debug("chg_term_ua:%d, batt_type:%d\n",
3903 chip->chg_term_ua,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003904 chip->batt_type);
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08003905 pr_debug("ignore_shutdown_soc:%d, use_voltage_soc:%d\n",
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08003906 chip->ignore_shutdown_soc, chip->use_voltage_soc);
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003907 pr_debug("use external rsense: %d\n", chip->use_external_rsense);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003908 return 0;
3909}
3910
3911static inline void bms_initialize_constants(struct qpnp_bms_chip *chip)
3912{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003913 chip->prev_pc_unusable = -EINVAL;
3914 chip->soc_at_cv = -EINVAL;
3915 chip->calculated_soc = -EINVAL;
Xiaozhe Shie118c692012-09-24 15:17:43 -07003916 chip->last_soc = -EINVAL;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07003917 chip->last_soc_est = -EINVAL;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003918 chip->battery_present = -EINVAL;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003919 chip->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08003920 chip->last_cc_uah = INT_MIN;
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08003921 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
3922 chip->prev_last_good_ocv_raw = OCV_RAW_UNINITIALIZED;
Xiaozhe Shie118c692012-09-24 15:17:43 -07003923 chip->first_time_calc_soc = 1;
3924 chip->first_time_calc_uuc = 1;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003925}
3926
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003927#define SPMI_FIND_IRQ(chip, irq_name) \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003928do { \
3929 chip->irq_name##_irq.irq = spmi_get_irq_byname(chip->spmi, \
3930 resource, #irq_name); \
3931 if (chip->irq_name##_irq.irq < 0) { \
3932 pr_err("Unable to get " #irq_name " irq\n"); \
3933 return -ENXIO; \
3934 } \
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003935} while (0)
3936
3937static int bms_find_irqs(struct qpnp_bms_chip *chip,
3938 struct spmi_resource *resource)
3939{
3940 SPMI_FIND_IRQ(chip, sw_cc_thr);
3941 SPMI_FIND_IRQ(chip, ocv_thr);
3942 return 0;
3943}
3944
3945#define SPMI_REQUEST_IRQ(chip, rc, irq_name) \
3946do { \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003947 rc = devm_request_irq(chip->dev, chip->irq_name##_irq.irq, \
3948 bms_##irq_name##_irq_handler, \
3949 IRQF_TRIGGER_RISING, #irq_name, chip); \
3950 if (rc < 0) { \
3951 pr_err("Unable to request " #irq_name " irq: %d\n", rc);\
3952 return -ENXIO; \
3953 } \
Xiaozhe Shif511a6e2014-02-20 14:37:18 -08003954 chip->irq_name##_irq.ready = true; \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003955} while (0)
3956
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003957static int bms_request_irqs(struct qpnp_bms_chip *chip)
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003958{
3959 int rc;
3960
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003961 SPMI_REQUEST_IRQ(chip, rc, sw_cc_thr);
Abhijeet Dharmapurikardce21e62013-08-07 15:42:32 -07003962 disable_bms_irq(&chip->sw_cc_thr_irq);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003963 enable_irq_wake(chip->sw_cc_thr_irq.irq);
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003964 SPMI_REQUEST_IRQ(chip, rc, ocv_thr);
Abhijeet Dharmapurikardce21e62013-08-07 15:42:32 -07003965 disable_bms_irq(&chip->ocv_thr_irq);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003966 enable_irq_wake(chip->ocv_thr_irq.irq);
3967 return 0;
3968}
3969
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003970#define REG_OFFSET_PERP_TYPE 0x04
3971#define REG_OFFSET_PERP_SUBTYPE 0x05
3972#define BMS_BMS_TYPE 0xD
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003973#define BMS_BMS1_SUBTYPE 0x1
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003974#define BMS_IADC_TYPE 0x8
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003975#define BMS_IADC1_SUBTYPE 0x3
3976#define BMS_IADC2_SUBTYPE 0x5
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003977
3978static int register_spmi(struct qpnp_bms_chip *chip, struct spmi_device *spmi)
3979{
3980 struct spmi_resource *spmi_resource;
3981 struct resource *resource;
3982 int rc;
3983 u8 type, subtype;
3984
3985 chip->dev = &(spmi->dev);
3986 chip->spmi = spmi;
3987
3988 spmi_for_each_container_dev(spmi_resource, spmi) {
3989 if (!spmi_resource) {
3990 pr_err("qpnp_bms: spmi resource absent\n");
3991 return -ENXIO;
3992 }
3993
3994 resource = spmi_get_resource(spmi, spmi_resource,
3995 IORESOURCE_MEM, 0);
3996 if (!(resource && resource->start)) {
3997 pr_err("node %s IO resource absent!\n",
3998 spmi->dev.of_node->full_name);
3999 return -ENXIO;
4000 }
4001
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07004002 pr_debug("Node name = %s\n", spmi_resource->of_node->name);
4003
4004 if (strcmp("qcom,batt-pres-status",
4005 spmi_resource->of_node->name) == 0) {
4006 chip->batt_pres_addr = resource->start;
4007 continue;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07004008 } else if (strcmp("qcom,soc-storage-reg",
4009 spmi_resource->of_node->name) == 0) {
4010 chip->soc_storage_addr = resource->start;
4011 continue;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07004012 }
4013
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004014 rc = qpnp_read_wrapper(chip, &type,
4015 resource->start + REG_OFFSET_PERP_TYPE, 1);
4016 if (rc) {
4017 pr_err("Peripheral type read failed rc=%d\n", rc);
4018 return rc;
4019 }
4020 rc = qpnp_read_wrapper(chip, &subtype,
4021 resource->start + REG_OFFSET_PERP_SUBTYPE, 1);
4022 if (rc) {
4023 pr_err("Peripheral subtype read failed rc=%d\n", rc);
4024 return rc;
4025 }
4026
Xiaozhe Shief6274c2013-03-06 15:23:52 -08004027 if (type == BMS_BMS_TYPE && subtype == BMS_BMS1_SUBTYPE) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004028 chip->base = resource->start;
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07004029 rc = bms_find_irqs(chip, spmi_resource);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004030 if (rc) {
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07004031 pr_err("Could not find irqs\n");
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004032 return rc;
4033 }
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004034 } else if (type == BMS_IADC_TYPE
Xiaozhe Shief6274c2013-03-06 15:23:52 -08004035 && (subtype == BMS_IADC1_SUBTYPE
4036 || subtype == BMS_IADC2_SUBTYPE)) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004037 chip->iadc_base = resource->start;
4038 } else {
4039 pr_err("Invalid peripheral start=0x%x type=0x%x, subtype=0x%x\n",
4040 resource->start, type, subtype);
4041 }
4042 }
4043
4044 if (chip->base == 0) {
4045 dev_err(&spmi->dev, "BMS peripheral was not registered\n");
4046 return -EINVAL;
4047 }
4048 if (chip->iadc_base == 0) {
4049 dev_err(&spmi->dev, "BMS_IADC peripheral was not registered\n");
4050 return -EINVAL;
4051 }
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07004052 if (chip->soc_storage_addr == 0) {
4053 /* default to dvdd backed BMS data reg0 */
4054 chip->soc_storage_addr = chip->base + SOC_STORAGE_REG;
4055 }
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004056
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07004057 pr_debug("bms-base = 0x%04x, iadc-base = 0x%04x, bat-pres-reg = 0x%04x, soc-storage-reg = 0x%04x\n",
4058 chip->base, chip->iadc_base,
4059 chip->batt_pres_addr, chip->soc_storage_addr);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004060 return 0;
4061}
4062
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004063#define ADC_CH_SEL_MASK 0x7
4064#define ADC_INT_RSNSN_CTL_MASK 0x3
4065#define ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE 0x2
4066#define FAST_AVG_EN_MASK 0x80
4067#define FAST_AVG_EN_VALUE_EXT_RSENSE 0x80
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004068static int read_iadc_channel_select(struct qpnp_bms_chip *chip)
4069{
4070 u8 iadc_channel_select;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004071 int32_t rds_rsense_nohm;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004072 int rc;
4073
4074 rc = qpnp_read_wrapper(chip, &iadc_channel_select,
4075 chip->iadc_base + IADC1_BMS_ADC_CH_SEL_CTL, 1);
4076 if (rc) {
4077 pr_err("Error reading bms_iadc channel register %d\n", rc);
4078 return rc;
4079 }
4080
4081 iadc_channel_select &= ADC_CH_SEL_MASK;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004082 if (iadc_channel_select != EXTERNAL_RSENSE
4083 && iadc_channel_select != INTERNAL_RSENSE) {
4084 pr_err("IADC1_BMS_IADC configured incorrectly. Selected channel = %d\n",
4085 iadc_channel_select);
4086 return -EINVAL;
4087 }
4088
4089 if (chip->use_external_rsense) {
4090 pr_debug("External rsense selected\n");
4091 if (iadc_channel_select == INTERNAL_RSENSE) {
4092 pr_debug("Internal rsense detected; Changing rsense to external\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004093 rc = qpnp_masked_write_iadc(chip,
4094 IADC1_BMS_ADC_CH_SEL_CTL,
4095 ADC_CH_SEL_MASK,
4096 EXTERNAL_RSENSE);
4097 if (rc) {
4098 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
4099 IADC1_BMS_ADC_CH_SEL_CTL,
4100 EXTERNAL_RSENSE, rc);
4101 return rc;
4102 }
Xiaozhe Shif3da8622013-06-10 14:50:56 -07004103 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07004104 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07004105 chip->software_shdw_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004106 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004107 } else {
4108 pr_debug("Internal rsense selected\n");
4109 if (iadc_channel_select == EXTERNAL_RSENSE) {
4110 pr_debug("External rsense detected; Changing rsense to internal\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004111 rc = qpnp_masked_write_iadc(chip,
4112 IADC1_BMS_ADC_CH_SEL_CTL,
4113 ADC_CH_SEL_MASK,
4114 INTERNAL_RSENSE);
4115 if (rc) {
4116 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
4117 IADC1_BMS_ADC_CH_SEL_CTL,
4118 INTERNAL_RSENSE, rc);
4119 return rc;
4120 }
Xiaozhe Shif3da8622013-06-10 14:50:56 -07004121 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
4122 chip->software_shdw_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004123 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004124
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07004125 rc = qpnp_iadc_get_rsense(chip->iadc_dev, &rds_rsense_nohm);
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004126 if (rc) {
4127 pr_err("Unable to read RDS resistance value from IADC; rc = %d\n",
4128 rc);
4129 return rc;
4130 }
Xiaozhe Shid0a79542012-11-06 10:00:38 -08004131 chip->r_sense_uohm = rds_rsense_nohm/1000;
4132 pr_debug("rds_rsense = %d nOhm, saved as %d uOhm\n",
4133 rds_rsense_nohm, chip->r_sense_uohm);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004134 }
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004135 /* prevent shorting of leads by IADC_BMS when external Rsense is used */
4136 if (chip->use_external_rsense) {
4137 if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX) {
4138 rc = qpnp_masked_write_iadc(chip,
4139 IADC1_BMS_ADC_INT_RSNSN_CTL,
4140 ADC_INT_RSNSN_CTL_MASK,
4141 ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE);
4142 if (rc) {
4143 pr_err("Unable to set batfet config %x to %x: %d\n",
4144 IADC1_BMS_ADC_INT_RSNSN_CTL,
4145 ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE, rc);
4146 return rc;
4147 }
4148 } else {
4149 /* In older PMICS use FAST_AVG_EN register bit 7 */
4150 rc = qpnp_masked_write_iadc(chip,
4151 IADC1_BMS_FAST_AVG_EN,
4152 FAST_AVG_EN_MASK,
4153 FAST_AVG_EN_VALUE_EXT_RSENSE);
4154 if (rc) {
4155 pr_err("Unable to set batfet config %x to %x: %d\n",
4156 IADC1_BMS_FAST_AVG_EN,
4157 FAST_AVG_EN_VALUE_EXT_RSENSE, rc);
4158 return rc;
4159 }
4160 }
4161 }
4162
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004163 return 0;
4164}
4165
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004166static int refresh_die_temp_monitor(struct qpnp_bms_chip *chip)
4167{
4168 struct qpnp_vadc_result result;
4169 int rc;
4170
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07004171 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004172
4173 pr_debug("low = %lld, high = %lld\n",
4174 result.physical - chip->temperature_margin,
4175 result.physical + chip->temperature_margin);
4176 chip->die_temp_monitor_params.high_temp = result.physical
4177 + chip->temperature_margin;
4178 chip->die_temp_monitor_params.low_temp = result.physical
4179 - chip->temperature_margin;
4180 chip->die_temp_monitor_params.state_request =
4181 ADC_TM_HIGH_LOW_THR_ENABLE;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004182 return qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
4183 &chip->die_temp_monitor_params);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004184}
4185
4186static void btm_notify_die_temp(enum qpnp_tm_state state, void *ctx)
4187{
4188 struct qpnp_bms_chip *chip = ctx;
4189 struct qpnp_vadc_result result;
4190 int rc;
4191
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07004192 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004193
4194 if (state == ADC_TM_LOW_STATE)
4195 pr_debug("low state triggered\n");
4196 else if (state == ADC_TM_HIGH_STATE)
4197 pr_debug("high state triggered\n");
4198 pr_debug("die temp = %lld, raw = 0x%x\n",
4199 result.physical, result.adc_code);
4200 schedule_work(&chip->recalc_work);
4201 refresh_die_temp_monitor(chip);
4202}
4203
4204static int setup_die_temp_monitoring(struct qpnp_bms_chip *chip)
4205{
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004206 int rc;
4207
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004208 chip->die_temp_monitor_params.channel = DIE_TEMP;
4209 chip->die_temp_monitor_params.btm_ctx = (void *)chip;
4210 chip->die_temp_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
4211 chip->die_temp_monitor_params.threshold_notification =
4212 &btm_notify_die_temp;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004213 rc = refresh_die_temp_monitor(chip);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004214 if (rc) {
4215 pr_err("tm setup failed: %d\n", rc);
4216 return rc;
4217 }
4218 pr_debug("setup complete\n");
4219 return 0;
4220}
4221
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004222static int __devinit qpnp_bms_probe(struct spmi_device *spmi)
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004223{
4224 struct qpnp_bms_chip *chip;
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004225 bool warm_reset;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004226 int rc, vbatt;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004227
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07004228 chip = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_bms_chip),
4229 GFP_KERNEL);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004230
4231 if (chip == NULL) {
4232 pr_err("kzalloc() failed.\n");
4233 return -ENOMEM;
4234 }
4235
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004236 rc = bms_get_adc(chip, spmi);
4237 if (rc < 0)
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004238 goto error_read;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004239
Xiaozhe Shi3eaf0b62013-07-11 09:48:08 -07004240 mutex_init(&chip->bms_output_lock);
4241 mutex_init(&chip->last_ocv_uv_mutex);
4242 mutex_init(&chip->vbat_monitor_mutex);
4243 mutex_init(&chip->soc_invalidation_mutex);
4244 mutex_init(&chip->last_soc_mutex);
Xiaozhe Shibda84992013-09-05 10:39:11 -07004245 mutex_init(&chip->status_lock);
Xiaozhe Shi27375822013-08-22 11:40:15 -07004246 init_waitqueue_head(&chip->bms_wait_queue);
Xiaozhe Shi3eaf0b62013-07-11 09:48:08 -07004247
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004248 warm_reset = qpnp_pon_is_warm_reset();
4249 rc = warm_reset;
4250 if (rc < 0)
4251 goto error_read;
4252
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004253 rc = register_spmi(chip, spmi);
4254 if (rc) {
4255 pr_err("error registering spmi resource %d\n", rc);
4256 goto error_resource;
4257 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004258
4259 rc = qpnp_read_wrapper(chip, &chip->revision1,
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004260 chip->base + REVISION1, 1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004261 if (rc) {
4262 pr_err("error reading version register %d\n", rc);
4263 goto error_read;
4264 }
4265
4266 rc = qpnp_read_wrapper(chip, &chip->revision2,
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004267 chip->base + REVISION2, 1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004268 if (rc) {
4269 pr_err("Error reading version register %d\n", rc);
4270 goto error_read;
4271 }
Xiaozhe Shia045a562012-11-28 16:55:39 -08004272 pr_debug("BMS version: %hhu.%hhu\n", chip->revision2, chip->revision1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004273
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004274 rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision2,
4275 chip->iadc_base + REVISION2, 1);
4276 if (rc) {
4277 pr_err("Error reading version register %d\n", rc);
4278 goto error_read;
4279 }
4280
4281 rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision1,
4282 chip->iadc_base + REVISION1, 1);
4283 if (rc) {
4284 pr_err("Error reading version register %d\n", rc);
4285 goto error_read;
4286 }
4287 pr_debug("IADC_BMS version: %hhu.%hhu\n",
4288 chip->iadc_bms_revision2, chip->iadc_bms_revision1);
4289
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004290 rc = bms_read_properties(chip);
4291 if (rc) {
4292 pr_err("Unable to read all bms properties, rc = %d\n", rc);
4293 goto error_read;
4294 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004295
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004296 rc = read_iadc_channel_select(chip);
4297 if (rc) {
4298 pr_err("Unable to get iadc selected channel = %d\n", rc);
4299 goto error_read;
4300 }
4301
Xiaozhe Shibdf14742012-12-05 12:41:48 -08004302 if (chip->use_ocv_thresholds) {
4303 rc = set_ocv_voltage_thresholds(chip,
4304 chip->ocv_low_threshold_uv,
4305 chip->ocv_high_threshold_uv);
4306 if (rc) {
4307 pr_err("Could not set ocv voltage thresholds: %d\n",
4308 rc);
4309 goto error_read;
4310 }
4311 }
4312
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004313 rc = set_battery_data(chip);
4314 if (rc) {
4315 pr_err("Bad battery data %d\n", rc);
4316 goto error_read;
4317 }
4318
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004319 bms_initialize_constants(chip);
4320
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004321 wakeup_source_init(&chip->soc_wake_source.source, "qpnp_soc_wake");
Xiaozhe Shi4be85782013-02-22 17:33:40 -08004322 wake_lock_init(&chip->low_voltage_wake_lock, WAKE_LOCK_SUSPEND,
4323 "qpnp_low_voltage_lock");
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004324 wake_lock_init(&chip->cv_wake_lock, WAKE_LOCK_SUSPEND,
4325 "qpnp_cv_lock");
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004326 INIT_DELAYED_WORK(&chip->calculate_soc_delayed_work,
4327 calculate_soc_work);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08004328 INIT_WORK(&chip->recalc_work, recalculate_work);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07004329 INIT_WORK(&chip->batfet_open_work, batfet_open_work);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004330
Xiaozhe Shif9f99242013-08-29 12:27:50 -07004331 dev_set_drvdata(&spmi->dev, chip);
4332 device_init_wakeup(&spmi->dev, 1);
4333
4334 load_shutdown_data(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004335
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05304336 if (chip->enable_fcc_learning) {
Anirudh Ghayale0c02932013-07-08 16:26:35 +05304337 if (chip->battery_removed) {
4338 rc = discard_backup_fcc_data(chip);
4339 if (rc)
4340 pr_err("Could not discard backed-up FCC data\n");
4341 } else {
4342 rc = read_chgcycle_data_from_backup(chip);
4343 if (rc)
4344 pr_err("Unable to restore charge-cycle data\n");
4345
4346 rc = read_fcc_data_from_backup(chip);
4347 if (rc)
4348 pr_err("Unable to restore FCC-learning data\n");
4349 else
4350 attempt_learning_new_fcc(chip);
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05304351 }
4352 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05304353
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004354 rc = setup_vbat_monitoring(chip);
4355 if (rc < 0) {
4356 pr_err("failed to set up voltage notifications: %d\n", rc);
4357 goto error_setup;
Xiaozhe Shid5d21412013-02-06 17:14:41 -08004358 }
4359
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004360 rc = setup_die_temp_monitoring(chip);
4361 if (rc < 0) {
4362 pr_err("failed to set up die temp notifications: %d\n", rc);
4363 goto error_setup;
4364 }
4365
Xu Kai870f8e82014-01-16 19:21:01 +08004366 rc = bms_request_irqs(chip);
4367 if (rc) {
4368 pr_err("error requesting bms irqs, rc = %d\n", rc);
4369 goto error_setup;
4370 }
4371
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07004372 battery_insertion_check(chip);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07004373 batfet_status_check(chip);
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07004374 battery_status_check(chip);
4375
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004376 calculate_soc_work(&(chip->calculate_soc_delayed_work.work));
4377
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004378 /* setup & register the battery power supply */
4379 chip->bms_psy.name = "bms";
4380 chip->bms_psy.type = POWER_SUPPLY_TYPE_BMS;
4381 chip->bms_psy.properties = msm_bms_power_props;
4382 chip->bms_psy.num_properties = ARRAY_SIZE(msm_bms_power_props);
4383 chip->bms_psy.get_property = qpnp_bms_power_get_property;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004384 chip->bms_psy.external_power_changed =
4385 qpnp_bms_external_power_changed;
4386 chip->bms_psy.supplied_to = qpnp_bms_supplicants;
4387 chip->bms_psy.num_supplicants = ARRAY_SIZE(qpnp_bms_supplicants);
4388
4389 rc = power_supply_register(chip->dev, &chip->bms_psy);
4390
4391 if (rc < 0) {
4392 pr_err("power_supply_register bms failed rc = %d\n", rc);
4393 goto unregister_dc;
4394 }
4395
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07004396 chip->bms_psy_registered = true;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004397 vbatt = 0;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07004398 rc = get_battery_voltage(chip, &vbatt);
Xiaozhe Shi36458962013-02-06 16:19:57 -08004399 if (rc) {
4400 pr_err("error reading vbat_sns adc channel = %d, rc = %d\n",
4401 VBAT_SNS, rc);
4402 goto unregister_dc;
4403 }
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004404
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004405 pr_info("probe success: soc =%d vbatt = %d ocv = %d r_sense_uohm = %u warm_reset = %d\n",
4406 get_prop_bms_capacity(chip), vbatt, chip->last_ocv_uv,
4407 chip->r_sense_uohm, warm_reset);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004408 return 0;
4409
4410unregister_dc:
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07004411 chip->bms_psy_registered = false;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004412 power_supply_unregister(&chip->bms_psy);
4413error_setup:
4414 dev_set_drvdata(&spmi->dev, NULL);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004415 wakeup_source_trash(&chip->soc_wake_source.source);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08004416 wake_lock_destroy(&chip->low_voltage_wake_lock);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004417 wake_lock_destroy(&chip->cv_wake_lock);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004418error_resource:
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004419error_read:
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004420 return rc;
4421}
4422
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07004423static int qpnp_bms_remove(struct spmi_device *spmi)
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004424{
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004425 dev_set_drvdata(&spmi->dev, NULL);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004426 return 0;
4427}
4428
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004429static int bms_suspend(struct device *dev)
4430{
4431 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
4432
4433 cancel_delayed_work_sync(&chip->calculate_soc_delayed_work);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07004434 chip->was_charging_at_sleep = is_battery_charging(chip);
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004435 return 0;
4436}
4437
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004438static int bms_resume(struct device *dev)
4439{
4440 int rc;
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004441 int soc_calc_period;
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004442 int time_until_next_recalc = 0;
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004443 unsigned long time_since_last_recalc;
4444 unsigned long tm_now_sec;
4445 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
4446
4447 rc = get_current_time(&tm_now_sec);
4448 if (rc) {
4449 pr_err("Could not read current time: %d\n", rc);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004450 } else {
Xiaozhe Shicb487b12013-10-14 17:42:07 -07004451 soc_calc_period = get_calculation_delay_ms(chip);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004452 time_since_last_recalc = tm_now_sec - chip->last_recalc_time;
4453 pr_debug("Time since last recalc: %lu\n",
4454 time_since_last_recalc);
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004455 time_until_next_recalc = max(0, soc_calc_period
4456 - (int)(time_since_last_recalc * 1000));
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004457 }
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004458
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004459 if (time_until_next_recalc == 0)
4460 bms_stay_awake(&chip->soc_wake_source);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004461 schedule_delayed_work(&chip->calculate_soc_delayed_work,
4462 round_jiffies_relative(msecs_to_jiffies
4463 (time_until_next_recalc)));
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004464 return 0;
4465}
4466
4467static const struct dev_pm_ops qpnp_bms_pm_ops = {
4468 .resume = bms_resume,
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004469 .suspend = bms_suspend,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004470};
4471
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004472static struct spmi_driver qpnp_bms_driver = {
4473 .probe = qpnp_bms_probe,
4474 .remove = __devexit_p(qpnp_bms_remove),
4475 .driver = {
4476 .name = QPNP_BMS_DEV_NAME,
4477 .owner = THIS_MODULE,
4478 .of_match_table = qpnp_bms_match_table,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004479 .pm = &qpnp_bms_pm_ops,
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004480 },
4481};
4482
4483static int __init qpnp_bms_init(void)
4484{
4485 pr_info("QPNP BMS INIT\n");
4486 return spmi_driver_register(&qpnp_bms_driver);
4487}
4488
4489static void __exit qpnp_bms_exit(void)
4490{
4491 pr_info("QPNP BMS EXIT\n");
4492 return spmi_driver_unregister(&qpnp_bms_driver);
4493}
4494
4495module_init(qpnp_bms_init);
4496module_exit(qpnp_bms_exit);
4497
4498MODULE_DESCRIPTION("QPNP BMS Driver");
4499MODULE_LICENSE("GPL v2");
4500MODULE_ALIAS("platform:" QPNP_BMS_DEV_NAME);