blob: 2a8e12770d43c0215294e8acc6b20deae0037ded [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;
Xiaozhe Shicb487b12013-10-14 17:42:07 -0700206 int low_voltage_calculate_soc_ms;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700207 int calculate_soc_ms;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800208 struct bms_wakeup_source soc_wake_source;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700209 struct wake_lock cv_wake_lock;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700210
Xiaozhe Shie118c692012-09-24 15:17:43 -0700211 uint16_t ocv_reading_at_100;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700212 uint16_t prev_last_good_ocv_raw;
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700213 int insertion_ocv_uv;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700214 int last_ocv_uv;
Xiaozhe Shicc48e992013-05-28 16:42:24 -0700215 int charging_adjusted_ocv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800216 int last_ocv_temp;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700217 int last_cc_uah;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700218 unsigned long last_soc_change_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700219 unsigned long tm_sec;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700220 unsigned long report_tm_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700221 bool first_time_calc_soc;
222 bool first_time_calc_uuc;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700223 int64_t software_cc_uah;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700224 int64_t software_shdw_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700225
226 int iavg_samples_ma[IAVG_SAMPLES];
227 int iavg_index;
228 int iavg_num_samples;
229 struct timespec t_soc_queried;
230 int last_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -0700231 int last_soc_est;
Xiaozhe Shicc137262013-03-10 06:21:41 -0700232 int last_soc_unbound;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700233 bool was_charging_at_sleep;
234 int charge_start_tm_sec;
235 int catch_up_time_sec;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700236 struct single_row_lut *adjusted_fcc_temp_lut;
237
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700238 struct qpnp_adc_tm_btm_param vbat_monitor_params;
Xiaozhe Shi535494d2013-04-05 12:27:51 -0700239 struct qpnp_adc_tm_btm_param die_temp_monitor_params;
240 int temperature_margin;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700241 unsigned int vadc_v0625;
242 unsigned int vadc_v1250;
243
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -0700244 int system_load_count;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700245 int prev_uuc_iavg_ma;
246 int prev_pc_unusable;
247 int ibat_at_cv_ua;
248 int soc_at_cv;
249 int prev_chg_soc;
250 int calculated_soc;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -0800251 int prev_voltage_based_soc;
252 bool use_voltage_soc;
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700253 bool in_cv_range;
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800254
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800255 int prev_batt_terminal_uv;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -0700256 int high_ocv_correction_limit_uv;
257 int low_ocv_correction_limit_uv;
258 int flat_ocv_threshold_uv;
259 int hold_soc_est;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800260
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800261 int ocv_high_threshold_uv;
262 int ocv_low_threshold_uv;
Xiaozhe Shicdeee312012-12-18 15:10:18 -0800263 unsigned long last_recalc_time;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530264
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530265 struct fcc_sample *fcc_learning_samples;
266 u8 fcc_sample_count;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530267 int enable_fcc_learning;
268 int min_fcc_learning_soc;
269 int min_fcc_ocv_pc;
270 int min_fcc_learning_samples;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +0530271 int start_soc;
272 int end_soc;
273 int start_pc;
274 int start_cc_uah;
275 int start_real_soc;
276 int end_cc_uah;
277 uint16_t fcc_new_mah;
278 int fcc_new_batt_temp;
279 uint16_t charge_cycles;
280 u8 charge_increase;
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530281 int fcc_resolution;
282 bool battery_removed;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800283 struct bms_irq sw_cc_thr_irq;
284 struct bms_irq ocv_thr_irq;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700285 struct qpnp_vadc_chip *vadc_dev;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700286 struct qpnp_iadc_chip *iadc_dev;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -0700287 struct qpnp_adc_tm_chip *adc_tm_dev;
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700288};
289
290static struct of_device_id qpnp_bms_match_table[] = {
291 { .compatible = QPNP_BMS_DEV_NAME },
292 {}
293};
294
295static char *qpnp_bms_supplicants[] = {
296 "battery"
297};
298
299static enum power_supply_property msm_bms_power_props[] = {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700300 POWER_SUPPLY_PROP_CAPACITY,
Xiaozhe Shibda84992013-09-05 10:39:11 -0700301 POWER_SUPPLY_PROP_STATUS,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700302 POWER_SUPPLY_PROP_CURRENT_NOW,
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -0700303 POWER_SUPPLY_PROP_RESISTANCE,
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -0700304 POWER_SUPPLY_PROP_CHARGE_COUNTER,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700305 POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700306 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +0530307 POWER_SUPPLY_PROP_CHARGE_FULL,
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +0530308 POWER_SUPPLY_PROP_CYCLE_COUNT,
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700309};
310
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530311static int discard_backup_fcc_data(struct qpnp_bms_chip *chip);
312static void backup_charge_cycle(struct qpnp_bms_chip *chip);
313
Xiaozhe Shi20640b52013-01-03 11:49:30 -0800314static bool bms_reset;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -0800315
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700316static int qpnp_read_wrapper(struct qpnp_bms_chip *chip, u8 *val,
317 u16 base, int count)
318{
319 int rc;
320 struct spmi_device *spmi = chip->spmi;
321
322 rc = spmi_ext_register_readl(spmi->ctrl, spmi->sid, base, val, count);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700323 if (rc) {
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700324 pr_err("SPMI read failed rc=%d\n", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700325 return rc;
326 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -0700327 return 0;
328}
329
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700330static int qpnp_write_wrapper(struct qpnp_bms_chip *chip, u8 *val,
331 u16 base, int count)
332{
333 int rc;
334 struct spmi_device *spmi = chip->spmi;
335
336 rc = spmi_ext_register_writel(spmi->ctrl, spmi->sid, base, val, count);
337 if (rc) {
338 pr_err("SPMI write failed rc=%d\n", rc);
339 return rc;
340 }
341 return 0;
342}
343
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800344static int qpnp_masked_write_base(struct qpnp_bms_chip *chip, u16 addr,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700345 u8 mask, u8 val)
346{
347 int rc;
348 u8 reg;
349
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800350 rc = qpnp_read_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700351 if (rc) {
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800352 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700353 return rc;
354 }
355 reg &= ~mask;
356 reg |= val & mask;
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800357 rc = qpnp_write_wrapper(chip, &reg, addr, 1);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700358 if (rc) {
359 pr_err("write failed addr = %03X, val = %02x, mask = %02x, reg = %02x, rc = %d\n",
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800360 addr, val, mask, reg, rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700361 return rc;
362 }
363 return 0;
364}
365
Xiaozhe Shidffbe692012-12-11 15:35:46 -0800366static int qpnp_masked_write_iadc(struct qpnp_bms_chip *chip, u16 addr,
367 u8 mask, u8 val)
368{
369 return qpnp_masked_write_base(chip, chip->iadc_base + addr, mask, val);
370}
371
372static int qpnp_masked_write(struct qpnp_bms_chip *chip, u16 addr,
373 u8 mask, u8 val)
374{
375 return qpnp_masked_write_base(chip, chip->base + addr, mask, val);
376}
377
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800378static void bms_stay_awake(struct bms_wakeup_source *source)
379{
380 if (__test_and_clear_bit(0, &source->disabled)) {
381 __pm_stay_awake(&source->source);
382 pr_debug("enabled source %s\n", source->source.name);
383 }
384}
385
386static void bms_relax(struct bms_wakeup_source *source)
387{
388 if (!__test_and_set_bit(0, &source->disabled)) {
389 __pm_relax(&source->source);
390 pr_debug("disabled source %s\n", source->source.name);
391 }
392}
393
394static void enable_bms_irq(struct bms_irq *irq)
395{
396 if (__test_and_clear_bit(0, &irq->disabled)) {
397 enable_irq(irq->irq);
398 pr_debug("enabled irq %d\n", irq->irq);
399 }
400}
401
402static void disable_bms_irq(struct bms_irq *irq)
403{
404 if (!__test_and_set_bit(0, &irq->disabled)) {
405 disable_irq(irq->irq);
406 pr_debug("disabled irq %d\n", irq->irq);
407 }
408}
409
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700410#define HOLD_OREG_DATA BIT(0)
411static int lock_output_data(struct qpnp_bms_chip *chip)
412{
413 int rc;
414
415 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL,
416 HOLD_OREG_DATA, HOLD_OREG_DATA);
417 if (rc) {
418 pr_err("couldnt lock bms output rc = %d\n", rc);
419 return rc;
420 }
421 return 0;
422}
423
424static int unlock_output_data(struct qpnp_bms_chip *chip)
425{
426 int rc;
427
428 rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL, HOLD_OREG_DATA, 0);
429 if (rc) {
430 pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc);
431 return rc;
432 }
433 return 0;
434}
435
436#define V_PER_BIT_MUL_FACTOR 97656
437#define V_PER_BIT_DIV_FACTOR 1000
438#define VADC_INTRINSIC_OFFSET 0x6000
439
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800440static int vadc_reading_to_uv(int reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700441{
442 if (reading <= VADC_INTRINSIC_OFFSET)
443 return 0;
444
445 return (reading - VADC_INTRINSIC_OFFSET)
446 * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR;
447}
448
449#define VADC_CALIB_UV 625000
450#define VBATT_MUL_FACTOR 3
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800451static int adjust_vbatt_reading(struct qpnp_bms_chip *chip, int reading_uv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700452{
453 s64 numerator, denominator;
454
455 if (reading_uv == 0)
456 return 0;
457
458 /* don't adjust if not calibrated */
459 if (chip->vadc_v0625 == 0 || chip->vadc_v1250 == 0) {
460 pr_debug("No cal yet return %d\n",
461 VBATT_MUL_FACTOR * reading_uv);
462 return VBATT_MUL_FACTOR * reading_uv;
463 }
464
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700465 numerator = ((s64)reading_uv - chip->vadc_v0625) * VADC_CALIB_UV;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700466 denominator = (s64)chip->vadc_v1250 - chip->vadc_v0625;
467 if (denominator == 0)
468 return reading_uv * VBATT_MUL_FACTOR;
469 return (VADC_CALIB_UV + div_s64(numerator, denominator))
470 * VBATT_MUL_FACTOR;
471}
472
Xiaozhe Shibdf14742012-12-05 12:41:48 -0800473static int convert_vbatt_uv_to_raw(struct qpnp_bms_chip *chip,
474 int unadjusted_vbatt)
475{
476 int scaled_vbatt = unadjusted_vbatt / VBATT_MUL_FACTOR;
477
478 if (scaled_vbatt <= 0)
479 return VADC_INTRINSIC_OFFSET;
480 return ((scaled_vbatt * V_PER_BIT_DIV_FACTOR) / V_PER_BIT_MUL_FACTOR)
481 + VADC_INTRINSIC_OFFSET;
482}
483
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700484static inline int convert_vbatt_raw_to_uv(struct qpnp_bms_chip *chip,
485 uint16_t reading)
486{
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700487 int64_t uv;
488 int rc;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700489
490 uv = vadc_reading_to_uv(reading);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700491 pr_debug("%u raw converted into %lld uv\n", reading, uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700492 uv = adjust_vbatt_reading(chip, uv);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700493 pr_debug("adjusted into %lld uv\n", uv);
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700494 rc = qpnp_vbat_sns_comp_result(chip->vadc_dev, &uv);
Xiaozhe Shi4dbc01b2013-04-30 11:27:52 -0700495 if (rc)
496 pr_debug("could not compensate vbatt\n");
497 pr_debug("compensated into %lld uv\n", uv);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700498 return uv;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700499}
500
501#define CC_READING_RESOLUTION_N 542535
502#define CC_READING_RESOLUTION_D 100000
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -0700503static s64 cc_reading_to_uv(s64 reading)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700504{
505 return div_s64(reading * CC_READING_RESOLUTION_N,
506 CC_READING_RESOLUTION_D);
507}
508
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800509#define QPNP_ADC_GAIN_IDEAL 3291LL
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700510static s64 cc_adjust_for_gain(s64 uv, uint16_t gain)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700511{
512 s64 result_uv;
513
514 pr_debug("adjusting_uv = %lld\n", uv);
Xiaozhe Shi820a47a2012-11-27 13:23:27 -0800515 if (gain == 0) {
516 pr_debug("gain is %d, not adjusting\n", gain);
517 return uv;
518 }
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700519 pr_debug("adjusting by factor: %lld/%hu = %lld%%\n",
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800520 QPNP_ADC_GAIN_IDEAL, gain,
521 div_s64(QPNP_ADC_GAIN_IDEAL * 100LL, (s64)gain));
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700522
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800523 result_uv = div_s64(uv * QPNP_ADC_GAIN_IDEAL, (s64)gain);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700524 pr_debug("result_uv = %lld\n", result_uv);
525 return result_uv;
526}
527
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700528static s64 cc_reverse_adjust_for_gain(struct qpnp_bms_chip *chip, s64 uv)
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800529{
530 struct qpnp_iadc_calib calibration;
531 int gain;
532 s64 result_uv;
533
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700534 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800535 gain = (int)calibration.gain_raw - (int)calibration.offset_raw;
536
537 pr_debug("reverse adjusting_uv = %lld\n", uv);
538 if (gain == 0) {
539 pr_debug("gain is %d, not adjusting\n", gain);
540 return uv;
541 }
542 pr_debug("adjusting by factor: %hu/%lld = %lld%%\n",
543 gain, QPNP_ADC_GAIN_IDEAL,
544 div64_s64((s64)gain * 100LL,
545 (s64)QPNP_ADC_GAIN_IDEAL));
546
547 result_uv = div64_s64(uv * (s64)gain, QPNP_ADC_GAIN_IDEAL);
548 pr_debug("result_uv = %lld\n", result_uv);
549 return result_uv;
550}
551
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700552static int convert_vsense_to_uv(struct qpnp_bms_chip *chip,
553 int16_t reading)
554{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700555 struct qpnp_iadc_calib calibration;
556
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700557 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700558 return cc_adjust_for_gain(cc_reading_to_uv(reading),
Xiaozhe Shi0c484932013-02-05 16:14:10 -0800559 calibration.gain_raw - calibration.offset_raw);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700560}
561
562static int read_vsense_avg(struct qpnp_bms_chip *chip, int *result_uv)
563{
564 int rc;
565 int16_t reading;
566
567 rc = qpnp_read_wrapper(chip, (u8 *)&reading,
568 chip->base + BMS1_VSENSE_AVG_DATA0, 2);
569
570 if (rc) {
571 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
572 return rc;
573 }
574
575 *result_uv = convert_vsense_to_uv(chip, reading);
576 return 0;
577}
578
579static int get_battery_current(struct qpnp_bms_chip *chip, int *result_ua)
580{
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700581 int rc, vsense_uv = 0;
582 int64_t temp_current;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700583
Xiaozhe Shid0a79542012-11-06 10:00:38 -0800584 if (chip->r_sense_uohm == 0) {
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700585 pr_err("r_sense is zero\n");
586 return -EINVAL;
587 }
588
589 mutex_lock(&chip->bms_output_lock);
590 lock_output_data(chip);
591 read_vsense_avg(chip, &vsense_uv);
592 unlock_output_data(chip);
593 mutex_unlock(&chip->bms_output_lock);
594
595 pr_debug("vsense_uv=%duV\n", vsense_uv);
596 /* cast for signed division */
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700597 temp_current = div_s64((vsense_uv * 1000000LL),
598 (int)chip->r_sense_uohm);
599
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700600 rc = qpnp_iadc_comp_result(chip->iadc_dev, &temp_current);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700601 if (rc)
602 pr_debug("error compensation failed: %d\n", rc);
603
604 *result_ua = temp_current;
605 pr_debug("err compensated ibat=%duA\n", *result_ua);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700606 return 0;
607}
608
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700609static int get_battery_voltage(struct qpnp_bms_chip *chip, int *result_uv)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700610{
611 int rc;
612 struct qpnp_vadc_result adc_result;
613
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700614 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &adc_result);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700615 if (rc) {
616 pr_err("error reading adc channel = %d, rc = %d\n",
617 VBAT_SNS, rc);
618 return rc;
619 }
620 pr_debug("mvolts phy = %lld meas = 0x%llx\n", adc_result.physical,
621 adc_result.measurement);
622 *result_uv = (int)adc_result.physical;
623 return 0;
624}
625
Xiaozhe Shie118c692012-09-24 15:17:43 -0700626#define CC_36_BIT_MASK 0xFFFFFFFFFLL
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800627static uint64_t convert_s64_to_s36(int64_t raw64)
628{
629 return (uint64_t) raw64 & CC_36_BIT_MASK;
630}
631
632#define SIGN_EXTEND_36_TO_64_MASK (-1LL ^ CC_36_BIT_MASK)
633static int64_t convert_s36_to_s64(uint64_t raw36)
634{
635 raw36 = raw36 & CC_36_BIT_MASK;
636 /* convert 36 bit signed value into 64 signed value */
637 return (raw36 >> 35) == 0LL ?
638 raw36 : (SIGN_EXTEND_36_TO_64_MASK | raw36);
639}
640
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700641static int read_cc_raw(struct qpnp_bms_chip *chip, int64_t *reading,
642 int cc_type)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700643{
644 int64_t raw_reading;
645 int rc;
646
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700647 if (cc_type == SHDW_CC)
648 rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
649 chip->base + BMS1_SW_CC_DATA0, 5);
650 else
651 rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading,
652 chip->base + BMS1_CC_DATA0, 5);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700653 if (rc) {
654 pr_err("Error reading cc: rc = %d\n", rc);
655 return -ENXIO;
656 }
657
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -0800658 *reading = convert_s36_to_s64(raw_reading);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700659
660 return 0;
661}
662
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700663static int calib_vadc(struct qpnp_bms_chip *chip)
664{
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700665 int rc, raw_0625, raw_1250;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700666 struct qpnp_vadc_result result;
667
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700668 rc = qpnp_vadc_read(chip->vadc_dev, REF_625MV, &result);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700669 if (rc) {
670 pr_debug("vadc read failed with rc = %d\n", rc);
671 return rc;
672 }
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700673 raw_0625 = result.adc_code;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700674
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700675 rc = qpnp_vadc_read(chip->vadc_dev, REF_125V, &result);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700676 if (rc) {
677 pr_debug("vadc read failed with rc = %d\n", rc);
678 return rc;
679 }
Xiaozhe Shif62c0152013-03-28 17:57:19 -0700680 raw_1250 = result.adc_code;
681 chip->vadc_v0625 = vadc_reading_to_uv(raw_0625);
682 chip->vadc_v1250 = vadc_reading_to_uv(raw_1250);
683 pr_debug("vadc calib: 0625 = %d raw (%d uv), 1250 = %d raw (%d uv)\n",
684 raw_0625, chip->vadc_v0625,
685 raw_1250, chip->vadc_v1250);
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700686 return 0;
687}
688
Xiaozhe Shie118c692012-09-24 15:17:43 -0700689static void convert_and_store_ocv(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800690 struct raw_soc_params *raw,
691 int batt_temp)
Xiaozhe Shie118c692012-09-24 15:17:43 -0700692{
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700693 int rc;
694
695 pr_debug("prev_last_good_ocv_raw = %d, last_good_ocv_raw = %d\n",
696 chip->prev_last_good_ocv_raw,
697 raw->last_good_ocv_raw);
698 rc = calib_vadc(chip);
699 if (rc)
700 pr_err("Vadc reference voltage read failed, rc = %d\n", rc);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700701 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
702 raw->last_good_ocv_uv = convert_vbatt_raw_to_uv(chip,
703 raw->last_good_ocv_raw);
704 chip->last_ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800705 chip->last_ocv_temp = batt_temp;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700706 chip->software_cc_uah = 0;
Xiaozhe Shi4e376652012-10-25 12:38:50 -0700707 pr_debug("last_good_ocv_uv = %d\n", raw->last_good_ocv_uv);
Xiaozhe Shie118c692012-09-24 15:17:43 -0700708}
709
Xiaozhe Shia045a562012-11-28 16:55:39 -0800710#define CLEAR_CC BIT(7)
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700711#define CLEAR_SHDW_CC BIT(6)
Xiaozhe Shia045a562012-11-28 16:55:39 -0800712/**
713 * reset both cc and sw-cc.
714 * note: this should only be ever called from one thread
715 * or there may be a race condition where CC is never enabled
716 * again
717 */
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700718static void reset_cc(struct qpnp_bms_chip *chip, u8 flags)
Xiaozhe Shia045a562012-11-28 16:55:39 -0800719{
720 int rc;
721
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700722 pr_debug("resetting cc manually with flags %hhu\n", flags);
723 mutex_lock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800724 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700725 flags,
726 flags);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800727 if (rc)
728 pr_err("cc reset failed: %d\n", rc);
729
730 /* wait for 100us for cc to reset */
731 udelay(100);
732
733 rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL,
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700734 flags, 0);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800735 if (rc)
736 pr_err("cc reenable failed: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700737 mutex_unlock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800738}
739
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700740static int get_battery_status(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800741{
742 union power_supply_propval ret = {0,};
743
744 if (chip->batt_psy == NULL)
745 chip->batt_psy = power_supply_get_by_name("battery");
746 if (chip->batt_psy) {
747 /* if battery has been registered, use the status property */
748 chip->batt_psy->get_property(chip->batt_psy,
749 POWER_SUPPLY_PROP_STATUS, &ret);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700750 return ret.intval;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800751 }
752
753 /* Default to false if the battery power supply is not registered. */
754 pr_debug("battery power supply is not registered\n");
Xiaozhe Shi890fbf42013-05-02 16:42:53 -0700755 return POWER_SUPPLY_STATUS_UNKNOWN;
756}
757
758static bool is_battery_charging(struct qpnp_bms_chip *chip)
759{
760 return get_battery_status(chip) == POWER_SUPPLY_STATUS_CHARGING;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800761}
762
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700763static bool is_battery_full(struct qpnp_bms_chip *chip)
764{
765 return get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL;
766}
767
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700768static bool is_battery_present(struct qpnp_bms_chip *chip)
769{
770 union power_supply_propval ret = {0,};
771
772 if (chip->batt_psy == NULL)
773 chip->batt_psy = power_supply_get_by_name("battery");
774 if (chip->batt_psy) {
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700775 /* if battery has been registered, use the present property */
Xiaozhe Shia6618a22013-03-27 10:26:29 -0700776 chip->batt_psy->get_property(chip->batt_psy,
777 POWER_SUPPLY_PROP_PRESENT, &ret);
778 return ret.intval;
779 }
780
781 /* Default to false if the battery power supply is not registered. */
782 pr_debug("battery power supply is not registered\n");
783 return false;
784}
785
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700786static int get_battery_insertion_ocv_uv(struct qpnp_bms_chip *chip)
787{
788 union power_supply_propval ret = {0,};
789 int rc, vbat;
790
791 if (chip->batt_psy == NULL)
792 chip->batt_psy = power_supply_get_by_name("battery");
793 if (chip->batt_psy) {
794 /* if battery has been registered, use the ocv property */
795 rc = chip->batt_psy->get_property(chip->batt_psy,
796 POWER_SUPPLY_PROP_VOLTAGE_OCV, &ret);
797 if (rc) {
798 /*
799 * Default to vbatt if the battery OCV is not
800 * registered.
801 */
802 pr_debug("Battery psy does not have voltage ocv\n");
803 rc = get_battery_voltage(chip, &vbat);
804 if (rc)
805 return -EINVAL;
806 return vbat;
807 }
808 return ret.intval;
809 }
810
811 pr_debug("battery power supply is not registered\n");
812 return -EINVAL;
813}
814
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700815static bool is_batfet_closed(struct qpnp_bms_chip *chip)
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800816{
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -0700817 union power_supply_propval ret = {0,};
818
819 if (chip->batt_psy == NULL)
820 chip->batt_psy = power_supply_get_by_name("battery");
821 if (chip->batt_psy) {
822 /* if battery has been registered, use the online property */
823 chip->batt_psy->get_property(chip->batt_psy,
824 POWER_SUPPLY_PROP_ONLINE, &ret);
825 return !!ret.intval;
826 }
827
828 /* Default to true if the battery power supply is not registered. */
829 pr_debug("battery power supply is not registered\n");
830 return true;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800831}
832
833static int get_simultaneous_batt_v_and_i(struct qpnp_bms_chip *chip,
834 int *ibat_ua, int *vbat_uv)
835{
836 struct qpnp_iadc_result i_result;
837 struct qpnp_vadc_result v_result;
838 enum qpnp_iadc_channels iadc_channel;
839 int rc;
840
841 iadc_channel = chip->use_external_rsense ?
842 EXTERNAL_RSENSE : INTERNAL_RSENSE;
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700843 if (is_battery_full(chip)) {
844 rc = get_battery_current(chip, ibat_ua);
845 if (rc) {
846 pr_err("bms current read failed with rc: %d\n", rc);
847 return rc;
848 }
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -0700849 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &v_result);
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700850 if (rc) {
851 pr_err("vadc read failed with rc: %d\n", rc);
852 return rc;
853 }
854 *vbat_uv = (int)v_result.physical;
855 } else {
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -0700856 rc = qpnp_iadc_vadc_sync_read(chip->iadc_dev,
857 iadc_channel, &i_result,
Xiaozhe Shi8658c982013-04-30 11:33:07 -0700858 VBAT_SNS, &v_result);
859 if (rc) {
860 pr_err("adc sync read failed with rc: %d\n", rc);
861 return rc;
862 }
863 /*
864 * reverse the current read by the iadc, since the bms uses
865 * flipped battery current polarity.
866 */
867 *ibat_ua = -1 * (int)i_result.result_ua;
868 *vbat_uv = (int)v_result.physical;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800869 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800870
871 return 0;
872}
873
874static int estimate_ocv(struct qpnp_bms_chip *chip)
875{
876 int ibat_ua, vbat_uv, ocv_est_uv;
877 int rc;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -0700878 int rbatt_mohm = chip->default_rbatt_mohm + chip->r_conn_mohm
879 + chip->rbatt_capacitive_mohm;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800880
881 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
882 if (rc) {
883 pr_err("simultaneous failed rc = %d\n", rc);
884 return rc;
885 }
886
887 ocv_est_uv = vbat_uv + (ibat_ua * rbatt_mohm) / 1000;
888 pr_debug("estimated pon ocv = %d\n", ocv_est_uv);
889 return ocv_est_uv;
890}
891
892static void reset_for_new_battery(struct qpnp_bms_chip *chip, int batt_temp)
893{
Xiaozhe Shi24f91a02013-08-29 17:15:05 -0700894 chip->last_ocv_uv = chip->insertion_ocv_uv;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700895 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800896 chip->last_soc = -EINVAL;
Xiaozhe Shi04da0992013-04-26 16:32:14 -0700897 chip->last_soc_invalid = true;
898 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800899 chip->soc_at_cv = -EINVAL;
900 chip->shutdown_soc_invalid = true;
901 chip->shutdown_soc = 0;
902 chip->shutdown_iavg_ma = 0;
903 chip->prev_pc_unusable = -EINVAL;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700904 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -0700905 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700906 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800907 chip->last_cc_uah = INT_MIN;
908 chip->last_ocv_temp = batt_temp;
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -0800909 chip->prev_batt_terminal_uv = 0;
Anirudh Ghayale0c02932013-07-08 16:26:35 +0530910 if (chip->enable_fcc_learning) {
911 chip->adjusted_fcc_temp_lut = NULL;
912 chip->fcc_new_mah = -EINVAL;
913 /* reset the charge-cycle and charge-increase registers */
914 chip->charge_increase = 0;
915 chip->charge_cycles = 0;
916 backup_charge_cycle(chip);
917 /* discard all the FCC learnt data and reset the local table */
918 discard_backup_fcc_data(chip);
919 memset(chip->fcc_learning_samples, 0,
920 chip->min_fcc_learning_samples *
921 sizeof(struct fcc_sample));
922 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -0800923}
924
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -0700925#define SIGN(x) ((x) < 0 ? -1 : 1)
926#define UV_PER_SPIN 50000
927static int find_ocv_for_pc(struct qpnp_bms_chip *chip, int batt_temp, int pc)
928{
929 int new_pc;
930 int batt_temp_degc = batt_temp / 10;
931 int ocv_mv;
932 int delta_mv = 5;
933 int max_spin_count;
934 int count = 0;
935 int sign, new_sign;
936
937 ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut, batt_temp_degc, pc);
938
939 new_pc = interpolate_pc(chip->pc_temp_ocv_lut, batt_temp_degc, ocv_mv);
940 pr_debug("test revlookup pc = %d for ocv = %d\n", new_pc, ocv_mv);
941 max_spin_count = 1 + (chip->max_voltage_uv - chip->v_cutoff_uv)
942 / UV_PER_SPIN;
943 sign = SIGN(pc - new_pc);
944
945 while (abs(new_pc - pc) != 0 && count < max_spin_count) {
946 /*
947 * If the newly interpolated pc is larger than the lookup pc,
948 * the ocv should be reduced and vice versa
949 */
950 new_sign = SIGN(pc - new_pc);
951 /*
952 * If the sign has changed, then we have passed the lookup pc.
953 * reduce the ocv step size to get finer results.
954 *
955 * If we have already reduced the ocv step size and still
956 * passed the lookup pc, just stop and use the current ocv.
957 * This can only happen if the batterydata profile is
958 * non-monotonic anyways.
959 */
960 if (new_sign != sign) {
961 if (delta_mv > 1)
962 delta_mv = 1;
963 else
964 break;
965 }
966 sign = new_sign;
967
968 ocv_mv = ocv_mv + delta_mv * sign;
969 new_pc = interpolate_pc(chip->pc_temp_ocv_lut,
970 batt_temp_degc, ocv_mv);
971 pr_debug("test revlookup pc = %d for ocv = %d\n",
972 new_pc, ocv_mv);
973 count++;
974 }
975
976 return ocv_mv * 1000;
977}
978
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -0800979#define OCV_RAW_UNINITIALIZED 0xFFFF
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -0700980#define MIN_OCV_UV 2000000
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700981static int read_soc_params_raw(struct qpnp_bms_chip *chip,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -0800982 struct raw_soc_params *raw,
983 int batt_temp)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -0700984{
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -0700985 int warm_reset, rc;
Xiaozhe Shie118c692012-09-24 15:17:43 -0700986
987 mutex_lock(&chip->bms_output_lock);
Xiaozhe Shia045a562012-11-28 16:55:39 -0800988
Xiaozhe Shie118c692012-09-24 15:17:43 -0700989 lock_output_data(chip);
990
991 rc = qpnp_read_wrapper(chip, (u8 *)&raw->last_good_ocv_raw,
992 chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
993 if (rc) {
994 pr_err("Error reading ocv: rc = %d\n", rc);
995 return -ENXIO;
996 }
997
Xiaozhe Shif3da8622013-06-10 14:50:56 -0700998 rc = read_cc_raw(chip, &raw->cc, CC);
999 rc = read_cc_raw(chip, &raw->shdw_cc, SHDW_CC);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001000 if (rc) {
1001 pr_err("Failed to read raw cc data, rc = %d\n", rc);
1002 return rc;
1003 }
1004
1005 unlock_output_data(chip);
1006 mutex_unlock(&chip->bms_output_lock);
1007
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08001008 if (chip->prev_last_good_ocv_raw == OCV_RAW_UNINITIALIZED) {
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001009 convert_and_store_ocv(chip, raw, batt_temp);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001010 pr_debug("PON_OCV_UV = %d, cc = %llx\n",
1011 chip->last_ocv_uv, raw->cc);
1012 warm_reset = qpnp_pon_is_warm_reset();
1013 if (raw->last_good_ocv_uv < MIN_OCV_UV
1014 || warm_reset > 0) {
1015 pr_debug("OCV is stale or bad, estimating new OCV.\n");
1016 chip->last_ocv_uv = estimate_ocv(chip);
1017 raw->last_good_ocv_uv = chip->last_ocv_uv;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001018 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07001019 pr_debug("New PON_OCV_UV = %d, cc = %llx\n",
1020 chip->last_ocv_uv, raw->cc);
1021 }
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001022 } else if (chip->new_battery) {
1023 /* if a new battery was inserted, estimate the ocv */
1024 reset_for_new_battery(chip, batt_temp);
1025 raw->cc = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001026 raw->shdw_cc = 0;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001027 raw->last_good_ocv_uv = chip->last_ocv_uv;
1028 chip->new_battery = false;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001029 } else if (chip->done_charging) {
1030 chip->done_charging = false;
1031 /* if we just finished charging, reset CC and fake 100% */
1032 chip->ocv_reading_at_100 = raw->last_good_ocv_raw;
Xiaozhe Shi39a5dc62013-10-30 11:33:27 -07001033 chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp, 100);
1034 raw->last_good_ocv_uv = chip->last_ocv_uv;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001035 raw->cc = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001036 raw->shdw_cc = 0;
1037 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001038 chip->last_ocv_temp = batt_temp;
1039 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001040 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001041 chip->last_cc_uah = INT_MIN;
1042 pr_debug("EOC Battery full ocv_reading = 0x%x\n",
1043 chip->ocv_reading_at_100);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001044 } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) {
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001045 convert_and_store_ocv(chip, raw, batt_temp);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001046 /* forget the old cc value upon ocv */
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001047 chip->last_cc_uah = INT_MIN;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001048 } else {
1049 raw->last_good_ocv_uv = chip->last_ocv_uv;
1050 }
1051
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001052 /* stop faking a high OCV if we get a new OCV */
1053 if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw)
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08001054 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
Xiaozhe Shi74548b92013-05-02 16:47:08 -07001055
Xiaozhe Shie118c692012-09-24 15:17:43 -07001056 pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n",
1057 raw->last_good_ocv_raw, raw->last_good_ocv_uv);
1058 pr_debug("cc_raw= 0x%llx\n", raw->cc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07001059 return 0;
1060}
1061
Xiaozhe Shie118c692012-09-24 15:17:43 -07001062static int calculate_pc(struct qpnp_bms_chip *chip, int ocv_uv,
1063 int batt_temp)
1064{
1065 int pc;
1066
1067 pc = interpolate_pc(chip->pc_temp_ocv_lut,
1068 batt_temp / 10, ocv_uv / 1000);
1069 pr_debug("pc = %u %% for ocv = %d uv batt_temp = %d\n",
1070 pc, ocv_uv, batt_temp);
1071 /* Multiply the initial FCC value by the scale factor. */
1072 return pc;
1073}
1074
1075static int calculate_fcc(struct qpnp_bms_chip *chip, int batt_temp)
1076{
1077 int fcc_uah;
1078
1079 if (chip->adjusted_fcc_temp_lut == NULL) {
1080 /* interpolate_fcc returns a mv value. */
1081 fcc_uah = interpolate_fcc(chip->fcc_temp_lut,
1082 batt_temp) * 1000;
1083 pr_debug("fcc = %d uAh\n", fcc_uah);
1084 return fcc_uah;
1085 } else {
1086 return 1000 * interpolate_fcc(chip->adjusted_fcc_temp_lut,
1087 batt_temp);
1088 }
1089}
1090
1091/* calculate remaining charge at the time of ocv */
1092static int calculate_ocv_charge(struct qpnp_bms_chip *chip,
1093 struct raw_soc_params *raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001094 int fcc_uah)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001095{
1096 int ocv_uv, pc;
1097
1098 ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001099 pc = calculate_pc(chip, ocv_uv, chip->last_ocv_temp);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001100 pr_debug("ocv_uv = %d pc = %d\n", ocv_uv, pc);
1101 return (fcc_uah * pc) / 100;
1102}
1103
Xiaozhe Shie118c692012-09-24 15:17:43 -07001104#define CC_READING_TICKS 56
1105#define SLEEP_CLK_HZ 32764
1106#define SECONDS_PER_HOUR 3600
1107
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001108static s64 cc_uv_to_pvh(s64 cc_uv)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001109{
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001110 /* Note that it is necessary need to multiply by 1000000 to convert
1111 * from uvh to pvh here.
1112 * However, the maximum Coulomb Counter value is 2^35, which can cause
1113 * an over flow.
1114 * Multiply by 100000 first to perserve as much precision as possible
1115 * then multiply by 10 after doing the division in order to avoid
1116 * overflow on the maximum Coulomb Counter value.
1117 */
1118 return div_s64(cc_uv * CC_READING_TICKS * 100000,
1119 SLEEP_CLK_HZ * SECONDS_PER_HOUR) * 10;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001120}
1121
1122/**
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001123 * calculate_cc() - converts a hardware coulomb counter reading into uah
Xiaozhe Shie118c692012-09-24 15:17:43 -07001124 * @chip: the bms chip pointer
1125 * @cc: the cc reading from bms h/w
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001126 * @cc_type: calcualte cc from regular or shadow coulomb counter
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001127 * @clear_cc: whether this function should clear the hardware counter
1128 * after reading
Xiaozhe Shie118c692012-09-24 15:17:43 -07001129 *
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001130 * Converts the 64 bit hardware coulomb counter into microamp-hour by taking
1131 * into account hardware resolution and adc errors.
1132 *
1133 * Return: the coulomb counter based charge in uAh (micro-amp hour)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001134 */
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001135static int calculate_cc(struct qpnp_bms_chip *chip, int64_t cc,
1136 int cc_type, int clear_cc)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001137{
Xiaozhe Shi4e376652012-10-25 12:38:50 -07001138 struct qpnp_iadc_calib calibration;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001139 struct qpnp_vadc_result result;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001140 int64_t cc_voltage_uv, cc_pvh, cc_uah, *software_counter;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001141 int rc;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001142
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001143 software_counter = cc_type == SHDW_CC ?
1144 &chip->software_shdw_cc_uah : &chip->software_cc_uah;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001145 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001146 if (rc) {
1147 pr_err("could not read pmic die temperature: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001148 return *software_counter;
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001149 }
1150
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07001151 qpnp_iadc_get_gain_and_offset(chip->iadc_dev, &calibration);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001152 pr_debug("%scc = %lld, die_temp = %lld\n",
1153 cc_type == SHDW_CC ? "shdw_" : "",
1154 cc, result.physical);
Xiaozhe Shi8f5dd1b2013-04-30 10:27:58 -07001155 cc_voltage_uv = cc_reading_to_uv(cc);
Xiaozhe Shi0c484932013-02-05 16:14:10 -08001156 cc_voltage_uv = cc_adjust_for_gain(cc_voltage_uv,
1157 calibration.gain_raw
1158 - calibration.offset_raw);
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001159 cc_pvh = cc_uv_to_pvh(cc_voltage_uv);
Xiaozhe Shia9b597d2013-02-12 11:00:39 -08001160 cc_uah = div_s64(cc_pvh, chip->r_sense_uohm);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07001161 rc = qpnp_iadc_comp_result(chip->iadc_dev, &cc_uah);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001162 if (rc)
1163 pr_debug("error compensation failed: %d\n", rc);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001164 if (clear_cc == RESET) {
1165 pr_debug("software_%scc = %lld, added cc_uah = %lld\n",
1166 cc_type == SHDW_CC ? "sw_" : "",
1167 *software_counter, cc_uah);
1168 *software_counter += cc_uah;
1169 reset_cc(chip, cc_type == SHDW_CC ? CLEAR_SHDW_CC : CLEAR_CC);
1170 return (int)*software_counter;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001171 } else {
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001172 pr_debug("software_%scc = %lld, cc_uah = %lld, total = %lld\n",
1173 cc_type == SHDW_CC ? "shdw_" : "",
1174 *software_counter, cc_uah,
1175 *software_counter + cc_uah);
1176 return *software_counter + cc_uah;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07001177 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07001178}
1179
1180static int get_rbatt(struct qpnp_bms_chip *chip,
1181 int soc_rbatt_mohm, int batt_temp)
1182{
1183 int rbatt_mohm, scalefactor;
1184
1185 rbatt_mohm = chip->default_rbatt_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001186 if (chip->rbatt_sf_lut == NULL) {
1187 pr_debug("RBATT = %d\n", rbatt_mohm);
1188 return rbatt_mohm;
1189 }
1190 /* Convert the batt_temp to DegC from deciDegC */
1191 batt_temp = batt_temp / 10;
1192 scalefactor = interpolate_scalingfactor(chip->rbatt_sf_lut,
1193 batt_temp, soc_rbatt_mohm);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001194 rbatt_mohm = (rbatt_mohm * scalefactor) / 100;
1195
1196 rbatt_mohm += chip->r_conn_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07001197 rbatt_mohm += chip->rbatt_capacitive_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001198 return rbatt_mohm;
1199}
1200
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -07001201#define IAVG_MINIMAL_TIME 2
Xiaozhe Shie118c692012-09-24 15:17:43 -07001202static void calculate_iavg(struct qpnp_bms_chip *chip, int cc_uah,
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001203 int *iavg_ua, int delta_time_s)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001204{
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001205 int delta_cc_uah = 0;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001206
Xiaozhe Shi06b67cc2013-03-29 12:07:40 -07001207 /*
1208 * use the battery current if called too quickly
1209 */
1210 if (delta_time_s < IAVG_MINIMAL_TIME
1211 || chip->last_cc_uah == INT_MIN) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001212 get_battery_current(chip, iavg_ua);
1213 goto out;
1214 }
1215
Xiaozhe Shie118c692012-09-24 15:17:43 -07001216 delta_cc_uah = cc_uah - chip->last_cc_uah;
1217
1218 *iavg_ua = div_s64((s64)delta_cc_uah * 3600, delta_time_s);
1219
Xiaozhe Shie118c692012-09-24 15:17:43 -07001220out:
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001221 pr_debug("delta_cc = %d iavg_ua = %d\n", delta_cc_uah, (int)*iavg_ua);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001222
1223 /* remember cc_uah */
1224 chip->last_cc_uah = cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001225}
1226
1227static int calculate_termination_uuc(struct qpnp_bms_chip *chip,
1228 struct soc_params *params,
1229 int batt_temp, int uuc_iavg_ma,
1230 int *ret_pc_unusable)
1231{
1232 int unusable_uv, pc_unusable, uuc_uah;
1233 int i = 0;
1234 int ocv_mv;
1235 int batt_temp_degc = batt_temp / 10;
1236 int rbatt_mohm;
1237 int delta_uv;
1238 int prev_delta_uv = 0;
1239 int prev_rbatt_mohm = 0;
1240 int uuc_rbatt_mohm;
1241
1242 for (i = 0; i <= 100; i++) {
1243 ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
1244 batt_temp_degc, i);
1245 rbatt_mohm = get_rbatt(chip, i, batt_temp);
1246 unusable_uv = (rbatt_mohm * uuc_iavg_ma)
1247 + (chip->v_cutoff_uv);
1248 delta_uv = ocv_mv * 1000 - unusable_uv;
1249
Xiaozhe Shie118c692012-09-24 15:17:43 -07001250 if (delta_uv > 0)
1251 break;
1252
1253 prev_delta_uv = delta_uv;
1254 prev_rbatt_mohm = rbatt_mohm;
1255 }
1256
1257 uuc_rbatt_mohm = linear_interpolate(rbatt_mohm, delta_uv,
1258 prev_rbatt_mohm, prev_delta_uv,
1259 0);
1260
1261 unusable_uv = (uuc_rbatt_mohm * uuc_iavg_ma) + (chip->v_cutoff_uv);
1262
1263 pc_unusable = calculate_pc(chip, unusable_uv, batt_temp);
1264 uuc_uah = (params->fcc_uah * pc_unusable) / 100;
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001265 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 -07001266 uuc_iavg_ma,
1267 uuc_rbatt_mohm, unusable_uv,
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001268 pc_unusable, i, uuc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001269 *ret_pc_unusable = pc_unusable;
1270 return uuc_uah;
1271}
1272
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001273#define TIME_PER_PERCENT_UUC 60
Xiaozhe Shie118c692012-09-24 15:17:43 -07001274static int adjust_uuc(struct qpnp_bms_chip *chip,
1275 struct soc_params *params,
1276 int new_pc_unusable,
1277 int new_uuc_uah,
1278 int batt_temp)
1279{
1280 int new_unusable_mv, new_iavg_ma;
1281 int batt_temp_degc = batt_temp / 10;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001282 int max_percent_change;
1283
1284 max_percent_change = max(params->delta_time_s
1285 / TIME_PER_PERCENT_UUC, 1);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001286
1287 if (chip->prev_pc_unusable == -EINVAL
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001288 || abs(chip->prev_pc_unusable - new_pc_unusable)
1289 <= max_percent_change) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07001290 chip->prev_pc_unusable = new_pc_unusable;
1291 return new_uuc_uah;
1292 }
1293
1294 /* the uuc is trying to change more than 1% restrict it */
1295 if (new_pc_unusable > chip->prev_pc_unusable)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001296 chip->prev_pc_unusable += max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001297 else
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001298 chip->prev_pc_unusable -= max_percent_change;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001299
1300 new_uuc_uah = (params->fcc_uah * chip->prev_pc_unusable) / 100;
1301
1302 /* also find update the iavg_ma accordingly */
1303 new_unusable_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
1304 batt_temp_degc, chip->prev_pc_unusable);
1305 if (new_unusable_mv < chip->v_cutoff_uv/1000)
1306 new_unusable_mv = chip->v_cutoff_uv/1000;
1307
1308 new_iavg_ma = (new_unusable_mv * 1000 - chip->v_cutoff_uv)
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001309 / params->rbatt_mohm;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001310 if (new_iavg_ma == 0)
1311 new_iavg_ma = 1;
1312 chip->prev_uuc_iavg_ma = new_iavg_ma;
1313 pr_debug("Restricting UUC to %d (%d%%) unusable_mv = %d iavg_ma = %d\n",
1314 new_uuc_uah, chip->prev_pc_unusable,
1315 new_unusable_mv, new_iavg_ma);
1316
1317 return new_uuc_uah;
1318}
1319
Abhijeet Dharmapurikarbdf8ba82012-12-20 18:33:56 -08001320#define MIN_IAVG_MA 250
Xiaozhe Shie118c692012-09-24 15:17:43 -07001321static int calculate_unusable_charge_uah(struct qpnp_bms_chip *chip,
1322 struct soc_params *params,
1323 int batt_temp)
1324{
1325 int uuc_uah_iavg;
1326 int i;
1327 int uuc_iavg_ma = params->iavg_ua / 1000;
1328 int pc_unusable;
1329
1330 /*
1331 * if called first time, fill all the samples with
1332 * the shutdown_iavg_ma
1333 */
1334 if (chip->first_time_calc_uuc && chip->shutdown_iavg_ma != 0) {
1335 pr_debug("Using shutdown_iavg_ma = %d in all samples\n",
1336 chip->shutdown_iavg_ma);
1337 for (i = 0; i < IAVG_SAMPLES; i++)
1338 chip->iavg_samples_ma[i] = chip->shutdown_iavg_ma;
1339
1340 chip->iavg_index = 0;
1341 chip->iavg_num_samples = IAVG_SAMPLES;
1342 }
1343
Xiaozhe Shi70633922013-09-23 15:50:53 -07001344 if (params->delta_time_s >= IAVG_MINIMAL_TIME) {
1345 /*
1346 * if charging use a nominal avg current to keep
1347 * a reasonable UUC while charging
1348 */
1349 if (uuc_iavg_ma < MIN_IAVG_MA)
1350 uuc_iavg_ma = MIN_IAVG_MA;
1351 chip->iavg_samples_ma[chip->iavg_index] = uuc_iavg_ma;
1352 chip->iavg_index = (chip->iavg_index + 1) % IAVG_SAMPLES;
1353 chip->iavg_num_samples++;
1354 if (chip->iavg_num_samples >= IAVG_SAMPLES)
1355 chip->iavg_num_samples = IAVG_SAMPLES;
1356 }
Xiaozhe Shie118c692012-09-24 15:17:43 -07001357
1358 /* now that this sample is added calcualte the average */
1359 uuc_iavg_ma = 0;
1360 if (chip->iavg_num_samples != 0) {
1361 for (i = 0; i < chip->iavg_num_samples; i++) {
1362 pr_debug("iavg_samples_ma[%d] = %d\n", i,
1363 chip->iavg_samples_ma[i]);
1364 uuc_iavg_ma += chip->iavg_samples_ma[i];
1365 }
1366
1367 uuc_iavg_ma = DIV_ROUND_CLOSEST(uuc_iavg_ma,
1368 chip->iavg_num_samples);
1369 }
1370
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001371 /*
1372 * if we're in bms reset mode, force uuc to be 3% of fcc
1373 */
1374 if (bms_reset)
1375 return (params->fcc_uah * 3) / 100;
1376
Xiaozhe Shi75e5efe2013-02-07 09:51:43 -08001377 uuc_uah_iavg = calculate_termination_uuc(chip, params, batt_temp,
1378 uuc_iavg_ma, &pc_unusable);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001379 pr_debug("uuc_iavg_ma = %d uuc with iavg = %d\n",
1380 uuc_iavg_ma, uuc_uah_iavg);
1381
1382 chip->prev_uuc_iavg_ma = uuc_iavg_ma;
1383 /* restrict the uuc such that it can increase only by one percent */
1384 uuc_uah_iavg = adjust_uuc(chip, params, pc_unusable,
1385 uuc_uah_iavg, batt_temp);
1386
Xiaozhe Shie118c692012-09-24 15:17:43 -07001387 return uuc_uah_iavg;
1388}
1389
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001390static s64 find_ocv_charge_for_soc(struct qpnp_bms_chip *chip,
1391 struct soc_params *params, int soc)
Xiaozhe Shie118c692012-09-24 15:17:43 -07001392{
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001393 return div_s64((s64)soc * (params->fcc_uah - params->uuc_uah),
1394 100) + params->cc_uah + params->uuc_uah;
1395}
Xiaozhe Shie118c692012-09-24 15:17:43 -07001396
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001397static int find_pc_for_soc(struct qpnp_bms_chip *chip,
1398 struct soc_params *params, int soc)
1399{
1400 int ocv_charge_uah = find_ocv_charge_for_soc(chip, params, soc);
1401 int pc;
1402
Xiaozhe Shie118c692012-09-24 15:17:43 -07001403 pc = DIV_ROUND_CLOSEST((int)ocv_charge_uah * 100, params->fcc_uah);
1404 pc = clamp(pc, 0, 100);
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001405 pr_debug("soc = %d, fcc = %d uuc = %d rc = %d pc = %d\n",
1406 soc, params->fcc_uah, params->uuc_uah,
1407 ocv_charge_uah, pc);
1408 return pc;
1409}
Xiaozhe Shie118c692012-09-24 15:17:43 -07001410
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001411static int get_current_time(unsigned long *now_tm_sec)
1412{
1413 struct rtc_time tm;
1414 struct rtc_device *rtc;
1415 int rc;
1416
1417 rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
1418 if (rtc == NULL) {
1419 pr_err("%s: unable to open rtc device (%s)\n",
1420 __FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
Xiaozhe Shi0e01af62013-05-06 12:56:08 -07001421 return -EINVAL;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001422 }
1423
1424 rc = rtc_read_time(rtc, &tm);
1425 if (rc) {
1426 pr_err("Error reading rtc device (%s) : %d\n",
1427 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1428 goto close_time;
1429 }
1430
1431 rc = rtc_valid_tm(&tm);
1432 if (rc) {
1433 pr_err("Invalid RTC time (%s): %d\n",
1434 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1435 goto close_time;
1436 }
1437 rtc_tm_to_time(&tm, now_tm_sec);
1438
1439close_time:
1440 rtc_class_close(rtc);
1441 return rc;
1442}
1443
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08001444/* Returns estimated battery resistance */
1445static int get_prop_bms_batt_resistance(struct qpnp_bms_chip *chip)
1446{
1447 return chip->rbatt_mohm * 1000;
1448}
1449
1450/* Returns instantaneous current in uA */
1451static int get_prop_bms_current_now(struct qpnp_bms_chip *chip)
1452{
1453 int rc, result_ua;
1454
1455 rc = get_battery_current(chip, &result_ua);
1456 if (rc) {
1457 pr_err("failed to get current: %d\n", rc);
1458 return rc;
1459 }
1460 return result_ua;
1461}
1462
1463/* Returns coulomb counter in uAh */
1464static int get_prop_bms_charge_counter(struct qpnp_bms_chip *chip)
1465{
1466 int64_t cc_raw;
1467
1468 mutex_lock(&chip->bms_output_lock);
1469 lock_output_data(chip);
1470 read_cc_raw(chip, &cc_raw, false);
1471 unlock_output_data(chip);
1472 mutex_unlock(&chip->bms_output_lock);
1473
1474 return calculate_cc(chip, cc_raw, CC, NORESET);
1475}
1476
1477/* Returns shadow coulomb counter in uAh */
1478static int get_prop_bms_charge_counter_shadow(struct qpnp_bms_chip *chip)
1479{
1480 int64_t cc_raw;
1481
1482 mutex_lock(&chip->bms_output_lock);
1483 lock_output_data(chip);
1484 read_cc_raw(chip, &cc_raw, true);
1485 unlock_output_data(chip);
1486 mutex_unlock(&chip->bms_output_lock);
1487
1488 return calculate_cc(chip, cc_raw, SHDW_CC, NORESET);
1489}
1490
1491/* Returns full charge design in uAh */
1492static int get_prop_bms_charge_full_design(struct qpnp_bms_chip *chip)
1493{
1494 return chip->fcc_mah * 1000;
1495}
1496
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05301497/* Returns the current full charge in uAh */
1498static int get_prop_bms_charge_full(struct qpnp_bms_chip *chip)
1499{
1500 int rc;
1501 struct qpnp_vadc_result result;
1502
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001503 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05301504 if (rc) {
1505 pr_err("Unable to read battery temperature\n");
1506 return rc;
1507 }
1508
1509 return calculate_fcc(chip, (int)result.physical);
1510}
1511
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001512static int calculate_delta_time(unsigned long *time_stamp, int *delta_time_s)
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001513{
1514 unsigned long now_tm_sec = 0;
1515
1516 /* default to delta time = 0 if anything fails */
1517 *delta_time_s = 0;
1518
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001519 if (get_current_time(&now_tm_sec)) {
1520 pr_err("RTC read failed\n");
1521 return 0;
1522 }
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001523
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001524 *delta_time_s = (now_tm_sec - *time_stamp);
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001525
1526 /* remember this time */
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001527 *time_stamp = now_tm_sec;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001528 return 0;
1529}
1530
Xiaozhe Shie118c692012-09-24 15:17:43 -07001531static void calculate_soc_params(struct qpnp_bms_chip *chip,
1532 struct raw_soc_params *raw,
1533 struct soc_params *params,
1534 int batt_temp)
1535{
Xiaozhe Shi219cb222013-06-10 15:49:59 -07001536 int soc_rbatt, shdw_cc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07001537
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001538 calculate_delta_time(&chip->tm_sec, &params->delta_time_s);
1539 pr_debug("tm_sec = %ld, delta_s = %d\n",
1540 chip->tm_sec, params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001541 params->fcc_uah = calculate_fcc(chip, batt_temp);
1542 pr_debug("FCC = %uuAh batt_temp = %d\n", params->fcc_uah, batt_temp);
1543
1544 /* calculate remainging charge */
1545 params->ocv_charge_uah = calculate_ocv_charge(
1546 chip, raw,
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08001547 params->fcc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001548 pr_debug("ocv_charge_uah = %uuAh\n", params->ocv_charge_uah);
1549
1550 /* calculate cc micro_volt_hour */
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001551 params->cc_uah = calculate_cc(chip, raw->cc, CC, RESET);
Xiaozhe Shi219cb222013-06-10 15:49:59 -07001552 shdw_cc_uah = calculate_cc(chip, raw->shdw_cc, SHDW_CC, RESET);
1553 pr_debug("cc_uah = %duAh raw->cc = %llx, shdw_cc_uah = %duAh raw->shdw_cc = %llx\n",
1554 params->cc_uah, raw->cc,
1555 shdw_cc_uah, raw->shdw_cc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001556
1557 soc_rbatt = ((params->ocv_charge_uah - params->cc_uah) * 100)
1558 / params->fcc_uah;
1559 if (soc_rbatt < 0)
1560 soc_rbatt = 0;
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08001561 params->rbatt_mohm = get_rbatt(chip, soc_rbatt, batt_temp);
Xiaozhe Shi794607c2013-02-19 10:25:59 -08001562 pr_debug("rbatt_mohm = %d\n", params->rbatt_mohm);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001563
Xiaozhe Shi1e87cda2013-05-17 10:18:56 -07001564 if (params->rbatt_mohm != chip->rbatt_mohm) {
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07001565 chip->rbatt_mohm = params->rbatt_mohm;
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07001566 if (chip->bms_psy_registered)
Xiaozhe Shi1e87cda2013-05-17 10:18:56 -07001567 power_supply_changed(&chip->bms_psy);
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07001568 }
1569
Xiaozhe Shif36d2862013-01-04 10:17:35 -08001570 calculate_iavg(chip, params->cc_uah, &params->iavg_ua,
1571 params->delta_time_s);
Xiaozhe Shie118c692012-09-24 15:17:43 -07001572
1573 params->uuc_uah = calculate_unusable_charge_uah(chip, params,
1574 batt_temp);
1575 pr_debug("UUC = %uuAh\n", params->uuc_uah);
1576}
1577
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001578static int bound_soc(int soc)
1579{
1580 soc = max(0, soc);
1581 soc = min(100, soc);
1582 return soc;
1583}
1584
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001585#define IBAT_TOL_MASK 0x0F
1586#define OCV_TOL_MASK 0xF0
1587#define IBAT_TOL_DEFAULT 0x03
1588#define IBAT_TOL_NOCHG 0x0F
1589#define OCV_TOL_DEFAULT 0x20
1590#define OCV_TOL_NO_OCV 0x00
1591static int stop_ocv_updates(struct qpnp_bms_chip *chip)
1592{
1593 pr_debug("stopping ocv updates\n");
1594 return qpnp_masked_write(chip, BMS1_TOL_CTL,
1595 OCV_TOL_MASK, OCV_TOL_NO_OCV);
1596}
1597
1598static int reset_bms_for_test(struct qpnp_bms_chip *chip)
1599{
Xiaozhe Shi95da77f2013-02-20 13:40:06 -08001600 int ibat_ua = 0, vbat_uv = 0, rc;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001601 int ocv_est_uv;
1602
1603 if (!chip) {
1604 pr_err("BMS driver has not been initialized yet!\n");
1605 return -EINVAL;
1606 }
1607
1608 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
1609
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07001610 /*
1611 * Don't include rbatt and rbatt_capacitative since we expect this to
1612 * be used with a fake battery which does not have internal resistances
1613 */
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001614 ocv_est_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
1615 pr_debug("forcing ocv to be %d due to bms reset mode\n", ocv_est_uv);
1616 chip->last_ocv_uv = ocv_est_uv;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001617 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001618 chip->last_soc = -EINVAL;
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08001619 chip->last_soc_invalid = true;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07001620 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001621 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07001622 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07001623 chip->software_shdw_cc_uah = 0;
Xiaozhe Shi20640b52013-01-03 11:49:30 -08001624 chip->last_cc_uah = INT_MIN;
1625 stop_ocv_updates(chip);
1626
1627 pr_debug("bms reset to ocv = %duv vbat_ua = %d ibat_ua = %d\n",
1628 chip->last_ocv_uv, vbat_uv, ibat_ua);
1629
1630 return rc;
1631}
1632
1633static int bms_reset_set(const char *val, const struct kernel_param *kp)
1634{
1635 int rc;
1636
1637 rc = param_set_bool(val, kp);
1638 if (rc) {
1639 pr_err("Unable to set bms_reset: %d\n", rc);
1640 return rc;
1641 }
1642
1643 if (*(bool *)kp->arg) {
1644 struct power_supply *bms_psy = power_supply_get_by_name("bms");
1645 struct qpnp_bms_chip *chip = container_of(bms_psy,
1646 struct qpnp_bms_chip, bms_psy);
1647
1648 rc = reset_bms_for_test(chip);
1649 if (rc) {
1650 pr_err("Unable to modify bms_reset: %d\n", rc);
1651 return rc;
1652 }
1653 }
1654 return 0;
1655}
1656
1657static struct kernel_param_ops bms_reset_ops = {
1658 .set = bms_reset_set,
1659 .get = param_get_bool,
1660};
1661
1662module_param_cb(bms_reset, &bms_reset_ops, &bms_reset, 0644);
1663
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001664#define SOC_STORAGE_MASK 0xFE
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001665static void backup_soc_and_iavg(struct qpnp_bms_chip *chip, int batt_temp,
1666 int soc)
1667{
1668 u8 temp;
1669 int rc;
1670 int iavg_ma = chip->prev_uuc_iavg_ma;
1671
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07001672 if (iavg_ma > MIN_IAVG_MA)
1673 temp = (iavg_ma - MIN_IAVG_MA) / IAVG_STEP_SIZE_MA;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001674 else
1675 temp = 0;
1676
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001677 rc = qpnp_write_wrapper(chip, &temp, chip->base + IAVG_STORAGE_REG, 1);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001678
1679 /* don't store soc if temperature is below 5degC */
1680 if (batt_temp > IGNORE_SOC_TEMP_DECIDEG)
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07001681 qpnp_masked_write_base(chip, chip->soc_storage_addr,
1682 SOC_STORAGE_MASK, (soc + 1) << 1);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001683}
1684
1685static int scale_soc_while_chg(struct qpnp_bms_chip *chip, int chg_time_sec,
1686 int catch_up_sec, int new_soc, int prev_soc)
1687{
1688 int scaled_soc;
1689 int numerator;
1690
1691 /*
1692 * Don't report a high value immediately slowly scale the
1693 * value from prev_soc to the new soc based on a charge time
1694 * weighted average
1695 */
1696 pr_debug("cts = %d catch_up_sec = %d\n", chg_time_sec, catch_up_sec);
1697 if (catch_up_sec == 0)
1698 return new_soc;
1699
1700 if (chg_time_sec > catch_up_sec)
1701 return new_soc;
1702
1703 numerator = (catch_up_sec - chg_time_sec) * prev_soc
1704 + chg_time_sec * new_soc;
1705 scaled_soc = numerator / catch_up_sec;
1706
1707 pr_debug("cts = %d new_soc = %d prev_soc = %d scaled_soc = %d\n",
1708 chg_time_sec, new_soc, prev_soc, scaled_soc);
1709
1710 return scaled_soc;
1711}
1712
1713/*
1714 * bms_fake_battery is set in setups where a battery emulator is used instead
1715 * of a real battery. This makes the bms driver report a different/fake value
1716 * regardless of the calculated state of charge.
1717 */
1718static int bms_fake_battery = -EINVAL;
1719module_param(bms_fake_battery, int, 0644);
1720
1721static int report_voltage_based_soc(struct qpnp_bms_chip *chip)
1722{
1723 pr_debug("Reported voltage based soc = %d\n",
1724 chip->prev_voltage_based_soc);
1725 return chip->prev_voltage_based_soc;
1726}
1727
1728#define SOC_CATCHUP_SEC_MAX 600
1729#define SOC_CATCHUP_SEC_PER_PERCENT 60
1730#define MAX_CATCHUP_SOC (SOC_CATCHUP_SEC_MAX / SOC_CATCHUP_SEC_PER_PERCENT)
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07001731#define SOC_CHANGE_PER_SEC 5
Xiaozhe Shi27375822013-08-22 11:40:15 -07001732#define REPORT_SOC_WAIT_MS 10000
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001733static int report_cc_based_soc(struct qpnp_bms_chip *chip)
1734{
1735 int soc, soc_change;
1736 int time_since_last_change_sec, charge_time_sec = 0;
1737 unsigned long last_change_sec;
1738 struct timespec now;
1739 struct qpnp_vadc_result result;
1740 int batt_temp;
1741 int rc;
1742 bool charging, charging_since_last_report;
1743
Xiaozhe Shi27375822013-08-22 11:40:15 -07001744 rc = wait_event_interruptible_timeout(chip->bms_wait_queue,
1745 chip->calculated_soc != -EINVAL,
1746 round_jiffies_relative(msecs_to_jiffies
1747 (REPORT_SOC_WAIT_MS)));
1748
1749 if (rc == 0 && chip->calculated_soc == -EINVAL) {
1750 pr_debug("calculate soc timed out\n");
1751 } else if (rc == -ERESTARTSYS) {
1752 pr_err("Wait for SoC interrupted.\n");
1753 return rc;
1754 }
1755
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07001756 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001757
1758 if (rc) {
1759 pr_err("error reading adc channel = %d, rc = %d\n",
1760 LR_MUX1_BATT_THERM, rc);
1761 return rc;
1762 }
1763 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
1764 result.measurement);
1765 batt_temp = (int)result.physical;
1766
1767 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shifa6ea692013-05-31 11:15:13 -07001768 soc = chip->calculated_soc;
1769
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001770 last_change_sec = chip->last_soc_change_sec;
1771 calculate_delta_time(&last_change_sec, &time_since_last_change_sec);
1772
1773 charging = is_battery_charging(chip);
1774 charging_since_last_report = charging || (chip->last_soc_unbound
1775 && chip->was_charging_at_sleep);
1776 /*
1777 * account for charge time - limit it to SOC_CATCHUP_SEC to
1778 * avoid overflows when charging continues for extended periods
1779 */
1780 if (charging) {
1781 if (chip->charge_start_tm_sec == 0) {
1782 /*
1783 * calculating soc for the first time
1784 * after start of chg. Initialize catchup time
1785 */
1786 if (abs(soc - chip->last_soc) < MAX_CATCHUP_SOC)
1787 chip->catch_up_time_sec =
1788 (soc - chip->last_soc)
1789 * SOC_CATCHUP_SEC_PER_PERCENT;
1790 else
1791 chip->catch_up_time_sec = SOC_CATCHUP_SEC_MAX;
1792
1793 if (chip->catch_up_time_sec < 0)
1794 chip->catch_up_time_sec = 0;
1795 chip->charge_start_tm_sec = last_change_sec;
1796 }
1797
1798 charge_time_sec = min(SOC_CATCHUP_SEC_MAX, (int)last_change_sec
1799 - chip->charge_start_tm_sec);
1800
1801 /* end catchup if calculated soc and last soc are same */
1802 if (chip->last_soc == soc)
1803 chip->catch_up_time_sec = 0;
1804 }
1805
1806 if (chip->last_soc != -EINVAL) {
1807 /*
1808 * last_soc < soc ... if we have not been charging at all
1809 * since the last time this was called, report previous SoC.
1810 * Otherwise, scale and catch up.
1811 */
1812 if (chip->last_soc < soc && !charging_since_last_report)
1813 soc = chip->last_soc;
1814 else if (chip->last_soc < soc && soc != 100)
1815 soc = scale_soc_while_chg(chip, charge_time_sec,
1816 chip->catch_up_time_sec,
1817 soc, chip->last_soc);
1818
1819 soc_change = min((int)abs(chip->last_soc - soc),
1820 time_since_last_change_sec / SOC_CHANGE_PER_SEC);
1821 if (chip->last_soc_unbound) {
1822 chip->last_soc_unbound = false;
1823 } else {
1824 /*
1825 * if soc have not been unbound by resume,
1826 * only change reported SoC by 1.
1827 */
1828 soc_change = min(1, soc_change);
1829 }
1830
1831 if (soc < chip->last_soc && soc != 0)
1832 soc = chip->last_soc - soc_change;
1833 if (soc > chip->last_soc && soc != 100)
1834 soc = chip->last_soc + soc_change;
1835 }
1836
Xiaozhe Shi208b8e52013-05-28 10:16:32 -07001837 if (chip->last_soc != soc && !chip->last_soc_unbound)
Xiaozhe Shi83484e32013-05-16 10:59:59 -07001838 chip->last_soc_change_sec = last_change_sec;
1839
1840 pr_debug("last_soc = %d, calculated_soc = %d, soc = %d, time since last change = %d\n",
1841 chip->last_soc, chip->calculated_soc,
1842 soc, time_since_last_change_sec);
1843 chip->last_soc = bound_soc(soc);
1844 backup_soc_and_iavg(chip, batt_temp, chip->last_soc);
1845 pr_debug("Reported SOC = %d\n", chip->last_soc);
1846 chip->t_soc_queried = now;
1847 mutex_unlock(&chip->last_soc_mutex);
1848
1849 return soc;
1850}
1851
1852static int report_state_of_charge(struct qpnp_bms_chip *chip)
1853{
1854 if (bms_fake_battery != -EINVAL) {
1855 pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery);
1856 return bms_fake_battery;
1857 } else if (chip->use_voltage_soc)
1858 return report_voltage_based_soc(chip);
1859 else
1860 return report_cc_based_soc(chip);
1861}
1862
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07001863#define VDD_MAX_ERR 5000
1864#define VDD_STEP_SIZE 10000
1865#define MAX_COUNT_BEFORE_RESET_TO_CC 3
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001866static int charging_adjustments(struct qpnp_bms_chip *chip,
1867 struct soc_params *params, int soc,
1868 int vbat_uv, int ibat_ua, int batt_temp)
1869{
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001870 int chg_soc, soc_ibat, batt_terminal_uv, weight_ibat, weight_cc;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001871
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001872 batt_terminal_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001873
1874 if (chip->soc_at_cv == -EINVAL) {
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001875 if (batt_terminal_uv >= chip->max_voltage_uv - VDD_MAX_ERR) {
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001876 chip->soc_at_cv = soc;
1877 chip->prev_chg_soc = soc;
1878 chip->ibat_at_cv_ua = ibat_ua;
1879 pr_debug("CC_TO_CV ibat_ua = %d CHG SOC %d\n",
1880 ibat_ua, soc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001881 } else {
1882 /* In constant current charging return the calc soc */
1883 pr_debug("CC CHG SOC %d\n", soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001884 }
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001885
1886 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07001887 chip->system_load_count = 0;
1888 return soc;
1889 } else if (ibat_ua > 0 && batt_terminal_uv
1890 < chip->max_voltage_uv - (VDD_MAX_ERR * 2)) {
1891 if (chip->system_load_count > MAX_COUNT_BEFORE_RESET_TO_CC) {
1892 chip->soc_at_cv = -EINVAL;
1893 pr_debug("Vbat below CV threshold, resetting CC_TO_CV\n");
1894 chip->system_load_count = 0;
1895 } else {
1896 chip->system_load_count += 1;
1897 pr_debug("Vbat below CV threshold, count: %d\n",
1898 chip->system_load_count);
1899 }
1900 return soc;
1901 } else if (ibat_ua > 0) {
1902 pr_debug("NOT CHARGING SOC %d\n", soc);
1903 chip->system_load_count = 0;
1904 chip->prev_chg_soc = soc;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001905 return soc;
1906 }
1907
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07001908 chip->system_load_count = 0;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001909 /*
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001910 * battery is in CV phase - begin linear interpolation of soc based on
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001911 * battery charge current
1912 */
1913
1914 /*
1915 * if voltage lessened (possibly because of a system load)
1916 * keep reporting the prev chg soc
1917 */
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001918 if (batt_terminal_uv <= chip->prev_batt_terminal_uv - VDD_STEP_SIZE) {
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08001919 pr_debug("batt_terminal_uv %d < (max = %d - 10000); CC CHG SOC %d\n",
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001920 batt_terminal_uv, chip->prev_batt_terminal_uv,
1921 chip->prev_chg_soc);
1922 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001923 return chip->prev_chg_soc;
1924 }
1925
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001926 soc_ibat = bound_soc(linear_interpolate(chip->soc_at_cv,
1927 chip->ibat_at_cv_ua,
Abhijeet Dharmapurikareef88662012-11-08 17:26:29 -08001928 100, -1 * chip->chg_term_ua,
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001929 ibat_ua));
1930 weight_ibat = bound_soc(linear_interpolate(1, chip->soc_at_cv,
1931 100, 100, chip->prev_chg_soc));
1932 weight_cc = 100 - weight_ibat;
Xiaozhe Shieabcebf2013-05-28 10:41:09 -07001933 chg_soc = bound_soc(DIV_ROUND_CLOSEST(soc_ibat * weight_ibat
1934 + weight_cc * soc, 100));
1935
Xiaozhe Shifd98ddf2013-05-20 15:20:19 -07001936 pr_debug("weight_ibat = %d, weight_cc = %d, soc_ibat = %d, soc_cc = %d\n",
1937 weight_ibat, weight_cc, soc_ibat, soc);
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001938
1939 /* always report a higher soc */
1940 if (chg_soc > chip->prev_chg_soc) {
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001941 chip->prev_chg_soc = chg_soc;
1942
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07001943 chip->charging_adjusted_ocv = find_ocv_for_pc(chip, batt_temp,
1944 find_pc_for_soc(chip, params, chg_soc));
1945 pr_debug("CC CHG ADJ OCV = %d CHG SOC %d\n",
1946 chip->charging_adjusted_ocv,
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001947 chip->prev_chg_soc);
1948 }
1949
1950 pr_debug("Reporting CHG SOC %d\n", chip->prev_chg_soc);
Xiaozhe Shifc2f5a02013-01-28 15:09:04 -08001951 chip->prev_batt_terminal_uv = batt_terminal_uv;
Xiaozhe Shi41bc1f12012-09-26 16:55:22 -07001952 return chip->prev_chg_soc;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07001953}
1954
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001955static void very_low_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
1956{
1957 /*
1958 * if battery is very low (v_cutoff voltage + 20mv) hold
1959 * a wakelock untill soc = 0%
1960 */
1961 if (vbat_uv <= chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001962 && !wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001963 pr_debug("voltage = %d low holding wakelock\n", vbat_uv);
1964 wake_lock(&chip->low_voltage_wake_lock);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001965 } else if (vbat_uv > chip->low_voltage_threshold
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001966 && wake_lock_active(&chip->low_voltage_wake_lock)) {
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001967 pr_debug("voltage = %d releasing wakelock\n", vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08001968 wake_unlock(&chip->low_voltage_wake_lock);
1969 }
1970}
1971
Xiaozhe Shia6618a22013-03-27 10:26:29 -07001972#define VBATT_ERROR_MARGIN 20000
1973static void cv_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv)
1974{
1975 /*
1976 * if battery is very low (v_cutoff voltage + 20mv) hold
1977 * a wakelock untill soc = 0%
1978 */
1979 if (wake_lock_active(&chip->cv_wake_lock)) {
1980 if (chip->soc_at_cv != -EINVAL) {
1981 pr_debug("hit CV, releasing cv wakelock\n");
1982 wake_unlock(&chip->cv_wake_lock);
1983 } else if (!is_battery_charging(chip)) {
1984 pr_debug("charging stopped, releasing cv wakelock\n");
1985 wake_unlock(&chip->cv_wake_lock);
1986 }
1987 } else if (vbat_uv > chip->max_voltage_uv - VBATT_ERROR_MARGIN
1988 && chip->soc_at_cv == -EINVAL
1989 && is_battery_charging(chip)
1990 && !wake_lock_active(&chip->cv_wake_lock)) {
1991 pr_debug("voltage = %d holding cv wakelock\n", vbat_uv);
1992 wake_lock(&chip->cv_wake_lock);
1993 }
1994}
1995
Xiaozhe Shi2b647872013-10-31 14:30:27 -07001996#define NO_ADJUST_HIGH_SOC_THRESHOLD 98
Xiaozhe Shie118c692012-09-24 15:17:43 -07001997static int adjust_soc(struct qpnp_bms_chip *chip, struct soc_params *params,
1998 int soc, int batt_temp)
1999{
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002000 int ibat_ua = 0, vbat_uv = 0;
2001 int ocv_est_uv = 0, soc_est = 0, pc_est = 0, pc = 0;
2002 int delta_ocv_uv = 0;
2003 int n = 0;
2004 int rc_new_uah = 0;
2005 int pc_new = 0;
2006 int soc_new = 0;
2007 int slope = 0;
2008 int rc = 0;
2009 int delta_ocv_uv_limit = 0;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002010 int correction_limit_uv = 0;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002011
2012 rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv);
2013 if (rc < 0) {
2014 pr_err("simultaneous vbat ibat failed err = %d\n", rc);
2015 goto out;
2016 }
2017
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002018 very_low_voltage_check(chip, vbat_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002019 cv_voltage_check(chip, vbat_uv);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08002020
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002021 delta_ocv_uv_limit = DIV_ROUND_CLOSEST(ibat_ua, 1000);
2022
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08002023 ocv_est_uv = vbat_uv + (ibat_ua * params->rbatt_mohm)/1000;
2024
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002025 pc_est = calculate_pc(chip, ocv_est_uv, batt_temp);
2026 soc_est = div_s64((s64)params->fcc_uah * pc_est - params->uuc_uah*100,
2027 (s64)params->fcc_uah - params->uuc_uah);
2028 soc_est = bound_soc(soc_est);
2029
Xiaozhe Shi20640b52013-01-03 11:49:30 -08002030 /* never adjust during bms reset mode */
2031 if (bms_reset) {
2032 pr_debug("bms reset mode, SOC adjustment skipped\n");
2033 goto out;
2034 }
2035
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07002036 if (is_battery_charging(chip)) {
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002037 soc = charging_adjustments(chip, params, soc, vbat_uv, ibat_ua,
2038 batt_temp);
Xiaozhe Shiee39e5d2013-10-07 13:56:04 -07002039 /* Skip adjustments if we are in CV or ibat is negative */
2040 if (chip->soc_at_cv != -EINVAL || ibat_ua < 0)
2041 goto out;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002042 }
2043
2044 /*
2045 * do not adjust
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07002046 * if soc_est is same as what bms calculated
2047 * OR if soc_est > adjust_soc_low_threshold
2048 * OR if soc is above 90
2049 * because we might pull it low
2050 * and cause a bad user experience
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002051 */
2052 if (soc_est == soc
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07002053 || soc_est > chip->adjust_soc_low_threshold
2054 || soc >= NO_ADJUST_HIGH_SOC_THRESHOLD)
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002055 goto out;
2056
2057 if (chip->last_soc_est == -EINVAL)
2058 chip->last_soc_est = soc;
2059
2060 n = min(200, max(1 , soc + soc_est + chip->last_soc_est));
2061 chip->last_soc_est = soc_est;
2062
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002063 pc = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002064 if (pc > 0) {
2065 pc_new = calculate_pc(chip,
2066 chip->last_ocv_uv - (++slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002067 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002068 while (pc_new == pc) {
2069 /* start taking 10mV steps */
2070 slope = slope + 10;
2071 pc_new = calculate_pc(chip,
2072 chip->last_ocv_uv - (slope * 1000),
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002073 chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002074 }
2075 } else {
2076 /*
2077 * pc is already at the lowest point,
2078 * assume 1 millivolt translates to 1% pc
2079 */
2080 pc = 1;
2081 pc_new = 0;
2082 slope = 1;
2083 }
2084
2085 delta_ocv_uv = div_s64((soc - soc_est) * (s64)slope * 1000,
2086 n * (pc - pc_new));
2087
2088 if (abs(delta_ocv_uv) > delta_ocv_uv_limit) {
2089 pr_debug("limiting delta ocv %d limit = %d\n", delta_ocv_uv,
2090 delta_ocv_uv_limit);
2091
2092 if (delta_ocv_uv > 0)
2093 delta_ocv_uv = delta_ocv_uv_limit;
2094 else
2095 delta_ocv_uv = -1 * delta_ocv_uv_limit;
2096 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
2097 }
2098
Xiaozhe Shi08d9aa72013-07-31 17:15:12 -07002099 if (wake_lock_active(&chip->low_voltage_wake_lock))
2100 goto skip_limits;
2101
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002102 if (chip->last_ocv_uv > chip->flat_ocv_threshold_uv)
2103 correction_limit_uv = chip->high_ocv_correction_limit_uv;
2104 else
2105 correction_limit_uv = chip->low_ocv_correction_limit_uv;
2106
2107 if (abs(delta_ocv_uv) > correction_limit_uv) {
2108 pr_debug("limiting delta ocv %d limit = %d\n",
2109 delta_ocv_uv, correction_limit_uv);
2110 if (delta_ocv_uv > 0)
2111 delta_ocv_uv = correction_limit_uv;
2112 else
2113 delta_ocv_uv = -correction_limit_uv;
2114 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
2115 }
2116
Xiaozhe Shi08d9aa72013-07-31 17:15:12 -07002117skip_limits:
2118
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002119 chip->last_ocv_uv -= delta_ocv_uv;
2120
2121 if (chip->last_ocv_uv >= chip->max_voltage_uv)
2122 chip->last_ocv_uv = chip->max_voltage_uv;
2123
2124 /* calculate the soc based on this new ocv */
Abhijeet Dharmapurikar4b97cdd2012-12-26 21:10:53 -08002125 pc_new = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002126 rc_new_uah = (params->fcc_uah * pc_new) / 100;
2127 soc_new = (rc_new_uah - params->cc_uah - params->uuc_uah)*100
2128 / (params->fcc_uah - params->uuc_uah);
2129 soc_new = bound_soc(soc_new);
2130
2131 /*
2132 * if soc_new is ZERO force it higher so that phone doesnt report soc=0
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002133 * soc = 0 should happen only when soc_est is above a set value
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002134 */
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07002135 if (soc_new == 0 && soc_est >= chip->hold_soc_est)
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002136 soc_new = 1;
2137
2138 soc = soc_new;
2139
2140out:
2141 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",
2142 ibat_ua, vbat_uv, ocv_est_uv, pc_est,
2143 soc_est, n, delta_ocv_uv, chip->last_ocv_uv,
Xiaozhe Shi904f1f72012-12-04 12:47:21 -08002144 pc_new, soc_new, params->rbatt_mohm, slope);
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07002145
Xiaozhe Shie118c692012-09-24 15:17:43 -07002146 return soc;
2147}
2148
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002149static int clamp_soc_based_on_voltage(struct qpnp_bms_chip *chip, int soc)
2150{
2151 int rc, vbat_uv;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002152
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002153 rc = get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shi36458962013-02-06 16:19:57 -08002154 if (rc < 0) {
2155 pr_err("adc vbat failed err = %d\n", rc);
2156 return soc;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002157 }
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002158 if (soc == 0 && vbat_uv > chip->v_cutoff_uv) {
2159 pr_debug("clamping soc to 1, vbat (%d) > cutoff (%d)\n",
2160 vbat_uv, chip->v_cutoff_uv);
2161 return 1;
Xiaozhe Shi2542c602012-11-28 10:08:07 -08002162 } else {
2163 pr_debug("not clamping, using soc = %d, vbat = %d and cutoff = %d\n",
2164 soc, vbat_uv, chip->v_cutoff_uv);
2165 return soc;
2166 }
2167}
2168
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002169static int64_t convert_cc_uah_to_raw(struct qpnp_bms_chip *chip, int64_t cc_uah)
2170{
2171 int64_t cc_uv, cc_pvh, cc_raw;
2172
2173 cc_pvh = cc_uah * chip->r_sense_uohm;
2174 cc_uv = div_s64(cc_pvh * SLEEP_CLK_HZ * SECONDS_PER_HOUR,
2175 CC_READING_TICKS * 1000000LL);
2176 cc_raw = div_s64(cc_uv * CC_READING_RESOLUTION_D,
2177 CC_READING_RESOLUTION_N);
2178 return cc_raw;
2179}
2180
2181#define CC_STEP_INCREMENT_UAH 1500
2182#define OCV_STEP_INCREMENT 0x10
2183static void configure_soc_wakeup(struct qpnp_bms_chip *chip,
2184 struct soc_params *params,
2185 int batt_temp, int target_soc)
2186{
2187 int target_ocv_uv;
2188 int64_t target_cc_uah, cc_raw_64, current_shdw_cc_raw_64;
2189 int64_t current_shdw_cc_uah, iadc_comp_factor;
2190 uint64_t cc_raw, current_shdw_cc_raw;
2191 int16_t ocv_raw, current_ocv_raw;
2192
2193 current_shdw_cc_raw = 0;
2194 mutex_lock(&chip->bms_output_lock);
2195 lock_output_data(chip);
2196 qpnp_read_wrapper(chip, (u8 *)&current_ocv_raw,
2197 chip->base + BMS1_OCV_FOR_SOC_DATA0, 2);
2198 unlock_output_data(chip);
2199 mutex_unlock(&chip->bms_output_lock);
2200 current_shdw_cc_uah = get_prop_bms_charge_counter_shadow(chip);
2201 current_shdw_cc_raw_64 = convert_cc_uah_to_raw(chip,
2202 current_shdw_cc_uah);
2203
2204 /*
2205 * Calculate the target shadow coulomb counter threshold for when
2206 * the SoC changes.
2207 *
2208 * Since the BMS driver resets the shadow coulomb counter every
2209 * 20 seconds when the device is awake, calculate the threshold as
2210 * a delta from the current shadow coulomb count.
2211 */
2212 target_cc_uah = (100 - target_soc)
2213 * (params->fcc_uah - params->uuc_uah)
2214 / 100 - current_shdw_cc_uah;
2215 if (target_cc_uah < 0) {
2216 /*
2217 * If the target cc is below 0, that means we have already
2218 * passed the point where SoC should have fallen.
2219 * Set a wakeup in a few more mAh and check back again
2220 */
2221 target_cc_uah = CC_STEP_INCREMENT_UAH;
2222 }
2223 iadc_comp_factor = 100000;
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002224 qpnp_iadc_comp_result(chip->iadc_dev, &iadc_comp_factor);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002225 target_cc_uah = div64_s64(target_cc_uah * 100000, iadc_comp_factor);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002226 target_cc_uah = cc_reverse_adjust_for_gain(chip, target_cc_uah);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002227 cc_raw_64 = convert_cc_uah_to_raw(chip, target_cc_uah);
2228 cc_raw = convert_s64_to_s36(cc_raw_64);
2229
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002230 target_ocv_uv = find_ocv_for_pc(chip, batt_temp,
2231 find_pc_for_soc(chip, params, target_soc));
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002232 ocv_raw = convert_vbatt_uv_to_raw(chip, target_ocv_uv);
2233
2234 /*
2235 * If the current_ocv_raw was updated since reaching 100% and is lower
2236 * than the calculated target ocv threshold, set the new target
2237 * threshold 1.5mAh lower in order to check if the SoC changed yet.
2238 */
2239 if (current_ocv_raw != chip->ocv_reading_at_100
2240 && current_ocv_raw < ocv_raw)
2241 ocv_raw = current_ocv_raw - OCV_STEP_INCREMENT;
2242
2243 qpnp_write_wrapper(chip, (u8 *)&cc_raw,
2244 chip->base + BMS1_SW_CC_THR0, 5);
2245 qpnp_write_wrapper(chip, (u8 *)&ocv_raw,
2246 chip->base + BMS1_OCV_THR0, 2);
2247
2248 pr_debug("current sw_cc_raw = 0x%llx, current ocv = 0x%hx\n",
2249 current_shdw_cc_raw, (uint16_t)current_ocv_raw);
2250 pr_debug("target_cc_uah = %lld, raw64 = 0x%llx, raw 36 = 0x%llx, ocv_raw = 0x%hx\n",
2251 target_cc_uah,
2252 (uint64_t)cc_raw_64, cc_raw,
2253 (uint16_t)ocv_raw);
2254}
2255
Xiaozhe Shi5e791032013-10-25 11:30:42 -07002256#define BAD_SOC_THRESH -10
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002257static int calculate_raw_soc(struct qpnp_bms_chip *chip,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002258 struct raw_soc_params *raw,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002259 struct soc_params *params,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002260 int batt_temp)
2261{
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002262 int soc, remaining_usable_charge_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002263
Xiaozhe Shie118c692012-09-24 15:17:43 -07002264 /* calculate remaining usable charge */
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002265 remaining_usable_charge_uah = params->ocv_charge_uah
2266 - params->cc_uah
2267 - params->uuc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002268 pr_debug("RUC = %duAh\n", remaining_usable_charge_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002269
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002270 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002271 (params->fcc_uah - params->uuc_uah));
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002272
Xiaozhe Shi5e791032013-10-25 11:30:42 -07002273 if (chip->first_time_calc_soc && soc > BAD_SOC_THRESH && soc < 0) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07002274 /*
2275 * first time calcualtion and the pon ocv is too low resulting
2276 * in a bad soc. Adjust ocv to get 0 soc
2277 */
2278 pr_debug("soc is %d, adjusting pon ocv to make it 0\n", soc);
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002279 chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp,
2280 find_pc_for_soc(chip, params, 0));
2281 params->ocv_charge_uah = find_ocv_charge_for_soc(chip,
2282 params, 0);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002283
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002284 remaining_usable_charge_uah = params->ocv_charge_uah
2285 - params->cc_uah
2286 - params->uuc_uah;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002287
2288 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002289 (params->fcc_uah
2290 - params->uuc_uah));
Xiaozhe Shie118c692012-09-24 15:17:43 -07002291 pr_debug("DONE for O soc is %d, pon ocv adjusted to %duV\n",
2292 soc, chip->last_ocv_uv);
2293 }
2294
2295 if (soc > 100)
2296 soc = 100;
2297
Xiaozhe Shi5e791032013-10-25 11:30:42 -07002298 if (soc > BAD_SOC_THRESH && soc < 0) {
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002299 pr_debug("bad rem_usb_chg = %d rem_chg %d, cc_uah %d, unusb_chg %d\n",
Xiaozhe Shie118c692012-09-24 15:17:43 -07002300 remaining_usable_charge_uah,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002301 params->ocv_charge_uah,
2302 params->cc_uah, params->uuc_uah);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002303
Xiaozhe Shicb386a22012-11-29 12:11:42 -08002304 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 -07002305 chip->last_ocv_uv, batt_temp,
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002306 params->fcc_uah, soc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002307 soc = 0;
2308 }
2309
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002310 return soc;
2311}
2312
2313#define SLEEP_RECALC_INTERVAL 3
2314static int calculate_state_of_charge(struct qpnp_bms_chip *chip,
2315 struct raw_soc_params *raw,
2316 int batt_temp)
2317{
2318 struct soc_params params;
2319 int soc, previous_soc, shutdown_soc, new_calculated_soc;
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002320 int remaining_usable_charge_uah;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002321
2322 calculate_soc_params(chip, raw, &params, batt_temp);
2323 if (!is_battery_present(chip)) {
2324 pr_debug("battery gone, reporting 100\n");
2325 new_calculated_soc = 100;
2326 goto done_calculating;
2327 }
2328
2329 if (params.fcc_uah - params.uuc_uah <= 0) {
2330 pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
2331 params.fcc_uah,
2332 params.uuc_uah);
2333 new_calculated_soc = 0;
2334 goto done_calculating;
2335 }
2336
2337 soc = calculate_raw_soc(chip, raw, &params, batt_temp);
2338
Xiaozhe Shie118c692012-09-24 15:17:43 -07002339 mutex_lock(&chip->soc_invalidation_mutex);
2340 shutdown_soc = chip->shutdown_soc;
2341
2342 if (chip->first_time_calc_soc && soc != shutdown_soc
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002343 && !chip->shutdown_soc_invalid) {
Xiaozhe Shie118c692012-09-24 15:17:43 -07002344 /*
2345 * soc for the first time - use shutdown soc
2346 * to adjust pon ocv since it is a small percent away from
2347 * the real soc
2348 */
2349 pr_debug("soc = %d before forcing shutdown_soc = %d\n",
2350 soc, shutdown_soc);
Xiaozhe Shid9ffdaf2013-09-19 16:23:21 -07002351 chip->last_ocv_uv = find_ocv_for_pc(chip, batt_temp,
2352 find_pc_for_soc(chip, &params, shutdown_soc));
2353 params.ocv_charge_uah = find_ocv_charge_for_soc(chip,
2354 &params, shutdown_soc);
Xiaozhe Shie118c692012-09-24 15:17:43 -07002355
2356 remaining_usable_charge_uah = params.ocv_charge_uah
2357 - params.cc_uah
2358 - params.uuc_uah;
2359
2360 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2361 (params.fcc_uah
2362 - params.uuc_uah));
2363
2364 pr_debug("DONE for shutdown_soc = %d soc is %d, adjusted ocv to %duV\n",
2365 shutdown_soc, soc, chip->last_ocv_uv);
2366 }
2367 mutex_unlock(&chip->soc_invalidation_mutex);
2368
2369 pr_debug("SOC before adjustment = %d\n", soc);
2370 new_calculated_soc = adjust_soc(chip, &params, soc, batt_temp);
2371
Xiaozhe Shi445d2492013-03-27 18:10:18 -07002372 /* always clamp soc due to BMS hw/sw immaturities */
2373 new_calculated_soc = clamp_soc_based_on_voltage(chip,
2374 new_calculated_soc);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002375 /*
2376 * If the battery is full, configure the cc threshold so the system
2377 * wakes up after SoC changes
2378 */
2379 if (is_battery_full(chip))
2380 configure_soc_wakeup(chip, &params,
2381 batt_temp, bound_soc(new_calculated_soc - 1));
Xiaozhe Shifd8cd482013-02-12 10:00:38 -08002382done_calculating:
Xiaozhe Shifa6ea692013-05-31 11:15:13 -07002383 mutex_lock(&chip->last_soc_mutex);
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07002384 previous_soc = chip->calculated_soc;
Xiaozhe Shie118c692012-09-24 15:17:43 -07002385 chip->calculated_soc = new_calculated_soc;
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002386 pr_debug("CC based calculated SOC = %d\n", chip->calculated_soc);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002387 if (chip->last_soc_invalid) {
2388 chip->last_soc_invalid = false;
2389 chip->last_soc = -EINVAL;
2390 }
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002391 /*
2392 * Check if more than a long time has passed since the last
2393 * calculation (more than n times compared to the soc recalculation
2394 * rate, where n is defined by SLEEP_RECALC_INTERVAL). If this is true,
2395 * then the system must have gone through a long sleep, and SoC can be
2396 * allowed to become unbounded by the last reported SoC
2397 */
2398 if (params.delta_time_s * 1000 >
2399 chip->calculate_soc_ms * SLEEP_RECALC_INTERVAL
2400 && !chip->first_time_calc_soc) {
2401 chip->last_soc_unbound = true;
2402 chip->last_soc_change_sec = chip->last_recalc_time;
2403 pr_debug("last_soc unbound because elapsed time = %d\n",
2404 params.delta_time_s);
2405 }
2406 mutex_unlock(&chip->last_soc_mutex);
Xiaozhe Shi27375822013-08-22 11:40:15 -07002407 wake_up_interruptible(&chip->bms_wait_queue);
Xiaozhe Shi83484e32013-05-16 10:59:59 -07002408
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002409 if (new_calculated_soc != previous_soc && chip->bms_psy_registered) {
Xiaozhe Shi83484e32013-05-16 10:59:59 -07002410 power_supply_changed(&chip->bms_psy);
2411 pr_debug("power supply changed\n");
2412 } else {
2413 /*
2414 * Call report state of charge anyways to periodically update
2415 * reported SoC. This prevents reported SoC from being stuck
2416 * when calculated soc doesn't change.
2417 */
2418 report_state_of_charge(chip);
2419 }
2420
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002421 get_current_time(&chip->last_recalc_time);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07002422 chip->first_time_calc_soc = 0;
Xiaozhe Shi70633922013-09-23 15:50:53 -07002423 chip->first_time_calc_uuc = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002424 return chip->calculated_soc;
2425}
2426
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002427static int calculate_soc_from_voltage(struct qpnp_bms_chip *chip)
2428{
2429 int voltage_range_uv, voltage_remaining_uv, voltage_based_soc;
Xiaozhe Shi36458962013-02-06 16:19:57 -08002430 int rc, vbat_uv;
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002431
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002432 rc = get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shi36458962013-02-06 16:19:57 -08002433 if (rc < 0) {
2434 pr_err("adc vbat failed err = %d\n", rc);
2435 return rc;
2436 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002437 voltage_range_uv = chip->max_voltage_uv - chip->v_cutoff_uv;
2438 voltage_remaining_uv = vbat_uv - chip->v_cutoff_uv;
2439 voltage_based_soc = voltage_remaining_uv * 100 / voltage_range_uv;
2440
2441 voltage_based_soc = clamp(voltage_based_soc, 0, 100);
2442
2443 if (chip->prev_voltage_based_soc != voltage_based_soc
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002444 && chip->bms_psy_registered) {
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002445 power_supply_changed(&chip->bms_psy);
2446 pr_debug("power supply changed\n");
2447 }
2448 chip->prev_voltage_based_soc = voltage_based_soc;
2449
2450 pr_debug("vbat used = %duv\n", vbat_uv);
2451 pr_debug("Calculated voltage based soc = %d\n", voltage_based_soc);
2452 return voltage_based_soc;
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08002453}
2454
Xiaozhe Shif9f99242013-08-29 12:27:50 -07002455static int recalculate_raw_soc(struct qpnp_bms_chip *chip)
2456{
2457 int batt_temp, rc, soc;
2458 struct qpnp_vadc_result result;
2459 struct raw_soc_params raw;
2460 struct soc_params params;
2461
2462 bms_stay_awake(&chip->soc_wake_source);
2463 if (chip->use_voltage_soc) {
2464 soc = calculate_soc_from_voltage(chip);
2465 } else {
2466 if (!chip->batfet_closed)
2467 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, true);
2468 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM,
2469 &result);
2470 if (rc) {
2471 pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n",
2472 LR_MUX1_BATT_THERM, rc);
2473 soc = chip->calculated_soc;
2474 } else {
2475 pr_debug("batt_temp phy = %lld meas = 0x%llx\n",
2476 result.physical,
2477 result.measurement);
2478 batt_temp = (int)result.physical;
2479
2480 mutex_lock(&chip->last_ocv_uv_mutex);
2481 read_soc_params_raw(chip, &raw, batt_temp);
2482 calculate_soc_params(chip, &raw, &params, batt_temp);
2483 if (!is_battery_present(chip)) {
2484 pr_debug("battery gone\n");
2485 soc = 0;
2486 } else if (params.fcc_uah - params.uuc_uah <= 0) {
2487 pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
2488 params.fcc_uah,
2489 params.uuc_uah);
2490 soc = 0;
2491 } else {
2492 soc = calculate_raw_soc(chip, &raw,
2493 &params, batt_temp);
2494 }
2495 mutex_unlock(&chip->last_ocv_uv_mutex);
2496 }
2497 }
2498 bms_relax(&chip->soc_wake_source);
2499 return soc;
2500}
2501
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002502static int recalculate_soc(struct qpnp_bms_chip *chip)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002503{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002504 int batt_temp, rc, soc;
2505 struct qpnp_vadc_result result;
2506 struct raw_soc_params raw;
2507
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002508 bms_stay_awake(&chip->soc_wake_source);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002509 mutex_lock(&chip->vbat_monitor_mutex);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002510 if (chip->vbat_monitor_params.state_request !=
2511 ADC_TM_HIGH_LOW_THR_DISABLE)
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002512 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2513 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002514 mutex_unlock(&chip->vbat_monitor_mutex);
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002515 if (chip->use_voltage_soc) {
2516 soc = calculate_soc_from_voltage(chip);
2517 } else {
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07002518 if (!chip->batfet_closed)
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07002519 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, true);
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002520 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM,
2521 &result);
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002522 if (rc) {
2523 pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n",
2524 LR_MUX1_BATT_THERM, rc);
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002525 soc = chip->calculated_soc;
2526 } else {
2527 pr_debug("batt_temp phy = %lld meas = 0x%llx\n",
2528 result.physical,
2529 result.measurement);
2530 batt_temp = (int)result.physical;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002531
Abhijeet Dharmapurikar713b60a2012-12-26 21:30:05 -08002532 mutex_lock(&chip->last_ocv_uv_mutex);
2533 read_soc_params_raw(chip, &raw, batt_temp);
2534 soc = calculate_state_of_charge(chip, &raw, batt_temp);
2535 mutex_unlock(&chip->last_ocv_uv_mutex);
2536 }
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08002537 }
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08002538 bms_relax(&chip->soc_wake_source);
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002539 return soc;
2540}
2541
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08002542static void recalculate_work(struct work_struct *work)
2543{
2544 struct qpnp_bms_chip *chip = container_of(work,
2545 struct qpnp_bms_chip,
2546 recalc_work);
2547
2548 recalculate_soc(chip);
2549}
2550
Xiaozhe Shicb487b12013-10-14 17:42:07 -07002551static int get_calculation_delay_ms(struct qpnp_bms_chip *chip)
2552{
2553 if (wake_lock_active(&chip->low_voltage_wake_lock))
2554 return chip->low_voltage_calculate_soc_ms;
2555 else if (chip->calculated_soc < chip->low_soc_calc_threshold)
2556 return chip->low_soc_calculate_soc_ms;
2557 else
2558 return chip->calculate_soc_ms;
2559}
2560
Xiaozhe Shicdeee312012-12-18 15:10:18 -08002561static void calculate_soc_work(struct work_struct *work)
2562{
2563 struct qpnp_bms_chip *chip = container_of(work,
2564 struct qpnp_bms_chip,
2565 calculate_soc_delayed_work.work);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002566
Xiaozhe Shicb487b12013-10-14 17:42:07 -07002567 recalculate_soc(chip);
2568 schedule_delayed_work(&chip->calculate_soc_delayed_work,
2569 round_jiffies_relative(msecs_to_jiffies
2570 (get_calculation_delay_ms(chip))));
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07002571}
2572
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002573static void configure_vbat_monitor_low(struct qpnp_bms_chip *chip)
2574{
2575 mutex_lock(&chip->vbat_monitor_mutex);
2576 if (chip->vbat_monitor_params.state_request
2577 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2578 /*
2579 * Battery is now around or below v_cutoff
2580 */
2581 pr_debug("battery entered cutoff range\n");
2582 if (!wake_lock_active(&chip->low_voltage_wake_lock)) {
2583 pr_debug("voltage low, holding wakelock\n");
2584 wake_lock(&chip->low_voltage_wake_lock);
2585 cancel_delayed_work_sync(
2586 &chip->calculate_soc_delayed_work);
2587 schedule_delayed_work(
2588 &chip->calculate_soc_delayed_work, 0);
2589 }
2590 chip->vbat_monitor_params.state_request =
2591 ADC_TM_HIGH_THR_ENABLE;
2592 chip->vbat_monitor_params.high_thr =
2593 (chip->low_voltage_threshold + VBATT_ERROR_MARGIN);
2594 pr_debug("set low thr to %d and high to %d\n",
2595 chip->vbat_monitor_params.low_thr,
2596 chip->vbat_monitor_params.high_thr);
2597 chip->vbat_monitor_params.low_thr = 0;
2598 } else if (chip->vbat_monitor_params.state_request
2599 == ADC_TM_LOW_THR_ENABLE) {
2600 /*
2601 * Battery is in normal operation range.
2602 */
2603 pr_debug("battery entered normal range\n");
2604 if (wake_lock_active(&chip->cv_wake_lock)) {
2605 wake_unlock(&chip->cv_wake_lock);
2606 pr_debug("releasing cv wake lock\n");
2607 }
2608 chip->in_cv_range = false;
2609 chip->vbat_monitor_params.state_request =
2610 ADC_TM_HIGH_LOW_THR_ENABLE;
2611 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2612 - VBATT_ERROR_MARGIN;
2613 chip->vbat_monitor_params.low_thr =
2614 chip->low_voltage_threshold;
2615 pr_debug("set low thr to %d and high to %d\n",
2616 chip->vbat_monitor_params.low_thr,
2617 chip->vbat_monitor_params.high_thr);
2618 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002619 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2620 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002621 mutex_unlock(&chip->vbat_monitor_mutex);
2622}
2623
2624#define CV_LOW_THRESHOLD_HYST_UV 100000
2625static void configure_vbat_monitor_high(struct qpnp_bms_chip *chip)
2626{
2627 mutex_lock(&chip->vbat_monitor_mutex);
2628 if (chip->vbat_monitor_params.state_request
2629 == ADC_TM_HIGH_LOW_THR_ENABLE) {
2630 /*
2631 * Battery is around vddmax
2632 */
2633 pr_debug("battery entered vddmax range\n");
2634 chip->in_cv_range = true;
2635 if (!wake_lock_active(&chip->cv_wake_lock)) {
2636 wake_lock(&chip->cv_wake_lock);
2637 pr_debug("holding cv wake lock\n");
2638 }
2639 schedule_work(&chip->recalc_work);
2640 chip->vbat_monitor_params.state_request =
2641 ADC_TM_LOW_THR_ENABLE;
2642 chip->vbat_monitor_params.low_thr =
2643 (chip->max_voltage_uv - CV_LOW_THRESHOLD_HYST_UV);
2644 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv * 2;
2645 pr_debug("set low thr to %d and high to %d\n",
2646 chip->vbat_monitor_params.low_thr,
2647 chip->vbat_monitor_params.high_thr);
2648 } else if (chip->vbat_monitor_params.state_request
2649 == ADC_TM_HIGH_THR_ENABLE) {
2650 /*
2651 * Battery is in normal operation range.
2652 */
2653 pr_debug("battery entered normal range\n");
2654 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2655 pr_debug("voltage high, releasing wakelock\n");
2656 wake_unlock(&chip->low_voltage_wake_lock);
2657 }
2658 chip->vbat_monitor_params.state_request =
2659 ADC_TM_HIGH_LOW_THR_ENABLE;
2660 chip->vbat_monitor_params.high_thr =
2661 chip->max_voltage_uv - VBATT_ERROR_MARGIN;
2662 chip->vbat_monitor_params.low_thr =
2663 chip->low_voltage_threshold;
2664 pr_debug("set low thr to %d and high to %d\n",
2665 chip->vbat_monitor_params.low_thr,
2666 chip->vbat_monitor_params.high_thr);
2667 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002668 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2669 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002670 mutex_unlock(&chip->vbat_monitor_mutex);
2671}
2672
2673static void btm_notify_vbat(enum qpnp_tm_state state, void *ctx)
2674{
2675 struct qpnp_bms_chip *chip = ctx;
2676 int vbat_uv;
2677 struct qpnp_vadc_result result;
2678 int rc;
2679
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002680 rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &result);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002681 pr_debug("vbat = %lld, raw = 0x%x\n", result.physical, result.adc_code);
2682
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07002683 get_battery_voltage(chip, &vbat_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002684 pr_debug("vbat is at %d, state is at %d\n", vbat_uv, state);
2685
2686 if (state == ADC_TM_LOW_STATE) {
2687 pr_debug("low voltage btm notification triggered\n");
2688 if (vbat_uv - VBATT_ERROR_MARGIN
2689 < chip->vbat_monitor_params.low_thr) {
2690 configure_vbat_monitor_low(chip);
2691 } else {
2692 pr_debug("faulty btm trigger, discarding\n");
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002693 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002694 &chip->vbat_monitor_params);
2695 }
2696 } else if (state == ADC_TM_HIGH_STATE) {
2697 pr_debug("high voltage btm notification triggered\n");
2698 if (vbat_uv + VBATT_ERROR_MARGIN
2699 > chip->vbat_monitor_params.high_thr) {
2700 configure_vbat_monitor_high(chip);
2701 } else {
2702 pr_debug("faulty btm trigger, discarding\n");
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002703 qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002704 &chip->vbat_monitor_params);
2705 }
2706 } else {
2707 pr_debug("unknown voltage notification state: %d\n", state);
2708 }
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07002709 if (chip->bms_psy_registered)
Xiaozhe Shifa120db2013-06-06 15:57:19 -07002710 power_supply_changed(&chip->bms_psy);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002711}
2712
2713static int reset_vbat_monitoring(struct qpnp_bms_chip *chip)
2714{
2715 int rc;
2716
2717 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_DISABLE;
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002718
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002719 rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2720 &chip->vbat_monitor_params);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002721 if (rc) {
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002722 pr_err("tm disable failed: %d\n", rc);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002723 return rc;
2724 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002725 if (wake_lock_active(&chip->low_voltage_wake_lock)) {
2726 pr_debug("battery removed, releasing wakelock\n");
2727 wake_unlock(&chip->low_voltage_wake_lock);
2728 }
2729 if (chip->in_cv_range) {
2730 pr_debug("battery removed, removing in_cv_range state\n");
2731 chip->in_cv_range = false;
2732 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002733 return 0;
2734}
2735
2736static int setup_vbat_monitoring(struct qpnp_bms_chip *chip)
2737{
2738 int rc;
2739
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002740 chip->vbat_monitor_params.low_thr = chip->low_voltage_threshold;
2741 chip->vbat_monitor_params.high_thr = chip->max_voltage_uv
2742 - VBATT_ERROR_MARGIN;
2743 chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_ENABLE;
2744 chip->vbat_monitor_params.channel = VBAT_SNS;
2745 chip->vbat_monitor_params.btm_ctx = (void *)chip;
2746 chip->vbat_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
2747 chip->vbat_monitor_params.threshold_notification = &btm_notify_vbat;
2748 pr_debug("set low thr to %d and high to %d\n",
2749 chip->vbat_monitor_params.low_thr,
2750 chip->vbat_monitor_params.high_thr);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002751
2752 if (!is_battery_present(chip)) {
2753 pr_debug("no battery inserted, do not enable vbat monitoring\n");
2754 chip->vbat_monitor_params.state_request =
2755 ADC_TM_HIGH_LOW_THR_DISABLE;
2756 } else {
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002757 rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
2758 &chip->vbat_monitor_params);
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002759 if (rc) {
2760 pr_err("tm setup failed: %d\n", rc);
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002761 return rc;
Xiaozhe Shib5689fb2013-07-15 17:20:49 -07002762 }
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002763 }
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07002764
Xiaozhe Shia6618a22013-03-27 10:26:29 -07002765 pr_debug("setup complete\n");
2766 return 0;
2767}
2768
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302769static void readjust_fcc_table(struct qpnp_bms_chip *chip)
2770{
2771 struct single_row_lut *temp, *old;
2772 int i, fcc, ratio;
2773
2774 if (!chip->enable_fcc_learning)
2775 return;
2776
2777 if (!chip->fcc_temp_lut) {
2778 pr_err("The static fcc lut table is NULL\n");
2779 return;
2780 }
2781
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07002782 temp = devm_kzalloc(chip->dev, sizeof(struct single_row_lut),
2783 GFP_KERNEL);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302784 if (!temp) {
2785 pr_err("Cannot allocate memory for adjusted fcc table\n");
2786 return;
2787 }
2788
2789 fcc = interpolate_fcc(chip->fcc_temp_lut, chip->fcc_new_batt_temp);
2790
2791 temp->cols = chip->fcc_temp_lut->cols;
2792 for (i = 0; i < chip->fcc_temp_lut->cols; i++) {
2793 temp->x[i] = chip->fcc_temp_lut->x[i];
2794 ratio = div_u64(chip->fcc_temp_lut->y[i] * 1000, fcc);
2795 temp->y[i] = (ratio * chip->fcc_new_mah);
2796 temp->y[i] /= 1000;
2797 }
2798
2799 old = chip->adjusted_fcc_temp_lut;
2800 chip->adjusted_fcc_temp_lut = temp;
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07002801 devm_kfree(chip->dev, old);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302802}
2803
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302804static int read_fcc_data_from_backup(struct qpnp_bms_chip *chip)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302805{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302806 int rc, i;
2807 u8 fcc = 0, chgcyl = 0;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302808
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302809 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2810 rc = qpnp_read_wrapper(chip, &fcc,
2811 chip->base + BMS_FCC_BASE_REG + i, 1);
2812 rc |= qpnp_read_wrapper(chip, &chgcyl,
2813 chip->base + BMS_CHGCYL_BASE_REG + i, 1);
2814 if (rc) {
2815 pr_err("Unable to read FCC data\n");
2816 return rc;
2817 }
2818 if (fcc == 0 || (fcc == 0xFF && chgcyl == 0xFF)) {
2819 /* FCC invalid/not present */
2820 chip->fcc_learning_samples[i].fcc_new = 0;
2821 chip->fcc_learning_samples[i].chargecycles = 0;
2822 } else {
2823 /* valid FCC data */
2824 chip->fcc_sample_count++;
2825 chip->fcc_learning_samples[i].fcc_new =
2826 fcc * chip->fcc_resolution;
2827 chip->fcc_learning_samples[i].chargecycles =
2828 chgcyl * CHGCYL_RESOLUTION;
2829 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302830 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302831
2832 return 0;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302833}
2834
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302835static int discard_backup_fcc_data(struct qpnp_bms_chip *chip)
2836{
2837 int rc = 0, i;
2838 u8 temp_u8 = 0;
2839
2840 chip->fcc_sample_count = 0;
2841 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2842 rc = qpnp_write_wrapper(chip, &temp_u8,
2843 chip->base + BMS_FCC_BASE_REG + i, 1);
2844 rc |= qpnp_write_wrapper(chip, &temp_u8,
2845 chip->base + BMS_CHGCYL_BASE_REG + i, 1);
2846 if (rc) {
2847 pr_err("Unable to clear FCC data\n");
2848 return rc;
2849 }
2850 }
2851
2852 return 0;
2853}
2854
2855static void
2856average_fcc_samples_and_readjust_fcc_table(struct qpnp_bms_chip *chip)
2857{
2858 int i, temp_fcc_avg = 0, temp_fcc_delta = 0, new_fcc_avg = 0;
2859 struct fcc_sample *ft;
2860
2861 for (i = 0; i < chip->min_fcc_learning_samples; i++)
2862 temp_fcc_avg += chip->fcc_learning_samples[i].fcc_new;
2863
2864 temp_fcc_avg /= chip->min_fcc_learning_samples;
2865 temp_fcc_delta = div_u64(temp_fcc_avg * DELTA_FCC_PERCENT, 100);
2866
2867 /* fix the fcc if its an outlier i.e. > 5% of the average */
2868 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2869 ft = &chip->fcc_learning_samples[i];
2870 if (abs(ft->fcc_new - temp_fcc_avg) > temp_fcc_delta)
2871 new_fcc_avg += temp_fcc_avg;
2872 else
2873 new_fcc_avg += ft->fcc_new;
2874 }
2875 new_fcc_avg /= chip->min_fcc_learning_samples;
2876
2877 chip->fcc_new_mah = new_fcc_avg;
2878 chip->fcc_new_batt_temp = FCC_DEFAULT_TEMP;
2879 pr_info("FCC update: New fcc_mah=%d, fcc_batt_temp=%d\n",
2880 new_fcc_avg, FCC_DEFAULT_TEMP);
2881 readjust_fcc_table(chip);
2882}
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302883
2884static void backup_charge_cycle(struct qpnp_bms_chip *chip)
2885{
2886 int rc = 0;
2887
2888 if (chip->charge_increase >= 0) {
2889 rc = qpnp_write_wrapper(chip, &chip->charge_increase,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302890 chip->base + CHARGE_INCREASE_STORAGE, 1);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302891 if (rc)
2892 pr_err("Unable to backup charge_increase\n");
2893 }
2894
2895 if (chip->charge_cycles >= 0) {
2896 rc = qpnp_write_wrapper(chip, (u8 *)&chip->charge_cycles,
2897 chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
2898 if (rc)
2899 pr_err("Unable to backup charge_cycles\n");
2900 }
2901}
2902
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302903static bool chargecycles_in_range(struct qpnp_bms_chip *chip)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302904{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302905 int i, min_cycle, max_cycle, valid_range;
2906
2907 /* find the smallest and largest charge cycle */
2908 max_cycle = min_cycle = chip->fcc_learning_samples[0].chargecycles;
2909 for (i = 1; i < chip->min_fcc_learning_samples; i++) {
2910 if (min_cycle > chip->fcc_learning_samples[i].chargecycles)
2911 min_cycle = chip->fcc_learning_samples[i].chargecycles;
2912 if (max_cycle < chip->fcc_learning_samples[i].chargecycles)
2913 max_cycle = chip->fcc_learning_samples[i].chargecycles;
2914 }
2915
2916 /* check if chargecyles are in range to continue with FCC update */
2917 valid_range = DIV_ROUND_UP(VALID_FCC_CHGCYL_RANGE,
2918 CHGCYL_RESOLUTION) * CHGCYL_RESOLUTION;
2919 if (abs(max_cycle - min_cycle) > valid_range)
2920 return false;
2921
2922 return true;
2923}
2924
2925static int read_chgcycle_data_from_backup(struct qpnp_bms_chip *chip)
2926{
2927 int rc;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302928 uint16_t temp_u16 = 0;
2929 u8 temp_u8 = 0;
2930
2931 rc = qpnp_read_wrapper(chip, &temp_u8,
2932 chip->base + CHARGE_INCREASE_STORAGE, 1);
2933 if (!rc && temp_u8 != 0xFF)
2934 chip->charge_increase = temp_u8;
2935
2936 rc = qpnp_read_wrapper(chip, (u8 *)&temp_u16,
2937 chip->base + CHARGE_CYCLE_STORAGE_LSB, 2);
2938 if (!rc && temp_u16 != 0xFFFF)
2939 chip->charge_cycles = temp_u16;
2940
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302941 return rc;
2942}
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302943
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302944static void
2945attempt_learning_new_fcc(struct qpnp_bms_chip *chip)
2946{
2947 pr_debug("Total FCC sample count=%d\n", chip->fcc_sample_count);
2948
2949 /* update FCC if we have the required samples */
2950 if ((chip->fcc_sample_count == chip->min_fcc_learning_samples) &&
2951 chargecycles_in_range(chip))
2952 average_fcc_samples_and_readjust_fcc_table(chip);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302953}
2954
2955static int calculate_real_soc(struct qpnp_bms_chip *chip,
2956 int batt_temp, struct raw_soc_params *raw, int cc_uah)
2957{
2958 int fcc_uah, rc_uah;
2959
2960 fcc_uah = calculate_fcc(chip, batt_temp);
2961 rc_uah = calculate_ocv_charge(chip, raw, fcc_uah);
2962
2963 return ((rc_uah - cc_uah) * 100) / fcc_uah;
2964}
2965
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302966#define MAX_U8_VALUE ((u8)(~0U))
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302967
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302968static int backup_new_fcc(struct qpnp_bms_chip *chip, int fcc_mah,
2969 int chargecycles)
2970{
2971 int rc, min_cycle, i;
2972 u8 fcc_new, chgcyl, pos = 0;
2973 struct fcc_sample *ft;
2974
2975 if ((fcc_mah > (chip->fcc_resolution * MAX_U8_VALUE)) ||
2976 (chargecycles > (CHGCYL_RESOLUTION * MAX_U8_VALUE))) {
2977 pr_warn("FCC/Chgcyl beyond storage limit. FCC=%d, chgcyl=%d\n",
2978 fcc_mah, chargecycles);
2979 return -EINVAL;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05302980 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05302981
2982 if (chip->fcc_sample_count == chip->min_fcc_learning_samples) {
2983 /* search best location - oldest entry */
2984 min_cycle = chip->fcc_learning_samples[0].chargecycles;
2985 for (i = 1; i < chip->min_fcc_learning_samples; i++) {
2986 if (min_cycle >
2987 chip->fcc_learning_samples[i].chargecycles)
2988 pos = i;
2989 }
2990 } else {
2991 /* find an empty location */
2992 for (i = 0; i < chip->min_fcc_learning_samples; i++) {
2993 ft = &chip->fcc_learning_samples[i];
2994 if (ft->fcc_new == 0 || (ft->fcc_new == 0xFF &&
2995 ft->chargecycles == 0xFF)) {
2996 pos = i;
2997 break;
2998 }
2999 }
3000 chip->fcc_sample_count++;
3001 }
3002 chip->fcc_learning_samples[pos].fcc_new = fcc_mah;
3003 chip->fcc_learning_samples[pos].chargecycles = chargecycles;
3004
3005 fcc_new = DIV_ROUND_UP(fcc_mah, chip->fcc_resolution);
3006 rc = qpnp_write_wrapper(chip, (u8 *)&fcc_new,
3007 chip->base + BMS_FCC_BASE_REG + pos, 1);
3008 if (rc)
3009 return rc;
3010
3011 chgcyl = DIV_ROUND_UP(chargecycles, CHGCYL_RESOLUTION);
3012 rc = qpnp_write_wrapper(chip, (u8 *)&chgcyl,
3013 chip->base + BMS_CHGCYL_BASE_REG + pos, 1);
3014 if (rc)
3015 return rc;
3016
3017 pr_debug("Backup new FCC: fcc_new=%d, chargecycle=%d, pos=%d\n",
3018 fcc_new, chgcyl, pos);
3019
3020 return rc;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303021}
3022
3023static void update_fcc_learning_table(struct qpnp_bms_chip *chip,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303024 int new_fcc_uah, int chargecycles, int batt_temp)
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303025{
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303026 int rc, fcc_default, fcc_temp;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303027
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303028 /* convert the fcc at batt_temp to new fcc at FCC_DEFAULT_TEMP */
3029 fcc_default = calculate_fcc(chip, FCC_DEFAULT_TEMP) / 1000;
3030 fcc_temp = calculate_fcc(chip, batt_temp) / 1000;
3031 new_fcc_uah = (new_fcc_uah / fcc_temp) * fcc_default;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303032
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303033 rc = backup_new_fcc(chip, new_fcc_uah / 1000, chargecycles);
3034 if (rc) {
3035 pr_err("Unable to backup new FCC\n");
3036 return;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303037 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303038 /* check if FCC can be updated */
3039 attempt_learning_new_fcc(chip);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303040}
3041
3042static bool is_new_fcc_valid(int new_fcc_uah, int fcc_uah)
3043{
3044 if ((new_fcc_uah >= (fcc_uah / 2)) &&
3045 ((new_fcc_uah * 100) <= (fcc_uah * 105)))
3046 return true;
3047
3048 pr_debug("FCC rejected - not within valid limit\n");
3049 return false;
3050}
3051
3052static void fcc_learning_config(struct qpnp_bms_chip *chip, bool start)
3053{
3054 int rc, batt_temp;
3055 struct raw_soc_params raw;
3056 struct qpnp_vadc_result result;
3057 int fcc_uah, new_fcc_uah, delta_cc_uah, delta_soc;
3058
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003059 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &result);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303060 if (rc) {
3061 pr_err("Unable to read batt_temp\n");
3062 return;
3063 } else {
3064 batt_temp = (int)result.physical;
3065 }
3066
3067 rc = read_soc_params_raw(chip, &raw, batt_temp);
3068 if (rc) {
3069 pr_err("Unable to read CC, cannot update FCC\n");
3070 return;
3071 }
3072
3073 if (start) {
3074 chip->start_pc = interpolate_pc(chip->pc_temp_ocv_lut,
3075 batt_temp / 10, raw.last_good_ocv_uv / 1000);
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003076 chip->start_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303077 chip->start_real_soc = calculate_real_soc(chip,
3078 batt_temp, &raw, chip->start_cc_uah);
3079 pr_debug("start_pc=%d, start_cc=%d, start_soc=%d real_soc=%d\n",
3080 chip->start_pc, chip->start_cc_uah,
3081 chip->start_soc, chip->start_real_soc);
3082 } else {
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003083 chip->end_cc_uah = calculate_cc(chip, raw.cc, CC, NORESET);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303084 delta_soc = 100 - chip->start_real_soc;
3085 delta_cc_uah = abs(chip->end_cc_uah - chip->start_cc_uah);
3086 new_fcc_uah = div_u64(delta_cc_uah * 100, delta_soc);
3087 fcc_uah = calculate_fcc(chip, batt_temp);
3088 pr_debug("start_soc=%d, start_pc=%d, start_real_soc=%d, start_cc=%d, end_cc=%d, new_fcc=%d\n",
3089 chip->start_soc, chip->start_pc, chip->start_real_soc,
3090 chip->start_cc_uah, chip->end_cc_uah, new_fcc_uah);
3091
3092 if (is_new_fcc_valid(new_fcc_uah, fcc_uah))
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303093 update_fcc_learning_table(chip, new_fcc_uah,
3094 chip->charge_cycles, batt_temp);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303095 }
3096}
3097
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003098#define MAX_CAL_TRIES 200
3099#define MIN_CAL_UA 3000
3100static void batfet_open_work(struct work_struct *work)
3101{
3102 int i;
3103 int rc;
3104 int result_ua;
3105 u8 orig_delay, sample_delay;
3106 struct qpnp_bms_chip *chip = container_of(work,
3107 struct qpnp_bms_chip,
3108 batfet_open_work);
3109
3110 rc = qpnp_read_wrapper(chip, &orig_delay,
3111 chip->base + BMS1_S1_DELAY_CTL, 1);
3112
3113 sample_delay = 0x0;
3114 rc = qpnp_write_wrapper(chip, &sample_delay,
3115 chip->base + BMS1_S1_DELAY_CTL, 1);
3116
3117 /*
3118 * In certain PMICs there is a coupling issue which causes
3119 * bad calibration value that result in a huge battery current
3120 * even when the BATFET is open. Do continious calibrations until
3121 * we hit reasonable cal values which result in low battery current
3122 */
3123
3124 for (i = 0; (!chip->batfet_closed) && i < MAX_CAL_TRIES; i++) {
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003125 rc = qpnp_iadc_calibrate_for_trim(chip->iadc_dev, false);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003126 /*
3127 * Wait 20mS after calibration and before reading battery
3128 * current. The BMS h/w uses calibration values in the
3129 * next sampling of vsense.
3130 */
3131 msleep(20);
3132 rc |= get_battery_current(chip, &result_ua);
3133 if (rc == 0 && abs(result_ua) <= MIN_CAL_UA) {
3134 pr_debug("good cal at %d attempt\n", i);
3135 break;
3136 }
3137 }
3138 pr_debug("batfet_closed = %d i = %d result_ua = %d\n",
3139 chip->batfet_closed, i, result_ua);
3140
3141 rc = qpnp_write_wrapper(chip, &orig_delay,
3142 chip->base + BMS1_S1_DELAY_CTL, 1);
3143}
3144
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003145static void charging_began(struct qpnp_bms_chip *chip)
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003146{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003147 mutex_lock(&chip->last_soc_mutex);
3148 chip->charge_start_tm_sec = 0;
3149 chip->catch_up_time_sec = 0;
3150 mutex_unlock(&chip->last_soc_mutex);
3151
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303152 chip->start_soc = report_state_of_charge(chip);
3153
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003154 mutex_lock(&chip->last_ocv_uv_mutex);
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303155 if (chip->enable_fcc_learning)
3156 fcc_learning_config(chip, true);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003157 chip->soc_at_cv = -EINVAL;
3158 chip->prev_chg_soc = -EINVAL;
3159 mutex_unlock(&chip->last_ocv_uv_mutex);
3160}
3161
3162static void charging_ended(struct qpnp_bms_chip *chip)
3163{
3164 mutex_lock(&chip->last_soc_mutex);
3165 chip->charge_start_tm_sec = 0;
3166 chip->catch_up_time_sec = 0;
3167 mutex_unlock(&chip->last_soc_mutex);
3168
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303169 chip->end_soc = report_state_of_charge(chip);
3170
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003171 mutex_lock(&chip->last_ocv_uv_mutex);
3172 chip->soc_at_cv = -EINVAL;
3173 chip->prev_chg_soc = -EINVAL;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303174
3175 /* update the chargecycles */
3176 if (chip->end_soc > chip->start_soc) {
3177 chip->charge_increase += (chip->end_soc - chip->start_soc);
3178 if (chip->charge_increase > 100) {
3179 chip->charge_cycles++;
3180 chip->charge_increase = chip->charge_increase % 100;
3181 }
3182 if (chip->enable_fcc_learning)
3183 backup_charge_cycle(chip);
3184 }
3185
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003186 if (get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL) {
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303187 if (chip->enable_fcc_learning &&
3188 (chip->start_soc <= chip->min_fcc_learning_soc) &&
3189 (chip->start_pc <= chip->min_fcc_ocv_pc))
3190 fcc_learning_config(chip, false);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003191 chip->done_charging = true;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003192 chip->last_soc_invalid = true;
Xiaozhe Shicc48e992013-05-28 16:42:24 -07003193 } else if (chip->charging_adjusted_ocv > 0) {
3194 pr_debug("Charging stopped before full, adjusted OCV = %d\n",
3195 chip->charging_adjusted_ocv);
3196 chip->last_ocv_uv = chip->charging_adjusted_ocv;
Xiaozhe Shi83484e32013-05-16 10:59:59 -07003197 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303198
Xiaozhe Shicc48e992013-05-28 16:42:24 -07003199 chip->charging_adjusted_ocv = -EINVAL;
3200
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003201 mutex_unlock(&chip->last_ocv_uv_mutex);
3202}
3203
3204static void battery_status_check(struct qpnp_bms_chip *chip)
3205{
3206 int status = get_battery_status(chip);
3207
Xiaozhe Shibda84992013-09-05 10:39:11 -07003208 mutex_lock(&chip->status_lock);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003209 if (chip->battery_status != status) {
Xiaozhe Shi30e94802013-08-19 16:40:53 -07003210 pr_debug("status = %d, shadow status = %d\n",
3211 status, chip->battery_status);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003212 if (status == POWER_SUPPLY_STATUS_CHARGING) {
3213 pr_debug("charging started\n");
3214 charging_began(chip);
3215 } else if (chip->battery_status
3216 == POWER_SUPPLY_STATUS_CHARGING) {
3217 pr_debug("charging ended\n");
3218 charging_ended(chip);
3219 }
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003220
3221 if (status == POWER_SUPPLY_STATUS_FULL) {
3222 pr_debug("battery full\n");
3223 enable_bms_irq(&chip->ocv_thr_irq);
3224 enable_bms_irq(&chip->sw_cc_thr_irq);
Xiaozhe Shibda84992013-09-05 10:39:11 -07003225 recalculate_soc(chip);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003226 } else if (chip->battery_status
3227 == POWER_SUPPLY_STATUS_FULL) {
3228 pr_debug("battery not full any more\n");
3229 disable_bms_irq(&chip->ocv_thr_irq);
3230 disable_bms_irq(&chip->sw_cc_thr_irq);
3231 }
3232
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003233 chip->battery_status = status;
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003234 /* battery charge status has changed, so force a soc
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003235 * recalculation to update the SoC */
3236 schedule_work(&chip->recalc_work);
3237 }
Xiaozhe Shibda84992013-09-05 10:39:11 -07003238 mutex_unlock(&chip->status_lock);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003239}
3240
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003241#define CALIB_WRKARND_DIG_MAJOR_MAX 0x03
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003242static void batfet_status_check(struct qpnp_bms_chip *chip)
3243{
3244 bool batfet_closed;
3245
3246 if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX)
3247 return;
3248
3249 batfet_closed = is_batfet_closed(chip);
3250 if (chip->batfet_closed != batfet_closed) {
3251 chip->batfet_closed = batfet_closed;
3252 if (batfet_closed == false) {
3253 /* batfet opened */
3254 schedule_work(&chip->batfet_open_work);
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003255 qpnp_iadc_skip_calibration(chip->iadc_dev);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003256 } else {
3257 /* batfet closed */
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07003258 qpnp_iadc_calibrate_for_trim(chip->iadc_dev, true);
3259 qpnp_iadc_resume_calibration(chip->iadc_dev);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003260 }
3261 }
3262}
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003263
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003264static void battery_insertion_check(struct qpnp_bms_chip *chip)
3265{
Xiaozhe Shi90f3a412013-08-21 10:31:35 -07003266 int present = (int)is_battery_present(chip);
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07003267 int insertion_ocv_uv = get_battery_insertion_ocv_uv(chip);
3268 int insertion_ocv_taken = (insertion_ocv_uv > 0);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003269
3270 mutex_lock(&chip->vbat_monitor_mutex);
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07003271 if (chip->battery_present != present
3272 && (present == insertion_ocv_taken
3273 || chip->battery_present == -EINVAL)) {
3274 pr_debug("status = %d, shadow status = %d, insertion_ocv_uv = %d\n",
3275 present, chip->battery_present,
3276 insertion_ocv_uv);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003277 if (chip->battery_present != -EINVAL) {
3278 if (present) {
Xiaozhe Shi24f91a02013-08-29 17:15:05 -07003279 chip->insertion_ocv_uv = insertion_ocv_uv;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003280 setup_vbat_monitoring(chip);
3281 chip->new_battery = true;
3282 } else {
3283 reset_vbat_monitoring(chip);
3284 }
3285 }
3286 chip->battery_present = present;
3287 /* a new battery was inserted or removed, so force a soc
3288 * recalculation to update the SoC */
3289 schedule_work(&chip->recalc_work);
3290 }
3291 mutex_unlock(&chip->vbat_monitor_mutex);
3292}
3293
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003294/* Returns capacity as a SoC percentage between 0 and 100 */
3295static int get_prop_bms_capacity(struct qpnp_bms_chip *chip)
3296{
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08003297 return report_state_of_charge(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003298}
3299
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003300static void qpnp_bms_external_power_changed(struct power_supply *psy)
3301{
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003302 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
3303 bms_psy);
3304
3305 battery_insertion_check(chip);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07003306 batfet_status_check(chip);
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003307 battery_status_check(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003308}
3309
3310static int qpnp_bms_power_get_property(struct power_supply *psy,
3311 enum power_supply_property psp,
3312 union power_supply_propval *val)
3313{
3314 struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip,
3315 bms_psy);
3316
3317 switch (psp) {
3318 case POWER_SUPPLY_PROP_CAPACITY:
3319 val->intval = get_prop_bms_capacity(chip);
3320 break;
Xiaozhe Shibda84992013-09-05 10:39:11 -07003321 case POWER_SUPPLY_PROP_STATUS:
3322 val->intval = chip->battery_status;
3323 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003324 case POWER_SUPPLY_PROP_CURRENT_NOW:
3325 val->intval = get_prop_bms_current_now(chip);
3326 break;
Xiaozhe Shi6dc56f12013-05-02 15:56:55 -07003327 case POWER_SUPPLY_PROP_RESISTANCE:
3328 val->intval = get_prop_bms_batt_resistance(chip);
3329 break;
Xiaozhe Shifb37f3b2013-05-20 16:56:19 -07003330 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
3331 val->intval = get_prop_bms_charge_counter(chip);
3332 break;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003333 case POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW:
3334 val->intval = get_prop_bms_charge_counter_shadow(chip);
3335 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003336 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
3337 val->intval = get_prop_bms_charge_full_design(chip);
3338 break;
Anirudh Ghayalc9d981a2013-06-24 09:50:33 +05303339 case POWER_SUPPLY_PROP_CHARGE_FULL:
3340 val->intval = get_prop_bms_charge_full(chip);
3341 break;
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05303342 case POWER_SUPPLY_PROP_CYCLE_COUNT:
3343 val->intval = chip->charge_cycles;
3344 break;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003345 default:
3346 return -EINVAL;
3347 }
3348 return 0;
3349}
3350
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003351#define OCV_USE_LIMIT_EN BIT(7)
3352static int set_ocv_voltage_thresholds(struct qpnp_bms_chip *chip,
3353 int low_voltage_threshold,
3354 int high_voltage_threshold)
3355{
3356 uint16_t low_voltage_raw, high_voltage_raw;
3357 int rc;
3358
3359 low_voltage_raw = convert_vbatt_uv_to_raw(chip,
3360 low_voltage_threshold);
3361 high_voltage_raw = convert_vbatt_uv_to_raw(chip,
3362 high_voltage_threshold);
3363 rc = qpnp_write_wrapper(chip, (u8 *)&low_voltage_raw,
3364 chip->base + BMS1_OCV_USE_LOW_LIMIT_THR0, 2);
3365 if (rc) {
3366 pr_err("Failed to set ocv low voltage threshold: %d\n", rc);
3367 return rc;
3368 }
3369 rc = qpnp_write_wrapper(chip, (u8 *)&high_voltage_raw,
3370 chip->base + BMS1_OCV_USE_HIGH_LIMIT_THR0, 2);
3371 if (rc) {
3372 pr_err("Failed to set ocv high voltage threshold: %d\n", rc);
3373 return rc;
3374 }
3375 rc = qpnp_masked_write(chip, BMS1_OCV_USE_LIMIT_CTL,
3376 OCV_USE_LIMIT_EN, OCV_USE_LIMIT_EN);
3377 if (rc) {
3378 pr_err("Failed to enabled ocv voltage thresholds: %d\n", rc);
3379 return rc;
3380 }
3381 pr_debug("ocv low threshold set to %d uv or 0x%x raw\n",
3382 low_voltage_threshold, low_voltage_raw);
3383 pr_debug("ocv high threshold set to %d uv or 0x%x raw\n",
3384 high_voltage_threshold, high_voltage_raw);
3385 return 0;
3386}
3387
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003388static int read_shutdown_iavg_ma(struct qpnp_bms_chip *chip)
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003389{
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003390 u8 iavg;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003391 int rc;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003392
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003393 rc = qpnp_read_wrapper(chip, &iavg, chip->base + IAVG_STORAGE_REG, 1);
3394 if (rc) {
3395 pr_err("failed to read addr = %d %d assuming %d\n",
3396 chip->base + IAVG_STORAGE_REG, rc,
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003397 MIN_IAVG_MA);
3398 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003399 } else if (iavg == IAVG_INVALID) {
3400 pr_err("invalid iavg read from BMS1_DATA_REG_1, using %d\n",
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003401 MIN_IAVG_MA);
3402 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003403 } else {
3404 if (iavg == 0)
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003405 return MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003406 else
Xiaozhe Shi68a1bb22013-09-23 14:52:00 -07003407 return MIN_IAVG_MA + IAVG_STEP_SIZE_MA * iavg;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003408 }
3409}
3410
3411static int read_shutdown_soc(struct qpnp_bms_chip *chip)
3412{
3413 u8 stored_soc;
3414 int rc, shutdown_soc;
3415
3416 /*
3417 * The previous SOC is stored in the first 7 bits of the register as
3418 * (Shutdown SOC + 1). This allows for register reset values of both
3419 * 0x00 and 0x7F.
3420 */
3421 rc = qpnp_read_wrapper(chip, &stored_soc, chip->soc_storage_addr, 1);
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003422 if (rc) {
3423 pr_err("failed to read addr = %d %d\n",
3424 chip->soc_storage_addr, rc);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003425 return SOC_INVALID;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003426 }
3427
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003428 if ((stored_soc >> 1) > 0)
3429 shutdown_soc = (stored_soc >> 1) - 1;
3430 else
3431 shutdown_soc = SOC_INVALID;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003432
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003433 pr_debug("stored soc = 0x%02x, shutdown_soc = %d\n",
3434 stored_soc, shutdown_soc);
3435 return shutdown_soc;
3436}
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003437
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003438#define BAT_REMOVED_OFFMODE_BIT BIT(6)
3439static bool is_battery_replaced_in_offmode(struct qpnp_bms_chip *chip)
3440{
3441 u8 batt_pres;
3442 int rc;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003443
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003444 if (chip->batt_pres_addr) {
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003445 rc = qpnp_read_wrapper(chip, &batt_pres,
3446 chip->batt_pres_addr, 1);
3447 pr_debug("offmode removed: %02x\n", batt_pres);
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003448 if (!rc && (batt_pres & BAT_REMOVED_OFFMODE_BIT))
3449 return true;
3450 }
3451 return false;
3452}
3453
3454static void load_shutdown_data(struct qpnp_bms_chip *chip)
3455{
3456 int calculated_soc, shutdown_soc;
3457 bool invalid_stored_soc;
3458 bool offmode_battery_replaced;
3459 bool shutdown_soc_out_of_limit;
3460
3461 /*
3462 * Read the saved shutdown SoC from the configured register and
3463 * check if the value has been reset
3464 */
3465 shutdown_soc = read_shutdown_soc(chip);
3466 invalid_stored_soc = (shutdown_soc == SOC_INVALID);
3467
3468 /*
3469 * Do a quick run of SoC calculation to find whether the shutdown soc
3470 * is close enough.
3471 */
3472 calculated_soc = recalculate_raw_soc(chip);
3473 shutdown_soc_out_of_limit = (abs(shutdown_soc - calculated_soc)
3474 > chip->shutdown_soc_valid_limit);
3475 pr_debug("calculated_soc = %d, valid_limit = %d\n",
3476 calculated_soc, chip->shutdown_soc_valid_limit);
3477
3478 /*
3479 * Check if the battery has been replaced while the system was powered
3480 * down.
3481 */
3482 offmode_battery_replaced = is_battery_replaced_in_offmode(chip);
3483
3484 /* Invalidate the shutdown SoC if any of these conditions hold true */
3485 if (chip->ignore_shutdown_soc
3486 || invalid_stored_soc
3487 || offmode_battery_replaced
3488 || shutdown_soc_out_of_limit) {
3489 chip->battery_removed = true;
3490 chip->shutdown_soc_invalid = true;
Xiaozhe Shic92cfd92013-10-25 11:36:42 -07003491 chip->shutdown_iavg_ma = MIN_IAVG_MA;
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003492 pr_debug("Ignoring shutdown SoC: invalid = %d, offmode = %d, out_of_limit = %d\n",
3493 invalid_stored_soc, offmode_battery_replaced,
3494 shutdown_soc_out_of_limit);
3495 } else {
3496 chip->shutdown_iavg_ma = read_shutdown_iavg_ma(chip);
3497 chip->shutdown_soc = shutdown_soc;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07003498 }
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303499
Xiaozhe Shif9f99242013-08-29 12:27:50 -07003500 pr_debug("raw_soc = %d shutdown_soc = %d shutdown_iavg = %d shutdown_soc_invalid = %d, battery_removed = %d\n",
3501 calculated_soc,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003502 chip->shutdown_soc,
3503 chip->shutdown_iavg_ma,
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303504 chip->shutdown_soc_invalid,
3505 chip->battery_removed);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003506}
3507
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003508static irqreturn_t bms_ocv_thr_irq_handler(int irq, void *_chip)
3509{
3510 struct qpnp_bms_chip *chip = _chip;
3511
3512 pr_debug("ocv_thr irq triggered\n");
3513 bms_stay_awake(&chip->soc_wake_source);
3514 schedule_work(&chip->recalc_work);
3515 return IRQ_HANDLED;
3516}
3517
3518static irqreturn_t bms_sw_cc_thr_irq_handler(int irq, void *_chip)
3519{
3520 struct qpnp_bms_chip *chip = _chip;
3521
3522 pr_debug("sw_cc_thr irq triggered\n");
3523 bms_stay_awake(&chip->soc_wake_source);
3524 schedule_work(&chip->recalc_work);
3525 return IRQ_HANDLED;
3526}
3527
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003528static int64_t read_battery_id(struct qpnp_bms_chip *chip)
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003529{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003530 int rc;
3531 struct qpnp_vadc_result result;
3532
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07003533 rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX2_BAT_ID, &result);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003534 if (rc) {
3535 pr_err("error reading batt id channel = %d, rc = %d\n",
3536 LR_MUX2_BAT_ID, rc);
3537 return rc;
3538 }
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003539
3540 return result.physical;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003541}
3542
3543static int set_battery_data(struct qpnp_bms_chip *chip)
3544{
3545 int64_t battery_id;
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003546 int rc = 0, dt_data = false;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003547 struct bms_battery_data *batt_data;
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003548 struct device_node *node;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003549
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003550 if (chip->batt_type == BATT_DESAY) {
3551 batt_data = &desay_5200_data;
3552 } else if (chip->batt_type == BATT_PALLADIUM) {
3553 batt_data = &palladium_1500_data;
3554 } else if (chip->batt_type == BATT_OEM) {
3555 batt_data = &oem_batt_data;
Wu Fenglin2ac88aa2013-04-25 12:43:40 +08003556 } else if (chip->batt_type == BATT_QRD_4V35_2000MAH) {
3557 batt_data = &QRD_4v35_2000mAh_data;
tingtingf50326f2013-06-05 15:07:24 +08003558 } else if (chip->batt_type == BATT_QRD_4V2_1300MAH) {
3559 batt_data = &qrd_4v2_1300mah_data;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003560 } else {
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003561 battery_id = read_battery_id(chip);
3562 if (battery_id < 0) {
3563 pr_err("cannot read battery id err = %lld\n",
3564 battery_id);
3565 return battery_id;
3566 }
3567
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003568 node = of_find_node_by_name(chip->spmi->dev.of_node,
3569 "qcom,battery-data");
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003570 if (!node) {
3571 pr_warn("No available batterydata, using palladium 1500\n");
3572 batt_data = &palladium_1500_data;
3573 goto assign_data;
3574 }
3575 batt_data = devm_kzalloc(chip->dev,
3576 sizeof(struct bms_battery_data), GFP_KERNEL);
3577 if (!batt_data) {
3578 pr_err("Could not alloc battery data\n");
3579 batt_data = &palladium_1500_data;
3580 goto assign_data;
3581 }
3582 batt_data->fcc_temp_lut = devm_kzalloc(chip->dev,
3583 sizeof(struct single_row_lut),
3584 GFP_KERNEL);
3585 batt_data->pc_temp_ocv_lut = devm_kzalloc(chip->dev,
3586 sizeof(struct pc_temp_ocv_lut),
3587 GFP_KERNEL);
3588 batt_data->rbatt_sf_lut = devm_kzalloc(chip->dev,
3589 sizeof(struct sf_lut),
3590 GFP_KERNEL);
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003591
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003592 batt_data->max_voltage_uv = -1;
3593 batt_data->cutoff_uv = -1;
3594 batt_data->iterm_ua = -1;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003595
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003596 /*
3597 * if the alloced luts are 0s, of_batterydata_read_data ignores
3598 * them.
3599 */
3600 rc = of_batterydata_read_data(node, batt_data, battery_id);
3601 if (rc == 0 && batt_data->fcc_temp_lut
3602 && batt_data->pc_temp_ocv_lut
3603 && batt_data->rbatt_sf_lut) {
3604 dt_data = true;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003605 } else {
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003606 pr_err("battery data load failed, using palladium 1500\n");
3607 devm_kfree(chip->dev, batt_data->fcc_temp_lut);
3608 devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut);
3609 devm_kfree(chip->dev, batt_data->rbatt_sf_lut);
3610 devm_kfree(chip->dev, batt_data);
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003611 batt_data = &palladium_1500_data;
3612 }
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003613 }
3614
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003615assign_data:
Xiaozhe Shi976618f2013-04-30 10:49:30 -07003616 chip->fcc_mah = batt_data->fcc;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003617 chip->fcc_temp_lut = batt_data->fcc_temp_lut;
3618 chip->fcc_sf_lut = batt_data->fcc_sf_lut;
3619 chip->pc_temp_ocv_lut = batt_data->pc_temp_ocv_lut;
3620 chip->pc_sf_lut = batt_data->pc_sf_lut;
3621 chip->rbatt_sf_lut = batt_data->rbatt_sf_lut;
3622 chip->default_rbatt_mohm = batt_data->default_rbatt_mohm;
Xiaozhe Shi1a10aff2013-04-01 15:40:05 -07003623 chip->rbatt_capacitive_mohm = batt_data->rbatt_capacitive_mohm;
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07003624 chip->flat_ocv_threshold_uv = batt_data->flat_ocv_threshold_uv;
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003625
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003626 /* Override battery properties if specified in the battery profile */
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003627 if (batt_data->max_voltage_uv >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003628 chip->max_voltage_uv = batt_data->max_voltage_uv;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003629 if (batt_data->cutoff_uv >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003630 chip->v_cutoff_uv = batt_data->cutoff_uv;
Xiaozhe Shi23174ea2013-07-30 17:51:09 -07003631 if (batt_data->iterm_ua >= 0 && dt_data)
Xiaozhe Shiaf203c22013-06-19 12:01:38 -07003632 chip->chg_term_ua = batt_data->iterm_ua;
3633
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003634 if (chip->pc_temp_ocv_lut == NULL) {
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003635 pr_err("temp ocv lut table has not been loaded\n");
3636 if (dt_data) {
3637 devm_kfree(chip->dev, batt_data->fcc_temp_lut);
3638 devm_kfree(chip->dev, batt_data->pc_temp_ocv_lut);
3639 devm_kfree(chip->dev, batt_data->rbatt_sf_lut);
3640 devm_kfree(chip->dev, batt_data);
3641 }
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003642 return -EINVAL;
3643 }
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003644
3645 if (dt_data)
3646 devm_kfree(chip->dev, batt_data);
3647
Xiaozhe Shi77a5b052012-12-14 16:37:45 -08003648 return 0;
Xiaozhe Shi73a65692012-09-18 17:51:57 -07003649}
3650
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07003651static int bms_get_adc(struct qpnp_bms_chip *chip,
3652 struct spmi_device *spmi)
3653{
3654 int rc = 0;
3655
3656 chip->vadc_dev = qpnp_get_vadc(&spmi->dev, "bms");
3657 if (IS_ERR(chip->vadc_dev)) {
3658 rc = PTR_ERR(chip->vadc_dev);
3659 if (rc != -EPROBE_DEFER)
3660 pr_err("vadc property missing, rc=%d\n", rc);
3661 return rc;
3662 }
3663
3664 chip->iadc_dev = qpnp_get_iadc(&spmi->dev, "bms");
3665 if (IS_ERR(chip->iadc_dev)) {
3666 rc = PTR_ERR(chip->iadc_dev);
3667 if (rc != -EPROBE_DEFER)
3668 pr_err("iadc property missing, rc=%d\n", rc);
3669 return rc;
3670 }
3671
3672 chip->adc_tm_dev = qpnp_get_adc_tm(&spmi->dev, "bms");
3673 if (IS_ERR(chip->adc_tm_dev)) {
3674 rc = PTR_ERR(chip->adc_tm_dev);
3675 if (rc != -EPROBE_DEFER)
3676 pr_err("adc-tm not ready, defer probe\n");
3677 return rc;
3678 }
3679
3680 return 0;
3681}
3682
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003683#define SPMI_PROP_READ(chip_prop, qpnp_spmi_property, retval) \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003684do { \
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003685 if (retval) \
3686 break; \
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003687 retval = of_property_read_u32(chip->spmi->dev.of_node, \
Xiaozhe Shi9bd24622013-01-23 15:54:54 -08003688 "qcom," qpnp_spmi_property, \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003689 &chip->chip_prop); \
3690 if (retval) { \
3691 pr_err("Error reading " #qpnp_spmi_property \
3692 " property %d\n", rc); \
Xiaozhe Shib19f7032012-08-16 12:14:16 -07003693 } \
3694} while (0)
3695
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303696#define SPMI_PROP_READ_BOOL(chip_prop, qpnp_spmi_property) \
3697do { \
3698 chip->chip_prop = of_property_read_bool(chip->spmi->dev.of_node,\
3699 "qcom," qpnp_spmi_property); \
3700} while (0)
3701
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003702static inline int bms_read_properties(struct qpnp_bms_chip *chip)
3703{
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003704 int rc = 0;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003705
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003706 SPMI_PROP_READ(r_sense_uohm, "r-sense-uohm", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003707 SPMI_PROP_READ(v_cutoff_uv, "v-cutoff-uv", rc);
3708 SPMI_PROP_READ(max_voltage_uv, "max-voltage-uv", rc);
3709 SPMI_PROP_READ(r_conn_mohm, "r-conn-mohm", rc);
3710 SPMI_PROP_READ(chg_term_ua, "chg-term-ua", rc);
3711 SPMI_PROP_READ(shutdown_soc_valid_limit,
3712 "shutdown-soc-valid-limit", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003713 SPMI_PROP_READ(adjust_soc_low_threshold,
3714 "adjust-soc-low-threshold", rc);
3715 SPMI_PROP_READ(batt_type, "batt-type", rc);
3716 SPMI_PROP_READ(low_soc_calc_threshold,
3717 "low-soc-calculate-soc-threshold", rc);
3718 SPMI_PROP_READ(low_soc_calculate_soc_ms,
3719 "low-soc-calculate-soc-ms", rc);
Xiaozhe Shicb487b12013-10-14 17:42:07 -07003720 SPMI_PROP_READ(low_voltage_calculate_soc_ms,
3721 "low-voltage-calculate-soc-ms", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003722 SPMI_PROP_READ(calculate_soc_ms, "calculate-soc-ms", rc);
Xiaozhe Shi0ac7a002013-03-26 13:14:03 -07003723 SPMI_PROP_READ(high_ocv_correction_limit_uv,
3724 "high-ocv-correction-limit-uv", rc);
3725 SPMI_PROP_READ(low_ocv_correction_limit_uv,
3726 "low-ocv-correction-limit-uv", rc);
3727 SPMI_PROP_READ(hold_soc_est,
3728 "hold-soc-est", rc);
3729 SPMI_PROP_READ(ocv_high_threshold_uv,
3730 "ocv-voltage-high-threshold-uv", rc);
Xiaozhe Shibdf14742012-12-05 12:41:48 -08003731 SPMI_PROP_READ(ocv_low_threshold_uv,
3732 "ocv-voltage-low-threshold-uv", rc);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08003733 SPMI_PROP_READ(low_voltage_threshold, "low-voltage-threshold", rc);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07003734 SPMI_PROP_READ(temperature_margin, "tm-temp-margin", rc);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003735
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003736 chip->use_external_rsense = of_property_read_bool(
3737 chip->spmi->dev.of_node,
3738 "qcom,use-external-rsense");
3739 chip->ignore_shutdown_soc = of_property_read_bool(
3740 chip->spmi->dev.of_node,
3741 "qcom,ignore-shutdown-soc");
3742 chip->use_voltage_soc = of_property_read_bool(chip->spmi->dev.of_node,
3743 "qcom,use-voltage-soc");
3744 chip->use_ocv_thresholds = of_property_read_bool(
3745 chip->spmi->dev.of_node,
3746 "qcom,use-ocv-thresholds");
3747
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003748 if (chip->adjust_soc_low_threshold >= 45)
3749 chip->adjust_soc_low_threshold = 45;
3750
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303751 SPMI_PROP_READ_BOOL(enable_fcc_learning, "enable-fcc-learning");
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303752 if (chip->enable_fcc_learning) {
3753 SPMI_PROP_READ(min_fcc_learning_soc,
3754 "min-fcc-learning-soc", rc);
3755 SPMI_PROP_READ(min_fcc_ocv_pc,
3756 "min-fcc-ocv-pc", rc);
3757 SPMI_PROP_READ(min_fcc_learning_samples,
3758 "min-fcc-learning-samples", rc);
Anirudh Ghayale0c02932013-07-08 16:26:35 +05303759 SPMI_PROP_READ(fcc_resolution,
3760 "fcc-resolution", rc);
3761 if (chip->min_fcc_learning_samples > MAX_FCC_CYCLES)
3762 chip->min_fcc_learning_samples = MAX_FCC_CYCLES;
3763 chip->fcc_learning_samples = devm_kzalloc(&chip->spmi->dev,
3764 (sizeof(struct fcc_sample) *
3765 chip->min_fcc_learning_samples), GFP_KERNEL);
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07003766 if (chip->fcc_learning_samples == NULL)
3767 return -ENOMEM;
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05303768 pr_debug("min-fcc-soc=%d, min-fcc-pc=%d, min-fcc-cycles=%d\n",
3769 chip->min_fcc_learning_soc, chip->min_fcc_ocv_pc,
3770 chip->min_fcc_learning_samples);
3771 }
3772
Xiaozhe Shi2e476682013-07-22 14:57:22 -07003773 if (rc) {
3774 pr_err("Missing required properties.\n");
3775 return rc;
3776 }
3777
Xiaozhe Shid0a79542012-11-06 10:00:38 -08003778 pr_debug("dts data: r_sense_uohm:%d, v_cutoff_uv:%d, max_v:%d\n",
3779 chip->r_sense_uohm, chip->v_cutoff_uv,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003780 chip->max_voltage_uv);
3781 pr_debug("r_conn:%d, shutdown_soc: %d, adjust_soc_low:%d\n",
3782 chip->r_conn_mohm, chip->shutdown_soc_valid_limit,
3783 chip->adjust_soc_low_threshold);
Xiaozhe Shi561ebf72013-03-25 13:51:27 -07003784 pr_debug("chg_term_ua:%d, batt_type:%d\n",
3785 chip->chg_term_ua,
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003786 chip->batt_type);
Xiaozhe Shi781b0a22012-11-05 17:18:27 -08003787 pr_debug("ignore_shutdown_soc:%d, use_voltage_soc:%d\n",
Xiaozhe Shi79d6c1d2012-11-26 13:19:50 -08003788 chip->ignore_shutdown_soc, chip->use_voltage_soc);
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003789 pr_debug("use external rsense: %d\n", chip->use_external_rsense);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003790 return 0;
3791}
3792
3793static inline void bms_initialize_constants(struct qpnp_bms_chip *chip)
3794{
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003795 chip->prev_pc_unusable = -EINVAL;
3796 chip->soc_at_cv = -EINVAL;
3797 chip->calculated_soc = -EINVAL;
Xiaozhe Shie118c692012-09-24 15:17:43 -07003798 chip->last_soc = -EINVAL;
Xiaozhe Shi7edde5d2012-09-26 11:23:09 -07003799 chip->last_soc_est = -EINVAL;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07003800 chip->battery_present = -EINVAL;
Xiaozhe Shi890fbf42013-05-02 16:42:53 -07003801 chip->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
Xiaozhe Shif36d2862013-01-04 10:17:35 -08003802 chip->last_cc_uah = INT_MIN;
Abhijeet Dharmapurikar15f30fb2012-12-27 17:20:29 -08003803 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
3804 chip->prev_last_good_ocv_raw = OCV_RAW_UNINITIALIZED;
Xiaozhe Shie118c692012-09-24 15:17:43 -07003805 chip->first_time_calc_soc = 1;
3806 chip->first_time_calc_uuc = 1;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07003807}
3808
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003809#define SPMI_FIND_IRQ(chip, irq_name) \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003810do { \
3811 chip->irq_name##_irq.irq = spmi_get_irq_byname(chip->spmi, \
3812 resource, #irq_name); \
3813 if (chip->irq_name##_irq.irq < 0) { \
3814 pr_err("Unable to get " #irq_name " irq\n"); \
3815 return -ENXIO; \
3816 } \
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003817} while (0)
3818
3819static int bms_find_irqs(struct qpnp_bms_chip *chip,
3820 struct spmi_resource *resource)
3821{
3822 SPMI_FIND_IRQ(chip, sw_cc_thr);
3823 SPMI_FIND_IRQ(chip, ocv_thr);
3824 return 0;
3825}
3826
3827#define SPMI_REQUEST_IRQ(chip, rc, irq_name) \
3828do { \
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003829 rc = devm_request_irq(chip->dev, chip->irq_name##_irq.irq, \
3830 bms_##irq_name##_irq_handler, \
3831 IRQF_TRIGGER_RISING, #irq_name, chip); \
3832 if (rc < 0) { \
3833 pr_err("Unable to request " #irq_name " irq: %d\n", rc);\
3834 return -ENXIO; \
3835 } \
3836} while (0)
3837
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003838static int bms_request_irqs(struct qpnp_bms_chip *chip)
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003839{
3840 int rc;
3841
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003842 SPMI_REQUEST_IRQ(chip, rc, sw_cc_thr);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003843 enable_irq_wake(chip->sw_cc_thr_irq.irq);
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003844 SPMI_REQUEST_IRQ(chip, rc, ocv_thr);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003845 enable_irq_wake(chip->ocv_thr_irq.irq);
3846 return 0;
3847}
3848
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003849#define REG_OFFSET_PERP_TYPE 0x04
3850#define REG_OFFSET_PERP_SUBTYPE 0x05
3851#define BMS_BMS_TYPE 0xD
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003852#define BMS_BMS1_SUBTYPE 0x1
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003853#define BMS_IADC_TYPE 0x8
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003854#define BMS_IADC1_SUBTYPE 0x3
3855#define BMS_IADC2_SUBTYPE 0x5
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003856
3857static int register_spmi(struct qpnp_bms_chip *chip, struct spmi_device *spmi)
3858{
3859 struct spmi_resource *spmi_resource;
3860 struct resource *resource;
3861 int rc;
3862 u8 type, subtype;
3863
3864 chip->dev = &(spmi->dev);
3865 chip->spmi = spmi;
3866
3867 spmi_for_each_container_dev(spmi_resource, spmi) {
3868 if (!spmi_resource) {
3869 pr_err("qpnp_bms: spmi resource absent\n");
3870 return -ENXIO;
3871 }
3872
3873 resource = spmi_get_resource(spmi, spmi_resource,
3874 IORESOURCE_MEM, 0);
3875 if (!(resource && resource->start)) {
3876 pr_err("node %s IO resource absent!\n",
3877 spmi->dev.of_node->full_name);
3878 return -ENXIO;
3879 }
3880
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07003881 pr_debug("Node name = %s\n", spmi_resource->of_node->name);
3882
3883 if (strcmp("qcom,batt-pres-status",
3884 spmi_resource->of_node->name) == 0) {
3885 chip->batt_pres_addr = resource->start;
3886 continue;
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003887 } else if (strcmp("qcom,soc-storage-reg",
3888 spmi_resource->of_node->name) == 0) {
3889 chip->soc_storage_addr = resource->start;
3890 continue;
Xiaozhe Shi7c41a292013-08-16 16:50:17 -07003891 }
3892
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003893 rc = qpnp_read_wrapper(chip, &type,
3894 resource->start + REG_OFFSET_PERP_TYPE, 1);
3895 if (rc) {
3896 pr_err("Peripheral type read failed rc=%d\n", rc);
3897 return rc;
3898 }
3899 rc = qpnp_read_wrapper(chip, &subtype,
3900 resource->start + REG_OFFSET_PERP_SUBTYPE, 1);
3901 if (rc) {
3902 pr_err("Peripheral subtype read failed rc=%d\n", rc);
3903 return rc;
3904 }
3905
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003906 if (type == BMS_BMS_TYPE && subtype == BMS_BMS1_SUBTYPE) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003907 chip->base = resource->start;
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003908 rc = bms_find_irqs(chip, spmi_resource);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003909 if (rc) {
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07003910 pr_err("Could not find irqs\n");
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08003911 return rc;
3912 }
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003913 } else if (type == BMS_IADC_TYPE
Xiaozhe Shief6274c2013-03-06 15:23:52 -08003914 && (subtype == BMS_IADC1_SUBTYPE
3915 || subtype == BMS_IADC2_SUBTYPE)) {
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003916 chip->iadc_base = resource->start;
3917 } else {
3918 pr_err("Invalid peripheral start=0x%x type=0x%x, subtype=0x%x\n",
3919 resource->start, type, subtype);
3920 }
3921 }
3922
3923 if (chip->base == 0) {
3924 dev_err(&spmi->dev, "BMS peripheral was not registered\n");
3925 return -EINVAL;
3926 }
3927 if (chip->iadc_base == 0) {
3928 dev_err(&spmi->dev, "BMS_IADC peripheral was not registered\n");
3929 return -EINVAL;
3930 }
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003931 if (chip->soc_storage_addr == 0) {
3932 /* default to dvdd backed BMS data reg0 */
3933 chip->soc_storage_addr = chip->base + SOC_STORAGE_REG;
3934 }
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003935
Xiaozhe Shi12b25be2013-08-29 11:51:59 -07003936 pr_debug("bms-base = 0x%04x, iadc-base = 0x%04x, bat-pres-reg = 0x%04x, soc-storage-reg = 0x%04x\n",
3937 chip->base, chip->iadc_base,
3938 chip->batt_pres_addr, chip->soc_storage_addr);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003939 return 0;
3940}
3941
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07003942#define ADC_CH_SEL_MASK 0x7
3943#define ADC_INT_RSNSN_CTL_MASK 0x3
3944#define ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE 0x2
3945#define FAST_AVG_EN_MASK 0x80
3946#define FAST_AVG_EN_VALUE_EXT_RSENSE 0x80
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003947static int read_iadc_channel_select(struct qpnp_bms_chip *chip)
3948{
3949 u8 iadc_channel_select;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08003950 int32_t rds_rsense_nohm;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08003951 int rc;
3952
3953 rc = qpnp_read_wrapper(chip, &iadc_channel_select,
3954 chip->iadc_base + IADC1_BMS_ADC_CH_SEL_CTL, 1);
3955 if (rc) {
3956 pr_err("Error reading bms_iadc channel register %d\n", rc);
3957 return rc;
3958 }
3959
3960 iadc_channel_select &= ADC_CH_SEL_MASK;
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08003961 if (iadc_channel_select != EXTERNAL_RSENSE
3962 && iadc_channel_select != INTERNAL_RSENSE) {
3963 pr_err("IADC1_BMS_IADC configured incorrectly. Selected channel = %d\n",
3964 iadc_channel_select);
3965 return -EINVAL;
3966 }
3967
3968 if (chip->use_external_rsense) {
3969 pr_debug("External rsense selected\n");
3970 if (iadc_channel_select == INTERNAL_RSENSE) {
3971 pr_debug("Internal rsense detected; Changing rsense to external\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003972 rc = qpnp_masked_write_iadc(chip,
3973 IADC1_BMS_ADC_CH_SEL_CTL,
3974 ADC_CH_SEL_MASK,
3975 EXTERNAL_RSENSE);
3976 if (rc) {
3977 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
3978 IADC1_BMS_ADC_CH_SEL_CTL,
3979 EXTERNAL_RSENSE, rc);
3980 return rc;
3981 }
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003982 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
Xiaozhe Shi80ba88d2013-04-05 10:23:34 -07003983 chip->software_cc_uah = 0;
Xiaozhe Shif3da8622013-06-10 14:50:56 -07003984 chip->software_shdw_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003985 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08003986 } else {
3987 pr_debug("Internal rsense selected\n");
3988 if (iadc_channel_select == EXTERNAL_RSENSE) {
3989 pr_debug("External rsense detected; Changing rsense to internal\n");
Xiaozhe Shidffbe692012-12-11 15:35:46 -08003990 rc = qpnp_masked_write_iadc(chip,
3991 IADC1_BMS_ADC_CH_SEL_CTL,
3992 ADC_CH_SEL_MASK,
3993 INTERNAL_RSENSE);
3994 if (rc) {
3995 pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n",
3996 IADC1_BMS_ADC_CH_SEL_CTL,
3997 INTERNAL_RSENSE, rc);
3998 return rc;
3999 }
Xiaozhe Shif3da8622013-06-10 14:50:56 -07004000 reset_cc(chip, CLEAR_CC | CLEAR_SHDW_CC);
4001 chip->software_shdw_cc_uah = 0;
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004002 }
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004003
Siddartha Mohanadoss55d0bca2013-06-24 08:29:34 -07004004 rc = qpnp_iadc_get_rsense(chip->iadc_dev, &rds_rsense_nohm);
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08004005 if (rc) {
4006 pr_err("Unable to read RDS resistance value from IADC; rc = %d\n",
4007 rc);
4008 return rc;
4009 }
Xiaozhe Shid0a79542012-11-06 10:00:38 -08004010 chip->r_sense_uohm = rds_rsense_nohm/1000;
4011 pr_debug("rds_rsense = %d nOhm, saved as %d uOhm\n",
4012 rds_rsense_nohm, chip->r_sense_uohm);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004013 }
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004014 /* prevent shorting of leads by IADC_BMS when external Rsense is used */
4015 if (chip->use_external_rsense) {
4016 if (chip->iadc_bms_revision2 > CALIB_WRKARND_DIG_MAJOR_MAX) {
4017 rc = qpnp_masked_write_iadc(chip,
4018 IADC1_BMS_ADC_INT_RSNSN_CTL,
4019 ADC_INT_RSNSN_CTL_MASK,
4020 ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE);
4021 if (rc) {
4022 pr_err("Unable to set batfet config %x to %x: %d\n",
4023 IADC1_BMS_ADC_INT_RSNSN_CTL,
4024 ADC_INT_RSNSN_CTL_VALUE_EXT_RENSE, rc);
4025 return rc;
4026 }
4027 } else {
4028 /* In older PMICS use FAST_AVG_EN register bit 7 */
4029 rc = qpnp_masked_write_iadc(chip,
4030 IADC1_BMS_FAST_AVG_EN,
4031 FAST_AVG_EN_MASK,
4032 FAST_AVG_EN_VALUE_EXT_RSENSE);
4033 if (rc) {
4034 pr_err("Unable to set batfet config %x to %x: %d\n",
4035 IADC1_BMS_FAST_AVG_EN,
4036 FAST_AVG_EN_VALUE_EXT_RSENSE, rc);
4037 return rc;
4038 }
4039 }
4040 }
4041
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004042 return 0;
4043}
4044
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004045static int refresh_die_temp_monitor(struct qpnp_bms_chip *chip)
4046{
4047 struct qpnp_vadc_result result;
4048 int rc;
4049
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07004050 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004051
4052 pr_debug("low = %lld, high = %lld\n",
4053 result.physical - chip->temperature_margin,
4054 result.physical + chip->temperature_margin);
4055 chip->die_temp_monitor_params.high_temp = result.physical
4056 + chip->temperature_margin;
4057 chip->die_temp_monitor_params.low_temp = result.physical
4058 - chip->temperature_margin;
4059 chip->die_temp_monitor_params.state_request =
4060 ADC_TM_HIGH_LOW_THR_ENABLE;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004061 return qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
4062 &chip->die_temp_monitor_params);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004063}
4064
4065static void btm_notify_die_temp(enum qpnp_tm_state state, void *ctx)
4066{
4067 struct qpnp_bms_chip *chip = ctx;
4068 struct qpnp_vadc_result result;
4069 int rc;
4070
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07004071 rc = qpnp_vadc_read(chip->vadc_dev, DIE_TEMP, &result);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004072
4073 if (state == ADC_TM_LOW_STATE)
4074 pr_debug("low state triggered\n");
4075 else if (state == ADC_TM_HIGH_STATE)
4076 pr_debug("high state triggered\n");
4077 pr_debug("die temp = %lld, raw = 0x%x\n",
4078 result.physical, result.adc_code);
4079 schedule_work(&chip->recalc_work);
4080 refresh_die_temp_monitor(chip);
4081}
4082
4083static int setup_die_temp_monitoring(struct qpnp_bms_chip *chip)
4084{
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004085 int rc;
4086
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004087 chip->die_temp_monitor_params.channel = DIE_TEMP;
4088 chip->die_temp_monitor_params.btm_ctx = (void *)chip;
4089 chip->die_temp_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S;
4090 chip->die_temp_monitor_params.threshold_notification =
4091 &btm_notify_die_temp;
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004092 rc = refresh_die_temp_monitor(chip);
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004093 if (rc) {
4094 pr_err("tm setup failed: %d\n", rc);
4095 return rc;
4096 }
4097 pr_debug("setup complete\n");
4098 return 0;
4099}
4100
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004101static int __devinit qpnp_bms_probe(struct spmi_device *spmi)
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004102{
4103 struct qpnp_bms_chip *chip;
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004104 bool warm_reset;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004105 int rc, vbatt;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004106
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07004107 chip = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_bms_chip),
4108 GFP_KERNEL);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004109
4110 if (chip == NULL) {
4111 pr_err("kzalloc() failed.\n");
4112 return -ENOMEM;
4113 }
4114
Siddartha Mohanadoss88a3fde2013-06-24 16:18:52 -07004115 rc = bms_get_adc(chip, spmi);
4116 if (rc < 0)
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004117 goto error_read;
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004118
Xiaozhe Shi3eaf0b62013-07-11 09:48:08 -07004119 mutex_init(&chip->bms_output_lock);
4120 mutex_init(&chip->last_ocv_uv_mutex);
4121 mutex_init(&chip->vbat_monitor_mutex);
4122 mutex_init(&chip->soc_invalidation_mutex);
4123 mutex_init(&chip->last_soc_mutex);
Xiaozhe Shibda84992013-09-05 10:39:11 -07004124 mutex_init(&chip->status_lock);
Xiaozhe Shi27375822013-08-22 11:40:15 -07004125 init_waitqueue_head(&chip->bms_wait_queue);
Xiaozhe Shi3eaf0b62013-07-11 09:48:08 -07004126
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004127 warm_reset = qpnp_pon_is_warm_reset();
4128 rc = warm_reset;
4129 if (rc < 0)
4130 goto error_read;
4131
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004132 rc = register_spmi(chip, spmi);
4133 if (rc) {
4134 pr_err("error registering spmi resource %d\n", rc);
4135 goto error_resource;
4136 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004137
4138 rc = qpnp_read_wrapper(chip, &chip->revision1,
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004139 chip->base + REVISION1, 1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004140 if (rc) {
4141 pr_err("error reading version register %d\n", rc);
4142 goto error_read;
4143 }
4144
4145 rc = qpnp_read_wrapper(chip, &chip->revision2,
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004146 chip->base + REVISION2, 1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004147 if (rc) {
4148 pr_err("Error reading version register %d\n", rc);
4149 goto error_read;
4150 }
Xiaozhe Shia045a562012-11-28 16:55:39 -08004151 pr_debug("BMS version: %hhu.%hhu\n", chip->revision2, chip->revision1);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004152
Abhijeet Dharmapurikar604461d2013-07-09 13:34:33 -07004153 rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision2,
4154 chip->iadc_base + REVISION2, 1);
4155 if (rc) {
4156 pr_err("Error reading version register %d\n", rc);
4157 goto error_read;
4158 }
4159
4160 rc = qpnp_read_wrapper(chip, &chip->iadc_bms_revision1,
4161 chip->iadc_base + REVISION1, 1);
4162 if (rc) {
4163 pr_err("Error reading version register %d\n", rc);
4164 goto error_read;
4165 }
4166 pr_debug("IADC_BMS version: %hhu.%hhu\n",
4167 chip->iadc_bms_revision2, chip->iadc_bms_revision1);
4168
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004169 rc = bms_read_properties(chip);
4170 if (rc) {
4171 pr_err("Unable to read all bms properties, rc = %d\n", rc);
4172 goto error_read;
4173 }
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004174
Xiaozhe Shidffbe692012-12-11 15:35:46 -08004175 rc = read_iadc_channel_select(chip);
4176 if (rc) {
4177 pr_err("Unable to get iadc selected channel = %d\n", rc);
4178 goto error_read;
4179 }
4180
Xiaozhe Shibdf14742012-12-05 12:41:48 -08004181 if (chip->use_ocv_thresholds) {
4182 rc = set_ocv_voltage_thresholds(chip,
4183 chip->ocv_low_threshold_uv,
4184 chip->ocv_high_threshold_uv);
4185 if (rc) {
4186 pr_err("Could not set ocv voltage thresholds: %d\n",
4187 rc);
4188 goto error_read;
4189 }
4190 }
4191
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004192 rc = set_battery_data(chip);
4193 if (rc) {
4194 pr_err("Bad battery data %d\n", rc);
4195 goto error_read;
4196 }
4197
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004198 bms_initialize_constants(chip);
4199
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004200 wakeup_source_init(&chip->soc_wake_source.source, "qpnp_soc_wake");
Xiaozhe Shi4be85782013-02-22 17:33:40 -08004201 wake_lock_init(&chip->low_voltage_wake_lock, WAKE_LOCK_SUSPEND,
4202 "qpnp_low_voltage_lock");
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004203 wake_lock_init(&chip->cv_wake_lock, WAKE_LOCK_SUSPEND,
4204 "qpnp_cv_lock");
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004205 INIT_DELAYED_WORK(&chip->calculate_soc_delayed_work,
4206 calculate_soc_work);
Xiaozhe Shi5cf7a672013-02-06 17:18:05 -08004207 INIT_WORK(&chip->recalc_work, recalculate_work);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07004208 INIT_WORK(&chip->batfet_open_work, batfet_open_work);
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004209
Xiaozhe Shif9f99242013-08-29 12:27:50 -07004210 dev_set_drvdata(&spmi->dev, chip);
4211 device_init_wakeup(&spmi->dev, 1);
4212
4213 load_shutdown_data(chip);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004214
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05304215 if (chip->enable_fcc_learning) {
Anirudh Ghayale0c02932013-07-08 16:26:35 +05304216 if (chip->battery_removed) {
4217 rc = discard_backup_fcc_data(chip);
4218 if (rc)
4219 pr_err("Could not discard backed-up FCC data\n");
4220 } else {
4221 rc = read_chgcycle_data_from_backup(chip);
4222 if (rc)
4223 pr_err("Unable to restore charge-cycle data\n");
4224
4225 rc = read_fcc_data_from_backup(chip);
4226 if (rc)
4227 pr_err("Unable to restore FCC-learning data\n");
4228 else
4229 attempt_learning_new_fcc(chip);
Anirudh Ghayal9dd582d2013-06-07 17:48:58 +05304230 }
4231 }
Anirudh Ghayald71d9f82013-06-05 11:11:46 +05304232
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004233 rc = setup_vbat_monitoring(chip);
4234 if (rc < 0) {
4235 pr_err("failed to set up voltage notifications: %d\n", rc);
4236 goto error_setup;
Xiaozhe Shid5d21412013-02-06 17:14:41 -08004237 }
4238
Xiaozhe Shi535494d2013-04-05 12:27:51 -07004239 rc = setup_die_temp_monitoring(chip);
4240 if (rc < 0) {
4241 pr_err("failed to set up die temp notifications: %d\n", rc);
4242 goto error_setup;
4243 }
4244
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07004245 battery_insertion_check(chip);
Abhijeet Dharmapurikar84b13dd2013-07-08 18:43:56 -07004246 batfet_status_check(chip);
Xiaozhe Shie7fafe62013-06-05 15:25:16 -07004247 battery_status_check(chip);
4248
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004249 calculate_soc_work(&(chip->calculate_soc_delayed_work.work));
4250
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004251 /* setup & register the battery power supply */
4252 chip->bms_psy.name = "bms";
4253 chip->bms_psy.type = POWER_SUPPLY_TYPE_BMS;
4254 chip->bms_psy.properties = msm_bms_power_props;
4255 chip->bms_psy.num_properties = ARRAY_SIZE(msm_bms_power_props);
4256 chip->bms_psy.get_property = qpnp_bms_power_get_property;
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004257 chip->bms_psy.external_power_changed =
4258 qpnp_bms_external_power_changed;
4259 chip->bms_psy.supplied_to = qpnp_bms_supplicants;
4260 chip->bms_psy.num_supplicants = ARRAY_SIZE(qpnp_bms_supplicants);
4261
4262 rc = power_supply_register(chip->dev, &chip->bms_psy);
4263
4264 if (rc < 0) {
4265 pr_err("power_supply_register bms failed rc = %d\n", rc);
4266 goto unregister_dc;
4267 }
4268
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07004269 chip->bms_psy_registered = true;
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004270 vbatt = 0;
Siddartha Mohanadoss3cb2b6b2013-06-21 12:07:05 -07004271 rc = get_battery_voltage(chip, &vbatt);
Xiaozhe Shi36458962013-02-06 16:19:57 -08004272 if (rc) {
4273 pr_err("error reading vbat_sns adc channel = %d, rc = %d\n",
4274 VBAT_SNS, rc);
4275 goto unregister_dc;
4276 }
Xiaozhe Shicd7e5302012-10-17 12:29:53 -07004277
Abhijeet Dharmapurikar3bd4bbf2013-07-01 12:06:37 -07004278 rc = bms_request_irqs(chip);
4279 if (rc) {
4280 pr_err("error requesting bms irqs, rc = %d\n", rc);
4281 goto unregister_dc;
4282 }
4283
Xiaozhe Shi77ec38d2013-04-29 16:41:58 -07004284 pr_info("probe success: soc =%d vbatt = %d ocv = %d r_sense_uohm = %u warm_reset = %d\n",
4285 get_prop_bms_capacity(chip), vbatt, chip->last_ocv_uv,
4286 chip->r_sense_uohm, warm_reset);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004287 return 0;
4288
4289unregister_dc:
Abhijeet Dharmapurikar8e322492013-06-25 19:48:18 -07004290 chip->bms_psy_registered = false;
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004291 power_supply_unregister(&chip->bms_psy);
4292error_setup:
4293 dev_set_drvdata(&spmi->dev, NULL);
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004294 wakeup_source_trash(&chip->soc_wake_source.source);
Xiaozhe Shi4be85782013-02-22 17:33:40 -08004295 wake_lock_destroy(&chip->low_voltage_wake_lock);
Xiaozhe Shia6618a22013-03-27 10:26:29 -07004296 wake_lock_destroy(&chip->cv_wake_lock);
Xiaozhe Shic40b3972012-11-30 14:11:16 -08004297error_resource:
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004298error_read:
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004299 return rc;
4300}
4301
Xiaozhe Shi81f66b52013-09-20 11:43:52 -07004302static int qpnp_bms_remove(struct spmi_device *spmi)
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004303{
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004304 dev_set_drvdata(&spmi->dev, NULL);
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004305 return 0;
4306}
4307
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004308static int bms_suspend(struct device *dev)
4309{
4310 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
4311
4312 cancel_delayed_work_sync(&chip->calculate_soc_delayed_work);
Xiaozhe Shi04da0992013-04-26 16:32:14 -07004313 chip->was_charging_at_sleep = is_battery_charging(chip);
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004314 return 0;
4315}
4316
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004317static int bms_resume(struct device *dev)
4318{
4319 int rc;
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004320 int soc_calc_period;
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004321 int time_until_next_recalc = 0;
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004322 unsigned long time_since_last_recalc;
4323 unsigned long tm_now_sec;
4324 struct qpnp_bms_chip *chip = dev_get_drvdata(dev);
4325
4326 rc = get_current_time(&tm_now_sec);
4327 if (rc) {
4328 pr_err("Could not read current time: %d\n", rc);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004329 } else {
Xiaozhe Shicb487b12013-10-14 17:42:07 -07004330 soc_calc_period = get_calculation_delay_ms(chip);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004331 time_since_last_recalc = tm_now_sec - chip->last_recalc_time;
4332 pr_debug("Time since last recalc: %lu\n",
4333 time_since_last_recalc);
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004334 time_until_next_recalc = max(0, soc_calc_period
4335 - (int)(time_since_last_recalc * 1000));
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004336 }
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004337
Xiaozhe Shi1ae8a952013-02-14 12:23:31 -08004338 if (time_until_next_recalc == 0)
4339 bms_stay_awake(&chip->soc_wake_source);
Xiaozhe Shid921f9f72013-05-23 18:55:15 -07004340 schedule_delayed_work(&chip->calculate_soc_delayed_work,
4341 round_jiffies_relative(msecs_to_jiffies
4342 (time_until_next_recalc)));
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004343 return 0;
4344}
4345
4346static const struct dev_pm_ops qpnp_bms_pm_ops = {
4347 .resume = bms_resume,
Xiaozhe Shid6168d82013-04-18 16:06:17 -07004348 .suspend = bms_suspend,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004349};
4350
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004351static struct spmi_driver qpnp_bms_driver = {
4352 .probe = qpnp_bms_probe,
4353 .remove = __devexit_p(qpnp_bms_remove),
4354 .driver = {
4355 .name = QPNP_BMS_DEV_NAME,
4356 .owner = THIS_MODULE,
4357 .of_match_table = qpnp_bms_match_table,
Xiaozhe Shicdeee312012-12-18 15:10:18 -08004358 .pm = &qpnp_bms_pm_ops,
Xiaozhe Shib19f7032012-08-16 12:14:16 -07004359 },
4360};
4361
4362static int __init qpnp_bms_init(void)
4363{
4364 pr_info("QPNP BMS INIT\n");
4365 return spmi_driver_register(&qpnp_bms_driver);
4366}
4367
4368static void __exit qpnp_bms_exit(void)
4369{
4370 pr_info("QPNP BMS EXIT\n");
4371 return spmi_driver_unregister(&qpnp_bms_driver);
4372}
4373
4374module_init(qpnp_bms_init);
4375module_exit(qpnp_bms_exit);
4376
4377MODULE_DESCRIPTION("QPNP BMS Driver");
4378MODULE_LICENSE("GPL v2");
4379MODULE_ALIAS("platform:" QPNP_BMS_DEV_NAME);