blob: 82d58efdbcab253e08bfc559a06dd8dcf9db9d38 [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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070062enum pmic_bms_interrupts {
63 PM8921_BMS_SBI_WRITE_OK,
64 PM8921_BMS_CC_THR,
65 PM8921_BMS_VSENSE_THR,
66 PM8921_BMS_VSENSE_FOR_R,
67 PM8921_BMS_OCV_FOR_R,
68 PM8921_BMS_GOOD_OCV,
69 PM8921_BMS_VSENSE_AVG,
70 PM_BMS_MAX_INTS,
71};
72
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080073struct pm8921_soc_params {
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -080074 uint16_t last_good_ocv_raw;
75 int cc;
76
77 int last_good_ocv_uv;
78};
79
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080080/**
81 * struct pm8921_bms_chip -
82 * @bms_output_lock: lock to prevent concurrent bms reads
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080083 *
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -070084 * @last_ocv_uv_mutex: mutex to protect simultaneous invocations of calculate
85 * state of charge, note that last_ocv_uv could be
86 * changed as soc is adjusted. This mutex protects
87 * simultaneous updates of last_ocv_uv as well. This mutex
88 * also protects changes to *_at_100 variables used in
89 * faking 100% SOC.
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080090 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091struct pm8921_bms_chip {
92 struct device *dev;
93 struct dentry *dent;
Xiaozhe Shid69c91e2012-11-06 10:00:38 -080094 int r_sense_uohm;
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -070095 unsigned int v_cutoff;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096 unsigned int fcc;
97 struct single_row_lut *fcc_temp_lut;
98 struct single_row_lut *fcc_sf_lut;
99 struct pc_temp_ocv_lut *pc_temp_ocv_lut;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800100 struct sf_lut *pc_sf_lut;
101 struct sf_lut *rbatt_sf_lut;
Abhijeet Dharmapurikarded189b2012-03-30 10:12:28 -0700102 int delta_rbatt_mohm;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700103 struct work_struct calib_hkadc_work;
Xiaozhe Shid035f6a2012-12-18 16:16:33 -0800104 unsigned long last_calib_time;
105 int last_calib_temp;
Abhijeet Dharmapurikar9c0e6042012-07-12 14:55:13 -0700106 struct mutex calib_mutex;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700107 unsigned int revision;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800108 unsigned int xoadc_v0625_usb_present;
109 unsigned int xoadc_v0625_usb_absent;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700110 unsigned int xoadc_v0625;
111 unsigned int xoadc_v125;
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700112 unsigned int batt_temp_channel;
113 unsigned int vbat_channel;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700114 unsigned int ref625mv_channel;
115 unsigned int ref1p25v_channel;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -0700116 unsigned int batt_id_channel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700117 unsigned int pmic_bms_irq[PM_BMS_MAX_INTS];
118 DECLARE_BITMAP(enabled_irqs, PM_BMS_MAX_INTS);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800119 struct mutex bms_output_lock;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700120 struct single_row_lut *adjusted_fcc_temp_lut;
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -0700121 unsigned int charging_began;
122 unsigned int start_percent;
123 unsigned int end_percent;
Anirudh Ghayal6fe4ffa2013-02-01 11:44:33 +0530124 unsigned int alarm_low_mv;
125 unsigned int alarm_high_mv;
126
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -0700127 int charge_time_us;
128 int catch_up_time_us;
David Keitel35e11872012-02-17 17:40:42 -0800129 enum battery_type batt_type;
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -0800130 uint16_t ocv_reading_at_100;
131 int cc_reading_at_100;
Abhijeet Dharmapurikara93ede82011-11-17 12:20:03 -0800132 int max_voltage_uv;
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800133
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -0700134 int chg_term_ua;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800135 int default_rbatt_mohm;
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700136 int amux_2_trim_delta;
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -0700137 uint16_t prev_last_good_ocv_raw;
Abhijeet Dharmapurikara82a00a2012-11-08 17:26:29 -0800138 int rconn_mohm;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -0700139 struct mutex last_ocv_uv_mutex;
140 int last_ocv_uv;
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -0800141 int last_ocv_temp_decidegc;
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -0700142 int pon_ocv_uv;
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -0700143 int last_cc_uah;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -0700144 unsigned long tm_sec;
Abhijeet Dharmapurikare88f2462012-04-25 18:22:38 -0700145 int enable_fcc_learning;
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -0700146 int shutdown_soc;
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -0700147 int shutdown_iavg_ua;
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -0700148 struct delayed_work calculate_soc_delayed_work;
149 struct timespec t_soc_queried;
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -0800150 unsigned long last_recalc_time;
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -0700151 int shutdown_soc_valid_limit;
Abhijeet Dharmapurikar1f4cc512012-07-21 00:10:39 -0700152 int ignore_shutdown_soc;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -0700153 int prev_iavg_ua;
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -0700154 int prev_uuc_iavg_ma;
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -0700155 int prev_pc_unusable;
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -0700156 int adjust_soc_low_threshold;
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -0700157
158 int ibat_at_cv_ua;
159 int soc_at_cv;
160 int prev_chg_soc;
Abhijeet Dharmapurikar0622a432012-08-28 16:34:18 -0700161 struct power_supply *batt_psy;
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -0700162 bool low_voltage_wake_lock_held;
163 struct wake_lock low_voltage_wake_lock;
Abhijeet Dharmapurikar683472d2012-10-30 16:21:58 -0700164 int soc_calc_period;
165 int normal_voltage_calc_ms;
166 int low_voltage_calc_ms;
Abhijeet Dharmapurikareca7b3e2012-12-26 21:30:29 -0800167 int imax_ua;
Abhijeet Dharmapurikarb2aeb7e2012-12-26 21:30:05 -0800168 struct wake_lock soc_wake_lock;
Abhijeet Dharmapurikar1939ede2013-01-10 15:59:33 -0800169 int disable_flat_portion_ocv;
170 int ocv_dis_high_soc;
171 int ocv_dis_low_soc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172};
173
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -0700174/*
175 * protects against simultaneous adjustment of ocv based on shutdown soc and
176 * invalidating the shutdown soc
177 */
178static DEFINE_MUTEX(soc_invalidation_mutex);
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -0700179static int shutdown_soc_invalid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180static struct pm8921_bms_chip *the_chip;
181
182#define DEFAULT_RBATT_MOHMS 128
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700183#define DEFAULT_OCV_MICROVOLTS 3900000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700184#define DEFAULT_CHARGE_CYCLES 0
185
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800186static int last_usb_cal_delta_uv = 1800;
187module_param(last_usb_cal_delta_uv, int, 0644);
188
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700189static int last_chargecycles = DEFAULT_CHARGE_CYCLES;
190static int last_charge_increase;
191module_param(last_chargecycles, int, 0644);
192module_param(last_charge_increase, int, 0644);
193
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -0700194static int calculated_soc = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700195static int last_soc = -EINVAL;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800196static int last_real_fcc_mah = -EINVAL;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700197static int last_real_fcc_batt_temp = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198
Anirudh Ghayal6fe4ffa2013-02-01 11:44:33 +0530199static int pm8921_battery_gauge_alarm_notify(struct notifier_block *nb,
200 unsigned long status, void *unused);
201
202static struct notifier_block alarm_notifier = {
203 .notifier_call = pm8921_battery_gauge_alarm_notify,
204};
205
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700206static int bms_ops_set(const char *val, const struct kernel_param *kp)
207{
208 if (*(int *)kp->arg == -EINVAL)
209 return param_set_int(val, kp);
210 else
211 return 0;
212}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700214static struct kernel_param_ops bms_param_ops = {
215 .set = bms_ops_set,
216 .get = param_get_int,
217};
218
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700219module_param_cb(last_soc, &bms_param_ops, &last_soc, 0644);
220
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -0800221/*
Abhijeet Dharmapurikar986bd8a2012-01-05 15:15:15 -0800222 * bms_fake_battery is set in setups where a battery emulator is used instead
223 * of a real battery. This makes the bms driver report a different/fake value
224 * regardless of the calculated state of charge.
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -0800225 */
Abhijeet Dharmapurikar986bd8a2012-01-05 15:15:15 -0800226static int bms_fake_battery = -EINVAL;
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -0800227module_param(bms_fake_battery, int, 0644);
228
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800229/* bms_start_XXX and bms_end_XXX are read only */
230static int bms_start_percent;
231static int bms_start_ocv_uv;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800232static int bms_start_cc_uah;
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800233static int bms_end_percent;
234static int bms_end_ocv_uv;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800235static int bms_end_cc_uah;
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800236
237static int bms_ro_ops_set(const char *val, const struct kernel_param *kp)
238{
239 return -EINVAL;
240}
241
242static struct kernel_param_ops bms_ro_param_ops = {
243 .set = bms_ro_ops_set,
244 .get = param_get_int,
245};
246module_param_cb(bms_start_percent, &bms_ro_param_ops, &bms_start_percent, 0644);
247module_param_cb(bms_start_ocv_uv, &bms_ro_param_ops, &bms_start_ocv_uv, 0644);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800248module_param_cb(bms_start_cc_uah, &bms_ro_param_ops, &bms_start_cc_uah, 0644);
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800249
250module_param_cb(bms_end_percent, &bms_ro_param_ops, &bms_end_percent, 0644);
251module_param_cb(bms_end_ocv_uv, &bms_ro_param_ops, &bms_end_ocv_uv, 0644);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800252module_param_cb(bms_end_cc_uah, &bms_ro_param_ops, &bms_end_cc_uah, 0644);
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800253
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700254static void readjust_fcc_table(void)
255{
256 struct single_row_lut *temp, *old;
257 int i, fcc, ratio;
258
259 if (!the_chip->fcc_temp_lut) {
260 pr_err("The static fcc lut table is NULL\n");
261 return;
262 }
263
264 temp = kzalloc(sizeof(struct single_row_lut), GFP_KERNEL);
265 if (!temp) {
266 pr_err("Cannot allocate memory for adjusted fcc table\n");
267 return;
268 }
269
Xiaozhe Shi5ee95192012-09-12 15:16:34 -0700270 fcc = interpolate_fcc(the_chip->fcc_temp_lut, last_real_fcc_batt_temp);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700271
272 temp->cols = the_chip->fcc_temp_lut->cols;
273 for (i = 0; i < the_chip->fcc_temp_lut->cols; i++) {
274 temp->x[i] = the_chip->fcc_temp_lut->x[i];
275 ratio = div_u64(the_chip->fcc_temp_lut->y[i] * 1000, fcc);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800276 temp->y[i] = (ratio * last_real_fcc_mah);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700277 temp->y[i] /= 1000;
278 pr_debug("temp=%d, staticfcc=%d, adjfcc=%d, ratio=%d\n",
279 temp->x[i], the_chip->fcc_temp_lut->y[i],
280 temp->y[i], ratio);
281 }
282
283 old = the_chip->adjusted_fcc_temp_lut;
284 the_chip->adjusted_fcc_temp_lut = temp;
285 kfree(old);
286}
287
288static int bms_last_real_fcc_set(const char *val,
289 const struct kernel_param *kp)
290{
291 int rc = 0;
292
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800293 if (last_real_fcc_mah == -EINVAL)
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700294 rc = param_set_int(val, kp);
295 if (rc) {
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800296 pr_err("Failed to set last_real_fcc_mah rc=%d\n", rc);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700297 return rc;
298 }
299 if (last_real_fcc_batt_temp != -EINVAL)
300 readjust_fcc_table();
301 return rc;
302}
303static struct kernel_param_ops bms_last_real_fcc_param_ops = {
304 .set = bms_last_real_fcc_set,
305 .get = param_get_int,
306};
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800307module_param_cb(last_real_fcc_mah, &bms_last_real_fcc_param_ops,
308 &last_real_fcc_mah, 0644);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700309
310static int bms_last_real_fcc_batt_temp_set(const char *val,
311 const struct kernel_param *kp)
312{
313 int rc = 0;
314
315 if (last_real_fcc_batt_temp == -EINVAL)
316 rc = param_set_int(val, kp);
317 if (rc) {
318 pr_err("Failed to set last_real_fcc_batt_temp rc=%d\n", rc);
319 return rc;
320 }
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800321 if (last_real_fcc_mah != -EINVAL)
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700322 readjust_fcc_table();
323 return rc;
324}
325
326static struct kernel_param_ops bms_last_real_fcc_batt_temp_param_ops = {
327 .set = bms_last_real_fcc_batt_temp_set,
328 .get = param_get_int,
329};
330module_param_cb(last_real_fcc_batt_temp, &bms_last_real_fcc_batt_temp_param_ops,
331 &last_real_fcc_batt_temp, 0644);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700332
333static int pm_bms_get_rt_status(struct pm8921_bms_chip *chip, int irq_id)
334{
335 return pm8xxx_read_irq_stat(chip->dev->parent,
336 chip->pmic_bms_irq[irq_id]);
337}
338
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700339static void pm8921_bms_enable_irq(struct pm8921_bms_chip *chip, int interrupt)
340{
341 if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) {
342 dev_dbg(chip->dev, "%s %d\n", __func__,
343 chip->pmic_bms_irq[interrupt]);
344 enable_irq(chip->pmic_bms_irq[interrupt]);
345 }
346}
347
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348static void pm8921_bms_disable_irq(struct pm8921_bms_chip *chip, int interrupt)
349{
350 if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700351 pr_debug("%d\n", chip->pmic_bms_irq[interrupt]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700352 disable_irq_nosync(chip->pmic_bms_irq[interrupt]);
353 }
354}
355
356static int pm_bms_masked_write(struct pm8921_bms_chip *chip, u16 addr,
357 u8 mask, u8 val)
358{
359 int rc;
360 u8 reg;
361
362 rc = pm8xxx_readb(chip->dev->parent, addr, &reg);
363 if (rc) {
364 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
365 return rc;
366 }
367 reg &= ~mask;
368 reg |= val & mask;
369 rc = pm8xxx_writeb(chip->dev->parent, addr, reg);
370 if (rc) {
371 pr_err("write failed addr = %03X, rc = %d\n", addr, rc);
372 return rc;
373 }
374 return 0;
375}
376
Abhijeet Dharmapurikar9e948982012-08-28 15:21:23 -0700377static int usb_chg_plugged_in(struct pm8921_bms_chip *chip)
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800378{
Abhijeet Dharmapurikar3da61762012-07-12 15:56:58 -0700379 int val = pm8921_is_usb_chg_plugged_in();
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800380
Abhijeet Dharmapurikar9e948982012-08-28 15:21:23 -0700381 /* if the charger driver was not initialized, use the restart reason */
382 if (val == -EINVAL) {
383 if (pm8xxx_restart_reason(chip->dev->parent)
384 == PM8XXX_RESTART_CHG)
385 val = 1;
386 else
387 val = 0;
388 }
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800389
Abhijeet Dharmapurikar3da61762012-07-12 15:56:58 -0700390 return val;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800391}
392
Anirudh Ghayal6fe4ffa2013-02-01 11:44:33 +0530393static int pm8921_bms_enable_batt_alarm(struct pm8921_bms_chip *chip)
394{
395 int rc = 0;
396
397 rc = pm8xxx_batt_alarm_enable(PM8XXX_BATT_ALARM_LOWER_COMPARATOR);
398 if (!rc)
399 rc = pm8xxx_batt_alarm_disable(
400 PM8XXX_BATT_ALARM_UPPER_COMPARATOR);
401 if (rc) {
402 pr_err("unable to set batt alarm state rc=%d\n", rc);
403 return rc;
404 }
405
406 return rc;
407}
408
409static int pm8921_bms_configure_batt_alarm(struct pm8921_bms_chip *chip)
410{
411 int rc = 0;
412
413 rc = pm8xxx_batt_alarm_disable(PM8XXX_BATT_ALARM_UPPER_COMPARATOR);
414 if (!rc)
415 rc = pm8xxx_batt_alarm_disable(
416 PM8XXX_BATT_ALARM_LOWER_COMPARATOR);
417 if (rc) {
418 pr_err("unable to set batt alarm state rc=%d\n", rc);
419 return rc;
420 }
421
422 /*
423 * The batt-alarm driver requires sane values for both min / max,
424 * regardless of whether they're both activated.
425 */
426 rc = pm8xxx_batt_alarm_threshold_set(
427 PM8XXX_BATT_ALARM_LOWER_COMPARATOR,
428 chip->alarm_low_mv);
429 if (!rc)
430 rc = pm8xxx_batt_alarm_threshold_set(
431 PM8XXX_BATT_ALARM_UPPER_COMPARATOR,
432 chip->alarm_high_mv);
433 if (rc) {
434 pr_err("unable to set batt alarm threshold rc=%d\n", rc);
435 return rc;
436 }
437
438 rc = pm8xxx_batt_alarm_hold_time_set(
439 PM8XXX_BATT_ALARM_HOLD_TIME_16_MS);
440 if (rc) {
441 pr_err("unable to set batt alarm hold time rc=%d\n", rc);
442 return rc;
443 }
444
445 /* PWM enabled at 2Hz */
446 rc = pm8xxx_batt_alarm_pwm_rate_set(1, 7, 4);
447 if (rc) {
448 pr_err("unable to set batt alarm pwm rate rc=%d\n", rc);
449 return rc;
450 }
451
452 rc = pm8xxx_batt_alarm_register_notifier(&alarm_notifier);
453 if (rc) {
454 pr_err("unable to register alarm notifier rc=%d\n", rc);
455 return rc;
456 }
457
458 return rc;
459}
460
461static int pm8921_battery_gauge_alarm_notify(struct notifier_block *nb,
462 unsigned long status, void *unused)
463{
464 int rc;
465
466 if (!the_chip) {
467 pr_err("not initialized\n");
468 return -EINVAL;
469 }
470
471 switch (status) {
472 case 0:
473 pr_debug("spurious interrupt\n");
474 break;
475 case 1:
476 pr_debug("Low voltage alarm triggered\n");
477 /*
478 * hold the low voltage wakelock until the soc
479 * work finds it appropriate to release it.
480 */
481 wake_lock(&the_chip->low_voltage_wake_lock);
482 the_chip->low_voltage_wake_lock_held = 1;
483
484 rc = pm8xxx_batt_alarm_disable(
485 PM8XXX_BATT_ALARM_LOWER_COMPARATOR);
486 if (!rc)
487 rc = pm8xxx_batt_alarm_enable(
488 PM8XXX_BATT_ALARM_UPPER_COMPARATOR);
489 if (rc)
490 pr_err("unable to set alarm state rc=%d\n", rc);
491 break;
492 case 2:
493 rc = pm8xxx_batt_alarm_disable(
494 PM8XXX_BATT_ALARM_UPPER_COMPARATOR);
495 if (!rc)
496 rc = pm8xxx_batt_alarm_enable(
497 PM8XXX_BATT_ALARM_LOWER_COMPARATOR);
498 if (rc)
499 pr_err("unable to set alarm state rc=%d\n", rc);
500
501 break;
502 default:
503 pr_err("error received\n");
504 break;
505 }
506
507 return 0;
508};
509
510
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700511#define HOLD_OREG_DATA BIT(1)
512static int pm_bms_lock_output_data(struct pm8921_bms_chip *chip)
513{
514 int rc;
515
516 rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA,
517 HOLD_OREG_DATA);
518 if (rc) {
519 pr_err("couldnt lock bms output rc = %d\n", rc);
520 return rc;
521 }
522 return 0;
523}
524
525static int pm_bms_unlock_output_data(struct pm8921_bms_chip *chip)
526{
527 int rc;
528
529 rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA, 0);
530 if (rc) {
531 pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc);
532 return rc;
533 }
534 return 0;
535}
536
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700537#define SELECT_OUTPUT_DATA 0x1C
538#define SELECT_OUTPUT_TYPE_SHIFT 2
539#define OCV_FOR_RBATT 0x0
540#define VSENSE_FOR_RBATT 0x1
541#define VBATT_FOR_RBATT 0x2
542#define CC_MSB 0x3
543#define CC_LSB 0x4
544#define LAST_GOOD_OCV_VALUE 0x5
545#define VSENSE_AVG 0x6
546#define VBATT_AVG 0x7
547
548static int pm_bms_read_output_data(struct pm8921_bms_chip *chip, int type,
549 int16_t *result)
550{
551 int rc;
552 u8 reg;
553
554 if (!result) {
555 pr_err("result pointer null\n");
556 return -EINVAL;
557 }
558 *result = 0;
559 if (type < OCV_FOR_RBATT || type > VBATT_AVG) {
560 pr_err("invalid type %d asked to read\n", type);
561 return -EINVAL;
562 }
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700563
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564 rc = pm_bms_masked_write(chip, BMS_CONTROL, SELECT_OUTPUT_DATA,
565 type << SELECT_OUTPUT_TYPE_SHIFT);
566 if (rc) {
567 pr_err("fail to select %d type in BMS_CONTROL rc = %d\n",
568 type, rc);
569 return rc;
570 }
571
572 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT0, &reg);
573 if (rc) {
574 pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n",
575 type, rc);
576 return rc;
577 }
578 *result = reg;
579 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT1, &reg);
580 if (rc) {
581 pr_err("fail to read BMS_OUTPUT1 for type %d rc = %d\n",
582 type, rc);
583 return rc;
584 }
585 *result |= reg << 8;
586 pr_debug("type %d result %x", type, *result);
587 return 0;
588}
589
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700590#define V_PER_BIT_MUL_FACTOR 97656
591#define V_PER_BIT_DIV_FACTOR 1000
592#define XOADC_INTRINSIC_OFFSET 0x6000
593static int xoadc_reading_to_microvolt(unsigned int a)
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700594{
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700595 if (a <= XOADC_INTRINSIC_OFFSET)
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700596 return 0;
597
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700598 return (a - XOADC_INTRINSIC_OFFSET)
599 * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR;
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700600}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700602#define XOADC_CALIB_UV 625000
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700603#define VBATT_MUL_FACTOR 3
604static int adjust_xo_vbatt_reading(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800605 int usb_chg, unsigned int uv)
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700606{
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800607 s64 numerator, denominator;
608 int local_delta;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700609
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700610 if (uv == 0)
611 return 0;
612
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800613 /* dont adjust if not calibrated */
614 if (chip->xoadc_v0625 == 0 || chip->xoadc_v125 == 0) {
615 pr_debug("No cal yet return %d\n", VBATT_MUL_FACTOR * uv);
616 return VBATT_MUL_FACTOR * uv;
617 }
618
619 if (usb_chg)
620 local_delta = last_usb_cal_delta_uv;
621 else
622 local_delta = 0;
623
624 pr_debug("using delta = %d\n", local_delta);
625 numerator = ((s64)uv - chip->xoadc_v0625 - local_delta)
626 * XOADC_CALIB_UV;
627 denominator = (s64)chip->xoadc_v125 - chip->xoadc_v0625 - local_delta;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700628 if (denominator == 0)
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700629 return uv * VBATT_MUL_FACTOR;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800630 return (XOADC_CALIB_UV + local_delta + div_s64(numerator, denominator))
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700631 * VBATT_MUL_FACTOR;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700632}
633
Abhijeet Dharmapurikar5adb2532012-06-21 16:15:43 -0700634#define CC_RESOLUTION_N 868056
635#define CC_RESOLUTION_D 10000
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700636
637static s64 cc_to_microvolt(struct pm8921_bms_chip *chip, s64 cc)
638{
Abhijeet Dharmapurikar5adb2532012-06-21 16:15:43 -0700639 return div_s64(cc * CC_RESOLUTION_N, CC_RESOLUTION_D);
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700640}
641
Abhijeet Dharmapurikar2feabe32012-07-23 21:42:17 -0700642#define CC_READING_TICKS 56
643#define SLEEP_CLK_HZ 32764
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700644#define SECONDS_PER_HOUR 3600
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800645/**
Xiaozhe Shi35b82e42013-02-12 23:17:54 -0800646 * ccmicrovolt_to_pvh -
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800647 * @cc_uv: coulumb counter converted to uV
648 *
Xiaozhe Shi35b82e42013-02-12 23:17:54 -0800649 * RETURNS: coulumb counter based charge in pVh
650 * (pico Volt Hour)
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800651 */
Xiaozhe Shi35b82e42013-02-12 23:17:54 -0800652static s64 ccmicrovolt_to_pvh(s64 cc_uv)
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700653{
Xiaozhe Shi35b82e42013-02-12 23:17:54 -0800654 return div_s64(cc_uv * CC_READING_TICKS * 1000000L,
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700655 SLEEP_CLK_HZ * SECONDS_PER_HOUR);
656}
657
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700658/* returns the signed value read from the hardware */
659static int read_cc(struct pm8921_bms_chip *chip, int *result)
660{
661 int rc;
662 uint16_t msw, lsw;
663
Anirudh Ghayal3a38a382012-12-05 10:42:17 +0530664 *result = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665 rc = pm_bms_read_output_data(chip, CC_LSB, &lsw);
666 if (rc) {
667 pr_err("fail to read CC_LSB rc = %d\n", rc);
668 return rc;
669 }
670 rc = pm_bms_read_output_data(chip, CC_MSB, &msw);
671 if (rc) {
672 pr_err("fail to read CC_MSB rc = %d\n", rc);
673 return rc;
674 }
675 *result = msw << 16 | lsw;
676 pr_debug("msw = %04x lsw = %04x cc = %d\n", msw, lsw, *result);
677 return 0;
678}
679
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700680static int adjust_xo_vbatt_reading_for_mbg(struct pm8921_bms_chip *chip,
681 int result)
682{
683 int64_t numerator;
684 int64_t denominator;
685
686 if (chip->amux_2_trim_delta == 0)
687 return result;
688
689 numerator = (s64)result * 1000000;
690 denominator = (1000000 + (410 * (s64)chip->amux_2_trim_delta));
691 return div_s64(numerator, denominator);
692}
693
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800694static int convert_vbatt_raw_to_uv(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800695 int usb_chg,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800696 uint16_t reading, int *result)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700697{
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700698 *result = xoadc_reading_to_microvolt(reading);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800699 pr_debug("raw = %04x vbatt = %u\n", reading, *result);
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800700 *result = adjust_xo_vbatt_reading(chip, usb_chg, *result);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800701 pr_debug("after adj vbatt = %u\n", *result);
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700702 *result = adjust_xo_vbatt_reading_for_mbg(chip, *result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703 return 0;
704}
705
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800706static int convert_vsense_to_uv(struct pm8921_bms_chip *chip,
707 int16_t reading, int *result)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708{
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800709 *result = pm8xxx_ccadc_reading_to_microvolt(chip->revision, reading);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800710 pr_debug("raw = %04x vsense = %d\n", reading, *result);
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800711 *result = pm8xxx_cc_adjust_for_gain(*result);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800712 pr_debug("after adj vsense = %d\n", *result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700713 return 0;
714}
715
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700716static int read_vsense_avg(struct pm8921_bms_chip *chip, int *result)
717{
718 int rc;
719 int16_t reading;
720
721 rc = pm_bms_read_output_data(chip, VSENSE_AVG, &reading);
722 if (rc) {
723 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
724 return rc;
725 }
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800726
727 convert_vsense_to_uv(chip, reading, result);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700728 return 0;
729}
730
Abhijeet Dharmapurikara81e3a22013-01-14 18:19:18 -0800731static int get_batt_temp(struct pm8921_bms_chip *chip, int *batt_temp)
732{
733 int rc;
734 struct pm8xxx_adc_chan_result result;
735
736 rc = pm8xxx_adc_read(chip->batt_temp_channel, &result);
737 if (rc) {
738 pr_err("error reading batt_temp_channel = %d, rc = %d\n",
739 chip->batt_temp_channel, rc);
740 return rc;
741 }
742 *batt_temp = result.physical;
743 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
744 result.measurement);
745 return 0;
746}
747
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800748#define BMS_MODE_BIT BIT(6)
749#define EN_VBAT_BIT BIT(5)
750#define OVERRIDE_MODE_DELAY_MS 20
Abhijeet Dharmapurikarb2680132012-05-15 16:32:51 -0700751int override_mode_simultaneous_battery_voltage_and_current(int *ibat_ua,
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800752 int *vbat_uv)
753{
754 int16_t vsense_raw;
755 int16_t vbat_raw;
756 int vsense_uv;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800757 int usb_chg;
Abhijeet Dharmapurikara81e3a22013-01-14 18:19:18 -0800758 int batt_temp;
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800759
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800760 mutex_lock(&the_chip->bms_output_lock);
761
762 pm8xxx_writeb(the_chip->dev->parent, BMS_S1_DELAY, 0x00);
763 pm_bms_masked_write(the_chip, BMS_CONTROL,
764 BMS_MODE_BIT | EN_VBAT_BIT, BMS_MODE_BIT | EN_VBAT_BIT);
765
766 msleep(OVERRIDE_MODE_DELAY_MS);
767
768 pm_bms_lock_output_data(the_chip);
769 pm_bms_read_output_data(the_chip, VSENSE_AVG, &vsense_raw);
770 pm_bms_read_output_data(the_chip, VBATT_AVG, &vbat_raw);
771 pm_bms_unlock_output_data(the_chip);
772 pm_bms_masked_write(the_chip, BMS_CONTROL,
Abhijeet Dharmapurikara81e3a22013-01-14 18:19:18 -0800773 BMS_MODE_BIT | EN_VBAT_BIT, 0);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800774
775 pm8xxx_writeb(the_chip->dev->parent, BMS_S1_DELAY, 0x0B);
776
777 mutex_unlock(&the_chip->bms_output_lock);
778
Abhijeet Dharmapurikara81e3a22013-01-14 18:19:18 -0800779 get_batt_temp(the_chip, &batt_temp);
Abhijeet Dharmapurikar9e948982012-08-28 15:21:23 -0700780 usb_chg = usb_chg_plugged_in(the_chip);
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800781
782 convert_vbatt_raw_to_uv(the_chip, usb_chg, vbat_raw, vbat_uv);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800783 convert_vsense_to_uv(the_chip, vsense_raw, &vsense_uv);
Xiaozhe Shid69c91e2012-11-06 10:00:38 -0800784 *ibat_ua = div_s64((s64)vsense_uv * 1000000LL, the_chip->r_sense_uohm);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800785
786 pr_debug("vsense_raw = 0x%x vbat_raw = 0x%x"
787 " ibat_ua = %d vbat_uv = %d\n",
788 (uint16_t)vsense_raw, (uint16_t)vbat_raw,
789 *ibat_ua, *vbat_uv);
790 return 0;
791}
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800792
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -0800793#define MBG_TRANSIENT_ERROR_UV 15000
794static void adjust_pon_ocv(struct pm8921_bms_chip *chip, int *uv)
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700795{
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -0800796 /*
797 * In 8921 parts the PON ocv is taken when the MBG is not settled.
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700798 * decrease the pon ocv by 15mV raw value to account for it
799 * Since a 1/3rd of vbatt is supplied to the adc the raw value
800 * needs to be adjusted by 5mV worth bits
801 */
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -0800802 if (*uv >= MBG_TRANSIENT_ERROR_UV)
803 *uv -= MBG_TRANSIENT_ERROR_UV;
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700804}
805
Abhijeet Dharmapurikarecdb5db2012-10-09 16:57:18 -0700806#define SEL_ALT_OREG_BIT BIT(2)
807static int ocv_ir_compensation(struct pm8921_bms_chip *chip, int ocv)
808{
809 int compensated_ocv;
810 int ibatt_ua;
811 int rbatt_mohm = chip->default_rbatt_mohm + chip->rconn_mohm;
812
813 pm_bms_masked_write(chip, BMS_TEST1,
814 SEL_ALT_OREG_BIT, SEL_ALT_OREG_BIT);
815
816 /* since the SEL_ALT_OREG_BIT is set this will give us VSENSE_OCV */
817 pm8921_bms_get_battery_current(&ibatt_ua);
818 compensated_ocv = ocv + div_s64((s64)ibatt_ua * rbatt_mohm, 1000);
819 pr_debug("comp ocv = %d, ocv = %d, ibatt_ua = %d, rbatt_mohm = %d\n",
820 compensated_ocv, ocv, ibatt_ua, rbatt_mohm);
821
822 pm_bms_masked_write(chip, BMS_TEST1, SEL_ALT_OREG_BIT, 0);
823 return compensated_ocv;
824}
825
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -0800826#define RESET_CC_BIT BIT(3)
827static int reset_cc(struct pm8921_bms_chip *chip)
828{
829 int rc;
830
831 rc = pm_bms_masked_write(chip, BMS_TEST1, RESET_CC_BIT, RESET_CC_BIT);
832 if (rc < 0) {
833 pr_err("err setting cc reset rc = %d\n", rc);
834 return rc;
835 }
836
837 /* sleep 100uS for the coulomb counter to reset */
838 udelay(100);
839
840 rc = pm_bms_masked_write(chip, BMS_TEST1, RESET_CC_BIT, 0);
841 if (rc < 0)
842 pr_err("err clearing cc reset rc = %d\n", rc);
843 return rc;
844}
845
846static int estimate_ocv(struct pm8921_bms_chip *chip)
847{
848 int ibat_ua, vbat_uv, ocv_est_uv;
849 int rc;
850
851 int rbatt_mohm = chip->default_rbatt_mohm + chip->rconn_mohm;
852
853 rc = pm8921_bms_get_simultaneous_battery_voltage_and_current(
854 &ibat_ua,
855 &vbat_uv);
856 if (rc) {
857 pr_err("simultaneous failed rc = %d\n", rc);
858 return rc;
859 }
860
861 ocv_est_uv = vbat_uv + (ibat_ua * rbatt_mohm) / 1000;
862 pr_debug("estimated pon ocv = %d\n", ocv_est_uv);
863 return ocv_est_uv;
864}
865
866static bool is_warm_restart(struct pm8921_bms_chip *chip)
867{
868 u8 reg;
869 int rc;
870
871 rc = pm8xxx_readb(chip->dev->parent, PON_CNTRL_6, &reg);
872 if (rc) {
873 pr_err("err reading pon 6 rc = %d\n", rc);
874 return false;
875 }
876 return reg & WD_BIT;
877}
Abhijeet Dharmapurikar7df608c2012-12-26 21:28:43 -0800878
879#define IBAT_TOL_MASK 0x0F
880#define OCV_TOL_MASK 0xF0
881#define IBAT_TOL_DEFAULT 0x03
882#define IBAT_TOL_NOCHG 0x0F
883#define OCV_TOL_DEFAULT 0x20
884#define OCV_TOL_NO_OCV 0x00
885static int pm8921_bms_stop_ocv_updates(void)
886{
887 if (!the_chip) {
888 pr_err("BMS driver has not been initialized yet!\n");
889 return -EINVAL;
890 }
891 pr_debug("stopping ocv updates\n");
892 return pm_bms_masked_write(the_chip, BMS_TOLERANCES,
893 OCV_TOL_MASK, OCV_TOL_NO_OCV);
894}
895
896static int pm8921_bms_start_ocv_updates(void)
897{
898 if (!the_chip) {
899 pr_err("BMS driver has not been initialized yet!\n");
900 return -EINVAL;
901 }
Abhijeet Dharmapurikar1939ede2013-01-10 15:59:33 -0800902 pr_debug("starting ocv updates\n");
Abhijeet Dharmapurikar7df608c2012-12-26 21:28:43 -0800903 return pm_bms_masked_write(the_chip, BMS_TOLERANCES,
904 OCV_TOL_MASK, OCV_TOL_DEFAULT);
905}
906
907static int reset_bms_for_test(void)
908{
909 int ibat_ua, vbat_uv, rc;
910 int ocv_est_uv;
911
912 if (!the_chip) {
913 pr_err("BMS driver has not been initialized yet!\n");
914 return -EINVAL;
915 }
916
917 rc = pm8921_bms_get_simultaneous_battery_voltage_and_current(
918 &ibat_ua,
919 &vbat_uv);
920
921 ocv_est_uv = vbat_uv + (ibat_ua * the_chip->rconn_mohm) / 1000;
922 pr_debug("forcing ocv to be %d due to bms reset mode\n", ocv_est_uv);
923 the_chip->last_ocv_uv = ocv_est_uv;
924 last_soc = -EINVAL;
925 reset_cc(the_chip);
926 the_chip->last_cc_uah = 0;
927 pm8921_bms_stop_ocv_updates();
928
929 pr_debug("bms reset to ocv = %duv vbat_ua = %d ibat_ua = %d\n",
930 the_chip->last_ocv_uv, vbat_uv, ibat_ua);
931
932 return rc;
933}
934
935static int bms_reset_set(const char *val, const struct kernel_param *kp)
936{
937 int rc;
938
939 rc = param_set_bool(val, kp);
940 if (rc) {
941 pr_err("Unable to set bms_reset: %d\n", rc);
942 return rc;
943 }
944
945 if (*val == 'Y') {
946 rc = reset_bms_for_test();
947 if (rc) {
948 pr_err("Unable to modify bms_reset: %d\n", rc);
949 return rc;
950 }
951 }
952 return 0;
953}
954
955static struct kernel_param_ops bms_reset_ops = {
956 .set = bms_reset_set,
957 .get = param_get_bool,
958};
959
960static bool bms_reset;
961module_param_cb(bms_reset, &bms_reset_ops, &bms_reset, 0644);
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -0800962/*
963 * This reflects what should the CC readings should be for
964 * a 5mAh discharge. This value is dependent on
965 * CC_RESOLUTION_N, CC_RESOLUTION_D, CC_READING_TICKS
966 * and rsense
967 */
Abhijeet Dharmapurikar61275b92012-12-27 17:20:29 -0800968#define CC_RAW_5MAH 0x00110000
969#define MIN_OCV_UV 2000000
970#define OCV_RAW_UNINITIALIZED 0xFFFF
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800971static int read_soc_params_raw(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -0800972 struct pm8921_soc_params *raw,
973 int batt_temp_decidegc)
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800974{
975 int usb_chg;
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -0800976 int est_ocv_uv;
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800977
978 mutex_lock(&chip->bms_output_lock);
979 pm_bms_lock_output_data(chip);
980
981 pm_bms_read_output_data(chip,
982 LAST_GOOD_OCV_VALUE, &raw->last_good_ocv_raw);
983 read_cc(chip, &raw->cc);
984
985 pm_bms_unlock_output_data(chip);
986 mutex_unlock(&chip->bms_output_lock);
987
Abhijeet Dharmapurikar9e948982012-08-28 15:21:23 -0700988 usb_chg = usb_chg_plugged_in(chip);
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -0700989
Abhijeet Dharmapurikar61275b92012-12-27 17:20:29 -0800990 if (chip->prev_last_good_ocv_raw == OCV_RAW_UNINITIALIZED) {
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700991 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -0800992
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700993 convert_vbatt_raw_to_uv(chip, usb_chg,
994 raw->last_good_ocv_raw, &raw->last_good_ocv_uv);
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -0800995 adjust_pon_ocv(chip, &raw->last_good_ocv_uv);
Abhijeet Dharmapurikarecdb5db2012-10-09 16:57:18 -0700996 raw->last_good_ocv_uv = ocv_ir_compensation(chip,
997 raw->last_good_ocv_uv);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -0700998 chip->last_ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar3ae2d412012-11-07 19:00:32 -0800999
1000 if (is_warm_restart(chip)
1001 || raw->cc > CC_RAW_5MAH
1002 || (raw->last_good_ocv_uv < MIN_OCV_UV
1003 && raw->cc > 0)) {
1004 /*
1005 * The CC value is higher than 5mAh.
1006 * The phone started without going through a pon
1007 * sequence
1008 * OR
1009 * The ocv was very small and there was no
1010 * charging in the bootloader
1011 * - reset the CC and take an ocv again
1012 */
1013 pr_debug("cc_raw = 0x%x may be > 5mAh(0x%x)\n",
1014 raw->cc, CC_RAW_5MAH);
1015 pr_debug("ocv_uv = %d ocv_raw = 0x%x may be < 2V\n",
1016 chip->last_ocv_uv,
1017 raw->last_good_ocv_raw);
1018 est_ocv_uv = estimate_ocv(chip);
1019 if (est_ocv_uv > 0) {
1020 raw->last_good_ocv_uv = est_ocv_uv;
1021 chip->last_ocv_uv = est_ocv_uv;
1022 reset_cc(chip);
1023 raw->cc = 0;
1024 }
1025 }
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001026 chip->last_ocv_temp_decidegc = batt_temp_decidegc;
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -07001027 pr_debug("PON_OCV_UV = %d\n", chip->last_ocv_uv);
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -07001028 } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) {
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -07001029 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
1030 convert_vbatt_raw_to_uv(chip, usb_chg,
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001031 raw->last_good_ocv_raw, &raw->last_good_ocv_uv);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001032 chip->last_ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001033 chip->last_ocv_temp_decidegc = batt_temp_decidegc;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001034 /* forget the old cc value upon ocv */
1035 chip->last_cc_uah = 0;
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -07001036 } else {
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001037 raw->last_good_ocv_uv = chip->last_ocv_uv;
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -07001038 }
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001039
Abhijeet Dharmapurikar079d6c52013-01-25 13:10:07 -08001040 /* stop faking 100% after an OCV event */
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001041 if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw) {
Abhijeet Dharmapurikar61275b92012-12-27 17:20:29 -08001042 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001043 chip->cc_reading_at_100 = 0;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001044 }
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001045 pr_debug("0p625 = %duV\n", chip->xoadc_v0625);
1046 pr_debug("1p25 = %duV\n", chip->xoadc_v125);
Abhijeet Dharmapurikar1b8e8292012-01-17 11:01:44 -08001047 pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n",
1048 raw->last_good_ocv_raw, raw->last_good_ocv_uv);
1049 pr_debug("cc_raw= 0x%x\n", raw->cc);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001050 return 0;
1051}
1052
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001053static int get_rbatt(struct pm8921_bms_chip *chip, int soc_rbatt, int batt_temp)
1054{
1055 int rbatt, scalefactor;
1056
Abhijeet Dharmapurikar0711c522012-05-22 18:45:25 -07001057 rbatt = chip->default_rbatt_mohm;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001058 pr_debug("rbatt before scaling = %d\n", rbatt);
1059 if (chip->rbatt_sf_lut == NULL) {
1060 pr_debug("RBATT = %d\n", rbatt);
1061 return rbatt;
1062 }
Abhijeet Dharmapurikar12a891c2012-04-12 23:52:06 -07001063 /* Convert the batt_temp to DegC from deciDegC */
1064 batt_temp = batt_temp / 10;
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001065 scalefactor = interpolate_scalingfactor(chip->rbatt_sf_lut,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001066 batt_temp, soc_rbatt);
1067 pr_debug("rbatt sf = %d for batt_temp = %d, soc_rbatt = %d\n",
1068 scalefactor, batt_temp, soc_rbatt);
1069 rbatt = (rbatt * scalefactor) / 100;
1070
Abhijeet Dharmapurikarbaffba42012-03-22 14:41:10 -07001071 rbatt += the_chip->rconn_mohm;
1072 pr_debug("adding rconn_mohm = %d rbatt = %d\n",
1073 the_chip->rconn_mohm, rbatt);
1074
Abhijeet Dharmapurikarded189b2012-03-30 10:12:28 -07001075 if (is_between(20, 10, soc_rbatt))
1076 rbatt = rbatt
1077 + ((20 - soc_rbatt) * chip->delta_rbatt_mohm) / 10;
1078 else
1079 if (is_between(10, 0, soc_rbatt))
1080 rbatt = rbatt + chip->delta_rbatt_mohm;
1081
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001082 pr_debug("RBATT = %d\n", rbatt);
1083 return rbatt;
1084}
1085
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001086static int calculate_fcc_uah(struct pm8921_bms_chip *chip, int batt_temp,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001087 int chargecycles)
1088{
1089 int initfcc, result, scalefactor = 0;
1090
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001091 if (chip->adjusted_fcc_temp_lut == NULL) {
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001092 initfcc = interpolate_fcc(chip->fcc_temp_lut, batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001093
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001094 scalefactor = interpolate_scalingfactor_fcc(chip->fcc_sf_lut,
1095 chargecycles);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001096
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001097 /* Multiply the initial FCC value by the scale factor. */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001098 result = (initfcc * scalefactor * 1000) / 100;
1099 pr_debug("fcc = %d uAh\n", result);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001100 return result;
1101 } else {
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001102 return 1000 * interpolate_fcc(chip->adjusted_fcc_temp_lut,
1103 batt_temp);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001104 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001105}
1106
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001107static int get_battery_uvolts(struct pm8921_bms_chip *chip, int *uvolts)
1108{
1109 int rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001110 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001111
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001112 rc = pm8xxx_adc_read(chip->vbat_channel, &result);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001113 if (rc) {
1114 pr_err("error reading adc channel = %d, rc = %d\n",
1115 chip->vbat_channel, rc);
1116 return rc;
1117 }
1118 pr_debug("mvolts phy = %lld meas = 0x%llx", result.physical,
1119 result.measurement);
1120 *uvolts = (int)result.physical;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001121 return 0;
1122}
1123
1124static int adc_based_ocv(struct pm8921_bms_chip *chip, int *ocv)
1125{
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001126 int vbatt, rbatt, ibatt_ua, rc;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001127
1128 rc = get_battery_uvolts(chip, &vbatt);
1129 if (rc) {
1130 pr_err("failed to read vbatt from adc rc = %d\n", rc);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001131 return rc;
1132 }
1133
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001134 rc = pm8921_bms_get_battery_current(&ibatt_ua);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001135 if (rc) {
1136 pr_err("failed to read batt current rc = %d\n", rc);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001137 return rc;
1138 }
1139
Abhijeet Dharmapurikar0711c522012-05-22 18:45:25 -07001140 rbatt = chip->default_rbatt_mohm;
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001141 *ocv = vbatt + (ibatt_ua * rbatt)/1000;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001142 return 0;
1143}
1144
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001145static int calculate_pc(struct pm8921_bms_chip *chip, int ocv_uv,
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001146 int batt_temp_decidegc, int chargecycles)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001147{
1148 int pc, scalefactor;
1149
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001150 pc = interpolate_pc(chip->pc_temp_ocv_lut,
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001151 batt_temp_decidegc / 10, ocv_uv / 1000);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001152 pr_debug("pc = %u for ocv = %dmicroVolts batt_temp = %d\n",
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001153 pc, ocv_uv, batt_temp_decidegc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001154
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001155 scalefactor = interpolate_scalingfactor(chip->pc_sf_lut,
1156 chargecycles, pc);
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001157 pr_debug("scalefactor = %u batt_temp = %d\n",
1158 scalefactor, batt_temp_decidegc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001159
1160 /* Multiply the initial FCC value by the scale factor. */
1161 pc = (pc * scalefactor) / 100;
1162 return pc;
1163}
1164
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001165/**
1166 * calculate_cc_uah -
1167 * @chip: the bms chip pointer
1168 * @cc: the cc reading from bms h/w
1169 * @val: return value
1170 * @coulumb_counter: adjusted coulumb counter for 100%
1171 *
1172 * RETURNS: in val pointer coulumb counter based charger in uAh
1173 * (micro Amp hour)
1174 */
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001175static void calculate_cc_uah(struct pm8921_bms_chip *chip, int cc, int *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001176{
Xiaozhe Shi35b82e42013-02-12 23:17:54 -08001177 int64_t cc_voltage_uv, cc_pvh, cc_uah;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001179 cc_voltage_uv = cc;
1180 cc_voltage_uv -= chip->cc_reading_at_100;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001181 pr_debug("cc = %d. after subtracting 0x%x cc = %lld\n",
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001182 cc, chip->cc_reading_at_100,
1183 cc_voltage_uv);
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -07001184 cc_voltage_uv = cc_to_microvolt(chip, cc_voltage_uv);
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08001185 cc_voltage_uv = pm8xxx_cc_adjust_for_gain(cc_voltage_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001186 pr_debug("cc_voltage_uv = %lld microvolts\n", cc_voltage_uv);
Xiaozhe Shi35b82e42013-02-12 23:17:54 -08001187 cc_pvh = ccmicrovolt_to_pvh(cc_voltage_uv);
1188 pr_debug("cc_pvh = %lld pico_volt_hour\n", cc_pvh);
1189 cc_uah = div_s64(cc_pvh, chip->r_sense_uohm);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001190 *val = cc_uah;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001191}
1192
Abhijeet Dharmapurikar4c78f462012-10-16 15:43:55 -07001193int pm8921_bms_cc_uah(int *cc_uah)
1194{
1195 int cc;
1196
1197 *cc_uah = 0;
1198
1199 if (!the_chip)
1200 return -EINVAL;
1201
1202 read_cc(the_chip, &cc);
1203 calculate_cc_uah(the_chip, cc, cc_uah);
1204
1205 return 0;
1206}
1207EXPORT_SYMBOL(pm8921_bms_cc_uah);
1208
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001209static int calculate_termination_uuc(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001210 int batt_temp, int chargecycles,
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001211 int fcc_uah, int i_ma,
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001212 int *ret_pc_unusable)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001213{
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001214 int unusable_uv, pc_unusable, uuc;
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001215 int i = 0;
1216 int ocv_mv;
1217 int batt_temp_degc = batt_temp / 10;
1218 int rbatt_mohm;
1219 int delta_uv;
1220 int prev_delta_uv = 0;
1221 int prev_rbatt_mohm = 0;
1222 int prev_ocv_mv = 0;
1223 int uuc_rbatt_uv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001224
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001225 for (i = 0; i <= 100; i++) {
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001226 ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
1227 batt_temp_degc, i);
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001228 rbatt_mohm = get_rbatt(chip, i, batt_temp);
1229 unusable_uv = (rbatt_mohm * i_ma) + (chip->v_cutoff * 1000);
1230 delta_uv = ocv_mv * 1000 - unusable_uv;
1231
1232 pr_debug("soc = %d ocv = %d rbat = %d u_uv = %d delta_v = %d\n",
1233 i, ocv_mv, rbatt_mohm, unusable_uv, delta_uv);
1234
1235 if (delta_uv > 0)
1236 break;
1237
1238 prev_delta_uv = delta_uv;
1239 prev_rbatt_mohm = rbatt_mohm;
1240 prev_ocv_mv = ocv_mv;
1241 }
1242
1243 uuc_rbatt_uv = linear_interpolate(rbatt_mohm, delta_uv,
1244 prev_rbatt_mohm, prev_delta_uv,
1245 0);
1246
1247 unusable_uv = (uuc_rbatt_uv * i_ma) + (chip->v_cutoff * 1000);
1248
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001249 pc_unusable = calculate_pc(chip, unusable_uv, batt_temp, chargecycles);
1250 uuc = (fcc_uah * pc_unusable) / 100;
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001251 pr_debug("For i_ma = %d, unusable_rbatt = %d unusable_uv = %d unusable_pc = %d uuc = %d\n",
1252 i_ma, uuc_rbatt_uv, unusable_uv,
1253 pc_unusable, uuc);
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001254 *ret_pc_unusable = pc_unusable;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001255 return uuc;
1256}
1257
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001258#define TIME_PER_PERCENT_UUC 60
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001259static int adjust_uuc(struct pm8921_bms_chip *chip, int fcc_uah,
1260 int new_pc_unusable,
1261 int new_uuc,
1262 int batt_temp,
1263 int rbatt,
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001264 int *iavg_ma,
1265 int delta_time_s)
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001266{
1267 int new_unusable_mv;
1268 int batt_temp_degc = batt_temp / 10;
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001269 int max_percent_change;
1270
1271 max_percent_change = max(delta_time_s / TIME_PER_PERCENT_UUC, 1);
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001272
1273 if (chip->prev_pc_unusable == -EINVAL
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001274 || abs(chip->prev_pc_unusable - new_pc_unusable)
1275 <= max_percent_change) {
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001276 chip->prev_pc_unusable = new_pc_unusable;
1277 return new_uuc;
1278 }
1279
1280 /* the uuc is trying to change more than 1% restrict it */
1281 if (new_pc_unusable > chip->prev_pc_unusable)
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001282 chip->prev_pc_unusable += max_percent_change;
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001283 else
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001284 chip->prev_pc_unusable -= max_percent_change;
1285 chip->prev_pc_unusable = clamp(chip->prev_pc_unusable, 0, 100);
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001286 new_uuc = (fcc_uah * chip->prev_pc_unusable) / 100;
1287
1288 /* also find update the iavg_ma accordingly */
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001289 new_unusable_mv = interpolate_ocv(chip->pc_temp_ocv_lut,
1290 batt_temp_degc, chip->prev_pc_unusable);
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001291 if (new_unusable_mv < chip->v_cutoff)
1292 new_unusable_mv = chip->v_cutoff;
1293
1294 *iavg_ma = (new_unusable_mv - chip->v_cutoff) * 1000 / rbatt;
1295 if (*iavg_ma == 0)
1296 *iavg_ma = 1;
1297 pr_debug("Restricting UUC to %d (%d%%) unusable_mv = %d iavg_ma = %d\n",
1298 new_uuc, chip->prev_pc_unusable,
1299 new_unusable_mv, *iavg_ma);
1300
1301 return new_uuc;
1302}
1303
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08001304static int get_current_time(unsigned long *now_tm_sec)
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001305{
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001306 struct rtc_time tm;
1307 struct rtc_device *rtc;
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08001308 int rc;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001309
1310 rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
1311 if (rtc == NULL) {
1312 pr_err("%s: unable to open rtc device (%s)\n",
1313 __FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08001314 return -EINVAL;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001315 }
1316
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001317 rc = rtc_read_time(rtc, &tm);
1318 if (rc) {
1319 pr_err("Error reading rtc device (%s) : %d\n",
1320 CONFIG_RTC_HCTOSYS_DEVICE, rc);
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08001321 return rc;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001322 }
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001323
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001324 rc = rtc_valid_tm(&tm);
1325 if (rc) {
1326 pr_err("Invalid RTC time (%s): %d\n",
1327 CONFIG_RTC_HCTOSYS_DEVICE, rc);
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08001328 return rc;
1329 }
1330 rtc_tm_to_time(&tm, now_tm_sec);
1331
1332 return 0;
1333}
1334
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001335static int calculate_delta_time(struct pm8921_bms_chip *chip, int *delta_time_s)
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08001336{
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08001337 unsigned long now_tm_sec = 0;
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001338
1339 /* default to delta time = 0 if anything fails */
1340 *delta_time_s = 0;
1341
1342 get_current_time(&now_tm_sec);
1343
1344 *delta_time_s = (now_tm_sec - chip->tm_sec);
1345 pr_debug("tm_sec = %ld, now_tm_sec = %ld delta_s = %d\n",
1346 chip->tm_sec, now_tm_sec, *delta_time_s);
1347
1348 /* remember this time */
1349 chip->tm_sec = now_tm_sec;
1350 return 0;
1351}
1352
1353static void calculate_iavg_ua(struct pm8921_bms_chip *chip, int cc_uah,
1354 int *iavg_ua, int delta_time_s)
1355{
1356 int delta_cc_uah = 0;
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08001357
1358 /* if anything fails report the previous iavg_ua */
1359 *iavg_ua = chip->prev_iavg_ua;
1360
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001361 if (chip->last_cc_uah == INT_MIN) {
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001362 pm8921_bms_get_battery_current(iavg_ua);
1363 goto out;
1364 }
1365
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001366 /* use the previous iavg if called within 15 seconds */
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001367 if (delta_time_s < 15) {
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001368 *iavg_ua = chip->prev_iavg_ua;
1369 goto out;
1370 }
1371
1372 delta_cc_uah = cc_uah - chip->last_cc_uah;
1373
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001374 *iavg_ua = div_s64((s64)delta_cc_uah * 3600, delta_time_s);
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001375
1376out:
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001377 pr_debug("delta_cc = %d iavg_ua = %d\n", delta_cc_uah, (int)*iavg_ua);
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001378 /* remember the iavg */
1379 chip->prev_iavg_ua = *iavg_ua;
1380
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001381 /* remember cc_uah */
1382 chip->last_cc_uah = cc_uah;
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001383}
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001384
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001385#define IAVG_SAMPLES 16
Abhijeet Dharmapurikare6503da2012-12-20 18:33:56 -08001386#define MIN_IAVG_MA 250
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001387#define MIN_SECONDS_FOR_VALID_SAMPLE 20
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001388static int calculate_unusable_charge_uah(struct pm8921_bms_chip *chip,
1389 int rbatt, int fcc_uah, int cc_uah,
1390 int soc_rbatt, int batt_temp, int chargecycles,
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001391 int iavg_ua, int delta_time_s)
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001392{
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001393 int uuc_uah_iavg;
1394 int i;
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001395 int iavg_ma = iavg_ua / 1000;
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001396 static int iavg_samples[IAVG_SAMPLES];
1397 static int iavg_index;
1398 static int iavg_num_samples;
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001399 static int firsttime = 1;
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001400 int pc_unusable;
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001401
1402 /*
1403 * if we are called first time fill all the
1404 * samples with the the shutdown_iavg_ua
1405 */
1406 if (firsttime && chip->shutdown_iavg_ua != 0) {
Abhijeet Dharmapurikar6b4af162012-10-10 09:16:27 -07001407 pr_debug("Using shutdown_iavg_ua = %d in all samples\n",
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001408 chip->shutdown_iavg_ua);
1409 for (i = 0; i < IAVG_SAMPLES; i++)
1410 iavg_samples[i] = chip->shutdown_iavg_ua;
1411
1412 iavg_index = 0;
1413 iavg_num_samples = IAVG_SAMPLES;
1414 }
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001415
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001416 /*
1417 * if we are charging use a nominal avg current so that we keep
1418 * a reasonable UUC while charging
1419 */
Abhijeet Dharmapurikare6503da2012-12-20 18:33:56 -08001420 if (iavg_ma < MIN_IAVG_MA)
1421 iavg_ma = MIN_IAVG_MA;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001422 iavg_samples[iavg_index] = iavg_ma;
1423 iavg_index = (iavg_index + 1) % IAVG_SAMPLES;
1424 iavg_num_samples++;
1425 if (iavg_num_samples >= IAVG_SAMPLES)
1426 iavg_num_samples = IAVG_SAMPLES;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001427
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001428 /* now that this sample is added calcualte the average */
1429 iavg_ma = 0;
1430 if (iavg_num_samples != 0) {
1431 for (i = 0; i < iavg_num_samples; i++) {
1432 pr_debug("iavg_samples[%d] = %d\n", i, iavg_samples[i]);
1433 iavg_ma += iavg_samples[i];
1434 }
1435
1436 iavg_ma = DIV_ROUND_CLOSEST(iavg_ma, iavg_num_samples);
1437 }
1438
Abhijeet Dharmapurikar7df608c2012-12-26 21:28:43 -08001439 /*
1440 * if we're in bms reset mode, force uuc to be 3% of fcc
1441 */
1442 if (bms_reset)
1443 return (fcc_uah * 3) / 100;
1444
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001445 uuc_uah_iavg = calculate_termination_uuc(chip,
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001446 batt_temp, chargecycles,
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001447 fcc_uah, iavg_ma,
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001448 &pc_unusable);
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001449 pr_debug("iavg = %d uuc_iavg = %d\n", iavg_ma, uuc_uah_iavg);
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001450
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001451 /* restrict the uuc change to one percent per 60 seconds */
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001452 uuc_uah_iavg = adjust_uuc(chip, fcc_uah, pc_unusable, uuc_uah_iavg,
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001453 batt_temp, rbatt, &iavg_ma, delta_time_s);
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001454
1455 /* find out what the avg current should be for this uuc */
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001456 chip->prev_uuc_iavg_ma = iavg_ma;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001457
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001458 firsttime = 0;
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001459 return uuc_uah_iavg;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001460}
Abhijeet Dharmapurikar52aa9682011-07-20 14:05:47 -07001461
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001462/* calculate remainging charge at the time of ocv */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001463static int calculate_remaining_charge_uah(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001464 struct pm8921_soc_params *raw,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001465 int fcc_uah, int batt_temp,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001466 int chargecycles)
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001467{
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001468 int ocv, pc, batt_temp_decidegc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001469
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001470 ocv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001471 batt_temp_decidegc = chip->last_ocv_temp_decidegc;
1472 pc = calculate_pc(chip, ocv, batt_temp_decidegc, chargecycles);
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001473 pr_debug("ocv = %d pc = %d\n", ocv, pc);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001474 return (fcc_uah * pc) / 100;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001475}
1476
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001477static void calculate_soc_params(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001478 struct pm8921_soc_params *raw,
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001479 int batt_temp, int chargecycles,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001480 int *fcc_uah,
1481 int *unusable_charge_uah,
1482 int *remaining_charge_uah,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001483 int *cc_uah,
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001484 int *rbatt,
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001485 int *iavg_ua)
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001486{
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001487 int soc_rbatt;
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001488 int delta_time_s;
1489 int rc;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001490
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001491 rc = calculate_delta_time(chip, &delta_time_s);
1492 if (rc) {
1493 pr_err("Failed to get delta time from RTC: %d\n", rc);
1494 delta_time_s = 0;
1495 }
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001496 *fcc_uah = calculate_fcc_uah(chip, batt_temp, chargecycles);
1497 pr_debug("FCC = %uuAh batt_temp = %d, cycles = %d\n",
1498 *fcc_uah, batt_temp, chargecycles);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001499
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001500
1501 /* calculate remainging charge */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001502 *remaining_charge_uah = calculate_remaining_charge_uah(chip, raw,
1503 *fcc_uah, batt_temp, chargecycles);
1504 pr_debug("RC = %uuAh\n", *remaining_charge_uah);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001505
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001506 /* calculate cc micro_volt_hour */
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001507 calculate_cc_uah(chip, raw->cc, cc_uah);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001508 pr_debug("cc_uah = %duAh raw->cc = %x cc = %lld after subtracting %x\n",
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001509 *cc_uah, raw->cc,
1510 (int64_t)raw->cc - chip->cc_reading_at_100,
1511 chip->cc_reading_at_100);
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001512
1513 soc_rbatt = ((*remaining_charge_uah - *cc_uah) * 100) / *fcc_uah;
1514 if (soc_rbatt < 0)
1515 soc_rbatt = 0;
1516 *rbatt = get_rbatt(chip, soc_rbatt, batt_temp);
1517
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001518 calculate_iavg_ua(chip, *cc_uah, iavg_ua, delta_time_s);
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001519
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001520 *unusable_charge_uah = calculate_unusable_charge_uah(chip, *rbatt,
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001521 *fcc_uah, *cc_uah, soc_rbatt,
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001522 batt_temp, chargecycles, *iavg_ua,
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001523 delta_time_s);
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001524 pr_debug("UUC = %uuAh\n", *unusable_charge_uah);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001525}
1526
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001527static int calculate_real_fcc_uah(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001528 struct pm8921_soc_params *raw,
1529 int batt_temp, int chargecycles,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001530 int *ret_fcc_uah)
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001531{
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001532 int fcc_uah, unusable_charge_uah;
1533 int remaining_charge_uah;
1534 int cc_uah;
1535 int real_fcc_uah;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001536 int rbatt;
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001537 int iavg_ua;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001538
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001539 calculate_soc_params(chip, raw, batt_temp, chargecycles,
1540 &fcc_uah,
1541 &unusable_charge_uah,
1542 &remaining_charge_uah,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001543 &cc_uah,
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001544 &rbatt,
Xiaozhe Shid28e4972012-12-04 15:13:32 -08001545 &iavg_ua);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001546
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001547 real_fcc_uah = remaining_charge_uah - cc_uah;
1548 *ret_fcc_uah = fcc_uah;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001549 pr_debug("real_fcc = %d, RC = %d CC = %d fcc = %d\n",
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001550 real_fcc_uah, remaining_charge_uah, cc_uah, fcc_uah);
1551 return real_fcc_uah;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001552}
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001553
Abhijeet Dharmapurikarb2680132012-05-15 16:32:51 -07001554int pm8921_bms_get_simultaneous_battery_voltage_and_current(int *ibat_ua,
1555 int *vbat_uv)
1556{
1557 int rc;
1558
1559 if (the_chip == NULL) {
1560 pr_err("Called too early\n");
1561 return -EINVAL;
1562 }
1563
1564 if (pm8921_is_batfet_closed()) {
1565 return override_mode_simultaneous_battery_voltage_and_current(
1566 ibat_ua,
1567 vbat_uv);
1568 } else {
1569 pr_debug("batfet is open using separate vbat and ibat meas\n");
1570 rc = get_battery_uvolts(the_chip, vbat_uv);
1571 if (rc < 0) {
1572 pr_err("adc vbat failed err = %d\n", rc);
1573 return rc;
1574 }
1575 rc = pm8921_bms_get_battery_current(ibat_ua);
1576 if (rc < 0) {
1577 pr_err("bms ibat failed err = %d\n", rc);
1578 return rc;
1579 }
1580 }
1581
1582 return 0;
1583}
1584EXPORT_SYMBOL(pm8921_bms_get_simultaneous_battery_voltage_and_current);
1585
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001586static void find_ocv_for_soc(struct pm8921_bms_chip *chip,
1587 int batt_temp,
1588 int chargecycles,
1589 int fcc_uah,
1590 int uuc_uah,
1591 int cc_uah,
1592 int shutdown_soc,
1593 int *rc_uah,
1594 int *ocv_uv)
1595{
1596 s64 rc;
1597 int pc, new_pc;
1598 int batt_temp_degc = batt_temp / 10;
1599 int ocv;
Abhijeet Dharmapurikarcfddddb2012-10-15 12:46:15 -07001600 int count = 0;
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001601
1602 rc = (s64)shutdown_soc * (fcc_uah - uuc_uah);
1603 rc = div_s64(rc, 100) + cc_uah + uuc_uah;
1604 pc = DIV_ROUND_CLOSEST((int)rc * 100, fcc_uah);
1605 pc = clamp(pc, 0, 100);
1606
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001607 ocv = interpolate_ocv(chip->pc_temp_ocv_lut, batt_temp_degc, pc);
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001608
1609 pr_debug("s_soc = %d, fcc = %d uuc = %d rc = %d, pc = %d, ocv mv = %d\n",
1610 shutdown_soc, fcc_uah, uuc_uah, (int)rc, pc, ocv);
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001611 new_pc = interpolate_pc(chip->pc_temp_ocv_lut, batt_temp_degc, ocv);
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001612 pr_debug("test revlookup pc = %d for ocv = %d\n", new_pc, ocv);
1613
Abhijeet Dharmapurikarcfddddb2012-10-15 12:46:15 -07001614 /* try 10 times to get a close enough pc */
1615 while (abs(new_pc - pc) > 1 && count++ < 10) {
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001616 int delta_mv = 5;
1617
1618 if (new_pc > pc)
1619 delta_mv = -1 * delta_mv;
1620
1621 ocv = ocv + delta_mv;
Xiaozhe Shi5ee95192012-09-12 15:16:34 -07001622 new_pc = interpolate_pc(chip->pc_temp_ocv_lut,
1623 batt_temp_degc, ocv);
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001624 pr_debug("test revlookup pc = %d for ocv = %d\n", new_pc, ocv);
1625 }
1626
1627 *ocv_uv = ocv * 1000;
1628 *rc_uah = (int)rc;
1629}
1630
1631static void adjust_rc_and_uuc_for_specific_soc(
1632 struct pm8921_bms_chip *chip,
1633 int batt_temp,
1634 int chargecycles,
1635 int soc,
1636 int fcc_uah,
1637 int uuc_uah,
1638 int cc_uah,
1639 int rc_uah,
1640 int rbatt,
1641 int *ret_ocv,
1642 int *ret_rc,
1643 int *ret_uuc,
1644 int *ret_rbatt)
1645{
1646 int ocv_uv;
1647
1648 find_ocv_for_soc(chip, batt_temp, chargecycles,
1649 fcc_uah, uuc_uah, cc_uah,
1650 soc,
1651 &rc_uah, &ocv_uv);
1652
1653 *ret_ocv = ocv_uv;
1654 *ret_rbatt = rbatt;
1655 *ret_rc = rc_uah;
1656 *ret_uuc = uuc_uah;
1657}
Abhijeet Dharmapurikareca7b3e2012-12-26 21:30:29 -08001658
1659static void calc_current_max(struct pm8921_bms_chip *chip, int ocv_uv,
1660 int rbatt_mohm)
1661{
1662 chip->imax_ua = 1000 * (ocv_uv - chip->v_cutoff * 1000) / rbatt_mohm;
1663}
1664
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001665static int bound_soc(int soc)
1666{
1667 soc = max(0, soc);
1668 soc = min(100, soc);
1669 return soc;
1670}
1671
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001672static int charging_adjustments(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001673 int soc, int vbat_uv, int ibat_ua,
1674 int batt_temp, int chargecycles,
1675 int fcc_uah, int cc_uah, int uuc_uah)
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001676{
1677 int chg_soc;
Abhijeet Dharmapurikara82a00a2012-11-08 17:26:29 -08001678 int vbat_batt_terminal_uv = vbat_uv
1679 + (ibat_ua * chip->rconn_mohm) / 1000;
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001680
1681 if (chip->soc_at_cv == -EINVAL) {
1682 /* In constant current charging return the calc soc */
Abhijeet Dharmapurikara82a00a2012-11-08 17:26:29 -08001683 if (vbat_batt_terminal_uv <= chip->max_voltage_uv)
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001684 pr_debug("CC CHG SOC %d\n", soc);
1685
1686 /* Note the CC to CV point */
Abhijeet Dharmapurikara82a00a2012-11-08 17:26:29 -08001687 if (vbat_batt_terminal_uv >= chip->max_voltage_uv) {
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001688 chip->soc_at_cv = soc;
1689 chip->prev_chg_soc = soc;
1690 chip->ibat_at_cv_ua = ibat_ua;
1691 pr_debug("CC_TO_CV ibat_ua = %d CHG SOC %d\n",
1692 ibat_ua, soc);
1693 }
1694 return soc;
1695 }
1696
1697 /*
1698 * battery is in CV phase - begin liner inerpolation of soc based on
1699 * battery charge current
1700 */
1701
1702 /*
Abhijeet Dharmapurikara82a00a2012-11-08 17:26:29 -08001703 * if voltage lessened by more than 10mV (possibly because of
1704 * a sudden increase in system load) keep reporting the prev chg soc
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001705 */
Abhijeet Dharmapurikara82a00a2012-11-08 17:26:29 -08001706 if (vbat_batt_terminal_uv <= chip->max_voltage_uv - 10000) {
1707 pr_debug("vbat_terminals %d < max = %d CC CHG SOC %d\n",
1708 vbat_batt_terminal_uv,
1709 chip->max_voltage_uv, chip->prev_chg_soc);
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001710 return chip->prev_chg_soc;
1711 }
1712
1713 chg_soc = linear_interpolate(chip->soc_at_cv, chip->ibat_at_cv_ua,
Abhijeet Dharmapurikara82a00a2012-11-08 17:26:29 -08001714 100, -1 * chip->chg_term_ua,
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001715 ibat_ua);
Xiaozhe Shi33985a92012-12-10 13:02:14 -08001716 chg_soc = bound_soc(chg_soc);
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001717
1718 /* always report a higher soc */
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001719 if (chg_soc > chip->prev_chg_soc) {
1720 int new_ocv_uv;
1721 int new_rc;
1722
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001723 chip->prev_chg_soc = chg_soc;
1724
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001725 find_ocv_for_soc(chip, batt_temp, chargecycles,
1726 fcc_uah, uuc_uah, cc_uah,
1727 chg_soc,
1728 &new_rc, &new_ocv_uv);
1729 the_chip->last_ocv_uv = new_ocv_uv;
1730 pr_debug("CC CHG ADJ OCV = %d CHG SOC %d\n",
1731 new_ocv_uv,
1732 chip->prev_chg_soc);
1733 }
1734
Abhijeet Dharmapurikard3c435e2012-08-16 19:23:18 -07001735 pr_debug("Reporting CHG SOC %d\n", chip->prev_chg_soc);
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001736 return chip->prev_chg_soc;
1737}
1738
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07001739static void very_low_voltage_check(struct pm8921_bms_chip *chip,
1740 int ibat_ua, int vbat_uv)
1741{
Anirudh Ghayal6fe4ffa2013-02-01 11:44:33 +05301742 int rc;
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07001743 /*
1744 * if battery is very low (v_cutoff voltage + 20mv) hold
1745 * a wakelock untill soc = 0%
1746 */
1747 if (vbat_uv <= (chip->v_cutoff + 20) * 1000
1748 && !chip->low_voltage_wake_lock_held) {
1749 pr_debug("voltage = %d low holding wakelock\n", vbat_uv);
1750 wake_lock(&chip->low_voltage_wake_lock);
1751 chip->low_voltage_wake_lock_held = 1;
Abhijeet Dharmapurikar683472d2012-10-30 16:21:58 -07001752 chip->soc_calc_period = chip->low_voltage_calc_ms;
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07001753 }
1754
1755 if (vbat_uv > (chip->v_cutoff + 20) * 1000
1756 && chip->low_voltage_wake_lock_held) {
1757 pr_debug("voltage = %d releasing wakelock\n", vbat_uv);
1758 chip->low_voltage_wake_lock_held = 0;
1759 wake_unlock(&chip->low_voltage_wake_lock);
Abhijeet Dharmapurikar683472d2012-10-30 16:21:58 -07001760 chip->soc_calc_period = chip->normal_voltage_calc_ms;
Anirudh Ghayal6fe4ffa2013-02-01 11:44:33 +05301761 rc = pm8921_bms_enable_batt_alarm(chip);
1762 if (rc)
1763 pr_err("Unable to enable batt alarm\n");
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07001764 }
1765}
1766
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001767static int last_soc_est = -EINVAL;
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001768static int adjust_soc(struct pm8921_bms_chip *chip, int soc,
1769 int batt_temp, int chargecycles,
1770 int rbatt, int fcc_uah, int uuc_uah, int cc_uah)
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001771{
1772 int ibat_ua = 0, vbat_uv = 0;
1773 int ocv_est_uv = 0, soc_est = 0, pc_est = 0, pc = 0;
1774 int delta_ocv_uv = 0;
1775 int n = 0;
1776 int rc_new_uah = 0;
1777 int pc_new = 0;
1778 int soc_new = 0;
1779 int m = 0;
Abhijeet Dharmapurikarb2680132012-05-15 16:32:51 -07001780 int rc = 0;
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -07001781 int delta_ocv_uv_limit = 0;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001782
Abhijeet Dharmapurikarb2680132012-05-15 16:32:51 -07001783 rc = pm8921_bms_get_simultaneous_battery_voltage_and_current(
1784 &ibat_ua,
1785 &vbat_uv);
1786 if (rc < 0) {
1787 pr_err("simultaneous vbat ibat failed err = %d\n", rc);
1788 goto out;
1789 }
1790
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07001791 very_low_voltage_check(chip, ibat_ua, vbat_uv);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001792
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -07001793 delta_ocv_uv_limit = DIV_ROUND_CLOSEST(ibat_ua, 1000);
1794
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001795 ocv_est_uv = vbat_uv + (ibat_ua * rbatt)/1000;
Abhijeet Dharmapurikareca7b3e2012-12-26 21:30:29 -08001796 calc_current_max(chip, ocv_est_uv, rbatt);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001797 pc_est = calculate_pc(chip, ocv_est_uv, batt_temp, last_chargecycles);
1798 soc_est = div_s64((s64)fcc_uah * pc_est - uuc_uah*100,
1799 (s64)fcc_uah - uuc_uah);
1800 soc_est = bound_soc(soc_est);
1801
Abhijeet Dharmapurikar7df608c2012-12-26 21:28:43 -08001802 /* never adjust during bms reset mode */
1803 if (bms_reset) {
1804 pr_debug("bms reset mode, SOC adjustment skipped\n");
1805 goto out;
1806 }
1807
Abhijeet Dharmapurikar552df982012-10-23 19:13:45 -07001808 if (ibat_ua < 0 && pm8921_is_batfet_closed()) {
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001809 soc = charging_adjustments(chip, soc, vbat_uv, ibat_ua,
1810 batt_temp, chargecycles,
1811 fcc_uah, cc_uah, uuc_uah);
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001812 goto out;
1813 }
1814
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001815 /*
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001816 * do not adjust
1817 * if soc is same as what bms calculated
1818 * if soc_est is between 45 and 25, this is the flat portion of the
1819 * curve where soc_est is not so accurate. We generally don't want to
1820 * adjust when soc_est is inaccurate except for the cases when soc is
1821 * way far off (higher than 50 or lesser than 20).
Abhijeet Dharmapurikar52daf652012-07-19 11:50:47 -07001822 * Also don't adjust soc if it is above 90 becuase we might pull it low
1823 * and cause a bad user experience
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001824 */
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001825 if (soc_est == soc
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -07001826 || (is_between(45, chip->adjust_soc_low_threshold, soc_est)
1827 && is_between(50, chip->adjust_soc_low_threshold - 5, soc))
Abhijeet Dharmapurikar52daf652012-07-19 11:50:47 -07001828 || soc >= 90)
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001829 goto out;
1830
1831 if (last_soc_est == -EINVAL)
1832 last_soc_est = soc;
1833
1834 n = min(200, max(1 , soc + soc_est + last_soc_est));
1835 /* remember the last soc_est in last_soc_est */
1836 last_soc_est = soc_est;
1837
1838 pc = calculate_pc(chip, chip->last_ocv_uv,
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001839 chip->last_ocv_temp_decidegc, last_chargecycles);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001840 if (pc > 0) {
1841 pc_new = calculate_pc(chip, chip->last_ocv_uv - (++m * 1000),
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001842 chip->last_ocv_temp_decidegc,
1843 last_chargecycles);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001844 while (pc_new == pc) {
1845 /* start taking 10mV steps */
1846 m = m + 10;
1847 pc_new = calculate_pc(chip,
1848 chip->last_ocv_uv - (m * 1000),
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001849 chip->last_ocv_temp_decidegc,
1850 last_chargecycles);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001851 }
1852 } else {
1853 /*
1854 * pc is already at the lowest point,
1855 * assume 1 millivolt translates to 1% pc
1856 */
1857 pc = 1;
1858 pc_new = 0;
1859 m = 1;
1860 }
1861
1862 delta_ocv_uv = div_s64((soc - soc_est) * (s64)m * 1000,
1863 n * (pc - pc_new));
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -07001864
1865 if (abs(delta_ocv_uv) > delta_ocv_uv_limit) {
1866 pr_debug("limiting delta ocv %d limit = %d\n", delta_ocv_uv,
1867 delta_ocv_uv_limit);
1868
1869 if (delta_ocv_uv > 0)
1870 delta_ocv_uv = delta_ocv_uv_limit;
1871 else
1872 delta_ocv_uv = -1 * delta_ocv_uv_limit;
1873 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
1874 }
1875
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001876 chip->last_ocv_uv -= delta_ocv_uv;
1877
1878 if (chip->last_ocv_uv >= chip->max_voltage_uv)
1879 chip->last_ocv_uv = chip->max_voltage_uv;
1880
1881 /* calculate the soc based on this new ocv */
1882 pc_new = calculate_pc(chip, chip->last_ocv_uv,
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08001883 chip->last_ocv_temp_decidegc, last_chargecycles);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001884 rc_new_uah = (fcc_uah * pc_new) / 100;
1885 soc_new = (rc_new_uah - cc_uah - uuc_uah)*100 / (fcc_uah - uuc_uah);
1886 soc_new = bound_soc(soc_new);
1887
1888 /*
1889 * if soc_new is ZERO force it higher so that phone doesnt report soc=0
1890 * soc = 0 should happen only when soc_est == 0
1891 */
1892 if (soc_new == 0 && soc_est != 0)
1893 soc_new = 1;
1894
1895 soc = soc_new;
1896
1897out:
1898 pr_debug("ibat_ua = %d, vbat_uv = %d, ocv_est_uv = %d, pc_est = %d, "
1899 "soc_est = %d, n = %d, delta_ocv_uv = %d, last_ocv_uv = %d, "
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001900 "pc_new = %d, soc_new = %d, rbatt = %d, m = %d\n",
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001901 ibat_ua, vbat_uv, ocv_est_uv, pc_est,
1902 soc_est, n, delta_ocv_uv, chip->last_ocv_uv,
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001903 pc_new, soc_new, rbatt, m);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001904
1905 return soc;
1906}
1907
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -07001908#define IGNORE_SOC_TEMP_DECIDEG 50
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001909#define IAVG_STEP_SIZE_MA 50
1910#define IAVG_START 600
1911#define SOC_ZERO 0xFF
1912static void backup_soc_and_iavg(struct pm8921_bms_chip *chip, int batt_temp,
1913 int soc)
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001914{
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001915 u8 temp;
1916 int iavg_ma = chip->prev_uuc_iavg_ma;
1917
1918 if (iavg_ma > IAVG_START)
1919 temp = (iavg_ma - IAVG_START) / IAVG_STEP_SIZE_MA;
1920 else
1921 temp = 0;
1922
1923 pm_bms_masked_write(chip, TEMP_IAVG_STORAGE,
1924 TEMP_IAVG_STORAGE_USE_MASK, temp);
1925
1926 /* since only 6 bits are available for SOC, we store half the soc */
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -07001927 if (soc == 0)
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001928 temp = SOC_ZERO;
1929 else
1930 temp = soc;
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001931
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001932 /* don't store soc if temperature is below 5degC */
1933 if (batt_temp > IGNORE_SOC_TEMP_DECIDEG)
1934 pm8xxx_writeb(the_chip->dev->parent, TEMP_SOC_STORAGE, temp);
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001935}
1936
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001937static void read_shutdown_soc_and_iavg(struct pm8921_bms_chip *chip)
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001938{
1939 int rc;
1940 u8 temp;
1941
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001942 rc = pm8xxx_readb(chip->dev->parent, TEMP_IAVG_STORAGE, &temp);
1943 if (rc) {
1944 pr_err("failed to read addr = %d %d assuming %d\n",
1945 TEMP_IAVG_STORAGE, rc, IAVG_START);
1946 chip->shutdown_iavg_ua = IAVG_START;
1947 } else {
1948 temp &= TEMP_IAVG_STORAGE_USE_MASK;
1949
1950 if (temp == 0) {
1951 chip->shutdown_iavg_ua = IAVG_START;
1952 } else {
1953 chip->shutdown_iavg_ua = IAVG_START
1954 + IAVG_STEP_SIZE_MA * (temp + 1);
1955 }
1956 }
1957
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001958 rc = pm8xxx_readb(chip->dev->parent, TEMP_SOC_STORAGE, &temp);
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001959 if (rc) {
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001960 pr_err("failed to read addr = %d %d\n", TEMP_SOC_STORAGE, rc);
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001961 } else {
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001962 chip->shutdown_soc = temp;
1963
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001964 if (chip->shutdown_soc == 0) {
1965 pr_debug("No shutdown soc available\n");
1966 shutdown_soc_invalid = 1;
1967 chip->shutdown_iavg_ua = 0;
1968 } else if (chip->shutdown_soc == SOC_ZERO) {
1969 chip->shutdown_soc = 0;
1970 }
1971 }
Abhijeet Dharmapurikar1f4cc512012-07-21 00:10:39 -07001972
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001973 if (chip->ignore_shutdown_soc) {
1974 shutdown_soc_invalid = 1;
1975 chip->shutdown_soc = 0;
1976 chip->shutdown_iavg_ua = 0;
1977 }
1978
1979 pr_debug("shutdown_soc = %d shutdown_iavg = %d shutdown_soc_invalid = %d\n",
1980 chip->shutdown_soc,
1981 chip->shutdown_iavg_ua,
1982 shutdown_soc_invalid);
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001983}
1984
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07001985#define SOC_CATCHUP_SEC_MAX 600
1986#define SOC_CATCHUP_SEC_PER_PERCENT 60
1987#define MAX_CATCHUP_SOC (SOC_CATCHUP_SEC_MAX/SOC_CATCHUP_SEC_PER_PERCENT)
1988static int scale_soc_while_chg(struct pm8921_bms_chip *chip,
1989 int delta_time_us, int new_soc, int prev_soc)
1990{
1991 int chg_time_sec;
1992 int catch_up_sec;
1993 int scaled_soc;
1994 int numerator;
1995
1996 /*
1997 * The device must be charging for reporting a higher soc, if
1998 * not ignore this soc and continue reporting the prev_soc.
1999 * Also don't report a high value immediately slowly scale the
2000 * value from prev_soc to the new soc based on a charge time
2001 * weighted average
2002 */
2003
2004 /* if we are not charging return last soc */
2005 if (the_chip->start_percent == -EINVAL)
2006 return prev_soc;
2007
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002008 chg_time_sec = DIV_ROUND_UP(the_chip->charge_time_us, USEC_PER_SEC);
2009 catch_up_sec = DIV_ROUND_UP(the_chip->catch_up_time_us, USEC_PER_SEC);
Xiaozhe Shid3ce9862012-12-17 19:34:50 -08002010 if (catch_up_sec == 0)
2011 return new_soc;
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002012 pr_debug("cts= %d catch_up_sec = %d\n", chg_time_sec, catch_up_sec);
2013
2014 /*
2015 * if we have been charging for more than catch_up time simply return
2016 * new soc
2017 */
2018 if (chg_time_sec > catch_up_sec)
2019 return new_soc;
2020
2021 numerator = (catch_up_sec - chg_time_sec) * prev_soc
2022 + chg_time_sec * new_soc;
2023 scaled_soc = numerator / catch_up_sec;
2024
2025 pr_debug("cts = %d new_soc = %d prev_soc = %d scaled_soc = %d\n",
2026 chg_time_sec, new_soc, prev_soc, scaled_soc);
2027
2028 return scaled_soc;
2029}
2030
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002031static bool is_shutdown_soc_within_limits(struct pm8921_bms_chip *chip, int soc)
2032{
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002033 if (shutdown_soc_invalid) {
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002034 pr_debug("NOT forcing shutdown soc = %d\n", chip->shutdown_soc);
2035 return 0;
2036 }
2037
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002038 if (abs(chip->shutdown_soc - soc) > chip->shutdown_soc_valid_limit) {
2039 pr_debug("rejecting shutdown soc = %d, soc = %d limit = %d\n",
2040 chip->shutdown_soc, soc,
2041 chip->shutdown_soc_valid_limit);
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002042 shutdown_soc_invalid = 1;
2043 return 0;
2044 }
2045
2046 return 1;
2047}
Abhijeet Dharmapurikar0622a432012-08-28 16:34:18 -07002048
2049static void update_power_supply(struct pm8921_bms_chip *chip)
2050{
2051 if (chip->batt_psy == NULL || chip->batt_psy < 0)
2052 chip->batt_psy = power_supply_get_by_name("battery");
2053
2054 if (chip->batt_psy > 0)
2055 power_supply_changed(chip->batt_psy);
2056}
2057
Xiaozhe Shid035f6a2012-12-18 16:16:33 -08002058#define MIN_DELTA_625_UV 1000
2059static void calib_hkadc(struct pm8921_bms_chip *chip)
2060{
2061 int voltage, rc;
2062 struct pm8xxx_adc_chan_result result;
2063 int usb_chg;
2064 int this_delta;
2065
2066 mutex_lock(&chip->calib_mutex);
2067 rc = pm8xxx_adc_read(the_chip->ref1p25v_channel, &result);
2068 if (rc) {
2069 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
2070 goto out;
2071 }
2072 voltage = xoadc_reading_to_microvolt(result.adc_code);
2073
2074 pr_debug("result 1.25v = 0x%x, voltage = %duV adc_meas = %lld\n",
2075 result.adc_code, voltage, result.measurement);
2076
2077 chip->xoadc_v125 = voltage;
2078
2079 rc = pm8xxx_adc_read(the_chip->ref625mv_channel, &result);
2080 if (rc) {
2081 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
2082 goto out;
2083 }
2084 voltage = xoadc_reading_to_microvolt(result.adc_code);
2085
2086 usb_chg = usb_chg_plugged_in(chip);
2087 pr_debug("result 0.625V = 0x%x, voltage = %duV adc_meas = %lld usb_chg = %d\n",
2088 result.adc_code, voltage, result.measurement,
2089 usb_chg);
2090
2091 if (usb_chg)
2092 chip->xoadc_v0625_usb_present = voltage;
2093 else
2094 chip->xoadc_v0625_usb_absent = voltage;
2095
2096 chip->xoadc_v0625 = voltage;
2097 if (chip->xoadc_v0625_usb_present && chip->xoadc_v0625_usb_absent) {
2098 this_delta = chip->xoadc_v0625_usb_present
2099 - chip->xoadc_v0625_usb_absent;
2100 pr_debug("this_delta= %duV\n", this_delta);
2101 if (this_delta > MIN_DELTA_625_UV)
2102 last_usb_cal_delta_uv = this_delta;
2103 pr_debug("625V_present= %d, 625V_absent= %d, delta = %duV\n",
2104 chip->xoadc_v0625_usb_present,
2105 chip->xoadc_v0625_usb_absent,
2106 last_usb_cal_delta_uv);
2107 }
2108 pr_debug("calibration batt_temp = %d\n", chip->last_calib_temp);
2109out:
2110 mutex_unlock(&chip->calib_mutex);
2111}
2112
2113#define HKADC_CALIB_DELAY_S 600
2114#define HKADC_CALIB_DELTA_TEMP 20
2115static void calib_hkadc_check(struct pm8921_bms_chip *chip, int batt_temp)
2116{
2117 unsigned long time_since_last_calib;
2118 unsigned long tm_now_sec;
2119 int delta_temp;
2120 int rc;
2121
2122 rc = get_current_time(&tm_now_sec);
2123 if (rc) {
2124 pr_err("Could not read current time: %d\n", rc);
2125 return;
2126 }
2127 if (tm_now_sec > chip->last_calib_time) {
2128 time_since_last_calib = tm_now_sec - chip->last_calib_time;
2129 delta_temp = abs(chip->last_calib_temp - batt_temp);
2130 pr_debug("time since last calib: %lu, temp diff = %d\n",
2131 time_since_last_calib, delta_temp);
2132 if (time_since_last_calib >= HKADC_CALIB_DELAY_S
2133 || delta_temp > HKADC_CALIB_DELTA_TEMP) {
2134 chip->last_calib_temp = batt_temp;
2135 chip->last_calib_time = tm_now_sec;
2136 calib_hkadc(chip);
2137 }
2138 }
2139}
2140
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07002141/*
2142 * Remaining Usable Charge = remaining_charge (charge at ocv instance)
2143 * - coloumb counter charge
2144 * - unusable charge (due to battery resistance)
2145 * SOC% = (remaining usable charge/ fcc - usable_charge);
2146 */
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07002147static int calculate_state_of_charge(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002148 struct pm8921_soc_params *raw,
2149 int batt_temp, int chargecycles)
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07002150{
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002151 int remaining_usable_charge_uah, fcc_uah, unusable_charge_uah;
2152 int remaining_charge_uah, soc;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002153 int cc_uah;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002154 int rbatt;
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07002155 int iavg_ua;
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002156 int new_ocv;
Abhijeet Dharmapurikar6f42a232012-07-17 22:11:13 -07002157 int new_rc_uah;
2158 int new_ucc_uah;
2159 int new_rbatt;
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002160 int shutdown_soc;
Abhijeet Dharmapurikar0622a432012-08-28 16:34:18 -07002161 int new_calculated_soc;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07002162 static int firsttime = 1;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07002163
Xiaozhe Shid035f6a2012-12-18 16:16:33 -08002164 calib_hkadc_check(chip, batt_temp);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002165 calculate_soc_params(chip, raw, batt_temp, chargecycles,
2166 &fcc_uah,
2167 &unusable_charge_uah,
2168 &remaining_charge_uah,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002169 &cc_uah,
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07002170 &rbatt,
Xiaozhe Shid28e4972012-12-04 15:13:32 -08002171 &iavg_ua);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002172
2173 /* calculate remaining usable charge */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002174 remaining_usable_charge_uah = remaining_charge_uah
2175 - cc_uah
2176 - unusable_charge_uah;
2177
2178 pr_debug("RUC = %duAh\n", remaining_usable_charge_uah);
Abhijeet Dharmapurikarbbae88312012-02-14 18:56:42 -08002179 if (fcc_uah - unusable_charge_uah <= 0) {
Xiaozhe Shieec91df2012-11-29 12:11:42 -08002180 pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
Abhijeet Dharmapurikarbbae88312012-02-14 18:56:42 -08002181 fcc_uah, unusable_charge_uah);
2182 soc = 0;
2183 } else {
Abhijeet Dharmapurikar4ff213c2012-07-11 21:28:29 -07002184 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2185 (fcc_uah - unusable_charge_uah));
Abhijeet Dharmapurikarbbae88312012-02-14 18:56:42 -08002186 }
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002187
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07002188 if (firsttime && soc < 0) {
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002189 /*
2190 * first time calcualtion and the pon ocv is too low resulting
2191 * in a bad soc. Adjust ocv such that we get 0 soc
2192 */
2193 pr_debug("soc is %d, adjusting pon ocv to make it 0\n", soc);
2194 adjust_rc_and_uuc_for_specific_soc(
2195 chip,
2196 batt_temp, chargecycles,
2197 0,
2198 fcc_uah, unusable_charge_uah,
2199 cc_uah, remaining_charge_uah,
2200 rbatt,
2201 &new_ocv,
2202 &new_rc_uah, &new_ucc_uah,
2203 &new_rbatt);
2204 chip->last_ocv_uv = new_ocv;
2205 remaining_charge_uah = new_rc_uah;
2206 unusable_charge_uah = new_ucc_uah;
2207 rbatt = new_rbatt;
2208
2209 remaining_usable_charge_uah = remaining_charge_uah
2210 - cc_uah
2211 - unusable_charge_uah;
2212
2213 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2214 (fcc_uah - unusable_charge_uah));
2215 pr_debug("DONE for O soc is %d, pon ocv adjusted to %duV\n",
2216 soc, chip->last_ocv_uv);
2217 }
2218
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07002219 if (soc > 100)
2220 soc = 100;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07002221
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07002222 if (soc < 0) {
Xiaozhe Shieec91df2012-11-29 12:11:42 -08002223 pr_debug("bad rem_usb_chg = %d rem_chg %d,"
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002224 "cc_uah %d, unusb_chg %d\n",
2225 remaining_usable_charge_uah,
2226 remaining_charge_uah,
2227 cc_uah, unusable_charge_uah);
2228
Xiaozhe Shieec91df2012-11-29 12:11:42 -08002229 pr_debug("for bad rem_usb_chg last_ocv_uv = %d"
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07002230 "chargecycles = %d, batt_temp = %d"
2231 "fcc = %d soc =%d\n",
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002232 chip->last_ocv_uv, chargecycles, batt_temp,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002233 fcc_uah, soc);
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -08002234 soc = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002235 }
2236
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002237 mutex_lock(&soc_invalidation_mutex);
2238 shutdown_soc = chip->shutdown_soc;
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002239
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07002240 if (firsttime && soc != shutdown_soc
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002241 && is_shutdown_soc_within_limits(chip, soc)) {
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002242 /*
2243 * soc for the first time - use shutdown soc
2244 * to adjust pon ocv since it is a small percent away from
2245 * the real soc
2246 */
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002247 pr_debug("soc = %d before forcing shutdown_soc = %d\n",
2248 soc, shutdown_soc);
2249 adjust_rc_and_uuc_for_specific_soc(
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002250 chip,
2251 batt_temp, chargecycles,
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002252 shutdown_soc,
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002253 fcc_uah, unusable_charge_uah,
2254 cc_uah, remaining_charge_uah,
2255 rbatt,
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002256 &new_ocv,
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002257 &new_rc_uah, &new_ucc_uah,
2258 &new_rbatt);
2259
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002260 chip->pon_ocv_uv = chip->last_ocv_uv;
2261 chip->last_ocv_uv = new_ocv;
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002262 remaining_charge_uah = new_rc_uah;
2263 unusable_charge_uah = new_ucc_uah;
2264 rbatt = new_rbatt;
2265
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002266 remaining_usable_charge_uah = remaining_charge_uah
2267 - cc_uah
2268 - unusable_charge_uah;
2269
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002270 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2271 (fcc_uah - unusable_charge_uah));
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002272
2273 pr_debug("DONE for shutdown_soc = %d soc is %d, adjusted ocv to %duV\n",
2274 shutdown_soc, soc, chip->last_ocv_uv);
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002275 }
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002276 mutex_unlock(&soc_invalidation_mutex);
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002277
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07002278 pr_debug("SOC before adjustment = %d\n", soc);
Abhijeet Dharmapurikar0622a432012-08-28 16:34:18 -07002279 new_calculated_soc = adjust_soc(chip, soc, batt_temp, chargecycles,
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002280 rbatt, fcc_uah, unusable_charge_uah, cc_uah);
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07002281
Abhijeet Dharmapurikar0622a432012-08-28 16:34:18 -07002282 pr_debug("calculated SOC = %d\n", new_calculated_soc);
2283 if (new_calculated_soc != calculated_soc)
2284 update_power_supply(chip);
2285
2286 calculated_soc = new_calculated_soc;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07002287 firsttime = 0;
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08002288 get_current_time(&chip->last_recalc_time);
Abhijeet Dharmapurikar1939ede2013-01-10 15:59:33 -08002289
2290 if (chip->disable_flat_portion_ocv) {
2291 if (is_between(chip->ocv_dis_high_soc, chip->ocv_dis_low_soc,
2292 calculated_soc)) {
2293 pm8921_bms_stop_ocv_updates();
2294 } else {
2295 pm8921_bms_start_ocv_updates();
2296 }
2297 }
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002298 return calculated_soc;
2299}
2300
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08002301static int recalculate_soc(struct pm8921_bms_chip *chip)
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002302{
Xiaozhe Shid035f6a2012-12-18 16:16:33 -08002303 int batt_temp;
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002304 struct pm8921_soc_params raw;
2305 int soc;
2306
Abhijeet Dharmapurikarb2aeb7e2012-12-26 21:30:05 -08002307 wake_lock(&the_chip->soc_wake_lock);
Xiaozhe Shid035f6a2012-12-18 16:16:33 -08002308 get_batt_temp(chip, &batt_temp);
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002309
2310 mutex_lock(&chip->last_ocv_uv_mutex);
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08002311 read_soc_params_raw(chip, &raw, batt_temp);
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002312
2313 soc = calculate_state_of_charge(chip, &raw,
2314 batt_temp, last_chargecycles);
2315 mutex_unlock(&chip->last_ocv_uv_mutex);
Abhijeet Dharmapurikarb2aeb7e2012-12-26 21:30:05 -08002316 wake_unlock(&the_chip->soc_wake_lock);
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08002317 return soc;
2318}
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002319
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08002320static void calculate_soc_work(struct work_struct *work)
2321{
Willie Ruan38760d32013-01-14 15:46:01 -08002322 struct delayed_work *dwork = to_delayed_work(work);
2323 struct pm8921_bms_chip *chip = container_of(dwork,
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08002324 struct pm8921_bms_chip,
Willie Ruan38760d32013-01-14 15:46:01 -08002325 calculate_soc_delayed_work);
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08002326
2327 recalculate_soc(chip);
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002328 schedule_delayed_work(&chip->calculate_soc_delayed_work,
2329 round_jiffies_relative(msecs_to_jiffies
Abhijeet Dharmapurikar683472d2012-10-30 16:21:58 -07002330 (chip->soc_calc_period)));
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002331}
2332
2333static int report_state_of_charge(struct pm8921_bms_chip *chip)
2334{
2335 int soc = calculated_soc;
2336 int delta_time_us;
2337 struct timespec now;
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002338 int batt_temp;
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002339
Abhijeet Dharmapurikard02bd882012-08-22 17:48:16 -07002340 if (bms_fake_battery != -EINVAL) {
2341 pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery);
2342 return bms_fake_battery;
2343 }
2344
Xiaozhe Shid035f6a2012-12-18 16:16:33 -08002345 get_batt_temp(chip, &batt_temp);
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002346
2347 do_posix_clock_monotonic_gettime(&now);
2348 if (chip->t_soc_queried.tv_sec != 0) {
2349 delta_time_us
2350 = (now.tv_sec - chip->t_soc_queried.tv_sec) * USEC_PER_SEC
2351 + (now.tv_nsec - chip->t_soc_queried.tv_nsec) / 1000;
2352 } else {
2353 /* calculation for the first time */
2354 delta_time_us = 0;
2355 }
2356
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002357 /*
2358 * account for charge time - limit it to SOC_CATCHUP_SEC to
2359 * avoid overflows when charging continues for extended periods
2360 */
2361 if (the_chip->start_percent != -EINVAL) {
2362 if (the_chip->charge_time_us == 0) {
2363 /*
2364 * calculating soc for the first time
2365 * after start of chg. Initialize catchup time
2366 */
2367 if (abs(soc - last_soc) < MAX_CATCHUP_SOC)
2368 the_chip->catch_up_time_us =
2369 (soc - last_soc) * SOC_CATCHUP_SEC_PER_PERCENT
2370 * USEC_PER_SEC;
2371 else
2372 the_chip->catch_up_time_us =
2373 SOC_CATCHUP_SEC_MAX * USEC_PER_SEC;
2374
2375 if (the_chip->catch_up_time_us < 0)
2376 the_chip->catch_up_time_us = 0;
2377 }
2378
2379 /* add charge time */
2380 if (the_chip->charge_time_us
2381 < SOC_CATCHUP_SEC_MAX * USEC_PER_SEC)
2382 chip->charge_time_us += delta_time_us;
2383
2384 /* end catchup if calculated soc and last soc are same */
2385 if (last_soc == soc)
2386 the_chip->catch_up_time_us = 0;
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07002387 }
2388
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002389 /* last_soc < soc ... scale and catch up */
Xiaozhe Shid3ce9862012-12-17 19:34:50 -08002390 if (last_soc != -EINVAL && last_soc < soc && soc != 100)
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002391 soc = scale_soc_while_chg(chip, delta_time_us, soc, last_soc);
2392
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002393 last_soc = soc;
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002394 backup_soc_and_iavg(chip, batt_temp, last_soc);
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -07002395 pr_debug("Reported SOC = %d\n", last_soc);
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002396 chip->t_soc_queried = now;
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -07002397
2398 return last_soc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002399}
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07002400
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002401void pm8921_bms_invalidate_shutdown_soc(void)
2402{
2403 int calculate_soc = 0;
2404 struct pm8921_bms_chip *chip = the_chip;
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002405
2406 pr_debug("Invalidating shutdown soc - the battery was removed\n");
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002407 if (shutdown_soc_invalid)
2408 return;
2409
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002410 mutex_lock(&soc_invalidation_mutex);
2411 shutdown_soc_invalid = 1;
2412 last_soc = -EINVAL;
2413 if (the_chip) {
2414 /* reset to pon ocv undoing what the adjusting did */
2415 if (the_chip->pon_ocv_uv) {
2416 the_chip->last_ocv_uv = the_chip->pon_ocv_uv;
2417 calculate_soc = 1;
2418 pr_debug("resetting ocv to pon_ocv = %d\n",
2419 the_chip->pon_ocv_uv);
2420 }
2421 }
2422 mutex_unlock(&soc_invalidation_mutex);
2423 if (!calculate_soc)
2424 return;
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08002425 recalculate_soc(chip);
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002426}
2427EXPORT_SYMBOL(pm8921_bms_invalidate_shutdown_soc);
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002428
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002429static void calibrate_hkadc_work(struct work_struct *work)
2430{
2431 struct pm8921_bms_chip *chip = container_of(work,
2432 struct pm8921_bms_chip, calib_hkadc_work);
2433
2434 calib_hkadc(chip);
2435}
2436
Abhijeet Dharmapurikar1b8e8292012-01-17 11:01:44 -08002437void pm8921_bms_calibrate_hkadc(void)
2438{
2439 schedule_work(&the_chip->calib_hkadc_work);
2440}
2441
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07002442int pm8921_bms_get_vsense_avg(int *result)
2443{
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002444 int rc = -EINVAL;
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002445
2446 if (the_chip) {
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002447 mutex_lock(&the_chip->bms_output_lock);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002448 pm_bms_lock_output_data(the_chip);
2449 rc = read_vsense_avg(the_chip, result);
2450 pm_bms_unlock_output_data(the_chip);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002451 mutex_unlock(&the_chip->bms_output_lock);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002452 }
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07002453
2454 pr_err("called before initialization\n");
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002455 return rc;
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07002456}
2457EXPORT_SYMBOL(pm8921_bms_get_vsense_avg);
2458
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08002459int pm8921_bms_get_battery_current(int *result_ua)
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07002460{
Xiaozhe Shid69c91e2012-11-06 10:00:38 -08002461 int vsense_uv;
Anirudh Ghayal3a38a382012-12-05 10:42:17 +05302462 int rc = 0;
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002463
Anirudh Ghayal3a38a382012-12-05 10:42:17 +05302464 *result_ua = 0;
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07002465 if (!the_chip) {
2466 pr_err("called before initialization\n");
2467 return -EINVAL;
2468 }
Xiaozhe Shid69c91e2012-11-06 10:00:38 -08002469 if (the_chip->r_sense_uohm == 0) {
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07002470 pr_err("r_sense is zero\n");
2471 return -EINVAL;
2472 }
2473
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002474 mutex_lock(&the_chip->bms_output_lock);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002475 pm_bms_lock_output_data(the_chip);
Anirudh Ghayal3a38a382012-12-05 10:42:17 +05302476 rc = read_vsense_avg(the_chip, &vsense_uv);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002477 pm_bms_unlock_output_data(the_chip);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002478 mutex_unlock(&the_chip->bms_output_lock);
Anirudh Ghayal3a38a382012-12-05 10:42:17 +05302479 if (rc) {
2480 pr_err("Unable to read vsense average\n");
2481 goto error_vsense;
2482 }
Xiaozhe Shid69c91e2012-11-06 10:00:38 -08002483 pr_debug("vsense=%duV\n", vsense_uv);
Abhijeet Dharmapurikara7a1c6b2011-08-17 10:04:58 -07002484 /* cast for signed division */
Xiaozhe Shid69c91e2012-11-06 10:00:38 -08002485 *result_ua = div_s64(vsense_uv * 1000000LL, the_chip->r_sense_uohm);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002486 pr_debug("ibat=%duA\n", *result_ua);
Anirudh Ghayal3a38a382012-12-05 10:42:17 +05302487
2488error_vsense:
2489 return rc;
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07002490}
2491EXPORT_SYMBOL(pm8921_bms_get_battery_current);
2492
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002493int pm8921_bms_get_percent_charge(void)
2494{
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07002495 if (!the_chip) {
2496 pr_err("called before initialization\n");
2497 return -EINVAL;
2498 }
2499
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002500 return report_state_of_charge(the_chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002501}
2502EXPORT_SYMBOL_GPL(pm8921_bms_get_percent_charge);
2503
Abhijeet Dharmapurikareca7b3e2012-12-26 21:30:29 -08002504int pm8921_bms_get_current_max(void)
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002505{
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002506 if (!the_chip) {
2507 pr_err("called before initialization\n");
2508 return -EINVAL;
2509 }
Abhijeet Dharmapurikareca7b3e2012-12-26 21:30:29 -08002510 return the_chip->imax_ua;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002511}
Abhijeet Dharmapurikareca7b3e2012-12-26 21:30:29 -08002512EXPORT_SYMBOL_GPL(pm8921_bms_get_current_max);
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002513
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07002514int pm8921_bms_get_fcc(void)
2515{
Xiaozhe Shid035f6a2012-12-18 16:16:33 -08002516 int batt_temp;
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07002517
2518 if (!the_chip) {
2519 pr_err("called before initialization\n");
2520 return -EINVAL;
2521 }
2522
Xiaozhe Shid035f6a2012-12-18 16:16:33 -08002523 get_batt_temp(the_chip, &batt_temp);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002524 return calculate_fcc_uah(the_chip, batt_temp, last_chargecycles);
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07002525}
2526EXPORT_SYMBOL_GPL(pm8921_bms_get_fcc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002527void pm8921_bms_charging_began(void)
2528{
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08002529 struct pm8921_soc_params raw;
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08002530 int batt_temp;
2531
2532 get_batt_temp(the_chip, &batt_temp);
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08002533
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002534 mutex_lock(&the_chip->last_ocv_uv_mutex);
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08002535 read_soc_params_raw(the_chip, &raw, batt_temp);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002536 mutex_unlock(&the_chip->last_ocv_uv_mutex);
2537
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002538 the_chip->start_percent = report_state_of_charge(the_chip);
2539
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08002540 bms_start_percent = the_chip->start_percent;
2541 bms_start_ocv_uv = raw.last_good_ocv_uv;
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08002542 calculate_cc_uah(the_chip, raw.cc, &bms_start_cc_uah);
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -08002543 pm_bms_masked_write(the_chip, BMS_TOLERANCES,
2544 IBAT_TOL_MASK, IBAT_TOL_DEFAULT);
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002545 the_chip->charge_time_us = 0;
2546 the_chip->catch_up_time_us = 0;
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07002547
2548 the_chip->soc_at_cv = -EINVAL;
2549 the_chip->prev_chg_soc = -EINVAL;
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07002550 pr_debug("start_percent = %u%%\n", the_chip->start_percent);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002551}
2552EXPORT_SYMBOL_GPL(pm8921_bms_charging_began);
2553
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08002554#define DELTA_FCC_PERCENT 3
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002555#define MIN_START_PERCENT_FOR_LEARNING 30
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07002556void pm8921_bms_charging_end(int is_battery_full)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002557{
Xiaozhe Shid035f6a2012-12-18 16:16:33 -08002558 int batt_temp;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002559 struct pm8921_soc_params raw;
2560
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002561 if (the_chip == NULL)
2562 return;
2563
Xiaozhe Shid035f6a2012-12-18 16:16:33 -08002564 get_batt_temp(the_chip, &batt_temp);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002565
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002566 mutex_lock(&the_chip->last_ocv_uv_mutex);
2567
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08002568 read_soc_params_raw(the_chip, &raw, batt_temp);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002569
Abhijeet Dharmapurikar57aeb922012-01-25 13:22:26 -08002570 calculate_cc_uah(the_chip, raw.cc, &bms_end_cc_uah);
2571
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002572 bms_end_ocv_uv = raw.last_good_ocv_uv;
2573
Abhijeet Dharmapurikare88f2462012-04-25 18:22:38 -07002574 if (is_battery_full && the_chip->enable_fcc_learning
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002575 && the_chip->start_percent <= MIN_START_PERCENT_FOR_LEARNING) {
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002576 int fcc_uah, new_fcc_uah, delta_fcc_uah;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07002577
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002578 new_fcc_uah = calculate_real_fcc_uah(the_chip, &raw,
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08002579 batt_temp, last_chargecycles,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002580 &fcc_uah);
2581 delta_fcc_uah = new_fcc_uah - fcc_uah;
2582 if (delta_fcc_uah < 0)
2583 delta_fcc_uah = -delta_fcc_uah;
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08002584
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002585 if (delta_fcc_uah * 100 > (DELTA_FCC_PERCENT * fcc_uah)) {
2586 /* new_fcc_uah is outside the scope limit it */
2587 if (new_fcc_uah > fcc_uah)
2588 new_fcc_uah
Abhijeet Dharmapurikar873ffd12012-04-17 00:06:15 -07002589 = (fcc_uah +
2590 (DELTA_FCC_PERCENT * fcc_uah) / 100);
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002591 else
2592 new_fcc_uah
Abhijeet Dharmapurikar873ffd12012-04-17 00:06:15 -07002593 = (fcc_uah -
2594 (DELTA_FCC_PERCENT * fcc_uah) / 100);
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002595
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08002596 pr_debug("delta_fcc=%d > %d percent of fcc=%d"
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002597 "restring it to %d\n",
2598 delta_fcc_uah, DELTA_FCC_PERCENT,
2599 fcc_uah, new_fcc_uah);
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08002600 }
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -08002601
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002602 last_real_fcc_mah = new_fcc_uah/1000;
2603 last_real_fcc_batt_temp = batt_temp;
2604 readjust_fcc_table();
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002605 }
2606
2607 if (is_battery_full) {
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002608 the_chip->ocv_reading_at_100 = raw.last_good_ocv_raw;
2609 the_chip->cc_reading_at_100 = raw.cc;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002610
2611 the_chip->last_ocv_uv = the_chip->max_voltage_uv;
2612 raw.last_good_ocv_uv = the_chip->max_voltage_uv;
Abhijeet Dharmapurikar079d6c52013-01-25 13:10:07 -08002613 the_chip->last_ocv_temp_decidegc = batt_temp;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07002614 /*
2615 * since we are treating this as an ocv event
2616 * forget the old cc value
2617 */
2618 the_chip->last_cc_uah = 0;
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002619 pr_debug("EOC BATT_FULL ocv_reading = 0x%x cc = 0x%x\n",
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -08002620 the_chip->ocv_reading_at_100,
2621 the_chip->cc_reading_at_100);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07002622 }
2623
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002624 the_chip->end_percent = calculate_state_of_charge(the_chip, &raw,
2625 batt_temp, last_chargecycles);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002626 mutex_unlock(&the_chip->last_ocv_uv_mutex);
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08002627
2628 bms_end_percent = the_chip->end_percent;
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08002629
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07002630 if (the_chip->end_percent > the_chip->start_percent) {
Abhijeet Dharmapurikar8a113f82012-01-19 10:58:45 -08002631 last_charge_increase +=
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07002632 the_chip->end_percent - the_chip->start_percent;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002633 if (last_charge_increase > 100) {
2634 last_chargecycles++;
2635 last_charge_increase = last_charge_increase % 100;
2636 }
2637 }
2638 pr_debug("end_percent = %u%% last_charge_increase = %d"
2639 "last_chargecycles = %d\n",
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07002640 the_chip->end_percent,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002641 last_charge_increase,
2642 last_chargecycles);
Abhijeet Dharmapurikardba91f42011-12-21 16:56:13 -08002643 the_chip->start_percent = -EINVAL;
2644 the_chip->end_percent = -EINVAL;
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002645 the_chip->charge_time_us = 0;
2646 the_chip->catch_up_time_us = 0;
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07002647 the_chip->soc_at_cv = -EINVAL;
2648 the_chip->prev_chg_soc = -EINVAL;
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -08002649 pm_bms_masked_write(the_chip, BMS_TOLERANCES,
2650 IBAT_TOL_MASK, IBAT_TOL_NOCHG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002651}
2652EXPORT_SYMBOL_GPL(pm8921_bms_charging_end);
2653
2654static irqreturn_t pm8921_bms_sbi_write_ok_handler(int irq, void *data)
2655{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002656 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002657 return IRQ_HANDLED;
2658}
2659
2660static irqreturn_t pm8921_bms_cc_thr_handler(int irq, void *data)
2661{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002662 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002663 return IRQ_HANDLED;
2664}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002665
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002666static irqreturn_t pm8921_bms_vsense_thr_handler(int irq, void *data)
2667{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002668 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002669 return IRQ_HANDLED;
2670}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002671
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002672static irqreturn_t pm8921_bms_vsense_for_r_handler(int irq, void *data)
2673{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002674 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002675 return IRQ_HANDLED;
2676}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002677
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002678static irqreturn_t pm8921_bms_ocv_for_r_handler(int irq, void *data)
2679{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002680 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002681
2682 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002683 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002684 return IRQ_HANDLED;
2685}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002686
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002687static irqreturn_t pm8921_bms_good_ocv_handler(int irq, void *data)
2688{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002689 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002690
2691 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002692 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002693 return IRQ_HANDLED;
2694}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002695
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002696struct pm_bms_irq_init_data {
2697 unsigned int irq_id;
2698 char *name;
2699 unsigned long flags;
2700 irqreturn_t (*handler)(int, void *);
2701};
2702
2703#define BMS_IRQ(_id, _flags, _handler) \
2704{ \
2705 .irq_id = _id, \
2706 .name = #_id, \
2707 .flags = _flags, \
2708 .handler = _handler, \
2709}
2710
2711struct pm_bms_irq_init_data bms_irq_data[] = {
2712 BMS_IRQ(PM8921_BMS_SBI_WRITE_OK, IRQF_TRIGGER_RISING,
2713 pm8921_bms_sbi_write_ok_handler),
2714 BMS_IRQ(PM8921_BMS_CC_THR, IRQF_TRIGGER_RISING,
2715 pm8921_bms_cc_thr_handler),
2716 BMS_IRQ(PM8921_BMS_VSENSE_THR, IRQF_TRIGGER_RISING,
2717 pm8921_bms_vsense_thr_handler),
2718 BMS_IRQ(PM8921_BMS_VSENSE_FOR_R, IRQF_TRIGGER_RISING,
2719 pm8921_bms_vsense_for_r_handler),
2720 BMS_IRQ(PM8921_BMS_OCV_FOR_R, IRQF_TRIGGER_RISING,
2721 pm8921_bms_ocv_for_r_handler),
2722 BMS_IRQ(PM8921_BMS_GOOD_OCV, IRQF_TRIGGER_RISING,
2723 pm8921_bms_good_ocv_handler),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002724};
2725
2726static void free_irqs(struct pm8921_bms_chip *chip)
2727{
2728 int i;
2729
2730 for (i = 0; i < PM_BMS_MAX_INTS; i++)
2731 if (chip->pmic_bms_irq[i]) {
2732 free_irq(chip->pmic_bms_irq[i], NULL);
2733 chip->pmic_bms_irq[i] = 0;
2734 }
2735}
2736
2737static int __devinit request_irqs(struct pm8921_bms_chip *chip,
2738 struct platform_device *pdev)
2739{
2740 struct resource *res;
2741 int ret, i;
2742
2743 ret = 0;
2744 bitmap_fill(chip->enabled_irqs, PM_BMS_MAX_INTS);
2745
2746 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
2747 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
2748 bms_irq_data[i].name);
2749 if (res == NULL) {
2750 pr_err("couldn't find %s\n", bms_irq_data[i].name);
2751 goto err_out;
2752 }
2753 ret = request_irq(res->start, bms_irq_data[i].handler,
2754 bms_irq_data[i].flags,
2755 bms_irq_data[i].name, chip);
2756 if (ret < 0) {
2757 pr_err("couldn't request %d (%s) %d\n", res->start,
2758 bms_irq_data[i].name, ret);
2759 goto err_out;
2760 }
2761 chip->pmic_bms_irq[bms_irq_data[i].irq_id] = res->start;
2762 pm8921_bms_disable_irq(chip, bms_irq_data[i].irq_id);
2763 }
2764 return 0;
2765
2766err_out:
2767 free_irqs(chip);
2768 return -EINVAL;
2769}
2770
2771#define EN_BMS_BIT BIT(7)
2772#define EN_PON_HS_BIT BIT(0)
2773static int __devinit pm8921_bms_hw_init(struct pm8921_bms_chip *chip)
2774{
2775 int rc;
2776
2777 rc = pm_bms_masked_write(chip, BMS_CONTROL,
2778 EN_BMS_BIT | EN_PON_HS_BIT, EN_BMS_BIT | EN_PON_HS_BIT);
2779 if (rc) {
2780 pr_err("failed to enable pon and bms addr = %d %d",
2781 BMS_CONTROL, rc);
2782 }
2783
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -08002784 /* The charger will call start charge later if usb is present */
2785 pm_bms_masked_write(chip, BMS_TOLERANCES,
2786 IBAT_TOL_MASK, IBAT_TOL_NOCHG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002787 return 0;
2788}
2789
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002790static void check_initial_ocv(struct pm8921_bms_chip *chip)
2791{
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002792 int ocv_uv, rc;
2793 int16_t ocv_raw;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08002794 int usb_chg;
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002795
2796 /*
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07002797 * Check if a ocv is available in bms hw,
2798 * if not compute it here at boot time and save it
2799 * in the last_ocv_uv.
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002800 */
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002801 ocv_uv = 0;
2802 pm_bms_read_output_data(chip, LAST_GOOD_OCV_VALUE, &ocv_raw);
Abhijeet Dharmapurikar9e948982012-08-28 15:21:23 -07002803 usb_chg = usb_chg_plugged_in(chip);
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08002804 rc = convert_vbatt_raw_to_uv(chip, usb_chg, ocv_raw, &ocv_uv);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002805 if (rc || ocv_uv == 0) {
2806 rc = adc_based_ocv(chip, &ocv_uv);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07002807 if (rc) {
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002808 pr_err("failed to read adc based ocv_uv rc = %d\n", rc);
2809 ocv_uv = DEFAULT_OCV_MICROVOLTS;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07002810 }
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002811 }
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002812 chip->last_ocv_uv = ocv_uv;
2813 pr_debug("ocv_uv = %d last_ocv_uv = %d\n", ocv_uv, chip->last_ocv_uv);
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002814}
2815
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002816static int64_t read_battery_id(struct pm8921_bms_chip *chip)
2817{
2818 int rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07002819 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002820
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07002821 rc = pm8xxx_adc_read(chip->batt_id_channel, &result);
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002822 if (rc) {
2823 pr_err("error reading batt id channel = %d, rc = %d\n",
2824 chip->vbat_channel, rc);
2825 return rc;
2826 }
2827 pr_debug("batt_id phy = %lld meas = 0x%llx\n", result.physical,
2828 result.measurement);
David Keitel8f2601b2012-02-14 22:31:07 -08002829 return result.adc_code;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002830}
2831
David Keitel8f2601b2012-02-14 22:31:07 -08002832#define PALLADIUM_ID_MIN 0x7F40
2833#define PALLADIUM_ID_MAX 0x7F5A
2834#define DESAY_5200_ID_MIN 0x7F7F
2835#define DESAY_5200_ID_MAX 0x802F
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002836static int set_battery_data(struct pm8921_bms_chip *chip)
2837{
2838 int64_t battery_id;
2839
David Keitel35e11872012-02-17 17:40:42 -08002840 if (chip->batt_type == BATT_DESAY)
2841 goto desay;
2842 else if (chip->batt_type == BATT_PALLADIUM)
2843 goto palladium;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002844
David Keitel35e11872012-02-17 17:40:42 -08002845 battery_id = read_battery_id(chip);
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002846 if (battery_id < 0) {
2847 pr_err("cannot read battery id err = %lld\n", battery_id);
2848 return battery_id;
2849 }
2850
2851 if (is_between(PALLADIUM_ID_MIN, PALLADIUM_ID_MAX, battery_id)) {
David Keitel35e11872012-02-17 17:40:42 -08002852 goto palladium;
2853 } else if (is_between(DESAY_5200_ID_MIN, DESAY_5200_ID_MAX,
2854 battery_id)) {
2855 goto desay;
2856 } else {
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002857 pr_warn("invalid battid, palladium 1500 assumed batt_id %llx\n",
2858 battery_id);
2859 goto palladium;
David Keitel35e11872012-02-17 17:40:42 -08002860 }
2861
2862palladium:
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002863 chip->fcc = palladium_1500_data.fcc;
2864 chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut;
2865 chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut;
2866 chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut;
2867 chip->pc_sf_lut = palladium_1500_data.pc_sf_lut;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002868 chip->rbatt_sf_lut = palladium_1500_data.rbatt_sf_lut;
2869 chip->default_rbatt_mohm
2870 = palladium_1500_data.default_rbatt_mohm;
Abhijeet Dharmapurikarded189b2012-03-30 10:12:28 -07002871 chip->delta_rbatt_mohm = palladium_1500_data.delta_rbatt_mohm;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002872 return 0;
David Keitel35e11872012-02-17 17:40:42 -08002873desay:
David Keitel8f2601b2012-02-14 22:31:07 -08002874 chip->fcc = desay_5200_data.fcc;
2875 chip->fcc_temp_lut = desay_5200_data.fcc_temp_lut;
David Keitel8f2601b2012-02-14 22:31:07 -08002876 chip->pc_temp_ocv_lut = desay_5200_data.pc_temp_ocv_lut;
2877 chip->pc_sf_lut = desay_5200_data.pc_sf_lut;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002878 chip->rbatt_sf_lut = desay_5200_data.rbatt_sf_lut;
2879 chip->default_rbatt_mohm = desay_5200_data.default_rbatt_mohm;
Abhijeet Dharmapurikarded189b2012-03-30 10:12:28 -07002880 chip->delta_rbatt_mohm = desay_5200_data.delta_rbatt_mohm;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002881 return 0;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002882}
2883
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002884enum bms_request_operation {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002885 CALC_FCC,
2886 CALC_PC,
2887 CALC_SOC,
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002888 CALIB_HKADC,
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002889 CALIB_CCADC,
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002890 GET_VBAT_VSENSE_SIMULTANEOUS,
Abhijeet Dharmapurikarc99f5862012-03-14 19:33:16 -07002891 STOP_OCV,
2892 START_OCV,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002893};
2894
2895static int test_batt_temp = 5;
2896static int test_chargecycle = 150;
2897static int test_ocv = 3900000;
2898enum {
2899 TEST_BATT_TEMP,
2900 TEST_CHARGE_CYCLE,
2901 TEST_OCV,
2902};
2903static int get_test_param(void *data, u64 * val)
2904{
2905 switch ((int)data) {
2906 case TEST_BATT_TEMP:
2907 *val = test_batt_temp;
2908 break;
2909 case TEST_CHARGE_CYCLE:
2910 *val = test_chargecycle;
2911 break;
2912 case TEST_OCV:
2913 *val = test_ocv;
2914 break;
2915 default:
2916 return -EINVAL;
2917 }
2918 return 0;
2919}
2920static int set_test_param(void *data, u64 val)
2921{
2922 switch ((int)data) {
2923 case TEST_BATT_TEMP:
2924 test_batt_temp = (int)val;
2925 break;
2926 case TEST_CHARGE_CYCLE:
2927 test_chargecycle = (int)val;
2928 break;
2929 case TEST_OCV:
2930 test_ocv = (int)val;
2931 break;
2932 default:
2933 return -EINVAL;
2934 }
2935 return 0;
2936}
2937DEFINE_SIMPLE_ATTRIBUTE(temp_fops, get_test_param, set_test_param, "%llu\n");
2938
2939static int get_calc(void *data, u64 * val)
2940{
2941 int param = (int)data;
2942 int ret = 0;
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002943 int ibat_ua, vbat_uv;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002944 struct pm8921_soc_params raw;
2945
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08002946 read_soc_params_raw(the_chip, &raw, 300);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002947
2948 *val = 0;
2949
2950 /* global irq number passed in via data */
2951 switch (param) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002952 case CALC_FCC:
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002953 *val = calculate_fcc_uah(the_chip, test_batt_temp,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002954 test_chargecycle);
2955 break;
2956 case CALC_PC:
2957 *val = calculate_pc(the_chip, test_ocv, test_batt_temp,
2958 test_chargecycle);
2959 break;
2960 case CALC_SOC:
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002961 *val = calculate_state_of_charge(the_chip, &raw,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002962 test_batt_temp, test_chargecycle);
2963 break;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002964 case CALIB_HKADC:
2965 /* reading this will trigger calibration */
2966 *val = 0;
2967 calib_hkadc(the_chip);
2968 break;
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002969 case CALIB_CCADC:
2970 /* reading this will trigger calibration */
2971 *val = 0;
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08002972 pm8xxx_calib_ccadc();
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002973 break;
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002974 case GET_VBAT_VSENSE_SIMULTANEOUS:
2975 /* reading this will call simultaneous vbat and vsense */
2976 *val =
2977 pm8921_bms_get_simultaneous_battery_voltage_and_current(
2978 &ibat_ua,
2979 &vbat_uv);
Abhijeet Dharmapurikarc99f5862012-03-14 19:33:16 -07002980 default:
2981 ret = -EINVAL;
2982 }
2983 return ret;
2984}
2985
2986static int set_calc(void *data, u64 val)
2987{
2988 int param = (int)data;
2989 int ret = 0;
2990
2991 switch (param) {
2992 case STOP_OCV:
Abhijeet Dharmapurikar7df608c2012-12-26 21:28:43 -08002993 pm8921_bms_stop_ocv_updates();
Abhijeet Dharmapurikarc99f5862012-03-14 19:33:16 -07002994 break;
2995 case START_OCV:
Abhijeet Dharmapurikar7df608c2012-12-26 21:28:43 -08002996 pm8921_bms_start_ocv_updates();
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002997 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002998 default:
2999 ret = -EINVAL;
3000 }
3001 return ret;
3002}
Abhijeet Dharmapurikarc99f5862012-03-14 19:33:16 -07003003DEFINE_SIMPLE_ATTRIBUTE(calc_fops, get_calc, set_calc, "%llu\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003004
3005static int get_reading(void *data, u64 * val)
3006{
3007 int param = (int)data;
3008 int ret = 0;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08003009 struct pm8921_soc_params raw;
3010
Abhijeet Dharmapurikarf425d9a2012-10-01 16:40:18 -07003011 mutex_lock(&the_chip->bms_output_lock);
Abhijeet Dharmapurikarbe2271f2012-12-26 21:10:53 -08003012 read_soc_params_raw(the_chip, &raw, 300);
Abhijeet Dharmapurikarf425d9a2012-10-01 16:40:18 -07003013 mutex_unlock(&the_chip->bms_output_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003014
3015 *val = 0;
3016
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003017 switch (param) {
3018 case CC_MSB:
3019 case CC_LSB:
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08003020 *val = raw.cc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003021 break;
3022 case LAST_GOOD_OCV_VALUE:
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08003023 *val = raw.last_good_ocv_uv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003024 break;
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07003025 case VSENSE_AVG:
3026 read_vsense_avg(the_chip, (uint *)val);
3027 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003028 default:
3029 ret = -EINVAL;
3030 }
3031 return ret;
3032}
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07003033DEFINE_SIMPLE_ATTRIBUTE(reading_fops, get_reading, NULL, "%lld\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003034
3035static int get_rt_status(void *data, u64 * val)
3036{
3037 int i = (int)data;
3038 int ret;
3039
3040 /* global irq number passed in via data */
3041 ret = pm_bms_get_rt_status(the_chip, i);
3042 *val = ret;
3043 return 0;
3044}
3045DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n");
3046
3047static int get_reg(void *data, u64 * val)
3048{
3049 int addr = (int)data;
3050 int ret;
3051 u8 temp;
3052
3053 ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp);
3054 if (ret) {
3055 pr_err("pm8xxx_readb to %x value = %d errored = %d\n",
3056 addr, temp, ret);
3057 return -EAGAIN;
3058 }
3059 *val = temp;
3060 return 0;
3061}
3062
3063static int set_reg(void *data, u64 val)
3064{
3065 int addr = (int)data;
3066 int ret;
3067 u8 temp;
3068
3069 temp = (u8) val;
3070 ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp);
3071 if (ret) {
3072 pr_err("pm8xxx_writeb to %x value = %d errored = %d\n",
3073 addr, temp, ret);
3074 return -EAGAIN;
3075 }
3076 return 0;
3077}
3078DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
3079
3080static void create_debugfs_entries(struct pm8921_bms_chip *chip)
3081{
3082 int i;
3083
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08003084 chip->dent = debugfs_create_dir("pm8921-bms", NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003085
3086 if (IS_ERR(chip->dent)) {
3087 pr_err("pmic bms couldnt create debugfs dir\n");
3088 return;
3089 }
3090
3091 debugfs_create_file("BMS_CONTROL", 0644, chip->dent,
3092 (void *)BMS_CONTROL, &reg_fops);
3093 debugfs_create_file("BMS_OUTPUT0", 0644, chip->dent,
3094 (void *)BMS_OUTPUT0, &reg_fops);
3095 debugfs_create_file("BMS_OUTPUT1", 0644, chip->dent,
3096 (void *)BMS_OUTPUT1, &reg_fops);
3097 debugfs_create_file("BMS_TEST1", 0644, chip->dent,
3098 (void *)BMS_TEST1, &reg_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003099
3100 debugfs_create_file("test_batt_temp", 0644, chip->dent,
3101 (void *)TEST_BATT_TEMP, &temp_fops);
3102 debugfs_create_file("test_chargecycle", 0644, chip->dent,
3103 (void *)TEST_CHARGE_CYCLE, &temp_fops);
3104 debugfs_create_file("test_ocv", 0644, chip->dent,
3105 (void *)TEST_OCV, &temp_fops);
3106
3107 debugfs_create_file("read_cc", 0644, chip->dent,
3108 (void *)CC_MSB, &reading_fops);
3109 debugfs_create_file("read_last_good_ocv", 0644, chip->dent,
3110 (void *)LAST_GOOD_OCV_VALUE, &reading_fops);
3111 debugfs_create_file("read_vbatt_for_rbatt", 0644, chip->dent,
3112 (void *)VBATT_FOR_RBATT, &reading_fops);
3113 debugfs_create_file("read_vsense_for_rbatt", 0644, chip->dent,
3114 (void *)VSENSE_FOR_RBATT, &reading_fops);
3115 debugfs_create_file("read_ocv_for_rbatt", 0644, chip->dent,
3116 (void *)OCV_FOR_RBATT, &reading_fops);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07003117 debugfs_create_file("read_vsense_avg", 0644, chip->dent,
3118 (void *)VSENSE_AVG, &reading_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003119
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003120 debugfs_create_file("show_fcc", 0644, chip->dent,
3121 (void *)CALC_FCC, &calc_fops);
3122 debugfs_create_file("show_pc", 0644, chip->dent,
3123 (void *)CALC_PC, &calc_fops);
3124 debugfs_create_file("show_soc", 0644, chip->dent,
3125 (void *)CALC_SOC, &calc_fops);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07003126 debugfs_create_file("calib_hkadc", 0644, chip->dent,
3127 (void *)CALIB_HKADC, &calc_fops);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07003128 debugfs_create_file("calib_ccadc", 0644, chip->dent,
3129 (void *)CALIB_CCADC, &calc_fops);
Abhijeet Dharmapurikarc99f5862012-03-14 19:33:16 -07003130 debugfs_create_file("stop_ocv", 0644, chip->dent,
3131 (void *)STOP_OCV, &calc_fops);
3132 debugfs_create_file("start_ocv", 0644, chip->dent,
3133 (void *)START_OCV, &calc_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003134
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08003135 debugfs_create_file("simultaneous", 0644, chip->dent,
3136 (void *)GET_VBAT_VSENSE_SIMULTANEOUS, &calc_fops);
3137
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003138 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
3139 if (chip->pmic_bms_irq[bms_irq_data[i].irq_id])
3140 debugfs_create_file(bms_irq_data[i].name, 0444,
3141 chip->dent,
3142 (void *)bms_irq_data[i].irq_id,
3143 &rt_fops);
3144 }
3145}
3146
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -07003147#define REG_SBI_CONFIG 0x04F
3148#define PAGE3_ENABLE_MASK 0x6
3149#define PROGRAM_REV_MASK 0x0F
3150#define PROGRAM_REV 0x9
3151static int read_ocv_trim(struct pm8921_bms_chip *chip)
3152{
3153 int rc;
3154 u8 reg, sbi_config;
3155
3156 rc = pm8xxx_readb(chip->dev->parent, REG_SBI_CONFIG, &sbi_config);
3157 if (rc) {
3158 pr_err("error = %d reading sbi config reg\n", rc);
3159 return rc;
3160 }
3161
3162 reg = sbi_config | PAGE3_ENABLE_MASK;
3163 rc = pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, reg);
3164 if (rc) {
3165 pr_err("error = %d writing sbi config reg\n", rc);
3166 return rc;
3167 }
3168
3169 rc = pm8xxx_readb(chip->dev->parent, TEST_PROGRAM_REV, &reg);
3170 if (rc)
3171 pr_err("Error %d reading %d addr %d\n",
3172 rc, reg, TEST_PROGRAM_REV);
3173 pr_err("program rev reg is 0x%x\n", reg);
3174 reg &= PROGRAM_REV_MASK;
3175
3176 /* If the revision is equal or higher do not adjust trim delta */
3177 if (reg >= PROGRAM_REV) {
3178 chip->amux_2_trim_delta = 0;
3179 goto restore_sbi_config;
3180 }
3181
3182 rc = pm8xxx_readb(chip->dev->parent, AMUX_TRIM_2, &reg);
3183 if (rc) {
3184 pr_err("error = %d reading trim reg\n", rc);
3185 return rc;
3186 }
3187
3188 pr_err("trim reg is 0x%x\n", reg);
3189 chip->amux_2_trim_delta = abs(0x49 - reg);
3190 pr_err("trim delta is %d\n", chip->amux_2_trim_delta);
3191
3192restore_sbi_config:
3193 rc = pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, sbi_config);
3194 if (rc) {
3195 pr_err("error = %d writing sbi config reg\n", rc);
3196 return rc;
3197 }
3198
3199 return 0;
3200}
3201
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003202static int __devinit pm8921_bms_probe(struct platform_device *pdev)
3203{
3204 int rc = 0;
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07003205 int vbatt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003206 struct pm8921_bms_chip *chip;
3207 const struct pm8921_bms_platform_data *pdata
3208 = pdev->dev.platform_data;
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07003209
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003210 if (!pdata) {
3211 pr_err("missing platform data\n");
3212 return -EINVAL;
3213 }
3214
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07003215 chip = kzalloc(sizeof(struct pm8921_bms_chip), GFP_KERNEL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003216 if (!chip) {
3217 pr_err("Cannot allocate pm_bms_chip\n");
3218 return -ENOMEM;
3219 }
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07003220
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08003221 mutex_init(&chip->bms_output_lock);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07003222 mutex_init(&chip->last_ocv_uv_mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003223 chip->dev = &pdev->dev;
Xiaozhe Shid69c91e2012-11-06 10:00:38 -08003224 chip->r_sense_uohm = pdata->r_sense_uohm;
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07003225 chip->v_cutoff = pdata->v_cutoff;
Abhijeet Dharmapurikara93ede82011-11-17 12:20:03 -08003226 chip->max_voltage_uv = pdata->max_voltage_uv;
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07003227 chip->chg_term_ua = pdata->chg_term_ua;
David Keitel35e11872012-02-17 17:40:42 -08003228 chip->batt_type = pdata->battery_type;
Abhijeet Dharmapurikarbaffba42012-03-22 14:41:10 -07003229 chip->rconn_mohm = pdata->rconn_mohm;
Abhijeet Dharmapurikardba91f42011-12-21 16:56:13 -08003230 chip->start_percent = -EINVAL;
3231 chip->end_percent = -EINVAL;
Xiaozhe Shid28e4972012-12-04 15:13:32 -08003232 chip->last_cc_uah = INT_MIN;
Abhijeet Dharmapurikar61275b92012-12-27 17:20:29 -08003233 chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED;
3234 chip->prev_last_good_ocv_raw = OCV_RAW_UNINITIALIZED;
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07003235 chip->shutdown_soc_valid_limit = pdata->shutdown_soc_valid_limit;
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -07003236 chip->adjust_soc_low_threshold = pdata->adjust_soc_low_threshold;
Abhijeet Dharmapurikar683472d2012-10-30 16:21:58 -07003237
3238 chip->normal_voltage_calc_ms = pdata->normal_voltage_calc_ms;
3239 chip->low_voltage_calc_ms = pdata->low_voltage_calc_ms;
3240
3241 chip->soc_calc_period = pdata->normal_voltage_calc_ms;
3242
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -07003243 if (chip->adjust_soc_low_threshold >= 45)
3244 chip->adjust_soc_low_threshold = 45;
3245
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07003246 chip->prev_pc_unusable = -EINVAL;
Abhijeet Dharmapurikard3c435e2012-08-16 19:23:18 -07003247 chip->soc_at_cv = -EINVAL;
Abhijeet Dharmapurikareca7b3e2012-12-26 21:30:29 -08003248 chip->imax_ua = -EINVAL;
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07003249
Abhijeet Dharmapurikar1f4cc512012-07-21 00:10:39 -07003250 chip->ignore_shutdown_soc = pdata->ignore_shutdown_soc;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07003251 rc = set_battery_data(chip);
3252 if (rc) {
3253 pr_err("%s bad battery data %d\n", __func__, rc);
Abhijeet Dharmapurikar4a4456e2011-09-27 15:10:38 -07003254 goto free_chip;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07003255 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003256
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08003257 if (chip->pc_temp_ocv_lut == NULL) {
3258 pr_err("temp ocv lut table is NULL\n");
3259 rc = -EINVAL;
3260 goto free_chip;
3261 }
3262
3263 /* set defaults in the battery data */
3264 if (chip->default_rbatt_mohm <= 0)
3265 chip->default_rbatt_mohm = DEFAULT_RBATT_MOHMS;
3266
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07003267 chip->batt_temp_channel = pdata->bms_cdata.batt_temp_channel;
3268 chip->vbat_channel = pdata->bms_cdata.vbat_channel;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07003269 chip->ref625mv_channel = pdata->bms_cdata.ref625mv_channel;
3270 chip->ref1p25v_channel = pdata->bms_cdata.ref1p25v_channel;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07003271 chip->batt_id_channel = pdata->bms_cdata.batt_id_channel;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -07003272 chip->revision = pm8xxx_get_revision(chip->dev->parent);
Abhijeet Dharmapurikare88f2462012-04-25 18:22:38 -07003273 chip->enable_fcc_learning = pdata->enable_fcc_learning;
Abhijeet Dharmapurikar9c0e6042012-07-12 14:55:13 -07003274
Abhijeet Dharmapurikar1939ede2013-01-10 15:59:33 -08003275 chip->disable_flat_portion_ocv = pdata->disable_flat_portion_ocv;
3276 chip->ocv_dis_high_soc = pdata->ocv_dis_high_soc;
3277 chip->ocv_dis_low_soc = pdata->ocv_dis_low_soc;
3278
Anirudh Ghayal6fe4ffa2013-02-01 11:44:33 +05303279 chip->alarm_low_mv = pdata->alarm_low_mv;
3280 chip->alarm_high_mv = pdata->alarm_high_mv;
3281
Abhijeet Dharmapurikar9c0e6042012-07-12 14:55:13 -07003282 mutex_init(&chip->calib_mutex);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07003283 INIT_WORK(&chip->calib_hkadc_work, calibrate_hkadc_work);
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07003284
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07003285 INIT_DELAYED_WORK(&chip->calculate_soc_delayed_work,
3286 calculate_soc_work);
3287
Abhijeet Dharmapurikarb2aeb7e2012-12-26 21:30:05 -08003288 wake_lock_init(&chip->soc_wake_lock,
3289 WAKE_LOCK_SUSPEND, "pm8921_soc_lock");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003290 rc = request_irqs(chip, pdev);
3291 if (rc) {
3292 pr_err("couldn't register interrupts rc = %d\n", rc);
Abhijeet Dharmapurikarb2aeb7e2012-12-26 21:30:05 -08003293 goto destroy_soc_wl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003294 }
3295
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07003296 wake_lock_init(&chip->low_voltage_wake_lock,
3297 WAKE_LOCK_SUSPEND, "pm8921_bms_low");
3298
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07003299 rc = pm8921_bms_hw_init(chip);
3300 if (rc) {
3301 pr_err("couldn't init hardware rc = %d\n", rc);
3302 goto free_irqs;
3303 }
3304
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07003305 read_shutdown_soc_and_iavg(chip);
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07003306
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003307 platform_set_drvdata(pdev, chip);
3308 the_chip = chip;
3309 create_debugfs_entries(chip);
3310
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -07003311 rc = read_ocv_trim(chip);
3312 if (rc) {
3313 pr_err("couldn't adjust ocv_trim rc= %d\n", rc);
3314 goto free_irqs;
3315 }
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07003316 check_initial_ocv(chip);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07003317
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07003318 /* enable the vbatt reading interrupts for scheduling hkadc calib */
3319 pm8921_bms_enable_irq(chip, PM8921_BMS_GOOD_OCV);
3320 pm8921_bms_enable_irq(chip, PM8921_BMS_OCV_FOR_R);
Abhijeet Dharmapurikar21ebadd2011-09-28 21:14:16 -07003321
Anirudh Ghayal6fe4ffa2013-02-01 11:44:33 +05303322 rc = pm8921_bms_configure_batt_alarm(chip);
3323 if (rc) {
3324 pr_err("Couldn't configure battery alarm! rc=%d\n", rc);
3325 goto free_irqs;
3326 }
3327
3328 rc = pm8921_bms_enable_batt_alarm(chip);
3329 if (rc) {
3330 pr_err("Couldn't enable battery alarm! rc=%d\n", rc);
3331 goto free_irqs;
3332 }
3333
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07003334 calculate_soc_work(&(chip->calculate_soc_delayed_work.work));
3335
Anirudh Ghayal3a38a382012-12-05 10:42:17 +05303336 rc = get_battery_uvolts(chip, &vbatt);
3337 if (!rc)
3338 pr_info("OK battery_capacity_at_boot=%d volt = %d ocv = %d\n",
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07003339 pm8921_bms_get_percent_charge(),
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07003340 vbatt, chip->last_ocv_uv);
Anirudh Ghayal3a38a382012-12-05 10:42:17 +05303341 else
3342 pr_info("Unable to read battery voltage at boot\n");
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -07003343
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003344 return 0;
3345
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07003346free_irqs:
Abhijeet Dharmapurikarb2aeb7e2012-12-26 21:30:05 -08003347 wake_lock_destroy(&chip->low_voltage_wake_lock);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07003348 free_irqs(chip);
Abhijeet Dharmapurikarb2aeb7e2012-12-26 21:30:05 -08003349destroy_soc_wl:
3350 wake_lock_destroy(&chip->soc_wake_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003351free_chip:
3352 kfree(chip);
3353 return rc;
3354}
3355
3356static int __devexit pm8921_bms_remove(struct platform_device *pdev)
3357{
3358 struct pm8921_bms_chip *chip = platform_get_drvdata(pdev);
3359
3360 free_irqs(chip);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07003361 kfree(chip->adjusted_fcc_temp_lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003362 platform_set_drvdata(pdev, NULL);
3363 the_chip = NULL;
3364 kfree(chip);
3365 return 0;
3366}
3367
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07003368static int pm8921_bms_resume(struct device *dev)
3369{
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08003370 int rc;
3371 unsigned long time_since_last_recalc;
3372 unsigned long tm_now_sec;
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07003373
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08003374 rc = get_current_time(&tm_now_sec);
3375 if (rc) {
3376 pr_err("Could not read current time: %d\n", rc);
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07003377 return 0;
3378 }
Xiaozhe Shib2acb9f2012-12-18 15:10:18 -08003379 if (tm_now_sec > the_chip->last_recalc_time) {
3380 time_since_last_recalc = tm_now_sec -
3381 the_chip->last_recalc_time;
3382 pr_debug("Time since last recalc: %lu\n",
3383 time_since_last_recalc);
3384 if (time_since_last_recalc >= the_chip->soc_calc_period) {
3385 the_chip->last_recalc_time = tm_now_sec;
3386 recalculate_soc(the_chip);
3387 }
3388 }
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07003389
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07003390 return 0;
3391}
3392
3393static const struct dev_pm_ops pm8921_bms_pm_ops = {
3394 .resume = pm8921_bms_resume,
3395};
3396
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003397static struct platform_driver pm8921_bms_driver = {
3398 .probe = pm8921_bms_probe,
3399 .remove = __devexit_p(pm8921_bms_remove),
3400 .driver = {
3401 .name = PM8921_BMS_DEV_NAME,
3402 .owner = THIS_MODULE,
Abhijeet Dharmapurikard1c761d2012-08-14 19:15:53 -07003403 .pm = &pm8921_bms_pm_ops,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003404 },
3405};
3406
3407static int __init pm8921_bms_init(void)
3408{
3409 return platform_driver_register(&pm8921_bms_driver);
3410}
3411
3412static void __exit pm8921_bms_exit(void)
3413{
3414 platform_driver_unregister(&pm8921_bms_driver);
3415}
3416
3417late_initcall(pm8921_bms_init);
3418module_exit(pm8921_bms_exit);
3419
3420MODULE_LICENSE("GPL v2");
3421MODULE_DESCRIPTION("PMIC8921 bms driver");
3422MODULE_VERSION("1.0");
3423MODULE_ALIAS("platform:" PM8921_BMS_DEV_NAME);