blob: 56d4a30d9eccf116015e0392591ef4a530fc98ed [file] [log] [blame]
Xiaozhe Shi72a72f22013-12-26 13:54:29 -08001/* Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
Xiaozhe Shi73a65692012-09-18 17:51:57 -070013#define pr_fmt(fmt) "BMS: %s: " fmt, __func__
Xiaozhe Shib19f7032012-08-16 12:14:16 -070014
15#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/init.h>
18#include <linux/slab.h>
19#include <linux/err.h>
20#include <linux/of.h>
21#include <linux/of_device.h>
22#include <linux/power_supply.h>
23#include <linux/spmi.h>
Xiaozhe Shie118c692012-09-24 15:17:43 -070024#include <linux/rtc.h>
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -070025#include <linux/delay.h>
Xiaozhe Shi27375822013-08-22 11:40:15 -070026#include <linux/sched.h>
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070027#include <linux/qpnp/qpnp-adc.h>
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -070028#include <linux/qpnp/power-on.h>
Xiaozhe Shiaf203c22013-06-19 12:01:38 -070029#include <linux/of_batterydata.h>
Xiaozhe Shib19f7032012-08-16 12:14:16 -070030
Xiaozhe Shib19f7032012-08-16 12:14:16 -070031/* BMS Register Offsets */
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -070032#define REVISION1 0x0
33#define REVISION2 0x1
Xiaozhe Shib19f7032012-08-16 12:14:16 -070034#define BMS1_STATUS1 0x8
35#define BMS1_MODE_CTL 0X40
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070036/* Coulomb counter clear registers */
Xiaozhe Shib19f7032012-08-16 12:14:16 -070037#define BMS1_CC_DATA_CTL 0x42
Xiaozhe Shia045a562012-11-28 16:55:39 -080038#define BMS1_CC_CLEAR_CTL 0x43
Xiaozhe Shi20640b52013-01-03 11:49:30 -080039/* BMS Tolerances */
40#define BMS1_TOL_CTL 0X44
Xiaozhe Shib19f7032012-08-16 12:14:16 -070041/* OCV limit registers */
42#define BMS1_OCV_USE_LOW_LIMIT_THR0 0x48
43#define BMS1_OCV_USE_LOW_LIMIT_THR1 0x49
44#define BMS1_OCV_USE_HIGH_LIMIT_THR0 0x4A
45#define BMS1_OCV_USE_HIGH_LIMIT_THR1 0x4B
46#define BMS1_OCV_USE_LIMIT_CTL 0x4C
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -070047/* Delay control */
48#define BMS1_S1_DELAY_CTL 0x5A
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -080049/* OCV interrupt threshold */
50#define BMS1_OCV_THR0 0x50
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -070051#define BMS1_S2_SAMP_AVG_CTL 0x61
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -080052/* SW CC interrupt threshold */
53#define BMS1_SW_CC_THR0 0xA0
Xiaozhe Shib19f7032012-08-16 12:14:16 -070054/* OCV for r registers */
55#define BMS1_OCV_FOR_R_DATA0 0x80
Xiaozhe Shib19f7032012-08-16 12:14:16 -070056#define BMS1_VSENSE_FOR_R_DATA0 0x82
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070057/* Coulomb counter data */
Xiaozhe Shib19f7032012-08-16 12:14:16 -070058#define BMS1_CC_DATA0 0x8A
Xiaozhe Shif3da8622013-06-10 14:50:56 -070059/* Shadow Coulomb counter data */
60#define BMS1_SW_CC_DATA0 0xA8
Xiaozhe Shib19f7032012-08-16 12:14:16 -070061/* OCV for soc data */
62#define BMS1_OCV_FOR_SOC_DATA0 0x90
Xiaozhe Shib19f7032012-08-16 12:14:16 -070063#define BMS1_VSENSE_PON_DATA0 0x94
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070064#define BMS1_VSENSE_AVG_DATA0 0x98
Xiaozhe Shib19f7032012-08-16 12:14:16 -070065#define BMS1_VBAT_AVG_DATA0 0x9E
Xiaozhe Shib19f7032012-08-16 12:14:16 -070066/* Extra bms registers */
Xiaozhe Shi57058942013-03-27 16:54:54 -070067#define SOC_STORAGE_REG 0xB0
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070068#define IAVG_STORAGE_REG 0xB1
Anirudh Ghayale0c02932013-07-08 16:26:35 +053069#define BMS_FCC_COUNT 0xB2
70#define BMS_FCC_BASE_REG 0xB3 /* FCC updates - 0xB3 to 0xB7 */
71#define BMS_CHGCYL_BASE_REG 0xB8 /* FCC chgcyl - 0xB8 to 0xBC */
72#define CHARGE_INCREASE_STORAGE 0xBD
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053073#define CHARGE_CYCLE_STORAGE_LSB 0xBE /* LSB=0xBE, MSB=0xBF */
74
Xiaozhe Shic40b3972012-11-30 14:11:16 -080075/* IADC Channel Select */
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -070076#define IADC1_BMS_REVISION2 0x01
Xiaozhe Shic40b3972012-11-30 14:11:16 -080077#define IADC1_BMS_ADC_CH_SEL_CTL 0x48
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -070078#define IADC1_BMS_ADC_INT_RSNSN_CTL 0x49
79#define IADC1_BMS_FAST_AVG_EN 0x5B
Xiaozhe Shib19f7032012-08-16 12:14:16 -070080
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070081/* Configuration for saving of shutdown soc/iavg */
82#define IGNORE_SOC_TEMP_DECIDEG 50
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -070083#define IAVG_STEP_SIZE_MA 10
Xiaozhe Shif5f966d2013-02-19 14:23:11 -080084#define IAVG_INVALID 0xFF
Xiaozhe Shif9f99242013-08-29 12:27:50 -070085#define SOC_INVALID 0x7E
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070086
Xiaozhe Shie118c692012-09-24 15:17:43 -070087#define IAVG_SAMPLES 16
88
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053089/* FCC learning constants */
Anirudh Ghayale0c02932013-07-08 16:26:35 +053090#define MAX_FCC_CYCLES 5
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053091#define DELTA_FCC_PERCENT 5
92#define VALID_FCC_CHGCYL_RANGE 50
Anirudh Ghayale0c02932013-07-08 16:26:35 +053093#define CHGCYL_RESOLUTION 20
94#define FCC_DEFAULT_TEMP 250
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053095
Xiaozhe Shib19f7032012-08-16 12:14:16 -070096#define QPNP_BMS_DEV_NAME "qcom,qpnp-bms"
97
Xiaozhe Shif3da8622013-06-10 14:50:56 -070098enum {
99 SHDW_CC,
100 CC
101};
102
103enum {
104 NORESET,
105 RESET
106};
107
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700108struct soc_params {
109 int fcc_uah;
110 int cc_uah;
Xiaozhe Shi904f1f72012-12-04 12:47:21 -0800111 int rbatt_mohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700112 int iavg_ua;
113 int uuc_uah;
114 int ocv_charge_uah;
Xiaozhe Shif36d2862013-01-04 10:17:35 -0800115 int delta_time_s;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700116};
117
118struct raw_soc_params {
119 uint16_t last_good_ocv_raw;
120 int64_t cc;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700121 int64_t shdw_cc;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700122 int last_good_ocv_uv;
123};
124
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530125struct fcc_sample {
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530126 int fcc_new;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530127 int chargecycles;
128};
129
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800130struct bms_irq {
131 unsigned int irq;
132 unsigned long disabled;
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800133 bool ready;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800134};
135
136struct bms_wakeup_source {
137 struct wakeup_source source;
138 unsigned long disabled;
139};
140
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700141struct qpnp_bms_chip {
142 struct device *dev;
143 struct power_supply bms_psy;
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -0700144 bool bms_psy_registered;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -0700145 struct power_supply *batt_psy;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700146 struct spmi_device *spmi;
Xiaozhe Shi27375822013-08-22 11:40:15 -0700147 wait_queue_head_t bms_wait_queue;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700148 u16 base;
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800149 u16 iadc_base;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -0700150 u16 batt_pres_addr;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -0700151 u16 soc_storage_addr;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700152
153 u8 revision1;
154 u8 revision2;
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -0700155
156 u8 iadc_bms_revision1;
157 u8 iadc_bms_revision2;
158
Xiaozhe Shid5d21412013-02-06 17:14:41 -0800159 int battery_present;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700160 int battery_status;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700161 bool batfet_closed;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800162 bool new_battery;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700163 bool done_charging;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800164 bool last_soc_invalid;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700165 /* platform data */
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -0800166 int r_sense_uohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700167 unsigned int v_cutoff_uv;
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -0800168 int max_voltage_uv;
169 int r_conn_mohm;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700170 int shutdown_soc_valid_limit;
171 int adjust_soc_low_threshold;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700172 int chg_term_ua;
Xiaozhe Shi73a65692012-09-18 17:51:57 -0700173 enum battery_type batt_type;
Xiaozhe Shi976618f2013-04-30 10:49:30 -0700174 unsigned int fcc_mah;
Xiaozhe Shi73a65692012-09-18 17:51:57 -0700175 struct single_row_lut *fcc_temp_lut;
176 struct single_row_lut *fcc_sf_lut;
177 struct pc_temp_ocv_lut *pc_temp_ocv_lut;
178 struct sf_lut *pc_sf_lut;
179 struct sf_lut *rbatt_sf_lut;
180 int default_rbatt_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -0700181 int rbatt_capacitive_mohm;
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -0700182 int rbatt_mohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700183
184 struct delayed_work calculate_soc_delayed_work;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800185 struct work_struct recalc_work;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700186 struct work_struct batfet_open_work;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700187
188 struct mutex bms_output_lock;
189 struct mutex last_ocv_uv_mutex;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700190 struct mutex vbat_monitor_mutex;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700191 struct mutex soc_invalidation_mutex;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700192 struct mutex last_soc_mutex;
Xiaozhe Shibda84992013-09-05 10:39:11 -0700193 struct mutex status_lock;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700194
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800195 bool use_external_rsense;
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800196 bool use_ocv_thresholds;
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800197
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700198 bool ignore_shutdown_soc;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800199 bool shutdown_soc_invalid;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700200 int shutdown_soc;
201 int shutdown_iavg_ma;
202
Xiaozhe Shi4be85782013-02-22 17:33:40 -0800203 struct wake_lock low_voltage_wake_lock;
Xiaozhe Shi4be85782013-02-22 17:33:40 -0800204 int low_voltage_threshold;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700205 int low_soc_calc_threshold;
206 int low_soc_calculate_soc_ms;
Xiaozhe Shicb487b12013-10-14 17:42:07 -0700207 int low_voltage_calculate_soc_ms;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700208 int calculate_soc_ms;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800209 struct bms_wakeup_source soc_wake_source;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700210 struct wake_lock cv_wake_lock;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700211
Xiaozhe Shie118c692012-09-24 15:17:43 -0700212 uint16_t ocv_reading_at_100;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700213 uint16_t prev_last_good_ocv_raw;
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700214 int insertion_ocv_uv;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700215 int last_ocv_uv;
Xiaozhe Shicc48e992013-05-28 16:42:24 -0700216 int charging_adjusted_ocv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800217 int last_ocv_temp;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700218 int last_cc_uah;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700219 unsigned long last_soc_change_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700220 unsigned long tm_sec;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700221 unsigned long report_tm_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700222 bool first_time_calc_soc;
223 bool first_time_calc_uuc;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700224 int64_t software_cc_uah;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700225 int64_t software_shdw_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700226
227 int iavg_samples_ma[IAVG_SAMPLES];
228 int iavg_index;
229 int iavg_num_samples;
230 struct timespec t_soc_queried;
231 int last_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -0700232 int last_soc_est;
Xiaozhe Shicc137262013-03-10 06:21:41 -0700233 int last_soc_unbound;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700234 bool was_charging_at_sleep;
235 int charge_start_tm_sec;
236 int catch_up_time_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700237 struct single_row_lut *adjusted_fcc_temp_lut;
238
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700239 struct qpnp_adc_tm_btm_param vbat_monitor_params;
Xiaozhe Shi535494d2013-04-05 12:27:51 -0700240 struct qpnp_adc_tm_btm_param die_temp_monitor_params;
241 int temperature_margin;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700242 unsigned int vadc_v0625;
243 unsigned int vadc_v1250;
244
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -0700245 int system_load_count;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700246 int prev_uuc_iavg_ma;
247 int prev_pc_unusable;
248 int ibat_at_cv_ua;
249 int soc_at_cv;
250 int prev_chg_soc;
251 int calculated_soc;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -0800252 int prev_voltage_based_soc;
253 bool use_voltage_soc;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700254 bool in_cv_range;
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800255
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800256 int prev_batt_terminal_uv;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -0700257 int high_ocv_correction_limit_uv;
258 int low_ocv_correction_limit_uv;
259 int flat_ocv_threshold_uv;
260 int hold_soc_est;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800261
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800262 int ocv_high_threshold_uv;
263 int ocv_low_threshold_uv;
Xiaozhe Shicdeee312012-12-18 15:10:18 -0800264 unsigned long last_recalc_time;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530265
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530266 struct fcc_sample *fcc_learning_samples;
267 u8 fcc_sample_count;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530268 int enable_fcc_learning;
269 int min_fcc_learning_soc;
270 int min_fcc_ocv_pc;
271 int min_fcc_learning_samples;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530272 int start_soc;
273 int end_soc;
274 int start_pc;
275 int start_cc_uah;
276 int start_real_soc;
277 int end_cc_uah;
278 uint16_t fcc_new_mah;
279 int fcc_new_batt_temp;
280 uint16_t charge_cycles;
281 u8 charge_increase;
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530282 int fcc_resolution;
283 bool battery_removed;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800284 struct bms_irq sw_cc_thr_irq;
285 struct bms_irq ocv_thr_irq;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700286 struct qpnp_vadc_chip *vadc_dev;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700287 struct qpnp_iadc_chip *iadc_dev;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -0700288 struct qpnp_adc_tm_chip *adc_tm_dev;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700289};
290
291static struct of_device_id qpnp_bms_match_table[] = {
292 { .compatible = QPNP_BMS_DEV_NAME },
293 {}
294};
295
296static char *qpnp_bms_supplicants[] = {
297 "battery"
298};
299
300static enum power_supply_property msm_bms_power_props[] = {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700301 POWER_SUPPLY_PROP_CAPACITY,
Xiaozhe Shibda84992013-09-05 10:39:11 -0700302 POWER_SUPPLY_PROP_STATUS,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700303 POWER_SUPPLY_PROP_CURRENT_NOW,
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -0700304 POWER_SUPPLY_PROP_RESISTANCE,
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -0700305 POWER_SUPPLY_PROP_CHARGE_COUNTER,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700306 POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700307 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +0530308 POWER_SUPPLY_PROP_CHARGE_FULL,
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +0530309 POWER_SUPPLY_PROP_CYCLE_COUNT,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700310};
311
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530312static int discard_backup_fcc_data(struct qpnp_bms_chip *chip);
313static void backup_charge_cycle(struct qpnp_bms_chip *chip);
314
Xiaozhe Shi20640b52013-01-03 11:49:30 -0800315static bool bms_reset;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -0800316
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700317static int qpnp_read_wrapper(struct qpnp_bms_chip *chip, u8 *val,
318 u16 base, int count)
319{
320 int rc;
321 struct spmi_device *spmi = chip->spmi;
322
323 rc = spmi_ext_register_readl(spmi->ctrl, spmi->sid, base, val, count);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700324 if (rc) {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700325 pr_err("SPMI read failed rc=%d\n", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700326 return rc;
327 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700328 return 0;
329}
330
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700331static int qpnp_write_wrapper(struct qpnp_bms_chip *chip, u8 *val,
332 u16 base, int count)
333{
334 int rc;
335 struct spmi_device *spmi = chip->spmi;
336
337 rc = spmi_ext_register_writel(spmi->ctrl, spmi->sid, base, val, count);
338 if (rc) {
339 pr_err("SPMI write failed rc=%d\n", rc);
340 return rc;
341 }
342 return 0;
343}
344
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800345static int qpnp_masked_write_base(struct qpnp_bms_chip *chip, u16 addr,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700346 u8 mask, u8 val)
347{
348 int rc;
349 u8 reg;
350
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800351 rc = qpnp_read_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700352 if (rc) {
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800353 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700354 return rc;
355 }
356 reg &= ~mask;
357 reg |= val & mask;
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800358 rc = qpnp_write_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700359 if (rc) {
360 pr_err("write failed addr = %03X, val = %02x, mask = %02x, reg = %02x, rc = %d\n",
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800361 addr, val, mask, reg, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700362 return rc;
363 }
364 return 0;
365}
366
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800367static int qpnp_masked_write_iadc(struct qpnp_bms_chip *chip, u16 addr,
368 u8 mask, u8 val)
369{
370 return qpnp_masked_write_base(chip, chip->iadc_base + addr, mask, val);
371}
372
373static int qpnp_masked_write(struct qpnp_bms_chip *chip, u16 addr,
374 u8 mask, u8 val)
375{
376 return qpnp_masked_write_base(chip, chip->base + addr, mask, val);
377}
378
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800379static void bms_stay_awake(struct bms_wakeup_source *source)
380{
381 if (__test_and_clear_bit(0, &source->disabled)) {
382 __pm_stay_awake(&source->source);
383 pr_debug("enabled source %s\n", source->source.name);
384 }
385}
386
387static void bms_relax(struct bms_wakeup_source *source)
388{
389 if (!__test_and_set_bit(0, &source->disabled)) {
390 __pm_relax(&source->source);
391 pr_debug("disabled source %s\n", source->source.name);
392 }
393}
394
395static void enable_bms_irq(struct bms_irq *irq)
396{
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800397 if (irq->ready && __test_and_clear_bit(0, &irq->disabled)) {
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800398 enable_irq(irq->irq);
399 pr_debug("enabled irq %d\n", irq->irq);
400 }
401}
402
403static void disable_bms_irq(struct bms_irq *irq)
404{
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800405 if (irq->ready && !__test_and_set_bit(0, &irq->disabled)) {
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800406 disable_irq(irq->irq);
407 pr_debug("disabled irq %d\n", irq->irq);
408 }
409}
410
Anirudh Ghayal1166eef2013-12-23 19:05:33 +0530411static void disable_bms_irq_nosync(struct bms_irq *irq)
412{
Xiaozhe Shif511a6e2014-02-20 14:37:18 -0800413 if (irq->ready && !__test_and_set_bit(0, &irq->disabled)) {
Anirudh Ghayal1166eef2013-12-23 19:05:33 +0530414 disable_irq_nosync(irq->irq);
415 pr_debug("disabled irq %d\n", irq->irq);
416 }
417}
418
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700419#define HOLD_OREG_DATA BIT(0)
420static int lock_output_data(struct qpnp_bms_chip *chip)
421{
422 int rc;
423
424 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL,
425 HOLD_OREG_DATA, HOLD_OREG_DATA);
426 if (rc) {
427 pr_err("couldnt lock bms output rc = %d\n", rc);
428 return rc;
429 }
430 return 0;
431}
432
433static int unlock_output_data(struct qpnp_bms_chip *chip)
434{
435 int rc;
436
437 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL, HOLD_OREG_DATA, 0);
438 if (rc) {
439 pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc);
440 return rc;
441 }
442 return 0;
443}
444
445#define V_PER_BIT_MUL_FACTOR 97656
446#define V_PER_BIT_DIV_FACTOR 1000
447#define VADC_INTRINSIC_OFFSET 0x6000
448
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800449static int vadc_reading_to_uv(int reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700450{
451 if (reading <= VADC_INTRINSIC_OFFSET)
452 return 0;
453
454 return (reading - VADC_INTRINSIC_OFFSET)
455 * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR;
456}
457
458#define VADC_CALIB_UV 625000
459#define VBATT_MUL_FACTOR 3
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800460static int adjust_vbatt_reading(struct qpnp_bms_chip *chip, int reading_uv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700461{
462 s64 numerator, denominator;
463
464 if (reading_uv == 0)
465 return 0;
466
467 /* don't adjust if not calibrated */
468 if (chip->vadc_v0625 == 0 || chip->vadc_v1250 == 0) {
469 pr_debug("No cal yet return %d\n",
470 VBATT_MUL_FACTOR * reading_uv);
471 return VBATT_MUL_FACTOR * reading_uv;
472 }
473
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700474 numerator = ((s64)reading_uv - chip->vadc_v0625) * VADC_CALIB_UV;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700475 denominator = (s64)chip->vadc_v1250 - chip->vadc_v0625;
476 if (denominator == 0)
477 return reading_uv * VBATT_MUL_FACTOR;
478 return (VADC_CALIB_UV + div_s64(numerator, denominator))
479 * VBATT_MUL_FACTOR;
480}
481
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800482static int convert_vbatt_uv_to_raw(struct qpnp_bms_chip *chip,
483 int unadjusted_vbatt)
484{
485 int scaled_vbatt = unadjusted_vbatt / VBATT_MUL_FACTOR;
486
487 if (scaled_vbatt <= 0)
488 return VADC_INTRINSIC_OFFSET;
489 return ((scaled_vbatt * V_PER_BIT_DIV_FACTOR) / V_PER_BIT_MUL_FACTOR)
490 + VADC_INTRINSIC_OFFSET;
491}
492
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700493static inline int convert_vbatt_raw_to_uv(struct qpnp_bms_chip *chip,
Xiaozhe Shi80754222013-10-30 14:11:41 -0700494 uint16_t reading, bool is_pon_ocv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700495{
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700496 int64_t uv;
497 int rc;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700498
499 uv = vadc_reading_to_uv(reading);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700500 pr_debug("%u raw converted into %lld uv\n", reading, uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700501 uv = adjust_vbatt_reading(chip, uv);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700502 pr_debug("adjusted into %lld uv\n", uv);
Xiaozhe Shi80754222013-10-30 14:11:41 -0700503 rc = qpnp_vbat_sns_comp_result(chip->vadc_dev, &uv, is_pon_ocv);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700504 if (rc)
505 pr_debug("could not compensate vbatt\n");
506 pr_debug("compensated into %lld uv\n", uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700507 return uv;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700508}
509
510#define CC_READING_RESOLUTION_N 542535
511#define CC_READING_RESOLUTION_D 100000
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -0700512static s64 cc_reading_to_uv(s64 reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700513{
514 return div_s64(reading * CC_READING_RESOLUTION_N,
515 CC_READING_RESOLUTION_D);
516}
517
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800518#define QPNP_ADC_GAIN_IDEAL 3291LL
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700519static s64 cc_adjust_for_gain(s64 uv, uint16_t gain)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700520{
521 s64 result_uv;
522
523 pr_debug("adjusting_uv = %lld\n", uv);
Xiaozhe Shi820a47a2012-11-27 13:23:27 -0800524 if (gain == 0) {
525 pr_debug("gain is %d, not adjusting\n", gain);
526 return uv;
527 }
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700528 pr_debug("adjusting by factor: %lld/%hu = %lld%%\n",
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800529 QPNP_ADC_GAIN_IDEAL, gain,
530 div_s64(QPNP_ADC_GAIN_IDEAL * 100LL, (s64)gain));
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700531
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800532 result_uv = div_s64(uv * QPNP_ADC_GAIN_IDEAL, (s64)gain);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700533 pr_debug("result_uv = %lld\n", result_uv);
534 return result_uv;
535}
536
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700537static s64 cc_reverse_adjust_for_gain(struct qpnp_bms_chip *chip, s64 uv)
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800538{
539 struct qpnp_iadc_calib calibration;
540 int gain;
541 s64 result_uv;
542
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700543 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800544 gain = (int)calibration.gain_raw - (int)calibration.offset_raw;
545
546 pr_debug("reverse adjusting_uv = %lld\n", uv);
547 if (gain == 0) {
548 pr_debug("gain is %d, not adjusting\n", gain);
549 return uv;
550 }
551 pr_debug("adjusting by factor: %hu/%lld = %lld%%\n",
552 gain, QPNP_ADC_GAIN_IDEAL,
553 div64_s64((s64)gain * 100LL,
554 (s64)QPNP_ADC_GAIN_IDEAL));
555
556 result_uv = div64_s64(uv * (s64)gain, QPNP_ADC_GAIN_IDEAL);
557 pr_debug("result_uv = %lld\n", result_uv);
558 return result_uv;
559}
560
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700561static int convert_vsense_to_uv(struct qpnp_bms_chip *chip,
562 int16_t reading)
563{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700564 struct qpnp_iadc_calib calibration;
565
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700566 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700567 return cc_adjust_for_gain(cc_reading_to_uv(reading),
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800568 calibration.gain_raw - calibration.offset_raw);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700569}
570
571static int read_vsense_avg(struct qpnp_bms_chip *chip, int *result_uv)
572{
573 int rc;
574 int16_t reading;
575
576 rc = qpnp_read_wrapper(chip, (u8 *)&reading,
577 chip->base + BMS1_VSENSE_AVG_DATA0, 2);
578
579 if (rc) {
580 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
581 return rc;
582 }
583
584 *result_uv = convert_vsense_to_uv(chip, reading);
585 return 0;
586}
587
588static int get_battery_current(struct qpnp_bms_chip *chip, int *result_ua)
589{
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700590 int rc, vsense_uv = 0;
591 int64_t temp_current;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700592
Xiaozhe Shid0a79542012-11-06 10:00:38 -0800593 if (chip->r_sense_uohm == 0) {
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700594 pr_err("r_sense is zero\n");
595 return -EINVAL;
596 }
597
598 mutex_lock(&chip->bms_output_lock);
599 lock_output_data(chip);
600 read_vsense_avg(chip, &vsense_uv);
601 unlock_output_data(chip);
602 mutex_unlock(&chip->bms_output_lock);
603
604 pr_debug("vsense_uv=%duV\n", vsense_uv);
605 /* cast for signed division */
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700606 temp_current = div_s64((vsense_uv * 1000000LL),
607 (int)chip->r_sense_uohm);
608
Xiaozhe Shi4c8458a2013-11-26 13:00:56 -0800609 *result_ua = temp_current;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700610 rc = qpnp_iadc_comp_result(chip->iadc_dev, &temp_current);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700611 if (rc)
612 pr_debug("error compensation failed: %d\n", rc);
613
Xiaozhe Shi4c8458a2013-11-26 13:00:56 -0800614 pr_debug("%d uA err compensated ibat=%llduA\n",
615 *result_ua, temp_current);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700616 *result_ua = temp_current;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700617 return 0;
618}
619
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700620static int get_battery_voltage(struct qpnp_bms_chip *chip, int *result_uv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700621{
622 int rc;
623 struct qpnp_vadc_result adc_result;
624
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700625 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &adc_result);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700626 if (rc) {
627 pr_err("error reading adc channel = %d, rc = %d\n",
628 VBAT_SNS, rc);
629 return rc;
630 }
631 pr_debug("mvolts phy = %lld meas = 0x%llx\n", adc_result.physical,
632 adc_result.measurement);
633 *result_uv = (int)adc_result.physical;
634 return 0;
635}
636
Xiaozhe Shie118c692012-09-24 15:17:43 -0700637#define CC_36_BIT_MASK 0xFFFFFFFFFLL
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800638static uint64_t convert_s64_to_s36(int64_t raw64)
639{
640 return (uint64_t) raw64 & CC_36_BIT_MASK;
641}
642
643#define SIGN_EXTEND_36_TO_64_MASK (-1LL ^ CC_36_BIT_MASK)
644static int64_t convert_s36_to_s64(uint64_t raw36)
645{
646 raw36 = raw36 & CC_36_BIT_MASK;
647 /* convert 36 bit signed value into 64 signed value */
648 return (raw36 >> 35) == 0LL ?
649 raw36 : (SIGN_EXTEND_36_TO_64_MASK | raw36);
650}
651
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700652static int read_cc_raw(struct qpnp_bms_chip *chip, int64_t *reading,
653 int cc_type)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700654{
655 int64_t raw_reading;
656 int rc;
657
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700658 if (cc_type == SHDW_CC)
659 rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
660 chip->base + BMS1_SW_CC_DATA0, 5);
661 else
662 rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
663 chip->base + BMS1_CC_DATA0, 5);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700664 if (rc) {
665 pr_err("Error reading cc: rc = %d\n", rc);
666 return -ENXIO;
667 }
668
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800669 *reading = convert_s36_to_s64(raw_reading);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700670
671 return 0;
672}
673
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700674static int calib_vadc(struct qpnp_bms_chip *chip)
675{
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700676 int rc, raw_0625, raw_1250;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700677 struct qpnp_vadc_result result;
678
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700679 rc = qpnp_vadc_read(chip->vadc_dev, REF_625MV, &result);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700680 if (rc) {
681 pr_debug("vadc read failed with rc = %d\n", rc);
682 return rc;
683 }
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700684 raw_0625 = result.adc_code;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700685
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700686 rc = qpnp_vadc_read(chip->vadc_dev, REF_125V, &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_1250 = result.adc_code;
692 chip->vadc_v0625 = vadc_reading_to_uv(raw_0625);
693 chip->vadc_v1250 = vadc_reading_to_uv(raw_1250);
694 pr_debug("vadc calib: 0625 = %d raw (%d uv), 1250 = %d raw (%d uv)\n",
695 raw_0625, chip->vadc_v0625,
696 raw_1250, chip->vadc_v1250);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700697 return 0;
698}
699
Xiaozhe Shie118c692012-09-24 15:17:43 -0700700static void convert_and_store_ocv(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800701 struct raw_soc_params *raw,
Xiaozhe Shi80754222013-10-30 14:11:41 -0700702 int batt_temp, bool is_pon_ocv)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700703{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700704 int rc;
705
706 pr_debug("prev_last_good_ocv_raw = %d, last_good_ocv_raw = %d\n",
707 chip->prev_last_good_ocv_raw,
708 raw->last_good_ocv_raw);
709 rc = calib_vadc(chip);
710 if (rc)
711 pr_err("Vadc reference voltage read failed, rc = %d\n", rc);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700712 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
713 raw->last_good_ocv_uv = convert_vbatt_raw_to_uv(chip,
Xiaozhe Shi80754222013-10-30 14:11:41 -0700714 raw->last_good_ocv_raw, is_pon_ocv);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700715 chip->last_ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800716 chip->last_ocv_temp = batt_temp;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700717 chip->software_cc_uah = 0;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700718 pr_debug("last_good_ocv_uv = %d\n", raw->last_good_ocv_uv);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700719}
720
Xiaozhe Shia045a562012-11-28 16:55:39 -0800721#define CLEAR_CC BIT(7)
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700722#define CLEAR_SHDW_CC BIT(6)
Xiaozhe Shia045a562012-11-28 16:55:39 -0800723/**
724 * reset both cc and sw-cc.
725 * note: this should only be ever called from one thread
726 * or there may be a race condition where CC is never enabled
727 * again
728 */
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700729static void reset_cc(struct qpnp_bms_chip *chip, u8 flags)
Xiaozhe Shia045a562012-11-28 16:55:39 -0800730{
731 int rc;
732
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700733 pr_debug("resetting cc manually with flags %hhu\n", flags);
734 mutex_lock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800735 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700736 flags,
737 flags);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800738 if (rc)
739 pr_err("cc reset failed: %d\n", rc);
740
741 /* wait for 100us for cc to reset */
742 udelay(100);
743
744 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700745 flags, 0);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800746 if (rc)
747 pr_err("cc reenable failed: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700748 mutex_unlock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800749}
750
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700751static int get_battery_status(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800752{
753 union power_supply_propval ret = {0,};
754
755 if (chip->batt_psy == NULL)
756 chip->batt_psy = power_supply_get_by_name("battery");
757 if (chip->batt_psy) {
758 /* if battery has been registered, use the status property */
759 chip->batt_psy->get_property(chip->batt_psy,
760 POWER_SUPPLY_PROP_STATUS, &ret);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700761 return ret.intval;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800762 }
763
764 /* Default to false if the battery power supply is not registered. */
765 pr_debug("battery power supply is not registered\n");
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700766 return POWER_SUPPLY_STATUS_UNKNOWN;
767}
768
769static bool is_battery_charging(struct qpnp_bms_chip *chip)
770{
Xiaozhe Shi72a72f22013-12-26 13:54:29 -0800771 union power_supply_propval ret = {0,};
772
773 if (chip->batt_psy == NULL)
774 chip->batt_psy = power_supply_get_by_name("battery");
775 if (chip->batt_psy) {
776 /* if battery has been registered, use the status property */
777 chip->batt_psy->get_property(chip->batt_psy,
778 POWER_SUPPLY_PROP_CHARGE_TYPE, &ret);
779 return ret.intval != POWER_SUPPLY_CHARGE_TYPE_NONE;
780 }
781
782 /* Default to false if the battery power supply is not registered. */
783 pr_debug("battery power supply is not registered\n");
784 return false;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800785}
786
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700787static bool is_battery_full(struct qpnp_bms_chip *chip)
788{
789 return get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL;
790}
791
Xiaozhe Shi4515c762013-11-13 16:36:54 -0800792#define BAT_PRES_BIT BIT(7)
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700793static bool is_battery_present(struct qpnp_bms_chip *chip)
794{
795 union power_supply_propval ret = {0,};
Xiaozhe Shi4515c762013-11-13 16:36:54 -0800796 int rc;
797 u8 batt_pres;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700798
Xiaozhe Shi4515c762013-11-13 16:36:54 -0800799 /* first try to use the batt_pres register if given */
800 if (chip->batt_pres_addr) {
801 rc = qpnp_read_wrapper(chip, &batt_pres,
802 chip->batt_pres_addr, 1);
803 if (!rc && (batt_pres & BAT_PRES_BIT))
804 return true;
805 else
806 return false;
807 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700808 if (chip->batt_psy == NULL)
809 chip->batt_psy = power_supply_get_by_name("battery");
810 if (chip->batt_psy) {
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700811 /* if battery has been registered, use the present property */
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700812 chip->batt_psy->get_property(chip->batt_psy,
813 POWER_SUPPLY_PROP_PRESENT, &ret);
814 return ret.intval;
815 }
816
817 /* Default to false if the battery power supply is not registered. */
818 pr_debug("battery power supply is not registered\n");
819 return false;
820}
821
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700822static int get_battery_insertion_ocv_uv(struct qpnp_bms_chip *chip)
823{
824 union power_supply_propval ret = {0,};
825 int rc, vbat;
826
827 if (chip->batt_psy == NULL)
828 chip->batt_psy = power_supply_get_by_name("battery");
829 if (chip->batt_psy) {
830 /* if battery has been registered, use the ocv property */
831 rc = chip->batt_psy->get_property(chip->batt_psy,
832 POWER_SUPPLY_PROP_VOLTAGE_OCV, &ret);
833 if (rc) {
834 /*
835 * Default to vbatt if the battery OCV is not
836 * registered.
837 */
838 pr_debug("Battery psy does not have voltage ocv\n");
839 rc = get_battery_voltage(chip, &vbat);
840 if (rc)
841 return -EINVAL;
842 return vbat;
843 }
844 return ret.intval;
845 }
846
847 pr_debug("battery power supply is not registered\n");
848 return -EINVAL;
849}
850
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700851static bool is_batfet_closed(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800852{
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700853 union power_supply_propval ret = {0,};
854
855 if (chip->batt_psy == NULL)
856 chip->batt_psy = power_supply_get_by_name("battery");
857 if (chip->batt_psy) {
858 /* if battery has been registered, use the online property */
859 chip->batt_psy->get_property(chip->batt_psy,
860 POWER_SUPPLY_PROP_ONLINE, &ret);
861 return !!ret.intval;
862 }
863
864 /* Default to true if the battery power supply is not registered. */
865 pr_debug("battery power supply is not registered\n");
866 return true;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800867}
868
869static int get_simultaneous_batt_v_and_i(struct qpnp_bms_chip *chip,
870 int *ibat_ua, int *vbat_uv)
871{
872 struct qpnp_iadc_result i_result;
873 struct qpnp_vadc_result v_result;
874 enum qpnp_iadc_channels iadc_channel;
875 int rc;
876
877 iadc_channel = chip->use_external_rsense ?
878 EXTERNAL_RSENSE : INTERNAL_RSENSE;
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700879 if (is_battery_full(chip)) {
880 rc = get_battery_current(chip, ibat_ua);
881 if (rc) {
882 pr_err("bms current read failed with rc: %d\n", rc);
883 return rc;
884 }
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700885 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &v_result);
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700886 if (rc) {
887 pr_err("vadc read failed with rc: %d\n", rc);
888 return rc;
889 }
890 *vbat_uv = (int)v_result.physical;
891 } else {
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700892 rc = qpnp_iadc_vadc_sync_read(chip->iadc_dev,
893 iadc_channel, &i_result,
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700894 VBAT_SNS, &v_result);
895 if (rc) {
896 pr_err("adc sync read failed with rc: %d\n", rc);
897 return rc;
898 }
899 /*
900 * reverse the current read by the iadc, since the bms uses
901 * flipped battery current polarity.
902 */
903 *ibat_ua = -1 * (int)i_result.result_ua;
904 *vbat_uv = (int)v_result.physical;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800905 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800906
907 return 0;
908}
909
910static int estimate_ocv(struct qpnp_bms_chip *chip)
911{
912 int ibat_ua, vbat_uv, ocv_est_uv;
913 int rc;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -0700914 int rbatt_mohm = chip->default_rbatt_mohm + chip->r_conn_mohm
915 + chip->rbatt_capacitive_mohm;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800916
917 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
918 if (rc) {
919 pr_err("simultaneous failed rc = %d\n", rc);
920 return rc;
921 }
922
923 ocv_est_uv = vbat_uv + (ibat_ua * rbatt_mohm) / 1000;
924 pr_debug("estimated pon ocv = %d\n", ocv_est_uv);
925 return ocv_est_uv;
926}
927
928static void reset_for_new_battery(struct qpnp_bms_chip *chip, int batt_temp)
929{
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700930 chip->last_ocv_uv = chip->insertion_ocv_uv;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700931 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800932 chip->last_soc = -EINVAL;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700933 chip->last_soc_invalid = true;
934 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800935 chip->soc_at_cv = -EINVAL;
936 chip->shutdown_soc_invalid = true;
937 chip->shutdown_soc = 0;
938 chip->shutdown_iavg_ma = 0;
939 chip->prev_pc_unusable = -EINVAL;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700940 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700941 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700942 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800943 chip->last_cc_uah = INT_MIN;
944 chip->last_ocv_temp = batt_temp;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800945 chip->prev_batt_terminal_uv = 0;
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530946 if (chip->enable_fcc_learning) {
947 chip->adjusted_fcc_temp_lut = NULL;
948 chip->fcc_new_mah = -EINVAL;
949 /* reset the charge-cycle and charge-increase registers */
950 chip->charge_increase = 0;
951 chip->charge_cycles = 0;
952 backup_charge_cycle(chip);
953 /* discard all the FCC learnt data and reset the local table */
954 discard_backup_fcc_data(chip);
955 memset(chip->fcc_learning_samples, 0,
956 chip->min_fcc_learning_samples *
957 sizeof(struct fcc_sample));
958 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800959}
960
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -0700961#define SIGN(x) ((x) < 0 ? -1 : 1)
962#define UV_PER_SPIN 50000
963static int find_ocv_for_pc(struct qpnp_bms_chip *chip, int batt_temp, int pc)
964{
965 int new_pc;
966 int batt_temp_degc = batt_temp / 10;
967 int ocv_mv;
968 int delta_mv = 5;
969 int max_spin_count;
970 int count = 0;
971 int sign, new_sign;
972
973 ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut, batt_temp_degc, pc);
974
975 new_pc = interpolate_pc(chip->pc_temp_ocv_lut, batt_temp_degc, ocv_mv);
976 pr_debug("test revlookup pc = %d for ocv = %d\n", new_pc, ocv_mv);
977 max_spin_count = 1 + (chip->max_voltage_uv - chip->v_cutoff_uv)
978 / UV_PER_SPIN;
979 sign = SIGN(pc - new_pc);
980
981 while (abs(new_pc - pc) != 0 && count < max_spin_count) {
982 /*
983 * If the newly interpolated pc is larger than the lookup pc,
984 * the ocv should be reduced and vice versa
985 */
986 new_sign = SIGN(pc - new_pc);
987 /*
988 * If the sign has changed, then we have passed the lookup pc.
989 * reduce the ocv step size to get finer results.
990 *
991 * If we have already reduced the ocv step size and still
992 * passed the lookup pc, just stop and use the current ocv.
993 * This can only happen if the batterydata profile is
994 * non-monotonic anyways.
995 */
996 if (new_sign != sign) {
997 if (delta_mv > 1)
998 delta_mv = 1;
999 else
1000 break;
1001 }
1002 sign = new_sign;
1003
1004 ocv_mv = ocv_mv + delta_mv * sign;
1005 new_pc = interpolate_pc(chip->pc_temp_ocv_lut,
1006 batt_temp_degc, ocv_mv);
1007 pr_debug("test revlookup pc = %d for ocv = %d\n",
1008 new_pc, ocv_mv);
1009 count++;
1010 }
1011
1012 return ocv_mv * 1000;
1013}
1014
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08001015#define OCV_RAW_UNINITIALIZED 0xFFFF
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001016#define MIN_OCV_UV 2000000
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001017static int read_soc_params_raw(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001018 struct raw_soc_params *raw,
1019 int batt_temp)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001020{
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001021 int warm_reset, rc;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001022
1023 mutex_lock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -08001024
Xiaozhe Shie118c692012-09-24 15:17:43 -07001025 lock_output_data(chip);
1026
1027 rc = qpnp_read_wrapper(chip, (u8 *)&raw->last_good_ocv_raw,
1028 chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
1029 if (rc) {
1030 pr_err("Error reading ocv: rc = %d\n", rc);
1031 return -ENXIO;
1032 }
1033
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001034 rc = read_cc_raw(chip, &raw->cc, CC);
1035 rc = read_cc_raw(chip, &raw->shdw_cc, SHDW_CC);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001036 if (rc) {
1037 pr_err("Failed to read raw cc data, rc = %d\n", rc);
1038 return rc;
1039 }
1040
1041 unlock_output_data(chip);
1042 mutex_unlock(&chip->bms_output_lock);
1043
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08001044 if (chip->prev_last_good_ocv_raw == OCV_RAW_UNINITIALIZED) {
Xiaozhe Shi80754222013-10-30 14:11:41 -07001045 convert_and_store_ocv(chip, raw, batt_temp, true);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001046 pr_debug("PON_OCV_UV = %d, cc = %llx\n",
1047 chip->last_ocv_uv, raw->cc);
1048 warm_reset = qpnp_pon_is_warm_reset();
1049 if (raw->last_good_ocv_uv < MIN_OCV_UV
1050 || warm_reset > 0) {
1051 pr_debug("OCV is stale or bad, estimating new OCV.\n");
1052 chip->last_ocv_uv = estimate_ocv(chip);
1053 raw->last_good_ocv_uv = chip->last_ocv_uv;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001054 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001055 pr_debug("New PON_OCV_UV = %d, cc = %llx\n",
1056 chip->last_ocv_uv, raw->cc);
1057 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001058 } else if (chip->new_battery) {
1059 /* if a new battery was inserted, estimate the ocv */
1060 reset_for_new_battery(chip, batt_temp);
1061 raw->cc = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001062 raw->shdw_cc = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001063 raw->last_good_ocv_uv = chip->last_ocv_uv;
1064 chip->new_battery = false;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001065 } else if (chip->done_charging) {
1066 chip->done_charging = false;
1067 /* if we just finished charging, reset CC and fake 100% */
1068 chip->ocv_reading_at_100 = raw->last_good_ocv_raw;
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001069 chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp, 100);
1070 raw->last_good_ocv_uv = chip->last_ocv_uv;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001071 raw->cc = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001072 raw->shdw_cc = 0;
1073 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001074 chip->last_ocv_temp = batt_temp;
1075 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001076 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001077 chip->last_cc_uah = INT_MIN;
1078 pr_debug("EOC Battery full ocv_reading = 0x%x\n",
1079 chip->ocv_reading_at_100);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001080 } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) {
Xiaozhe Shi80754222013-10-30 14:11:41 -07001081 convert_and_store_ocv(chip, raw, batt_temp, false);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001082 /* forget the old cc value upon ocv */
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001083 chip->last_cc_uah = INT_MIN;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001084 } else {
1085 raw->last_good_ocv_uv = chip->last_ocv_uv;
1086 }
1087
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001088 /* stop faking a high OCV if we get a new OCV */
1089 if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw)
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08001090 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001091
Xiaozhe Shie118c692012-09-24 15:17:43 -07001092 pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n",
1093 raw->last_good_ocv_raw, raw->last_good_ocv_uv);
1094 pr_debug("cc_raw= 0x%llx\n", raw->cc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001095 return 0;
1096}
1097
Xiaozhe Shie118c692012-09-24 15:17:43 -07001098static int calculate_pc(struct qpnp_bms_chip *chip, int ocv_uv,
1099 int batt_temp)
1100{
1101 int pc;
1102
1103 pc = interpolate_pc(chip->pc_temp_ocv_lut,
1104 batt_temp / 10, ocv_uv / 1000);
1105 pr_debug("pc = %u %% for ocv = %d uv batt_temp = %d\n",
1106 pc, ocv_uv, batt_temp);
1107 /* Multiply the initial FCC value by the scale factor. */
1108 return pc;
1109}
1110
1111static int calculate_fcc(struct qpnp_bms_chip *chip, int batt_temp)
1112{
1113 int fcc_uah;
1114
1115 if (chip->adjusted_fcc_temp_lut == NULL) {
1116 /* interpolate_fcc returns a mv value. */
1117 fcc_uah = interpolate_fcc(chip->fcc_temp_lut,
1118 batt_temp) * 1000;
1119 pr_debug("fcc = %d uAh\n", fcc_uah);
1120 return fcc_uah;
1121 } else {
1122 return 1000 * interpolate_fcc(chip->adjusted_fcc_temp_lut,
1123 batt_temp);
1124 }
1125}
1126
1127/* calculate remaining charge at the time of ocv */
1128static int calculate_ocv_charge(struct qpnp_bms_chip *chip,
1129 struct raw_soc_params *raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001130 int fcc_uah)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001131{
1132 int ocv_uv, pc;
1133
1134 ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001135 pc = calculate_pc(chip, ocv_uv, chip->last_ocv_temp);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001136 pr_debug("ocv_uv = %d pc = %d\n", ocv_uv, pc);
1137 return (fcc_uah * pc) / 100;
1138}
1139
Xiaozhe Shie118c692012-09-24 15:17:43 -07001140#define CC_READING_TICKS 56
1141#define SLEEP_CLK_HZ 32764
1142#define SECONDS_PER_HOUR 3600
1143
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001144static s64 cc_uv_to_pvh(s64 cc_uv)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001145{
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001146 /* Note that it is necessary need to multiply by 1000000 to convert
1147 * from uvh to pvh here.
1148 * However, the maximum Coulomb Counter value is 2^35, which can cause
1149 * an over flow.
1150 * Multiply by 100000 first to perserve as much precision as possible
1151 * then multiply by 10 after doing the division in order to avoid
1152 * overflow on the maximum Coulomb Counter value.
1153 */
1154 return div_s64(cc_uv * CC_READING_TICKS * 100000,
1155 SLEEP_CLK_HZ * SECONDS_PER_HOUR) * 10;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001156}
1157
1158/**
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001159 * calculate_cc() - converts a hardware coulomb counter reading into uah
Xiaozhe Shie118c692012-09-24 15:17:43 -07001160 * @chip: the bms chip pointer
1161 * @cc: the cc reading from bms h/w
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001162 * @cc_type: calcualte cc from regular or shadow coulomb counter
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001163 * @clear_cc: whether this function should clear the hardware counter
1164 * after reading
Xiaozhe Shie118c692012-09-24 15:17:43 -07001165 *
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001166 * Converts the 64 bit hardware coulomb counter into microamp-hour by taking
1167 * into account hardware resolution and adc errors.
1168 *
1169 * Return: the coulomb counter based charge in uAh (micro-amp hour)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001170 */
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001171static int calculate_cc(struct qpnp_bms_chip *chip, int64_t cc,
1172 int cc_type, int clear_cc)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001173{
Xiaozhe Shi4e376652012-10-25 12:38:50 -07001174 struct qpnp_iadc_calib calibration;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001175 struct qpnp_vadc_result result;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001176 int64_t cc_voltage_uv, cc_pvh, cc_uah, *software_counter;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001177 int rc;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001178
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001179 software_counter = cc_type == SHDW_CC ?
1180 &chip->software_shdw_cc_uah : &chip->software_cc_uah;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001181 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001182 if (rc) {
1183 pr_err("could not read pmic die temperature: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001184 return *software_counter;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001185 }
1186
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07001187 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001188 pr_debug("%scc = %lld, die_temp = %lld\n",
1189 cc_type == SHDW_CC ? "shdw_" : "",
1190 cc, result.physical);
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -07001191 cc_voltage_uv = cc_reading_to_uv(cc);
Xiaozhe Shi0c484932013-02-05 16:14:10 -08001192 cc_voltage_uv = cc_adjust_for_gain(cc_voltage_uv,
1193 calibration.gain_raw
1194 - calibration.offset_raw);
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001195 cc_pvh = cc_uv_to_pvh(cc_voltage_uv);
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001196 cc_uah = div_s64(cc_pvh, chip->r_sense_uohm);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07001197 rc = qpnp_iadc_comp_result(chip->iadc_dev, &cc_uah);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001198 if (rc)
1199 pr_debug("error compensation failed: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001200 if (clear_cc == RESET) {
1201 pr_debug("software_%scc = %lld, added cc_uah = %lld\n",
1202 cc_type == SHDW_CC ? "sw_" : "",
1203 *software_counter, cc_uah);
1204 *software_counter += cc_uah;
1205 reset_cc(chip, cc_type == SHDW_CC ? CLEAR_SHDW_CC : CLEAR_CC);
1206 return (int)*software_counter;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001207 } else {
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001208 pr_debug("software_%scc = %lld, cc_uah = %lld, total = %lld\n",
1209 cc_type == SHDW_CC ? "shdw_" : "",
1210 *software_counter, cc_uah,
1211 *software_counter + cc_uah);
1212 return *software_counter + cc_uah;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001213 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07001214}
1215
1216static int get_rbatt(struct qpnp_bms_chip *chip,
1217 int soc_rbatt_mohm, int batt_temp)
1218{
1219 int rbatt_mohm, scalefactor;
1220
1221 rbatt_mohm = chip->default_rbatt_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001222 if (chip->rbatt_sf_lut == NULL) {
1223 pr_debug("RBATT = %d\n", rbatt_mohm);
1224 return rbatt_mohm;
1225 }
1226 /* Convert the batt_temp to DegC from deciDegC */
1227 batt_temp = batt_temp / 10;
1228 scalefactor = interpolate_scalingfactor(chip->rbatt_sf_lut,
1229 batt_temp, soc_rbatt_mohm);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001230 rbatt_mohm = (rbatt_mohm * scalefactor) / 100;
1231
1232 rbatt_mohm += chip->r_conn_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07001233 rbatt_mohm += chip->rbatt_capacitive_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001234 return rbatt_mohm;
1235}
1236
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -07001237#define IAVG_MINIMAL_TIME 2
Xiaozhe Shie118c692012-09-24 15:17:43 -07001238static void calculate_iavg(struct qpnp_bms_chip *chip, int cc_uah,
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001239 int *iavg_ua, int delta_time_s)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001240{
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001241 int delta_cc_uah = 0;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001242
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -07001243 /*
1244 * use the battery current if called too quickly
1245 */
1246 if (delta_time_s < IAVG_MINIMAL_TIME
1247 || chip->last_cc_uah == INT_MIN) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001248 get_battery_current(chip, iavg_ua);
1249 goto out;
1250 }
1251
Xiaozhe Shie118c692012-09-24 15:17:43 -07001252 delta_cc_uah = cc_uah - chip->last_cc_uah;
1253
1254 *iavg_ua = div_s64((s64)delta_cc_uah * 3600, delta_time_s);
1255
Xiaozhe Shie118c692012-09-24 15:17:43 -07001256out:
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001257 pr_debug("delta_cc = %d iavg_ua = %d\n", delta_cc_uah, (int)*iavg_ua);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001258
1259 /* remember cc_uah */
1260 chip->last_cc_uah = cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001261}
1262
1263static int calculate_termination_uuc(struct qpnp_bms_chip *chip,
1264 struct soc_params *params,
1265 int batt_temp, int uuc_iavg_ma,
1266 int *ret_pc_unusable)
1267{
1268 int unusable_uv, pc_unusable, uuc_uah;
1269 int i = 0;
1270 int ocv_mv;
1271 int batt_temp_degc = batt_temp / 10;
1272 int rbatt_mohm;
1273 int delta_uv;
1274 int prev_delta_uv = 0;
1275 int prev_rbatt_mohm = 0;
1276 int uuc_rbatt_mohm;
1277
1278 for (i = 0; i <= 100; i++) {
1279 ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
1280 batt_temp_degc, i);
1281 rbatt_mohm = get_rbatt(chip, i, batt_temp);
1282 unusable_uv = (rbatt_mohm * uuc_iavg_ma)
1283 + (chip->v_cutoff_uv);
1284 delta_uv = ocv_mv * 1000 - unusable_uv;
1285
Xiaozhe Shie118c692012-09-24 15:17:43 -07001286 if (delta_uv > 0)
1287 break;
1288
1289 prev_delta_uv = delta_uv;
1290 prev_rbatt_mohm = rbatt_mohm;
1291 }
1292
1293 uuc_rbatt_mohm = linear_interpolate(rbatt_mohm, delta_uv,
1294 prev_rbatt_mohm, prev_delta_uv,
1295 0);
1296
1297 unusable_uv = (uuc_rbatt_mohm * uuc_iavg_ma) + (chip->v_cutoff_uv);
1298
1299 pc_unusable = calculate_pc(chip, unusable_uv, batt_temp);
1300 uuc_uah = (params->fcc_uah * pc_unusable) / 100;
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001301 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 -07001302 uuc_iavg_ma,
1303 uuc_rbatt_mohm, unusable_uv,
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001304 pc_unusable, i, uuc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001305 *ret_pc_unusable = pc_unusable;
1306 return uuc_uah;
1307}
1308
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001309#define TIME_PER_PERCENT_UUC 60
Xiaozhe Shie118c692012-09-24 15:17:43 -07001310static int adjust_uuc(struct qpnp_bms_chip *chip,
1311 struct soc_params *params,
1312 int new_pc_unusable,
1313 int new_uuc_uah,
1314 int batt_temp)
1315{
1316 int new_unusable_mv, new_iavg_ma;
1317 int batt_temp_degc = batt_temp / 10;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001318 int max_percent_change;
1319
1320 max_percent_change = max(params->delta_time_s
1321 / TIME_PER_PERCENT_UUC, 1);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001322
1323 if (chip->prev_pc_unusable == -EINVAL
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001324 || abs(chip->prev_pc_unusable - new_pc_unusable)
1325 <= max_percent_change) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001326 chip->prev_pc_unusable = new_pc_unusable;
1327 return new_uuc_uah;
1328 }
1329
1330 /* the uuc is trying to change more than 1% restrict it */
1331 if (new_pc_unusable > chip->prev_pc_unusable)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001332 chip->prev_pc_unusable += max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001333 else
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001334 chip->prev_pc_unusable -= max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001335
1336 new_uuc_uah = (params->fcc_uah * chip->prev_pc_unusable) / 100;
1337
1338 /* also find update the iavg_ma accordingly */
1339 new_unusable_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
1340 batt_temp_degc, chip->prev_pc_unusable);
1341 if (new_unusable_mv < chip->v_cutoff_uv/1000)
1342 new_unusable_mv = chip->v_cutoff_uv/1000;
1343
1344 new_iavg_ma = (new_unusable_mv * 1000 - chip->v_cutoff_uv)
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001345 / params->rbatt_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001346 if (new_iavg_ma == 0)
1347 new_iavg_ma = 1;
1348 chip->prev_uuc_iavg_ma = new_iavg_ma;
1349 pr_debug("Restricting UUC to %d (%d%%) unusable_mv = %d iavg_ma = %d\n",
1350 new_uuc_uah, chip->prev_pc_unusable,
1351 new_unusable_mv, new_iavg_ma);
1352
1353 return new_uuc_uah;
1354}
1355
Abhijeet Dharmapurikarbdf8ba82012-12-20 18:33:56 -08001356#define MIN_IAVG_MA 250
Xiaozhe Shie118c692012-09-24 15:17:43 -07001357static int calculate_unusable_charge_uah(struct qpnp_bms_chip *chip,
1358 struct soc_params *params,
1359 int batt_temp)
1360{
1361 int uuc_uah_iavg;
1362 int i;
1363 int uuc_iavg_ma = params->iavg_ua / 1000;
1364 int pc_unusable;
1365
1366 /*
1367 * if called first time, fill all the samples with
1368 * the shutdown_iavg_ma
1369 */
1370 if (chip->first_time_calc_uuc && chip->shutdown_iavg_ma != 0) {
1371 pr_debug("Using shutdown_iavg_ma = %d in all samples\n",
1372 chip->shutdown_iavg_ma);
1373 for (i = 0; i < IAVG_SAMPLES; i++)
1374 chip->iavg_samples_ma[i] = chip->shutdown_iavg_ma;
1375
1376 chip->iavg_index = 0;
1377 chip->iavg_num_samples = IAVG_SAMPLES;
1378 }
1379
Xiaozhe Shi70633922013-09-23 15:50:53 -07001380 if (params->delta_time_s >= IAVG_MINIMAL_TIME) {
1381 /*
1382 * if charging use a nominal avg current to keep
1383 * a reasonable UUC while charging
1384 */
1385 if (uuc_iavg_ma < MIN_IAVG_MA)
1386 uuc_iavg_ma = MIN_IAVG_MA;
1387 chip->iavg_samples_ma[chip->iavg_index] = uuc_iavg_ma;
1388 chip->iavg_index = (chip->iavg_index + 1) % IAVG_SAMPLES;
1389 chip->iavg_num_samples++;
1390 if (chip->iavg_num_samples >= IAVG_SAMPLES)
1391 chip->iavg_num_samples = IAVG_SAMPLES;
1392 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07001393
1394 /* now that this sample is added calcualte the average */
1395 uuc_iavg_ma = 0;
1396 if (chip->iavg_num_samples != 0) {
1397 for (i = 0; i < chip->iavg_num_samples; i++) {
1398 pr_debug("iavg_samples_ma[%d] = %d\n", i,
1399 chip->iavg_samples_ma[i]);
1400 uuc_iavg_ma += chip->iavg_samples_ma[i];
1401 }
1402
1403 uuc_iavg_ma = DIV_ROUND_CLOSEST(uuc_iavg_ma,
1404 chip->iavg_num_samples);
1405 }
1406
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001407 /*
1408 * if we're in bms reset mode, force uuc to be 3% of fcc
1409 */
1410 if (bms_reset)
1411 return (params->fcc_uah * 3) / 100;
1412
Xiaozhe Shi75e5efe2013-02-07 09:51:43 -08001413 uuc_uah_iavg = calculate_termination_uuc(chip, params, batt_temp,
1414 uuc_iavg_ma, &pc_unusable);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001415 pr_debug("uuc_iavg_ma = %d uuc with iavg = %d\n",
1416 uuc_iavg_ma, uuc_uah_iavg);
1417
1418 chip->prev_uuc_iavg_ma = uuc_iavg_ma;
1419 /* restrict the uuc such that it can increase only by one percent */
1420 uuc_uah_iavg = adjust_uuc(chip, params, pc_unusable,
1421 uuc_uah_iavg, batt_temp);
1422
Xiaozhe Shie118c692012-09-24 15:17:43 -07001423 return uuc_uah_iavg;
1424}
1425
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001426static s64 find_ocv_charge_for_soc(struct qpnp_bms_chip *chip,
1427 struct soc_params *params, int soc)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001428{
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001429 return div_s64((s64)soc * (params->fcc_uah - params->uuc_uah),
1430 100) + params->cc_uah + params->uuc_uah;
1431}
Xiaozhe Shie118c692012-09-24 15:17:43 -07001432
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001433static int find_pc_for_soc(struct qpnp_bms_chip *chip,
1434 struct soc_params *params, int soc)
1435{
1436 int ocv_charge_uah = find_ocv_charge_for_soc(chip, params, soc);
1437 int pc;
1438
Xiaozhe Shie118c692012-09-24 15:17:43 -07001439 pc = DIV_ROUND_CLOSEST((int)ocv_charge_uah * 100, params->fcc_uah);
1440 pc = clamp(pc, 0, 100);
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001441 pr_debug("soc = %d, fcc = %d uuc = %d rc = %d pc = %d\n",
1442 soc, params->fcc_uah, params->uuc_uah,
1443 ocv_charge_uah, pc);
1444 return pc;
1445}
Xiaozhe Shie118c692012-09-24 15:17:43 -07001446
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001447static int get_current_time(unsigned long *now_tm_sec)
1448{
1449 struct rtc_time tm;
1450 struct rtc_device *rtc;
1451 int rc;
1452
1453 rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
1454 if (rtc == NULL) {
1455 pr_err("%s: unable to open rtc device (%s)\n",
1456 __FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
Xiaozhe Shi0e01af62013-05-06 12:56:08 -07001457 return -EINVAL;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001458 }
1459
1460 rc = rtc_read_time(rtc, &tm);
1461 if (rc) {
1462 pr_err("Error reading rtc device (%s) : %d\n",
1463 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1464 goto close_time;
1465 }
1466
1467 rc = rtc_valid_tm(&tm);
1468 if (rc) {
1469 pr_err("Invalid RTC time (%s): %d\n",
1470 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1471 goto close_time;
1472 }
1473 rtc_tm_to_time(&tm, now_tm_sec);
1474
1475close_time:
1476 rtc_class_close(rtc);
1477 return rc;
1478}
1479
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08001480/* Returns estimated battery resistance */
1481static int get_prop_bms_batt_resistance(struct qpnp_bms_chip *chip)
1482{
1483 return chip->rbatt_mohm * 1000;
1484}
1485
1486/* Returns instantaneous current in uA */
1487static int get_prop_bms_current_now(struct qpnp_bms_chip *chip)
1488{
1489 int rc, result_ua;
1490
1491 rc = get_battery_current(chip, &result_ua);
1492 if (rc) {
1493 pr_err("failed to get current: %d\n", rc);
1494 return rc;
1495 }
1496 return result_ua;
1497}
1498
1499/* Returns coulomb counter in uAh */
1500static int get_prop_bms_charge_counter(struct qpnp_bms_chip *chip)
1501{
1502 int64_t cc_raw;
1503
1504 mutex_lock(&chip->bms_output_lock);
1505 lock_output_data(chip);
1506 read_cc_raw(chip, &cc_raw, false);
1507 unlock_output_data(chip);
1508 mutex_unlock(&chip->bms_output_lock);
1509
1510 return calculate_cc(chip, cc_raw, CC, NORESET);
1511}
1512
1513/* Returns shadow coulomb counter in uAh */
1514static int get_prop_bms_charge_counter_shadow(struct qpnp_bms_chip *chip)
1515{
1516 int64_t cc_raw;
1517
1518 mutex_lock(&chip->bms_output_lock);
1519 lock_output_data(chip);
1520 read_cc_raw(chip, &cc_raw, true);
1521 unlock_output_data(chip);
1522 mutex_unlock(&chip->bms_output_lock);
1523
1524 return calculate_cc(chip, cc_raw, SHDW_CC, NORESET);
1525}
1526
1527/* Returns full charge design in uAh */
1528static int get_prop_bms_charge_full_design(struct qpnp_bms_chip *chip)
1529{
1530 return chip->fcc_mah * 1000;
1531}
1532
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05301533/* Returns the current full charge in uAh */
1534static int get_prop_bms_charge_full(struct qpnp_bms_chip *chip)
1535{
1536 int rc;
1537 struct qpnp_vadc_result result;
1538
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001539 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05301540 if (rc) {
1541 pr_err("Unable to read battery temperature\n");
1542 return rc;
1543 }
1544
1545 return calculate_fcc(chip, (int)result.physical);
1546}
1547
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001548static int calculate_delta_time(unsigned long *time_stamp, int *delta_time_s)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001549{
1550 unsigned long now_tm_sec = 0;
1551
1552 /* default to delta time = 0 if anything fails */
1553 *delta_time_s = 0;
1554
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001555 if (get_current_time(&now_tm_sec)) {
1556 pr_err("RTC read failed\n");
1557 return 0;
1558 }
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001559
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001560 *delta_time_s = (now_tm_sec - *time_stamp);
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001561
1562 /* remember this time */
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001563 *time_stamp = now_tm_sec;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001564 return 0;
1565}
1566
Xiaozhe Shie118c692012-09-24 15:17:43 -07001567static void calculate_soc_params(struct qpnp_bms_chip *chip,
1568 struct raw_soc_params *raw,
1569 struct soc_params *params,
1570 int batt_temp)
1571{
Xiaozhe Shi219cb222013-06-10 15:49:59 -07001572 int soc_rbatt, shdw_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001573
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001574 calculate_delta_time(&chip->tm_sec, &params->delta_time_s);
1575 pr_debug("tm_sec = %ld, delta_s = %d\n",
1576 chip->tm_sec, params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001577 params->fcc_uah = calculate_fcc(chip, batt_temp);
1578 pr_debug("FCC = %uuAh batt_temp = %d\n", params->fcc_uah, batt_temp);
1579
1580 /* calculate remainging charge */
1581 params->ocv_charge_uah = calculate_ocv_charge(
1582 chip, raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001583 params->fcc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001584 pr_debug("ocv_charge_uah = %uuAh\n", params->ocv_charge_uah);
1585
1586 /* calculate cc micro_volt_hour */
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001587 params->cc_uah = calculate_cc(chip, raw->cc, CC, RESET);
Xiaozhe Shi219cb222013-06-10 15:49:59 -07001588 shdw_cc_uah = calculate_cc(chip, raw->shdw_cc, SHDW_CC, RESET);
1589 pr_debug("cc_uah = %duAh raw->cc = %llx, shdw_cc_uah = %duAh raw->shdw_cc = %llx\n",
1590 params->cc_uah, raw->cc,
1591 shdw_cc_uah, raw->shdw_cc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001592
1593 soc_rbatt = ((params->ocv_charge_uah - params->cc_uah) * 100)
1594 / params->fcc_uah;
1595 if (soc_rbatt < 0)
1596 soc_rbatt = 0;
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001597 params->rbatt_mohm = get_rbatt(chip, soc_rbatt, batt_temp);
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001598 pr_debug("rbatt_mohm = %d\n", params->rbatt_mohm);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001599
Xiaozhe Shi1e87cda2013-05-17 10:18:56 -07001600 if (params->rbatt_mohm != chip->rbatt_mohm) {
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07001601 chip->rbatt_mohm = params->rbatt_mohm;
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07001602 if (chip->bms_psy_registered)
Xiaozhe Shi1e87cda2013-05-17 10:18:56 -07001603 power_supply_changed(&chip->bms_psy);
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07001604 }
1605
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001606 calculate_iavg(chip, params->cc_uah, &params->iavg_ua,
1607 params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001608
1609 params->uuc_uah = calculate_unusable_charge_uah(chip, params,
1610 batt_temp);
1611 pr_debug("UUC = %uuAh\n", params->uuc_uah);
1612}
1613
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001614static int bound_soc(int soc)
1615{
1616 soc = max(0, soc);
1617 soc = min(100, soc);
1618 return soc;
1619}
1620
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001621#define IBAT_TOL_MASK 0x0F
1622#define OCV_TOL_MASK 0xF0
1623#define IBAT_TOL_DEFAULT 0x03
1624#define IBAT_TOL_NOCHG 0x0F
1625#define OCV_TOL_DEFAULT 0x20
1626#define OCV_TOL_NO_OCV 0x00
1627static int stop_ocv_updates(struct qpnp_bms_chip *chip)
1628{
1629 pr_debug("stopping ocv updates\n");
1630 return qpnp_masked_write(chip, BMS1_TOL_CTL,
1631 OCV_TOL_MASK, OCV_TOL_NO_OCV);
1632}
1633
1634static int reset_bms_for_test(struct qpnp_bms_chip *chip)
1635{
Xiaozhe Shi95da77f2013-02-20 13:40:06 -08001636 int ibat_ua = 0, vbat_uv = 0, rc;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001637 int ocv_est_uv;
1638
1639 if (!chip) {
1640 pr_err("BMS driver has not been initialized yet!\n");
1641 return -EINVAL;
1642 }
1643
1644 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
1645
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07001646 /*
1647 * Don't include rbatt and rbatt_capacitative since we expect this to
1648 * be used with a fake battery which does not have internal resistances
1649 */
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001650 ocv_est_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
1651 pr_debug("forcing ocv to be %d due to bms reset mode\n", ocv_est_uv);
1652 chip->last_ocv_uv = ocv_est_uv;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001653 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001654 chip->last_soc = -EINVAL;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001655 chip->last_soc_invalid = true;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001656 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001657 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001658 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001659 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001660 chip->last_cc_uah = INT_MIN;
1661 stop_ocv_updates(chip);
1662
1663 pr_debug("bms reset to ocv = %duv vbat_ua = %d ibat_ua = %d\n",
1664 chip->last_ocv_uv, vbat_uv, ibat_ua);
1665
1666 return rc;
1667}
1668
1669static int bms_reset_set(const char *val, const struct kernel_param *kp)
1670{
1671 int rc;
1672
1673 rc = param_set_bool(val, kp);
1674 if (rc) {
1675 pr_err("Unable to set bms_reset: %d\n", rc);
1676 return rc;
1677 }
1678
1679 if (*(bool *)kp->arg) {
1680 struct power_supply *bms_psy = power_supply_get_by_name("bms");
1681 struct qpnp_bms_chip *chip = container_of(bms_psy,
1682 struct qpnp_bms_chip, bms_psy);
1683
1684 rc = reset_bms_for_test(chip);
1685 if (rc) {
1686 pr_err("Unable to modify bms_reset: %d\n", rc);
1687 return rc;
1688 }
1689 }
1690 return 0;
1691}
1692
1693static struct kernel_param_ops bms_reset_ops = {
1694 .set = bms_reset_set,
1695 .get = param_get_bool,
1696};
1697
1698module_param_cb(bms_reset, &bms_reset_ops, &bms_reset, 0644);
1699
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001700#define SOC_STORAGE_MASK 0xFE
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001701static void backup_soc_and_iavg(struct qpnp_bms_chip *chip, int batt_temp,
1702 int soc)
1703{
1704 u8 temp;
1705 int rc;
1706 int iavg_ma = chip->prev_uuc_iavg_ma;
1707
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07001708 if (iavg_ma > MIN_IAVG_MA)
1709 temp = (iavg_ma - MIN_IAVG_MA) / IAVG_STEP_SIZE_MA;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001710 else
1711 temp = 0;
1712
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001713 rc = qpnp_write_wrapper(chip, &temp, chip->base + IAVG_STORAGE_REG, 1);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001714
Xiaozhe Shie945a8a2013-11-11 10:20:14 -08001715 /* store an invalid soc if temperature is below 5degC */
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001716 if (batt_temp > IGNORE_SOC_TEMP_DECIDEG)
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001717 qpnp_masked_write_base(chip, chip->soc_storage_addr,
1718 SOC_STORAGE_MASK, (soc + 1) << 1);
Xiaozhe Shie945a8a2013-11-11 10:20:14 -08001719 else
1720 qpnp_masked_write_base(chip, chip->soc_storage_addr,
1721 SOC_STORAGE_MASK, SOC_STORAGE_MASK);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001722}
1723
1724static int scale_soc_while_chg(struct qpnp_bms_chip *chip, int chg_time_sec,
1725 int catch_up_sec, int new_soc, int prev_soc)
1726{
1727 int scaled_soc;
1728 int numerator;
1729
1730 /*
1731 * Don't report a high value immediately slowly scale the
1732 * value from prev_soc to the new soc based on a charge time
1733 * weighted average
1734 */
1735 pr_debug("cts = %d catch_up_sec = %d\n", chg_time_sec, catch_up_sec);
1736 if (catch_up_sec == 0)
1737 return new_soc;
1738
1739 if (chg_time_sec > catch_up_sec)
1740 return new_soc;
1741
1742 numerator = (catch_up_sec - chg_time_sec) * prev_soc
1743 + chg_time_sec * new_soc;
1744 scaled_soc = numerator / catch_up_sec;
1745
1746 pr_debug("cts = %d new_soc = %d prev_soc = %d scaled_soc = %d\n",
1747 chg_time_sec, new_soc, prev_soc, scaled_soc);
1748
1749 return scaled_soc;
1750}
1751
1752/*
1753 * bms_fake_battery is set in setups where a battery emulator is used instead
1754 * of a real battery. This makes the bms driver report a different/fake value
1755 * regardless of the calculated state of charge.
1756 */
1757static int bms_fake_battery = -EINVAL;
1758module_param(bms_fake_battery, int, 0644);
1759
1760static int report_voltage_based_soc(struct qpnp_bms_chip *chip)
1761{
1762 pr_debug("Reported voltage based soc = %d\n",
1763 chip->prev_voltage_based_soc);
1764 return chip->prev_voltage_based_soc;
1765}
1766
1767#define SOC_CATCHUP_SEC_MAX 600
1768#define SOC_CATCHUP_SEC_PER_PERCENT 60
1769#define MAX_CATCHUP_SOC (SOC_CATCHUP_SEC_MAX / SOC_CATCHUP_SEC_PER_PERCENT)
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07001770#define SOC_CHANGE_PER_SEC 5
Xiaozhe Shi27375822013-08-22 11:40:15 -07001771#define REPORT_SOC_WAIT_MS 10000
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001772static int report_cc_based_soc(struct qpnp_bms_chip *chip)
1773{
1774 int soc, soc_change;
1775 int time_since_last_change_sec, charge_time_sec = 0;
1776 unsigned long last_change_sec;
1777 struct timespec now;
1778 struct qpnp_vadc_result result;
1779 int batt_temp;
1780 int rc;
1781 bool charging, charging_since_last_report;
1782
Xiaozhe Shi27375822013-08-22 11:40:15 -07001783 rc = wait_event_interruptible_timeout(chip->bms_wait_queue,
1784 chip->calculated_soc != -EINVAL,
1785 round_jiffies_relative(msecs_to_jiffies
1786 (REPORT_SOC_WAIT_MS)));
1787
1788 if (rc == 0 && chip->calculated_soc == -EINVAL) {
1789 pr_debug("calculate soc timed out\n");
1790 } else if (rc == -ERESTARTSYS) {
1791 pr_err("Wait for SoC interrupted.\n");
1792 return rc;
1793 }
1794
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001795 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001796
1797 if (rc) {
1798 pr_err("error reading adc channel = %d, rc = %d\n",
1799 LR_MUX1_BATT_THERM, rc);
1800 return rc;
1801 }
1802 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
1803 result.measurement);
1804 batt_temp = (int)result.physical;
1805
1806 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shifa6ea692013-05-31 11:15:13 -07001807 soc = chip->calculated_soc;
1808
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001809 last_change_sec = chip->last_soc_change_sec;
1810 calculate_delta_time(&last_change_sec, &time_since_last_change_sec);
1811
1812 charging = is_battery_charging(chip);
1813 charging_since_last_report = charging || (chip->last_soc_unbound
1814 && chip->was_charging_at_sleep);
1815 /*
1816 * account for charge time - limit it to SOC_CATCHUP_SEC to
1817 * avoid overflows when charging continues for extended periods
1818 */
1819 if (charging) {
1820 if (chip->charge_start_tm_sec == 0) {
1821 /*
1822 * calculating soc for the first time
1823 * after start of chg. Initialize catchup time
1824 */
1825 if (abs(soc - chip->last_soc) < MAX_CATCHUP_SOC)
1826 chip->catch_up_time_sec =
1827 (soc - chip->last_soc)
1828 * SOC_CATCHUP_SEC_PER_PERCENT;
1829 else
1830 chip->catch_up_time_sec = SOC_CATCHUP_SEC_MAX;
1831
1832 if (chip->catch_up_time_sec < 0)
1833 chip->catch_up_time_sec = 0;
1834 chip->charge_start_tm_sec = last_change_sec;
1835 }
1836
1837 charge_time_sec = min(SOC_CATCHUP_SEC_MAX, (int)last_change_sec
1838 - chip->charge_start_tm_sec);
1839
1840 /* end catchup if calculated soc and last soc are same */
1841 if (chip->last_soc == soc)
1842 chip->catch_up_time_sec = 0;
1843 }
1844
1845 if (chip->last_soc != -EINVAL) {
1846 /*
1847 * last_soc < soc ... if we have not been charging at all
1848 * since the last time this was called, report previous SoC.
1849 * Otherwise, scale and catch up.
1850 */
1851 if (chip->last_soc < soc && !charging_since_last_report)
1852 soc = chip->last_soc;
1853 else if (chip->last_soc < soc && soc != 100)
1854 soc = scale_soc_while_chg(chip, charge_time_sec,
1855 chip->catch_up_time_sec,
1856 soc, chip->last_soc);
1857
Xiaozhe Shibd56b052013-10-21 11:51:30 -07001858 /* if the battery is close to cutoff allow more change */
1859 if (wake_lock_active(&chip->low_voltage_wake_lock))
1860 soc_change = min((int)abs(chip->last_soc - soc),
1861 time_since_last_change_sec);
1862 else
1863 soc_change = min((int)abs(chip->last_soc - soc),
1864 time_since_last_change_sec
1865 / SOC_CHANGE_PER_SEC);
1866
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001867 if (chip->last_soc_unbound) {
1868 chip->last_soc_unbound = false;
1869 } else {
1870 /*
1871 * if soc have not been unbound by resume,
1872 * only change reported SoC by 1.
1873 */
1874 soc_change = min(1, soc_change);
1875 }
1876
1877 if (soc < chip->last_soc && soc != 0)
1878 soc = chip->last_soc - soc_change;
1879 if (soc > chip->last_soc && soc != 100)
1880 soc = chip->last_soc + soc_change;
1881 }
1882
Xiaozhe Shi208b8e52013-05-28 10:16:32 -07001883 if (chip->last_soc != soc && !chip->last_soc_unbound)
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001884 chip->last_soc_change_sec = last_change_sec;
1885
1886 pr_debug("last_soc = %d, calculated_soc = %d, soc = %d, time since last change = %d\n",
1887 chip->last_soc, chip->calculated_soc,
1888 soc, time_since_last_change_sec);
1889 chip->last_soc = bound_soc(soc);
1890 backup_soc_and_iavg(chip, batt_temp, chip->last_soc);
1891 pr_debug("Reported SOC = %d\n", chip->last_soc);
1892 chip->t_soc_queried = now;
1893 mutex_unlock(&chip->last_soc_mutex);
1894
1895 return soc;
1896}
1897
1898static int report_state_of_charge(struct qpnp_bms_chip *chip)
1899{
1900 if (bms_fake_battery != -EINVAL) {
1901 pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery);
1902 return bms_fake_battery;
1903 } else if (chip->use_voltage_soc)
1904 return report_voltage_based_soc(chip);
1905 else
1906 return report_cc_based_soc(chip);
1907}
1908
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07001909#define VDD_MAX_ERR 5000
1910#define VDD_STEP_SIZE 10000
1911#define MAX_COUNT_BEFORE_RESET_TO_CC 3
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001912static int charging_adjustments(struct qpnp_bms_chip *chip,
1913 struct soc_params *params, int soc,
1914 int vbat_uv, int ibat_ua, int batt_temp)
1915{
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001916 int chg_soc, soc_ibat, batt_terminal_uv, weight_ibat, weight_cc;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001917
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001918 batt_terminal_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001919
1920 if (chip->soc_at_cv == -EINVAL) {
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001921 if (batt_terminal_uv >= chip->max_voltage_uv - VDD_MAX_ERR) {
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001922 chip->soc_at_cv = soc;
1923 chip->prev_chg_soc = soc;
Xiaozhe Shifc7af172013-11-04 14:15:44 -08001924 chip->ibat_at_cv_ua = params->iavg_ua;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001925 pr_debug("CC_TO_CV ibat_ua = %d CHG SOC %d\n",
1926 ibat_ua, soc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001927 } else {
1928 /* In constant current charging return the calc soc */
1929 pr_debug("CC CHG SOC %d\n", soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001930 }
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001931
1932 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07001933 chip->system_load_count = 0;
1934 return soc;
1935 } else if (ibat_ua > 0 && batt_terminal_uv
1936 < chip->max_voltage_uv - (VDD_MAX_ERR * 2)) {
1937 if (chip->system_load_count > MAX_COUNT_BEFORE_RESET_TO_CC) {
1938 chip->soc_at_cv = -EINVAL;
1939 pr_debug("Vbat below CV threshold, resetting CC_TO_CV\n");
1940 chip->system_load_count = 0;
1941 } else {
1942 chip->system_load_count += 1;
1943 pr_debug("Vbat below CV threshold, count: %d\n",
1944 chip->system_load_count);
1945 }
1946 return soc;
1947 } else if (ibat_ua > 0) {
1948 pr_debug("NOT CHARGING SOC %d\n", soc);
1949 chip->system_load_count = 0;
1950 chip->prev_chg_soc = soc;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001951 return soc;
1952 }
1953
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07001954 chip->system_load_count = 0;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001955 /*
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001956 * battery is in CV phase - begin linear interpolation of soc based on
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001957 * battery charge current
1958 */
1959
1960 /*
1961 * if voltage lessened (possibly because of a system load)
1962 * keep reporting the prev chg soc
1963 */
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001964 if (batt_terminal_uv <= chip->prev_batt_terminal_uv - VDD_STEP_SIZE) {
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08001965 pr_debug("batt_terminal_uv %d < (max = %d - 10000); CC CHG SOC %d\n",
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001966 batt_terminal_uv, chip->prev_batt_terminal_uv,
1967 chip->prev_chg_soc);
1968 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001969 return chip->prev_chg_soc;
1970 }
1971
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001972 soc_ibat = bound_soc(linear_interpolate(chip->soc_at_cv,
1973 chip->ibat_at_cv_ua,
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08001974 100, -1 * chip->chg_term_ua,
Xiaozhe Shifc7af172013-11-04 14:15:44 -08001975 params->iavg_ua));
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001976 weight_ibat = bound_soc(linear_interpolate(1, chip->soc_at_cv,
1977 100, 100, chip->prev_chg_soc));
1978 weight_cc = 100 - weight_ibat;
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001979 chg_soc = bound_soc(DIV_ROUND_CLOSEST(soc_ibat * weight_ibat
1980 + weight_cc * soc, 100));
1981
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001982 pr_debug("weight_ibat = %d, weight_cc = %d, soc_ibat = %d, soc_cc = %d\n",
1983 weight_ibat, weight_cc, soc_ibat, soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001984
1985 /* always report a higher soc */
1986 if (chg_soc > chip->prev_chg_soc) {
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001987 chip->prev_chg_soc = chg_soc;
1988
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001989 chip->charging_adjusted_ocv = find_ocv_for_pc(chip, batt_temp,
1990 find_pc_for_soc(chip, params, chg_soc));
1991 pr_debug("CC CHG ADJ OCV = %d CHG SOC %d\n",
1992 chip->charging_adjusted_ocv,
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001993 chip->prev_chg_soc);
1994 }
1995
1996 pr_debug("Reporting CHG SOC %d\n", chip->prev_chg_soc);
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001997 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001998 return chip->prev_chg_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001999}
2000
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002001static void very_low_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
2002{
2003 /*
2004 * if battery is very low (v_cutoff voltage + 20mv) hold
2005 * a wakelock untill soc = 0%
2006 */
2007 if (vbat_uv <= chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002008 && !wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002009 pr_debug("voltage = %d low holding wakelock\n", vbat_uv);
2010 wake_lock(&chip->low_voltage_wake_lock);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002011 } else if (vbat_uv > chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002012 && wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002013 pr_debug("voltage = %d releasing wakelock\n", vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002014 wake_unlock(&chip->low_voltage_wake_lock);
2015 }
2016}
2017
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002018#define VBATT_ERROR_MARGIN 20000
2019static void cv_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
2020{
2021 /*
2022 * if battery is very low (v_cutoff voltage + 20mv) hold
2023 * a wakelock untill soc = 0%
2024 */
2025 if (wake_lock_active(&chip->cv_wake_lock)) {
2026 if (chip->soc_at_cv != -EINVAL) {
2027 pr_debug("hit CV, releasing cv wakelock\n");
2028 wake_unlock(&chip->cv_wake_lock);
2029 } else if (!is_battery_charging(chip)) {
2030 pr_debug("charging stopped, releasing cv wakelock\n");
2031 wake_unlock(&chip->cv_wake_lock);
2032 }
2033 } else if (vbat_uv > chip->max_voltage_uv - VBATT_ERROR_MARGIN
2034 && chip->soc_at_cv == -EINVAL
2035 && is_battery_charging(chip)
2036 && !wake_lock_active(&chip->cv_wake_lock)) {
2037 pr_debug("voltage = %d holding cv wakelock\n", vbat_uv);
2038 wake_lock(&chip->cv_wake_lock);
2039 }
2040}
2041
Xiaozhe Shi2b647872013-10-31 14:30:27 -07002042#define NO_ADJUST_HIGH_SOC_THRESHOLD 98
Xiaozhe Shie118c692012-09-24 15:17:43 -07002043static int adjust_soc(struct qpnp_bms_chip *chip, struct soc_params *params,
2044 int soc, int batt_temp)
2045{
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002046 int ibat_ua = 0, vbat_uv = 0;
2047 int ocv_est_uv = 0, soc_est = 0, pc_est = 0, pc = 0;
2048 int delta_ocv_uv = 0;
2049 int n = 0;
2050 int rc_new_uah = 0;
2051 int pc_new = 0;
2052 int soc_new = 0;
2053 int slope = 0;
2054 int rc = 0;
2055 int delta_ocv_uv_limit = 0;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002056 int correction_limit_uv = 0;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002057
2058 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
2059 if (rc < 0) {
2060 pr_err("simultaneous vbat ibat failed err = %d\n", rc);
2061 goto out;
2062 }
2063
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002064 very_low_voltage_check(chip, vbat_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002065 cv_voltage_check(chip, vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002066
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002067 delta_ocv_uv_limit = DIV_ROUND_CLOSEST(ibat_ua, 1000);
2068
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08002069 ocv_est_uv = vbat_uv + (ibat_ua * params->rbatt_mohm)/1000;
2070
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002071 pc_est = calculate_pc(chip, ocv_est_uv, batt_temp);
2072 soc_est = div_s64((s64)params->fcc_uah * pc_est - params->uuc_uah*100,
2073 (s64)params->fcc_uah - params->uuc_uah);
2074 soc_est = bound_soc(soc_est);
2075
Xiaozhe Shi20640b52013-01-03 11:49:30 -08002076 /* never adjust during bms reset mode */
2077 if (bms_reset) {
2078 pr_debug("bms reset mode, SOC adjustment skipped\n");
2079 goto out;
2080 }
2081
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07002082 if (is_battery_charging(chip)) {
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002083 soc = charging_adjustments(chip, params, soc, vbat_uv, ibat_ua,
2084 batt_temp);
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07002085 /* Skip adjustments if we are in CV or ibat is negative */
2086 if (chip->soc_at_cv != -EINVAL || ibat_ua < 0)
2087 goto out;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002088 }
2089
2090 /*
2091 * do not adjust
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07002092 * if soc_est is same as what bms calculated
2093 * OR if soc_est > adjust_soc_low_threshold
2094 * OR if soc is above 90
2095 * because we might pull it low
2096 * and cause a bad user experience
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002097 */
Xiaozhe Shibd56b052013-10-21 11:51:30 -07002098 if (!wake_lock_active(&chip->low_voltage_wake_lock) &&
2099 (soc_est == soc
2100 || soc_est > chip->adjust_soc_low_threshold
2101 || soc >= NO_ADJUST_HIGH_SOC_THRESHOLD))
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002102 goto out;
2103
2104 if (chip->last_soc_est == -EINVAL)
2105 chip->last_soc_est = soc;
2106
2107 n = min(200, max(1 , soc + soc_est + chip->last_soc_est));
2108 chip->last_soc_est = soc_est;
2109
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002110 pc = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002111 if (pc > 0) {
2112 pc_new = calculate_pc(chip,
2113 chip->last_ocv_uv - (++slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002114 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002115 while (pc_new == pc) {
2116 /* start taking 10mV steps */
2117 slope = slope + 10;
2118 pc_new = calculate_pc(chip,
2119 chip->last_ocv_uv - (slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002120 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002121 }
2122 } else {
2123 /*
2124 * pc is already at the lowest point,
2125 * assume 1 millivolt translates to 1% pc
2126 */
2127 pc = 1;
2128 pc_new = 0;
2129 slope = 1;
2130 }
2131
2132 delta_ocv_uv = div_s64((soc - soc_est) * (s64)slope * 1000,
2133 n * (pc - pc_new));
2134
2135 if (abs(delta_ocv_uv) > delta_ocv_uv_limit) {
2136 pr_debug("limiting delta ocv %d limit = %d\n", delta_ocv_uv,
2137 delta_ocv_uv_limit);
2138
2139 if (delta_ocv_uv > 0)
2140 delta_ocv_uv = delta_ocv_uv_limit;
2141 else
2142 delta_ocv_uv = -1 * delta_ocv_uv_limit;
2143 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
2144 }
2145
Xiaozhe Shibd56b052013-10-21 11:51:30 -07002146 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2147 /* when in the cutoff region, do not correct upwards */
2148 delta_ocv_uv = max(0, delta_ocv_uv);
Xiaozhe Shi08d9aa72013-07-31 17:15:12 -07002149 goto skip_limits;
Xiaozhe Shibd56b052013-10-21 11:51:30 -07002150 }
Xiaozhe Shi08d9aa72013-07-31 17:15:12 -07002151
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002152 if (chip->last_ocv_uv > chip->flat_ocv_threshold_uv)
2153 correction_limit_uv = chip->high_ocv_correction_limit_uv;
2154 else
2155 correction_limit_uv = chip->low_ocv_correction_limit_uv;
2156
2157 if (abs(delta_ocv_uv) > correction_limit_uv) {
2158 pr_debug("limiting delta ocv %d limit = %d\n",
2159 delta_ocv_uv, correction_limit_uv);
2160 if (delta_ocv_uv > 0)
2161 delta_ocv_uv = correction_limit_uv;
2162 else
2163 delta_ocv_uv = -correction_limit_uv;
2164 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
2165 }
2166
Xiaozhe Shi08d9aa72013-07-31 17:15:12 -07002167skip_limits:
2168
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002169 chip->last_ocv_uv -= delta_ocv_uv;
2170
2171 if (chip->last_ocv_uv >= chip->max_voltage_uv)
2172 chip->last_ocv_uv = chip->max_voltage_uv;
2173
2174 /* calculate the soc based on this new ocv */
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002175 pc_new = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002176 rc_new_uah = (params->fcc_uah * pc_new) / 100;
2177 soc_new = (rc_new_uah - params->cc_uah - params->uuc_uah)*100
2178 / (params->fcc_uah - params->uuc_uah);
2179 soc_new = bound_soc(soc_new);
2180
2181 /*
2182 * if soc_new is ZERO force it higher so that phone doesnt report soc=0
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002183 * soc = 0 should happen only when soc_est is above a set value
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002184 */
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002185 if (soc_new == 0 && soc_est >= chip->hold_soc_est)
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002186 soc_new = 1;
2187
2188 soc = soc_new;
2189
2190out:
2191 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",
2192 ibat_ua, vbat_uv, ocv_est_uv, pc_est,
2193 soc_est, n, delta_ocv_uv, chip->last_ocv_uv,
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08002194 pc_new, soc_new, params->rbatt_mohm, slope);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002195
Xiaozhe Shie118c692012-09-24 15:17:43 -07002196 return soc;
2197}
2198
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002199static int clamp_soc_based_on_voltage(struct qpnp_bms_chip *chip, int soc)
2200{
2201 int rc, vbat_uv;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002202
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002203 rc = get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shi36458962013-02-06 16:19:57 -08002204 if (rc < 0) {
2205 pr_err("adc vbat failed err = %d\n", rc);
2206 return soc;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002207 }
David Keitele50091e2014-03-05 09:55:57 -08002208 if (soc <= 0 && vbat_uv > chip->v_cutoff_uv) {
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002209 pr_debug("clamping soc to 1, vbat (%d) > cutoff (%d)\n",
2210 vbat_uv, chip->v_cutoff_uv);
2211 return 1;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002212 } else {
2213 pr_debug("not clamping, using soc = %d, vbat = %d and cutoff = %d\n",
2214 soc, vbat_uv, chip->v_cutoff_uv);
2215 return soc;
2216 }
2217}
2218
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002219static int64_t convert_cc_uah_to_raw(struct qpnp_bms_chip *chip, int64_t cc_uah)
2220{
2221 int64_t cc_uv, cc_pvh, cc_raw;
2222
2223 cc_pvh = cc_uah * chip->r_sense_uohm;
2224 cc_uv = div_s64(cc_pvh * SLEEP_CLK_HZ * SECONDS_PER_HOUR,
2225 CC_READING_TICKS * 1000000LL);
2226 cc_raw = div_s64(cc_uv * CC_READING_RESOLUTION_D,
2227 CC_READING_RESOLUTION_N);
2228 return cc_raw;
2229}
2230
2231#define CC_STEP_INCREMENT_UAH 1500
2232#define OCV_STEP_INCREMENT 0x10
2233static void configure_soc_wakeup(struct qpnp_bms_chip *chip,
2234 struct soc_params *params,
2235 int batt_temp, int target_soc)
2236{
2237 int target_ocv_uv;
2238 int64_t target_cc_uah, cc_raw_64, current_shdw_cc_raw_64;
2239 int64_t current_shdw_cc_uah, iadc_comp_factor;
2240 uint64_t cc_raw, current_shdw_cc_raw;
2241 int16_t ocv_raw, current_ocv_raw;
2242
2243 current_shdw_cc_raw = 0;
2244 mutex_lock(&chip->bms_output_lock);
2245 lock_output_data(chip);
2246 qpnp_read_wrapper(chip, (u8 *)&current_ocv_raw,
2247 chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
2248 unlock_output_data(chip);
2249 mutex_unlock(&chip->bms_output_lock);
2250 current_shdw_cc_uah = get_prop_bms_charge_counter_shadow(chip);
2251 current_shdw_cc_raw_64 = convert_cc_uah_to_raw(chip,
2252 current_shdw_cc_uah);
2253
2254 /*
2255 * Calculate the target shadow coulomb counter threshold for when
2256 * the SoC changes.
2257 *
2258 * Since the BMS driver resets the shadow coulomb counter every
2259 * 20 seconds when the device is awake, calculate the threshold as
2260 * a delta from the current shadow coulomb count.
2261 */
2262 target_cc_uah = (100 - target_soc)
2263 * (params->fcc_uah - params->uuc_uah)
2264 / 100 - current_shdw_cc_uah;
2265 if (target_cc_uah < 0) {
2266 /*
2267 * If the target cc is below 0, that means we have already
2268 * passed the point where SoC should have fallen.
2269 * Set a wakeup in a few more mAh and check back again
2270 */
2271 target_cc_uah = CC_STEP_INCREMENT_UAH;
2272 }
2273 iadc_comp_factor = 100000;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002274 qpnp_iadc_comp_result(chip->iadc_dev, &iadc_comp_factor);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002275 target_cc_uah = div64_s64(target_cc_uah * 100000, iadc_comp_factor);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002276 target_cc_uah = cc_reverse_adjust_for_gain(chip, target_cc_uah);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002277 cc_raw_64 = convert_cc_uah_to_raw(chip, target_cc_uah);
2278 cc_raw = convert_s64_to_s36(cc_raw_64);
2279
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002280 target_ocv_uv = find_ocv_for_pc(chip, batt_temp,
2281 find_pc_for_soc(chip, params, target_soc));
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002282 ocv_raw = convert_vbatt_uv_to_raw(chip, target_ocv_uv);
2283
2284 /*
2285 * If the current_ocv_raw was updated since reaching 100% and is lower
2286 * than the calculated target ocv threshold, set the new target
2287 * threshold 1.5mAh lower in order to check if the SoC changed yet.
2288 */
2289 if (current_ocv_raw != chip->ocv_reading_at_100
2290 && current_ocv_raw < ocv_raw)
2291 ocv_raw = current_ocv_raw - OCV_STEP_INCREMENT;
2292
2293 qpnp_write_wrapper(chip, (u8 *)&cc_raw,
2294 chip->base + BMS1_SW_CC_THR0, 5);
2295 qpnp_write_wrapper(chip, (u8 *)&ocv_raw,
2296 chip->base + BMS1_OCV_THR0, 2);
2297
Xiaozhe Shif5d87832013-11-21 17:05:39 -08002298 enable_bms_irq(&chip->ocv_thr_irq);
2299 enable_bms_irq(&chip->sw_cc_thr_irq);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002300 pr_debug("current sw_cc_raw = 0x%llx, current ocv = 0x%hx\n",
2301 current_shdw_cc_raw, (uint16_t)current_ocv_raw);
2302 pr_debug("target_cc_uah = %lld, raw64 = 0x%llx, raw 36 = 0x%llx, ocv_raw = 0x%hx\n",
2303 target_cc_uah,
2304 (uint64_t)cc_raw_64, cc_raw,
2305 (uint16_t)ocv_raw);
2306}
2307
Xiaozhe Shi5e791032013-10-25 11:30:42 -07002308#define BAD_SOC_THRESH -10
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002309static int calculate_raw_soc(struct qpnp_bms_chip *chip,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002310 struct raw_soc_params *raw,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002311 struct soc_params *params,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002312 int batt_temp)
2313{
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002314 int soc, remaining_usable_charge_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002315
Xiaozhe Shie118c692012-09-24 15:17:43 -07002316 /* calculate remaining usable charge */
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002317 remaining_usable_charge_uah = params->ocv_charge_uah
2318 - params->cc_uah
2319 - params->uuc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002320 pr_debug("RUC = %duAh\n", remaining_usable_charge_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002321
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002322 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002323 (params->fcc_uah - params->uuc_uah));
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002324
Xiaozhe Shi5e791032013-10-25 11:30:42 -07002325 if (chip->first_time_calc_soc && soc > BAD_SOC_THRESH && soc < 0) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07002326 /*
2327 * first time calcualtion and the pon ocv is too low resulting
2328 * in a bad soc. Adjust ocv to get 0 soc
2329 */
2330 pr_debug("soc is %d, adjusting pon ocv to make it 0\n", soc);
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002331 chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp,
2332 find_pc_for_soc(chip, params, 0));
2333 params->ocv_charge_uah = find_ocv_charge_for_soc(chip,
2334 params, 0);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002335
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002336 remaining_usable_charge_uah = params->ocv_charge_uah
2337 - params->cc_uah
2338 - params->uuc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002339
2340 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002341 (params->fcc_uah
2342 - params->uuc_uah));
Xiaozhe Shie118c692012-09-24 15:17:43 -07002343 pr_debug("DONE for O soc is %d, pon ocv adjusted to %duV\n",
2344 soc, chip->last_ocv_uv);
2345 }
2346
2347 if (soc > 100)
2348 soc = 100;
2349
Xiaozhe Shi5e791032013-10-25 11:30:42 -07002350 if (soc > BAD_SOC_THRESH && soc < 0) {
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002351 pr_debug("bad rem_usb_chg = %d rem_chg %d, cc_uah %d, unusb_chg %d\n",
Xiaozhe Shie118c692012-09-24 15:17:43 -07002352 remaining_usable_charge_uah,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002353 params->ocv_charge_uah,
2354 params->cc_uah, params->uuc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002355
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002356 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 -07002357 chip->last_ocv_uv, batt_temp,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002358 params->fcc_uah, soc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002359 soc = 0;
2360 }
2361
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002362 return soc;
2363}
2364
2365#define SLEEP_RECALC_INTERVAL 3
2366static int calculate_state_of_charge(struct qpnp_bms_chip *chip,
2367 struct raw_soc_params *raw,
2368 int batt_temp)
2369{
2370 struct soc_params params;
2371 int soc, previous_soc, shutdown_soc, new_calculated_soc;
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002372 int remaining_usable_charge_uah;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002373
2374 calculate_soc_params(chip, raw, &params, batt_temp);
2375 if (!is_battery_present(chip)) {
2376 pr_debug("battery gone, reporting 100\n");
2377 new_calculated_soc = 100;
2378 goto done_calculating;
2379 }
2380
2381 if (params.fcc_uah - params.uuc_uah <= 0) {
2382 pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
2383 params.fcc_uah,
2384 params.uuc_uah);
2385 new_calculated_soc = 0;
2386 goto done_calculating;
2387 }
2388
2389 soc = calculate_raw_soc(chip, raw, &params, batt_temp);
2390
Xiaozhe Shie118c692012-09-24 15:17:43 -07002391 mutex_lock(&chip->soc_invalidation_mutex);
2392 shutdown_soc = chip->shutdown_soc;
2393
2394 if (chip->first_time_calc_soc && soc != shutdown_soc
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002395 && !chip->shutdown_soc_invalid) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07002396 /*
2397 * soc for the first time - use shutdown soc
2398 * to adjust pon ocv since it is a small percent away from
2399 * the real soc
2400 */
2401 pr_debug("soc = %d before forcing shutdown_soc = %d\n",
2402 soc, shutdown_soc);
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002403 chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp,
2404 find_pc_for_soc(chip, &params, shutdown_soc));
2405 params.ocv_charge_uah = find_ocv_charge_for_soc(chip,
2406 &params, shutdown_soc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002407
2408 remaining_usable_charge_uah = params.ocv_charge_uah
2409 - params.cc_uah
2410 - params.uuc_uah;
2411
2412 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2413 (params.fcc_uah
2414 - params.uuc_uah));
2415
2416 pr_debug("DONE for shutdown_soc = %d soc is %d, adjusted ocv to %duV\n",
2417 shutdown_soc, soc, chip->last_ocv_uv);
2418 }
2419 mutex_unlock(&chip->soc_invalidation_mutex);
2420
Xiaozhe Shicbce8042014-02-13 14:08:47 -08002421 if (chip->first_time_calc_soc && !chip->shutdown_soc_invalid) {
2422 pr_debug("Skip adjustment when shutdown SOC has been forced\n");
2423 new_calculated_soc = soc;
2424 } else {
2425 pr_debug("SOC before adjustment = %d\n", soc);
2426 new_calculated_soc = adjust_soc(chip, &params, soc, batt_temp);
2427 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07002428
Xiaozhe Shi445d2492013-03-27 18:10:18 -07002429 /* always clamp soc due to BMS hw/sw immaturities */
2430 new_calculated_soc = clamp_soc_based_on_voltage(chip,
2431 new_calculated_soc);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002432 /*
2433 * If the battery is full, configure the cc threshold so the system
2434 * wakes up after SoC changes
2435 */
Xiaozhe Shif5d87832013-11-21 17:05:39 -08002436 if (is_battery_full(chip)) {
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002437 configure_soc_wakeup(chip, &params,
2438 batt_temp, bound_soc(new_calculated_soc - 1));
Xiaozhe Shif5d87832013-11-21 17:05:39 -08002439 } else {
2440 disable_bms_irq(&chip->ocv_thr_irq);
2441 disable_bms_irq(&chip->sw_cc_thr_irq);
2442 }
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002443done_calculating:
Xiaozhe Shifa6ea692013-05-31 11:15:13 -07002444 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07002445 previous_soc = chip->calculated_soc;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002446 chip->calculated_soc = new_calculated_soc;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002447 pr_debug("CC based calculated SOC = %d\n", chip->calculated_soc);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002448 if (chip->last_soc_invalid) {
2449 chip->last_soc_invalid = false;
2450 chip->last_soc = -EINVAL;
2451 }
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002452 /*
2453 * Check if more than a long time has passed since the last
2454 * calculation (more than n times compared to the soc recalculation
2455 * rate, where n is defined by SLEEP_RECALC_INTERVAL). If this is true,
2456 * then the system must have gone through a long sleep, and SoC can be
2457 * allowed to become unbounded by the last reported SoC
2458 */
2459 if (params.delta_time_s * 1000 >
2460 chip->calculate_soc_ms * SLEEP_RECALC_INTERVAL
2461 && !chip->first_time_calc_soc) {
2462 chip->last_soc_unbound = true;
2463 chip->last_soc_change_sec = chip->last_recalc_time;
2464 pr_debug("last_soc unbound because elapsed time = %d\n",
2465 params.delta_time_s);
2466 }
2467 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi27375822013-08-22 11:40:15 -07002468 wake_up_interruptible(&chip->bms_wait_queue);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07002469
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002470 if (new_calculated_soc != previous_soc && chip->bms_psy_registered) {
Xiaozhe Shi83484e32013-05-16 10:59:59 -07002471 power_supply_changed(&chip->bms_psy);
2472 pr_debug("power supply changed\n");
2473 } else {
2474 /*
2475 * Call report state of charge anyways to periodically update
2476 * reported SoC. This prevents reported SoC from being stuck
2477 * when calculated soc doesn't change.
2478 */
2479 report_state_of_charge(chip);
2480 }
2481
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002482 get_current_time(&chip->last_recalc_time);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002483 chip->first_time_calc_soc = 0;
Xiaozhe Shi70633922013-09-23 15:50:53 -07002484 chip->first_time_calc_uuc = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002485 return chip->calculated_soc;
2486}
2487
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002488static int calculate_soc_from_voltage(struct qpnp_bms_chip *chip)
2489{
2490 int voltage_range_uv, voltage_remaining_uv, voltage_based_soc;
Xiaozhe Shi36458962013-02-06 16:19:57 -08002491 int rc, vbat_uv;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002492
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002493 rc = get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shi36458962013-02-06 16:19:57 -08002494 if (rc < 0) {
2495 pr_err("adc vbat failed err = %d\n", rc);
2496 return rc;
2497 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002498 voltage_range_uv = chip->max_voltage_uv - chip->v_cutoff_uv;
2499 voltage_remaining_uv = vbat_uv - chip->v_cutoff_uv;
2500 voltage_based_soc = voltage_remaining_uv * 100 / voltage_range_uv;
2501
2502 voltage_based_soc = clamp(voltage_based_soc, 0, 100);
2503
2504 if (chip->prev_voltage_based_soc != voltage_based_soc
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002505 && chip->bms_psy_registered) {
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002506 power_supply_changed(&chip->bms_psy);
2507 pr_debug("power supply changed\n");
2508 }
2509 chip->prev_voltage_based_soc = voltage_based_soc;
2510
2511 pr_debug("vbat used = %duv\n", vbat_uv);
2512 pr_debug("Calculated voltage based soc = %d\n", voltage_based_soc);
2513 return voltage_based_soc;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08002514}
2515
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002516static int recalculate_raw_soc(struct qpnp_bms_chip *chip)
2517{
2518 int batt_temp, rc, soc;
2519 struct qpnp_vadc_result result;
2520 struct raw_soc_params raw;
2521 struct soc_params params;
2522
2523 bms_stay_awake(&chip->soc_wake_source);
2524 if (chip->use_voltage_soc) {
2525 soc = calculate_soc_from_voltage(chip);
2526 } else {
2527 if (!chip->batfet_closed)
Xiaozhe Shiffb7cfc2014-01-03 10:47:36 -08002528 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002529 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM,
2530 &result);
2531 if (rc) {
2532 pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n",
2533 LR_MUX1_BATT_THERM, rc);
2534 soc = chip->calculated_soc;
2535 } else {
2536 pr_debug("batt_temp phy = %lld meas = 0x%llx\n",
2537 result.physical,
2538 result.measurement);
2539 batt_temp = (int)result.physical;
2540
2541 mutex_lock(&chip->last_ocv_uv_mutex);
2542 read_soc_params_raw(chip, &raw, batt_temp);
2543 calculate_soc_params(chip, &raw, &params, batt_temp);
2544 if (!is_battery_present(chip)) {
2545 pr_debug("battery gone\n");
2546 soc = 0;
2547 } else if (params.fcc_uah - params.uuc_uah <= 0) {
2548 pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
2549 params.fcc_uah,
2550 params.uuc_uah);
2551 soc = 0;
2552 } else {
2553 soc = calculate_raw_soc(chip, &raw,
2554 &params, batt_temp);
2555 }
2556 mutex_unlock(&chip->last_ocv_uv_mutex);
2557 }
2558 }
2559 bms_relax(&chip->soc_wake_source);
2560 return soc;
2561}
2562
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002563static int recalculate_soc(struct qpnp_bms_chip *chip)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002564{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002565 int batt_temp, rc, soc;
2566 struct qpnp_vadc_result result;
2567 struct raw_soc_params raw;
2568
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002569 bms_stay_awake(&chip->soc_wake_source);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002570 mutex_lock(&chip->vbat_monitor_mutex);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002571 if (chip->vbat_monitor_params.state_request !=
2572 ADC_TM_HIGH_LOW_THR_DISABLE)
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002573 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2574 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002575 mutex_unlock(&chip->vbat_monitor_mutex);
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002576 if (chip->use_voltage_soc) {
2577 soc = calculate_soc_from_voltage(chip);
2578 } else {
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07002579 if (!chip->batfet_closed)
Xiaozhe Shiffb7cfc2014-01-03 10:47:36 -08002580 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002581 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM,
2582 &result);
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002583 if (rc) {
2584 pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n",
2585 LR_MUX1_BATT_THERM, rc);
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002586 soc = chip->calculated_soc;
2587 } else {
2588 pr_debug("batt_temp phy = %lld meas = 0x%llx\n",
2589 result.physical,
2590 result.measurement);
2591 batt_temp = (int)result.physical;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002592
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002593 mutex_lock(&chip->last_ocv_uv_mutex);
2594 read_soc_params_raw(chip, &raw, batt_temp);
2595 soc = calculate_state_of_charge(chip, &raw, batt_temp);
2596 mutex_unlock(&chip->last_ocv_uv_mutex);
2597 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002598 }
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002599 bms_relax(&chip->soc_wake_source);
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002600 return soc;
2601}
2602
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002603static void recalculate_work(struct work_struct *work)
2604{
2605 struct qpnp_bms_chip *chip = container_of(work,
2606 struct qpnp_bms_chip,
2607 recalc_work);
2608
2609 recalculate_soc(chip);
2610}
2611
Xiaozhe Shicb487b12013-10-14 17:42:07 -07002612static int get_calculation_delay_ms(struct qpnp_bms_chip *chip)
2613{
2614 if (wake_lock_active(&chip->low_voltage_wake_lock))
2615 return chip->low_voltage_calculate_soc_ms;
2616 else if (chip->calculated_soc < chip->low_soc_calc_threshold)
2617 return chip->low_soc_calculate_soc_ms;
2618 else
2619 return chip->calculate_soc_ms;
2620}
2621
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002622static void calculate_soc_work(struct work_struct *work)
2623{
2624 struct qpnp_bms_chip *chip = container_of(work,
2625 struct qpnp_bms_chip,
2626 calculate_soc_delayed_work.work);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002627
Xiaozhe Shicb487b12013-10-14 17:42:07 -07002628 recalculate_soc(chip);
2629 schedule_delayed_work(&chip->calculate_soc_delayed_work,
2630 round_jiffies_relative(msecs_to_jiffies
2631 (get_calculation_delay_ms(chip))));
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002632}
2633
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002634static void configure_vbat_monitor_low(struct qpnp_bms_chip *chip)
2635{
2636 mutex_lock(&chip->vbat_monitor_mutex);
2637 if (chip->vbat_monitor_params.state_request
2638 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2639 /*
2640 * Battery is now around or below v_cutoff
2641 */
2642 pr_debug("battery entered cutoff range\n");
2643 if (!wake_lock_active(&chip->low_voltage_wake_lock)) {
2644 pr_debug("voltage low, holding wakelock\n");
2645 wake_lock(&chip->low_voltage_wake_lock);
2646 cancel_delayed_work_sync(
2647 &chip->calculate_soc_delayed_work);
2648 schedule_delayed_work(
2649 &chip->calculate_soc_delayed_work, 0);
2650 }
2651 chip->vbat_monitor_params.state_request =
2652 ADC_TM_HIGH_THR_ENABLE;
2653 chip->vbat_monitor_params.high_thr =
2654 (chip->low_voltage_threshold + VBATT_ERROR_MARGIN);
2655 pr_debug("set low thr to %d and high to %d\n",
2656 chip->vbat_monitor_params.low_thr,
2657 chip->vbat_monitor_params.high_thr);
2658 chip->vbat_monitor_params.low_thr = 0;
2659 } else if (chip->vbat_monitor_params.state_request
2660 == ADC_TM_LOW_THR_ENABLE) {
2661 /*
2662 * Battery is in normal operation range.
2663 */
2664 pr_debug("battery entered normal range\n");
2665 if (wake_lock_active(&chip->cv_wake_lock)) {
2666 wake_unlock(&chip->cv_wake_lock);
2667 pr_debug("releasing cv wake lock\n");
2668 }
2669 chip->in_cv_range = false;
2670 chip->vbat_monitor_params.state_request =
2671 ADC_TM_HIGH_LOW_THR_ENABLE;
2672 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2673 - VBATT_ERROR_MARGIN;
2674 chip->vbat_monitor_params.low_thr =
2675 chip->low_voltage_threshold;
2676 pr_debug("set low thr to %d and high to %d\n",
2677 chip->vbat_monitor_params.low_thr,
2678 chip->vbat_monitor_params.high_thr);
2679 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002680 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2681 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002682 mutex_unlock(&chip->vbat_monitor_mutex);
2683}
2684
2685#define CV_LOW_THRESHOLD_HYST_UV 100000
2686static void configure_vbat_monitor_high(struct qpnp_bms_chip *chip)
2687{
2688 mutex_lock(&chip->vbat_monitor_mutex);
2689 if (chip->vbat_monitor_params.state_request
2690 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2691 /*
2692 * Battery is around vddmax
2693 */
2694 pr_debug("battery entered vddmax range\n");
2695 chip->in_cv_range = true;
2696 if (!wake_lock_active(&chip->cv_wake_lock)) {
2697 wake_lock(&chip->cv_wake_lock);
2698 pr_debug("holding cv wake lock\n");
2699 }
2700 schedule_work(&chip->recalc_work);
2701 chip->vbat_monitor_params.state_request =
2702 ADC_TM_LOW_THR_ENABLE;
2703 chip->vbat_monitor_params.low_thr =
2704 (chip->max_voltage_uv - CV_LOW_THRESHOLD_HYST_UV);
2705 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv * 2;
2706 pr_debug("set low thr to %d and high to %d\n",
2707 chip->vbat_monitor_params.low_thr,
2708 chip->vbat_monitor_params.high_thr);
2709 } else if (chip->vbat_monitor_params.state_request
2710 == ADC_TM_HIGH_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->low_voltage_wake_lock)) {
2716 pr_debug("voltage high, releasing wakelock\n");
2717 wake_unlock(&chip->low_voltage_wake_lock);
2718 }
2719 chip->vbat_monitor_params.state_request =
2720 ADC_TM_HIGH_LOW_THR_ENABLE;
2721 chip->vbat_monitor_params.high_thr =
2722 chip->max_voltage_uv - VBATT_ERROR_MARGIN;
2723 chip->vbat_monitor_params.low_thr =
2724 chip->low_voltage_threshold;
2725 pr_debug("set low thr to %d and high to %d\n",
2726 chip->vbat_monitor_params.low_thr,
2727 chip->vbat_monitor_params.high_thr);
2728 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002729 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2730 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002731 mutex_unlock(&chip->vbat_monitor_mutex);
2732}
2733
2734static void btm_notify_vbat(enum qpnp_tm_state state, void *ctx)
2735{
2736 struct qpnp_bms_chip *chip = ctx;
2737 int vbat_uv;
2738 struct qpnp_vadc_result result;
2739 int rc;
2740
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002741 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &result);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002742 pr_debug("vbat = %lld, raw = 0x%x\n", result.physical, result.adc_code);
2743
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002744 get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002745 pr_debug("vbat is at %d, state is at %d\n", vbat_uv, state);
2746
2747 if (state == ADC_TM_LOW_STATE) {
2748 pr_debug("low voltage btm notification triggered\n");
2749 if (vbat_uv - VBATT_ERROR_MARGIN
2750 < chip->vbat_monitor_params.low_thr) {
2751 configure_vbat_monitor_low(chip);
2752 } else {
2753 pr_debug("faulty btm trigger, discarding\n");
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002754 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002755 &chip->vbat_monitor_params);
2756 }
2757 } else if (state == ADC_TM_HIGH_STATE) {
2758 pr_debug("high voltage btm notification triggered\n");
2759 if (vbat_uv + VBATT_ERROR_MARGIN
2760 > chip->vbat_monitor_params.high_thr) {
2761 configure_vbat_monitor_high(chip);
2762 } else {
2763 pr_debug("faulty btm trigger, discarding\n");
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002764 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002765 &chip->vbat_monitor_params);
2766 }
2767 } else {
2768 pr_debug("unknown voltage notification state: %d\n", state);
2769 }
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002770 if (chip->bms_psy_registered)
Xiaozhe Shifa120db2013-06-06 15:57:19 -07002771 power_supply_changed(&chip->bms_psy);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002772}
2773
2774static int reset_vbat_monitoring(struct qpnp_bms_chip *chip)
2775{
2776 int rc;
2777
2778 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_DISABLE;
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002779
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002780 rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2781 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002782 if (rc) {
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002783 pr_err("tm disable failed: %d\n", rc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002784 return rc;
2785 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002786 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2787 pr_debug("battery removed, releasing wakelock\n");
2788 wake_unlock(&chip->low_voltage_wake_lock);
2789 }
2790 if (chip->in_cv_range) {
2791 pr_debug("battery removed, removing in_cv_range state\n");
2792 chip->in_cv_range = false;
2793 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002794 return 0;
2795}
2796
2797static int setup_vbat_monitoring(struct qpnp_bms_chip *chip)
2798{
2799 int rc;
2800
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002801 chip->vbat_monitor_params.low_thr = chip->low_voltage_threshold;
2802 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2803 - VBATT_ERROR_MARGIN;
2804 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_ENABLE;
2805 chip->vbat_monitor_params.channel = VBAT_SNS;
2806 chip->vbat_monitor_params.btm_ctx = (void *)chip;
2807 chip->vbat_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
2808 chip->vbat_monitor_params.threshold_notification = &btm_notify_vbat;
2809 pr_debug("set low thr to %d and high to %d\n",
2810 chip->vbat_monitor_params.low_thr,
2811 chip->vbat_monitor_params.high_thr);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002812
2813 if (!is_battery_present(chip)) {
2814 pr_debug("no battery inserted, do not enable vbat monitoring\n");
2815 chip->vbat_monitor_params.state_request =
2816 ADC_TM_HIGH_LOW_THR_DISABLE;
2817 } else {
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002818 rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2819 &chip->vbat_monitor_params);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002820 if (rc) {
2821 pr_err("tm setup failed: %d\n", rc);
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002822 return rc;
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002823 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002824 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002825
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002826 pr_debug("setup complete\n");
2827 return 0;
2828}
2829
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302830static void readjust_fcc_table(struct qpnp_bms_chip *chip)
2831{
2832 struct single_row_lut *temp, *old;
2833 int i, fcc, ratio;
2834
2835 if (!chip->enable_fcc_learning)
2836 return;
2837
2838 if (!chip->fcc_temp_lut) {
2839 pr_err("The static fcc lut table is NULL\n");
2840 return;
2841 }
2842
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07002843 temp = devm_kzalloc(chip->dev, sizeof(struct single_row_lut),
2844 GFP_KERNEL);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302845 if (!temp) {
2846 pr_err("Cannot allocate memory for adjusted fcc table\n");
2847 return;
2848 }
2849
2850 fcc = interpolate_fcc(chip->fcc_temp_lut, chip->fcc_new_batt_temp);
2851
2852 temp->cols = chip->fcc_temp_lut->cols;
2853 for (i = 0; i < chip->fcc_temp_lut->cols; i++) {
2854 temp->x[i] = chip->fcc_temp_lut->x[i];
2855 ratio = div_u64(chip->fcc_temp_lut->y[i] * 1000, fcc);
2856 temp->y[i] = (ratio * chip->fcc_new_mah);
2857 temp->y[i] /= 1000;
2858 }
2859
2860 old = chip->adjusted_fcc_temp_lut;
2861 chip->adjusted_fcc_temp_lut = temp;
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07002862 devm_kfree(chip->dev, old);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302863}
2864
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302865static int read_fcc_data_from_backup(struct qpnp_bms_chip *chip)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302866{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302867 int rc, i;
2868 u8 fcc = 0, chgcyl = 0;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302869
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302870 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2871 rc = qpnp_read_wrapper(chip, &fcc,
2872 chip->base + BMS_FCC_BASE_REG + i, 1);
2873 rc |= qpnp_read_wrapper(chip, &chgcyl,
2874 chip->base + BMS_CHGCYL_BASE_REG + i, 1);
2875 if (rc) {
2876 pr_err("Unable to read FCC data\n");
2877 return rc;
2878 }
2879 if (fcc == 0 || (fcc == 0xFF && chgcyl == 0xFF)) {
2880 /* FCC invalid/not present */
2881 chip->fcc_learning_samples[i].fcc_new = 0;
2882 chip->fcc_learning_samples[i].chargecycles = 0;
2883 } else {
2884 /* valid FCC data */
2885 chip->fcc_sample_count++;
2886 chip->fcc_learning_samples[i].fcc_new =
2887 fcc * chip->fcc_resolution;
2888 chip->fcc_learning_samples[i].chargecycles =
2889 chgcyl * CHGCYL_RESOLUTION;
2890 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302891 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302892
2893 return 0;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302894}
2895
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302896static int discard_backup_fcc_data(struct qpnp_bms_chip *chip)
2897{
2898 int rc = 0, i;
2899 u8 temp_u8 = 0;
2900
2901 chip->fcc_sample_count = 0;
2902 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2903 rc = qpnp_write_wrapper(chip, &temp_u8,
2904 chip->base + BMS_FCC_BASE_REG + i, 1);
2905 rc |= qpnp_write_wrapper(chip, &temp_u8,
2906 chip->base + BMS_CHGCYL_BASE_REG + i, 1);
2907 if (rc) {
2908 pr_err("Unable to clear FCC data\n");
2909 return rc;
2910 }
2911 }
2912
2913 return 0;
2914}
2915
2916static void
2917average_fcc_samples_and_readjust_fcc_table(struct qpnp_bms_chip *chip)
2918{
2919 int i, temp_fcc_avg = 0, temp_fcc_delta = 0, new_fcc_avg = 0;
2920 struct fcc_sample *ft;
2921
2922 for (i = 0; i < chip->min_fcc_learning_samples; i++)
2923 temp_fcc_avg += chip->fcc_learning_samples[i].fcc_new;
2924
2925 temp_fcc_avg /= chip->min_fcc_learning_samples;
2926 temp_fcc_delta = div_u64(temp_fcc_avg * DELTA_FCC_PERCENT, 100);
2927
2928 /* fix the fcc if its an outlier i.e. > 5% of the average */
2929 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2930 ft = &chip->fcc_learning_samples[i];
2931 if (abs(ft->fcc_new - temp_fcc_avg) > temp_fcc_delta)
2932 new_fcc_avg += temp_fcc_avg;
2933 else
2934 new_fcc_avg += ft->fcc_new;
2935 }
2936 new_fcc_avg /= chip->min_fcc_learning_samples;
2937
2938 chip->fcc_new_mah = new_fcc_avg;
2939 chip->fcc_new_batt_temp = FCC_DEFAULT_TEMP;
2940 pr_info("FCC update: New fcc_mah=%d, fcc_batt_temp=%d\n",
2941 new_fcc_avg, FCC_DEFAULT_TEMP);
2942 readjust_fcc_table(chip);
2943}
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302944
2945static void backup_charge_cycle(struct qpnp_bms_chip *chip)
2946{
2947 int rc = 0;
2948
2949 if (chip->charge_increase >= 0) {
2950 rc = qpnp_write_wrapper(chip, &chip->charge_increase,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302951 chip->base + CHARGE_INCREASE_STORAGE, 1);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302952 if (rc)
2953 pr_err("Unable to backup charge_increase\n");
2954 }
2955
2956 if (chip->charge_cycles >= 0) {
2957 rc = qpnp_write_wrapper(chip, (u8 *)&chip->charge_cycles,
2958 chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
2959 if (rc)
2960 pr_err("Unable to backup charge_cycles\n");
2961 }
2962}
2963
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302964static bool chargecycles_in_range(struct qpnp_bms_chip *chip)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302965{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302966 int i, min_cycle, max_cycle, valid_range;
2967
2968 /* find the smallest and largest charge cycle */
2969 max_cycle = min_cycle = chip->fcc_learning_samples[0].chargecycles;
2970 for (i = 1; i < chip->min_fcc_learning_samples; i++) {
2971 if (min_cycle > chip->fcc_learning_samples[i].chargecycles)
2972 min_cycle = chip->fcc_learning_samples[i].chargecycles;
2973 if (max_cycle < chip->fcc_learning_samples[i].chargecycles)
2974 max_cycle = chip->fcc_learning_samples[i].chargecycles;
2975 }
2976
2977 /* check if chargecyles are in range to continue with FCC update */
2978 valid_range = DIV_ROUND_UP(VALID_FCC_CHGCYL_RANGE,
2979 CHGCYL_RESOLUTION) * CHGCYL_RESOLUTION;
2980 if (abs(max_cycle - min_cycle) > valid_range)
2981 return false;
2982
2983 return true;
2984}
2985
2986static int read_chgcycle_data_from_backup(struct qpnp_bms_chip *chip)
2987{
2988 int rc;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302989 uint16_t temp_u16 = 0;
2990 u8 temp_u8 = 0;
2991
2992 rc = qpnp_read_wrapper(chip, &temp_u8,
2993 chip->base + CHARGE_INCREASE_STORAGE, 1);
2994 if (!rc && temp_u8 != 0xFF)
2995 chip->charge_increase = temp_u8;
2996
2997 rc = qpnp_read_wrapper(chip, (u8 *)&temp_u16,
2998 chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
2999 if (!rc && temp_u16 != 0xFFFF)
3000 chip->charge_cycles = temp_u16;
3001
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303002 return rc;
3003}
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303004
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303005static void
3006attempt_learning_new_fcc(struct qpnp_bms_chip *chip)
3007{
3008 pr_debug("Total FCC sample count=%d\n", chip->fcc_sample_count);
3009
3010 /* update FCC if we have the required samples */
3011 if ((chip->fcc_sample_count == chip->min_fcc_learning_samples) &&
3012 chargecycles_in_range(chip))
3013 average_fcc_samples_and_readjust_fcc_table(chip);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303014}
3015
3016static int calculate_real_soc(struct qpnp_bms_chip *chip,
3017 int batt_temp, struct raw_soc_params *raw, int cc_uah)
3018{
3019 int fcc_uah, rc_uah;
3020
3021 fcc_uah = calculate_fcc(chip, batt_temp);
3022 rc_uah = calculate_ocv_charge(chip, raw, fcc_uah);
3023
3024 return ((rc_uah - cc_uah) * 100) / fcc_uah;
3025}
3026
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303027#define MAX_U8_VALUE ((u8)(~0U))
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303028
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303029static int backup_new_fcc(struct qpnp_bms_chip *chip, int fcc_mah,
3030 int chargecycles)
3031{
3032 int rc, min_cycle, i;
3033 u8 fcc_new, chgcyl, pos = 0;
3034 struct fcc_sample *ft;
3035
3036 if ((fcc_mah > (chip->fcc_resolution * MAX_U8_VALUE)) ||
3037 (chargecycles > (CHGCYL_RESOLUTION * MAX_U8_VALUE))) {
3038 pr_warn("FCC/Chgcyl beyond storage limit. FCC=%d, chgcyl=%d\n",
3039 fcc_mah, chargecycles);
3040 return -EINVAL;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303041 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303042
3043 if (chip->fcc_sample_count == chip->min_fcc_learning_samples) {
3044 /* search best location - oldest entry */
3045 min_cycle = chip->fcc_learning_samples[0].chargecycles;
3046 for (i = 1; i < chip->min_fcc_learning_samples; i++) {
3047 if (min_cycle >
3048 chip->fcc_learning_samples[i].chargecycles)
3049 pos = i;
3050 }
3051 } else {
3052 /* find an empty location */
3053 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
3054 ft = &chip->fcc_learning_samples[i];
3055 if (ft->fcc_new == 0 || (ft->fcc_new == 0xFF &&
3056 ft->chargecycles == 0xFF)) {
3057 pos = i;
3058 break;
3059 }
3060 }
3061 chip->fcc_sample_count++;
3062 }
3063 chip->fcc_learning_samples[pos].fcc_new = fcc_mah;
3064 chip->fcc_learning_samples[pos].chargecycles = chargecycles;
3065
3066 fcc_new = DIV_ROUND_UP(fcc_mah, chip->fcc_resolution);
3067 rc = qpnp_write_wrapper(chip, (u8 *)&fcc_new,
3068 chip->base + BMS_FCC_BASE_REG + pos, 1);
3069 if (rc)
3070 return rc;
3071
3072 chgcyl = DIV_ROUND_UP(chargecycles, CHGCYL_RESOLUTION);
3073 rc = qpnp_write_wrapper(chip, (u8 *)&chgcyl,
3074 chip->base + BMS_CHGCYL_BASE_REG + pos, 1);
3075 if (rc)
3076 return rc;
3077
3078 pr_debug("Backup new FCC: fcc_new=%d, chargecycle=%d, pos=%d\n",
3079 fcc_new, chgcyl, pos);
3080
3081 return rc;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303082}
3083
3084static void update_fcc_learning_table(struct qpnp_bms_chip *chip,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303085 int new_fcc_uah, int chargecycles, int batt_temp)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303086{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303087 int rc, fcc_default, fcc_temp;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303088
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303089 /* convert the fcc at batt_temp to new fcc at FCC_DEFAULT_TEMP */
3090 fcc_default = calculate_fcc(chip, FCC_DEFAULT_TEMP) / 1000;
3091 fcc_temp = calculate_fcc(chip, batt_temp) / 1000;
3092 new_fcc_uah = (new_fcc_uah / fcc_temp) * fcc_default;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303093
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303094 rc = backup_new_fcc(chip, new_fcc_uah / 1000, chargecycles);
3095 if (rc) {
3096 pr_err("Unable to backup new FCC\n");
3097 return;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303098 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303099 /* check if FCC can be updated */
3100 attempt_learning_new_fcc(chip);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303101}
3102
3103static bool is_new_fcc_valid(int new_fcc_uah, int fcc_uah)
3104{
3105 if ((new_fcc_uah >= (fcc_uah / 2)) &&
3106 ((new_fcc_uah * 100) <= (fcc_uah * 105)))
3107 return true;
3108
3109 pr_debug("FCC rejected - not within valid limit\n");
3110 return false;
3111}
3112
3113static void fcc_learning_config(struct qpnp_bms_chip *chip, bool start)
3114{
3115 int rc, batt_temp;
3116 struct raw_soc_params raw;
3117 struct qpnp_vadc_result result;
3118 int fcc_uah, new_fcc_uah, delta_cc_uah, delta_soc;
3119
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003120 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303121 if (rc) {
3122 pr_err("Unable to read batt_temp\n");
3123 return;
3124 } else {
3125 batt_temp = (int)result.physical;
3126 }
3127
3128 rc = read_soc_params_raw(chip, &raw, batt_temp);
3129 if (rc) {
3130 pr_err("Unable to read CC, cannot update FCC\n");
3131 return;
3132 }
3133
3134 if (start) {
3135 chip->start_pc = interpolate_pc(chip->pc_temp_ocv_lut,
3136 batt_temp / 10, raw.last_good_ocv_uv / 1000);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003137 chip->start_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303138 chip->start_real_soc = calculate_real_soc(chip,
3139 batt_temp, &raw, chip->start_cc_uah);
3140 pr_debug("start_pc=%d, start_cc=%d, start_soc=%d real_soc=%d\n",
3141 chip->start_pc, chip->start_cc_uah,
3142 chip->start_soc, chip->start_real_soc);
3143 } else {
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003144 chip->end_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303145 delta_soc = 100 - chip->start_real_soc;
3146 delta_cc_uah = abs(chip->end_cc_uah - chip->start_cc_uah);
3147 new_fcc_uah = div_u64(delta_cc_uah * 100, delta_soc);
3148 fcc_uah = calculate_fcc(chip, batt_temp);
3149 pr_debug("start_soc=%d, start_pc=%d, start_real_soc=%d, start_cc=%d, end_cc=%d, new_fcc=%d\n",
3150 chip->start_soc, chip->start_pc, chip->start_real_soc,
3151 chip->start_cc_uah, chip->end_cc_uah, new_fcc_uah);
3152
3153 if (is_new_fcc_valid(new_fcc_uah, fcc_uah))
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303154 update_fcc_learning_table(chip, new_fcc_uah,
3155 chip->charge_cycles, batt_temp);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303156 }
3157}
3158
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003159#define MAX_CAL_TRIES 200
3160#define MIN_CAL_UA 3000
3161static void batfet_open_work(struct work_struct *work)
3162{
3163 int i;
3164 int rc;
3165 int result_ua;
3166 u8 orig_delay, sample_delay;
3167 struct qpnp_bms_chip *chip = container_of(work,
3168 struct qpnp_bms_chip,
3169 batfet_open_work);
3170
3171 rc = qpnp_read_wrapper(chip, &orig_delay,
3172 chip->base + BMS1_S1_DELAY_CTL, 1);
3173
3174 sample_delay = 0x0;
3175 rc = qpnp_write_wrapper(chip, &sample_delay,
3176 chip->base + BMS1_S1_DELAY_CTL, 1);
3177
3178 /*
3179 * In certain PMICs there is a coupling issue which causes
3180 * bad calibration value that result in a huge battery current
3181 * even when the BATFET is open. Do continious calibrations until
3182 * we hit reasonable cal values which result in low battery current
3183 */
3184
3185 for (i = 0; (!chip->batfet_closed) && i < MAX_CAL_TRIES; i++) {
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003186 rc = qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003187 /*
3188 * Wait 20mS after calibration and before reading battery
3189 * current. The BMS h/w uses calibration values in the
3190 * next sampling of vsense.
3191 */
3192 msleep(20);
3193 rc |= get_battery_current(chip, &result_ua);
3194 if (rc == 0 && abs(result_ua) <= MIN_CAL_UA) {
3195 pr_debug("good cal at %d attempt\n", i);
3196 break;
3197 }
3198 }
3199 pr_debug("batfet_closed = %d i = %d result_ua = %d\n",
3200 chip->batfet_closed, i, result_ua);
3201
3202 rc = qpnp_write_wrapper(chip, &orig_delay,
3203 chip->base + BMS1_S1_DELAY_CTL, 1);
3204}
3205
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003206static void charging_began(struct qpnp_bms_chip *chip)
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003207{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003208 mutex_lock(&chip->last_soc_mutex);
3209 chip->charge_start_tm_sec = 0;
3210 chip->catch_up_time_sec = 0;
3211 mutex_unlock(&chip->last_soc_mutex);
3212
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303213 chip->start_soc = report_state_of_charge(chip);
3214
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003215 mutex_lock(&chip->last_ocv_uv_mutex);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303216 if (chip->enable_fcc_learning)
3217 fcc_learning_config(chip, true);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003218 chip->soc_at_cv = -EINVAL;
3219 chip->prev_chg_soc = -EINVAL;
3220 mutex_unlock(&chip->last_ocv_uv_mutex);
3221}
3222
3223static void charging_ended(struct qpnp_bms_chip *chip)
3224{
3225 mutex_lock(&chip->last_soc_mutex);
3226 chip->charge_start_tm_sec = 0;
3227 chip->catch_up_time_sec = 0;
3228 mutex_unlock(&chip->last_soc_mutex);
3229
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303230 chip->end_soc = report_state_of_charge(chip);
3231
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003232 mutex_lock(&chip->last_ocv_uv_mutex);
3233 chip->soc_at_cv = -EINVAL;
3234 chip->prev_chg_soc = -EINVAL;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303235
3236 /* update the chargecycles */
3237 if (chip->end_soc > chip->start_soc) {
3238 chip->charge_increase += (chip->end_soc - chip->start_soc);
3239 if (chip->charge_increase > 100) {
3240 chip->charge_cycles++;
3241 chip->charge_increase = chip->charge_increase % 100;
3242 }
3243 if (chip->enable_fcc_learning)
3244 backup_charge_cycle(chip);
3245 }
3246
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003247 if (get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL) {
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303248 if (chip->enable_fcc_learning &&
3249 (chip->start_soc <= chip->min_fcc_learning_soc) &&
3250 (chip->start_pc <= chip->min_fcc_ocv_pc))
3251 fcc_learning_config(chip, false);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003252 chip->done_charging = true;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003253 chip->last_soc_invalid = true;
Xiaozhe Shicc48e992013-05-28 16:42:24 -07003254 } else if (chip->charging_adjusted_ocv > 0) {
3255 pr_debug("Charging stopped before full, adjusted OCV = %d\n",
3256 chip->charging_adjusted_ocv);
3257 chip->last_ocv_uv = chip->charging_adjusted_ocv;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003258 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303259
Xiaozhe Shicc48e992013-05-28 16:42:24 -07003260 chip->charging_adjusted_ocv = -EINVAL;
3261
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003262 mutex_unlock(&chip->last_ocv_uv_mutex);
3263}
3264
3265static void battery_status_check(struct qpnp_bms_chip *chip)
3266{
3267 int status = get_battery_status(chip);
3268
Xiaozhe Shibda84992013-09-05 10:39:11 -07003269 mutex_lock(&chip->status_lock);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003270 if (chip->battery_status != status) {
Xiaozhe Shi30e94802013-08-19 16:40:53 -07003271 pr_debug("status = %d, shadow status = %d\n",
3272 status, chip->battery_status);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003273 if (status == POWER_SUPPLY_STATUS_CHARGING) {
3274 pr_debug("charging started\n");
3275 charging_began(chip);
3276 } else if (chip->battery_status
3277 == POWER_SUPPLY_STATUS_CHARGING) {
3278 pr_debug("charging ended\n");
3279 charging_ended(chip);
3280 }
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003281
3282 if (status == POWER_SUPPLY_STATUS_FULL) {
3283 pr_debug("battery full\n");
Xiaozhe Shibda84992013-09-05 10:39:11 -07003284 recalculate_soc(chip);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003285 } else if (chip->battery_status
3286 == POWER_SUPPLY_STATUS_FULL) {
3287 pr_debug("battery not full any more\n");
3288 disable_bms_irq(&chip->ocv_thr_irq);
3289 disable_bms_irq(&chip->sw_cc_thr_irq);
3290 }
3291
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003292 chip->battery_status = status;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003293 /* battery charge status has changed, so force a soc
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003294 * recalculation to update the SoC */
3295 schedule_work(&chip->recalc_work);
3296 }
Xiaozhe Shibda84992013-09-05 10:39:11 -07003297 mutex_unlock(&chip->status_lock);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003298}
3299
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003300#define CALIB_WRKARND_DIG_MAJOR_MAX 0x03
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003301static void batfet_status_check(struct qpnp_bms_chip *chip)
3302{
3303 bool batfet_closed;
3304
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003305 batfet_closed = is_batfet_closed(chip);
3306 if (chip->batfet_closed != batfet_closed) {
3307 chip->batfet_closed = batfet_closed;
Xiaozhe Shiffb7cfc2014-01-03 10:47:36 -08003308 if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX)
3309 return;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003310 if (batfet_closed == false) {
3311 /* batfet opened */
3312 schedule_work(&chip->batfet_open_work);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003313 qpnp_iadc_skip_calibration(chip->iadc_dev);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003314 } else {
3315 /* batfet closed */
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003316 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, true);
3317 qpnp_iadc_resume_calibration(chip->iadc_dev);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003318 }
3319 }
3320}
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003321
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003322static void battery_insertion_check(struct qpnp_bms_chip *chip)
3323{
Xiaozhe Shi90f3a412013-08-21 10:31:35 -07003324 int present = (int)is_battery_present(chip);
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07003325 int insertion_ocv_uv = get_battery_insertion_ocv_uv(chip);
3326 int insertion_ocv_taken = (insertion_ocv_uv > 0);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003327
3328 mutex_lock(&chip->vbat_monitor_mutex);
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07003329 if (chip->battery_present != present
3330 && (present == insertion_ocv_taken
3331 || chip->battery_present == -EINVAL)) {
3332 pr_debug("status = %d, shadow status = %d, insertion_ocv_uv = %d\n",
3333 present, chip->battery_present,
3334 insertion_ocv_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003335 if (chip->battery_present != -EINVAL) {
3336 if (present) {
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07003337 chip->insertion_ocv_uv = insertion_ocv_uv;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003338 setup_vbat_monitoring(chip);
3339 chip->new_battery = true;
3340 } else {
3341 reset_vbat_monitoring(chip);
3342 }
3343 }
3344 chip->battery_present = present;
3345 /* a new battery was inserted or removed, so force a soc
3346 * recalculation to update the SoC */
3347 schedule_work(&chip->recalc_work);
3348 }
3349 mutex_unlock(&chip->vbat_monitor_mutex);
3350}
3351
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003352/* Returns capacity as a SoC percentage between 0 and 100 */
3353static int get_prop_bms_capacity(struct qpnp_bms_chip *chip)
3354{
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08003355 return report_state_of_charge(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003356}
3357
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003358static void qpnp_bms_external_power_changed(struct power_supply *psy)
3359{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003360 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
3361 bms_psy);
3362
3363 battery_insertion_check(chip);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003364 batfet_status_check(chip);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003365 battery_status_check(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003366}
3367
3368static int qpnp_bms_power_get_property(struct power_supply *psy,
3369 enum power_supply_property psp,
3370 union power_supply_propval *val)
3371{
3372 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
3373 bms_psy);
3374
3375 switch (psp) {
3376 case POWER_SUPPLY_PROP_CAPACITY:
3377 val->intval = get_prop_bms_capacity(chip);
3378 break;
Xiaozhe Shibda84992013-09-05 10:39:11 -07003379 case POWER_SUPPLY_PROP_STATUS:
3380 val->intval = chip->battery_status;
3381 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003382 case POWER_SUPPLY_PROP_CURRENT_NOW:
3383 val->intval = get_prop_bms_current_now(chip);
3384 break;
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07003385 case POWER_SUPPLY_PROP_RESISTANCE:
3386 val->intval = get_prop_bms_batt_resistance(chip);
3387 break;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07003388 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
3389 val->intval = get_prop_bms_charge_counter(chip);
3390 break;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003391 case POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW:
3392 val->intval = get_prop_bms_charge_counter_shadow(chip);
3393 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003394 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
3395 val->intval = get_prop_bms_charge_full_design(chip);
3396 break;
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05303397 case POWER_SUPPLY_PROP_CHARGE_FULL:
3398 val->intval = get_prop_bms_charge_full(chip);
3399 break;
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05303400 case POWER_SUPPLY_PROP_CYCLE_COUNT:
3401 val->intval = chip->charge_cycles;
3402 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003403 default:
3404 return -EINVAL;
3405 }
3406 return 0;
3407}
3408
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003409#define OCV_USE_LIMIT_EN BIT(7)
3410static int set_ocv_voltage_thresholds(struct qpnp_bms_chip *chip,
3411 int low_voltage_threshold,
3412 int high_voltage_threshold)
3413{
3414 uint16_t low_voltage_raw, high_voltage_raw;
3415 int rc;
3416
3417 low_voltage_raw = convert_vbatt_uv_to_raw(chip,
3418 low_voltage_threshold);
3419 high_voltage_raw = convert_vbatt_uv_to_raw(chip,
3420 high_voltage_threshold);
3421 rc = qpnp_write_wrapper(chip, (u8 *)&low_voltage_raw,
3422 chip->base + BMS1_OCV_USE_LOW_LIMIT_THR0, 2);
3423 if (rc) {
3424 pr_err("Failed to set ocv low voltage threshold: %d\n", rc);
3425 return rc;
3426 }
3427 rc = qpnp_write_wrapper(chip, (u8 *)&high_voltage_raw,
3428 chip->base + BMS1_OCV_USE_HIGH_LIMIT_THR0, 2);
3429 if (rc) {
3430 pr_err("Failed to set ocv high voltage threshold: %d\n", rc);
3431 return rc;
3432 }
3433 rc = qpnp_masked_write(chip, BMS1_OCV_USE_LIMIT_CTL,
3434 OCV_USE_LIMIT_EN, OCV_USE_LIMIT_EN);
3435 if (rc) {
3436 pr_err("Failed to enabled ocv voltage thresholds: %d\n", rc);
3437 return rc;
3438 }
3439 pr_debug("ocv low threshold set to %d uv or 0x%x raw\n",
3440 low_voltage_threshold, low_voltage_raw);
3441 pr_debug("ocv high threshold set to %d uv or 0x%x raw\n",
3442 high_voltage_threshold, high_voltage_raw);
3443 return 0;
3444}
3445
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003446static int read_shutdown_iavg_ma(struct qpnp_bms_chip *chip)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003447{
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003448 u8 iavg;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003449 int rc;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003450
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003451 rc = qpnp_read_wrapper(chip, &iavg, chip->base + IAVG_STORAGE_REG, 1);
3452 if (rc) {
3453 pr_err("failed to read addr = %d %d assuming %d\n",
3454 chip->base + IAVG_STORAGE_REG, rc,
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003455 MIN_IAVG_MA);
3456 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003457 } else if (iavg == IAVG_INVALID) {
3458 pr_err("invalid iavg read from BMS1_DATA_REG_1, using %d\n",
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003459 MIN_IAVG_MA);
3460 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003461 } else {
3462 if (iavg == 0)
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003463 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003464 else
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003465 return MIN_IAVG_MA + IAVG_STEP_SIZE_MA * iavg;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003466 }
3467}
3468
3469static int read_shutdown_soc(struct qpnp_bms_chip *chip)
3470{
3471 u8 stored_soc;
3472 int rc, shutdown_soc;
3473
3474 /*
3475 * The previous SOC is stored in the first 7 bits of the register as
3476 * (Shutdown SOC + 1). This allows for register reset values of both
3477 * 0x00 and 0x7F.
3478 */
3479 rc = qpnp_read_wrapper(chip, &stored_soc, chip->soc_storage_addr, 1);
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003480 if (rc) {
3481 pr_err("failed to read addr = %d %d\n",
3482 chip->soc_storage_addr, rc);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003483 return SOC_INVALID;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003484 }
3485
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003486 if ((stored_soc >> 1) > 0)
3487 shutdown_soc = (stored_soc >> 1) - 1;
3488 else
3489 shutdown_soc = SOC_INVALID;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003490
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003491 pr_debug("stored soc = 0x%02x, shutdown_soc = %d\n",
3492 stored_soc, shutdown_soc);
3493 return shutdown_soc;
3494}
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003495
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003496#define BAT_REMOVED_OFFMODE_BIT BIT(6)
3497static bool is_battery_replaced_in_offmode(struct qpnp_bms_chip *chip)
3498{
3499 u8 batt_pres;
3500 int rc;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003501
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003502 if (chip->batt_pres_addr) {
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003503 rc = qpnp_read_wrapper(chip, &batt_pres,
3504 chip->batt_pres_addr, 1);
3505 pr_debug("offmode removed: %02x\n", batt_pres);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003506 if (!rc && (batt_pres & BAT_REMOVED_OFFMODE_BIT))
3507 return true;
3508 }
3509 return false;
3510}
3511
3512static void load_shutdown_data(struct qpnp_bms_chip *chip)
3513{
3514 int calculated_soc, shutdown_soc;
3515 bool invalid_stored_soc;
3516 bool offmode_battery_replaced;
3517 bool shutdown_soc_out_of_limit;
3518
3519 /*
3520 * Read the saved shutdown SoC from the configured register and
3521 * check if the value has been reset
3522 */
3523 shutdown_soc = read_shutdown_soc(chip);
3524 invalid_stored_soc = (shutdown_soc == SOC_INVALID);
3525
3526 /*
3527 * Do a quick run of SoC calculation to find whether the shutdown soc
3528 * is close enough.
3529 */
3530 calculated_soc = recalculate_raw_soc(chip);
3531 shutdown_soc_out_of_limit = (abs(shutdown_soc - calculated_soc)
3532 > chip->shutdown_soc_valid_limit);
3533 pr_debug("calculated_soc = %d, valid_limit = %d\n",
3534 calculated_soc, chip->shutdown_soc_valid_limit);
3535
3536 /*
3537 * Check if the battery has been replaced while the system was powered
3538 * down.
3539 */
3540 offmode_battery_replaced = is_battery_replaced_in_offmode(chip);
3541
3542 /* Invalidate the shutdown SoC if any of these conditions hold true */
3543 if (chip->ignore_shutdown_soc
3544 || invalid_stored_soc
3545 || offmode_battery_replaced
3546 || shutdown_soc_out_of_limit) {
3547 chip->battery_removed = true;
3548 chip->shutdown_soc_invalid = true;
Xiaozhe Shic92cfd92013-10-25 11:36:42 -07003549 chip->shutdown_iavg_ma = MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003550 pr_debug("Ignoring shutdown SoC: invalid = %d, offmode = %d, out_of_limit = %d\n",
3551 invalid_stored_soc, offmode_battery_replaced,
3552 shutdown_soc_out_of_limit);
3553 } else {
3554 chip->shutdown_iavg_ma = read_shutdown_iavg_ma(chip);
3555 chip->shutdown_soc = shutdown_soc;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07003556 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303557
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003558 pr_debug("raw_soc = %d shutdown_soc = %d shutdown_iavg = %d shutdown_soc_invalid = %d, battery_removed = %d\n",
3559 calculated_soc,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003560 chip->shutdown_soc,
3561 chip->shutdown_iavg_ma,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303562 chip->shutdown_soc_invalid,
3563 chip->battery_removed);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003564}
3565
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003566static irqreturn_t bms_ocv_thr_irq_handler(int irq, void *_chip)
3567{
3568 struct qpnp_bms_chip *chip = _chip;
3569
3570 pr_debug("ocv_thr irq triggered\n");
3571 bms_stay_awake(&chip->soc_wake_source);
3572 schedule_work(&chip->recalc_work);
3573 return IRQ_HANDLED;
3574}
3575
3576static irqreturn_t bms_sw_cc_thr_irq_handler(int irq, void *_chip)
3577{
3578 struct qpnp_bms_chip *chip = _chip;
3579
3580 pr_debug("sw_cc_thr irq triggered\n");
Anirudh Ghayal1166eef2013-12-23 19:05:33 +05303581 disable_bms_irq_nosync(&chip->sw_cc_thr_irq);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003582 bms_stay_awake(&chip->soc_wake_source);
3583 schedule_work(&chip->recalc_work);
3584 return IRQ_HANDLED;
3585}
3586
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003587static int64_t read_battery_id(struct qpnp_bms_chip *chip)
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003588{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003589 int rc;
3590 struct qpnp_vadc_result result;
3591
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003592 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX2_BAT_ID, &result);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003593 if (rc) {
3594 pr_err("error reading batt id channel = %d, rc = %d\n",
3595 LR_MUX2_BAT_ID, rc);
3596 return rc;
3597 }
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003598
3599 return result.physical;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003600}
3601
3602static int set_battery_data(struct qpnp_bms_chip *chip)
3603{
3604 int64_t battery_id;
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003605 int rc = 0, dt_data = false;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003606 struct bms_battery_data *batt_data;
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003607 struct device_node *node;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003608
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003609 if (chip->batt_type == BATT_DESAY) {
3610 batt_data = &desay_5200_data;
3611 } else if (chip->batt_type == BATT_PALLADIUM) {
3612 batt_data = &palladium_1500_data;
3613 } else if (chip->batt_type == BATT_OEM) {
3614 batt_data = &oem_batt_data;
Wu Fenglin2ac88aa2013-04-25 12:43:40 +08003615 } else if (chip->batt_type == BATT_QRD_4V35_2000MAH) {
3616 batt_data = &QRD_4v35_2000mAh_data;
tingtingf50326f2013-06-05 15:07:24 +08003617 } else if (chip->batt_type == BATT_QRD_4V2_1300MAH) {
3618 batt_data = &qrd_4v2_1300mah_data;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003619 } else {
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003620 battery_id = read_battery_id(chip);
3621 if (battery_id < 0) {
3622 pr_err("cannot read battery id err = %lld\n",
3623 battery_id);
3624 return battery_id;
3625 }
3626
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003627 node = of_find_node_by_name(chip->spmi->dev.of_node,
3628 "qcom,battery-data");
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003629 if (!node) {
3630 pr_warn("No available batterydata, using palladium 1500\n");
3631 batt_data = &palladium_1500_data;
3632 goto assign_data;
3633 }
3634 batt_data = devm_kzalloc(chip->dev,
3635 sizeof(struct bms_battery_data), GFP_KERNEL);
3636 if (!batt_data) {
3637 pr_err("Could not alloc battery data\n");
3638 batt_data = &palladium_1500_data;
3639 goto assign_data;
3640 }
3641 batt_data->fcc_temp_lut = devm_kzalloc(chip->dev,
3642 sizeof(struct single_row_lut),
3643 GFP_KERNEL);
3644 batt_data->pc_temp_ocv_lut = devm_kzalloc(chip->dev,
3645 sizeof(struct pc_temp_ocv_lut),
3646 GFP_KERNEL);
3647 batt_data->rbatt_sf_lut = devm_kzalloc(chip->dev,
3648 sizeof(struct sf_lut),
3649 GFP_KERNEL);
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003650
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003651 batt_data->max_voltage_uv = -1;
3652 batt_data->cutoff_uv = -1;
3653 batt_data->iterm_ua = -1;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003654
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003655 /*
3656 * if the alloced luts are 0s, of_batterydata_read_data ignores
3657 * them.
3658 */
3659 rc = of_batterydata_read_data(node, batt_data, battery_id);
3660 if (rc == 0 && batt_data->fcc_temp_lut
3661 && batt_data->pc_temp_ocv_lut
3662 && batt_data->rbatt_sf_lut) {
3663 dt_data = true;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003664 } else {
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003665 pr_err("battery data load failed, using palladium 1500\n");
3666 devm_kfree(chip->dev, batt_data->fcc_temp_lut);
3667 devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut);
3668 devm_kfree(chip->dev, batt_data->rbatt_sf_lut);
3669 devm_kfree(chip->dev, batt_data);
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003670 batt_data = &palladium_1500_data;
3671 }
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003672 }
3673
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003674assign_data:
Xiaozhe Shi976618f2013-04-30 10:49:30 -07003675 chip->fcc_mah = batt_data->fcc;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003676 chip->fcc_temp_lut = batt_data->fcc_temp_lut;
3677 chip->fcc_sf_lut = batt_data->fcc_sf_lut;
3678 chip->pc_temp_ocv_lut = batt_data->pc_temp_ocv_lut;
3679 chip->pc_sf_lut = batt_data->pc_sf_lut;
3680 chip->rbatt_sf_lut = batt_data->rbatt_sf_lut;
3681 chip->default_rbatt_mohm = batt_data->default_rbatt_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07003682 chip->rbatt_capacitive_mohm = batt_data->rbatt_capacitive_mohm;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07003683 chip->flat_ocv_threshold_uv = batt_data->flat_ocv_threshold_uv;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003684
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003685 /* Override battery properties if specified in the battery profile */
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003686 if (batt_data->max_voltage_uv >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003687 chip->max_voltage_uv = batt_data->max_voltage_uv;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003688 if (batt_data->cutoff_uv >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003689 chip->v_cutoff_uv = batt_data->cutoff_uv;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003690 if (batt_data->iterm_ua >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003691 chip->chg_term_ua = batt_data->iterm_ua;
3692
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003693 if (chip->pc_temp_ocv_lut == NULL) {
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003694 pr_err("temp ocv lut table has not been loaded\n");
3695 if (dt_data) {
3696 devm_kfree(chip->dev, batt_data->fcc_temp_lut);
3697 devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut);
3698 devm_kfree(chip->dev, batt_data->rbatt_sf_lut);
3699 devm_kfree(chip->dev, batt_data);
3700 }
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003701 return -EINVAL;
3702 }
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003703
3704 if (dt_data)
3705 devm_kfree(chip->dev, batt_data);
3706
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003707 return 0;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003708}
3709
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07003710static int bms_get_adc(struct qpnp_bms_chip *chip,
3711 struct spmi_device *spmi)
3712{
3713 int rc = 0;
3714
3715 chip->vadc_dev = qpnp_get_vadc(&spmi->dev, "bms");
3716 if (IS_ERR(chip->vadc_dev)) {
3717 rc = PTR_ERR(chip->vadc_dev);
3718 if (rc != -EPROBE_DEFER)
3719 pr_err("vadc property missing, rc=%d\n", rc);
3720 return rc;
3721 }
3722
3723 chip->iadc_dev = qpnp_get_iadc(&spmi->dev, "bms");
3724 if (IS_ERR(chip->iadc_dev)) {
3725 rc = PTR_ERR(chip->iadc_dev);
3726 if (rc != -EPROBE_DEFER)
3727 pr_err("iadc property missing, rc=%d\n", rc);
3728 return rc;
3729 }
3730
3731 chip->adc_tm_dev = qpnp_get_adc_tm(&spmi->dev, "bms");
3732 if (IS_ERR(chip->adc_tm_dev)) {
3733 rc = PTR_ERR(chip->adc_tm_dev);
3734 if (rc != -EPROBE_DEFER)
3735 pr_err("adc-tm not ready, defer probe\n");
3736 return rc;
3737 }
3738
3739 return 0;
3740}
3741
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003742#define SPMI_PROP_READ(chip_prop, qpnp_spmi_property, retval) \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003743do { \
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003744 if (retval) \
3745 break; \
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003746 retval = of_property_read_u32(chip->spmi->dev.of_node, \
Xiaozhe Shi9bd24622013-01-23 15:54:54 -08003747 "qcom," qpnp_spmi_property, \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003748 &chip->chip_prop); \
3749 if (retval) { \
3750 pr_err("Error reading " #qpnp_spmi_property \
3751 " property %d\n", rc); \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003752 } \
3753} while (0)
3754
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303755#define SPMI_PROP_READ_BOOL(chip_prop, qpnp_spmi_property) \
3756do { \
3757 chip->chip_prop = of_property_read_bool(chip->spmi->dev.of_node,\
3758 "qcom," qpnp_spmi_property); \
3759} while (0)
3760
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003761static inline int bms_read_properties(struct qpnp_bms_chip *chip)
3762{
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003763 int rc = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003764
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003765 SPMI_PROP_READ(r_sense_uohm, "r-sense-uohm", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003766 SPMI_PROP_READ(v_cutoff_uv, "v-cutoff-uv", rc);
3767 SPMI_PROP_READ(max_voltage_uv, "max-voltage-uv", rc);
3768 SPMI_PROP_READ(r_conn_mohm, "r-conn-mohm", rc);
3769 SPMI_PROP_READ(chg_term_ua, "chg-term-ua", rc);
3770 SPMI_PROP_READ(shutdown_soc_valid_limit,
3771 "shutdown-soc-valid-limit", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003772 SPMI_PROP_READ(adjust_soc_low_threshold,
3773 "adjust-soc-low-threshold", rc);
3774 SPMI_PROP_READ(batt_type, "batt-type", rc);
3775 SPMI_PROP_READ(low_soc_calc_threshold,
3776 "low-soc-calculate-soc-threshold", rc);
3777 SPMI_PROP_READ(low_soc_calculate_soc_ms,
3778 "low-soc-calculate-soc-ms", rc);
Xiaozhe Shicb487b12013-10-14 17:42:07 -07003779 SPMI_PROP_READ(low_voltage_calculate_soc_ms,
3780 "low-voltage-calculate-soc-ms", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003781 SPMI_PROP_READ(calculate_soc_ms, "calculate-soc-ms", rc);
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07003782 SPMI_PROP_READ(high_ocv_correction_limit_uv,
3783 "high-ocv-correction-limit-uv", rc);
3784 SPMI_PROP_READ(low_ocv_correction_limit_uv,
3785 "low-ocv-correction-limit-uv", rc);
3786 SPMI_PROP_READ(hold_soc_est,
3787 "hold-soc-est", rc);
3788 SPMI_PROP_READ(ocv_high_threshold_uv,
3789 "ocv-voltage-high-threshold-uv", rc);
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003790 SPMI_PROP_READ(ocv_low_threshold_uv,
3791 "ocv-voltage-low-threshold-uv", rc);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08003792 SPMI_PROP_READ(low_voltage_threshold, "low-voltage-threshold", rc);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003793 SPMI_PROP_READ(temperature_margin, "tm-temp-margin", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003794
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003795 chip->use_external_rsense = of_property_read_bool(
3796 chip->spmi->dev.of_node,
3797 "qcom,use-external-rsense");
3798 chip->ignore_shutdown_soc = of_property_read_bool(
3799 chip->spmi->dev.of_node,
3800 "qcom,ignore-shutdown-soc");
3801 chip->use_voltage_soc = of_property_read_bool(chip->spmi->dev.of_node,
3802 "qcom,use-voltage-soc");
3803 chip->use_ocv_thresholds = of_property_read_bool(
3804 chip->spmi->dev.of_node,
3805 "qcom,use-ocv-thresholds");
3806
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003807 if (chip->adjust_soc_low_threshold >= 45)
3808 chip->adjust_soc_low_threshold = 45;
3809
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303810 SPMI_PROP_READ_BOOL(enable_fcc_learning, "enable-fcc-learning");
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303811 if (chip->enable_fcc_learning) {
3812 SPMI_PROP_READ(min_fcc_learning_soc,
3813 "min-fcc-learning-soc", rc);
3814 SPMI_PROP_READ(min_fcc_ocv_pc,
3815 "min-fcc-ocv-pc", rc);
3816 SPMI_PROP_READ(min_fcc_learning_samples,
3817 "min-fcc-learning-samples", rc);
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303818 SPMI_PROP_READ(fcc_resolution,
3819 "fcc-resolution", rc);
3820 if (chip->min_fcc_learning_samples > MAX_FCC_CYCLES)
3821 chip->min_fcc_learning_samples = MAX_FCC_CYCLES;
3822 chip->fcc_learning_samples = devm_kzalloc(&chip->spmi->dev,
3823 (sizeof(struct fcc_sample) *
3824 chip->min_fcc_learning_samples), GFP_KERNEL);
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003825 if (chip->fcc_learning_samples == NULL)
3826 return -ENOMEM;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303827 pr_debug("min-fcc-soc=%d, min-fcc-pc=%d, min-fcc-cycles=%d\n",
3828 chip->min_fcc_learning_soc, chip->min_fcc_ocv_pc,
3829 chip->min_fcc_learning_samples);
3830 }
3831
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003832 if (rc) {
3833 pr_err("Missing required properties.\n");
3834 return rc;
3835 }
3836
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003837 pr_debug("dts data: r_sense_uohm:%d, v_cutoff_uv:%d, max_v:%d\n",
3838 chip->r_sense_uohm, chip->v_cutoff_uv,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003839 chip->max_voltage_uv);
3840 pr_debug("r_conn:%d, shutdown_soc: %d, adjust_soc_low:%d\n",
3841 chip->r_conn_mohm, chip->shutdown_soc_valid_limit,
3842 chip->adjust_soc_low_threshold);
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07003843 pr_debug("chg_term_ua:%d, batt_type:%d\n",
3844 chip->chg_term_ua,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003845 chip->batt_type);
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08003846 pr_debug("ignore_shutdown_soc:%d, use_voltage_soc:%d\n",
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08003847 chip->ignore_shutdown_soc, chip->use_voltage_soc);
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003848 pr_debug("use external rsense: %d\n", chip->use_external_rsense);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003849 return 0;
3850}
3851
3852static inline void bms_initialize_constants(struct qpnp_bms_chip *chip)
3853{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003854 chip->prev_pc_unusable = -EINVAL;
3855 chip->soc_at_cv = -EINVAL;
3856 chip->calculated_soc = -EINVAL;
Xiaozhe Shie118c692012-09-24 15:17:43 -07003857 chip->last_soc = -EINVAL;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07003858 chip->last_soc_est = -EINVAL;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003859 chip->battery_present = -EINVAL;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003860 chip->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08003861 chip->last_cc_uah = INT_MIN;
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08003862 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
3863 chip->prev_last_good_ocv_raw = OCV_RAW_UNINITIALIZED;
Xiaozhe Shie118c692012-09-24 15:17:43 -07003864 chip->first_time_calc_soc = 1;
3865 chip->first_time_calc_uuc = 1;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003866}
3867
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003868#define SPMI_FIND_IRQ(chip, irq_name) \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003869do { \
3870 chip->irq_name##_irq.irq = spmi_get_irq_byname(chip->spmi, \
3871 resource, #irq_name); \
3872 if (chip->irq_name##_irq.irq < 0) { \
3873 pr_err("Unable to get " #irq_name " irq\n"); \
3874 return -ENXIO; \
3875 } \
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003876} while (0)
3877
3878static int bms_find_irqs(struct qpnp_bms_chip *chip,
3879 struct spmi_resource *resource)
3880{
3881 SPMI_FIND_IRQ(chip, sw_cc_thr);
3882 SPMI_FIND_IRQ(chip, ocv_thr);
3883 return 0;
3884}
3885
3886#define SPMI_REQUEST_IRQ(chip, rc, irq_name) \
3887do { \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003888 rc = devm_request_irq(chip->dev, chip->irq_name##_irq.irq, \
3889 bms_##irq_name##_irq_handler, \
3890 IRQF_TRIGGER_RISING, #irq_name, chip); \
3891 if (rc < 0) { \
3892 pr_err("Unable to request " #irq_name " irq: %d\n", rc);\
3893 return -ENXIO; \
3894 } \
Xiaozhe Shif511a6e2014-02-20 14:37:18 -08003895 chip->irq_name##_irq.ready = true; \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003896} while (0)
3897
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003898static int bms_request_irqs(struct qpnp_bms_chip *chip)
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003899{
3900 int rc;
3901
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003902 SPMI_REQUEST_IRQ(chip, rc, sw_cc_thr);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003903 enable_irq_wake(chip->sw_cc_thr_irq.irq);
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003904 SPMI_REQUEST_IRQ(chip, rc, ocv_thr);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003905 enable_irq_wake(chip->ocv_thr_irq.irq);
3906 return 0;
3907}
3908
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003909#define REG_OFFSET_PERP_TYPE 0x04
3910#define REG_OFFSET_PERP_SUBTYPE 0x05
3911#define BMS_BMS_TYPE 0xD
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003912#define BMS_BMS1_SUBTYPE 0x1
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003913#define BMS_IADC_TYPE 0x8
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003914#define BMS_IADC1_SUBTYPE 0x3
3915#define BMS_IADC2_SUBTYPE 0x5
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003916
3917static int register_spmi(struct qpnp_bms_chip *chip, struct spmi_device *spmi)
3918{
3919 struct spmi_resource *spmi_resource;
3920 struct resource *resource;
3921 int rc;
3922 u8 type, subtype;
3923
3924 chip->dev = &(spmi->dev);
3925 chip->spmi = spmi;
3926
3927 spmi_for_each_container_dev(spmi_resource, spmi) {
3928 if (!spmi_resource) {
3929 pr_err("qpnp_bms: spmi resource absent\n");
3930 return -ENXIO;
3931 }
3932
3933 resource = spmi_get_resource(spmi, spmi_resource,
3934 IORESOURCE_MEM, 0);
3935 if (!(resource && resource->start)) {
3936 pr_err("node %s IO resource absent!\n",
3937 spmi->dev.of_node->full_name);
3938 return -ENXIO;
3939 }
3940
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07003941 pr_debug("Node name = %s\n", spmi_resource->of_node->name);
3942
3943 if (strcmp("qcom,batt-pres-status",
3944 spmi_resource->of_node->name) == 0) {
3945 chip->batt_pres_addr = resource->start;
3946 continue;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003947 } else if (strcmp("qcom,soc-storage-reg",
3948 spmi_resource->of_node->name) == 0) {
3949 chip->soc_storage_addr = resource->start;
3950 continue;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07003951 }
3952
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003953 rc = qpnp_read_wrapper(chip, &type,
3954 resource->start + REG_OFFSET_PERP_TYPE, 1);
3955 if (rc) {
3956 pr_err("Peripheral type read failed rc=%d\n", rc);
3957 return rc;
3958 }
3959 rc = qpnp_read_wrapper(chip, &subtype,
3960 resource->start + REG_OFFSET_PERP_SUBTYPE, 1);
3961 if (rc) {
3962 pr_err("Peripheral subtype read failed rc=%d\n", rc);
3963 return rc;
3964 }
3965
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003966 if (type == BMS_BMS_TYPE && subtype == BMS_BMS1_SUBTYPE) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003967 chip->base = resource->start;
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003968 rc = bms_find_irqs(chip, spmi_resource);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003969 if (rc) {
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003970 pr_err("Could not find irqs\n");
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003971 return rc;
3972 }
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003973 } else if (type == BMS_IADC_TYPE
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003974 && (subtype == BMS_IADC1_SUBTYPE
3975 || subtype == BMS_IADC2_SUBTYPE)) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003976 chip->iadc_base = resource->start;
3977 } else {
3978 pr_err("Invalid peripheral start=0x%x type=0x%x, subtype=0x%x\n",
3979 resource->start, type, subtype);
3980 }
3981 }
3982
3983 if (chip->base == 0) {
3984 dev_err(&spmi->dev, "BMS peripheral was not registered\n");
3985 return -EINVAL;
3986 }
3987 if (chip->iadc_base == 0) {
3988 dev_err(&spmi->dev, "BMS_IADC peripheral was not registered\n");
3989 return -EINVAL;
3990 }
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003991 if (chip->soc_storage_addr == 0) {
3992 /* default to dvdd backed BMS data reg0 */
3993 chip->soc_storage_addr = chip->base + SOC_STORAGE_REG;
3994 }
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003995
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003996 pr_debug("bms-base = 0x%04x, iadc-base = 0x%04x, bat-pres-reg = 0x%04x, soc-storage-reg = 0x%04x\n",
3997 chip->base, chip->iadc_base,
3998 chip->batt_pres_addr, chip->soc_storage_addr);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003999 return 0;
4000}
4001
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004002#define ADC_CH_SEL_MASK 0x7
4003#define ADC_INT_RSNSN_CTL_MASK 0x3
4004#define ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE 0x2
4005#define FAST_AVG_EN_MASK 0x80
4006#define FAST_AVG_EN_VALUE_EXT_RSENSE 0x80
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004007static int read_iadc_channel_select(struct qpnp_bms_chip *chip)
4008{
4009 u8 iadc_channel_select;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004010 int32_t rds_rsense_nohm;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004011 int rc;
4012
4013 rc = qpnp_read_wrapper(chip, &iadc_channel_select,
4014 chip->iadc_base + IADC1_BMS_ADC_CH_SEL_CTL, 1);
4015 if (rc) {
4016 pr_err("Error reading bms_iadc channel register %d\n", rc);
4017 return rc;
4018 }
4019
4020 iadc_channel_select &= ADC_CH_SEL_MASK;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004021 if (iadc_channel_select != EXTERNAL_RSENSE
4022 && iadc_channel_select != INTERNAL_RSENSE) {
4023 pr_err("IADC1_BMS_IADC configured incorrectly. Selected channel = %d\n",
4024 iadc_channel_select);
4025 return -EINVAL;
4026 }
4027
4028 if (chip->use_external_rsense) {
4029 pr_debug("External rsense selected\n");
4030 if (iadc_channel_select == INTERNAL_RSENSE) {
4031 pr_debug("Internal rsense detected; Changing rsense to external\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004032 rc = qpnp_masked_write_iadc(chip,
4033 IADC1_BMS_ADC_CH_SEL_CTL,
4034 ADC_CH_SEL_MASK,
4035 EXTERNAL_RSENSE);
4036 if (rc) {
4037 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
4038 IADC1_BMS_ADC_CH_SEL_CTL,
4039 EXTERNAL_RSENSE, rc);
4040 return rc;
4041 }
Xiaozhe Shif3da8622013-06-10 14:50:56 -07004042 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07004043 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07004044 chip->software_shdw_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004045 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004046 } else {
4047 pr_debug("Internal rsense selected\n");
4048 if (iadc_channel_select == EXTERNAL_RSENSE) {
4049 pr_debug("External rsense detected; Changing rsense to internal\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004050 rc = qpnp_masked_write_iadc(chip,
4051 IADC1_BMS_ADC_CH_SEL_CTL,
4052 ADC_CH_SEL_MASK,
4053 INTERNAL_RSENSE);
4054 if (rc) {
4055 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
4056 IADC1_BMS_ADC_CH_SEL_CTL,
4057 INTERNAL_RSENSE, rc);
4058 return rc;
4059 }
Xiaozhe Shif3da8622013-06-10 14:50:56 -07004060 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
4061 chip->software_shdw_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004062 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004063
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07004064 rc = qpnp_iadc_get_rsense(chip->iadc_dev, &rds_rsense_nohm);
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004065 if (rc) {
4066 pr_err("Unable to read RDS resistance value from IADC; rc = %d\n",
4067 rc);
4068 return rc;
4069 }
Xiaozhe Shid0a79542012-11-06 10:00:38 -08004070 chip->r_sense_uohm = rds_rsense_nohm/1000;
4071 pr_debug("rds_rsense = %d nOhm, saved as %d uOhm\n",
4072 rds_rsense_nohm, chip->r_sense_uohm);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004073 }
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004074 /* prevent shorting of leads by IADC_BMS when external Rsense is used */
4075 if (chip->use_external_rsense) {
4076 if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX) {
4077 rc = qpnp_masked_write_iadc(chip,
4078 IADC1_BMS_ADC_INT_RSNSN_CTL,
4079 ADC_INT_RSNSN_CTL_MASK,
4080 ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE);
4081 if (rc) {
4082 pr_err("Unable to set batfet config %x to %x: %d\n",
4083 IADC1_BMS_ADC_INT_RSNSN_CTL,
4084 ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE, rc);
4085 return rc;
4086 }
4087 } else {
4088 /* In older PMICS use FAST_AVG_EN register bit 7 */
4089 rc = qpnp_masked_write_iadc(chip,
4090 IADC1_BMS_FAST_AVG_EN,
4091 FAST_AVG_EN_MASK,
4092 FAST_AVG_EN_VALUE_EXT_RSENSE);
4093 if (rc) {
4094 pr_err("Unable to set batfet config %x to %x: %d\n",
4095 IADC1_BMS_FAST_AVG_EN,
4096 FAST_AVG_EN_VALUE_EXT_RSENSE, rc);
4097 return rc;
4098 }
4099 }
4100 }
4101
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004102 return 0;
4103}
4104
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004105static int refresh_die_temp_monitor(struct qpnp_bms_chip *chip)
4106{
4107 struct qpnp_vadc_result result;
4108 int rc;
4109
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07004110 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004111
4112 pr_debug("low = %lld, high = %lld\n",
4113 result.physical - chip->temperature_margin,
4114 result.physical + chip->temperature_margin);
4115 chip->die_temp_monitor_params.high_temp = result.physical
4116 + chip->temperature_margin;
4117 chip->die_temp_monitor_params.low_temp = result.physical
4118 - chip->temperature_margin;
4119 chip->die_temp_monitor_params.state_request =
4120 ADC_TM_HIGH_LOW_THR_ENABLE;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004121 return qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
4122 &chip->die_temp_monitor_params);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004123}
4124
4125static void btm_notify_die_temp(enum qpnp_tm_state state, void *ctx)
4126{
4127 struct qpnp_bms_chip *chip = ctx;
4128 struct qpnp_vadc_result result;
4129 int rc;
4130
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07004131 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004132
4133 if (state == ADC_TM_LOW_STATE)
4134 pr_debug("low state triggered\n");
4135 else if (state == ADC_TM_HIGH_STATE)
4136 pr_debug("high state triggered\n");
4137 pr_debug("die temp = %lld, raw = 0x%x\n",
4138 result.physical, result.adc_code);
4139 schedule_work(&chip->recalc_work);
4140 refresh_die_temp_monitor(chip);
4141}
4142
4143static int setup_die_temp_monitoring(struct qpnp_bms_chip *chip)
4144{
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004145 int rc;
4146
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004147 chip->die_temp_monitor_params.channel = DIE_TEMP;
4148 chip->die_temp_monitor_params.btm_ctx = (void *)chip;
4149 chip->die_temp_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
4150 chip->die_temp_monitor_params.threshold_notification =
4151 &btm_notify_die_temp;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004152 rc = refresh_die_temp_monitor(chip);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004153 if (rc) {
4154 pr_err("tm setup failed: %d\n", rc);
4155 return rc;
4156 }
4157 pr_debug("setup complete\n");
4158 return 0;
4159}
4160
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004161static int __devinit qpnp_bms_probe(struct spmi_device *spmi)
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004162{
4163 struct qpnp_bms_chip *chip;
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004164 bool warm_reset;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004165 int rc, vbatt;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004166
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07004167 chip = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_bms_chip),
4168 GFP_KERNEL);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004169
4170 if (chip == NULL) {
4171 pr_err("kzalloc() failed.\n");
4172 return -ENOMEM;
4173 }
4174
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004175 rc = bms_get_adc(chip, spmi);
4176 if (rc < 0)
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004177 goto error_read;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004178
Xiaozhe Shi3eaf0b62013-07-11 09:48:08 -07004179 mutex_init(&chip->bms_output_lock);
4180 mutex_init(&chip->last_ocv_uv_mutex);
4181 mutex_init(&chip->vbat_monitor_mutex);
4182 mutex_init(&chip->soc_invalidation_mutex);
4183 mutex_init(&chip->last_soc_mutex);
Xiaozhe Shibda84992013-09-05 10:39:11 -07004184 mutex_init(&chip->status_lock);
Xiaozhe Shi27375822013-08-22 11:40:15 -07004185 init_waitqueue_head(&chip->bms_wait_queue);
Xiaozhe Shi3eaf0b62013-07-11 09:48:08 -07004186
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004187 warm_reset = qpnp_pon_is_warm_reset();
4188 rc = warm_reset;
4189 if (rc < 0)
4190 goto error_read;
4191
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004192 rc = register_spmi(chip, spmi);
4193 if (rc) {
4194 pr_err("error registering spmi resource %d\n", rc);
4195 goto error_resource;
4196 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004197
4198 rc = qpnp_read_wrapper(chip, &chip->revision1,
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004199 chip->base + REVISION1, 1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004200 if (rc) {
4201 pr_err("error reading version register %d\n", rc);
4202 goto error_read;
4203 }
4204
4205 rc = qpnp_read_wrapper(chip, &chip->revision2,
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004206 chip->base + REVISION2, 1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004207 if (rc) {
4208 pr_err("Error reading version register %d\n", rc);
4209 goto error_read;
4210 }
Xiaozhe Shia045a562012-11-28 16:55:39 -08004211 pr_debug("BMS version: %hhu.%hhu\n", chip->revision2, chip->revision1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004212
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004213 rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision2,
4214 chip->iadc_base + REVISION2, 1);
4215 if (rc) {
4216 pr_err("Error reading version register %d\n", rc);
4217 goto error_read;
4218 }
4219
4220 rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision1,
4221 chip->iadc_base + REVISION1, 1);
4222 if (rc) {
4223 pr_err("Error reading version register %d\n", rc);
4224 goto error_read;
4225 }
4226 pr_debug("IADC_BMS version: %hhu.%hhu\n",
4227 chip->iadc_bms_revision2, chip->iadc_bms_revision1);
4228
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004229 rc = bms_read_properties(chip);
4230 if (rc) {
4231 pr_err("Unable to read all bms properties, rc = %d\n", rc);
4232 goto error_read;
4233 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004234
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004235 rc = read_iadc_channel_select(chip);
4236 if (rc) {
4237 pr_err("Unable to get iadc selected channel = %d\n", rc);
4238 goto error_read;
4239 }
4240
Xiaozhe Shibdf14742012-12-05 12:41:48 -08004241 if (chip->use_ocv_thresholds) {
4242 rc = set_ocv_voltage_thresholds(chip,
4243 chip->ocv_low_threshold_uv,
4244 chip->ocv_high_threshold_uv);
4245 if (rc) {
4246 pr_err("Could not set ocv voltage thresholds: %d\n",
4247 rc);
4248 goto error_read;
4249 }
4250 }
4251
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004252 rc = set_battery_data(chip);
4253 if (rc) {
4254 pr_err("Bad battery data %d\n", rc);
4255 goto error_read;
4256 }
4257
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004258 bms_initialize_constants(chip);
4259
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004260 wakeup_source_init(&chip->soc_wake_source.source, "qpnp_soc_wake");
Xiaozhe Shi4be85782013-02-22 17:33:40 -08004261 wake_lock_init(&chip->low_voltage_wake_lock, WAKE_LOCK_SUSPEND,
4262 "qpnp_low_voltage_lock");
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004263 wake_lock_init(&chip->cv_wake_lock, WAKE_LOCK_SUSPEND,
4264 "qpnp_cv_lock");
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004265 INIT_DELAYED_WORK(&chip->calculate_soc_delayed_work,
4266 calculate_soc_work);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08004267 INIT_WORK(&chip->recalc_work, recalculate_work);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07004268 INIT_WORK(&chip->batfet_open_work, batfet_open_work);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004269
Xiaozhe Shif9f99242013-08-29 12:27:50 -07004270 dev_set_drvdata(&spmi->dev, chip);
4271 device_init_wakeup(&spmi->dev, 1);
4272
4273 load_shutdown_data(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004274
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05304275 if (chip->enable_fcc_learning) {
Anirudh Ghayale0c02932013-07-08 16:26:35 +05304276 if (chip->battery_removed) {
4277 rc = discard_backup_fcc_data(chip);
4278 if (rc)
4279 pr_err("Could not discard backed-up FCC data\n");
4280 } else {
4281 rc = read_chgcycle_data_from_backup(chip);
4282 if (rc)
4283 pr_err("Unable to restore charge-cycle data\n");
4284
4285 rc = read_fcc_data_from_backup(chip);
4286 if (rc)
4287 pr_err("Unable to restore FCC-learning data\n");
4288 else
4289 attempt_learning_new_fcc(chip);
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05304290 }
4291 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05304292
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004293 rc = setup_vbat_monitoring(chip);
4294 if (rc < 0) {
4295 pr_err("failed to set up voltage notifications: %d\n", rc);
4296 goto error_setup;
Xiaozhe Shid5d21412013-02-06 17:14:41 -08004297 }
4298
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004299 rc = setup_die_temp_monitoring(chip);
4300 if (rc < 0) {
4301 pr_err("failed to set up die temp notifications: %d\n", rc);
4302 goto error_setup;
4303 }
4304
Xu Kai870f8e82014-01-16 19:21:01 +08004305 rc = bms_request_irqs(chip);
4306 if (rc) {
4307 pr_err("error requesting bms irqs, rc = %d\n", rc);
4308 goto error_setup;
4309 }
4310
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07004311 battery_insertion_check(chip);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07004312 batfet_status_check(chip);
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07004313 battery_status_check(chip);
4314
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004315 calculate_soc_work(&(chip->calculate_soc_delayed_work.work));
4316
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004317 /* setup & register the battery power supply */
4318 chip->bms_psy.name = "bms";
4319 chip->bms_psy.type = POWER_SUPPLY_TYPE_BMS;
4320 chip->bms_psy.properties = msm_bms_power_props;
4321 chip->bms_psy.num_properties = ARRAY_SIZE(msm_bms_power_props);
4322 chip->bms_psy.get_property = qpnp_bms_power_get_property;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004323 chip->bms_psy.external_power_changed =
4324 qpnp_bms_external_power_changed;
4325 chip->bms_psy.supplied_to = qpnp_bms_supplicants;
4326 chip->bms_psy.num_supplicants = ARRAY_SIZE(qpnp_bms_supplicants);
4327
4328 rc = power_supply_register(chip->dev, &chip->bms_psy);
4329
4330 if (rc < 0) {
4331 pr_err("power_supply_register bms failed rc = %d\n", rc);
4332 goto unregister_dc;
4333 }
4334
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07004335 chip->bms_psy_registered = true;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004336 vbatt = 0;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07004337 rc = get_battery_voltage(chip, &vbatt);
Xiaozhe Shi36458962013-02-06 16:19:57 -08004338 if (rc) {
4339 pr_err("error reading vbat_sns adc channel = %d, rc = %d\n",
4340 VBAT_SNS, rc);
4341 goto unregister_dc;
4342 }
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004343
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004344 pr_info("probe success: soc =%d vbatt = %d ocv = %d r_sense_uohm = %u warm_reset = %d\n",
4345 get_prop_bms_capacity(chip), vbatt, chip->last_ocv_uv,
4346 chip->r_sense_uohm, warm_reset);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004347 return 0;
4348
4349unregister_dc:
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07004350 chip->bms_psy_registered = false;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004351 power_supply_unregister(&chip->bms_psy);
4352error_setup:
4353 dev_set_drvdata(&spmi->dev, NULL);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004354 wakeup_source_trash(&chip->soc_wake_source.source);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08004355 wake_lock_destroy(&chip->low_voltage_wake_lock);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004356 wake_lock_destroy(&chip->cv_wake_lock);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004357error_resource:
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004358error_read:
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004359 return rc;
4360}
4361
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07004362static int qpnp_bms_remove(struct spmi_device *spmi)
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004363{
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004364 dev_set_drvdata(&spmi->dev, NULL);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004365 return 0;
4366}
4367
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004368static int bms_suspend(struct device *dev)
4369{
4370 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
4371
4372 cancel_delayed_work_sync(&chip->calculate_soc_delayed_work);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07004373 chip->was_charging_at_sleep = is_battery_charging(chip);
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004374 return 0;
4375}
4376
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004377static int bms_resume(struct device *dev)
4378{
4379 int rc;
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004380 int soc_calc_period;
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004381 int time_until_next_recalc = 0;
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004382 unsigned long time_since_last_recalc;
4383 unsigned long tm_now_sec;
4384 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
4385
4386 rc = get_current_time(&tm_now_sec);
4387 if (rc) {
4388 pr_err("Could not read current time: %d\n", rc);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004389 } else {
Xiaozhe Shicb487b12013-10-14 17:42:07 -07004390 soc_calc_period = get_calculation_delay_ms(chip);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004391 time_since_last_recalc = tm_now_sec - chip->last_recalc_time;
4392 pr_debug("Time since last recalc: %lu\n",
4393 time_since_last_recalc);
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004394 time_until_next_recalc = max(0, soc_calc_period
4395 - (int)(time_since_last_recalc * 1000));
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004396 }
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004397
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004398 if (time_until_next_recalc == 0)
4399 bms_stay_awake(&chip->soc_wake_source);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004400 schedule_delayed_work(&chip->calculate_soc_delayed_work,
4401 round_jiffies_relative(msecs_to_jiffies
4402 (time_until_next_recalc)));
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004403 return 0;
4404}
4405
4406static const struct dev_pm_ops qpnp_bms_pm_ops = {
4407 .resume = bms_resume,
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004408 .suspend = bms_suspend,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004409};
4410
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004411static struct spmi_driver qpnp_bms_driver = {
4412 .probe = qpnp_bms_probe,
4413 .remove = __devexit_p(qpnp_bms_remove),
4414 .driver = {
4415 .name = QPNP_BMS_DEV_NAME,
4416 .owner = THIS_MODULE,
4417 .of_match_table = qpnp_bms_match_table,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004418 .pm = &qpnp_bms_pm_ops,
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004419 },
4420};
4421
4422static int __init qpnp_bms_init(void)
4423{
4424 pr_info("QPNP BMS INIT\n");
4425 return spmi_driver_register(&qpnp_bms_driver);
4426}
4427
4428static void __exit qpnp_bms_exit(void)
4429{
4430 pr_info("QPNP BMS EXIT\n");
4431 return spmi_driver_unregister(&qpnp_bms_driver);
4432}
4433
4434module_init(qpnp_bms_init);
4435module_exit(qpnp_bms_exit);
4436
4437MODULE_DESCRIPTION("QPNP BMS Driver");
4438MODULE_LICENSE("GPL v2");
4439MODULE_ALIAS("platform:" QPNP_BMS_DEV_NAME);