blob: 9a642d6c51fe328b9a16436ea731548748a6b61e [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 Shi73a65692012-09-18 17:51:57 -070028#include <linux/mfd/pm8xxx/batterydata-lib.h>
Xiaozhe Shib19f7032012-08-16 12:14:16 -070029
Xiaozhe Shib19f7032012-08-16 12:14:16 -070030/* BMS Register Offsets */
31#define BMS1_REVISION1 0x0
32#define BMS1_REVISION2 0x1
33#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 Shib19f7032012-08-16 12:14:16 -070048/* CC interrupt threshold */
49#define BMS1_CC_THR0 0x7A
50#define BMS1_CC_THR1 0x7B
51#define BMS1_CC_THR2 0x7C
52#define BMS1_CC_THR3 0x7D
53#define BMS1_CC_THR4 0x7E
54/* OCV for r registers */
55#define BMS1_OCV_FOR_R_DATA0 0x80
56#define BMS1_OCV_FOR_R_DATA1 0x81
57#define BMS1_VSENSE_FOR_R_DATA0 0x82
58#define BMS1_VSENSE_FOR_R_DATA1 0x83
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070059/* Coulomb counter data */
Xiaozhe Shib19f7032012-08-16 12:14:16 -070060#define BMS1_CC_DATA0 0x8A
61#define BMS1_CC_DATA1 0x8B
62#define BMS1_CC_DATA2 0x8C
63#define BMS1_CC_DATA3 0x8D
64#define BMS1_CC_DATA4 0x8E
65/* OCV for soc data */
66#define BMS1_OCV_FOR_SOC_DATA0 0x90
67#define BMS1_OCV_FOR_SOC_DATA1 0x91
68#define BMS1_VSENSE_PON_DATA0 0x94
69#define BMS1_VSENSE_PON_DATA1 0x95
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070070#define BMS1_VSENSE_AVG_DATA0 0x98
71#define BMS1_VSENSE_AVG_DATA1 0x99
Xiaozhe Shib19f7032012-08-16 12:14:16 -070072#define BMS1_VBAT_AVG_DATA0 0x9E
73#define BMS1_VBAT_AVG_DATA1 0x9F
74/* Extra bms registers */
Xiaozhe Shi57058942013-03-27 16:54:54 -070075#define SOC_STORAGE_REG 0xB0
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070076#define IAVG_STORAGE_REG 0xB1
Xiaozhe Shi57058942013-03-27 16:54:54 -070077#define BMS1_BMS_DATA_REG_2 0xB2
Xiaozhe Shib19f7032012-08-16 12:14:16 -070078#define BMS1_BMS_DATA_REG_3 0xB3
Xiaozhe Shic40b3972012-11-30 14:11:16 -080079/* IADC Channel Select */
80#define IADC1_BMS_ADC_CH_SEL_CTL 0x48
Xiaozhe Shib19f7032012-08-16 12:14:16 -070081
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070082/* Configuration for saving of shutdown soc/iavg */
83#define IGNORE_SOC_TEMP_DECIDEG 50
84#define IAVG_STEP_SIZE_MA 50
85#define IAVG_START 600
Xiaozhe Shif5f966d2013-02-19 14:23:11 -080086#define IAVG_INVALID 0xFF
Xiaozhe Shic7cbd052013-03-29 12:03:11 -070087#define SOC_INVALID 0xFF
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070088
Xiaozhe Shie118c692012-09-24 15:17:43 -070089#define IAVG_SAMPLES 16
90
Xiaozhe Shib19f7032012-08-16 12:14:16 -070091#define QPNP_BMS_DEV_NAME "qcom,qpnp-bms"
92
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070093struct soc_params {
94 int fcc_uah;
95 int cc_uah;
Xiaozhe Shi904f1f72012-12-04 12:47:21 -080096 int rbatt_mohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070097 int iavg_ua;
98 int uuc_uah;
99 int ocv_charge_uah;
Xiaozhe Shif36d2862013-01-04 10:17:35 -0800100 int delta_time_s;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700101};
102
103struct raw_soc_params {
104 uint16_t last_good_ocv_raw;
105 int64_t cc;
106 int last_good_ocv_uv;
107};
108
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700109struct qpnp_bms_chip {
110 struct device *dev;
111 struct power_supply bms_psy;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -0700112 struct power_supply *batt_psy;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700113 struct spmi_device *spmi;
114 u16 base;
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800115 u16 iadc_base;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700116
117 u8 revision1;
118 u8 revision2;
Xiaozhe Shid5d21412013-02-06 17:14:41 -0800119 int battery_present;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700120 int battery_status;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800121 bool new_battery;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700122 bool done_charging;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800123 bool last_soc_invalid;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700124 /* platform data */
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -0800125 int r_sense_uohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700126 unsigned int v_cutoff_uv;
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -0800127 int max_voltage_uv;
128 int r_conn_mohm;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700129 int shutdown_soc_valid_limit;
130 int adjust_soc_low_threshold;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700131 int chg_term_ua;
Xiaozhe Shi73a65692012-09-18 17:51:57 -0700132 enum battery_type batt_type;
Xiaozhe Shi976618f2013-04-30 10:49:30 -0700133 unsigned int fcc_mah;
Xiaozhe Shi73a65692012-09-18 17:51:57 -0700134 struct single_row_lut *fcc_temp_lut;
135 struct single_row_lut *fcc_sf_lut;
136 struct pc_temp_ocv_lut *pc_temp_ocv_lut;
137 struct sf_lut *pc_sf_lut;
138 struct sf_lut *rbatt_sf_lut;
139 int default_rbatt_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -0700140 int rbatt_capacitive_mohm;
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -0700141 int rbatt_mohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700142
143 struct delayed_work calculate_soc_delayed_work;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800144 struct work_struct recalc_work;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700145
146 struct mutex bms_output_lock;
147 struct mutex last_ocv_uv_mutex;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700148 struct mutex vbat_monitor_mutex;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700149 struct mutex soc_invalidation_mutex;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700150 struct mutex last_soc_mutex;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700151
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800152 bool use_external_rsense;
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800153 bool use_ocv_thresholds;
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800154
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700155 bool ignore_shutdown_soc;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800156 bool shutdown_soc_invalid;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700157 int shutdown_soc;
158 int shutdown_iavg_ma;
159
Xiaozhe Shi4be85782013-02-22 17:33:40 -0800160 struct wake_lock low_voltage_wake_lock;
Xiaozhe Shi4be85782013-02-22 17:33:40 -0800161 int low_voltage_threshold;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700162 int low_soc_calc_threshold;
163 int low_soc_calculate_soc_ms;
164 int calculate_soc_ms;
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -0800165 struct wake_lock soc_wake_lock;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700166 struct wake_lock cv_wake_lock;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700167
Xiaozhe Shie118c692012-09-24 15:17:43 -0700168 uint16_t ocv_reading_at_100;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700169 uint16_t prev_last_good_ocv_raw;
170 int last_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800171 int last_ocv_temp;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700172 int last_cc_uah;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700173 unsigned long last_soc_change_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700174 unsigned long tm_sec;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700175 unsigned long report_tm_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700176 bool first_time_calc_soc;
177 bool first_time_calc_uuc;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700178 int64_t software_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700179
180 int iavg_samples_ma[IAVG_SAMPLES];
181 int iavg_index;
182 int iavg_num_samples;
183 struct timespec t_soc_queried;
184 int last_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -0700185 int last_soc_est;
Xiaozhe Shicc137262013-03-10 06:21:41 -0700186 int last_soc_unbound;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700187 bool was_charging_at_sleep;
188 int charge_start_tm_sec;
189 int catch_up_time_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700190 struct single_row_lut *adjusted_fcc_temp_lut;
191
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700192 struct qpnp_adc_tm_btm_param vbat_monitor_params;
Xiaozhe Shi535494d2013-04-05 12:27:51 -0700193 struct qpnp_adc_tm_btm_param die_temp_monitor_params;
194 int temperature_margin;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700195 unsigned int vadc_v0625;
196 unsigned int vadc_v1250;
197
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700198 int prev_uuc_iavg_ma;
199 int prev_pc_unusable;
200 int ibat_at_cv_ua;
201 int soc_at_cv;
202 int prev_chg_soc;
203 int calculated_soc;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -0800204 int prev_voltage_based_soc;
205 bool use_voltage_soc;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700206 bool in_cv_range;
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800207
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800208 int prev_batt_terminal_uv;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -0700209 int high_ocv_correction_limit_uv;
210 int low_ocv_correction_limit_uv;
211 int flat_ocv_threshold_uv;
212 int hold_soc_est;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800213
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800214 int ocv_high_threshold_uv;
215 int ocv_low_threshold_uv;
Xiaozhe Shicdeee312012-12-18 15:10:18 -0800216 unsigned long last_recalc_time;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700217};
218
219static struct of_device_id qpnp_bms_match_table[] = {
220 { .compatible = QPNP_BMS_DEV_NAME },
221 {}
222};
223
224static char *qpnp_bms_supplicants[] = {
225 "battery"
226};
227
228static enum power_supply_property msm_bms_power_props[] = {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700229 POWER_SUPPLY_PROP_CAPACITY,
230 POWER_SUPPLY_PROP_CURRENT_NOW,
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -0700231 POWER_SUPPLY_PROP_RESISTANCE,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700232 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
233};
234
Xiaozhe Shi20640b52013-01-03 11:49:30 -0800235static bool bms_reset;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -0800236
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700237static int qpnp_read_wrapper(struct qpnp_bms_chip *chip, u8 *val,
238 u16 base, int count)
239{
240 int rc;
241 struct spmi_device *spmi = chip->spmi;
242
243 rc = spmi_ext_register_readl(spmi->ctrl, spmi->sid, base, val, count);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700244 if (rc) {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700245 pr_err("SPMI read failed rc=%d\n", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700246 return rc;
247 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700248 return 0;
249}
250
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700251static int qpnp_write_wrapper(struct qpnp_bms_chip *chip, u8 *val,
252 u16 base, int count)
253{
254 int rc;
255 struct spmi_device *spmi = chip->spmi;
256
257 rc = spmi_ext_register_writel(spmi->ctrl, spmi->sid, base, val, count);
258 if (rc) {
259 pr_err("SPMI write failed rc=%d\n", rc);
260 return rc;
261 }
262 return 0;
263}
264
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800265static int qpnp_masked_write_base(struct qpnp_bms_chip *chip, u16 addr,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700266 u8 mask, u8 val)
267{
268 int rc;
269 u8 reg;
270
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800271 rc = qpnp_read_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700272 if (rc) {
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800273 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700274 return rc;
275 }
276 reg &= ~mask;
277 reg |= val & mask;
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800278 rc = qpnp_write_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700279 if (rc) {
280 pr_err("write failed addr = %03X, val = %02x, mask = %02x, reg = %02x, rc = %d\n",
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800281 addr, val, mask, reg, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700282 return rc;
283 }
284 return 0;
285}
286
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800287static int qpnp_masked_write_iadc(struct qpnp_bms_chip *chip, u16 addr,
288 u8 mask, u8 val)
289{
290 return qpnp_masked_write_base(chip, chip->iadc_base + addr, mask, val);
291}
292
293static int qpnp_masked_write(struct qpnp_bms_chip *chip, u16 addr,
294 u8 mask, u8 val)
295{
296 return qpnp_masked_write_base(chip, chip->base + addr, mask, val);
297}
298
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700299#define HOLD_OREG_DATA BIT(0)
300static int lock_output_data(struct qpnp_bms_chip *chip)
301{
302 int rc;
303
304 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL,
305 HOLD_OREG_DATA, HOLD_OREG_DATA);
306 if (rc) {
307 pr_err("couldnt lock bms output rc = %d\n", rc);
308 return rc;
309 }
310 return 0;
311}
312
313static int unlock_output_data(struct qpnp_bms_chip *chip)
314{
315 int rc;
316
317 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL, HOLD_OREG_DATA, 0);
318 if (rc) {
319 pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc);
320 return rc;
321 }
322 return 0;
323}
324
325#define V_PER_BIT_MUL_FACTOR 97656
326#define V_PER_BIT_DIV_FACTOR 1000
327#define VADC_INTRINSIC_OFFSET 0x6000
328
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800329static int vadc_reading_to_uv(int reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700330{
331 if (reading <= VADC_INTRINSIC_OFFSET)
332 return 0;
333
334 return (reading - VADC_INTRINSIC_OFFSET)
335 * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR;
336}
337
338#define VADC_CALIB_UV 625000
339#define VBATT_MUL_FACTOR 3
340
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800341static int adjust_vbatt_reading(struct qpnp_bms_chip *chip, int reading_uv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700342{
343 s64 numerator, denominator;
344
345 if (reading_uv == 0)
346 return 0;
347
348 /* don't adjust if not calibrated */
349 if (chip->vadc_v0625 == 0 || chip->vadc_v1250 == 0) {
350 pr_debug("No cal yet return %d\n",
351 VBATT_MUL_FACTOR * reading_uv);
352 return VBATT_MUL_FACTOR * reading_uv;
353 }
354
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700355 numerator = ((s64)reading_uv - chip->vadc_v0625) * VADC_CALIB_UV;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700356 denominator = (s64)chip->vadc_v1250 - chip->vadc_v0625;
357 if (denominator == 0)
358 return reading_uv * VBATT_MUL_FACTOR;
359 return (VADC_CALIB_UV + div_s64(numerator, denominator))
360 * VBATT_MUL_FACTOR;
361}
362
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800363static int convert_vbatt_uv_to_raw(struct qpnp_bms_chip *chip,
364 int unadjusted_vbatt)
365{
366 int scaled_vbatt = unadjusted_vbatt / VBATT_MUL_FACTOR;
367
368 if (scaled_vbatt <= 0)
369 return VADC_INTRINSIC_OFFSET;
370 return ((scaled_vbatt * V_PER_BIT_DIV_FACTOR) / V_PER_BIT_MUL_FACTOR)
371 + VADC_INTRINSIC_OFFSET;
372}
373
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700374static inline int convert_vbatt_raw_to_uv(struct qpnp_bms_chip *chip,
375 uint16_t reading)
376{
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700377 int64_t uv;
378 int rc;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700379
380 uv = vadc_reading_to_uv(reading);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700381 pr_debug("%u raw converted into %lld uv\n", reading, uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700382 uv = adjust_vbatt_reading(chip, uv);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700383 pr_debug("adjusted into %lld uv\n", uv);
384 rc = qpnp_vbat_sns_comp_result(&uv);
385 if (rc)
386 pr_debug("could not compensate vbatt\n");
387 pr_debug("compensated into %lld uv\n", uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700388 return uv;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700389}
390
391#define CC_READING_RESOLUTION_N 542535
392#define CC_READING_RESOLUTION_D 100000
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -0700393static s64 cc_reading_to_uv(s64 reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700394{
395 return div_s64(reading * CC_READING_RESOLUTION_N,
396 CC_READING_RESOLUTION_D);
397}
398
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800399#define QPNP_ADC_GAIN_IDEAL 3291LL
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700400static s64 cc_adjust_for_gain(s64 uv, uint16_t gain)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700401{
402 s64 result_uv;
403
404 pr_debug("adjusting_uv = %lld\n", uv);
Xiaozhe Shi820a47a2012-11-27 13:23:27 -0800405 if (gain == 0) {
406 pr_debug("gain is %d, not adjusting\n", gain);
407 return uv;
408 }
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700409 pr_debug("adjusting by factor: %lld/%hu = %lld%%\n",
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800410 QPNP_ADC_GAIN_IDEAL, gain,
411 div_s64(QPNP_ADC_GAIN_IDEAL * 100LL, (s64)gain));
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700412
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800413 result_uv = div_s64(uv * QPNP_ADC_GAIN_IDEAL, (s64)gain);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700414 pr_debug("result_uv = %lld\n", result_uv);
415 return result_uv;
416}
417
418static int convert_vsense_to_uv(struct qpnp_bms_chip *chip,
419 int16_t reading)
420{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700421 struct qpnp_iadc_calib calibration;
422
423 qpnp_iadc_get_gain_and_offset(&calibration);
424 return cc_adjust_for_gain(cc_reading_to_uv(reading),
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800425 calibration.gain_raw - calibration.offset_raw);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700426}
427
428static int read_vsense_avg(struct qpnp_bms_chip *chip, int *result_uv)
429{
430 int rc;
431 int16_t reading;
432
433 rc = qpnp_read_wrapper(chip, (u8 *)&reading,
434 chip->base + BMS1_VSENSE_AVG_DATA0, 2);
435
436 if (rc) {
437 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
438 return rc;
439 }
440
441 *result_uv = convert_vsense_to_uv(chip, reading);
442 return 0;
443}
444
445static int get_battery_current(struct qpnp_bms_chip *chip, int *result_ua)
446{
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700447 int rc, vsense_uv = 0;
448 int64_t temp_current;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700449
Xiaozhe Shid0a79542012-11-06 10:00:38 -0800450 if (chip->r_sense_uohm == 0) {
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700451 pr_err("r_sense is zero\n");
452 return -EINVAL;
453 }
454
455 mutex_lock(&chip->bms_output_lock);
456 lock_output_data(chip);
457 read_vsense_avg(chip, &vsense_uv);
458 unlock_output_data(chip);
459 mutex_unlock(&chip->bms_output_lock);
460
461 pr_debug("vsense_uv=%duV\n", vsense_uv);
462 /* cast for signed division */
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700463 temp_current = div_s64((vsense_uv * 1000000LL),
464 (int)chip->r_sense_uohm);
465
466 rc = qpnp_iadc_comp_result(&temp_current);
467 if (rc)
468 pr_debug("error compensation failed: %d\n", rc);
469
470 *result_ua = temp_current;
471 pr_debug("err compensated ibat=%duA\n", *result_ua);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700472 return 0;
473}
474
475static int get_battery_voltage(int *result_uv)
476{
477 int rc;
478 struct qpnp_vadc_result adc_result;
479
480 rc = qpnp_vadc_read(VBAT_SNS, &adc_result);
481 if (rc) {
482 pr_err("error reading adc channel = %d, rc = %d\n",
483 VBAT_SNS, rc);
484 return rc;
485 }
486 pr_debug("mvolts phy = %lld meas = 0x%llx\n", adc_result.physical,
487 adc_result.measurement);
488 *result_uv = (int)adc_result.physical;
489 return 0;
490}
491
Xiaozhe Shie118c692012-09-24 15:17:43 -0700492#define CC_36_BIT_MASK 0xFFFFFFFFFLL
493
494static int read_cc_raw(struct qpnp_bms_chip *chip, int64_t *reading)
495{
496 int64_t raw_reading;
497 int rc;
498
499 rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
500 chip->base + BMS1_CC_DATA0, 5);
501 if (rc) {
502 pr_err("Error reading cc: rc = %d\n", rc);
503 return -ENXIO;
504 }
505
506 raw_reading = raw_reading & CC_36_BIT_MASK;
507 /* convert 36 bit signed value into 64 signed value */
508 *reading = (raw_reading >> 35) == 0LL ?
509 raw_reading : ((-1LL ^ CC_36_BIT_MASK) | raw_reading);
510 pr_debug("before conversion: %llx, after conversion: %llx\n",
511 raw_reading, *reading);
512
513 return 0;
514}
515
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700516static int calib_vadc(struct qpnp_bms_chip *chip)
517{
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700518 int rc, raw_0625, raw_1250;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700519 struct qpnp_vadc_result result;
520
521 rc = qpnp_vadc_read(REF_625MV, &result);
522 if (rc) {
523 pr_debug("vadc read failed with rc = %d\n", rc);
524 return rc;
525 }
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700526 raw_0625 = result.adc_code;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700527
528 rc = qpnp_vadc_read(REF_125V, &result);
529 if (rc) {
530 pr_debug("vadc read failed with rc = %d\n", rc);
531 return rc;
532 }
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700533 raw_1250 = result.adc_code;
534 chip->vadc_v0625 = vadc_reading_to_uv(raw_0625);
535 chip->vadc_v1250 = vadc_reading_to_uv(raw_1250);
536 pr_debug("vadc calib: 0625 = %d raw (%d uv), 1250 = %d raw (%d uv)\n",
537 raw_0625, chip->vadc_v0625,
538 raw_1250, chip->vadc_v1250);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700539 return 0;
540}
541
Xiaozhe Shie118c692012-09-24 15:17:43 -0700542static void convert_and_store_ocv(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800543 struct raw_soc_params *raw,
544 int batt_temp)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700545{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700546 int rc;
547
548 pr_debug("prev_last_good_ocv_raw = %d, last_good_ocv_raw = %d\n",
549 chip->prev_last_good_ocv_raw,
550 raw->last_good_ocv_raw);
551 rc = calib_vadc(chip);
552 if (rc)
553 pr_err("Vadc reference voltage read failed, rc = %d\n", rc);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700554 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
555 raw->last_good_ocv_uv = convert_vbatt_raw_to_uv(chip,
556 raw->last_good_ocv_raw);
557 chip->last_ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800558 chip->last_ocv_temp = batt_temp;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700559 chip->software_cc_uah = 0;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700560 pr_debug("last_good_ocv_uv = %d\n", raw->last_good_ocv_uv);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700561}
562
Xiaozhe Shia045a562012-11-28 16:55:39 -0800563#define CLEAR_CC BIT(7)
564#define CLEAR_SW_CC BIT(6)
565/**
566 * reset both cc and sw-cc.
567 * note: this should only be ever called from one thread
568 * or there may be a race condition where CC is never enabled
569 * again
570 */
571static void reset_cc(struct qpnp_bms_chip *chip)
572{
573 int rc;
574
575 pr_debug("resetting cc manually\n");
576 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
577 CLEAR_CC | CLEAR_SW_CC,
578 CLEAR_CC | CLEAR_SW_CC);
579 if (rc)
580 pr_err("cc reset failed: %d\n", rc);
581
582 /* wait for 100us for cc to reset */
583 udelay(100);
584
585 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
586 CLEAR_CC | CLEAR_SW_CC, 0);
587 if (rc)
588 pr_err("cc reenable failed: %d\n", rc);
589}
590
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700591static int get_battery_status(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800592{
593 union power_supply_propval ret = {0,};
594
595 if (chip->batt_psy == NULL)
596 chip->batt_psy = power_supply_get_by_name("battery");
597 if (chip->batt_psy) {
598 /* if battery has been registered, use the status property */
599 chip->batt_psy->get_property(chip->batt_psy,
600 POWER_SUPPLY_PROP_STATUS, &ret);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700601 return ret.intval;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800602 }
603
604 /* Default to false if the battery power supply is not registered. */
605 pr_debug("battery power supply is not registered\n");
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700606 return POWER_SUPPLY_STATUS_UNKNOWN;
607}
608
609static bool is_battery_charging(struct qpnp_bms_chip *chip)
610{
611 return get_battery_status(chip) == POWER_SUPPLY_STATUS_CHARGING;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800612}
613
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700614static bool is_battery_present(struct qpnp_bms_chip *chip)
615{
616 union power_supply_propval ret = {0,};
617
618 if (chip->batt_psy == NULL)
619 chip->batt_psy = power_supply_get_by_name("battery");
620 if (chip->batt_psy) {
621 /* if battery has been registered, use the status property */
622 chip->batt_psy->get_property(chip->batt_psy,
623 POWER_SUPPLY_PROP_PRESENT, &ret);
624 return ret.intval;
625 }
626
627 /* Default to false if the battery power supply is not registered. */
628 pr_debug("battery power supply is not registered\n");
629 return false;
630}
631
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700632static bool is_battery_full(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800633{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700634 return get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800635}
636
637static int get_simultaneous_batt_v_and_i(struct qpnp_bms_chip *chip,
638 int *ibat_ua, int *vbat_uv)
639{
640 struct qpnp_iadc_result i_result;
641 struct qpnp_vadc_result v_result;
642 enum qpnp_iadc_channels iadc_channel;
643 int rc;
644
645 iadc_channel = chip->use_external_rsense ?
646 EXTERNAL_RSENSE : INTERNAL_RSENSE;
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700647 if (is_battery_full(chip)) {
648 rc = get_battery_current(chip, ibat_ua);
649 if (rc) {
650 pr_err("bms current read failed with rc: %d\n", rc);
651 return rc;
652 }
653 rc = qpnp_vadc_read(VBAT_SNS, &v_result);
654 if (rc) {
655 pr_err("vadc read failed with rc: %d\n", rc);
656 return rc;
657 }
658 *vbat_uv = (int)v_result.physical;
659 } else {
660 rc = qpnp_iadc_vadc_sync_read(iadc_channel, &i_result,
661 VBAT_SNS, &v_result);
662 if (rc) {
663 pr_err("adc sync read failed with rc: %d\n", rc);
664 return rc;
665 }
666 /*
667 * reverse the current read by the iadc, since the bms uses
668 * flipped battery current polarity.
669 */
670 *ibat_ua = -1 * (int)i_result.result_ua;
671 *vbat_uv = (int)v_result.physical;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800672 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800673
674 return 0;
675}
676
677static int estimate_ocv(struct qpnp_bms_chip *chip)
678{
679 int ibat_ua, vbat_uv, ocv_est_uv;
680 int rc;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -0700681 int rbatt_mohm = chip->default_rbatt_mohm + chip->r_conn_mohm
682 + chip->rbatt_capacitive_mohm;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800683
684 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
685 if (rc) {
686 pr_err("simultaneous failed rc = %d\n", rc);
687 return rc;
688 }
689
690 ocv_est_uv = vbat_uv + (ibat_ua * rbatt_mohm) / 1000;
691 pr_debug("estimated pon ocv = %d\n", ocv_est_uv);
692 return ocv_est_uv;
693}
694
695static void reset_for_new_battery(struct qpnp_bms_chip *chip, int batt_temp)
696{
697 chip->last_ocv_uv = estimate_ocv(chip);
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700698 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800699 chip->last_soc = -EINVAL;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700700 chip->last_soc_invalid = true;
701 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800702 chip->soc_at_cv = -EINVAL;
703 chip->shutdown_soc_invalid = true;
704 chip->shutdown_soc = 0;
705 chip->shutdown_iavg_ma = 0;
706 chip->prev_pc_unusable = -EINVAL;
707 reset_cc(chip);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700708 chip->software_cc_uah = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800709 chip->last_cc_uah = INT_MIN;
710 chip->last_ocv_temp = batt_temp;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800711 chip->prev_batt_terminal_uv = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800712}
713
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -0800714#define OCV_RAW_UNINITIALIZED 0xFFFF
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -0700715#define MIN_OCV_UV 2000000
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700716static int read_soc_params_raw(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800717 struct raw_soc_params *raw,
718 int batt_temp)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700719{
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -0700720 bool warm_reset = false;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700721 int rc;
722
723 mutex_lock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800724
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -0800725 if (chip->prev_last_good_ocv_raw == OCV_RAW_UNINITIALIZED) {
Xiaozhe Shia045a562012-11-28 16:55:39 -0800726 /* software workaround for BMS 1.0
727 * The coulomb counter does not reset upon PON, so reset it
728 * manually upon probe. */
729 if (chip->revision1 == 0 && chip->revision2 == 0)
730 reset_cc(chip);
731 }
732
Xiaozhe Shie118c692012-09-24 15:17:43 -0700733 lock_output_data(chip);
734
735 rc = qpnp_read_wrapper(chip, (u8 *)&raw->last_good_ocv_raw,
736 chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
737 if (rc) {
738 pr_err("Error reading ocv: rc = %d\n", rc);
739 return -ENXIO;
740 }
741
742 rc = read_cc_raw(chip, &raw->cc);
743 if (rc) {
744 pr_err("Failed to read raw cc data, rc = %d\n", rc);
745 return rc;
746 }
747
748 unlock_output_data(chip);
749 mutex_unlock(&chip->bms_output_lock);
750
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -0800751 if (chip->prev_last_good_ocv_raw == OCV_RAW_UNINITIALIZED) {
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800752 convert_and_store_ocv(chip, raw, batt_temp);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -0700753 pr_debug("PON_OCV_UV = %d, cc = %llx\n",
754 chip->last_ocv_uv, raw->cc);
755 warm_reset = qpnp_pon_is_warm_reset();
756 if (raw->last_good_ocv_uv < MIN_OCV_UV
757 || warm_reset > 0) {
758 pr_debug("OCV is stale or bad, estimating new OCV.\n");
759 chip->last_ocv_uv = estimate_ocv(chip);
760 raw->last_good_ocv_uv = chip->last_ocv_uv;
761 reset_cc(chip);
762 pr_debug("New PON_OCV_UV = %d, cc = %llx\n",
763 chip->last_ocv_uv, raw->cc);
764 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800765 } else if (chip->new_battery) {
766 /* if a new battery was inserted, estimate the ocv */
767 reset_for_new_battery(chip, batt_temp);
768 raw->cc = 0;
769 raw->last_good_ocv_uv = chip->last_ocv_uv;
770 chip->new_battery = false;
Xiaozhe Shi74548b92013-05-02 16:47:08 -0700771 } else if (chip->done_charging) {
772 chip->done_charging = false;
773 /* if we just finished charging, reset CC and fake 100% */
774 chip->ocv_reading_at_100 = raw->last_good_ocv_raw;
775 chip->last_ocv_uv = chip->max_voltage_uv;
776 raw->last_good_ocv_uv = chip->max_voltage_uv;
777 raw->cc = 0;
778 reset_cc(chip);
779 chip->last_ocv_temp = batt_temp;
780 chip->software_cc_uah = 0;
781 chip->last_cc_uah = INT_MIN;
782 pr_debug("EOC Battery full ocv_reading = 0x%x\n",
783 chip->ocv_reading_at_100);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700784 } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) {
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800785 convert_and_store_ocv(chip, raw, batt_temp);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700786 /* forget the old cc value upon ocv */
Xiaozhe Shif36d2862013-01-04 10:17:35 -0800787 chip->last_cc_uah = INT_MIN;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700788 } else {
789 raw->last_good_ocv_uv = chip->last_ocv_uv;
790 }
791
Xiaozhe Shi74548b92013-05-02 16:47:08 -0700792 /* stop faking a high OCV if we get a new OCV */
793 if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw)
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -0800794 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
Xiaozhe Shi74548b92013-05-02 16:47:08 -0700795
Xiaozhe Shie118c692012-09-24 15:17:43 -0700796 pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n",
797 raw->last_good_ocv_raw, raw->last_good_ocv_uv);
798 pr_debug("cc_raw= 0x%llx\n", raw->cc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700799 return 0;
800}
801
Xiaozhe Shie118c692012-09-24 15:17:43 -0700802static int calculate_pc(struct qpnp_bms_chip *chip, int ocv_uv,
803 int batt_temp)
804{
805 int pc;
806
807 pc = interpolate_pc(chip->pc_temp_ocv_lut,
808 batt_temp / 10, ocv_uv / 1000);
809 pr_debug("pc = %u %% for ocv = %d uv batt_temp = %d\n",
810 pc, ocv_uv, batt_temp);
811 /* Multiply the initial FCC value by the scale factor. */
812 return pc;
813}
814
815static int calculate_fcc(struct qpnp_bms_chip *chip, int batt_temp)
816{
817 int fcc_uah;
818
819 if (chip->adjusted_fcc_temp_lut == NULL) {
820 /* interpolate_fcc returns a mv value. */
821 fcc_uah = interpolate_fcc(chip->fcc_temp_lut,
822 batt_temp) * 1000;
823 pr_debug("fcc = %d uAh\n", fcc_uah);
824 return fcc_uah;
825 } else {
826 return 1000 * interpolate_fcc(chip->adjusted_fcc_temp_lut,
827 batt_temp);
828 }
829}
830
831/* calculate remaining charge at the time of ocv */
832static int calculate_ocv_charge(struct qpnp_bms_chip *chip,
833 struct raw_soc_params *raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800834 int fcc_uah)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700835{
836 int ocv_uv, pc;
837
838 ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800839 pc = calculate_pc(chip, ocv_uv, chip->last_ocv_temp);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700840 pr_debug("ocv_uv = %d pc = %d\n", ocv_uv, pc);
841 return (fcc_uah * pc) / 100;
842}
843
Xiaozhe Shie118c692012-09-24 15:17:43 -0700844#define CC_READING_TICKS 56
845#define SLEEP_CLK_HZ 32764
846#define SECONDS_PER_HOUR 3600
847
Xiaozhe Shia9b597d2013-02-12 11:00:39 -0800848static s64 cc_uv_to_pvh(s64 cc_uv)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700849{
Xiaozhe Shia9b597d2013-02-12 11:00:39 -0800850 /* Note that it is necessary need to multiply by 1000000 to convert
851 * from uvh to pvh here.
852 * However, the maximum Coulomb Counter value is 2^35, which can cause
853 * an over flow.
854 * Multiply by 100000 first to perserve as much precision as possible
855 * then multiply by 10 after doing the division in order to avoid
856 * overflow on the maximum Coulomb Counter value.
857 */
858 return div_s64(cc_uv * CC_READING_TICKS * 100000,
859 SLEEP_CLK_HZ * SECONDS_PER_HOUR) * 10;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700860}
861
862/**
863 * calculate_cc-
864 * @chip: the bms chip pointer
865 * @cc: the cc reading from bms h/w
866 * @val: return value
867 * @coulomb_counter: adjusted coulomb counter for 100%
868 *
869 * RETURNS: in val pointer coulomb counter based charger in uAh
870 * (micro Amp hour)
871 */
872static int calculate_cc(struct qpnp_bms_chip *chip, int64_t cc)
873{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700874 struct qpnp_iadc_calib calibration;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700875 struct qpnp_vadc_result result;
876 int64_t cc_voltage_uv, cc_pvh, cc_uah;
877 int ibat_ua, rc;
878
879 rc = qpnp_vadc_read(DIE_TEMP, &result);
880 if (rc) {
881 pr_err("could not read pmic die temperature: %d\n", rc);
882 return chip->software_cc_uah;
883 }
884
885 rc = get_battery_current(chip, &ibat_ua);
886 if (rc) {
887 pr_err("could not read battery current: %d\n", rc);
888 return chip->software_cc_uah;
889 }
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700890
891 qpnp_iadc_get_gain_and_offset(&calibration);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700892 pr_debug("cc = %lld, die_temp = %lld\n", cc, result.physical);
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -0700893 cc_voltage_uv = cc_reading_to_uv(cc);
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800894 cc_voltage_uv = cc_adjust_for_gain(cc_voltage_uv,
895 calibration.gain_raw
896 - calibration.offset_raw);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700897 pr_debug("cc_voltage_uv = %lld uv\n", cc_voltage_uv);
Xiaozhe Shia9b597d2013-02-12 11:00:39 -0800898 cc_pvh = cc_uv_to_pvh(cc_voltage_uv);
899 pr_debug("cc_pvh = %lld pvh\n", cc_pvh);
900 cc_uah = div_s64(cc_pvh, chip->r_sense_uohm);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700901 rc = qpnp_iadc_comp_result(&cc_uah);
902 if (rc)
903 pr_debug("error compensation failed: %d\n", rc);
904 chip->software_cc_uah += cc_uah;
905 reset_cc(chip);
906 return (int)chip->software_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700907}
908
909static int get_rbatt(struct qpnp_bms_chip *chip,
910 int soc_rbatt_mohm, int batt_temp)
911{
912 int rbatt_mohm, scalefactor;
913
914 rbatt_mohm = chip->default_rbatt_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700915 if (chip->rbatt_sf_lut == NULL) {
916 pr_debug("RBATT = %d\n", rbatt_mohm);
917 return rbatt_mohm;
918 }
919 /* Convert the batt_temp to DegC from deciDegC */
920 batt_temp = batt_temp / 10;
921 scalefactor = interpolate_scalingfactor(chip->rbatt_sf_lut,
922 batt_temp, soc_rbatt_mohm);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700923 rbatt_mohm = (rbatt_mohm * scalefactor) / 100;
924
925 rbatt_mohm += chip->r_conn_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -0700926 rbatt_mohm += chip->rbatt_capacitive_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700927 return rbatt_mohm;
928}
929
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -0700930#define IAVG_MINIMAL_TIME 2
Xiaozhe Shie118c692012-09-24 15:17:43 -0700931static void calculate_iavg(struct qpnp_bms_chip *chip, int cc_uah,
Xiaozhe Shif36d2862013-01-04 10:17:35 -0800932 int *iavg_ua, int delta_time_s)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700933{
Xiaozhe Shif36d2862013-01-04 10:17:35 -0800934 int delta_cc_uah = 0;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700935
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -0700936 /*
937 * use the battery current if called too quickly
938 */
939 if (delta_time_s < IAVG_MINIMAL_TIME
940 || chip->last_cc_uah == INT_MIN) {
Xiaozhe Shie118c692012-09-24 15:17:43 -0700941 get_battery_current(chip, iavg_ua);
942 goto out;
943 }
944
Xiaozhe Shie118c692012-09-24 15:17:43 -0700945 delta_cc_uah = cc_uah - chip->last_cc_uah;
946
947 *iavg_ua = div_s64((s64)delta_cc_uah * 3600, delta_time_s);
948
Xiaozhe Shie118c692012-09-24 15:17:43 -0700949out:
Xiaozhe Shif36d2862013-01-04 10:17:35 -0800950 pr_debug("delta_cc = %d iavg_ua = %d\n", delta_cc_uah, (int)*iavg_ua);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700951
952 /* remember cc_uah */
953 chip->last_cc_uah = cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700954}
955
956static int calculate_termination_uuc(struct qpnp_bms_chip *chip,
957 struct soc_params *params,
958 int batt_temp, int uuc_iavg_ma,
959 int *ret_pc_unusable)
960{
961 int unusable_uv, pc_unusable, uuc_uah;
962 int i = 0;
963 int ocv_mv;
964 int batt_temp_degc = batt_temp / 10;
965 int rbatt_mohm;
966 int delta_uv;
967 int prev_delta_uv = 0;
968 int prev_rbatt_mohm = 0;
969 int uuc_rbatt_mohm;
970
971 for (i = 0; i <= 100; i++) {
972 ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
973 batt_temp_degc, i);
974 rbatt_mohm = get_rbatt(chip, i, batt_temp);
975 unusable_uv = (rbatt_mohm * uuc_iavg_ma)
976 + (chip->v_cutoff_uv);
977 delta_uv = ocv_mv * 1000 - unusable_uv;
978
Xiaozhe Shie118c692012-09-24 15:17:43 -0700979 if (delta_uv > 0)
980 break;
981
982 prev_delta_uv = delta_uv;
983 prev_rbatt_mohm = rbatt_mohm;
984 }
985
986 uuc_rbatt_mohm = linear_interpolate(rbatt_mohm, delta_uv,
987 prev_rbatt_mohm, prev_delta_uv,
988 0);
989
990 unusable_uv = (uuc_rbatt_mohm * uuc_iavg_ma) + (chip->v_cutoff_uv);
991
992 pc_unusable = calculate_pc(chip, unusable_uv, batt_temp);
993 uuc_uah = (params->fcc_uah * pc_unusable) / 100;
Xiaozhe Shi794607c2013-02-19 10:25:59 -0800994 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 -0700995 uuc_iavg_ma,
996 uuc_rbatt_mohm, unusable_uv,
Xiaozhe Shi794607c2013-02-19 10:25:59 -0800997 pc_unusable, i, uuc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700998 *ret_pc_unusable = pc_unusable;
999 return uuc_uah;
1000}
1001
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001002#define TIME_PER_PERCENT_UUC 60
Xiaozhe Shie118c692012-09-24 15:17:43 -07001003static int adjust_uuc(struct qpnp_bms_chip *chip,
1004 struct soc_params *params,
1005 int new_pc_unusable,
1006 int new_uuc_uah,
1007 int batt_temp)
1008{
1009 int new_unusable_mv, new_iavg_ma;
1010 int batt_temp_degc = batt_temp / 10;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001011 int max_percent_change;
1012
1013 max_percent_change = max(params->delta_time_s
1014 / TIME_PER_PERCENT_UUC, 1);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001015
1016 if (chip->prev_pc_unusable == -EINVAL
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001017 || abs(chip->prev_pc_unusable - new_pc_unusable)
1018 <= max_percent_change) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001019 chip->prev_pc_unusable = new_pc_unusable;
1020 return new_uuc_uah;
1021 }
1022
1023 /* the uuc is trying to change more than 1% restrict it */
1024 if (new_pc_unusable > chip->prev_pc_unusable)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001025 chip->prev_pc_unusable += max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001026 else
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001027 chip->prev_pc_unusable -= max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001028
1029 new_uuc_uah = (params->fcc_uah * chip->prev_pc_unusable) / 100;
1030
1031 /* also find update the iavg_ma accordingly */
1032 new_unusable_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
1033 batt_temp_degc, chip->prev_pc_unusable);
1034 if (new_unusable_mv < chip->v_cutoff_uv/1000)
1035 new_unusable_mv = chip->v_cutoff_uv/1000;
1036
1037 new_iavg_ma = (new_unusable_mv * 1000 - chip->v_cutoff_uv)
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001038 / params->rbatt_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001039 if (new_iavg_ma == 0)
1040 new_iavg_ma = 1;
1041 chip->prev_uuc_iavg_ma = new_iavg_ma;
1042 pr_debug("Restricting UUC to %d (%d%%) unusable_mv = %d iavg_ma = %d\n",
1043 new_uuc_uah, chip->prev_pc_unusable,
1044 new_unusable_mv, new_iavg_ma);
1045
1046 return new_uuc_uah;
1047}
1048
Abhijeet Dharmapurikarbdf8ba82012-12-20 18:33:56 -08001049#define MIN_IAVG_MA 250
Xiaozhe Shie118c692012-09-24 15:17:43 -07001050#define MIN_SECONDS_FOR_VALID_SAMPLE 20
1051static int calculate_unusable_charge_uah(struct qpnp_bms_chip *chip,
1052 struct soc_params *params,
1053 int batt_temp)
1054{
1055 int uuc_uah_iavg;
1056 int i;
1057 int uuc_iavg_ma = params->iavg_ua / 1000;
1058 int pc_unusable;
1059
1060 /*
1061 * if called first time, fill all the samples with
1062 * the shutdown_iavg_ma
1063 */
1064 if (chip->first_time_calc_uuc && chip->shutdown_iavg_ma != 0) {
1065 pr_debug("Using shutdown_iavg_ma = %d in all samples\n",
1066 chip->shutdown_iavg_ma);
1067 for (i = 0; i < IAVG_SAMPLES; i++)
1068 chip->iavg_samples_ma[i] = chip->shutdown_iavg_ma;
1069
1070 chip->iavg_index = 0;
1071 chip->iavg_num_samples = IAVG_SAMPLES;
1072 }
1073
1074 /*
1075 * if charging use a nominal avg current to keep
1076 * a reasonable UUC while charging
1077 */
Abhijeet Dharmapurikarbdf8ba82012-12-20 18:33:56 -08001078 if (uuc_iavg_ma < MIN_IAVG_MA)
1079 uuc_iavg_ma = MIN_IAVG_MA;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001080 chip->iavg_samples_ma[chip->iavg_index] = uuc_iavg_ma;
1081 chip->iavg_index = (chip->iavg_index + 1) % IAVG_SAMPLES;
1082 chip->iavg_num_samples++;
1083 if (chip->iavg_num_samples >= IAVG_SAMPLES)
1084 chip->iavg_num_samples = IAVG_SAMPLES;
1085
1086 /* now that this sample is added calcualte the average */
1087 uuc_iavg_ma = 0;
1088 if (chip->iavg_num_samples != 0) {
1089 for (i = 0; i < chip->iavg_num_samples; i++) {
1090 pr_debug("iavg_samples_ma[%d] = %d\n", i,
1091 chip->iavg_samples_ma[i]);
1092 uuc_iavg_ma += chip->iavg_samples_ma[i];
1093 }
1094
1095 uuc_iavg_ma = DIV_ROUND_CLOSEST(uuc_iavg_ma,
1096 chip->iavg_num_samples);
1097 }
1098
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001099 /*
1100 * if we're in bms reset mode, force uuc to be 3% of fcc
1101 */
1102 if (bms_reset)
1103 return (params->fcc_uah * 3) / 100;
1104
Xiaozhe Shi75e5efe2013-02-07 09:51:43 -08001105 uuc_uah_iavg = calculate_termination_uuc(chip, params, batt_temp,
1106 uuc_iavg_ma, &pc_unusable);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001107 pr_debug("uuc_iavg_ma = %d uuc with iavg = %d\n",
1108 uuc_iavg_ma, uuc_uah_iavg);
1109
1110 chip->prev_uuc_iavg_ma = uuc_iavg_ma;
1111 /* restrict the uuc such that it can increase only by one percent */
1112 uuc_uah_iavg = adjust_uuc(chip, params, pc_unusable,
1113 uuc_uah_iavg, batt_temp);
1114
1115 chip->first_time_calc_uuc = 0;
1116 return uuc_uah_iavg;
1117}
1118
1119static void find_ocv_for_soc(struct qpnp_bms_chip *chip,
1120 struct soc_params *params,
1121 int batt_temp,
1122 int shutdown_soc,
1123 int *ret_ocv_uv)
1124{
1125 s64 ocv_charge_uah;
1126 int pc, new_pc;
1127 int batt_temp_degc = batt_temp / 10;
1128 int ocv_uv;
1129
1130 ocv_charge_uah = (s64)shutdown_soc
1131 * (params->fcc_uah - params->uuc_uah);
1132 ocv_charge_uah = div_s64(ocv_charge_uah, 100)
1133 + params->cc_uah + params->uuc_uah;
1134 pc = DIV_ROUND_CLOSEST((int)ocv_charge_uah * 100, params->fcc_uah);
1135 pc = clamp(pc, 0, 100);
1136
1137 ocv_uv = interpolate_ocv(chip->pc_temp_ocv_lut, batt_temp_degc, pc);
1138
1139 pr_debug("s_soc = %d, fcc = %d uuc = %d rc = %d, pc = %d, ocv mv = %d\n",
1140 shutdown_soc, params->fcc_uah,
1141 params->uuc_uah, (int)ocv_charge_uah,
1142 pc, ocv_uv);
1143 new_pc = interpolate_pc(chip->pc_temp_ocv_lut, batt_temp_degc, ocv_uv);
1144 pr_debug("test revlookup pc = %d for ocv = %d\n", new_pc, ocv_uv);
1145
1146 while (abs(new_pc - pc) > 1) {
1147 int delta_mv = 5;
1148
1149 if (new_pc > pc)
1150 delta_mv = -1 * delta_mv;
1151
1152 ocv_uv = ocv_uv + delta_mv;
1153 new_pc = interpolate_pc(chip->pc_temp_ocv_lut,
1154 batt_temp_degc, ocv_uv);
1155 pr_debug("test revlookup pc = %d for ocv = %d\n",
1156 new_pc, ocv_uv);
1157 }
1158
1159 *ret_ocv_uv = ocv_uv * 1000;
1160 params->ocv_charge_uah = (int)ocv_charge_uah;
1161}
1162
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001163static int get_current_time(unsigned long *now_tm_sec)
1164{
1165 struct rtc_time tm;
1166 struct rtc_device *rtc;
1167 int rc;
1168
1169 rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
1170 if (rtc == NULL) {
1171 pr_err("%s: unable to open rtc device (%s)\n",
1172 __FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
Xiaozhe Shi0e01af62013-05-06 12:56:08 -07001173 return -EINVAL;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001174 }
1175
1176 rc = rtc_read_time(rtc, &tm);
1177 if (rc) {
1178 pr_err("Error reading rtc device (%s) : %d\n",
1179 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1180 goto close_time;
1181 }
1182
1183 rc = rtc_valid_tm(&tm);
1184 if (rc) {
1185 pr_err("Invalid RTC time (%s): %d\n",
1186 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1187 goto close_time;
1188 }
1189 rtc_tm_to_time(&tm, now_tm_sec);
1190
1191close_time:
1192 rtc_class_close(rtc);
1193 return rc;
1194}
1195
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001196static int calculate_delta_time(unsigned long *time_stamp, int *delta_time_s)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001197{
1198 unsigned long now_tm_sec = 0;
1199
1200 /* default to delta time = 0 if anything fails */
1201 *delta_time_s = 0;
1202
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001203 if (get_current_time(&now_tm_sec)) {
1204 pr_err("RTC read failed\n");
1205 return 0;
1206 }
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001207
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001208 *delta_time_s = (now_tm_sec - *time_stamp);
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001209
1210 /* remember this time */
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001211 *time_stamp = now_tm_sec;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001212 return 0;
1213}
1214
Xiaozhe Shie118c692012-09-24 15:17:43 -07001215static void calculate_soc_params(struct qpnp_bms_chip *chip,
1216 struct raw_soc_params *raw,
1217 struct soc_params *params,
1218 int batt_temp)
1219{
1220 int soc_rbatt;
1221
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001222 calculate_delta_time(&chip->tm_sec, &params->delta_time_s);
1223 pr_debug("tm_sec = %ld, delta_s = %d\n",
1224 chip->tm_sec, params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001225 params->fcc_uah = calculate_fcc(chip, batt_temp);
1226 pr_debug("FCC = %uuAh batt_temp = %d\n", params->fcc_uah, batt_temp);
1227
1228 /* calculate remainging charge */
1229 params->ocv_charge_uah = calculate_ocv_charge(
1230 chip, raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001231 params->fcc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001232 pr_debug("ocv_charge_uah = %uuAh\n", params->ocv_charge_uah);
1233
1234 /* calculate cc micro_volt_hour */
1235 params->cc_uah = calculate_cc(chip, raw->cc);
Xiaozhe Shi1c63a892013-02-13 15:49:40 -08001236 pr_debug("cc_uah = %duAh raw->cc = %llx\n", params->cc_uah, raw->cc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001237
1238 soc_rbatt = ((params->ocv_charge_uah - params->cc_uah) * 100)
1239 / params->fcc_uah;
1240 if (soc_rbatt < 0)
1241 soc_rbatt = 0;
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001242 params->rbatt_mohm = get_rbatt(chip, soc_rbatt, batt_temp);
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001243 pr_debug("rbatt_mohm = %d\n", params->rbatt_mohm);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001244
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07001245 if (params->rbatt_mohm != chip->rbatt_mohm
1246 && chip->bms_psy.name != NULL) {
1247 chip->rbatt_mohm = params->rbatt_mohm;
1248 power_supply_changed(&chip->bms_psy);
1249 }
1250
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001251 calculate_iavg(chip, params->cc_uah, &params->iavg_ua,
1252 params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001253
1254 params->uuc_uah = calculate_unusable_charge_uah(chip, params,
1255 batt_temp);
1256 pr_debug("UUC = %uuAh\n", params->uuc_uah);
1257}
1258
1259static bool is_shutdown_soc_within_limits(struct qpnp_bms_chip *chip, int soc)
1260{
1261 if (chip->shutdown_soc_invalid) {
1262 pr_debug("NOT forcing shutdown soc = %d\n", chip->shutdown_soc);
1263 return 0;
1264 }
1265
1266 if (abs(chip->shutdown_soc - soc) > chip->shutdown_soc_valid_limit) {
1267 pr_debug("rejecting shutdown soc = %d, soc = %d limit = %d\n",
1268 chip->shutdown_soc, soc,
1269 chip->shutdown_soc_valid_limit);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001270 chip->shutdown_soc_invalid = true;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001271 return 0;
1272 }
1273
1274 return 1;
1275}
1276
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001277static int bound_soc(int soc)
1278{
1279 soc = max(0, soc);
1280 soc = min(100, soc);
1281 return soc;
1282}
1283
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001284#define IBAT_TOL_MASK 0x0F
1285#define OCV_TOL_MASK 0xF0
1286#define IBAT_TOL_DEFAULT 0x03
1287#define IBAT_TOL_NOCHG 0x0F
1288#define OCV_TOL_DEFAULT 0x20
1289#define OCV_TOL_NO_OCV 0x00
1290static int stop_ocv_updates(struct qpnp_bms_chip *chip)
1291{
1292 pr_debug("stopping ocv updates\n");
1293 return qpnp_masked_write(chip, BMS1_TOL_CTL,
1294 OCV_TOL_MASK, OCV_TOL_NO_OCV);
1295}
1296
1297static int reset_bms_for_test(struct qpnp_bms_chip *chip)
1298{
Xiaozhe Shi95da77f2013-02-20 13:40:06 -08001299 int ibat_ua = 0, vbat_uv = 0, rc;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001300 int ocv_est_uv;
1301
1302 if (!chip) {
1303 pr_err("BMS driver has not been initialized yet!\n");
1304 return -EINVAL;
1305 }
1306
1307 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
1308
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07001309 /*
1310 * Don't include rbatt and rbatt_capacitative since we expect this to
1311 * be used with a fake battery which does not have internal resistances
1312 */
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001313 ocv_est_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
1314 pr_debug("forcing ocv to be %d due to bms reset mode\n", ocv_est_uv);
1315 chip->last_ocv_uv = ocv_est_uv;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001316 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001317 chip->last_soc = -EINVAL;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001318 chip->last_soc_invalid = true;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001319 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001320 reset_cc(chip);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001321 chip->software_cc_uah = 0;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001322 chip->last_cc_uah = INT_MIN;
1323 stop_ocv_updates(chip);
1324
1325 pr_debug("bms reset to ocv = %duv vbat_ua = %d ibat_ua = %d\n",
1326 chip->last_ocv_uv, vbat_uv, ibat_ua);
1327
1328 return rc;
1329}
1330
1331static int bms_reset_set(const char *val, const struct kernel_param *kp)
1332{
1333 int rc;
1334
1335 rc = param_set_bool(val, kp);
1336 if (rc) {
1337 pr_err("Unable to set bms_reset: %d\n", rc);
1338 return rc;
1339 }
1340
1341 if (*(bool *)kp->arg) {
1342 struct power_supply *bms_psy = power_supply_get_by_name("bms");
1343 struct qpnp_bms_chip *chip = container_of(bms_psy,
1344 struct qpnp_bms_chip, bms_psy);
1345
1346 rc = reset_bms_for_test(chip);
1347 if (rc) {
1348 pr_err("Unable to modify bms_reset: %d\n", rc);
1349 return rc;
1350 }
1351 }
1352 return 0;
1353}
1354
1355static struct kernel_param_ops bms_reset_ops = {
1356 .set = bms_reset_set,
1357 .get = param_get_bool,
1358};
1359
1360module_param_cb(bms_reset, &bms_reset_ops, &bms_reset, 0644);
1361
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001362#define VBATT_ERROR_MARGIN 20000
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001363static int charging_adjustments(struct qpnp_bms_chip *chip,
1364 struct soc_params *params, int soc,
1365 int vbat_uv, int ibat_ua, int batt_temp)
1366{
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001367 int chg_soc, batt_terminal_uv;
1368
1369 batt_terminal_uv = vbat_uv + VBATT_ERROR_MARGIN
1370 + (ibat_ua * chip->r_conn_mohm) / 1000;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001371
1372 if (chip->soc_at_cv == -EINVAL) {
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08001373 if (batt_terminal_uv >= chip->max_voltage_uv) {
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001374 chip->soc_at_cv = soc;
1375 chip->prev_chg_soc = soc;
1376 chip->ibat_at_cv_ua = ibat_ua;
1377 pr_debug("CC_TO_CV ibat_ua = %d CHG SOC %d\n",
1378 ibat_ua, soc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001379 } else {
1380 /* In constant current charging return the calc soc */
1381 pr_debug("CC CHG SOC %d\n", soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001382 }
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001383
1384 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001385 return soc;
1386 }
1387
1388 /*
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001389 * battery is in CV phase - begin linear interpolation of soc based on
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001390 * battery charge current
1391 */
1392
1393 /*
1394 * if voltage lessened (possibly because of a system load)
1395 * keep reporting the prev chg soc
1396 */
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001397 if (batt_terminal_uv <= chip->prev_batt_terminal_uv) {
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08001398 pr_debug("batt_terminal_uv %d < (max = %d - 10000); CC CHG SOC %d\n",
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001399 batt_terminal_uv, chip->prev_batt_terminal_uv,
1400 chip->prev_chg_soc);
1401 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001402 return chip->prev_chg_soc;
1403 }
1404
1405 chg_soc = linear_interpolate(chip->soc_at_cv, chip->ibat_at_cv_ua,
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08001406 100, -1 * chip->chg_term_ua,
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001407 ibat_ua);
Xiaozhe Shi78d0c532012-12-10 13:02:14 -08001408 chg_soc = bound_soc(chg_soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001409
1410 /* always report a higher soc */
1411 if (chg_soc > chip->prev_chg_soc) {
1412 int new_ocv_uv;
1413
1414 chip->prev_chg_soc = chg_soc;
1415
1416 find_ocv_for_soc(chip, params, batt_temp, chg_soc, &new_ocv_uv);
1417 chip->last_ocv_uv = new_ocv_uv;
1418 pr_debug("CC CHG ADJ OCV = %d CHG SOC %d\n",
1419 new_ocv_uv,
1420 chip->prev_chg_soc);
1421 }
1422
1423 pr_debug("Reporting CHG SOC %d\n", chip->prev_chg_soc);
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001424 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001425 return chip->prev_chg_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001426}
1427
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001428static void very_low_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
1429{
1430 /*
1431 * if battery is very low (v_cutoff voltage + 20mv) hold
1432 * a wakelock untill soc = 0%
1433 */
1434 if (vbat_uv <= chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001435 && !wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001436 pr_debug("voltage = %d low holding wakelock\n", vbat_uv);
1437 wake_lock(&chip->low_voltage_wake_lock);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001438 } else if (vbat_uv > chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001439 && wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001440 pr_debug("voltage = %d releasing wakelock\n", vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001441 wake_unlock(&chip->low_voltage_wake_lock);
1442 }
1443}
1444
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001445#define VBATT_ERROR_MARGIN 20000
1446static void cv_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
1447{
1448 /*
1449 * if battery is very low (v_cutoff voltage + 20mv) hold
1450 * a wakelock untill soc = 0%
1451 */
1452 if (wake_lock_active(&chip->cv_wake_lock)) {
1453 if (chip->soc_at_cv != -EINVAL) {
1454 pr_debug("hit CV, releasing cv wakelock\n");
1455 wake_unlock(&chip->cv_wake_lock);
1456 } else if (!is_battery_charging(chip)) {
1457 pr_debug("charging stopped, releasing cv wakelock\n");
1458 wake_unlock(&chip->cv_wake_lock);
1459 }
1460 } else if (vbat_uv > chip->max_voltage_uv - VBATT_ERROR_MARGIN
1461 && chip->soc_at_cv == -EINVAL
1462 && is_battery_charging(chip)
1463 && !wake_lock_active(&chip->cv_wake_lock)) {
1464 pr_debug("voltage = %d holding cv wakelock\n", vbat_uv);
1465 wake_lock(&chip->cv_wake_lock);
1466 }
1467}
1468
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07001469#define NO_ADJUST_HIGH_SOC_THRESHOLD 90
Xiaozhe Shie118c692012-09-24 15:17:43 -07001470static int adjust_soc(struct qpnp_bms_chip *chip, struct soc_params *params,
1471 int soc, int batt_temp)
1472{
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001473 int ibat_ua = 0, vbat_uv = 0;
1474 int ocv_est_uv = 0, soc_est = 0, pc_est = 0, pc = 0;
1475 int delta_ocv_uv = 0;
1476 int n = 0;
1477 int rc_new_uah = 0;
1478 int pc_new = 0;
1479 int soc_new = 0;
1480 int slope = 0;
1481 int rc = 0;
1482 int delta_ocv_uv_limit = 0;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07001483 int correction_limit_uv = 0;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001484
1485 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
1486 if (rc < 0) {
1487 pr_err("simultaneous vbat ibat failed err = %d\n", rc);
1488 goto out;
1489 }
1490
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001491 very_low_voltage_check(chip, vbat_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001492 cv_voltage_check(chip, vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001493
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001494 delta_ocv_uv_limit = DIV_ROUND_CLOSEST(ibat_ua, 1000);
1495
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001496 ocv_est_uv = vbat_uv + (ibat_ua * params->rbatt_mohm)/1000;
1497
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001498 pc_est = calculate_pc(chip, ocv_est_uv, batt_temp);
1499 soc_est = div_s64((s64)params->fcc_uah * pc_est - params->uuc_uah*100,
1500 (s64)params->fcc_uah - params->uuc_uah);
1501 soc_est = bound_soc(soc_est);
1502
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001503 /* never adjust during bms reset mode */
1504 if (bms_reset) {
1505 pr_debug("bms reset mode, SOC adjustment skipped\n");
1506 goto out;
1507 }
1508
Xiaozhe Shi8658c982013-04-30 11:33:07 -07001509 if (ibat_ua < 0 && !is_battery_full(chip)) {
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001510 soc = charging_adjustments(chip, params, soc, vbat_uv, ibat_ua,
1511 batt_temp);
1512 goto out;
1513 }
1514
1515 /*
1516 * do not adjust
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07001517 * if soc_est is same as what bms calculated
1518 * OR if soc_est > adjust_soc_low_threshold
1519 * OR if soc is above 90
1520 * because we might pull it low
1521 * and cause a bad user experience
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001522 */
1523 if (soc_est == soc
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07001524 || soc_est > chip->adjust_soc_low_threshold
1525 || soc >= NO_ADJUST_HIGH_SOC_THRESHOLD)
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001526 goto out;
1527
1528 if (chip->last_soc_est == -EINVAL)
1529 chip->last_soc_est = soc;
1530
1531 n = min(200, max(1 , soc + soc_est + chip->last_soc_est));
1532 chip->last_soc_est = soc_est;
1533
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001534 pc = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001535 if (pc > 0) {
1536 pc_new = calculate_pc(chip,
1537 chip->last_ocv_uv - (++slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001538 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001539 while (pc_new == pc) {
1540 /* start taking 10mV steps */
1541 slope = slope + 10;
1542 pc_new = calculate_pc(chip,
1543 chip->last_ocv_uv - (slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001544 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001545 }
1546 } else {
1547 /*
1548 * pc is already at the lowest point,
1549 * assume 1 millivolt translates to 1% pc
1550 */
1551 pc = 1;
1552 pc_new = 0;
1553 slope = 1;
1554 }
1555
1556 delta_ocv_uv = div_s64((soc - soc_est) * (s64)slope * 1000,
1557 n * (pc - pc_new));
1558
1559 if (abs(delta_ocv_uv) > delta_ocv_uv_limit) {
1560 pr_debug("limiting delta ocv %d limit = %d\n", delta_ocv_uv,
1561 delta_ocv_uv_limit);
1562
1563 if (delta_ocv_uv > 0)
1564 delta_ocv_uv = delta_ocv_uv_limit;
1565 else
1566 delta_ocv_uv = -1 * delta_ocv_uv_limit;
1567 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
1568 }
1569
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07001570 if (chip->last_ocv_uv > chip->flat_ocv_threshold_uv)
1571 correction_limit_uv = chip->high_ocv_correction_limit_uv;
1572 else
1573 correction_limit_uv = chip->low_ocv_correction_limit_uv;
1574
1575 if (abs(delta_ocv_uv) > correction_limit_uv) {
1576 pr_debug("limiting delta ocv %d limit = %d\n",
1577 delta_ocv_uv, correction_limit_uv);
1578 if (delta_ocv_uv > 0)
1579 delta_ocv_uv = correction_limit_uv;
1580 else
1581 delta_ocv_uv = -correction_limit_uv;
1582 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
1583 }
1584
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001585 chip->last_ocv_uv -= delta_ocv_uv;
1586
1587 if (chip->last_ocv_uv >= chip->max_voltage_uv)
1588 chip->last_ocv_uv = chip->max_voltage_uv;
1589
1590 /* calculate the soc based on this new ocv */
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001591 pc_new = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001592 rc_new_uah = (params->fcc_uah * pc_new) / 100;
1593 soc_new = (rc_new_uah - params->cc_uah - params->uuc_uah)*100
1594 / (params->fcc_uah - params->uuc_uah);
1595 soc_new = bound_soc(soc_new);
1596
1597 /*
1598 * if soc_new is ZERO force it higher so that phone doesnt report soc=0
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07001599 * soc = 0 should happen only when soc_est is above a set value
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001600 */
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07001601 if (soc_new == 0 && soc_est >= chip->hold_soc_est)
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001602 soc_new = 1;
1603
1604 soc = soc_new;
1605
1606out:
1607 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",
1608 ibat_ua, vbat_uv, ocv_est_uv, pc_est,
1609 soc_est, n, delta_ocv_uv, chip->last_ocv_uv,
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001610 pc_new, soc_new, params->rbatt_mohm, slope);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001611
Xiaozhe Shie118c692012-09-24 15:17:43 -07001612 return soc;
1613}
1614
Xiaozhe Shi2542c602012-11-28 10:08:07 -08001615static int clamp_soc_based_on_voltage(struct qpnp_bms_chip *chip, int soc)
1616{
1617 int rc, vbat_uv;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08001618
Xiaozhe Shi36458962013-02-06 16:19:57 -08001619 rc = get_battery_voltage(&vbat_uv);
1620 if (rc < 0) {
1621 pr_err("adc vbat failed err = %d\n", rc);
1622 return soc;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08001623 }
Xiaozhe Shi2542c602012-11-28 10:08:07 -08001624 if (soc == 0 && vbat_uv > chip->v_cutoff_uv) {
1625 pr_debug("clamping soc to 1, vbat (%d) > cutoff (%d)\n",
1626 vbat_uv, chip->v_cutoff_uv);
1627 return 1;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08001628 } else {
1629 pr_debug("not clamping, using soc = %d, vbat = %d and cutoff = %d\n",
1630 soc, vbat_uv, chip->v_cutoff_uv);
1631 return soc;
1632 }
1633}
1634
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001635#define SLEEP_RECALC_INTERVAL 3
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001636static int calculate_state_of_charge(struct qpnp_bms_chip *chip,
1637 struct raw_soc_params *raw,
1638 int batt_temp)
1639{
Xiaozhe Shie118c692012-09-24 15:17:43 -07001640 int soc, new_ocv_uv;
1641 int shutdown_soc, new_calculated_soc, remaining_usable_charge_uah;
1642 struct soc_params params;
1643
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001644 if (!is_battery_present(chip)) {
Xiaozhe Shi026fa9b2013-03-22 17:00:50 -07001645 pr_debug("battery gone, reporting 100\n");
1646 new_calculated_soc = 100;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001647 goto done_calculating;
1648 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07001649 calculate_soc_params(chip, raw, &params, batt_temp);
1650 /* calculate remaining usable charge */
1651 remaining_usable_charge_uah = params.ocv_charge_uah
1652 - params.cc_uah
1653 - params.uuc_uah;
1654
1655 pr_debug("RUC = %duAh\n", remaining_usable_charge_uah);
1656 if (params.fcc_uah - params.uuc_uah <= 0) {
Xiaozhe Shicb386a22012-11-29 12:11:42 -08001657 pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
Xiaozhe Shie118c692012-09-24 15:17:43 -07001658 params.fcc_uah,
1659 params.uuc_uah);
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08001660 new_calculated_soc = 0;
1661 goto done_calculating;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001662 }
1663
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08001664 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
1665 (params.fcc_uah - params.uuc_uah));
1666
Xiaozhe Shie118c692012-09-24 15:17:43 -07001667 if (chip->first_time_calc_soc && soc < 0) {
1668 /*
1669 * first time calcualtion and the pon ocv is too low resulting
1670 * in a bad soc. Adjust ocv to get 0 soc
1671 */
1672 pr_debug("soc is %d, adjusting pon ocv to make it 0\n", soc);
1673 find_ocv_for_soc(chip, &params, batt_temp, 0, &new_ocv_uv);
1674 chip->last_ocv_uv = new_ocv_uv;
1675
1676 remaining_usable_charge_uah = params.ocv_charge_uah
1677 - params.cc_uah
1678 - params.uuc_uah;
1679
1680 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
1681 (params.fcc_uah
1682 - params.uuc_uah));
1683 pr_debug("DONE for O soc is %d, pon ocv adjusted to %duV\n",
1684 soc, chip->last_ocv_uv);
1685 }
1686
1687 if (soc > 100)
1688 soc = 100;
1689
1690 if (soc < 0) {
Xiaozhe Shicb386a22012-11-29 12:11:42 -08001691 pr_debug("bad rem_usb_chg = %d rem_chg %d, cc_uah %d, unusb_chg %d\n",
Xiaozhe Shie118c692012-09-24 15:17:43 -07001692 remaining_usable_charge_uah,
1693 params.ocv_charge_uah,
1694 params.cc_uah, params.uuc_uah);
1695
Xiaozhe Shicb386a22012-11-29 12:11:42 -08001696 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 -07001697 chip->last_ocv_uv, batt_temp,
1698 params.fcc_uah, soc);
1699 soc = 0;
1700 }
1701
1702 mutex_lock(&chip->soc_invalidation_mutex);
1703 shutdown_soc = chip->shutdown_soc;
1704
1705 if (chip->first_time_calc_soc && soc != shutdown_soc
1706 && is_shutdown_soc_within_limits(chip, soc)) {
1707 /*
1708 * soc for the first time - use shutdown soc
1709 * to adjust pon ocv since it is a small percent away from
1710 * the real soc
1711 */
1712 pr_debug("soc = %d before forcing shutdown_soc = %d\n",
1713 soc, shutdown_soc);
1714 find_ocv_for_soc(chip, &params, batt_temp,
1715 shutdown_soc, &new_ocv_uv);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001716 chip->last_ocv_uv = new_ocv_uv;
1717
1718 remaining_usable_charge_uah = params.ocv_charge_uah
1719 - params.cc_uah
1720 - params.uuc_uah;
1721
1722 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
1723 (params.fcc_uah
1724 - params.uuc_uah));
1725
1726 pr_debug("DONE for shutdown_soc = %d soc is %d, adjusted ocv to %duV\n",
1727 shutdown_soc, soc, chip->last_ocv_uv);
1728 }
1729 mutex_unlock(&chip->soc_invalidation_mutex);
1730
1731 pr_debug("SOC before adjustment = %d\n", soc);
1732 new_calculated_soc = adjust_soc(chip, &params, soc, batt_temp);
1733
Xiaozhe Shi445d2492013-03-27 18:10:18 -07001734 /* always clamp soc due to BMS hw/sw immaturities */
1735 new_calculated_soc = clamp_soc_based_on_voltage(chip,
1736 new_calculated_soc);
Xiaozhe Shi2542c602012-11-28 10:08:07 -08001737
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08001738done_calculating:
Xiaozhe Shie118c692012-09-24 15:17:43 -07001739 if (new_calculated_soc != chip->calculated_soc
1740 && chip->bms_psy.name != NULL) {
1741 power_supply_changed(&chip->bms_psy);
1742 pr_debug("power supply changed\n");
1743 }
1744
1745 chip->calculated_soc = new_calculated_soc;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001746 pr_debug("CC based calculated SOC = %d\n", chip->calculated_soc);
1747 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001748 if (chip->last_soc_invalid) {
1749 chip->last_soc_invalid = false;
1750 chip->last_soc = -EINVAL;
1751 }
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001752 /*
1753 * Check if more than a long time has passed since the last
1754 * calculation (more than n times compared to the soc recalculation
1755 * rate, where n is defined by SLEEP_RECALC_INTERVAL). If this is true,
1756 * then the system must have gone through a long sleep, and SoC can be
1757 * allowed to become unbounded by the last reported SoC
1758 */
1759 if (params.delta_time_s * 1000 >
1760 chip->calculate_soc_ms * SLEEP_RECALC_INTERVAL
1761 && !chip->first_time_calc_soc) {
1762 chip->last_soc_unbound = true;
1763 chip->last_soc_change_sec = chip->last_recalc_time;
1764 pr_debug("last_soc unbound because elapsed time = %d\n",
1765 params.delta_time_s);
1766 }
1767 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shicdeee312012-12-18 15:10:18 -08001768 get_current_time(&chip->last_recalc_time);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001769 chip->first_time_calc_soc = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001770 return chip->calculated_soc;
1771}
1772
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08001773static int calculate_soc_from_voltage(struct qpnp_bms_chip *chip)
1774{
1775 int voltage_range_uv, voltage_remaining_uv, voltage_based_soc;
Xiaozhe Shi36458962013-02-06 16:19:57 -08001776 int rc, vbat_uv;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08001777
Xiaozhe Shi36458962013-02-06 16:19:57 -08001778 rc = get_battery_voltage(&vbat_uv);
1779 if (rc < 0) {
1780 pr_err("adc vbat failed err = %d\n", rc);
1781 return rc;
1782 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08001783 voltage_range_uv = chip->max_voltage_uv - chip->v_cutoff_uv;
1784 voltage_remaining_uv = vbat_uv - chip->v_cutoff_uv;
1785 voltage_based_soc = voltage_remaining_uv * 100 / voltage_range_uv;
1786
1787 voltage_based_soc = clamp(voltage_based_soc, 0, 100);
1788
1789 if (chip->prev_voltage_based_soc != voltage_based_soc
1790 && chip->bms_psy.name != NULL) {
1791 power_supply_changed(&chip->bms_psy);
1792 pr_debug("power supply changed\n");
1793 }
1794 chip->prev_voltage_based_soc = voltage_based_soc;
1795
1796 pr_debug("vbat used = %duv\n", vbat_uv);
1797 pr_debug("Calculated voltage based soc = %d\n", voltage_based_soc);
1798 return voltage_based_soc;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08001799}
1800
Xiaozhe Shicdeee312012-12-18 15:10:18 -08001801static int recalculate_soc(struct qpnp_bms_chip *chip)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001802{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001803 int batt_temp, rc, soc;
1804 struct qpnp_vadc_result result;
1805 struct raw_soc_params raw;
1806
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001807 if (!wake_lock_active(&chip->soc_wake_lock))
1808 wake_lock(&chip->soc_wake_lock);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001809 mutex_lock(&chip->vbat_monitor_mutex);
1810 qpnp_adc_tm_channel_measure(&chip->vbat_monitor_params);
1811 mutex_unlock(&chip->vbat_monitor_mutex);
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08001812 if (chip->use_voltage_soc) {
1813 soc = calculate_soc_from_voltage(chip);
1814 } else {
Xiaozhe Shi535494d2013-04-05 12:27:51 -07001815 qpnp_iadc_calibrate_for_trim();
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08001816 rc = qpnp_vadc_read(LR_MUX1_BATT_THERM, &result);
1817 if (rc) {
1818 pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n",
1819 LR_MUX1_BATT_THERM, rc);
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08001820 soc = chip->calculated_soc;
1821 } else {
1822 pr_debug("batt_temp phy = %lld meas = 0x%llx\n",
1823 result.physical,
1824 result.measurement);
1825 batt_temp = (int)result.physical;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001826
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08001827 mutex_lock(&chip->last_ocv_uv_mutex);
1828 read_soc_params_raw(chip, &raw, batt_temp);
1829 soc = calculate_state_of_charge(chip, &raw, batt_temp);
1830 mutex_unlock(&chip->last_ocv_uv_mutex);
1831 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08001832 }
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08001833 wake_unlock(&chip->soc_wake_lock);
Xiaozhe Shicdeee312012-12-18 15:10:18 -08001834 return soc;
1835}
1836
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001837static void recalculate_work(struct work_struct *work)
1838{
1839 struct qpnp_bms_chip *chip = container_of(work,
1840 struct qpnp_bms_chip,
1841 recalc_work);
1842
1843 recalculate_soc(chip);
1844}
1845
Xiaozhe Shicdeee312012-12-18 15:10:18 -08001846static void calculate_soc_work(struct work_struct *work)
1847{
1848 struct qpnp_bms_chip *chip = container_of(work,
1849 struct qpnp_bms_chip,
1850 calculate_soc_delayed_work.work);
1851 int soc = recalculate_soc(chip);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001852
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001853 if (soc < chip->low_soc_calc_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001854 || wake_lock_active(&chip->low_voltage_wake_lock))
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001855 schedule_delayed_work(&chip->calculate_soc_delayed_work,
1856 round_jiffies_relative(msecs_to_jiffies
1857 (chip->low_soc_calculate_soc_ms)));
1858 else
1859 schedule_delayed_work(&chip->calculate_soc_delayed_work,
1860 round_jiffies_relative(msecs_to_jiffies
1861 (chip->calculate_soc_ms)));
1862}
1863
Xiaozhe Shie118c692012-09-24 15:17:43 -07001864static void backup_soc_and_iavg(struct qpnp_bms_chip *chip, int batt_temp,
1865 int soc)
1866{
1867 u8 temp;
1868 int rc;
1869 int iavg_ma = chip->prev_uuc_iavg_ma;
1870
1871 if (iavg_ma > IAVG_START)
1872 temp = (iavg_ma - IAVG_START) / IAVG_STEP_SIZE_MA;
1873 else
1874 temp = 0;
1875
1876 rc = qpnp_write_wrapper(chip, &temp,
1877 chip->base + IAVG_STORAGE_REG, 1);
1878
Xiaozhe Shic7cbd052013-03-29 12:03:11 -07001879 temp = soc;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001880
1881 /* don't store soc if temperature is below 5degC */
1882 if (batt_temp > IGNORE_SOC_TEMP_DECIDEG)
1883 rc = qpnp_write_wrapper(chip, &temp,
1884 chip->base + SOC_STORAGE_REG, 1);
1885}
1886
1887#define SOC_CATCHUP_SEC_MAX 600
1888#define SOC_CATCHUP_SEC_PER_PERCENT 60
1889#define MAX_CATCHUP_SOC (SOC_CATCHUP_SEC_MAX/SOC_CATCHUP_SEC_PER_PERCENT)
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001890static int scale_soc_while_chg(struct qpnp_bms_chip *chip, int chg_time_sec,
1891 int catch_up_sec, int new_soc, int prev_soc)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001892{
Xiaozhe Shie118c692012-09-24 15:17:43 -07001893 int scaled_soc;
1894 int numerator;
1895
1896 /*
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001897 * Don't report a high value immediately slowly scale the
Xiaozhe Shie118c692012-09-24 15:17:43 -07001898 * value from prev_soc to the new soc based on a charge time
1899 * weighted average
1900 */
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001901 pr_debug("cts = %d catch_up_sec = %d\n", chg_time_sec, catch_up_sec);
Xiaozhe Shi4532d2d2012-12-17 19:34:50 -08001902 if (catch_up_sec == 0)
1903 return new_soc;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001904 /*
1905 * if charging for more than catch_up time, simply return
1906 * new soc
1907 */
1908 if (chg_time_sec > catch_up_sec)
1909 return new_soc;
1910
1911 numerator = (catch_up_sec - chg_time_sec) * prev_soc
1912 + chg_time_sec * new_soc;
1913 scaled_soc = numerator / catch_up_sec;
1914
1915 pr_debug("cts = %d new_soc = %d prev_soc = %d scaled_soc = %d\n",
1916 chg_time_sec, new_soc, prev_soc, scaled_soc);
1917
1918 return scaled_soc;
1919}
1920
1921/*
1922 * bms_fake_battery is set in setups where a battery emulator is used instead
1923 * of a real battery. This makes the bms driver report a different/fake value
1924 * regardless of the calculated state of charge.
1925 */
1926static int bms_fake_battery = -EINVAL;
1927module_param(bms_fake_battery, int, 0644);
1928
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08001929static int report_voltage_based_soc(struct qpnp_bms_chip *chip)
1930{
1931 pr_debug("Reported voltage based soc = %d\n",
1932 chip->prev_voltage_based_soc);
1933 return chip->prev_voltage_based_soc;
1934}
1935
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001936#define SOC_CHANGE_PER_SEC 20
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08001937static int report_cc_based_soc(struct qpnp_bms_chip *chip)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001938{
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001939 int soc, soc_change;
1940 int time_since_last_change_sec, charge_time_sec = 0;
1941 unsigned long last_change_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001942 struct timespec now;
1943 struct qpnp_vadc_result result;
1944 int batt_temp;
1945 int rc;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001946 bool charging, charging_since_last_report;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001947
Xiaozhe Shie118c692012-09-24 15:17:43 -07001948 soc = chip->calculated_soc;
1949
1950 rc = qpnp_vadc_read(LR_MUX1_BATT_THERM, &result);
1951
1952 if (rc) {
1953 pr_err("error reading adc channel = %d, rc = %d\n",
1954 LR_MUX1_BATT_THERM, rc);
1955 return rc;
1956 }
1957 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
1958 result.measurement);
1959 batt_temp = (int)result.physical;
1960
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001961 mutex_lock(&chip->last_soc_mutex);
1962 last_change_sec = chip->last_soc_change_sec;
1963 calculate_delta_time(&last_change_sec, &time_since_last_change_sec);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001964
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001965 charging = is_battery_charging(chip);
1966 charging_since_last_report = charging || (chip->last_soc_unbound
1967 && chip->was_charging_at_sleep);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001968 /*
1969 * account for charge time - limit it to SOC_CATCHUP_SEC to
1970 * avoid overflows when charging continues for extended periods
1971 */
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001972 if (charging) {
1973 if (chip->charge_start_tm_sec == 0) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001974 /*
1975 * calculating soc for the first time
1976 * after start of chg. Initialize catchup time
1977 */
1978 if (abs(soc - chip->last_soc) < MAX_CATCHUP_SOC)
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001979 chip->catch_up_time_sec =
Xiaozhe Shie118c692012-09-24 15:17:43 -07001980 (soc - chip->last_soc)
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001981 * SOC_CATCHUP_SEC_PER_PERCENT;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001982 else
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001983 chip->catch_up_time_sec = SOC_CATCHUP_SEC_MAX;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001984
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001985 if (chip->catch_up_time_sec < 0)
1986 chip->catch_up_time_sec = 0;
1987 chip->charge_start_tm_sec = last_change_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001988 }
1989
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001990 charge_time_sec = min(SOC_CATCHUP_SEC_MAX, (int)last_change_sec
1991 - chip->charge_start_tm_sec);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001992
1993 /* end catchup if calculated soc and last soc are same */
1994 if (chip->last_soc == soc)
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001995 chip->catch_up_time_sec = 0;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001996 }
1997
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001998 if (chip->last_soc != -EINVAL) {
1999 /* last_soc < soc ... if we have not been charging at all
2000 * since the last time this was called, report previous SoC.
2001 * Otherwise, scale and catch up.
2002 */
2003 if (chip->last_soc < soc && !charging_since_last_report)
2004 soc = chip->last_soc;
2005 else if (chip->last_soc < soc && soc != 100)
2006 soc = scale_soc_while_chg(chip, charge_time_sec,
2007 chip->catch_up_time_sec,
2008 soc, chip->last_soc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002009
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002010 soc_change = min((int)abs(chip->last_soc - soc),
2011 time_since_last_change_sec / SOC_CHANGE_PER_SEC);
2012 if (chip->last_soc_unbound) {
2013 chip->last_soc_unbound = false;
2014 } else {
2015 /*
2016 * if soc have not been unbound by resume,
2017 * only change reported SoC by 1.
2018 */
2019 soc_change = min(1, soc_change);
2020 }
2021
Xiaozhe Shicc137262013-03-10 06:21:41 -07002022 if (soc < chip->last_soc && soc != 0)
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002023 soc = chip->last_soc - soc_change;
Xiaozhe Shicc137262013-03-10 06:21:41 -07002024 if (soc > chip->last_soc && soc != 100)
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002025 soc = chip->last_soc + soc_change;
Xiaozhe Shicc137262013-03-10 06:21:41 -07002026 }
2027
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002028 if (chip->last_soc != soc)
2029 chip->last_soc_change_sec = last_change_sec;
2030
2031 pr_debug("last_soc = %d, calculated_soc = %d, soc = %d, time since last change = %d\n",
2032 chip->last_soc, chip->calculated_soc,
2033 soc, time_since_last_change_sec);
Xiaozhe Shicc137262013-03-10 06:21:41 -07002034 chip->last_soc = bound_soc(soc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002035 backup_soc_and_iavg(chip, batt_temp, chip->last_soc);
2036 pr_debug("Reported SOC = %d\n", chip->last_soc);
2037 chip->t_soc_queried = now;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002038 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002039
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002040 return soc;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002041}
2042
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002043static int report_state_of_charge(struct qpnp_bms_chip *chip)
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08002044{
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002045 if (bms_fake_battery != -EINVAL) {
2046 pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery);
2047 return bms_fake_battery;
2048 } else if (chip->use_voltage_soc)
2049 return report_voltage_based_soc(chip);
2050 else
2051 return report_cc_based_soc(chip);
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08002052}
2053
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002054static void configure_vbat_monitor_low(struct qpnp_bms_chip *chip)
2055{
2056 mutex_lock(&chip->vbat_monitor_mutex);
2057 if (chip->vbat_monitor_params.state_request
2058 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2059 /*
2060 * Battery is now around or below v_cutoff
2061 */
2062 pr_debug("battery entered cutoff range\n");
2063 if (!wake_lock_active(&chip->low_voltage_wake_lock)) {
2064 pr_debug("voltage low, holding wakelock\n");
2065 wake_lock(&chip->low_voltage_wake_lock);
2066 cancel_delayed_work_sync(
2067 &chip->calculate_soc_delayed_work);
2068 schedule_delayed_work(
2069 &chip->calculate_soc_delayed_work, 0);
2070 }
2071 chip->vbat_monitor_params.state_request =
2072 ADC_TM_HIGH_THR_ENABLE;
2073 chip->vbat_monitor_params.high_thr =
2074 (chip->low_voltage_threshold + VBATT_ERROR_MARGIN);
2075 pr_debug("set low thr to %d and high to %d\n",
2076 chip->vbat_monitor_params.low_thr,
2077 chip->vbat_monitor_params.high_thr);
2078 chip->vbat_monitor_params.low_thr = 0;
2079 } else if (chip->vbat_monitor_params.state_request
2080 == ADC_TM_LOW_THR_ENABLE) {
2081 /*
2082 * Battery is in normal operation range.
2083 */
2084 pr_debug("battery entered normal range\n");
2085 if (wake_lock_active(&chip->cv_wake_lock)) {
2086 wake_unlock(&chip->cv_wake_lock);
2087 pr_debug("releasing cv wake lock\n");
2088 }
2089 chip->in_cv_range = false;
2090 chip->vbat_monitor_params.state_request =
2091 ADC_TM_HIGH_LOW_THR_ENABLE;
2092 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2093 - VBATT_ERROR_MARGIN;
2094 chip->vbat_monitor_params.low_thr =
2095 chip->low_voltage_threshold;
2096 pr_debug("set low thr to %d and high to %d\n",
2097 chip->vbat_monitor_params.low_thr,
2098 chip->vbat_monitor_params.high_thr);
2099 }
2100 qpnp_adc_tm_channel_measure(&chip->vbat_monitor_params);
2101 mutex_unlock(&chip->vbat_monitor_mutex);
2102}
2103
2104#define CV_LOW_THRESHOLD_HYST_UV 100000
2105static void configure_vbat_monitor_high(struct qpnp_bms_chip *chip)
2106{
2107 mutex_lock(&chip->vbat_monitor_mutex);
2108 if (chip->vbat_monitor_params.state_request
2109 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2110 /*
2111 * Battery is around vddmax
2112 */
2113 pr_debug("battery entered vddmax range\n");
2114 chip->in_cv_range = true;
2115 if (!wake_lock_active(&chip->cv_wake_lock)) {
2116 wake_lock(&chip->cv_wake_lock);
2117 pr_debug("holding cv wake lock\n");
2118 }
2119 schedule_work(&chip->recalc_work);
2120 chip->vbat_monitor_params.state_request =
2121 ADC_TM_LOW_THR_ENABLE;
2122 chip->vbat_monitor_params.low_thr =
2123 (chip->max_voltage_uv - CV_LOW_THRESHOLD_HYST_UV);
2124 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv * 2;
2125 pr_debug("set low thr to %d and high to %d\n",
2126 chip->vbat_monitor_params.low_thr,
2127 chip->vbat_monitor_params.high_thr);
2128 } else if (chip->vbat_monitor_params.state_request
2129 == ADC_TM_HIGH_THR_ENABLE) {
2130 /*
2131 * Battery is in normal operation range.
2132 */
2133 pr_debug("battery entered normal range\n");
2134 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2135 pr_debug("voltage high, releasing wakelock\n");
2136 wake_unlock(&chip->low_voltage_wake_lock);
2137 }
2138 chip->vbat_monitor_params.state_request =
2139 ADC_TM_HIGH_LOW_THR_ENABLE;
2140 chip->vbat_monitor_params.high_thr =
2141 chip->max_voltage_uv - VBATT_ERROR_MARGIN;
2142 chip->vbat_monitor_params.low_thr =
2143 chip->low_voltage_threshold;
2144 pr_debug("set low thr to %d and high to %d\n",
2145 chip->vbat_monitor_params.low_thr,
2146 chip->vbat_monitor_params.high_thr);
2147 }
2148 qpnp_adc_tm_channel_measure(&chip->vbat_monitor_params);
2149 mutex_unlock(&chip->vbat_monitor_mutex);
2150}
2151
2152static void btm_notify_vbat(enum qpnp_tm_state state, void *ctx)
2153{
2154 struct qpnp_bms_chip *chip = ctx;
2155 int vbat_uv;
2156 struct qpnp_vadc_result result;
2157 int rc;
2158
2159 rc = qpnp_vadc_read(VBAT_SNS, &result);
2160 pr_debug("vbat = %lld, raw = 0x%x\n", result.physical, result.adc_code);
2161
2162 get_battery_voltage(&vbat_uv);
2163 pr_debug("vbat is at %d, state is at %d\n", vbat_uv, state);
2164
2165 if (state == ADC_TM_LOW_STATE) {
2166 pr_debug("low voltage btm notification triggered\n");
2167 if (vbat_uv - VBATT_ERROR_MARGIN
2168 < chip->vbat_monitor_params.low_thr) {
2169 configure_vbat_monitor_low(chip);
2170 } else {
2171 pr_debug("faulty btm trigger, discarding\n");
2172 qpnp_adc_tm_channel_measure(
2173 &chip->vbat_monitor_params);
2174 }
2175 } else if (state == ADC_TM_HIGH_STATE) {
2176 pr_debug("high voltage btm notification triggered\n");
2177 if (vbat_uv + VBATT_ERROR_MARGIN
2178 > chip->vbat_monitor_params.high_thr) {
2179 configure_vbat_monitor_high(chip);
2180 } else {
2181 pr_debug("faulty btm trigger, discarding\n");
2182 qpnp_adc_tm_channel_measure(
2183 &chip->vbat_monitor_params);
2184 }
2185 } else {
2186 pr_debug("unknown voltage notification state: %d\n", state);
2187 }
2188 power_supply_changed(&chip->bms_psy);
2189}
2190
2191static int reset_vbat_monitoring(struct qpnp_bms_chip *chip)
2192{
2193 int rc;
2194
2195 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_DISABLE;
2196 rc = qpnp_adc_tm_channel_measure(&chip->vbat_monitor_params);
2197 if (rc) {
2198 pr_err("tm measure failed: %d\n", rc);
2199 return rc;
2200 }
2201 mutex_lock(&chip->vbat_monitor_mutex);
2202 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2203 pr_debug("battery removed, releasing wakelock\n");
2204 wake_unlock(&chip->low_voltage_wake_lock);
2205 }
2206 if (chip->in_cv_range) {
2207 pr_debug("battery removed, removing in_cv_range state\n");
2208 chip->in_cv_range = false;
2209 }
2210 mutex_unlock(&chip->vbat_monitor_mutex);
2211 return 0;
2212}
2213
2214static int setup_vbat_monitoring(struct qpnp_bms_chip *chip)
2215{
2216 int rc;
2217
2218 rc = qpnp_adc_tm_is_ready();
2219 if (rc) {
2220 pr_info("adc tm is not ready yet: %d, defer probe\n", rc);
2221 return -EPROBE_DEFER;
2222 }
2223
2224 if (!is_battery_present(chip)) {
2225 pr_debug("no battery inserted, do not setup vbat monitoring\n");
2226 return 0;
2227 }
2228
2229 chip->vbat_monitor_params.low_thr = chip->low_voltage_threshold;
2230 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2231 - VBATT_ERROR_MARGIN;
2232 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_ENABLE;
2233 chip->vbat_monitor_params.channel = VBAT_SNS;
2234 chip->vbat_monitor_params.btm_ctx = (void *)chip;
2235 chip->vbat_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
2236 chip->vbat_monitor_params.threshold_notification = &btm_notify_vbat;
2237 pr_debug("set low thr to %d and high to %d\n",
2238 chip->vbat_monitor_params.low_thr,
2239 chip->vbat_monitor_params.high_thr);
2240 rc = qpnp_adc_tm_channel_measure(&chip->vbat_monitor_params);
2241 if (rc) {
2242 pr_err("tm setup failed: %d\n", rc);
2243 return rc;
2244 }
2245 pr_debug("setup complete\n");
2246 return 0;
2247}
2248
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07002249static void charging_began(struct qpnp_bms_chip *chip)
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002250{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07002251 mutex_lock(&chip->last_soc_mutex);
2252 chip->charge_start_tm_sec = 0;
2253 chip->catch_up_time_sec = 0;
2254 mutex_unlock(&chip->last_soc_mutex);
2255
2256 mutex_lock(&chip->last_ocv_uv_mutex);
2257 chip->soc_at_cv = -EINVAL;
2258 chip->prev_chg_soc = -EINVAL;
2259 mutex_unlock(&chip->last_ocv_uv_mutex);
2260}
2261
2262static void charging_ended(struct qpnp_bms_chip *chip)
2263{
2264 mutex_lock(&chip->last_soc_mutex);
2265 chip->charge_start_tm_sec = 0;
2266 chip->catch_up_time_sec = 0;
2267 mutex_unlock(&chip->last_soc_mutex);
2268
2269 mutex_lock(&chip->last_ocv_uv_mutex);
2270 chip->soc_at_cv = -EINVAL;
2271 chip->prev_chg_soc = -EINVAL;
2272 if (get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL)
2273 chip->done_charging = true;
2274 mutex_unlock(&chip->last_ocv_uv_mutex);
2275}
2276
2277static void battery_status_check(struct qpnp_bms_chip *chip)
2278{
2279 int status = get_battery_status(chip);
2280
2281 if (chip->battery_status != status) {
2282 if (status == POWER_SUPPLY_STATUS_CHARGING) {
2283 pr_debug("charging started\n");
2284 charging_began(chip);
2285 } else if (chip->battery_status
2286 == POWER_SUPPLY_STATUS_CHARGING) {
2287 pr_debug("charging ended\n");
2288 charging_ended(chip);
2289 }
2290 chip->battery_status = status;
2291 /* a new battery was inserted or removed, so force a soc
2292 * recalculation to update the SoC */
2293 schedule_work(&chip->recalc_work);
2294 }
2295}
2296
2297static void battery_insertion_check(struct qpnp_bms_chip *chip)
2298{
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002299 bool present = is_battery_present(chip);
2300
2301 mutex_lock(&chip->vbat_monitor_mutex);
2302 if (chip->battery_present != present) {
2303 if (chip->battery_present != -EINVAL) {
2304 if (present) {
2305 setup_vbat_monitoring(chip);
2306 chip->new_battery = true;
2307 } else {
2308 reset_vbat_monitoring(chip);
2309 }
2310 }
2311 chip->battery_present = present;
2312 /* a new battery was inserted or removed, so force a soc
2313 * recalculation to update the SoC */
2314 schedule_work(&chip->recalc_work);
2315 }
2316 mutex_unlock(&chip->vbat_monitor_mutex);
2317}
2318
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002319/* Returns capacity as a SoC percentage between 0 and 100 */
2320static int get_prop_bms_capacity(struct qpnp_bms_chip *chip)
2321{
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002322 return report_state_of_charge(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002323}
2324
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07002325/* Returns estimated battery resistance */
2326static int get_prop_bms_batt_resistance(struct qpnp_bms_chip *chip)
2327{
2328 return chip->rbatt_mohm * 1000;
2329}
2330
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002331/* Returns instantaneous current in uA */
2332static int get_prop_bms_current_now(struct qpnp_bms_chip *chip)
2333{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002334 int rc, result_ua;
2335
2336 rc = get_battery_current(chip, &result_ua);
2337 if (rc) {
2338 pr_err("failed to get current: %d\n", rc);
2339 return rc;
2340 }
2341 return result_ua;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002342}
2343
2344/* Returns full charge design in uAh */
2345static int get_prop_bms_charge_full_design(struct qpnp_bms_chip *chip)
2346{
Xiaozhe Shi976618f2013-04-30 10:49:30 -07002347 return chip->fcc_mah * 1000;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002348}
2349
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002350static void qpnp_bms_external_power_changed(struct power_supply *psy)
2351{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07002352 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
2353 bms_psy);
2354
2355 battery_insertion_check(chip);
2356 battery_status_check(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002357}
2358
2359static int qpnp_bms_power_get_property(struct power_supply *psy,
2360 enum power_supply_property psp,
2361 union power_supply_propval *val)
2362{
2363 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
2364 bms_psy);
2365
2366 switch (psp) {
2367 case POWER_SUPPLY_PROP_CAPACITY:
2368 val->intval = get_prop_bms_capacity(chip);
2369 break;
2370 case POWER_SUPPLY_PROP_CURRENT_NOW:
2371 val->intval = get_prop_bms_current_now(chip);
2372 break;
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07002373 case POWER_SUPPLY_PROP_RESISTANCE:
2374 val->intval = get_prop_bms_batt_resistance(chip);
2375 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002376 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
2377 val->intval = get_prop_bms_charge_full_design(chip);
2378 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002379 default:
2380 return -EINVAL;
2381 }
2382 return 0;
2383}
2384
Xiaozhe Shibdf14742012-12-05 12:41:48 -08002385#define OCV_USE_LIMIT_EN BIT(7)
2386static int set_ocv_voltage_thresholds(struct qpnp_bms_chip *chip,
2387 int low_voltage_threshold,
2388 int high_voltage_threshold)
2389{
2390 uint16_t low_voltage_raw, high_voltage_raw;
2391 int rc;
2392
2393 low_voltage_raw = convert_vbatt_uv_to_raw(chip,
2394 low_voltage_threshold);
2395 high_voltage_raw = convert_vbatt_uv_to_raw(chip,
2396 high_voltage_threshold);
2397 rc = qpnp_write_wrapper(chip, (u8 *)&low_voltage_raw,
2398 chip->base + BMS1_OCV_USE_LOW_LIMIT_THR0, 2);
2399 if (rc) {
2400 pr_err("Failed to set ocv low voltage threshold: %d\n", rc);
2401 return rc;
2402 }
2403 rc = qpnp_write_wrapper(chip, (u8 *)&high_voltage_raw,
2404 chip->base + BMS1_OCV_USE_HIGH_LIMIT_THR0, 2);
2405 if (rc) {
2406 pr_err("Failed to set ocv high voltage threshold: %d\n", rc);
2407 return rc;
2408 }
2409 rc = qpnp_masked_write(chip, BMS1_OCV_USE_LIMIT_CTL,
2410 OCV_USE_LIMIT_EN, OCV_USE_LIMIT_EN);
2411 if (rc) {
2412 pr_err("Failed to enabled ocv voltage thresholds: %d\n", rc);
2413 return rc;
2414 }
2415 pr_debug("ocv low threshold set to %d uv or 0x%x raw\n",
2416 low_voltage_threshold, low_voltage_raw);
2417 pr_debug("ocv high threshold set to %d uv or 0x%x raw\n",
2418 high_voltage_threshold, high_voltage_raw);
2419 return 0;
2420}
2421
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002422static void read_shutdown_soc_and_iavg(struct qpnp_bms_chip *chip)
2423{
2424 int rc;
2425 u8 temp;
2426
2427 if (chip->ignore_shutdown_soc) {
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002428 chip->shutdown_soc_invalid = true;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002429 chip->shutdown_soc = 0;
2430 chip->shutdown_iavg_ma = 0;
2431 } else {
2432 rc = qpnp_read_wrapper(chip, &temp,
2433 chip->base + IAVG_STORAGE_REG, 1);
2434 if (rc) {
2435 pr_err("failed to read addr = %d %d assuming %d\n",
2436 chip->base + IAVG_STORAGE_REG, rc,
2437 IAVG_START);
2438 chip->shutdown_iavg_ma = IAVG_START;
Xiaozhe Shif5f966d2013-02-19 14:23:11 -08002439 } else if (temp == IAVG_INVALID) {
2440 pr_err("invalid iavg read from BMS1_DATA_REG_1, using %d\n",
2441 IAVG_START);
2442 chip->shutdown_iavg_ma = IAVG_START;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002443 } else {
2444 if (temp == 0) {
2445 chip->shutdown_iavg_ma = IAVG_START;
2446 } else {
2447 chip->shutdown_iavg_ma = IAVG_START
2448 + IAVG_STEP_SIZE_MA * (temp + 1);
2449 }
2450 }
2451
2452 rc = qpnp_read_wrapper(chip, &temp,
2453 chip->base + SOC_STORAGE_REG, 1);
2454 if (rc) {
2455 pr_err("failed to read addr = %d %d\n",
2456 chip->base + SOC_STORAGE_REG, rc);
2457 } else {
2458 chip->shutdown_soc = temp;
2459
Xiaozhe Shic7cbd052013-03-29 12:03:11 -07002460 if (chip->shutdown_soc == SOC_INVALID) {
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002461 pr_debug("No shutdown soc available\n");
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002462 chip->shutdown_soc_invalid = true;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002463 chip->shutdown_iavg_ma = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002464 }
2465 }
2466 }
2467
2468 pr_debug("shutdown_soc = %d shutdown_iavg = %d shutdown_soc_invalid = %d\n",
2469 chip->shutdown_soc,
2470 chip->shutdown_iavg_ma,
2471 chip->shutdown_soc_invalid);
2472}
2473
Xiaozhe Shi73a65692012-09-18 17:51:57 -07002474#define PALLADIUM_ID_MIN 0x7F40
2475#define PALLADIUM_ID_MAX 0x7F5A
2476#define DESAY_5200_ID_MIN 0x7F7F
2477#define DESAY_5200_ID_MAX 0x802F
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002478static int32_t read_battery_id(struct qpnp_bms_chip *chip)
Xiaozhe Shi73a65692012-09-18 17:51:57 -07002479{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002480 int rc;
2481 struct qpnp_vadc_result result;
2482
2483 rc = qpnp_vadc_read(LR_MUX2_BAT_ID, &result);
2484 if (rc) {
2485 pr_err("error reading batt id channel = %d, rc = %d\n",
2486 LR_MUX2_BAT_ID, rc);
2487 return rc;
2488 }
2489 pr_debug("batt_id phy = %lld meas = 0x%llx\n", result.physical,
2490 result.measurement);
2491 pr_debug("raw_code = 0x%x\n", result.adc_code);
2492 return result.adc_code;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07002493}
2494
2495static int set_battery_data(struct qpnp_bms_chip *chip)
2496{
2497 int64_t battery_id;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08002498 struct bms_battery_data *batt_data;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07002499
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08002500 if (chip->batt_type == BATT_DESAY) {
2501 batt_data = &desay_5200_data;
2502 } else if (chip->batt_type == BATT_PALLADIUM) {
2503 batt_data = &palladium_1500_data;
2504 } else if (chip->batt_type == BATT_OEM) {
2505 batt_data = &oem_batt_data;
Wu Fenglin2ac88aa2013-04-25 12:43:40 +08002506 } else if (chip->batt_type == BATT_QRD_4V35_2000MAH) {
2507 batt_data = &QRD_4v35_2000mAh_data;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07002508 } else {
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08002509 battery_id = read_battery_id(chip);
2510 if (battery_id < 0) {
2511 pr_err("cannot read battery id err = %lld\n",
2512 battery_id);
2513 return battery_id;
2514 }
2515
2516 if (is_between(PALLADIUM_ID_MIN, PALLADIUM_ID_MAX,
2517 battery_id)) {
2518 batt_data = &palladium_1500_data;
2519 } else if (is_between(DESAY_5200_ID_MIN, DESAY_5200_ID_MAX,
2520 battery_id)) {
2521 batt_data = &desay_5200_data;
2522 } else {
2523 pr_warn("invalid battid, palladium 1500 assumed batt_id %llx\n",
2524 battery_id);
2525 batt_data = &palladium_1500_data;
2526 }
Xiaozhe Shi73a65692012-09-18 17:51:57 -07002527 }
2528
Xiaozhe Shi976618f2013-04-30 10:49:30 -07002529 chip->fcc_mah = batt_data->fcc;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08002530 chip->fcc_temp_lut = batt_data->fcc_temp_lut;
2531 chip->fcc_sf_lut = batt_data->fcc_sf_lut;
2532 chip->pc_temp_ocv_lut = batt_data->pc_temp_ocv_lut;
2533 chip->pc_sf_lut = batt_data->pc_sf_lut;
2534 chip->rbatt_sf_lut = batt_data->rbatt_sf_lut;
2535 chip->default_rbatt_mohm = batt_data->default_rbatt_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07002536 chip->rbatt_capacitive_mohm = batt_data->rbatt_capacitive_mohm;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002537 chip->flat_ocv_threshold_uv = batt_data->flat_ocv_threshold_uv;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08002538
2539 if (chip->pc_temp_ocv_lut == NULL) {
2540 pr_err("temp ocv lut table is NULL\n");
2541 return -EINVAL;
2542 }
2543 return 0;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07002544}
2545
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002546#define SPMI_PROP_READ(chip_prop, qpnp_spmi_property, retval) \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002547do { \
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002548 retval = of_property_read_u32(chip->spmi->dev.of_node, \
Xiaozhe Shi9bd24622013-01-23 15:54:54 -08002549 "qcom," qpnp_spmi_property, \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002550 &chip->chip_prop); \
2551 if (retval) { \
2552 pr_err("Error reading " #qpnp_spmi_property \
2553 " property %d\n", rc); \
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002554 return -EINVAL; \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002555 } \
2556} while (0)
2557
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002558static inline int bms_read_properties(struct qpnp_bms_chip *chip)
2559{
2560 int rc;
2561
Xiaozhe Shid0a79542012-11-06 10:00:38 -08002562 SPMI_PROP_READ(r_sense_uohm, "r-sense-uohm", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002563 SPMI_PROP_READ(v_cutoff_uv, "v-cutoff-uv", rc);
2564 SPMI_PROP_READ(max_voltage_uv, "max-voltage-uv", rc);
2565 SPMI_PROP_READ(r_conn_mohm, "r-conn-mohm", rc);
2566 SPMI_PROP_READ(chg_term_ua, "chg-term-ua", rc);
2567 SPMI_PROP_READ(shutdown_soc_valid_limit,
2568 "shutdown-soc-valid-limit", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002569 SPMI_PROP_READ(adjust_soc_low_threshold,
2570 "adjust-soc-low-threshold", rc);
2571 SPMI_PROP_READ(batt_type, "batt-type", rc);
2572 SPMI_PROP_READ(low_soc_calc_threshold,
2573 "low-soc-calculate-soc-threshold", rc);
2574 SPMI_PROP_READ(low_soc_calculate_soc_ms,
2575 "low-soc-calculate-soc-ms", rc);
2576 SPMI_PROP_READ(calculate_soc_ms, "calculate-soc-ms", rc);
Xiaozhe Shidffbe692012-12-11 15:35:46 -08002577 chip->use_external_rsense = of_property_read_bool(
2578 chip->spmi->dev.of_node,
Xiaozhe Shi9bd24622013-01-23 15:54:54 -08002579 "qcom,use-external-rsense");
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002580 chip->ignore_shutdown_soc = of_property_read_bool(
2581 chip->spmi->dev.of_node,
Xiaozhe Shi9bd24622013-01-23 15:54:54 -08002582 "qcom,ignore-shutdown-soc");
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002583 chip->use_voltage_soc = of_property_read_bool(chip->spmi->dev.of_node,
Xiaozhe Shi9bd24622013-01-23 15:54:54 -08002584 "qcom,use-voltage-soc");
Xiaozhe Shibdf14742012-12-05 12:41:48 -08002585 chip->use_ocv_thresholds = of_property_read_bool(
2586 chip->spmi->dev.of_node,
Xiaozhe Shi9bd24622013-01-23 15:54:54 -08002587 "qcom,use-ocv-thresholds");
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002588 SPMI_PROP_READ(high_ocv_correction_limit_uv,
2589 "high-ocv-correction-limit-uv", rc);
2590 SPMI_PROP_READ(low_ocv_correction_limit_uv,
2591 "low-ocv-correction-limit-uv", rc);
2592 SPMI_PROP_READ(hold_soc_est,
2593 "hold-soc-est", rc);
2594 SPMI_PROP_READ(ocv_high_threshold_uv,
2595 "ocv-voltage-high-threshold-uv", rc);
Xiaozhe Shibdf14742012-12-05 12:41:48 -08002596 SPMI_PROP_READ(ocv_low_threshold_uv,
2597 "ocv-voltage-low-threshold-uv", rc);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002598 SPMI_PROP_READ(low_voltage_threshold, "low-voltage-threshold", rc);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07002599 SPMI_PROP_READ(temperature_margin, "tm-temp-margin", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002600
2601 if (chip->adjust_soc_low_threshold >= 45)
2602 chip->adjust_soc_low_threshold = 45;
2603
Xiaozhe Shid0a79542012-11-06 10:00:38 -08002604 pr_debug("dts data: r_sense_uohm:%d, v_cutoff_uv:%d, max_v:%d\n",
2605 chip->r_sense_uohm, chip->v_cutoff_uv,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002606 chip->max_voltage_uv);
2607 pr_debug("r_conn:%d, shutdown_soc: %d, adjust_soc_low:%d\n",
2608 chip->r_conn_mohm, chip->shutdown_soc_valid_limit,
2609 chip->adjust_soc_low_threshold);
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07002610 pr_debug("chg_term_ua:%d, batt_type:%d\n",
2611 chip->chg_term_ua,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002612 chip->batt_type);
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08002613 pr_debug("ignore_shutdown_soc:%d, use_voltage_soc:%d\n",
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002614 chip->ignore_shutdown_soc, chip->use_voltage_soc);
Xiaozhe Shidffbe692012-12-11 15:35:46 -08002615 pr_debug("use external rsense: %d\n", chip->use_external_rsense);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002616 return 0;
2617}
2618
2619static inline void bms_initialize_constants(struct qpnp_bms_chip *chip)
2620{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002621 chip->prev_pc_unusable = -EINVAL;
2622 chip->soc_at_cv = -EINVAL;
2623 chip->calculated_soc = -EINVAL;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002624 chip->last_soc = -EINVAL;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002625 chip->last_soc_est = -EINVAL;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002626 chip->battery_present = -EINVAL;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07002627 chip->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08002628 chip->last_cc_uah = INT_MIN;
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08002629 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
2630 chip->prev_last_good_ocv_raw = OCV_RAW_UNINITIALIZED;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002631 chip->first_time_calc_soc = 1;
2632 chip->first_time_calc_uuc = 1;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002633}
2634
Xiaozhe Shic40b3972012-11-30 14:11:16 -08002635#define REG_OFFSET_PERP_TYPE 0x04
2636#define REG_OFFSET_PERP_SUBTYPE 0x05
2637#define BMS_BMS_TYPE 0xD
Xiaozhe Shief6274c2013-03-06 15:23:52 -08002638#define BMS_BMS1_SUBTYPE 0x1
Xiaozhe Shic40b3972012-11-30 14:11:16 -08002639#define BMS_IADC_TYPE 0x8
Xiaozhe Shief6274c2013-03-06 15:23:52 -08002640#define BMS_IADC1_SUBTYPE 0x3
2641#define BMS_IADC2_SUBTYPE 0x5
Xiaozhe Shic40b3972012-11-30 14:11:16 -08002642
2643static int register_spmi(struct qpnp_bms_chip *chip, struct spmi_device *spmi)
2644{
2645 struct spmi_resource *spmi_resource;
2646 struct resource *resource;
2647 int rc;
2648 u8 type, subtype;
2649
2650 chip->dev = &(spmi->dev);
2651 chip->spmi = spmi;
2652
2653 spmi_for_each_container_dev(spmi_resource, spmi) {
2654 if (!spmi_resource) {
2655 pr_err("qpnp_bms: spmi resource absent\n");
2656 return -ENXIO;
2657 }
2658
2659 resource = spmi_get_resource(spmi, spmi_resource,
2660 IORESOURCE_MEM, 0);
2661 if (!(resource && resource->start)) {
2662 pr_err("node %s IO resource absent!\n",
2663 spmi->dev.of_node->full_name);
2664 return -ENXIO;
2665 }
2666
2667 rc = qpnp_read_wrapper(chip, &type,
2668 resource->start + REG_OFFSET_PERP_TYPE, 1);
2669 if (rc) {
2670 pr_err("Peripheral type read failed rc=%d\n", rc);
2671 return rc;
2672 }
2673 rc = qpnp_read_wrapper(chip, &subtype,
2674 resource->start + REG_OFFSET_PERP_SUBTYPE, 1);
2675 if (rc) {
2676 pr_err("Peripheral subtype read failed rc=%d\n", rc);
2677 return rc;
2678 }
2679
Xiaozhe Shief6274c2013-03-06 15:23:52 -08002680 if (type == BMS_BMS_TYPE && subtype == BMS_BMS1_SUBTYPE) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08002681 chip->base = resource->start;
2682 } else if (type == BMS_IADC_TYPE
Xiaozhe Shief6274c2013-03-06 15:23:52 -08002683 && (subtype == BMS_IADC1_SUBTYPE
2684 || subtype == BMS_IADC2_SUBTYPE)) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08002685 chip->iadc_base = resource->start;
2686 } else {
2687 pr_err("Invalid peripheral start=0x%x type=0x%x, subtype=0x%x\n",
2688 resource->start, type, subtype);
2689 }
2690 }
2691
2692 if (chip->base == 0) {
2693 dev_err(&spmi->dev, "BMS peripheral was not registered\n");
2694 return -EINVAL;
2695 }
2696 if (chip->iadc_base == 0) {
2697 dev_err(&spmi->dev, "BMS_IADC peripheral was not registered\n");
2698 return -EINVAL;
2699 }
2700
2701 return 0;
2702}
2703
2704#define ADC_CH_SEL_MASK 0x7
2705static int read_iadc_channel_select(struct qpnp_bms_chip *chip)
2706{
2707 u8 iadc_channel_select;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08002708 int32_t rds_rsense_nohm;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08002709 int rc;
2710
2711 rc = qpnp_read_wrapper(chip, &iadc_channel_select,
2712 chip->iadc_base + IADC1_BMS_ADC_CH_SEL_CTL, 1);
2713 if (rc) {
2714 pr_err("Error reading bms_iadc channel register %d\n", rc);
2715 return rc;
2716 }
2717
2718 iadc_channel_select &= ADC_CH_SEL_MASK;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08002719 if (iadc_channel_select != EXTERNAL_RSENSE
2720 && iadc_channel_select != INTERNAL_RSENSE) {
2721 pr_err("IADC1_BMS_IADC configured incorrectly. Selected channel = %d\n",
2722 iadc_channel_select);
2723 return -EINVAL;
2724 }
2725
2726 if (chip->use_external_rsense) {
2727 pr_debug("External rsense selected\n");
2728 if (iadc_channel_select == INTERNAL_RSENSE) {
2729 pr_debug("Internal rsense detected; Changing rsense to external\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08002730 rc = qpnp_masked_write_iadc(chip,
2731 IADC1_BMS_ADC_CH_SEL_CTL,
2732 ADC_CH_SEL_MASK,
2733 EXTERNAL_RSENSE);
2734 if (rc) {
2735 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
2736 IADC1_BMS_ADC_CH_SEL_CTL,
2737 EXTERNAL_RSENSE, rc);
2738 return rc;
2739 }
2740 reset_cc(chip);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07002741 chip->software_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08002742 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08002743 } else {
2744 pr_debug("Internal rsense selected\n");
2745 if (iadc_channel_select == EXTERNAL_RSENSE) {
2746 pr_debug("External rsense detected; Changing rsense to internal\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08002747 rc = qpnp_masked_write_iadc(chip,
2748 IADC1_BMS_ADC_CH_SEL_CTL,
2749 ADC_CH_SEL_MASK,
2750 INTERNAL_RSENSE);
2751 if (rc) {
2752 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
2753 IADC1_BMS_ADC_CH_SEL_CTL,
2754 INTERNAL_RSENSE, rc);
2755 return rc;
2756 }
2757 reset_cc(chip);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07002758 chip->software_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08002759 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08002760
2761 rc = qpnp_iadc_get_rsense(&rds_rsense_nohm);
2762 if (rc) {
2763 pr_err("Unable to read RDS resistance value from IADC; rc = %d\n",
2764 rc);
2765 return rc;
2766 }
Xiaozhe Shid0a79542012-11-06 10:00:38 -08002767 chip->r_sense_uohm = rds_rsense_nohm/1000;
2768 pr_debug("rds_rsense = %d nOhm, saved as %d uOhm\n",
2769 rds_rsense_nohm, chip->r_sense_uohm);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08002770 }
2771 return 0;
2772}
2773
Xiaozhe Shi535494d2013-04-05 12:27:51 -07002774static int refresh_die_temp_monitor(struct qpnp_bms_chip *chip)
2775{
2776 struct qpnp_vadc_result result;
2777 int rc;
2778
2779 rc = qpnp_vadc_read(DIE_TEMP, &result);
2780
2781 pr_debug("low = %lld, high = %lld\n",
2782 result.physical - chip->temperature_margin,
2783 result.physical + chip->temperature_margin);
2784 chip->die_temp_monitor_params.high_temp = result.physical
2785 + chip->temperature_margin;
2786 chip->die_temp_monitor_params.low_temp = result.physical
2787 - chip->temperature_margin;
2788 chip->die_temp_monitor_params.state_request =
2789 ADC_TM_HIGH_LOW_THR_ENABLE;
2790 return qpnp_adc_tm_channel_measure(&chip->die_temp_monitor_params);
2791}
2792
2793static void btm_notify_die_temp(enum qpnp_tm_state state, void *ctx)
2794{
2795 struct qpnp_bms_chip *chip = ctx;
2796 struct qpnp_vadc_result result;
2797 int rc;
2798
2799 rc = qpnp_vadc_read(DIE_TEMP, &result);
2800
2801 if (state == ADC_TM_LOW_STATE)
2802 pr_debug("low state triggered\n");
2803 else if (state == ADC_TM_HIGH_STATE)
2804 pr_debug("high state triggered\n");
2805 pr_debug("die temp = %lld, raw = 0x%x\n",
2806 result.physical, result.adc_code);
2807 schedule_work(&chip->recalc_work);
2808 refresh_die_temp_monitor(chip);
2809}
2810
2811static int setup_die_temp_monitoring(struct qpnp_bms_chip *chip)
2812{
2813 int rc = qpnp_adc_tm_is_ready();
2814 if (rc) {
2815 pr_info("adc tm is not ready yet: %d, defer probe\n", rc);
2816 return -EPROBE_DEFER;
2817 }
2818 chip->die_temp_monitor_params.channel = DIE_TEMP;
2819 chip->die_temp_monitor_params.btm_ctx = (void *)chip;
2820 chip->die_temp_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
2821 chip->die_temp_monitor_params.threshold_notification =
2822 &btm_notify_die_temp;
2823 refresh_die_temp_monitor(chip);
2824 if (rc) {
2825 pr_err("tm setup failed: %d\n", rc);
2826 return rc;
2827 }
2828 pr_debug("setup complete\n");
2829 return 0;
2830}
2831
Xiaozhe Shic40b3972012-11-30 14:11:16 -08002832static int __devinit qpnp_bms_probe(struct spmi_device *spmi)
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002833{
2834 struct qpnp_bms_chip *chip;
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07002835 bool warm_reset;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002836 int rc, vbatt;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002837
2838 chip = kzalloc(sizeof *chip, GFP_KERNEL);
2839
2840 if (chip == NULL) {
2841 pr_err("kzalloc() failed.\n");
2842 return -ENOMEM;
2843 }
2844
Xiaozhe Shic40b3972012-11-30 14:11:16 -08002845 rc = qpnp_vadc_is_ready();
2846 if (rc) {
2847 pr_info("vadc not ready: %d, deferring probe\n", rc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002848 rc = -EPROBE_DEFER;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08002849 goto error_read;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002850 }
Xiaozhe Shic40b3972012-11-30 14:11:16 -08002851
2852 rc = qpnp_iadc_is_ready();
2853 if (rc) {
2854 pr_info("iadc not ready: %d, deferring probe\n", rc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002855 rc = -EPROBE_DEFER;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08002856 goto error_read;
2857 }
2858
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07002859 warm_reset = qpnp_pon_is_warm_reset();
2860 rc = warm_reset;
2861 if (rc < 0)
2862 goto error_read;
2863
Xiaozhe Shic40b3972012-11-30 14:11:16 -08002864 rc = register_spmi(chip, spmi);
2865 if (rc) {
2866 pr_err("error registering spmi resource %d\n", rc);
2867 goto error_resource;
2868 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002869
2870 rc = qpnp_read_wrapper(chip, &chip->revision1,
2871 chip->base + BMS1_REVISION1, 1);
2872 if (rc) {
2873 pr_err("error reading version register %d\n", rc);
2874 goto error_read;
2875 }
2876
2877 rc = qpnp_read_wrapper(chip, &chip->revision2,
2878 chip->base + BMS1_REVISION2, 1);
2879 if (rc) {
2880 pr_err("Error reading version register %d\n", rc);
2881 goto error_read;
2882 }
Xiaozhe Shia045a562012-11-28 16:55:39 -08002883 pr_debug("BMS version: %hhu.%hhu\n", chip->revision2, chip->revision1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002884
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002885 rc = bms_read_properties(chip);
2886 if (rc) {
2887 pr_err("Unable to read all bms properties, rc = %d\n", rc);
2888 goto error_read;
2889 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002890
Xiaozhe Shidffbe692012-12-11 15:35:46 -08002891 rc = read_iadc_channel_select(chip);
2892 if (rc) {
2893 pr_err("Unable to get iadc selected channel = %d\n", rc);
2894 goto error_read;
2895 }
2896
Xiaozhe Shibdf14742012-12-05 12:41:48 -08002897 if (chip->use_ocv_thresholds) {
2898 rc = set_ocv_voltage_thresholds(chip,
2899 chip->ocv_low_threshold_uv,
2900 chip->ocv_high_threshold_uv);
2901 if (rc) {
2902 pr_err("Could not set ocv voltage thresholds: %d\n",
2903 rc);
2904 goto error_read;
2905 }
2906 }
2907
Xiaozhe Shic40b3972012-11-30 14:11:16 -08002908 rc = set_battery_data(chip);
2909 if (rc) {
2910 pr_err("Bad battery data %d\n", rc);
2911 goto error_read;
2912 }
2913
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002914 bms_initialize_constants(chip);
2915
Xiaozhe Shic40b3972012-11-30 14:11:16 -08002916 mutex_init(&chip->bms_output_lock);
2917 mutex_init(&chip->last_ocv_uv_mutex);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002918 mutex_init(&chip->vbat_monitor_mutex);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08002919 mutex_init(&chip->soc_invalidation_mutex);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002920 mutex_init(&chip->last_soc_mutex);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08002921
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002922 wake_lock_init(&chip->soc_wake_lock, WAKE_LOCK_SUSPEND,
2923 "qpnp_soc_lock");
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002924 wake_lock_init(&chip->low_voltage_wake_lock, WAKE_LOCK_SUSPEND,
2925 "qpnp_low_voltage_lock");
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002926 wake_lock_init(&chip->cv_wake_lock, WAKE_LOCK_SUSPEND,
2927 "qpnp_cv_lock");
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002928 INIT_DELAYED_WORK(&chip->calculate_soc_delayed_work,
2929 calculate_soc_work);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002930 INIT_WORK(&chip->recalc_work, recalculate_work);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002931
2932 read_shutdown_soc_and_iavg(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002933
2934 dev_set_drvdata(&spmi->dev, chip);
2935 device_init_wakeup(&spmi->dev, 1);
2936
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002937 rc = setup_vbat_monitoring(chip);
2938 if (rc < 0) {
2939 pr_err("failed to set up voltage notifications: %d\n", rc);
2940 goto error_setup;
Xiaozhe Shid5d21412013-02-06 17:14:41 -08002941 }
2942
Xiaozhe Shi535494d2013-04-05 12:27:51 -07002943 rc = setup_die_temp_monitoring(chip);
2944 if (rc < 0) {
2945 pr_err("failed to set up die temp notifications: %d\n", rc);
2946 goto error_setup;
2947 }
2948
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002949 calculate_soc_work(&(chip->calculate_soc_delayed_work.work));
2950
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002951 /* setup & register the battery power supply */
2952 chip->bms_psy.name = "bms";
2953 chip->bms_psy.type = POWER_SUPPLY_TYPE_BMS;
2954 chip->bms_psy.properties = msm_bms_power_props;
2955 chip->bms_psy.num_properties = ARRAY_SIZE(msm_bms_power_props);
2956 chip->bms_psy.get_property = qpnp_bms_power_get_property;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002957 chip->bms_psy.external_power_changed =
2958 qpnp_bms_external_power_changed;
2959 chip->bms_psy.supplied_to = qpnp_bms_supplicants;
2960 chip->bms_psy.num_supplicants = ARRAY_SIZE(qpnp_bms_supplicants);
2961
2962 rc = power_supply_register(chip->dev, &chip->bms_psy);
2963
2964 if (rc < 0) {
2965 pr_err("power_supply_register bms failed rc = %d\n", rc);
2966 goto unregister_dc;
2967 }
2968
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002969 vbatt = 0;
Xiaozhe Shi36458962013-02-06 16:19:57 -08002970 rc = get_battery_voltage(&vbatt);
2971 if (rc) {
2972 pr_err("error reading vbat_sns adc channel = %d, rc = %d\n",
2973 VBAT_SNS, rc);
2974 goto unregister_dc;
2975 }
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002976
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07002977 pr_info("probe success: soc =%d vbatt = %d ocv = %d r_sense_uohm = %u warm_reset = %d\n",
2978 get_prop_bms_capacity(chip), vbatt, chip->last_ocv_uv,
2979 chip->r_sense_uohm, warm_reset);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002980 return 0;
2981
2982unregister_dc:
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002983 power_supply_unregister(&chip->bms_psy);
2984error_setup:
2985 dev_set_drvdata(&spmi->dev, NULL);
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002986 wake_lock_destroy(&chip->soc_wake_lock);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002987 wake_lock_destroy(&chip->low_voltage_wake_lock);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002988 wake_lock_destroy(&chip->cv_wake_lock);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08002989error_resource:
Xiaozhe Shib19f7032012-08-16 12:14:16 -07002990error_read:
2991 kfree(chip);
2992 return rc;
2993}
2994
2995static int __devexit
2996qpnp_bms_remove(struct spmi_device *spmi)
2997{
2998 struct qpnp_bms_chip *chip = dev_get_drvdata(&spmi->dev);
2999
3000 dev_set_drvdata(&spmi->dev, NULL);
3001 kfree(chip);
3002 return 0;
3003}
3004
Xiaozhe Shid6168d82013-04-18 16:06:17 -07003005static int bms_suspend(struct device *dev)
3006{
3007 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
3008
3009 cancel_delayed_work_sync(&chip->calculate_soc_delayed_work);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07003010 chip->was_charging_at_sleep = is_battery_charging(chip);
Xiaozhe Shid6168d82013-04-18 16:06:17 -07003011 return 0;
3012}
3013
Xiaozhe Shicdeee312012-12-18 15:10:18 -08003014static int bms_resume(struct device *dev)
3015{
3016 int rc;
Xiaozhe Shid6168d82013-04-18 16:06:17 -07003017 int soc_calc_period;
3018 int time_until_next_recalc;
Xiaozhe Shicdeee312012-12-18 15:10:18 -08003019 unsigned long time_since_last_recalc;
3020 unsigned long tm_now_sec;
3021 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
3022
3023 rc = get_current_time(&tm_now_sec);
3024 if (rc) {
3025 pr_err("Could not read current time: %d\n", rc);
3026 } else if (tm_now_sec > chip->last_recalc_time) {
3027 time_since_last_recalc = tm_now_sec - chip->last_recalc_time;
3028 pr_debug("Time since last recalc: %lu\n",
3029 time_since_last_recalc);
3030 if (chip->calculated_soc < chip->low_soc_calc_threshold)
3031 soc_calc_period = chip->low_soc_calculate_soc_ms;
3032 else
3033 soc_calc_period = chip->calculate_soc_ms;
3034
Xiaozhe Shid6168d82013-04-18 16:06:17 -07003035 time_until_next_recalc = max(0, soc_calc_period
3036 - (int)(time_since_last_recalc * 1000));
3037
Xiaozhe Shi04da0992013-04-26 16:32:14 -07003038 if (!wake_lock_active(&chip->soc_wake_lock)
3039 && time_until_next_recalc == 0)
3040 wake_lock(&chip->soc_wake_lock);
3041
Xiaozhe Shid6168d82013-04-18 16:06:17 -07003042 schedule_delayed_work(&chip->calculate_soc_delayed_work,
3043 round_jiffies_relative(msecs_to_jiffies
3044 (time_until_next_recalc)));
Xiaozhe Shicdeee312012-12-18 15:10:18 -08003045 }
3046 return 0;
3047}
3048
3049static const struct dev_pm_ops qpnp_bms_pm_ops = {
3050 .resume = bms_resume,
Xiaozhe Shid6168d82013-04-18 16:06:17 -07003051 .suspend = bms_suspend,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08003052};
3053
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003054static struct spmi_driver qpnp_bms_driver = {
3055 .probe = qpnp_bms_probe,
3056 .remove = __devexit_p(qpnp_bms_remove),
3057 .driver = {
3058 .name = QPNP_BMS_DEV_NAME,
3059 .owner = THIS_MODULE,
3060 .of_match_table = qpnp_bms_match_table,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08003061 .pm = &qpnp_bms_pm_ops,
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003062 },
3063};
3064
3065static int __init qpnp_bms_init(void)
3066{
3067 pr_info("QPNP BMS INIT\n");
3068 return spmi_driver_register(&qpnp_bms_driver);
3069}
3070
3071static void __exit qpnp_bms_exit(void)
3072{
3073 pr_info("QPNP BMS EXIT\n");
3074 return spmi_driver_unregister(&qpnp_bms_driver);
3075}
3076
3077module_init(qpnp_bms_init);
3078module_exit(qpnp_bms_exit);
3079
3080MODULE_DESCRIPTION("QPNP BMS Driver");
3081MODULE_LICENSE("GPL v2");
3082MODULE_ALIAS("platform:" QPNP_BMS_DEV_NAME);