blob: c09373ae9db1e86522b537c74874336bbe7cdb84 [file] [log] [blame]
Abhijeet Dharmapurikarb2aeb7e2012-12-26 21:30:05 -08001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -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 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070013
Xiaozhe Shi5ee95192012-09-12 15:16:34 -070014#define pr_fmt(fmt) "%s: " fmt, __func__
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070015#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/platform_device.h>
18#include <linux/errno.h>
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -080019#include <linux/power_supply.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070020#include <linux/mfd/pm8xxx/pm8921-bms.h>
21#include <linux/mfd/pm8xxx/core.h>
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -070022#include <linux/mfd/pm8xxx/pm8xxx-adc.h>
Abhijeet Dharmapurikarb2680132012-05-15 16:32:51 -070023#include <linux/mfd/pm8xxx/pm8921-charger.h>
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -080024#include <linux/mfd/pm8xxx/ccadc.h>
Xiaozhe Shi5ee95192012-09-12 15:16:34 -070025#include <linux/mfd/pm8xxx/batterydata-lib.h>
Anirudh Ghayal6fe4ffa2013-02-01 11:44:33 +053026#include <linux/mfd/pm8xxx/batt-alarm.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027#include <linux/interrupt.h>
28#include <linux/bitops.h>
29#include <linux/debugfs.h>
30#include <linux/slab.h>
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070031#include <linux/delay.h>
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -080032#include <linux/mutex.h>
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -070033#include <linux/rtc.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070034
35#define BMS_CONTROL 0x224
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -080036#define BMS_S1_DELAY 0x225
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037#define BMS_OUTPUT0 0x230
38#define BMS_OUTPUT1 0x231
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -080039#define BMS_TOLERANCES 0x232
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040#define BMS_TEST1 0x237
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070041
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070042#define ADC_ARB_SECP_CNTRL 0x190
43#define ADC_ARB_SECP_AMUX_CNTRL 0x191
44#define ADC_ARB_SECP_ANA_PARAM 0x192
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070045#define ADC_ARB_SECP_DIG_PARAM 0x193
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070046#define ADC_ARB_SECP_RSV 0x194
47#define ADC_ARB_SECP_DATA1 0x195
48#define ADC_ARB_SECP_DATA0 0x196
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070050#define ADC_ARB_BMS_CNTRL 0x18D
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -070051#define AMUX_TRIM_2 0x322
52#define TEST_PROGRAM_REV 0x339
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070053
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -070054#define TEMP_SOC_STORAGE 0x107
55
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -070056#define TEMP_IAVG_STORAGE 0x105
57#define TEMP_IAVG_STORAGE_USE_MASK 0x0F
58
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -080059#define PON_CNTRL_6 0x018
60#define WD_BIT BIT(7)
61
Anirudh Ghayal0c7c2bc2013-03-22 12:57:50 +053062#define BATT_ALARM_ACCURACY 50 /* 50mV */
63
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070064enum pmic_bms_interrupts {
65 PM8921_BMS_SBI_WRITE_OK,
66 PM8921_BMS_CC_THR,
67 PM8921_BMS_VSENSE_THR,
68 PM8921_BMS_VSENSE_FOR_R,
69 PM8921_BMS_OCV_FOR_R,
70 PM8921_BMS_GOOD_OCV,
71 PM8921_BMS_VSENSE_AVG,
72 PM_BMS_MAX_INTS,
73};
74
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080075struct pm8921_soc_params {
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -080076 uint16_t last_good_ocv_raw;
77 int cc;
78
79 int last_good_ocv_uv;
80};
81
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080082/**
83 * struct pm8921_bms_chip -
84 * @bms_output_lock: lock to prevent concurrent bms reads
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080085 *
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -070086 * @last_ocv_uv_mutex: mutex to protect simultaneous invocations of calculate
87 * state of charge, note that last_ocv_uv could be
88 * changed as soc is adjusted. This mutex protects
89 * simultaneous updates of last_ocv_uv as well. This mutex
90 * also protects changes to *_at_100 variables used in
91 * faking 100% SOC.
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080092 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070093struct pm8921_bms_chip {
94 struct device *dev;
95 struct dentry *dent;
Xiaozhe Shid69c91e2012-11-06 10:00:38 -080096 int r_sense_uohm;
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -070097 unsigned int v_cutoff;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098 unsigned int fcc;
99 struct single_row_lut *fcc_temp_lut;
100 struct single_row_lut *fcc_sf_lut;
101 struct pc_temp_ocv_lut *pc_temp_ocv_lut;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800102 struct sf_lut *pc_sf_lut;
103 struct sf_lut *rbatt_sf_lut;
Abhijeet Dharmapurikarded189b2012-03-30 10:12:28 -0700104 int delta_rbatt_mohm;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700105 struct work_struct calib_hkadc_work;
Xiaozhe Shid035f6a2012-12-18 16:16:33 -0800106 unsigned long last_calib_time;
107 int last_calib_temp;
Abhijeet Dharmapurikar9c0e6042012-07-12 14:55:13 -0700108 struct mutex calib_mutex;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700109 unsigned int revision;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800110 unsigned int xoadc_v0625_usb_present;
111 unsigned int xoadc_v0625_usb_absent;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700112 unsigned int xoadc_v0625;
113 unsigned int xoadc_v125;
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700114 unsigned int batt_temp_channel;
115 unsigned int vbat_channel;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700116 unsigned int ref625mv_channel;
117 unsigned int ref1p25v_channel;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -0700118 unsigned int batt_id_channel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119 unsigned int pmic_bms_irq[PM_BMS_MAX_INTS];
120 DECLARE_BITMAP(enabled_irqs, PM_BMS_MAX_INTS);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800121 struct mutex bms_output_lock;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700122 struct single_row_lut *adjusted_fcc_temp_lut;
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -0700123 unsigned int charging_began;
124 unsigned int start_percent;
125 unsigned int end_percent;
Anirudh Ghayal6fe4ffa2013-02-01 11:44:33 +0530126 unsigned int alarm_low_mv;
127 unsigned int alarm_high_mv;
128
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -0700129 int charge_time_us;
130 int catch_up_time_us;
David Keitel35e11872012-02-17 17:40:42 -0800131 enum battery_type batt_type;
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -0800132 uint16_t ocv_reading_at_100;
Abhijeet Dharmapurikara93ede82011-11-17 12:20:03 -0800133 int max_voltage_uv;
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800134
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -0700135 int chg_term_ua;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800136 int default_rbatt_mohm;
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700137 int amux_2_trim_delta;
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -0700138 uint16_t prev_last_good_ocv_raw;
Abhijeet Dharmapurikara82a00a2012-11-08 17:26:29 -0800139 int rconn_mohm;
Abhijeet Dharmapurikar03c6c232013-02-13 17:57:40 -0800140 int rbatt_capacitive_mohm;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -0700141 struct mutex last_ocv_uv_mutex;
142 int last_ocv_uv;
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -0800143 int last_ocv_temp_decidegc;
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -0700144 int pon_ocv_uv;
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -0700145 int last_cc_uah;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -0700146 unsigned long tm_sec;
Abhijeet Dharmapurikare88f2462012-04-25 18:22:38 -0700147 int enable_fcc_learning;
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -0700148 int shutdown_soc;
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -0700149 int shutdown_iavg_ua;
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -0700150 struct delayed_work calculate_soc_delayed_work;
151 struct timespec t_soc_queried;
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -0800152 unsigned long last_recalc_time;
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -0700153 int shutdown_soc_valid_limit;
Abhijeet Dharmapurikar1f4cc512012-07-21 00:10:39 -0700154 int ignore_shutdown_soc;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -0700155 int prev_iavg_ua;
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -0700156 int prev_uuc_iavg_ma;
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -0700157 int prev_pc_unusable;
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -0700158 int adjust_soc_low_threshold;
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -0700159
160 int ibat_at_cv_ua;
161 int soc_at_cv;
162 int prev_chg_soc;
Abhijeet Dharmapurikar0622a432012-08-28 16:34:18 -0700163 struct power_supply *batt_psy;
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -0700164 struct wake_lock low_voltage_wake_lock;
Abhijeet Dharmapurikar683472d2012-10-30 16:21:58 -0700165 int soc_calc_period;
166 int normal_voltage_calc_ms;
167 int low_voltage_calc_ms;
Abhijeet Dharmapurikareca7b3e2012-12-26 21:30:29 -0800168 int imax_ua;
Abhijeet Dharmapurikarb2aeb7e2012-12-26 21:30:05 -0800169 struct wake_lock soc_wake_lock;
Abhijeet Dharmapurikar1939ede2013-01-10 15:59:33 -0800170 int disable_flat_portion_ocv;
171 int ocv_dis_high_soc;
172 int ocv_dis_low_soc;
Abhijeet Dharmapurikar48707612013-03-26 13:14:03 -0700173 int high_ocv_correction_limit_uv;
174 int low_ocv_correction_limit_uv;
175 int hold_soc_est;
Abhijeet Dharmapurikarcbde3db2013-01-28 15:09:04 -0800176 int prev_vbat_batt_terminal_uv;
Anirudh Ghayal0c7c2bc2013-03-22 12:57:50 +0530177 int vbatt_cutoff_count;
178 int low_voltage_detect;
179 int vbatt_cutoff_retries;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180};
181
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -0700182/*
183 * protects against simultaneous adjustment of ocv based on shutdown soc and
184 * invalidating the shutdown soc
185 */
186static DEFINE_MUTEX(soc_invalidation_mutex);
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -0700187static int shutdown_soc_invalid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188static struct pm8921_bms_chip *the_chip;
189
190#define DEFAULT_RBATT_MOHMS 128
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191#define DEFAULT_OCV_MICROVOLTS 3900000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192#define DEFAULT_CHARGE_CYCLES 0
193
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800194static int last_usb_cal_delta_uv = 1800;
195module_param(last_usb_cal_delta_uv, int, 0644);
196
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700197static int last_chargecycles = DEFAULT_CHARGE_CYCLES;
198static int last_charge_increase;
199module_param(last_chargecycles, int, 0644);
200module_param(last_charge_increase, int, 0644);
201
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -0700202static int calculated_soc = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700203static int last_soc = -EINVAL;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800204static int last_real_fcc_mah = -EINVAL;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700205static int last_real_fcc_batt_temp = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206
Anirudh Ghayal6fe4ffa2013-02-01 11:44:33 +0530207static int pm8921_battery_gauge_alarm_notify(struct notifier_block *nb,
208 unsigned long status, void *unused);
209
210static struct notifier_block alarm_notifier = {
211 .notifier_call = pm8921_battery_gauge_alarm_notify,
212};
213
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700214static int bms_ops_set(const char *val, const struct kernel_param *kp)
215{
216 if (*(int *)kp->arg == -EINVAL)
217 return param_set_int(val, kp);
218 else
219 return 0;
220}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700221
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700222static struct kernel_param_ops bms_param_ops = {
223 .set = bms_ops_set,
224 .get = param_get_int,
225};
226
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700227module_param_cb(last_soc, &bms_param_ops, &last_soc, 0644);
228
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -0800229/*
Abhijeet Dharmapurikar986bd8a2012-01-05 15:15:15 -0800230 * bms_fake_battery is set in setups where a battery emulator is used instead
231 * of a real battery. This makes the bms driver report a different/fake value
232 * regardless of the calculated state of charge.
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -0800233 */
Abhijeet Dharmapurikar986bd8a2012-01-05 15:15:15 -0800234static int bms_fake_battery = -EINVAL;
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -0800235module_param(bms_fake_battery, int, 0644);
236
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800237/* bms_start_XXX and bms_end_XXX are read only */
238static int bms_start_percent;
239static int bms_start_ocv_uv;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800240static int bms_start_cc_uah;
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800241static int bms_end_percent;
242static int bms_end_ocv_uv;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800243static int bms_end_cc_uah;
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800244
245static int bms_ro_ops_set(const char *val, const struct kernel_param *kp)
246{
247 return -EINVAL;
248}
249
250static struct kernel_param_ops bms_ro_param_ops = {
251 .set = bms_ro_ops_set,
252 .get = param_get_int,
253};
254module_param_cb(bms_start_percent, &bms_ro_param_ops, &bms_start_percent, 0644);
255module_param_cb(bms_start_ocv_uv, &bms_ro_param_ops, &bms_start_ocv_uv, 0644);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800256module_param_cb(bms_start_cc_uah, &bms_ro_param_ops, &bms_start_cc_uah, 0644);
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800257
258module_param_cb(bms_end_percent, &bms_ro_param_ops, &bms_end_percent, 0644);
259module_param_cb(bms_end_ocv_uv, &bms_ro_param_ops, &bms_end_ocv_uv, 0644);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800260module_param_cb(bms_end_cc_uah, &bms_ro_param_ops, &bms_end_cc_uah, 0644);
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800261
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700262static void readjust_fcc_table(void)
263{
264 struct single_row_lut *temp, *old;
265 int i, fcc, ratio;
266
267 if (!the_chip->fcc_temp_lut) {
268 pr_err("The static fcc lut table is NULL\n");
269 return;
270 }
271
272 temp = kzalloc(sizeof(struct single_row_lut), GFP_KERNEL);
273 if (!temp) {
274 pr_err("Cannot allocate memory for adjusted fcc table\n");
275 return;
276 }
277
Xiaozhe Shi5ee95192012-09-12 15:16:34 -0700278 fcc = interpolate_fcc(the_chip->fcc_temp_lut, last_real_fcc_batt_temp);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700279
280 temp->cols = the_chip->fcc_temp_lut->cols;
281 for (i = 0; i < the_chip->fcc_temp_lut->cols; i++) {
282 temp->x[i] = the_chip->fcc_temp_lut->x[i];
283 ratio = div_u64(the_chip->fcc_temp_lut->y[i] * 1000, fcc);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800284 temp->y[i] = (ratio * last_real_fcc_mah);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700285 temp->y[i] /= 1000;
286 pr_debug("temp=%d, staticfcc=%d, adjfcc=%d, ratio=%d\n",
287 temp->x[i], the_chip->fcc_temp_lut->y[i],
288 temp->y[i], ratio);
289 }
290
291 old = the_chip->adjusted_fcc_temp_lut;
292 the_chip->adjusted_fcc_temp_lut = temp;
293 kfree(old);
294}
295
296static int bms_last_real_fcc_set(const char *val,
297 const struct kernel_param *kp)
298{
299 int rc = 0;
300
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800301 if (last_real_fcc_mah == -EINVAL)
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700302 rc = param_set_int(val, kp);
303 if (rc) {
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800304 pr_err("Failed to set last_real_fcc_mah rc=%d\n", rc);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700305 return rc;
306 }
307 if (last_real_fcc_batt_temp != -EINVAL)
308 readjust_fcc_table();
309 return rc;
310}
311static struct kernel_param_ops bms_last_real_fcc_param_ops = {
312 .set = bms_last_real_fcc_set,
313 .get = param_get_int,
314};
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800315module_param_cb(last_real_fcc_mah, &bms_last_real_fcc_param_ops,
316 &last_real_fcc_mah, 0644);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700317
318static int bms_last_real_fcc_batt_temp_set(const char *val,
319 const struct kernel_param *kp)
320{
321 int rc = 0;
322
323 if (last_real_fcc_batt_temp == -EINVAL)
324 rc = param_set_int(val, kp);
325 if (rc) {
326 pr_err("Failed to set last_real_fcc_batt_temp rc=%d\n", rc);
327 return rc;
328 }
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800329 if (last_real_fcc_mah != -EINVAL)
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700330 readjust_fcc_table();
331 return rc;
332}
333
334static struct kernel_param_ops bms_last_real_fcc_batt_temp_param_ops = {
335 .set = bms_last_real_fcc_batt_temp_set,
336 .get = param_get_int,
337};
338module_param_cb(last_real_fcc_batt_temp, &bms_last_real_fcc_batt_temp_param_ops,
339 &last_real_fcc_batt_temp, 0644);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700340
341static int pm_bms_get_rt_status(struct pm8921_bms_chip *chip, int irq_id)
342{
343 return pm8xxx_read_irq_stat(chip->dev->parent,
344 chip->pmic_bms_irq[irq_id]);
345}
346
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700347static void pm8921_bms_enable_irq(struct pm8921_bms_chip *chip, int interrupt)
348{
349 if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) {
350 dev_dbg(chip->dev, "%s %d\n", __func__,
351 chip->pmic_bms_irq[interrupt]);
352 enable_irq(chip->pmic_bms_irq[interrupt]);
353 }
354}
355
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700356static void pm8921_bms_disable_irq(struct pm8921_bms_chip *chip, int interrupt)
357{
358 if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700359 pr_debug("%d\n", chip->pmic_bms_irq[interrupt]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700360 disable_irq_nosync(chip->pmic_bms_irq[interrupt]);
361 }
362}
363
364static int pm_bms_masked_write(struct pm8921_bms_chip *chip, u16 addr,
365 u8 mask, u8 val)
366{
367 int rc;
368 u8 reg;
369
370 rc = pm8xxx_readb(chip->dev->parent, addr, &reg);
371 if (rc) {
372 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
373 return rc;
374 }
375 reg &= ~mask;
376 reg |= val & mask;
377 rc = pm8xxx_writeb(chip->dev->parent, addr, reg);
378 if (rc) {
379 pr_err("write failed addr = %03X, rc = %d\n", addr, rc);
380 return rc;
381 }
382 return 0;
383}
384
Abhijeet Dharmapurikar9e948982012-08-28 15:21:23 -0700385static int usb_chg_plugged_in(struct pm8921_bms_chip *chip)
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800386{
Abhijeet Dharmapurikar3da61762012-07-12 15:56:58 -0700387 int val = pm8921_is_usb_chg_plugged_in();
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800388
Abhijeet Dharmapurikar9e948982012-08-28 15:21:23 -0700389 /* if the charger driver was not initialized, use the restart reason */
390 if (val == -EINVAL) {
391 if (pm8xxx_restart_reason(chip->dev->parent)
392 == PM8XXX_RESTART_CHG)
393 val = 1;
394 else
395 val = 0;
396 }
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800397
Abhijeet Dharmapurikar3da61762012-07-12 15:56:58 -0700398 return val;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800399}
400
Anirudh Ghayal0c7c2bc2013-03-22 12:57:50 +0530401static void pm8921_bms_low_voltage_config(struct pm8921_bms_chip *chip,
402 int time_ms)
403{
404 int ms = 0;
405
406 /* if work was pending and was cancelled, calculate SOC immediately */
407 if (!cancel_delayed_work_sync(&chip->calculate_soc_delayed_work))
408 ms = time_ms;
409
410 chip->soc_calc_period = time_ms;
411 schedule_delayed_work(&chip->calculate_soc_delayed_work,
412 msecs_to_jiffies(ms));
413}
414
Anirudh Ghayal6fe4ffa2013-02-01 11:44:33 +0530415static int pm8921_bms_enable_batt_alarm(struct pm8921_bms_chip *chip)
416{
417 int rc = 0;
418
419 rc = pm8xxx_batt_alarm_enable(PM8XXX_BATT_ALARM_LOWER_COMPARATOR);
420 if (!rc)
421 rc = pm8xxx_batt_alarm_disable(
422 PM8XXX_BATT_ALARM_UPPER_COMPARATOR);
423 if (rc) {
424 pr_err("unable to set batt alarm state rc=%d\n", rc);
425 return rc;
426 }
427
428 return rc;
429}
430
431static int pm8921_bms_configure_batt_alarm(struct pm8921_bms_chip *chip)
432{
433 int rc = 0;
434
435 rc = pm8xxx_batt_alarm_disable(PM8XXX_BATT_ALARM_UPPER_COMPARATOR);
436 if (!rc)
437 rc = pm8xxx_batt_alarm_disable(
438 PM8XXX_BATT_ALARM_LOWER_COMPARATOR);
439 if (rc) {
440 pr_err("unable to set batt alarm state rc=%d\n", rc);
441 return rc;
442 }
443
444 /*
445 * The batt-alarm driver requires sane values for both min / max,
446 * regardless of whether they're both activated.
447 */
448 rc = pm8xxx_batt_alarm_threshold_set(
449 PM8XXX_BATT_ALARM_LOWER_COMPARATOR,
450 chip->alarm_low_mv);
451 if (!rc)
452 rc = pm8xxx_batt_alarm_threshold_set(
453 PM8XXX_BATT_ALARM_UPPER_COMPARATOR,
454 chip->alarm_high_mv);
455 if (rc) {
456 pr_err("unable to set batt alarm threshold rc=%d\n", rc);
457 return rc;
458 }
459
460 rc = pm8xxx_batt_alarm_hold_time_set(
461 PM8XXX_BATT_ALARM_HOLD_TIME_16_MS);
462 if (rc) {
463 pr_err("unable to set batt alarm hold time rc=%d\n", rc);
464 return rc;
465 }
466
467 /* PWM enabled at 2Hz */
468 rc = pm8xxx_batt_alarm_pwm_rate_set(1, 7, 4);
469 if (rc) {
470 pr_err("unable to set batt alarm pwm rate rc=%d\n", rc);
471 return rc;
472 }
473
474 rc = pm8xxx_batt_alarm_register_notifier(&alarm_notifier);
475 if (rc) {
476 pr_err("unable to register alarm notifier rc=%d\n", rc);
477 return rc;
478 }
479
480 return rc;
481}
482
483static int pm8921_battery_gauge_alarm_notify(struct notifier_block *nb,
484 unsigned long status, void *unused)
485{
486 int rc;
487
488 if (!the_chip) {
489 pr_err("not initialized\n");
490 return -EINVAL;
491 }
492
493 switch (status) {
494 case 0:
495 pr_debug("spurious interrupt\n");
496 break;
497 case 1:
498 pr_debug("Low voltage alarm triggered\n");
499 /*
500 * hold the low voltage wakelock until the soc
501 * work finds it appropriate to release it.
502 */
Anirudh Ghayal0c7c2bc2013-03-22 12:57:50 +0530503 if (!wake_lock_active(&the_chip->low_voltage_wake_lock)) {
504 pr_debug("Holding low voltage wakelock\n");
505 wake_lock(&the_chip->low_voltage_wake_lock);
506 pm8921_bms_low_voltage_config(the_chip,
507 the_chip->low_voltage_calc_ms);
508 }
Anirudh Ghayal6fe4ffa2013-02-01 11:44:33 +0530509
510 rc = pm8xxx_batt_alarm_disable(
511 PM8XXX_BATT_ALARM_LOWER_COMPARATOR);
512 if (!rc)
513 rc = pm8xxx_batt_alarm_enable(
514 PM8XXX_BATT_ALARM_UPPER_COMPARATOR);
515 if (rc)
516 pr_err("unable to set alarm state rc=%d\n", rc);
517 break;
518 case 2:
519 rc = pm8xxx_batt_alarm_disable(
520 PM8XXX_BATT_ALARM_UPPER_COMPARATOR);
521 if (!rc)
522 rc = pm8xxx_batt_alarm_enable(
523 PM8XXX_BATT_ALARM_LOWER_COMPARATOR);
524 if (rc)
525 pr_err("unable to set alarm state rc=%d\n", rc);
526
527 break;
528 default:
529 pr_err("error received\n");
530 break;
531 }
532
533 return 0;
534};
535
536
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700537#define HOLD_OREG_DATA BIT(1)
538static int pm_bms_lock_output_data(struct pm8921_bms_chip *chip)
539{
540 int rc;
541
542 rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA,
543 HOLD_OREG_DATA);
544 if (rc) {
545 pr_err("couldnt lock bms output rc = %d\n", rc);
546 return rc;
547 }
548 return 0;
549}
550
551static int pm_bms_unlock_output_data(struct pm8921_bms_chip *chip)
552{
553 int rc;
554
555 rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA, 0);
556 if (rc) {
557 pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc);
558 return rc;
559 }
560 return 0;
561}
562
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563#define SELECT_OUTPUT_DATA 0x1C
564#define SELECT_OUTPUT_TYPE_SHIFT 2
565#define OCV_FOR_RBATT 0x0
566#define VSENSE_FOR_RBATT 0x1
567#define VBATT_FOR_RBATT 0x2
568#define CC_MSB 0x3
569#define CC_LSB 0x4
570#define LAST_GOOD_OCV_VALUE 0x5
571#define VSENSE_AVG 0x6
572#define VBATT_AVG 0x7
573
574static int pm_bms_read_output_data(struct pm8921_bms_chip *chip, int type,
575 int16_t *result)
576{
577 int rc;
578 u8 reg;
579
580 if (!result) {
581 pr_err("result pointer null\n");
582 return -EINVAL;
583 }
584 *result = 0;
585 if (type < OCV_FOR_RBATT || type > VBATT_AVG) {
586 pr_err("invalid type %d asked to read\n", type);
587 return -EINVAL;
588 }
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700589
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590 rc = pm_bms_masked_write(chip, BMS_CONTROL, SELECT_OUTPUT_DATA,
591 type << SELECT_OUTPUT_TYPE_SHIFT);
592 if (rc) {
593 pr_err("fail to select %d type in BMS_CONTROL rc = %d\n",
594 type, rc);
595 return rc;
596 }
597
598 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT0, &reg);
599 if (rc) {
600 pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n",
601 type, rc);
602 return rc;
603 }
604 *result = reg;
605 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT1, &reg);
606 if (rc) {
607 pr_err("fail to read BMS_OUTPUT1 for type %d rc = %d\n",
608 type, rc);
609 return rc;
610 }
611 *result |= reg << 8;
612 pr_debug("type %d result %x", type, *result);
613 return 0;
614}
615
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700616#define V_PER_BIT_MUL_FACTOR 97656
617#define V_PER_BIT_DIV_FACTOR 1000
618#define XOADC_INTRINSIC_OFFSET 0x6000
619static int xoadc_reading_to_microvolt(unsigned int a)
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700620{
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700621 if (a <= XOADC_INTRINSIC_OFFSET)
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700622 return 0;
623
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700624 return (a - XOADC_INTRINSIC_OFFSET)
625 * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR;
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700626}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700628#define XOADC_CALIB_UV 625000
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700629#define VBATT_MUL_FACTOR 3
630static int adjust_xo_vbatt_reading(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800631 int usb_chg, unsigned int uv)
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700632{
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800633 s64 numerator, denominator;
634 int local_delta;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700635
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700636 if (uv == 0)
637 return 0;
638
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800639 /* dont adjust if not calibrated */
640 if (chip->xoadc_v0625 == 0 || chip->xoadc_v125 == 0) {
641 pr_debug("No cal yet return %d\n", VBATT_MUL_FACTOR * uv);
642 return VBATT_MUL_FACTOR * uv;
643 }
644
645 if (usb_chg)
646 local_delta = last_usb_cal_delta_uv;
647 else
648 local_delta = 0;
649
650 pr_debug("using delta = %d\n", local_delta);
651 numerator = ((s64)uv - chip->xoadc_v0625 - local_delta)
652 * XOADC_CALIB_UV;
653 denominator = (s64)chip->xoadc_v125 - chip->xoadc_v0625 - local_delta;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700654 if (denominator == 0)
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700655 return uv * VBATT_MUL_FACTOR;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800656 return (XOADC_CALIB_UV + local_delta + div_s64(numerator, denominator))
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700657 * VBATT_MUL_FACTOR;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700658}
659
Abhijeet Dharmapurikar5adb2532012-06-21 16:15:43 -0700660#define CC_RESOLUTION_N 868056
661#define CC_RESOLUTION_D 10000
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700662
663static s64 cc_to_microvolt(struct pm8921_bms_chip *chip, s64 cc)
664{
Abhijeet Dharmapurikar5adb2532012-06-21 16:15:43 -0700665 return div_s64(cc * CC_RESOLUTION_N, CC_RESOLUTION_D);
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700666}
667
Abhijeet Dharmapurikar2feabe32012-07-23 21:42:17 -0700668#define CC_READING_TICKS 56
669#define SLEEP_CLK_HZ 32764
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700670#define SECONDS_PER_HOUR 3600
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800671/**
Xiaozhe Shi35b82e42013-02-12 23:17:54 -0800672 * ccmicrovolt_to_pvh -
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800673 * @cc_uv: coulumb counter converted to uV
674 *
Xiaozhe Shi35b82e42013-02-12 23:17:54 -0800675 * RETURNS: coulumb counter based charge in pVh
676 * (pico Volt Hour)
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800677 */
Xiaozhe Shi35b82e42013-02-12 23:17:54 -0800678static s64 ccmicrovolt_to_pvh(s64 cc_uv)
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700679{
Xiaozhe Shi35b82e42013-02-12 23:17:54 -0800680 return div_s64(cc_uv * CC_READING_TICKS * 1000000L,
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700681 SLEEP_CLK_HZ * SECONDS_PER_HOUR);
682}
683
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700684/* returns the signed value read from the hardware */
685static int read_cc(struct pm8921_bms_chip *chip, int *result)
686{
687 int rc;
688 uint16_t msw, lsw;
689
Anirudh Ghayal3a38a382012-12-05 10:42:17 +0530690 *result = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700691 rc = pm_bms_read_output_data(chip, CC_LSB, &lsw);
692 if (rc) {
693 pr_err("fail to read CC_LSB rc = %d\n", rc);
694 return rc;
695 }
696 rc = pm_bms_read_output_data(chip, CC_MSB, &msw);
697 if (rc) {
698 pr_err("fail to read CC_MSB rc = %d\n", rc);
699 return rc;
700 }
701 *result = msw << 16 | lsw;
702 pr_debug("msw = %04x lsw = %04x cc = %d\n", msw, lsw, *result);
703 return 0;
704}
705
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700706static int adjust_xo_vbatt_reading_for_mbg(struct pm8921_bms_chip *chip,
707 int result)
708{
709 int64_t numerator;
710 int64_t denominator;
711
712 if (chip->amux_2_trim_delta == 0)
713 return result;
714
715 numerator = (s64)result * 1000000;
716 denominator = (1000000 + (410 * (s64)chip->amux_2_trim_delta));
717 return div_s64(numerator, denominator);
718}
719
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800720static int convert_vbatt_raw_to_uv(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800721 int usb_chg,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800722 uint16_t reading, int *result)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723{
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700724 *result = xoadc_reading_to_microvolt(reading);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800725 pr_debug("raw = %04x vbatt = %u\n", reading, *result);
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800726 *result = adjust_xo_vbatt_reading(chip, usb_chg, *result);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800727 pr_debug("after adj vbatt = %u\n", *result);
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700728 *result = adjust_xo_vbatt_reading_for_mbg(chip, *result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700729 return 0;
730}
731
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800732static int convert_vsense_to_uv(struct pm8921_bms_chip *chip,
733 int16_t reading, int *result)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700734{
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800735 *result = pm8xxx_ccadc_reading_to_microvolt(chip->revision, reading);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800736 pr_debug("raw = %04x vsense = %d\n", reading, *result);
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800737 *result = pm8xxx_cc_adjust_for_gain(*result);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800738 pr_debug("after adj vsense = %d\n", *result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700739 return 0;
740}
741
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700742static int read_vsense_avg(struct pm8921_bms_chip *chip, int *result)
743{
744 int rc;
745 int16_t reading;
746
747 rc = pm_bms_read_output_data(chip, VSENSE_AVG, &reading);
748 if (rc) {
749 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
750 return rc;
751 }
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800752
753 convert_vsense_to_uv(chip, reading, result);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700754 return 0;
755}
756
Abhijeet Dharmapurikara81e3a22013-01-14 18:19:18 -0800757static int get_batt_temp(struct pm8921_bms_chip *chip, int *batt_temp)
758{
759 int rc;
760 struct pm8xxx_adc_chan_result result;
761
762 rc = pm8xxx_adc_read(chip->batt_temp_channel, &result);
763 if (rc) {
764 pr_err("error reading batt_temp_channel = %d, rc = %d\n",
765 chip->batt_temp_channel, rc);
766 return rc;
767 }
768 *batt_temp = result.physical;
769 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
770 result.measurement);
771 return 0;
772}
773
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800774#define BMS_MODE_BIT BIT(6)
775#define EN_VBAT_BIT BIT(5)
776#define OVERRIDE_MODE_DELAY_MS 20
Abhijeet Dharmapurikarb2680132012-05-15 16:32:51 -0700777int override_mode_simultaneous_battery_voltage_and_current(int *ibat_ua,
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800778 int *vbat_uv)
779{
780 int16_t vsense_raw;
781 int16_t vbat_raw;
782 int vsense_uv;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800783 int usb_chg;
Abhijeet Dharmapurikara81e3a22013-01-14 18:19:18 -0800784 int batt_temp;
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800785
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800786 mutex_lock(&the_chip->bms_output_lock);
787
788 pm8xxx_writeb(the_chip->dev->parent, BMS_S1_DELAY, 0x00);
789 pm_bms_masked_write(the_chip, BMS_CONTROL,
790 BMS_MODE_BIT | EN_VBAT_BIT, BMS_MODE_BIT | EN_VBAT_BIT);
791
792 msleep(OVERRIDE_MODE_DELAY_MS);
793
794 pm_bms_lock_output_data(the_chip);
795 pm_bms_read_output_data(the_chip, VSENSE_AVG, &vsense_raw);
796 pm_bms_read_output_data(the_chip, VBATT_AVG, &vbat_raw);
797 pm_bms_unlock_output_data(the_chip);
798 pm_bms_masked_write(the_chip, BMS_CONTROL,
Abhijeet Dharmapurikara81e3a22013-01-14 18:19:18 -0800799 BMS_MODE_BIT | EN_VBAT_BIT, 0);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800800
801 pm8xxx_writeb(the_chip->dev->parent, BMS_S1_DELAY, 0x0B);
802
803 mutex_unlock(&the_chip->bms_output_lock);
804
Abhijeet Dharmapurikara81e3a22013-01-14 18:19:18 -0800805 get_batt_temp(the_chip, &batt_temp);
Abhijeet Dharmapurikar9e948982012-08-28 15:21:23 -0700806 usb_chg = usb_chg_plugged_in(the_chip);
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800807
808 convert_vbatt_raw_to_uv(the_chip, usb_chg, vbat_raw, vbat_uv);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800809 convert_vsense_to_uv(the_chip, vsense_raw, &vsense_uv);
Xiaozhe Shid69c91e2012-11-06 10:00:38 -0800810 *ibat_ua = div_s64((s64)vsense_uv * 1000000LL, the_chip->r_sense_uohm);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800811
812 pr_debug("vsense_raw = 0x%x vbat_raw = 0x%x"
813 " ibat_ua = %d vbat_uv = %d\n",
814 (uint16_t)vsense_raw, (uint16_t)vbat_raw,
815 *ibat_ua, *vbat_uv);
816 return 0;
817}
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800818
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -0800819#define MBG_TRANSIENT_ERROR_UV 15000
820static void adjust_pon_ocv(struct pm8921_bms_chip *chip, int *uv)
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700821{
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -0800822 /*
823 * In 8921 parts the PON ocv is taken when the MBG is not settled.
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700824 * decrease the pon ocv by 15mV raw value to account for it
825 * Since a 1/3rd of vbatt is supplied to the adc the raw value
826 * needs to be adjusted by 5mV worth bits
827 */
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -0800828 if (*uv >= MBG_TRANSIENT_ERROR_UV)
829 *uv -= MBG_TRANSIENT_ERROR_UV;
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700830}
831
Abhijeet Dharmapurikarecdb5db2012-10-09 16:57:18 -0700832#define SEL_ALT_OREG_BIT BIT(2)
833static int ocv_ir_compensation(struct pm8921_bms_chip *chip, int ocv)
834{
835 int compensated_ocv;
836 int ibatt_ua;
837 int rbatt_mohm = chip->default_rbatt_mohm + chip->rconn_mohm;
838
839 pm_bms_masked_write(chip, BMS_TEST1,
840 SEL_ALT_OREG_BIT, SEL_ALT_OREG_BIT);
841
842 /* since the SEL_ALT_OREG_BIT is set this will give us VSENSE_OCV */
843 pm8921_bms_get_battery_current(&ibatt_ua);
844 compensated_ocv = ocv + div_s64((s64)ibatt_ua * rbatt_mohm, 1000);
845 pr_debug("comp ocv = %d, ocv = %d, ibatt_ua = %d, rbatt_mohm = %d\n",
846 compensated_ocv, ocv, ibatt_ua, rbatt_mohm);
847
848 pm_bms_masked_write(chip, BMS_TEST1, SEL_ALT_OREG_BIT, 0);
849 return compensated_ocv;
850}
851
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -0800852#define RESET_CC_BIT BIT(3)
853static int reset_cc(struct pm8921_bms_chip *chip)
854{
855 int rc;
856
857 rc = pm_bms_masked_write(chip, BMS_TEST1, RESET_CC_BIT, RESET_CC_BIT);
858 if (rc < 0) {
859 pr_err("err setting cc reset rc = %d\n", rc);
860 return rc;
861 }
862
863 /* sleep 100uS for the coulomb counter to reset */
864 udelay(100);
865
866 rc = pm_bms_masked_write(chip, BMS_TEST1, RESET_CC_BIT, 0);
867 if (rc < 0)
868 pr_err("err clearing cc reset rc = %d\n", rc);
869 return rc;
870}
871
872static int estimate_ocv(struct pm8921_bms_chip *chip)
873{
874 int ibat_ua, vbat_uv, ocv_est_uv;
875 int rc;
Abhijeet Dharmapurikar03c6c232013-02-13 17:57:40 -0800876 int rbatt_mohm = chip->default_rbatt_mohm + chip->rconn_mohm
877 + chip->rbatt_capacitive_mohm;
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -0800878
879 rc = pm8921_bms_get_simultaneous_battery_voltage_and_current(
880 &ibat_ua,
881 &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 bool is_warm_restart(struct pm8921_bms_chip *chip)
893{
894 u8 reg;
895 int rc;
896
897 rc = pm8xxx_readb(chip->dev->parent, PON_CNTRL_6, &reg);
898 if (rc) {
899 pr_err("err reading pon 6 rc = %d\n", rc);
900 return false;
901 }
902 return reg & WD_BIT;
903}
Abhijeet Dharmapurikar7df608c2012-12-26 21:28:43 -0800904
905#define IBAT_TOL_MASK 0x0F
906#define OCV_TOL_MASK 0xF0
907#define IBAT_TOL_DEFAULT 0x03
908#define IBAT_TOL_NOCHG 0x0F
909#define OCV_TOL_DEFAULT 0x20
910#define OCV_TOL_NO_OCV 0x00
911static int pm8921_bms_stop_ocv_updates(void)
912{
913 if (!the_chip) {
914 pr_err("BMS driver has not been initialized yet!\n");
915 return -EINVAL;
916 }
917 pr_debug("stopping ocv updates\n");
918 return pm_bms_masked_write(the_chip, BMS_TOLERANCES,
919 OCV_TOL_MASK, OCV_TOL_NO_OCV);
920}
921
922static int pm8921_bms_start_ocv_updates(void)
923{
924 if (!the_chip) {
925 pr_err("BMS driver has not been initialized yet!\n");
926 return -EINVAL;
927 }
Abhijeet Dharmapurikar1939ede2013-01-10 15:59:33 -0800928 pr_debug("starting ocv updates\n");
Abhijeet Dharmapurikar7df608c2012-12-26 21:28:43 -0800929 return pm_bms_masked_write(the_chip, BMS_TOLERANCES,
930 OCV_TOL_MASK, OCV_TOL_DEFAULT);
931}
932
933static int reset_bms_for_test(void)
934{
935 int ibat_ua, vbat_uv, rc;
936 int ocv_est_uv;
937
938 if (!the_chip) {
939 pr_err("BMS driver has not been initialized yet!\n");
940 return -EINVAL;
941 }
942
943 rc = pm8921_bms_get_simultaneous_battery_voltage_and_current(
944 &ibat_ua,
945 &vbat_uv);
Abhijeet Dharmapurikar03c6c232013-02-13 17:57:40 -0800946 /*
947 * don't include rbatt and rbatt_capacitve since we expect this to
948 * be used with a fake battery which does not have internal resistnaces
949 */
Abhijeet Dharmapurikar7df608c2012-12-26 21:28:43 -0800950 ocv_est_uv = vbat_uv + (ibat_ua * the_chip->rconn_mohm) / 1000;
951 pr_debug("forcing ocv to be %d due to bms reset mode\n", ocv_est_uv);
952 the_chip->last_ocv_uv = ocv_est_uv;
953 last_soc = -EINVAL;
954 reset_cc(the_chip);
955 the_chip->last_cc_uah = 0;
956 pm8921_bms_stop_ocv_updates();
957
958 pr_debug("bms reset to ocv = %duv vbat_ua = %d ibat_ua = %d\n",
959 the_chip->last_ocv_uv, vbat_uv, ibat_ua);
960
961 return rc;
962}
963
964static int bms_reset_set(const char *val, const struct kernel_param *kp)
965{
966 int rc;
967
968 rc = param_set_bool(val, kp);
969 if (rc) {
970 pr_err("Unable to set bms_reset: %d\n", rc);
971 return rc;
972 }
973
974 if (*val == 'Y') {
975 rc = reset_bms_for_test();
976 if (rc) {
977 pr_err("Unable to modify bms_reset: %d\n", rc);
978 return rc;
979 }
980 }
981 return 0;
982}
983
984static struct kernel_param_ops bms_reset_ops = {
985 .set = bms_reset_set,
986 .get = param_get_bool,
987};
988
989static bool bms_reset;
990module_param_cb(bms_reset, &bms_reset_ops, &bms_reset, 0644);
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -0800991/*
992 * This reflects what should the CC readings should be for
993 * a 5mAh discharge. This value is dependent on
994 * CC_RESOLUTION_N, CC_RESOLUTION_D, CC_READING_TICKS
995 * and rsense
996 */
Abhijeet Dharmapurikar61275b92012-12-27 17:20:29 -0800997#define CC_RAW_5MAH 0x00110000
998#define MIN_OCV_UV 2000000
999#define OCV_RAW_UNINITIALIZED 0xFFFF
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001000static int read_soc_params_raw(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001001 struct pm8921_soc_params *raw,
1002 int batt_temp_decidegc)
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001003{
1004 int usb_chg;
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -08001005 int est_ocv_uv;
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001006
1007 mutex_lock(&chip->bms_output_lock);
1008 pm_bms_lock_output_data(chip);
1009
1010 pm_bms_read_output_data(chip,
1011 LAST_GOOD_OCV_VALUE, &raw->last_good_ocv_raw);
1012 read_cc(chip, &raw->cc);
1013
1014 pm_bms_unlock_output_data(chip);
1015 mutex_unlock(&chip->bms_output_lock);
1016
Abhijeet Dharmapurikar9e948982012-08-28 15:21:23 -07001017 usb_chg = usb_chg_plugged_in(chip);
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -07001018
Abhijeet Dharmapurikar61275b92012-12-27 17:20:29 -08001019 if (chip->prev_last_good_ocv_raw == OCV_RAW_UNINITIALIZED) {
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -07001020 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -08001021
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -07001022 convert_vbatt_raw_to_uv(chip, usb_chg,
1023 raw->last_good_ocv_raw, &raw->last_good_ocv_uv);
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -08001024 adjust_pon_ocv(chip, &raw->last_good_ocv_uv);
Abhijeet Dharmapurikarecdb5db2012-10-09 16:57:18 -07001025 raw->last_good_ocv_uv = ocv_ir_compensation(chip,
1026 raw->last_good_ocv_uv);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001027 chip->last_ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -08001028
1029 if (is_warm_restart(chip)
1030 || raw->cc > CC_RAW_5MAH
1031 || (raw->last_good_ocv_uv < MIN_OCV_UV
1032 && raw->cc > 0)) {
1033 /*
1034 * The CC value is higher than 5mAh.
1035 * The phone started without going through a pon
1036 * sequence
1037 * OR
1038 * The ocv was very small and there was no
1039 * charging in the bootloader
1040 * - reset the CC and take an ocv again
1041 */
1042 pr_debug("cc_raw = 0x%x may be > 5mAh(0x%x)\n",
1043 raw->cc, CC_RAW_5MAH);
1044 pr_debug("ocv_uv = %d ocv_raw = 0x%x may be < 2V\n",
1045 chip->last_ocv_uv,
1046 raw->last_good_ocv_raw);
1047 est_ocv_uv = estimate_ocv(chip);
1048 if (est_ocv_uv > 0) {
1049 raw->last_good_ocv_uv = est_ocv_uv;
1050 chip->last_ocv_uv = est_ocv_uv;
1051 reset_cc(chip);
1052 raw->cc = 0;
1053 }
1054 }
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001055 chip->last_ocv_temp_decidegc = batt_temp_decidegc;
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -07001056 pr_debug("PON_OCV_UV = %d\n", chip->last_ocv_uv);
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -07001057 } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) {
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -07001058 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
1059 convert_vbatt_raw_to_uv(chip, usb_chg,
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001060 raw->last_good_ocv_raw, &raw->last_good_ocv_uv);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001061 chip->last_ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001062 chip->last_ocv_temp_decidegc = batt_temp_decidegc;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001063 /* forget the old cc value upon ocv */
1064 chip->last_cc_uah = 0;
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -07001065 } else {
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001066 raw->last_good_ocv_uv = chip->last_ocv_uv;
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -07001067 }
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001068
Abhijeet Dharmapurikar079d6c52013-01-25 13:10:07 -08001069 /* stop faking 100% after an OCV event */
Abhijeet Dharmapurikar28d51372013-02-27 19:16:22 -08001070 if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw)
Abhijeet Dharmapurikar61275b92012-12-27 17:20:29 -08001071 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001072 pr_debug("0p625 = %duV\n", chip->xoadc_v0625);
1073 pr_debug("1p25 = %duV\n", chip->xoadc_v125);
Abhijeet Dharmapurikar1b8e8292012-01-17 11:01:44 -08001074 pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n",
1075 raw->last_good_ocv_raw, raw->last_good_ocv_uv);
1076 pr_debug("cc_raw= 0x%x\n", raw->cc);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001077 return 0;
1078}
1079
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001080static int get_rbatt(struct pm8921_bms_chip *chip, int soc_rbatt, int batt_temp)
1081{
1082 int rbatt, scalefactor;
1083
Abhijeet Dharmapurikar0711c522012-05-22 18:45:25 -07001084 rbatt = chip->default_rbatt_mohm;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001085 pr_debug("rbatt before scaling = %d\n", rbatt);
1086 if (chip->rbatt_sf_lut == NULL) {
1087 pr_debug("RBATT = %d\n", rbatt);
1088 return rbatt;
1089 }
Abhijeet Dharmapurikar12a891c2012-04-12 23:52:06 -07001090 /* Convert the batt_temp to DegC from deciDegC */
1091 batt_temp = batt_temp / 10;
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001092 scalefactor = interpolate_scalingfactor(chip->rbatt_sf_lut,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001093 batt_temp, soc_rbatt);
1094 pr_debug("rbatt sf = %d for batt_temp = %d, soc_rbatt = %d\n",
1095 scalefactor, batt_temp, soc_rbatt);
1096 rbatt = (rbatt * scalefactor) / 100;
1097
Abhijeet Dharmapurikarbaffba42012-03-22 14:41:10 -07001098 rbatt += the_chip->rconn_mohm;
1099 pr_debug("adding rconn_mohm = %d rbatt = %d\n",
1100 the_chip->rconn_mohm, rbatt);
1101
Abhijeet Dharmapurikar03c6c232013-02-13 17:57:40 -08001102 rbatt += the_chip->rbatt_capacitive_mohm;
1103 pr_debug("adding rbatt_capacitive_mohm = %d rbatt = %d\n",
1104 the_chip->rbatt_capacitive_mohm, rbatt);
1105
Abhijeet Dharmapurikarded189b2012-03-30 10:12:28 -07001106 if (is_between(20, 10, soc_rbatt))
1107 rbatt = rbatt
1108 + ((20 - soc_rbatt) * chip->delta_rbatt_mohm) / 10;
1109 else
1110 if (is_between(10, 0, soc_rbatt))
1111 rbatt = rbatt + chip->delta_rbatt_mohm;
1112
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001113 pr_debug("RBATT = %d\n", rbatt);
1114 return rbatt;
1115}
1116
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001117static int calculate_fcc_uah(struct pm8921_bms_chip *chip, int batt_temp,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001118 int chargecycles)
1119{
1120 int initfcc, result, scalefactor = 0;
1121
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001122 if (chip->adjusted_fcc_temp_lut == NULL) {
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001123 initfcc = interpolate_fcc(chip->fcc_temp_lut, batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001124
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001125 scalefactor = interpolate_scalingfactor_fcc(chip->fcc_sf_lut,
1126 chargecycles);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001127
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001128 /* Multiply the initial FCC value by the scale factor. */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001129 result = (initfcc * scalefactor * 1000) / 100;
1130 pr_debug("fcc = %d uAh\n", result);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001131 return result;
1132 } else {
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001133 return 1000 * interpolate_fcc(chip->adjusted_fcc_temp_lut,
1134 batt_temp);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001135 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001136}
1137
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001138static int get_battery_uvolts(struct pm8921_bms_chip *chip, int *uvolts)
1139{
1140 int rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001141 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001142
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001143 rc = pm8xxx_adc_read(chip->vbat_channel, &result);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001144 if (rc) {
1145 pr_err("error reading adc channel = %d, rc = %d\n",
1146 chip->vbat_channel, rc);
1147 return rc;
1148 }
1149 pr_debug("mvolts phy = %lld meas = 0x%llx", result.physical,
1150 result.measurement);
1151 *uvolts = (int)result.physical;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001152 return 0;
1153}
1154
1155static int adc_based_ocv(struct pm8921_bms_chip *chip, int *ocv)
1156{
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001157 int vbatt, rbatt, ibatt_ua, rc;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001158
1159 rc = get_battery_uvolts(chip, &vbatt);
1160 if (rc) {
1161 pr_err("failed to read vbatt from adc rc = %d\n", rc);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001162 return rc;
1163 }
1164
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001165 rc = pm8921_bms_get_battery_current(&ibatt_ua);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001166 if (rc) {
1167 pr_err("failed to read batt current rc = %d\n", rc);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001168 return rc;
1169 }
1170
Abhijeet Dharmapurikar0711c522012-05-22 18:45:25 -07001171 rbatt = chip->default_rbatt_mohm;
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001172 *ocv = vbatt + (ibatt_ua * rbatt)/1000;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001173 return 0;
1174}
1175
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001176static int calculate_pc(struct pm8921_bms_chip *chip, int ocv_uv,
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001177 int batt_temp_decidegc, int chargecycles)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178{
1179 int pc, scalefactor;
1180
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001181 pc = interpolate_pc(chip->pc_temp_ocv_lut,
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001182 batt_temp_decidegc / 10, ocv_uv / 1000);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001183 pr_debug("pc = %u for ocv = %dmicroVolts batt_temp = %d\n",
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001184 pc, ocv_uv, batt_temp_decidegc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001185
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001186 scalefactor = interpolate_scalingfactor(chip->pc_sf_lut,
1187 chargecycles, pc);
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001188 pr_debug("scalefactor = %u batt_temp = %d\n",
1189 scalefactor, batt_temp_decidegc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001190
1191 /* Multiply the initial FCC value by the scale factor. */
1192 pc = (pc * scalefactor) / 100;
1193 return pc;
1194}
1195
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001196/**
1197 * calculate_cc_uah -
1198 * @chip: the bms chip pointer
1199 * @cc: the cc reading from bms h/w
1200 * @val: return value
1201 * @coulumb_counter: adjusted coulumb counter for 100%
1202 *
1203 * RETURNS: in val pointer coulumb counter based charger in uAh
1204 * (micro Amp hour)
1205 */
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001206static void calculate_cc_uah(struct pm8921_bms_chip *chip, int cc, int *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001207{
Xiaozhe Shi35b82e42013-02-12 23:17:54 -08001208 int64_t cc_voltage_uv, cc_pvh, cc_uah;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001209
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001210 cc_voltage_uv = cc;
Abhijeet Dharmapurikar28d51372013-02-27 19:16:22 -08001211 pr_debug("cc = %d\n", cc);
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -07001212 cc_voltage_uv = cc_to_microvolt(chip, cc_voltage_uv);
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08001213 cc_voltage_uv = pm8xxx_cc_adjust_for_gain(cc_voltage_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001214 pr_debug("cc_voltage_uv = %lld microvolts\n", cc_voltage_uv);
Xiaozhe Shi35b82e42013-02-12 23:17:54 -08001215 cc_pvh = ccmicrovolt_to_pvh(cc_voltage_uv);
1216 pr_debug("cc_pvh = %lld pico_volt_hour\n", cc_pvh);
1217 cc_uah = div_s64(cc_pvh, chip->r_sense_uohm);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001218 *val = cc_uah;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001219}
1220
Abhijeet Dharmapurikar4c78f462012-10-16 15:43:55 -07001221int pm8921_bms_cc_uah(int *cc_uah)
1222{
1223 int cc;
1224
1225 *cc_uah = 0;
1226
1227 if (!the_chip)
1228 return -EINVAL;
1229
1230 read_cc(the_chip, &cc);
1231 calculate_cc_uah(the_chip, cc, cc_uah);
1232
1233 return 0;
1234}
1235EXPORT_SYMBOL(pm8921_bms_cc_uah);
1236
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001237static int calculate_termination_uuc(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001238 int batt_temp, int chargecycles,
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001239 int fcc_uah, int i_ma,
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001240 int *ret_pc_unusable)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001241{
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001242 int unusable_uv, pc_unusable, uuc;
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001243 int i = 0;
1244 int ocv_mv;
1245 int batt_temp_degc = batt_temp / 10;
1246 int rbatt_mohm;
1247 int delta_uv;
1248 int prev_delta_uv = 0;
1249 int prev_rbatt_mohm = 0;
1250 int prev_ocv_mv = 0;
1251 int uuc_rbatt_uv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001252
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001253 for (i = 0; i <= 100; i++) {
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001254 ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
1255 batt_temp_degc, i);
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001256 rbatt_mohm = get_rbatt(chip, i, batt_temp);
1257 unusable_uv = (rbatt_mohm * i_ma) + (chip->v_cutoff * 1000);
1258 delta_uv = ocv_mv * 1000 - unusable_uv;
1259
1260 pr_debug("soc = %d ocv = %d rbat = %d u_uv = %d delta_v = %d\n",
1261 i, ocv_mv, rbatt_mohm, unusable_uv, delta_uv);
1262
1263 if (delta_uv > 0)
1264 break;
1265
1266 prev_delta_uv = delta_uv;
1267 prev_rbatt_mohm = rbatt_mohm;
1268 prev_ocv_mv = ocv_mv;
1269 }
1270
1271 uuc_rbatt_uv = linear_interpolate(rbatt_mohm, delta_uv,
1272 prev_rbatt_mohm, prev_delta_uv,
1273 0);
1274
1275 unusable_uv = (uuc_rbatt_uv * i_ma) + (chip->v_cutoff * 1000);
1276
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001277 pc_unusable = calculate_pc(chip, unusable_uv, batt_temp, chargecycles);
1278 uuc = (fcc_uah * pc_unusable) / 100;
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001279 pr_debug("For i_ma = %d, unusable_rbatt = %d unusable_uv = %d unusable_pc = %d uuc = %d\n",
1280 i_ma, uuc_rbatt_uv, unusable_uv,
1281 pc_unusable, uuc);
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001282 *ret_pc_unusable = pc_unusable;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001283 return uuc;
1284}
1285
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001286#define TIME_PER_PERCENT_UUC 60
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001287static int adjust_uuc(struct pm8921_bms_chip *chip, int fcc_uah,
1288 int new_pc_unusable,
1289 int new_uuc,
1290 int batt_temp,
1291 int rbatt,
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001292 int *iavg_ma,
1293 int delta_time_s)
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001294{
1295 int new_unusable_mv;
1296 int batt_temp_degc = batt_temp / 10;
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001297 int max_percent_change;
1298
1299 max_percent_change = max(delta_time_s / TIME_PER_PERCENT_UUC, 1);
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001300
1301 if (chip->prev_pc_unusable == -EINVAL
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001302 || abs(chip->prev_pc_unusable - new_pc_unusable)
1303 <= max_percent_change) {
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001304 chip->prev_pc_unusable = new_pc_unusable;
1305 return new_uuc;
1306 }
1307
1308 /* the uuc is trying to change more than 1% restrict it */
1309 if (new_pc_unusable > chip->prev_pc_unusable)
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001310 chip->prev_pc_unusable += max_percent_change;
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001311 else
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001312 chip->prev_pc_unusable -= max_percent_change;
1313 chip->prev_pc_unusable = clamp(chip->prev_pc_unusable, 0, 100);
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001314 new_uuc = (fcc_uah * chip->prev_pc_unusable) / 100;
1315
1316 /* also find update the iavg_ma accordingly */
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001317 new_unusable_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
1318 batt_temp_degc, chip->prev_pc_unusable);
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001319 if (new_unusable_mv < chip->v_cutoff)
1320 new_unusable_mv = chip->v_cutoff;
1321
1322 *iavg_ma = (new_unusable_mv - chip->v_cutoff) * 1000 / rbatt;
1323 if (*iavg_ma == 0)
1324 *iavg_ma = 1;
1325 pr_debug("Restricting UUC to %d (%d%%) unusable_mv = %d iavg_ma = %d\n",
1326 new_uuc, chip->prev_pc_unusable,
1327 new_unusable_mv, *iavg_ma);
1328
1329 return new_uuc;
1330}
1331
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08001332static int get_current_time(unsigned long *now_tm_sec)
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001333{
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001334 struct rtc_time tm;
1335 struct rtc_device *rtc;
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08001336 int rc;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001337
1338 rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
1339 if (rtc == NULL) {
1340 pr_err("%s: unable to open rtc device (%s)\n",
1341 __FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08001342 return -EINVAL;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001343 }
1344
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001345 rc = rtc_read_time(rtc, &tm);
1346 if (rc) {
1347 pr_err("Error reading rtc device (%s) : %d\n",
1348 CONFIG_RTC_HCTOSYS_DEVICE, rc);
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08001349 return rc;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001350 }
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001351
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001352 rc = rtc_valid_tm(&tm);
1353 if (rc) {
1354 pr_err("Invalid RTC time (%s): %d\n",
1355 CONFIG_RTC_HCTOSYS_DEVICE, rc);
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08001356 return rc;
1357 }
1358 rtc_tm_to_time(&tm, now_tm_sec);
1359
1360 return 0;
1361}
1362
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001363static int calculate_delta_time(struct pm8921_bms_chip *chip, int *delta_time_s)
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08001364{
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08001365 unsigned long now_tm_sec = 0;
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001366
1367 /* default to delta time = 0 if anything fails */
1368 *delta_time_s = 0;
1369
1370 get_current_time(&now_tm_sec);
1371
1372 *delta_time_s = (now_tm_sec - chip->tm_sec);
1373 pr_debug("tm_sec = %ld, now_tm_sec = %ld delta_s = %d\n",
1374 chip->tm_sec, now_tm_sec, *delta_time_s);
1375
1376 /* remember this time */
1377 chip->tm_sec = now_tm_sec;
1378 return 0;
1379}
1380
1381static void calculate_iavg_ua(struct pm8921_bms_chip *chip, int cc_uah,
1382 int *iavg_ua, int delta_time_s)
1383{
1384 int delta_cc_uah = 0;
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08001385
1386 /* if anything fails report the previous iavg_ua */
1387 *iavg_ua = chip->prev_iavg_ua;
1388
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001389 if (chip->last_cc_uah == INT_MIN) {
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001390 pm8921_bms_get_battery_current(iavg_ua);
1391 goto out;
1392 }
1393
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001394 /* use the previous iavg if called within 15 seconds */
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001395 if (delta_time_s < 15) {
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001396 *iavg_ua = chip->prev_iavg_ua;
1397 goto out;
1398 }
1399
1400 delta_cc_uah = cc_uah - chip->last_cc_uah;
1401
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001402 *iavg_ua = div_s64((s64)delta_cc_uah * 3600, delta_time_s);
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001403
1404out:
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001405 pr_debug("delta_cc = %d iavg_ua = %d\n", delta_cc_uah, (int)*iavg_ua);
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001406 /* remember the iavg */
1407 chip->prev_iavg_ua = *iavg_ua;
1408
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001409 /* remember cc_uah */
1410 chip->last_cc_uah = cc_uah;
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001411}
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001412
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001413#define IAVG_SAMPLES 16
Abhijeet Dharmapurikare6503da2012-12-20 18:33:56 -08001414#define MIN_IAVG_MA 250
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001415#define MIN_SECONDS_FOR_VALID_SAMPLE 20
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001416static int calculate_unusable_charge_uah(struct pm8921_bms_chip *chip,
1417 int rbatt, int fcc_uah, int cc_uah,
1418 int soc_rbatt, int batt_temp, int chargecycles,
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001419 int iavg_ua, int delta_time_s)
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001420{
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001421 int uuc_uah_iavg;
1422 int i;
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001423 int iavg_ma = iavg_ua / 1000;
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001424 static int iavg_samples[IAVG_SAMPLES];
1425 static int iavg_index;
1426 static int iavg_num_samples;
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001427 static int firsttime = 1;
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001428 int pc_unusable;
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001429
1430 /*
1431 * if we are called first time fill all the
1432 * samples with the the shutdown_iavg_ua
1433 */
1434 if (firsttime && chip->shutdown_iavg_ua != 0) {
Abhijeet Dharmapurikar6b4af162012-10-10 09:16:27 -07001435 pr_debug("Using shutdown_iavg_ua = %d in all samples\n",
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001436 chip->shutdown_iavg_ua);
1437 for (i = 0; i < IAVG_SAMPLES; i++)
1438 iavg_samples[i] = chip->shutdown_iavg_ua;
1439
1440 iavg_index = 0;
1441 iavg_num_samples = IAVG_SAMPLES;
1442 }
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001443
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001444 /*
1445 * if we are charging use a nominal avg current so that we keep
1446 * a reasonable UUC while charging
1447 */
Abhijeet Dharmapurikare6503da2012-12-20 18:33:56 -08001448 if (iavg_ma < MIN_IAVG_MA)
1449 iavg_ma = MIN_IAVG_MA;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001450 iavg_samples[iavg_index] = iavg_ma;
1451 iavg_index = (iavg_index + 1) % IAVG_SAMPLES;
1452 iavg_num_samples++;
1453 if (iavg_num_samples >= IAVG_SAMPLES)
1454 iavg_num_samples = IAVG_SAMPLES;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001455
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001456 /* now that this sample is added calcualte the average */
1457 iavg_ma = 0;
1458 if (iavg_num_samples != 0) {
1459 for (i = 0; i < iavg_num_samples; i++) {
1460 pr_debug("iavg_samples[%d] = %d\n", i, iavg_samples[i]);
1461 iavg_ma += iavg_samples[i];
1462 }
1463
1464 iavg_ma = DIV_ROUND_CLOSEST(iavg_ma, iavg_num_samples);
1465 }
1466
Abhijeet Dharmapurikar7df608c2012-12-26 21:28:43 -08001467 /*
1468 * if we're in bms reset mode, force uuc to be 3% of fcc
1469 */
1470 if (bms_reset)
1471 return (fcc_uah * 3) / 100;
1472
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001473 uuc_uah_iavg = calculate_termination_uuc(chip,
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001474 batt_temp, chargecycles,
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001475 fcc_uah, iavg_ma,
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001476 &pc_unusable);
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001477 pr_debug("iavg = %d uuc_iavg = %d\n", iavg_ma, uuc_uah_iavg);
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001478
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001479 /* restrict the uuc change to one percent per 60 seconds */
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001480 uuc_uah_iavg = adjust_uuc(chip, fcc_uah, pc_unusable, uuc_uah_iavg,
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001481 batt_temp, rbatt, &iavg_ma, delta_time_s);
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001482
1483 /* find out what the avg current should be for this uuc */
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001484 chip->prev_uuc_iavg_ma = iavg_ma;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001485
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001486 firsttime = 0;
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001487 return uuc_uah_iavg;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001488}
Abhijeet Dharmapurikar52aa9682011-07-20 14:05:47 -07001489
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001490/* calculate remainging charge at the time of ocv */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001491static int calculate_remaining_charge_uah(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001492 struct pm8921_soc_params *raw,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001493 int fcc_uah, int batt_temp,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001494 int chargecycles)
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001495{
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001496 int ocv, pc, batt_temp_decidegc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001497
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001498 ocv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001499 batt_temp_decidegc = chip->last_ocv_temp_decidegc;
1500 pc = calculate_pc(chip, ocv, batt_temp_decidegc, chargecycles);
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001501 pr_debug("ocv = %d pc = %d\n", ocv, pc);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001502 return (fcc_uah * pc) / 100;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001503}
1504
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001505static void calculate_soc_params(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001506 struct pm8921_soc_params *raw,
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001507 int batt_temp, int chargecycles,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001508 int *fcc_uah,
1509 int *unusable_charge_uah,
1510 int *remaining_charge_uah,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001511 int *cc_uah,
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001512 int *rbatt,
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001513 int *iavg_ua)
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001514{
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001515 int soc_rbatt;
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001516 int delta_time_s;
1517 int rc;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001518
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001519 rc = calculate_delta_time(chip, &delta_time_s);
1520 if (rc) {
1521 pr_err("Failed to get delta time from RTC: %d\n", rc);
1522 delta_time_s = 0;
1523 }
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001524 *fcc_uah = calculate_fcc_uah(chip, batt_temp, chargecycles);
1525 pr_debug("FCC = %uuAh batt_temp = %d, cycles = %d\n",
1526 *fcc_uah, batt_temp, chargecycles);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001527
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001528
1529 /* calculate remainging charge */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001530 *remaining_charge_uah = calculate_remaining_charge_uah(chip, raw,
1531 *fcc_uah, batt_temp, chargecycles);
1532 pr_debug("RC = %uuAh\n", *remaining_charge_uah);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001533
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001534 /* calculate cc micro_volt_hour */
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001535 calculate_cc_uah(chip, raw->cc, cc_uah);
Abhijeet Dharmapurikar28d51372013-02-27 19:16:22 -08001536 pr_debug("cc_uah = %duAh raw->cc = %x\n", *cc_uah, raw->cc);
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001537
1538 soc_rbatt = ((*remaining_charge_uah - *cc_uah) * 100) / *fcc_uah;
1539 if (soc_rbatt < 0)
1540 soc_rbatt = 0;
1541 *rbatt = get_rbatt(chip, soc_rbatt, batt_temp);
1542
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001543 calculate_iavg_ua(chip, *cc_uah, iavg_ua, delta_time_s);
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001544
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001545 *unusable_charge_uah = calculate_unusable_charge_uah(chip, *rbatt,
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001546 *fcc_uah, *cc_uah, soc_rbatt,
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001547 batt_temp, chargecycles, *iavg_ua,
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001548 delta_time_s);
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001549 pr_debug("UUC = %uuAh\n", *unusable_charge_uah);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001550}
1551
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001552static int calculate_real_fcc_uah(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001553 struct pm8921_soc_params *raw,
1554 int batt_temp, int chargecycles,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001555 int *ret_fcc_uah)
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001556{
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001557 int fcc_uah, unusable_charge_uah;
1558 int remaining_charge_uah;
1559 int cc_uah;
1560 int real_fcc_uah;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001561 int rbatt;
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001562 int iavg_ua;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001563
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001564 calculate_soc_params(chip, raw, batt_temp, chargecycles,
1565 &fcc_uah,
1566 &unusable_charge_uah,
1567 &remaining_charge_uah,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001568 &cc_uah,
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001569 &rbatt,
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001570 &iavg_ua);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001571
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001572 real_fcc_uah = remaining_charge_uah - cc_uah;
1573 *ret_fcc_uah = fcc_uah;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001574 pr_debug("real_fcc = %d, RC = %d CC = %d fcc = %d\n",
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001575 real_fcc_uah, remaining_charge_uah, cc_uah, fcc_uah);
1576 return real_fcc_uah;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001577}
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001578
Abhijeet Dharmapurikarb2680132012-05-15 16:32:51 -07001579int pm8921_bms_get_simultaneous_battery_voltage_and_current(int *ibat_ua,
1580 int *vbat_uv)
1581{
1582 int rc;
1583
1584 if (the_chip == NULL) {
1585 pr_err("Called too early\n");
1586 return -EINVAL;
1587 }
1588
1589 if (pm8921_is_batfet_closed()) {
1590 return override_mode_simultaneous_battery_voltage_and_current(
1591 ibat_ua,
1592 vbat_uv);
1593 } else {
1594 pr_debug("batfet is open using separate vbat and ibat meas\n");
1595 rc = get_battery_uvolts(the_chip, vbat_uv);
1596 if (rc < 0) {
1597 pr_err("adc vbat failed err = %d\n", rc);
1598 return rc;
1599 }
1600 rc = pm8921_bms_get_battery_current(ibat_ua);
1601 if (rc < 0) {
1602 pr_err("bms ibat failed err = %d\n", rc);
1603 return rc;
1604 }
1605 }
1606
1607 return 0;
1608}
1609EXPORT_SYMBOL(pm8921_bms_get_simultaneous_battery_voltage_and_current);
1610
Abhijeet Dharmapurikarf1913b62013-02-13 16:47:14 -08001611#define SIGN(x) ((x) < 0 ? -1 : 1)
1612
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001613static void find_ocv_for_soc(struct pm8921_bms_chip *chip,
1614 int batt_temp,
1615 int chargecycles,
1616 int fcc_uah,
1617 int uuc_uah,
1618 int cc_uah,
1619 int shutdown_soc,
1620 int *rc_uah,
1621 int *ocv_uv)
1622{
1623 s64 rc;
1624 int pc, new_pc;
1625 int batt_temp_degc = batt_temp / 10;
1626 int ocv;
1627
1628 rc = (s64)shutdown_soc * (fcc_uah - uuc_uah);
1629 rc = div_s64(rc, 100) + cc_uah + uuc_uah;
1630 pc = DIV_ROUND_CLOSEST((int)rc * 100, fcc_uah);
1631 pc = clamp(pc, 0, 100);
1632
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001633 ocv = interpolate_ocv(chip->pc_temp_ocv_lut, batt_temp_degc, pc);
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001634
1635 pr_debug("s_soc = %d, fcc = %d uuc = %d rc = %d, pc = %d, ocv mv = %d\n",
1636 shutdown_soc, fcc_uah, uuc_uah, (int)rc, pc, ocv);
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001637 new_pc = interpolate_pc(chip->pc_temp_ocv_lut, batt_temp_degc, ocv);
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001638 pr_debug("test revlookup pc = %d for ocv = %d\n", new_pc, ocv);
1639
Abhijeet Dharmapurikarf1913b62013-02-13 16:47:14 -08001640 if (abs(new_pc - pc) > 0) {
1641 /* Maximum spins to make in while-loop when searching in
1642 * full resolution.
1643 */
1644 const unsigned int max_spin_count =
1645 chip->max_voltage_uv / 1000 - chip->v_cutoff + 1;
1646 unsigned int count = 0;
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001647 int delta_mv = 5;
Abhijeet Dharmapurikarf1913b62013-02-13 16:47:14 -08001648 int diff = abs(new_pc - pc);
1649 char sign = SIGN(new_pc - pc);
1650 char old_sign;
1651 int old_diff;
1652 int old_ocv;
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001653
Abhijeet Dharmapurikarf1913b62013-02-13 16:47:14 -08001654 do {
1655 count++;
1656 old_ocv = ocv;
1657 old_diff = diff;
1658 old_sign = sign;
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001659
Abhijeet Dharmapurikarf1913b62013-02-13 16:47:14 -08001660 if (new_pc > pc)
1661 ocv -= delta_mv;
1662 else
1663 ocv += delta_mv;
1664
1665 new_pc = interpolate_pc(chip->pc_temp_ocv_lut,
1666 batt_temp_degc, ocv);
1667 pr_debug("test revlookup pc = %d for ocv = %d\n",
1668 new_pc, ocv);
1669 diff = abs(new_pc - pc);
1670 sign = SIGN(new_pc - pc);
1671
1672 if (sign != old_sign) {
1673 if (delta_mv == 5) {
1674 /*
1675 * we crossed our desired PC probably
1676 * becuase we were overcorrecting
1677 */
1678 delta_mv = 1;
1679 } else {
1680 /* we crossed our desired PC even with
1681 * 1mV steps, choose the best of two */
1682 if (diff > old_diff)
1683 ocv = old_ocv;
1684
1685 break;
1686 }
1687 }
1688 } while (count <= max_spin_count && diff > 0);
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001689 }
1690
1691 *ocv_uv = ocv * 1000;
1692 *rc_uah = (int)rc;
1693}
1694
1695static void adjust_rc_and_uuc_for_specific_soc(
1696 struct pm8921_bms_chip *chip,
1697 int batt_temp,
1698 int chargecycles,
1699 int soc,
1700 int fcc_uah,
1701 int uuc_uah,
1702 int cc_uah,
1703 int rc_uah,
1704 int rbatt,
1705 int *ret_ocv,
1706 int *ret_rc,
1707 int *ret_uuc,
1708 int *ret_rbatt)
1709{
1710 int ocv_uv;
1711
1712 find_ocv_for_soc(chip, batt_temp, chargecycles,
1713 fcc_uah, uuc_uah, cc_uah,
1714 soc,
1715 &rc_uah, &ocv_uv);
1716
1717 *ret_ocv = ocv_uv;
1718 *ret_rbatt = rbatt;
1719 *ret_rc = rc_uah;
1720 *ret_uuc = uuc_uah;
1721}
Abhijeet Dharmapurikareca7b3e2012-12-26 21:30:29 -08001722
1723static void calc_current_max(struct pm8921_bms_chip *chip, int ocv_uv,
1724 int rbatt_mohm)
1725{
1726 chip->imax_ua = 1000 * (ocv_uv - chip->v_cutoff * 1000) / rbatt_mohm;
1727}
1728
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001729static int bound_soc(int soc)
1730{
1731 soc = max(0, soc);
1732 soc = min(100, soc);
1733 return soc;
1734}
1735
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001736static int charging_adjustments(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001737 int soc, int vbat_uv, int ibat_ua,
1738 int batt_temp, int chargecycles,
1739 int fcc_uah, int cc_uah, int uuc_uah)
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001740{
1741 int chg_soc;
Abhijeet Dharmapurikara82a00a2012-11-08 17:26:29 -08001742 int vbat_batt_terminal_uv = vbat_uv
1743 + (ibat_ua * chip->rconn_mohm) / 1000;
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001744
1745 if (chip->soc_at_cv == -EINVAL) {
1746 /* In constant current charging return the calc soc */
Abhijeet Dharmapurikara82a00a2012-11-08 17:26:29 -08001747 if (vbat_batt_terminal_uv <= chip->max_voltage_uv)
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001748 pr_debug("CC CHG SOC %d\n", soc);
1749
1750 /* Note the CC to CV point */
Abhijeet Dharmapurikara82a00a2012-11-08 17:26:29 -08001751 if (vbat_batt_terminal_uv >= chip->max_voltage_uv) {
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001752 chip->soc_at_cv = soc;
1753 chip->prev_chg_soc = soc;
1754 chip->ibat_at_cv_ua = ibat_ua;
1755 pr_debug("CC_TO_CV ibat_ua = %d CHG SOC %d\n",
1756 ibat_ua, soc);
1757 }
Abhijeet Dharmapurikarcbde3db2013-01-28 15:09:04 -08001758
1759 chip->prev_vbat_batt_terminal_uv = vbat_batt_terminal_uv;
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001760 return soc;
1761 }
1762
1763 /*
1764 * battery is in CV phase - begin liner inerpolation of soc based on
1765 * battery charge current
1766 */
1767
1768 /*
Abhijeet Dharmapurikarcbde3db2013-01-28 15:09:04 -08001769 * if the battery terminal voltage lessened (possibly because of
Abhijeet Dharmapurikara82a00a2012-11-08 17:26:29 -08001770 * a sudden increase in system load) keep reporting the prev chg soc
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001771 */
Abhijeet Dharmapurikarcbde3db2013-01-28 15:09:04 -08001772 if (vbat_batt_terminal_uv < chip->prev_vbat_batt_terminal_uv) {
1773 pr_debug("vbat_terminals %d < prev = %d CC CHG SOC %d\n",
Abhijeet Dharmapurikara82a00a2012-11-08 17:26:29 -08001774 vbat_batt_terminal_uv,
Abhijeet Dharmapurikarcbde3db2013-01-28 15:09:04 -08001775 chip->prev_vbat_batt_terminal_uv,
1776 chip->prev_chg_soc);
1777 chip->prev_vbat_batt_terminal_uv = vbat_batt_terminal_uv;
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001778 return chip->prev_chg_soc;
1779 }
1780
1781 chg_soc = linear_interpolate(chip->soc_at_cv, chip->ibat_at_cv_ua,
Abhijeet Dharmapurikara82a00a2012-11-08 17:26:29 -08001782 100, -1 * chip->chg_term_ua,
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001783 ibat_ua);
Xiaozhe Shi33985a92012-12-10 13:02:14 -08001784 chg_soc = bound_soc(chg_soc);
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001785
1786 /* always report a higher soc */
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001787 if (chg_soc > chip->prev_chg_soc) {
1788 int new_ocv_uv;
1789 int new_rc;
1790
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001791 chip->prev_chg_soc = chg_soc;
1792
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001793 find_ocv_for_soc(chip, batt_temp, chargecycles,
1794 fcc_uah, uuc_uah, cc_uah,
1795 chg_soc,
1796 &new_rc, &new_ocv_uv);
1797 the_chip->last_ocv_uv = new_ocv_uv;
1798 pr_debug("CC CHG ADJ OCV = %d CHG SOC %d\n",
1799 new_ocv_uv,
1800 chip->prev_chg_soc);
1801 }
1802
Abhijeet Dharmapurikard3c435e2012-08-16 19:23:18 -07001803 pr_debug("Reporting CHG SOC %d\n", chip->prev_chg_soc);
Abhijeet Dharmapurikarcbde3db2013-01-28 15:09:04 -08001804 chip->prev_vbat_batt_terminal_uv = vbat_batt_terminal_uv;
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001805 return chip->prev_chg_soc;
1806}
1807
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07001808static void very_low_voltage_check(struct pm8921_bms_chip *chip,
1809 int ibat_ua, int vbat_uv)
1810{
Anirudh Ghayal6fe4ffa2013-02-01 11:44:33 +05301811 int rc;
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07001812 /*
1813 * if battery is very low (v_cutoff voltage + 20mv) hold
1814 * a wakelock untill soc = 0%
1815 */
Anirudh Ghayal0c7c2bc2013-03-22 12:57:50 +05301816 if (vbat_uv <= (chip->alarm_low_mv + 20) * 1000 &&
1817 !wake_lock_active(&the_chip->low_voltage_wake_lock)) {
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07001818 pr_debug("voltage = %d low holding wakelock\n", vbat_uv);
1819 wake_lock(&chip->low_voltage_wake_lock);
Abhijeet Dharmapurikar683472d2012-10-30 16:21:58 -07001820 chip->soc_calc_period = chip->low_voltage_calc_ms;
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07001821 }
1822
Anirudh Ghayal0c7c2bc2013-03-22 12:57:50 +05301823 if (vbat_uv > (chip->alarm_low_mv + 20 + BATT_ALARM_ACCURACY) * 1000
1824 && wake_lock_active(&the_chip->low_voltage_wake_lock)) {
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07001825 pr_debug("voltage = %d releasing wakelock\n", vbat_uv);
Anirudh Ghayal0c7c2bc2013-03-22 12:57:50 +05301826 chip->vbatt_cutoff_count = 0;
Abhijeet Dharmapurikar683472d2012-10-30 16:21:58 -07001827 chip->soc_calc_period = chip->normal_voltage_calc_ms;
Anirudh Ghayal6fe4ffa2013-02-01 11:44:33 +05301828 rc = pm8921_bms_enable_batt_alarm(chip);
1829 if (rc)
1830 pr_err("Unable to enable batt alarm\n");
Anirudh Ghayal0c7c2bc2013-03-22 12:57:50 +05301831 wake_unlock(&chip->low_voltage_wake_lock);
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07001832 }
1833}
1834
Anirudh Ghayal0c7c2bc2013-03-22 12:57:50 +05301835static bool is_voltage_below_cutoff_window(struct pm8921_bms_chip *chip,
1836 int ibat_ua, int vbat_uv)
1837{
1838 if (vbat_uv < (chip->v_cutoff * 1000) && ibat_ua > 0) {
1839 chip->vbatt_cutoff_count++;
1840 if (chip->vbatt_cutoff_count >= chip->vbatt_cutoff_retries) {
1841 pr_debug("cutoff_count >= %d\n",
1842 chip->vbatt_cutoff_retries);
1843 return true;
1844 }
1845 } else {
1846 chip->vbatt_cutoff_count = 0;
1847 }
1848
1849 return false;
1850}
1851
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001852static int last_soc_est = -EINVAL;
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001853static int adjust_soc(struct pm8921_bms_chip *chip, int soc,
1854 int batt_temp, int chargecycles,
1855 int rbatt, int fcc_uah, int uuc_uah, int cc_uah)
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001856{
1857 int ibat_ua = 0, vbat_uv = 0;
1858 int ocv_est_uv = 0, soc_est = 0, pc_est = 0, pc = 0;
1859 int delta_ocv_uv = 0;
1860 int n = 0;
1861 int rc_new_uah = 0;
1862 int pc_new = 0;
1863 int soc_new = 0;
1864 int m = 0;
Abhijeet Dharmapurikarb2680132012-05-15 16:32:51 -07001865 int rc = 0;
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -07001866 int delta_ocv_uv_limit = 0;
Abhijeet Dharmapurikar48707612013-03-26 13:14:03 -07001867 int correction_limit_uv = 0;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001868
Abhijeet Dharmapurikarb2680132012-05-15 16:32:51 -07001869 rc = pm8921_bms_get_simultaneous_battery_voltage_and_current(
1870 &ibat_ua,
1871 &vbat_uv);
1872 if (rc < 0) {
1873 pr_err("simultaneous vbat ibat failed err = %d\n", rc);
1874 goto out;
1875 }
1876
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07001877 very_low_voltage_check(chip, ibat_ua, vbat_uv);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001878
Anirudh Ghayal0c7c2bc2013-03-22 12:57:50 +05301879 if (chip->low_voltage_detect &&
1880 wake_lock_active(&chip->low_voltage_wake_lock)) {
1881 if (is_voltage_below_cutoff_window(chip, ibat_ua, vbat_uv)) {
1882 soc = 0;
1883 pr_info("Voltage below cutoff, setting soc to 0\n");
1884 goto out;
1885 }
1886 }
1887
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -07001888 delta_ocv_uv_limit = DIV_ROUND_CLOSEST(ibat_ua, 1000);
1889
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001890 ocv_est_uv = vbat_uv + (ibat_ua * rbatt)/1000;
Abhijeet Dharmapurikareca7b3e2012-12-26 21:30:29 -08001891 calc_current_max(chip, ocv_est_uv, rbatt);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001892 pc_est = calculate_pc(chip, ocv_est_uv, batt_temp, last_chargecycles);
1893 soc_est = div_s64((s64)fcc_uah * pc_est - uuc_uah*100,
1894 (s64)fcc_uah - uuc_uah);
1895 soc_est = bound_soc(soc_est);
1896
Abhijeet Dharmapurikar7df608c2012-12-26 21:28:43 -08001897 /* never adjust during bms reset mode */
1898 if (bms_reset) {
1899 pr_debug("bms reset mode, SOC adjustment skipped\n");
1900 goto out;
1901 }
1902
Abhijeet Dharmapurikar552df982012-10-23 19:13:45 -07001903 if (ibat_ua < 0 && pm8921_is_batfet_closed()) {
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001904 soc = charging_adjustments(chip, soc, vbat_uv, ibat_ua,
1905 batt_temp, chargecycles,
1906 fcc_uah, cc_uah, uuc_uah);
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001907 goto out;
1908 }
1909
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001910 /*
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001911 * do not adjust
Abhijeet Dharmapurikar321bcb12013-03-25 13:51:27 -07001912 * if soc_est is same as what bms calculated
1913 * OR if soc_est > 15
1914 * OR if soc it is above 90 because we might pull it low
Abhijeet Dharmapurikar52daf652012-07-19 11:50:47 -07001915 * and cause a bad user experience
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001916 */
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001917 if (soc_est == soc
Abhijeet Dharmapurikar321bcb12013-03-25 13:51:27 -07001918 || soc_est > 15
Abhijeet Dharmapurikar52daf652012-07-19 11:50:47 -07001919 || soc >= 90)
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001920 goto out;
1921
1922 if (last_soc_est == -EINVAL)
1923 last_soc_est = soc;
1924
1925 n = min(200, max(1 , soc + soc_est + last_soc_est));
1926 /* remember the last soc_est in last_soc_est */
1927 last_soc_est = soc_est;
1928
1929 pc = calculate_pc(chip, chip->last_ocv_uv,
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001930 chip->last_ocv_temp_decidegc, last_chargecycles);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001931 if (pc > 0) {
1932 pc_new = calculate_pc(chip, chip->last_ocv_uv - (++m * 1000),
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001933 chip->last_ocv_temp_decidegc,
1934 last_chargecycles);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001935 while (pc_new == pc) {
1936 /* start taking 10mV steps */
1937 m = m + 10;
1938 pc_new = calculate_pc(chip,
1939 chip->last_ocv_uv - (m * 1000),
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001940 chip->last_ocv_temp_decidegc,
1941 last_chargecycles);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001942 }
1943 } else {
1944 /*
1945 * pc is already at the lowest point,
1946 * assume 1 millivolt translates to 1% pc
1947 */
1948 pc = 1;
1949 pc_new = 0;
1950 m = 1;
1951 }
1952
1953 delta_ocv_uv = div_s64((soc - soc_est) * (s64)m * 1000,
1954 n * (pc - pc_new));
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -07001955
1956 if (abs(delta_ocv_uv) > delta_ocv_uv_limit) {
1957 pr_debug("limiting delta ocv %d limit = %d\n", delta_ocv_uv,
1958 delta_ocv_uv_limit);
1959
1960 if (delta_ocv_uv > 0)
1961 delta_ocv_uv = delta_ocv_uv_limit;
1962 else
1963 delta_ocv_uv = -1 * delta_ocv_uv_limit;
1964 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
1965 }
1966
Abhijeet Dharmapurikar48707612013-03-26 13:14:03 -07001967 if (chip->last_ocv_uv > 3800000)
1968 correction_limit_uv = the_chip->high_ocv_correction_limit_uv;
1969 else
1970 correction_limit_uv = the_chip->low_ocv_correction_limit_uv;
1971
1972 if (abs(delta_ocv_uv) > correction_limit_uv) {
1973 pr_debug("limiting delta ocv %d limit = %d\n", delta_ocv_uv,
1974 correction_limit_uv);
1975
1976 if (delta_ocv_uv > 0)
1977 delta_ocv_uv = correction_limit_uv;
1978 else
1979 delta_ocv_uv = -1 * correction_limit_uv;
1980 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
1981 }
1982
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001983 chip->last_ocv_uv -= delta_ocv_uv;
1984
1985 if (chip->last_ocv_uv >= chip->max_voltage_uv)
1986 chip->last_ocv_uv = chip->max_voltage_uv;
1987
1988 /* calculate the soc based on this new ocv */
1989 pc_new = calculate_pc(chip, chip->last_ocv_uv,
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001990 chip->last_ocv_temp_decidegc, last_chargecycles);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001991 rc_new_uah = (fcc_uah * pc_new) / 100;
1992 soc_new = (rc_new_uah - cc_uah - uuc_uah)*100 / (fcc_uah - uuc_uah);
1993 soc_new = bound_soc(soc_new);
1994
1995 /*
1996 * if soc_new is ZERO force it higher so that phone doesnt report soc=0
1997 * soc = 0 should happen only when soc_est == 0
1998 */
Abhijeet Dharmapurikar48707612013-03-26 13:14:03 -07001999 if (soc_new == 0 && soc_est >= the_chip->hold_soc_est)
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002000 soc_new = 1;
2001
2002 soc = soc_new;
2003
2004out:
2005 pr_debug("ibat_ua = %d, vbat_uv = %d, ocv_est_uv = %d, pc_est = %d, "
2006 "soc_est = %d, n = %d, delta_ocv_uv = %d, last_ocv_uv = %d, "
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07002007 "pc_new = %d, soc_new = %d, rbatt = %d, m = %d\n",
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002008 ibat_ua, vbat_uv, ocv_est_uv, pc_est,
2009 soc_est, n, delta_ocv_uv, chip->last_ocv_uv,
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07002010 pc_new, soc_new, rbatt, m);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002011
2012 return soc;
2013}
2014
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -07002015#define IGNORE_SOC_TEMP_DECIDEG 50
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002016#define IAVG_STEP_SIZE_MA 50
2017#define IAVG_START 600
2018#define SOC_ZERO 0xFF
2019static void backup_soc_and_iavg(struct pm8921_bms_chip *chip, int batt_temp,
2020 int soc)
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07002021{
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002022 u8 temp;
2023 int iavg_ma = chip->prev_uuc_iavg_ma;
2024
2025 if (iavg_ma > IAVG_START)
2026 temp = (iavg_ma - IAVG_START) / IAVG_STEP_SIZE_MA;
2027 else
2028 temp = 0;
2029
2030 pm_bms_masked_write(chip, TEMP_IAVG_STORAGE,
2031 TEMP_IAVG_STORAGE_USE_MASK, temp);
2032
2033 /* since only 6 bits are available for SOC, we store half the soc */
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -07002034 if (soc == 0)
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002035 temp = SOC_ZERO;
2036 else
2037 temp = soc;
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07002038
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002039 /* don't store soc if temperature is below 5degC */
2040 if (batt_temp > IGNORE_SOC_TEMP_DECIDEG)
2041 pm8xxx_writeb(the_chip->dev->parent, TEMP_SOC_STORAGE, temp);
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07002042}
2043
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002044static void read_shutdown_soc_and_iavg(struct pm8921_bms_chip *chip)
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07002045{
2046 int rc;
2047 u8 temp;
2048
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002049 rc = pm8xxx_readb(chip->dev->parent, TEMP_IAVG_STORAGE, &temp);
2050 if (rc) {
2051 pr_err("failed to read addr = %d %d assuming %d\n",
2052 TEMP_IAVG_STORAGE, rc, IAVG_START);
2053 chip->shutdown_iavg_ua = IAVG_START;
2054 } else {
2055 temp &= TEMP_IAVG_STORAGE_USE_MASK;
2056
2057 if (temp == 0) {
2058 chip->shutdown_iavg_ua = IAVG_START;
2059 } else {
2060 chip->shutdown_iavg_ua = IAVG_START
2061 + IAVG_STEP_SIZE_MA * (temp + 1);
2062 }
2063 }
2064
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07002065 rc = pm8xxx_readb(chip->dev->parent, TEMP_SOC_STORAGE, &temp);
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002066 if (rc) {
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07002067 pr_err("failed to read addr = %d %d\n", TEMP_SOC_STORAGE, rc);
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002068 } else {
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07002069 chip->shutdown_soc = temp;
2070
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002071 if (chip->shutdown_soc == 0) {
2072 pr_debug("No shutdown soc available\n");
2073 shutdown_soc_invalid = 1;
2074 chip->shutdown_iavg_ua = 0;
2075 } else if (chip->shutdown_soc == SOC_ZERO) {
2076 chip->shutdown_soc = 0;
2077 }
2078 }
Abhijeet Dharmapurikar1f4cc512012-07-21 00:10:39 -07002079
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002080 if (chip->ignore_shutdown_soc) {
2081 shutdown_soc_invalid = 1;
2082 chip->shutdown_soc = 0;
2083 chip->shutdown_iavg_ua = 0;
2084 }
2085
2086 pr_debug("shutdown_soc = %d shutdown_iavg = %d shutdown_soc_invalid = %d\n",
2087 chip->shutdown_soc,
2088 chip->shutdown_iavg_ua,
2089 shutdown_soc_invalid);
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07002090}
2091
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002092#define SOC_CATCHUP_SEC_MAX 600
2093#define SOC_CATCHUP_SEC_PER_PERCENT 60
2094#define MAX_CATCHUP_SOC (SOC_CATCHUP_SEC_MAX/SOC_CATCHUP_SEC_PER_PERCENT)
2095static int scale_soc_while_chg(struct pm8921_bms_chip *chip,
2096 int delta_time_us, int new_soc, int prev_soc)
2097{
2098 int chg_time_sec;
2099 int catch_up_sec;
2100 int scaled_soc;
2101 int numerator;
2102
2103 /*
2104 * The device must be charging for reporting a higher soc, if
2105 * not ignore this soc and continue reporting the prev_soc.
2106 * Also don't report a high value immediately slowly scale the
2107 * value from prev_soc to the new soc based on a charge time
2108 * weighted average
2109 */
2110
2111 /* if we are not charging return last soc */
2112 if (the_chip->start_percent == -EINVAL)
2113 return prev_soc;
2114
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002115 chg_time_sec = DIV_ROUND_UP(the_chip->charge_time_us, USEC_PER_SEC);
2116 catch_up_sec = DIV_ROUND_UP(the_chip->catch_up_time_us, USEC_PER_SEC);
Xiaozhe Shid3ce9862012-12-17 19:34:50 -08002117 if (catch_up_sec == 0)
2118 return new_soc;
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002119 pr_debug("cts= %d catch_up_sec = %d\n", chg_time_sec, catch_up_sec);
2120
2121 /*
2122 * if we have been charging for more than catch_up time simply return
2123 * new soc
2124 */
2125 if (chg_time_sec > catch_up_sec)
2126 return new_soc;
2127
2128 numerator = (catch_up_sec - chg_time_sec) * prev_soc
2129 + chg_time_sec * new_soc;
2130 scaled_soc = numerator / catch_up_sec;
2131
2132 pr_debug("cts = %d new_soc = %d prev_soc = %d scaled_soc = %d\n",
2133 chg_time_sec, new_soc, prev_soc, scaled_soc);
2134
2135 return scaled_soc;
2136}
2137
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002138static bool is_shutdown_soc_within_limits(struct pm8921_bms_chip *chip, int soc)
2139{
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002140 if (shutdown_soc_invalid) {
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002141 pr_debug("NOT forcing shutdown soc = %d\n", chip->shutdown_soc);
2142 return 0;
2143 }
2144
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002145 if (abs(chip->shutdown_soc - soc) > chip->shutdown_soc_valid_limit) {
2146 pr_debug("rejecting shutdown soc = %d, soc = %d limit = %d\n",
2147 chip->shutdown_soc, soc,
2148 chip->shutdown_soc_valid_limit);
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002149 shutdown_soc_invalid = 1;
2150 return 0;
2151 }
2152
2153 return 1;
2154}
Abhijeet Dharmapurikar0622a432012-08-28 16:34:18 -07002155
2156static void update_power_supply(struct pm8921_bms_chip *chip)
2157{
2158 if (chip->batt_psy == NULL || chip->batt_psy < 0)
2159 chip->batt_psy = power_supply_get_by_name("battery");
2160
2161 if (chip->batt_psy > 0)
2162 power_supply_changed(chip->batt_psy);
2163}
2164
Xiaozhe Shid035f6a2012-12-18 16:16:33 -08002165#define MIN_DELTA_625_UV 1000
2166static void calib_hkadc(struct pm8921_bms_chip *chip)
2167{
2168 int voltage, rc;
2169 struct pm8xxx_adc_chan_result result;
2170 int usb_chg;
2171 int this_delta;
2172
2173 mutex_lock(&chip->calib_mutex);
2174 rc = pm8xxx_adc_read(the_chip->ref1p25v_channel, &result);
2175 if (rc) {
2176 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
2177 goto out;
2178 }
2179 voltage = xoadc_reading_to_microvolt(result.adc_code);
2180
2181 pr_debug("result 1.25v = 0x%x, voltage = %duV adc_meas = %lld\n",
2182 result.adc_code, voltage, result.measurement);
2183
2184 chip->xoadc_v125 = voltage;
2185
2186 rc = pm8xxx_adc_read(the_chip->ref625mv_channel, &result);
2187 if (rc) {
2188 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
2189 goto out;
2190 }
2191 voltage = xoadc_reading_to_microvolt(result.adc_code);
2192
2193 usb_chg = usb_chg_plugged_in(chip);
2194 pr_debug("result 0.625V = 0x%x, voltage = %duV adc_meas = %lld usb_chg = %d\n",
2195 result.adc_code, voltage, result.measurement,
2196 usb_chg);
2197
2198 if (usb_chg)
2199 chip->xoadc_v0625_usb_present = voltage;
2200 else
2201 chip->xoadc_v0625_usb_absent = voltage;
2202
2203 chip->xoadc_v0625 = voltage;
2204 if (chip->xoadc_v0625_usb_present && chip->xoadc_v0625_usb_absent) {
2205 this_delta = chip->xoadc_v0625_usb_present
2206 - chip->xoadc_v0625_usb_absent;
2207 pr_debug("this_delta= %duV\n", this_delta);
2208 if (this_delta > MIN_DELTA_625_UV)
2209 last_usb_cal_delta_uv = this_delta;
2210 pr_debug("625V_present= %d, 625V_absent= %d, delta = %duV\n",
2211 chip->xoadc_v0625_usb_present,
2212 chip->xoadc_v0625_usb_absent,
2213 last_usb_cal_delta_uv);
2214 }
2215 pr_debug("calibration batt_temp = %d\n", chip->last_calib_temp);
2216out:
2217 mutex_unlock(&chip->calib_mutex);
2218}
2219
2220#define HKADC_CALIB_DELAY_S 600
2221#define HKADC_CALIB_DELTA_TEMP 20
2222static void calib_hkadc_check(struct pm8921_bms_chip *chip, int batt_temp)
2223{
2224 unsigned long time_since_last_calib;
2225 unsigned long tm_now_sec;
2226 int delta_temp;
2227 int rc;
2228
2229 rc = get_current_time(&tm_now_sec);
2230 if (rc) {
2231 pr_err("Could not read current time: %d\n", rc);
2232 return;
2233 }
2234 if (tm_now_sec > chip->last_calib_time) {
2235 time_since_last_calib = tm_now_sec - chip->last_calib_time;
2236 delta_temp = abs(chip->last_calib_temp - batt_temp);
2237 pr_debug("time since last calib: %lu, temp diff = %d\n",
2238 time_since_last_calib, delta_temp);
2239 if (time_since_last_calib >= HKADC_CALIB_DELAY_S
2240 || delta_temp > HKADC_CALIB_DELTA_TEMP) {
2241 chip->last_calib_temp = batt_temp;
2242 chip->last_calib_time = tm_now_sec;
2243 calib_hkadc(chip);
2244 }
2245 }
2246}
2247
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07002248/*
2249 * Remaining Usable Charge = remaining_charge (charge at ocv instance)
2250 * - coloumb counter charge
2251 * - unusable charge (due to battery resistance)
2252 * SOC% = (remaining usable charge/ fcc - usable_charge);
2253 */
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07002254static int calculate_state_of_charge(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002255 struct pm8921_soc_params *raw,
2256 int batt_temp, int chargecycles)
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07002257{
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002258 int remaining_usable_charge_uah, fcc_uah, unusable_charge_uah;
2259 int remaining_charge_uah, soc;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002260 int cc_uah;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002261 int rbatt;
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07002262 int iavg_ua;
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002263 int new_ocv;
Abhijeet Dharmapurikar6f42a232012-07-17 22:11:13 -07002264 int new_rc_uah;
2265 int new_ucc_uah;
2266 int new_rbatt;
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002267 int shutdown_soc;
Abhijeet Dharmapurikar0622a432012-08-28 16:34:18 -07002268 int new_calculated_soc;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07002269 static int firsttime = 1;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07002270
Xiaozhe Shid035f6a2012-12-18 16:16:33 -08002271 calib_hkadc_check(chip, batt_temp);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002272 calculate_soc_params(chip, raw, batt_temp, chargecycles,
2273 &fcc_uah,
2274 &unusable_charge_uah,
2275 &remaining_charge_uah,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002276 &cc_uah,
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07002277 &rbatt,
Xiaozhe Shid28e4972012-12-04 15:13:32 -08002278 &iavg_ua);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002279
2280 /* calculate remaining usable charge */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002281 remaining_usable_charge_uah = remaining_charge_uah
2282 - cc_uah
2283 - unusable_charge_uah;
2284
2285 pr_debug("RUC = %duAh\n", remaining_usable_charge_uah);
Abhijeet Dharmapurikarbbae88312012-02-14 18:56:42 -08002286 if (fcc_uah - unusable_charge_uah <= 0) {
Xiaozhe Shieec91df2012-11-29 12:11:42 -08002287 pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
Abhijeet Dharmapurikarbbae88312012-02-14 18:56:42 -08002288 fcc_uah, unusable_charge_uah);
2289 soc = 0;
2290 } else {
Abhijeet Dharmapurikar4ff213c2012-07-11 21:28:29 -07002291 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2292 (fcc_uah - unusable_charge_uah));
Abhijeet Dharmapurikarbbae88312012-02-14 18:56:42 -08002293 }
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002294
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07002295 if (firsttime && soc < 0) {
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002296 /*
2297 * first time calcualtion and the pon ocv is too low resulting
2298 * in a bad soc. Adjust ocv such that we get 0 soc
2299 */
2300 pr_debug("soc is %d, adjusting pon ocv to make it 0\n", soc);
2301 adjust_rc_and_uuc_for_specific_soc(
2302 chip,
2303 batt_temp, chargecycles,
2304 0,
2305 fcc_uah, unusable_charge_uah,
2306 cc_uah, remaining_charge_uah,
2307 rbatt,
2308 &new_ocv,
2309 &new_rc_uah, &new_ucc_uah,
2310 &new_rbatt);
2311 chip->last_ocv_uv = new_ocv;
2312 remaining_charge_uah = new_rc_uah;
2313 unusable_charge_uah = new_ucc_uah;
2314 rbatt = new_rbatt;
2315
2316 remaining_usable_charge_uah = remaining_charge_uah
2317 - cc_uah
2318 - unusable_charge_uah;
2319
2320 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2321 (fcc_uah - unusable_charge_uah));
2322 pr_debug("DONE for O soc is %d, pon ocv adjusted to %duV\n",
2323 soc, chip->last_ocv_uv);
2324 }
2325
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07002326 if (soc > 100)
2327 soc = 100;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07002328
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07002329 if (soc < 0) {
Xiaozhe Shieec91df2012-11-29 12:11:42 -08002330 pr_debug("bad rem_usb_chg = %d rem_chg %d,"
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002331 "cc_uah %d, unusb_chg %d\n",
2332 remaining_usable_charge_uah,
2333 remaining_charge_uah,
2334 cc_uah, unusable_charge_uah);
2335
Xiaozhe Shieec91df2012-11-29 12:11:42 -08002336 pr_debug("for bad rem_usb_chg last_ocv_uv = %d"
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07002337 "chargecycles = %d, batt_temp = %d"
2338 "fcc = %d soc =%d\n",
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002339 chip->last_ocv_uv, chargecycles, batt_temp,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002340 fcc_uah, soc);
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -08002341 soc = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002342 }
2343
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002344 mutex_lock(&soc_invalidation_mutex);
2345 shutdown_soc = chip->shutdown_soc;
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002346
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07002347 if (firsttime && soc != shutdown_soc
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002348 && is_shutdown_soc_within_limits(chip, soc)) {
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002349 /*
2350 * soc for the first time - use shutdown soc
2351 * to adjust pon ocv since it is a small percent away from
2352 * the real soc
2353 */
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002354 pr_debug("soc = %d before forcing shutdown_soc = %d\n",
2355 soc, shutdown_soc);
2356 adjust_rc_and_uuc_for_specific_soc(
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002357 chip,
2358 batt_temp, chargecycles,
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002359 shutdown_soc,
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002360 fcc_uah, unusable_charge_uah,
2361 cc_uah, remaining_charge_uah,
2362 rbatt,
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002363 &new_ocv,
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002364 &new_rc_uah, &new_ucc_uah,
2365 &new_rbatt);
2366
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002367 chip->pon_ocv_uv = chip->last_ocv_uv;
2368 chip->last_ocv_uv = new_ocv;
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002369 remaining_charge_uah = new_rc_uah;
2370 unusable_charge_uah = new_ucc_uah;
2371 rbatt = new_rbatt;
2372
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002373 remaining_usable_charge_uah = remaining_charge_uah
2374 - cc_uah
2375 - unusable_charge_uah;
2376
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002377 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2378 (fcc_uah - unusable_charge_uah));
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002379
2380 pr_debug("DONE for shutdown_soc = %d soc is %d, adjusted ocv to %duV\n",
2381 shutdown_soc, soc, chip->last_ocv_uv);
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002382 }
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002383 mutex_unlock(&soc_invalidation_mutex);
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002384
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07002385 pr_debug("SOC before adjustment = %d\n", soc);
Abhijeet Dharmapurikar0622a432012-08-28 16:34:18 -07002386 new_calculated_soc = adjust_soc(chip, soc, batt_temp, chargecycles,
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002387 rbatt, fcc_uah, unusable_charge_uah, cc_uah);
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07002388
Abhijeet Dharmapurikar0622a432012-08-28 16:34:18 -07002389 pr_debug("calculated SOC = %d\n", new_calculated_soc);
2390 if (new_calculated_soc != calculated_soc)
2391 update_power_supply(chip);
2392
2393 calculated_soc = new_calculated_soc;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07002394 firsttime = 0;
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08002395 get_current_time(&chip->last_recalc_time);
Abhijeet Dharmapurikar1939ede2013-01-10 15:59:33 -08002396
2397 if (chip->disable_flat_portion_ocv) {
2398 if (is_between(chip->ocv_dis_high_soc, chip->ocv_dis_low_soc,
2399 calculated_soc)) {
2400 pm8921_bms_stop_ocv_updates();
2401 } else {
2402 pm8921_bms_start_ocv_updates();
2403 }
2404 }
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002405 return calculated_soc;
2406}
2407
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08002408static int recalculate_soc(struct pm8921_bms_chip *chip)
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002409{
Xiaozhe Shid035f6a2012-12-18 16:16:33 -08002410 int batt_temp;
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002411 struct pm8921_soc_params raw;
2412 int soc;
2413
Abhijeet Dharmapurikarb2aeb7e2012-12-26 21:30:05 -08002414 wake_lock(&the_chip->soc_wake_lock);
Xiaozhe Shid035f6a2012-12-18 16:16:33 -08002415 get_batt_temp(chip, &batt_temp);
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002416
2417 mutex_lock(&chip->last_ocv_uv_mutex);
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08002418 read_soc_params_raw(chip, &raw, batt_temp);
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002419
2420 soc = calculate_state_of_charge(chip, &raw,
2421 batt_temp, last_chargecycles);
2422 mutex_unlock(&chip->last_ocv_uv_mutex);
Abhijeet Dharmapurikarb2aeb7e2012-12-26 21:30:05 -08002423 wake_unlock(&the_chip->soc_wake_lock);
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08002424 return soc;
2425}
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002426
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08002427static void calculate_soc_work(struct work_struct *work)
2428{
Willie Ruan38760d32013-01-14 15:46:01 -08002429 struct delayed_work *dwork = to_delayed_work(work);
2430 struct pm8921_bms_chip *chip = container_of(dwork,
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08002431 struct pm8921_bms_chip,
Willie Ruan38760d32013-01-14 15:46:01 -08002432 calculate_soc_delayed_work);
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08002433
2434 recalculate_soc(chip);
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002435 schedule_delayed_work(&chip->calculate_soc_delayed_work,
2436 round_jiffies_relative(msecs_to_jiffies
Abhijeet Dharmapurikar683472d2012-10-30 16:21:58 -07002437 (chip->soc_calc_period)));
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002438}
2439
2440static int report_state_of_charge(struct pm8921_bms_chip *chip)
2441{
2442 int soc = calculated_soc;
2443 int delta_time_us;
2444 struct timespec now;
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002445 int batt_temp;
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002446
Abhijeet Dharmapurikard02bd882012-08-22 17:48:16 -07002447 if (bms_fake_battery != -EINVAL) {
2448 pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery);
2449 return bms_fake_battery;
2450 }
2451
Xiaozhe Shid035f6a2012-12-18 16:16:33 -08002452 get_batt_temp(chip, &batt_temp);
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002453
2454 do_posix_clock_monotonic_gettime(&now);
2455 if (chip->t_soc_queried.tv_sec != 0) {
2456 delta_time_us
2457 = (now.tv_sec - chip->t_soc_queried.tv_sec) * USEC_PER_SEC
2458 + (now.tv_nsec - chip->t_soc_queried.tv_nsec) / 1000;
2459 } else {
2460 /* calculation for the first time */
2461 delta_time_us = 0;
2462 }
2463
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002464 /*
2465 * account for charge time - limit it to SOC_CATCHUP_SEC to
2466 * avoid overflows when charging continues for extended periods
2467 */
2468 if (the_chip->start_percent != -EINVAL) {
2469 if (the_chip->charge_time_us == 0) {
2470 /*
2471 * calculating soc for the first time
2472 * after start of chg. Initialize catchup time
2473 */
2474 if (abs(soc - last_soc) < MAX_CATCHUP_SOC)
2475 the_chip->catch_up_time_us =
2476 (soc - last_soc) * SOC_CATCHUP_SEC_PER_PERCENT
2477 * USEC_PER_SEC;
2478 else
2479 the_chip->catch_up_time_us =
2480 SOC_CATCHUP_SEC_MAX * USEC_PER_SEC;
2481
2482 if (the_chip->catch_up_time_us < 0)
2483 the_chip->catch_up_time_us = 0;
2484 }
2485
2486 /* add charge time */
2487 if (the_chip->charge_time_us
2488 < SOC_CATCHUP_SEC_MAX * USEC_PER_SEC)
2489 chip->charge_time_us += delta_time_us;
2490
2491 /* end catchup if calculated soc and last soc are same */
2492 if (last_soc == soc)
2493 the_chip->catch_up_time_us = 0;
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07002494 }
2495
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002496 /* last_soc < soc ... scale and catch up */
Xiaozhe Shid3ce9862012-12-17 19:34:50 -08002497 if (last_soc != -EINVAL && last_soc < soc && soc != 100)
Abhijeet Dharmapurikar1ed94182013-03-10 06:21:41 -07002498 soc = scale_soc_while_chg(chip, delta_time_us,
2499 soc, last_soc);
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002500
Abhijeet Dharmapurikar1ed94182013-03-10 06:21:41 -07002501 /* restrict soc to 1% change */
2502 if (last_soc != -EINVAL) {
2503 if (soc < last_soc && soc != 0)
2504 soc = last_soc - 1;
2505 if (soc > last_soc && soc != 100)
2506 soc = last_soc + 1;
2507 }
2508
2509 last_soc = bound_soc(soc);
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002510 backup_soc_and_iavg(chip, batt_temp, last_soc);
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -07002511 pr_debug("Reported SOC = %d\n", last_soc);
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002512 chip->t_soc_queried = now;
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -07002513
2514 return last_soc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002515}
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07002516
Abhijeet Dharmapurikarbd5e5082013-03-25 14:21:27 -07002517void pm8921_bms_battery_removed(void)
2518{
2519 if (!the_chip) {
2520 pr_err("called before initialization\n");
2521 return;
2522 }
2523 pr_info("Battery Removed Cleaning up\n");
2524
2525 cancel_delayed_work_sync(&the_chip->calculate_soc_delayed_work);
2526 calculated_soc = 0;
2527 the_chip->start_percent = -EINVAL;
2528 the_chip->end_percent = -EINVAL;
2529 /* cleanup for charge time catchup */
2530 the_chip->charge_time_us = 0;
2531 the_chip->catch_up_time_us = 0;
2532 /* cleanup for charge time adjusting */
2533 the_chip->soc_at_cv = -EINVAL;
2534 the_chip->soc_at_cv = -EINVAL;
2535 the_chip->prev_chg_soc = -EINVAL;
2536 the_chip->ibat_at_cv_ua = 0;
2537 the_chip->prev_vbat_batt_terminal_uv = 0;
2538 /* ocv cleanups */
2539 the_chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
2540 the_chip->prev_last_good_ocv_raw = OCV_RAW_UNINITIALIZED;
2541 the_chip->last_ocv_temp_decidegc = -EINVAL;
2542
2543 /* cleanup delta time */
2544 the_chip->tm_sec = 0;
2545
2546 /* cc and avg current cleanups */
2547 the_chip->prev_iavg_ua = 0;
2548 the_chip->last_cc_uah = INT_MIN;
2549
2550 /* report SOC cleanups */
2551 the_chip->t_soc_queried.tv_sec = 0;
2552 the_chip->t_soc_queried.tv_nsec = 0;
2553
2554 last_soc = -EINVAL;
2555 /* store invalid soc */
2556 pm8xxx_writeb(the_chip->dev->parent, TEMP_SOC_STORAGE, 0);
2557
2558 /* UUC related data is left as is - use the same historical load avg */
2559 update_power_supply(the_chip);
2560}
2561EXPORT_SYMBOL(pm8921_bms_battery_removed);
2562
2563void pm8921_bms_battery_inserted(void)
2564{
2565 if (!the_chip) {
2566 pr_err("called before initialization\n");
2567 return;
2568 }
2569
2570 pr_info("Battery Inserted\n");
2571 the_chip->last_ocv_uv = estimate_ocv(the_chip);
2572 schedule_delayed_work(&the_chip->calculate_soc_delayed_work, 0);
2573}
2574EXPORT_SYMBOL(pm8921_bms_battery_inserted);
2575
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002576void pm8921_bms_invalidate_shutdown_soc(void)
2577{
2578 int calculate_soc = 0;
2579 struct pm8921_bms_chip *chip = the_chip;
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002580
2581 pr_debug("Invalidating shutdown soc - the battery was removed\n");
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002582 if (shutdown_soc_invalid)
2583 return;
2584
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002585 mutex_lock(&soc_invalidation_mutex);
2586 shutdown_soc_invalid = 1;
2587 last_soc = -EINVAL;
2588 if (the_chip) {
2589 /* reset to pon ocv undoing what the adjusting did */
2590 if (the_chip->pon_ocv_uv) {
2591 the_chip->last_ocv_uv = the_chip->pon_ocv_uv;
2592 calculate_soc = 1;
2593 pr_debug("resetting ocv to pon_ocv = %d\n",
2594 the_chip->pon_ocv_uv);
2595 }
2596 }
2597 mutex_unlock(&soc_invalidation_mutex);
2598 if (!calculate_soc)
2599 return;
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08002600 recalculate_soc(chip);
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002601}
2602EXPORT_SYMBOL(pm8921_bms_invalidate_shutdown_soc);
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002603
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002604static void calibrate_hkadc_work(struct work_struct *work)
2605{
2606 struct pm8921_bms_chip *chip = container_of(work,
2607 struct pm8921_bms_chip, calib_hkadc_work);
2608
2609 calib_hkadc(chip);
2610}
2611
Abhijeet Dharmapurikar1b8e8292012-01-17 11:01:44 -08002612void pm8921_bms_calibrate_hkadc(void)
2613{
2614 schedule_work(&the_chip->calib_hkadc_work);
2615}
2616
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07002617int pm8921_bms_get_vsense_avg(int *result)
2618{
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002619 int rc = -EINVAL;
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002620
2621 if (the_chip) {
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002622 mutex_lock(&the_chip->bms_output_lock);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002623 pm_bms_lock_output_data(the_chip);
2624 rc = read_vsense_avg(the_chip, result);
2625 pm_bms_unlock_output_data(the_chip);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002626 mutex_unlock(&the_chip->bms_output_lock);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002627 }
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07002628
2629 pr_err("called before initialization\n");
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002630 return rc;
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07002631}
2632EXPORT_SYMBOL(pm8921_bms_get_vsense_avg);
2633
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08002634int pm8921_bms_get_battery_current(int *result_ua)
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07002635{
Xiaozhe Shid69c91e2012-11-06 10:00:38 -08002636 int vsense_uv;
Anirudh Ghayal3a38a382012-12-05 10:42:17 +05302637 int rc = 0;
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002638
Anirudh Ghayal3a38a382012-12-05 10:42:17 +05302639 *result_ua = 0;
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07002640 if (!the_chip) {
2641 pr_err("called before initialization\n");
2642 return -EINVAL;
2643 }
Xiaozhe Shid69c91e2012-11-06 10:00:38 -08002644 if (the_chip->r_sense_uohm == 0) {
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07002645 pr_err("r_sense is zero\n");
2646 return -EINVAL;
2647 }
2648
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002649 mutex_lock(&the_chip->bms_output_lock);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002650 pm_bms_lock_output_data(the_chip);
Anirudh Ghayal3a38a382012-12-05 10:42:17 +05302651 rc = read_vsense_avg(the_chip, &vsense_uv);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002652 pm_bms_unlock_output_data(the_chip);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002653 mutex_unlock(&the_chip->bms_output_lock);
Anirudh Ghayal3a38a382012-12-05 10:42:17 +05302654 if (rc) {
2655 pr_err("Unable to read vsense average\n");
2656 goto error_vsense;
2657 }
Xiaozhe Shid69c91e2012-11-06 10:00:38 -08002658 pr_debug("vsense=%duV\n", vsense_uv);
Abhijeet Dharmapurikara7a1c6b2011-08-17 10:04:58 -07002659 /* cast for signed division */
Xiaozhe Shid69c91e2012-11-06 10:00:38 -08002660 *result_ua = div_s64(vsense_uv * 1000000LL, the_chip->r_sense_uohm);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002661 pr_debug("ibat=%duA\n", *result_ua);
Anirudh Ghayal3a38a382012-12-05 10:42:17 +05302662
2663error_vsense:
2664 return rc;
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07002665}
2666EXPORT_SYMBOL(pm8921_bms_get_battery_current);
2667
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002668int pm8921_bms_get_percent_charge(void)
2669{
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07002670 if (!the_chip) {
2671 pr_err("called before initialization\n");
2672 return -EINVAL;
2673 }
2674
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002675 return report_state_of_charge(the_chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002676}
2677EXPORT_SYMBOL_GPL(pm8921_bms_get_percent_charge);
2678
Abhijeet Dharmapurikareca7b3e2012-12-26 21:30:29 -08002679int pm8921_bms_get_current_max(void)
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002680{
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002681 if (!the_chip) {
2682 pr_err("called before initialization\n");
2683 return -EINVAL;
2684 }
Abhijeet Dharmapurikareca7b3e2012-12-26 21:30:29 -08002685 return the_chip->imax_ua;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002686}
Abhijeet Dharmapurikareca7b3e2012-12-26 21:30:29 -08002687EXPORT_SYMBOL_GPL(pm8921_bms_get_current_max);
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002688
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07002689int pm8921_bms_get_fcc(void)
2690{
Xiaozhe Shid035f6a2012-12-18 16:16:33 -08002691 int batt_temp;
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07002692
2693 if (!the_chip) {
2694 pr_err("called before initialization\n");
2695 return -EINVAL;
2696 }
2697
Xiaozhe Shid035f6a2012-12-18 16:16:33 -08002698 get_batt_temp(the_chip, &batt_temp);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002699 return calculate_fcc_uah(the_chip, batt_temp, last_chargecycles);
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07002700}
2701EXPORT_SYMBOL_GPL(pm8921_bms_get_fcc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002702void pm8921_bms_charging_began(void)
2703{
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08002704 struct pm8921_soc_params raw;
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08002705 int batt_temp;
2706
2707 get_batt_temp(the_chip, &batt_temp);
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08002708
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002709 mutex_lock(&the_chip->last_ocv_uv_mutex);
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08002710 read_soc_params_raw(the_chip, &raw, batt_temp);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002711 mutex_unlock(&the_chip->last_ocv_uv_mutex);
2712
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002713 the_chip->start_percent = report_state_of_charge(the_chip);
2714
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08002715 bms_start_percent = the_chip->start_percent;
2716 bms_start_ocv_uv = raw.last_good_ocv_uv;
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08002717 calculate_cc_uah(the_chip, raw.cc, &bms_start_cc_uah);
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -08002718 pm_bms_masked_write(the_chip, BMS_TOLERANCES,
2719 IBAT_TOL_MASK, IBAT_TOL_DEFAULT);
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002720 the_chip->charge_time_us = 0;
2721 the_chip->catch_up_time_us = 0;
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07002722
2723 the_chip->soc_at_cv = -EINVAL;
2724 the_chip->prev_chg_soc = -EINVAL;
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07002725 pr_debug("start_percent = %u%%\n", the_chip->start_percent);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002726}
2727EXPORT_SYMBOL_GPL(pm8921_bms_charging_began);
2728
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08002729#define DELTA_FCC_PERCENT 3
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002730#define MIN_START_PERCENT_FOR_LEARNING 30
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07002731void pm8921_bms_charging_end(int is_battery_full)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002732{
Xiaozhe Shid035f6a2012-12-18 16:16:33 -08002733 int batt_temp;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002734 struct pm8921_soc_params raw;
2735
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002736 if (the_chip == NULL)
2737 return;
2738
Xiaozhe Shid035f6a2012-12-18 16:16:33 -08002739 get_batt_temp(the_chip, &batt_temp);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002740
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002741 mutex_lock(&the_chip->last_ocv_uv_mutex);
2742
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08002743 read_soc_params_raw(the_chip, &raw, batt_temp);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002744
Abhijeet Dharmapurikar57aeb922012-01-25 13:22:26 -08002745 calculate_cc_uah(the_chip, raw.cc, &bms_end_cc_uah);
2746
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002747 bms_end_ocv_uv = raw.last_good_ocv_uv;
2748
Abhijeet Dharmapurikare88f2462012-04-25 18:22:38 -07002749 if (is_battery_full && the_chip->enable_fcc_learning
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002750 && the_chip->start_percent <= MIN_START_PERCENT_FOR_LEARNING) {
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002751 int fcc_uah, new_fcc_uah, delta_fcc_uah;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07002752
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002753 new_fcc_uah = calculate_real_fcc_uah(the_chip, &raw,
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08002754 batt_temp, last_chargecycles,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002755 &fcc_uah);
2756 delta_fcc_uah = new_fcc_uah - fcc_uah;
2757 if (delta_fcc_uah < 0)
2758 delta_fcc_uah = -delta_fcc_uah;
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08002759
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002760 if (delta_fcc_uah * 100 > (DELTA_FCC_PERCENT * fcc_uah)) {
2761 /* new_fcc_uah is outside the scope limit it */
2762 if (new_fcc_uah > fcc_uah)
2763 new_fcc_uah
Abhijeet Dharmapurikar873ffd12012-04-17 00:06:15 -07002764 = (fcc_uah +
2765 (DELTA_FCC_PERCENT * fcc_uah) / 100);
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002766 else
2767 new_fcc_uah
Abhijeet Dharmapurikar873ffd12012-04-17 00:06:15 -07002768 = (fcc_uah -
2769 (DELTA_FCC_PERCENT * fcc_uah) / 100);
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002770
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08002771 pr_debug("delta_fcc=%d > %d percent of fcc=%d"
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002772 "restring it to %d\n",
2773 delta_fcc_uah, DELTA_FCC_PERCENT,
2774 fcc_uah, new_fcc_uah);
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08002775 }
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -08002776
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002777 last_real_fcc_mah = new_fcc_uah/1000;
2778 last_real_fcc_batt_temp = batt_temp;
2779 readjust_fcc_table();
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002780 }
2781
2782 if (is_battery_full) {
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002783 the_chip->ocv_reading_at_100 = raw.last_good_ocv_raw;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002784
2785 the_chip->last_ocv_uv = the_chip->max_voltage_uv;
2786 raw.last_good_ocv_uv = the_chip->max_voltage_uv;
Abhijeet Dharmapurikar28d51372013-02-27 19:16:22 -08002787 raw.cc = 0;
2788 /* reset the cc in h/w */
2789 reset_cc(the_chip);
Abhijeet Dharmapurikar079d6c52013-01-25 13:10:07 -08002790 the_chip->last_ocv_temp_decidegc = batt_temp;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07002791 /*
2792 * since we are treating this as an ocv event
2793 * forget the old cc value
2794 */
2795 the_chip->last_cc_uah = 0;
Abhijeet Dharmapurikar28d51372013-02-27 19:16:22 -08002796 pr_debug("EOC BATT_FULL ocv_reading = 0x%x\n",
2797 the_chip->ocv_reading_at_100);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07002798 }
2799
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002800 the_chip->end_percent = calculate_state_of_charge(the_chip, &raw,
2801 batt_temp, last_chargecycles);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002802 mutex_unlock(&the_chip->last_ocv_uv_mutex);
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08002803
2804 bms_end_percent = the_chip->end_percent;
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08002805
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07002806 if (the_chip->end_percent > the_chip->start_percent) {
Abhijeet Dharmapurikar8a113f82012-01-19 10:58:45 -08002807 last_charge_increase +=
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07002808 the_chip->end_percent - the_chip->start_percent;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002809 if (last_charge_increase > 100) {
2810 last_chargecycles++;
2811 last_charge_increase = last_charge_increase % 100;
2812 }
2813 }
2814 pr_debug("end_percent = %u%% last_charge_increase = %d"
2815 "last_chargecycles = %d\n",
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07002816 the_chip->end_percent,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002817 last_charge_increase,
2818 last_chargecycles);
Abhijeet Dharmapurikardba91f42011-12-21 16:56:13 -08002819 the_chip->start_percent = -EINVAL;
2820 the_chip->end_percent = -EINVAL;
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002821 the_chip->charge_time_us = 0;
2822 the_chip->catch_up_time_us = 0;
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07002823 the_chip->soc_at_cv = -EINVAL;
2824 the_chip->prev_chg_soc = -EINVAL;
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -08002825 pm_bms_masked_write(the_chip, BMS_TOLERANCES,
2826 IBAT_TOL_MASK, IBAT_TOL_NOCHG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002827}
2828EXPORT_SYMBOL_GPL(pm8921_bms_charging_end);
2829
2830static irqreturn_t pm8921_bms_sbi_write_ok_handler(int irq, void *data)
2831{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002832 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002833 return IRQ_HANDLED;
2834}
2835
2836static irqreturn_t pm8921_bms_cc_thr_handler(int irq, void *data)
2837{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002838 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002839 return IRQ_HANDLED;
2840}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002841
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002842static irqreturn_t pm8921_bms_vsense_thr_handler(int irq, void *data)
2843{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002844 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002845 return IRQ_HANDLED;
2846}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002847
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002848static irqreturn_t pm8921_bms_vsense_for_r_handler(int irq, void *data)
2849{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002850 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002851 return IRQ_HANDLED;
2852}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002853
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002854static irqreturn_t pm8921_bms_ocv_for_r_handler(int irq, void *data)
2855{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002856 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002857
2858 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002859 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002860 return IRQ_HANDLED;
2861}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002862
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002863static irqreturn_t pm8921_bms_good_ocv_handler(int irq, void *data)
2864{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002865 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002866
2867 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002868 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002869 return IRQ_HANDLED;
2870}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002871
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002872struct pm_bms_irq_init_data {
2873 unsigned int irq_id;
2874 char *name;
2875 unsigned long flags;
2876 irqreturn_t (*handler)(int, void *);
2877};
2878
2879#define BMS_IRQ(_id, _flags, _handler) \
2880{ \
2881 .irq_id = _id, \
2882 .name = #_id, \
2883 .flags = _flags, \
2884 .handler = _handler, \
2885}
2886
2887struct pm_bms_irq_init_data bms_irq_data[] = {
2888 BMS_IRQ(PM8921_BMS_SBI_WRITE_OK, IRQF_TRIGGER_RISING,
2889 pm8921_bms_sbi_write_ok_handler),
2890 BMS_IRQ(PM8921_BMS_CC_THR, IRQF_TRIGGER_RISING,
2891 pm8921_bms_cc_thr_handler),
2892 BMS_IRQ(PM8921_BMS_VSENSE_THR, IRQF_TRIGGER_RISING,
2893 pm8921_bms_vsense_thr_handler),
2894 BMS_IRQ(PM8921_BMS_VSENSE_FOR_R, IRQF_TRIGGER_RISING,
2895 pm8921_bms_vsense_for_r_handler),
2896 BMS_IRQ(PM8921_BMS_OCV_FOR_R, IRQF_TRIGGER_RISING,
2897 pm8921_bms_ocv_for_r_handler),
2898 BMS_IRQ(PM8921_BMS_GOOD_OCV, IRQF_TRIGGER_RISING,
2899 pm8921_bms_good_ocv_handler),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002900};
2901
2902static void free_irqs(struct pm8921_bms_chip *chip)
2903{
2904 int i;
2905
2906 for (i = 0; i < PM_BMS_MAX_INTS; i++)
2907 if (chip->pmic_bms_irq[i]) {
2908 free_irq(chip->pmic_bms_irq[i], NULL);
2909 chip->pmic_bms_irq[i] = 0;
2910 }
2911}
2912
2913static int __devinit request_irqs(struct pm8921_bms_chip *chip,
2914 struct platform_device *pdev)
2915{
2916 struct resource *res;
2917 int ret, i;
2918
2919 ret = 0;
2920 bitmap_fill(chip->enabled_irqs, PM_BMS_MAX_INTS);
2921
2922 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
2923 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
2924 bms_irq_data[i].name);
2925 if (res == NULL) {
2926 pr_err("couldn't find %s\n", bms_irq_data[i].name);
2927 goto err_out;
2928 }
2929 ret = request_irq(res->start, bms_irq_data[i].handler,
2930 bms_irq_data[i].flags,
2931 bms_irq_data[i].name, chip);
2932 if (ret < 0) {
2933 pr_err("couldn't request %d (%s) %d\n", res->start,
2934 bms_irq_data[i].name, ret);
2935 goto err_out;
2936 }
2937 chip->pmic_bms_irq[bms_irq_data[i].irq_id] = res->start;
2938 pm8921_bms_disable_irq(chip, bms_irq_data[i].irq_id);
2939 }
2940 return 0;
2941
2942err_out:
2943 free_irqs(chip);
2944 return -EINVAL;
2945}
2946
2947#define EN_BMS_BIT BIT(7)
2948#define EN_PON_HS_BIT BIT(0)
2949static int __devinit pm8921_bms_hw_init(struct pm8921_bms_chip *chip)
2950{
2951 int rc;
2952
2953 rc = pm_bms_masked_write(chip, BMS_CONTROL,
2954 EN_BMS_BIT | EN_PON_HS_BIT, EN_BMS_BIT | EN_PON_HS_BIT);
2955 if (rc) {
2956 pr_err("failed to enable pon and bms addr = %d %d",
2957 BMS_CONTROL, rc);
2958 }
2959
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -08002960 /* The charger will call start charge later if usb is present */
2961 pm_bms_masked_write(chip, BMS_TOLERANCES,
2962 IBAT_TOL_MASK, IBAT_TOL_NOCHG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002963 return 0;
2964}
2965
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002966static void check_initial_ocv(struct pm8921_bms_chip *chip)
2967{
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002968 int ocv_uv, rc;
2969 int16_t ocv_raw;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08002970 int usb_chg;
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002971
2972 /*
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07002973 * Check if a ocv is available in bms hw,
2974 * if not compute it here at boot time and save it
2975 * in the last_ocv_uv.
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002976 */
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002977 ocv_uv = 0;
2978 pm_bms_read_output_data(chip, LAST_GOOD_OCV_VALUE, &ocv_raw);
Abhijeet Dharmapurikar9e948982012-08-28 15:21:23 -07002979 usb_chg = usb_chg_plugged_in(chip);
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08002980 rc = convert_vbatt_raw_to_uv(chip, usb_chg, ocv_raw, &ocv_uv);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002981 if (rc || ocv_uv == 0) {
2982 rc = adc_based_ocv(chip, &ocv_uv);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07002983 if (rc) {
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002984 pr_err("failed to read adc based ocv_uv rc = %d\n", rc);
2985 ocv_uv = DEFAULT_OCV_MICROVOLTS;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07002986 }
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002987 }
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002988 chip->last_ocv_uv = ocv_uv;
2989 pr_debug("ocv_uv = %d last_ocv_uv = %d\n", ocv_uv, chip->last_ocv_uv);
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002990}
2991
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002992static int64_t read_battery_id(struct pm8921_bms_chip *chip)
2993{
2994 int rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07002995 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002996
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07002997 rc = pm8xxx_adc_read(chip->batt_id_channel, &result);
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002998 if (rc) {
2999 pr_err("error reading batt id channel = %d, rc = %d\n",
3000 chip->vbat_channel, rc);
3001 return rc;
3002 }
3003 pr_debug("batt_id phy = %lld meas = 0x%llx\n", result.physical,
3004 result.measurement);
David Keitel8f2601b2012-02-14 22:31:07 -08003005 return result.adc_code;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07003006}
3007
David Keitel8f2601b2012-02-14 22:31:07 -08003008#define PALLADIUM_ID_MIN 0x7F40
3009#define PALLADIUM_ID_MAX 0x7F5A
3010#define DESAY_5200_ID_MIN 0x7F7F
3011#define DESAY_5200_ID_MAX 0x802F
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07003012static int set_battery_data(struct pm8921_bms_chip *chip)
3013{
3014 int64_t battery_id;
3015
David Keitel35e11872012-02-17 17:40:42 -08003016 if (chip->batt_type == BATT_DESAY)
3017 goto desay;
3018 else if (chip->batt_type == BATT_PALLADIUM)
3019 goto palladium;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07003020
David Keitel35e11872012-02-17 17:40:42 -08003021 battery_id = read_battery_id(chip);
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07003022 if (battery_id < 0) {
3023 pr_err("cannot read battery id err = %lld\n", battery_id);
3024 return battery_id;
3025 }
3026
3027 if (is_between(PALLADIUM_ID_MIN, PALLADIUM_ID_MAX, battery_id)) {
David Keitel35e11872012-02-17 17:40:42 -08003028 goto palladium;
3029 } else if (is_between(DESAY_5200_ID_MIN, DESAY_5200_ID_MAX,
3030 battery_id)) {
3031 goto desay;
3032 } else {
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08003033 pr_warn("invalid battid, palladium 1500 assumed batt_id %llx\n",
3034 battery_id);
3035 goto palladium;
David Keitel35e11872012-02-17 17:40:42 -08003036 }
3037
3038palladium:
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07003039 chip->fcc = palladium_1500_data.fcc;
3040 chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut;
3041 chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut;
3042 chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut;
3043 chip->pc_sf_lut = palladium_1500_data.pc_sf_lut;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08003044 chip->rbatt_sf_lut = palladium_1500_data.rbatt_sf_lut;
3045 chip->default_rbatt_mohm
3046 = palladium_1500_data.default_rbatt_mohm;
Abhijeet Dharmapurikarded189b2012-03-30 10:12:28 -07003047 chip->delta_rbatt_mohm = palladium_1500_data.delta_rbatt_mohm;
Abhijeet Dharmapurikar03c6c232013-02-13 17:57:40 -08003048 chip->rbatt_capacitive_mohm
3049 = palladium_1500_data.rbatt_capacitive_mohm;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07003050 return 0;
David Keitel35e11872012-02-17 17:40:42 -08003051desay:
David Keitel8f2601b2012-02-14 22:31:07 -08003052 chip->fcc = desay_5200_data.fcc;
3053 chip->fcc_temp_lut = desay_5200_data.fcc_temp_lut;
David Keitel8f2601b2012-02-14 22:31:07 -08003054 chip->pc_temp_ocv_lut = desay_5200_data.pc_temp_ocv_lut;
3055 chip->pc_sf_lut = desay_5200_data.pc_sf_lut;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08003056 chip->rbatt_sf_lut = desay_5200_data.rbatt_sf_lut;
3057 chip->default_rbatt_mohm = desay_5200_data.default_rbatt_mohm;
Abhijeet Dharmapurikarded189b2012-03-30 10:12:28 -07003058 chip->delta_rbatt_mohm = desay_5200_data.delta_rbatt_mohm;
Abhijeet Dharmapurikar03c6c232013-02-13 17:57:40 -08003059 chip->rbatt_capacitive_mohm
3060 = desay_5200_data.rbatt_capacitive_mohm;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07003061 return 0;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07003062}
3063
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08003064enum bms_request_operation {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003065 CALC_FCC,
3066 CALC_PC,
3067 CALC_SOC,
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07003068 CALIB_HKADC,
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07003069 CALIB_CCADC,
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08003070 GET_VBAT_VSENSE_SIMULTANEOUS,
Abhijeet Dharmapurikarc99f5862012-03-14 19:33:16 -07003071 STOP_OCV,
3072 START_OCV,
Abhijeet Dharmapurikar1ed94182013-03-10 06:21:41 -07003073 SET_OCV,
Abhijeet Dharmapurikarbd5e5082013-03-25 14:21:27 -07003074 BATT_PRESENT,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003075};
3076
3077static int test_batt_temp = 5;
3078static int test_chargecycle = 150;
3079static int test_ocv = 3900000;
3080enum {
3081 TEST_BATT_TEMP,
3082 TEST_CHARGE_CYCLE,
3083 TEST_OCV,
3084};
3085static int get_test_param(void *data, u64 * val)
3086{
3087 switch ((int)data) {
3088 case TEST_BATT_TEMP:
3089 *val = test_batt_temp;
3090 break;
3091 case TEST_CHARGE_CYCLE:
3092 *val = test_chargecycle;
3093 break;
3094 case TEST_OCV:
3095 *val = test_ocv;
3096 break;
3097 default:
3098 return -EINVAL;
3099 }
3100 return 0;
3101}
3102static int set_test_param(void *data, u64 val)
3103{
3104 switch ((int)data) {
3105 case TEST_BATT_TEMP:
3106 test_batt_temp = (int)val;
3107 break;
3108 case TEST_CHARGE_CYCLE:
3109 test_chargecycle = (int)val;
3110 break;
3111 case TEST_OCV:
3112 test_ocv = (int)val;
3113 break;
3114 default:
3115 return -EINVAL;
3116 }
3117 return 0;
3118}
3119DEFINE_SIMPLE_ATTRIBUTE(temp_fops, get_test_param, set_test_param, "%llu\n");
3120
3121static int get_calc(void *data, u64 * val)
3122{
3123 int param = (int)data;
3124 int ret = 0;
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08003125 int ibat_ua, vbat_uv;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08003126 struct pm8921_soc_params raw;
3127
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08003128 read_soc_params_raw(the_chip, &raw, 300);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003129
3130 *val = 0;
3131
3132 /* global irq number passed in via data */
3133 switch (param) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003134 case CALC_FCC:
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08003135 *val = calculate_fcc_uah(the_chip, test_batt_temp,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003136 test_chargecycle);
3137 break;
3138 case CALC_PC:
3139 *val = calculate_pc(the_chip, test_ocv, test_batt_temp,
3140 test_chargecycle);
3141 break;
3142 case CALC_SOC:
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08003143 *val = calculate_state_of_charge(the_chip, &raw,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003144 test_batt_temp, test_chargecycle);
3145 break;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07003146 case CALIB_HKADC:
3147 /* reading this will trigger calibration */
3148 *val = 0;
3149 calib_hkadc(the_chip);
3150 break;
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07003151 case CALIB_CCADC:
3152 /* reading this will trigger calibration */
3153 *val = 0;
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08003154 pm8xxx_calib_ccadc();
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07003155 break;
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08003156 case GET_VBAT_VSENSE_SIMULTANEOUS:
3157 /* reading this will call simultaneous vbat and vsense */
3158 *val =
3159 pm8921_bms_get_simultaneous_battery_voltage_and_current(
3160 &ibat_ua,
3161 &vbat_uv);
Abhijeet Dharmapurikarc99f5862012-03-14 19:33:16 -07003162 default:
3163 ret = -EINVAL;
3164 }
3165 return ret;
3166}
3167
3168static int set_calc(void *data, u64 val)
3169{
3170 int param = (int)data;
3171 int ret = 0;
3172
3173 switch (param) {
3174 case STOP_OCV:
Abhijeet Dharmapurikar7df608c2012-12-26 21:28:43 -08003175 pm8921_bms_stop_ocv_updates();
Abhijeet Dharmapurikarc99f5862012-03-14 19:33:16 -07003176 break;
3177 case START_OCV:
Abhijeet Dharmapurikar7df608c2012-12-26 21:28:43 -08003178 pm8921_bms_start_ocv_updates();
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08003179 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003180 default:
3181 ret = -EINVAL;
3182 }
3183 return ret;
3184}
Abhijeet Dharmapurikarc99f5862012-03-14 19:33:16 -07003185DEFINE_SIMPLE_ATTRIBUTE(calc_fops, get_calc, set_calc, "%llu\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003186
3187static int get_reading(void *data, u64 * val)
3188{
3189 int param = (int)data;
3190 int ret = 0;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08003191 struct pm8921_soc_params raw;
3192
Abhijeet Dharmapurikara937bc42013-02-13 19:51:56 -08003193 mutex_lock(&the_chip->last_ocv_uv_mutex);
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08003194 read_soc_params_raw(the_chip, &raw, 300);
Abhijeet Dharmapurikara937bc42013-02-13 19:51:56 -08003195 mutex_lock(&the_chip->last_ocv_uv_mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003196
3197 *val = 0;
3198
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003199 switch (param) {
3200 case CC_MSB:
3201 case CC_LSB:
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08003202 *val = raw.cc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003203 break;
3204 case LAST_GOOD_OCV_VALUE:
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08003205 *val = raw.last_good_ocv_uv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003206 break;
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07003207 case VSENSE_AVG:
3208 read_vsense_avg(the_chip, (uint *)val);
3209 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003210 default:
3211 ret = -EINVAL;
3212 }
3213 return ret;
3214}
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07003215DEFINE_SIMPLE_ATTRIBUTE(reading_fops, get_reading, NULL, "%lld\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003216
3217static int get_rt_status(void *data, u64 * val)
3218{
3219 int i = (int)data;
3220 int ret;
3221
3222 /* global irq number passed in via data */
3223 ret = pm_bms_get_rt_status(the_chip, i);
3224 *val = ret;
3225 return 0;
3226}
3227DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n");
3228
3229static int get_reg(void *data, u64 * val)
3230{
3231 int addr = (int)data;
3232 int ret;
3233 u8 temp;
3234
3235 ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp);
3236 if (ret) {
3237 pr_err("pm8xxx_readb to %x value = %d errored = %d\n",
3238 addr, temp, ret);
3239 return -EAGAIN;
3240 }
3241 *val = temp;
3242 return 0;
3243}
3244
3245static int set_reg(void *data, u64 val)
3246{
3247 int addr = (int)data;
3248 int ret;
3249 u8 temp;
3250
3251 temp = (u8) val;
3252 ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp);
3253 if (ret) {
3254 pr_err("pm8xxx_writeb to %x value = %d errored = %d\n",
3255 addr, temp, ret);
3256 return -EAGAIN;
3257 }
3258 return 0;
3259}
3260DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
3261
3262static void create_debugfs_entries(struct pm8921_bms_chip *chip)
3263{
3264 int i;
3265
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08003266 chip->dent = debugfs_create_dir("pm8921-bms", NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003267
3268 if (IS_ERR(chip->dent)) {
3269 pr_err("pmic bms couldnt create debugfs dir\n");
3270 return;
3271 }
3272
3273 debugfs_create_file("BMS_CONTROL", 0644, chip->dent,
3274 (void *)BMS_CONTROL, &reg_fops);
3275 debugfs_create_file("BMS_OUTPUT0", 0644, chip->dent,
3276 (void *)BMS_OUTPUT0, &reg_fops);
3277 debugfs_create_file("BMS_OUTPUT1", 0644, chip->dent,
3278 (void *)BMS_OUTPUT1, &reg_fops);
3279 debugfs_create_file("BMS_TEST1", 0644, chip->dent,
3280 (void *)BMS_TEST1, &reg_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003281
3282 debugfs_create_file("test_batt_temp", 0644, chip->dent,
3283 (void *)TEST_BATT_TEMP, &temp_fops);
3284 debugfs_create_file("test_chargecycle", 0644, chip->dent,
3285 (void *)TEST_CHARGE_CYCLE, &temp_fops);
3286 debugfs_create_file("test_ocv", 0644, chip->dent,
3287 (void *)TEST_OCV, &temp_fops);
3288
3289 debugfs_create_file("read_cc", 0644, chip->dent,
3290 (void *)CC_MSB, &reading_fops);
3291 debugfs_create_file("read_last_good_ocv", 0644, chip->dent,
3292 (void *)LAST_GOOD_OCV_VALUE, &reading_fops);
3293 debugfs_create_file("read_vbatt_for_rbatt", 0644, chip->dent,
3294 (void *)VBATT_FOR_RBATT, &reading_fops);
3295 debugfs_create_file("read_vsense_for_rbatt", 0644, chip->dent,
3296 (void *)VSENSE_FOR_RBATT, &reading_fops);
3297 debugfs_create_file("read_ocv_for_rbatt", 0644, chip->dent,
3298 (void *)OCV_FOR_RBATT, &reading_fops);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07003299 debugfs_create_file("read_vsense_avg", 0644, chip->dent,
3300 (void *)VSENSE_AVG, &reading_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003301
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003302 debugfs_create_file("show_fcc", 0644, chip->dent,
3303 (void *)CALC_FCC, &calc_fops);
3304 debugfs_create_file("show_pc", 0644, chip->dent,
3305 (void *)CALC_PC, &calc_fops);
3306 debugfs_create_file("show_soc", 0644, chip->dent,
3307 (void *)CALC_SOC, &calc_fops);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07003308 debugfs_create_file("calib_hkadc", 0644, chip->dent,
3309 (void *)CALIB_HKADC, &calc_fops);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07003310 debugfs_create_file("calib_ccadc", 0644, chip->dent,
3311 (void *)CALIB_CCADC, &calc_fops);
Abhijeet Dharmapurikarc99f5862012-03-14 19:33:16 -07003312 debugfs_create_file("stop_ocv", 0644, chip->dent,
3313 (void *)STOP_OCV, &calc_fops);
3314 debugfs_create_file("start_ocv", 0644, chip->dent,
3315 (void *)START_OCV, &calc_fops);
Abhijeet Dharmapurikar1ed94182013-03-10 06:21:41 -07003316 debugfs_create_file("set_ocv", 0644, chip->dent,
3317 (void *)SET_OCV, &calc_fops);
Abhijeet Dharmapurikarbd5e5082013-03-25 14:21:27 -07003318 debugfs_create_file("batt_present", 0644, chip->dent,
3319 (void *)BATT_PRESENT, &calc_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003320
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08003321 debugfs_create_file("simultaneous", 0644, chip->dent,
3322 (void *)GET_VBAT_VSENSE_SIMULTANEOUS, &calc_fops);
3323
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003324 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
3325 if (chip->pmic_bms_irq[bms_irq_data[i].irq_id])
3326 debugfs_create_file(bms_irq_data[i].name, 0444,
3327 chip->dent,
3328 (void *)bms_irq_data[i].irq_id,
3329 &rt_fops);
3330 }
3331}
3332
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -07003333#define REG_SBI_CONFIG 0x04F
3334#define PAGE3_ENABLE_MASK 0x6
3335#define PROGRAM_REV_MASK 0x0F
3336#define PROGRAM_REV 0x9
3337static int read_ocv_trim(struct pm8921_bms_chip *chip)
3338{
3339 int rc;
3340 u8 reg, sbi_config;
3341
3342 rc = pm8xxx_readb(chip->dev->parent, REG_SBI_CONFIG, &sbi_config);
3343 if (rc) {
3344 pr_err("error = %d reading sbi config reg\n", rc);
3345 return rc;
3346 }
3347
3348 reg = sbi_config | PAGE3_ENABLE_MASK;
3349 rc = pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, reg);
3350 if (rc) {
3351 pr_err("error = %d writing sbi config reg\n", rc);
3352 return rc;
3353 }
3354
3355 rc = pm8xxx_readb(chip->dev->parent, TEST_PROGRAM_REV, &reg);
3356 if (rc)
3357 pr_err("Error %d reading %d addr %d\n",
3358 rc, reg, TEST_PROGRAM_REV);
3359 pr_err("program rev reg is 0x%x\n", reg);
3360 reg &= PROGRAM_REV_MASK;
3361
3362 /* If the revision is equal or higher do not adjust trim delta */
3363 if (reg >= PROGRAM_REV) {
3364 chip->amux_2_trim_delta = 0;
3365 goto restore_sbi_config;
3366 }
3367
3368 rc = pm8xxx_readb(chip->dev->parent, AMUX_TRIM_2, &reg);
3369 if (rc) {
3370 pr_err("error = %d reading trim reg\n", rc);
3371 return rc;
3372 }
3373
3374 pr_err("trim reg is 0x%x\n", reg);
3375 chip->amux_2_trim_delta = abs(0x49 - reg);
3376 pr_err("trim delta is %d\n", chip->amux_2_trim_delta);
3377
3378restore_sbi_config:
3379 rc = pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, sbi_config);
3380 if (rc) {
3381 pr_err("error = %d writing sbi config reg\n", rc);
3382 return rc;
3383 }
3384
3385 return 0;
3386}
3387
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003388static int __devinit pm8921_bms_probe(struct platform_device *pdev)
3389{
3390 int rc = 0;
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07003391 int vbatt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003392 struct pm8921_bms_chip *chip;
3393 const struct pm8921_bms_platform_data *pdata
3394 = pdev->dev.platform_data;
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07003395
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003396 if (!pdata) {
3397 pr_err("missing platform data\n");
3398 return -EINVAL;
3399 }
3400
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07003401 chip = kzalloc(sizeof(struct pm8921_bms_chip), GFP_KERNEL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003402 if (!chip) {
3403 pr_err("Cannot allocate pm_bms_chip\n");
3404 return -ENOMEM;
3405 }
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07003406
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08003407 mutex_init(&chip->bms_output_lock);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07003408 mutex_init(&chip->last_ocv_uv_mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003409 chip->dev = &pdev->dev;
Xiaozhe Shid69c91e2012-11-06 10:00:38 -08003410 chip->r_sense_uohm = pdata->r_sense_uohm;
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07003411 chip->v_cutoff = pdata->v_cutoff;
Abhijeet Dharmapurikara93ede82011-11-17 12:20:03 -08003412 chip->max_voltage_uv = pdata->max_voltage_uv;
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07003413 chip->chg_term_ua = pdata->chg_term_ua;
David Keitel35e11872012-02-17 17:40:42 -08003414 chip->batt_type = pdata->battery_type;
Abhijeet Dharmapurikarbaffba42012-03-22 14:41:10 -07003415 chip->rconn_mohm = pdata->rconn_mohm;
Abhijeet Dharmapurikardba91f42011-12-21 16:56:13 -08003416 chip->start_percent = -EINVAL;
3417 chip->end_percent = -EINVAL;
Xiaozhe Shid28e4972012-12-04 15:13:32 -08003418 chip->last_cc_uah = INT_MIN;
Abhijeet Dharmapurikar61275b92012-12-27 17:20:29 -08003419 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
3420 chip->prev_last_good_ocv_raw = OCV_RAW_UNINITIALIZED;
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07003421 chip->shutdown_soc_valid_limit = pdata->shutdown_soc_valid_limit;
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -07003422 chip->adjust_soc_low_threshold = pdata->adjust_soc_low_threshold;
Abhijeet Dharmapurikar683472d2012-10-30 16:21:58 -07003423
3424 chip->normal_voltage_calc_ms = pdata->normal_voltage_calc_ms;
3425 chip->low_voltage_calc_ms = pdata->low_voltage_calc_ms;
3426
3427 chip->soc_calc_period = pdata->normal_voltage_calc_ms;
3428
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -07003429 if (chip->adjust_soc_low_threshold >= 45)
3430 chip->adjust_soc_low_threshold = 45;
3431
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07003432 chip->prev_pc_unusable = -EINVAL;
Abhijeet Dharmapurikard3c435e2012-08-16 19:23:18 -07003433 chip->soc_at_cv = -EINVAL;
Abhijeet Dharmapurikareca7b3e2012-12-26 21:30:29 -08003434 chip->imax_ua = -EINVAL;
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07003435
Abhijeet Dharmapurikar1f4cc512012-07-21 00:10:39 -07003436 chip->ignore_shutdown_soc = pdata->ignore_shutdown_soc;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07003437 rc = set_battery_data(chip);
3438 if (rc) {
3439 pr_err("%s bad battery data %d\n", __func__, rc);
Abhijeet Dharmapurikar4a4456e2011-09-27 15:10:38 -07003440 goto free_chip;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07003441 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003442
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08003443 if (chip->pc_temp_ocv_lut == NULL) {
3444 pr_err("temp ocv lut table is NULL\n");
3445 rc = -EINVAL;
3446 goto free_chip;
3447 }
3448
3449 /* set defaults in the battery data */
3450 if (chip->default_rbatt_mohm <= 0)
3451 chip->default_rbatt_mohm = DEFAULT_RBATT_MOHMS;
3452
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07003453 chip->batt_temp_channel = pdata->bms_cdata.batt_temp_channel;
3454 chip->vbat_channel = pdata->bms_cdata.vbat_channel;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07003455 chip->ref625mv_channel = pdata->bms_cdata.ref625mv_channel;
3456 chip->ref1p25v_channel = pdata->bms_cdata.ref1p25v_channel;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07003457 chip->batt_id_channel = pdata->bms_cdata.batt_id_channel;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -07003458 chip->revision = pm8xxx_get_revision(chip->dev->parent);
Abhijeet Dharmapurikare88f2462012-04-25 18:22:38 -07003459 chip->enable_fcc_learning = pdata->enable_fcc_learning;
Abhijeet Dharmapurikar9c0e6042012-07-12 14:55:13 -07003460
Abhijeet Dharmapurikar1939ede2013-01-10 15:59:33 -08003461 chip->disable_flat_portion_ocv = pdata->disable_flat_portion_ocv;
3462 chip->ocv_dis_high_soc = pdata->ocv_dis_high_soc;
3463 chip->ocv_dis_low_soc = pdata->ocv_dis_low_soc;
3464
Abhijeet Dharmapurikar48707612013-03-26 13:14:03 -07003465 chip->high_ocv_correction_limit_uv
3466 = pdata->high_ocv_correction_limit_uv;
3467 chip->low_ocv_correction_limit_uv = pdata->low_ocv_correction_limit_uv;
3468 chip->hold_soc_est = pdata->hold_soc_est;
3469
Anirudh Ghayal6fe4ffa2013-02-01 11:44:33 +05303470 chip->alarm_low_mv = pdata->alarm_low_mv;
3471 chip->alarm_high_mv = pdata->alarm_high_mv;
Anirudh Ghayal0c7c2bc2013-03-22 12:57:50 +05303472 chip->low_voltage_detect = pdata->low_voltage_detect;
3473 chip->vbatt_cutoff_retries = pdata->vbatt_cutoff_retries;
Anirudh Ghayal6fe4ffa2013-02-01 11:44:33 +05303474
Abhijeet Dharmapurikar9c0e6042012-07-12 14:55:13 -07003475 mutex_init(&chip->calib_mutex);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07003476 INIT_WORK(&chip->calib_hkadc_work, calibrate_hkadc_work);
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07003477
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07003478 INIT_DELAYED_WORK(&chip->calculate_soc_delayed_work,
3479 calculate_soc_work);
3480
Abhijeet Dharmapurikarb2aeb7e2012-12-26 21:30:05 -08003481 wake_lock_init(&chip->soc_wake_lock,
3482 WAKE_LOCK_SUSPEND, "pm8921_soc_lock");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003483 rc = request_irqs(chip, pdev);
3484 if (rc) {
3485 pr_err("couldn't register interrupts rc = %d\n", rc);
Abhijeet Dharmapurikarb2aeb7e2012-12-26 21:30:05 -08003486 goto destroy_soc_wl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003487 }
3488
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07003489 wake_lock_init(&chip->low_voltage_wake_lock,
3490 WAKE_LOCK_SUSPEND, "pm8921_bms_low");
3491
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07003492 rc = pm8921_bms_hw_init(chip);
3493 if (rc) {
3494 pr_err("couldn't init hardware rc = %d\n", rc);
3495 goto free_irqs;
3496 }
3497
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07003498 read_shutdown_soc_and_iavg(chip);
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07003499
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003500 platform_set_drvdata(pdev, chip);
3501 the_chip = chip;
3502 create_debugfs_entries(chip);
3503
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -07003504 rc = read_ocv_trim(chip);
3505 if (rc) {
3506 pr_err("couldn't adjust ocv_trim rc= %d\n", rc);
3507 goto free_irqs;
3508 }
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07003509 check_initial_ocv(chip);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07003510
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07003511 /* enable the vbatt reading interrupts for scheduling hkadc calib */
3512 pm8921_bms_enable_irq(chip, PM8921_BMS_GOOD_OCV);
3513 pm8921_bms_enable_irq(chip, PM8921_BMS_OCV_FOR_R);
Abhijeet Dharmapurikar21ebadd2011-09-28 21:14:16 -07003514
Anirudh Ghayal6fe4ffa2013-02-01 11:44:33 +05303515 rc = pm8921_bms_configure_batt_alarm(chip);
3516 if (rc) {
3517 pr_err("Couldn't configure battery alarm! rc=%d\n", rc);
3518 goto free_irqs;
3519 }
3520
3521 rc = pm8921_bms_enable_batt_alarm(chip);
3522 if (rc) {
3523 pr_err("Couldn't enable battery alarm! rc=%d\n", rc);
3524 goto free_irqs;
3525 }
3526
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07003527 calculate_soc_work(&(chip->calculate_soc_delayed_work.work));
3528
Anirudh Ghayal3a38a382012-12-05 10:42:17 +05303529 rc = get_battery_uvolts(chip, &vbatt);
3530 if (!rc)
3531 pr_info("OK battery_capacity_at_boot=%d volt = %d ocv = %d\n",
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07003532 pm8921_bms_get_percent_charge(),
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07003533 vbatt, chip->last_ocv_uv);
Anirudh Ghayal3a38a382012-12-05 10:42:17 +05303534 else
3535 pr_info("Unable to read battery voltage at boot\n");
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -07003536
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003537 return 0;
3538
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07003539free_irqs:
Abhijeet Dharmapurikarb2aeb7e2012-12-26 21:30:05 -08003540 wake_lock_destroy(&chip->low_voltage_wake_lock);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07003541 free_irqs(chip);
Abhijeet Dharmapurikarb2aeb7e2012-12-26 21:30:05 -08003542destroy_soc_wl:
3543 wake_lock_destroy(&chip->soc_wake_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003544free_chip:
3545 kfree(chip);
3546 return rc;
3547}
3548
3549static int __devexit pm8921_bms_remove(struct platform_device *pdev)
3550{
3551 struct pm8921_bms_chip *chip = platform_get_drvdata(pdev);
3552
3553 free_irqs(chip);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07003554 kfree(chip->adjusted_fcc_temp_lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003555 platform_set_drvdata(pdev, NULL);
3556 the_chip = NULL;
3557 kfree(chip);
3558 return 0;
3559}
3560
Abhijeet Dharmapurikar1ed94182013-03-10 06:21:41 -07003561static int pm8921_bms_suspend(struct device *dev)
3562{
3563 /*
3564 * set the last reported soc to invalid, so that
3565 * next time we resume we don't want to restrict
3566 * the decrease of soc by only 1%
3567 */
3568 last_soc = -EINVAL;
3569
3570 return 0;
3571}
3572
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07003573static int pm8921_bms_resume(struct device *dev)
3574{
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08003575 int rc;
3576 unsigned long time_since_last_recalc;
3577 unsigned long tm_now_sec;
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07003578
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08003579 rc = get_current_time(&tm_now_sec);
3580 if (rc) {
3581 pr_err("Could not read current time: %d\n", rc);
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07003582 return 0;
3583 }
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08003584 if (tm_now_sec > the_chip->last_recalc_time) {
3585 time_since_last_recalc = tm_now_sec -
3586 the_chip->last_recalc_time;
3587 pr_debug("Time since last recalc: %lu\n",
3588 time_since_last_recalc);
3589 if (time_since_last_recalc >= the_chip->soc_calc_period) {
3590 the_chip->last_recalc_time = tm_now_sec;
3591 recalculate_soc(the_chip);
3592 }
3593 }
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07003594
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07003595 return 0;
3596}
3597
3598static const struct dev_pm_ops pm8921_bms_pm_ops = {
3599 .resume = pm8921_bms_resume,
Abhijeet Dharmapurikar1ed94182013-03-10 06:21:41 -07003600 .suspend = pm8921_bms_suspend,
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07003601};
3602
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003603static struct platform_driver pm8921_bms_driver = {
3604 .probe = pm8921_bms_probe,
3605 .remove = __devexit_p(pm8921_bms_remove),
3606 .driver = {
3607 .name = PM8921_BMS_DEV_NAME,
3608 .owner = THIS_MODULE,
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07003609 .pm = &pm8921_bms_pm_ops,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003610 },
3611};
3612
3613static int __init pm8921_bms_init(void)
3614{
3615 return platform_driver_register(&pm8921_bms_driver);
3616}
3617
3618static void __exit pm8921_bms_exit(void)
3619{
3620 platform_driver_unregister(&pm8921_bms_driver);
3621}
3622
3623late_initcall(pm8921_bms_init);
3624module_exit(pm8921_bms_exit);
3625
3626MODULE_LICENSE("GPL v2");
3627MODULE_DESCRIPTION("PMIC8921 bms driver");
3628MODULE_VERSION("1.0");
3629MODULE_ALIAS("platform:" PM8921_BMS_DEV_NAME);