blob: e666bfc8b5bd18ced385ba8a68f926e629d3bad6 [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 Mohanadoss77d106e2011-09-20 16:25:59 -070021#include <linux/mfd/pm8xxx/pm8921-adc.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070022#include <linux/interrupt.h>
23#include <linux/bitops.h>
24#include <linux/debugfs.h>
25#include <linux/slab.h>
26
27#define BMS_CONTROL 0x224
28#define BMS_OUTPUT0 0x230
29#define BMS_OUTPUT1 0x231
30#define BMS_TEST1 0x237
31#define CCADC_ANA_PARAM 0x240
32#define CCADC_DIG_PARAM 0x241
33#define CCADC_RSV 0x242
34#define CCADC_DATA0 0x244
35#define CCADC_DATA1 0x245
36#define CCADC_OFFSET_TRIM1 0x34A
37#define CCADC_OFFSET_TRIM0 0x34B
38
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070039#define ADC_ARB_SECP_CNTRL 0x190
40#define ADC_ARB_SECP_AMUX_CNTRL 0x191
41#define ADC_ARB_SECP_ANA_PARAM 0x192
42#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
46enum pmic_bms_interrupts {
47 PM8921_BMS_SBI_WRITE_OK,
48 PM8921_BMS_CC_THR,
49 PM8921_BMS_VSENSE_THR,
50 PM8921_BMS_VSENSE_FOR_R,
51 PM8921_BMS_OCV_FOR_R,
52 PM8921_BMS_GOOD_OCV,
53 PM8921_BMS_VSENSE_AVG,
54 PM_BMS_MAX_INTS,
55};
56
57/**
58 * struct pm8921_bms_chip -device information
59 * @dev: device pointer to access the parent
60 * @dent: debugfs directory
61 * @r_sense: batt sense resistance value
62 * @i_test: peak current
63 * @v_failure: battery dead voltage
64 * @fcc: battery capacity
65 *
66 */
67struct pm8921_bms_chip {
68 struct device *dev;
69 struct dentry *dent;
70 unsigned int r_sense;
71 unsigned int i_test;
72 unsigned int v_failure;
73 unsigned int fcc;
74 struct single_row_lut *fcc_temp_lut;
75 struct single_row_lut *fcc_sf_lut;
76 struct pc_temp_ocv_lut *pc_temp_ocv_lut;
77 struct pc_sf_lut *pc_sf_lut;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070078 struct work_struct calib_hkadc_work;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079 unsigned int calib_delay_ms;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -070080 unsigned int revision;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070081 unsigned int xoadc_v0625;
82 unsigned int xoadc_v125;
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -070083 unsigned int batt_temp_channel;
84 unsigned int vbat_channel;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070085 unsigned int ref625mv_channel;
86 unsigned int ref1p25v_channel;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -070087 unsigned int batt_id_channel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070088 unsigned int pmic_bms_irq[PM_BMS_MAX_INTS];
89 DECLARE_BITMAP(enabled_irqs, PM_BMS_MAX_INTS);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -070090 spinlock_t bms_output_lock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091};
92
93static struct pm8921_bms_chip *the_chip;
94
95#define DEFAULT_RBATT_MOHMS 128
96#define DEFAULT_UNUSABLE_CHARGE_MAH 10
97#define DEFAULT_OCV_MICROVOLTS 3900000
98#define DEFAULT_REMAINING_CHARGE_MAH 990
99#define DEFAULT_COULUMB_COUNTER 0
100#define DEFAULT_CHARGE_CYCLES 0
101
102static int last_rbatt = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700103static int last_ocv_uv = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104static int last_soc = -EINVAL;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700105static int last_real_fcc = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106
107static int last_chargecycles = DEFAULT_CHARGE_CYCLES;
108static int last_charge_increase;
109
110module_param(last_rbatt, int, 0644);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111module_param(last_ocv_uv, int, 0644);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112module_param(last_chargecycles, int, 0644);
113module_param(last_charge_increase, int, 0644);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700114module_param(last_real_fcc, int, 0644);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700115
116static int pm_bms_get_rt_status(struct pm8921_bms_chip *chip, int irq_id)
117{
118 return pm8xxx_read_irq_stat(chip->dev->parent,
119 chip->pmic_bms_irq[irq_id]);
120}
121
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700122static void pm8921_bms_enable_irq(struct pm8921_bms_chip *chip, int interrupt)
123{
124 if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) {
125 dev_dbg(chip->dev, "%s %d\n", __func__,
126 chip->pmic_bms_irq[interrupt]);
127 enable_irq(chip->pmic_bms_irq[interrupt]);
128 }
129}
130
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700131static void pm8921_bms_disable_irq(struct pm8921_bms_chip *chip, int interrupt)
132{
133 if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700134 pr_debug("%d\n", chip->pmic_bms_irq[interrupt]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135 disable_irq_nosync(chip->pmic_bms_irq[interrupt]);
136 }
137}
138
139static int pm_bms_masked_write(struct pm8921_bms_chip *chip, u16 addr,
140 u8 mask, u8 val)
141{
142 int rc;
143 u8 reg;
144
145 rc = pm8xxx_readb(chip->dev->parent, addr, &reg);
146 if (rc) {
147 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
148 return rc;
149 }
150 reg &= ~mask;
151 reg |= val & mask;
152 rc = pm8xxx_writeb(chip->dev->parent, addr, reg);
153 if (rc) {
154 pr_err("write failed addr = %03X, rc = %d\n", addr, rc);
155 return rc;
156 }
157 return 0;
158}
159
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700160#define HOLD_OREG_DATA BIT(1)
161static int pm_bms_lock_output_data(struct pm8921_bms_chip *chip)
162{
163 int rc;
164
165 rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA,
166 HOLD_OREG_DATA);
167 if (rc) {
168 pr_err("couldnt lock bms output rc = %d\n", rc);
169 return rc;
170 }
171 return 0;
172}
173
174static int pm_bms_unlock_output_data(struct pm8921_bms_chip *chip)
175{
176 int rc;
177
178 rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA, 0);
179 if (rc) {
180 pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc);
181 return rc;
182 }
183 return 0;
184}
185
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186#define SELECT_OUTPUT_DATA 0x1C
187#define SELECT_OUTPUT_TYPE_SHIFT 2
188#define OCV_FOR_RBATT 0x0
189#define VSENSE_FOR_RBATT 0x1
190#define VBATT_FOR_RBATT 0x2
191#define CC_MSB 0x3
192#define CC_LSB 0x4
193#define LAST_GOOD_OCV_VALUE 0x5
194#define VSENSE_AVG 0x6
195#define VBATT_AVG 0x7
196
197static int pm_bms_read_output_data(struct pm8921_bms_chip *chip, int type,
198 int16_t *result)
199{
200 int rc;
201 u8 reg;
202
203 if (!result) {
204 pr_err("result pointer null\n");
205 return -EINVAL;
206 }
207 *result = 0;
208 if (type < OCV_FOR_RBATT || type > VBATT_AVG) {
209 pr_err("invalid type %d asked to read\n", type);
210 return -EINVAL;
211 }
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700212
213 /* make sure the bms registers are locked */
214 rc = pm8xxx_readb(chip->dev->parent, BMS_CONTROL, &reg);
215 if (rc) {
216 pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n",
217 type, rc);
218 return rc;
219 }
220
221 /* Output register data must be held (locked) while reading output */
222 WARN_ON(!(reg && HOLD_OREG_DATA));
223
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 rc = pm_bms_masked_write(chip, BMS_CONTROL, SELECT_OUTPUT_DATA,
225 type << SELECT_OUTPUT_TYPE_SHIFT);
226 if (rc) {
227 pr_err("fail to select %d type in BMS_CONTROL rc = %d\n",
228 type, rc);
229 return rc;
230 }
231
232 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT0, &reg);
233 if (rc) {
234 pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n",
235 type, rc);
236 return rc;
237 }
238 *result = reg;
239 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT1, &reg);
240 if (rc) {
241 pr_err("fail to read BMS_OUTPUT1 for type %d rc = %d\n",
242 type, rc);
243 return rc;
244 }
245 *result |= reg << 8;
246 pr_debug("type %d result %x", type, *result);
247 return 0;
248}
249
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700250#define V_PER_BIT_MUL_FACTOR 293
251#define INTRINSIC_OFFSET 0x6000
252static int vbatt_to_microvolt(unsigned int a)
253{
254 if (a <= INTRINSIC_OFFSET)
255 return 0;
256
257 return (a - INTRINSIC_OFFSET) * V_PER_BIT_MUL_FACTOR;
258}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700259
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700260#define XOADC_CALIB_UV 625000
261static int adjust_xo_reading(struct pm8921_bms_chip *chip, unsigned int uv)
262{
263 u64 numerator = ((u64)uv - chip->xoadc_v0625) * XOADC_CALIB_UV;
264 u64 denominator = chip->xoadc_v125 - chip->xoadc_v0625;
265
266 if (denominator == 0)
267 return uv;
268 return XOADC_CALIB_UV + div_u64(numerator, denominator);
269}
270
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700271#define CC_RESOLUTION_N_V1 1085069
272#define CC_RESOLUTION_D_V1 100000
273#define CC_RESOLUTION_N_V2 868056
274#define CC_RESOLUTION_D_V2 10000
275static s64 cc_to_microvolt_v1(s64 cc)
276{
277 return div_s64(cc * CC_RESOLUTION_N_V1, CC_RESOLUTION_D_V1);
278}
279
280static s64 cc_to_microvolt_v2(s64 cc)
281{
282 return div_s64(cc * CC_RESOLUTION_N_V2, CC_RESOLUTION_D_V2);
283}
284
285static s64 cc_to_microvolt(struct pm8921_bms_chip *chip, s64 cc)
286{
287 /*
288 * resolution (the value of a single bit) was changed after revision 2.0
289 * for more accurate readings
290 */
291 return (chip->revision < PM8XXX_REVISION_8901_2p0) ?
292 cc_to_microvolt_v1((s64)cc) :
293 cc_to_microvolt_v2((s64)cc);
294}
295
296#define CC_READING_TICKS 55
297#define SLEEP_CLK_HZ 32768
298#define SECONDS_PER_HOUR 3600
299static s64 ccmicrovolt_to_uvh(s64 cc_uv)
300{
301 return div_s64(cc_uv * CC_READING_TICKS,
302 SLEEP_CLK_HZ * SECONDS_PER_HOUR);
303}
304
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305/* returns the signed value read from the hardware */
306static int read_cc(struct pm8921_bms_chip *chip, int *result)
307{
308 int rc;
309 uint16_t msw, lsw;
310
311 rc = pm_bms_read_output_data(chip, CC_LSB, &lsw);
312 if (rc) {
313 pr_err("fail to read CC_LSB rc = %d\n", rc);
314 return rc;
315 }
316 rc = pm_bms_read_output_data(chip, CC_MSB, &msw);
317 if (rc) {
318 pr_err("fail to read CC_MSB rc = %d\n", rc);
319 return rc;
320 }
321 *result = msw << 16 | lsw;
322 pr_debug("msw = %04x lsw = %04x cc = %d\n", msw, lsw, *result);
323 return 0;
324}
325
326static int read_last_good_ocv(struct pm8921_bms_chip *chip, uint *result)
327{
328 int rc;
329 uint16_t reading;
330
331 rc = pm_bms_read_output_data(chip, LAST_GOOD_OCV_VALUE, &reading);
332 if (rc) {
333 pr_err("fail to read LAST_GOOD_OCV_VALUE rc = %d\n", rc);
334 return rc;
335 }
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700336 *result = vbatt_to_microvolt(reading);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337 pr_debug("raw = %04x ocv_microV = %u\n", reading, *result);
338 return 0;
339}
340
341static int read_vbatt_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
342{
343 int rc;
344 uint16_t reading;
345
346 rc = pm_bms_read_output_data(chip, VBATT_FOR_RBATT, &reading);
347 if (rc) {
348 pr_err("fail to read VBATT_FOR_RBATT rc = %d\n", rc);
349 return rc;
350 }
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700351 *result = vbatt_to_microvolt(reading);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700352 pr_debug("raw = %04x vbatt_for_r_microV = %u\n", reading, *result);
353 return 0;
354}
355
356static int read_vsense_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
357{
358 int rc;
359 uint16_t reading;
360
361 rc = pm_bms_read_output_data(chip, VSENSE_FOR_RBATT, &reading);
362 if (rc) {
363 pr_err("fail to read VSENSE_FOR_RBATT rc = %d\n", rc);
364 return rc;
365 }
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700366 *result = cc_to_microvolt(chip, reading);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700367 pr_debug("raw = %04x vsense_for_r_microV = %u\n", reading, *result);
368 return 0;
369}
370
371static int read_ocv_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
372{
373 int rc;
374 uint16_t reading;
375
376 rc = pm_bms_read_output_data(chip, OCV_FOR_RBATT, &reading);
377 if (rc) {
378 pr_err("fail to read OCV_FOR_RBATT rc = %d\n", rc);
379 return rc;
380 }
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700381 *result = vbatt_to_microvolt(reading);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700382 pr_debug("read = %04x ocv_for_r_microV = %u\n", reading, *result);
383 return 0;
384}
385
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700386static int read_vsense_avg(struct pm8921_bms_chip *chip, int *result)
387{
388 int rc;
389 int16_t reading;
390
391 rc = pm_bms_read_output_data(chip, VSENSE_AVG, &reading);
392 if (rc) {
393 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
394 return rc;
395 }
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700396 *result = cc_to_microvolt(chip, reading);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700397 pr_debug("read = %04x vsense = %d\n", reading, *result);
398 return 0;
399}
400
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401static int linear_interpolate(int y0, int x0, int y1, int x1, int x)
402{
403 if (y0 == y1 || x == x0)
404 return y0;
405 if (x1 == x0 || x == x1)
406 return y1;
407
408 return y0 + ((y1 - y0) * (x - x0) / (x1 - x0));
409}
410
411static int interpolate_single_lut(struct single_row_lut *lut, int x)
412{
413 int i, result;
414
415 if (x < lut->x[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700416 pr_debug("x %d less than known range return y = %d lut = %pS\n",
417 x, lut->y[0], lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700418 return lut->y[0];
419 }
420 if (x > lut->x[lut->cols - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700421 pr_debug("x %d more than known range return y = %d lut = %pS\n",
422 x, lut->y[lut->cols - 1], lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423 return lut->y[lut->cols - 1];
424 }
425
426 for (i = 0; i < lut->cols; i++)
427 if (x <= lut->x[i])
428 break;
429 if (x == lut->x[i]) {
430 result = lut->y[i];
431 } else {
432 result = linear_interpolate(
433 lut->y[i - 1],
434 lut->x[i - 1],
435 lut->y[i],
436 lut->x[i],
437 x);
438 }
439 return result;
440}
441
442static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp)
443{
444 return interpolate_single_lut(chip->fcc_temp_lut, batt_temp);
445}
446
447static int interpolate_scalingfactor_fcc(struct pm8921_bms_chip *chip,
448 int cycles)
449{
450 return interpolate_single_lut(chip->fcc_sf_lut, cycles);
451}
452
453static int interpolate_scalingfactor_pc(struct pm8921_bms_chip *chip,
454 int cycles, int pc)
455{
Abhijeet Dharmapurikar4a4456e2011-09-27 15:10:38 -0700456 int i, scalefactorrow1, scalefactorrow2, scalefactor;
457 int row1 = 0;
458 int row2 = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 int rows = chip->pc_sf_lut->rows;
460 int cols = chip->pc_sf_lut->cols;
461
462 if (pc > chip->pc_sf_lut->percent[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700463 pr_debug("pc %d greater than known pc ranges for sfd\n", pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464 row1 = 0;
465 row2 = 0;
466 }
467 if (pc < chip->pc_sf_lut->percent[rows - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700468 pr_debug("pc %d less than known pc ranges for sf", pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700469 row1 = rows - 1;
470 row2 = rows - 1;
471 }
472 for (i = 0; i < rows; i++) {
473 if (pc == chip->pc_sf_lut->percent[i]) {
474 row1 = i;
475 row2 = i;
476 break;
477 }
478 if (pc > chip->pc_sf_lut->percent[i]) {
479 row1 = i - 1;
480 row2 = i;
481 break;
482 }
483 }
484
485 if (cycles < chip->pc_sf_lut->cycles[0])
486 cycles = chip->pc_sf_lut->cycles[0];
487 if (cycles > chip->pc_sf_lut->cycles[cols - 1])
488 cycles = chip->pc_sf_lut->cycles[cols - 1];
489
490 for (i = 0; i < cols; i++)
491 if (cycles <= chip->pc_sf_lut->cycles[i])
492 break;
493 if (cycles == chip->pc_sf_lut->cycles[i]) {
494 scalefactor = linear_interpolate(
495 chip->pc_sf_lut->sf[row1][i],
496 chip->pc_sf_lut->percent[row1],
497 chip->pc_sf_lut->sf[row2][i],
498 chip->pc_sf_lut->percent[row2],
499 pc);
500 return scalefactor;
501 }
502
503 scalefactorrow1 = linear_interpolate(
504 chip->pc_sf_lut->sf[row1][i - 1],
505 chip->pc_sf_lut->cycles[i - 1],
506 chip->pc_sf_lut->sf[row1][i],
507 chip->pc_sf_lut->cycles[i],
508 cycles);
509
510 scalefactorrow2 = linear_interpolate(
511 chip->pc_sf_lut->sf[row2][i - 1],
512 chip->pc_sf_lut->cycles[i - 1],
513 chip->pc_sf_lut->sf[row2][i],
514 chip->pc_sf_lut->cycles[i],
515 cycles);
516
517 scalefactor = linear_interpolate(
518 scalefactorrow1,
519 chip->pc_sf_lut->percent[row1],
520 scalefactorrow2,
521 chip->pc_sf_lut->percent[row2],
522 pc);
523
524 return scalefactor;
525}
526
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700527static int is_between(int left, int right, int value)
528{
529 if (left >= right && left >= value && value >= right)
530 return 1;
531 if (left <= right && left <= value && value <= right)
532 return 1;
533
534 return 0;
535}
536
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700537static int interpolate_pc(struct pm8921_bms_chip *chip,
538 int batt_temp, int ocv)
539{
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700540 int i, j, pcj, pcj_minus_one, pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541 int rows = chip->pc_temp_ocv_lut->rows;
542 int cols = chip->pc_temp_ocv_lut->cols;
543
544 if (batt_temp < chip->pc_temp_ocv_lut->temp[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700545 pr_debug("batt_temp %d < known temp range for pc\n", batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700546 batt_temp = chip->pc_temp_ocv_lut->temp[0];
547 }
548 if (batt_temp > chip->pc_temp_ocv_lut->temp[cols - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700549 pr_debug("batt_temp %d > known temp range for pc\n", batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700550 batt_temp = chip->pc_temp_ocv_lut->temp[cols - 1];
551 }
552
553 for (j = 0; j < cols; j++)
554 if (batt_temp <= chip->pc_temp_ocv_lut->temp[j])
555 break;
556 if (batt_temp == chip->pc_temp_ocv_lut->temp[j]) {
557 /* found an exact match for temp in the table */
558 if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j])
559 return chip->pc_temp_ocv_lut->percent[0];
560 if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j])
561 return chip->pc_temp_ocv_lut->percent[rows - 1];
562 for (i = 0; i < rows; i++) {
563 if (ocv >= chip->pc_temp_ocv_lut->ocv[i][j]) {
564 if (ocv == chip->pc_temp_ocv_lut->ocv[i][j])
565 return
566 chip->pc_temp_ocv_lut->percent[i];
567 pc = linear_interpolate(
568 chip->pc_temp_ocv_lut->percent[i],
569 chip->pc_temp_ocv_lut->ocv[i][j],
570 chip->pc_temp_ocv_lut->percent[i - 1],
571 chip->pc_temp_ocv_lut->ocv[i - 1][j],
572 ocv);
573 return pc;
574 }
575 }
576 }
577
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700578 /*
579 * batt_temp is within temperature for
580 * column j-1 and j
581 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582 if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j])
583 return chip->pc_temp_ocv_lut->percent[0];
584 if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j - 1])
585 return chip->pc_temp_ocv_lut->percent[rows - 1];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700586
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700587 pcj_minus_one = 0;
588 pcj = 0;
589 for (i = 0; i < rows-1; i++) {
590 if (pcj == 0
591 && is_between(chip->pc_temp_ocv_lut->ocv[i][j],
592 chip->pc_temp_ocv_lut->ocv[i+1][j], ocv)) {
593 pcj = linear_interpolate(
594 chip->pc_temp_ocv_lut->percent[i],
595 chip->pc_temp_ocv_lut->ocv[i][j],
596 chip->pc_temp_ocv_lut->percent[i + 1],
597 chip->pc_temp_ocv_lut->ocv[i+1][j],
598 ocv);
599 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700601 if (pcj_minus_one == 0
602 && is_between(chip->pc_temp_ocv_lut->ocv[i][j-1],
603 chip->pc_temp_ocv_lut->ocv[i+1][j-1], ocv)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700605 pcj_minus_one = linear_interpolate(
606 chip->pc_temp_ocv_lut->percent[i],
607 chip->pc_temp_ocv_lut->ocv[i][j-1],
608 chip->pc_temp_ocv_lut->percent[i + 1],
609 chip->pc_temp_ocv_lut->ocv[i+1][j-1],
610 ocv);
611 }
612
613 if (pcj && pcj_minus_one) {
614 pc = linear_interpolate(
615 pcj_minus_one,
616 chip->pc_temp_ocv_lut->temp[j-1],
617 pcj,
618 chip->pc_temp_ocv_lut->temp[j],
619 batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700620 return pc;
621 }
622 }
623
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700624 if (pcj)
625 return pcj;
626
627 if (pcj_minus_one)
628 return pcj_minus_one;
629
630 pr_debug("%d ocv wasn't found for temp %d in the LUT returning 100%%",
631 ocv, batt_temp);
632 return 100;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700633}
634
635static int calculate_rbatt(struct pm8921_bms_chip *chip)
636{
637 int rc;
638 unsigned int ocv, vsense, vbatt, r_batt;
639
640 rc = read_ocv_for_rbatt(chip, &ocv);
641 if (rc) {
642 pr_err("fail to read ocv_for_rbatt rc = %d\n", rc);
643 ocv = 0;
644 }
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700645 ocv = adjust_xo_reading(chip, ocv);
646
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700647 rc = read_vbatt_for_rbatt(chip, &vbatt);
648 if (rc) {
649 pr_err("fail to read vbatt_for_rbatt rc = %d\n", rc);
650 ocv = 0;
651 }
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700652 vbatt = adjust_xo_reading(chip, vbatt);
653
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654 rc = read_vsense_for_rbatt(chip, &vsense);
655 if (rc) {
656 pr_err("fail to read vsense_for_rbatt rc = %d\n", rc);
657 ocv = 0;
658 }
659 if (ocv == 0
660 || ocv == vbatt
661 || vsense == 0) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700662 pr_debug("rbatt readings unavailable ocv = %d, vbatt = %d,"
663 "vsen = %d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664 ocv, vbatt, vsense);
665 return -EINVAL;
666 }
667 r_batt = ((ocv - vbatt) * chip->r_sense) / vsense;
668 pr_debug("r_batt = %umilliOhms", r_batt);
669 return r_batt;
670}
671
672static int calculate_fcc(struct pm8921_bms_chip *chip, int batt_temp,
673 int chargecycles)
674{
675 int initfcc, result, scalefactor = 0;
676
677 initfcc = interpolate_fcc(chip, batt_temp);
678 pr_debug("intfcc = %umAh batt_temp = %d\n", initfcc, batt_temp);
679
680 scalefactor = interpolate_scalingfactor_fcc(chip, chargecycles);
681 pr_debug("scalefactor = %d batt_temp = %d\n", scalefactor, batt_temp);
682
683 /* Multiply the initial FCC value by the scale factor. */
684 result = (initfcc * scalefactor) / 100;
685 pr_debug("fcc mAh = %d\n", result);
686 return result;
687}
688
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -0700689static int get_battery_uvolts(struct pm8921_bms_chip *chip, int *uvolts)
690{
691 int rc;
692 struct pm8921_adc_chan_result result;
693
694 rc = pm8921_adc_read(chip->vbat_channel, &result);
695 if (rc) {
696 pr_err("error reading adc channel = %d, rc = %d\n",
697 chip->vbat_channel, rc);
698 return rc;
699 }
700 pr_debug("mvolts phy = %lld meas = 0x%llx", result.physical,
701 result.measurement);
702 *uvolts = (int)result.physical;
703 *uvolts = *uvolts * 1000;
704 return 0;
705}
706
707static int adc_based_ocv(struct pm8921_bms_chip *chip, int *ocv)
708{
709 int vbatt, rbatt, ibatt, rc;
710
711 rc = get_battery_uvolts(chip, &vbatt);
712 if (rc) {
713 pr_err("failed to read vbatt from adc rc = %d\n", rc);
714 last_ocv_uv = DEFAULT_OCV_MICROVOLTS;
715 return rc;
716 }
717
718 rc = pm8921_bms_get_battery_current(&ibatt);
719 if (rc) {
720 pr_err("failed to read batt current rc = %d\n", rc);
721 last_ocv_uv = DEFAULT_OCV_MICROVOLTS;
722 return rc;
723 }
724
725 rbatt = calculate_rbatt(the_chip);
726 if (rbatt < 0)
727 rbatt = DEFAULT_RBATT_MOHMS;
728 *ocv = vbatt + ibatt * rbatt;
729 return 0;
730}
731
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700732static int calculate_pc(struct pm8921_bms_chip *chip, int ocv_uv, int batt_temp,
733 int chargecycles)
734{
735 int pc, scalefactor;
736
737 pc = interpolate_pc(chip, batt_temp, ocv_uv / 1000);
738 pr_debug("pc = %u for ocv = %dmicroVolts batt_temp = %d\n",
739 pc, ocv_uv, batt_temp);
740
741 scalefactor = interpolate_scalingfactor_pc(chip, chargecycles, pc);
742 pr_debug("scalefactor = %u batt_temp = %d\n", scalefactor, batt_temp);
743
744 /* Multiply the initial FCC value by the scale factor. */
745 pc = (pc * scalefactor) / 100;
746 return pc;
747}
748
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700749static void calculate_cc_mah(struct pm8921_bms_chip *chip, int64_t *val,
Abhijeet Dharmapurikar52aa9682011-07-20 14:05:47 -0700750 int *coulumb_counter)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700751{
752 int rc;
753 int64_t cc_voltage_uv, cc_uvh, cc_mah;
754
755 rc = read_cc(the_chip, coulumb_counter);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700756 cc_voltage_uv = (int64_t)*coulumb_counter;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700757 cc_voltage_uv = cc_to_microvolt(chip, cc_voltage_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758 pr_debug("cc_voltage_uv = %lld microvolts\n", cc_voltage_uv);
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700759 cc_uvh = ccmicrovolt_to_uvh(cc_voltage_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700760 pr_debug("cc_uvh = %lld micro_volt_hour\n", cc_uvh);
761 cc_mah = div_s64(cc_uvh, chip->r_sense);
762 *val = cc_mah;
763}
764
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700765static int calculate_unusable_charge_mah(struct pm8921_bms_chip *chip,
766 int fcc, int batt_temp, int chargecycles)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700767{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700768 int rbatt, voltage_unusable_uv, pc_unusable;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769
770 rbatt = calculate_rbatt(chip);
771 if (rbatt < 0) {
772 rbatt = (last_rbatt < 0) ? DEFAULT_RBATT_MOHMS : last_rbatt;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700773 pr_debug("rbatt unavailable assuming %d\n", rbatt);
774 } else {
775 last_rbatt = rbatt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700777
778 /* calculate unusable charge */
779 voltage_unusable_uv = (rbatt * chip->i_test)
780 + (chip->v_failure * 1000);
781 pc_unusable = calculate_pc(chip, voltage_unusable_uv,
782 batt_temp, chargecycles);
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700783 pr_debug("rbatt = %umilliOhms unusable_v =%d unusable_pc = %d\n",
784 rbatt, voltage_unusable_uv, pc_unusable);
785 return (fcc * pc_unusable) / 100;
786}
Abhijeet Dharmapurikar52aa9682011-07-20 14:05:47 -0700787
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700788/* calculate remainging charge at the time of ocv */
789static int calculate_remaining_charge_mah(struct pm8921_bms_chip *chip, int fcc,
790 int batt_temp, int chargecycles)
791{
792 int rc, ocv, pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700793
794 /* calculate remainging charge */
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -0700795 ocv = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700796 rc = read_last_good_ocv(chip, &ocv);
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -0700797 if (rc)
798 pr_debug("failed to read ocv rc = %d\n", rc);
799
800 if (ocv == 0) {
801 ocv = last_ocv_uv;
802 pr_debug("ocv not available using last_ocv_uv=%d\n", ocv);
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700803 } else {
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -0700804 /* update the usespace param since a good ocv is available */
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700805 last_ocv_uv = ocv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700806 }
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700807
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700808 pc = calculate_pc(chip, ocv, batt_temp, chargecycles);
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700809 pr_debug("ocv = %d pc = %d\n", ocv, pc);
810 return (fcc * pc) / 100;
811}
812
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700813static void calculate_charging_params(struct pm8921_bms_chip *chip,
814 int batt_temp, int chargecycles,
815 int *fcc,
816 int *unusable_charge,
817 int *remaining_charge,
818 int64_t *cc_mah)
819{
820 int coulumb_counter;
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700821 unsigned long flags;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700822
823 *fcc = calculate_fcc(chip, batt_temp, chargecycles);
824 pr_debug("FCC = %umAh batt_temp = %d, cycles = %d",
825 *fcc, batt_temp, chargecycles);
826
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700827 /* fcc doesnt need to be read from hardware, lock the bms now */
828 spin_lock_irqsave(&chip->bms_output_lock, flags);
829 pm_bms_lock_output_data(chip);
830
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700831 *unusable_charge = calculate_unusable_charge_mah(chip, *fcc,
832 batt_temp, chargecycles);
833
834 pr_debug("UUC = %umAh", *unusable_charge);
835
836 /* calculate remainging charge */
837 *remaining_charge = calculate_remaining_charge_mah(chip, *fcc,
838 batt_temp, chargecycles);
839 pr_debug("RC = %umAh\n", *remaining_charge);
840
841 /* calculate cc milli_volt_hour */
842 calculate_cc_mah(chip, cc_mah, &coulumb_counter);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700843
844 pm_bms_unlock_output_data(chip);
845 spin_unlock_irqrestore(&chip->bms_output_lock, flags);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700846 pr_debug("cc_mah = %lldmAh cc = %d\n", *cc_mah, coulumb_counter);
847}
848
849static int calculate_real_fcc(struct pm8921_bms_chip *chip,
850 int batt_temp, int chargecycles)
851{
852 int fcc, unusable_charge;
853 int remaining_charge;
854 int64_t cc_mah;
855 int real_fcc;
856
857 calculate_charging_params(chip, batt_temp, chargecycles,
858 &fcc,
859 &unusable_charge,
860 &remaining_charge,
861 &cc_mah);
862
863 real_fcc = remaining_charge - cc_mah;
864
865 return real_fcc;
866}
867/*
868 * Remaining Usable Charge = remaining_charge (charge at ocv instance)
869 * - coloumb counter charge
870 * - unusable charge (due to battery resistance)
871 * SOC% = (remaining usable charge/ fcc - usable_charge);
872 */
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -0700873#define BMS_BATT_NOMINAL 3700000
874#define MIN_OPERABLE_SOC 10
875#define BATTERY_POWER_SUPPLY_SOC 53
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700876static int calculate_state_of_charge(struct pm8921_bms_chip *chip,
877 int batt_temp, int chargecycles)
878{
879 int remaining_usable_charge, fcc, unusable_charge;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700880 int remaining_charge, soc;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700881 int update_userspace = 1;
882 int64_t cc_mah;
883
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700884 calculate_charging_params(chip, batt_temp, chargecycles,
885 &fcc,
886 &unusable_charge,
887 &remaining_charge,
888 &cc_mah);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889
890 /* calculate remaining usable charge */
891 remaining_usable_charge = remaining_charge - cc_mah - unusable_charge;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700892 pr_debug("RUC = %dmAh\n", remaining_usable_charge);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -0700893 soc = (remaining_usable_charge * 100) / (fcc - unusable_charge);
894 if (soc > 100)
895 soc = 100;
896 pr_debug("SOC = %u%%\n", soc);
897
898 if (soc < MIN_OPERABLE_SOC) {
899 int ocv = 0, rc;
900
901 rc = adc_based_ocv(chip, &ocv);
902 if (rc == 0 && ocv >= BMS_BATT_NOMINAL) {
903 /*
904 * The ocv doesnt seem to have dropped for
905 * soc to go negative.
906 * The setup must be using a power supply
907 * instead of real batteries.
908 * Fake high enough soc to prevent userspace
909 * shutdown for low battery
910 */
911 soc = BATTERY_POWER_SUPPLY_SOC;
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700912 pr_debug("Adjusting SOC to %d\n", soc);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -0700913 }
914 }
915
916 if (soc < 0) {
Abhijeet Dharmapurikar21ebadd2011-09-28 21:14:16 -0700917 pr_err("bad rem_usb_chg = %d rem_chg %d,"
918 "cc_mah %lld, unusb_chg %d\n",
919 remaining_usable_charge, remaining_charge,
920 cc_mah, unusable_charge);
921 pr_err("for bad rem_usb_chg last_ocv_uv = %d"
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -0700922 "chargecycles = %d, batt_temp = %d"
923 "fcc = %d soc =%d\n",
924 last_ocv_uv, chargecycles, batt_temp,
925 fcc, soc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700926 update_userspace = 0;
927 }
928
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700929 if (update_userspace) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700930 last_soc = soc;
931 }
932 return soc;
933}
934
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700935#define XOADC_MAX_1P25V 1312500
936#define XOADC_MIN_1P25V 1187500
937#define XOADC_MAX_0P625V 656250
938#define XOADC_MIN_0P625V 593750
939
940#define HKADC_V_PER_BIT_MUL_FACTOR 977
941#define HKADC_V_PER_BIT_DIV_FACTOR 10
942static int calib_hkadc_convert_microvolt(unsigned int phy)
943{
944 return phy * HKADC_V_PER_BIT_MUL_FACTOR / HKADC_V_PER_BIT_DIV_FACTOR;
945}
946
947static void calib_hkadc(struct pm8921_bms_chip *chip)
948{
949 int voltage, rc;
950 struct pm8921_adc_chan_result result;
951
952 rc = pm8921_adc_read(the_chip->ref1p25v_channel, &result);
953 if (rc) {
954 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
955 return;
956 }
957 voltage = calib_hkadc_convert_microvolt(result.physical);
958
959 pr_debug("result 1.25v = 0x%llx, voltage = %dmV adc_meas = %lld\n",
960 result.physical, voltage, result.measurement);
961
962 /* check for valid range */
963 if (voltage > XOADC_MAX_1P25V)
964 voltage = XOADC_MAX_1P25V;
965 else if (voltage < XOADC_MIN_1P25V)
966 voltage = XOADC_MIN_1P25V;
967 chip->xoadc_v125 = voltage;
968
969 rc = pm8921_adc_read(the_chip->ref625mv_channel, &result);
970 if (rc) {
971 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
972 return;
973 }
974 voltage = calib_hkadc_convert_microvolt(result.physical);
975 pr_debug("result 0.625V = 0x%llx, voltage = %dmV adc_mead = %lld\n",
976 result.physical, voltage, result.measurement);
977 /* check for valid range */
978 if (voltage > XOADC_MAX_0P625V)
979 voltage = XOADC_MAX_0P625V;
980 else if (voltage < XOADC_MIN_0P625V)
981 voltage = XOADC_MIN_0P625V;
982
983 chip->xoadc_v0625 = voltage;
984}
985
986static void calibrate_hkadc_work(struct work_struct *work)
987{
988 struct pm8921_bms_chip *chip = container_of(work,
989 struct pm8921_bms_chip, calib_hkadc_work);
990
991 calib_hkadc(chip);
992}
993
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700994int pm8921_bms_get_vsense_avg(int *result)
995{
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700996 int rc = -EINVAL;
997 unsigned long flags;
998
999 if (the_chip) {
1000 spin_lock_irqsave(&the_chip->bms_output_lock, flags);
1001 pm_bms_lock_output_data(the_chip);
1002 rc = read_vsense_avg(the_chip, result);
1003 pm_bms_unlock_output_data(the_chip);
1004 spin_unlock_irqrestore(&the_chip->bms_output_lock, flags);
1005 }
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001006
1007 pr_err("called before initialization\n");
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001008 return rc;
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001009}
1010EXPORT_SYMBOL(pm8921_bms_get_vsense_avg);
1011
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001012int pm8921_bms_get_battery_current(int *result)
1013{
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001014 unsigned long flags;
1015
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001016 if (!the_chip) {
1017 pr_err("called before initialization\n");
1018 return -EINVAL;
1019 }
1020 if (the_chip->r_sense == 0) {
1021 pr_err("r_sense is zero\n");
1022 return -EINVAL;
1023 }
1024
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001025 spin_lock_irqsave(&the_chip->bms_output_lock, flags);
1026 pm_bms_lock_output_data(the_chip);
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001027 read_vsense_avg(the_chip, result);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001028 pm_bms_unlock_output_data(the_chip);
1029 spin_unlock_irqrestore(&the_chip->bms_output_lock, flags);
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001030 pr_debug("vsense=%d\n", *result);
Abhijeet Dharmapurikara7a1c6b2011-08-17 10:04:58 -07001031 /* cast for signed division */
1032 *result = *result / (int)the_chip->r_sense;
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001033 return 0;
1034}
1035EXPORT_SYMBOL(pm8921_bms_get_battery_current);
1036
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001037int pm8921_bms_get_percent_charge(void)
1038{
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001039 int batt_temp, rc;
1040 struct pm8921_adc_chan_result result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001041
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001042 if (!the_chip) {
1043 pr_err("called before initialization\n");
1044 return -EINVAL;
1045 }
1046
1047 rc = pm8921_adc_read(the_chip->batt_temp_channel, &result);
1048 if (rc) {
1049 pr_err("error reading adc channel = %d, rc = %d\n",
1050 the_chip->batt_temp_channel, rc);
1051 return rc;
1052 }
1053 pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical,
1054 result.measurement);
1055 batt_temp = (int)result.physical;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001056 return calculate_state_of_charge(the_chip,
1057 batt_temp, last_chargecycles);
1058}
1059EXPORT_SYMBOL_GPL(pm8921_bms_get_percent_charge);
1060
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07001061int pm8921_bms_get_fcc(void)
1062{
1063 int batt_temp, rc;
1064 struct pm8921_adc_chan_result result;
1065
1066 if (!the_chip) {
1067 pr_err("called before initialization\n");
1068 return -EINVAL;
1069 }
1070
1071 rc = pm8921_adc_read(the_chip->batt_temp_channel, &result);
1072 if (rc) {
1073 pr_err("error reading adc channel = %d, rc = %d\n",
1074 the_chip->batt_temp_channel, rc);
1075 return rc;
1076 }
1077 pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical,
1078 result.measurement);
1079 batt_temp = (int)result.physical;
1080 return calculate_fcc(the_chip, batt_temp, last_chargecycles);
1081}
1082EXPORT_SYMBOL_GPL(pm8921_bms_get_fcc);
1083
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001084static int start_percent;
1085static int end_percent;
1086void pm8921_bms_charging_began(void)
1087{
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001088 start_percent = pm8921_bms_get_percent_charge();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001089 pr_debug("start_percent = %u%%\n", start_percent);
1090}
1091EXPORT_SYMBOL_GPL(pm8921_bms_charging_began);
1092
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001093void pm8921_bms_charging_end(int is_battery_full)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001094{
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001095 if (is_battery_full && the_chip != NULL) {
1096 int batt_temp, rc;
1097 struct pm8921_adc_chan_result result;
1098
1099 rc = pm8921_adc_read(the_chip->batt_temp_channel, &result);
1100 if (rc) {
1101 pr_err("error reading adc channel = %d, rc = %d\n",
1102 the_chip->batt_temp_channel, rc);
1103 goto charge_cycle_calculation;
1104 }
1105 pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical,
1106 result.measurement);
1107 batt_temp = (int)result.physical;
1108 last_real_fcc = calculate_real_fcc(the_chip,
1109 batt_temp, last_chargecycles);
1110 }
1111
1112charge_cycle_calculation:
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001113 end_percent = pm8921_bms_get_percent_charge();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001114 if (end_percent > start_percent) {
1115 last_charge_increase = end_percent - start_percent;
1116 if (last_charge_increase > 100) {
1117 last_chargecycles++;
1118 last_charge_increase = last_charge_increase % 100;
1119 }
1120 }
1121 pr_debug("end_percent = %u%% last_charge_increase = %d"
1122 "last_chargecycles = %d\n",
1123 end_percent,
1124 last_charge_increase,
1125 last_chargecycles);
1126}
1127EXPORT_SYMBOL_GPL(pm8921_bms_charging_end);
1128
1129static irqreturn_t pm8921_bms_sbi_write_ok_handler(int irq, void *data)
1130{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001131 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001132 return IRQ_HANDLED;
1133}
1134
1135static irqreturn_t pm8921_bms_cc_thr_handler(int irq, void *data)
1136{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001137 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001138 return IRQ_HANDLED;
1139}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001140
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001141static irqreturn_t pm8921_bms_vsense_thr_handler(int irq, void *data)
1142{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001143 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001144 return IRQ_HANDLED;
1145}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001146
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001147static irqreturn_t pm8921_bms_vsense_for_r_handler(int irq, void *data)
1148{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001149 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001150 return IRQ_HANDLED;
1151}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001152
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001153static irqreturn_t pm8921_bms_ocv_for_r_handler(int irq, void *data)
1154{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001155 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001156
1157 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001158 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001159 return IRQ_HANDLED;
1160}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001161
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001162static irqreturn_t pm8921_bms_good_ocv_handler(int irq, void *data)
1163{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001164 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001165
1166 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001167 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001168 return IRQ_HANDLED;
1169}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001170
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001171static irqreturn_t pm8921_bms_vsense_avg_handler(int irq, void *data)
1172{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001173 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001174 return IRQ_HANDLED;
1175}
1176
1177struct pm_bms_irq_init_data {
1178 unsigned int irq_id;
1179 char *name;
1180 unsigned long flags;
1181 irqreturn_t (*handler)(int, void *);
1182};
1183
1184#define BMS_IRQ(_id, _flags, _handler) \
1185{ \
1186 .irq_id = _id, \
1187 .name = #_id, \
1188 .flags = _flags, \
1189 .handler = _handler, \
1190}
1191
1192struct pm_bms_irq_init_data bms_irq_data[] = {
1193 BMS_IRQ(PM8921_BMS_SBI_WRITE_OK, IRQF_TRIGGER_RISING,
1194 pm8921_bms_sbi_write_ok_handler),
1195 BMS_IRQ(PM8921_BMS_CC_THR, IRQF_TRIGGER_RISING,
1196 pm8921_bms_cc_thr_handler),
1197 BMS_IRQ(PM8921_BMS_VSENSE_THR, IRQF_TRIGGER_RISING,
1198 pm8921_bms_vsense_thr_handler),
1199 BMS_IRQ(PM8921_BMS_VSENSE_FOR_R, IRQF_TRIGGER_RISING,
1200 pm8921_bms_vsense_for_r_handler),
1201 BMS_IRQ(PM8921_BMS_OCV_FOR_R, IRQF_TRIGGER_RISING,
1202 pm8921_bms_ocv_for_r_handler),
1203 BMS_IRQ(PM8921_BMS_GOOD_OCV, IRQF_TRIGGER_RISING,
1204 pm8921_bms_good_ocv_handler),
1205 BMS_IRQ(PM8921_BMS_VSENSE_AVG, IRQF_TRIGGER_RISING,
1206 pm8921_bms_vsense_avg_handler),
1207};
1208
1209static void free_irqs(struct pm8921_bms_chip *chip)
1210{
1211 int i;
1212
1213 for (i = 0; i < PM_BMS_MAX_INTS; i++)
1214 if (chip->pmic_bms_irq[i]) {
1215 free_irq(chip->pmic_bms_irq[i], NULL);
1216 chip->pmic_bms_irq[i] = 0;
1217 }
1218}
1219
1220static int __devinit request_irqs(struct pm8921_bms_chip *chip,
1221 struct platform_device *pdev)
1222{
1223 struct resource *res;
1224 int ret, i;
1225
1226 ret = 0;
1227 bitmap_fill(chip->enabled_irqs, PM_BMS_MAX_INTS);
1228
1229 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
1230 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1231 bms_irq_data[i].name);
1232 if (res == NULL) {
1233 pr_err("couldn't find %s\n", bms_irq_data[i].name);
1234 goto err_out;
1235 }
1236 ret = request_irq(res->start, bms_irq_data[i].handler,
1237 bms_irq_data[i].flags,
1238 bms_irq_data[i].name, chip);
1239 if (ret < 0) {
1240 pr_err("couldn't request %d (%s) %d\n", res->start,
1241 bms_irq_data[i].name, ret);
1242 goto err_out;
1243 }
1244 chip->pmic_bms_irq[bms_irq_data[i].irq_id] = res->start;
1245 pm8921_bms_disable_irq(chip, bms_irq_data[i].irq_id);
1246 }
1247 return 0;
1248
1249err_out:
1250 free_irqs(chip);
1251 return -EINVAL;
1252}
1253
1254#define EN_BMS_BIT BIT(7)
1255#define EN_PON_HS_BIT BIT(0)
1256static int __devinit pm8921_bms_hw_init(struct pm8921_bms_chip *chip)
1257{
1258 int rc;
1259
1260 rc = pm_bms_masked_write(chip, BMS_CONTROL,
1261 EN_BMS_BIT | EN_PON_HS_BIT, EN_BMS_BIT | EN_PON_HS_BIT);
1262 if (rc) {
1263 pr_err("failed to enable pon and bms addr = %d %d",
1264 BMS_CONTROL, rc);
1265 }
1266
1267 return 0;
1268}
1269
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001270static void check_initial_ocv(struct pm8921_bms_chip *chip)
1271{
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001272 int ocv, rc;
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001273
1274 /*
1275 * Check if a last_good_ocv is available,
1276 * if not compute it here at boot time.
1277 */
1278 rc = read_last_good_ocv(chip, &ocv);
1279 if (rc || ocv == 0) {
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001280 rc = adc_based_ocv(chip, &last_ocv_uv);
1281 pr_err("failed to read ocv from adc and bms rc = %d\n", rc);
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001282 }
1283 pr_debug("ocv = %d last_ocv_uv = %d\n", ocv, last_ocv_uv);
1284}
1285
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001286static int64_t read_battery_id(struct pm8921_bms_chip *chip)
1287{
1288 int rc;
1289 struct pm8921_adc_chan_result result;
1290
1291 rc = pm8921_adc_read(chip->batt_id_channel, &result);
1292 if (rc) {
1293 pr_err("error reading batt id channel = %d, rc = %d\n",
1294 chip->vbat_channel, rc);
1295 return rc;
1296 }
1297 pr_debug("batt_id phy = %lld meas = 0x%llx\n", result.physical,
1298 result.measurement);
1299 return result.physical;
1300}
1301
1302#define PALLADIUM_ID_MIN 2500
1303#define PALLADIUM_ID_MAX 4000
1304static int set_battery_data(struct pm8921_bms_chip *chip)
1305{
1306 int64_t battery_id;
1307
1308 battery_id = read_battery_id(chip);
1309
1310 if (battery_id < 0) {
1311 pr_err("cannot read battery id err = %lld\n", battery_id);
1312 return battery_id;
1313 }
1314
1315 if (is_between(PALLADIUM_ID_MIN, PALLADIUM_ID_MAX, battery_id)) {
1316 chip->fcc = palladium_1500_data.fcc;
1317 chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut;
1318 chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut;
1319 chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut;
1320 chip->pc_sf_lut = palladium_1500_data.pc_sf_lut;
1321 return 0;
1322 } else {
1323 pr_warn("invalid battery id, palladium 1500 assumed\n");
1324 chip->fcc = palladium_1500_data.fcc;
1325 chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut;
1326 chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut;
1327 chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut;
1328 chip->pc_sf_lut = palladium_1500_data.pc_sf_lut;
1329 return 0;
1330 }
1331}
1332
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001333enum {
1334 CALC_RBATT,
1335 CALC_FCC,
1336 CALC_PC,
1337 CALC_SOC,
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001338 CALIB_HKADC,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001339};
1340
1341static int test_batt_temp = 5;
1342static int test_chargecycle = 150;
1343static int test_ocv = 3900000;
1344enum {
1345 TEST_BATT_TEMP,
1346 TEST_CHARGE_CYCLE,
1347 TEST_OCV,
1348};
1349static int get_test_param(void *data, u64 * val)
1350{
1351 switch ((int)data) {
1352 case TEST_BATT_TEMP:
1353 *val = test_batt_temp;
1354 break;
1355 case TEST_CHARGE_CYCLE:
1356 *val = test_chargecycle;
1357 break;
1358 case TEST_OCV:
1359 *val = test_ocv;
1360 break;
1361 default:
1362 return -EINVAL;
1363 }
1364 return 0;
1365}
1366static int set_test_param(void *data, u64 val)
1367{
1368 switch ((int)data) {
1369 case TEST_BATT_TEMP:
1370 test_batt_temp = (int)val;
1371 break;
1372 case TEST_CHARGE_CYCLE:
1373 test_chargecycle = (int)val;
1374 break;
1375 case TEST_OCV:
1376 test_ocv = (int)val;
1377 break;
1378 default:
1379 return -EINVAL;
1380 }
1381 return 0;
1382}
1383DEFINE_SIMPLE_ATTRIBUTE(temp_fops, get_test_param, set_test_param, "%llu\n");
1384
1385static int get_calc(void *data, u64 * val)
1386{
1387 int param = (int)data;
1388 int ret = 0;
1389
1390 *val = 0;
1391
1392 /* global irq number passed in via data */
1393 switch (param) {
1394 case CALC_RBATT:
1395 *val = calculate_rbatt(the_chip);
1396 break;
1397 case CALC_FCC:
1398 *val = calculate_fcc(the_chip, test_batt_temp,
1399 test_chargecycle);
1400 break;
1401 case CALC_PC:
1402 *val = calculate_pc(the_chip, test_ocv, test_batt_temp,
1403 test_chargecycle);
1404 break;
1405 case CALC_SOC:
1406 *val = calculate_state_of_charge(the_chip,
1407 test_batt_temp, test_chargecycle);
1408 break;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001409 case CALIB_HKADC:
1410 /* reading this will trigger calibration */
1411 *val = 0;
1412 calib_hkadc(the_chip);
1413 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001414 default:
1415 ret = -EINVAL;
1416 }
1417 return ret;
1418}
1419DEFINE_SIMPLE_ATTRIBUTE(calc_fops, get_calc, NULL, "%llu\n");
1420
1421static int get_reading(void *data, u64 * val)
1422{
1423 int param = (int)data;
1424 int ret = 0;
1425
1426 *val = 0;
1427
1428 /* global irq number passed in via data */
1429 switch (param) {
1430 case CC_MSB:
1431 case CC_LSB:
1432 read_cc(the_chip, (int *)val);
1433 break;
1434 case LAST_GOOD_OCV_VALUE:
1435 read_last_good_ocv(the_chip, (uint *)val);
1436 break;
1437 case VBATT_FOR_RBATT:
1438 read_vbatt_for_rbatt(the_chip, (uint *)val);
1439 break;
1440 case VSENSE_FOR_RBATT:
1441 read_vsense_for_rbatt(the_chip, (uint *)val);
1442 break;
1443 case OCV_FOR_RBATT:
1444 read_ocv_for_rbatt(the_chip, (uint *)val);
1445 break;
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001446 case VSENSE_AVG:
1447 read_vsense_avg(the_chip, (uint *)val);
1448 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001449 default:
1450 ret = -EINVAL;
1451 }
1452 return ret;
1453}
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001454DEFINE_SIMPLE_ATTRIBUTE(reading_fops, get_reading, NULL, "%lld\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001455
1456static int get_rt_status(void *data, u64 * val)
1457{
1458 int i = (int)data;
1459 int ret;
1460
1461 /* global irq number passed in via data */
1462 ret = pm_bms_get_rt_status(the_chip, i);
1463 *val = ret;
1464 return 0;
1465}
1466DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n");
1467
1468static int get_reg(void *data, u64 * val)
1469{
1470 int addr = (int)data;
1471 int ret;
1472 u8 temp;
1473
1474 ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp);
1475 if (ret) {
1476 pr_err("pm8xxx_readb to %x value = %d errored = %d\n",
1477 addr, temp, ret);
1478 return -EAGAIN;
1479 }
1480 *val = temp;
1481 return 0;
1482}
1483
1484static int set_reg(void *data, u64 val)
1485{
1486 int addr = (int)data;
1487 int ret;
1488 u8 temp;
1489
1490 temp = (u8) val;
1491 ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp);
1492 if (ret) {
1493 pr_err("pm8xxx_writeb to %x value = %d errored = %d\n",
1494 addr, temp, ret);
1495 return -EAGAIN;
1496 }
1497 return 0;
1498}
1499DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
1500
1501static void create_debugfs_entries(struct pm8921_bms_chip *chip)
1502{
1503 int i;
1504
1505 chip->dent = debugfs_create_dir("pm8921_bms", NULL);
1506
1507 if (IS_ERR(chip->dent)) {
1508 pr_err("pmic bms couldnt create debugfs dir\n");
1509 return;
1510 }
1511
1512 debugfs_create_file("BMS_CONTROL", 0644, chip->dent,
1513 (void *)BMS_CONTROL, &reg_fops);
1514 debugfs_create_file("BMS_OUTPUT0", 0644, chip->dent,
1515 (void *)BMS_OUTPUT0, &reg_fops);
1516 debugfs_create_file("BMS_OUTPUT1", 0644, chip->dent,
1517 (void *)BMS_OUTPUT1, &reg_fops);
1518 debugfs_create_file("BMS_TEST1", 0644, chip->dent,
1519 (void *)BMS_TEST1, &reg_fops);
1520 debugfs_create_file("CCADC_ANA_PARAM", 0644, chip->dent,
1521 (void *)CCADC_ANA_PARAM, &reg_fops);
1522 debugfs_create_file("CCADC_DIG_PARAM", 0644, chip->dent,
1523 (void *)CCADC_DIG_PARAM, &reg_fops);
1524 debugfs_create_file("CCADC_RSV", 0644, chip->dent,
1525 (void *)CCADC_RSV, &reg_fops);
1526 debugfs_create_file("CCADC_DATA0", 0644, chip->dent,
1527 (void *)CCADC_DATA0, &reg_fops);
1528 debugfs_create_file("CCADC_DATA1", 0644, chip->dent,
1529 (void *)CCADC_DATA1, &reg_fops);
1530 debugfs_create_file("CCADC_OFFSET_TRIM1", 0644, chip->dent,
1531 (void *)CCADC_OFFSET_TRIM1, &reg_fops);
1532 debugfs_create_file("CCADC_OFFSET_TRIM0", 0644, chip->dent,
1533 (void *)CCADC_OFFSET_TRIM0, &reg_fops);
1534
1535 debugfs_create_file("test_batt_temp", 0644, chip->dent,
1536 (void *)TEST_BATT_TEMP, &temp_fops);
1537 debugfs_create_file("test_chargecycle", 0644, chip->dent,
1538 (void *)TEST_CHARGE_CYCLE, &temp_fops);
1539 debugfs_create_file("test_ocv", 0644, chip->dent,
1540 (void *)TEST_OCV, &temp_fops);
1541
1542 debugfs_create_file("read_cc", 0644, chip->dent,
1543 (void *)CC_MSB, &reading_fops);
1544 debugfs_create_file("read_last_good_ocv", 0644, chip->dent,
1545 (void *)LAST_GOOD_OCV_VALUE, &reading_fops);
1546 debugfs_create_file("read_vbatt_for_rbatt", 0644, chip->dent,
1547 (void *)VBATT_FOR_RBATT, &reading_fops);
1548 debugfs_create_file("read_vsense_for_rbatt", 0644, chip->dent,
1549 (void *)VSENSE_FOR_RBATT, &reading_fops);
1550 debugfs_create_file("read_ocv_for_rbatt", 0644, chip->dent,
1551 (void *)OCV_FOR_RBATT, &reading_fops);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001552 debugfs_create_file("read_vsense_avg", 0644, chip->dent,
1553 (void *)VSENSE_AVG, &reading_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001554
1555 debugfs_create_file("show_rbatt", 0644, chip->dent,
1556 (void *)CALC_RBATT, &calc_fops);
1557 debugfs_create_file("show_fcc", 0644, chip->dent,
1558 (void *)CALC_FCC, &calc_fops);
1559 debugfs_create_file("show_pc", 0644, chip->dent,
1560 (void *)CALC_PC, &calc_fops);
1561 debugfs_create_file("show_soc", 0644, chip->dent,
1562 (void *)CALC_SOC, &calc_fops);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001563 debugfs_create_file("calib_hkadc", 0644, chip->dent,
1564 (void *)CALIB_HKADC, &calc_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001565
1566 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
1567 if (chip->pmic_bms_irq[bms_irq_data[i].irq_id])
1568 debugfs_create_file(bms_irq_data[i].name, 0444,
1569 chip->dent,
1570 (void *)bms_irq_data[i].irq_id,
1571 &rt_fops);
1572 }
1573}
1574
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001575static int __devinit pm8921_bms_probe(struct platform_device *pdev)
1576{
1577 int rc = 0;
1578 struct pm8921_bms_chip *chip;
1579 const struct pm8921_bms_platform_data *pdata
1580 = pdev->dev.platform_data;
1581 if (!pdata) {
1582 pr_err("missing platform data\n");
1583 return -EINVAL;
1584 }
1585
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001586 chip = kzalloc(sizeof(struct pm8921_bms_chip), GFP_KERNEL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001587 if (!chip) {
1588 pr_err("Cannot allocate pm_bms_chip\n");
1589 return -ENOMEM;
1590 }
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001591 spin_lock_init(&chip->bms_output_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001592 chip->dev = &pdev->dev;
1593 chip->r_sense = pdata->r_sense;
1594 chip->i_test = pdata->i_test;
1595 chip->v_failure = pdata->v_failure;
1596 chip->calib_delay_ms = pdata->calib_delay_ms;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001597 rc = set_battery_data(chip);
1598 if (rc) {
1599 pr_err("%s bad battery data %d\n", __func__, rc);
Abhijeet Dharmapurikar4a4456e2011-09-27 15:10:38 -07001600 goto free_chip;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001601 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001602
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001603 chip->batt_temp_channel = pdata->bms_cdata.batt_temp_channel;
1604 chip->vbat_channel = pdata->bms_cdata.vbat_channel;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001605 chip->ref625mv_channel = pdata->bms_cdata.ref625mv_channel;
1606 chip->ref1p25v_channel = pdata->bms_cdata.ref1p25v_channel;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001607 chip->batt_id_channel = pdata->bms_cdata.batt_id_channel;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -07001608 chip->revision = pm8xxx_get_revision(chip->dev->parent);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001609 INIT_WORK(&chip->calib_hkadc_work, calibrate_hkadc_work);
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001610
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001611 rc = pm8921_bms_hw_init(chip);
1612 if (rc) {
1613 pr_err("couldn't init hardware rc = %d\n", rc);
1614 goto free_chip;
1615 }
1616
1617 rc = request_irqs(chip, pdev);
1618 if (rc) {
1619 pr_err("couldn't register interrupts rc = %d\n", rc);
1620 goto free_chip;
1621 }
1622
1623 platform_set_drvdata(pdev, chip);
1624 the_chip = chip;
1625 create_debugfs_entries(chip);
1626
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001627 check_initial_ocv(chip);
1628
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001629 /* enable the vbatt reading interrupts for scheduling hkadc calib */
1630 pm8921_bms_enable_irq(chip, PM8921_BMS_GOOD_OCV);
1631 pm8921_bms_enable_irq(chip, PM8921_BMS_OCV_FOR_R);
Abhijeet Dharmapurikar21ebadd2011-09-28 21:14:16 -07001632
1633 pr_info("OK battery_capacity_at_boot=%d\n",
1634 pm8921_bms_get_percent_charge());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001635 return 0;
1636
1637free_chip:
1638 kfree(chip);
1639 return rc;
1640}
1641
1642static int __devexit pm8921_bms_remove(struct platform_device *pdev)
1643{
1644 struct pm8921_bms_chip *chip = platform_get_drvdata(pdev);
1645
1646 free_irqs(chip);
1647 platform_set_drvdata(pdev, NULL);
1648 the_chip = NULL;
1649 kfree(chip);
1650 return 0;
1651}
1652
1653static struct platform_driver pm8921_bms_driver = {
1654 .probe = pm8921_bms_probe,
1655 .remove = __devexit_p(pm8921_bms_remove),
1656 .driver = {
1657 .name = PM8921_BMS_DEV_NAME,
1658 .owner = THIS_MODULE,
1659 },
1660};
1661
1662static int __init pm8921_bms_init(void)
1663{
1664 return platform_driver_register(&pm8921_bms_driver);
1665}
1666
1667static void __exit pm8921_bms_exit(void)
1668{
1669 platform_driver_unregister(&pm8921_bms_driver);
1670}
1671
1672late_initcall(pm8921_bms_init);
1673module_exit(pm8921_bms_exit);
1674
1675MODULE_LICENSE("GPL v2");
1676MODULE_DESCRIPTION("PMIC8921 bms driver");
1677MODULE_VERSION("1.0");
1678MODULE_ALIAS("platform:" PM8921_BMS_DEV_NAME);