blob: 85d8a0822c32de24fd8e7b495a586f17c56006ce [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
2 *
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>
19#include <linux/mfd/pm8xxx/pm8921-bms.h>
20#include <linux/mfd/pm8xxx/core.h>
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -070021#include <linux/mfd/pm8xxx/pm8xxx-adc.h>
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -080022#include <linux/mfd/pm8xxx/ccadc.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070023#include <linux/interrupt.h>
24#include <linux/bitops.h>
25#include <linux/debugfs.h>
26#include <linux/slab.h>
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070027#include <linux/delay.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028
29#define BMS_CONTROL 0x224
30#define BMS_OUTPUT0 0x230
31#define BMS_OUTPUT1 0x231
32#define BMS_TEST1 0x237
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070034#define ADC_ARB_SECP_CNTRL 0x190
35#define ADC_ARB_SECP_AMUX_CNTRL 0x191
36#define ADC_ARB_SECP_ANA_PARAM 0x192
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070037#define ADC_ARB_SECP_DIG_PARAM 0x193
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070038#define ADC_ARB_SECP_RSV 0x194
39#define ADC_ARB_SECP_DATA1 0x195
40#define ADC_ARB_SECP_DATA0 0x196
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070041
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070042#define ADC_ARB_BMS_CNTRL 0x18D
43
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044enum pmic_bms_interrupts {
45 PM8921_BMS_SBI_WRITE_OK,
46 PM8921_BMS_CC_THR,
47 PM8921_BMS_VSENSE_THR,
48 PM8921_BMS_VSENSE_FOR_R,
49 PM8921_BMS_OCV_FOR_R,
50 PM8921_BMS_GOOD_OCV,
51 PM8921_BMS_VSENSE_AVG,
52 PM_BMS_MAX_INTS,
53};
54
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055struct pm8921_bms_chip {
56 struct device *dev;
57 struct dentry *dent;
58 unsigned int r_sense;
59 unsigned int i_test;
60 unsigned int v_failure;
61 unsigned int fcc;
62 struct single_row_lut *fcc_temp_lut;
63 struct single_row_lut *fcc_sf_lut;
64 struct pc_temp_ocv_lut *pc_temp_ocv_lut;
65 struct pc_sf_lut *pc_sf_lut;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070066 struct work_struct calib_hkadc_work;
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070067 struct delayed_work calib_ccadc_work;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068 unsigned int calib_delay_ms;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -070069 unsigned int revision;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070070 unsigned int xoadc_v0625;
71 unsigned int xoadc_v125;
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -070072 unsigned int batt_temp_channel;
73 unsigned int vbat_channel;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070074 unsigned int ref625mv_channel;
75 unsigned int ref1p25v_channel;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -070076 unsigned int batt_id_channel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070077 unsigned int pmic_bms_irq[PM_BMS_MAX_INTS];
78 DECLARE_BITMAP(enabled_irqs, PM_BMS_MAX_INTS);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -070079 spinlock_t bms_output_lock;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -070080 struct single_row_lut *adjusted_fcc_temp_lut;
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -070081 unsigned int charging_began;
82 unsigned int start_percent;
83 unsigned int end_percent;
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -080084
85 uint16_t ocv_reading_at_100;
86 int cc_reading_at_100;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070087};
88
89static struct pm8921_bms_chip *the_chip;
90
91#define DEFAULT_RBATT_MOHMS 128
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092#define DEFAULT_OCV_MICROVOLTS 3900000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070093#define DEFAULT_CHARGE_CYCLES 0
94
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -070095static int last_chargecycles = DEFAULT_CHARGE_CYCLES;
96static int last_charge_increase;
97module_param(last_chargecycles, int, 0644);
98module_param(last_charge_increase, int, 0644);
99
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100static int last_rbatt = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101static int last_ocv_uv = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102static int last_soc = -EINVAL;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700103static int last_real_fcc = -EINVAL;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700104static int last_real_fcc_batt_temp = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700106static int bms_ops_set(const char *val, const struct kernel_param *kp)
107{
108 if (*(int *)kp->arg == -EINVAL)
109 return param_set_int(val, kp);
110 else
111 return 0;
112}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700113
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700114static struct kernel_param_ops bms_param_ops = {
115 .set = bms_ops_set,
116 .get = param_get_int,
117};
118
119module_param_cb(last_rbatt, &bms_param_ops, &last_rbatt, 0644);
120module_param_cb(last_ocv_uv, &bms_param_ops, &last_ocv_uv, 0644);
121module_param_cb(last_soc, &bms_param_ops, &last_soc, 0644);
122
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -0800123/*
124 * bms_fake_battery is write only, this value is set in setups where a
125 * battery emulator is used instead of a real battery. This makes the
126 * bms driver report a higher value of charge regardless of the calculated
127 * state of charge.
128 */
129static int bms_fake_battery;
130module_param(bms_fake_battery, int, 0644);
131
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700132static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp);
133static void readjust_fcc_table(void)
134{
135 struct single_row_lut *temp, *old;
136 int i, fcc, ratio;
137
138 if (!the_chip->fcc_temp_lut) {
139 pr_err("The static fcc lut table is NULL\n");
140 return;
141 }
142
143 temp = kzalloc(sizeof(struct single_row_lut), GFP_KERNEL);
144 if (!temp) {
145 pr_err("Cannot allocate memory for adjusted fcc table\n");
146 return;
147 }
148
149 fcc = interpolate_fcc(the_chip, last_real_fcc_batt_temp);
150
151 temp->cols = the_chip->fcc_temp_lut->cols;
152 for (i = 0; i < the_chip->fcc_temp_lut->cols; i++) {
153 temp->x[i] = the_chip->fcc_temp_lut->x[i];
154 ratio = div_u64(the_chip->fcc_temp_lut->y[i] * 1000, fcc);
155 temp->y[i] = (ratio * last_real_fcc);
156 temp->y[i] /= 1000;
157 pr_debug("temp=%d, staticfcc=%d, adjfcc=%d, ratio=%d\n",
158 temp->x[i], the_chip->fcc_temp_lut->y[i],
159 temp->y[i], ratio);
160 }
161
162 old = the_chip->adjusted_fcc_temp_lut;
163 the_chip->adjusted_fcc_temp_lut = temp;
164 kfree(old);
165}
166
167static int bms_last_real_fcc_set(const char *val,
168 const struct kernel_param *kp)
169{
170 int rc = 0;
171
172 if (last_real_fcc == -EINVAL)
173 rc = param_set_int(val, kp);
174 if (rc) {
175 pr_err("Failed to set last_real_fcc rc=%d\n", rc);
176 return rc;
177 }
178 if (last_real_fcc_batt_temp != -EINVAL)
179 readjust_fcc_table();
180 return rc;
181}
182static struct kernel_param_ops bms_last_real_fcc_param_ops = {
183 .set = bms_last_real_fcc_set,
184 .get = param_get_int,
185};
186module_param_cb(last_real_fcc, &bms_last_real_fcc_param_ops,
187 &last_real_fcc, 0644);
188
189static int bms_last_real_fcc_batt_temp_set(const char *val,
190 const struct kernel_param *kp)
191{
192 int rc = 0;
193
194 if (last_real_fcc_batt_temp == -EINVAL)
195 rc = param_set_int(val, kp);
196 if (rc) {
197 pr_err("Failed to set last_real_fcc_batt_temp rc=%d\n", rc);
198 return rc;
199 }
200 if (last_real_fcc != -EINVAL)
201 readjust_fcc_table();
202 return rc;
203}
204
205static struct kernel_param_ops bms_last_real_fcc_batt_temp_param_ops = {
206 .set = bms_last_real_fcc_batt_temp_set,
207 .get = param_get_int,
208};
209module_param_cb(last_real_fcc_batt_temp, &bms_last_real_fcc_batt_temp_param_ops,
210 &last_real_fcc_batt_temp, 0644);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211
212static int pm_bms_get_rt_status(struct pm8921_bms_chip *chip, int irq_id)
213{
214 return pm8xxx_read_irq_stat(chip->dev->parent,
215 chip->pmic_bms_irq[irq_id]);
216}
217
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700218static void pm8921_bms_enable_irq(struct pm8921_bms_chip *chip, int interrupt)
219{
220 if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) {
221 dev_dbg(chip->dev, "%s %d\n", __func__,
222 chip->pmic_bms_irq[interrupt]);
223 enable_irq(chip->pmic_bms_irq[interrupt]);
224 }
225}
226
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227static void pm8921_bms_disable_irq(struct pm8921_bms_chip *chip, int interrupt)
228{
229 if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700230 pr_debug("%d\n", chip->pmic_bms_irq[interrupt]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700231 disable_irq_nosync(chip->pmic_bms_irq[interrupt]);
232 }
233}
234
235static int pm_bms_masked_write(struct pm8921_bms_chip *chip, u16 addr,
236 u8 mask, u8 val)
237{
238 int rc;
239 u8 reg;
240
241 rc = pm8xxx_readb(chip->dev->parent, addr, &reg);
242 if (rc) {
243 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
244 return rc;
245 }
246 reg &= ~mask;
247 reg |= val & mask;
248 rc = pm8xxx_writeb(chip->dev->parent, addr, reg);
249 if (rc) {
250 pr_err("write failed addr = %03X, rc = %d\n", addr, rc);
251 return rc;
252 }
253 return 0;
254}
255
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700256#define HOLD_OREG_DATA BIT(1)
257static int pm_bms_lock_output_data(struct pm8921_bms_chip *chip)
258{
259 int rc;
260
261 rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA,
262 HOLD_OREG_DATA);
263 if (rc) {
264 pr_err("couldnt lock bms output rc = %d\n", rc);
265 return rc;
266 }
267 return 0;
268}
269
270static int pm_bms_unlock_output_data(struct pm8921_bms_chip *chip)
271{
272 int rc;
273
274 rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA, 0);
275 if (rc) {
276 pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc);
277 return rc;
278 }
279 return 0;
280}
281
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700282#define SELECT_OUTPUT_DATA 0x1C
283#define SELECT_OUTPUT_TYPE_SHIFT 2
284#define OCV_FOR_RBATT 0x0
285#define VSENSE_FOR_RBATT 0x1
286#define VBATT_FOR_RBATT 0x2
287#define CC_MSB 0x3
288#define CC_LSB 0x4
289#define LAST_GOOD_OCV_VALUE 0x5
290#define VSENSE_AVG 0x6
291#define VBATT_AVG 0x7
292
293static int pm_bms_read_output_data(struct pm8921_bms_chip *chip, int type,
294 int16_t *result)
295{
296 int rc;
297 u8 reg;
298
299 if (!result) {
300 pr_err("result pointer null\n");
301 return -EINVAL;
302 }
303 *result = 0;
304 if (type < OCV_FOR_RBATT || type > VBATT_AVG) {
305 pr_err("invalid type %d asked to read\n", type);
306 return -EINVAL;
307 }
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700308
309 /* make sure the bms registers are locked */
310 rc = pm8xxx_readb(chip->dev->parent, BMS_CONTROL, &reg);
311 if (rc) {
312 pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n",
313 type, rc);
314 return rc;
315 }
316
317 /* Output register data must be held (locked) while reading output */
318 WARN_ON(!(reg && HOLD_OREG_DATA));
319
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700320 rc = pm_bms_masked_write(chip, BMS_CONTROL, SELECT_OUTPUT_DATA,
321 type << SELECT_OUTPUT_TYPE_SHIFT);
322 if (rc) {
323 pr_err("fail to select %d type in BMS_CONTROL rc = %d\n",
324 type, rc);
325 return rc;
326 }
327
328 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT0, &reg);
329 if (rc) {
330 pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n",
331 type, rc);
332 return rc;
333 }
334 *result = reg;
335 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT1, &reg);
336 if (rc) {
337 pr_err("fail to read BMS_OUTPUT1 for type %d rc = %d\n",
338 type, rc);
339 return rc;
340 }
341 *result |= reg << 8;
342 pr_debug("type %d result %x", type, *result);
343 return 0;
344}
345
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700346#define V_PER_BIT_MUL_FACTOR 97656
347#define V_PER_BIT_DIV_FACTOR 1000
348#define XOADC_INTRINSIC_OFFSET 0x6000
349static int xoadc_reading_to_microvolt(unsigned int a)
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700350{
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700351 if (a <= XOADC_INTRINSIC_OFFSET)
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700352 return 0;
353
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700354 return (a - XOADC_INTRINSIC_OFFSET)
355 * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR;
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700356}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700357
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700358#define XOADC_CALIB_UV 625000
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700359#define VBATT_MUL_FACTOR 3
360static int adjust_xo_vbatt_reading(struct pm8921_bms_chip *chip,
361 unsigned int uv)
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700362{
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700363 u64 numerator, denominator;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700364
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700365 if (uv == 0)
366 return 0;
367
368 numerator = ((u64)uv - chip->xoadc_v0625) * XOADC_CALIB_UV;
369 denominator = chip->xoadc_v125 - chip->xoadc_v0625;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700370 if (denominator == 0)
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700371 return uv * VBATT_MUL_FACTOR;
372 return (XOADC_CALIB_UV + div_u64(numerator, denominator))
373 * VBATT_MUL_FACTOR;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700374}
375
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700376#define CC_RESOLUTION_N_V1 1085069
377#define CC_RESOLUTION_D_V1 100000
378#define CC_RESOLUTION_N_V2 868056
379#define CC_RESOLUTION_D_V2 10000
380static s64 cc_to_microvolt_v1(s64 cc)
381{
382 return div_s64(cc * CC_RESOLUTION_N_V1, CC_RESOLUTION_D_V1);
383}
384
385static s64 cc_to_microvolt_v2(s64 cc)
386{
387 return div_s64(cc * CC_RESOLUTION_N_V2, CC_RESOLUTION_D_V2);
388}
389
390static s64 cc_to_microvolt(struct pm8921_bms_chip *chip, s64 cc)
391{
392 /*
393 * resolution (the value of a single bit) was changed after revision 2.0
394 * for more accurate readings
395 */
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -0700396 return (chip->revision < PM8XXX_REVISION_8921_2p0) ?
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700397 cc_to_microvolt_v1((s64)cc) :
398 cc_to_microvolt_v2((s64)cc);
399}
400
401#define CC_READING_TICKS 55
402#define SLEEP_CLK_HZ 32768
403#define SECONDS_PER_HOUR 3600
404static s64 ccmicrovolt_to_uvh(s64 cc_uv)
405{
406 return div_s64(cc_uv * CC_READING_TICKS,
407 SLEEP_CLK_HZ * SECONDS_PER_HOUR);
408}
409
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700410/* returns the signed value read from the hardware */
411static int read_cc(struct pm8921_bms_chip *chip, int *result)
412{
413 int rc;
414 uint16_t msw, lsw;
415
416 rc = pm_bms_read_output_data(chip, CC_LSB, &lsw);
417 if (rc) {
418 pr_err("fail to read CC_LSB rc = %d\n", rc);
419 return rc;
420 }
421 rc = pm_bms_read_output_data(chip, CC_MSB, &msw);
422 if (rc) {
423 pr_err("fail to read CC_MSB rc = %d\n", rc);
424 return rc;
425 }
426 *result = msw << 16 | lsw;
427 pr_debug("msw = %04x lsw = %04x cc = %d\n", msw, lsw, *result);
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -0800428 *result = *result - chip->cc_reading_at_100;
429 pr_debug("cc = %d after subtracting %d\n",
430 *result, chip->cc_reading_at_100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431 return 0;
432}
433
434static int read_last_good_ocv(struct pm8921_bms_chip *chip, uint *result)
435{
436 int rc;
437 uint16_t reading;
438
439 rc = pm_bms_read_output_data(chip, LAST_GOOD_OCV_VALUE, &reading);
440 if (rc) {
441 pr_err("fail to read LAST_GOOD_OCV_VALUE rc = %d\n", rc);
442 return rc;
443 }
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -0800444
445 if (chip->ocv_reading_at_100 != reading) {
446 chip->ocv_reading_at_100 = 0;
447 chip->cc_reading_at_100 = 0;
448 *result = xoadc_reading_to_microvolt(reading);
449 pr_debug("raw = %04x ocv_uV = %u\n", reading, *result);
450 *result = adjust_xo_vbatt_reading(chip, *result);
451 pr_debug("after adj ocv_uV = %u\n", *result);
452 if (*result != 0)
453 last_ocv_uv = *result;
454 } else {
455 /*
456 * force 100% ocv by selecting the highest profiled ocv
457 * This is the first row last column entry in the ocv
458 * lookup table
459 */
460 int cols = chip->pc_temp_ocv_lut->cols;
461
462 pr_debug("Forcing max voltage %d\n",
463 1000 * chip->pc_temp_ocv_lut->ocv[0][cols-1]);
464 *result = 1000 * chip->pc_temp_ocv_lut->ocv[0][cols-1];
465 }
466
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700467 return 0;
468}
469
470static int read_vbatt_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
471{
472 int rc;
473 uint16_t reading;
474
475 rc = pm_bms_read_output_data(chip, VBATT_FOR_RBATT, &reading);
476 if (rc) {
477 pr_err("fail to read VBATT_FOR_RBATT rc = %d\n", rc);
478 return rc;
479 }
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700480 *result = xoadc_reading_to_microvolt(reading);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481 pr_debug("raw = %04x vbatt_for_r_microV = %u\n", reading, *result);
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700482 *result = adjust_xo_vbatt_reading(chip, *result);
483 pr_debug("after adj vbatt_for_r_uV = %u\n", *result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484 return 0;
485}
486
487static int read_vsense_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
488{
489 int rc;
490 uint16_t reading;
491
492 rc = pm_bms_read_output_data(chip, VSENSE_FOR_RBATT, &reading);
493 if (rc) {
494 pr_err("fail to read VSENSE_FOR_RBATT rc = %d\n", rc);
495 return rc;
496 }
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800497 *result = pm8xxx_ccadc_reading_to_microvolt(chip->revision, reading);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -0700498 pr_debug("raw = %04x vsense_for_r_uV = %u\n", reading, *result);
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800499 *result = pm8xxx_cc_adjust_for_gain(*result);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -0700500 pr_debug("after adj vsense_for_r_uV = %u\n", *result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700501 return 0;
502}
503
504static int read_ocv_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
505{
506 int rc;
507 uint16_t reading;
508
509 rc = pm_bms_read_output_data(chip, OCV_FOR_RBATT, &reading);
510 if (rc) {
511 pr_err("fail to read OCV_FOR_RBATT rc = %d\n", rc);
512 return rc;
513 }
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700514 *result = xoadc_reading_to_microvolt(reading);
515 pr_debug("raw = %04x ocv_for_r_uV = %u\n", reading, *result);
516 *result = adjust_xo_vbatt_reading(chip, *result);
517 pr_debug("after adj ocv_for_r_uV = %u\n", *result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700518 return 0;
519}
520
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700521static int read_vsense_avg(struct pm8921_bms_chip *chip, int *result)
522{
523 int rc;
524 int16_t reading;
525
526 rc = pm_bms_read_output_data(chip, VSENSE_AVG, &reading);
527 if (rc) {
528 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
529 return rc;
530 }
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800531 *result = pm8xxx_ccadc_reading_to_microvolt(the_chip->revision,
532 reading);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -0700533 pr_debug("raw = %04x vsense = %d\n", reading, *result);
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800534 *result = pm8xxx_cc_adjust_for_gain((s64)*result);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -0700535 pr_debug("after adj vsense = %d\n", *result);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700536 return 0;
537}
538
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539static int linear_interpolate(int y0, int x0, int y1, int x1, int x)
540{
541 if (y0 == y1 || x == x0)
542 return y0;
543 if (x1 == x0 || x == x1)
544 return y1;
545
546 return y0 + ((y1 - y0) * (x - x0) / (x1 - x0));
547}
548
549static int interpolate_single_lut(struct single_row_lut *lut, int x)
550{
551 int i, result;
552
553 if (x < lut->x[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700554 pr_debug("x %d less than known range return y = %d lut = %pS\n",
555 x, lut->y[0], lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556 return lut->y[0];
557 }
558 if (x > lut->x[lut->cols - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700559 pr_debug("x %d more than known range return y = %d lut = %pS\n",
560 x, lut->y[lut->cols - 1], lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561 return lut->y[lut->cols - 1];
562 }
563
564 for (i = 0; i < lut->cols; i++)
565 if (x <= lut->x[i])
566 break;
567 if (x == lut->x[i]) {
568 result = lut->y[i];
569 } else {
570 result = linear_interpolate(
571 lut->y[i - 1],
572 lut->x[i - 1],
573 lut->y[i],
574 lut->x[i],
575 x);
576 }
577 return result;
578}
579
580static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp)
581{
582 return interpolate_single_lut(chip->fcc_temp_lut, batt_temp);
583}
584
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700585static int interpolate_fcc_adjusted(struct pm8921_bms_chip *chip, int batt_temp)
586{
587 return interpolate_single_lut(chip->adjusted_fcc_temp_lut, batt_temp);
588}
589
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590static int interpolate_scalingfactor_fcc(struct pm8921_bms_chip *chip,
591 int cycles)
592{
Abhijeet Dharmapurikar205bbc02011-11-04 14:47:36 -0700593 /*
594 * sf table could be null when no battery aging data is available, in
595 * that case return 100%
596 */
597 if (chip->fcc_sf_lut)
598 return interpolate_single_lut(chip->fcc_sf_lut, cycles);
599 else
600 return 100;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601}
602
603static int interpolate_scalingfactor_pc(struct pm8921_bms_chip *chip,
604 int cycles, int pc)
605{
Abhijeet Dharmapurikar4a4456e2011-09-27 15:10:38 -0700606 int i, scalefactorrow1, scalefactorrow2, scalefactor;
Abhijeet Dharmapurikar205bbc02011-11-04 14:47:36 -0700607 int rows, cols;
Abhijeet Dharmapurikar4a4456e2011-09-27 15:10:38 -0700608 int row1 = 0;
609 int row2 = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700610
Abhijeet Dharmapurikar205bbc02011-11-04 14:47:36 -0700611 /*
612 * sf table could be null when no battery aging data is available, in
613 * that case return 100%
614 */
615 if (!chip->pc_sf_lut)
616 return 100;
617
618 rows = chip->pc_sf_lut->rows;
619 cols = chip->pc_sf_lut->cols;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700620 if (pc > chip->pc_sf_lut->percent[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700621 pr_debug("pc %d greater than known pc ranges for sfd\n", pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700622 row1 = 0;
623 row2 = 0;
624 }
625 if (pc < chip->pc_sf_lut->percent[rows - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700626 pr_debug("pc %d less than known pc ranges for sf", pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627 row1 = rows - 1;
628 row2 = rows - 1;
629 }
630 for (i = 0; i < rows; i++) {
631 if (pc == chip->pc_sf_lut->percent[i]) {
632 row1 = i;
633 row2 = i;
634 break;
635 }
636 if (pc > chip->pc_sf_lut->percent[i]) {
637 row1 = i - 1;
638 row2 = i;
639 break;
640 }
641 }
642
643 if (cycles < chip->pc_sf_lut->cycles[0])
644 cycles = chip->pc_sf_lut->cycles[0];
645 if (cycles > chip->pc_sf_lut->cycles[cols - 1])
646 cycles = chip->pc_sf_lut->cycles[cols - 1];
647
648 for (i = 0; i < cols; i++)
649 if (cycles <= chip->pc_sf_lut->cycles[i])
650 break;
651 if (cycles == chip->pc_sf_lut->cycles[i]) {
652 scalefactor = linear_interpolate(
653 chip->pc_sf_lut->sf[row1][i],
654 chip->pc_sf_lut->percent[row1],
655 chip->pc_sf_lut->sf[row2][i],
656 chip->pc_sf_lut->percent[row2],
657 pc);
658 return scalefactor;
659 }
660
661 scalefactorrow1 = linear_interpolate(
662 chip->pc_sf_lut->sf[row1][i - 1],
663 chip->pc_sf_lut->cycles[i - 1],
664 chip->pc_sf_lut->sf[row1][i],
665 chip->pc_sf_lut->cycles[i],
666 cycles);
667
668 scalefactorrow2 = linear_interpolate(
669 chip->pc_sf_lut->sf[row2][i - 1],
670 chip->pc_sf_lut->cycles[i - 1],
671 chip->pc_sf_lut->sf[row2][i],
672 chip->pc_sf_lut->cycles[i],
673 cycles);
674
675 scalefactor = linear_interpolate(
676 scalefactorrow1,
677 chip->pc_sf_lut->percent[row1],
678 scalefactorrow2,
679 chip->pc_sf_lut->percent[row2],
680 pc);
681
682 return scalefactor;
683}
684
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700685static int is_between(int left, int right, int value)
686{
687 if (left >= right && left >= value && value >= right)
688 return 1;
689 if (left <= right && left <= value && value <= right)
690 return 1;
691
692 return 0;
693}
694
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700695static int interpolate_pc(struct pm8921_bms_chip *chip,
696 int batt_temp, int ocv)
697{
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700698 int i, j, pcj, pcj_minus_one, pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700699 int rows = chip->pc_temp_ocv_lut->rows;
700 int cols = chip->pc_temp_ocv_lut->cols;
701
702 if (batt_temp < chip->pc_temp_ocv_lut->temp[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700703 pr_debug("batt_temp %d < known temp range for pc\n", batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700704 batt_temp = chip->pc_temp_ocv_lut->temp[0];
705 }
706 if (batt_temp > chip->pc_temp_ocv_lut->temp[cols - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700707 pr_debug("batt_temp %d > known temp range for pc\n", batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708 batt_temp = chip->pc_temp_ocv_lut->temp[cols - 1];
709 }
710
711 for (j = 0; j < cols; j++)
712 if (batt_temp <= chip->pc_temp_ocv_lut->temp[j])
713 break;
714 if (batt_temp == chip->pc_temp_ocv_lut->temp[j]) {
715 /* found an exact match for temp in the table */
716 if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j])
717 return chip->pc_temp_ocv_lut->percent[0];
718 if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j])
719 return chip->pc_temp_ocv_lut->percent[rows - 1];
720 for (i = 0; i < rows; i++) {
721 if (ocv >= chip->pc_temp_ocv_lut->ocv[i][j]) {
722 if (ocv == chip->pc_temp_ocv_lut->ocv[i][j])
723 return
724 chip->pc_temp_ocv_lut->percent[i];
725 pc = linear_interpolate(
726 chip->pc_temp_ocv_lut->percent[i],
727 chip->pc_temp_ocv_lut->ocv[i][j],
728 chip->pc_temp_ocv_lut->percent[i - 1],
729 chip->pc_temp_ocv_lut->ocv[i - 1][j],
730 ocv);
731 return pc;
732 }
733 }
734 }
735
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700736 /*
737 * batt_temp is within temperature for
738 * column j-1 and j
739 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740 if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j])
741 return chip->pc_temp_ocv_lut->percent[0];
742 if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j - 1])
743 return chip->pc_temp_ocv_lut->percent[rows - 1];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700745 pcj_minus_one = 0;
746 pcj = 0;
747 for (i = 0; i < rows-1; i++) {
748 if (pcj == 0
749 && is_between(chip->pc_temp_ocv_lut->ocv[i][j],
750 chip->pc_temp_ocv_lut->ocv[i+1][j], ocv)) {
751 pcj = linear_interpolate(
752 chip->pc_temp_ocv_lut->percent[i],
753 chip->pc_temp_ocv_lut->ocv[i][j],
754 chip->pc_temp_ocv_lut->percent[i + 1],
755 chip->pc_temp_ocv_lut->ocv[i+1][j],
756 ocv);
757 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700759 if (pcj_minus_one == 0
760 && is_between(chip->pc_temp_ocv_lut->ocv[i][j-1],
761 chip->pc_temp_ocv_lut->ocv[i+1][j-1], ocv)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700762
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700763 pcj_minus_one = linear_interpolate(
764 chip->pc_temp_ocv_lut->percent[i],
765 chip->pc_temp_ocv_lut->ocv[i][j-1],
766 chip->pc_temp_ocv_lut->percent[i + 1],
767 chip->pc_temp_ocv_lut->ocv[i+1][j-1],
768 ocv);
769 }
770
771 if (pcj && pcj_minus_one) {
772 pc = linear_interpolate(
773 pcj_minus_one,
774 chip->pc_temp_ocv_lut->temp[j-1],
775 pcj,
776 chip->pc_temp_ocv_lut->temp[j],
777 batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700778 return pc;
779 }
780 }
781
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700782 if (pcj)
783 return pcj;
784
785 if (pcj_minus_one)
786 return pcj_minus_one;
787
788 pr_debug("%d ocv wasn't found for temp %d in the LUT returning 100%%",
789 ocv, batt_temp);
790 return 100;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700791}
792
793static int calculate_rbatt(struct pm8921_bms_chip *chip)
794{
795 int rc;
796 unsigned int ocv, vsense, vbatt, r_batt;
797
798 rc = read_ocv_for_rbatt(chip, &ocv);
799 if (rc) {
800 pr_err("fail to read ocv_for_rbatt rc = %d\n", rc);
801 ocv = 0;
802 }
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700803
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700804 rc = read_vbatt_for_rbatt(chip, &vbatt);
805 if (rc) {
806 pr_err("fail to read vbatt_for_rbatt rc = %d\n", rc);
807 ocv = 0;
808 }
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700809
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700810 rc = read_vsense_for_rbatt(chip, &vsense);
811 if (rc) {
812 pr_err("fail to read vsense_for_rbatt rc = %d\n", rc);
813 ocv = 0;
814 }
815 if (ocv == 0
816 || ocv == vbatt
817 || vsense == 0) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700818 pr_debug("rbatt readings unavailable ocv = %d, vbatt = %d,"
819 "vsen = %d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700820 ocv, vbatt, vsense);
821 return -EINVAL;
822 }
823 r_batt = ((ocv - vbatt) * chip->r_sense) / vsense;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700824 last_rbatt = r_batt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700825 pr_debug("r_batt = %umilliOhms", r_batt);
826 return r_batt;
827}
828
829static int calculate_fcc(struct pm8921_bms_chip *chip, int batt_temp,
830 int chargecycles)
831{
832 int initfcc, result, scalefactor = 0;
833
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700834 if (chip->adjusted_fcc_temp_lut == NULL) {
835 initfcc = interpolate_fcc(chip, batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700836
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700837 scalefactor = interpolate_scalingfactor_fcc(chip, chargecycles);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700839 /* Multiply the initial FCC value by the scale factor. */
840 result = (initfcc * scalefactor) / 100;
841 pr_debug("fcc mAh = %d\n", result);
842 return result;
843 } else {
844 return interpolate_fcc_adjusted(chip, batt_temp);
845 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700846}
847
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -0700848static int get_battery_uvolts(struct pm8921_bms_chip *chip, int *uvolts)
849{
850 int rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -0700851 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -0700852
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -0700853 rc = pm8xxx_adc_read(chip->vbat_channel, &result);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -0700854 if (rc) {
855 pr_err("error reading adc channel = %d, rc = %d\n",
856 chip->vbat_channel, rc);
857 return rc;
858 }
859 pr_debug("mvolts phy = %lld meas = 0x%llx", result.physical,
860 result.measurement);
861 *uvolts = (int)result.physical;
862 *uvolts = *uvolts * 1000;
863 return 0;
864}
865
866static int adc_based_ocv(struct pm8921_bms_chip *chip, int *ocv)
867{
868 int vbatt, rbatt, ibatt, rc;
869
870 rc = get_battery_uvolts(chip, &vbatt);
871 if (rc) {
872 pr_err("failed to read vbatt from adc rc = %d\n", rc);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -0700873 return rc;
874 }
875
876 rc = pm8921_bms_get_battery_current(&ibatt);
877 if (rc) {
878 pr_err("failed to read batt current rc = %d\n", rc);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -0700879 return rc;
880 }
881
882 rbatt = calculate_rbatt(the_chip);
883 if (rbatt < 0)
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700884 rbatt = (last_rbatt < 0) ? DEFAULT_RBATT_MOHMS : last_rbatt;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -0700885 *ocv = vbatt + ibatt * rbatt;
886 return 0;
887}
888
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889static int calculate_pc(struct pm8921_bms_chip *chip, int ocv_uv, int batt_temp,
890 int chargecycles)
891{
892 int pc, scalefactor;
893
894 pc = interpolate_pc(chip, batt_temp, ocv_uv / 1000);
895 pr_debug("pc = %u for ocv = %dmicroVolts batt_temp = %d\n",
896 pc, ocv_uv, batt_temp);
897
898 scalefactor = interpolate_scalingfactor_pc(chip, chargecycles, pc);
899 pr_debug("scalefactor = %u batt_temp = %d\n", scalefactor, batt_temp);
900
901 /* Multiply the initial FCC value by the scale factor. */
902 pc = (pc * scalefactor) / 100;
903 return pc;
904}
905
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700906static void calculate_cc_mah(struct pm8921_bms_chip *chip, int64_t *val,
Abhijeet Dharmapurikar52aa9682011-07-20 14:05:47 -0700907 int *coulumb_counter)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908{
909 int rc;
910 int64_t cc_voltage_uv, cc_uvh, cc_mah;
911
912 rc = read_cc(the_chip, coulumb_counter);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700913 cc_voltage_uv = (int64_t)*coulumb_counter;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700914 cc_voltage_uv = cc_to_microvolt(chip, cc_voltage_uv);
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800915 cc_voltage_uv = pm8xxx_cc_adjust_for_gain(cc_voltage_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700916 pr_debug("cc_voltage_uv = %lld microvolts\n", cc_voltage_uv);
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700917 cc_uvh = ccmicrovolt_to_uvh(cc_voltage_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700918 pr_debug("cc_uvh = %lld micro_volt_hour\n", cc_uvh);
919 cc_mah = div_s64(cc_uvh, chip->r_sense);
920 *val = cc_mah;
921}
922
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700923static int calculate_unusable_charge_mah(struct pm8921_bms_chip *chip,
924 int fcc, int batt_temp, int chargecycles)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700926 int rbatt, voltage_unusable_uv, pc_unusable;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700927
928 rbatt = calculate_rbatt(chip);
929 if (rbatt < 0) {
930 rbatt = (last_rbatt < 0) ? DEFAULT_RBATT_MOHMS : last_rbatt;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700931 pr_debug("rbatt unavailable assuming %d\n", rbatt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700932 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700933
934 /* calculate unusable charge */
935 voltage_unusable_uv = (rbatt * chip->i_test)
936 + (chip->v_failure * 1000);
937 pc_unusable = calculate_pc(chip, voltage_unusable_uv,
938 batt_temp, chargecycles);
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700939 pr_debug("rbatt = %umilliOhms unusable_v =%d unusable_pc = %d\n",
940 rbatt, voltage_unusable_uv, pc_unusable);
941 return (fcc * pc_unusable) / 100;
942}
Abhijeet Dharmapurikar52aa9682011-07-20 14:05:47 -0700943
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700944/* calculate remainging charge at the time of ocv */
945static int calculate_remaining_charge_mah(struct pm8921_bms_chip *chip, int fcc,
946 int batt_temp, int chargecycles)
947{
948 int rc, ocv, pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700949
950 /* calculate remainging charge */
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -0700951 ocv = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952 rc = read_last_good_ocv(chip, &ocv);
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -0700953 if (rc)
954 pr_debug("failed to read ocv rc = %d\n", rc);
955
956 if (ocv == 0) {
957 ocv = last_ocv_uv;
958 pr_debug("ocv not available using last_ocv_uv=%d\n", ocv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700959 }
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700960
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700961 pc = calculate_pc(chip, ocv, batt_temp, chargecycles);
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700962 pr_debug("ocv = %d pc = %d\n", ocv, pc);
963 return (fcc * pc) / 100;
964}
965
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700966static void calculate_charging_params(struct pm8921_bms_chip *chip,
967 int batt_temp, int chargecycles,
968 int *fcc,
969 int *unusable_charge,
970 int *remaining_charge,
971 int64_t *cc_mah)
972{
973 int coulumb_counter;
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700974 unsigned long flags;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700975
976 *fcc = calculate_fcc(chip, batt_temp, chargecycles);
977 pr_debug("FCC = %umAh batt_temp = %d, cycles = %d",
978 *fcc, batt_temp, chargecycles);
979
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700980 /* fcc doesnt need to be read from hardware, lock the bms now */
981 spin_lock_irqsave(&chip->bms_output_lock, flags);
982 pm_bms_lock_output_data(chip);
983
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700984 *unusable_charge = calculate_unusable_charge_mah(chip, *fcc,
985 batt_temp, chargecycles);
986
987 pr_debug("UUC = %umAh", *unusable_charge);
988
989 /* calculate remainging charge */
990 *remaining_charge = calculate_remaining_charge_mah(chip, *fcc,
991 batt_temp, chargecycles);
992 pr_debug("RC = %umAh\n", *remaining_charge);
993
994 /* calculate cc milli_volt_hour */
995 calculate_cc_mah(chip, cc_mah, &coulumb_counter);
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -0800996 pr_debug("cc_mah = %lldmAh cc = %d\n", *cc_mah, coulumb_counter);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700997
998 pm_bms_unlock_output_data(chip);
999 spin_unlock_irqrestore(&chip->bms_output_lock, flags);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001000}
1001
1002static int calculate_real_fcc(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08001003 int batt_temp, int chargecycles,
1004 int *ret_fcc)
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001005{
1006 int fcc, unusable_charge;
1007 int remaining_charge;
1008 int64_t cc_mah;
1009 int real_fcc;
1010
1011 calculate_charging_params(chip, batt_temp, chargecycles,
1012 &fcc,
1013 &unusable_charge,
1014 &remaining_charge,
1015 &cc_mah);
1016
1017 real_fcc = remaining_charge - cc_mah;
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08001018 *ret_fcc = fcc;
1019 pr_debug("real_fcc = %d, RC = %d CC = %lld fcc = %d\n",
1020 real_fcc, remaining_charge, cc_mah, fcc);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001021 return real_fcc;
1022}
1023/*
1024 * Remaining Usable Charge = remaining_charge (charge at ocv instance)
1025 * - coloumb counter charge
1026 * - unusable charge (due to battery resistance)
1027 * SOC% = (remaining usable charge/ fcc - usable_charge);
1028 */
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001029#define BATTERY_POWER_SUPPLY_SOC 53
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001030static int calculate_state_of_charge(struct pm8921_bms_chip *chip,
1031 int batt_temp, int chargecycles)
1032{
1033 int remaining_usable_charge, fcc, unusable_charge;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001034 int remaining_charge, soc;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001035 int update_userspace = 1;
1036 int64_t cc_mah;
1037
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001038 calculate_charging_params(chip, batt_temp, chargecycles,
1039 &fcc,
1040 &unusable_charge,
1041 &remaining_charge,
1042 &cc_mah);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001043
1044 /* calculate remaining usable charge */
1045 remaining_usable_charge = remaining_charge - cc_mah - unusable_charge;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001046 pr_debug("RUC = %dmAh\n", remaining_usable_charge);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001047 soc = (remaining_usable_charge * 100) / (fcc - unusable_charge);
1048 if (soc > 100)
1049 soc = 100;
1050 pr_debug("SOC = %u%%\n", soc);
1051
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -08001052 if (bms_fake_battery) {
1053 soc = BATTERY_POWER_SUPPLY_SOC;
1054 pr_debug("setting SOC = %u%% bms_fake_battery = %d\n", soc,
1055 bms_fake_battery);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001056 }
1057
1058 if (soc < 0) {
Abhijeet Dharmapurikar21ebadd2011-09-28 21:14:16 -07001059 pr_err("bad rem_usb_chg = %d rem_chg %d,"
1060 "cc_mah %lld, unusb_chg %d\n",
1061 remaining_usable_charge, remaining_charge,
1062 cc_mah, unusable_charge);
1063 pr_err("for bad rem_usb_chg last_ocv_uv = %d"
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001064 "chargecycles = %d, batt_temp = %d"
1065 "fcc = %d soc =%d\n",
1066 last_ocv_uv, chargecycles, batt_temp,
1067 fcc, soc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001068 update_userspace = 0;
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -08001069 soc = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001070 }
1071
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001072 if (last_soc == -EINVAL || soc <= last_soc) {
1073 last_soc = update_userspace ? soc : last_soc;
1074 return soc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001075 }
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001076
1077 /*
1078 * soc > last_soc
1079 * the device must be charging for reporting a higher soc, if not ignore
1080 * this soc and continue reporting the last_soc
1081 */
1082 if (the_chip->start_percent != 0) {
1083 last_soc = soc;
1084 } else {
1085 pr_debug("soc = %d reporting last_soc = %d\n", soc, last_soc);
1086 soc = last_soc;
1087 }
1088
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001089 return soc;
1090}
1091
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001092#define XOADC_MAX_1P25V 1312500
1093#define XOADC_MIN_1P25V 1187500
1094#define XOADC_MAX_0P625V 656250
1095#define XOADC_MIN_0P625V 593750
1096
1097#define HKADC_V_PER_BIT_MUL_FACTOR 977
1098#define HKADC_V_PER_BIT_DIV_FACTOR 10
1099static int calib_hkadc_convert_microvolt(unsigned int phy)
1100{
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001101 return (phy - 0x6000) *
1102 HKADC_V_PER_BIT_MUL_FACTOR / HKADC_V_PER_BIT_DIV_FACTOR;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001103}
1104
1105static void calib_hkadc(struct pm8921_bms_chip *chip)
1106{
1107 int voltage, rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001108 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001109
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001110 rc = pm8xxx_adc_read(the_chip->ref1p25v_channel, &result);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001111 if (rc) {
1112 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
1113 return;
1114 }
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001115 voltage = calib_hkadc_convert_microvolt(result.adc_code);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001116
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001117 pr_debug("result 1.25v = 0x%x, voltage = %duV adc_meas = %lld\n",
1118 result.adc_code, voltage, result.measurement);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001119
1120 /* check for valid range */
1121 if (voltage > XOADC_MAX_1P25V)
1122 voltage = XOADC_MAX_1P25V;
1123 else if (voltage < XOADC_MIN_1P25V)
1124 voltage = XOADC_MIN_1P25V;
1125 chip->xoadc_v125 = voltage;
1126
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001127 rc = pm8xxx_adc_read(the_chip->ref625mv_channel, &result);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001128 if (rc) {
1129 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
1130 return;
1131 }
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001132 voltage = calib_hkadc_convert_microvolt(result.adc_code);
1133 pr_debug("result 0.625V = 0x%x, voltage = %duV adc_mead = %lld\n",
1134 result.adc_code, voltage, result.measurement);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001135 /* check for valid range */
1136 if (voltage > XOADC_MAX_0P625V)
1137 voltage = XOADC_MAX_0P625V;
1138 else if (voltage < XOADC_MIN_0P625V)
1139 voltage = XOADC_MIN_0P625V;
1140
1141 chip->xoadc_v0625 = voltage;
1142}
1143
1144static void calibrate_hkadc_work(struct work_struct *work)
1145{
1146 struct pm8921_bms_chip *chip = container_of(work,
1147 struct pm8921_bms_chip, calib_hkadc_work);
1148
1149 calib_hkadc(chip);
1150}
1151
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001152static void calibrate_ccadc_work(struct work_struct *work)
1153{
1154 struct pm8921_bms_chip *chip = container_of(work,
1155 struct pm8921_bms_chip, calib_ccadc_work.work);
1156
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08001157 pm8xxx_calib_ccadc();
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001158 schedule_delayed_work(&chip->calib_ccadc_work,
1159 round_jiffies_relative(msecs_to_jiffies
1160 (chip->calib_delay_ms)));
1161}
1162
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001163int pm8921_bms_get_vsense_avg(int *result)
1164{
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001165 int rc = -EINVAL;
1166 unsigned long flags;
1167
1168 if (the_chip) {
1169 spin_lock_irqsave(&the_chip->bms_output_lock, flags);
1170 pm_bms_lock_output_data(the_chip);
1171 rc = read_vsense_avg(the_chip, result);
1172 pm_bms_unlock_output_data(the_chip);
1173 spin_unlock_irqrestore(&the_chip->bms_output_lock, flags);
1174 }
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001175
1176 pr_err("called before initialization\n");
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001177 return rc;
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001178}
1179EXPORT_SYMBOL(pm8921_bms_get_vsense_avg);
1180
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001181int pm8921_bms_get_battery_current(int *result)
1182{
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001183 unsigned long flags;
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001184 int vsense;
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001185
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001186 if (!the_chip) {
1187 pr_err("called before initialization\n");
1188 return -EINVAL;
1189 }
1190 if (the_chip->r_sense == 0) {
1191 pr_err("r_sense is zero\n");
1192 return -EINVAL;
1193 }
1194
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001195 spin_lock_irqsave(&the_chip->bms_output_lock, flags);
1196 pm_bms_lock_output_data(the_chip);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001197 read_vsense_avg(the_chip, &vsense);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001198 pm_bms_unlock_output_data(the_chip);
1199 spin_unlock_irqrestore(&the_chip->bms_output_lock, flags);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001200 pr_debug("vsense=%d\n", vsense);
Abhijeet Dharmapurikara7a1c6b2011-08-17 10:04:58 -07001201 /* cast for signed division */
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001202 *result = vsense / (int)the_chip->r_sense;
1203
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001204 return 0;
1205}
1206EXPORT_SYMBOL(pm8921_bms_get_battery_current);
1207
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001208int pm8921_bms_get_percent_charge(void)
1209{
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001210 int batt_temp, rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001211 struct pm8xxx_adc_chan_result result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001212
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001213 if (!the_chip) {
1214 pr_err("called before initialization\n");
1215 return -EINVAL;
1216 }
1217
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001218 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001219 if (rc) {
1220 pr_err("error reading adc channel = %d, rc = %d\n",
1221 the_chip->batt_temp_channel, rc);
1222 return rc;
1223 }
1224 pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical,
1225 result.measurement);
1226 batt_temp = (int)result.physical;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001227 return calculate_state_of_charge(the_chip,
1228 batt_temp, last_chargecycles);
1229}
1230EXPORT_SYMBOL_GPL(pm8921_bms_get_percent_charge);
1231
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07001232int pm8921_bms_get_fcc(void)
1233{
1234 int batt_temp, rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001235 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07001236
1237 if (!the_chip) {
1238 pr_err("called before initialization\n");
1239 return -EINVAL;
1240 }
1241
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001242 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07001243 if (rc) {
1244 pr_err("error reading adc channel = %d, rc = %d\n",
1245 the_chip->batt_temp_channel, rc);
1246 return rc;
1247 }
1248 pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical,
1249 result.measurement);
1250 batt_temp = (int)result.physical;
1251 return calculate_fcc(the_chip, batt_temp, last_chargecycles);
1252}
1253EXPORT_SYMBOL_GPL(pm8921_bms_get_fcc);
1254
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001255void pm8921_bms_charging_began(void)
1256{
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001257 the_chip->start_percent = pm8921_bms_get_percent_charge();
1258 pr_debug("start_percent = %u%%\n", the_chip->start_percent);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001259}
1260EXPORT_SYMBOL_GPL(pm8921_bms_charging_began);
1261
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08001262#define DELTA_FCC_PERCENT 3
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001263void pm8921_bms_charging_end(int is_battery_full)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001264{
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001265 if (is_battery_full && the_chip != NULL) {
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -08001266 unsigned long flags;
1267 int batt_temp, rc, cc_reading;
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08001268 int fcc, new_fcc, delta_fcc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001269 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001270
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001271 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001272 if (rc) {
1273 pr_err("error reading adc channel = %d, rc = %d\n",
1274 the_chip->batt_temp_channel, rc);
1275 goto charge_cycle_calculation;
1276 }
1277 pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical,
1278 result.measurement);
1279 batt_temp = (int)result.physical;
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08001280 new_fcc = calculate_real_fcc(the_chip,
1281 batt_temp, last_chargecycles,
1282 &fcc);
1283 delta_fcc = new_fcc - fcc;
1284 if (delta_fcc < 0)
1285 delta_fcc = -delta_fcc;
1286
1287 if (delta_fcc * 100 <= (DELTA_FCC_PERCENT * fcc)) {
1288 pr_debug("delta_fcc=%d < %d percent of fcc=%d\n",
1289 delta_fcc, DELTA_FCC_PERCENT, fcc);
1290 last_real_fcc = new_fcc;
1291 last_real_fcc_batt_temp = batt_temp;
1292 readjust_fcc_table();
1293 } else {
1294 pr_debug("delta_fcc=%d > %d percent of fcc=%d"
1295 "will not update real fcc\n",
1296 delta_fcc, DELTA_FCC_PERCENT, fcc);
1297 }
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -08001298
1299 spin_lock_irqsave(&the_chip->bms_output_lock, flags);
1300 pm_bms_lock_output_data(the_chip);
1301 pm_bms_read_output_data(the_chip, LAST_GOOD_OCV_VALUE,
1302 &the_chip->ocv_reading_at_100);
1303 read_cc(the_chip, &cc_reading);
1304 pm_bms_unlock_output_data(the_chip);
1305 spin_unlock_irqrestore(&the_chip->bms_output_lock, flags);
1306 the_chip->cc_reading_at_100 = cc_reading;
1307 pr_debug("EOC ocv_reading = 0x%x cc_reading = %d\n",
1308 the_chip->ocv_reading_at_100,
1309 the_chip->cc_reading_at_100);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001310 }
1311
1312charge_cycle_calculation:
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001313 the_chip->end_percent = pm8921_bms_get_percent_charge();
1314 if (the_chip->end_percent > the_chip->start_percent) {
1315 last_charge_increase =
1316 the_chip->end_percent - the_chip->start_percent;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001317 if (last_charge_increase > 100) {
1318 last_chargecycles++;
1319 last_charge_increase = last_charge_increase % 100;
1320 }
1321 }
1322 pr_debug("end_percent = %u%% last_charge_increase = %d"
1323 "last_chargecycles = %d\n",
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001324 the_chip->end_percent,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001325 last_charge_increase,
1326 last_chargecycles);
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001327 the_chip->start_percent = 0;
1328 the_chip->end_percent = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329}
1330EXPORT_SYMBOL_GPL(pm8921_bms_charging_end);
1331
1332static irqreturn_t pm8921_bms_sbi_write_ok_handler(int irq, void *data)
1333{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001334 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001335 return IRQ_HANDLED;
1336}
1337
1338static irqreturn_t pm8921_bms_cc_thr_handler(int irq, void *data)
1339{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001340 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001341 return IRQ_HANDLED;
1342}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001343
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001344static irqreturn_t pm8921_bms_vsense_thr_handler(int irq, void *data)
1345{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001346 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001347 return IRQ_HANDLED;
1348}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001349
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001350static irqreturn_t pm8921_bms_vsense_for_r_handler(int irq, void *data)
1351{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001352 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001353 return IRQ_HANDLED;
1354}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001355
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001356static irqreturn_t pm8921_bms_ocv_for_r_handler(int irq, void *data)
1357{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001358 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001359
1360 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001361 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001362 return IRQ_HANDLED;
1363}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001364
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001365static irqreturn_t pm8921_bms_good_ocv_handler(int irq, void *data)
1366{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001367 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001368
1369 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001370 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001371 return IRQ_HANDLED;
1372}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001373
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001374static irqreturn_t pm8921_bms_vsense_avg_handler(int irq, void *data)
1375{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001376 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001377 return IRQ_HANDLED;
1378}
1379
1380struct pm_bms_irq_init_data {
1381 unsigned int irq_id;
1382 char *name;
1383 unsigned long flags;
1384 irqreturn_t (*handler)(int, void *);
1385};
1386
1387#define BMS_IRQ(_id, _flags, _handler) \
1388{ \
1389 .irq_id = _id, \
1390 .name = #_id, \
1391 .flags = _flags, \
1392 .handler = _handler, \
1393}
1394
1395struct pm_bms_irq_init_data bms_irq_data[] = {
1396 BMS_IRQ(PM8921_BMS_SBI_WRITE_OK, IRQF_TRIGGER_RISING,
1397 pm8921_bms_sbi_write_ok_handler),
1398 BMS_IRQ(PM8921_BMS_CC_THR, IRQF_TRIGGER_RISING,
1399 pm8921_bms_cc_thr_handler),
1400 BMS_IRQ(PM8921_BMS_VSENSE_THR, IRQF_TRIGGER_RISING,
1401 pm8921_bms_vsense_thr_handler),
1402 BMS_IRQ(PM8921_BMS_VSENSE_FOR_R, IRQF_TRIGGER_RISING,
1403 pm8921_bms_vsense_for_r_handler),
1404 BMS_IRQ(PM8921_BMS_OCV_FOR_R, IRQF_TRIGGER_RISING,
1405 pm8921_bms_ocv_for_r_handler),
1406 BMS_IRQ(PM8921_BMS_GOOD_OCV, IRQF_TRIGGER_RISING,
1407 pm8921_bms_good_ocv_handler),
1408 BMS_IRQ(PM8921_BMS_VSENSE_AVG, IRQF_TRIGGER_RISING,
1409 pm8921_bms_vsense_avg_handler),
1410};
1411
1412static void free_irqs(struct pm8921_bms_chip *chip)
1413{
1414 int i;
1415
1416 for (i = 0; i < PM_BMS_MAX_INTS; i++)
1417 if (chip->pmic_bms_irq[i]) {
1418 free_irq(chip->pmic_bms_irq[i], NULL);
1419 chip->pmic_bms_irq[i] = 0;
1420 }
1421}
1422
1423static int __devinit request_irqs(struct pm8921_bms_chip *chip,
1424 struct platform_device *pdev)
1425{
1426 struct resource *res;
1427 int ret, i;
1428
1429 ret = 0;
1430 bitmap_fill(chip->enabled_irqs, PM_BMS_MAX_INTS);
1431
1432 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
1433 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1434 bms_irq_data[i].name);
1435 if (res == NULL) {
1436 pr_err("couldn't find %s\n", bms_irq_data[i].name);
1437 goto err_out;
1438 }
1439 ret = request_irq(res->start, bms_irq_data[i].handler,
1440 bms_irq_data[i].flags,
1441 bms_irq_data[i].name, chip);
1442 if (ret < 0) {
1443 pr_err("couldn't request %d (%s) %d\n", res->start,
1444 bms_irq_data[i].name, ret);
1445 goto err_out;
1446 }
1447 chip->pmic_bms_irq[bms_irq_data[i].irq_id] = res->start;
1448 pm8921_bms_disable_irq(chip, bms_irq_data[i].irq_id);
1449 }
1450 return 0;
1451
1452err_out:
1453 free_irqs(chip);
1454 return -EINVAL;
1455}
1456
1457#define EN_BMS_BIT BIT(7)
1458#define EN_PON_HS_BIT BIT(0)
1459static int __devinit pm8921_bms_hw_init(struct pm8921_bms_chip *chip)
1460{
1461 int rc;
1462
1463 rc = pm_bms_masked_write(chip, BMS_CONTROL,
1464 EN_BMS_BIT | EN_PON_HS_BIT, EN_BMS_BIT | EN_PON_HS_BIT);
1465 if (rc) {
1466 pr_err("failed to enable pon and bms addr = %d %d",
1467 BMS_CONTROL, rc);
1468 }
1469
1470 return 0;
1471}
1472
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001473static void check_initial_ocv(struct pm8921_bms_chip *chip)
1474{
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001475 int ocv, rc;
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001476
1477 /*
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001478 * Check if a ocv is available in bms hw,
1479 * if not compute it here at boot time and save it
1480 * in the last_ocv_uv.
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001481 */
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001482 ocv = 0;
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001483 rc = read_last_good_ocv(chip, &ocv);
1484 if (rc || ocv == 0) {
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001485 rc = adc_based_ocv(chip, &ocv);
1486 if (rc) {
1487 pr_err("failed to read adc based ocv rc = %d\n", rc);
1488 ocv = DEFAULT_OCV_MICROVOLTS;
1489 }
1490 last_ocv_uv = ocv;
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001491 }
1492 pr_debug("ocv = %d last_ocv_uv = %d\n", ocv, last_ocv_uv);
1493}
1494
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001495static int64_t read_battery_id(struct pm8921_bms_chip *chip)
1496{
1497 int rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001498 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001499
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001500 rc = pm8xxx_adc_read(chip->batt_id_channel, &result);
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001501 if (rc) {
1502 pr_err("error reading batt id channel = %d, rc = %d\n",
1503 chip->vbat_channel, rc);
1504 return rc;
1505 }
1506 pr_debug("batt_id phy = %lld meas = 0x%llx\n", result.physical,
1507 result.measurement);
1508 return result.physical;
1509}
1510
1511#define PALLADIUM_ID_MIN 2500
1512#define PALLADIUM_ID_MAX 4000
1513static int set_battery_data(struct pm8921_bms_chip *chip)
1514{
1515 int64_t battery_id;
1516
1517 battery_id = read_battery_id(chip);
1518
1519 if (battery_id < 0) {
1520 pr_err("cannot read battery id err = %lld\n", battery_id);
1521 return battery_id;
1522 }
1523
1524 if (is_between(PALLADIUM_ID_MIN, PALLADIUM_ID_MAX, battery_id)) {
1525 chip->fcc = palladium_1500_data.fcc;
1526 chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut;
1527 chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut;
1528 chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut;
1529 chip->pc_sf_lut = palladium_1500_data.pc_sf_lut;
1530 return 0;
1531 } else {
1532 pr_warn("invalid battery id, palladium 1500 assumed\n");
1533 chip->fcc = palladium_1500_data.fcc;
1534 chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut;
1535 chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut;
1536 chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut;
1537 chip->pc_sf_lut = palladium_1500_data.pc_sf_lut;
1538 return 0;
1539 }
1540}
1541
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001542enum {
1543 CALC_RBATT,
1544 CALC_FCC,
1545 CALC_PC,
1546 CALC_SOC,
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001547 CALIB_HKADC,
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001548 CALIB_CCADC,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001549};
1550
1551static int test_batt_temp = 5;
1552static int test_chargecycle = 150;
1553static int test_ocv = 3900000;
1554enum {
1555 TEST_BATT_TEMP,
1556 TEST_CHARGE_CYCLE,
1557 TEST_OCV,
1558};
1559static int get_test_param(void *data, u64 * val)
1560{
1561 switch ((int)data) {
1562 case TEST_BATT_TEMP:
1563 *val = test_batt_temp;
1564 break;
1565 case TEST_CHARGE_CYCLE:
1566 *val = test_chargecycle;
1567 break;
1568 case TEST_OCV:
1569 *val = test_ocv;
1570 break;
1571 default:
1572 return -EINVAL;
1573 }
1574 return 0;
1575}
1576static int set_test_param(void *data, u64 val)
1577{
1578 switch ((int)data) {
1579 case TEST_BATT_TEMP:
1580 test_batt_temp = (int)val;
1581 break;
1582 case TEST_CHARGE_CYCLE:
1583 test_chargecycle = (int)val;
1584 break;
1585 case TEST_OCV:
1586 test_ocv = (int)val;
1587 break;
1588 default:
1589 return -EINVAL;
1590 }
1591 return 0;
1592}
1593DEFINE_SIMPLE_ATTRIBUTE(temp_fops, get_test_param, set_test_param, "%llu\n");
1594
1595static int get_calc(void *data, u64 * val)
1596{
1597 int param = (int)data;
1598 int ret = 0;
1599
1600 *val = 0;
1601
1602 /* global irq number passed in via data */
1603 switch (param) {
1604 case CALC_RBATT:
1605 *val = calculate_rbatt(the_chip);
1606 break;
1607 case CALC_FCC:
1608 *val = calculate_fcc(the_chip, test_batt_temp,
1609 test_chargecycle);
1610 break;
1611 case CALC_PC:
1612 *val = calculate_pc(the_chip, test_ocv, test_batt_temp,
1613 test_chargecycle);
1614 break;
1615 case CALC_SOC:
1616 *val = calculate_state_of_charge(the_chip,
1617 test_batt_temp, test_chargecycle);
1618 break;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001619 case CALIB_HKADC:
1620 /* reading this will trigger calibration */
1621 *val = 0;
1622 calib_hkadc(the_chip);
1623 break;
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001624 case CALIB_CCADC:
1625 /* reading this will trigger calibration */
1626 *val = 0;
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08001627 pm8xxx_calib_ccadc();
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001628 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001629 default:
1630 ret = -EINVAL;
1631 }
1632 return ret;
1633}
1634DEFINE_SIMPLE_ATTRIBUTE(calc_fops, get_calc, NULL, "%llu\n");
1635
1636static int get_reading(void *data, u64 * val)
1637{
1638 int param = (int)data;
1639 int ret = 0;
1640
1641 *val = 0;
1642
1643 /* global irq number passed in via data */
1644 switch (param) {
1645 case CC_MSB:
1646 case CC_LSB:
1647 read_cc(the_chip, (int *)val);
1648 break;
1649 case LAST_GOOD_OCV_VALUE:
1650 read_last_good_ocv(the_chip, (uint *)val);
1651 break;
1652 case VBATT_FOR_RBATT:
1653 read_vbatt_for_rbatt(the_chip, (uint *)val);
1654 break;
1655 case VSENSE_FOR_RBATT:
1656 read_vsense_for_rbatt(the_chip, (uint *)val);
1657 break;
1658 case OCV_FOR_RBATT:
1659 read_ocv_for_rbatt(the_chip, (uint *)val);
1660 break;
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001661 case VSENSE_AVG:
1662 read_vsense_avg(the_chip, (uint *)val);
1663 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001664 default:
1665 ret = -EINVAL;
1666 }
1667 return ret;
1668}
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001669DEFINE_SIMPLE_ATTRIBUTE(reading_fops, get_reading, NULL, "%lld\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001670
1671static int get_rt_status(void *data, u64 * val)
1672{
1673 int i = (int)data;
1674 int ret;
1675
1676 /* global irq number passed in via data */
1677 ret = pm_bms_get_rt_status(the_chip, i);
1678 *val = ret;
1679 return 0;
1680}
1681DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n");
1682
1683static int get_reg(void *data, u64 * val)
1684{
1685 int addr = (int)data;
1686 int ret;
1687 u8 temp;
1688
1689 ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp);
1690 if (ret) {
1691 pr_err("pm8xxx_readb to %x value = %d errored = %d\n",
1692 addr, temp, ret);
1693 return -EAGAIN;
1694 }
1695 *val = temp;
1696 return 0;
1697}
1698
1699static int set_reg(void *data, u64 val)
1700{
1701 int addr = (int)data;
1702 int ret;
1703 u8 temp;
1704
1705 temp = (u8) val;
1706 ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp);
1707 if (ret) {
1708 pr_err("pm8xxx_writeb to %x value = %d errored = %d\n",
1709 addr, temp, ret);
1710 return -EAGAIN;
1711 }
1712 return 0;
1713}
1714DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
1715
1716static void create_debugfs_entries(struct pm8921_bms_chip *chip)
1717{
1718 int i;
1719
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08001720 chip->dent = debugfs_create_dir("pm8921-bms", NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001721
1722 if (IS_ERR(chip->dent)) {
1723 pr_err("pmic bms couldnt create debugfs dir\n");
1724 return;
1725 }
1726
1727 debugfs_create_file("BMS_CONTROL", 0644, chip->dent,
1728 (void *)BMS_CONTROL, &reg_fops);
1729 debugfs_create_file("BMS_OUTPUT0", 0644, chip->dent,
1730 (void *)BMS_OUTPUT0, &reg_fops);
1731 debugfs_create_file("BMS_OUTPUT1", 0644, chip->dent,
1732 (void *)BMS_OUTPUT1, &reg_fops);
1733 debugfs_create_file("BMS_TEST1", 0644, chip->dent,
1734 (void *)BMS_TEST1, &reg_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001735
1736 debugfs_create_file("test_batt_temp", 0644, chip->dent,
1737 (void *)TEST_BATT_TEMP, &temp_fops);
1738 debugfs_create_file("test_chargecycle", 0644, chip->dent,
1739 (void *)TEST_CHARGE_CYCLE, &temp_fops);
1740 debugfs_create_file("test_ocv", 0644, chip->dent,
1741 (void *)TEST_OCV, &temp_fops);
1742
1743 debugfs_create_file("read_cc", 0644, chip->dent,
1744 (void *)CC_MSB, &reading_fops);
1745 debugfs_create_file("read_last_good_ocv", 0644, chip->dent,
1746 (void *)LAST_GOOD_OCV_VALUE, &reading_fops);
1747 debugfs_create_file("read_vbatt_for_rbatt", 0644, chip->dent,
1748 (void *)VBATT_FOR_RBATT, &reading_fops);
1749 debugfs_create_file("read_vsense_for_rbatt", 0644, chip->dent,
1750 (void *)VSENSE_FOR_RBATT, &reading_fops);
1751 debugfs_create_file("read_ocv_for_rbatt", 0644, chip->dent,
1752 (void *)OCV_FOR_RBATT, &reading_fops);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001753 debugfs_create_file("read_vsense_avg", 0644, chip->dent,
1754 (void *)VSENSE_AVG, &reading_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001755
1756 debugfs_create_file("show_rbatt", 0644, chip->dent,
1757 (void *)CALC_RBATT, &calc_fops);
1758 debugfs_create_file("show_fcc", 0644, chip->dent,
1759 (void *)CALC_FCC, &calc_fops);
1760 debugfs_create_file("show_pc", 0644, chip->dent,
1761 (void *)CALC_PC, &calc_fops);
1762 debugfs_create_file("show_soc", 0644, chip->dent,
1763 (void *)CALC_SOC, &calc_fops);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001764 debugfs_create_file("calib_hkadc", 0644, chip->dent,
1765 (void *)CALIB_HKADC, &calc_fops);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001766 debugfs_create_file("calib_ccadc", 0644, chip->dent,
1767 (void *)CALIB_CCADC, &calc_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001768
1769 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
1770 if (chip->pmic_bms_irq[bms_irq_data[i].irq_id])
1771 debugfs_create_file(bms_irq_data[i].name, 0444,
1772 chip->dent,
1773 (void *)bms_irq_data[i].irq_id,
1774 &rt_fops);
1775 }
1776}
1777
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001778static int __devinit pm8921_bms_probe(struct platform_device *pdev)
1779{
1780 int rc = 0;
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001781 int vbatt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001782 struct pm8921_bms_chip *chip;
1783 const struct pm8921_bms_platform_data *pdata
1784 = pdev->dev.platform_data;
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001785
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001786 if (!pdata) {
1787 pr_err("missing platform data\n");
1788 return -EINVAL;
1789 }
1790
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001791 chip = kzalloc(sizeof(struct pm8921_bms_chip), GFP_KERNEL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001792 if (!chip) {
1793 pr_err("Cannot allocate pm_bms_chip\n");
1794 return -ENOMEM;
1795 }
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001796 spin_lock_init(&chip->bms_output_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001797 chip->dev = &pdev->dev;
1798 chip->r_sense = pdata->r_sense;
1799 chip->i_test = pdata->i_test;
1800 chip->v_failure = pdata->v_failure;
1801 chip->calib_delay_ms = pdata->calib_delay_ms;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001802 rc = set_battery_data(chip);
1803 if (rc) {
1804 pr_err("%s bad battery data %d\n", __func__, rc);
Abhijeet Dharmapurikar4a4456e2011-09-27 15:10:38 -07001805 goto free_chip;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001806 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001807
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001808 chip->batt_temp_channel = pdata->bms_cdata.batt_temp_channel;
1809 chip->vbat_channel = pdata->bms_cdata.vbat_channel;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001810 chip->ref625mv_channel = pdata->bms_cdata.ref625mv_channel;
1811 chip->ref1p25v_channel = pdata->bms_cdata.ref1p25v_channel;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001812 chip->batt_id_channel = pdata->bms_cdata.batt_id_channel;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -07001813 chip->revision = pm8xxx_get_revision(chip->dev->parent);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001814 INIT_WORK(&chip->calib_hkadc_work, calibrate_hkadc_work);
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001815
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001816 rc = request_irqs(chip, pdev);
1817 if (rc) {
1818 pr_err("couldn't register interrupts rc = %d\n", rc);
1819 goto free_chip;
1820 }
1821
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001822 rc = pm8921_bms_hw_init(chip);
1823 if (rc) {
1824 pr_err("couldn't init hardware rc = %d\n", rc);
1825 goto free_irqs;
1826 }
1827
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001828 platform_set_drvdata(pdev, chip);
1829 the_chip = chip;
1830 create_debugfs_entries(chip);
1831
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001832 check_initial_ocv(chip);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001833
1834 INIT_DELAYED_WORK(&chip->calib_ccadc_work, calibrate_ccadc_work);
1835 /* begin calibration only on chips > 2.0 */
1836 if (chip->revision >= PM8XXX_REVISION_8921_2p0)
1837 calibrate_ccadc_work(&(chip->calib_ccadc_work.work));
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001838
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001839 /* initial hkadc calibration */
1840 schedule_work(&chip->calib_hkadc_work);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001841 /* enable the vbatt reading interrupts for scheduling hkadc calib */
1842 pm8921_bms_enable_irq(chip, PM8921_BMS_GOOD_OCV);
1843 pm8921_bms_enable_irq(chip, PM8921_BMS_OCV_FOR_R);
Abhijeet Dharmapurikar21ebadd2011-09-28 21:14:16 -07001844
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001845 get_battery_uvolts(chip, &vbatt);
1846 pr_info("OK battery_capacity_at_boot=%d volt = %d ocv = %d\n",
1847 pm8921_bms_get_percent_charge(),
1848 vbatt, last_ocv_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001849 return 0;
1850
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001851free_irqs:
1852 free_irqs(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001853free_chip:
1854 kfree(chip);
1855 return rc;
1856}
1857
1858static int __devexit pm8921_bms_remove(struct platform_device *pdev)
1859{
1860 struct pm8921_bms_chip *chip = platform_get_drvdata(pdev);
1861
1862 free_irqs(chip);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001863 kfree(chip->adjusted_fcc_temp_lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001864 platform_set_drvdata(pdev, NULL);
1865 the_chip = NULL;
1866 kfree(chip);
1867 return 0;
1868}
1869
1870static struct platform_driver pm8921_bms_driver = {
1871 .probe = pm8921_bms_probe,
1872 .remove = __devexit_p(pm8921_bms_remove),
1873 .driver = {
1874 .name = PM8921_BMS_DEV_NAME,
1875 .owner = THIS_MODULE,
1876 },
1877};
1878
1879static int __init pm8921_bms_init(void)
1880{
1881 return platform_driver_register(&pm8921_bms_driver);
1882}
1883
1884static void __exit pm8921_bms_exit(void)
1885{
1886 platform_driver_unregister(&pm8921_bms_driver);
1887}
1888
1889late_initcall(pm8921_bms_init);
1890module_exit(pm8921_bms_exit);
1891
1892MODULE_LICENSE("GPL v2");
1893MODULE_DESCRIPTION("PMIC8921 bms driver");
1894MODULE_VERSION("1.0");
1895MODULE_ALIAS("platform:" PM8921_BMS_DEV_NAME);