blob: 90f48f4edc20b0548872bd7b3bc2ae3e66a04aee [file] [log] [blame]
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001/* Copyright (c) 2011-2012, Code Aurora Forum. 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 */
13#define pr_fmt(fmt) "%s: " fmt, __func__
14
15#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 Dharmapurikar82d93982011-11-09 15:52:25 -080023#include <linux/mfd/pm8xxx/ccadc.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070024#include <linux/interrupt.h>
25#include <linux/bitops.h>
26#include <linux/debugfs.h>
27#include <linux/slab.h>
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070028#include <linux/delay.h>
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -080029#include <linux/mutex.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030
31#define BMS_CONTROL 0x224
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -080032#define BMS_S1_DELAY 0x225
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033#define BMS_OUTPUT0 0x230
34#define BMS_OUTPUT1 0x231
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -080035#define BMS_TOLERANCES 0x232
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036#define BMS_TEST1 0x237
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070038#define ADC_ARB_SECP_CNTRL 0x190
39#define ADC_ARB_SECP_AMUX_CNTRL 0x191
40#define ADC_ARB_SECP_ANA_PARAM 0x192
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070041#define ADC_ARB_SECP_DIG_PARAM 0x193
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070042#define ADC_ARB_SECP_RSV 0x194
43#define ADC_ARB_SECP_DATA1 0x195
44#define ADC_ARB_SECP_DATA0 0x196
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070046#define ADC_ARB_BMS_CNTRL 0x18D
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -070047#define AMUX_TRIM_2 0x322
48#define TEST_PROGRAM_REV 0x339
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070049
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050enum pmic_bms_interrupts {
51 PM8921_BMS_SBI_WRITE_OK,
52 PM8921_BMS_CC_THR,
53 PM8921_BMS_VSENSE_THR,
54 PM8921_BMS_VSENSE_FOR_R,
55 PM8921_BMS_OCV_FOR_R,
56 PM8921_BMS_GOOD_OCV,
57 PM8921_BMS_VSENSE_AVG,
58 PM_BMS_MAX_INTS,
59};
60
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080061struct pm8921_soc_params {
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -080062 uint16_t last_good_ocv_raw;
63 int cc;
64
65 int last_good_ocv_uv;
66};
67
68struct pm8921_rbatt_params {
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080069 uint16_t ocv_for_rbatt_raw;
70 uint16_t vsense_for_rbatt_raw;
71 uint16_t vbatt_for_rbatt_raw;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080072
73 int ocv_for_rbatt_uv;
74 int vsense_for_rbatt_uv;
75 int vbatt_for_rbatt_uv;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080076};
77
78/**
79 * struct pm8921_bms_chip -
80 * @bms_output_lock: lock to prevent concurrent bms reads
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080081 *
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -070082 * @last_ocv_uv_mutex: mutex to protect simultaneous invocations of calculate
83 * state of charge, note that last_ocv_uv could be
84 * changed as soc is adjusted. This mutex protects
85 * simultaneous updates of last_ocv_uv as well. This mutex
86 * also protects changes to *_at_100 variables used in
87 * faking 100% SOC.
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080088 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070089struct pm8921_bms_chip {
90 struct device *dev;
91 struct dentry *dent;
92 unsigned int r_sense;
93 unsigned int i_test;
94 unsigned int v_failure;
95 unsigned int fcc;
96 struct single_row_lut *fcc_temp_lut;
97 struct single_row_lut *fcc_sf_lut;
98 struct pc_temp_ocv_lut *pc_temp_ocv_lut;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -080099 struct sf_lut *pc_sf_lut;
100 struct sf_lut *rbatt_sf_lut;
Abhijeet Dharmapurikarded189b2012-03-30 10:12:28 -0700101 int delta_rbatt_mohm;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700102 struct work_struct calib_hkadc_work;
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -0700103 struct delayed_work calib_ccadc_work;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104 unsigned int calib_delay_ms;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700105 unsigned int revision;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800106 unsigned int xoadc_v0625_usb_present;
107 unsigned int xoadc_v0625_usb_absent;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700108 unsigned int xoadc_v0625;
109 unsigned int xoadc_v125;
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700110 unsigned int batt_temp_channel;
111 unsigned int vbat_channel;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700112 unsigned int ref625mv_channel;
113 unsigned int ref1p25v_channel;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -0700114 unsigned int batt_id_channel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700115 unsigned int pmic_bms_irq[PM_BMS_MAX_INTS];
116 DECLARE_BITMAP(enabled_irqs, PM_BMS_MAX_INTS);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800117 struct mutex bms_output_lock;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700118 struct single_row_lut *adjusted_fcc_temp_lut;
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -0700119 unsigned int charging_began;
120 unsigned int start_percent;
121 unsigned int end_percent;
David Keitel35e11872012-02-17 17:40:42 -0800122 enum battery_type batt_type;
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -0800123 uint16_t ocv_reading_at_100;
124 int cc_reading_at_100;
Abhijeet Dharmapurikara93ede82011-11-17 12:20:03 -0800125 int max_voltage_uv;
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800126
127 int batt_temp_suspend;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800128 int soc_rbatt_suspend;
129 int default_rbatt_mohm;
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700130 int amux_2_trim_delta;
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -0700131 uint16_t prev_last_good_ocv_raw;
Abhijeet Dharmapurikarbaffba42012-03-22 14:41:10 -0700132 unsigned int rconn_mohm;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -0700133 struct mutex last_ocv_uv_mutex;
134 int last_ocv_uv;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -0700135 int last_cc_uah; /* used for Iavg calc for UUC */
136 struct timeval t;
137 int last_uuc_uah;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138};
139
140static struct pm8921_bms_chip *the_chip;
141
142#define DEFAULT_RBATT_MOHMS 128
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143#define DEFAULT_OCV_MICROVOLTS 3900000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700144#define DEFAULT_CHARGE_CYCLES 0
145
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800146static int last_usb_cal_delta_uv = 1800;
147module_param(last_usb_cal_delta_uv, int, 0644);
148
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700149static int last_chargecycles = DEFAULT_CHARGE_CYCLES;
150static int last_charge_increase;
151module_param(last_chargecycles, int, 0644);
152module_param(last_charge_increase, int, 0644);
153
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700154static int last_rbatt = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700155static int last_soc = -EINVAL;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800156static int last_real_fcc_mah = -EINVAL;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700157static int last_real_fcc_batt_temp = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700158
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700159static int bms_ops_set(const char *val, const struct kernel_param *kp)
160{
161 if (*(int *)kp->arg == -EINVAL)
162 return param_set_int(val, kp);
163 else
164 return 0;
165}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700167static struct kernel_param_ops bms_param_ops = {
168 .set = bms_ops_set,
169 .get = param_get_int,
170};
171
172module_param_cb(last_rbatt, &bms_param_ops, &last_rbatt, 0644);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700173module_param_cb(last_soc, &bms_param_ops, &last_soc, 0644);
174
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -0800175/*
Abhijeet Dharmapurikar986bd8a2012-01-05 15:15:15 -0800176 * bms_fake_battery is set in setups where a battery emulator is used instead
177 * of a real battery. This makes the bms driver report a different/fake value
178 * regardless of the calculated state of charge.
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -0800179 */
Abhijeet Dharmapurikar986bd8a2012-01-05 15:15:15 -0800180static int bms_fake_battery = -EINVAL;
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -0800181module_param(bms_fake_battery, int, 0644);
182
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800183/* bms_start_XXX and bms_end_XXX are read only */
184static int bms_start_percent;
185static int bms_start_ocv_uv;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800186static int bms_start_cc_uah;
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800187static int bms_end_percent;
188static int bms_end_ocv_uv;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800189static int bms_end_cc_uah;
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800190
191static int bms_ro_ops_set(const char *val, const struct kernel_param *kp)
192{
193 return -EINVAL;
194}
195
196static struct kernel_param_ops bms_ro_param_ops = {
197 .set = bms_ro_ops_set,
198 .get = param_get_int,
199};
200module_param_cb(bms_start_percent, &bms_ro_param_ops, &bms_start_percent, 0644);
201module_param_cb(bms_start_ocv_uv, &bms_ro_param_ops, &bms_start_ocv_uv, 0644);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800202module_param_cb(bms_start_cc_uah, &bms_ro_param_ops, &bms_start_cc_uah, 0644);
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800203
204module_param_cb(bms_end_percent, &bms_ro_param_ops, &bms_end_percent, 0644);
205module_param_cb(bms_end_ocv_uv, &bms_ro_param_ops, &bms_end_ocv_uv, 0644);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800206module_param_cb(bms_end_cc_uah, &bms_ro_param_ops, &bms_end_cc_uah, 0644);
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800207
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700208static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp);
209static void readjust_fcc_table(void)
210{
211 struct single_row_lut *temp, *old;
212 int i, fcc, ratio;
213
214 if (!the_chip->fcc_temp_lut) {
215 pr_err("The static fcc lut table is NULL\n");
216 return;
217 }
218
219 temp = kzalloc(sizeof(struct single_row_lut), GFP_KERNEL);
220 if (!temp) {
221 pr_err("Cannot allocate memory for adjusted fcc table\n");
222 return;
223 }
224
225 fcc = interpolate_fcc(the_chip, last_real_fcc_batt_temp);
226
227 temp->cols = the_chip->fcc_temp_lut->cols;
228 for (i = 0; i < the_chip->fcc_temp_lut->cols; i++) {
229 temp->x[i] = the_chip->fcc_temp_lut->x[i];
230 ratio = div_u64(the_chip->fcc_temp_lut->y[i] * 1000, fcc);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800231 temp->y[i] = (ratio * last_real_fcc_mah);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700232 temp->y[i] /= 1000;
233 pr_debug("temp=%d, staticfcc=%d, adjfcc=%d, ratio=%d\n",
234 temp->x[i], the_chip->fcc_temp_lut->y[i],
235 temp->y[i], ratio);
236 }
237
238 old = the_chip->adjusted_fcc_temp_lut;
239 the_chip->adjusted_fcc_temp_lut = temp;
240 kfree(old);
241}
242
243static int bms_last_real_fcc_set(const char *val,
244 const struct kernel_param *kp)
245{
246 int rc = 0;
247
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800248 if (last_real_fcc_mah == -EINVAL)
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700249 rc = param_set_int(val, kp);
250 if (rc) {
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800251 pr_err("Failed to set last_real_fcc_mah rc=%d\n", rc);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700252 return rc;
253 }
254 if (last_real_fcc_batt_temp != -EINVAL)
255 readjust_fcc_table();
256 return rc;
257}
258static struct kernel_param_ops bms_last_real_fcc_param_ops = {
259 .set = bms_last_real_fcc_set,
260 .get = param_get_int,
261};
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800262module_param_cb(last_real_fcc_mah, &bms_last_real_fcc_param_ops,
263 &last_real_fcc_mah, 0644);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700264
265static int bms_last_real_fcc_batt_temp_set(const char *val,
266 const struct kernel_param *kp)
267{
268 int rc = 0;
269
270 if (last_real_fcc_batt_temp == -EINVAL)
271 rc = param_set_int(val, kp);
272 if (rc) {
273 pr_err("Failed to set last_real_fcc_batt_temp rc=%d\n", rc);
274 return rc;
275 }
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800276 if (last_real_fcc_mah != -EINVAL)
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700277 readjust_fcc_table();
278 return rc;
279}
280
281static struct kernel_param_ops bms_last_real_fcc_batt_temp_param_ops = {
282 .set = bms_last_real_fcc_batt_temp_set,
283 .get = param_get_int,
284};
285module_param_cb(last_real_fcc_batt_temp, &bms_last_real_fcc_batt_temp_param_ops,
286 &last_real_fcc_batt_temp, 0644);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287
288static int pm_bms_get_rt_status(struct pm8921_bms_chip *chip, int irq_id)
289{
290 return pm8xxx_read_irq_stat(chip->dev->parent,
291 chip->pmic_bms_irq[irq_id]);
292}
293
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700294static void pm8921_bms_enable_irq(struct pm8921_bms_chip *chip, int interrupt)
295{
296 if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) {
297 dev_dbg(chip->dev, "%s %d\n", __func__,
298 chip->pmic_bms_irq[interrupt]);
299 enable_irq(chip->pmic_bms_irq[interrupt]);
300 }
301}
302
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303static void pm8921_bms_disable_irq(struct pm8921_bms_chip *chip, int interrupt)
304{
305 if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700306 pr_debug("%d\n", chip->pmic_bms_irq[interrupt]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700307 disable_irq_nosync(chip->pmic_bms_irq[interrupt]);
308 }
309}
310
311static int pm_bms_masked_write(struct pm8921_bms_chip *chip, u16 addr,
312 u8 mask, u8 val)
313{
314 int rc;
315 u8 reg;
316
317 rc = pm8xxx_readb(chip->dev->parent, addr, &reg);
318 if (rc) {
319 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
320 return rc;
321 }
322 reg &= ~mask;
323 reg |= val & mask;
324 rc = pm8xxx_writeb(chip->dev->parent, addr, reg);
325 if (rc) {
326 pr_err("write failed addr = %03X, rc = %d\n", addr, rc);
327 return rc;
328 }
329 return 0;
330}
331
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800332static int usb_chg_plugged_in(void)
333{
334 union power_supply_propval ret = {0,};
335 static struct power_supply *psy;
336
337 if (psy == NULL) {
338 psy = power_supply_get_by_name("usb");
339 if (psy == NULL)
340 return 0;
341 }
342
343 if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
344 return 0;
345
346 return ret.intval;
347}
348
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700349#define HOLD_OREG_DATA BIT(1)
350static int pm_bms_lock_output_data(struct pm8921_bms_chip *chip)
351{
352 int rc;
353
354 rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA,
355 HOLD_OREG_DATA);
356 if (rc) {
357 pr_err("couldnt lock bms output rc = %d\n", rc);
358 return rc;
359 }
360 return 0;
361}
362
363static int pm_bms_unlock_output_data(struct pm8921_bms_chip *chip)
364{
365 int rc;
366
367 rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA, 0);
368 if (rc) {
369 pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc);
370 return rc;
371 }
372 return 0;
373}
374
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700375#define SELECT_OUTPUT_DATA 0x1C
376#define SELECT_OUTPUT_TYPE_SHIFT 2
377#define OCV_FOR_RBATT 0x0
378#define VSENSE_FOR_RBATT 0x1
379#define VBATT_FOR_RBATT 0x2
380#define CC_MSB 0x3
381#define CC_LSB 0x4
382#define LAST_GOOD_OCV_VALUE 0x5
383#define VSENSE_AVG 0x6
384#define VBATT_AVG 0x7
385
386static int pm_bms_read_output_data(struct pm8921_bms_chip *chip, int type,
387 int16_t *result)
388{
389 int rc;
390 u8 reg;
391
392 if (!result) {
393 pr_err("result pointer null\n");
394 return -EINVAL;
395 }
396 *result = 0;
397 if (type < OCV_FOR_RBATT || type > VBATT_AVG) {
398 pr_err("invalid type %d asked to read\n", type);
399 return -EINVAL;
400 }
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700401
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700402 rc = pm_bms_masked_write(chip, BMS_CONTROL, SELECT_OUTPUT_DATA,
403 type << SELECT_OUTPUT_TYPE_SHIFT);
404 if (rc) {
405 pr_err("fail to select %d type in BMS_CONTROL rc = %d\n",
406 type, rc);
407 return rc;
408 }
409
410 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT0, &reg);
411 if (rc) {
412 pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n",
413 type, rc);
414 return rc;
415 }
416 *result = reg;
417 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT1, &reg);
418 if (rc) {
419 pr_err("fail to read BMS_OUTPUT1 for type %d rc = %d\n",
420 type, rc);
421 return rc;
422 }
423 *result |= reg << 8;
424 pr_debug("type %d result %x", type, *result);
425 return 0;
426}
427
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700428#define V_PER_BIT_MUL_FACTOR 97656
429#define V_PER_BIT_DIV_FACTOR 1000
430#define XOADC_INTRINSIC_OFFSET 0x6000
431static int xoadc_reading_to_microvolt(unsigned int a)
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700432{
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700433 if (a <= XOADC_INTRINSIC_OFFSET)
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700434 return 0;
435
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700436 return (a - XOADC_INTRINSIC_OFFSET)
437 * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR;
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700438}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700440#define XOADC_CALIB_UV 625000
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700441#define VBATT_MUL_FACTOR 3
442static int adjust_xo_vbatt_reading(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800443 int usb_chg, unsigned int uv)
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700444{
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800445 s64 numerator, denominator;
446 int local_delta;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700447
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700448 if (uv == 0)
449 return 0;
450
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800451 /* dont adjust if not calibrated */
452 if (chip->xoadc_v0625 == 0 || chip->xoadc_v125 == 0) {
453 pr_debug("No cal yet return %d\n", VBATT_MUL_FACTOR * uv);
454 return VBATT_MUL_FACTOR * uv;
455 }
456
457 if (usb_chg)
458 local_delta = last_usb_cal_delta_uv;
459 else
460 local_delta = 0;
461
462 pr_debug("using delta = %d\n", local_delta);
463 numerator = ((s64)uv - chip->xoadc_v0625 - local_delta)
464 * XOADC_CALIB_UV;
465 denominator = (s64)chip->xoadc_v125 - chip->xoadc_v0625 - local_delta;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700466 if (denominator == 0)
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700467 return uv * VBATT_MUL_FACTOR;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800468 return (XOADC_CALIB_UV + local_delta + div_s64(numerator, denominator))
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700469 * VBATT_MUL_FACTOR;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700470}
471
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700472#define CC_RESOLUTION_N_V1 1085069
473#define CC_RESOLUTION_D_V1 100000
474#define CC_RESOLUTION_N_V2 868056
475#define CC_RESOLUTION_D_V2 10000
476static s64 cc_to_microvolt_v1(s64 cc)
477{
478 return div_s64(cc * CC_RESOLUTION_N_V1, CC_RESOLUTION_D_V1);
479}
480
481static s64 cc_to_microvolt_v2(s64 cc)
482{
483 return div_s64(cc * CC_RESOLUTION_N_V2, CC_RESOLUTION_D_V2);
484}
485
486static s64 cc_to_microvolt(struct pm8921_bms_chip *chip, s64 cc)
487{
488 /*
489 * resolution (the value of a single bit) was changed after revision 2.0
490 * for more accurate readings
491 */
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -0700492 return (chip->revision < PM8XXX_REVISION_8921_2p0) ?
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700493 cc_to_microvolt_v1((s64)cc) :
494 cc_to_microvolt_v2((s64)cc);
495}
496
497#define CC_READING_TICKS 55
498#define SLEEP_CLK_HZ 32768
499#define SECONDS_PER_HOUR 3600
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800500/**
501 * ccmicrovolt_to_nvh -
502 * @cc_uv: coulumb counter converted to uV
503 *
504 * RETURNS: coulumb counter based charge in nVh
505 * (nano Volt Hour)
506 */
507static s64 ccmicrovolt_to_nvh(s64 cc_uv)
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700508{
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800509 return div_s64(cc_uv * CC_READING_TICKS * 1000,
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700510 SLEEP_CLK_HZ * SECONDS_PER_HOUR);
511}
512
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513/* returns the signed value read from the hardware */
514static int read_cc(struct pm8921_bms_chip *chip, int *result)
515{
516 int rc;
517 uint16_t msw, lsw;
518
519 rc = pm_bms_read_output_data(chip, CC_LSB, &lsw);
520 if (rc) {
521 pr_err("fail to read CC_LSB rc = %d\n", rc);
522 return rc;
523 }
524 rc = pm_bms_read_output_data(chip, CC_MSB, &msw);
525 if (rc) {
526 pr_err("fail to read CC_MSB rc = %d\n", rc);
527 return rc;
528 }
529 *result = msw << 16 | lsw;
530 pr_debug("msw = %04x lsw = %04x cc = %d\n", msw, lsw, *result);
531 return 0;
532}
533
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700534static int adjust_xo_vbatt_reading_for_mbg(struct pm8921_bms_chip *chip,
535 int result)
536{
537 int64_t numerator;
538 int64_t denominator;
539
540 if (chip->amux_2_trim_delta == 0)
541 return result;
542
543 numerator = (s64)result * 1000000;
544 denominator = (1000000 + (410 * (s64)chip->amux_2_trim_delta));
545 return div_s64(numerator, denominator);
546}
547
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800548static int convert_vbatt_raw_to_uv(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800549 int usb_chg,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800550 uint16_t reading, int *result)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551{
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700552 *result = xoadc_reading_to_microvolt(reading);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800553 pr_debug("raw = %04x vbatt = %u\n", reading, *result);
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800554 *result = adjust_xo_vbatt_reading(chip, usb_chg, *result);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800555 pr_debug("after adj vbatt = %u\n", *result);
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700556 *result = adjust_xo_vbatt_reading_for_mbg(chip, *result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557 return 0;
558}
559
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800560static int convert_vsense_to_uv(struct pm8921_bms_chip *chip,
561 int16_t reading, int *result)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562{
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800563 *result = pm8xxx_ccadc_reading_to_microvolt(chip->revision, reading);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800564 pr_debug("raw = %04x vsense = %d\n", reading, *result);
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800565 *result = pm8xxx_cc_adjust_for_gain(*result);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800566 pr_debug("after adj vsense = %d\n", *result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700567 return 0;
568}
569
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700570static int read_vsense_avg(struct pm8921_bms_chip *chip, int *result)
571{
572 int rc;
573 int16_t reading;
574
575 rc = pm_bms_read_output_data(chip, VSENSE_AVG, &reading);
576 if (rc) {
577 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
578 return rc;
579 }
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800580
581 convert_vsense_to_uv(chip, reading, result);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700582 return 0;
583}
584
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585static int linear_interpolate(int y0, int x0, int y1, int x1, int x)
586{
587 if (y0 == y1 || x == x0)
588 return y0;
589 if (x1 == x0 || x == x1)
590 return y1;
591
592 return y0 + ((y1 - y0) * (x - x0) / (x1 - x0));
593}
594
595static int interpolate_single_lut(struct single_row_lut *lut, int x)
596{
597 int i, result;
598
599 if (x < lut->x[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700600 pr_debug("x %d less than known range return y = %d lut = %pS\n",
601 x, lut->y[0], lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602 return lut->y[0];
603 }
604 if (x > lut->x[lut->cols - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700605 pr_debug("x %d more than known range return y = %d lut = %pS\n",
606 x, lut->y[lut->cols - 1], lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607 return lut->y[lut->cols - 1];
608 }
609
610 for (i = 0; i < lut->cols; i++)
611 if (x <= lut->x[i])
612 break;
613 if (x == lut->x[i]) {
614 result = lut->y[i];
615 } else {
616 result = linear_interpolate(
617 lut->y[i - 1],
618 lut->x[i - 1],
619 lut->y[i],
620 lut->x[i],
621 x);
622 }
623 return result;
624}
625
626static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp)
627{
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -0800628 /* batt_temp is in tenths of degC - convert it to degC for lookups */
629 batt_temp = batt_temp/10;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700630 return interpolate_single_lut(chip->fcc_temp_lut, batt_temp);
631}
632
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700633static int interpolate_fcc_adjusted(struct pm8921_bms_chip *chip, int batt_temp)
634{
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -0800635 /* batt_temp is in tenths of degC - convert it to degC for lookups */
636 batt_temp = batt_temp/10;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700637 return interpolate_single_lut(chip->adjusted_fcc_temp_lut, batt_temp);
638}
639
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700640static int interpolate_scalingfactor_fcc(struct pm8921_bms_chip *chip,
641 int cycles)
642{
Abhijeet Dharmapurikar205bbc02011-11-04 14:47:36 -0700643 /*
644 * sf table could be null when no battery aging data is available, in
645 * that case return 100%
646 */
647 if (chip->fcc_sf_lut)
648 return interpolate_single_lut(chip->fcc_sf_lut, cycles);
649 else
650 return 100;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651}
652
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800653static int interpolate_scalingfactor(struct pm8921_bms_chip *chip,
654 struct sf_lut *sf_lut,
655 int row_entry, int pc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656{
Abhijeet Dharmapurikar4a4456e2011-09-27 15:10:38 -0700657 int i, scalefactorrow1, scalefactorrow2, scalefactor;
Abhijeet Dharmapurikar205bbc02011-11-04 14:47:36 -0700658 int rows, cols;
Abhijeet Dharmapurikar4a4456e2011-09-27 15:10:38 -0700659 int row1 = 0;
660 int row2 = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700661
Abhijeet Dharmapurikar205bbc02011-11-04 14:47:36 -0700662 /*
663 * sf table could be null when no battery aging data is available, in
664 * that case return 100%
665 */
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800666 if (!sf_lut)
Abhijeet Dharmapurikar205bbc02011-11-04 14:47:36 -0700667 return 100;
668
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800669 rows = sf_lut->rows;
670 cols = sf_lut->cols;
671 if (pc > sf_lut->percent[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700672 pr_debug("pc %d greater than known pc ranges for sfd\n", pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700673 row1 = 0;
674 row2 = 0;
675 }
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800676 if (pc < sf_lut->percent[rows - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700677 pr_debug("pc %d less than known pc ranges for sf", pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700678 row1 = rows - 1;
679 row2 = rows - 1;
680 }
681 for (i = 0; i < rows; i++) {
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800682 if (pc == sf_lut->percent[i]) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700683 row1 = i;
684 row2 = i;
685 break;
686 }
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800687 if (pc > sf_lut->percent[i]) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700688 row1 = i - 1;
689 row2 = i;
690 break;
691 }
692 }
693
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800694 if (row_entry < sf_lut->row_entries[0])
695 row_entry = sf_lut->row_entries[0];
696 if (row_entry > sf_lut->row_entries[cols - 1])
697 row_entry = sf_lut->row_entries[cols - 1];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700698
699 for (i = 0; i < cols; i++)
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800700 if (row_entry <= sf_lut->row_entries[i])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700701 break;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800702 if (row_entry == sf_lut->row_entries[i]) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703 scalefactor = linear_interpolate(
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800704 sf_lut->sf[row1][i],
705 sf_lut->percent[row1],
706 sf_lut->sf[row2][i],
707 sf_lut->percent[row2],
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708 pc);
709 return scalefactor;
710 }
711
712 scalefactorrow1 = linear_interpolate(
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800713 sf_lut->sf[row1][i - 1],
714 sf_lut->row_entries[i - 1],
715 sf_lut->sf[row1][i],
716 sf_lut->row_entries[i],
717 row_entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700718
719 scalefactorrow2 = linear_interpolate(
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800720 sf_lut->sf[row2][i - 1],
721 sf_lut->row_entries[i - 1],
722 sf_lut->sf[row2][i],
723 sf_lut->row_entries[i],
724 row_entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725
726 scalefactor = linear_interpolate(
727 scalefactorrow1,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800728 sf_lut->percent[row1],
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700729 scalefactorrow2,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800730 sf_lut->percent[row2],
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700731 pc);
732
733 return scalefactor;
734}
735
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700736static int is_between(int left, int right, int value)
737{
738 if (left >= right && left >= value && value >= right)
739 return 1;
740 if (left <= right && left <= value && value <= right)
741 return 1;
742
743 return 0;
744}
745
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700746static int interpolate_pc(struct pm8921_bms_chip *chip,
747 int batt_temp, int ocv)
748{
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700749 int i, j, pcj, pcj_minus_one, pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700750 int rows = chip->pc_temp_ocv_lut->rows;
751 int cols = chip->pc_temp_ocv_lut->cols;
752
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -0800753 /* batt_temp is in tenths of degC - convert it to degC for lookups */
754 batt_temp = batt_temp/10;
755
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700756 if (batt_temp < chip->pc_temp_ocv_lut->temp[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700757 pr_debug("batt_temp %d < known temp range for pc\n", batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758 batt_temp = chip->pc_temp_ocv_lut->temp[0];
759 }
760 if (batt_temp > chip->pc_temp_ocv_lut->temp[cols - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700761 pr_debug("batt_temp %d > known temp range for pc\n", batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700762 batt_temp = chip->pc_temp_ocv_lut->temp[cols - 1];
763 }
764
765 for (j = 0; j < cols; j++)
766 if (batt_temp <= chip->pc_temp_ocv_lut->temp[j])
767 break;
768 if (batt_temp == chip->pc_temp_ocv_lut->temp[j]) {
769 /* found an exact match for temp in the table */
770 if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j])
771 return chip->pc_temp_ocv_lut->percent[0];
772 if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j])
773 return chip->pc_temp_ocv_lut->percent[rows - 1];
774 for (i = 0; i < rows; i++) {
775 if (ocv >= chip->pc_temp_ocv_lut->ocv[i][j]) {
776 if (ocv == chip->pc_temp_ocv_lut->ocv[i][j])
777 return
778 chip->pc_temp_ocv_lut->percent[i];
779 pc = linear_interpolate(
780 chip->pc_temp_ocv_lut->percent[i],
781 chip->pc_temp_ocv_lut->ocv[i][j],
782 chip->pc_temp_ocv_lut->percent[i - 1],
783 chip->pc_temp_ocv_lut->ocv[i - 1][j],
784 ocv);
785 return pc;
786 }
787 }
788 }
789
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700790 /*
791 * batt_temp is within temperature for
792 * column j-1 and j
793 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700794 if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j])
795 return chip->pc_temp_ocv_lut->percent[0];
796 if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j - 1])
797 return chip->pc_temp_ocv_lut->percent[rows - 1];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700799 pcj_minus_one = 0;
800 pcj = 0;
801 for (i = 0; i < rows-1; i++) {
802 if (pcj == 0
803 && is_between(chip->pc_temp_ocv_lut->ocv[i][j],
804 chip->pc_temp_ocv_lut->ocv[i+1][j], ocv)) {
805 pcj = linear_interpolate(
806 chip->pc_temp_ocv_lut->percent[i],
807 chip->pc_temp_ocv_lut->ocv[i][j],
808 chip->pc_temp_ocv_lut->percent[i + 1],
809 chip->pc_temp_ocv_lut->ocv[i+1][j],
810 ocv);
811 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700812
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700813 if (pcj_minus_one == 0
814 && is_between(chip->pc_temp_ocv_lut->ocv[i][j-1],
815 chip->pc_temp_ocv_lut->ocv[i+1][j-1], ocv)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700816
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700817 pcj_minus_one = linear_interpolate(
818 chip->pc_temp_ocv_lut->percent[i],
819 chip->pc_temp_ocv_lut->ocv[i][j-1],
820 chip->pc_temp_ocv_lut->percent[i + 1],
821 chip->pc_temp_ocv_lut->ocv[i+1][j-1],
822 ocv);
823 }
824
825 if (pcj && pcj_minus_one) {
826 pc = linear_interpolate(
827 pcj_minus_one,
828 chip->pc_temp_ocv_lut->temp[j-1],
829 pcj,
830 chip->pc_temp_ocv_lut->temp[j],
831 batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700832 return pc;
833 }
834 }
835
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700836 if (pcj)
837 return pcj;
838
839 if (pcj_minus_one)
840 return pcj_minus_one;
841
842 pr_debug("%d ocv wasn't found for temp %d in the LUT returning 100%%",
843 ocv, batt_temp);
844 return 100;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845}
846
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800847#define BMS_MODE_BIT BIT(6)
848#define EN_VBAT_BIT BIT(5)
849#define OVERRIDE_MODE_DELAY_MS 20
850int pm8921_bms_get_simultaneous_battery_voltage_and_current(int *ibat_ua,
851 int *vbat_uv)
852{
853 int16_t vsense_raw;
854 int16_t vbat_raw;
855 int vsense_uv;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800856 int usb_chg;
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800857
858 if (the_chip == NULL) {
859 pr_err("Called to early\n");
860 return -EINVAL;
861 }
862
863 mutex_lock(&the_chip->bms_output_lock);
864
865 pm8xxx_writeb(the_chip->dev->parent, BMS_S1_DELAY, 0x00);
866 pm_bms_masked_write(the_chip, BMS_CONTROL,
867 BMS_MODE_BIT | EN_VBAT_BIT, BMS_MODE_BIT | EN_VBAT_BIT);
868
869 msleep(OVERRIDE_MODE_DELAY_MS);
870
871 pm_bms_lock_output_data(the_chip);
872 pm_bms_read_output_data(the_chip, VSENSE_AVG, &vsense_raw);
873 pm_bms_read_output_data(the_chip, VBATT_AVG, &vbat_raw);
874 pm_bms_unlock_output_data(the_chip);
875 pm_bms_masked_write(the_chip, BMS_CONTROL,
876 BMS_MODE_BIT | EN_VBAT_BIT, 0);
877
878 pm8xxx_writeb(the_chip->dev->parent, BMS_S1_DELAY, 0x0B);
879
880 mutex_unlock(&the_chip->bms_output_lock);
881
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800882 usb_chg = usb_chg_plugged_in();
883
884 convert_vbatt_raw_to_uv(the_chip, usb_chg, vbat_raw, vbat_uv);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800885 convert_vsense_to_uv(the_chip, vsense_raw, &vsense_uv);
886 *ibat_ua = vsense_uv * 1000 / (int)the_chip->r_sense;
887
888 pr_debug("vsense_raw = 0x%x vbat_raw = 0x%x"
889 " ibat_ua = %d vbat_uv = %d\n",
890 (uint16_t)vsense_raw, (uint16_t)vbat_raw,
891 *ibat_ua, *vbat_uv);
892 return 0;
893}
894EXPORT_SYMBOL(pm8921_bms_get_simultaneous_battery_voltage_and_current);
895
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800896static int read_rbatt_params_raw(struct pm8921_bms_chip *chip,
897 struct pm8921_rbatt_params *raw)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700898{
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800899 int usb_chg;
900
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800901 mutex_lock(&chip->bms_output_lock);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800902 pm_bms_lock_output_data(chip);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700903
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800904 pm_bms_read_output_data(chip,
905 OCV_FOR_RBATT, &raw->ocv_for_rbatt_raw);
906 pm_bms_read_output_data(chip,
907 VBATT_FOR_RBATT, &raw->vbatt_for_rbatt_raw);
908 pm_bms_read_output_data(chip,
909 VSENSE_FOR_RBATT, &raw->vsense_for_rbatt_raw);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700910
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800911 pm_bms_unlock_output_data(chip);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800912 mutex_unlock(&chip->bms_output_lock);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800913
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800914 usb_chg = usb_chg_plugged_in();
915 convert_vbatt_raw_to_uv(chip, usb_chg,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800916 raw->vbatt_for_rbatt_raw, &raw->vbatt_for_rbatt_uv);
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800917 convert_vbatt_raw_to_uv(chip, usb_chg,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800918 raw->ocv_for_rbatt_raw, &raw->ocv_for_rbatt_uv);
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800919 convert_vsense_to_uv(chip, raw->vsense_for_rbatt_raw,
920 &raw->vsense_for_rbatt_uv);
Abhijeet Dharmapurikar1b8e8292012-01-17 11:01:44 -0800921
922 pr_debug("vbatt_for_rbatt_raw = 0x%x, vbatt_for_rbatt= %duV\n",
923 raw->vbatt_for_rbatt_raw, raw->vbatt_for_rbatt_uv);
924 pr_debug("ocv_for_rbatt_raw = 0x%x, ocv_for_rbatt= %duV\n",
925 raw->ocv_for_rbatt_raw, raw->ocv_for_rbatt_uv);
926 pr_debug("vsense_for_rbatt_raw = 0x%x, vsense_for_rbatt= %duV\n",
927 raw->vsense_for_rbatt_raw, raw->vsense_for_rbatt_uv);
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800928 return 0;
929}
930
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700931#define MBG_TRANSIENT_ERROR_RAW 51
932static void adjust_pon_ocv_raw(struct pm8921_bms_chip *chip,
933 struct pm8921_soc_params *raw)
934{
935 /* in 8921 parts the PON ocv is taken when the MBG is not settled.
936 * decrease the pon ocv by 15mV raw value to account for it
937 * Since a 1/3rd of vbatt is supplied to the adc the raw value
938 * needs to be adjusted by 5mV worth bits
939 */
940 if (raw->last_good_ocv_raw >= MBG_TRANSIENT_ERROR_RAW)
941 raw->last_good_ocv_raw -= MBG_TRANSIENT_ERROR_RAW;
942}
943
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800944static int read_soc_params_raw(struct pm8921_bms_chip *chip,
945 struct pm8921_soc_params *raw)
946{
947 int usb_chg;
948
949 mutex_lock(&chip->bms_output_lock);
950 pm_bms_lock_output_data(chip);
951
952 pm_bms_read_output_data(chip,
953 LAST_GOOD_OCV_VALUE, &raw->last_good_ocv_raw);
954 read_cc(chip, &raw->cc);
955
956 pm_bms_unlock_output_data(chip);
957 mutex_unlock(&chip->bms_output_lock);
958
959 usb_chg = usb_chg_plugged_in();
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -0700960
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700961 if (chip->prev_last_good_ocv_raw == 0) {
962 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
963 adjust_pon_ocv_raw(chip, raw);
964 convert_vbatt_raw_to_uv(chip, usb_chg,
965 raw->last_good_ocv_raw, &raw->last_good_ocv_uv);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -0700966 chip->last_ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700967 } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) {
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -0700968 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
969 convert_vbatt_raw_to_uv(chip, usb_chg,
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800970 raw->last_good_ocv_raw, &raw->last_good_ocv_uv);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -0700971 chip->last_ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -0700972 /* forget the old cc value upon ocv */
973 chip->last_cc_uah = 0;
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -0700974 } else {
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -0700975 raw->last_good_ocv_uv = chip->last_ocv_uv;
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -0700976 }
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800977
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -0700978 /* fake a high OCV if we are just done charging */
979 if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw) {
980 chip->ocv_reading_at_100 = 0;
981 chip->cc_reading_at_100 = 0;
982 } else {
983 /*
984 * force 100% ocv by selecting the highest voltage the
985 * battery could ever reach
986 */
987 raw->last_good_ocv_uv = chip->max_voltage_uv;
988 chip->last_ocv_uv = chip->max_voltage_uv;
989 }
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800990 pr_debug("0p625 = %duV\n", chip->xoadc_v0625);
991 pr_debug("1p25 = %duV\n", chip->xoadc_v125);
Abhijeet Dharmapurikar1b8e8292012-01-17 11:01:44 -0800992 pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n",
993 raw->last_good_ocv_raw, raw->last_good_ocv_uv);
994 pr_debug("cc_raw= 0x%x\n", raw->cc);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800995 return 0;
996}
997
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800998static int get_rbatt(struct pm8921_bms_chip *chip, int soc_rbatt, int batt_temp)
999{
1000 int rbatt, scalefactor;
1001
1002 rbatt = (last_rbatt < 0) ? chip->default_rbatt_mohm : last_rbatt;
1003 pr_debug("rbatt before scaling = %d\n", rbatt);
1004 if (chip->rbatt_sf_lut == NULL) {
1005 pr_debug("RBATT = %d\n", rbatt);
1006 return rbatt;
1007 }
Abhijeet Dharmapurikar12a891c2012-04-12 23:52:06 -07001008 /* Convert the batt_temp to DegC from deciDegC */
1009 batt_temp = batt_temp / 10;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001010 scalefactor = interpolate_scalingfactor(chip, chip->rbatt_sf_lut,
1011 batt_temp, soc_rbatt);
1012 pr_debug("rbatt sf = %d for batt_temp = %d, soc_rbatt = %d\n",
1013 scalefactor, batt_temp, soc_rbatt);
1014 rbatt = (rbatt * scalefactor) / 100;
1015
Abhijeet Dharmapurikarbaffba42012-03-22 14:41:10 -07001016 rbatt += the_chip->rconn_mohm;
1017 pr_debug("adding rconn_mohm = %d rbatt = %d\n",
1018 the_chip->rconn_mohm, rbatt);
1019
Abhijeet Dharmapurikarded189b2012-03-30 10:12:28 -07001020 if (is_between(20, 10, soc_rbatt))
1021 rbatt = rbatt
1022 + ((20 - soc_rbatt) * chip->delta_rbatt_mohm) / 10;
1023 else
1024 if (is_between(10, 0, soc_rbatt))
1025 rbatt = rbatt + chip->delta_rbatt_mohm;
1026
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001027 pr_debug("RBATT = %d\n", rbatt);
1028 return rbatt;
1029}
1030
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001031static int calculate_rbatt_resume(struct pm8921_bms_chip *chip,
1032 struct pm8921_rbatt_params *raw)
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001033{
1034 unsigned int r_batt;
1035
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08001036 if (raw->ocv_for_rbatt_uv <= 0
1037 || raw->ocv_for_rbatt_uv <= raw->vbatt_for_rbatt_uv
1038 || raw->vsense_for_rbatt_raw <= 0) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001039 pr_debug("rbatt readings unavailable ocv = %d, vbatt = %d,"
1040 "vsen = %d\n",
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001041 raw->ocv_for_rbatt_uv,
1042 raw->vbatt_for_rbatt_uv,
1043 raw->vsense_for_rbatt_raw);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001044 return -EINVAL;
1045 }
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001046 r_batt = ((raw->ocv_for_rbatt_uv - raw->vbatt_for_rbatt_uv)
1047 * chip->r_sense) / raw->vsense_for_rbatt_uv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001048 pr_debug("r_batt = %umilliOhms", r_batt);
1049 return r_batt;
1050}
1051
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001052static int calculate_fcc_uah(struct pm8921_bms_chip *chip, int batt_temp,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001053 int chargecycles)
1054{
1055 int initfcc, result, scalefactor = 0;
1056
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001057 if (chip->adjusted_fcc_temp_lut == NULL) {
1058 initfcc = interpolate_fcc(chip, batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001059
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001060 scalefactor = interpolate_scalingfactor_fcc(chip, chargecycles);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001061
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001062 /* Multiply the initial FCC value by the scale factor. */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001063 result = (initfcc * scalefactor * 1000) / 100;
1064 pr_debug("fcc = %d uAh\n", result);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001065 return result;
1066 } else {
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001067 return 1000 * interpolate_fcc_adjusted(chip, batt_temp);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001068 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001069}
1070
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001071static int get_battery_uvolts(struct pm8921_bms_chip *chip, int *uvolts)
1072{
1073 int rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001074 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001075
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001076 rc = pm8xxx_adc_read(chip->vbat_channel, &result);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001077 if (rc) {
1078 pr_err("error reading adc channel = %d, rc = %d\n",
1079 chip->vbat_channel, rc);
1080 return rc;
1081 }
1082 pr_debug("mvolts phy = %lld meas = 0x%llx", result.physical,
1083 result.measurement);
1084 *uvolts = (int)result.physical;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001085 return 0;
1086}
1087
1088static int adc_based_ocv(struct pm8921_bms_chip *chip, int *ocv)
1089{
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001090 int vbatt, rbatt, ibatt_ua, rc;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001091
1092 rc = get_battery_uvolts(chip, &vbatt);
1093 if (rc) {
1094 pr_err("failed to read vbatt from adc rc = %d\n", rc);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001095 return rc;
1096 }
1097
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001098 rc = pm8921_bms_get_battery_current(&ibatt_ua);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001099 if (rc) {
1100 pr_err("failed to read batt current rc = %d\n", rc);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001101 return rc;
1102 }
1103
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001104 rbatt = (last_rbatt < 0) ? chip->default_rbatt_mohm : last_rbatt;
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001105 *ocv = vbatt + (ibatt_ua * rbatt)/1000;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001106 return 0;
1107}
1108
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001109static int calculate_pc(struct pm8921_bms_chip *chip, int ocv_uv, int batt_temp,
1110 int chargecycles)
1111{
1112 int pc, scalefactor;
1113
1114 pc = interpolate_pc(chip, batt_temp, ocv_uv / 1000);
1115 pr_debug("pc = %u for ocv = %dmicroVolts batt_temp = %d\n",
1116 pc, ocv_uv, batt_temp);
1117
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001118 scalefactor = interpolate_scalingfactor(chip,
1119 chip->pc_sf_lut, chargecycles, pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001120 pr_debug("scalefactor = %u batt_temp = %d\n", scalefactor, batt_temp);
1121
1122 /* Multiply the initial FCC value by the scale factor. */
1123 pc = (pc * scalefactor) / 100;
1124 return pc;
1125}
1126
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001127/**
1128 * calculate_cc_uah -
1129 * @chip: the bms chip pointer
1130 * @cc: the cc reading from bms h/w
1131 * @val: return value
1132 * @coulumb_counter: adjusted coulumb counter for 100%
1133 *
1134 * RETURNS: in val pointer coulumb counter based charger in uAh
1135 * (micro Amp hour)
1136 */
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001137static void calculate_cc_uah(struct pm8921_bms_chip *chip, int cc, int *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001138{
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001139 int64_t cc_voltage_uv, cc_nvh, cc_uah;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001140
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001141 cc_voltage_uv = cc;
1142 cc_voltage_uv -= chip->cc_reading_at_100;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001143 pr_debug("cc = %d. after subtracting 0x%x cc = %lld\n",
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001144 cc, chip->cc_reading_at_100,
1145 cc_voltage_uv);
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -07001146 cc_voltage_uv = cc_to_microvolt(chip, cc_voltage_uv);
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08001147 cc_voltage_uv = pm8xxx_cc_adjust_for_gain(cc_voltage_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001148 pr_debug("cc_voltage_uv = %lld microvolts\n", cc_voltage_uv);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001149 cc_nvh = ccmicrovolt_to_nvh(cc_voltage_uv);
1150 pr_debug("cc_nvh = %lld nano_volt_hour\n", cc_nvh);
1151 cc_uah = div_s64(cc_nvh, chip->r_sense);
1152 *val = cc_uah;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001153}
1154
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001155static int calculate_uuc_uah_at_given_current(struct pm8921_bms_chip *chip,
1156 int batt_temp, int chargecycles,
1157 int rbatt, int fcc_uah, int i_ma)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001158{
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001159 int unusable_uv, pc_unusable, uuc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001160
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001161 /* calculate unusable charge with itest */
1162 unusable_uv = (rbatt * i_ma) + (chip->v_failure * 1000);
1163 pc_unusable = calculate_pc(chip, unusable_uv, batt_temp, chargecycles);
1164 uuc = (fcc_uah * pc_unusable) / 100;
1165 pr_debug("For i_ma = %d, unusable_uv = %d unusable_pc = %d uuc = %d\n",
1166 i_ma, unusable_uv, pc_unusable, uuc);
1167 return uuc;
1168}
1169
1170/* soc_rbatt when uuc_reported should be equal to uuc_now */
1171#define SOC_RBATT_CHG 80
1172#define SOC_RBATT_DISCHG 10
1173static int calculate_unusable_charge_uah(struct pm8921_bms_chip *chip,
1174 int rbatt, int fcc_uah, int cc_uah,
1175 int soc_rbatt, int batt_temp, int chargecycles)
1176{
1177 struct timeval now;
1178 int delta_time_s;
1179 int delta_cc_uah;
1180 int iavg_ua, iavg_ma;
1181 int uuc_uah_itest, uuc_uah_iavg, uuc_now, uuc_reported;
1182 s64 stepsize = 0;
1183 int firsttime = 0;
1184
1185 delta_cc_uah = cc_uah - chip->last_cc_uah;
1186 do_gettimeofday(&now);
1187 if (chip->t.tv_sec != 0) {
1188 delta_time_s = (now.tv_sec - chip->t.tv_sec);
1189 } else {
1190 /* uuc calculation for the first time */
1191 delta_time_s = 0;
1192 firsttime = 1;
1193 }
1194
1195 if (delta_time_s != 0)
1196 iavg_ua = div_s64((s64)delta_cc_uah * 3600, delta_time_s);
1197 else
1198 iavg_ua = 0;
1199
1200 iavg_ma = iavg_ua/1000;
1201
1202 pr_debug("t.tv_sec = %d, now.tv_sec = %d\n", (int)chip->t.tv_sec,
1203 (int)now.tv_sec);
1204
1205 pr_debug("delta_time_s = %d iavg_ma = %d\n", delta_time_s, iavg_ma);
1206
1207 if (iavg_ma == 0) {
1208 pr_debug("Iavg = 0 returning last uuc = %d\n",
1209 chip->last_uuc_uah);
1210 uuc_reported = chip->last_uuc_uah;
1211 goto out;
1212 }
1213
1214 /* calculate unusable charge with itest */
1215 uuc_uah_itest = calculate_uuc_uah_at_given_current(chip,
1216 batt_temp, chargecycles,
1217 rbatt, fcc_uah, chip->i_test);
1218
1219 pr_debug("itest = %d uuc_itest = %d\n", chip->i_test, uuc_uah_itest);
1220
1221 /* calculate unusable charge with iavg */
1222 iavg_ma = max(0, iavg_ma);
1223 uuc_uah_iavg = calculate_uuc_uah_at_given_current(chip,
1224 batt_temp, chargecycles,
1225 rbatt, fcc_uah, iavg_ma);
1226 pr_debug("iavg = %d uuc_iavg = %d\n", iavg_ma, uuc_uah_iavg);
1227
1228 if (firsttime) {
1229 if (cc_uah < chip->last_cc_uah)
1230 chip->last_uuc_uah = uuc_uah_itest;
1231 else
1232 chip->last_uuc_uah = uuc_uah_iavg;
1233 pr_debug("firsttime uuc_prev = %d\n", chip->last_uuc_uah);
1234 }
1235
1236 uuc_now = min(uuc_uah_itest, uuc_uah_iavg);
1237
1238 uuc_reported = -EINVAL;
1239 if (cc_uah < chip->last_cc_uah) {
1240 /* charging */
1241 if (uuc_now < chip->last_uuc_uah) {
1242 stepsize = max(1, (SOC_RBATT_CHG - soc_rbatt));
1243 /* uuc_reported = uuc_prev + deltauuc / stepsize */
1244 uuc_reported = div_s64 (stepsize * chip->last_uuc_uah
1245 + (uuc_now - chip->last_uuc_uah),
1246 stepsize);
1247 uuc_reported = max(0, uuc_reported);
1248 }
1249 } else {
1250 if (uuc_now > chip->last_uuc_uah) {
1251 stepsize = max(1, (soc_rbatt - SOC_RBATT_DISCHG));
1252 /* uuc_reported = uuc_prev + deltauuc / stepsize */
1253 uuc_reported = div_s64 (stepsize * chip->last_uuc_uah
1254 + (uuc_now - chip->last_uuc_uah),
1255 stepsize);
1256 uuc_reported = max(0, uuc_reported);
1257 }
1258 }
1259 if (uuc_reported == -EINVAL)
1260 uuc_reported = chip->last_uuc_uah;
1261
1262 pr_debug("uuc_now = %d uuc_prev = %d stepsize = %d uuc_reported = %d\n",
1263 uuc_now, chip->last_uuc_uah, (int)stepsize,
1264 uuc_reported);
1265
1266out:
1267 /* remember the reported uuc */
1268 chip->last_uuc_uah = uuc_reported;
1269
1270 /* remember cc_uah */
1271 chip->last_cc_uah = cc_uah;
1272
1273 /* remember this time */
1274 chip->t = now;
1275
1276 return uuc_reported;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001277}
Abhijeet Dharmapurikar52aa9682011-07-20 14:05:47 -07001278
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001279/* calculate remainging charge at the time of ocv */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001280static int calculate_remaining_charge_uah(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001281 struct pm8921_soc_params *raw,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001282 int fcc_uah, int batt_temp,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001283 int chargecycles)
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001284{
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001285 int ocv, pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001286
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001287 ocv = raw->last_good_ocv_uv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001288 pc = calculate_pc(chip, ocv, batt_temp, chargecycles);
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001289 pr_debug("ocv = %d pc = %d\n", ocv, pc);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001290 return (fcc_uah * pc) / 100;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001291}
1292
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001293static void calculate_soc_params(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001294 struct pm8921_soc_params *raw,
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001295 int batt_temp, int chargecycles,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001296 int *fcc_uah,
1297 int *unusable_charge_uah,
1298 int *remaining_charge_uah,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001299 int *cc_uah,
1300 int *rbatt)
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001301{
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001302 int soc_rbatt;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001303
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001304 *fcc_uah = calculate_fcc_uah(chip, batt_temp, chargecycles);
1305 pr_debug("FCC = %uuAh batt_temp = %d, cycles = %d\n",
1306 *fcc_uah, batt_temp, chargecycles);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001307
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001308
1309 /* calculate remainging charge */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001310 *remaining_charge_uah = calculate_remaining_charge_uah(chip, raw,
1311 *fcc_uah, batt_temp, chargecycles);
1312 pr_debug("RC = %uuAh\n", *remaining_charge_uah);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001313
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001314 /* calculate cc micro_volt_hour */
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001315 calculate_cc_uah(chip, raw->cc, cc_uah);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001316 pr_debug("cc_uah = %duAh raw->cc = %x cc = %lld after subtracting %x\n",
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001317 *cc_uah, raw->cc,
1318 (int64_t)raw->cc - chip->cc_reading_at_100,
1319 chip->cc_reading_at_100);
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001320
1321 soc_rbatt = ((*remaining_charge_uah - *cc_uah) * 100) / *fcc_uah;
1322 if (soc_rbatt < 0)
1323 soc_rbatt = 0;
1324 *rbatt = get_rbatt(chip, soc_rbatt, batt_temp);
1325
1326 *unusable_charge_uah = calculate_unusable_charge_uah(chip, *rbatt,
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001327 *fcc_uah, *cc_uah, soc_rbatt,
1328 batt_temp,
1329 chargecycles);
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001330 pr_debug("UUC = %uuAh\n", *unusable_charge_uah);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001331}
1332
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001333static int calculate_real_fcc_uah(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001334 struct pm8921_soc_params *raw,
1335 int batt_temp, int chargecycles,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001336 int *ret_fcc_uah)
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001337{
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001338 int fcc_uah, unusable_charge_uah;
1339 int remaining_charge_uah;
1340 int cc_uah;
1341 int real_fcc_uah;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001342 int rbatt;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001343
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001344 calculate_soc_params(chip, raw, batt_temp, chargecycles,
1345 &fcc_uah,
1346 &unusable_charge_uah,
1347 &remaining_charge_uah,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001348 &cc_uah,
1349 &rbatt);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001350
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001351 real_fcc_uah = remaining_charge_uah - cc_uah;
1352 *ret_fcc_uah = fcc_uah;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001353 pr_debug("real_fcc = %d, RC = %d CC = %d fcc = %d\n",
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001354 real_fcc_uah, remaining_charge_uah, cc_uah, fcc_uah);
1355 return real_fcc_uah;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001356}
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001357
1358static int bound_soc(int soc)
1359{
1360 soc = max(0, soc);
1361 soc = min(100, soc);
1362 return soc;
1363}
1364
1365static int last_soc_est = -EINVAL;
1366static int adjust_soc(struct pm8921_bms_chip *chip, int soc, int batt_temp,
1367 int rbatt , int fcc_uah, int uuc_uah, int cc_uah)
1368{
1369 int ibat_ua = 0, vbat_uv = 0;
1370 int ocv_est_uv = 0, soc_est = 0, pc_est = 0, pc = 0;
1371 int delta_ocv_uv = 0;
1372 int n = 0;
1373 int rc_new_uah = 0;
1374 int pc_new = 0;
1375 int soc_new = 0;
1376 int m = 0;
1377
1378 pm8921_bms_get_simultaneous_battery_voltage_and_current(&ibat_ua,
1379 &vbat_uv);
1380
1381 if (ibat_ua < 0)
1382 goto out;
1383 ocv_est_uv = vbat_uv + (ibat_ua * rbatt)/1000;
1384 pc_est = calculate_pc(chip, ocv_est_uv, batt_temp, last_chargecycles);
1385 soc_est = div_s64((s64)fcc_uah * pc_est - uuc_uah*100,
1386 (s64)fcc_uah - uuc_uah);
1387 soc_est = bound_soc(soc_est);
1388
1389 /*
1390 * do not adjust if soc_est is between 45 and 25 OR soc_est is
1391 * same as what bms calculated
1392 */
1393 if (is_between(45, 25, soc_est) || soc_est == soc)
1394 goto out;
1395
1396 if (last_soc_est == -EINVAL)
1397 last_soc_est = soc;
1398
1399 n = min(200, max(1 , soc + soc_est + last_soc_est));
1400 /* remember the last soc_est in last_soc_est */
1401 last_soc_est = soc_est;
1402
1403 pc = calculate_pc(chip, chip->last_ocv_uv,
1404 batt_temp, last_chargecycles);
1405 if (pc > 0) {
1406 pc_new = calculate_pc(chip, chip->last_ocv_uv - (++m * 1000),
1407 batt_temp, last_chargecycles);
1408 while (pc_new == pc) {
1409 /* start taking 10mV steps */
1410 m = m + 10;
1411 pc_new = calculate_pc(chip,
1412 chip->last_ocv_uv - (m * 1000),
1413 batt_temp, last_chargecycles);
1414 }
1415 } else {
1416 /*
1417 * pc is already at the lowest point,
1418 * assume 1 millivolt translates to 1% pc
1419 */
1420 pc = 1;
1421 pc_new = 0;
1422 m = 1;
1423 }
1424
1425 delta_ocv_uv = div_s64((soc - soc_est) * (s64)m * 1000,
1426 n * (pc - pc_new));
1427 chip->last_ocv_uv -= delta_ocv_uv;
1428
1429 if (chip->last_ocv_uv >= chip->max_voltage_uv)
1430 chip->last_ocv_uv = chip->max_voltage_uv;
1431
1432 /* calculate the soc based on this new ocv */
1433 pc_new = calculate_pc(chip, chip->last_ocv_uv,
1434 batt_temp, last_chargecycles);
1435 rc_new_uah = (fcc_uah * pc_new) / 100;
1436 soc_new = (rc_new_uah - cc_uah - uuc_uah)*100 / (fcc_uah - uuc_uah);
1437 soc_new = bound_soc(soc_new);
1438
1439 /*
1440 * if soc_new is ZERO force it higher so that phone doesnt report soc=0
1441 * soc = 0 should happen only when soc_est == 0
1442 */
1443 if (soc_new == 0 && soc_est != 0)
1444 soc_new = 1;
1445
1446 soc = soc_new;
1447
1448out:
1449 pr_debug("ibat_ua = %d, vbat_uv = %d, ocv_est_uv = %d, pc_est = %d, "
1450 "soc_est = %d, n = %d, delta_ocv_uv = %d, last_ocv_uv = %d, "
1451 "pc_new = %d, soc_new = %d\n",
1452 ibat_ua, vbat_uv, ocv_est_uv, pc_est,
1453 soc_est, n, delta_ocv_uv, chip->last_ocv_uv,
1454 pc_new, soc_new);
1455
1456 return soc;
1457}
1458
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001459/*
1460 * Remaining Usable Charge = remaining_charge (charge at ocv instance)
1461 * - coloumb counter charge
1462 * - unusable charge (due to battery resistance)
1463 * SOC% = (remaining usable charge/ fcc - usable_charge);
1464 */
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001465static int calculate_state_of_charge(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001466 struct pm8921_soc_params *raw,
1467 int batt_temp, int chargecycles)
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001468{
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001469 int remaining_usable_charge_uah, fcc_uah, unusable_charge_uah;
1470 int remaining_charge_uah, soc;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001471 int cc_uah;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001472 int rbatt;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001473
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001474 calculate_soc_params(chip, raw, batt_temp, chargecycles,
1475 &fcc_uah,
1476 &unusable_charge_uah,
1477 &remaining_charge_uah,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001478 &cc_uah,
1479 &rbatt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001480
1481 /* calculate remaining usable charge */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001482 remaining_usable_charge_uah = remaining_charge_uah
1483 - cc_uah
1484 - unusable_charge_uah;
1485
1486 pr_debug("RUC = %duAh\n", remaining_usable_charge_uah);
Abhijeet Dharmapurikarbbae88312012-02-14 18:56:42 -08001487 if (fcc_uah - unusable_charge_uah <= 0) {
1488 pr_warn("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
1489 fcc_uah, unusable_charge_uah);
1490 soc = 0;
1491 } else {
1492 soc = (remaining_usable_charge_uah * 100)
1493 / (fcc_uah - unusable_charge_uah);
1494 }
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001495
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001496 if (soc > 100)
1497 soc = 100;
1498 pr_debug("SOC = %u%%\n", soc);
1499
Abhijeet Dharmapurikar986bd8a2012-01-05 15:15:15 -08001500 if (bms_fake_battery != -EINVAL) {
1501 pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery);
1502 return bms_fake_battery;
1503 }
1504
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001505 if (soc < 0) {
Abhijeet Dharmapurikar21ebadd2011-09-28 21:14:16 -07001506 pr_err("bad rem_usb_chg = %d rem_chg %d,"
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001507 "cc_uah %d, unusb_chg %d\n",
1508 remaining_usable_charge_uah,
1509 remaining_charge_uah,
1510 cc_uah, unusable_charge_uah);
1511
Abhijeet Dharmapurikar21ebadd2011-09-28 21:14:16 -07001512 pr_err("for bad rem_usb_chg last_ocv_uv = %d"
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001513 "chargecycles = %d, batt_temp = %d"
1514 "fcc = %d soc =%d\n",
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001515 chip->last_ocv_uv, chargecycles, batt_temp,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001516 fcc_uah, soc);
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -08001517 soc = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001518 }
1519
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001520 soc = adjust_soc(chip, soc, batt_temp, rbatt,
1521 fcc_uah, unusable_charge_uah, cc_uah);
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001522
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001523 if (last_soc == -EINVAL || soc <= last_soc) {
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001524 last_soc = soc;
1525 } else {
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001526 /*
1527 * soc > last_soc
1528 * the device must be charging for reporting a higher soc, if
1529 * not ignore this soc and continue reporting the last_soc
1530 */
1531 if (the_chip->start_percent != -EINVAL)
1532 last_soc = soc;
1533 else
1534 pr_debug("soc = %d reporting last_soc = %d\n", soc,
1535 last_soc);
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001536 }
1537
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001538 pr_debug("Reported SOC = %u%%\n", last_soc);
1539 return last_soc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001540}
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08001541#define MIN_DELTA_625_UV 1000
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001542static void calib_hkadc(struct pm8921_bms_chip *chip)
1543{
1544 int voltage, rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001545 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08001546 int usb_chg;
1547 int this_delta;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001548
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001549 rc = pm8xxx_adc_read(the_chip->ref1p25v_channel, &result);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001550 if (rc) {
1551 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
1552 return;
1553 }
Abhijeet Dharmapurikar619cb0c2011-12-15 15:28:14 -08001554 voltage = xoadc_reading_to_microvolt(result.adc_code);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001555
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001556 pr_debug("result 1.25v = 0x%x, voltage = %duV adc_meas = %lld\n",
1557 result.adc_code, voltage, result.measurement);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001558
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001559 chip->xoadc_v125 = voltage;
1560
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001561 rc = pm8xxx_adc_read(the_chip->ref625mv_channel, &result);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001562 if (rc) {
1563 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
1564 return;
1565 }
Abhijeet Dharmapurikar619cb0c2011-12-15 15:28:14 -08001566 voltage = xoadc_reading_to_microvolt(result.adc_code);
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08001567
1568 usb_chg = usb_chg_plugged_in();
1569 pr_debug("result 0.625V = 0x%x, voltage = %duV adc_meas = %lld "
1570 "usb_chg = %d\n",
1571 result.adc_code, voltage, result.measurement,
1572 usb_chg);
1573
1574 if (usb_chg)
1575 chip->xoadc_v0625_usb_present = voltage;
1576 else
1577 chip->xoadc_v0625_usb_absent = voltage;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001578
1579 chip->xoadc_v0625 = voltage;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08001580 if (chip->xoadc_v0625_usb_present && chip->xoadc_v0625_usb_absent) {
1581 this_delta = chip->xoadc_v0625_usb_present
1582 - chip->xoadc_v0625_usb_absent;
1583 pr_debug("this_delta= %duV\n", this_delta);
1584 if (this_delta > MIN_DELTA_625_UV)
1585 last_usb_cal_delta_uv = this_delta;
1586 pr_debug("625V_present= %d, 625V_absent= %d, delta = %duV\n",
1587 chip->xoadc_v0625_usb_present,
1588 chip->xoadc_v0625_usb_absent,
1589 last_usb_cal_delta_uv);
1590 }
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001591}
1592
1593static void calibrate_hkadc_work(struct work_struct *work)
1594{
1595 struct pm8921_bms_chip *chip = container_of(work,
1596 struct pm8921_bms_chip, calib_hkadc_work);
1597
1598 calib_hkadc(chip);
1599}
1600
Abhijeet Dharmapurikar1b8e8292012-01-17 11:01:44 -08001601void pm8921_bms_calibrate_hkadc(void)
1602{
1603 schedule_work(&the_chip->calib_hkadc_work);
1604}
1605
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001606static void calibrate_ccadc_work(struct work_struct *work)
1607{
1608 struct pm8921_bms_chip *chip = container_of(work,
1609 struct pm8921_bms_chip, calib_ccadc_work.work);
1610
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08001611 pm8xxx_calib_ccadc();
Abhijeet Dharmapurikar1b8e8292012-01-17 11:01:44 -08001612 calib_hkadc(chip);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001613 schedule_delayed_work(&chip->calib_ccadc_work,
1614 round_jiffies_relative(msecs_to_jiffies
1615 (chip->calib_delay_ms)));
1616}
1617
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001618int pm8921_bms_get_vsense_avg(int *result)
1619{
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001620 int rc = -EINVAL;
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001621
1622 if (the_chip) {
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08001623 mutex_lock(&the_chip->bms_output_lock);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001624 pm_bms_lock_output_data(the_chip);
1625 rc = read_vsense_avg(the_chip, result);
1626 pm_bms_unlock_output_data(the_chip);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08001627 mutex_unlock(&the_chip->bms_output_lock);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001628 }
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001629
1630 pr_err("called before initialization\n");
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001631 return rc;
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001632}
1633EXPORT_SYMBOL(pm8921_bms_get_vsense_avg);
1634
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001635int pm8921_bms_get_battery_current(int *result_ua)
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001636{
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001637 int vsense;
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001638
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001639 if (!the_chip) {
1640 pr_err("called before initialization\n");
1641 return -EINVAL;
1642 }
1643 if (the_chip->r_sense == 0) {
1644 pr_err("r_sense is zero\n");
1645 return -EINVAL;
1646 }
1647
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08001648 mutex_lock(&the_chip->bms_output_lock);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001649 pm_bms_lock_output_data(the_chip);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001650 read_vsense_avg(the_chip, &vsense);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001651 pm_bms_unlock_output_data(the_chip);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08001652 mutex_unlock(&the_chip->bms_output_lock);
1653 pr_debug("vsense=%duV\n", vsense);
Abhijeet Dharmapurikara7a1c6b2011-08-17 10:04:58 -07001654 /* cast for signed division */
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001655 *result_ua = vsense * 1000 / (int)the_chip->r_sense;
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08001656 pr_debug("ibat=%duA\n", *result_ua);
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001657 return 0;
1658}
1659EXPORT_SYMBOL(pm8921_bms_get_battery_current);
1660
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001661int pm8921_bms_get_percent_charge(void)
1662{
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001663 int batt_temp, rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001664 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001665 struct pm8921_soc_params raw;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001666 int soc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001667
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001668 if (!the_chip) {
1669 pr_err("called before initialization\n");
1670 return -EINVAL;
1671 }
1672
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001673 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001674 if (rc) {
1675 pr_err("error reading adc channel = %d, rc = %d\n",
1676 the_chip->batt_temp_channel, rc);
1677 return rc;
1678 }
1679 pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical,
1680 result.measurement);
1681 batt_temp = (int)result.physical;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001682
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001683 mutex_lock(&the_chip->last_ocv_uv_mutex);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001684 read_soc_params_raw(the_chip, &raw);
1685
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001686 soc = calculate_state_of_charge(the_chip, &raw,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001687 batt_temp, last_chargecycles);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001688 mutex_unlock(&the_chip->last_ocv_uv_mutex);
1689 return soc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001690}
1691EXPORT_SYMBOL_GPL(pm8921_bms_get_percent_charge);
1692
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001693int pm8921_bms_get_rbatt(void)
1694{
1695 int batt_temp, rc;
1696 struct pm8xxx_adc_chan_result result;
1697 struct pm8921_soc_params raw;
1698 int fcc_uah;
1699 int unusable_charge_uah;
1700 int remaining_charge_uah;
1701 int cc_uah;
1702 int rbatt;
1703
1704 if (!the_chip) {
1705 pr_err("called before initialization\n");
1706 return -EINVAL;
1707 }
1708
1709 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
1710 if (rc) {
1711 pr_err("error reading adc channel = %d, rc = %d\n",
1712 the_chip->batt_temp_channel, rc);
1713 return rc;
1714 }
1715 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
1716 result.measurement);
1717 batt_temp = (int)result.physical;
1718
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001719 mutex_lock(&the_chip->last_ocv_uv_mutex);
1720
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001721 read_soc_params_raw(the_chip, &raw);
1722
1723 calculate_soc_params(the_chip, &raw, batt_temp, last_chargecycles,
1724 &fcc_uah,
1725 &unusable_charge_uah,
1726 &remaining_charge_uah,
1727 &cc_uah,
1728 &rbatt);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001729 mutex_unlock(&the_chip->last_ocv_uv_mutex);
1730
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001731 return rbatt;
1732}
1733EXPORT_SYMBOL_GPL(pm8921_bms_get_rbatt);
1734
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07001735int pm8921_bms_get_fcc(void)
1736{
1737 int batt_temp, rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001738 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07001739
1740 if (!the_chip) {
1741 pr_err("called before initialization\n");
1742 return -EINVAL;
1743 }
1744
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001745 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07001746 if (rc) {
1747 pr_err("error reading adc channel = %d, rc = %d\n",
1748 the_chip->batt_temp_channel, rc);
1749 return rc;
1750 }
1751 pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical,
1752 result.measurement);
1753 batt_temp = (int)result.physical;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001754 return calculate_fcc_uah(the_chip, batt_temp, last_chargecycles);
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07001755}
1756EXPORT_SYMBOL_GPL(pm8921_bms_get_fcc);
1757
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -08001758#define IBAT_TOL_MASK 0x0F
1759#define OCV_TOL_MASK 0xF0
1760#define IBAT_TOL_DEFAULT 0x03
1761#define IBAT_TOL_NOCHG 0x0F
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001762void pm8921_bms_charging_began(void)
1763{
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001764 int batt_temp, rc;
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08001765 struct pm8xxx_adc_chan_result result;
1766 struct pm8921_soc_params raw;
1767
1768 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
1769 if (rc) {
1770 pr_err("error reading adc channel = %d, rc = %d\n",
1771 the_chip->batt_temp_channel, rc);
1772 return;
1773 }
1774 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
1775 result.measurement);
1776 batt_temp = (int)result.physical;
1777
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001778 mutex_lock(&the_chip->last_ocv_uv_mutex);
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08001779 read_soc_params_raw(the_chip, &raw);
1780
1781 the_chip->start_percent = calculate_state_of_charge(the_chip, &raw,
1782 batt_temp, last_chargecycles);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001783 mutex_unlock(&the_chip->last_ocv_uv_mutex);
1784
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08001785 bms_start_percent = the_chip->start_percent;
1786 bms_start_ocv_uv = raw.last_good_ocv_uv;
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001787 calculate_cc_uah(the_chip, raw.cc, &bms_start_cc_uah);
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -08001788 pm_bms_masked_write(the_chip, BMS_TOLERANCES,
1789 IBAT_TOL_MASK, IBAT_TOL_DEFAULT);
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001790 pr_debug("start_percent = %u%%\n", the_chip->start_percent);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001791}
1792EXPORT_SYMBOL_GPL(pm8921_bms_charging_began);
1793
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08001794#define DELTA_FCC_PERCENT 3
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08001795#define MIN_START_PERCENT_FOR_LEARNING 30
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001796void pm8921_bms_charging_end(int is_battery_full)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001797{
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001798 int batt_temp, rc;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001799 struct pm8xxx_adc_chan_result result;
1800 struct pm8921_soc_params raw;
1801
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001802 if (the_chip == NULL)
1803 return;
1804
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001805 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
1806 if (rc) {
1807 pr_err("error reading adc channel = %d, rc = %d\n",
1808 the_chip->batt_temp_channel, rc);
1809 return;
1810 }
1811 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
1812 result.measurement);
1813 batt_temp = (int)result.physical;
1814
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001815 mutex_lock(&the_chip->last_ocv_uv_mutex);
1816
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001817 read_soc_params_raw(the_chip, &raw);
1818
Abhijeet Dharmapurikar57aeb922012-01-25 13:22:26 -08001819 calculate_cc_uah(the_chip, raw.cc, &bms_end_cc_uah);
1820
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001821 bms_end_ocv_uv = raw.last_good_ocv_uv;
1822
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08001823 if (is_battery_full
1824 && the_chip->start_percent <= MIN_START_PERCENT_FOR_LEARNING) {
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001825 int fcc_uah, new_fcc_uah, delta_fcc_uah;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001826
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001827 new_fcc_uah = calculate_real_fcc_uah(the_chip, &raw,
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08001828 batt_temp, last_chargecycles,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001829 &fcc_uah);
1830 delta_fcc_uah = new_fcc_uah - fcc_uah;
1831 if (delta_fcc_uah < 0)
1832 delta_fcc_uah = -delta_fcc_uah;
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08001833
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08001834 if (delta_fcc_uah * 100 > (DELTA_FCC_PERCENT * fcc_uah)) {
1835 /* new_fcc_uah is outside the scope limit it */
1836 if (new_fcc_uah > fcc_uah)
1837 new_fcc_uah
Abhijeet Dharmapurikar873ffd12012-04-17 00:06:15 -07001838 = (fcc_uah +
1839 (DELTA_FCC_PERCENT * fcc_uah) / 100);
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08001840 else
1841 new_fcc_uah
Abhijeet Dharmapurikar873ffd12012-04-17 00:06:15 -07001842 = (fcc_uah -
1843 (DELTA_FCC_PERCENT * fcc_uah) / 100);
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08001844
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08001845 pr_debug("delta_fcc=%d > %d percent of fcc=%d"
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08001846 "restring it to %d\n",
1847 delta_fcc_uah, DELTA_FCC_PERCENT,
1848 fcc_uah, new_fcc_uah);
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08001849 }
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -08001850
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08001851 last_real_fcc_mah = new_fcc_uah/1000;
1852 last_real_fcc_batt_temp = batt_temp;
1853 readjust_fcc_table();
1854
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001855 }
1856
1857 if (is_battery_full) {
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001858 the_chip->ocv_reading_at_100 = raw.last_good_ocv_raw;
1859 the_chip->cc_reading_at_100 = raw.cc;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001860
1861 the_chip->last_ocv_uv = the_chip->max_voltage_uv;
1862 raw.last_good_ocv_uv = the_chip->max_voltage_uv;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001863 /*
1864 * since we are treating this as an ocv event
1865 * forget the old cc value
1866 */
1867 the_chip->last_cc_uah = 0;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001868 pr_debug("EOC ocv_reading = 0x%x cc = 0x%x\n",
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -08001869 the_chip->ocv_reading_at_100,
1870 the_chip->cc_reading_at_100);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001871 }
1872
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001873 the_chip->end_percent = calculate_state_of_charge(the_chip, &raw,
1874 batt_temp, last_chargecycles);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001875 mutex_unlock(&the_chip->last_ocv_uv_mutex);
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08001876
1877 bms_end_percent = the_chip->end_percent;
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08001878
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001879 if (the_chip->end_percent > the_chip->start_percent) {
Abhijeet Dharmapurikar8a113f82012-01-19 10:58:45 -08001880 last_charge_increase +=
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001881 the_chip->end_percent - the_chip->start_percent;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001882 if (last_charge_increase > 100) {
1883 last_chargecycles++;
1884 last_charge_increase = last_charge_increase % 100;
1885 }
1886 }
1887 pr_debug("end_percent = %u%% last_charge_increase = %d"
1888 "last_chargecycles = %d\n",
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001889 the_chip->end_percent,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001890 last_charge_increase,
1891 last_chargecycles);
Abhijeet Dharmapurikardba91f42011-12-21 16:56:13 -08001892 the_chip->start_percent = -EINVAL;
1893 the_chip->end_percent = -EINVAL;
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -08001894 pm_bms_masked_write(the_chip, BMS_TOLERANCES,
1895 IBAT_TOL_MASK, IBAT_TOL_NOCHG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001896}
1897EXPORT_SYMBOL_GPL(pm8921_bms_charging_end);
1898
1899static irqreturn_t pm8921_bms_sbi_write_ok_handler(int irq, void *data)
1900{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001901 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001902 return IRQ_HANDLED;
1903}
1904
1905static irqreturn_t pm8921_bms_cc_thr_handler(int irq, void *data)
1906{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001907 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001908 return IRQ_HANDLED;
1909}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001910
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001911static irqreturn_t pm8921_bms_vsense_thr_handler(int irq, void *data)
1912{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001913 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001914 return IRQ_HANDLED;
1915}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001916
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001917static irqreturn_t pm8921_bms_vsense_for_r_handler(int irq, void *data)
1918{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001919 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001920 return IRQ_HANDLED;
1921}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001922
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001923static irqreturn_t pm8921_bms_ocv_for_r_handler(int irq, void *data)
1924{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001925 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001926
1927 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001928 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001929 return IRQ_HANDLED;
1930}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001931
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001932static irqreturn_t pm8921_bms_good_ocv_handler(int irq, void *data)
1933{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001934 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001935
1936 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001937 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001938 return IRQ_HANDLED;
1939}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001940
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001941static irqreturn_t pm8921_bms_vsense_avg_handler(int irq, void *data)
1942{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001943 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001944 return IRQ_HANDLED;
1945}
1946
1947struct pm_bms_irq_init_data {
1948 unsigned int irq_id;
1949 char *name;
1950 unsigned long flags;
1951 irqreturn_t (*handler)(int, void *);
1952};
1953
1954#define BMS_IRQ(_id, _flags, _handler) \
1955{ \
1956 .irq_id = _id, \
1957 .name = #_id, \
1958 .flags = _flags, \
1959 .handler = _handler, \
1960}
1961
1962struct pm_bms_irq_init_data bms_irq_data[] = {
1963 BMS_IRQ(PM8921_BMS_SBI_WRITE_OK, IRQF_TRIGGER_RISING,
1964 pm8921_bms_sbi_write_ok_handler),
1965 BMS_IRQ(PM8921_BMS_CC_THR, IRQF_TRIGGER_RISING,
1966 pm8921_bms_cc_thr_handler),
1967 BMS_IRQ(PM8921_BMS_VSENSE_THR, IRQF_TRIGGER_RISING,
1968 pm8921_bms_vsense_thr_handler),
1969 BMS_IRQ(PM8921_BMS_VSENSE_FOR_R, IRQF_TRIGGER_RISING,
1970 pm8921_bms_vsense_for_r_handler),
1971 BMS_IRQ(PM8921_BMS_OCV_FOR_R, IRQF_TRIGGER_RISING,
1972 pm8921_bms_ocv_for_r_handler),
1973 BMS_IRQ(PM8921_BMS_GOOD_OCV, IRQF_TRIGGER_RISING,
1974 pm8921_bms_good_ocv_handler),
1975 BMS_IRQ(PM8921_BMS_VSENSE_AVG, IRQF_TRIGGER_RISING,
1976 pm8921_bms_vsense_avg_handler),
1977};
1978
1979static void free_irqs(struct pm8921_bms_chip *chip)
1980{
1981 int i;
1982
1983 for (i = 0; i < PM_BMS_MAX_INTS; i++)
1984 if (chip->pmic_bms_irq[i]) {
1985 free_irq(chip->pmic_bms_irq[i], NULL);
1986 chip->pmic_bms_irq[i] = 0;
1987 }
1988}
1989
1990static int __devinit request_irqs(struct pm8921_bms_chip *chip,
1991 struct platform_device *pdev)
1992{
1993 struct resource *res;
1994 int ret, i;
1995
1996 ret = 0;
1997 bitmap_fill(chip->enabled_irqs, PM_BMS_MAX_INTS);
1998
1999 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
2000 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
2001 bms_irq_data[i].name);
2002 if (res == NULL) {
2003 pr_err("couldn't find %s\n", bms_irq_data[i].name);
2004 goto err_out;
2005 }
2006 ret = request_irq(res->start, bms_irq_data[i].handler,
2007 bms_irq_data[i].flags,
2008 bms_irq_data[i].name, chip);
2009 if (ret < 0) {
2010 pr_err("couldn't request %d (%s) %d\n", res->start,
2011 bms_irq_data[i].name, ret);
2012 goto err_out;
2013 }
2014 chip->pmic_bms_irq[bms_irq_data[i].irq_id] = res->start;
2015 pm8921_bms_disable_irq(chip, bms_irq_data[i].irq_id);
2016 }
2017 return 0;
2018
2019err_out:
2020 free_irqs(chip);
2021 return -EINVAL;
2022}
2023
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002024static int pm8921_bms_suspend(struct device *dev)
2025{
2026 int rc;
2027 struct pm8xxx_adc_chan_result result;
2028 struct pm8921_bms_chip *chip = dev_get_drvdata(dev);
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002029 struct pm8921_soc_params raw;
2030 int fcc_uah;
2031 int remaining_charge_uah;
2032 int cc_uah;
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002033
2034 chip->batt_temp_suspend = 0;
2035 rc = pm8xxx_adc_read(chip->batt_temp_channel, &result);
2036 if (rc) {
2037 pr_err("error reading adc channel = %d, rc = %d\n",
2038 chip->batt_temp_channel, rc);
2039 }
2040 chip->batt_temp_suspend = (int)result.physical;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002041
2042 mutex_lock(&chip->last_ocv_uv_mutex);
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002043 read_soc_params_raw(chip, &raw);
2044
2045 fcc_uah = calculate_fcc_uah(chip,
2046 chip->batt_temp_suspend, last_chargecycles);
2047 pr_debug("FCC = %uuAh batt_temp = %d, cycles = %d\n",
2048 fcc_uah, chip->batt_temp_suspend, last_chargecycles);
2049 /* calculate remainging charge */
2050 remaining_charge_uah = calculate_remaining_charge_uah(chip, &raw,
2051 fcc_uah, chip->batt_temp_suspend,
2052 last_chargecycles);
2053 pr_debug("RC = %uuAh\n", remaining_charge_uah);
2054
2055 /* calculate cc micro_volt_hour */
2056 calculate_cc_uah(chip, raw.cc, &cc_uah);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002057 pr_debug("cc_uah = %duAh raw->cc = %x cc = %lld after subtracting %x\n",
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002058 cc_uah, raw.cc,
2059 (int64_t)raw.cc - chip->cc_reading_at_100,
2060 chip->cc_reading_at_100);
2061 chip->soc_rbatt_suspend = ((remaining_charge_uah - cc_uah) * 100)
2062 / fcc_uah;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002063 mutex_unlock(&chip->last_ocv_uv_mutex);
2064
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002065 return 0;
2066}
2067
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002068#define DELTA_RBATT_PERCENT 10
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002069static int pm8921_bms_resume(struct device *dev)
2070{
2071 struct pm8921_rbatt_params raw;
2072 struct pm8921_bms_chip *chip = dev_get_drvdata(dev);
2073 int rbatt;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002074 int expected_rbatt;
2075 int scalefactor;
2076 int delta_rbatt;
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002077
2078 read_rbatt_params_raw(chip, &raw);
2079 rbatt = calculate_rbatt_resume(chip, &raw);
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002080
2081 if (rbatt < 0)
2082 return 0;
2083
2084 expected_rbatt
2085 = (last_rbatt < 0) ? chip->default_rbatt_mohm : last_rbatt;
2086
2087 if (chip->rbatt_sf_lut) {
2088 scalefactor = interpolate_scalingfactor(chip,
2089 chip->rbatt_sf_lut,
Abhijeet Dharmapurikar12a891c2012-04-12 23:52:06 -07002090 chip->batt_temp_suspend / 10,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002091 chip->soc_rbatt_suspend);
2092 rbatt = rbatt * 100 / scalefactor;
2093 }
2094
2095 delta_rbatt = expected_rbatt - rbatt;
2096 if (delta_rbatt)
2097 delta_rbatt = -delta_rbatt;
2098 /*
2099 * only update last_rbatt if rbatt is within some
2100 * percent of expected_rbatt
2101 */
2102 if (delta_rbatt * 100 <= DELTA_RBATT_PERCENT * expected_rbatt)
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002103 last_rbatt = rbatt;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002104
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002105 return 0;
2106}
2107
2108static const struct dev_pm_ops pm8921_pm_ops = {
2109 .suspend = pm8921_bms_suspend,
2110 .resume = pm8921_bms_resume,
2111};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002112#define EN_BMS_BIT BIT(7)
2113#define EN_PON_HS_BIT BIT(0)
2114static int __devinit pm8921_bms_hw_init(struct pm8921_bms_chip *chip)
2115{
2116 int rc;
2117
2118 rc = pm_bms_masked_write(chip, BMS_CONTROL,
2119 EN_BMS_BIT | EN_PON_HS_BIT, EN_BMS_BIT | EN_PON_HS_BIT);
2120 if (rc) {
2121 pr_err("failed to enable pon and bms addr = %d %d",
2122 BMS_CONTROL, rc);
2123 }
2124
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -08002125 /* The charger will call start charge later if usb is present */
2126 pm_bms_masked_write(chip, BMS_TOLERANCES,
2127 IBAT_TOL_MASK, IBAT_TOL_NOCHG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002128 return 0;
2129}
2130
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002131static void check_initial_ocv(struct pm8921_bms_chip *chip)
2132{
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002133 int ocv_uv, rc;
2134 int16_t ocv_raw;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08002135 int usb_chg;
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002136
2137 /*
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07002138 * Check if a ocv is available in bms hw,
2139 * if not compute it here at boot time and save it
2140 * in the last_ocv_uv.
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002141 */
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002142 ocv_uv = 0;
2143 pm_bms_read_output_data(chip, LAST_GOOD_OCV_VALUE, &ocv_raw);
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08002144 usb_chg = usb_chg_plugged_in();
2145 rc = convert_vbatt_raw_to_uv(chip, usb_chg, ocv_raw, &ocv_uv);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002146 if (rc || ocv_uv == 0) {
2147 rc = adc_based_ocv(chip, &ocv_uv);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07002148 if (rc) {
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002149 pr_err("failed to read adc based ocv_uv rc = %d\n", rc);
2150 ocv_uv = DEFAULT_OCV_MICROVOLTS;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07002151 }
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002152 }
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002153 chip->last_ocv_uv = ocv_uv;
2154 pr_debug("ocv_uv = %d last_ocv_uv = %d\n", ocv_uv, chip->last_ocv_uv);
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002155}
2156
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002157static int64_t read_battery_id(struct pm8921_bms_chip *chip)
2158{
2159 int rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07002160 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002161
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07002162 rc = pm8xxx_adc_read(chip->batt_id_channel, &result);
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002163 if (rc) {
2164 pr_err("error reading batt id channel = %d, rc = %d\n",
2165 chip->vbat_channel, rc);
2166 return rc;
2167 }
2168 pr_debug("batt_id phy = %lld meas = 0x%llx\n", result.physical,
2169 result.measurement);
David Keitel8f2601b2012-02-14 22:31:07 -08002170 return result.adc_code;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002171}
2172
David Keitel8f2601b2012-02-14 22:31:07 -08002173#define PALLADIUM_ID_MIN 0x7F40
2174#define PALLADIUM_ID_MAX 0x7F5A
2175#define DESAY_5200_ID_MIN 0x7F7F
2176#define DESAY_5200_ID_MAX 0x802F
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002177static int set_battery_data(struct pm8921_bms_chip *chip)
2178{
2179 int64_t battery_id;
2180
David Keitel35e11872012-02-17 17:40:42 -08002181 if (chip->batt_type == BATT_DESAY)
2182 goto desay;
2183 else if (chip->batt_type == BATT_PALLADIUM)
2184 goto palladium;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002185
David Keitel35e11872012-02-17 17:40:42 -08002186 battery_id = read_battery_id(chip);
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002187 if (battery_id < 0) {
2188 pr_err("cannot read battery id err = %lld\n", battery_id);
2189 return battery_id;
2190 }
2191
2192 if (is_between(PALLADIUM_ID_MIN, PALLADIUM_ID_MAX, battery_id)) {
David Keitel35e11872012-02-17 17:40:42 -08002193 goto palladium;
2194 } else if (is_between(DESAY_5200_ID_MIN, DESAY_5200_ID_MAX,
2195 battery_id)) {
2196 goto desay;
2197 } else {
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002198 pr_warn("invalid battid, palladium 1500 assumed batt_id %llx\n",
2199 battery_id);
2200 goto palladium;
David Keitel35e11872012-02-17 17:40:42 -08002201 }
2202
2203palladium:
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002204 chip->fcc = palladium_1500_data.fcc;
2205 chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut;
2206 chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut;
2207 chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut;
2208 chip->pc_sf_lut = palladium_1500_data.pc_sf_lut;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002209 chip->rbatt_sf_lut = palladium_1500_data.rbatt_sf_lut;
2210 chip->default_rbatt_mohm
2211 = palladium_1500_data.default_rbatt_mohm;
Abhijeet Dharmapurikarded189b2012-03-30 10:12:28 -07002212 chip->delta_rbatt_mohm = palladium_1500_data.delta_rbatt_mohm;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002213 return 0;
David Keitel35e11872012-02-17 17:40:42 -08002214desay:
David Keitel8f2601b2012-02-14 22:31:07 -08002215 chip->fcc = desay_5200_data.fcc;
2216 chip->fcc_temp_lut = desay_5200_data.fcc_temp_lut;
David Keitel8f2601b2012-02-14 22:31:07 -08002217 chip->pc_temp_ocv_lut = desay_5200_data.pc_temp_ocv_lut;
2218 chip->pc_sf_lut = desay_5200_data.pc_sf_lut;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002219 chip->rbatt_sf_lut = desay_5200_data.rbatt_sf_lut;
2220 chip->default_rbatt_mohm = desay_5200_data.default_rbatt_mohm;
Abhijeet Dharmapurikarded189b2012-03-30 10:12:28 -07002221 chip->delta_rbatt_mohm = desay_5200_data.delta_rbatt_mohm;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002222 return 0;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002223}
2224
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002225enum bms_request_operation {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002226 CALC_RBATT,
2227 CALC_FCC,
2228 CALC_PC,
2229 CALC_SOC,
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002230 CALIB_HKADC,
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002231 CALIB_CCADC,
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002232 GET_VBAT_VSENSE_SIMULTANEOUS,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002233};
2234
2235static int test_batt_temp = 5;
2236static int test_chargecycle = 150;
2237static int test_ocv = 3900000;
2238enum {
2239 TEST_BATT_TEMP,
2240 TEST_CHARGE_CYCLE,
2241 TEST_OCV,
2242};
2243static int get_test_param(void *data, u64 * val)
2244{
2245 switch ((int)data) {
2246 case TEST_BATT_TEMP:
2247 *val = test_batt_temp;
2248 break;
2249 case TEST_CHARGE_CYCLE:
2250 *val = test_chargecycle;
2251 break;
2252 case TEST_OCV:
2253 *val = test_ocv;
2254 break;
2255 default:
2256 return -EINVAL;
2257 }
2258 return 0;
2259}
2260static int set_test_param(void *data, u64 val)
2261{
2262 switch ((int)data) {
2263 case TEST_BATT_TEMP:
2264 test_batt_temp = (int)val;
2265 break;
2266 case TEST_CHARGE_CYCLE:
2267 test_chargecycle = (int)val;
2268 break;
2269 case TEST_OCV:
2270 test_ocv = (int)val;
2271 break;
2272 default:
2273 return -EINVAL;
2274 }
2275 return 0;
2276}
2277DEFINE_SIMPLE_ATTRIBUTE(temp_fops, get_test_param, set_test_param, "%llu\n");
2278
2279static int get_calc(void *data, u64 * val)
2280{
2281 int param = (int)data;
2282 int ret = 0;
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002283 int ibat_ua, vbat_uv;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002284 struct pm8921_soc_params raw;
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002285 struct pm8921_rbatt_params rraw;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002286
2287 read_soc_params_raw(the_chip, &raw);
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002288 read_rbatt_params_raw(the_chip, &rraw);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002289
2290 *val = 0;
2291
2292 /* global irq number passed in via data */
2293 switch (param) {
2294 case CALC_RBATT:
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002295 *val = calculate_rbatt_resume(the_chip, &rraw);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002296 break;
2297 case CALC_FCC:
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002298 *val = calculate_fcc_uah(the_chip, test_batt_temp,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002299 test_chargecycle);
2300 break;
2301 case CALC_PC:
2302 *val = calculate_pc(the_chip, test_ocv, test_batt_temp,
2303 test_chargecycle);
2304 break;
2305 case CALC_SOC:
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002306 *val = calculate_state_of_charge(the_chip, &raw,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002307 test_batt_temp, test_chargecycle);
2308 break;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002309 case CALIB_HKADC:
2310 /* reading this will trigger calibration */
2311 *val = 0;
2312 calib_hkadc(the_chip);
2313 break;
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002314 case CALIB_CCADC:
2315 /* reading this will trigger calibration */
2316 *val = 0;
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08002317 pm8xxx_calib_ccadc();
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002318 break;
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002319 case GET_VBAT_VSENSE_SIMULTANEOUS:
2320 /* reading this will call simultaneous vbat and vsense */
2321 *val =
2322 pm8921_bms_get_simultaneous_battery_voltage_and_current(
2323 &ibat_ua,
2324 &vbat_uv);
2325 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002326 default:
2327 ret = -EINVAL;
2328 }
2329 return ret;
2330}
2331DEFINE_SIMPLE_ATTRIBUTE(calc_fops, get_calc, NULL, "%llu\n");
2332
2333static int get_reading(void *data, u64 * val)
2334{
2335 int param = (int)data;
2336 int ret = 0;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002337 struct pm8921_soc_params raw;
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002338 struct pm8921_rbatt_params rraw;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002339
2340 read_soc_params_raw(the_chip, &raw);
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002341 read_rbatt_params_raw(the_chip, &rraw);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002342
2343 *val = 0;
2344
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002345 switch (param) {
2346 case CC_MSB:
2347 case CC_LSB:
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002348 *val = raw.cc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002349 break;
2350 case LAST_GOOD_OCV_VALUE:
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002351 *val = raw.last_good_ocv_uv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002352 break;
2353 case VBATT_FOR_RBATT:
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002354 *val = rraw.vbatt_for_rbatt_uv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002355 break;
2356 case VSENSE_FOR_RBATT:
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002357 *val = rraw.vsense_for_rbatt_uv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002358 break;
2359 case OCV_FOR_RBATT:
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002360 *val = rraw.ocv_for_rbatt_uv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002361 break;
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07002362 case VSENSE_AVG:
2363 read_vsense_avg(the_chip, (uint *)val);
2364 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002365 default:
2366 ret = -EINVAL;
2367 }
2368 return ret;
2369}
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07002370DEFINE_SIMPLE_ATTRIBUTE(reading_fops, get_reading, NULL, "%lld\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002371
2372static int get_rt_status(void *data, u64 * val)
2373{
2374 int i = (int)data;
2375 int ret;
2376
2377 /* global irq number passed in via data */
2378 ret = pm_bms_get_rt_status(the_chip, i);
2379 *val = ret;
2380 return 0;
2381}
2382DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n");
2383
2384static int get_reg(void *data, u64 * val)
2385{
2386 int addr = (int)data;
2387 int ret;
2388 u8 temp;
2389
2390 ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp);
2391 if (ret) {
2392 pr_err("pm8xxx_readb to %x value = %d errored = %d\n",
2393 addr, temp, ret);
2394 return -EAGAIN;
2395 }
2396 *val = temp;
2397 return 0;
2398}
2399
2400static int set_reg(void *data, u64 val)
2401{
2402 int addr = (int)data;
2403 int ret;
2404 u8 temp;
2405
2406 temp = (u8) val;
2407 ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp);
2408 if (ret) {
2409 pr_err("pm8xxx_writeb to %x value = %d errored = %d\n",
2410 addr, temp, ret);
2411 return -EAGAIN;
2412 }
2413 return 0;
2414}
2415DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
2416
2417static void create_debugfs_entries(struct pm8921_bms_chip *chip)
2418{
2419 int i;
2420
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08002421 chip->dent = debugfs_create_dir("pm8921-bms", NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002422
2423 if (IS_ERR(chip->dent)) {
2424 pr_err("pmic bms couldnt create debugfs dir\n");
2425 return;
2426 }
2427
2428 debugfs_create_file("BMS_CONTROL", 0644, chip->dent,
2429 (void *)BMS_CONTROL, &reg_fops);
2430 debugfs_create_file("BMS_OUTPUT0", 0644, chip->dent,
2431 (void *)BMS_OUTPUT0, &reg_fops);
2432 debugfs_create_file("BMS_OUTPUT1", 0644, chip->dent,
2433 (void *)BMS_OUTPUT1, &reg_fops);
2434 debugfs_create_file("BMS_TEST1", 0644, chip->dent,
2435 (void *)BMS_TEST1, &reg_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002436
2437 debugfs_create_file("test_batt_temp", 0644, chip->dent,
2438 (void *)TEST_BATT_TEMP, &temp_fops);
2439 debugfs_create_file("test_chargecycle", 0644, chip->dent,
2440 (void *)TEST_CHARGE_CYCLE, &temp_fops);
2441 debugfs_create_file("test_ocv", 0644, chip->dent,
2442 (void *)TEST_OCV, &temp_fops);
2443
2444 debugfs_create_file("read_cc", 0644, chip->dent,
2445 (void *)CC_MSB, &reading_fops);
2446 debugfs_create_file("read_last_good_ocv", 0644, chip->dent,
2447 (void *)LAST_GOOD_OCV_VALUE, &reading_fops);
2448 debugfs_create_file("read_vbatt_for_rbatt", 0644, chip->dent,
2449 (void *)VBATT_FOR_RBATT, &reading_fops);
2450 debugfs_create_file("read_vsense_for_rbatt", 0644, chip->dent,
2451 (void *)VSENSE_FOR_RBATT, &reading_fops);
2452 debugfs_create_file("read_ocv_for_rbatt", 0644, chip->dent,
2453 (void *)OCV_FOR_RBATT, &reading_fops);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07002454 debugfs_create_file("read_vsense_avg", 0644, chip->dent,
2455 (void *)VSENSE_AVG, &reading_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002456
2457 debugfs_create_file("show_rbatt", 0644, chip->dent,
2458 (void *)CALC_RBATT, &calc_fops);
2459 debugfs_create_file("show_fcc", 0644, chip->dent,
2460 (void *)CALC_FCC, &calc_fops);
2461 debugfs_create_file("show_pc", 0644, chip->dent,
2462 (void *)CALC_PC, &calc_fops);
2463 debugfs_create_file("show_soc", 0644, chip->dent,
2464 (void *)CALC_SOC, &calc_fops);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002465 debugfs_create_file("calib_hkadc", 0644, chip->dent,
2466 (void *)CALIB_HKADC, &calc_fops);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002467 debugfs_create_file("calib_ccadc", 0644, chip->dent,
2468 (void *)CALIB_CCADC, &calc_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002469
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002470 debugfs_create_file("simultaneous", 0644, chip->dent,
2471 (void *)GET_VBAT_VSENSE_SIMULTANEOUS, &calc_fops);
2472
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002473 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
2474 if (chip->pmic_bms_irq[bms_irq_data[i].irq_id])
2475 debugfs_create_file(bms_irq_data[i].name, 0444,
2476 chip->dent,
2477 (void *)bms_irq_data[i].irq_id,
2478 &rt_fops);
2479 }
2480}
2481
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -07002482#define REG_SBI_CONFIG 0x04F
2483#define PAGE3_ENABLE_MASK 0x6
2484#define PROGRAM_REV_MASK 0x0F
2485#define PROGRAM_REV 0x9
2486static int read_ocv_trim(struct pm8921_bms_chip *chip)
2487{
2488 int rc;
2489 u8 reg, sbi_config;
2490
2491 rc = pm8xxx_readb(chip->dev->parent, REG_SBI_CONFIG, &sbi_config);
2492 if (rc) {
2493 pr_err("error = %d reading sbi config reg\n", rc);
2494 return rc;
2495 }
2496
2497 reg = sbi_config | PAGE3_ENABLE_MASK;
2498 rc = pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, reg);
2499 if (rc) {
2500 pr_err("error = %d writing sbi config reg\n", rc);
2501 return rc;
2502 }
2503
2504 rc = pm8xxx_readb(chip->dev->parent, TEST_PROGRAM_REV, &reg);
2505 if (rc)
2506 pr_err("Error %d reading %d addr %d\n",
2507 rc, reg, TEST_PROGRAM_REV);
2508 pr_err("program rev reg is 0x%x\n", reg);
2509 reg &= PROGRAM_REV_MASK;
2510
2511 /* If the revision is equal or higher do not adjust trim delta */
2512 if (reg >= PROGRAM_REV) {
2513 chip->amux_2_trim_delta = 0;
2514 goto restore_sbi_config;
2515 }
2516
2517 rc = pm8xxx_readb(chip->dev->parent, AMUX_TRIM_2, &reg);
2518 if (rc) {
2519 pr_err("error = %d reading trim reg\n", rc);
2520 return rc;
2521 }
2522
2523 pr_err("trim reg is 0x%x\n", reg);
2524 chip->amux_2_trim_delta = abs(0x49 - reg);
2525 pr_err("trim delta is %d\n", chip->amux_2_trim_delta);
2526
2527restore_sbi_config:
2528 rc = pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, sbi_config);
2529 if (rc) {
2530 pr_err("error = %d writing sbi config reg\n", rc);
2531 return rc;
2532 }
2533
2534 return 0;
2535}
2536
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002537static int __devinit pm8921_bms_probe(struct platform_device *pdev)
2538{
2539 int rc = 0;
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07002540 int vbatt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002541 struct pm8921_bms_chip *chip;
2542 const struct pm8921_bms_platform_data *pdata
2543 = pdev->dev.platform_data;
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07002544
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002545 if (!pdata) {
2546 pr_err("missing platform data\n");
2547 return -EINVAL;
2548 }
2549
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002550 chip = kzalloc(sizeof(struct pm8921_bms_chip), GFP_KERNEL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002551 if (!chip) {
2552 pr_err("Cannot allocate pm_bms_chip\n");
2553 return -ENOMEM;
2554 }
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002555
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002556 mutex_init(&chip->bms_output_lock);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002557 mutex_init(&chip->last_ocv_uv_mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002558 chip->dev = &pdev->dev;
2559 chip->r_sense = pdata->r_sense;
2560 chip->i_test = pdata->i_test;
2561 chip->v_failure = pdata->v_failure;
2562 chip->calib_delay_ms = pdata->calib_delay_ms;
Abhijeet Dharmapurikara93ede82011-11-17 12:20:03 -08002563 chip->max_voltage_uv = pdata->max_voltage_uv;
David Keitel35e11872012-02-17 17:40:42 -08002564 chip->batt_type = pdata->battery_type;
Abhijeet Dharmapurikarbaffba42012-03-22 14:41:10 -07002565 chip->rconn_mohm = pdata->rconn_mohm;
Abhijeet Dharmapurikardba91f42011-12-21 16:56:13 -08002566 chip->start_percent = -EINVAL;
2567 chip->end_percent = -EINVAL;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002568 rc = set_battery_data(chip);
2569 if (rc) {
2570 pr_err("%s bad battery data %d\n", __func__, rc);
Abhijeet Dharmapurikar4a4456e2011-09-27 15:10:38 -07002571 goto free_chip;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002572 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002573
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002574 if (chip->pc_temp_ocv_lut == NULL) {
2575 pr_err("temp ocv lut table is NULL\n");
2576 rc = -EINVAL;
2577 goto free_chip;
2578 }
2579
2580 /* set defaults in the battery data */
2581 if (chip->default_rbatt_mohm <= 0)
2582 chip->default_rbatt_mohm = DEFAULT_RBATT_MOHMS;
2583
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07002584 chip->batt_temp_channel = pdata->bms_cdata.batt_temp_channel;
2585 chip->vbat_channel = pdata->bms_cdata.vbat_channel;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002586 chip->ref625mv_channel = pdata->bms_cdata.ref625mv_channel;
2587 chip->ref1p25v_channel = pdata->bms_cdata.ref1p25v_channel;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002588 chip->batt_id_channel = pdata->bms_cdata.batt_id_channel;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -07002589 chip->revision = pm8xxx_get_revision(chip->dev->parent);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002590 INIT_WORK(&chip->calib_hkadc_work, calibrate_hkadc_work);
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07002591
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002592 rc = request_irqs(chip, pdev);
2593 if (rc) {
2594 pr_err("couldn't register interrupts rc = %d\n", rc);
2595 goto free_chip;
2596 }
2597
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002598 rc = pm8921_bms_hw_init(chip);
2599 if (rc) {
2600 pr_err("couldn't init hardware rc = %d\n", rc);
2601 goto free_irqs;
2602 }
2603
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002604 platform_set_drvdata(pdev, chip);
2605 the_chip = chip;
2606 create_debugfs_entries(chip);
2607
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -07002608 rc = read_ocv_trim(chip);
2609 if (rc) {
2610 pr_err("couldn't adjust ocv_trim rc= %d\n", rc);
2611 goto free_irqs;
2612 }
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002613 check_initial_ocv(chip);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002614
2615 INIT_DELAYED_WORK(&chip->calib_ccadc_work, calibrate_ccadc_work);
2616 /* begin calibration only on chips > 2.0 */
2617 if (chip->revision >= PM8XXX_REVISION_8921_2p0)
Abhijeet Dharmapurikarccfc4f32012-01-16 17:35:18 -08002618 schedule_delayed_work(&chip->calib_ccadc_work, 0);
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002619
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07002620 /* initial hkadc calibration */
2621 schedule_work(&chip->calib_hkadc_work);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002622 /* enable the vbatt reading interrupts for scheduling hkadc calib */
2623 pm8921_bms_enable_irq(chip, PM8921_BMS_GOOD_OCV);
2624 pm8921_bms_enable_irq(chip, PM8921_BMS_OCV_FOR_R);
Abhijeet Dharmapurikar21ebadd2011-09-28 21:14:16 -07002625
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07002626 get_battery_uvolts(chip, &vbatt);
2627 pr_info("OK battery_capacity_at_boot=%d volt = %d ocv = %d\n",
2628 pm8921_bms_get_percent_charge(),
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002629 vbatt, chip->last_ocv_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002630 return 0;
2631
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002632free_irqs:
2633 free_irqs(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002634free_chip:
2635 kfree(chip);
2636 return rc;
2637}
2638
2639static int __devexit pm8921_bms_remove(struct platform_device *pdev)
2640{
2641 struct pm8921_bms_chip *chip = platform_get_drvdata(pdev);
2642
2643 free_irqs(chip);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07002644 kfree(chip->adjusted_fcc_temp_lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002645 platform_set_drvdata(pdev, NULL);
2646 the_chip = NULL;
2647 kfree(chip);
2648 return 0;
2649}
2650
2651static struct platform_driver pm8921_bms_driver = {
2652 .probe = pm8921_bms_probe,
2653 .remove = __devexit_p(pm8921_bms_remove),
2654 .driver = {
2655 .name = PM8921_BMS_DEV_NAME,
2656 .owner = THIS_MODULE,
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002657 .pm = &pm8921_pm_ops
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002658 },
2659};
2660
2661static int __init pm8921_bms_init(void)
2662{
2663 return platform_driver_register(&pm8921_bms_driver);
2664}
2665
2666static void __exit pm8921_bms_exit(void)
2667{
2668 platform_driver_unregister(&pm8921_bms_driver);
2669}
2670
2671late_initcall(pm8921_bms_init);
2672module_exit(pm8921_bms_exit);
2673
2674MODULE_LICENSE("GPL v2");
2675MODULE_DESCRIPTION("PMIC8921 bms driver");
2676MODULE_VERSION("1.0");
2677MODULE_ALIAS("platform:" PM8921_BMS_DEV_NAME);