blob: 3f2ab1056b6d3783b9260cef31cc644d0be1ae81 [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 Shi27375822013-08-22 11:40:15 -070026#include <linux/sched.h>
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070027#include <linux/qpnp/qpnp-adc.h>
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -070028#include <linux/qpnp/power-on.h>
Xiaozhe Shiaf203c22013-06-19 12:01:38 -070029#include <linux/of_batterydata.h>
Xiaozhe Shib19f7032012-08-16 12:14:16 -070030
Xiaozhe Shib19f7032012-08-16 12:14:16 -070031/* BMS Register Offsets */
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -070032#define REVISION1 0x0
33#define REVISION2 0x1
Xiaozhe Shib19f7032012-08-16 12:14:16 -070034#define BMS1_STATUS1 0x8
35#define BMS1_MODE_CTL 0X40
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070036/* Coulomb counter clear registers */
Xiaozhe Shib19f7032012-08-16 12:14:16 -070037#define BMS1_CC_DATA_CTL 0x42
Xiaozhe Shia045a562012-11-28 16:55:39 -080038#define BMS1_CC_CLEAR_CTL 0x43
Xiaozhe Shi20640b52013-01-03 11:49:30 -080039/* BMS Tolerances */
40#define BMS1_TOL_CTL 0X44
Xiaozhe Shib19f7032012-08-16 12:14:16 -070041/* OCV limit registers */
42#define BMS1_OCV_USE_LOW_LIMIT_THR0 0x48
43#define BMS1_OCV_USE_LOW_LIMIT_THR1 0x49
44#define BMS1_OCV_USE_HIGH_LIMIT_THR0 0x4A
45#define BMS1_OCV_USE_HIGH_LIMIT_THR1 0x4B
46#define BMS1_OCV_USE_LIMIT_CTL 0x4C
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -070047/* Delay control */
48#define BMS1_S1_DELAY_CTL 0x5A
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -080049/* OCV interrupt threshold */
50#define BMS1_OCV_THR0 0x50
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -070051#define BMS1_S2_SAMP_AVG_CTL 0x61
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -080052/* SW CC interrupt threshold */
53#define BMS1_SW_CC_THR0 0xA0
Xiaozhe Shib19f7032012-08-16 12:14:16 -070054/* OCV for r registers */
55#define BMS1_OCV_FOR_R_DATA0 0x80
Xiaozhe Shib19f7032012-08-16 12:14:16 -070056#define BMS1_VSENSE_FOR_R_DATA0 0x82
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070057/* Coulomb counter data */
Xiaozhe Shib19f7032012-08-16 12:14:16 -070058#define BMS1_CC_DATA0 0x8A
Xiaozhe Shif3da8622013-06-10 14:50:56 -070059/* Shadow Coulomb counter data */
60#define BMS1_SW_CC_DATA0 0xA8
Xiaozhe Shib19f7032012-08-16 12:14:16 -070061/* OCV for soc data */
62#define BMS1_OCV_FOR_SOC_DATA0 0x90
Xiaozhe Shib19f7032012-08-16 12:14:16 -070063#define BMS1_VSENSE_PON_DATA0 0x94
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070064#define BMS1_VSENSE_AVG_DATA0 0x98
Xiaozhe Shib19f7032012-08-16 12:14:16 -070065#define BMS1_VBAT_AVG_DATA0 0x9E
Xiaozhe Shib19f7032012-08-16 12:14:16 -070066/* Extra bms registers */
Xiaozhe Shi57058942013-03-27 16:54:54 -070067#define SOC_STORAGE_REG 0xB0
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070068#define IAVG_STORAGE_REG 0xB1
Anirudh Ghayale0c02932013-07-08 16:26:35 +053069#define BMS_FCC_COUNT 0xB2
70#define BMS_FCC_BASE_REG 0xB3 /* FCC updates - 0xB3 to 0xB7 */
71#define BMS_CHGCYL_BASE_REG 0xB8 /* FCC chgcyl - 0xB8 to 0xBC */
72#define CHARGE_INCREASE_STORAGE 0xBD
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053073#define CHARGE_CYCLE_STORAGE_LSB 0xBE /* LSB=0xBE, MSB=0xBF */
74
Xiaozhe Shic40b3972012-11-30 14:11:16 -080075/* IADC Channel Select */
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -070076#define IADC1_BMS_REVISION2 0x01
Xiaozhe Shic40b3972012-11-30 14:11:16 -080077#define IADC1_BMS_ADC_CH_SEL_CTL 0x48
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -070078#define IADC1_BMS_ADC_INT_RSNSN_CTL 0x49
79#define IADC1_BMS_FAST_AVG_EN 0x5B
Xiaozhe Shib19f7032012-08-16 12:14:16 -070080
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070081/* Configuration for saving of shutdown soc/iavg */
82#define IGNORE_SOC_TEMP_DECIDEG 50
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -070083#define IAVG_STEP_SIZE_MA 10
Xiaozhe Shif5f966d2013-02-19 14:23:11 -080084#define IAVG_INVALID 0xFF
Xiaozhe Shif9f99242013-08-29 12:27:50 -070085#define SOC_INVALID 0x7E
Xiaozhe Shicd7e5302012-10-17 12:29:53 -070086
Xiaozhe Shie118c692012-09-24 15:17:43 -070087#define IAVG_SAMPLES 16
88
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053089/* FCC learning constants */
Anirudh Ghayale0c02932013-07-08 16:26:35 +053090#define MAX_FCC_CYCLES 5
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053091#define DELTA_FCC_PERCENT 5
92#define VALID_FCC_CHGCYL_RANGE 50
Anirudh Ghayale0c02932013-07-08 16:26:35 +053093#define CHGCYL_RESOLUTION 20
94#define FCC_DEFAULT_TEMP 250
Anirudh Ghayald71d9f82013-06-05 11:11:46 +053095
Xiaozhe Shib19f7032012-08-16 12:14:16 -070096#define QPNP_BMS_DEV_NAME "qcom,qpnp-bms"
97
Xiaozhe Shif3da8622013-06-10 14:50:56 -070098enum {
99 SHDW_CC,
100 CC
101};
102
103enum {
104 NORESET,
105 RESET
106};
107
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700108struct soc_params {
109 int fcc_uah;
110 int cc_uah;
Xiaozhe Shi904f1f72012-12-04 12:47:21 -0800111 int rbatt_mohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700112 int iavg_ua;
113 int uuc_uah;
114 int ocv_charge_uah;
Xiaozhe Shif36d2862013-01-04 10:17:35 -0800115 int delta_time_s;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700116};
117
118struct raw_soc_params {
119 uint16_t last_good_ocv_raw;
120 int64_t cc;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700121 int64_t shdw_cc;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700122 int last_good_ocv_uv;
123};
124
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530125struct fcc_sample {
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530126 int fcc_new;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530127 int chargecycles;
128};
129
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800130struct bms_irq {
131 unsigned int irq;
132 unsigned long disabled;
133};
134
135struct bms_wakeup_source {
136 struct wakeup_source source;
137 unsigned long disabled;
138};
139
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700140struct qpnp_bms_chip {
141 struct device *dev;
142 struct power_supply bms_psy;
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -0700143 bool bms_psy_registered;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -0700144 struct power_supply *batt_psy;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700145 struct spmi_device *spmi;
Xiaozhe Shi27375822013-08-22 11:40:15 -0700146 wait_queue_head_t bms_wait_queue;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700147 u16 base;
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800148 u16 iadc_base;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -0700149 u16 batt_pres_addr;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -0700150 u16 soc_storage_addr;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700151
152 u8 revision1;
153 u8 revision2;
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -0700154
155 u8 iadc_bms_revision1;
156 u8 iadc_bms_revision2;
157
Xiaozhe Shid5d21412013-02-06 17:14:41 -0800158 int battery_present;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700159 int battery_status;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700160 bool batfet_closed;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800161 bool new_battery;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700162 bool done_charging;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800163 bool last_soc_invalid;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700164 /* platform data */
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -0800165 int r_sense_uohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700166 unsigned int v_cutoff_uv;
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -0800167 int max_voltage_uv;
168 int r_conn_mohm;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700169 int shutdown_soc_valid_limit;
170 int adjust_soc_low_threshold;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700171 int chg_term_ua;
Xiaozhe Shi73a65692012-09-18 17:51:57 -0700172 enum battery_type batt_type;
Xiaozhe Shi976618f2013-04-30 10:49:30 -0700173 unsigned int fcc_mah;
Xiaozhe Shi73a65692012-09-18 17:51:57 -0700174 struct single_row_lut *fcc_temp_lut;
175 struct single_row_lut *fcc_sf_lut;
176 struct pc_temp_ocv_lut *pc_temp_ocv_lut;
177 struct sf_lut *pc_sf_lut;
178 struct sf_lut *rbatt_sf_lut;
179 int default_rbatt_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -0700180 int rbatt_capacitive_mohm;
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -0700181 int rbatt_mohm;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700182
183 struct delayed_work calculate_soc_delayed_work;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800184 struct work_struct recalc_work;
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700185 struct work_struct batfet_open_work;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700186
187 struct mutex bms_output_lock;
188 struct mutex last_ocv_uv_mutex;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700189 struct mutex vbat_monitor_mutex;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700190 struct mutex soc_invalidation_mutex;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700191 struct mutex last_soc_mutex;
Xiaozhe Shibda84992013-09-05 10:39:11 -0700192 struct mutex status_lock;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700193
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800194 bool use_external_rsense;
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800195 bool use_ocv_thresholds;
Xiaozhe Shic40b3972012-11-30 14:11:16 -0800196
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700197 bool ignore_shutdown_soc;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800198 bool shutdown_soc_invalid;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700199 int shutdown_soc;
200 int shutdown_iavg_ma;
201
Xiaozhe Shi4be85782013-02-22 17:33:40 -0800202 struct wake_lock low_voltage_wake_lock;
Xiaozhe Shi4be85782013-02-22 17:33:40 -0800203 int low_voltage_threshold;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700204 int low_soc_calc_threshold;
205 int low_soc_calculate_soc_ms;
206 int calculate_soc_ms;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800207 struct bms_wakeup_source soc_wake_source;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700208 struct wake_lock cv_wake_lock;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700209
Xiaozhe Shie118c692012-09-24 15:17:43 -0700210 uint16_t ocv_reading_at_100;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700211 uint16_t prev_last_good_ocv_raw;
212 int last_ocv_uv;
Xiaozhe Shicc48e992013-05-28 16:42:24 -0700213 int charging_adjusted_ocv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800214 int last_ocv_temp;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700215 int last_cc_uah;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700216 unsigned long last_soc_change_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700217 unsigned long tm_sec;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700218 unsigned long report_tm_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700219 bool first_time_calc_soc;
220 bool first_time_calc_uuc;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700221 int64_t software_cc_uah;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700222 int64_t software_shdw_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700223
224 int iavg_samples_ma[IAVG_SAMPLES];
225 int iavg_index;
226 int iavg_num_samples;
227 struct timespec t_soc_queried;
228 int last_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -0700229 int last_soc_est;
Xiaozhe Shicc137262013-03-10 06:21:41 -0700230 int last_soc_unbound;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700231 bool was_charging_at_sleep;
232 int charge_start_tm_sec;
233 int catch_up_time_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700234 struct single_row_lut *adjusted_fcc_temp_lut;
235
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700236 struct qpnp_adc_tm_btm_param vbat_monitor_params;
Xiaozhe Shi535494d2013-04-05 12:27:51 -0700237 struct qpnp_adc_tm_btm_param die_temp_monitor_params;
238 int temperature_margin;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700239 unsigned int vadc_v0625;
240 unsigned int vadc_v1250;
241
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700242 int prev_uuc_iavg_ma;
243 int prev_pc_unusable;
244 int ibat_at_cv_ua;
245 int soc_at_cv;
246 int prev_chg_soc;
247 int calculated_soc;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -0800248 int prev_voltage_based_soc;
249 bool use_voltage_soc;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700250 bool in_cv_range;
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800251
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800252 int prev_batt_terminal_uv;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -0700253 int high_ocv_correction_limit_uv;
254 int low_ocv_correction_limit_uv;
255 int flat_ocv_threshold_uv;
256 int hold_soc_est;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800257
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800258 int ocv_high_threshold_uv;
259 int ocv_low_threshold_uv;
Xiaozhe Shicdeee312012-12-18 15:10:18 -0800260 unsigned long last_recalc_time;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530261
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530262 struct fcc_sample *fcc_learning_samples;
263 u8 fcc_sample_count;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530264 int enable_fcc_learning;
265 int min_fcc_learning_soc;
266 int min_fcc_ocv_pc;
267 int min_fcc_learning_samples;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530268 int start_soc;
269 int end_soc;
270 int start_pc;
271 int start_cc_uah;
272 int start_real_soc;
273 int end_cc_uah;
274 uint16_t fcc_new_mah;
275 int fcc_new_batt_temp;
276 uint16_t charge_cycles;
277 u8 charge_increase;
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530278 int fcc_resolution;
279 bool battery_removed;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800280 struct bms_irq sw_cc_thr_irq;
281 struct bms_irq ocv_thr_irq;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700282 struct qpnp_vadc_chip *vadc_dev;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700283 struct qpnp_iadc_chip *iadc_dev;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -0700284 struct qpnp_adc_tm_chip *adc_tm_dev;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700285};
286
287static struct of_device_id qpnp_bms_match_table[] = {
288 { .compatible = QPNP_BMS_DEV_NAME },
289 {}
290};
291
292static char *qpnp_bms_supplicants[] = {
293 "battery"
294};
295
296static enum power_supply_property msm_bms_power_props[] = {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700297 POWER_SUPPLY_PROP_CAPACITY,
Xiaozhe Shibda84992013-09-05 10:39:11 -0700298 POWER_SUPPLY_PROP_STATUS,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700299 POWER_SUPPLY_PROP_CURRENT_NOW,
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -0700300 POWER_SUPPLY_PROP_RESISTANCE,
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -0700301 POWER_SUPPLY_PROP_CHARGE_COUNTER,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700302 POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700303 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +0530304 POWER_SUPPLY_PROP_CHARGE_FULL,
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +0530305 POWER_SUPPLY_PROP_CYCLE_COUNT,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700306};
307
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530308static int discard_backup_fcc_data(struct qpnp_bms_chip *chip);
309static void backup_charge_cycle(struct qpnp_bms_chip *chip);
310
Xiaozhe Shi20640b52013-01-03 11:49:30 -0800311static bool bms_reset;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -0800312
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700313static int qpnp_read_wrapper(struct qpnp_bms_chip *chip, u8 *val,
314 u16 base, int count)
315{
316 int rc;
317 struct spmi_device *spmi = chip->spmi;
318
319 rc = spmi_ext_register_readl(spmi->ctrl, spmi->sid, base, val, count);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700320 if (rc) {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700321 pr_err("SPMI read failed rc=%d\n", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700322 return rc;
323 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700324 return 0;
325}
326
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700327static int qpnp_write_wrapper(struct qpnp_bms_chip *chip, u8 *val,
328 u16 base, int count)
329{
330 int rc;
331 struct spmi_device *spmi = chip->spmi;
332
333 rc = spmi_ext_register_writel(spmi->ctrl, spmi->sid, base, val, count);
334 if (rc) {
335 pr_err("SPMI write failed rc=%d\n", rc);
336 return rc;
337 }
338 return 0;
339}
340
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800341static int qpnp_masked_write_base(struct qpnp_bms_chip *chip, u16 addr,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700342 u8 mask, u8 val)
343{
344 int rc;
345 u8 reg;
346
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800347 rc = qpnp_read_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700348 if (rc) {
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800349 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700350 return rc;
351 }
352 reg &= ~mask;
353 reg |= val & mask;
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800354 rc = qpnp_write_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700355 if (rc) {
356 pr_err("write failed addr = %03X, val = %02x, mask = %02x, reg = %02x, rc = %d\n",
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800357 addr, val, mask, reg, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700358 return rc;
359 }
360 return 0;
361}
362
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800363static int qpnp_masked_write_iadc(struct qpnp_bms_chip *chip, u16 addr,
364 u8 mask, u8 val)
365{
366 return qpnp_masked_write_base(chip, chip->iadc_base + addr, mask, val);
367}
368
369static int qpnp_masked_write(struct qpnp_bms_chip *chip, u16 addr,
370 u8 mask, u8 val)
371{
372 return qpnp_masked_write_base(chip, chip->base + addr, mask, val);
373}
374
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800375static void bms_stay_awake(struct bms_wakeup_source *source)
376{
377 if (__test_and_clear_bit(0, &source->disabled)) {
378 __pm_stay_awake(&source->source);
379 pr_debug("enabled source %s\n", source->source.name);
380 }
381}
382
383static void bms_relax(struct bms_wakeup_source *source)
384{
385 if (!__test_and_set_bit(0, &source->disabled)) {
386 __pm_relax(&source->source);
387 pr_debug("disabled source %s\n", source->source.name);
388 }
389}
390
391static void enable_bms_irq(struct bms_irq *irq)
392{
393 if (__test_and_clear_bit(0, &irq->disabled)) {
394 enable_irq(irq->irq);
395 pr_debug("enabled irq %d\n", irq->irq);
396 }
397}
398
399static void disable_bms_irq(struct bms_irq *irq)
400{
401 if (!__test_and_set_bit(0, &irq->disabled)) {
402 disable_irq(irq->irq);
403 pr_debug("disabled irq %d\n", irq->irq);
404 }
405}
406
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700407#define HOLD_OREG_DATA BIT(0)
408static int lock_output_data(struct qpnp_bms_chip *chip)
409{
410 int rc;
411
412 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL,
413 HOLD_OREG_DATA, HOLD_OREG_DATA);
414 if (rc) {
415 pr_err("couldnt lock bms output rc = %d\n", rc);
416 return rc;
417 }
418 return 0;
419}
420
421static int unlock_output_data(struct qpnp_bms_chip *chip)
422{
423 int rc;
424
425 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL, HOLD_OREG_DATA, 0);
426 if (rc) {
427 pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc);
428 return rc;
429 }
430 return 0;
431}
432
433#define V_PER_BIT_MUL_FACTOR 97656
434#define V_PER_BIT_DIV_FACTOR 1000
435#define VADC_INTRINSIC_OFFSET 0x6000
436
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800437static int vadc_reading_to_uv(int reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700438{
439 if (reading <= VADC_INTRINSIC_OFFSET)
440 return 0;
441
442 return (reading - VADC_INTRINSIC_OFFSET)
443 * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR;
444}
445
446#define VADC_CALIB_UV 625000
447#define VBATT_MUL_FACTOR 3
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800448static int adjust_vbatt_reading(struct qpnp_bms_chip *chip, int reading_uv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700449{
450 s64 numerator, denominator;
451
452 if (reading_uv == 0)
453 return 0;
454
455 /* don't adjust if not calibrated */
456 if (chip->vadc_v0625 == 0 || chip->vadc_v1250 == 0) {
457 pr_debug("No cal yet return %d\n",
458 VBATT_MUL_FACTOR * reading_uv);
459 return VBATT_MUL_FACTOR * reading_uv;
460 }
461
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700462 numerator = ((s64)reading_uv - chip->vadc_v0625) * VADC_CALIB_UV;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700463 denominator = (s64)chip->vadc_v1250 - chip->vadc_v0625;
464 if (denominator == 0)
465 return reading_uv * VBATT_MUL_FACTOR;
466 return (VADC_CALIB_UV + div_s64(numerator, denominator))
467 * VBATT_MUL_FACTOR;
468}
469
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800470static int convert_vbatt_uv_to_raw(struct qpnp_bms_chip *chip,
471 int unadjusted_vbatt)
472{
473 int scaled_vbatt = unadjusted_vbatt / VBATT_MUL_FACTOR;
474
475 if (scaled_vbatt <= 0)
476 return VADC_INTRINSIC_OFFSET;
477 return ((scaled_vbatt * V_PER_BIT_DIV_FACTOR) / V_PER_BIT_MUL_FACTOR)
478 + VADC_INTRINSIC_OFFSET;
479}
480
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700481static inline int convert_vbatt_raw_to_uv(struct qpnp_bms_chip *chip,
482 uint16_t reading)
483{
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700484 int64_t uv;
485 int rc;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700486
487 uv = vadc_reading_to_uv(reading);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700488 pr_debug("%u raw converted into %lld uv\n", reading, uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700489 uv = adjust_vbatt_reading(chip, uv);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700490 pr_debug("adjusted into %lld uv\n", uv);
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700491 rc = qpnp_vbat_sns_comp_result(chip->vadc_dev, &uv);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700492 if (rc)
493 pr_debug("could not compensate vbatt\n");
494 pr_debug("compensated into %lld uv\n", uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700495 return uv;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700496}
497
498#define CC_READING_RESOLUTION_N 542535
499#define CC_READING_RESOLUTION_D 100000
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -0700500static s64 cc_reading_to_uv(s64 reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700501{
502 return div_s64(reading * CC_READING_RESOLUTION_N,
503 CC_READING_RESOLUTION_D);
504}
505
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800506#define QPNP_ADC_GAIN_IDEAL 3291LL
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700507static s64 cc_adjust_for_gain(s64 uv, uint16_t gain)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700508{
509 s64 result_uv;
510
511 pr_debug("adjusting_uv = %lld\n", uv);
Xiaozhe Shi820a47a2012-11-27 13:23:27 -0800512 if (gain == 0) {
513 pr_debug("gain is %d, not adjusting\n", gain);
514 return uv;
515 }
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700516 pr_debug("adjusting by factor: %lld/%hu = %lld%%\n",
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800517 QPNP_ADC_GAIN_IDEAL, gain,
518 div_s64(QPNP_ADC_GAIN_IDEAL * 100LL, (s64)gain));
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700519
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800520 result_uv = div_s64(uv * QPNP_ADC_GAIN_IDEAL, (s64)gain);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700521 pr_debug("result_uv = %lld\n", result_uv);
522 return result_uv;
523}
524
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700525static s64 cc_reverse_adjust_for_gain(struct qpnp_bms_chip *chip, s64 uv)
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800526{
527 struct qpnp_iadc_calib calibration;
528 int gain;
529 s64 result_uv;
530
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700531 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800532 gain = (int)calibration.gain_raw - (int)calibration.offset_raw;
533
534 pr_debug("reverse adjusting_uv = %lld\n", uv);
535 if (gain == 0) {
536 pr_debug("gain is %d, not adjusting\n", gain);
537 return uv;
538 }
539 pr_debug("adjusting by factor: %hu/%lld = %lld%%\n",
540 gain, QPNP_ADC_GAIN_IDEAL,
541 div64_s64((s64)gain * 100LL,
542 (s64)QPNP_ADC_GAIN_IDEAL));
543
544 result_uv = div64_s64(uv * (s64)gain, QPNP_ADC_GAIN_IDEAL);
545 pr_debug("result_uv = %lld\n", result_uv);
546 return result_uv;
547}
548
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700549static int convert_vsense_to_uv(struct qpnp_bms_chip *chip,
550 int16_t reading)
551{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700552 struct qpnp_iadc_calib calibration;
553
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700554 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700555 return cc_adjust_for_gain(cc_reading_to_uv(reading),
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800556 calibration.gain_raw - calibration.offset_raw);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700557}
558
559static int read_vsense_avg(struct qpnp_bms_chip *chip, int *result_uv)
560{
561 int rc;
562 int16_t reading;
563
564 rc = qpnp_read_wrapper(chip, (u8 *)&reading,
565 chip->base + BMS1_VSENSE_AVG_DATA0, 2);
566
567 if (rc) {
568 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
569 return rc;
570 }
571
572 *result_uv = convert_vsense_to_uv(chip, reading);
573 return 0;
574}
575
576static int get_battery_current(struct qpnp_bms_chip *chip, int *result_ua)
577{
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700578 int rc, vsense_uv = 0;
579 int64_t temp_current;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700580
Xiaozhe Shid0a79542012-11-06 10:00:38 -0800581 if (chip->r_sense_uohm == 0) {
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700582 pr_err("r_sense is zero\n");
583 return -EINVAL;
584 }
585
586 mutex_lock(&chip->bms_output_lock);
587 lock_output_data(chip);
588 read_vsense_avg(chip, &vsense_uv);
589 unlock_output_data(chip);
590 mutex_unlock(&chip->bms_output_lock);
591
592 pr_debug("vsense_uv=%duV\n", vsense_uv);
593 /* cast for signed division */
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700594 temp_current = div_s64((vsense_uv * 1000000LL),
595 (int)chip->r_sense_uohm);
596
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700597 rc = qpnp_iadc_comp_result(chip->iadc_dev, &temp_current);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700598 if (rc)
599 pr_debug("error compensation failed: %d\n", rc);
600
601 *result_ua = temp_current;
602 pr_debug("err compensated ibat=%duA\n", *result_ua);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700603 return 0;
604}
605
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700606static int get_battery_voltage(struct qpnp_bms_chip *chip, int *result_uv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700607{
608 int rc;
609 struct qpnp_vadc_result adc_result;
610
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700611 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &adc_result);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700612 if (rc) {
613 pr_err("error reading adc channel = %d, rc = %d\n",
614 VBAT_SNS, rc);
615 return rc;
616 }
617 pr_debug("mvolts phy = %lld meas = 0x%llx\n", adc_result.physical,
618 adc_result.measurement);
619 *result_uv = (int)adc_result.physical;
620 return 0;
621}
622
Xiaozhe Shie118c692012-09-24 15:17:43 -0700623#define CC_36_BIT_MASK 0xFFFFFFFFFLL
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800624static uint64_t convert_s64_to_s36(int64_t raw64)
625{
626 return (uint64_t) raw64 & CC_36_BIT_MASK;
627}
628
629#define SIGN_EXTEND_36_TO_64_MASK (-1LL ^ CC_36_BIT_MASK)
630static int64_t convert_s36_to_s64(uint64_t raw36)
631{
632 raw36 = raw36 & CC_36_BIT_MASK;
633 /* convert 36 bit signed value into 64 signed value */
634 return (raw36 >> 35) == 0LL ?
635 raw36 : (SIGN_EXTEND_36_TO_64_MASK | raw36);
636}
637
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700638static int read_cc_raw(struct qpnp_bms_chip *chip, int64_t *reading,
639 int cc_type)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700640{
641 int64_t raw_reading;
642 int rc;
643
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700644 if (cc_type == SHDW_CC)
645 rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
646 chip->base + BMS1_SW_CC_DATA0, 5);
647 else
648 rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
649 chip->base + BMS1_CC_DATA0, 5);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700650 if (rc) {
651 pr_err("Error reading cc: rc = %d\n", rc);
652 return -ENXIO;
653 }
654
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800655 *reading = convert_s36_to_s64(raw_reading);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700656
657 return 0;
658}
659
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700660static int calib_vadc(struct qpnp_bms_chip *chip)
661{
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700662 int rc, raw_0625, raw_1250;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700663 struct qpnp_vadc_result result;
664
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700665 rc = qpnp_vadc_read(chip->vadc_dev, REF_625MV, &result);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700666 if (rc) {
667 pr_debug("vadc read failed with rc = %d\n", rc);
668 return rc;
669 }
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700670 raw_0625 = result.adc_code;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700671
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700672 rc = qpnp_vadc_read(chip->vadc_dev, REF_125V, &result);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700673 if (rc) {
674 pr_debug("vadc read failed with rc = %d\n", rc);
675 return rc;
676 }
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700677 raw_1250 = result.adc_code;
678 chip->vadc_v0625 = vadc_reading_to_uv(raw_0625);
679 chip->vadc_v1250 = vadc_reading_to_uv(raw_1250);
680 pr_debug("vadc calib: 0625 = %d raw (%d uv), 1250 = %d raw (%d uv)\n",
681 raw_0625, chip->vadc_v0625,
682 raw_1250, chip->vadc_v1250);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700683 return 0;
684}
685
Xiaozhe Shie118c692012-09-24 15:17:43 -0700686static void convert_and_store_ocv(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800687 struct raw_soc_params *raw,
688 int batt_temp)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700689{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700690 int rc;
691
692 pr_debug("prev_last_good_ocv_raw = %d, last_good_ocv_raw = %d\n",
693 chip->prev_last_good_ocv_raw,
694 raw->last_good_ocv_raw);
695 rc = calib_vadc(chip);
696 if (rc)
697 pr_err("Vadc reference voltage read failed, rc = %d\n", rc);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700698 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
699 raw->last_good_ocv_uv = convert_vbatt_raw_to_uv(chip,
700 raw->last_good_ocv_raw);
701 chip->last_ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800702 chip->last_ocv_temp = batt_temp;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700703 chip->software_cc_uah = 0;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700704 pr_debug("last_good_ocv_uv = %d\n", raw->last_good_ocv_uv);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700705}
706
Xiaozhe Shia045a562012-11-28 16:55:39 -0800707#define CLEAR_CC BIT(7)
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700708#define CLEAR_SHDW_CC BIT(6)
Xiaozhe Shia045a562012-11-28 16:55:39 -0800709/**
710 * reset both cc and sw-cc.
711 * note: this should only be ever called from one thread
712 * or there may be a race condition where CC is never enabled
713 * again
714 */
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700715static void reset_cc(struct qpnp_bms_chip *chip, u8 flags)
Xiaozhe Shia045a562012-11-28 16:55:39 -0800716{
717 int rc;
718
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700719 pr_debug("resetting cc manually with flags %hhu\n", flags);
720 mutex_lock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800721 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700722 flags,
723 flags);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800724 if (rc)
725 pr_err("cc reset failed: %d\n", rc);
726
727 /* wait for 100us for cc to reset */
728 udelay(100);
729
730 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700731 flags, 0);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800732 if (rc)
733 pr_err("cc reenable failed: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700734 mutex_unlock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800735}
736
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700737static int get_battery_status(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800738{
739 union power_supply_propval ret = {0,};
740
741 if (chip->batt_psy == NULL)
742 chip->batt_psy = power_supply_get_by_name("battery");
743 if (chip->batt_psy) {
744 /* if battery has been registered, use the status property */
745 chip->batt_psy->get_property(chip->batt_psy,
746 POWER_SUPPLY_PROP_STATUS, &ret);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700747 return ret.intval;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800748 }
749
750 /* Default to false if the battery power supply is not registered. */
751 pr_debug("battery power supply is not registered\n");
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700752 return POWER_SUPPLY_STATUS_UNKNOWN;
753}
754
755static bool is_battery_charging(struct qpnp_bms_chip *chip)
756{
757 return get_battery_status(chip) == POWER_SUPPLY_STATUS_CHARGING;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800758}
759
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700760static bool is_battery_full(struct qpnp_bms_chip *chip)
761{
762 return get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL;
763}
764
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700765static bool is_battery_present(struct qpnp_bms_chip *chip)
766{
767 union power_supply_propval ret = {0,};
768
769 if (chip->batt_psy == NULL)
770 chip->batt_psy = power_supply_get_by_name("battery");
771 if (chip->batt_psy) {
772 /* if battery has been registered, use the status property */
773 chip->batt_psy->get_property(chip->batt_psy,
774 POWER_SUPPLY_PROP_PRESENT, &ret);
775 return ret.intval;
776 }
777
778 /* Default to false if the battery power supply is not registered. */
779 pr_debug("battery power supply is not registered\n");
780 return false;
781}
782
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700783static bool is_batfet_closed(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800784{
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700785 union power_supply_propval ret = {0,};
786
787 if (chip->batt_psy == NULL)
788 chip->batt_psy = power_supply_get_by_name("battery");
789 if (chip->batt_psy) {
790 /* if battery has been registered, use the online property */
791 chip->batt_psy->get_property(chip->batt_psy,
792 POWER_SUPPLY_PROP_ONLINE, &ret);
793 return !!ret.intval;
794 }
795
796 /* Default to true if the battery power supply is not registered. */
797 pr_debug("battery power supply is not registered\n");
798 return true;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800799}
800
801static int get_simultaneous_batt_v_and_i(struct qpnp_bms_chip *chip,
802 int *ibat_ua, int *vbat_uv)
803{
804 struct qpnp_iadc_result i_result;
805 struct qpnp_vadc_result v_result;
806 enum qpnp_iadc_channels iadc_channel;
807 int rc;
808
809 iadc_channel = chip->use_external_rsense ?
810 EXTERNAL_RSENSE : INTERNAL_RSENSE;
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700811 if (is_battery_full(chip)) {
812 rc = get_battery_current(chip, ibat_ua);
813 if (rc) {
814 pr_err("bms current read failed with rc: %d\n", rc);
815 return rc;
816 }
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700817 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &v_result);
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700818 if (rc) {
819 pr_err("vadc read failed with rc: %d\n", rc);
820 return rc;
821 }
822 *vbat_uv = (int)v_result.physical;
823 } else {
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700824 rc = qpnp_iadc_vadc_sync_read(chip->iadc_dev,
825 iadc_channel, &i_result,
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700826 VBAT_SNS, &v_result);
827 if (rc) {
828 pr_err("adc sync read failed with rc: %d\n", rc);
829 return rc;
830 }
831 /*
832 * reverse the current read by the iadc, since the bms uses
833 * flipped battery current polarity.
834 */
835 *ibat_ua = -1 * (int)i_result.result_ua;
836 *vbat_uv = (int)v_result.physical;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800837 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800838
839 return 0;
840}
841
842static int estimate_ocv(struct qpnp_bms_chip *chip)
843{
844 int ibat_ua, vbat_uv, ocv_est_uv;
845 int rc;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -0700846 int rbatt_mohm = chip->default_rbatt_mohm + chip->r_conn_mohm
847 + chip->rbatt_capacitive_mohm;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800848
849 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
850 if (rc) {
851 pr_err("simultaneous failed rc = %d\n", rc);
852 return rc;
853 }
854
855 ocv_est_uv = vbat_uv + (ibat_ua * rbatt_mohm) / 1000;
856 pr_debug("estimated pon ocv = %d\n", ocv_est_uv);
857 return ocv_est_uv;
858}
859
860static void reset_for_new_battery(struct qpnp_bms_chip *chip, int batt_temp)
861{
862 chip->last_ocv_uv = estimate_ocv(chip);
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700863 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800864 chip->last_soc = -EINVAL;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700865 chip->last_soc_invalid = true;
866 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800867 chip->soc_at_cv = -EINVAL;
868 chip->shutdown_soc_invalid = true;
869 chip->shutdown_soc = 0;
870 chip->shutdown_iavg_ma = 0;
871 chip->prev_pc_unusable = -EINVAL;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700872 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700873 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700874 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800875 chip->last_cc_uah = INT_MIN;
876 chip->last_ocv_temp = batt_temp;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800877 chip->prev_batt_terminal_uv = 0;
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530878 if (chip->enable_fcc_learning) {
879 chip->adjusted_fcc_temp_lut = NULL;
880 chip->fcc_new_mah = -EINVAL;
881 /* reset the charge-cycle and charge-increase registers */
882 chip->charge_increase = 0;
883 chip->charge_cycles = 0;
884 backup_charge_cycle(chip);
885 /* discard all the FCC learnt data and reset the local table */
886 discard_backup_fcc_data(chip);
887 memset(chip->fcc_learning_samples, 0,
888 chip->min_fcc_learning_samples *
889 sizeof(struct fcc_sample));
890 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800891}
892
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -0800893#define OCV_RAW_UNINITIALIZED 0xFFFF
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -0700894#define MIN_OCV_UV 2000000
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700895static int read_soc_params_raw(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800896 struct raw_soc_params *raw,
897 int batt_temp)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700898{
Xiaozhe Shi90f3a412013-08-21 10:31:35 -0700899 int warm_reset;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700900 int rc;
901
902 mutex_lock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800903
Xiaozhe Shie118c692012-09-24 15:17:43 -0700904 lock_output_data(chip);
905
906 rc = qpnp_read_wrapper(chip, (u8 *)&raw->last_good_ocv_raw,
907 chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
908 if (rc) {
909 pr_err("Error reading ocv: rc = %d\n", rc);
910 return -ENXIO;
911 }
912
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700913 rc = read_cc_raw(chip, &raw->cc, CC);
914 rc = read_cc_raw(chip, &raw->shdw_cc, SHDW_CC);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700915 if (rc) {
916 pr_err("Failed to read raw cc data, rc = %d\n", rc);
917 return rc;
918 }
919
920 unlock_output_data(chip);
921 mutex_unlock(&chip->bms_output_lock);
922
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -0800923 if (chip->prev_last_good_ocv_raw == OCV_RAW_UNINITIALIZED) {
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800924 convert_and_store_ocv(chip, raw, batt_temp);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -0700925 pr_debug("PON_OCV_UV = %d, cc = %llx\n",
926 chip->last_ocv_uv, raw->cc);
927 warm_reset = qpnp_pon_is_warm_reset();
928 if (raw->last_good_ocv_uv < MIN_OCV_UV
929 || warm_reset > 0) {
930 pr_debug("OCV is stale or bad, estimating new OCV.\n");
931 chip->last_ocv_uv = estimate_ocv(chip);
932 raw->last_good_ocv_uv = chip->last_ocv_uv;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700933 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -0700934 pr_debug("New PON_OCV_UV = %d, cc = %llx\n",
935 chip->last_ocv_uv, raw->cc);
936 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800937 } else if (chip->new_battery) {
938 /* if a new battery was inserted, estimate the ocv */
939 reset_for_new_battery(chip, batt_temp);
940 raw->cc = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700941 raw->shdw_cc = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800942 raw->last_good_ocv_uv = chip->last_ocv_uv;
943 chip->new_battery = false;
Xiaozhe Shi74548b92013-05-02 16:47:08 -0700944 } else if (chip->done_charging) {
945 chip->done_charging = false;
946 /* if we just finished charging, reset CC and fake 100% */
947 chip->ocv_reading_at_100 = raw->last_good_ocv_raw;
948 chip->last_ocv_uv = chip->max_voltage_uv;
949 raw->last_good_ocv_uv = chip->max_voltage_uv;
950 raw->cc = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700951 raw->shdw_cc = 0;
952 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi74548b92013-05-02 16:47:08 -0700953 chip->last_ocv_temp = batt_temp;
954 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700955 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi74548b92013-05-02 16:47:08 -0700956 chip->last_cc_uah = INT_MIN;
957 pr_debug("EOC Battery full ocv_reading = 0x%x\n",
958 chip->ocv_reading_at_100);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700959 } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) {
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800960 convert_and_store_ocv(chip, raw, batt_temp);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700961 /* forget the old cc value upon ocv */
Xiaozhe Shif36d2862013-01-04 10:17:35 -0800962 chip->last_cc_uah = INT_MIN;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700963 } else {
964 raw->last_good_ocv_uv = chip->last_ocv_uv;
965 }
966
Xiaozhe Shi74548b92013-05-02 16:47:08 -0700967 /* stop faking a high OCV if we get a new OCV */
968 if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw)
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -0800969 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
Xiaozhe Shi74548b92013-05-02 16:47:08 -0700970
Xiaozhe Shie118c692012-09-24 15:17:43 -0700971 pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n",
972 raw->last_good_ocv_raw, raw->last_good_ocv_uv);
973 pr_debug("cc_raw= 0x%llx\n", raw->cc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700974 return 0;
975}
976
Xiaozhe Shie118c692012-09-24 15:17:43 -0700977static int calculate_pc(struct qpnp_bms_chip *chip, int ocv_uv,
978 int batt_temp)
979{
980 int pc;
981
982 pc = interpolate_pc(chip->pc_temp_ocv_lut,
983 batt_temp / 10, ocv_uv / 1000);
984 pr_debug("pc = %u %% for ocv = %d uv batt_temp = %d\n",
985 pc, ocv_uv, batt_temp);
986 /* Multiply the initial FCC value by the scale factor. */
987 return pc;
988}
989
990static int calculate_fcc(struct qpnp_bms_chip *chip, int batt_temp)
991{
992 int fcc_uah;
993
994 if (chip->adjusted_fcc_temp_lut == NULL) {
995 /* interpolate_fcc returns a mv value. */
996 fcc_uah = interpolate_fcc(chip->fcc_temp_lut,
997 batt_temp) * 1000;
998 pr_debug("fcc = %d uAh\n", fcc_uah);
999 return fcc_uah;
1000 } else {
1001 return 1000 * interpolate_fcc(chip->adjusted_fcc_temp_lut,
1002 batt_temp);
1003 }
1004}
1005
1006/* calculate remaining charge at the time of ocv */
1007static int calculate_ocv_charge(struct qpnp_bms_chip *chip,
1008 struct raw_soc_params *raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001009 int fcc_uah)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001010{
1011 int ocv_uv, pc;
1012
1013 ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001014 pc = calculate_pc(chip, ocv_uv, chip->last_ocv_temp);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001015 pr_debug("ocv_uv = %d pc = %d\n", ocv_uv, pc);
1016 return (fcc_uah * pc) / 100;
1017}
1018
Xiaozhe Shie118c692012-09-24 15:17:43 -07001019#define CC_READING_TICKS 56
1020#define SLEEP_CLK_HZ 32764
1021#define SECONDS_PER_HOUR 3600
1022
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001023static s64 cc_uv_to_pvh(s64 cc_uv)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001024{
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001025 /* Note that it is necessary need to multiply by 1000000 to convert
1026 * from uvh to pvh here.
1027 * However, the maximum Coulomb Counter value is 2^35, which can cause
1028 * an over flow.
1029 * Multiply by 100000 first to perserve as much precision as possible
1030 * then multiply by 10 after doing the division in order to avoid
1031 * overflow on the maximum Coulomb Counter value.
1032 */
1033 return div_s64(cc_uv * CC_READING_TICKS * 100000,
1034 SLEEP_CLK_HZ * SECONDS_PER_HOUR) * 10;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001035}
1036
1037/**
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001038 * calculate_cc() - converts a hardware coulomb counter reading into uah
Xiaozhe Shie118c692012-09-24 15:17:43 -07001039 * @chip: the bms chip pointer
1040 * @cc: the cc reading from bms h/w
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001041 * @cc_type: calcualte cc from regular or shadow coulomb counter
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001042 * @clear_cc: whether this function should clear the hardware counter
1043 * after reading
Xiaozhe Shie118c692012-09-24 15:17:43 -07001044 *
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001045 * Converts the 64 bit hardware coulomb counter into microamp-hour by taking
1046 * into account hardware resolution and adc errors.
1047 *
1048 * Return: the coulomb counter based charge in uAh (micro-amp hour)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001049 */
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001050static int calculate_cc(struct qpnp_bms_chip *chip, int64_t cc,
1051 int cc_type, int clear_cc)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001052{
Xiaozhe Shi4e376652012-10-25 12:38:50 -07001053 struct qpnp_iadc_calib calibration;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001054 struct qpnp_vadc_result result;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001055 int64_t cc_voltage_uv, cc_pvh, cc_uah, *software_counter;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001056 int rc;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001057
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001058 software_counter = cc_type == SHDW_CC ?
1059 &chip->software_shdw_cc_uah : &chip->software_cc_uah;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001060 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001061 if (rc) {
1062 pr_err("could not read pmic die temperature: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001063 return *software_counter;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001064 }
1065
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07001066 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001067 pr_debug("%scc = %lld, die_temp = %lld\n",
1068 cc_type == SHDW_CC ? "shdw_" : "",
1069 cc, result.physical);
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -07001070 cc_voltage_uv = cc_reading_to_uv(cc);
Xiaozhe Shi0c484932013-02-05 16:14:10 -08001071 cc_voltage_uv = cc_adjust_for_gain(cc_voltage_uv,
1072 calibration.gain_raw
1073 - calibration.offset_raw);
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001074 cc_pvh = cc_uv_to_pvh(cc_voltage_uv);
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001075 cc_uah = div_s64(cc_pvh, chip->r_sense_uohm);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07001076 rc = qpnp_iadc_comp_result(chip->iadc_dev, &cc_uah);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001077 if (rc)
1078 pr_debug("error compensation failed: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001079 if (clear_cc == RESET) {
1080 pr_debug("software_%scc = %lld, added cc_uah = %lld\n",
1081 cc_type == SHDW_CC ? "sw_" : "",
1082 *software_counter, cc_uah);
1083 *software_counter += cc_uah;
1084 reset_cc(chip, cc_type == SHDW_CC ? CLEAR_SHDW_CC : CLEAR_CC);
1085 return (int)*software_counter;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001086 } else {
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001087 pr_debug("software_%scc = %lld, cc_uah = %lld, total = %lld\n",
1088 cc_type == SHDW_CC ? "shdw_" : "",
1089 *software_counter, cc_uah,
1090 *software_counter + cc_uah);
1091 return *software_counter + cc_uah;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001092 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07001093}
1094
1095static int get_rbatt(struct qpnp_bms_chip *chip,
1096 int soc_rbatt_mohm, int batt_temp)
1097{
1098 int rbatt_mohm, scalefactor;
1099
1100 rbatt_mohm = chip->default_rbatt_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001101 if (chip->rbatt_sf_lut == NULL) {
1102 pr_debug("RBATT = %d\n", rbatt_mohm);
1103 return rbatt_mohm;
1104 }
1105 /* Convert the batt_temp to DegC from deciDegC */
1106 batt_temp = batt_temp / 10;
1107 scalefactor = interpolate_scalingfactor(chip->rbatt_sf_lut,
1108 batt_temp, soc_rbatt_mohm);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001109 rbatt_mohm = (rbatt_mohm * scalefactor) / 100;
1110
1111 rbatt_mohm += chip->r_conn_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07001112 rbatt_mohm += chip->rbatt_capacitive_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001113 return rbatt_mohm;
1114}
1115
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -07001116#define IAVG_MINIMAL_TIME 2
Xiaozhe Shie118c692012-09-24 15:17:43 -07001117static void calculate_iavg(struct qpnp_bms_chip *chip, int cc_uah,
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001118 int *iavg_ua, int delta_time_s)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001119{
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001120 int delta_cc_uah = 0;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001121
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -07001122 /*
1123 * use the battery current if called too quickly
1124 */
1125 if (delta_time_s < IAVG_MINIMAL_TIME
1126 || chip->last_cc_uah == INT_MIN) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001127 get_battery_current(chip, iavg_ua);
1128 goto out;
1129 }
1130
Xiaozhe Shie118c692012-09-24 15:17:43 -07001131 delta_cc_uah = cc_uah - chip->last_cc_uah;
1132
1133 *iavg_ua = div_s64((s64)delta_cc_uah * 3600, delta_time_s);
1134
Xiaozhe Shie118c692012-09-24 15:17:43 -07001135out:
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001136 pr_debug("delta_cc = %d iavg_ua = %d\n", delta_cc_uah, (int)*iavg_ua);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001137
1138 /* remember cc_uah */
1139 chip->last_cc_uah = cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001140}
1141
1142static int calculate_termination_uuc(struct qpnp_bms_chip *chip,
1143 struct soc_params *params,
1144 int batt_temp, int uuc_iavg_ma,
1145 int *ret_pc_unusable)
1146{
1147 int unusable_uv, pc_unusable, uuc_uah;
1148 int i = 0;
1149 int ocv_mv;
1150 int batt_temp_degc = batt_temp / 10;
1151 int rbatt_mohm;
1152 int delta_uv;
1153 int prev_delta_uv = 0;
1154 int prev_rbatt_mohm = 0;
1155 int uuc_rbatt_mohm;
1156
1157 for (i = 0; i <= 100; i++) {
1158 ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
1159 batt_temp_degc, i);
1160 rbatt_mohm = get_rbatt(chip, i, batt_temp);
1161 unusable_uv = (rbatt_mohm * uuc_iavg_ma)
1162 + (chip->v_cutoff_uv);
1163 delta_uv = ocv_mv * 1000 - unusable_uv;
1164
Xiaozhe Shie118c692012-09-24 15:17:43 -07001165 if (delta_uv > 0)
1166 break;
1167
1168 prev_delta_uv = delta_uv;
1169 prev_rbatt_mohm = rbatt_mohm;
1170 }
1171
1172 uuc_rbatt_mohm = linear_interpolate(rbatt_mohm, delta_uv,
1173 prev_rbatt_mohm, prev_delta_uv,
1174 0);
1175
1176 unusable_uv = (uuc_rbatt_mohm * uuc_iavg_ma) + (chip->v_cutoff_uv);
1177
1178 pc_unusable = calculate_pc(chip, unusable_uv, batt_temp);
1179 uuc_uah = (params->fcc_uah * pc_unusable) / 100;
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001180 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 -07001181 uuc_iavg_ma,
1182 uuc_rbatt_mohm, unusable_uv,
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001183 pc_unusable, i, uuc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001184 *ret_pc_unusable = pc_unusable;
1185 return uuc_uah;
1186}
1187
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001188#define TIME_PER_PERCENT_UUC 60
Xiaozhe Shie118c692012-09-24 15:17:43 -07001189static int adjust_uuc(struct qpnp_bms_chip *chip,
1190 struct soc_params *params,
1191 int new_pc_unusable,
1192 int new_uuc_uah,
1193 int batt_temp)
1194{
1195 int new_unusable_mv, new_iavg_ma;
1196 int batt_temp_degc = batt_temp / 10;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001197 int max_percent_change;
1198
1199 max_percent_change = max(params->delta_time_s
1200 / TIME_PER_PERCENT_UUC, 1);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001201
1202 if (chip->prev_pc_unusable == -EINVAL
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001203 || abs(chip->prev_pc_unusable - new_pc_unusable)
1204 <= max_percent_change) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001205 chip->prev_pc_unusable = new_pc_unusable;
1206 return new_uuc_uah;
1207 }
1208
1209 /* the uuc is trying to change more than 1% restrict it */
1210 if (new_pc_unusable > chip->prev_pc_unusable)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001211 chip->prev_pc_unusable += max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001212 else
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001213 chip->prev_pc_unusable -= max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001214
1215 new_uuc_uah = (params->fcc_uah * chip->prev_pc_unusable) / 100;
1216
1217 /* also find update the iavg_ma accordingly */
1218 new_unusable_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
1219 batt_temp_degc, chip->prev_pc_unusable);
1220 if (new_unusable_mv < chip->v_cutoff_uv/1000)
1221 new_unusable_mv = chip->v_cutoff_uv/1000;
1222
1223 new_iavg_ma = (new_unusable_mv * 1000 - chip->v_cutoff_uv)
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001224 / params->rbatt_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001225 if (new_iavg_ma == 0)
1226 new_iavg_ma = 1;
1227 chip->prev_uuc_iavg_ma = new_iavg_ma;
1228 pr_debug("Restricting UUC to %d (%d%%) unusable_mv = %d iavg_ma = %d\n",
1229 new_uuc_uah, chip->prev_pc_unusable,
1230 new_unusable_mv, new_iavg_ma);
1231
1232 return new_uuc_uah;
1233}
1234
Abhijeet Dharmapurikarbdf8ba82012-12-20 18:33:56 -08001235#define MIN_IAVG_MA 250
Xiaozhe Shie118c692012-09-24 15:17:43 -07001236static int calculate_unusable_charge_uah(struct qpnp_bms_chip *chip,
1237 struct soc_params *params,
1238 int batt_temp)
1239{
1240 int uuc_uah_iavg;
1241 int i;
1242 int uuc_iavg_ma = params->iavg_ua / 1000;
1243 int pc_unusable;
1244
1245 /*
1246 * if called first time, fill all the samples with
1247 * the shutdown_iavg_ma
1248 */
1249 if (chip->first_time_calc_uuc && chip->shutdown_iavg_ma != 0) {
1250 pr_debug("Using shutdown_iavg_ma = %d in all samples\n",
1251 chip->shutdown_iavg_ma);
1252 for (i = 0; i < IAVG_SAMPLES; i++)
1253 chip->iavg_samples_ma[i] = chip->shutdown_iavg_ma;
1254
1255 chip->iavg_index = 0;
1256 chip->iavg_num_samples = IAVG_SAMPLES;
1257 }
1258
Xiaozhe Shi70633922013-09-23 15:50:53 -07001259 if (params->delta_time_s >= IAVG_MINIMAL_TIME) {
1260 /*
1261 * if charging use a nominal avg current to keep
1262 * a reasonable UUC while charging
1263 */
1264 if (uuc_iavg_ma < MIN_IAVG_MA)
1265 uuc_iavg_ma = MIN_IAVG_MA;
1266 chip->iavg_samples_ma[chip->iavg_index] = uuc_iavg_ma;
1267 chip->iavg_index = (chip->iavg_index + 1) % IAVG_SAMPLES;
1268 chip->iavg_num_samples++;
1269 if (chip->iavg_num_samples >= IAVG_SAMPLES)
1270 chip->iavg_num_samples = IAVG_SAMPLES;
1271 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07001272
1273 /* now that this sample is added calcualte the average */
1274 uuc_iavg_ma = 0;
1275 if (chip->iavg_num_samples != 0) {
1276 for (i = 0; i < chip->iavg_num_samples; i++) {
1277 pr_debug("iavg_samples_ma[%d] = %d\n", i,
1278 chip->iavg_samples_ma[i]);
1279 uuc_iavg_ma += chip->iavg_samples_ma[i];
1280 }
1281
1282 uuc_iavg_ma = DIV_ROUND_CLOSEST(uuc_iavg_ma,
1283 chip->iavg_num_samples);
1284 }
1285
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001286 /*
1287 * if we're in bms reset mode, force uuc to be 3% of fcc
1288 */
1289 if (bms_reset)
1290 return (params->fcc_uah * 3) / 100;
1291
Xiaozhe Shi75e5efe2013-02-07 09:51:43 -08001292 uuc_uah_iavg = calculate_termination_uuc(chip, params, batt_temp,
1293 uuc_iavg_ma, &pc_unusable);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001294 pr_debug("uuc_iavg_ma = %d uuc with iavg = %d\n",
1295 uuc_iavg_ma, uuc_uah_iavg);
1296
1297 chip->prev_uuc_iavg_ma = uuc_iavg_ma;
1298 /* restrict the uuc such that it can increase only by one percent */
1299 uuc_uah_iavg = adjust_uuc(chip, params, pc_unusable,
1300 uuc_uah_iavg, batt_temp);
1301
Xiaozhe Shie118c692012-09-24 15:17:43 -07001302 return uuc_uah_iavg;
1303}
1304
1305static void find_ocv_for_soc(struct qpnp_bms_chip *chip,
1306 struct soc_params *params,
1307 int batt_temp,
1308 int shutdown_soc,
1309 int *ret_ocv_uv)
1310{
1311 s64 ocv_charge_uah;
1312 int pc, new_pc;
1313 int batt_temp_degc = batt_temp / 10;
1314 int ocv_uv;
1315
1316 ocv_charge_uah = (s64)shutdown_soc
1317 * (params->fcc_uah - params->uuc_uah);
1318 ocv_charge_uah = div_s64(ocv_charge_uah, 100)
1319 + params->cc_uah + params->uuc_uah;
1320 pc = DIV_ROUND_CLOSEST((int)ocv_charge_uah * 100, params->fcc_uah);
1321 pc = clamp(pc, 0, 100);
1322
1323 ocv_uv = interpolate_ocv(chip->pc_temp_ocv_lut, batt_temp_degc, pc);
1324
1325 pr_debug("s_soc = %d, fcc = %d uuc = %d rc = %d, pc = %d, ocv mv = %d\n",
1326 shutdown_soc, params->fcc_uah,
1327 params->uuc_uah, (int)ocv_charge_uah,
1328 pc, ocv_uv);
1329 new_pc = interpolate_pc(chip->pc_temp_ocv_lut, batt_temp_degc, ocv_uv);
1330 pr_debug("test revlookup pc = %d for ocv = %d\n", new_pc, ocv_uv);
1331
1332 while (abs(new_pc - pc) > 1) {
1333 int delta_mv = 5;
1334
1335 if (new_pc > pc)
1336 delta_mv = -1 * delta_mv;
1337
1338 ocv_uv = ocv_uv + delta_mv;
1339 new_pc = interpolate_pc(chip->pc_temp_ocv_lut,
1340 batt_temp_degc, ocv_uv);
1341 pr_debug("test revlookup pc = %d for ocv = %d\n",
1342 new_pc, ocv_uv);
1343 }
1344
1345 *ret_ocv_uv = ocv_uv * 1000;
1346 params->ocv_charge_uah = (int)ocv_charge_uah;
1347}
1348
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001349static int get_current_time(unsigned long *now_tm_sec)
1350{
1351 struct rtc_time tm;
1352 struct rtc_device *rtc;
1353 int rc;
1354
1355 rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
1356 if (rtc == NULL) {
1357 pr_err("%s: unable to open rtc device (%s)\n",
1358 __FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
Xiaozhe Shi0e01af62013-05-06 12:56:08 -07001359 return -EINVAL;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001360 }
1361
1362 rc = rtc_read_time(rtc, &tm);
1363 if (rc) {
1364 pr_err("Error reading rtc device (%s) : %d\n",
1365 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1366 goto close_time;
1367 }
1368
1369 rc = rtc_valid_tm(&tm);
1370 if (rc) {
1371 pr_err("Invalid RTC time (%s): %d\n",
1372 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1373 goto close_time;
1374 }
1375 rtc_tm_to_time(&tm, now_tm_sec);
1376
1377close_time:
1378 rtc_class_close(rtc);
1379 return rc;
1380}
1381
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08001382/* Returns estimated battery resistance */
1383static int get_prop_bms_batt_resistance(struct qpnp_bms_chip *chip)
1384{
1385 return chip->rbatt_mohm * 1000;
1386}
1387
1388/* Returns instantaneous current in uA */
1389static int get_prop_bms_current_now(struct qpnp_bms_chip *chip)
1390{
1391 int rc, result_ua;
1392
1393 rc = get_battery_current(chip, &result_ua);
1394 if (rc) {
1395 pr_err("failed to get current: %d\n", rc);
1396 return rc;
1397 }
1398 return result_ua;
1399}
1400
1401/* Returns coulomb counter in uAh */
1402static int get_prop_bms_charge_counter(struct qpnp_bms_chip *chip)
1403{
1404 int64_t cc_raw;
1405
1406 mutex_lock(&chip->bms_output_lock);
1407 lock_output_data(chip);
1408 read_cc_raw(chip, &cc_raw, false);
1409 unlock_output_data(chip);
1410 mutex_unlock(&chip->bms_output_lock);
1411
1412 return calculate_cc(chip, cc_raw, CC, NORESET);
1413}
1414
1415/* Returns shadow coulomb counter in uAh */
1416static int get_prop_bms_charge_counter_shadow(struct qpnp_bms_chip *chip)
1417{
1418 int64_t cc_raw;
1419
1420 mutex_lock(&chip->bms_output_lock);
1421 lock_output_data(chip);
1422 read_cc_raw(chip, &cc_raw, true);
1423 unlock_output_data(chip);
1424 mutex_unlock(&chip->bms_output_lock);
1425
1426 return calculate_cc(chip, cc_raw, SHDW_CC, NORESET);
1427}
1428
1429/* Returns full charge design in uAh */
1430static int get_prop_bms_charge_full_design(struct qpnp_bms_chip *chip)
1431{
1432 return chip->fcc_mah * 1000;
1433}
1434
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05301435/* Returns the current full charge in uAh */
1436static int get_prop_bms_charge_full(struct qpnp_bms_chip *chip)
1437{
1438 int rc;
1439 struct qpnp_vadc_result result;
1440
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001441 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05301442 if (rc) {
1443 pr_err("Unable to read battery temperature\n");
1444 return rc;
1445 }
1446
1447 return calculate_fcc(chip, (int)result.physical);
1448}
1449
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001450static int calculate_delta_time(unsigned long *time_stamp, int *delta_time_s)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001451{
1452 unsigned long now_tm_sec = 0;
1453
1454 /* default to delta time = 0 if anything fails */
1455 *delta_time_s = 0;
1456
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001457 if (get_current_time(&now_tm_sec)) {
1458 pr_err("RTC read failed\n");
1459 return 0;
1460 }
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001461
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001462 *delta_time_s = (now_tm_sec - *time_stamp);
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001463
1464 /* remember this time */
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001465 *time_stamp = now_tm_sec;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001466 return 0;
1467}
1468
Xiaozhe Shie118c692012-09-24 15:17:43 -07001469static void calculate_soc_params(struct qpnp_bms_chip *chip,
1470 struct raw_soc_params *raw,
1471 struct soc_params *params,
1472 int batt_temp)
1473{
Xiaozhe Shi219cb222013-06-10 15:49:59 -07001474 int soc_rbatt, shdw_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001475
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001476 calculate_delta_time(&chip->tm_sec, &params->delta_time_s);
1477 pr_debug("tm_sec = %ld, delta_s = %d\n",
1478 chip->tm_sec, params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001479 params->fcc_uah = calculate_fcc(chip, batt_temp);
1480 pr_debug("FCC = %uuAh batt_temp = %d\n", params->fcc_uah, batt_temp);
1481
1482 /* calculate remainging charge */
1483 params->ocv_charge_uah = calculate_ocv_charge(
1484 chip, raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001485 params->fcc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001486 pr_debug("ocv_charge_uah = %uuAh\n", params->ocv_charge_uah);
1487
1488 /* calculate cc micro_volt_hour */
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001489 params->cc_uah = calculate_cc(chip, raw->cc, CC, RESET);
Xiaozhe Shi219cb222013-06-10 15:49:59 -07001490 shdw_cc_uah = calculate_cc(chip, raw->shdw_cc, SHDW_CC, RESET);
1491 pr_debug("cc_uah = %duAh raw->cc = %llx, shdw_cc_uah = %duAh raw->shdw_cc = %llx\n",
1492 params->cc_uah, raw->cc,
1493 shdw_cc_uah, raw->shdw_cc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001494
1495 soc_rbatt = ((params->ocv_charge_uah - params->cc_uah) * 100)
1496 / params->fcc_uah;
1497 if (soc_rbatt < 0)
1498 soc_rbatt = 0;
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001499 params->rbatt_mohm = get_rbatt(chip, soc_rbatt, batt_temp);
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001500 pr_debug("rbatt_mohm = %d\n", params->rbatt_mohm);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001501
Xiaozhe Shi1e87cda2013-05-17 10:18:56 -07001502 if (params->rbatt_mohm != chip->rbatt_mohm) {
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07001503 chip->rbatt_mohm = params->rbatt_mohm;
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07001504 if (chip->bms_psy_registered)
Xiaozhe Shi1e87cda2013-05-17 10:18:56 -07001505 power_supply_changed(&chip->bms_psy);
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07001506 }
1507
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001508 calculate_iavg(chip, params->cc_uah, &params->iavg_ua,
1509 params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001510
1511 params->uuc_uah = calculate_unusable_charge_uah(chip, params,
1512 batt_temp);
1513 pr_debug("UUC = %uuAh\n", params->uuc_uah);
1514}
1515
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001516static int bound_soc(int soc)
1517{
1518 soc = max(0, soc);
1519 soc = min(100, soc);
1520 return soc;
1521}
1522
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001523#define IBAT_TOL_MASK 0x0F
1524#define OCV_TOL_MASK 0xF0
1525#define IBAT_TOL_DEFAULT 0x03
1526#define IBAT_TOL_NOCHG 0x0F
1527#define OCV_TOL_DEFAULT 0x20
1528#define OCV_TOL_NO_OCV 0x00
1529static int stop_ocv_updates(struct qpnp_bms_chip *chip)
1530{
1531 pr_debug("stopping ocv updates\n");
1532 return qpnp_masked_write(chip, BMS1_TOL_CTL,
1533 OCV_TOL_MASK, OCV_TOL_NO_OCV);
1534}
1535
1536static int reset_bms_for_test(struct qpnp_bms_chip *chip)
1537{
Xiaozhe Shi95da77f2013-02-20 13:40:06 -08001538 int ibat_ua = 0, vbat_uv = 0, rc;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001539 int ocv_est_uv;
1540
1541 if (!chip) {
1542 pr_err("BMS driver has not been initialized yet!\n");
1543 return -EINVAL;
1544 }
1545
1546 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
1547
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07001548 /*
1549 * Don't include rbatt and rbatt_capacitative since we expect this to
1550 * be used with a fake battery which does not have internal resistances
1551 */
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001552 ocv_est_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
1553 pr_debug("forcing ocv to be %d due to bms reset mode\n", ocv_est_uv);
1554 chip->last_ocv_uv = ocv_est_uv;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001555 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001556 chip->last_soc = -EINVAL;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001557 chip->last_soc_invalid = true;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001558 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001559 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001560 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001561 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001562 chip->last_cc_uah = INT_MIN;
1563 stop_ocv_updates(chip);
1564
1565 pr_debug("bms reset to ocv = %duv vbat_ua = %d ibat_ua = %d\n",
1566 chip->last_ocv_uv, vbat_uv, ibat_ua);
1567
1568 return rc;
1569}
1570
1571static int bms_reset_set(const char *val, const struct kernel_param *kp)
1572{
1573 int rc;
1574
1575 rc = param_set_bool(val, kp);
1576 if (rc) {
1577 pr_err("Unable to set bms_reset: %d\n", rc);
1578 return rc;
1579 }
1580
1581 if (*(bool *)kp->arg) {
1582 struct power_supply *bms_psy = power_supply_get_by_name("bms");
1583 struct qpnp_bms_chip *chip = container_of(bms_psy,
1584 struct qpnp_bms_chip, bms_psy);
1585
1586 rc = reset_bms_for_test(chip);
1587 if (rc) {
1588 pr_err("Unable to modify bms_reset: %d\n", rc);
1589 return rc;
1590 }
1591 }
1592 return 0;
1593}
1594
1595static struct kernel_param_ops bms_reset_ops = {
1596 .set = bms_reset_set,
1597 .get = param_get_bool,
1598};
1599
1600module_param_cb(bms_reset, &bms_reset_ops, &bms_reset, 0644);
1601
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001602#define SOC_STORAGE_MASK 0xFE
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001603static void backup_soc_and_iavg(struct qpnp_bms_chip *chip, int batt_temp,
1604 int soc)
1605{
1606 u8 temp;
1607 int rc;
1608 int iavg_ma = chip->prev_uuc_iavg_ma;
1609
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07001610 if (iavg_ma > MIN_IAVG_MA)
1611 temp = (iavg_ma - MIN_IAVG_MA) / IAVG_STEP_SIZE_MA;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001612 else
1613 temp = 0;
1614
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001615 rc = qpnp_write_wrapper(chip, &temp, chip->base + IAVG_STORAGE_REG, 1);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001616
1617 /* don't store soc if temperature is below 5degC */
1618 if (batt_temp > IGNORE_SOC_TEMP_DECIDEG)
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001619 qpnp_masked_write_base(chip, chip->soc_storage_addr,
1620 SOC_STORAGE_MASK, (soc + 1) << 1);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001621}
1622
1623static int scale_soc_while_chg(struct qpnp_bms_chip *chip, int chg_time_sec,
1624 int catch_up_sec, int new_soc, int prev_soc)
1625{
1626 int scaled_soc;
1627 int numerator;
1628
1629 /*
1630 * Don't report a high value immediately slowly scale the
1631 * value from prev_soc to the new soc based on a charge time
1632 * weighted average
1633 */
1634 pr_debug("cts = %d catch_up_sec = %d\n", chg_time_sec, catch_up_sec);
1635 if (catch_up_sec == 0)
1636 return new_soc;
1637
1638 if (chg_time_sec > catch_up_sec)
1639 return new_soc;
1640
1641 numerator = (catch_up_sec - chg_time_sec) * prev_soc
1642 + chg_time_sec * new_soc;
1643 scaled_soc = numerator / catch_up_sec;
1644
1645 pr_debug("cts = %d new_soc = %d prev_soc = %d scaled_soc = %d\n",
1646 chg_time_sec, new_soc, prev_soc, scaled_soc);
1647
1648 return scaled_soc;
1649}
1650
1651/*
1652 * bms_fake_battery is set in setups where a battery emulator is used instead
1653 * of a real battery. This makes the bms driver report a different/fake value
1654 * regardless of the calculated state of charge.
1655 */
1656static int bms_fake_battery = -EINVAL;
1657module_param(bms_fake_battery, int, 0644);
1658
1659static int report_voltage_based_soc(struct qpnp_bms_chip *chip)
1660{
1661 pr_debug("Reported voltage based soc = %d\n",
1662 chip->prev_voltage_based_soc);
1663 return chip->prev_voltage_based_soc;
1664}
1665
1666#define SOC_CATCHUP_SEC_MAX 600
1667#define SOC_CATCHUP_SEC_PER_PERCENT 60
1668#define MAX_CATCHUP_SOC (SOC_CATCHUP_SEC_MAX / SOC_CATCHUP_SEC_PER_PERCENT)
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07001669#define SOC_CHANGE_PER_SEC 5
Xiaozhe Shi27375822013-08-22 11:40:15 -07001670#define REPORT_SOC_WAIT_MS 10000
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001671static int report_cc_based_soc(struct qpnp_bms_chip *chip)
1672{
1673 int soc, soc_change;
1674 int time_since_last_change_sec, charge_time_sec = 0;
1675 unsigned long last_change_sec;
1676 struct timespec now;
1677 struct qpnp_vadc_result result;
1678 int batt_temp;
1679 int rc;
1680 bool charging, charging_since_last_report;
1681
Xiaozhe Shi27375822013-08-22 11:40:15 -07001682 rc = wait_event_interruptible_timeout(chip->bms_wait_queue,
1683 chip->calculated_soc != -EINVAL,
1684 round_jiffies_relative(msecs_to_jiffies
1685 (REPORT_SOC_WAIT_MS)));
1686
1687 if (rc == 0 && chip->calculated_soc == -EINVAL) {
1688 pr_debug("calculate soc timed out\n");
1689 } else if (rc == -ERESTARTSYS) {
1690 pr_err("Wait for SoC interrupted.\n");
1691 return rc;
1692 }
1693
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001694 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001695
1696 if (rc) {
1697 pr_err("error reading adc channel = %d, rc = %d\n",
1698 LR_MUX1_BATT_THERM, rc);
1699 return rc;
1700 }
1701 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
1702 result.measurement);
1703 batt_temp = (int)result.physical;
1704
1705 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shifa6ea692013-05-31 11:15:13 -07001706 soc = chip->calculated_soc;
1707
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001708 last_change_sec = chip->last_soc_change_sec;
1709 calculate_delta_time(&last_change_sec, &time_since_last_change_sec);
1710
1711 charging = is_battery_charging(chip);
1712 charging_since_last_report = charging || (chip->last_soc_unbound
1713 && chip->was_charging_at_sleep);
1714 /*
1715 * account for charge time - limit it to SOC_CATCHUP_SEC to
1716 * avoid overflows when charging continues for extended periods
1717 */
1718 if (charging) {
1719 if (chip->charge_start_tm_sec == 0) {
1720 /*
1721 * calculating soc for the first time
1722 * after start of chg. Initialize catchup time
1723 */
1724 if (abs(soc - chip->last_soc) < MAX_CATCHUP_SOC)
1725 chip->catch_up_time_sec =
1726 (soc - chip->last_soc)
1727 * SOC_CATCHUP_SEC_PER_PERCENT;
1728 else
1729 chip->catch_up_time_sec = SOC_CATCHUP_SEC_MAX;
1730
1731 if (chip->catch_up_time_sec < 0)
1732 chip->catch_up_time_sec = 0;
1733 chip->charge_start_tm_sec = last_change_sec;
1734 }
1735
1736 charge_time_sec = min(SOC_CATCHUP_SEC_MAX, (int)last_change_sec
1737 - chip->charge_start_tm_sec);
1738
1739 /* end catchup if calculated soc and last soc are same */
1740 if (chip->last_soc == soc)
1741 chip->catch_up_time_sec = 0;
1742 }
1743
1744 if (chip->last_soc != -EINVAL) {
1745 /*
1746 * last_soc < soc ... if we have not been charging at all
1747 * since the last time this was called, report previous SoC.
1748 * Otherwise, scale and catch up.
1749 */
1750 if (chip->last_soc < soc && !charging_since_last_report)
1751 soc = chip->last_soc;
1752 else if (chip->last_soc < soc && soc != 100)
1753 soc = scale_soc_while_chg(chip, charge_time_sec,
1754 chip->catch_up_time_sec,
1755 soc, chip->last_soc);
1756
1757 soc_change = min((int)abs(chip->last_soc - soc),
1758 time_since_last_change_sec / SOC_CHANGE_PER_SEC);
1759 if (chip->last_soc_unbound) {
1760 chip->last_soc_unbound = false;
1761 } else {
1762 /*
1763 * if soc have not been unbound by resume,
1764 * only change reported SoC by 1.
1765 */
1766 soc_change = min(1, soc_change);
1767 }
1768
1769 if (soc < chip->last_soc && soc != 0)
1770 soc = chip->last_soc - soc_change;
1771 if (soc > chip->last_soc && soc != 100)
1772 soc = chip->last_soc + soc_change;
1773 }
1774
Xiaozhe Shi208b8e52013-05-28 10:16:32 -07001775 if (chip->last_soc != soc && !chip->last_soc_unbound)
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001776 chip->last_soc_change_sec = last_change_sec;
1777
1778 pr_debug("last_soc = %d, calculated_soc = %d, soc = %d, time since last change = %d\n",
1779 chip->last_soc, chip->calculated_soc,
1780 soc, time_since_last_change_sec);
1781 chip->last_soc = bound_soc(soc);
1782 backup_soc_and_iavg(chip, batt_temp, chip->last_soc);
1783 pr_debug("Reported SOC = %d\n", chip->last_soc);
1784 chip->t_soc_queried = now;
1785 mutex_unlock(&chip->last_soc_mutex);
1786
1787 return soc;
1788}
1789
1790static int report_state_of_charge(struct qpnp_bms_chip *chip)
1791{
1792 if (bms_fake_battery != -EINVAL) {
1793 pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery);
1794 return bms_fake_battery;
1795 } else if (chip->use_voltage_soc)
1796 return report_voltage_based_soc(chip);
1797 else
1798 return report_cc_based_soc(chip);
1799}
1800
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001801#define VDD_MAX_ERR 5000
1802#define VDD_STEP_SIZE 10000
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001803static int charging_adjustments(struct qpnp_bms_chip *chip,
1804 struct soc_params *params, int soc,
1805 int vbat_uv, int ibat_ua, int batt_temp)
1806{
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001807 int chg_soc, soc_ibat, batt_terminal_uv, weight_ibat, weight_cc;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07001808 int new_ocv_uv;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001809
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001810 batt_terminal_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001811
1812 if (chip->soc_at_cv == -EINVAL) {
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001813 if (batt_terminal_uv >= chip->max_voltage_uv - VDD_MAX_ERR) {
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001814 chip->soc_at_cv = soc;
1815 chip->prev_chg_soc = soc;
1816 chip->ibat_at_cv_ua = ibat_ua;
1817 pr_debug("CC_TO_CV ibat_ua = %d CHG SOC %d\n",
1818 ibat_ua, soc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001819 } else {
1820 /* In constant current charging return the calc soc */
1821 pr_debug("CC CHG SOC %d\n", soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001822 }
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001823
1824 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001825 return soc;
1826 }
1827
1828 /*
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001829 * battery is in CV phase - begin linear interpolation of soc based on
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001830 * battery charge current
1831 */
1832
1833 /*
1834 * if voltage lessened (possibly because of a system load)
1835 * keep reporting the prev chg soc
1836 */
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001837 if (batt_terminal_uv <= chip->prev_batt_terminal_uv - VDD_STEP_SIZE) {
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08001838 pr_debug("batt_terminal_uv %d < (max = %d - 10000); CC CHG SOC %d\n",
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001839 batt_terminal_uv, chip->prev_batt_terminal_uv,
1840 chip->prev_chg_soc);
1841 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001842 return chip->prev_chg_soc;
1843 }
1844
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001845 soc_ibat = bound_soc(linear_interpolate(chip->soc_at_cv,
1846 chip->ibat_at_cv_ua,
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08001847 100, -1 * chip->chg_term_ua,
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001848 ibat_ua));
1849 weight_ibat = bound_soc(linear_interpolate(1, chip->soc_at_cv,
1850 100, 100, chip->prev_chg_soc));
1851 weight_cc = 100 - weight_ibat;
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001852 chg_soc = bound_soc(DIV_ROUND_CLOSEST(soc_ibat * weight_ibat
1853 + weight_cc * soc, 100));
1854
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001855 pr_debug("weight_ibat = %d, weight_cc = %d, soc_ibat = %d, soc_cc = %d\n",
1856 weight_ibat, weight_cc, soc_ibat, soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001857
1858 /* always report a higher soc */
1859 if (chg_soc > chip->prev_chg_soc) {
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001860 chip->prev_chg_soc = chg_soc;
1861
1862 find_ocv_for_soc(chip, params, batt_temp, chg_soc, &new_ocv_uv);
Xiaozhe Shicc48e992013-05-28 16:42:24 -07001863 chip->charging_adjusted_ocv = new_ocv_uv;
1864 pr_debug("CC CHG ADJ OCV = %d CHG SOC %d\n", new_ocv_uv,
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001865 chip->prev_chg_soc);
1866 }
1867
1868 pr_debug("Reporting CHG SOC %d\n", chip->prev_chg_soc);
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001869 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001870 return chip->prev_chg_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001871}
1872
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001873static void very_low_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
1874{
1875 /*
1876 * if battery is very low (v_cutoff voltage + 20mv) hold
1877 * a wakelock untill soc = 0%
1878 */
1879 if (vbat_uv <= chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001880 && !wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001881 pr_debug("voltage = %d low holding wakelock\n", vbat_uv);
1882 wake_lock(&chip->low_voltage_wake_lock);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001883 } else if (vbat_uv > chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001884 && wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001885 pr_debug("voltage = %d releasing wakelock\n", vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001886 wake_unlock(&chip->low_voltage_wake_lock);
1887 }
1888}
1889
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001890#define VBATT_ERROR_MARGIN 20000
1891static void cv_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
1892{
1893 /*
1894 * if battery is very low (v_cutoff voltage + 20mv) hold
1895 * a wakelock untill soc = 0%
1896 */
1897 if (wake_lock_active(&chip->cv_wake_lock)) {
1898 if (chip->soc_at_cv != -EINVAL) {
1899 pr_debug("hit CV, releasing cv wakelock\n");
1900 wake_unlock(&chip->cv_wake_lock);
1901 } else if (!is_battery_charging(chip)) {
1902 pr_debug("charging stopped, releasing cv wakelock\n");
1903 wake_unlock(&chip->cv_wake_lock);
1904 }
1905 } else if (vbat_uv > chip->max_voltage_uv - VBATT_ERROR_MARGIN
1906 && chip->soc_at_cv == -EINVAL
1907 && is_battery_charging(chip)
1908 && !wake_lock_active(&chip->cv_wake_lock)) {
1909 pr_debug("voltage = %d holding cv wakelock\n", vbat_uv);
1910 wake_lock(&chip->cv_wake_lock);
1911 }
1912}
1913
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07001914#define NO_ADJUST_HIGH_SOC_THRESHOLD 90
Xiaozhe Shie118c692012-09-24 15:17:43 -07001915static int adjust_soc(struct qpnp_bms_chip *chip, struct soc_params *params,
1916 int soc, int batt_temp)
1917{
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001918 int ibat_ua = 0, vbat_uv = 0;
1919 int ocv_est_uv = 0, soc_est = 0, pc_est = 0, pc = 0;
1920 int delta_ocv_uv = 0;
1921 int n = 0;
1922 int rc_new_uah = 0;
1923 int pc_new = 0;
1924 int soc_new = 0;
1925 int slope = 0;
1926 int rc = 0;
1927 int delta_ocv_uv_limit = 0;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07001928 int correction_limit_uv = 0;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001929
1930 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
1931 if (rc < 0) {
1932 pr_err("simultaneous vbat ibat failed err = %d\n", rc);
1933 goto out;
1934 }
1935
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001936 very_low_voltage_check(chip, vbat_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001937 cv_voltage_check(chip, vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001938
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001939 delta_ocv_uv_limit = DIV_ROUND_CLOSEST(ibat_ua, 1000);
1940
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001941 ocv_est_uv = vbat_uv + (ibat_ua * params->rbatt_mohm)/1000;
1942
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001943 pc_est = calculate_pc(chip, ocv_est_uv, batt_temp);
1944 soc_est = div_s64((s64)params->fcc_uah * pc_est - params->uuc_uah*100,
1945 (s64)params->fcc_uah - params->uuc_uah);
1946 soc_est = bound_soc(soc_est);
1947
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001948 /* never adjust during bms reset mode */
1949 if (bms_reset) {
1950 pr_debug("bms reset mode, SOC adjustment skipped\n");
1951 goto out;
1952 }
1953
Xiaozhe Shi8658c982013-04-30 11:33:07 -07001954 if (ibat_ua < 0 && !is_battery_full(chip)) {
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001955 soc = charging_adjustments(chip, params, soc, vbat_uv, ibat_ua,
1956 batt_temp);
1957 goto out;
1958 }
1959
1960 /*
1961 * do not adjust
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07001962 * if soc_est is same as what bms calculated
1963 * OR if soc_est > adjust_soc_low_threshold
1964 * OR if soc is above 90
1965 * because we might pull it low
1966 * and cause a bad user experience
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001967 */
1968 if (soc_est == soc
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07001969 || soc_est > chip->adjust_soc_low_threshold
1970 || soc >= NO_ADJUST_HIGH_SOC_THRESHOLD)
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001971 goto out;
1972
1973 if (chip->last_soc_est == -EINVAL)
1974 chip->last_soc_est = soc;
1975
1976 n = min(200, max(1 , soc + soc_est + chip->last_soc_est));
1977 chip->last_soc_est = soc_est;
1978
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001979 pc = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001980 if (pc > 0) {
1981 pc_new = calculate_pc(chip,
1982 chip->last_ocv_uv - (++slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001983 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001984 while (pc_new == pc) {
1985 /* start taking 10mV steps */
1986 slope = slope + 10;
1987 pc_new = calculate_pc(chip,
1988 chip->last_ocv_uv - (slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001989 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001990 }
1991 } else {
1992 /*
1993 * pc is already at the lowest point,
1994 * assume 1 millivolt translates to 1% pc
1995 */
1996 pc = 1;
1997 pc_new = 0;
1998 slope = 1;
1999 }
2000
2001 delta_ocv_uv = div_s64((soc - soc_est) * (s64)slope * 1000,
2002 n * (pc - pc_new));
2003
2004 if (abs(delta_ocv_uv) > delta_ocv_uv_limit) {
2005 pr_debug("limiting delta ocv %d limit = %d\n", delta_ocv_uv,
2006 delta_ocv_uv_limit);
2007
2008 if (delta_ocv_uv > 0)
2009 delta_ocv_uv = delta_ocv_uv_limit;
2010 else
2011 delta_ocv_uv = -1 * delta_ocv_uv_limit;
2012 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
2013 }
2014
Xiaozhe Shi08d9aa72013-07-31 17:15:12 -07002015 if (wake_lock_active(&chip->low_voltage_wake_lock))
2016 goto skip_limits;
2017
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002018 if (chip->last_ocv_uv > chip->flat_ocv_threshold_uv)
2019 correction_limit_uv = chip->high_ocv_correction_limit_uv;
2020 else
2021 correction_limit_uv = chip->low_ocv_correction_limit_uv;
2022
2023 if (abs(delta_ocv_uv) > correction_limit_uv) {
2024 pr_debug("limiting delta ocv %d limit = %d\n",
2025 delta_ocv_uv, correction_limit_uv);
2026 if (delta_ocv_uv > 0)
2027 delta_ocv_uv = correction_limit_uv;
2028 else
2029 delta_ocv_uv = -correction_limit_uv;
2030 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
2031 }
2032
Xiaozhe Shi08d9aa72013-07-31 17:15:12 -07002033skip_limits:
2034
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002035 chip->last_ocv_uv -= delta_ocv_uv;
2036
2037 if (chip->last_ocv_uv >= chip->max_voltage_uv)
2038 chip->last_ocv_uv = chip->max_voltage_uv;
2039
2040 /* calculate the soc based on this new ocv */
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002041 pc_new = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002042 rc_new_uah = (params->fcc_uah * pc_new) / 100;
2043 soc_new = (rc_new_uah - params->cc_uah - params->uuc_uah)*100
2044 / (params->fcc_uah - params->uuc_uah);
2045 soc_new = bound_soc(soc_new);
2046
2047 /*
2048 * if soc_new is ZERO force it higher so that phone doesnt report soc=0
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002049 * soc = 0 should happen only when soc_est is above a set value
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002050 */
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002051 if (soc_new == 0 && soc_est >= chip->hold_soc_est)
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002052 soc_new = 1;
2053
2054 soc = soc_new;
2055
2056out:
2057 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",
2058 ibat_ua, vbat_uv, ocv_est_uv, pc_est,
2059 soc_est, n, delta_ocv_uv, chip->last_ocv_uv,
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08002060 pc_new, soc_new, params->rbatt_mohm, slope);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002061
Xiaozhe Shie118c692012-09-24 15:17:43 -07002062 return soc;
2063}
2064
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002065static int clamp_soc_based_on_voltage(struct qpnp_bms_chip *chip, int soc)
2066{
2067 int rc, vbat_uv;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002068
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002069 rc = get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shi36458962013-02-06 16:19:57 -08002070 if (rc < 0) {
2071 pr_err("adc vbat failed err = %d\n", rc);
2072 return soc;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002073 }
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002074 if (soc == 0 && vbat_uv > chip->v_cutoff_uv) {
2075 pr_debug("clamping soc to 1, vbat (%d) > cutoff (%d)\n",
2076 vbat_uv, chip->v_cutoff_uv);
2077 return 1;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002078 } else {
2079 pr_debug("not clamping, using soc = %d, vbat = %d and cutoff = %d\n",
2080 soc, vbat_uv, chip->v_cutoff_uv);
2081 return soc;
2082 }
2083}
2084
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002085static int64_t convert_cc_uah_to_raw(struct qpnp_bms_chip *chip, int64_t cc_uah)
2086{
2087 int64_t cc_uv, cc_pvh, cc_raw;
2088
2089 cc_pvh = cc_uah * chip->r_sense_uohm;
2090 cc_uv = div_s64(cc_pvh * SLEEP_CLK_HZ * SECONDS_PER_HOUR,
2091 CC_READING_TICKS * 1000000LL);
2092 cc_raw = div_s64(cc_uv * CC_READING_RESOLUTION_D,
2093 CC_READING_RESOLUTION_N);
2094 return cc_raw;
2095}
2096
2097#define CC_STEP_INCREMENT_UAH 1500
2098#define OCV_STEP_INCREMENT 0x10
2099static void configure_soc_wakeup(struct qpnp_bms_chip *chip,
2100 struct soc_params *params,
2101 int batt_temp, int target_soc)
2102{
2103 int target_ocv_uv;
2104 int64_t target_cc_uah, cc_raw_64, current_shdw_cc_raw_64;
2105 int64_t current_shdw_cc_uah, iadc_comp_factor;
2106 uint64_t cc_raw, current_shdw_cc_raw;
2107 int16_t ocv_raw, current_ocv_raw;
2108
2109 current_shdw_cc_raw = 0;
2110 mutex_lock(&chip->bms_output_lock);
2111 lock_output_data(chip);
2112 qpnp_read_wrapper(chip, (u8 *)&current_ocv_raw,
2113 chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
2114 unlock_output_data(chip);
2115 mutex_unlock(&chip->bms_output_lock);
2116 current_shdw_cc_uah = get_prop_bms_charge_counter_shadow(chip);
2117 current_shdw_cc_raw_64 = convert_cc_uah_to_raw(chip,
2118 current_shdw_cc_uah);
2119
2120 /*
2121 * Calculate the target shadow coulomb counter threshold for when
2122 * the SoC changes.
2123 *
2124 * Since the BMS driver resets the shadow coulomb counter every
2125 * 20 seconds when the device is awake, calculate the threshold as
2126 * a delta from the current shadow coulomb count.
2127 */
2128 target_cc_uah = (100 - target_soc)
2129 * (params->fcc_uah - params->uuc_uah)
2130 / 100 - current_shdw_cc_uah;
2131 if (target_cc_uah < 0) {
2132 /*
2133 * If the target cc is below 0, that means we have already
2134 * passed the point where SoC should have fallen.
2135 * Set a wakeup in a few more mAh and check back again
2136 */
2137 target_cc_uah = CC_STEP_INCREMENT_UAH;
2138 }
2139 iadc_comp_factor = 100000;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002140 qpnp_iadc_comp_result(chip->iadc_dev, &iadc_comp_factor);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002141 target_cc_uah = div64_s64(target_cc_uah * 100000, iadc_comp_factor);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002142 target_cc_uah = cc_reverse_adjust_for_gain(chip, target_cc_uah);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002143 cc_raw_64 = convert_cc_uah_to_raw(chip, target_cc_uah);
2144 cc_raw = convert_s64_to_s36(cc_raw_64);
2145
2146 find_ocv_for_soc(chip, params, batt_temp, target_soc, &target_ocv_uv);
2147 ocv_raw = convert_vbatt_uv_to_raw(chip, target_ocv_uv);
2148
2149 /*
2150 * If the current_ocv_raw was updated since reaching 100% and is lower
2151 * than the calculated target ocv threshold, set the new target
2152 * threshold 1.5mAh lower in order to check if the SoC changed yet.
2153 */
2154 if (current_ocv_raw != chip->ocv_reading_at_100
2155 && current_ocv_raw < ocv_raw)
2156 ocv_raw = current_ocv_raw - OCV_STEP_INCREMENT;
2157
2158 qpnp_write_wrapper(chip, (u8 *)&cc_raw,
2159 chip->base + BMS1_SW_CC_THR0, 5);
2160 qpnp_write_wrapper(chip, (u8 *)&ocv_raw,
2161 chip->base + BMS1_OCV_THR0, 2);
2162
2163 pr_debug("current sw_cc_raw = 0x%llx, current ocv = 0x%hx\n",
2164 current_shdw_cc_raw, (uint16_t)current_ocv_raw);
2165 pr_debug("target_cc_uah = %lld, raw64 = 0x%llx, raw 36 = 0x%llx, ocv_raw = 0x%hx\n",
2166 target_cc_uah,
2167 (uint64_t)cc_raw_64, cc_raw,
2168 (uint16_t)ocv_raw);
2169}
2170
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002171static int calculate_raw_soc(struct qpnp_bms_chip *chip,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002172 struct raw_soc_params *raw,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002173 struct soc_params *params,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002174 int batt_temp)
2175{
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002176 int soc, new_ocv_uv;
2177 int remaining_usable_charge_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002178
Xiaozhe Shie118c692012-09-24 15:17:43 -07002179 /* calculate remaining usable charge */
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002180 remaining_usable_charge_uah = params->ocv_charge_uah
2181 - params->cc_uah
2182 - params->uuc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002183 pr_debug("RUC = %duAh\n", remaining_usable_charge_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002184
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002185 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002186 (params->fcc_uah - params->uuc_uah));
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002187
Xiaozhe Shie118c692012-09-24 15:17:43 -07002188 if (chip->first_time_calc_soc && soc < 0) {
2189 /*
2190 * first time calcualtion and the pon ocv is too low resulting
2191 * in a bad soc. Adjust ocv to get 0 soc
2192 */
2193 pr_debug("soc is %d, adjusting pon ocv to make it 0\n", soc);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002194 find_ocv_for_soc(chip, params, batt_temp, 0, &new_ocv_uv);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002195 chip->last_ocv_uv = new_ocv_uv;
2196
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002197 remaining_usable_charge_uah = params->ocv_charge_uah
2198 - params->cc_uah
2199 - params->uuc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002200
2201 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002202 (params->fcc_uah
2203 - params->uuc_uah));
Xiaozhe Shie118c692012-09-24 15:17:43 -07002204 pr_debug("DONE for O soc is %d, pon ocv adjusted to %duV\n",
2205 soc, chip->last_ocv_uv);
2206 }
2207
2208 if (soc > 100)
2209 soc = 100;
2210
2211 if (soc < 0) {
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002212 pr_debug("bad rem_usb_chg = %d rem_chg %d, cc_uah %d, unusb_chg %d\n",
Xiaozhe Shie118c692012-09-24 15:17:43 -07002213 remaining_usable_charge_uah,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002214 params->ocv_charge_uah,
2215 params->cc_uah, params->uuc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002216
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002217 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 -07002218 chip->last_ocv_uv, batt_temp,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002219 params->fcc_uah, soc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002220 soc = 0;
2221 }
2222
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002223 return soc;
2224}
2225
2226#define SLEEP_RECALC_INTERVAL 3
2227static int calculate_state_of_charge(struct qpnp_bms_chip *chip,
2228 struct raw_soc_params *raw,
2229 int batt_temp)
2230{
2231 struct soc_params params;
2232 int soc, previous_soc, shutdown_soc, new_calculated_soc;
2233 int remaining_usable_charge_uah, new_ocv_uv;
2234
2235 calculate_soc_params(chip, raw, &params, batt_temp);
2236 if (!is_battery_present(chip)) {
2237 pr_debug("battery gone, reporting 100\n");
2238 new_calculated_soc = 100;
2239 goto done_calculating;
2240 }
2241
2242 if (params.fcc_uah - params.uuc_uah <= 0) {
2243 pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
2244 params.fcc_uah,
2245 params.uuc_uah);
2246 new_calculated_soc = 0;
2247 goto done_calculating;
2248 }
2249
2250 soc = calculate_raw_soc(chip, raw, &params, batt_temp);
2251
Xiaozhe Shie118c692012-09-24 15:17:43 -07002252 mutex_lock(&chip->soc_invalidation_mutex);
2253 shutdown_soc = chip->shutdown_soc;
2254
2255 if (chip->first_time_calc_soc && soc != shutdown_soc
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002256 && !chip->shutdown_soc_invalid) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07002257 /*
2258 * soc for the first time - use shutdown soc
2259 * to adjust pon ocv since it is a small percent away from
2260 * the real soc
2261 */
2262 pr_debug("soc = %d before forcing shutdown_soc = %d\n",
2263 soc, shutdown_soc);
2264 find_ocv_for_soc(chip, &params, batt_temp,
2265 shutdown_soc, &new_ocv_uv);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002266 chip->last_ocv_uv = new_ocv_uv;
2267
2268 remaining_usable_charge_uah = params.ocv_charge_uah
2269 - params.cc_uah
2270 - params.uuc_uah;
2271
2272 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2273 (params.fcc_uah
2274 - params.uuc_uah));
2275
2276 pr_debug("DONE for shutdown_soc = %d soc is %d, adjusted ocv to %duV\n",
2277 shutdown_soc, soc, chip->last_ocv_uv);
2278 }
2279 mutex_unlock(&chip->soc_invalidation_mutex);
2280
2281 pr_debug("SOC before adjustment = %d\n", soc);
2282 new_calculated_soc = adjust_soc(chip, &params, soc, batt_temp);
2283
Xiaozhe Shi445d2492013-03-27 18:10:18 -07002284 /* always clamp soc due to BMS hw/sw immaturities */
2285 new_calculated_soc = clamp_soc_based_on_voltage(chip,
2286 new_calculated_soc);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002287 /*
2288 * If the battery is full, configure the cc threshold so the system
2289 * wakes up after SoC changes
2290 */
2291 if (is_battery_full(chip))
2292 configure_soc_wakeup(chip, &params,
2293 batt_temp, bound_soc(new_calculated_soc - 1));
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002294done_calculating:
Xiaozhe Shifa6ea692013-05-31 11:15:13 -07002295 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07002296 previous_soc = chip->calculated_soc;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002297 chip->calculated_soc = new_calculated_soc;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002298 pr_debug("CC based calculated SOC = %d\n", chip->calculated_soc);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002299 if (chip->last_soc_invalid) {
2300 chip->last_soc_invalid = false;
2301 chip->last_soc = -EINVAL;
2302 }
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002303 /*
2304 * Check if more than a long time has passed since the last
2305 * calculation (more than n times compared to the soc recalculation
2306 * rate, where n is defined by SLEEP_RECALC_INTERVAL). If this is true,
2307 * then the system must have gone through a long sleep, and SoC can be
2308 * allowed to become unbounded by the last reported SoC
2309 */
2310 if (params.delta_time_s * 1000 >
2311 chip->calculate_soc_ms * SLEEP_RECALC_INTERVAL
2312 && !chip->first_time_calc_soc) {
2313 chip->last_soc_unbound = true;
2314 chip->last_soc_change_sec = chip->last_recalc_time;
2315 pr_debug("last_soc unbound because elapsed time = %d\n",
2316 params.delta_time_s);
2317 }
2318 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi27375822013-08-22 11:40:15 -07002319 wake_up_interruptible(&chip->bms_wait_queue);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07002320
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002321 if (new_calculated_soc != previous_soc && chip->bms_psy_registered) {
Xiaozhe Shi83484e32013-05-16 10:59:59 -07002322 power_supply_changed(&chip->bms_psy);
2323 pr_debug("power supply changed\n");
2324 } else {
2325 /*
2326 * Call report state of charge anyways to periodically update
2327 * reported SoC. This prevents reported SoC from being stuck
2328 * when calculated soc doesn't change.
2329 */
2330 report_state_of_charge(chip);
2331 }
2332
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002333 get_current_time(&chip->last_recalc_time);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002334 chip->first_time_calc_soc = 0;
Xiaozhe Shi70633922013-09-23 15:50:53 -07002335 chip->first_time_calc_uuc = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002336 return chip->calculated_soc;
2337}
2338
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002339static int calculate_soc_from_voltage(struct qpnp_bms_chip *chip)
2340{
2341 int voltage_range_uv, voltage_remaining_uv, voltage_based_soc;
Xiaozhe Shi36458962013-02-06 16:19:57 -08002342 int rc, vbat_uv;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002343
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002344 rc = get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shi36458962013-02-06 16:19:57 -08002345 if (rc < 0) {
2346 pr_err("adc vbat failed err = %d\n", rc);
2347 return rc;
2348 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002349 voltage_range_uv = chip->max_voltage_uv - chip->v_cutoff_uv;
2350 voltage_remaining_uv = vbat_uv - chip->v_cutoff_uv;
2351 voltage_based_soc = voltage_remaining_uv * 100 / voltage_range_uv;
2352
2353 voltage_based_soc = clamp(voltage_based_soc, 0, 100);
2354
2355 if (chip->prev_voltage_based_soc != voltage_based_soc
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002356 && chip->bms_psy_registered) {
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002357 power_supply_changed(&chip->bms_psy);
2358 pr_debug("power supply changed\n");
2359 }
2360 chip->prev_voltage_based_soc = voltage_based_soc;
2361
2362 pr_debug("vbat used = %duv\n", vbat_uv);
2363 pr_debug("Calculated voltage based soc = %d\n", voltage_based_soc);
2364 return voltage_based_soc;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08002365}
2366
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002367static int recalculate_raw_soc(struct qpnp_bms_chip *chip)
2368{
2369 int batt_temp, rc, soc;
2370 struct qpnp_vadc_result result;
2371 struct raw_soc_params raw;
2372 struct soc_params params;
2373
2374 bms_stay_awake(&chip->soc_wake_source);
2375 if (chip->use_voltage_soc) {
2376 soc = calculate_soc_from_voltage(chip);
2377 } else {
2378 if (!chip->batfet_closed)
2379 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, true);
2380 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM,
2381 &result);
2382 if (rc) {
2383 pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n",
2384 LR_MUX1_BATT_THERM, rc);
2385 soc = chip->calculated_soc;
2386 } else {
2387 pr_debug("batt_temp phy = %lld meas = 0x%llx\n",
2388 result.physical,
2389 result.measurement);
2390 batt_temp = (int)result.physical;
2391
2392 mutex_lock(&chip->last_ocv_uv_mutex);
2393 read_soc_params_raw(chip, &raw, batt_temp);
2394 calculate_soc_params(chip, &raw, &params, batt_temp);
2395 if (!is_battery_present(chip)) {
2396 pr_debug("battery gone\n");
2397 soc = 0;
2398 } else if (params.fcc_uah - params.uuc_uah <= 0) {
2399 pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
2400 params.fcc_uah,
2401 params.uuc_uah);
2402 soc = 0;
2403 } else {
2404 soc = calculate_raw_soc(chip, &raw,
2405 &params, batt_temp);
2406 }
2407 mutex_unlock(&chip->last_ocv_uv_mutex);
2408 }
2409 }
2410 bms_relax(&chip->soc_wake_source);
2411 return soc;
2412}
2413
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002414static int recalculate_soc(struct qpnp_bms_chip *chip)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002415{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002416 int batt_temp, rc, soc;
2417 struct qpnp_vadc_result result;
2418 struct raw_soc_params raw;
2419
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002420 bms_stay_awake(&chip->soc_wake_source);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002421 mutex_lock(&chip->vbat_monitor_mutex);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002422 if (chip->vbat_monitor_params.state_request !=
2423 ADC_TM_HIGH_LOW_THR_DISABLE)
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002424 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2425 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002426 mutex_unlock(&chip->vbat_monitor_mutex);
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002427 if (chip->use_voltage_soc) {
2428 soc = calculate_soc_from_voltage(chip);
2429 } else {
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07002430 if (!chip->batfet_closed)
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002431 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, true);
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002432 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM,
2433 &result);
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002434 if (rc) {
2435 pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n",
2436 LR_MUX1_BATT_THERM, rc);
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002437 soc = chip->calculated_soc;
2438 } else {
2439 pr_debug("batt_temp phy = %lld meas = 0x%llx\n",
2440 result.physical,
2441 result.measurement);
2442 batt_temp = (int)result.physical;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002443
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002444 mutex_lock(&chip->last_ocv_uv_mutex);
2445 read_soc_params_raw(chip, &raw, batt_temp);
2446 soc = calculate_state_of_charge(chip, &raw, batt_temp);
2447 mutex_unlock(&chip->last_ocv_uv_mutex);
2448 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002449 }
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002450 bms_relax(&chip->soc_wake_source);
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002451 return soc;
2452}
2453
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002454static void recalculate_work(struct work_struct *work)
2455{
2456 struct qpnp_bms_chip *chip = container_of(work,
2457 struct qpnp_bms_chip,
2458 recalc_work);
2459
2460 recalculate_soc(chip);
2461}
2462
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002463static void calculate_soc_work(struct work_struct *work)
2464{
2465 struct qpnp_bms_chip *chip = container_of(work,
2466 struct qpnp_bms_chip,
2467 calculate_soc_delayed_work.work);
2468 int soc = recalculate_soc(chip);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002469
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002470 if (soc < chip->low_soc_calc_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002471 || wake_lock_active(&chip->low_voltage_wake_lock))
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002472 schedule_delayed_work(&chip->calculate_soc_delayed_work,
2473 round_jiffies_relative(msecs_to_jiffies
2474 (chip->low_soc_calculate_soc_ms)));
2475 else
2476 schedule_delayed_work(&chip->calculate_soc_delayed_work,
2477 round_jiffies_relative(msecs_to_jiffies
2478 (chip->calculate_soc_ms)));
2479}
2480
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002481static void configure_vbat_monitor_low(struct qpnp_bms_chip *chip)
2482{
2483 mutex_lock(&chip->vbat_monitor_mutex);
2484 if (chip->vbat_monitor_params.state_request
2485 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2486 /*
2487 * Battery is now around or below v_cutoff
2488 */
2489 pr_debug("battery entered cutoff range\n");
2490 if (!wake_lock_active(&chip->low_voltage_wake_lock)) {
2491 pr_debug("voltage low, holding wakelock\n");
2492 wake_lock(&chip->low_voltage_wake_lock);
2493 cancel_delayed_work_sync(
2494 &chip->calculate_soc_delayed_work);
2495 schedule_delayed_work(
2496 &chip->calculate_soc_delayed_work, 0);
2497 }
2498 chip->vbat_monitor_params.state_request =
2499 ADC_TM_HIGH_THR_ENABLE;
2500 chip->vbat_monitor_params.high_thr =
2501 (chip->low_voltage_threshold + VBATT_ERROR_MARGIN);
2502 pr_debug("set low thr to %d and high to %d\n",
2503 chip->vbat_monitor_params.low_thr,
2504 chip->vbat_monitor_params.high_thr);
2505 chip->vbat_monitor_params.low_thr = 0;
2506 } else if (chip->vbat_monitor_params.state_request
2507 == ADC_TM_LOW_THR_ENABLE) {
2508 /*
2509 * Battery is in normal operation range.
2510 */
2511 pr_debug("battery entered normal range\n");
2512 if (wake_lock_active(&chip->cv_wake_lock)) {
2513 wake_unlock(&chip->cv_wake_lock);
2514 pr_debug("releasing cv wake lock\n");
2515 }
2516 chip->in_cv_range = false;
2517 chip->vbat_monitor_params.state_request =
2518 ADC_TM_HIGH_LOW_THR_ENABLE;
2519 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2520 - VBATT_ERROR_MARGIN;
2521 chip->vbat_monitor_params.low_thr =
2522 chip->low_voltage_threshold;
2523 pr_debug("set low thr to %d and high to %d\n",
2524 chip->vbat_monitor_params.low_thr,
2525 chip->vbat_monitor_params.high_thr);
2526 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002527 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2528 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002529 mutex_unlock(&chip->vbat_monitor_mutex);
2530}
2531
2532#define CV_LOW_THRESHOLD_HYST_UV 100000
2533static void configure_vbat_monitor_high(struct qpnp_bms_chip *chip)
2534{
2535 mutex_lock(&chip->vbat_monitor_mutex);
2536 if (chip->vbat_monitor_params.state_request
2537 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2538 /*
2539 * Battery is around vddmax
2540 */
2541 pr_debug("battery entered vddmax range\n");
2542 chip->in_cv_range = true;
2543 if (!wake_lock_active(&chip->cv_wake_lock)) {
2544 wake_lock(&chip->cv_wake_lock);
2545 pr_debug("holding cv wake lock\n");
2546 }
2547 schedule_work(&chip->recalc_work);
2548 chip->vbat_monitor_params.state_request =
2549 ADC_TM_LOW_THR_ENABLE;
2550 chip->vbat_monitor_params.low_thr =
2551 (chip->max_voltage_uv - CV_LOW_THRESHOLD_HYST_UV);
2552 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv * 2;
2553 pr_debug("set low thr to %d and high to %d\n",
2554 chip->vbat_monitor_params.low_thr,
2555 chip->vbat_monitor_params.high_thr);
2556 } else if (chip->vbat_monitor_params.state_request
2557 == ADC_TM_HIGH_THR_ENABLE) {
2558 /*
2559 * Battery is in normal operation range.
2560 */
2561 pr_debug("battery entered normal range\n");
2562 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2563 pr_debug("voltage high, releasing wakelock\n");
2564 wake_unlock(&chip->low_voltage_wake_lock);
2565 }
2566 chip->vbat_monitor_params.state_request =
2567 ADC_TM_HIGH_LOW_THR_ENABLE;
2568 chip->vbat_monitor_params.high_thr =
2569 chip->max_voltage_uv - VBATT_ERROR_MARGIN;
2570 chip->vbat_monitor_params.low_thr =
2571 chip->low_voltage_threshold;
2572 pr_debug("set low thr to %d and high to %d\n",
2573 chip->vbat_monitor_params.low_thr,
2574 chip->vbat_monitor_params.high_thr);
2575 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002576 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2577 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002578 mutex_unlock(&chip->vbat_monitor_mutex);
2579}
2580
2581static void btm_notify_vbat(enum qpnp_tm_state state, void *ctx)
2582{
2583 struct qpnp_bms_chip *chip = ctx;
2584 int vbat_uv;
2585 struct qpnp_vadc_result result;
2586 int rc;
2587
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002588 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &result);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002589 pr_debug("vbat = %lld, raw = 0x%x\n", result.physical, result.adc_code);
2590
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002591 get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002592 pr_debug("vbat is at %d, state is at %d\n", vbat_uv, state);
2593
2594 if (state == ADC_TM_LOW_STATE) {
2595 pr_debug("low voltage btm notification triggered\n");
2596 if (vbat_uv - VBATT_ERROR_MARGIN
2597 < chip->vbat_monitor_params.low_thr) {
2598 configure_vbat_monitor_low(chip);
2599 } else {
2600 pr_debug("faulty btm trigger, discarding\n");
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002601 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002602 &chip->vbat_monitor_params);
2603 }
2604 } else if (state == ADC_TM_HIGH_STATE) {
2605 pr_debug("high voltage btm notification triggered\n");
2606 if (vbat_uv + VBATT_ERROR_MARGIN
2607 > chip->vbat_monitor_params.high_thr) {
2608 configure_vbat_monitor_high(chip);
2609 } else {
2610 pr_debug("faulty btm trigger, discarding\n");
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002611 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002612 &chip->vbat_monitor_params);
2613 }
2614 } else {
2615 pr_debug("unknown voltage notification state: %d\n", state);
2616 }
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002617 if (chip->bms_psy_registered)
Xiaozhe Shifa120db2013-06-06 15:57:19 -07002618 power_supply_changed(&chip->bms_psy);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002619}
2620
2621static int reset_vbat_monitoring(struct qpnp_bms_chip *chip)
2622{
2623 int rc;
2624
2625 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_DISABLE;
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002626
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002627 rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2628 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002629 if (rc) {
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002630 pr_err("tm disable failed: %d\n", rc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002631 return rc;
2632 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002633 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2634 pr_debug("battery removed, releasing wakelock\n");
2635 wake_unlock(&chip->low_voltage_wake_lock);
2636 }
2637 if (chip->in_cv_range) {
2638 pr_debug("battery removed, removing in_cv_range state\n");
2639 chip->in_cv_range = false;
2640 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002641 return 0;
2642}
2643
2644static int setup_vbat_monitoring(struct qpnp_bms_chip *chip)
2645{
2646 int rc;
2647
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002648 chip->vbat_monitor_params.low_thr = chip->low_voltage_threshold;
2649 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2650 - VBATT_ERROR_MARGIN;
2651 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_ENABLE;
2652 chip->vbat_monitor_params.channel = VBAT_SNS;
2653 chip->vbat_monitor_params.btm_ctx = (void *)chip;
2654 chip->vbat_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
2655 chip->vbat_monitor_params.threshold_notification = &btm_notify_vbat;
2656 pr_debug("set low thr to %d and high to %d\n",
2657 chip->vbat_monitor_params.low_thr,
2658 chip->vbat_monitor_params.high_thr);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002659
2660 if (!is_battery_present(chip)) {
2661 pr_debug("no battery inserted, do not enable vbat monitoring\n");
2662 chip->vbat_monitor_params.state_request =
2663 ADC_TM_HIGH_LOW_THR_DISABLE;
2664 } else {
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002665 rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2666 &chip->vbat_monitor_params);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002667 if (rc) {
2668 pr_err("tm setup failed: %d\n", rc);
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002669 return rc;
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002670 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002671 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002672
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002673 pr_debug("setup complete\n");
2674 return 0;
2675}
2676
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302677static void readjust_fcc_table(struct qpnp_bms_chip *chip)
2678{
2679 struct single_row_lut *temp, *old;
2680 int i, fcc, ratio;
2681
2682 if (!chip->enable_fcc_learning)
2683 return;
2684
2685 if (!chip->fcc_temp_lut) {
2686 pr_err("The static fcc lut table is NULL\n");
2687 return;
2688 }
2689
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07002690 temp = devm_kzalloc(chip->dev, sizeof(struct single_row_lut),
2691 GFP_KERNEL);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302692 if (!temp) {
2693 pr_err("Cannot allocate memory for adjusted fcc table\n");
2694 return;
2695 }
2696
2697 fcc = interpolate_fcc(chip->fcc_temp_lut, chip->fcc_new_batt_temp);
2698
2699 temp->cols = chip->fcc_temp_lut->cols;
2700 for (i = 0; i < chip->fcc_temp_lut->cols; i++) {
2701 temp->x[i] = chip->fcc_temp_lut->x[i];
2702 ratio = div_u64(chip->fcc_temp_lut->y[i] * 1000, fcc);
2703 temp->y[i] = (ratio * chip->fcc_new_mah);
2704 temp->y[i] /= 1000;
2705 }
2706
2707 old = chip->adjusted_fcc_temp_lut;
2708 chip->adjusted_fcc_temp_lut = temp;
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07002709 devm_kfree(chip->dev, old);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302710}
2711
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302712static int read_fcc_data_from_backup(struct qpnp_bms_chip *chip)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302713{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302714 int rc, i;
2715 u8 fcc = 0, chgcyl = 0;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302716
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302717 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2718 rc = qpnp_read_wrapper(chip, &fcc,
2719 chip->base + BMS_FCC_BASE_REG + i, 1);
2720 rc |= qpnp_read_wrapper(chip, &chgcyl,
2721 chip->base + BMS_CHGCYL_BASE_REG + i, 1);
2722 if (rc) {
2723 pr_err("Unable to read FCC data\n");
2724 return rc;
2725 }
2726 if (fcc == 0 || (fcc == 0xFF && chgcyl == 0xFF)) {
2727 /* FCC invalid/not present */
2728 chip->fcc_learning_samples[i].fcc_new = 0;
2729 chip->fcc_learning_samples[i].chargecycles = 0;
2730 } else {
2731 /* valid FCC data */
2732 chip->fcc_sample_count++;
2733 chip->fcc_learning_samples[i].fcc_new =
2734 fcc * chip->fcc_resolution;
2735 chip->fcc_learning_samples[i].chargecycles =
2736 chgcyl * CHGCYL_RESOLUTION;
2737 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302738 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302739
2740 return 0;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302741}
2742
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302743static int discard_backup_fcc_data(struct qpnp_bms_chip *chip)
2744{
2745 int rc = 0, i;
2746 u8 temp_u8 = 0;
2747
2748 chip->fcc_sample_count = 0;
2749 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2750 rc = qpnp_write_wrapper(chip, &temp_u8,
2751 chip->base + BMS_FCC_BASE_REG + i, 1);
2752 rc |= qpnp_write_wrapper(chip, &temp_u8,
2753 chip->base + BMS_CHGCYL_BASE_REG + i, 1);
2754 if (rc) {
2755 pr_err("Unable to clear FCC data\n");
2756 return rc;
2757 }
2758 }
2759
2760 return 0;
2761}
2762
2763static void
2764average_fcc_samples_and_readjust_fcc_table(struct qpnp_bms_chip *chip)
2765{
2766 int i, temp_fcc_avg = 0, temp_fcc_delta = 0, new_fcc_avg = 0;
2767 struct fcc_sample *ft;
2768
2769 for (i = 0; i < chip->min_fcc_learning_samples; i++)
2770 temp_fcc_avg += chip->fcc_learning_samples[i].fcc_new;
2771
2772 temp_fcc_avg /= chip->min_fcc_learning_samples;
2773 temp_fcc_delta = div_u64(temp_fcc_avg * DELTA_FCC_PERCENT, 100);
2774
2775 /* fix the fcc if its an outlier i.e. > 5% of the average */
2776 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2777 ft = &chip->fcc_learning_samples[i];
2778 if (abs(ft->fcc_new - temp_fcc_avg) > temp_fcc_delta)
2779 new_fcc_avg += temp_fcc_avg;
2780 else
2781 new_fcc_avg += ft->fcc_new;
2782 }
2783 new_fcc_avg /= chip->min_fcc_learning_samples;
2784
2785 chip->fcc_new_mah = new_fcc_avg;
2786 chip->fcc_new_batt_temp = FCC_DEFAULT_TEMP;
2787 pr_info("FCC update: New fcc_mah=%d, fcc_batt_temp=%d\n",
2788 new_fcc_avg, FCC_DEFAULT_TEMP);
2789 readjust_fcc_table(chip);
2790}
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302791
2792static void backup_charge_cycle(struct qpnp_bms_chip *chip)
2793{
2794 int rc = 0;
2795
2796 if (chip->charge_increase >= 0) {
2797 rc = qpnp_write_wrapper(chip, &chip->charge_increase,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302798 chip->base + CHARGE_INCREASE_STORAGE, 1);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302799 if (rc)
2800 pr_err("Unable to backup charge_increase\n");
2801 }
2802
2803 if (chip->charge_cycles >= 0) {
2804 rc = qpnp_write_wrapper(chip, (u8 *)&chip->charge_cycles,
2805 chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
2806 if (rc)
2807 pr_err("Unable to backup charge_cycles\n");
2808 }
2809}
2810
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302811static bool chargecycles_in_range(struct qpnp_bms_chip *chip)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302812{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302813 int i, min_cycle, max_cycle, valid_range;
2814
2815 /* find the smallest and largest charge cycle */
2816 max_cycle = min_cycle = chip->fcc_learning_samples[0].chargecycles;
2817 for (i = 1; i < chip->min_fcc_learning_samples; i++) {
2818 if (min_cycle > chip->fcc_learning_samples[i].chargecycles)
2819 min_cycle = chip->fcc_learning_samples[i].chargecycles;
2820 if (max_cycle < chip->fcc_learning_samples[i].chargecycles)
2821 max_cycle = chip->fcc_learning_samples[i].chargecycles;
2822 }
2823
2824 /* check if chargecyles are in range to continue with FCC update */
2825 valid_range = DIV_ROUND_UP(VALID_FCC_CHGCYL_RANGE,
2826 CHGCYL_RESOLUTION) * CHGCYL_RESOLUTION;
2827 if (abs(max_cycle - min_cycle) > valid_range)
2828 return false;
2829
2830 return true;
2831}
2832
2833static int read_chgcycle_data_from_backup(struct qpnp_bms_chip *chip)
2834{
2835 int rc;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302836 uint16_t temp_u16 = 0;
2837 u8 temp_u8 = 0;
2838
2839 rc = qpnp_read_wrapper(chip, &temp_u8,
2840 chip->base + CHARGE_INCREASE_STORAGE, 1);
2841 if (!rc && temp_u8 != 0xFF)
2842 chip->charge_increase = temp_u8;
2843
2844 rc = qpnp_read_wrapper(chip, (u8 *)&temp_u16,
2845 chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
2846 if (!rc && temp_u16 != 0xFFFF)
2847 chip->charge_cycles = temp_u16;
2848
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302849 return rc;
2850}
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302851
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302852static void
2853attempt_learning_new_fcc(struct qpnp_bms_chip *chip)
2854{
2855 pr_debug("Total FCC sample count=%d\n", chip->fcc_sample_count);
2856
2857 /* update FCC if we have the required samples */
2858 if ((chip->fcc_sample_count == chip->min_fcc_learning_samples) &&
2859 chargecycles_in_range(chip))
2860 average_fcc_samples_and_readjust_fcc_table(chip);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302861}
2862
2863static int calculate_real_soc(struct qpnp_bms_chip *chip,
2864 int batt_temp, struct raw_soc_params *raw, int cc_uah)
2865{
2866 int fcc_uah, rc_uah;
2867
2868 fcc_uah = calculate_fcc(chip, batt_temp);
2869 rc_uah = calculate_ocv_charge(chip, raw, fcc_uah);
2870
2871 return ((rc_uah - cc_uah) * 100) / fcc_uah;
2872}
2873
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302874#define MAX_U8_VALUE ((u8)(~0U))
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302875
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302876static int backup_new_fcc(struct qpnp_bms_chip *chip, int fcc_mah,
2877 int chargecycles)
2878{
2879 int rc, min_cycle, i;
2880 u8 fcc_new, chgcyl, pos = 0;
2881 struct fcc_sample *ft;
2882
2883 if ((fcc_mah > (chip->fcc_resolution * MAX_U8_VALUE)) ||
2884 (chargecycles > (CHGCYL_RESOLUTION * MAX_U8_VALUE))) {
2885 pr_warn("FCC/Chgcyl beyond storage limit. FCC=%d, chgcyl=%d\n",
2886 fcc_mah, chargecycles);
2887 return -EINVAL;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302888 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302889
2890 if (chip->fcc_sample_count == chip->min_fcc_learning_samples) {
2891 /* search best location - oldest entry */
2892 min_cycle = chip->fcc_learning_samples[0].chargecycles;
2893 for (i = 1; i < chip->min_fcc_learning_samples; i++) {
2894 if (min_cycle >
2895 chip->fcc_learning_samples[i].chargecycles)
2896 pos = i;
2897 }
2898 } else {
2899 /* find an empty location */
2900 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2901 ft = &chip->fcc_learning_samples[i];
2902 if (ft->fcc_new == 0 || (ft->fcc_new == 0xFF &&
2903 ft->chargecycles == 0xFF)) {
2904 pos = i;
2905 break;
2906 }
2907 }
2908 chip->fcc_sample_count++;
2909 }
2910 chip->fcc_learning_samples[pos].fcc_new = fcc_mah;
2911 chip->fcc_learning_samples[pos].chargecycles = chargecycles;
2912
2913 fcc_new = DIV_ROUND_UP(fcc_mah, chip->fcc_resolution);
2914 rc = qpnp_write_wrapper(chip, (u8 *)&fcc_new,
2915 chip->base + BMS_FCC_BASE_REG + pos, 1);
2916 if (rc)
2917 return rc;
2918
2919 chgcyl = DIV_ROUND_UP(chargecycles, CHGCYL_RESOLUTION);
2920 rc = qpnp_write_wrapper(chip, (u8 *)&chgcyl,
2921 chip->base + BMS_CHGCYL_BASE_REG + pos, 1);
2922 if (rc)
2923 return rc;
2924
2925 pr_debug("Backup new FCC: fcc_new=%d, chargecycle=%d, pos=%d\n",
2926 fcc_new, chgcyl, pos);
2927
2928 return rc;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302929}
2930
2931static void update_fcc_learning_table(struct qpnp_bms_chip *chip,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302932 int new_fcc_uah, int chargecycles, int batt_temp)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302933{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302934 int rc, fcc_default, fcc_temp;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302935
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302936 /* convert the fcc at batt_temp to new fcc at FCC_DEFAULT_TEMP */
2937 fcc_default = calculate_fcc(chip, FCC_DEFAULT_TEMP) / 1000;
2938 fcc_temp = calculate_fcc(chip, batt_temp) / 1000;
2939 new_fcc_uah = (new_fcc_uah / fcc_temp) * fcc_default;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302940
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302941 rc = backup_new_fcc(chip, new_fcc_uah / 1000, chargecycles);
2942 if (rc) {
2943 pr_err("Unable to backup new FCC\n");
2944 return;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302945 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302946 /* check if FCC can be updated */
2947 attempt_learning_new_fcc(chip);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302948}
2949
2950static bool is_new_fcc_valid(int new_fcc_uah, int fcc_uah)
2951{
2952 if ((new_fcc_uah >= (fcc_uah / 2)) &&
2953 ((new_fcc_uah * 100) <= (fcc_uah * 105)))
2954 return true;
2955
2956 pr_debug("FCC rejected - not within valid limit\n");
2957 return false;
2958}
2959
2960static void fcc_learning_config(struct qpnp_bms_chip *chip, bool start)
2961{
2962 int rc, batt_temp;
2963 struct raw_soc_params raw;
2964 struct qpnp_vadc_result result;
2965 int fcc_uah, new_fcc_uah, delta_cc_uah, delta_soc;
2966
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002967 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302968 if (rc) {
2969 pr_err("Unable to read batt_temp\n");
2970 return;
2971 } else {
2972 batt_temp = (int)result.physical;
2973 }
2974
2975 rc = read_soc_params_raw(chip, &raw, batt_temp);
2976 if (rc) {
2977 pr_err("Unable to read CC, cannot update FCC\n");
2978 return;
2979 }
2980
2981 if (start) {
2982 chip->start_pc = interpolate_pc(chip->pc_temp_ocv_lut,
2983 batt_temp / 10, raw.last_good_ocv_uv / 1000);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07002984 chip->start_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302985 chip->start_real_soc = calculate_real_soc(chip,
2986 batt_temp, &raw, chip->start_cc_uah);
2987 pr_debug("start_pc=%d, start_cc=%d, start_soc=%d real_soc=%d\n",
2988 chip->start_pc, chip->start_cc_uah,
2989 chip->start_soc, chip->start_real_soc);
2990 } else {
Xiaozhe Shif3da8622013-06-10 14:50:56 -07002991 chip->end_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302992 delta_soc = 100 - chip->start_real_soc;
2993 delta_cc_uah = abs(chip->end_cc_uah - chip->start_cc_uah);
2994 new_fcc_uah = div_u64(delta_cc_uah * 100, delta_soc);
2995 fcc_uah = calculate_fcc(chip, batt_temp);
2996 pr_debug("start_soc=%d, start_pc=%d, start_real_soc=%d, start_cc=%d, end_cc=%d, new_fcc=%d\n",
2997 chip->start_soc, chip->start_pc, chip->start_real_soc,
2998 chip->start_cc_uah, chip->end_cc_uah, new_fcc_uah);
2999
3000 if (is_new_fcc_valid(new_fcc_uah, fcc_uah))
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303001 update_fcc_learning_table(chip, new_fcc_uah,
3002 chip->charge_cycles, batt_temp);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303003 }
3004}
3005
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003006#define MAX_CAL_TRIES 200
3007#define MIN_CAL_UA 3000
3008static void batfet_open_work(struct work_struct *work)
3009{
3010 int i;
3011 int rc;
3012 int result_ua;
3013 u8 orig_delay, sample_delay;
3014 struct qpnp_bms_chip *chip = container_of(work,
3015 struct qpnp_bms_chip,
3016 batfet_open_work);
3017
3018 rc = qpnp_read_wrapper(chip, &orig_delay,
3019 chip->base + BMS1_S1_DELAY_CTL, 1);
3020
3021 sample_delay = 0x0;
3022 rc = qpnp_write_wrapper(chip, &sample_delay,
3023 chip->base + BMS1_S1_DELAY_CTL, 1);
3024
3025 /*
3026 * In certain PMICs there is a coupling issue which causes
3027 * bad calibration value that result in a huge battery current
3028 * even when the BATFET is open. Do continious calibrations until
3029 * we hit reasonable cal values which result in low battery current
3030 */
3031
3032 for (i = 0; (!chip->batfet_closed) && i < MAX_CAL_TRIES; i++) {
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003033 rc = qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003034 /*
3035 * Wait 20mS after calibration and before reading battery
3036 * current. The BMS h/w uses calibration values in the
3037 * next sampling of vsense.
3038 */
3039 msleep(20);
3040 rc |= get_battery_current(chip, &result_ua);
3041 if (rc == 0 && abs(result_ua) <= MIN_CAL_UA) {
3042 pr_debug("good cal at %d attempt\n", i);
3043 break;
3044 }
3045 }
3046 pr_debug("batfet_closed = %d i = %d result_ua = %d\n",
3047 chip->batfet_closed, i, result_ua);
3048
3049 rc = qpnp_write_wrapper(chip, &orig_delay,
3050 chip->base + BMS1_S1_DELAY_CTL, 1);
3051}
3052
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003053static void charging_began(struct qpnp_bms_chip *chip)
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003054{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003055 mutex_lock(&chip->last_soc_mutex);
3056 chip->charge_start_tm_sec = 0;
3057 chip->catch_up_time_sec = 0;
3058 mutex_unlock(&chip->last_soc_mutex);
3059
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303060 chip->start_soc = report_state_of_charge(chip);
3061
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003062 mutex_lock(&chip->last_ocv_uv_mutex);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303063 if (chip->enable_fcc_learning)
3064 fcc_learning_config(chip, true);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003065 chip->soc_at_cv = -EINVAL;
3066 chip->prev_chg_soc = -EINVAL;
3067 mutex_unlock(&chip->last_ocv_uv_mutex);
3068}
3069
3070static void charging_ended(struct qpnp_bms_chip *chip)
3071{
3072 mutex_lock(&chip->last_soc_mutex);
3073 chip->charge_start_tm_sec = 0;
3074 chip->catch_up_time_sec = 0;
3075 mutex_unlock(&chip->last_soc_mutex);
3076
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303077 chip->end_soc = report_state_of_charge(chip);
3078
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003079 mutex_lock(&chip->last_ocv_uv_mutex);
3080 chip->soc_at_cv = -EINVAL;
3081 chip->prev_chg_soc = -EINVAL;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303082
3083 /* update the chargecycles */
3084 if (chip->end_soc > chip->start_soc) {
3085 chip->charge_increase += (chip->end_soc - chip->start_soc);
3086 if (chip->charge_increase > 100) {
3087 chip->charge_cycles++;
3088 chip->charge_increase = chip->charge_increase % 100;
3089 }
3090 if (chip->enable_fcc_learning)
3091 backup_charge_cycle(chip);
3092 }
3093
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003094 if (get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL) {
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303095 if (chip->enable_fcc_learning &&
3096 (chip->start_soc <= chip->min_fcc_learning_soc) &&
3097 (chip->start_pc <= chip->min_fcc_ocv_pc))
3098 fcc_learning_config(chip, false);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003099 chip->done_charging = true;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003100 chip->last_soc_invalid = true;
Xiaozhe Shicc48e992013-05-28 16:42:24 -07003101 } else if (chip->charging_adjusted_ocv > 0) {
3102 pr_debug("Charging stopped before full, adjusted OCV = %d\n",
3103 chip->charging_adjusted_ocv);
3104 chip->last_ocv_uv = chip->charging_adjusted_ocv;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003105 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303106
Xiaozhe Shicc48e992013-05-28 16:42:24 -07003107 chip->charging_adjusted_ocv = -EINVAL;
3108
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003109 mutex_unlock(&chip->last_ocv_uv_mutex);
3110}
3111
3112static void battery_status_check(struct qpnp_bms_chip *chip)
3113{
3114 int status = get_battery_status(chip);
3115
Xiaozhe Shibda84992013-09-05 10:39:11 -07003116 mutex_lock(&chip->status_lock);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003117 if (chip->battery_status != status) {
Xiaozhe Shi30e94802013-08-19 16:40:53 -07003118 pr_debug("status = %d, shadow status = %d\n",
3119 status, chip->battery_status);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003120 if (status == POWER_SUPPLY_STATUS_CHARGING) {
3121 pr_debug("charging started\n");
3122 charging_began(chip);
3123 } else if (chip->battery_status
3124 == POWER_SUPPLY_STATUS_CHARGING) {
3125 pr_debug("charging ended\n");
3126 charging_ended(chip);
3127 }
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003128
3129 if (status == POWER_SUPPLY_STATUS_FULL) {
3130 pr_debug("battery full\n");
3131 enable_bms_irq(&chip->ocv_thr_irq);
3132 enable_bms_irq(&chip->sw_cc_thr_irq);
Xiaozhe Shibda84992013-09-05 10:39:11 -07003133 recalculate_soc(chip);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003134 } else if (chip->battery_status
3135 == POWER_SUPPLY_STATUS_FULL) {
3136 pr_debug("battery not full any more\n");
3137 disable_bms_irq(&chip->ocv_thr_irq);
3138 disable_bms_irq(&chip->sw_cc_thr_irq);
3139 }
3140
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003141 chip->battery_status = status;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003142 /* battery charge status has changed, so force a soc
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003143 * recalculation to update the SoC */
3144 schedule_work(&chip->recalc_work);
3145 }
Xiaozhe Shibda84992013-09-05 10:39:11 -07003146 mutex_unlock(&chip->status_lock);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003147}
3148
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003149#define CALIB_WRKARND_DIG_MAJOR_MAX 0x03
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003150static void batfet_status_check(struct qpnp_bms_chip *chip)
3151{
3152 bool batfet_closed;
3153
3154 if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX)
3155 return;
3156
3157 batfet_closed = is_batfet_closed(chip);
3158 if (chip->batfet_closed != batfet_closed) {
3159 chip->batfet_closed = batfet_closed;
3160 if (batfet_closed == false) {
3161 /* batfet opened */
3162 schedule_work(&chip->batfet_open_work);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003163 qpnp_iadc_skip_calibration(chip->iadc_dev);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003164 } else {
3165 /* batfet closed */
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003166 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, true);
3167 qpnp_iadc_resume_calibration(chip->iadc_dev);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003168 }
3169 }
3170}
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003171
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003172static void battery_insertion_check(struct qpnp_bms_chip *chip)
3173{
Xiaozhe Shi90f3a412013-08-21 10:31:35 -07003174 int present = (int)is_battery_present(chip);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003175
3176 mutex_lock(&chip->vbat_monitor_mutex);
3177 if (chip->battery_present != present) {
3178 if (chip->battery_present != -EINVAL) {
3179 if (present) {
3180 setup_vbat_monitoring(chip);
3181 chip->new_battery = true;
3182 } else {
3183 reset_vbat_monitoring(chip);
3184 }
3185 }
3186 chip->battery_present = present;
3187 /* a new battery was inserted or removed, so force a soc
3188 * recalculation to update the SoC */
3189 schedule_work(&chip->recalc_work);
3190 }
3191 mutex_unlock(&chip->vbat_monitor_mutex);
3192}
3193
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003194/* Returns capacity as a SoC percentage between 0 and 100 */
3195static int get_prop_bms_capacity(struct qpnp_bms_chip *chip)
3196{
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08003197 return report_state_of_charge(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003198}
3199
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003200static void qpnp_bms_external_power_changed(struct power_supply *psy)
3201{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003202 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
3203 bms_psy);
3204
3205 battery_insertion_check(chip);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003206 batfet_status_check(chip);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003207 battery_status_check(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003208}
3209
3210static int qpnp_bms_power_get_property(struct power_supply *psy,
3211 enum power_supply_property psp,
3212 union power_supply_propval *val)
3213{
3214 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
3215 bms_psy);
3216
3217 switch (psp) {
3218 case POWER_SUPPLY_PROP_CAPACITY:
3219 val->intval = get_prop_bms_capacity(chip);
3220 break;
Xiaozhe Shibda84992013-09-05 10:39:11 -07003221 case POWER_SUPPLY_PROP_STATUS:
3222 val->intval = chip->battery_status;
3223 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003224 case POWER_SUPPLY_PROP_CURRENT_NOW:
3225 val->intval = get_prop_bms_current_now(chip);
3226 break;
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07003227 case POWER_SUPPLY_PROP_RESISTANCE:
3228 val->intval = get_prop_bms_batt_resistance(chip);
3229 break;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07003230 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
3231 val->intval = get_prop_bms_charge_counter(chip);
3232 break;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003233 case POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW:
3234 val->intval = get_prop_bms_charge_counter_shadow(chip);
3235 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003236 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
3237 val->intval = get_prop_bms_charge_full_design(chip);
3238 break;
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05303239 case POWER_SUPPLY_PROP_CHARGE_FULL:
3240 val->intval = get_prop_bms_charge_full(chip);
3241 break;
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05303242 case POWER_SUPPLY_PROP_CYCLE_COUNT:
3243 val->intval = chip->charge_cycles;
3244 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003245 default:
3246 return -EINVAL;
3247 }
3248 return 0;
3249}
3250
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003251#define OCV_USE_LIMIT_EN BIT(7)
3252static int set_ocv_voltage_thresholds(struct qpnp_bms_chip *chip,
3253 int low_voltage_threshold,
3254 int high_voltage_threshold)
3255{
3256 uint16_t low_voltage_raw, high_voltage_raw;
3257 int rc;
3258
3259 low_voltage_raw = convert_vbatt_uv_to_raw(chip,
3260 low_voltage_threshold);
3261 high_voltage_raw = convert_vbatt_uv_to_raw(chip,
3262 high_voltage_threshold);
3263 rc = qpnp_write_wrapper(chip, (u8 *)&low_voltage_raw,
3264 chip->base + BMS1_OCV_USE_LOW_LIMIT_THR0, 2);
3265 if (rc) {
3266 pr_err("Failed to set ocv low voltage threshold: %d\n", rc);
3267 return rc;
3268 }
3269 rc = qpnp_write_wrapper(chip, (u8 *)&high_voltage_raw,
3270 chip->base + BMS1_OCV_USE_HIGH_LIMIT_THR0, 2);
3271 if (rc) {
3272 pr_err("Failed to set ocv high voltage threshold: %d\n", rc);
3273 return rc;
3274 }
3275 rc = qpnp_masked_write(chip, BMS1_OCV_USE_LIMIT_CTL,
3276 OCV_USE_LIMIT_EN, OCV_USE_LIMIT_EN);
3277 if (rc) {
3278 pr_err("Failed to enabled ocv voltage thresholds: %d\n", rc);
3279 return rc;
3280 }
3281 pr_debug("ocv low threshold set to %d uv or 0x%x raw\n",
3282 low_voltage_threshold, low_voltage_raw);
3283 pr_debug("ocv high threshold set to %d uv or 0x%x raw\n",
3284 high_voltage_threshold, high_voltage_raw);
3285 return 0;
3286}
3287
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003288static int read_shutdown_iavg_ma(struct qpnp_bms_chip *chip)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003289{
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003290 u8 iavg;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003291 int rc;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003292
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003293 rc = qpnp_read_wrapper(chip, &iavg, chip->base + IAVG_STORAGE_REG, 1);
3294 if (rc) {
3295 pr_err("failed to read addr = %d %d assuming %d\n",
3296 chip->base + IAVG_STORAGE_REG, rc,
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003297 MIN_IAVG_MA);
3298 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003299 } else if (iavg == IAVG_INVALID) {
3300 pr_err("invalid iavg read from BMS1_DATA_REG_1, using %d\n",
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003301 MIN_IAVG_MA);
3302 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003303 } else {
3304 if (iavg == 0)
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003305 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003306 else
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003307 return MIN_IAVG_MA + IAVG_STEP_SIZE_MA * iavg;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003308 }
3309}
3310
3311static int read_shutdown_soc(struct qpnp_bms_chip *chip)
3312{
3313 u8 stored_soc;
3314 int rc, shutdown_soc;
3315
3316 /*
3317 * The previous SOC is stored in the first 7 bits of the register as
3318 * (Shutdown SOC + 1). This allows for register reset values of both
3319 * 0x00 and 0x7F.
3320 */
3321 rc = qpnp_read_wrapper(chip, &stored_soc, chip->soc_storage_addr, 1);
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003322 if (rc) {
3323 pr_err("failed to read addr = %d %d\n",
3324 chip->soc_storage_addr, rc);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003325 return SOC_INVALID;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003326 }
3327
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003328 if ((stored_soc >> 1) > 0)
3329 shutdown_soc = (stored_soc >> 1) - 1;
3330 else
3331 shutdown_soc = SOC_INVALID;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003332
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003333 pr_debug("stored soc = 0x%02x, shutdown_soc = %d\n",
3334 stored_soc, shutdown_soc);
3335 return shutdown_soc;
3336}
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003337
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003338#define BAT_REMOVED_OFFMODE_BIT BIT(6)
3339static bool is_battery_replaced_in_offmode(struct qpnp_bms_chip *chip)
3340{
3341 u8 batt_pres;
3342 int rc;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003343
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003344 if (chip->batt_pres_addr) {
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003345 rc = qpnp_read_wrapper(chip, &batt_pres,
3346 chip->batt_pres_addr, 1);
3347 pr_debug("offmode removed: %02x\n", batt_pres);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003348 if (!rc && (batt_pres & BAT_REMOVED_OFFMODE_BIT))
3349 return true;
3350 }
3351 return false;
3352}
3353
3354static void load_shutdown_data(struct qpnp_bms_chip *chip)
3355{
3356 int calculated_soc, shutdown_soc;
3357 bool invalid_stored_soc;
3358 bool offmode_battery_replaced;
3359 bool shutdown_soc_out_of_limit;
3360
3361 /*
3362 * Read the saved shutdown SoC from the configured register and
3363 * check if the value has been reset
3364 */
3365 shutdown_soc = read_shutdown_soc(chip);
3366 invalid_stored_soc = (shutdown_soc == SOC_INVALID);
3367
3368 /*
3369 * Do a quick run of SoC calculation to find whether the shutdown soc
3370 * is close enough.
3371 */
3372 calculated_soc = recalculate_raw_soc(chip);
3373 shutdown_soc_out_of_limit = (abs(shutdown_soc - calculated_soc)
3374 > chip->shutdown_soc_valid_limit);
3375 pr_debug("calculated_soc = %d, valid_limit = %d\n",
3376 calculated_soc, chip->shutdown_soc_valid_limit);
3377
3378 /*
3379 * Check if the battery has been replaced while the system was powered
3380 * down.
3381 */
3382 offmode_battery_replaced = is_battery_replaced_in_offmode(chip);
3383
3384 /* Invalidate the shutdown SoC if any of these conditions hold true */
3385 if (chip->ignore_shutdown_soc
3386 || invalid_stored_soc
3387 || offmode_battery_replaced
3388 || shutdown_soc_out_of_limit) {
3389 chip->battery_removed = true;
3390 chip->shutdown_soc_invalid = true;
3391 chip->shutdown_iavg_ma = 0;
3392 pr_debug("Ignoring shutdown SoC: invalid = %d, offmode = %d, out_of_limit = %d\n",
3393 invalid_stored_soc, offmode_battery_replaced,
3394 shutdown_soc_out_of_limit);
3395 } else {
3396 chip->shutdown_iavg_ma = read_shutdown_iavg_ma(chip);
3397 chip->shutdown_soc = shutdown_soc;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07003398 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303399
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003400 pr_debug("raw_soc = %d shutdown_soc = %d shutdown_iavg = %d shutdown_soc_invalid = %d, battery_removed = %d\n",
3401 calculated_soc,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003402 chip->shutdown_soc,
3403 chip->shutdown_iavg_ma,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303404 chip->shutdown_soc_invalid,
3405 chip->battery_removed);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003406}
3407
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003408static irqreturn_t bms_ocv_thr_irq_handler(int irq, void *_chip)
3409{
3410 struct qpnp_bms_chip *chip = _chip;
3411
3412 pr_debug("ocv_thr irq triggered\n");
3413 bms_stay_awake(&chip->soc_wake_source);
3414 schedule_work(&chip->recalc_work);
3415 return IRQ_HANDLED;
3416}
3417
3418static irqreturn_t bms_sw_cc_thr_irq_handler(int irq, void *_chip)
3419{
3420 struct qpnp_bms_chip *chip = _chip;
3421
3422 pr_debug("sw_cc_thr irq triggered\n");
3423 bms_stay_awake(&chip->soc_wake_source);
3424 schedule_work(&chip->recalc_work);
3425 return IRQ_HANDLED;
3426}
3427
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003428static int64_t read_battery_id(struct qpnp_bms_chip *chip)
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003429{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003430 int rc;
3431 struct qpnp_vadc_result result;
3432
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003433 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX2_BAT_ID, &result);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003434 if (rc) {
3435 pr_err("error reading batt id channel = %d, rc = %d\n",
3436 LR_MUX2_BAT_ID, rc);
3437 return rc;
3438 }
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003439
3440 return result.physical;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003441}
3442
3443static int set_battery_data(struct qpnp_bms_chip *chip)
3444{
3445 int64_t battery_id;
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003446 int rc = 0, dt_data = false;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003447 struct bms_battery_data *batt_data;
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003448 struct device_node *node;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003449
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003450 if (chip->batt_type == BATT_DESAY) {
3451 batt_data = &desay_5200_data;
3452 } else if (chip->batt_type == BATT_PALLADIUM) {
3453 batt_data = &palladium_1500_data;
3454 } else if (chip->batt_type == BATT_OEM) {
3455 batt_data = &oem_batt_data;
Wu Fenglin2ac88aa2013-04-25 12:43:40 +08003456 } else if (chip->batt_type == BATT_QRD_4V35_2000MAH) {
3457 batt_data = &QRD_4v35_2000mAh_data;
tingtingf50326f2013-06-05 15:07:24 +08003458 } else if (chip->batt_type == BATT_QRD_4V2_1300MAH) {
3459 batt_data = &qrd_4v2_1300mah_data;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003460 } else {
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003461 battery_id = read_battery_id(chip);
3462 if (battery_id < 0) {
3463 pr_err("cannot read battery id err = %lld\n",
3464 battery_id);
3465 return battery_id;
3466 }
3467
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003468 node = of_find_node_by_name(chip->spmi->dev.of_node,
3469 "qcom,battery-data");
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003470 if (!node) {
3471 pr_warn("No available batterydata, using palladium 1500\n");
3472 batt_data = &palladium_1500_data;
3473 goto assign_data;
3474 }
3475 batt_data = devm_kzalloc(chip->dev,
3476 sizeof(struct bms_battery_data), GFP_KERNEL);
3477 if (!batt_data) {
3478 pr_err("Could not alloc battery data\n");
3479 batt_data = &palladium_1500_data;
3480 goto assign_data;
3481 }
3482 batt_data->fcc_temp_lut = devm_kzalloc(chip->dev,
3483 sizeof(struct single_row_lut),
3484 GFP_KERNEL);
3485 batt_data->pc_temp_ocv_lut = devm_kzalloc(chip->dev,
3486 sizeof(struct pc_temp_ocv_lut),
3487 GFP_KERNEL);
3488 batt_data->rbatt_sf_lut = devm_kzalloc(chip->dev,
3489 sizeof(struct sf_lut),
3490 GFP_KERNEL);
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003491
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003492 batt_data->max_voltage_uv = -1;
3493 batt_data->cutoff_uv = -1;
3494 batt_data->iterm_ua = -1;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003495
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003496 /*
3497 * if the alloced luts are 0s, of_batterydata_read_data ignores
3498 * them.
3499 */
3500 rc = of_batterydata_read_data(node, batt_data, battery_id);
3501 if (rc == 0 && batt_data->fcc_temp_lut
3502 && batt_data->pc_temp_ocv_lut
3503 && batt_data->rbatt_sf_lut) {
3504 dt_data = true;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003505 } else {
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003506 pr_err("battery data load failed, using palladium 1500\n");
3507 devm_kfree(chip->dev, batt_data->fcc_temp_lut);
3508 devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut);
3509 devm_kfree(chip->dev, batt_data->rbatt_sf_lut);
3510 devm_kfree(chip->dev, batt_data);
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003511 batt_data = &palladium_1500_data;
3512 }
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003513 }
3514
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003515assign_data:
Xiaozhe Shi976618f2013-04-30 10:49:30 -07003516 chip->fcc_mah = batt_data->fcc;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003517 chip->fcc_temp_lut = batt_data->fcc_temp_lut;
3518 chip->fcc_sf_lut = batt_data->fcc_sf_lut;
3519 chip->pc_temp_ocv_lut = batt_data->pc_temp_ocv_lut;
3520 chip->pc_sf_lut = batt_data->pc_sf_lut;
3521 chip->rbatt_sf_lut = batt_data->rbatt_sf_lut;
3522 chip->default_rbatt_mohm = batt_data->default_rbatt_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07003523 chip->rbatt_capacitive_mohm = batt_data->rbatt_capacitive_mohm;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07003524 chip->flat_ocv_threshold_uv = batt_data->flat_ocv_threshold_uv;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003525
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003526 /* Override battery properties if specified in the battery profile */
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003527 if (batt_data->max_voltage_uv >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003528 chip->max_voltage_uv = batt_data->max_voltage_uv;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003529 if (batt_data->cutoff_uv >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003530 chip->v_cutoff_uv = batt_data->cutoff_uv;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003531 if (batt_data->iterm_ua >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003532 chip->chg_term_ua = batt_data->iterm_ua;
3533
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003534 if (chip->pc_temp_ocv_lut == NULL) {
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003535 pr_err("temp ocv lut table has not been loaded\n");
3536 if (dt_data) {
3537 devm_kfree(chip->dev, batt_data->fcc_temp_lut);
3538 devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut);
3539 devm_kfree(chip->dev, batt_data->rbatt_sf_lut);
3540 devm_kfree(chip->dev, batt_data);
3541 }
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003542 return -EINVAL;
3543 }
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003544
3545 if (dt_data)
3546 devm_kfree(chip->dev, batt_data);
3547
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003548 return 0;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003549}
3550
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07003551static int bms_get_adc(struct qpnp_bms_chip *chip,
3552 struct spmi_device *spmi)
3553{
3554 int rc = 0;
3555
3556 chip->vadc_dev = qpnp_get_vadc(&spmi->dev, "bms");
3557 if (IS_ERR(chip->vadc_dev)) {
3558 rc = PTR_ERR(chip->vadc_dev);
3559 if (rc != -EPROBE_DEFER)
3560 pr_err("vadc property missing, rc=%d\n", rc);
3561 return rc;
3562 }
3563
3564 chip->iadc_dev = qpnp_get_iadc(&spmi->dev, "bms");
3565 if (IS_ERR(chip->iadc_dev)) {
3566 rc = PTR_ERR(chip->iadc_dev);
3567 if (rc != -EPROBE_DEFER)
3568 pr_err("iadc property missing, rc=%d\n", rc);
3569 return rc;
3570 }
3571
3572 chip->adc_tm_dev = qpnp_get_adc_tm(&spmi->dev, "bms");
3573 if (IS_ERR(chip->adc_tm_dev)) {
3574 rc = PTR_ERR(chip->adc_tm_dev);
3575 if (rc != -EPROBE_DEFER)
3576 pr_err("adc-tm not ready, defer probe\n");
3577 return rc;
3578 }
3579
3580 return 0;
3581}
3582
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003583#define SPMI_PROP_READ(chip_prop, qpnp_spmi_property, retval) \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003584do { \
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003585 if (retval) \
3586 break; \
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003587 retval = of_property_read_u32(chip->spmi->dev.of_node, \
Xiaozhe Shi9bd24622013-01-23 15:54:54 -08003588 "qcom," qpnp_spmi_property, \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003589 &chip->chip_prop); \
3590 if (retval) { \
3591 pr_err("Error reading " #qpnp_spmi_property \
3592 " property %d\n", rc); \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003593 } \
3594} while (0)
3595
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303596#define SPMI_PROP_READ_BOOL(chip_prop, qpnp_spmi_property) \
3597do { \
3598 chip->chip_prop = of_property_read_bool(chip->spmi->dev.of_node,\
3599 "qcom," qpnp_spmi_property); \
3600} while (0)
3601
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003602static inline int bms_read_properties(struct qpnp_bms_chip *chip)
3603{
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003604 int rc = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003605
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003606 SPMI_PROP_READ(r_sense_uohm, "r-sense-uohm", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003607 SPMI_PROP_READ(v_cutoff_uv, "v-cutoff-uv", rc);
3608 SPMI_PROP_READ(max_voltage_uv, "max-voltage-uv", rc);
3609 SPMI_PROP_READ(r_conn_mohm, "r-conn-mohm", rc);
3610 SPMI_PROP_READ(chg_term_ua, "chg-term-ua", rc);
3611 SPMI_PROP_READ(shutdown_soc_valid_limit,
3612 "shutdown-soc-valid-limit", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003613 SPMI_PROP_READ(adjust_soc_low_threshold,
3614 "adjust-soc-low-threshold", rc);
3615 SPMI_PROP_READ(batt_type, "batt-type", rc);
3616 SPMI_PROP_READ(low_soc_calc_threshold,
3617 "low-soc-calculate-soc-threshold", rc);
3618 SPMI_PROP_READ(low_soc_calculate_soc_ms,
3619 "low-soc-calculate-soc-ms", rc);
3620 SPMI_PROP_READ(calculate_soc_ms, "calculate-soc-ms", rc);
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07003621 SPMI_PROP_READ(high_ocv_correction_limit_uv,
3622 "high-ocv-correction-limit-uv", rc);
3623 SPMI_PROP_READ(low_ocv_correction_limit_uv,
3624 "low-ocv-correction-limit-uv", rc);
3625 SPMI_PROP_READ(hold_soc_est,
3626 "hold-soc-est", rc);
3627 SPMI_PROP_READ(ocv_high_threshold_uv,
3628 "ocv-voltage-high-threshold-uv", rc);
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003629 SPMI_PROP_READ(ocv_low_threshold_uv,
3630 "ocv-voltage-low-threshold-uv", rc);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08003631 SPMI_PROP_READ(low_voltage_threshold, "low-voltage-threshold", rc);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003632 SPMI_PROP_READ(temperature_margin, "tm-temp-margin", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003633
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003634 chip->use_external_rsense = of_property_read_bool(
3635 chip->spmi->dev.of_node,
3636 "qcom,use-external-rsense");
3637 chip->ignore_shutdown_soc = of_property_read_bool(
3638 chip->spmi->dev.of_node,
3639 "qcom,ignore-shutdown-soc");
3640 chip->use_voltage_soc = of_property_read_bool(chip->spmi->dev.of_node,
3641 "qcom,use-voltage-soc");
3642 chip->use_ocv_thresholds = of_property_read_bool(
3643 chip->spmi->dev.of_node,
3644 "qcom,use-ocv-thresholds");
3645
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003646 if (chip->adjust_soc_low_threshold >= 45)
3647 chip->adjust_soc_low_threshold = 45;
3648
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303649 SPMI_PROP_READ_BOOL(enable_fcc_learning, "enable-fcc-learning");
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303650 if (chip->enable_fcc_learning) {
3651 SPMI_PROP_READ(min_fcc_learning_soc,
3652 "min-fcc-learning-soc", rc);
3653 SPMI_PROP_READ(min_fcc_ocv_pc,
3654 "min-fcc-ocv-pc", rc);
3655 SPMI_PROP_READ(min_fcc_learning_samples,
3656 "min-fcc-learning-samples", rc);
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303657 SPMI_PROP_READ(fcc_resolution,
3658 "fcc-resolution", rc);
3659 if (chip->min_fcc_learning_samples > MAX_FCC_CYCLES)
3660 chip->min_fcc_learning_samples = MAX_FCC_CYCLES;
3661 chip->fcc_learning_samples = devm_kzalloc(&chip->spmi->dev,
3662 (sizeof(struct fcc_sample) *
3663 chip->min_fcc_learning_samples), GFP_KERNEL);
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003664 if (chip->fcc_learning_samples == NULL)
3665 return -ENOMEM;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303666 pr_debug("min-fcc-soc=%d, min-fcc-pc=%d, min-fcc-cycles=%d\n",
3667 chip->min_fcc_learning_soc, chip->min_fcc_ocv_pc,
3668 chip->min_fcc_learning_samples);
3669 }
3670
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003671 if (rc) {
3672 pr_err("Missing required properties.\n");
3673 return rc;
3674 }
3675
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003676 pr_debug("dts data: r_sense_uohm:%d, v_cutoff_uv:%d, max_v:%d\n",
3677 chip->r_sense_uohm, chip->v_cutoff_uv,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003678 chip->max_voltage_uv);
3679 pr_debug("r_conn:%d, shutdown_soc: %d, adjust_soc_low:%d\n",
3680 chip->r_conn_mohm, chip->shutdown_soc_valid_limit,
3681 chip->adjust_soc_low_threshold);
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07003682 pr_debug("chg_term_ua:%d, batt_type:%d\n",
3683 chip->chg_term_ua,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003684 chip->batt_type);
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08003685 pr_debug("ignore_shutdown_soc:%d, use_voltage_soc:%d\n",
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08003686 chip->ignore_shutdown_soc, chip->use_voltage_soc);
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003687 pr_debug("use external rsense: %d\n", chip->use_external_rsense);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003688 return 0;
3689}
3690
3691static inline void bms_initialize_constants(struct qpnp_bms_chip *chip)
3692{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003693 chip->prev_pc_unusable = -EINVAL;
3694 chip->soc_at_cv = -EINVAL;
3695 chip->calculated_soc = -EINVAL;
Xiaozhe Shie118c692012-09-24 15:17:43 -07003696 chip->last_soc = -EINVAL;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07003697 chip->last_soc_est = -EINVAL;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003698 chip->battery_present = -EINVAL;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003699 chip->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08003700 chip->last_cc_uah = INT_MIN;
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08003701 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
3702 chip->prev_last_good_ocv_raw = OCV_RAW_UNINITIALIZED;
Xiaozhe Shie118c692012-09-24 15:17:43 -07003703 chip->first_time_calc_soc = 1;
3704 chip->first_time_calc_uuc = 1;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003705}
3706
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003707#define SPMI_FIND_IRQ(chip, irq_name) \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003708do { \
3709 chip->irq_name##_irq.irq = spmi_get_irq_byname(chip->spmi, \
3710 resource, #irq_name); \
3711 if (chip->irq_name##_irq.irq < 0) { \
3712 pr_err("Unable to get " #irq_name " irq\n"); \
3713 return -ENXIO; \
3714 } \
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003715} while (0)
3716
3717static int bms_find_irqs(struct qpnp_bms_chip *chip,
3718 struct spmi_resource *resource)
3719{
3720 SPMI_FIND_IRQ(chip, sw_cc_thr);
3721 SPMI_FIND_IRQ(chip, ocv_thr);
3722 return 0;
3723}
3724
3725#define SPMI_REQUEST_IRQ(chip, rc, irq_name) \
3726do { \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003727 rc = devm_request_irq(chip->dev, chip->irq_name##_irq.irq, \
3728 bms_##irq_name##_irq_handler, \
3729 IRQF_TRIGGER_RISING, #irq_name, chip); \
3730 if (rc < 0) { \
3731 pr_err("Unable to request " #irq_name " irq: %d\n", rc);\
3732 return -ENXIO; \
3733 } \
3734} while (0)
3735
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003736static int bms_request_irqs(struct qpnp_bms_chip *chip)
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003737{
3738 int rc;
3739
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003740 SPMI_REQUEST_IRQ(chip, rc, sw_cc_thr);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003741 enable_irq_wake(chip->sw_cc_thr_irq.irq);
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003742 SPMI_REQUEST_IRQ(chip, rc, ocv_thr);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003743 enable_irq_wake(chip->ocv_thr_irq.irq);
3744 return 0;
3745}
3746
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003747#define REG_OFFSET_PERP_TYPE 0x04
3748#define REG_OFFSET_PERP_SUBTYPE 0x05
3749#define BMS_BMS_TYPE 0xD
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003750#define BMS_BMS1_SUBTYPE 0x1
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003751#define BMS_IADC_TYPE 0x8
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003752#define BMS_IADC1_SUBTYPE 0x3
3753#define BMS_IADC2_SUBTYPE 0x5
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003754
3755static int register_spmi(struct qpnp_bms_chip *chip, struct spmi_device *spmi)
3756{
3757 struct spmi_resource *spmi_resource;
3758 struct resource *resource;
3759 int rc;
3760 u8 type, subtype;
3761
3762 chip->dev = &(spmi->dev);
3763 chip->spmi = spmi;
3764
3765 spmi_for_each_container_dev(spmi_resource, spmi) {
3766 if (!spmi_resource) {
3767 pr_err("qpnp_bms: spmi resource absent\n");
3768 return -ENXIO;
3769 }
3770
3771 resource = spmi_get_resource(spmi, spmi_resource,
3772 IORESOURCE_MEM, 0);
3773 if (!(resource && resource->start)) {
3774 pr_err("node %s IO resource absent!\n",
3775 spmi->dev.of_node->full_name);
3776 return -ENXIO;
3777 }
3778
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07003779 pr_debug("Node name = %s\n", spmi_resource->of_node->name);
3780
3781 if (strcmp("qcom,batt-pres-status",
3782 spmi_resource->of_node->name) == 0) {
3783 chip->batt_pres_addr = resource->start;
3784 continue;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003785 } else if (strcmp("qcom,soc-storage-reg",
3786 spmi_resource->of_node->name) == 0) {
3787 chip->soc_storage_addr = resource->start;
3788 continue;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07003789 }
3790
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003791 rc = qpnp_read_wrapper(chip, &type,
3792 resource->start + REG_OFFSET_PERP_TYPE, 1);
3793 if (rc) {
3794 pr_err("Peripheral type read failed rc=%d\n", rc);
3795 return rc;
3796 }
3797 rc = qpnp_read_wrapper(chip, &subtype,
3798 resource->start + REG_OFFSET_PERP_SUBTYPE, 1);
3799 if (rc) {
3800 pr_err("Peripheral subtype read failed rc=%d\n", rc);
3801 return rc;
3802 }
3803
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003804 if (type == BMS_BMS_TYPE && subtype == BMS_BMS1_SUBTYPE) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003805 chip->base = resource->start;
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003806 rc = bms_find_irqs(chip, spmi_resource);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003807 if (rc) {
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003808 pr_err("Could not find irqs\n");
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003809 return rc;
3810 }
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003811 } else if (type == BMS_IADC_TYPE
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003812 && (subtype == BMS_IADC1_SUBTYPE
3813 || subtype == BMS_IADC2_SUBTYPE)) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003814 chip->iadc_base = resource->start;
3815 } else {
3816 pr_err("Invalid peripheral start=0x%x type=0x%x, subtype=0x%x\n",
3817 resource->start, type, subtype);
3818 }
3819 }
3820
3821 if (chip->base == 0) {
3822 dev_err(&spmi->dev, "BMS peripheral was not registered\n");
3823 return -EINVAL;
3824 }
3825 if (chip->iadc_base == 0) {
3826 dev_err(&spmi->dev, "BMS_IADC peripheral was not registered\n");
3827 return -EINVAL;
3828 }
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003829 if (chip->soc_storage_addr == 0) {
3830 /* default to dvdd backed BMS data reg0 */
3831 chip->soc_storage_addr = chip->base + SOC_STORAGE_REG;
3832 }
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003833
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003834 pr_debug("bms-base = 0x%04x, iadc-base = 0x%04x, bat-pres-reg = 0x%04x, soc-storage-reg = 0x%04x\n",
3835 chip->base, chip->iadc_base,
3836 chip->batt_pres_addr, chip->soc_storage_addr);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003837 return 0;
3838}
3839
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003840#define ADC_CH_SEL_MASK 0x7
3841#define ADC_INT_RSNSN_CTL_MASK 0x3
3842#define ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE 0x2
3843#define FAST_AVG_EN_MASK 0x80
3844#define FAST_AVG_EN_VALUE_EXT_RSENSE 0x80
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003845static int read_iadc_channel_select(struct qpnp_bms_chip *chip)
3846{
3847 u8 iadc_channel_select;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08003848 int32_t rds_rsense_nohm;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003849 int rc;
3850
3851 rc = qpnp_read_wrapper(chip, &iadc_channel_select,
3852 chip->iadc_base + IADC1_BMS_ADC_CH_SEL_CTL, 1);
3853 if (rc) {
3854 pr_err("Error reading bms_iadc channel register %d\n", rc);
3855 return rc;
3856 }
3857
3858 iadc_channel_select &= ADC_CH_SEL_MASK;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08003859 if (iadc_channel_select != EXTERNAL_RSENSE
3860 && iadc_channel_select != INTERNAL_RSENSE) {
3861 pr_err("IADC1_BMS_IADC configured incorrectly. Selected channel = %d\n",
3862 iadc_channel_select);
3863 return -EINVAL;
3864 }
3865
3866 if (chip->use_external_rsense) {
3867 pr_debug("External rsense selected\n");
3868 if (iadc_channel_select == INTERNAL_RSENSE) {
3869 pr_debug("Internal rsense detected; Changing rsense to external\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003870 rc = qpnp_masked_write_iadc(chip,
3871 IADC1_BMS_ADC_CH_SEL_CTL,
3872 ADC_CH_SEL_MASK,
3873 EXTERNAL_RSENSE);
3874 if (rc) {
3875 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
3876 IADC1_BMS_ADC_CH_SEL_CTL,
3877 EXTERNAL_RSENSE, rc);
3878 return rc;
3879 }
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003880 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07003881 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003882 chip->software_shdw_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003883 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08003884 } else {
3885 pr_debug("Internal rsense selected\n");
3886 if (iadc_channel_select == EXTERNAL_RSENSE) {
3887 pr_debug("External rsense detected; Changing rsense to internal\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003888 rc = qpnp_masked_write_iadc(chip,
3889 IADC1_BMS_ADC_CH_SEL_CTL,
3890 ADC_CH_SEL_MASK,
3891 INTERNAL_RSENSE);
3892 if (rc) {
3893 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
3894 IADC1_BMS_ADC_CH_SEL_CTL,
3895 INTERNAL_RSENSE, rc);
3896 return rc;
3897 }
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003898 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
3899 chip->software_shdw_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003900 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08003901
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003902 rc = qpnp_iadc_get_rsense(chip->iadc_dev, &rds_rsense_nohm);
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08003903 if (rc) {
3904 pr_err("Unable to read RDS resistance value from IADC; rc = %d\n",
3905 rc);
3906 return rc;
3907 }
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003908 chip->r_sense_uohm = rds_rsense_nohm/1000;
3909 pr_debug("rds_rsense = %d nOhm, saved as %d uOhm\n",
3910 rds_rsense_nohm, chip->r_sense_uohm);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003911 }
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003912 /* prevent shorting of leads by IADC_BMS when external Rsense is used */
3913 if (chip->use_external_rsense) {
3914 if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX) {
3915 rc = qpnp_masked_write_iadc(chip,
3916 IADC1_BMS_ADC_INT_RSNSN_CTL,
3917 ADC_INT_RSNSN_CTL_MASK,
3918 ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE);
3919 if (rc) {
3920 pr_err("Unable to set batfet config %x to %x: %d\n",
3921 IADC1_BMS_ADC_INT_RSNSN_CTL,
3922 ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE, rc);
3923 return rc;
3924 }
3925 } else {
3926 /* In older PMICS use FAST_AVG_EN register bit 7 */
3927 rc = qpnp_masked_write_iadc(chip,
3928 IADC1_BMS_FAST_AVG_EN,
3929 FAST_AVG_EN_MASK,
3930 FAST_AVG_EN_VALUE_EXT_RSENSE);
3931 if (rc) {
3932 pr_err("Unable to set batfet config %x to %x: %d\n",
3933 IADC1_BMS_FAST_AVG_EN,
3934 FAST_AVG_EN_VALUE_EXT_RSENSE, rc);
3935 return rc;
3936 }
3937 }
3938 }
3939
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003940 return 0;
3941}
3942
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003943static int refresh_die_temp_monitor(struct qpnp_bms_chip *chip)
3944{
3945 struct qpnp_vadc_result result;
3946 int rc;
3947
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003948 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003949
3950 pr_debug("low = %lld, high = %lld\n",
3951 result.physical - chip->temperature_margin,
3952 result.physical + chip->temperature_margin);
3953 chip->die_temp_monitor_params.high_temp = result.physical
3954 + chip->temperature_margin;
3955 chip->die_temp_monitor_params.low_temp = result.physical
3956 - chip->temperature_margin;
3957 chip->die_temp_monitor_params.state_request =
3958 ADC_TM_HIGH_LOW_THR_ENABLE;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07003959 return qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
3960 &chip->die_temp_monitor_params);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003961}
3962
3963static void btm_notify_die_temp(enum qpnp_tm_state state, void *ctx)
3964{
3965 struct qpnp_bms_chip *chip = ctx;
3966 struct qpnp_vadc_result result;
3967 int rc;
3968
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003969 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003970
3971 if (state == ADC_TM_LOW_STATE)
3972 pr_debug("low state triggered\n");
3973 else if (state == ADC_TM_HIGH_STATE)
3974 pr_debug("high state triggered\n");
3975 pr_debug("die temp = %lld, raw = 0x%x\n",
3976 result.physical, result.adc_code);
3977 schedule_work(&chip->recalc_work);
3978 refresh_die_temp_monitor(chip);
3979}
3980
3981static int setup_die_temp_monitoring(struct qpnp_bms_chip *chip)
3982{
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07003983 int rc;
3984
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003985 chip->die_temp_monitor_params.channel = DIE_TEMP;
3986 chip->die_temp_monitor_params.btm_ctx = (void *)chip;
3987 chip->die_temp_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
3988 chip->die_temp_monitor_params.threshold_notification =
3989 &btm_notify_die_temp;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07003990 rc = refresh_die_temp_monitor(chip);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003991 if (rc) {
3992 pr_err("tm setup failed: %d\n", rc);
3993 return rc;
3994 }
3995 pr_debug("setup complete\n");
3996 return 0;
3997}
3998
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003999static int __devinit qpnp_bms_probe(struct spmi_device *spmi)
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004000{
4001 struct qpnp_bms_chip *chip;
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004002 bool warm_reset;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004003 int rc, vbatt;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004004
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07004005 chip = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_bms_chip),
4006 GFP_KERNEL);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004007
4008 if (chip == NULL) {
4009 pr_err("kzalloc() failed.\n");
4010 return -ENOMEM;
4011 }
4012
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004013 rc = bms_get_adc(chip, spmi);
4014 if (rc < 0)
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004015 goto error_read;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004016
Xiaozhe Shi3eaf0b62013-07-11 09:48:08 -07004017 mutex_init(&chip->bms_output_lock);
4018 mutex_init(&chip->last_ocv_uv_mutex);
4019 mutex_init(&chip->vbat_monitor_mutex);
4020 mutex_init(&chip->soc_invalidation_mutex);
4021 mutex_init(&chip->last_soc_mutex);
Xiaozhe Shibda84992013-09-05 10:39:11 -07004022 mutex_init(&chip->status_lock);
Xiaozhe Shi27375822013-08-22 11:40:15 -07004023 init_waitqueue_head(&chip->bms_wait_queue);
Xiaozhe Shi3eaf0b62013-07-11 09:48:08 -07004024
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004025 warm_reset = qpnp_pon_is_warm_reset();
4026 rc = warm_reset;
4027 if (rc < 0)
4028 goto error_read;
4029
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004030 rc = register_spmi(chip, spmi);
4031 if (rc) {
4032 pr_err("error registering spmi resource %d\n", rc);
4033 goto error_resource;
4034 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004035
4036 rc = qpnp_read_wrapper(chip, &chip->revision1,
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004037 chip->base + REVISION1, 1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004038 if (rc) {
4039 pr_err("error reading version register %d\n", rc);
4040 goto error_read;
4041 }
4042
4043 rc = qpnp_read_wrapper(chip, &chip->revision2,
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004044 chip->base + REVISION2, 1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004045 if (rc) {
4046 pr_err("Error reading version register %d\n", rc);
4047 goto error_read;
4048 }
Xiaozhe Shia045a562012-11-28 16:55:39 -08004049 pr_debug("BMS version: %hhu.%hhu\n", chip->revision2, chip->revision1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004050
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004051 rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision2,
4052 chip->iadc_base + REVISION2, 1);
4053 if (rc) {
4054 pr_err("Error reading version register %d\n", rc);
4055 goto error_read;
4056 }
4057
4058 rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision1,
4059 chip->iadc_base + REVISION1, 1);
4060 if (rc) {
4061 pr_err("Error reading version register %d\n", rc);
4062 goto error_read;
4063 }
4064 pr_debug("IADC_BMS version: %hhu.%hhu\n",
4065 chip->iadc_bms_revision2, chip->iadc_bms_revision1);
4066
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004067 rc = bms_read_properties(chip);
4068 if (rc) {
4069 pr_err("Unable to read all bms properties, rc = %d\n", rc);
4070 goto error_read;
4071 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004072
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004073 rc = read_iadc_channel_select(chip);
4074 if (rc) {
4075 pr_err("Unable to get iadc selected channel = %d\n", rc);
4076 goto error_read;
4077 }
4078
Xiaozhe Shibdf14742012-12-05 12:41:48 -08004079 if (chip->use_ocv_thresholds) {
4080 rc = set_ocv_voltage_thresholds(chip,
4081 chip->ocv_low_threshold_uv,
4082 chip->ocv_high_threshold_uv);
4083 if (rc) {
4084 pr_err("Could not set ocv voltage thresholds: %d\n",
4085 rc);
4086 goto error_read;
4087 }
4088 }
4089
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004090 rc = set_battery_data(chip);
4091 if (rc) {
4092 pr_err("Bad battery data %d\n", rc);
4093 goto error_read;
4094 }
4095
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004096 bms_initialize_constants(chip);
4097
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004098 wakeup_source_init(&chip->soc_wake_source.source, "qpnp_soc_wake");
Xiaozhe Shi4be85782013-02-22 17:33:40 -08004099 wake_lock_init(&chip->low_voltage_wake_lock, WAKE_LOCK_SUSPEND,
4100 "qpnp_low_voltage_lock");
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004101 wake_lock_init(&chip->cv_wake_lock, WAKE_LOCK_SUSPEND,
4102 "qpnp_cv_lock");
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004103 INIT_DELAYED_WORK(&chip->calculate_soc_delayed_work,
4104 calculate_soc_work);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08004105 INIT_WORK(&chip->recalc_work, recalculate_work);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07004106 INIT_WORK(&chip->batfet_open_work, batfet_open_work);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004107
Xiaozhe Shif9f99242013-08-29 12:27:50 -07004108 dev_set_drvdata(&spmi->dev, chip);
4109 device_init_wakeup(&spmi->dev, 1);
4110
4111 load_shutdown_data(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004112
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05304113 if (chip->enable_fcc_learning) {
Anirudh Ghayale0c02932013-07-08 16:26:35 +05304114 if (chip->battery_removed) {
4115 rc = discard_backup_fcc_data(chip);
4116 if (rc)
4117 pr_err("Could not discard backed-up FCC data\n");
4118 } else {
4119 rc = read_chgcycle_data_from_backup(chip);
4120 if (rc)
4121 pr_err("Unable to restore charge-cycle data\n");
4122
4123 rc = read_fcc_data_from_backup(chip);
4124 if (rc)
4125 pr_err("Unable to restore FCC-learning data\n");
4126 else
4127 attempt_learning_new_fcc(chip);
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05304128 }
4129 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05304130
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004131 rc = setup_vbat_monitoring(chip);
4132 if (rc < 0) {
4133 pr_err("failed to set up voltage notifications: %d\n", rc);
4134 goto error_setup;
Xiaozhe Shid5d21412013-02-06 17:14:41 -08004135 }
4136
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004137 rc = setup_die_temp_monitoring(chip);
4138 if (rc < 0) {
4139 pr_err("failed to set up die temp notifications: %d\n", rc);
4140 goto error_setup;
4141 }
4142
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07004143 battery_insertion_check(chip);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07004144 batfet_status_check(chip);
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07004145 battery_status_check(chip);
4146
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004147 calculate_soc_work(&(chip->calculate_soc_delayed_work.work));
4148
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004149 /* setup & register the battery power supply */
4150 chip->bms_psy.name = "bms";
4151 chip->bms_psy.type = POWER_SUPPLY_TYPE_BMS;
4152 chip->bms_psy.properties = msm_bms_power_props;
4153 chip->bms_psy.num_properties = ARRAY_SIZE(msm_bms_power_props);
4154 chip->bms_psy.get_property = qpnp_bms_power_get_property;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004155 chip->bms_psy.external_power_changed =
4156 qpnp_bms_external_power_changed;
4157 chip->bms_psy.supplied_to = qpnp_bms_supplicants;
4158 chip->bms_psy.num_supplicants = ARRAY_SIZE(qpnp_bms_supplicants);
4159
4160 rc = power_supply_register(chip->dev, &chip->bms_psy);
4161
4162 if (rc < 0) {
4163 pr_err("power_supply_register bms failed rc = %d\n", rc);
4164 goto unregister_dc;
4165 }
4166
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07004167 chip->bms_psy_registered = true;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004168 vbatt = 0;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07004169 rc = get_battery_voltage(chip, &vbatt);
Xiaozhe Shi36458962013-02-06 16:19:57 -08004170 if (rc) {
4171 pr_err("error reading vbat_sns adc channel = %d, rc = %d\n",
4172 VBAT_SNS, rc);
4173 goto unregister_dc;
4174 }
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004175
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07004176 rc = bms_request_irqs(chip);
4177 if (rc) {
4178 pr_err("error requesting bms irqs, rc = %d\n", rc);
4179 goto unregister_dc;
4180 }
4181
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004182 pr_info("probe success: soc =%d vbatt = %d ocv = %d r_sense_uohm = %u warm_reset = %d\n",
4183 get_prop_bms_capacity(chip), vbatt, chip->last_ocv_uv,
4184 chip->r_sense_uohm, warm_reset);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004185 return 0;
4186
4187unregister_dc:
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07004188 chip->bms_psy_registered = false;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004189 power_supply_unregister(&chip->bms_psy);
4190error_setup:
4191 dev_set_drvdata(&spmi->dev, NULL);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004192 wakeup_source_trash(&chip->soc_wake_source.source);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08004193 wake_lock_destroy(&chip->low_voltage_wake_lock);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004194 wake_lock_destroy(&chip->cv_wake_lock);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004195error_resource:
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004196error_read:
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004197 return rc;
4198}
4199
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07004200static int qpnp_bms_remove(struct spmi_device *spmi)
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004201{
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004202 dev_set_drvdata(&spmi->dev, NULL);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004203 return 0;
4204}
4205
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004206static int bms_suspend(struct device *dev)
4207{
4208 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
4209
4210 cancel_delayed_work_sync(&chip->calculate_soc_delayed_work);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07004211 chip->was_charging_at_sleep = is_battery_charging(chip);
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004212 return 0;
4213}
4214
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004215static int bms_resume(struct device *dev)
4216{
4217 int rc;
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004218 int soc_calc_period;
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004219 int time_until_next_recalc = 0;
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004220 unsigned long time_since_last_recalc;
4221 unsigned long tm_now_sec;
4222 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
4223
4224 rc = get_current_time(&tm_now_sec);
4225 if (rc) {
4226 pr_err("Could not read current time: %d\n", rc);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004227 } else {
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004228 if (chip->calculated_soc < chip->low_soc_calc_threshold)
4229 soc_calc_period = chip->low_soc_calculate_soc_ms;
4230 else
4231 soc_calc_period = chip->calculate_soc_ms;
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004232 time_since_last_recalc = tm_now_sec - chip->last_recalc_time;
4233 pr_debug("Time since last recalc: %lu\n",
4234 time_since_last_recalc);
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004235 time_until_next_recalc = max(0, soc_calc_period
4236 - (int)(time_since_last_recalc * 1000));
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004237 }
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004238
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004239 if (time_until_next_recalc == 0)
4240 bms_stay_awake(&chip->soc_wake_source);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004241 schedule_delayed_work(&chip->calculate_soc_delayed_work,
4242 round_jiffies_relative(msecs_to_jiffies
4243 (time_until_next_recalc)));
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004244 return 0;
4245}
4246
4247static const struct dev_pm_ops qpnp_bms_pm_ops = {
4248 .resume = bms_resume,
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004249 .suspend = bms_suspend,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004250};
4251
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004252static struct spmi_driver qpnp_bms_driver = {
4253 .probe = qpnp_bms_probe,
4254 .remove = __devexit_p(qpnp_bms_remove),
4255 .driver = {
4256 .name = QPNP_BMS_DEV_NAME,
4257 .owner = THIS_MODULE,
4258 .of_match_table = qpnp_bms_match_table,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004259 .pm = &qpnp_bms_pm_ops,
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004260 },
4261};
4262
4263static int __init qpnp_bms_init(void)
4264{
4265 pr_info("QPNP BMS INIT\n");
4266 return spmi_driver_register(&qpnp_bms_driver);
4267}
4268
4269static void __exit qpnp_bms_exit(void)
4270{
4271 pr_info("QPNP BMS EXIT\n");
4272 return spmi_driver_unregister(&qpnp_bms_driver);
4273}
4274
4275module_init(qpnp_bms_init);
4276module_exit(qpnp_bms_exit);
4277
4278MODULE_DESCRIPTION("QPNP BMS Driver");
4279MODULE_LICENSE("GPL v2");
4280MODULE_ALIAS("platform:" QPNP_BMS_DEV_NAME);