blob: c43a77753410a6e6cebc1359b5255b86aecd1dda [file] [log] [blame]
Xiaozhe Shi28f5dd52013-01-04 12:19:58 -08001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
Xiaozhe Shi73a65692012-09-18 17:51:57 -070013#define pr_fmt(fmt) "BMS: %s: " fmt, __func__
Xiaozhe Shib19f7032012-08-16 12:14:16 -070014
15#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/init.h>
18#include <linux/slab.h>
19#include <linux/err.h>
20#include <linux/of.h>
21#include <linux/of_device.h>
22#include <linux/power_supply.h>
23#include <linux/spmi.h>
Xiaozhe Shie118c692012-09-24 15:17:43 -070024#include <linux/rtc.h>
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -070025#include <linux/delay.h>
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070026#include <linux/qpnp/qpnp-adc.h>
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -070027#include <linux/qpnp/power-on.h>
Xiaozhe Shiaf203c22013-06-19 12:01:38 -070028#include <linux/of_batterydata.h>
Xiaozhe Shib19f7032012-08-16 12:14:16 -070029
Xiaozhe Shib19f7032012-08-16 12:14:16 -070030/* BMS Register Offsets */
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -070031#define REVISION1 0x0
32#define REVISION2 0x1
Xiaozhe Shib19f7032012-08-16 12:14:16 -070033#define BMS1_STATUS1 0x8
34#define BMS1_MODE_CTL 0X40
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070035/* Coulomb counter clear registers */
Xiaozhe Shib19f7032012-08-16 12:14:16 -070036#define BMS1_CC_DATA_CTL 0x42
Xiaozhe Shia045a562012-11-28 16:55:39 -080037#define BMS1_CC_CLEAR_CTL 0x43
Xiaozhe Shi20640b52013-01-03 11:49:30 -080038/* BMS Tolerances */
39#define BMS1_TOL_CTL 0X44
Xiaozhe Shib19f7032012-08-16 12:14:16 -070040/* OCV limit registers */
41#define BMS1_OCV_USE_LOW_LIMIT_THR0 0x48
42#define BMS1_OCV_USE_LOW_LIMIT_THR1 0x49
43#define BMS1_OCV_USE_HIGH_LIMIT_THR0 0x4A
44#define BMS1_OCV_USE_HIGH_LIMIT_THR1 0x4B
45#define BMS1_OCV_USE_LIMIT_CTL 0x4C
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -070046/* Delay control */
47#define BMS1_S1_DELAY_CTL 0x5A
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -080048/* OCV interrupt threshold */
49#define BMS1_OCV_THR0 0x50
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -070050#define BMS1_S2_SAMP_AVG_CTL 0x61
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -080051/* SW CC interrupt threshold */
52#define BMS1_SW_CC_THR0 0xA0
Xiaozhe Shib19f7032012-08-16 12:14:16 -070053/* OCV for r registers */
54#define BMS1_OCV_FOR_R_DATA0 0x80
Xiaozhe Shib19f7032012-08-16 12:14:16 -070055#define BMS1_VSENSE_FOR_R_DATA0 0x82
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070056/* Coulomb counter data */
Xiaozhe Shib19f7032012-08-16 12:14:16 -070057#define BMS1_CC_DATA0 0x8A
Xiaozhe Shif3da8622013-06-10 14:50:56 -070058/* Shadow Coulomb counter data */
59#define BMS1_SW_CC_DATA0 0xA8
Xiaozhe Shib19f7032012-08-16 12:14:16 -070060/* OCV for soc data */
61#define BMS1_OCV_FOR_SOC_DATA0 0x90
Xiaozhe Shib19f7032012-08-16 12:14:16 -070062#define BMS1_VSENSE_PON_DATA0 0x94
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070063#define BMS1_VSENSE_AVG_DATA0 0x98
Xiaozhe Shib19f7032012-08-16 12:14:16 -070064#define BMS1_VBAT_AVG_DATA0 0x9E
Xiaozhe Shib19f7032012-08-16 12:14:16 -070065/* Extra bms registers */
Xiaozhe Shi57058942013-03-27 16:54:54 -070066#define SOC_STORAGE_REG 0xB0
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070067#define IAVG_STORAGE_REG 0xB1
Anirudh Ghayale0c02932013-07-08 16:26:35 +053068#define BMS_FCC_COUNT 0xB2
69#define BMS_FCC_BASE_REG 0xB3 /* FCC updates - 0xB3 to 0xB7 */
70#define BMS_CHGCYL_BASE_REG 0xB8 /* FCC chgcyl - 0xB8 to 0xBC */
71#define CHARGE_INCREASE_STORAGE 0xBD
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053072#define CHARGE_CYCLE_STORAGE_LSB 0xBE /* LSB=0xBE, MSB=0xBF */
73
Xiaozhe Shic40b3972012-11-30 14:11:16 -080074/* IADC Channel Select */
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -070075#define IADC1_BMS_REVISION2 0x01
Xiaozhe Shic40b3972012-11-30 14:11:16 -080076#define IADC1_BMS_ADC_CH_SEL_CTL 0x48
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -070077#define IADC1_BMS_ADC_INT_RSNSN_CTL 0x49
78#define IADC1_BMS_FAST_AVG_EN 0x5B
Xiaozhe Shib19f7032012-08-16 12:14:16 -070079
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070080/* Configuration for saving of shutdown soc/iavg */
81#define IGNORE_SOC_TEMP_DECIDEG 50
82#define IAVG_STEP_SIZE_MA 50
83#define IAVG_START 600
Xiaozhe Shif5f966d2013-02-19 14:23:11 -080084#define IAVG_INVALID 0xFF
Xiaozhe Shic7cbd052013-03-29 12:03:11 -070085#define SOC_INVALID 0xFF
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070086
Xiaozhe Shie118c692012-09-24 15:17:43 -070087#define IAVG_SAMPLES 16
88
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053089/* FCC learning constants */
Anirudh Ghayale0c02932013-07-08 16:26:35 +053090#define MAX_FCC_CYCLES 5
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053091#define DELTA_FCC_PERCENT 5
92#define VALID_FCC_CHGCYL_RANGE 50
Anirudh Ghayale0c02932013-07-08 16:26:35 +053093#define CHGCYL_RESOLUTION 20
94#define FCC_DEFAULT_TEMP 250
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053095
Xiaozhe Shib19f7032012-08-16 12:14:16 -070096#define QPNP_BMS_DEV_NAME "qcom,qpnp-bms"
97
Xiaozhe Shif3da8622013-06-10 14:50:56 -070098enum {
99 SHDW_CC,
100 CC
101};
102
103enum {
104 NORESET,
105 RESET
106};
107
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700108struct soc_params {
109 int fcc_uah;
110 int cc_uah;
Xiaozhe Shi904f1f72012-12-04 12:47:21 -0800111 int rbatt_mohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700112 int iavg_ua;
113 int uuc_uah;
114 int ocv_charge_uah;
Xiaozhe Shif36d2862013-01-04 10:17:35 -0800115 int delta_time_s;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700116};
117
118struct raw_soc_params {
119 uint16_t last_good_ocv_raw;
120 int64_t cc;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700121 int64_t shdw_cc;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700122 int last_good_ocv_uv;
123};
124
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530125struct fcc_sample {
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530126 int fcc_new;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530127 int chargecycles;
128};
129
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800130struct bms_irq {
131 unsigned int irq;
132 unsigned long disabled;
133};
134
135struct bms_wakeup_source {
136 struct wakeup_source source;
137 unsigned long disabled;
138};
139
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700140struct qpnp_bms_chip {
141 struct device *dev;
142 struct power_supply bms_psy;
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -0700143 bool bms_psy_registered;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -0700144 struct power_supply *batt_psy;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700145 struct spmi_device *spmi;
146 u16 base;
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800147 u16 iadc_base;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700148
149 u8 revision1;
150 u8 revision2;
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -0700151
152 u8 iadc_bms_revision1;
153 u8 iadc_bms_revision2;
154
Xiaozhe Shid5d21412013-02-06 17:14:41 -0800155 int battery_present;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700156 int battery_status;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700157 bool batfet_closed;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800158 bool new_battery;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700159 bool done_charging;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800160 bool last_soc_invalid;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700161 /* platform data */
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -0800162 int r_sense_uohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700163 unsigned int v_cutoff_uv;
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -0800164 int max_voltage_uv;
165 int r_conn_mohm;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700166 int shutdown_soc_valid_limit;
167 int adjust_soc_low_threshold;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700168 int chg_term_ua;
Xiaozhe Shi73a65692012-09-18 17:51:57 -0700169 enum battery_type batt_type;
Xiaozhe Shi976618f2013-04-30 10:49:30 -0700170 unsigned int fcc_mah;
Xiaozhe Shi73a65692012-09-18 17:51:57 -0700171 struct single_row_lut *fcc_temp_lut;
172 struct single_row_lut *fcc_sf_lut;
173 struct pc_temp_ocv_lut *pc_temp_ocv_lut;
174 struct sf_lut *pc_sf_lut;
175 struct sf_lut *rbatt_sf_lut;
176 int default_rbatt_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -0700177 int rbatt_capacitive_mohm;
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -0700178 int rbatt_mohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700179
180 struct delayed_work calculate_soc_delayed_work;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800181 struct work_struct recalc_work;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700182 struct work_struct batfet_open_work;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700183
184 struct mutex bms_output_lock;
185 struct mutex last_ocv_uv_mutex;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700186 struct mutex vbat_monitor_mutex;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700187 struct mutex soc_invalidation_mutex;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700188 struct mutex last_soc_mutex;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700189
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800190 bool use_external_rsense;
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800191 bool use_ocv_thresholds;
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800192
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700193 bool ignore_shutdown_soc;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800194 bool shutdown_soc_invalid;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700195 int shutdown_soc;
196 int shutdown_iavg_ma;
197
Xiaozhe Shi4be85782013-02-22 17:33:40 -0800198 struct wake_lock low_voltage_wake_lock;
Xiaozhe Shi4be85782013-02-22 17:33:40 -0800199 int low_voltage_threshold;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700200 int low_soc_calc_threshold;
201 int low_soc_calculate_soc_ms;
202 int calculate_soc_ms;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800203 struct bms_wakeup_source soc_wake_source;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700204 struct wake_lock cv_wake_lock;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700205
Xiaozhe Shie118c692012-09-24 15:17:43 -0700206 uint16_t ocv_reading_at_100;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700207 uint16_t prev_last_good_ocv_raw;
208 int last_ocv_uv;
Xiaozhe Shicc48e992013-05-28 16:42:24 -0700209 int charging_adjusted_ocv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800210 int last_ocv_temp;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700211 int last_cc_uah;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700212 unsigned long last_soc_change_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700213 unsigned long tm_sec;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700214 unsigned long report_tm_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700215 bool first_time_calc_soc;
216 bool first_time_calc_uuc;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700217 int64_t software_cc_uah;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700218 int64_t software_shdw_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700219
220 int iavg_samples_ma[IAVG_SAMPLES];
221 int iavg_index;
222 int iavg_num_samples;
223 struct timespec t_soc_queried;
224 int last_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -0700225 int last_soc_est;
Xiaozhe Shicc137262013-03-10 06:21:41 -0700226 int last_soc_unbound;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700227 bool was_charging_at_sleep;
228 int charge_start_tm_sec;
229 int catch_up_time_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700230 struct single_row_lut *adjusted_fcc_temp_lut;
231
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700232 struct qpnp_adc_tm_btm_param vbat_monitor_params;
Xiaozhe Shi535494d2013-04-05 12:27:51 -0700233 struct qpnp_adc_tm_btm_param die_temp_monitor_params;
234 int temperature_margin;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700235 unsigned int vadc_v0625;
236 unsigned int vadc_v1250;
237
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700238 int prev_uuc_iavg_ma;
239 int prev_pc_unusable;
240 int ibat_at_cv_ua;
241 int soc_at_cv;
242 int prev_chg_soc;
243 int calculated_soc;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -0800244 int prev_voltage_based_soc;
245 bool use_voltage_soc;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700246 bool in_cv_range;
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800247
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800248 int prev_batt_terminal_uv;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -0700249 int high_ocv_correction_limit_uv;
250 int low_ocv_correction_limit_uv;
251 int flat_ocv_threshold_uv;
252 int hold_soc_est;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800253
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800254 int ocv_high_threshold_uv;
255 int ocv_low_threshold_uv;
Xiaozhe Shicdeee312012-12-18 15:10:18 -0800256 unsigned long last_recalc_time;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530257
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530258 struct fcc_sample *fcc_learning_samples;
259 u8 fcc_sample_count;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530260 int enable_fcc_learning;
261 int min_fcc_learning_soc;
262 int min_fcc_ocv_pc;
263 int min_fcc_learning_samples;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530264 int start_soc;
265 int end_soc;
266 int start_pc;
267 int start_cc_uah;
268 int start_real_soc;
269 int end_cc_uah;
270 uint16_t fcc_new_mah;
271 int fcc_new_batt_temp;
272 uint16_t charge_cycles;
273 u8 charge_increase;
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530274 int fcc_resolution;
275 bool battery_removed;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800276 struct bms_irq sw_cc_thr_irq;
277 struct bms_irq ocv_thr_irq;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700278 struct qpnp_vadc_chip *vadc_dev;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700279};
280
281static struct of_device_id qpnp_bms_match_table[] = {
282 { .compatible = QPNP_BMS_DEV_NAME },
283 {}
284};
285
286static char *qpnp_bms_supplicants[] = {
287 "battery"
288};
289
290static enum power_supply_property msm_bms_power_props[] = {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700291 POWER_SUPPLY_PROP_CAPACITY,
292 POWER_SUPPLY_PROP_CURRENT_NOW,
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -0700293 POWER_SUPPLY_PROP_RESISTANCE,
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -0700294 POWER_SUPPLY_PROP_CHARGE_COUNTER,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700295 POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700296 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +0530297 POWER_SUPPLY_PROP_CHARGE_FULL,
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +0530298 POWER_SUPPLY_PROP_CYCLE_COUNT,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700299};
300
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530301static int discard_backup_fcc_data(struct qpnp_bms_chip *chip);
302static void backup_charge_cycle(struct qpnp_bms_chip *chip);
303
Xiaozhe Shi20640b52013-01-03 11:49:30 -0800304static bool bms_reset;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -0800305
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700306static int qpnp_read_wrapper(struct qpnp_bms_chip *chip, u8 *val,
307 u16 base, int count)
308{
309 int rc;
310 struct spmi_device *spmi = chip->spmi;
311
312 rc = spmi_ext_register_readl(spmi->ctrl, spmi->sid, base, val, count);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700313 if (rc) {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700314 pr_err("SPMI read failed rc=%d\n", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700315 return rc;
316 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700317 return 0;
318}
319
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700320static int qpnp_write_wrapper(struct qpnp_bms_chip *chip, u8 *val,
321 u16 base, int count)
322{
323 int rc;
324 struct spmi_device *spmi = chip->spmi;
325
326 rc = spmi_ext_register_writel(spmi->ctrl, spmi->sid, base, val, count);
327 if (rc) {
328 pr_err("SPMI write failed rc=%d\n", rc);
329 return rc;
330 }
331 return 0;
332}
333
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800334static int qpnp_masked_write_base(struct qpnp_bms_chip *chip, u16 addr,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700335 u8 mask, u8 val)
336{
337 int rc;
338 u8 reg;
339
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800340 rc = qpnp_read_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700341 if (rc) {
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800342 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700343 return rc;
344 }
345 reg &= ~mask;
346 reg |= val & mask;
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800347 rc = qpnp_write_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700348 if (rc) {
349 pr_err("write failed addr = %03X, val = %02x, mask = %02x, reg = %02x, rc = %d\n",
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800350 addr, val, mask, reg, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700351 return rc;
352 }
353 return 0;
354}
355
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800356static int qpnp_masked_write_iadc(struct qpnp_bms_chip *chip, u16 addr,
357 u8 mask, u8 val)
358{
359 return qpnp_masked_write_base(chip, chip->iadc_base + addr, mask, val);
360}
361
362static int qpnp_masked_write(struct qpnp_bms_chip *chip, u16 addr,
363 u8 mask, u8 val)
364{
365 return qpnp_masked_write_base(chip, chip->base + addr, mask, val);
366}
367
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800368static void bms_stay_awake(struct bms_wakeup_source *source)
369{
370 if (__test_and_clear_bit(0, &source->disabled)) {
371 __pm_stay_awake(&source->source);
372 pr_debug("enabled source %s\n", source->source.name);
373 }
374}
375
376static void bms_relax(struct bms_wakeup_source *source)
377{
378 if (!__test_and_set_bit(0, &source->disabled)) {
379 __pm_relax(&source->source);
380 pr_debug("disabled source %s\n", source->source.name);
381 }
382}
383
384static void enable_bms_irq(struct bms_irq *irq)
385{
386 if (__test_and_clear_bit(0, &irq->disabled)) {
387 enable_irq(irq->irq);
388 pr_debug("enabled irq %d\n", irq->irq);
389 }
390}
391
392static void disable_bms_irq(struct bms_irq *irq)
393{
394 if (!__test_and_set_bit(0, &irq->disabled)) {
395 disable_irq(irq->irq);
396 pr_debug("disabled irq %d\n", irq->irq);
397 }
398}
399
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700400#define HOLD_OREG_DATA BIT(0)
401static int lock_output_data(struct qpnp_bms_chip *chip)
402{
403 int rc;
404
405 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL,
406 HOLD_OREG_DATA, HOLD_OREG_DATA);
407 if (rc) {
408 pr_err("couldnt lock bms output rc = %d\n", rc);
409 return rc;
410 }
411 return 0;
412}
413
414static int unlock_output_data(struct qpnp_bms_chip *chip)
415{
416 int rc;
417
418 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL, HOLD_OREG_DATA, 0);
419 if (rc) {
420 pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc);
421 return rc;
422 }
423 return 0;
424}
425
426#define V_PER_BIT_MUL_FACTOR 97656
427#define V_PER_BIT_DIV_FACTOR 1000
428#define VADC_INTRINSIC_OFFSET 0x6000
429
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800430static int vadc_reading_to_uv(int reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700431{
432 if (reading <= VADC_INTRINSIC_OFFSET)
433 return 0;
434
435 return (reading - VADC_INTRINSIC_OFFSET)
436 * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR;
437}
438
439#define VADC_CALIB_UV 625000
440#define VBATT_MUL_FACTOR 3
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800441static int adjust_vbatt_reading(struct qpnp_bms_chip *chip, int reading_uv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700442{
443 s64 numerator, denominator;
444
445 if (reading_uv == 0)
446 return 0;
447
448 /* don't adjust if not calibrated */
449 if (chip->vadc_v0625 == 0 || chip->vadc_v1250 == 0) {
450 pr_debug("No cal yet return %d\n",
451 VBATT_MUL_FACTOR * reading_uv);
452 return VBATT_MUL_FACTOR * reading_uv;
453 }
454
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700455 numerator = ((s64)reading_uv - chip->vadc_v0625) * VADC_CALIB_UV;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700456 denominator = (s64)chip->vadc_v1250 - chip->vadc_v0625;
457 if (denominator == 0)
458 return reading_uv * VBATT_MUL_FACTOR;
459 return (VADC_CALIB_UV + div_s64(numerator, denominator))
460 * VBATT_MUL_FACTOR;
461}
462
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800463static int convert_vbatt_uv_to_raw(struct qpnp_bms_chip *chip,
464 int unadjusted_vbatt)
465{
466 int scaled_vbatt = unadjusted_vbatt / VBATT_MUL_FACTOR;
467
468 if (scaled_vbatt <= 0)
469 return VADC_INTRINSIC_OFFSET;
470 return ((scaled_vbatt * V_PER_BIT_DIV_FACTOR) / V_PER_BIT_MUL_FACTOR)
471 + VADC_INTRINSIC_OFFSET;
472}
473
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700474static inline int convert_vbatt_raw_to_uv(struct qpnp_bms_chip *chip,
475 uint16_t reading)
476{
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700477 int64_t uv;
478 int rc;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700479
480 uv = vadc_reading_to_uv(reading);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700481 pr_debug("%u raw converted into %lld uv\n", reading, uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700482 uv = adjust_vbatt_reading(chip, uv);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700483 pr_debug("adjusted into %lld uv\n", uv);
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700484 rc = qpnp_vbat_sns_comp_result(chip->vadc_dev, &uv);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700485 if (rc)
486 pr_debug("could not compensate vbatt\n");
487 pr_debug("compensated into %lld uv\n", uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700488 return uv;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700489}
490
491#define CC_READING_RESOLUTION_N 542535
492#define CC_READING_RESOLUTION_D 100000
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -0700493static s64 cc_reading_to_uv(s64 reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700494{
495 return div_s64(reading * CC_READING_RESOLUTION_N,
496 CC_READING_RESOLUTION_D);
497}
498
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800499#define QPNP_ADC_GAIN_IDEAL 3291LL
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700500static s64 cc_adjust_for_gain(s64 uv, uint16_t gain)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700501{
502 s64 result_uv;
503
504 pr_debug("adjusting_uv = %lld\n", uv);
Xiaozhe Shi820a47a2012-11-27 13:23:27 -0800505 if (gain == 0) {
506 pr_debug("gain is %d, not adjusting\n", gain);
507 return uv;
508 }
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700509 pr_debug("adjusting by factor: %lld/%hu = %lld%%\n",
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800510 QPNP_ADC_GAIN_IDEAL, gain,
511 div_s64(QPNP_ADC_GAIN_IDEAL * 100LL, (s64)gain));
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700512
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800513 result_uv = div_s64(uv * QPNP_ADC_GAIN_IDEAL, (s64)gain);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700514 pr_debug("result_uv = %lld\n", result_uv);
515 return result_uv;
516}
517
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800518static s64 cc_reverse_adjust_for_gain(s64 uv)
519{
520 struct qpnp_iadc_calib calibration;
521 int gain;
522 s64 result_uv;
523
524 qpnp_iadc_get_gain_and_offset(&calibration);
525 gain = (int)calibration.gain_raw - (int)calibration.offset_raw;
526
527 pr_debug("reverse adjusting_uv = %lld\n", uv);
528 if (gain == 0) {
529 pr_debug("gain is %d, not adjusting\n", gain);
530 return uv;
531 }
532 pr_debug("adjusting by factor: %hu/%lld = %lld%%\n",
533 gain, QPNP_ADC_GAIN_IDEAL,
534 div64_s64((s64)gain * 100LL,
535 (s64)QPNP_ADC_GAIN_IDEAL));
536
537 result_uv = div64_s64(uv * (s64)gain, QPNP_ADC_GAIN_IDEAL);
538 pr_debug("result_uv = %lld\n", result_uv);
539 return result_uv;
540}
541
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700542static int convert_vsense_to_uv(struct qpnp_bms_chip *chip,
543 int16_t reading)
544{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700545 struct qpnp_iadc_calib calibration;
546
547 qpnp_iadc_get_gain_and_offset(&calibration);
548 return cc_adjust_for_gain(cc_reading_to_uv(reading),
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800549 calibration.gain_raw - calibration.offset_raw);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700550}
551
552static int read_vsense_avg(struct qpnp_bms_chip *chip, int *result_uv)
553{
554 int rc;
555 int16_t reading;
556
557 rc = qpnp_read_wrapper(chip, (u8 *)&reading,
558 chip->base + BMS1_VSENSE_AVG_DATA0, 2);
559
560 if (rc) {
561 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
562 return rc;
563 }
564
565 *result_uv = convert_vsense_to_uv(chip, reading);
566 return 0;
567}
568
569static int get_battery_current(struct qpnp_bms_chip *chip, int *result_ua)
570{
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700571 int rc, vsense_uv = 0;
572 int64_t temp_current;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700573
Xiaozhe Shid0a79542012-11-06 10:00:38 -0800574 if (chip->r_sense_uohm == 0) {
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700575 pr_err("r_sense is zero\n");
576 return -EINVAL;
577 }
578
579 mutex_lock(&chip->bms_output_lock);
580 lock_output_data(chip);
581 read_vsense_avg(chip, &vsense_uv);
582 unlock_output_data(chip);
583 mutex_unlock(&chip->bms_output_lock);
584
585 pr_debug("vsense_uv=%duV\n", vsense_uv);
586 /* cast for signed division */
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700587 temp_current = div_s64((vsense_uv * 1000000LL),
588 (int)chip->r_sense_uohm);
589
590 rc = qpnp_iadc_comp_result(&temp_current);
591 if (rc)
592 pr_debug("error compensation failed: %d\n", rc);
593
594 *result_ua = temp_current;
595 pr_debug("err compensated ibat=%duA\n", *result_ua);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700596 return 0;
597}
598
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700599static int get_battery_voltage(struct qpnp_bms_chip *chip, int *result_uv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700600{
601 int rc;
602 struct qpnp_vadc_result adc_result;
603
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700604 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &adc_result);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700605 if (rc) {
606 pr_err("error reading adc channel = %d, rc = %d\n",
607 VBAT_SNS, rc);
608 return rc;
609 }
610 pr_debug("mvolts phy = %lld meas = 0x%llx\n", adc_result.physical,
611 adc_result.measurement);
612 *result_uv = (int)adc_result.physical;
613 return 0;
614}
615
Xiaozhe Shie118c692012-09-24 15:17:43 -0700616#define CC_36_BIT_MASK 0xFFFFFFFFFLL
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800617static uint64_t convert_s64_to_s36(int64_t raw64)
618{
619 return (uint64_t) raw64 & CC_36_BIT_MASK;
620}
621
622#define SIGN_EXTEND_36_TO_64_MASK (-1LL ^ CC_36_BIT_MASK)
623static int64_t convert_s36_to_s64(uint64_t raw36)
624{
625 raw36 = raw36 & CC_36_BIT_MASK;
626 /* convert 36 bit signed value into 64 signed value */
627 return (raw36 >> 35) == 0LL ?
628 raw36 : (SIGN_EXTEND_36_TO_64_MASK | raw36);
629}
630
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700631static int read_cc_raw(struct qpnp_bms_chip *chip, int64_t *reading,
632 int cc_type)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700633{
634 int64_t raw_reading;
635 int rc;
636
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700637 if (cc_type == SHDW_CC)
638 rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
639 chip->base + BMS1_SW_CC_DATA0, 5);
640 else
641 rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
642 chip->base + BMS1_CC_DATA0, 5);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700643 if (rc) {
644 pr_err("Error reading cc: rc = %d\n", rc);
645 return -ENXIO;
646 }
647
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800648 *reading = convert_s36_to_s64(raw_reading);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700649
650 return 0;
651}
652
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700653static int calib_vadc(struct qpnp_bms_chip *chip)
654{
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700655 int rc, raw_0625, raw_1250;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700656 struct qpnp_vadc_result result;
657
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700658 rc = qpnp_vadc_read(chip->vadc_dev, REF_625MV, &result);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700659 if (rc) {
660 pr_debug("vadc read failed with rc = %d\n", rc);
661 return rc;
662 }
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700663 raw_0625 = result.adc_code;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700664
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700665 rc = qpnp_vadc_read(chip->vadc_dev, REF_125V, &result);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700666 if (rc) {
667 pr_debug("vadc read failed with rc = %d\n", rc);
668 return rc;
669 }
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700670 raw_1250 = result.adc_code;
671 chip->vadc_v0625 = vadc_reading_to_uv(raw_0625);
672 chip->vadc_v1250 = vadc_reading_to_uv(raw_1250);
673 pr_debug("vadc calib: 0625 = %d raw (%d uv), 1250 = %d raw (%d uv)\n",
674 raw_0625, chip->vadc_v0625,
675 raw_1250, chip->vadc_v1250);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700676 return 0;
677}
678
Xiaozhe Shie118c692012-09-24 15:17:43 -0700679static void convert_and_store_ocv(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800680 struct raw_soc_params *raw,
681 int batt_temp)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700682{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700683 int rc;
684
685 pr_debug("prev_last_good_ocv_raw = %d, last_good_ocv_raw = %d\n",
686 chip->prev_last_good_ocv_raw,
687 raw->last_good_ocv_raw);
688 rc = calib_vadc(chip);
689 if (rc)
690 pr_err("Vadc reference voltage read failed, rc = %d\n", rc);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700691 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
692 raw->last_good_ocv_uv = convert_vbatt_raw_to_uv(chip,
693 raw->last_good_ocv_raw);
694 chip->last_ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800695 chip->last_ocv_temp = batt_temp;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700696 chip->software_cc_uah = 0;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700697 pr_debug("last_good_ocv_uv = %d\n", raw->last_good_ocv_uv);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700698}
699
Xiaozhe Shia045a562012-11-28 16:55:39 -0800700#define CLEAR_CC BIT(7)
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700701#define CLEAR_SHDW_CC BIT(6)
Xiaozhe Shia045a562012-11-28 16:55:39 -0800702/**
703 * reset both cc and sw-cc.
704 * note: this should only be ever called from one thread
705 * or there may be a race condition where CC is never enabled
706 * again
707 */
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700708static void reset_cc(struct qpnp_bms_chip *chip, u8 flags)
Xiaozhe Shia045a562012-11-28 16:55:39 -0800709{
710 int rc;
711
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700712 pr_debug("resetting cc manually with flags %hhu\n", flags);
713 mutex_lock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800714 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700715 flags,
716 flags);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800717 if (rc)
718 pr_err("cc reset failed: %d\n", rc);
719
720 /* wait for 100us for cc to reset */
721 udelay(100);
722
723 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700724 flags, 0);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800725 if (rc)
726 pr_err("cc reenable failed: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700727 mutex_unlock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800728}
729
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700730static int get_battery_status(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800731{
732 union power_supply_propval ret = {0,};
733
734 if (chip->batt_psy == NULL)
735 chip->batt_psy = power_supply_get_by_name("battery");
736 if (chip->batt_psy) {
737 /* if battery has been registered, use the status property */
738 chip->batt_psy->get_property(chip->batt_psy,
739 POWER_SUPPLY_PROP_STATUS, &ret);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700740 return ret.intval;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800741 }
742
743 /* Default to false if the battery power supply is not registered. */
744 pr_debug("battery power supply is not registered\n");
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700745 return POWER_SUPPLY_STATUS_UNKNOWN;
746}
747
748static bool is_battery_charging(struct qpnp_bms_chip *chip)
749{
750 return get_battery_status(chip) == POWER_SUPPLY_STATUS_CHARGING;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800751}
752
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700753static bool is_battery_full(struct qpnp_bms_chip *chip)
754{
755 return get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL;
756}
757
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700758static bool is_battery_present(struct qpnp_bms_chip *chip)
759{
760 union power_supply_propval ret = {0,};
761
762 if (chip->batt_psy == NULL)
763 chip->batt_psy = power_supply_get_by_name("battery");
764 if (chip->batt_psy) {
765 /* if battery has been registered, use the status property */
766 chip->batt_psy->get_property(chip->batt_psy,
767 POWER_SUPPLY_PROP_PRESENT, &ret);
768 return ret.intval;
769 }
770
771 /* Default to false if the battery power supply is not registered. */
772 pr_debug("battery power supply is not registered\n");
773 return false;
774}
775
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700776static bool is_batfet_closed(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800777{
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700778 union power_supply_propval ret = {0,};
779
780 if (chip->batt_psy == NULL)
781 chip->batt_psy = power_supply_get_by_name("battery");
782 if (chip->batt_psy) {
783 /* if battery has been registered, use the online property */
784 chip->batt_psy->get_property(chip->batt_psy,
785 POWER_SUPPLY_PROP_ONLINE, &ret);
786 return !!ret.intval;
787 }
788
789 /* Default to true if the battery power supply is not registered. */
790 pr_debug("battery power supply is not registered\n");
791 return true;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800792}
793
794static int get_simultaneous_batt_v_and_i(struct qpnp_bms_chip *chip,
795 int *ibat_ua, int *vbat_uv)
796{
797 struct qpnp_iadc_result i_result;
798 struct qpnp_vadc_result v_result;
799 enum qpnp_iadc_channels iadc_channel;
800 int rc;
801
802 iadc_channel = chip->use_external_rsense ?
803 EXTERNAL_RSENSE : INTERNAL_RSENSE;
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700804 if (is_battery_full(chip)) {
805 rc = get_battery_current(chip, ibat_ua);
806 if (rc) {
807 pr_err("bms current read failed with rc: %d\n", rc);
808 return rc;
809 }
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700810 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &v_result);
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700811 if (rc) {
812 pr_err("vadc read failed with rc: %d\n", rc);
813 return rc;
814 }
815 *vbat_uv = (int)v_result.physical;
816 } else {
817 rc = qpnp_iadc_vadc_sync_read(iadc_channel, &i_result,
818 VBAT_SNS, &v_result);
819 if (rc) {
820 pr_err("adc sync read failed with rc: %d\n", rc);
821 return rc;
822 }
823 /*
824 * reverse the current read by the iadc, since the bms uses
825 * flipped battery current polarity.
826 */
827 *ibat_ua = -1 * (int)i_result.result_ua;
828 *vbat_uv = (int)v_result.physical;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800829 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800830
831 return 0;
832}
833
834static int estimate_ocv(struct qpnp_bms_chip *chip)
835{
836 int ibat_ua, vbat_uv, ocv_est_uv;
837 int rc;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -0700838 int rbatt_mohm = chip->default_rbatt_mohm + chip->r_conn_mohm
839 + chip->rbatt_capacitive_mohm;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800840
841 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
842 if (rc) {
843 pr_err("simultaneous failed rc = %d\n", rc);
844 return rc;
845 }
846
847 ocv_est_uv = vbat_uv + (ibat_ua * rbatt_mohm) / 1000;
848 pr_debug("estimated pon ocv = %d\n", ocv_est_uv);
849 return ocv_est_uv;
850}
851
852static void reset_for_new_battery(struct qpnp_bms_chip *chip, int batt_temp)
853{
854 chip->last_ocv_uv = estimate_ocv(chip);
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700855 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800856 chip->last_soc = -EINVAL;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700857 chip->last_soc_invalid = true;
858 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800859 chip->soc_at_cv = -EINVAL;
860 chip->shutdown_soc_invalid = true;
861 chip->shutdown_soc = 0;
862 chip->shutdown_iavg_ma = 0;
863 chip->prev_pc_unusable = -EINVAL;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700864 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700865 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700866 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800867 chip->last_cc_uah = INT_MIN;
868 chip->last_ocv_temp = batt_temp;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800869 chip->prev_batt_terminal_uv = 0;
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530870 if (chip->enable_fcc_learning) {
871 chip->adjusted_fcc_temp_lut = NULL;
872 chip->fcc_new_mah = -EINVAL;
873 /* reset the charge-cycle and charge-increase registers */
874 chip->charge_increase = 0;
875 chip->charge_cycles = 0;
876 backup_charge_cycle(chip);
877 /* discard all the FCC learnt data and reset the local table */
878 discard_backup_fcc_data(chip);
879 memset(chip->fcc_learning_samples, 0,
880 chip->min_fcc_learning_samples *
881 sizeof(struct fcc_sample));
882 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800883}
884
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -0800885#define OCV_RAW_UNINITIALIZED 0xFFFF
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -0700886#define MIN_OCV_UV 2000000
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700887static int read_soc_params_raw(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800888 struct raw_soc_params *raw,
889 int batt_temp)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700890{
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -0700891 bool warm_reset = false;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700892 int rc;
893
894 mutex_lock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800895
Xiaozhe Shie118c692012-09-24 15:17:43 -0700896 lock_output_data(chip);
897
898 rc = qpnp_read_wrapper(chip, (u8 *)&raw->last_good_ocv_raw,
899 chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
900 if (rc) {
901 pr_err("Error reading ocv: rc = %d\n", rc);
902 return -ENXIO;
903 }
904
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700905 rc = read_cc_raw(chip, &raw->cc, CC);
906 rc = read_cc_raw(chip, &raw->shdw_cc, SHDW_CC);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700907 if (rc) {
908 pr_err("Failed to read raw cc data, rc = %d\n", rc);
909 return rc;
910 }
911
912 unlock_output_data(chip);
913 mutex_unlock(&chip->bms_output_lock);
914
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -0800915 if (chip->prev_last_good_ocv_raw == OCV_RAW_UNINITIALIZED) {
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800916 convert_and_store_ocv(chip, raw, batt_temp);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -0700917 pr_debug("PON_OCV_UV = %d, cc = %llx\n",
918 chip->last_ocv_uv, raw->cc);
919 warm_reset = qpnp_pon_is_warm_reset();
920 if (raw->last_good_ocv_uv < MIN_OCV_UV
921 || warm_reset > 0) {
922 pr_debug("OCV is stale or bad, estimating new OCV.\n");
923 chip->last_ocv_uv = estimate_ocv(chip);
924 raw->last_good_ocv_uv = chip->last_ocv_uv;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700925 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -0700926 pr_debug("New PON_OCV_UV = %d, cc = %llx\n",
927 chip->last_ocv_uv, raw->cc);
928 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800929 } else if (chip->new_battery) {
930 /* if a new battery was inserted, estimate the ocv */
931 reset_for_new_battery(chip, batt_temp);
932 raw->cc = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700933 raw->shdw_cc = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800934 raw->last_good_ocv_uv = chip->last_ocv_uv;
935 chip->new_battery = false;
Xiaozhe Shi74548b92013-05-02 16:47:08 -0700936 } else if (chip->done_charging) {
937 chip->done_charging = false;
938 /* if we just finished charging, reset CC and fake 100% */
939 chip->ocv_reading_at_100 = raw->last_good_ocv_raw;
940 chip->last_ocv_uv = chip->max_voltage_uv;
941 raw->last_good_ocv_uv = chip->max_voltage_uv;
942 raw->cc = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700943 raw->shdw_cc = 0;
944 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi74548b92013-05-02 16:47:08 -0700945 chip->last_ocv_temp = batt_temp;
946 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700947 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi74548b92013-05-02 16:47:08 -0700948 chip->last_cc_uah = INT_MIN;
949 pr_debug("EOC Battery full ocv_reading = 0x%x\n",
950 chip->ocv_reading_at_100);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700951 } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) {
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800952 convert_and_store_ocv(chip, raw, batt_temp);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700953 /* forget the old cc value upon ocv */
Xiaozhe Shif36d2862013-01-04 10:17:35 -0800954 chip->last_cc_uah = INT_MIN;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700955 } else {
956 raw->last_good_ocv_uv = chip->last_ocv_uv;
957 }
958
Xiaozhe Shi74548b92013-05-02 16:47:08 -0700959 /* stop faking a high OCV if we get a new OCV */
960 if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw)
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -0800961 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
Xiaozhe Shi74548b92013-05-02 16:47:08 -0700962
Xiaozhe Shie118c692012-09-24 15:17:43 -0700963 pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n",
964 raw->last_good_ocv_raw, raw->last_good_ocv_uv);
965 pr_debug("cc_raw= 0x%llx\n", raw->cc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700966 return 0;
967}
968
Xiaozhe Shie118c692012-09-24 15:17:43 -0700969static int calculate_pc(struct qpnp_bms_chip *chip, int ocv_uv,
970 int batt_temp)
971{
972 int pc;
973
974 pc = interpolate_pc(chip->pc_temp_ocv_lut,
975 batt_temp / 10, ocv_uv / 1000);
976 pr_debug("pc = %u %% for ocv = %d uv batt_temp = %d\n",
977 pc, ocv_uv, batt_temp);
978 /* Multiply the initial FCC value by the scale factor. */
979 return pc;
980}
981
982static int calculate_fcc(struct qpnp_bms_chip *chip, int batt_temp)
983{
984 int fcc_uah;
985
986 if (chip->adjusted_fcc_temp_lut == NULL) {
987 /* interpolate_fcc returns a mv value. */
988 fcc_uah = interpolate_fcc(chip->fcc_temp_lut,
989 batt_temp) * 1000;
990 pr_debug("fcc = %d uAh\n", fcc_uah);
991 return fcc_uah;
992 } else {
993 return 1000 * interpolate_fcc(chip->adjusted_fcc_temp_lut,
994 batt_temp);
995 }
996}
997
998/* calculate remaining charge at the time of ocv */
999static int calculate_ocv_charge(struct qpnp_bms_chip *chip,
1000 struct raw_soc_params *raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001001 int fcc_uah)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001002{
1003 int ocv_uv, pc;
1004
1005 ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001006 pc = calculate_pc(chip, ocv_uv, chip->last_ocv_temp);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001007 pr_debug("ocv_uv = %d pc = %d\n", ocv_uv, pc);
1008 return (fcc_uah * pc) / 100;
1009}
1010
Xiaozhe Shie118c692012-09-24 15:17:43 -07001011#define CC_READING_TICKS 56
1012#define SLEEP_CLK_HZ 32764
1013#define SECONDS_PER_HOUR 3600
1014
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001015static s64 cc_uv_to_pvh(s64 cc_uv)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001016{
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001017 /* Note that it is necessary need to multiply by 1000000 to convert
1018 * from uvh to pvh here.
1019 * However, the maximum Coulomb Counter value is 2^35, which can cause
1020 * an over flow.
1021 * Multiply by 100000 first to perserve as much precision as possible
1022 * then multiply by 10 after doing the division in order to avoid
1023 * overflow on the maximum Coulomb Counter value.
1024 */
1025 return div_s64(cc_uv * CC_READING_TICKS * 100000,
1026 SLEEP_CLK_HZ * SECONDS_PER_HOUR) * 10;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001027}
1028
1029/**
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001030 * calculate_cc() - converts a hardware coulomb counter reading into uah
Xiaozhe Shie118c692012-09-24 15:17:43 -07001031 * @chip: the bms chip pointer
1032 * @cc: the cc reading from bms h/w
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001033 * @cc_type: calcualte cc from regular or shadow coulomb counter
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001034 * @clear_cc: whether this function should clear the hardware counter
1035 * after reading
Xiaozhe Shie118c692012-09-24 15:17:43 -07001036 *
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001037 * Converts the 64 bit hardware coulomb counter into microamp-hour by taking
1038 * into account hardware resolution and adc errors.
1039 *
1040 * Return: the coulomb counter based charge in uAh (micro-amp hour)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001041 */
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001042static int calculate_cc(struct qpnp_bms_chip *chip, int64_t cc,
1043 int cc_type, int clear_cc)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001044{
Xiaozhe Shi4e376652012-10-25 12:38:50 -07001045 struct qpnp_iadc_calib calibration;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001046 struct qpnp_vadc_result result;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001047 int64_t cc_voltage_uv, cc_pvh, cc_uah, *software_counter;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001048 int rc;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001049
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001050 software_counter = cc_type == SHDW_CC ?
1051 &chip->software_shdw_cc_uah : &chip->software_cc_uah;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001052 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001053 if (rc) {
1054 pr_err("could not read pmic die temperature: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001055 return *software_counter;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001056 }
1057
Xiaozhe Shi4e376652012-10-25 12:38:50 -07001058 qpnp_iadc_get_gain_and_offset(&calibration);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001059 pr_debug("%scc = %lld, die_temp = %lld\n",
1060 cc_type == SHDW_CC ? "shdw_" : "",
1061 cc, result.physical);
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -07001062 cc_voltage_uv = cc_reading_to_uv(cc);
Xiaozhe Shi0c484932013-02-05 16:14:10 -08001063 cc_voltage_uv = cc_adjust_for_gain(cc_voltage_uv,
1064 calibration.gain_raw
1065 - calibration.offset_raw);
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001066 cc_pvh = cc_uv_to_pvh(cc_voltage_uv);
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001067 cc_uah = div_s64(cc_pvh, chip->r_sense_uohm);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001068 rc = qpnp_iadc_comp_result(&cc_uah);
1069 if (rc)
1070 pr_debug("error compensation failed: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001071 if (clear_cc == RESET) {
1072 pr_debug("software_%scc = %lld, added cc_uah = %lld\n",
1073 cc_type == SHDW_CC ? "sw_" : "",
1074 *software_counter, cc_uah);
1075 *software_counter += cc_uah;
1076 reset_cc(chip, cc_type == SHDW_CC ? CLEAR_SHDW_CC : CLEAR_CC);
1077 return (int)*software_counter;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001078 } else {
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001079 pr_debug("software_%scc = %lld, cc_uah = %lld, total = %lld\n",
1080 cc_type == SHDW_CC ? "shdw_" : "",
1081 *software_counter, cc_uah,
1082 *software_counter + cc_uah);
1083 return *software_counter + cc_uah;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001084 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07001085}
1086
1087static int get_rbatt(struct qpnp_bms_chip *chip,
1088 int soc_rbatt_mohm, int batt_temp)
1089{
1090 int rbatt_mohm, scalefactor;
1091
1092 rbatt_mohm = chip->default_rbatt_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001093 if (chip->rbatt_sf_lut == NULL) {
1094 pr_debug("RBATT = %d\n", rbatt_mohm);
1095 return rbatt_mohm;
1096 }
1097 /* Convert the batt_temp to DegC from deciDegC */
1098 batt_temp = batt_temp / 10;
1099 scalefactor = interpolate_scalingfactor(chip->rbatt_sf_lut,
1100 batt_temp, soc_rbatt_mohm);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001101 rbatt_mohm = (rbatt_mohm * scalefactor) / 100;
1102
1103 rbatt_mohm += chip->r_conn_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07001104 rbatt_mohm += chip->rbatt_capacitive_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001105 return rbatt_mohm;
1106}
1107
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -07001108#define IAVG_MINIMAL_TIME 2
Xiaozhe Shie118c692012-09-24 15:17:43 -07001109static void calculate_iavg(struct qpnp_bms_chip *chip, int cc_uah,
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001110 int *iavg_ua, int delta_time_s)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001111{
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001112 int delta_cc_uah = 0;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001113
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -07001114 /*
1115 * use the battery current if called too quickly
1116 */
1117 if (delta_time_s < IAVG_MINIMAL_TIME
1118 || chip->last_cc_uah == INT_MIN) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001119 get_battery_current(chip, iavg_ua);
1120 goto out;
1121 }
1122
Xiaozhe Shie118c692012-09-24 15:17:43 -07001123 delta_cc_uah = cc_uah - chip->last_cc_uah;
1124
1125 *iavg_ua = div_s64((s64)delta_cc_uah * 3600, delta_time_s);
1126
Xiaozhe Shie118c692012-09-24 15:17:43 -07001127out:
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001128 pr_debug("delta_cc = %d iavg_ua = %d\n", delta_cc_uah, (int)*iavg_ua);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001129
1130 /* remember cc_uah */
1131 chip->last_cc_uah = cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001132}
1133
1134static int calculate_termination_uuc(struct qpnp_bms_chip *chip,
1135 struct soc_params *params,
1136 int batt_temp, int uuc_iavg_ma,
1137 int *ret_pc_unusable)
1138{
1139 int unusable_uv, pc_unusable, uuc_uah;
1140 int i = 0;
1141 int ocv_mv;
1142 int batt_temp_degc = batt_temp / 10;
1143 int rbatt_mohm;
1144 int delta_uv;
1145 int prev_delta_uv = 0;
1146 int prev_rbatt_mohm = 0;
1147 int uuc_rbatt_mohm;
1148
1149 for (i = 0; i <= 100; i++) {
1150 ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
1151 batt_temp_degc, i);
1152 rbatt_mohm = get_rbatt(chip, i, batt_temp);
1153 unusable_uv = (rbatt_mohm * uuc_iavg_ma)
1154 + (chip->v_cutoff_uv);
1155 delta_uv = ocv_mv * 1000 - unusable_uv;
1156
Xiaozhe Shie118c692012-09-24 15:17:43 -07001157 if (delta_uv > 0)
1158 break;
1159
1160 prev_delta_uv = delta_uv;
1161 prev_rbatt_mohm = rbatt_mohm;
1162 }
1163
1164 uuc_rbatt_mohm = linear_interpolate(rbatt_mohm, delta_uv,
1165 prev_rbatt_mohm, prev_delta_uv,
1166 0);
1167
1168 unusable_uv = (uuc_rbatt_mohm * uuc_iavg_ma) + (chip->v_cutoff_uv);
1169
1170 pc_unusable = calculate_pc(chip, unusable_uv, batt_temp);
1171 uuc_uah = (params->fcc_uah * pc_unusable) / 100;
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001172 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 -07001173 uuc_iavg_ma,
1174 uuc_rbatt_mohm, unusable_uv,
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001175 pc_unusable, i, uuc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001176 *ret_pc_unusable = pc_unusable;
1177 return uuc_uah;
1178}
1179
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001180#define TIME_PER_PERCENT_UUC 60
Xiaozhe Shie118c692012-09-24 15:17:43 -07001181static int adjust_uuc(struct qpnp_bms_chip *chip,
1182 struct soc_params *params,
1183 int new_pc_unusable,
1184 int new_uuc_uah,
1185 int batt_temp)
1186{
1187 int new_unusable_mv, new_iavg_ma;
1188 int batt_temp_degc = batt_temp / 10;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001189 int max_percent_change;
1190
1191 max_percent_change = max(params->delta_time_s
1192 / TIME_PER_PERCENT_UUC, 1);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001193
1194 if (chip->prev_pc_unusable == -EINVAL
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001195 || abs(chip->prev_pc_unusable - new_pc_unusable)
1196 <= max_percent_change) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001197 chip->prev_pc_unusable = new_pc_unusable;
1198 return new_uuc_uah;
1199 }
1200
1201 /* the uuc is trying to change more than 1% restrict it */
1202 if (new_pc_unusable > chip->prev_pc_unusable)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001203 chip->prev_pc_unusable += max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001204 else
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001205 chip->prev_pc_unusable -= max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001206
1207 new_uuc_uah = (params->fcc_uah * chip->prev_pc_unusable) / 100;
1208
1209 /* also find update the iavg_ma accordingly */
1210 new_unusable_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
1211 batt_temp_degc, chip->prev_pc_unusable);
1212 if (new_unusable_mv < chip->v_cutoff_uv/1000)
1213 new_unusable_mv = chip->v_cutoff_uv/1000;
1214
1215 new_iavg_ma = (new_unusable_mv * 1000 - chip->v_cutoff_uv)
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001216 / params->rbatt_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001217 if (new_iavg_ma == 0)
1218 new_iavg_ma = 1;
1219 chip->prev_uuc_iavg_ma = new_iavg_ma;
1220 pr_debug("Restricting UUC to %d (%d%%) unusable_mv = %d iavg_ma = %d\n",
1221 new_uuc_uah, chip->prev_pc_unusable,
1222 new_unusable_mv, new_iavg_ma);
1223
1224 return new_uuc_uah;
1225}
1226
Abhijeet Dharmapurikarbdf8ba82012-12-20 18:33:56 -08001227#define MIN_IAVG_MA 250
Xiaozhe Shie118c692012-09-24 15:17:43 -07001228#define MIN_SECONDS_FOR_VALID_SAMPLE 20
1229static int calculate_unusable_charge_uah(struct qpnp_bms_chip *chip,
1230 struct soc_params *params,
1231 int batt_temp)
1232{
1233 int uuc_uah_iavg;
1234 int i;
1235 int uuc_iavg_ma = params->iavg_ua / 1000;
1236 int pc_unusable;
1237
1238 /*
1239 * if called first time, fill all the samples with
1240 * the shutdown_iavg_ma
1241 */
1242 if (chip->first_time_calc_uuc && chip->shutdown_iavg_ma != 0) {
1243 pr_debug("Using shutdown_iavg_ma = %d in all samples\n",
1244 chip->shutdown_iavg_ma);
1245 for (i = 0; i < IAVG_SAMPLES; i++)
1246 chip->iavg_samples_ma[i] = chip->shutdown_iavg_ma;
1247
1248 chip->iavg_index = 0;
1249 chip->iavg_num_samples = IAVG_SAMPLES;
1250 }
1251
1252 /*
1253 * if charging use a nominal avg current to keep
1254 * a reasonable UUC while charging
1255 */
Abhijeet Dharmapurikarbdf8ba82012-12-20 18:33:56 -08001256 if (uuc_iavg_ma < MIN_IAVG_MA)
1257 uuc_iavg_ma = MIN_IAVG_MA;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001258 chip->iavg_samples_ma[chip->iavg_index] = uuc_iavg_ma;
1259 chip->iavg_index = (chip->iavg_index + 1) % IAVG_SAMPLES;
1260 chip->iavg_num_samples++;
1261 if (chip->iavg_num_samples >= IAVG_SAMPLES)
1262 chip->iavg_num_samples = IAVG_SAMPLES;
1263
1264 /* now that this sample is added calcualte the average */
1265 uuc_iavg_ma = 0;
1266 if (chip->iavg_num_samples != 0) {
1267 for (i = 0; i < chip->iavg_num_samples; i++) {
1268 pr_debug("iavg_samples_ma[%d] = %d\n", i,
1269 chip->iavg_samples_ma[i]);
1270 uuc_iavg_ma += chip->iavg_samples_ma[i];
1271 }
1272
1273 uuc_iavg_ma = DIV_ROUND_CLOSEST(uuc_iavg_ma,
1274 chip->iavg_num_samples);
1275 }
1276
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001277 /*
1278 * if we're in bms reset mode, force uuc to be 3% of fcc
1279 */
1280 if (bms_reset)
1281 return (params->fcc_uah * 3) / 100;
1282
Xiaozhe Shi75e5efe2013-02-07 09:51:43 -08001283 uuc_uah_iavg = calculate_termination_uuc(chip, params, batt_temp,
1284 uuc_iavg_ma, &pc_unusable);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001285 pr_debug("uuc_iavg_ma = %d uuc with iavg = %d\n",
1286 uuc_iavg_ma, uuc_uah_iavg);
1287
1288 chip->prev_uuc_iavg_ma = uuc_iavg_ma;
1289 /* restrict the uuc such that it can increase only by one percent */
1290 uuc_uah_iavg = adjust_uuc(chip, params, pc_unusable,
1291 uuc_uah_iavg, batt_temp);
1292
1293 chip->first_time_calc_uuc = 0;
1294 return uuc_uah_iavg;
1295}
1296
1297static void find_ocv_for_soc(struct qpnp_bms_chip *chip,
1298 struct soc_params *params,
1299 int batt_temp,
1300 int shutdown_soc,
1301 int *ret_ocv_uv)
1302{
1303 s64 ocv_charge_uah;
1304 int pc, new_pc;
1305 int batt_temp_degc = batt_temp / 10;
1306 int ocv_uv;
1307
1308 ocv_charge_uah = (s64)shutdown_soc
1309 * (params->fcc_uah - params->uuc_uah);
1310 ocv_charge_uah = div_s64(ocv_charge_uah, 100)
1311 + params->cc_uah + params->uuc_uah;
1312 pc = DIV_ROUND_CLOSEST((int)ocv_charge_uah * 100, params->fcc_uah);
1313 pc = clamp(pc, 0, 100);
1314
1315 ocv_uv = interpolate_ocv(chip->pc_temp_ocv_lut, batt_temp_degc, pc);
1316
1317 pr_debug("s_soc = %d, fcc = %d uuc = %d rc = %d, pc = %d, ocv mv = %d\n",
1318 shutdown_soc, params->fcc_uah,
1319 params->uuc_uah, (int)ocv_charge_uah,
1320 pc, ocv_uv);
1321 new_pc = interpolate_pc(chip->pc_temp_ocv_lut, batt_temp_degc, ocv_uv);
1322 pr_debug("test revlookup pc = %d for ocv = %d\n", new_pc, ocv_uv);
1323
1324 while (abs(new_pc - pc) > 1) {
1325 int delta_mv = 5;
1326
1327 if (new_pc > pc)
1328 delta_mv = -1 * delta_mv;
1329
1330 ocv_uv = ocv_uv + delta_mv;
1331 new_pc = interpolate_pc(chip->pc_temp_ocv_lut,
1332 batt_temp_degc, ocv_uv);
1333 pr_debug("test revlookup pc = %d for ocv = %d\n",
1334 new_pc, ocv_uv);
1335 }
1336
1337 *ret_ocv_uv = ocv_uv * 1000;
1338 params->ocv_charge_uah = (int)ocv_charge_uah;
1339}
1340
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001341static int get_current_time(unsigned long *now_tm_sec)
1342{
1343 struct rtc_time tm;
1344 struct rtc_device *rtc;
1345 int rc;
1346
1347 rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
1348 if (rtc == NULL) {
1349 pr_err("%s: unable to open rtc device (%s)\n",
1350 __FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
Xiaozhe Shi0e01af62013-05-06 12:56:08 -07001351 return -EINVAL;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001352 }
1353
1354 rc = rtc_read_time(rtc, &tm);
1355 if (rc) {
1356 pr_err("Error reading rtc device (%s) : %d\n",
1357 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1358 goto close_time;
1359 }
1360
1361 rc = rtc_valid_tm(&tm);
1362 if (rc) {
1363 pr_err("Invalid RTC time (%s): %d\n",
1364 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1365 goto close_time;
1366 }
1367 rtc_tm_to_time(&tm, now_tm_sec);
1368
1369close_time:
1370 rtc_class_close(rtc);
1371 return rc;
1372}
1373
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08001374/* Returns estimated battery resistance */
1375static int get_prop_bms_batt_resistance(struct qpnp_bms_chip *chip)
1376{
1377 return chip->rbatt_mohm * 1000;
1378}
1379
1380/* Returns instantaneous current in uA */
1381static int get_prop_bms_current_now(struct qpnp_bms_chip *chip)
1382{
1383 int rc, result_ua;
1384
1385 rc = get_battery_current(chip, &result_ua);
1386 if (rc) {
1387 pr_err("failed to get current: %d\n", rc);
1388 return rc;
1389 }
1390 return result_ua;
1391}
1392
1393/* Returns coulomb counter in uAh */
1394static int get_prop_bms_charge_counter(struct qpnp_bms_chip *chip)
1395{
1396 int64_t cc_raw;
1397
1398 mutex_lock(&chip->bms_output_lock);
1399 lock_output_data(chip);
1400 read_cc_raw(chip, &cc_raw, false);
1401 unlock_output_data(chip);
1402 mutex_unlock(&chip->bms_output_lock);
1403
1404 return calculate_cc(chip, cc_raw, CC, NORESET);
1405}
1406
1407/* Returns shadow coulomb counter in uAh */
1408static int get_prop_bms_charge_counter_shadow(struct qpnp_bms_chip *chip)
1409{
1410 int64_t cc_raw;
1411
1412 mutex_lock(&chip->bms_output_lock);
1413 lock_output_data(chip);
1414 read_cc_raw(chip, &cc_raw, true);
1415 unlock_output_data(chip);
1416 mutex_unlock(&chip->bms_output_lock);
1417
1418 return calculate_cc(chip, cc_raw, SHDW_CC, NORESET);
1419}
1420
1421/* Returns full charge design in uAh */
1422static int get_prop_bms_charge_full_design(struct qpnp_bms_chip *chip)
1423{
1424 return chip->fcc_mah * 1000;
1425}
1426
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05301427/* Returns the current full charge in uAh */
1428static int get_prop_bms_charge_full(struct qpnp_bms_chip *chip)
1429{
1430 int rc;
1431 struct qpnp_vadc_result result;
1432
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001433 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05301434 if (rc) {
1435 pr_err("Unable to read battery temperature\n");
1436 return rc;
1437 }
1438
1439 return calculate_fcc(chip, (int)result.physical);
1440}
1441
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001442static int calculate_delta_time(unsigned long *time_stamp, int *delta_time_s)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001443{
1444 unsigned long now_tm_sec = 0;
1445
1446 /* default to delta time = 0 if anything fails */
1447 *delta_time_s = 0;
1448
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001449 if (get_current_time(&now_tm_sec)) {
1450 pr_err("RTC read failed\n");
1451 return 0;
1452 }
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001453
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001454 *delta_time_s = (now_tm_sec - *time_stamp);
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001455
1456 /* remember this time */
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001457 *time_stamp = now_tm_sec;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001458 return 0;
1459}
1460
Xiaozhe Shie118c692012-09-24 15:17:43 -07001461static void calculate_soc_params(struct qpnp_bms_chip *chip,
1462 struct raw_soc_params *raw,
1463 struct soc_params *params,
1464 int batt_temp)
1465{
Xiaozhe Shi219cb222013-06-10 15:49:59 -07001466 int soc_rbatt, shdw_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001467
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001468 calculate_delta_time(&chip->tm_sec, &params->delta_time_s);
1469 pr_debug("tm_sec = %ld, delta_s = %d\n",
1470 chip->tm_sec, params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001471 params->fcc_uah = calculate_fcc(chip, batt_temp);
1472 pr_debug("FCC = %uuAh batt_temp = %d\n", params->fcc_uah, batt_temp);
1473
1474 /* calculate remainging charge */
1475 params->ocv_charge_uah = calculate_ocv_charge(
1476 chip, raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001477 params->fcc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001478 pr_debug("ocv_charge_uah = %uuAh\n", params->ocv_charge_uah);
1479
1480 /* calculate cc micro_volt_hour */
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001481 params->cc_uah = calculate_cc(chip, raw->cc, CC, RESET);
Xiaozhe Shi219cb222013-06-10 15:49:59 -07001482 shdw_cc_uah = calculate_cc(chip, raw->shdw_cc, SHDW_CC, RESET);
1483 pr_debug("cc_uah = %duAh raw->cc = %llx, shdw_cc_uah = %duAh raw->shdw_cc = %llx\n",
1484 params->cc_uah, raw->cc,
1485 shdw_cc_uah, raw->shdw_cc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001486
1487 soc_rbatt = ((params->ocv_charge_uah - params->cc_uah) * 100)
1488 / params->fcc_uah;
1489 if (soc_rbatt < 0)
1490 soc_rbatt = 0;
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001491 params->rbatt_mohm = get_rbatt(chip, soc_rbatt, batt_temp);
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001492 pr_debug("rbatt_mohm = %d\n", params->rbatt_mohm);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001493
Xiaozhe Shi1e87cda2013-05-17 10:18:56 -07001494 if (params->rbatt_mohm != chip->rbatt_mohm) {
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07001495 chip->rbatt_mohm = params->rbatt_mohm;
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07001496 if (chip->bms_psy_registered)
Xiaozhe Shi1e87cda2013-05-17 10:18:56 -07001497 power_supply_changed(&chip->bms_psy);
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07001498 }
1499
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001500 calculate_iavg(chip, params->cc_uah, &params->iavg_ua,
1501 params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001502
1503 params->uuc_uah = calculate_unusable_charge_uah(chip, params,
1504 batt_temp);
1505 pr_debug("UUC = %uuAh\n", params->uuc_uah);
1506}
1507
1508static bool is_shutdown_soc_within_limits(struct qpnp_bms_chip *chip, int soc)
1509{
1510 if (chip->shutdown_soc_invalid) {
1511 pr_debug("NOT forcing shutdown soc = %d\n", chip->shutdown_soc);
1512 return 0;
1513 }
1514
1515 if (abs(chip->shutdown_soc - soc) > chip->shutdown_soc_valid_limit) {
1516 pr_debug("rejecting shutdown soc = %d, soc = %d limit = %d\n",
1517 chip->shutdown_soc, soc,
1518 chip->shutdown_soc_valid_limit);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001519 chip->shutdown_soc_invalid = true;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001520 return 0;
1521 }
1522
1523 return 1;
1524}
1525
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001526static int bound_soc(int soc)
1527{
1528 soc = max(0, soc);
1529 soc = min(100, soc);
1530 return soc;
1531}
1532
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001533#define IBAT_TOL_MASK 0x0F
1534#define OCV_TOL_MASK 0xF0
1535#define IBAT_TOL_DEFAULT 0x03
1536#define IBAT_TOL_NOCHG 0x0F
1537#define OCV_TOL_DEFAULT 0x20
1538#define OCV_TOL_NO_OCV 0x00
1539static int stop_ocv_updates(struct qpnp_bms_chip *chip)
1540{
1541 pr_debug("stopping ocv updates\n");
1542 return qpnp_masked_write(chip, BMS1_TOL_CTL,
1543 OCV_TOL_MASK, OCV_TOL_NO_OCV);
1544}
1545
1546static int reset_bms_for_test(struct qpnp_bms_chip *chip)
1547{
Xiaozhe Shi95da77f2013-02-20 13:40:06 -08001548 int ibat_ua = 0, vbat_uv = 0, rc;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001549 int ocv_est_uv;
1550
1551 if (!chip) {
1552 pr_err("BMS driver has not been initialized yet!\n");
1553 return -EINVAL;
1554 }
1555
1556 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
1557
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07001558 /*
1559 * Don't include rbatt and rbatt_capacitative since we expect this to
1560 * be used with a fake battery which does not have internal resistances
1561 */
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001562 ocv_est_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
1563 pr_debug("forcing ocv to be %d due to bms reset mode\n", ocv_est_uv);
1564 chip->last_ocv_uv = ocv_est_uv;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001565 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001566 chip->last_soc = -EINVAL;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001567 chip->last_soc_invalid = true;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001568 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001569 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001570 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001571 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001572 chip->last_cc_uah = INT_MIN;
1573 stop_ocv_updates(chip);
1574
1575 pr_debug("bms reset to ocv = %duv vbat_ua = %d ibat_ua = %d\n",
1576 chip->last_ocv_uv, vbat_uv, ibat_ua);
1577
1578 return rc;
1579}
1580
1581static int bms_reset_set(const char *val, const struct kernel_param *kp)
1582{
1583 int rc;
1584
1585 rc = param_set_bool(val, kp);
1586 if (rc) {
1587 pr_err("Unable to set bms_reset: %d\n", rc);
1588 return rc;
1589 }
1590
1591 if (*(bool *)kp->arg) {
1592 struct power_supply *bms_psy = power_supply_get_by_name("bms");
1593 struct qpnp_bms_chip *chip = container_of(bms_psy,
1594 struct qpnp_bms_chip, bms_psy);
1595
1596 rc = reset_bms_for_test(chip);
1597 if (rc) {
1598 pr_err("Unable to modify bms_reset: %d\n", rc);
1599 return rc;
1600 }
1601 }
1602 return 0;
1603}
1604
1605static struct kernel_param_ops bms_reset_ops = {
1606 .set = bms_reset_set,
1607 .get = param_get_bool,
1608};
1609
1610module_param_cb(bms_reset, &bms_reset_ops, &bms_reset, 0644);
1611
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001612static void backup_soc_and_iavg(struct qpnp_bms_chip *chip, int batt_temp,
1613 int soc)
1614{
1615 u8 temp;
1616 int rc;
1617 int iavg_ma = chip->prev_uuc_iavg_ma;
1618
1619 if (iavg_ma > IAVG_START)
1620 temp = (iavg_ma - IAVG_START) / IAVG_STEP_SIZE_MA;
1621 else
1622 temp = 0;
1623
1624 rc = qpnp_write_wrapper(chip, &temp,
1625 chip->base + IAVG_STORAGE_REG, 1);
1626
1627 temp = soc;
1628
1629 /* don't store soc if temperature is below 5degC */
1630 if (batt_temp > IGNORE_SOC_TEMP_DECIDEG)
1631 rc = qpnp_write_wrapper(chip, &temp,
1632 chip->base + SOC_STORAGE_REG, 1);
1633}
1634
1635static int scale_soc_while_chg(struct qpnp_bms_chip *chip, int chg_time_sec,
1636 int catch_up_sec, int new_soc, int prev_soc)
1637{
1638 int scaled_soc;
1639 int numerator;
1640
1641 /*
1642 * Don't report a high value immediately slowly scale the
1643 * value from prev_soc to the new soc based on a charge time
1644 * weighted average
1645 */
1646 pr_debug("cts = %d catch_up_sec = %d\n", chg_time_sec, catch_up_sec);
1647 if (catch_up_sec == 0)
1648 return new_soc;
1649
1650 if (chg_time_sec > catch_up_sec)
1651 return new_soc;
1652
1653 numerator = (catch_up_sec - chg_time_sec) * prev_soc
1654 + chg_time_sec * new_soc;
1655 scaled_soc = numerator / catch_up_sec;
1656
1657 pr_debug("cts = %d new_soc = %d prev_soc = %d scaled_soc = %d\n",
1658 chg_time_sec, new_soc, prev_soc, scaled_soc);
1659
1660 return scaled_soc;
1661}
1662
1663/*
1664 * bms_fake_battery is set in setups where a battery emulator is used instead
1665 * of a real battery. This makes the bms driver report a different/fake value
1666 * regardless of the calculated state of charge.
1667 */
1668static int bms_fake_battery = -EINVAL;
1669module_param(bms_fake_battery, int, 0644);
1670
1671static int report_voltage_based_soc(struct qpnp_bms_chip *chip)
1672{
1673 pr_debug("Reported voltage based soc = %d\n",
1674 chip->prev_voltage_based_soc);
1675 return chip->prev_voltage_based_soc;
1676}
1677
1678#define SOC_CATCHUP_SEC_MAX 600
1679#define SOC_CATCHUP_SEC_PER_PERCENT 60
1680#define MAX_CATCHUP_SOC (SOC_CATCHUP_SEC_MAX / SOC_CATCHUP_SEC_PER_PERCENT)
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07001681#define SOC_CHANGE_PER_SEC 5
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001682static int report_cc_based_soc(struct qpnp_bms_chip *chip)
1683{
1684 int soc, soc_change;
1685 int time_since_last_change_sec, charge_time_sec = 0;
1686 unsigned long last_change_sec;
1687 struct timespec now;
1688 struct qpnp_vadc_result result;
1689 int batt_temp;
1690 int rc;
1691 bool charging, charging_since_last_report;
1692
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001693 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001694
1695 if (rc) {
1696 pr_err("error reading adc channel = %d, rc = %d\n",
1697 LR_MUX1_BATT_THERM, rc);
1698 return rc;
1699 }
1700 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
1701 result.measurement);
1702 batt_temp = (int)result.physical;
1703
1704 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shifa6ea692013-05-31 11:15:13 -07001705 soc = chip->calculated_soc;
1706
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001707 last_change_sec = chip->last_soc_change_sec;
1708 calculate_delta_time(&last_change_sec, &time_since_last_change_sec);
1709
1710 charging = is_battery_charging(chip);
1711 charging_since_last_report = charging || (chip->last_soc_unbound
1712 && chip->was_charging_at_sleep);
1713 /*
1714 * account for charge time - limit it to SOC_CATCHUP_SEC to
1715 * avoid overflows when charging continues for extended periods
1716 */
1717 if (charging) {
1718 if (chip->charge_start_tm_sec == 0) {
1719 /*
1720 * calculating soc for the first time
1721 * after start of chg. Initialize catchup time
1722 */
1723 if (abs(soc - chip->last_soc) < MAX_CATCHUP_SOC)
1724 chip->catch_up_time_sec =
1725 (soc - chip->last_soc)
1726 * SOC_CATCHUP_SEC_PER_PERCENT;
1727 else
1728 chip->catch_up_time_sec = SOC_CATCHUP_SEC_MAX;
1729
1730 if (chip->catch_up_time_sec < 0)
1731 chip->catch_up_time_sec = 0;
1732 chip->charge_start_tm_sec = last_change_sec;
1733 }
1734
1735 charge_time_sec = min(SOC_CATCHUP_SEC_MAX, (int)last_change_sec
1736 - chip->charge_start_tm_sec);
1737
1738 /* end catchup if calculated soc and last soc are same */
1739 if (chip->last_soc == soc)
1740 chip->catch_up_time_sec = 0;
1741 }
1742
1743 if (chip->last_soc != -EINVAL) {
1744 /*
1745 * last_soc < soc ... if we have not been charging at all
1746 * since the last time this was called, report previous SoC.
1747 * Otherwise, scale and catch up.
1748 */
1749 if (chip->last_soc < soc && !charging_since_last_report)
1750 soc = chip->last_soc;
1751 else if (chip->last_soc < soc && soc != 100)
1752 soc = scale_soc_while_chg(chip, charge_time_sec,
1753 chip->catch_up_time_sec,
1754 soc, chip->last_soc);
1755
1756 soc_change = min((int)abs(chip->last_soc - soc),
1757 time_since_last_change_sec / SOC_CHANGE_PER_SEC);
1758 if (chip->last_soc_unbound) {
1759 chip->last_soc_unbound = false;
1760 } else {
1761 /*
1762 * if soc have not been unbound by resume,
1763 * only change reported SoC by 1.
1764 */
1765 soc_change = min(1, soc_change);
1766 }
1767
1768 if (soc < chip->last_soc && soc != 0)
1769 soc = chip->last_soc - soc_change;
1770 if (soc > chip->last_soc && soc != 100)
1771 soc = chip->last_soc + soc_change;
1772 }
1773
Xiaozhe Shi208b8e52013-05-28 10:16:32 -07001774 if (chip->last_soc != soc && !chip->last_soc_unbound)
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001775 chip->last_soc_change_sec = last_change_sec;
1776
1777 pr_debug("last_soc = %d, calculated_soc = %d, soc = %d, time since last change = %d\n",
1778 chip->last_soc, chip->calculated_soc,
1779 soc, time_since_last_change_sec);
1780 chip->last_soc = bound_soc(soc);
1781 backup_soc_and_iavg(chip, batt_temp, chip->last_soc);
1782 pr_debug("Reported SOC = %d\n", chip->last_soc);
1783 chip->t_soc_queried = now;
1784 mutex_unlock(&chip->last_soc_mutex);
1785
1786 return soc;
1787}
1788
1789static int report_state_of_charge(struct qpnp_bms_chip *chip)
1790{
1791 if (bms_fake_battery != -EINVAL) {
1792 pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery);
1793 return bms_fake_battery;
1794 } else if (chip->use_voltage_soc)
1795 return report_voltage_based_soc(chip);
1796 else
1797 return report_cc_based_soc(chip);
1798}
1799
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001800#define VDD_MAX_ERR 5000
1801#define VDD_STEP_SIZE 10000
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001802static int charging_adjustments(struct qpnp_bms_chip *chip,
1803 struct soc_params *params, int soc,
1804 int vbat_uv, int ibat_ua, int batt_temp)
1805{
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001806 int chg_soc, soc_ibat, batt_terminal_uv, weight_ibat, weight_cc;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001807
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001808 batt_terminal_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001809
1810 if (chip->soc_at_cv == -EINVAL) {
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001811 if (batt_terminal_uv >= chip->max_voltage_uv - VDD_MAX_ERR) {
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001812 chip->soc_at_cv = soc;
1813 chip->prev_chg_soc = soc;
1814 chip->ibat_at_cv_ua = ibat_ua;
1815 pr_debug("CC_TO_CV ibat_ua = %d CHG SOC %d\n",
1816 ibat_ua, soc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001817 } else {
1818 /* In constant current charging return the calc soc */
1819 pr_debug("CC CHG SOC %d\n", soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001820 }
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001821
1822 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001823 return soc;
1824 }
1825
1826 /*
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001827 * battery is in CV phase - begin linear interpolation of soc based on
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001828 * battery charge current
1829 */
1830
1831 /*
1832 * if voltage lessened (possibly because of a system load)
1833 * keep reporting the prev chg soc
1834 */
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001835 if (batt_terminal_uv <= chip->prev_batt_terminal_uv - VDD_STEP_SIZE) {
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08001836 pr_debug("batt_terminal_uv %d < (max = %d - 10000); CC CHG SOC %d\n",
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001837 batt_terminal_uv, chip->prev_batt_terminal_uv,
1838 chip->prev_chg_soc);
1839 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001840 return chip->prev_chg_soc;
1841 }
1842
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001843 soc_ibat = bound_soc(linear_interpolate(chip->soc_at_cv,
1844 chip->ibat_at_cv_ua,
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08001845 100, -1 * chip->chg_term_ua,
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001846 ibat_ua));
1847 weight_ibat = bound_soc(linear_interpolate(1, chip->soc_at_cv,
1848 100, 100, chip->prev_chg_soc));
1849 weight_cc = 100 - weight_ibat;
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001850 chg_soc = bound_soc(DIV_ROUND_CLOSEST(soc_ibat * weight_ibat
1851 + weight_cc * soc, 100));
1852
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001853 pr_debug("weight_ibat = %d, weight_cc = %d, soc_ibat = %d, soc_cc = %d\n",
1854 weight_ibat, weight_cc, soc_ibat, soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001855
1856 /* always report a higher soc */
1857 if (chg_soc > chip->prev_chg_soc) {
1858 int new_ocv_uv;
1859
1860 chip->prev_chg_soc = chg_soc;
1861
1862 find_ocv_for_soc(chip, params, batt_temp, chg_soc, &new_ocv_uv);
Xiaozhe Shicc48e992013-05-28 16:42:24 -07001863 chip->charging_adjusted_ocv = new_ocv_uv;
1864 pr_debug("CC CHG ADJ OCV = %d CHG SOC %d\n", new_ocv_uv,
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001865 chip->prev_chg_soc);
1866 }
1867
1868 pr_debug("Reporting CHG SOC %d\n", chip->prev_chg_soc);
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001869 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001870 return chip->prev_chg_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001871}
1872
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001873static void very_low_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
1874{
1875 /*
1876 * if battery is very low (v_cutoff voltage + 20mv) hold
1877 * a wakelock untill soc = 0%
1878 */
1879 if (vbat_uv <= chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001880 && !wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001881 pr_debug("voltage = %d low holding wakelock\n", vbat_uv);
1882 wake_lock(&chip->low_voltage_wake_lock);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001883 } else if (vbat_uv > chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001884 && wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001885 pr_debug("voltage = %d releasing wakelock\n", vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001886 wake_unlock(&chip->low_voltage_wake_lock);
1887 }
1888}
1889
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001890#define VBATT_ERROR_MARGIN 20000
1891static void cv_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
1892{
1893 /*
1894 * if battery is very low (v_cutoff voltage + 20mv) hold
1895 * a wakelock untill soc = 0%
1896 */
1897 if (wake_lock_active(&chip->cv_wake_lock)) {
1898 if (chip->soc_at_cv != -EINVAL) {
1899 pr_debug("hit CV, releasing cv wakelock\n");
1900 wake_unlock(&chip->cv_wake_lock);
1901 } else if (!is_battery_charging(chip)) {
1902 pr_debug("charging stopped, releasing cv wakelock\n");
1903 wake_unlock(&chip->cv_wake_lock);
1904 }
1905 } else if (vbat_uv > chip->max_voltage_uv - VBATT_ERROR_MARGIN
1906 && chip->soc_at_cv == -EINVAL
1907 && is_battery_charging(chip)
1908 && !wake_lock_active(&chip->cv_wake_lock)) {
1909 pr_debug("voltage = %d holding cv wakelock\n", vbat_uv);
1910 wake_lock(&chip->cv_wake_lock);
1911 }
1912}
1913
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07001914#define NO_ADJUST_HIGH_SOC_THRESHOLD 90
Xiaozhe Shie118c692012-09-24 15:17:43 -07001915static int adjust_soc(struct qpnp_bms_chip *chip, struct soc_params *params,
1916 int soc, int batt_temp)
1917{
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001918 int ibat_ua = 0, vbat_uv = 0;
1919 int ocv_est_uv = 0, soc_est = 0, pc_est = 0, pc = 0;
1920 int delta_ocv_uv = 0;
1921 int n = 0;
1922 int rc_new_uah = 0;
1923 int pc_new = 0;
1924 int soc_new = 0;
1925 int slope = 0;
1926 int rc = 0;
1927 int delta_ocv_uv_limit = 0;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07001928 int correction_limit_uv = 0;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001929
1930 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
1931 if (rc < 0) {
1932 pr_err("simultaneous vbat ibat failed err = %d\n", rc);
1933 goto out;
1934 }
1935
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001936 very_low_voltage_check(chip, vbat_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001937 cv_voltage_check(chip, vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001938
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001939 delta_ocv_uv_limit = DIV_ROUND_CLOSEST(ibat_ua, 1000);
1940
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001941 ocv_est_uv = vbat_uv + (ibat_ua * params->rbatt_mohm)/1000;
1942
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001943 pc_est = calculate_pc(chip, ocv_est_uv, batt_temp);
1944 soc_est = div_s64((s64)params->fcc_uah * pc_est - params->uuc_uah*100,
1945 (s64)params->fcc_uah - params->uuc_uah);
1946 soc_est = bound_soc(soc_est);
1947
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001948 /* never adjust during bms reset mode */
1949 if (bms_reset) {
1950 pr_debug("bms reset mode, SOC adjustment skipped\n");
1951 goto out;
1952 }
1953
Xiaozhe Shi8658c982013-04-30 11:33:07 -07001954 if (ibat_ua < 0 && !is_battery_full(chip)) {
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001955 soc = charging_adjustments(chip, params, soc, vbat_uv, ibat_ua,
1956 batt_temp);
1957 goto out;
1958 }
1959
1960 /*
1961 * do not adjust
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07001962 * if soc_est is same as what bms calculated
1963 * OR if soc_est > adjust_soc_low_threshold
1964 * OR if soc is above 90
1965 * because we might pull it low
1966 * and cause a bad user experience
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001967 */
1968 if (soc_est == soc
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07001969 || soc_est > chip->adjust_soc_low_threshold
1970 || soc >= NO_ADJUST_HIGH_SOC_THRESHOLD)
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001971 goto out;
1972
1973 if (chip->last_soc_est == -EINVAL)
1974 chip->last_soc_est = soc;
1975
1976 n = min(200, max(1 , soc + soc_est + chip->last_soc_est));
1977 chip->last_soc_est = soc_est;
1978
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001979 pc = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001980 if (pc > 0) {
1981 pc_new = calculate_pc(chip,
1982 chip->last_ocv_uv - (++slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001983 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001984 while (pc_new == pc) {
1985 /* start taking 10mV steps */
1986 slope = slope + 10;
1987 pc_new = calculate_pc(chip,
1988 chip->last_ocv_uv - (slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001989 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001990 }
1991 } else {
1992 /*
1993 * pc is already at the lowest point,
1994 * assume 1 millivolt translates to 1% pc
1995 */
1996 pc = 1;
1997 pc_new = 0;
1998 slope = 1;
1999 }
2000
2001 delta_ocv_uv = div_s64((soc - soc_est) * (s64)slope * 1000,
2002 n * (pc - pc_new));
2003
2004 if (abs(delta_ocv_uv) > delta_ocv_uv_limit) {
2005 pr_debug("limiting delta ocv %d limit = %d\n", delta_ocv_uv,
2006 delta_ocv_uv_limit);
2007
2008 if (delta_ocv_uv > 0)
2009 delta_ocv_uv = delta_ocv_uv_limit;
2010 else
2011 delta_ocv_uv = -1 * delta_ocv_uv_limit;
2012 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
2013 }
2014
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002015 if (chip->last_ocv_uv > chip->flat_ocv_threshold_uv)
2016 correction_limit_uv = chip->high_ocv_correction_limit_uv;
2017 else
2018 correction_limit_uv = chip->low_ocv_correction_limit_uv;
2019
2020 if (abs(delta_ocv_uv) > correction_limit_uv) {
2021 pr_debug("limiting delta ocv %d limit = %d\n",
2022 delta_ocv_uv, correction_limit_uv);
2023 if (delta_ocv_uv > 0)
2024 delta_ocv_uv = correction_limit_uv;
2025 else
2026 delta_ocv_uv = -correction_limit_uv;
2027 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
2028 }
2029
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002030 chip->last_ocv_uv -= delta_ocv_uv;
2031
2032 if (chip->last_ocv_uv >= chip->max_voltage_uv)
2033 chip->last_ocv_uv = chip->max_voltage_uv;
2034
2035 /* calculate the soc based on this new ocv */
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002036 pc_new = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002037 rc_new_uah = (params->fcc_uah * pc_new) / 100;
2038 soc_new = (rc_new_uah - params->cc_uah - params->uuc_uah)*100
2039 / (params->fcc_uah - params->uuc_uah);
2040 soc_new = bound_soc(soc_new);
2041
2042 /*
2043 * if soc_new is ZERO force it higher so that phone doesnt report soc=0
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002044 * soc = 0 should happen only when soc_est is above a set value
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002045 */
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002046 if (soc_new == 0 && soc_est >= chip->hold_soc_est)
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002047 soc_new = 1;
2048
2049 soc = soc_new;
2050
2051out:
2052 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",
2053 ibat_ua, vbat_uv, ocv_est_uv, pc_est,
2054 soc_est, n, delta_ocv_uv, chip->last_ocv_uv,
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08002055 pc_new, soc_new, params->rbatt_mohm, slope);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002056
Xiaozhe Shie118c692012-09-24 15:17:43 -07002057 return soc;
2058}
2059
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002060static int clamp_soc_based_on_voltage(struct qpnp_bms_chip *chip, int soc)
2061{
2062 int rc, vbat_uv;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002063
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002064 rc = get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shi36458962013-02-06 16:19:57 -08002065 if (rc < 0) {
2066 pr_err("adc vbat failed err = %d\n", rc);
2067 return soc;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002068 }
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002069 if (soc == 0 && vbat_uv > chip->v_cutoff_uv) {
2070 pr_debug("clamping soc to 1, vbat (%d) > cutoff (%d)\n",
2071 vbat_uv, chip->v_cutoff_uv);
2072 return 1;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002073 } else {
2074 pr_debug("not clamping, using soc = %d, vbat = %d and cutoff = %d\n",
2075 soc, vbat_uv, chip->v_cutoff_uv);
2076 return soc;
2077 }
2078}
2079
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002080static int64_t convert_cc_uah_to_raw(struct qpnp_bms_chip *chip, int64_t cc_uah)
2081{
2082 int64_t cc_uv, cc_pvh, cc_raw;
2083
2084 cc_pvh = cc_uah * chip->r_sense_uohm;
2085 cc_uv = div_s64(cc_pvh * SLEEP_CLK_HZ * SECONDS_PER_HOUR,
2086 CC_READING_TICKS * 1000000LL);
2087 cc_raw = div_s64(cc_uv * CC_READING_RESOLUTION_D,
2088 CC_READING_RESOLUTION_N);
2089 return cc_raw;
2090}
2091
2092#define CC_STEP_INCREMENT_UAH 1500
2093#define OCV_STEP_INCREMENT 0x10
2094static void configure_soc_wakeup(struct qpnp_bms_chip *chip,
2095 struct soc_params *params,
2096 int batt_temp, int target_soc)
2097{
2098 int target_ocv_uv;
2099 int64_t target_cc_uah, cc_raw_64, current_shdw_cc_raw_64;
2100 int64_t current_shdw_cc_uah, iadc_comp_factor;
2101 uint64_t cc_raw, current_shdw_cc_raw;
2102 int16_t ocv_raw, current_ocv_raw;
2103
2104 current_shdw_cc_raw = 0;
2105 mutex_lock(&chip->bms_output_lock);
2106 lock_output_data(chip);
2107 qpnp_read_wrapper(chip, (u8 *)&current_ocv_raw,
2108 chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
2109 unlock_output_data(chip);
2110 mutex_unlock(&chip->bms_output_lock);
2111 current_shdw_cc_uah = get_prop_bms_charge_counter_shadow(chip);
2112 current_shdw_cc_raw_64 = convert_cc_uah_to_raw(chip,
2113 current_shdw_cc_uah);
2114
2115 /*
2116 * Calculate the target shadow coulomb counter threshold for when
2117 * the SoC changes.
2118 *
2119 * Since the BMS driver resets the shadow coulomb counter every
2120 * 20 seconds when the device is awake, calculate the threshold as
2121 * a delta from the current shadow coulomb count.
2122 */
2123 target_cc_uah = (100 - target_soc)
2124 * (params->fcc_uah - params->uuc_uah)
2125 / 100 - current_shdw_cc_uah;
2126 if (target_cc_uah < 0) {
2127 /*
2128 * If the target cc is below 0, that means we have already
2129 * passed the point where SoC should have fallen.
2130 * Set a wakeup in a few more mAh and check back again
2131 */
2132 target_cc_uah = CC_STEP_INCREMENT_UAH;
2133 }
2134 iadc_comp_factor = 100000;
2135 qpnp_iadc_comp_result(&iadc_comp_factor);
2136 target_cc_uah = div64_s64(target_cc_uah * 100000, iadc_comp_factor);
2137 target_cc_uah = cc_reverse_adjust_for_gain(target_cc_uah);
2138 cc_raw_64 = convert_cc_uah_to_raw(chip, target_cc_uah);
2139 cc_raw = convert_s64_to_s36(cc_raw_64);
2140
2141 find_ocv_for_soc(chip, params, batt_temp, target_soc, &target_ocv_uv);
2142 ocv_raw = convert_vbatt_uv_to_raw(chip, target_ocv_uv);
2143
2144 /*
2145 * If the current_ocv_raw was updated since reaching 100% and is lower
2146 * than the calculated target ocv threshold, set the new target
2147 * threshold 1.5mAh lower in order to check if the SoC changed yet.
2148 */
2149 if (current_ocv_raw != chip->ocv_reading_at_100
2150 && current_ocv_raw < ocv_raw)
2151 ocv_raw = current_ocv_raw - OCV_STEP_INCREMENT;
2152
2153 qpnp_write_wrapper(chip, (u8 *)&cc_raw,
2154 chip->base + BMS1_SW_CC_THR0, 5);
2155 qpnp_write_wrapper(chip, (u8 *)&ocv_raw,
2156 chip->base + BMS1_OCV_THR0, 2);
2157
2158 pr_debug("current sw_cc_raw = 0x%llx, current ocv = 0x%hx\n",
2159 current_shdw_cc_raw, (uint16_t)current_ocv_raw);
2160 pr_debug("target_cc_uah = %lld, raw64 = 0x%llx, raw 36 = 0x%llx, ocv_raw = 0x%hx\n",
2161 target_cc_uah,
2162 (uint64_t)cc_raw_64, cc_raw,
2163 (uint16_t)ocv_raw);
2164}
2165
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002166#define SLEEP_RECALC_INTERVAL 3
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002167static int calculate_state_of_charge(struct qpnp_bms_chip *chip,
2168 struct raw_soc_params *raw,
2169 int batt_temp)
2170{
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07002171 int soc, new_ocv_uv, previous_soc;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002172 int shutdown_soc, new_calculated_soc, remaining_usable_charge_uah;
2173 struct soc_params params;
2174
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002175 if (!is_battery_present(chip)) {
Xiaozhe Shi026fa9b2013-03-22 17:00:50 -07002176 pr_debug("battery gone, reporting 100\n");
2177 new_calculated_soc = 100;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002178 goto done_calculating;
2179 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07002180 calculate_soc_params(chip, raw, &params, batt_temp);
2181 /* calculate remaining usable charge */
2182 remaining_usable_charge_uah = params.ocv_charge_uah
2183 - params.cc_uah
2184 - params.uuc_uah;
2185
2186 pr_debug("RUC = %duAh\n", remaining_usable_charge_uah);
2187 if (params.fcc_uah - params.uuc_uah <= 0) {
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002188 pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
Xiaozhe Shie118c692012-09-24 15:17:43 -07002189 params.fcc_uah,
2190 params.uuc_uah);
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002191 new_calculated_soc = 0;
2192 goto done_calculating;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002193 }
2194
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002195 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2196 (params.fcc_uah - params.uuc_uah));
2197
Xiaozhe Shie118c692012-09-24 15:17:43 -07002198 if (chip->first_time_calc_soc && soc < 0) {
2199 /*
2200 * first time calcualtion and the pon ocv is too low resulting
2201 * in a bad soc. Adjust ocv to get 0 soc
2202 */
2203 pr_debug("soc is %d, adjusting pon ocv to make it 0\n", soc);
2204 find_ocv_for_soc(chip, &params, batt_temp, 0, &new_ocv_uv);
2205 chip->last_ocv_uv = new_ocv_uv;
2206
2207 remaining_usable_charge_uah = params.ocv_charge_uah
2208 - params.cc_uah
2209 - params.uuc_uah;
2210
2211 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2212 (params.fcc_uah
2213 - params.uuc_uah));
2214 pr_debug("DONE for O soc is %d, pon ocv adjusted to %duV\n",
2215 soc, chip->last_ocv_uv);
2216 }
2217
2218 if (soc > 100)
2219 soc = 100;
2220
2221 if (soc < 0) {
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002222 pr_debug("bad rem_usb_chg = %d rem_chg %d, cc_uah %d, unusb_chg %d\n",
Xiaozhe Shie118c692012-09-24 15:17:43 -07002223 remaining_usable_charge_uah,
2224 params.ocv_charge_uah,
2225 params.cc_uah, params.uuc_uah);
2226
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002227 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 -07002228 chip->last_ocv_uv, batt_temp,
2229 params.fcc_uah, soc);
2230 soc = 0;
2231 }
2232
2233 mutex_lock(&chip->soc_invalidation_mutex);
2234 shutdown_soc = chip->shutdown_soc;
2235
2236 if (chip->first_time_calc_soc && soc != shutdown_soc
2237 && is_shutdown_soc_within_limits(chip, soc)) {
2238 /*
2239 * soc for the first time - use shutdown soc
2240 * to adjust pon ocv since it is a small percent away from
2241 * the real soc
2242 */
2243 pr_debug("soc = %d before forcing shutdown_soc = %d\n",
2244 soc, shutdown_soc);
2245 find_ocv_for_soc(chip, &params, batt_temp,
2246 shutdown_soc, &new_ocv_uv);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002247 chip->last_ocv_uv = new_ocv_uv;
2248
2249 remaining_usable_charge_uah = params.ocv_charge_uah
2250 - params.cc_uah
2251 - params.uuc_uah;
2252
2253 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2254 (params.fcc_uah
2255 - params.uuc_uah));
2256
2257 pr_debug("DONE for shutdown_soc = %d soc is %d, adjusted ocv to %duV\n",
2258 shutdown_soc, soc, chip->last_ocv_uv);
2259 }
2260 mutex_unlock(&chip->soc_invalidation_mutex);
2261
2262 pr_debug("SOC before adjustment = %d\n", soc);
2263 new_calculated_soc = adjust_soc(chip, &params, soc, batt_temp);
2264
Xiaozhe Shi445d2492013-03-27 18:10:18 -07002265 /* always clamp soc due to BMS hw/sw immaturities */
2266 new_calculated_soc = clamp_soc_based_on_voltage(chip,
2267 new_calculated_soc);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002268 /*
2269 * If the battery is full, configure the cc threshold so the system
2270 * wakes up after SoC changes
2271 */
2272 if (is_battery_full(chip))
2273 configure_soc_wakeup(chip, &params,
2274 batt_temp, bound_soc(new_calculated_soc - 1));
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002275done_calculating:
Xiaozhe Shifa6ea692013-05-31 11:15:13 -07002276 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07002277 previous_soc = chip->calculated_soc;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002278 chip->calculated_soc = new_calculated_soc;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002279 pr_debug("CC based calculated SOC = %d\n", chip->calculated_soc);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002280 if (chip->last_soc_invalid) {
2281 chip->last_soc_invalid = false;
2282 chip->last_soc = -EINVAL;
2283 }
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002284 /*
2285 * Check if more than a long time has passed since the last
2286 * calculation (more than n times compared to the soc recalculation
2287 * rate, where n is defined by SLEEP_RECALC_INTERVAL). If this is true,
2288 * then the system must have gone through a long sleep, and SoC can be
2289 * allowed to become unbounded by the last reported SoC
2290 */
2291 if (params.delta_time_s * 1000 >
2292 chip->calculate_soc_ms * SLEEP_RECALC_INTERVAL
2293 && !chip->first_time_calc_soc) {
2294 chip->last_soc_unbound = true;
2295 chip->last_soc_change_sec = chip->last_recalc_time;
2296 pr_debug("last_soc unbound because elapsed time = %d\n",
2297 params.delta_time_s);
2298 }
2299 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07002300
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002301 if (new_calculated_soc != previous_soc && chip->bms_psy_registered) {
Xiaozhe Shi83484e32013-05-16 10:59:59 -07002302 power_supply_changed(&chip->bms_psy);
2303 pr_debug("power supply changed\n");
2304 } else {
2305 /*
2306 * Call report state of charge anyways to periodically update
2307 * reported SoC. This prevents reported SoC from being stuck
2308 * when calculated soc doesn't change.
2309 */
2310 report_state_of_charge(chip);
2311 }
2312
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002313 get_current_time(&chip->last_recalc_time);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002314 chip->first_time_calc_soc = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002315 return chip->calculated_soc;
2316}
2317
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002318static int calculate_soc_from_voltage(struct qpnp_bms_chip *chip)
2319{
2320 int voltage_range_uv, voltage_remaining_uv, voltage_based_soc;
Xiaozhe Shi36458962013-02-06 16:19:57 -08002321 int rc, vbat_uv;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002322
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002323 rc = get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shi36458962013-02-06 16:19:57 -08002324 if (rc < 0) {
2325 pr_err("adc vbat failed err = %d\n", rc);
2326 return rc;
2327 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002328 voltage_range_uv = chip->max_voltage_uv - chip->v_cutoff_uv;
2329 voltage_remaining_uv = vbat_uv - chip->v_cutoff_uv;
2330 voltage_based_soc = voltage_remaining_uv * 100 / voltage_range_uv;
2331
2332 voltage_based_soc = clamp(voltage_based_soc, 0, 100);
2333
2334 if (chip->prev_voltage_based_soc != voltage_based_soc
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002335 && chip->bms_psy_registered) {
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002336 power_supply_changed(&chip->bms_psy);
2337 pr_debug("power supply changed\n");
2338 }
2339 chip->prev_voltage_based_soc = voltage_based_soc;
2340
2341 pr_debug("vbat used = %duv\n", vbat_uv);
2342 pr_debug("Calculated voltage based soc = %d\n", voltage_based_soc);
2343 return voltage_based_soc;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08002344}
2345
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002346static int recalculate_soc(struct qpnp_bms_chip *chip)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002347{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002348 int batt_temp, rc, soc;
2349 struct qpnp_vadc_result result;
2350 struct raw_soc_params raw;
2351
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002352 bms_stay_awake(&chip->soc_wake_source);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002353 mutex_lock(&chip->vbat_monitor_mutex);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002354 if (chip->vbat_monitor_params.state_request !=
2355 ADC_TM_HIGH_LOW_THR_DISABLE)
2356 qpnp_adc_tm_channel_measure(&chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002357 mutex_unlock(&chip->vbat_monitor_mutex);
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002358 if (chip->use_voltage_soc) {
2359 soc = calculate_soc_from_voltage(chip);
2360 } else {
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07002361 if (!chip->batfet_closed)
2362 qpnp_iadc_calibrate_for_trim(true);
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002363 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM,
2364 &result);
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002365 if (rc) {
2366 pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n",
2367 LR_MUX1_BATT_THERM, rc);
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002368 soc = chip->calculated_soc;
2369 } else {
2370 pr_debug("batt_temp phy = %lld meas = 0x%llx\n",
2371 result.physical,
2372 result.measurement);
2373 batt_temp = (int)result.physical;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002374
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002375 mutex_lock(&chip->last_ocv_uv_mutex);
2376 read_soc_params_raw(chip, &raw, batt_temp);
2377 soc = calculate_state_of_charge(chip, &raw, batt_temp);
2378 mutex_unlock(&chip->last_ocv_uv_mutex);
2379 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002380 }
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002381 bms_relax(&chip->soc_wake_source);
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002382 return soc;
2383}
2384
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002385static void recalculate_work(struct work_struct *work)
2386{
2387 struct qpnp_bms_chip *chip = container_of(work,
2388 struct qpnp_bms_chip,
2389 recalc_work);
2390
2391 recalculate_soc(chip);
2392}
2393
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002394static void calculate_soc_work(struct work_struct *work)
2395{
2396 struct qpnp_bms_chip *chip = container_of(work,
2397 struct qpnp_bms_chip,
2398 calculate_soc_delayed_work.work);
2399 int soc = recalculate_soc(chip);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002400
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002401 if (soc < chip->low_soc_calc_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002402 || wake_lock_active(&chip->low_voltage_wake_lock))
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002403 schedule_delayed_work(&chip->calculate_soc_delayed_work,
2404 round_jiffies_relative(msecs_to_jiffies
2405 (chip->low_soc_calculate_soc_ms)));
2406 else
2407 schedule_delayed_work(&chip->calculate_soc_delayed_work,
2408 round_jiffies_relative(msecs_to_jiffies
2409 (chip->calculate_soc_ms)));
2410}
2411
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002412static void configure_vbat_monitor_low(struct qpnp_bms_chip *chip)
2413{
2414 mutex_lock(&chip->vbat_monitor_mutex);
2415 if (chip->vbat_monitor_params.state_request
2416 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2417 /*
2418 * Battery is now around or below v_cutoff
2419 */
2420 pr_debug("battery entered cutoff range\n");
2421 if (!wake_lock_active(&chip->low_voltage_wake_lock)) {
2422 pr_debug("voltage low, holding wakelock\n");
2423 wake_lock(&chip->low_voltage_wake_lock);
2424 cancel_delayed_work_sync(
2425 &chip->calculate_soc_delayed_work);
2426 schedule_delayed_work(
2427 &chip->calculate_soc_delayed_work, 0);
2428 }
2429 chip->vbat_monitor_params.state_request =
2430 ADC_TM_HIGH_THR_ENABLE;
2431 chip->vbat_monitor_params.high_thr =
2432 (chip->low_voltage_threshold + VBATT_ERROR_MARGIN);
2433 pr_debug("set low thr to %d and high to %d\n",
2434 chip->vbat_monitor_params.low_thr,
2435 chip->vbat_monitor_params.high_thr);
2436 chip->vbat_monitor_params.low_thr = 0;
2437 } else if (chip->vbat_monitor_params.state_request
2438 == ADC_TM_LOW_THR_ENABLE) {
2439 /*
2440 * Battery is in normal operation range.
2441 */
2442 pr_debug("battery entered normal range\n");
2443 if (wake_lock_active(&chip->cv_wake_lock)) {
2444 wake_unlock(&chip->cv_wake_lock);
2445 pr_debug("releasing cv wake lock\n");
2446 }
2447 chip->in_cv_range = false;
2448 chip->vbat_monitor_params.state_request =
2449 ADC_TM_HIGH_LOW_THR_ENABLE;
2450 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2451 - VBATT_ERROR_MARGIN;
2452 chip->vbat_monitor_params.low_thr =
2453 chip->low_voltage_threshold;
2454 pr_debug("set low thr to %d and high to %d\n",
2455 chip->vbat_monitor_params.low_thr,
2456 chip->vbat_monitor_params.high_thr);
2457 }
2458 qpnp_adc_tm_channel_measure(&chip->vbat_monitor_params);
2459 mutex_unlock(&chip->vbat_monitor_mutex);
2460}
2461
2462#define CV_LOW_THRESHOLD_HYST_UV 100000
2463static void configure_vbat_monitor_high(struct qpnp_bms_chip *chip)
2464{
2465 mutex_lock(&chip->vbat_monitor_mutex);
2466 if (chip->vbat_monitor_params.state_request
2467 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2468 /*
2469 * Battery is around vddmax
2470 */
2471 pr_debug("battery entered vddmax range\n");
2472 chip->in_cv_range = true;
2473 if (!wake_lock_active(&chip->cv_wake_lock)) {
2474 wake_lock(&chip->cv_wake_lock);
2475 pr_debug("holding cv wake lock\n");
2476 }
2477 schedule_work(&chip->recalc_work);
2478 chip->vbat_monitor_params.state_request =
2479 ADC_TM_LOW_THR_ENABLE;
2480 chip->vbat_monitor_params.low_thr =
2481 (chip->max_voltage_uv - CV_LOW_THRESHOLD_HYST_UV);
2482 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv * 2;
2483 pr_debug("set low thr to %d and high to %d\n",
2484 chip->vbat_monitor_params.low_thr,
2485 chip->vbat_monitor_params.high_thr);
2486 } else if (chip->vbat_monitor_params.state_request
2487 == ADC_TM_HIGH_THR_ENABLE) {
2488 /*
2489 * Battery is in normal operation range.
2490 */
2491 pr_debug("battery entered normal range\n");
2492 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2493 pr_debug("voltage high, releasing wakelock\n");
2494 wake_unlock(&chip->low_voltage_wake_lock);
2495 }
2496 chip->vbat_monitor_params.state_request =
2497 ADC_TM_HIGH_LOW_THR_ENABLE;
2498 chip->vbat_monitor_params.high_thr =
2499 chip->max_voltage_uv - VBATT_ERROR_MARGIN;
2500 chip->vbat_monitor_params.low_thr =
2501 chip->low_voltage_threshold;
2502 pr_debug("set low thr to %d and high to %d\n",
2503 chip->vbat_monitor_params.low_thr,
2504 chip->vbat_monitor_params.high_thr);
2505 }
2506 qpnp_adc_tm_channel_measure(&chip->vbat_monitor_params);
2507 mutex_unlock(&chip->vbat_monitor_mutex);
2508}
2509
2510static void btm_notify_vbat(enum qpnp_tm_state state, void *ctx)
2511{
2512 struct qpnp_bms_chip *chip = ctx;
2513 int vbat_uv;
2514 struct qpnp_vadc_result result;
2515 int rc;
2516
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002517 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &result);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002518 pr_debug("vbat = %lld, raw = 0x%x\n", result.physical, result.adc_code);
2519
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002520 get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002521 pr_debug("vbat is at %d, state is at %d\n", vbat_uv, state);
2522
2523 if (state == ADC_TM_LOW_STATE) {
2524 pr_debug("low voltage btm notification triggered\n");
2525 if (vbat_uv - VBATT_ERROR_MARGIN
2526 < chip->vbat_monitor_params.low_thr) {
2527 configure_vbat_monitor_low(chip);
2528 } else {
2529 pr_debug("faulty btm trigger, discarding\n");
2530 qpnp_adc_tm_channel_measure(
2531 &chip->vbat_monitor_params);
2532 }
2533 } else if (state == ADC_TM_HIGH_STATE) {
2534 pr_debug("high voltage btm notification triggered\n");
2535 if (vbat_uv + VBATT_ERROR_MARGIN
2536 > chip->vbat_monitor_params.high_thr) {
2537 configure_vbat_monitor_high(chip);
2538 } else {
2539 pr_debug("faulty btm trigger, discarding\n");
2540 qpnp_adc_tm_channel_measure(
2541 &chip->vbat_monitor_params);
2542 }
2543 } else {
2544 pr_debug("unknown voltage notification state: %d\n", state);
2545 }
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002546 if (chip->bms_psy_registered)
Xiaozhe Shifa120db2013-06-06 15:57:19 -07002547 power_supply_changed(&chip->bms_psy);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002548}
2549
2550static int reset_vbat_monitoring(struct qpnp_bms_chip *chip)
2551{
2552 int rc;
2553
2554 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_DISABLE;
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002555
2556 rc = qpnp_adc_tm_disable_chan_meas(&chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002557 if (rc) {
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002558 pr_err("tm disable failed: %d\n", rc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002559 return rc;
2560 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002561 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2562 pr_debug("battery removed, releasing wakelock\n");
2563 wake_unlock(&chip->low_voltage_wake_lock);
2564 }
2565 if (chip->in_cv_range) {
2566 pr_debug("battery removed, removing in_cv_range state\n");
2567 chip->in_cv_range = false;
2568 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002569 return 0;
2570}
2571
2572static int setup_vbat_monitoring(struct qpnp_bms_chip *chip)
2573{
2574 int rc;
2575
2576 rc = qpnp_adc_tm_is_ready();
2577 if (rc) {
2578 pr_info("adc tm is not ready yet: %d, defer probe\n", rc);
2579 return -EPROBE_DEFER;
2580 }
2581
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002582 chip->vbat_monitor_params.low_thr = chip->low_voltage_threshold;
2583 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2584 - VBATT_ERROR_MARGIN;
2585 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_ENABLE;
2586 chip->vbat_monitor_params.channel = VBAT_SNS;
2587 chip->vbat_monitor_params.btm_ctx = (void *)chip;
2588 chip->vbat_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
2589 chip->vbat_monitor_params.threshold_notification = &btm_notify_vbat;
2590 pr_debug("set low thr to %d and high to %d\n",
2591 chip->vbat_monitor_params.low_thr,
2592 chip->vbat_monitor_params.high_thr);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002593
2594 if (!is_battery_present(chip)) {
2595 pr_debug("no battery inserted, do not enable vbat monitoring\n");
2596 chip->vbat_monitor_params.state_request =
2597 ADC_TM_HIGH_LOW_THR_DISABLE;
2598 } else {
2599 rc = qpnp_adc_tm_channel_measure(&chip->vbat_monitor_params);
2600 if (rc) {
2601 pr_err("tm setup failed: %d\n", rc);
2602 return rc;
2603 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002604 }
2605 pr_debug("setup complete\n");
2606 return 0;
2607}
2608
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302609static void readjust_fcc_table(struct qpnp_bms_chip *chip)
2610{
2611 struct single_row_lut *temp, *old;
2612 int i, fcc, ratio;
2613
2614 if (!chip->enable_fcc_learning)
2615 return;
2616
2617 if (!chip->fcc_temp_lut) {
2618 pr_err("The static fcc lut table is NULL\n");
2619 return;
2620 }
2621
2622 temp = kzalloc(sizeof(struct single_row_lut), GFP_KERNEL);
2623 if (!temp) {
2624 pr_err("Cannot allocate memory for adjusted fcc table\n");
2625 return;
2626 }
2627
2628 fcc = interpolate_fcc(chip->fcc_temp_lut, chip->fcc_new_batt_temp);
2629
2630 temp->cols = chip->fcc_temp_lut->cols;
2631 for (i = 0; i < chip->fcc_temp_lut->cols; i++) {
2632 temp->x[i] = chip->fcc_temp_lut->x[i];
2633 ratio = div_u64(chip->fcc_temp_lut->y[i] * 1000, fcc);
2634 temp->y[i] = (ratio * chip->fcc_new_mah);
2635 temp->y[i] /= 1000;
2636 }
2637
2638 old = chip->adjusted_fcc_temp_lut;
2639 chip->adjusted_fcc_temp_lut = temp;
2640 kfree(old);
2641}
2642
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302643static int read_fcc_data_from_backup(struct qpnp_bms_chip *chip)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302644{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302645 int rc, i;
2646 u8 fcc = 0, chgcyl = 0;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302647
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302648 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2649 rc = qpnp_read_wrapper(chip, &fcc,
2650 chip->base + BMS_FCC_BASE_REG + i, 1);
2651 rc |= qpnp_read_wrapper(chip, &chgcyl,
2652 chip->base + BMS_CHGCYL_BASE_REG + i, 1);
2653 if (rc) {
2654 pr_err("Unable to read FCC data\n");
2655 return rc;
2656 }
2657 if (fcc == 0 || (fcc == 0xFF && chgcyl == 0xFF)) {
2658 /* FCC invalid/not present */
2659 chip->fcc_learning_samples[i].fcc_new = 0;
2660 chip->fcc_learning_samples[i].chargecycles = 0;
2661 } else {
2662 /* valid FCC data */
2663 chip->fcc_sample_count++;
2664 chip->fcc_learning_samples[i].fcc_new =
2665 fcc * chip->fcc_resolution;
2666 chip->fcc_learning_samples[i].chargecycles =
2667 chgcyl * CHGCYL_RESOLUTION;
2668 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302669 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302670
2671 return 0;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302672}
2673
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302674static int discard_backup_fcc_data(struct qpnp_bms_chip *chip)
2675{
2676 int rc = 0, i;
2677 u8 temp_u8 = 0;
2678
2679 chip->fcc_sample_count = 0;
2680 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2681 rc = qpnp_write_wrapper(chip, &temp_u8,
2682 chip->base + BMS_FCC_BASE_REG + i, 1);
2683 rc |= qpnp_write_wrapper(chip, &temp_u8,
2684 chip->base + BMS_CHGCYL_BASE_REG + i, 1);
2685 if (rc) {
2686 pr_err("Unable to clear FCC data\n");
2687 return rc;
2688 }
2689 }
2690
2691 return 0;
2692}
2693
2694static void
2695average_fcc_samples_and_readjust_fcc_table(struct qpnp_bms_chip *chip)
2696{
2697 int i, temp_fcc_avg = 0, temp_fcc_delta = 0, new_fcc_avg = 0;
2698 struct fcc_sample *ft;
2699
2700 for (i = 0; i < chip->min_fcc_learning_samples; i++)
2701 temp_fcc_avg += chip->fcc_learning_samples[i].fcc_new;
2702
2703 temp_fcc_avg /= chip->min_fcc_learning_samples;
2704 temp_fcc_delta = div_u64(temp_fcc_avg * DELTA_FCC_PERCENT, 100);
2705
2706 /* fix the fcc if its an outlier i.e. > 5% of the average */
2707 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2708 ft = &chip->fcc_learning_samples[i];
2709 if (abs(ft->fcc_new - temp_fcc_avg) > temp_fcc_delta)
2710 new_fcc_avg += temp_fcc_avg;
2711 else
2712 new_fcc_avg += ft->fcc_new;
2713 }
2714 new_fcc_avg /= chip->min_fcc_learning_samples;
2715
2716 chip->fcc_new_mah = new_fcc_avg;
2717 chip->fcc_new_batt_temp = FCC_DEFAULT_TEMP;
2718 pr_info("FCC update: New fcc_mah=%d, fcc_batt_temp=%d\n",
2719 new_fcc_avg, FCC_DEFAULT_TEMP);
2720 readjust_fcc_table(chip);
2721}
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302722
2723static void backup_charge_cycle(struct qpnp_bms_chip *chip)
2724{
2725 int rc = 0;
2726
2727 if (chip->charge_increase >= 0) {
2728 rc = qpnp_write_wrapper(chip, &chip->charge_increase,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302729 chip->base + CHARGE_INCREASE_STORAGE, 1);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302730 if (rc)
2731 pr_err("Unable to backup charge_increase\n");
2732 }
2733
2734 if (chip->charge_cycles >= 0) {
2735 rc = qpnp_write_wrapper(chip, (u8 *)&chip->charge_cycles,
2736 chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
2737 if (rc)
2738 pr_err("Unable to backup charge_cycles\n");
2739 }
2740}
2741
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302742static bool chargecycles_in_range(struct qpnp_bms_chip *chip)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302743{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302744 int i, min_cycle, max_cycle, valid_range;
2745
2746 /* find the smallest and largest charge cycle */
2747 max_cycle = min_cycle = chip->fcc_learning_samples[0].chargecycles;
2748 for (i = 1; i < chip->min_fcc_learning_samples; i++) {
2749 if (min_cycle > chip->fcc_learning_samples[i].chargecycles)
2750 min_cycle = chip->fcc_learning_samples[i].chargecycles;
2751 if (max_cycle < chip->fcc_learning_samples[i].chargecycles)
2752 max_cycle = chip->fcc_learning_samples[i].chargecycles;
2753 }
2754
2755 /* check if chargecyles are in range to continue with FCC update */
2756 valid_range = DIV_ROUND_UP(VALID_FCC_CHGCYL_RANGE,
2757 CHGCYL_RESOLUTION) * CHGCYL_RESOLUTION;
2758 if (abs(max_cycle - min_cycle) > valid_range)
2759 return false;
2760
2761 return true;
2762}
2763
2764static int read_chgcycle_data_from_backup(struct qpnp_bms_chip *chip)
2765{
2766 int rc;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302767 uint16_t temp_u16 = 0;
2768 u8 temp_u8 = 0;
2769
2770 rc = qpnp_read_wrapper(chip, &temp_u8,
2771 chip->base + CHARGE_INCREASE_STORAGE, 1);
2772 if (!rc && temp_u8 != 0xFF)
2773 chip->charge_increase = temp_u8;
2774
2775 rc = qpnp_read_wrapper(chip, (u8 *)&temp_u16,
2776 chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
2777 if (!rc && temp_u16 != 0xFFFF)
2778 chip->charge_cycles = temp_u16;
2779
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302780 return rc;
2781}
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302782
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302783static void
2784attempt_learning_new_fcc(struct qpnp_bms_chip *chip)
2785{
2786 pr_debug("Total FCC sample count=%d\n", chip->fcc_sample_count);
2787
2788 /* update FCC if we have the required samples */
2789 if ((chip->fcc_sample_count == chip->min_fcc_learning_samples) &&
2790 chargecycles_in_range(chip))
2791 average_fcc_samples_and_readjust_fcc_table(chip);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302792}
2793
2794static int calculate_real_soc(struct qpnp_bms_chip *chip,
2795 int batt_temp, struct raw_soc_params *raw, int cc_uah)
2796{
2797 int fcc_uah, rc_uah;
2798
2799 fcc_uah = calculate_fcc(chip, batt_temp);
2800 rc_uah = calculate_ocv_charge(chip, raw, fcc_uah);
2801
2802 return ((rc_uah - cc_uah) * 100) / fcc_uah;
2803}
2804
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302805#define MAX_U8_VALUE ((u8)(~0U))
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302806
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302807static int backup_new_fcc(struct qpnp_bms_chip *chip, int fcc_mah,
2808 int chargecycles)
2809{
2810 int rc, min_cycle, i;
2811 u8 fcc_new, chgcyl, pos = 0;
2812 struct fcc_sample *ft;
2813
2814 if ((fcc_mah > (chip->fcc_resolution * MAX_U8_VALUE)) ||
2815 (chargecycles > (CHGCYL_RESOLUTION * MAX_U8_VALUE))) {
2816 pr_warn("FCC/Chgcyl beyond storage limit. FCC=%d, chgcyl=%d\n",
2817 fcc_mah, chargecycles);
2818 return -EINVAL;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302819 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302820
2821 if (chip->fcc_sample_count == chip->min_fcc_learning_samples) {
2822 /* search best location - oldest entry */
2823 min_cycle = chip->fcc_learning_samples[0].chargecycles;
2824 for (i = 1; i < chip->min_fcc_learning_samples; i++) {
2825 if (min_cycle >
2826 chip->fcc_learning_samples[i].chargecycles)
2827 pos = i;
2828 }
2829 } else {
2830 /* find an empty location */
2831 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2832 ft = &chip->fcc_learning_samples[i];
2833 if (ft->fcc_new == 0 || (ft->fcc_new == 0xFF &&
2834 ft->chargecycles == 0xFF)) {
2835 pos = i;
2836 break;
2837 }
2838 }
2839 chip->fcc_sample_count++;
2840 }
2841 chip->fcc_learning_samples[pos].fcc_new = fcc_mah;
2842 chip->fcc_learning_samples[pos].chargecycles = chargecycles;
2843
2844 fcc_new = DIV_ROUND_UP(fcc_mah, chip->fcc_resolution);
2845 rc = qpnp_write_wrapper(chip, (u8 *)&fcc_new,
2846 chip->base + BMS_FCC_BASE_REG + pos, 1);
2847 if (rc)
2848 return rc;
2849
2850 chgcyl = DIV_ROUND_UP(chargecycles, CHGCYL_RESOLUTION);
2851 rc = qpnp_write_wrapper(chip, (u8 *)&chgcyl,
2852 chip->base + BMS_CHGCYL_BASE_REG + pos, 1);
2853 if (rc)
2854 return rc;
2855
2856 pr_debug("Backup new FCC: fcc_new=%d, chargecycle=%d, pos=%d\n",
2857 fcc_new, chgcyl, pos);
2858
2859 return rc;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302860}
2861
2862static void update_fcc_learning_table(struct qpnp_bms_chip *chip,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302863 int new_fcc_uah, int chargecycles, int batt_temp)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302864{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302865 int rc, fcc_default, fcc_temp;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302866
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302867 /* convert the fcc at batt_temp to new fcc at FCC_DEFAULT_TEMP */
2868 fcc_default = calculate_fcc(chip, FCC_DEFAULT_TEMP) / 1000;
2869 fcc_temp = calculate_fcc(chip, batt_temp) / 1000;
2870 new_fcc_uah = (new_fcc_uah / fcc_temp) * fcc_default;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302871
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302872 rc = backup_new_fcc(chip, new_fcc_uah / 1000, chargecycles);
2873 if (rc) {
2874 pr_err("Unable to backup new FCC\n");
2875 return;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302876 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302877 /* check if FCC can be updated */
2878 attempt_learning_new_fcc(chip);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302879}
2880
2881static bool is_new_fcc_valid(int new_fcc_uah, int fcc_uah)
2882{
2883 if ((new_fcc_uah >= (fcc_uah / 2)) &&
2884 ((new_fcc_uah * 100) <= (fcc_uah * 105)))
2885 return true;
2886
2887 pr_debug("FCC rejected - not within valid limit\n");
2888 return false;
2889}
2890
2891static void fcc_learning_config(struct qpnp_bms_chip *chip, bool start)
2892{
2893 int rc, batt_temp;
2894 struct raw_soc_params raw;
2895 struct qpnp_vadc_result result;
2896 int fcc_uah, new_fcc_uah, delta_cc_uah, delta_soc;
2897
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002898 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302899 if (rc) {
2900 pr_err("Unable to read batt_temp\n");
2901 return;
2902 } else {
2903 batt_temp = (int)result.physical;
2904 }
2905
2906 rc = read_soc_params_raw(chip, &raw, batt_temp);
2907 if (rc) {
2908 pr_err("Unable to read CC, cannot update FCC\n");
2909 return;
2910 }
2911
2912 if (start) {
2913 chip->start_pc = interpolate_pc(chip->pc_temp_ocv_lut,
2914 batt_temp / 10, raw.last_good_ocv_uv / 1000);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07002915 chip->start_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302916 chip->start_real_soc = calculate_real_soc(chip,
2917 batt_temp, &raw, chip->start_cc_uah);
2918 pr_debug("start_pc=%d, start_cc=%d, start_soc=%d real_soc=%d\n",
2919 chip->start_pc, chip->start_cc_uah,
2920 chip->start_soc, chip->start_real_soc);
2921 } else {
Xiaozhe Shif3da8622013-06-10 14:50:56 -07002922 chip->end_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302923 delta_soc = 100 - chip->start_real_soc;
2924 delta_cc_uah = abs(chip->end_cc_uah - chip->start_cc_uah);
2925 new_fcc_uah = div_u64(delta_cc_uah * 100, delta_soc);
2926 fcc_uah = calculate_fcc(chip, batt_temp);
2927 pr_debug("start_soc=%d, start_pc=%d, start_real_soc=%d, start_cc=%d, end_cc=%d, new_fcc=%d\n",
2928 chip->start_soc, chip->start_pc, chip->start_real_soc,
2929 chip->start_cc_uah, chip->end_cc_uah, new_fcc_uah);
2930
2931 if (is_new_fcc_valid(new_fcc_uah, fcc_uah))
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302932 update_fcc_learning_table(chip, new_fcc_uah,
2933 chip->charge_cycles, batt_temp);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302934 }
2935}
2936
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07002937#define MAX_CAL_TRIES 200
2938#define MIN_CAL_UA 3000
2939static void batfet_open_work(struct work_struct *work)
2940{
2941 int i;
2942 int rc;
2943 int result_ua;
2944 u8 orig_delay, sample_delay;
2945 struct qpnp_bms_chip *chip = container_of(work,
2946 struct qpnp_bms_chip,
2947 batfet_open_work);
2948
2949 rc = qpnp_read_wrapper(chip, &orig_delay,
2950 chip->base + BMS1_S1_DELAY_CTL, 1);
2951
2952 sample_delay = 0x0;
2953 rc = qpnp_write_wrapper(chip, &sample_delay,
2954 chip->base + BMS1_S1_DELAY_CTL, 1);
2955
2956 /*
2957 * In certain PMICs there is a coupling issue which causes
2958 * bad calibration value that result in a huge battery current
2959 * even when the BATFET is open. Do continious calibrations until
2960 * we hit reasonable cal values which result in low battery current
2961 */
2962
2963 for (i = 0; (!chip->batfet_closed) && i < MAX_CAL_TRIES; i++) {
2964 rc = qpnp_iadc_calibrate_for_trim(false);
2965 /*
2966 * Wait 20mS after calibration and before reading battery
2967 * current. The BMS h/w uses calibration values in the
2968 * next sampling of vsense.
2969 */
2970 msleep(20);
2971 rc |= get_battery_current(chip, &result_ua);
2972 if (rc == 0 && abs(result_ua) <= MIN_CAL_UA) {
2973 pr_debug("good cal at %d attempt\n", i);
2974 break;
2975 }
2976 }
2977 pr_debug("batfet_closed = %d i = %d result_ua = %d\n",
2978 chip->batfet_closed, i, result_ua);
2979
2980 rc = qpnp_write_wrapper(chip, &orig_delay,
2981 chip->base + BMS1_S1_DELAY_CTL, 1);
2982}
2983
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07002984static void charging_began(struct qpnp_bms_chip *chip)
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002985{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07002986 mutex_lock(&chip->last_soc_mutex);
2987 chip->charge_start_tm_sec = 0;
2988 chip->catch_up_time_sec = 0;
2989 mutex_unlock(&chip->last_soc_mutex);
2990
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302991 chip->start_soc = report_state_of_charge(chip);
2992
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07002993 mutex_lock(&chip->last_ocv_uv_mutex);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302994 if (chip->enable_fcc_learning)
2995 fcc_learning_config(chip, true);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07002996 chip->soc_at_cv = -EINVAL;
2997 chip->prev_chg_soc = -EINVAL;
2998 mutex_unlock(&chip->last_ocv_uv_mutex);
2999}
3000
3001static void charging_ended(struct qpnp_bms_chip *chip)
3002{
3003 mutex_lock(&chip->last_soc_mutex);
3004 chip->charge_start_tm_sec = 0;
3005 chip->catch_up_time_sec = 0;
3006 mutex_unlock(&chip->last_soc_mutex);
3007
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303008 chip->end_soc = report_state_of_charge(chip);
3009
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003010 mutex_lock(&chip->last_ocv_uv_mutex);
3011 chip->soc_at_cv = -EINVAL;
3012 chip->prev_chg_soc = -EINVAL;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303013
3014 /* update the chargecycles */
3015 if (chip->end_soc > chip->start_soc) {
3016 chip->charge_increase += (chip->end_soc - chip->start_soc);
3017 if (chip->charge_increase > 100) {
3018 chip->charge_cycles++;
3019 chip->charge_increase = chip->charge_increase % 100;
3020 }
3021 if (chip->enable_fcc_learning)
3022 backup_charge_cycle(chip);
3023 }
3024
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003025 if (get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL) {
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303026 if (chip->enable_fcc_learning &&
3027 (chip->start_soc <= chip->min_fcc_learning_soc) &&
3028 (chip->start_pc <= chip->min_fcc_ocv_pc))
3029 fcc_learning_config(chip, false);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003030 chip->done_charging = true;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003031 chip->last_soc_invalid = true;
Xiaozhe Shicc48e992013-05-28 16:42:24 -07003032 } else if (chip->charging_adjusted_ocv > 0) {
3033 pr_debug("Charging stopped before full, adjusted OCV = %d\n",
3034 chip->charging_adjusted_ocv);
3035 chip->last_ocv_uv = chip->charging_adjusted_ocv;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003036 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303037
Xiaozhe Shicc48e992013-05-28 16:42:24 -07003038 chip->charging_adjusted_ocv = -EINVAL;
3039
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003040 mutex_unlock(&chip->last_ocv_uv_mutex);
3041}
3042
3043static void battery_status_check(struct qpnp_bms_chip *chip)
3044{
3045 int status = get_battery_status(chip);
3046
3047 if (chip->battery_status != status) {
3048 if (status == POWER_SUPPLY_STATUS_CHARGING) {
3049 pr_debug("charging started\n");
3050 charging_began(chip);
3051 } else if (chip->battery_status
3052 == POWER_SUPPLY_STATUS_CHARGING) {
3053 pr_debug("charging ended\n");
3054 charging_ended(chip);
3055 }
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003056
3057 if (status == POWER_SUPPLY_STATUS_FULL) {
3058 pr_debug("battery full\n");
3059 enable_bms_irq(&chip->ocv_thr_irq);
3060 enable_bms_irq(&chip->sw_cc_thr_irq);
3061 } else if (chip->battery_status
3062 == POWER_SUPPLY_STATUS_FULL) {
3063 pr_debug("battery not full any more\n");
3064 disable_bms_irq(&chip->ocv_thr_irq);
3065 disable_bms_irq(&chip->sw_cc_thr_irq);
3066 }
3067
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003068 chip->battery_status = status;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003069 /* battery charge status has changed, so force a soc
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003070 * recalculation to update the SoC */
3071 schedule_work(&chip->recalc_work);
3072 }
3073}
3074
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003075#define CALIB_WRKARND_DIG_MAJOR_MAX 0x03
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003076static void batfet_status_check(struct qpnp_bms_chip *chip)
3077{
3078 bool batfet_closed;
3079
3080 if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX)
3081 return;
3082
3083 batfet_closed = is_batfet_closed(chip);
3084 if (chip->batfet_closed != batfet_closed) {
3085 chip->batfet_closed = batfet_closed;
3086 if (batfet_closed == false) {
3087 /* batfet opened */
3088 schedule_work(&chip->batfet_open_work);
3089 qpnp_iadc_skip_calibration();
3090 } else {
3091 /* batfet closed */
3092 qpnp_iadc_calibrate_for_trim(true);
3093 qpnp_iadc_resume_calibration();
3094 }
3095 }
3096}
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003097
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003098static void battery_insertion_check(struct qpnp_bms_chip *chip)
3099{
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003100 bool present = is_battery_present(chip);
3101
3102 mutex_lock(&chip->vbat_monitor_mutex);
3103 if (chip->battery_present != present) {
3104 if (chip->battery_present != -EINVAL) {
3105 if (present) {
3106 setup_vbat_monitoring(chip);
3107 chip->new_battery = true;
3108 } else {
3109 reset_vbat_monitoring(chip);
3110 }
3111 }
3112 chip->battery_present = present;
3113 /* a new battery was inserted or removed, so force a soc
3114 * recalculation to update the SoC */
3115 schedule_work(&chip->recalc_work);
3116 }
3117 mutex_unlock(&chip->vbat_monitor_mutex);
3118}
3119
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003120/* Returns capacity as a SoC percentage between 0 and 100 */
3121static int get_prop_bms_capacity(struct qpnp_bms_chip *chip)
3122{
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08003123 return report_state_of_charge(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003124}
3125
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003126static void qpnp_bms_external_power_changed(struct power_supply *psy)
3127{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003128 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
3129 bms_psy);
3130
3131 battery_insertion_check(chip);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003132 batfet_status_check(chip);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003133 battery_status_check(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003134}
3135
3136static int qpnp_bms_power_get_property(struct power_supply *psy,
3137 enum power_supply_property psp,
3138 union power_supply_propval *val)
3139{
3140 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
3141 bms_psy);
3142
3143 switch (psp) {
3144 case POWER_SUPPLY_PROP_CAPACITY:
3145 val->intval = get_prop_bms_capacity(chip);
3146 break;
3147 case POWER_SUPPLY_PROP_CURRENT_NOW:
3148 val->intval = get_prop_bms_current_now(chip);
3149 break;
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07003150 case POWER_SUPPLY_PROP_RESISTANCE:
3151 val->intval = get_prop_bms_batt_resistance(chip);
3152 break;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07003153 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
3154 val->intval = get_prop_bms_charge_counter(chip);
3155 break;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003156 case POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW:
3157 val->intval = get_prop_bms_charge_counter_shadow(chip);
3158 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003159 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
3160 val->intval = get_prop_bms_charge_full_design(chip);
3161 break;
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05303162 case POWER_SUPPLY_PROP_CHARGE_FULL:
3163 val->intval = get_prop_bms_charge_full(chip);
3164 break;
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05303165 case POWER_SUPPLY_PROP_CYCLE_COUNT:
3166 val->intval = chip->charge_cycles;
3167 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003168 default:
3169 return -EINVAL;
3170 }
3171 return 0;
3172}
3173
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003174#define OCV_USE_LIMIT_EN BIT(7)
3175static int set_ocv_voltage_thresholds(struct qpnp_bms_chip *chip,
3176 int low_voltage_threshold,
3177 int high_voltage_threshold)
3178{
3179 uint16_t low_voltage_raw, high_voltage_raw;
3180 int rc;
3181
3182 low_voltage_raw = convert_vbatt_uv_to_raw(chip,
3183 low_voltage_threshold);
3184 high_voltage_raw = convert_vbatt_uv_to_raw(chip,
3185 high_voltage_threshold);
3186 rc = qpnp_write_wrapper(chip, (u8 *)&low_voltage_raw,
3187 chip->base + BMS1_OCV_USE_LOW_LIMIT_THR0, 2);
3188 if (rc) {
3189 pr_err("Failed to set ocv low voltage threshold: %d\n", rc);
3190 return rc;
3191 }
3192 rc = qpnp_write_wrapper(chip, (u8 *)&high_voltage_raw,
3193 chip->base + BMS1_OCV_USE_HIGH_LIMIT_THR0, 2);
3194 if (rc) {
3195 pr_err("Failed to set ocv high voltage threshold: %d\n", rc);
3196 return rc;
3197 }
3198 rc = qpnp_masked_write(chip, BMS1_OCV_USE_LIMIT_CTL,
3199 OCV_USE_LIMIT_EN, OCV_USE_LIMIT_EN);
3200 if (rc) {
3201 pr_err("Failed to enabled ocv voltage thresholds: %d\n", rc);
3202 return rc;
3203 }
3204 pr_debug("ocv low threshold set to %d uv or 0x%x raw\n",
3205 low_voltage_threshold, low_voltage_raw);
3206 pr_debug("ocv high threshold set to %d uv or 0x%x raw\n",
3207 high_voltage_threshold, high_voltage_raw);
3208 return 0;
3209}
3210
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003211static void read_shutdown_soc_and_iavg(struct qpnp_bms_chip *chip)
3212{
3213 int rc;
3214 u8 temp;
3215
3216 if (chip->ignore_shutdown_soc) {
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08003217 chip->shutdown_soc_invalid = true;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003218 chip->shutdown_soc = 0;
3219 chip->shutdown_iavg_ma = 0;
3220 } else {
3221 rc = qpnp_read_wrapper(chip, &temp,
3222 chip->base + IAVG_STORAGE_REG, 1);
3223 if (rc) {
3224 pr_err("failed to read addr = %d %d assuming %d\n",
3225 chip->base + IAVG_STORAGE_REG, rc,
3226 IAVG_START);
3227 chip->shutdown_iavg_ma = IAVG_START;
Xiaozhe Shif5f966d2013-02-19 14:23:11 -08003228 } else if (temp == IAVG_INVALID) {
3229 pr_err("invalid iavg read from BMS1_DATA_REG_1, using %d\n",
3230 IAVG_START);
3231 chip->shutdown_iavg_ma = IAVG_START;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003232 } else {
3233 if (temp == 0) {
3234 chip->shutdown_iavg_ma = IAVG_START;
3235 } else {
3236 chip->shutdown_iavg_ma = IAVG_START
3237 + IAVG_STEP_SIZE_MA * (temp + 1);
3238 }
3239 }
3240
3241 rc = qpnp_read_wrapper(chip, &temp,
3242 chip->base + SOC_STORAGE_REG, 1);
3243 if (rc) {
3244 pr_err("failed to read addr = %d %d\n",
3245 chip->base + SOC_STORAGE_REG, rc);
3246 } else {
3247 chip->shutdown_soc = temp;
3248
Xiaozhe Shic7cbd052013-03-29 12:03:11 -07003249 if (chip->shutdown_soc == SOC_INVALID) {
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003250 pr_debug("No shutdown soc available\n");
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08003251 chip->shutdown_soc_invalid = true;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003252 chip->shutdown_iavg_ma = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003253 }
3254 }
3255 }
3256
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303257 /* read the SOC storage to determine if there was a battery removal */
3258 rc = qpnp_read_wrapper(chip, &temp, chip->base + SOC_STORAGE_REG, 1);
3259 if (!rc) {
3260 if (temp == SOC_INVALID)
3261 chip->battery_removed = true;
3262 }
3263
3264
3265 pr_debug("shutdown_soc = %d shutdown_iavg = %d shutdown_soc_invalid = %d, battery_removed = %d\n",
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003266 chip->shutdown_soc,
3267 chip->shutdown_iavg_ma,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303268 chip->shutdown_soc_invalid,
3269 chip->battery_removed);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003270}
3271
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003272static irqreturn_t bms_ocv_thr_irq_handler(int irq, void *_chip)
3273{
3274 struct qpnp_bms_chip *chip = _chip;
3275
3276 pr_debug("ocv_thr irq triggered\n");
3277 bms_stay_awake(&chip->soc_wake_source);
3278 schedule_work(&chip->recalc_work);
3279 return IRQ_HANDLED;
3280}
3281
3282static irqreturn_t bms_sw_cc_thr_irq_handler(int irq, void *_chip)
3283{
3284 struct qpnp_bms_chip *chip = _chip;
3285
3286 pr_debug("sw_cc_thr irq triggered\n");
3287 bms_stay_awake(&chip->soc_wake_source);
3288 schedule_work(&chip->recalc_work);
3289 return IRQ_HANDLED;
3290}
3291
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003292static int64_t read_battery_id(struct qpnp_bms_chip *chip)
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003293{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003294 int rc;
3295 struct qpnp_vadc_result result;
3296
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003297 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX2_BAT_ID, &result);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003298 if (rc) {
3299 pr_err("error reading batt id channel = %d, rc = %d\n",
3300 LR_MUX2_BAT_ID, rc);
3301 return rc;
3302 }
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003303
3304 return result.physical;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003305}
3306
3307static int set_battery_data(struct qpnp_bms_chip *chip)
3308{
3309 int64_t battery_id;
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003310 int rc;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003311 struct bms_battery_data *batt_data;
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003312 struct device_node *node;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003313
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003314 if (chip->batt_type == BATT_DESAY) {
3315 batt_data = &desay_5200_data;
3316 } else if (chip->batt_type == BATT_PALLADIUM) {
3317 batt_data = &palladium_1500_data;
3318 } else if (chip->batt_type == BATT_OEM) {
3319 batt_data = &oem_batt_data;
Wu Fenglin2ac88aa2013-04-25 12:43:40 +08003320 } else if (chip->batt_type == BATT_QRD_4V35_2000MAH) {
3321 batt_data = &QRD_4v35_2000mAh_data;
tingtingf50326f2013-06-05 15:07:24 +08003322 } else if (chip->batt_type == BATT_QRD_4V2_1300MAH) {
3323 batt_data = &qrd_4v2_1300mah_data;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003324 } else {
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003325 battery_id = read_battery_id(chip);
3326 if (battery_id < 0) {
3327 pr_err("cannot read battery id err = %lld\n",
3328 battery_id);
3329 return battery_id;
3330 }
3331
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003332 node = of_find_node_by_name(chip->spmi->dev.of_node,
3333 "qcom,battery-data");
3334 if (node) {
3335 batt_data = kzalloc(sizeof(struct bms_battery_data),
3336 GFP_KERNEL);
3337 batt_data->fcc_temp_lut = kzalloc(
3338 sizeof(struct single_row_lut),
3339 GFP_KERNEL);
3340 batt_data->pc_temp_ocv_lut = kzalloc(
3341 sizeof(struct pc_temp_ocv_lut),
3342 GFP_KERNEL);
3343 batt_data->rbatt_sf_lut = kzalloc(
3344 sizeof(struct sf_lut), GFP_KERNEL);
3345
3346 rc = of_batterydata_read_data(node,
3347 batt_data, battery_id);
3348 if (rc) {
3349 pr_err("battery data load failed, using palladium 1500\n");
3350 kfree(batt_data->fcc_temp_lut);
3351 kfree(batt_data->pc_temp_ocv_lut);
3352 kfree(batt_data->rbatt_sf_lut);
3353 kfree(batt_data);
3354 batt_data = &palladium_1500_data;
3355 }
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003356 } else {
3357 pr_warn("invalid battid, palladium 1500 assumed batt_id %llx\n",
3358 battery_id);
3359 batt_data = &palladium_1500_data;
3360 }
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003361 }
3362
Xiaozhe Shi976618f2013-04-30 10:49:30 -07003363 chip->fcc_mah = batt_data->fcc;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003364 chip->fcc_temp_lut = batt_data->fcc_temp_lut;
3365 chip->fcc_sf_lut = batt_data->fcc_sf_lut;
3366 chip->pc_temp_ocv_lut = batt_data->pc_temp_ocv_lut;
3367 chip->pc_sf_lut = batt_data->pc_sf_lut;
3368 chip->rbatt_sf_lut = batt_data->rbatt_sf_lut;
3369 chip->default_rbatt_mohm = batt_data->default_rbatt_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07003370 chip->rbatt_capacitive_mohm = batt_data->rbatt_capacitive_mohm;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07003371 chip->flat_ocv_threshold_uv = batt_data->flat_ocv_threshold_uv;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003372
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003373 /* Override battery properties if specified in the battery profile */
3374 if (batt_data->max_voltage_uv >= 0)
3375 chip->max_voltage_uv = batt_data->max_voltage_uv;
3376 if (batt_data->cutoff_uv >= 0)
3377 chip->v_cutoff_uv = batt_data->cutoff_uv;
3378 if (batt_data->iterm_ua >= 0)
3379 chip->chg_term_ua = batt_data->iterm_ua;
3380
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003381 if (chip->pc_temp_ocv_lut == NULL) {
3382 pr_err("temp ocv lut table is NULL\n");
3383 return -EINVAL;
3384 }
3385 return 0;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003386}
3387
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003388#define SPMI_PROP_READ(chip_prop, qpnp_spmi_property, retval) \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003389do { \
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003390 if (retval) \
3391 break; \
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003392 retval = of_property_read_u32(chip->spmi->dev.of_node, \
Xiaozhe Shi9bd24622013-01-23 15:54:54 -08003393 "qcom," qpnp_spmi_property, \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003394 &chip->chip_prop); \
3395 if (retval) { \
3396 pr_err("Error reading " #qpnp_spmi_property \
3397 " property %d\n", rc); \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003398 } \
3399} while (0)
3400
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303401#define SPMI_PROP_READ_BOOL(chip_prop, qpnp_spmi_property) \
3402do { \
3403 chip->chip_prop = of_property_read_bool(chip->spmi->dev.of_node,\
3404 "qcom," qpnp_spmi_property); \
3405} while (0)
3406
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003407static inline int bms_read_properties(struct qpnp_bms_chip *chip)
3408{
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003409 int rc = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003410
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003411 SPMI_PROP_READ(r_sense_uohm, "r-sense-uohm", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003412 SPMI_PROP_READ(v_cutoff_uv, "v-cutoff-uv", rc);
3413 SPMI_PROP_READ(max_voltage_uv, "max-voltage-uv", rc);
3414 SPMI_PROP_READ(r_conn_mohm, "r-conn-mohm", rc);
3415 SPMI_PROP_READ(chg_term_ua, "chg-term-ua", rc);
3416 SPMI_PROP_READ(shutdown_soc_valid_limit,
3417 "shutdown-soc-valid-limit", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003418 SPMI_PROP_READ(adjust_soc_low_threshold,
3419 "adjust-soc-low-threshold", rc);
3420 SPMI_PROP_READ(batt_type, "batt-type", rc);
3421 SPMI_PROP_READ(low_soc_calc_threshold,
3422 "low-soc-calculate-soc-threshold", rc);
3423 SPMI_PROP_READ(low_soc_calculate_soc_ms,
3424 "low-soc-calculate-soc-ms", rc);
3425 SPMI_PROP_READ(calculate_soc_ms, "calculate-soc-ms", rc);
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07003426 SPMI_PROP_READ(high_ocv_correction_limit_uv,
3427 "high-ocv-correction-limit-uv", rc);
3428 SPMI_PROP_READ(low_ocv_correction_limit_uv,
3429 "low-ocv-correction-limit-uv", rc);
3430 SPMI_PROP_READ(hold_soc_est,
3431 "hold-soc-est", rc);
3432 SPMI_PROP_READ(ocv_high_threshold_uv,
3433 "ocv-voltage-high-threshold-uv", rc);
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003434 SPMI_PROP_READ(ocv_low_threshold_uv,
3435 "ocv-voltage-low-threshold-uv", rc);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08003436 SPMI_PROP_READ(low_voltage_threshold, "low-voltage-threshold", rc);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003437 SPMI_PROP_READ(temperature_margin, "tm-temp-margin", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003438
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003439 chip->use_external_rsense = of_property_read_bool(
3440 chip->spmi->dev.of_node,
3441 "qcom,use-external-rsense");
3442 chip->ignore_shutdown_soc = of_property_read_bool(
3443 chip->spmi->dev.of_node,
3444 "qcom,ignore-shutdown-soc");
3445 chip->use_voltage_soc = of_property_read_bool(chip->spmi->dev.of_node,
3446 "qcom,use-voltage-soc");
3447 chip->use_ocv_thresholds = of_property_read_bool(
3448 chip->spmi->dev.of_node,
3449 "qcom,use-ocv-thresholds");
3450
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003451 if (chip->adjust_soc_low_threshold >= 45)
3452 chip->adjust_soc_low_threshold = 45;
3453
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303454 SPMI_PROP_READ_BOOL(enable_fcc_learning, "enable-fcc-learning");
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303455 if (chip->enable_fcc_learning) {
3456 SPMI_PROP_READ(min_fcc_learning_soc,
3457 "min-fcc-learning-soc", rc);
3458 SPMI_PROP_READ(min_fcc_ocv_pc,
3459 "min-fcc-ocv-pc", rc);
3460 SPMI_PROP_READ(min_fcc_learning_samples,
3461 "min-fcc-learning-samples", rc);
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303462 SPMI_PROP_READ(fcc_resolution,
3463 "fcc-resolution", rc);
3464 if (chip->min_fcc_learning_samples > MAX_FCC_CYCLES)
3465 chip->min_fcc_learning_samples = MAX_FCC_CYCLES;
3466 chip->fcc_learning_samples = devm_kzalloc(&chip->spmi->dev,
3467 (sizeof(struct fcc_sample) *
3468 chip->min_fcc_learning_samples), GFP_KERNEL);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303469 pr_debug("min-fcc-soc=%d, min-fcc-pc=%d, min-fcc-cycles=%d\n",
3470 chip->min_fcc_learning_soc, chip->min_fcc_ocv_pc,
3471 chip->min_fcc_learning_samples);
3472 }
3473
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003474 if (rc) {
3475 pr_err("Missing required properties.\n");
3476 return rc;
3477 }
3478
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003479 pr_debug("dts data: r_sense_uohm:%d, v_cutoff_uv:%d, max_v:%d\n",
3480 chip->r_sense_uohm, chip->v_cutoff_uv,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003481 chip->max_voltage_uv);
3482 pr_debug("r_conn:%d, shutdown_soc: %d, adjust_soc_low:%d\n",
3483 chip->r_conn_mohm, chip->shutdown_soc_valid_limit,
3484 chip->adjust_soc_low_threshold);
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07003485 pr_debug("chg_term_ua:%d, batt_type:%d\n",
3486 chip->chg_term_ua,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003487 chip->batt_type);
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08003488 pr_debug("ignore_shutdown_soc:%d, use_voltage_soc:%d\n",
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08003489 chip->ignore_shutdown_soc, chip->use_voltage_soc);
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003490 pr_debug("use external rsense: %d\n", chip->use_external_rsense);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003491 return 0;
3492}
3493
3494static inline void bms_initialize_constants(struct qpnp_bms_chip *chip)
3495{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003496 chip->prev_pc_unusable = -EINVAL;
3497 chip->soc_at_cv = -EINVAL;
3498 chip->calculated_soc = -EINVAL;
Xiaozhe Shie118c692012-09-24 15:17:43 -07003499 chip->last_soc = -EINVAL;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07003500 chip->last_soc_est = -EINVAL;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003501 chip->battery_present = -EINVAL;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003502 chip->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08003503 chip->last_cc_uah = INT_MIN;
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08003504 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
3505 chip->prev_last_good_ocv_raw = OCV_RAW_UNINITIALIZED;
Xiaozhe Shie118c692012-09-24 15:17:43 -07003506 chip->first_time_calc_soc = 1;
3507 chip->first_time_calc_uuc = 1;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003508}
3509
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003510#define SPMI_FIND_IRQ(chip, irq_name) \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003511do { \
3512 chip->irq_name##_irq.irq = spmi_get_irq_byname(chip->spmi, \
3513 resource, #irq_name); \
3514 if (chip->irq_name##_irq.irq < 0) { \
3515 pr_err("Unable to get " #irq_name " irq\n"); \
3516 return -ENXIO; \
3517 } \
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003518} while (0)
3519
3520static int bms_find_irqs(struct qpnp_bms_chip *chip,
3521 struct spmi_resource *resource)
3522{
3523 SPMI_FIND_IRQ(chip, sw_cc_thr);
3524 SPMI_FIND_IRQ(chip, ocv_thr);
3525 return 0;
3526}
3527
3528#define SPMI_REQUEST_IRQ(chip, rc, irq_name) \
3529do { \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003530 rc = devm_request_irq(chip->dev, chip->irq_name##_irq.irq, \
3531 bms_##irq_name##_irq_handler, \
3532 IRQF_TRIGGER_RISING, #irq_name, chip); \
3533 if (rc < 0) { \
3534 pr_err("Unable to request " #irq_name " irq: %d\n", rc);\
3535 return -ENXIO; \
3536 } \
3537} while (0)
3538
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003539static int bms_request_irqs(struct qpnp_bms_chip *chip)
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003540{
3541 int rc;
3542
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003543 SPMI_REQUEST_IRQ(chip, rc, sw_cc_thr);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003544 enable_irq_wake(chip->sw_cc_thr_irq.irq);
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003545 SPMI_REQUEST_IRQ(chip, rc, ocv_thr);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003546 enable_irq_wake(chip->ocv_thr_irq.irq);
3547 return 0;
3548}
3549
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003550#define REG_OFFSET_PERP_TYPE 0x04
3551#define REG_OFFSET_PERP_SUBTYPE 0x05
3552#define BMS_BMS_TYPE 0xD
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003553#define BMS_BMS1_SUBTYPE 0x1
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003554#define BMS_IADC_TYPE 0x8
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003555#define BMS_IADC1_SUBTYPE 0x3
3556#define BMS_IADC2_SUBTYPE 0x5
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003557
3558static int register_spmi(struct qpnp_bms_chip *chip, struct spmi_device *spmi)
3559{
3560 struct spmi_resource *spmi_resource;
3561 struct resource *resource;
3562 int rc;
3563 u8 type, subtype;
3564
3565 chip->dev = &(spmi->dev);
3566 chip->spmi = spmi;
3567
3568 spmi_for_each_container_dev(spmi_resource, spmi) {
3569 if (!spmi_resource) {
3570 pr_err("qpnp_bms: spmi resource absent\n");
3571 return -ENXIO;
3572 }
3573
3574 resource = spmi_get_resource(spmi, spmi_resource,
3575 IORESOURCE_MEM, 0);
3576 if (!(resource && resource->start)) {
3577 pr_err("node %s IO resource absent!\n",
3578 spmi->dev.of_node->full_name);
3579 return -ENXIO;
3580 }
3581
3582 rc = qpnp_read_wrapper(chip, &type,
3583 resource->start + REG_OFFSET_PERP_TYPE, 1);
3584 if (rc) {
3585 pr_err("Peripheral type read failed rc=%d\n", rc);
3586 return rc;
3587 }
3588 rc = qpnp_read_wrapper(chip, &subtype,
3589 resource->start + REG_OFFSET_PERP_SUBTYPE, 1);
3590 if (rc) {
3591 pr_err("Peripheral subtype read failed rc=%d\n", rc);
3592 return rc;
3593 }
3594
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003595 if (type == BMS_BMS_TYPE && subtype == BMS_BMS1_SUBTYPE) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003596 chip->base = resource->start;
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003597 rc = bms_find_irqs(chip, spmi_resource);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003598 if (rc) {
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003599 pr_err("Could not find irqs\n");
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003600 return rc;
3601 }
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003602 } else if (type == BMS_IADC_TYPE
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003603 && (subtype == BMS_IADC1_SUBTYPE
3604 || subtype == BMS_IADC2_SUBTYPE)) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003605 chip->iadc_base = resource->start;
3606 } else {
3607 pr_err("Invalid peripheral start=0x%x type=0x%x, subtype=0x%x\n",
3608 resource->start, type, subtype);
3609 }
3610 }
3611
3612 if (chip->base == 0) {
3613 dev_err(&spmi->dev, "BMS peripheral was not registered\n");
3614 return -EINVAL;
3615 }
3616 if (chip->iadc_base == 0) {
3617 dev_err(&spmi->dev, "BMS_IADC peripheral was not registered\n");
3618 return -EINVAL;
3619 }
3620
3621 return 0;
3622}
3623
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003624#define ADC_CH_SEL_MASK 0x7
3625#define ADC_INT_RSNSN_CTL_MASK 0x3
3626#define ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE 0x2
3627#define FAST_AVG_EN_MASK 0x80
3628#define FAST_AVG_EN_VALUE_EXT_RSENSE 0x80
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003629static int read_iadc_channel_select(struct qpnp_bms_chip *chip)
3630{
3631 u8 iadc_channel_select;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08003632 int32_t rds_rsense_nohm;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003633 int rc;
3634
3635 rc = qpnp_read_wrapper(chip, &iadc_channel_select,
3636 chip->iadc_base + IADC1_BMS_ADC_CH_SEL_CTL, 1);
3637 if (rc) {
3638 pr_err("Error reading bms_iadc channel register %d\n", rc);
3639 return rc;
3640 }
3641
3642 iadc_channel_select &= ADC_CH_SEL_MASK;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08003643 if (iadc_channel_select != EXTERNAL_RSENSE
3644 && iadc_channel_select != INTERNAL_RSENSE) {
3645 pr_err("IADC1_BMS_IADC configured incorrectly. Selected channel = %d\n",
3646 iadc_channel_select);
3647 return -EINVAL;
3648 }
3649
3650 if (chip->use_external_rsense) {
3651 pr_debug("External rsense selected\n");
3652 if (iadc_channel_select == INTERNAL_RSENSE) {
3653 pr_debug("Internal rsense detected; Changing rsense to external\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003654 rc = qpnp_masked_write_iadc(chip,
3655 IADC1_BMS_ADC_CH_SEL_CTL,
3656 ADC_CH_SEL_MASK,
3657 EXTERNAL_RSENSE);
3658 if (rc) {
3659 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
3660 IADC1_BMS_ADC_CH_SEL_CTL,
3661 EXTERNAL_RSENSE, rc);
3662 return rc;
3663 }
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003664 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07003665 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003666 chip->software_shdw_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003667 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08003668 } else {
3669 pr_debug("Internal rsense selected\n");
3670 if (iadc_channel_select == EXTERNAL_RSENSE) {
3671 pr_debug("External rsense detected; Changing rsense to internal\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003672 rc = qpnp_masked_write_iadc(chip,
3673 IADC1_BMS_ADC_CH_SEL_CTL,
3674 ADC_CH_SEL_MASK,
3675 INTERNAL_RSENSE);
3676 if (rc) {
3677 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
3678 IADC1_BMS_ADC_CH_SEL_CTL,
3679 INTERNAL_RSENSE, rc);
3680 return rc;
3681 }
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003682 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
3683 chip->software_shdw_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003684 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08003685
3686 rc = qpnp_iadc_get_rsense(&rds_rsense_nohm);
3687 if (rc) {
3688 pr_err("Unable to read RDS resistance value from IADC; rc = %d\n",
3689 rc);
3690 return rc;
3691 }
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003692 chip->r_sense_uohm = rds_rsense_nohm/1000;
3693 pr_debug("rds_rsense = %d nOhm, saved as %d uOhm\n",
3694 rds_rsense_nohm, chip->r_sense_uohm);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003695 }
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003696 /* prevent shorting of leads by IADC_BMS when external Rsense is used */
3697 if (chip->use_external_rsense) {
3698 if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX) {
3699 rc = qpnp_masked_write_iadc(chip,
3700 IADC1_BMS_ADC_INT_RSNSN_CTL,
3701 ADC_INT_RSNSN_CTL_MASK,
3702 ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE);
3703 if (rc) {
3704 pr_err("Unable to set batfet config %x to %x: %d\n",
3705 IADC1_BMS_ADC_INT_RSNSN_CTL,
3706 ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE, rc);
3707 return rc;
3708 }
3709 } else {
3710 /* In older PMICS use FAST_AVG_EN register bit 7 */
3711 rc = qpnp_masked_write_iadc(chip,
3712 IADC1_BMS_FAST_AVG_EN,
3713 FAST_AVG_EN_MASK,
3714 FAST_AVG_EN_VALUE_EXT_RSENSE);
3715 if (rc) {
3716 pr_err("Unable to set batfet config %x to %x: %d\n",
3717 IADC1_BMS_FAST_AVG_EN,
3718 FAST_AVG_EN_VALUE_EXT_RSENSE, rc);
3719 return rc;
3720 }
3721 }
3722 }
3723
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003724 return 0;
3725}
3726
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003727static int refresh_die_temp_monitor(struct qpnp_bms_chip *chip)
3728{
3729 struct qpnp_vadc_result result;
3730 int rc;
3731
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003732 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003733
3734 pr_debug("low = %lld, high = %lld\n",
3735 result.physical - chip->temperature_margin,
3736 result.physical + chip->temperature_margin);
3737 chip->die_temp_monitor_params.high_temp = result.physical
3738 + chip->temperature_margin;
3739 chip->die_temp_monitor_params.low_temp = result.physical
3740 - chip->temperature_margin;
3741 chip->die_temp_monitor_params.state_request =
3742 ADC_TM_HIGH_LOW_THR_ENABLE;
3743 return qpnp_adc_tm_channel_measure(&chip->die_temp_monitor_params);
3744}
3745
3746static void btm_notify_die_temp(enum qpnp_tm_state state, void *ctx)
3747{
3748 struct qpnp_bms_chip *chip = ctx;
3749 struct qpnp_vadc_result result;
3750 int rc;
3751
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003752 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003753
3754 if (state == ADC_TM_LOW_STATE)
3755 pr_debug("low state triggered\n");
3756 else if (state == ADC_TM_HIGH_STATE)
3757 pr_debug("high state triggered\n");
3758 pr_debug("die temp = %lld, raw = 0x%x\n",
3759 result.physical, result.adc_code);
3760 schedule_work(&chip->recalc_work);
3761 refresh_die_temp_monitor(chip);
3762}
3763
3764static int setup_die_temp_monitoring(struct qpnp_bms_chip *chip)
3765{
3766 int rc = qpnp_adc_tm_is_ready();
3767 if (rc) {
3768 pr_info("adc tm is not ready yet: %d, defer probe\n", rc);
3769 return -EPROBE_DEFER;
3770 }
3771 chip->die_temp_monitor_params.channel = DIE_TEMP;
3772 chip->die_temp_monitor_params.btm_ctx = (void *)chip;
3773 chip->die_temp_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
3774 chip->die_temp_monitor_params.threshold_notification =
3775 &btm_notify_die_temp;
3776 refresh_die_temp_monitor(chip);
3777 if (rc) {
3778 pr_err("tm setup failed: %d\n", rc);
3779 return rc;
3780 }
3781 pr_debug("setup complete\n");
3782 return 0;
3783}
3784
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003785static int __devinit qpnp_bms_probe(struct spmi_device *spmi)
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003786{
3787 struct qpnp_bms_chip *chip;
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07003788 bool warm_reset;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003789 int rc, vbatt;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003790
3791 chip = kzalloc(sizeof *chip, GFP_KERNEL);
3792
3793 if (chip == NULL) {
3794 pr_err("kzalloc() failed.\n");
3795 return -ENOMEM;
3796 }
3797
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003798 chip->vadc_dev = qpnp_get_vadc(&(spmi->dev), "bms");
3799 if (IS_ERR(chip->vadc_dev)) {
3800 rc = PTR_ERR(chip->vadc_dev);
3801 if (rc != -EPROBE_DEFER)
3802 pr_err("vadc property missing, rc=%d\n", rc);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003803 goto error_read;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003804 }
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003805
3806 rc = qpnp_iadc_is_ready();
3807 if (rc) {
3808 pr_info("iadc not ready: %d, deferring probe\n", rc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003809 rc = -EPROBE_DEFER;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003810 goto error_read;
3811 }
3812
Xiaozhe Shi3eaf0b62013-07-11 09:48:08 -07003813 mutex_init(&chip->bms_output_lock);
3814 mutex_init(&chip->last_ocv_uv_mutex);
3815 mutex_init(&chip->vbat_monitor_mutex);
3816 mutex_init(&chip->soc_invalidation_mutex);
3817 mutex_init(&chip->last_soc_mutex);
3818
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07003819 warm_reset = qpnp_pon_is_warm_reset();
3820 rc = warm_reset;
3821 if (rc < 0)
3822 goto error_read;
3823
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003824 rc = register_spmi(chip, spmi);
3825 if (rc) {
3826 pr_err("error registering spmi resource %d\n", rc);
3827 goto error_resource;
3828 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003829
3830 rc = qpnp_read_wrapper(chip, &chip->revision1,
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003831 chip->base + REVISION1, 1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003832 if (rc) {
3833 pr_err("error reading version register %d\n", rc);
3834 goto error_read;
3835 }
3836
3837 rc = qpnp_read_wrapper(chip, &chip->revision2,
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003838 chip->base + REVISION2, 1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003839 if (rc) {
3840 pr_err("Error reading version register %d\n", rc);
3841 goto error_read;
3842 }
Xiaozhe Shia045a562012-11-28 16:55:39 -08003843 pr_debug("BMS version: %hhu.%hhu\n", chip->revision2, chip->revision1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003844
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003845 rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision2,
3846 chip->iadc_base + REVISION2, 1);
3847 if (rc) {
3848 pr_err("Error reading version register %d\n", rc);
3849 goto error_read;
3850 }
3851
3852 rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision1,
3853 chip->iadc_base + REVISION1, 1);
3854 if (rc) {
3855 pr_err("Error reading version register %d\n", rc);
3856 goto error_read;
3857 }
3858 pr_debug("IADC_BMS version: %hhu.%hhu\n",
3859 chip->iadc_bms_revision2, chip->iadc_bms_revision1);
3860
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003861 rc = bms_read_properties(chip);
3862 if (rc) {
3863 pr_err("Unable to read all bms properties, rc = %d\n", rc);
3864 goto error_read;
3865 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003866
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003867 rc = read_iadc_channel_select(chip);
3868 if (rc) {
3869 pr_err("Unable to get iadc selected channel = %d\n", rc);
3870 goto error_read;
3871 }
3872
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003873 if (chip->use_ocv_thresholds) {
3874 rc = set_ocv_voltage_thresholds(chip,
3875 chip->ocv_low_threshold_uv,
3876 chip->ocv_high_threshold_uv);
3877 if (rc) {
3878 pr_err("Could not set ocv voltage thresholds: %d\n",
3879 rc);
3880 goto error_read;
3881 }
3882 }
3883
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003884 rc = set_battery_data(chip);
3885 if (rc) {
3886 pr_err("Bad battery data %d\n", rc);
3887 goto error_read;
3888 }
3889
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003890 bms_initialize_constants(chip);
3891
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003892 wakeup_source_init(&chip->soc_wake_source.source, "qpnp_soc_wake");
Xiaozhe Shi4be85782013-02-22 17:33:40 -08003893 wake_lock_init(&chip->low_voltage_wake_lock, WAKE_LOCK_SUSPEND,
3894 "qpnp_low_voltage_lock");
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003895 wake_lock_init(&chip->cv_wake_lock, WAKE_LOCK_SUSPEND,
3896 "qpnp_cv_lock");
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003897 INIT_DELAYED_WORK(&chip->calculate_soc_delayed_work,
3898 calculate_soc_work);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08003899 INIT_WORK(&chip->recalc_work, recalculate_work);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003900 INIT_WORK(&chip->batfet_open_work, batfet_open_work);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003901
3902 read_shutdown_soc_and_iavg(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003903
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05303904 if (chip->enable_fcc_learning) {
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303905 if (chip->battery_removed) {
3906 rc = discard_backup_fcc_data(chip);
3907 if (rc)
3908 pr_err("Could not discard backed-up FCC data\n");
3909 } else {
3910 rc = read_chgcycle_data_from_backup(chip);
3911 if (rc)
3912 pr_err("Unable to restore charge-cycle data\n");
3913
3914 rc = read_fcc_data_from_backup(chip);
3915 if (rc)
3916 pr_err("Unable to restore FCC-learning data\n");
3917 else
3918 attempt_learning_new_fcc(chip);
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05303919 }
3920 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303921
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003922 dev_set_drvdata(&spmi->dev, chip);
3923 device_init_wakeup(&spmi->dev, 1);
3924
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003925 rc = setup_vbat_monitoring(chip);
3926 if (rc < 0) {
3927 pr_err("failed to set up voltage notifications: %d\n", rc);
3928 goto error_setup;
Xiaozhe Shid5d21412013-02-06 17:14:41 -08003929 }
3930
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003931 rc = setup_die_temp_monitoring(chip);
3932 if (rc < 0) {
3933 pr_err("failed to set up die temp notifications: %d\n", rc);
3934 goto error_setup;
3935 }
3936
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07003937 battery_insertion_check(chip);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003938 batfet_status_check(chip);
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07003939 battery_status_check(chip);
3940
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003941 calculate_soc_work(&(chip->calculate_soc_delayed_work.work));
3942
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003943 /* setup & register the battery power supply */
3944 chip->bms_psy.name = "bms";
3945 chip->bms_psy.type = POWER_SUPPLY_TYPE_BMS;
3946 chip->bms_psy.properties = msm_bms_power_props;
3947 chip->bms_psy.num_properties = ARRAY_SIZE(msm_bms_power_props);
3948 chip->bms_psy.get_property = qpnp_bms_power_get_property;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003949 chip->bms_psy.external_power_changed =
3950 qpnp_bms_external_power_changed;
3951 chip->bms_psy.supplied_to = qpnp_bms_supplicants;
3952 chip->bms_psy.num_supplicants = ARRAY_SIZE(qpnp_bms_supplicants);
3953
3954 rc = power_supply_register(chip->dev, &chip->bms_psy);
3955
3956 if (rc < 0) {
3957 pr_err("power_supply_register bms failed rc = %d\n", rc);
3958 goto unregister_dc;
3959 }
3960
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07003961 chip->bms_psy_registered = true;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003962 vbatt = 0;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003963 rc = get_battery_voltage(chip, &vbatt);
Xiaozhe Shi36458962013-02-06 16:19:57 -08003964 if (rc) {
3965 pr_err("error reading vbat_sns adc channel = %d, rc = %d\n",
3966 VBAT_SNS, rc);
3967 goto unregister_dc;
3968 }
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003969
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003970 rc = bms_request_irqs(chip);
3971 if (rc) {
3972 pr_err("error requesting bms irqs, rc = %d\n", rc);
3973 goto unregister_dc;
3974 }
3975
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07003976 pr_info("probe success: soc =%d vbatt = %d ocv = %d r_sense_uohm = %u warm_reset = %d\n",
3977 get_prop_bms_capacity(chip), vbatt, chip->last_ocv_uv,
3978 chip->r_sense_uohm, warm_reset);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003979 return 0;
3980
3981unregister_dc:
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07003982 chip->bms_psy_registered = false;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003983 power_supply_unregister(&chip->bms_psy);
3984error_setup:
3985 dev_set_drvdata(&spmi->dev, NULL);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003986 wakeup_source_trash(&chip->soc_wake_source.source);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08003987 wake_lock_destroy(&chip->low_voltage_wake_lock);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003988 wake_lock_destroy(&chip->cv_wake_lock);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003989error_resource:
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003990error_read:
3991 kfree(chip);
3992 return rc;
3993}
3994
3995static int __devexit
3996qpnp_bms_remove(struct spmi_device *spmi)
3997{
3998 struct qpnp_bms_chip *chip = dev_get_drvdata(&spmi->dev);
3999
4000 dev_set_drvdata(&spmi->dev, NULL);
4001 kfree(chip);
4002 return 0;
4003}
4004
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004005static int bms_suspend(struct device *dev)
4006{
4007 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
4008
4009 cancel_delayed_work_sync(&chip->calculate_soc_delayed_work);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07004010 chip->was_charging_at_sleep = is_battery_charging(chip);
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004011 return 0;
4012}
4013
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004014static int bms_resume(struct device *dev)
4015{
4016 int rc;
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004017 int soc_calc_period;
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004018 int time_until_next_recalc = 0;
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004019 unsigned long time_since_last_recalc;
4020 unsigned long tm_now_sec;
4021 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
4022
4023 rc = get_current_time(&tm_now_sec);
4024 if (rc) {
4025 pr_err("Could not read current time: %d\n", rc);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004026 } else {
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004027 if (chip->calculated_soc < chip->low_soc_calc_threshold)
4028 soc_calc_period = chip->low_soc_calculate_soc_ms;
4029 else
4030 soc_calc_period = chip->calculate_soc_ms;
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004031 time_since_last_recalc = tm_now_sec - chip->last_recalc_time;
4032 pr_debug("Time since last recalc: %lu\n",
4033 time_since_last_recalc);
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004034 time_until_next_recalc = max(0, soc_calc_period
4035 - (int)(time_since_last_recalc * 1000));
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004036 }
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004037
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004038 if (time_until_next_recalc == 0)
4039 bms_stay_awake(&chip->soc_wake_source);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004040 schedule_delayed_work(&chip->calculate_soc_delayed_work,
4041 round_jiffies_relative(msecs_to_jiffies
4042 (time_until_next_recalc)));
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004043 return 0;
4044}
4045
4046static const struct dev_pm_ops qpnp_bms_pm_ops = {
4047 .resume = bms_resume,
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004048 .suspend = bms_suspend,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004049};
4050
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004051static struct spmi_driver qpnp_bms_driver = {
4052 .probe = qpnp_bms_probe,
4053 .remove = __devexit_p(qpnp_bms_remove),
4054 .driver = {
4055 .name = QPNP_BMS_DEV_NAME,
4056 .owner = THIS_MODULE,
4057 .of_match_table = qpnp_bms_match_table,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004058 .pm = &qpnp_bms_pm_ops,
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004059 },
4060};
4061
4062static int __init qpnp_bms_init(void)
4063{
4064 pr_info("QPNP BMS INIT\n");
4065 return spmi_driver_register(&qpnp_bms_driver);
4066}
4067
4068static void __exit qpnp_bms_exit(void)
4069{
4070 pr_info("QPNP BMS EXIT\n");
4071 return spmi_driver_unregister(&qpnp_bms_driver);
4072}
4073
4074module_init(qpnp_bms_init);
4075module_exit(qpnp_bms_exit);
4076
4077MODULE_DESCRIPTION("QPNP BMS Driver");
4078MODULE_LICENSE("GPL v2");
4079MODULE_ALIAS("platform:" QPNP_BMS_DEV_NAME);