blob: 7ac2f42915b2b62b0f047a1ff16ab61b5bf1be4a [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);
90};
91
92static struct pm8921_bms_chip *the_chip;
93
94#define DEFAULT_RBATT_MOHMS 128
95#define DEFAULT_UNUSABLE_CHARGE_MAH 10
96#define DEFAULT_OCV_MICROVOLTS 3900000
97#define DEFAULT_REMAINING_CHARGE_MAH 990
98#define DEFAULT_COULUMB_COUNTER 0
99#define DEFAULT_CHARGE_CYCLES 0
100
101static int last_rbatt = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102static int last_ocv_uv = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700103static int last_soc = -EINVAL;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700104static int last_real_fcc = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105
106static int last_chargecycles = DEFAULT_CHARGE_CYCLES;
107static int last_charge_increase;
108
109module_param(last_rbatt, int, 0644);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110module_param(last_ocv_uv, int, 0644);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111module_param(last_chargecycles, int, 0644);
112module_param(last_charge_increase, int, 0644);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700113module_param(last_real_fcc, int, 0644);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114
115static int pm_bms_get_rt_status(struct pm8921_bms_chip *chip, int irq_id)
116{
117 return pm8xxx_read_irq_stat(chip->dev->parent,
118 chip->pmic_bms_irq[irq_id]);
119}
120
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700121static void pm8921_bms_enable_irq(struct pm8921_bms_chip *chip, int interrupt)
122{
123 if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) {
124 dev_dbg(chip->dev, "%s %d\n", __func__,
125 chip->pmic_bms_irq[interrupt]);
126 enable_irq(chip->pmic_bms_irq[interrupt]);
127 }
128}
129
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130static void pm8921_bms_disable_irq(struct pm8921_bms_chip *chip, int interrupt)
131{
132 if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700133 pr_debug("%d\n", chip->pmic_bms_irq[interrupt]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134 disable_irq_nosync(chip->pmic_bms_irq[interrupt]);
135 }
136}
137
138static int pm_bms_masked_write(struct pm8921_bms_chip *chip, u16 addr,
139 u8 mask, u8 val)
140{
141 int rc;
142 u8 reg;
143
144 rc = pm8xxx_readb(chip->dev->parent, addr, &reg);
145 if (rc) {
146 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
147 return rc;
148 }
149 reg &= ~mask;
150 reg |= val & mask;
151 rc = pm8xxx_writeb(chip->dev->parent, addr, reg);
152 if (rc) {
153 pr_err("write failed addr = %03X, rc = %d\n", addr, rc);
154 return rc;
155 }
156 return 0;
157}
158
159#define SELECT_OUTPUT_DATA 0x1C
160#define SELECT_OUTPUT_TYPE_SHIFT 2
161#define OCV_FOR_RBATT 0x0
162#define VSENSE_FOR_RBATT 0x1
163#define VBATT_FOR_RBATT 0x2
164#define CC_MSB 0x3
165#define CC_LSB 0x4
166#define LAST_GOOD_OCV_VALUE 0x5
167#define VSENSE_AVG 0x6
168#define VBATT_AVG 0x7
169
170static int pm_bms_read_output_data(struct pm8921_bms_chip *chip, int type,
171 int16_t *result)
172{
173 int rc;
174 u8 reg;
175
176 if (!result) {
177 pr_err("result pointer null\n");
178 return -EINVAL;
179 }
180 *result = 0;
181 if (type < OCV_FOR_RBATT || type > VBATT_AVG) {
182 pr_err("invalid type %d asked to read\n", type);
183 return -EINVAL;
184 }
185 rc = pm_bms_masked_write(chip, BMS_CONTROL, SELECT_OUTPUT_DATA,
186 type << SELECT_OUTPUT_TYPE_SHIFT);
187 if (rc) {
188 pr_err("fail to select %d type in BMS_CONTROL rc = %d\n",
189 type, rc);
190 return rc;
191 }
192
193 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT0, &reg);
194 if (rc) {
195 pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n",
196 type, rc);
197 return rc;
198 }
199 *result = reg;
200 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT1, &reg);
201 if (rc) {
202 pr_err("fail to read BMS_OUTPUT1 for type %d rc = %d\n",
203 type, rc);
204 return rc;
205 }
206 *result |= reg << 8;
207 pr_debug("type %d result %x", type, *result);
208 return 0;
209}
210
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700211#define V_PER_BIT_MUL_FACTOR 293
212#define INTRINSIC_OFFSET 0x6000
213static int vbatt_to_microvolt(unsigned int a)
214{
215 if (a <= INTRINSIC_OFFSET)
216 return 0;
217
218 return (a - INTRINSIC_OFFSET) * V_PER_BIT_MUL_FACTOR;
219}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700221#define XOADC_CALIB_UV 625000
222static int adjust_xo_reading(struct pm8921_bms_chip *chip, unsigned int uv)
223{
224 u64 numerator = ((u64)uv - chip->xoadc_v0625) * XOADC_CALIB_UV;
225 u64 denominator = chip->xoadc_v125 - chip->xoadc_v0625;
226
227 if (denominator == 0)
228 return uv;
229 return XOADC_CALIB_UV + div_u64(numerator, denominator);
230}
231
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700232#define CC_RESOLUTION_N_V1 1085069
233#define CC_RESOLUTION_D_V1 100000
234#define CC_RESOLUTION_N_V2 868056
235#define CC_RESOLUTION_D_V2 10000
236static s64 cc_to_microvolt_v1(s64 cc)
237{
238 return div_s64(cc * CC_RESOLUTION_N_V1, CC_RESOLUTION_D_V1);
239}
240
241static s64 cc_to_microvolt_v2(s64 cc)
242{
243 return div_s64(cc * CC_RESOLUTION_N_V2, CC_RESOLUTION_D_V2);
244}
245
246static s64 cc_to_microvolt(struct pm8921_bms_chip *chip, s64 cc)
247{
248 /*
249 * resolution (the value of a single bit) was changed after revision 2.0
250 * for more accurate readings
251 */
252 return (chip->revision < PM8XXX_REVISION_8901_2p0) ?
253 cc_to_microvolt_v1((s64)cc) :
254 cc_to_microvolt_v2((s64)cc);
255}
256
257#define CC_READING_TICKS 55
258#define SLEEP_CLK_HZ 32768
259#define SECONDS_PER_HOUR 3600
260static s64 ccmicrovolt_to_uvh(s64 cc_uv)
261{
262 return div_s64(cc_uv * CC_READING_TICKS,
263 SLEEP_CLK_HZ * SECONDS_PER_HOUR);
264}
265
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266/* returns the signed value read from the hardware */
267static int read_cc(struct pm8921_bms_chip *chip, int *result)
268{
269 int rc;
270 uint16_t msw, lsw;
271
272 rc = pm_bms_read_output_data(chip, CC_LSB, &lsw);
273 if (rc) {
274 pr_err("fail to read CC_LSB rc = %d\n", rc);
275 return rc;
276 }
277 rc = pm_bms_read_output_data(chip, CC_MSB, &msw);
278 if (rc) {
279 pr_err("fail to read CC_MSB rc = %d\n", rc);
280 return rc;
281 }
282 *result = msw << 16 | lsw;
283 pr_debug("msw = %04x lsw = %04x cc = %d\n", msw, lsw, *result);
284 return 0;
285}
286
287static int read_last_good_ocv(struct pm8921_bms_chip *chip, uint *result)
288{
289 int rc;
290 uint16_t reading;
291
292 rc = pm_bms_read_output_data(chip, LAST_GOOD_OCV_VALUE, &reading);
293 if (rc) {
294 pr_err("fail to read LAST_GOOD_OCV_VALUE rc = %d\n", rc);
295 return rc;
296 }
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700297 *result = vbatt_to_microvolt(reading);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298 pr_debug("raw = %04x ocv_microV = %u\n", reading, *result);
299 return 0;
300}
301
302static int read_vbatt_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
303{
304 int rc;
305 uint16_t reading;
306
307 rc = pm_bms_read_output_data(chip, VBATT_FOR_RBATT, &reading);
308 if (rc) {
309 pr_err("fail to read VBATT_FOR_RBATT rc = %d\n", rc);
310 return rc;
311 }
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700312 *result = vbatt_to_microvolt(reading);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700313 pr_debug("raw = %04x vbatt_for_r_microV = %u\n", reading, *result);
314 return 0;
315}
316
317static int read_vsense_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
318{
319 int rc;
320 uint16_t reading;
321
322 rc = pm_bms_read_output_data(chip, VSENSE_FOR_RBATT, &reading);
323 if (rc) {
324 pr_err("fail to read VSENSE_FOR_RBATT rc = %d\n", rc);
325 return rc;
326 }
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700327 *result = cc_to_microvolt(chip, reading);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700328 pr_debug("raw = %04x vsense_for_r_microV = %u\n", reading, *result);
329 return 0;
330}
331
332static int read_ocv_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
333{
334 int rc;
335 uint16_t reading;
336
337 rc = pm_bms_read_output_data(chip, OCV_FOR_RBATT, &reading);
338 if (rc) {
339 pr_err("fail to read OCV_FOR_RBATT rc = %d\n", rc);
340 return rc;
341 }
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700342 *result = vbatt_to_microvolt(reading);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700343 pr_debug("read = %04x ocv_for_r_microV = %u\n", reading, *result);
344 return 0;
345}
346
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700347static int read_vsense_avg(struct pm8921_bms_chip *chip, int *result)
348{
349 int rc;
350 int16_t reading;
351
352 rc = pm_bms_read_output_data(chip, VSENSE_AVG, &reading);
353 if (rc) {
354 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
355 return rc;
356 }
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700357 *result = cc_to_microvolt(chip, reading);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700358 pr_debug("read = %04x vsense = %d\n", reading, *result);
359 return 0;
360}
361
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700362static int linear_interpolate(int y0, int x0, int y1, int x1, int x)
363{
364 if (y0 == y1 || x == x0)
365 return y0;
366 if (x1 == x0 || x == x1)
367 return y1;
368
369 return y0 + ((y1 - y0) * (x - x0) / (x1 - x0));
370}
371
372static int interpolate_single_lut(struct single_row_lut *lut, int x)
373{
374 int i, result;
375
376 if (x < lut->x[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700377 pr_debug("x %d less than known range return y = %d lut = %pS\n",
378 x, lut->y[0], lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700379 return lut->y[0];
380 }
381 if (x > lut->x[lut->cols - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700382 pr_debug("x %d more than known range return y = %d lut = %pS\n",
383 x, lut->y[lut->cols - 1], lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700384 return lut->y[lut->cols - 1];
385 }
386
387 for (i = 0; i < lut->cols; i++)
388 if (x <= lut->x[i])
389 break;
390 if (x == lut->x[i]) {
391 result = lut->y[i];
392 } else {
393 result = linear_interpolate(
394 lut->y[i - 1],
395 lut->x[i - 1],
396 lut->y[i],
397 lut->x[i],
398 x);
399 }
400 return result;
401}
402
403static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp)
404{
405 return interpolate_single_lut(chip->fcc_temp_lut, batt_temp);
406}
407
408static int interpolate_scalingfactor_fcc(struct pm8921_bms_chip *chip,
409 int cycles)
410{
411 return interpolate_single_lut(chip->fcc_sf_lut, cycles);
412}
413
414static int interpolate_scalingfactor_pc(struct pm8921_bms_chip *chip,
415 int cycles, int pc)
416{
417 int i, scalefactorrow1, scalefactorrow2, scalefactor, row1, row2;
418 int rows = chip->pc_sf_lut->rows;
419 int cols = chip->pc_sf_lut->cols;
420
421 if (pc > chip->pc_sf_lut->percent[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700422 pr_debug("pc %d greater than known pc ranges for sfd\n", pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423 row1 = 0;
424 row2 = 0;
425 }
426 if (pc < chip->pc_sf_lut->percent[rows - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700427 pr_debug("pc %d less than known pc ranges for sf", pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700428 row1 = rows - 1;
429 row2 = rows - 1;
430 }
431 for (i = 0; i < rows; i++) {
432 if (pc == chip->pc_sf_lut->percent[i]) {
433 row1 = i;
434 row2 = i;
435 break;
436 }
437 if (pc > chip->pc_sf_lut->percent[i]) {
438 row1 = i - 1;
439 row2 = i;
440 break;
441 }
442 }
443
444 if (cycles < chip->pc_sf_lut->cycles[0])
445 cycles = chip->pc_sf_lut->cycles[0];
446 if (cycles > chip->pc_sf_lut->cycles[cols - 1])
447 cycles = chip->pc_sf_lut->cycles[cols - 1];
448
449 for (i = 0; i < cols; i++)
450 if (cycles <= chip->pc_sf_lut->cycles[i])
451 break;
452 if (cycles == chip->pc_sf_lut->cycles[i]) {
453 scalefactor = linear_interpolate(
454 chip->pc_sf_lut->sf[row1][i],
455 chip->pc_sf_lut->percent[row1],
456 chip->pc_sf_lut->sf[row2][i],
457 chip->pc_sf_lut->percent[row2],
458 pc);
459 return scalefactor;
460 }
461
462 scalefactorrow1 = linear_interpolate(
463 chip->pc_sf_lut->sf[row1][i - 1],
464 chip->pc_sf_lut->cycles[i - 1],
465 chip->pc_sf_lut->sf[row1][i],
466 chip->pc_sf_lut->cycles[i],
467 cycles);
468
469 scalefactorrow2 = linear_interpolate(
470 chip->pc_sf_lut->sf[row2][i - 1],
471 chip->pc_sf_lut->cycles[i - 1],
472 chip->pc_sf_lut->sf[row2][i],
473 chip->pc_sf_lut->cycles[i],
474 cycles);
475
476 scalefactor = linear_interpolate(
477 scalefactorrow1,
478 chip->pc_sf_lut->percent[row1],
479 scalefactorrow2,
480 chip->pc_sf_lut->percent[row2],
481 pc);
482
483 return scalefactor;
484}
485
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700486static int is_between(int left, int right, int value)
487{
488 if (left >= right && left >= value && value >= right)
489 return 1;
490 if (left <= right && left <= value && value <= right)
491 return 1;
492
493 return 0;
494}
495
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700496static int interpolate_pc(struct pm8921_bms_chip *chip,
497 int batt_temp, int ocv)
498{
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700499 int i, j, pcj, pcj_minus_one, pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500 int rows = chip->pc_temp_ocv_lut->rows;
501 int cols = chip->pc_temp_ocv_lut->cols;
502
503 if (batt_temp < chip->pc_temp_ocv_lut->temp[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700504 pr_debug("batt_temp %d < known temp range for pc\n", batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505 batt_temp = chip->pc_temp_ocv_lut->temp[0];
506 }
507 if (batt_temp > chip->pc_temp_ocv_lut->temp[cols - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700508 pr_debug("batt_temp %d > known temp range for pc\n", batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 batt_temp = chip->pc_temp_ocv_lut->temp[cols - 1];
510 }
511
512 for (j = 0; j < cols; j++)
513 if (batt_temp <= chip->pc_temp_ocv_lut->temp[j])
514 break;
515 if (batt_temp == chip->pc_temp_ocv_lut->temp[j]) {
516 /* found an exact match for temp in the table */
517 if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j])
518 return chip->pc_temp_ocv_lut->percent[0];
519 if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j])
520 return chip->pc_temp_ocv_lut->percent[rows - 1];
521 for (i = 0; i < rows; i++) {
522 if (ocv >= chip->pc_temp_ocv_lut->ocv[i][j]) {
523 if (ocv == chip->pc_temp_ocv_lut->ocv[i][j])
524 return
525 chip->pc_temp_ocv_lut->percent[i];
526 pc = linear_interpolate(
527 chip->pc_temp_ocv_lut->percent[i],
528 chip->pc_temp_ocv_lut->ocv[i][j],
529 chip->pc_temp_ocv_lut->percent[i - 1],
530 chip->pc_temp_ocv_lut->ocv[i - 1][j],
531 ocv);
532 return pc;
533 }
534 }
535 }
536
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700537 /*
538 * batt_temp is within temperature for
539 * column j-1 and j
540 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541 if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j])
542 return chip->pc_temp_ocv_lut->percent[0];
543 if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j - 1])
544 return chip->pc_temp_ocv_lut->percent[rows - 1];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700546 pcj_minus_one = 0;
547 pcj = 0;
548 for (i = 0; i < rows-1; i++) {
549 if (pcj == 0
550 && is_between(chip->pc_temp_ocv_lut->ocv[i][j],
551 chip->pc_temp_ocv_lut->ocv[i+1][j], ocv)) {
552 pcj = linear_interpolate(
553 chip->pc_temp_ocv_lut->percent[i],
554 chip->pc_temp_ocv_lut->ocv[i][j],
555 chip->pc_temp_ocv_lut->percent[i + 1],
556 chip->pc_temp_ocv_lut->ocv[i+1][j],
557 ocv);
558 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700560 if (pcj_minus_one == 0
561 && is_between(chip->pc_temp_ocv_lut->ocv[i][j-1],
562 chip->pc_temp_ocv_lut->ocv[i+1][j-1], ocv)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700564 pcj_minus_one = linear_interpolate(
565 chip->pc_temp_ocv_lut->percent[i],
566 chip->pc_temp_ocv_lut->ocv[i][j-1],
567 chip->pc_temp_ocv_lut->percent[i + 1],
568 chip->pc_temp_ocv_lut->ocv[i+1][j-1],
569 ocv);
570 }
571
572 if (pcj && pcj_minus_one) {
573 pc = linear_interpolate(
574 pcj_minus_one,
575 chip->pc_temp_ocv_lut->temp[j-1],
576 pcj,
577 chip->pc_temp_ocv_lut->temp[j],
578 batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579 return pc;
580 }
581 }
582
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700583 if (pcj)
584 return pcj;
585
586 if (pcj_minus_one)
587 return pcj_minus_one;
588
589 pr_debug("%d ocv wasn't found for temp %d in the LUT returning 100%%",
590 ocv, batt_temp);
591 return 100;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592}
593
594static int calculate_rbatt(struct pm8921_bms_chip *chip)
595{
596 int rc;
597 unsigned int ocv, vsense, vbatt, r_batt;
598
599 rc = read_ocv_for_rbatt(chip, &ocv);
600 if (rc) {
601 pr_err("fail to read ocv_for_rbatt rc = %d\n", rc);
602 ocv = 0;
603 }
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700604 ocv = adjust_xo_reading(chip, ocv);
605
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700606 rc = read_vbatt_for_rbatt(chip, &vbatt);
607 if (rc) {
608 pr_err("fail to read vbatt_for_rbatt rc = %d\n", rc);
609 ocv = 0;
610 }
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700611 vbatt = adjust_xo_reading(chip, vbatt);
612
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700613 rc = read_vsense_for_rbatt(chip, &vsense);
614 if (rc) {
615 pr_err("fail to read vsense_for_rbatt rc = %d\n", rc);
616 ocv = 0;
617 }
618 if (ocv == 0
619 || ocv == vbatt
620 || vsense == 0) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700621 pr_debug("rbatt readings unavailable ocv = %d, vbatt = %d,"
622 "vsen = %d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623 ocv, vbatt, vsense);
624 return -EINVAL;
625 }
626 r_batt = ((ocv - vbatt) * chip->r_sense) / vsense;
627 pr_debug("r_batt = %umilliOhms", r_batt);
628 return r_batt;
629}
630
631static int calculate_fcc(struct pm8921_bms_chip *chip, int batt_temp,
632 int chargecycles)
633{
634 int initfcc, result, scalefactor = 0;
635
636 initfcc = interpolate_fcc(chip, batt_temp);
637 pr_debug("intfcc = %umAh batt_temp = %d\n", initfcc, batt_temp);
638
639 scalefactor = interpolate_scalingfactor_fcc(chip, chargecycles);
640 pr_debug("scalefactor = %d batt_temp = %d\n", scalefactor, batt_temp);
641
642 /* Multiply the initial FCC value by the scale factor. */
643 result = (initfcc * scalefactor) / 100;
644 pr_debug("fcc mAh = %d\n", result);
645 return result;
646}
647
648static int calculate_pc(struct pm8921_bms_chip *chip, int ocv_uv, int batt_temp,
649 int chargecycles)
650{
651 int pc, scalefactor;
652
653 pc = interpolate_pc(chip, batt_temp, ocv_uv / 1000);
654 pr_debug("pc = %u for ocv = %dmicroVolts batt_temp = %d\n",
655 pc, ocv_uv, batt_temp);
656
657 scalefactor = interpolate_scalingfactor_pc(chip, chargecycles, pc);
658 pr_debug("scalefactor = %u batt_temp = %d\n", scalefactor, batt_temp);
659
660 /* Multiply the initial FCC value by the scale factor. */
661 pc = (pc * scalefactor) / 100;
662 return pc;
663}
664
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700665static void calculate_cc_mah(struct pm8921_bms_chip *chip, int64_t *val,
Abhijeet Dharmapurikar52aa9682011-07-20 14:05:47 -0700666 int *coulumb_counter)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700667{
668 int rc;
669 int64_t cc_voltage_uv, cc_uvh, cc_mah;
670
671 rc = read_cc(the_chip, coulumb_counter);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700672 cc_voltage_uv = (int64_t)*coulumb_counter;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700673 cc_voltage_uv = cc_to_microvolt(chip, cc_voltage_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700674 pr_debug("cc_voltage_uv = %lld microvolts\n", cc_voltage_uv);
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700675 cc_uvh = ccmicrovolt_to_uvh(cc_voltage_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700676 pr_debug("cc_uvh = %lld micro_volt_hour\n", cc_uvh);
677 cc_mah = div_s64(cc_uvh, chip->r_sense);
678 *val = cc_mah;
679}
680
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700681static int get_battery_uvolts(struct pm8921_bms_chip *chip, int *uvolts)
682{
683 int rc;
684 struct pm8921_adc_chan_result result;
685
686 rc = pm8921_adc_read(chip->vbat_channel, &result);
687 if (rc) {
688 pr_err("error reading adc channel = %d, rc = %d\n",
689 chip->vbat_channel, rc);
690 return rc;
691 }
692 pr_debug("mvolts phy = %lld meas = 0x%llx", result.physical,
693 result.measurement);
694 *uvolts = (int)result.physical;
695 *uvolts = *uvolts * 1000;
696 return 0;
697}
698
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700699static int calculate_unusable_charge_mah(struct pm8921_bms_chip *chip,
700 int fcc, int batt_temp, int chargecycles)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700701{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700702 int rbatt, voltage_unusable_uv, pc_unusable;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703
704 rbatt = calculate_rbatt(chip);
705 if (rbatt < 0) {
706 rbatt = (last_rbatt < 0) ? DEFAULT_RBATT_MOHMS : last_rbatt;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700707 pr_debug("rbatt unavailable assuming %d\n", rbatt);
708 } else {
709 last_rbatt = rbatt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700711
712 /* calculate unusable charge */
713 voltage_unusable_uv = (rbatt * chip->i_test)
714 + (chip->v_failure * 1000);
715 pc_unusable = calculate_pc(chip, voltage_unusable_uv,
716 batt_temp, chargecycles);
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700717 pr_debug("rbatt = %umilliOhms unusable_v =%d unusable_pc = %d\n",
718 rbatt, voltage_unusable_uv, pc_unusable);
719 return (fcc * pc_unusable) / 100;
720}
Abhijeet Dharmapurikar52aa9682011-07-20 14:05:47 -0700721
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700722/* calculate remainging charge at the time of ocv */
723static int calculate_remaining_charge_mah(struct pm8921_bms_chip *chip, int fcc,
724 int batt_temp, int chargecycles)
725{
726 int rc, ocv, pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700727
728 /* calculate remainging charge */
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -0700729 ocv = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700730 rc = read_last_good_ocv(chip, &ocv);
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -0700731 if (rc)
732 pr_debug("failed to read ocv rc = %d\n", rc);
733
734 if (ocv == 0) {
735 ocv = last_ocv_uv;
736 pr_debug("ocv not available using last_ocv_uv=%d\n", ocv);
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700737 } else {
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -0700738 /* update the usespace param since a good ocv is available */
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700739 last_ocv_uv = ocv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740 }
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700741
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700742 pc = calculate_pc(chip, ocv, batt_temp, chargecycles);
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700743 pr_debug("ocv = %d pc = %d\n", ocv, pc);
744 return (fcc * pc) / 100;
745}
746
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700747static void calculate_charging_params(struct pm8921_bms_chip *chip,
748 int batt_temp, int chargecycles,
749 int *fcc,
750 int *unusable_charge,
751 int *remaining_charge,
752 int64_t *cc_mah)
753{
754 int coulumb_counter;
755
756 *fcc = calculate_fcc(chip, batt_temp, chargecycles);
757 pr_debug("FCC = %umAh batt_temp = %d, cycles = %d",
758 *fcc, batt_temp, chargecycles);
759
760 *unusable_charge = calculate_unusable_charge_mah(chip, *fcc,
761 batt_temp, chargecycles);
762
763 pr_debug("UUC = %umAh", *unusable_charge);
764
765 /* calculate remainging charge */
766 *remaining_charge = calculate_remaining_charge_mah(chip, *fcc,
767 batt_temp, chargecycles);
768 pr_debug("RC = %umAh\n", *remaining_charge);
769
770 /* calculate cc milli_volt_hour */
771 calculate_cc_mah(chip, cc_mah, &coulumb_counter);
772 pr_debug("cc_mah = %lldmAh cc = %d\n", *cc_mah, coulumb_counter);
773}
774
775static int calculate_real_fcc(struct pm8921_bms_chip *chip,
776 int batt_temp, int chargecycles)
777{
778 int fcc, unusable_charge;
779 int remaining_charge;
780 int64_t cc_mah;
781 int real_fcc;
782
783 calculate_charging_params(chip, batt_temp, chargecycles,
784 &fcc,
785 &unusable_charge,
786 &remaining_charge,
787 &cc_mah);
788
789 real_fcc = remaining_charge - cc_mah;
790
791 return real_fcc;
792}
793/*
794 * Remaining Usable Charge = remaining_charge (charge at ocv instance)
795 * - coloumb counter charge
796 * - unusable charge (due to battery resistance)
797 * SOC% = (remaining usable charge/ fcc - usable_charge);
798 */
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700799static int calculate_state_of_charge(struct pm8921_bms_chip *chip,
800 int batt_temp, int chargecycles)
801{
802 int remaining_usable_charge, fcc, unusable_charge;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700803 int remaining_charge, soc;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700804 int update_userspace = 1;
805 int64_t cc_mah;
806
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700807 calculate_charging_params(chip, batt_temp, chargecycles,
808 &fcc,
809 &unusable_charge,
810 &remaining_charge,
811 &cc_mah);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700812
813 /* calculate remaining usable charge */
814 remaining_usable_charge = remaining_charge - cc_mah - unusable_charge;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700815 pr_debug("RUC = %dmAh\n", remaining_usable_charge);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700816 if (remaining_usable_charge < 0) {
817 pr_err("bad rem_usb_chg cc_mah %lld, rem_chg %d unusb_chg %d\n",
818 cc_mah, remaining_charge, unusable_charge);
819 update_userspace = 0;
820 }
821
822 soc = (remaining_usable_charge * 100) / (fcc - unusable_charge);
823 if (soc > 100 || soc < 0) {
824 pr_err("bad soc rem_usb_chg %d fcc %d unusb_chg %d\n",
825 remaining_usable_charge, fcc, unusable_charge);
826 update_userspace = 0;
827 }
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700828 pr_debug("SOC = %u%%\n", soc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700829
830 if (update_userspace) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700831 last_soc = soc;
832 }
833 return soc;
834}
835
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700836#define XOADC_MAX_1P25V 1312500
837#define XOADC_MIN_1P25V 1187500
838#define XOADC_MAX_0P625V 656250
839#define XOADC_MIN_0P625V 593750
840
841#define HKADC_V_PER_BIT_MUL_FACTOR 977
842#define HKADC_V_PER_BIT_DIV_FACTOR 10
843static int calib_hkadc_convert_microvolt(unsigned int phy)
844{
845 return phy * HKADC_V_PER_BIT_MUL_FACTOR / HKADC_V_PER_BIT_DIV_FACTOR;
846}
847
848static void calib_hkadc(struct pm8921_bms_chip *chip)
849{
850 int voltage, rc;
851 struct pm8921_adc_chan_result result;
852
853 rc = pm8921_adc_read(the_chip->ref1p25v_channel, &result);
854 if (rc) {
855 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
856 return;
857 }
858 voltage = calib_hkadc_convert_microvolt(result.physical);
859
860 pr_debug("result 1.25v = 0x%llx, voltage = %dmV adc_meas = %lld\n",
861 result.physical, voltage, result.measurement);
862
863 /* check for valid range */
864 if (voltage > XOADC_MAX_1P25V)
865 voltage = XOADC_MAX_1P25V;
866 else if (voltage < XOADC_MIN_1P25V)
867 voltage = XOADC_MIN_1P25V;
868 chip->xoadc_v125 = voltage;
869
870 rc = pm8921_adc_read(the_chip->ref625mv_channel, &result);
871 if (rc) {
872 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
873 return;
874 }
875 voltage = calib_hkadc_convert_microvolt(result.physical);
876 pr_debug("result 0.625V = 0x%llx, voltage = %dmV adc_mead = %lld\n",
877 result.physical, voltage, result.measurement);
878 /* check for valid range */
879 if (voltage > XOADC_MAX_0P625V)
880 voltage = XOADC_MAX_0P625V;
881 else if (voltage < XOADC_MIN_0P625V)
882 voltage = XOADC_MIN_0P625V;
883
884 chip->xoadc_v0625 = voltage;
885}
886
887static void calibrate_hkadc_work(struct work_struct *work)
888{
889 struct pm8921_bms_chip *chip = container_of(work,
890 struct pm8921_bms_chip, calib_hkadc_work);
891
892 calib_hkadc(chip);
893}
894
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700895int pm8921_bms_get_vsense_avg(int *result)
896{
897 if (the_chip)
898 return read_vsense_avg(the_chip, result);
899
900 pr_err("called before initialization\n");
901 return -EINVAL;
902}
903EXPORT_SYMBOL(pm8921_bms_get_vsense_avg);
904
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -0700905int pm8921_bms_get_battery_current(int *result)
906{
907 if (!the_chip) {
908 pr_err("called before initialization\n");
909 return -EINVAL;
910 }
911 if (the_chip->r_sense == 0) {
912 pr_err("r_sense is zero\n");
913 return -EINVAL;
914 }
915
916 read_vsense_avg(the_chip, result);
917 pr_debug("vsense=%d\n", *result);
Abhijeet Dharmapurikara7a1c6b2011-08-17 10:04:58 -0700918 /* cast for signed division */
919 *result = *result / (int)the_chip->r_sense;
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -0700920 return 0;
921}
922EXPORT_SYMBOL(pm8921_bms_get_battery_current);
923
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700924int pm8921_bms_get_percent_charge(void)
925{
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700926 int batt_temp, rc;
927 struct pm8921_adc_chan_result result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700928
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700929 if (!the_chip) {
930 pr_err("called before initialization\n");
931 return -EINVAL;
932 }
933
934 rc = pm8921_adc_read(the_chip->batt_temp_channel, &result);
935 if (rc) {
936 pr_err("error reading adc channel = %d, rc = %d\n",
937 the_chip->batt_temp_channel, rc);
938 return rc;
939 }
940 pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical,
941 result.measurement);
942 batt_temp = (int)result.physical;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700943 return calculate_state_of_charge(the_chip,
944 batt_temp, last_chargecycles);
945}
946EXPORT_SYMBOL_GPL(pm8921_bms_get_percent_charge);
947
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -0700948int pm8921_bms_get_fcc(void)
949{
950 int batt_temp, rc;
951 struct pm8921_adc_chan_result result;
952
953 if (!the_chip) {
954 pr_err("called before initialization\n");
955 return -EINVAL;
956 }
957
958 rc = pm8921_adc_read(the_chip->batt_temp_channel, &result);
959 if (rc) {
960 pr_err("error reading adc channel = %d, rc = %d\n",
961 the_chip->batt_temp_channel, rc);
962 return rc;
963 }
964 pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical,
965 result.measurement);
966 batt_temp = (int)result.physical;
967 return calculate_fcc(the_chip, batt_temp, last_chargecycles);
968}
969EXPORT_SYMBOL_GPL(pm8921_bms_get_fcc);
970
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700971static int start_percent;
972static int end_percent;
973void pm8921_bms_charging_began(void)
974{
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700975 start_percent = pm8921_bms_get_percent_charge();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700976 pr_debug("start_percent = %u%%\n", start_percent);
977}
978EXPORT_SYMBOL_GPL(pm8921_bms_charging_began);
979
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700980void pm8921_bms_charging_end(int is_battery_full)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700981{
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700982 if (is_battery_full && the_chip != NULL) {
983 int batt_temp, rc;
984 struct pm8921_adc_chan_result result;
985
986 rc = pm8921_adc_read(the_chip->batt_temp_channel, &result);
987 if (rc) {
988 pr_err("error reading adc channel = %d, rc = %d\n",
989 the_chip->batt_temp_channel, rc);
990 goto charge_cycle_calculation;
991 }
992 pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical,
993 result.measurement);
994 batt_temp = (int)result.physical;
995 last_real_fcc = calculate_real_fcc(the_chip,
996 batt_temp, last_chargecycles);
997 }
998
999charge_cycle_calculation:
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001000 end_percent = pm8921_bms_get_percent_charge();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001001 if (end_percent > start_percent) {
1002 last_charge_increase = end_percent - start_percent;
1003 if (last_charge_increase > 100) {
1004 last_chargecycles++;
1005 last_charge_increase = last_charge_increase % 100;
1006 }
1007 }
1008 pr_debug("end_percent = %u%% last_charge_increase = %d"
1009 "last_chargecycles = %d\n",
1010 end_percent,
1011 last_charge_increase,
1012 last_chargecycles);
1013}
1014EXPORT_SYMBOL_GPL(pm8921_bms_charging_end);
1015
1016static irqreturn_t pm8921_bms_sbi_write_ok_handler(int irq, void *data)
1017{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001018 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001019 return IRQ_HANDLED;
1020}
1021
1022static irqreturn_t pm8921_bms_cc_thr_handler(int irq, void *data)
1023{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001024 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001025 return IRQ_HANDLED;
1026}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001027
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001028static irqreturn_t pm8921_bms_vsense_thr_handler(int irq, void *data)
1029{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001030 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001031 return IRQ_HANDLED;
1032}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001033
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001034static irqreturn_t pm8921_bms_vsense_for_r_handler(int irq, void *data)
1035{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001036 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001037 return IRQ_HANDLED;
1038}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001039
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001040static irqreturn_t pm8921_bms_ocv_for_r_handler(int irq, void *data)
1041{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001042 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001043
1044 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001045 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001046 return IRQ_HANDLED;
1047}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001048
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001049static irqreturn_t pm8921_bms_good_ocv_handler(int irq, void *data)
1050{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001051 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001052
1053 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001054 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001055 return IRQ_HANDLED;
1056}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001057
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001058static irqreturn_t pm8921_bms_vsense_avg_handler(int irq, void *data)
1059{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001060 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001061 return IRQ_HANDLED;
1062}
1063
1064struct pm_bms_irq_init_data {
1065 unsigned int irq_id;
1066 char *name;
1067 unsigned long flags;
1068 irqreturn_t (*handler)(int, void *);
1069};
1070
1071#define BMS_IRQ(_id, _flags, _handler) \
1072{ \
1073 .irq_id = _id, \
1074 .name = #_id, \
1075 .flags = _flags, \
1076 .handler = _handler, \
1077}
1078
1079struct pm_bms_irq_init_data bms_irq_data[] = {
1080 BMS_IRQ(PM8921_BMS_SBI_WRITE_OK, IRQF_TRIGGER_RISING,
1081 pm8921_bms_sbi_write_ok_handler),
1082 BMS_IRQ(PM8921_BMS_CC_THR, IRQF_TRIGGER_RISING,
1083 pm8921_bms_cc_thr_handler),
1084 BMS_IRQ(PM8921_BMS_VSENSE_THR, IRQF_TRIGGER_RISING,
1085 pm8921_bms_vsense_thr_handler),
1086 BMS_IRQ(PM8921_BMS_VSENSE_FOR_R, IRQF_TRIGGER_RISING,
1087 pm8921_bms_vsense_for_r_handler),
1088 BMS_IRQ(PM8921_BMS_OCV_FOR_R, IRQF_TRIGGER_RISING,
1089 pm8921_bms_ocv_for_r_handler),
1090 BMS_IRQ(PM8921_BMS_GOOD_OCV, IRQF_TRIGGER_RISING,
1091 pm8921_bms_good_ocv_handler),
1092 BMS_IRQ(PM8921_BMS_VSENSE_AVG, IRQF_TRIGGER_RISING,
1093 pm8921_bms_vsense_avg_handler),
1094};
1095
1096static void free_irqs(struct pm8921_bms_chip *chip)
1097{
1098 int i;
1099
1100 for (i = 0; i < PM_BMS_MAX_INTS; i++)
1101 if (chip->pmic_bms_irq[i]) {
1102 free_irq(chip->pmic_bms_irq[i], NULL);
1103 chip->pmic_bms_irq[i] = 0;
1104 }
1105}
1106
1107static int __devinit request_irqs(struct pm8921_bms_chip *chip,
1108 struct platform_device *pdev)
1109{
1110 struct resource *res;
1111 int ret, i;
1112
1113 ret = 0;
1114 bitmap_fill(chip->enabled_irqs, PM_BMS_MAX_INTS);
1115
1116 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
1117 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1118 bms_irq_data[i].name);
1119 if (res == NULL) {
1120 pr_err("couldn't find %s\n", bms_irq_data[i].name);
1121 goto err_out;
1122 }
1123 ret = request_irq(res->start, bms_irq_data[i].handler,
1124 bms_irq_data[i].flags,
1125 bms_irq_data[i].name, chip);
1126 if (ret < 0) {
1127 pr_err("couldn't request %d (%s) %d\n", res->start,
1128 bms_irq_data[i].name, ret);
1129 goto err_out;
1130 }
1131 chip->pmic_bms_irq[bms_irq_data[i].irq_id] = res->start;
1132 pm8921_bms_disable_irq(chip, bms_irq_data[i].irq_id);
1133 }
1134 return 0;
1135
1136err_out:
1137 free_irqs(chip);
1138 return -EINVAL;
1139}
1140
1141#define EN_BMS_BIT BIT(7)
1142#define EN_PON_HS_BIT BIT(0)
1143static int __devinit pm8921_bms_hw_init(struct pm8921_bms_chip *chip)
1144{
1145 int rc;
1146
1147 rc = pm_bms_masked_write(chip, BMS_CONTROL,
1148 EN_BMS_BIT | EN_PON_HS_BIT, EN_BMS_BIT | EN_PON_HS_BIT);
1149 if (rc) {
1150 pr_err("failed to enable pon and bms addr = %d %d",
1151 BMS_CONTROL, rc);
1152 }
1153
1154 return 0;
1155}
1156
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001157static void check_initial_ocv(struct pm8921_bms_chip *chip)
1158{
1159 int ocv, vbatt, rbatt, ibatt, rc;
1160
1161 /*
1162 * Check if a last_good_ocv is available,
1163 * if not compute it here at boot time.
1164 */
1165 rc = read_last_good_ocv(chip, &ocv);
1166 if (rc || ocv == 0) {
1167 rc = get_battery_uvolts(chip, &vbatt);
1168 if (rc) {
1169 pr_err("failed to read vbatt from adc rc = %d\n", rc);
1170 last_ocv_uv = DEFAULT_OCV_MICROVOLTS;
1171 return;
1172 }
1173
1174 rc = pm8921_bms_get_battery_current(&ibatt);
1175 if (rc) {
1176 pr_err("failed to read batt current rc = %d\n", rc);
1177 last_ocv_uv = DEFAULT_OCV_MICROVOLTS;
1178 return;
1179 }
1180
1181 rbatt = calculate_rbatt(the_chip);
1182 if (rbatt < 0)
1183 rbatt = DEFAULT_RBATT_MOHMS;
1184 last_ocv_uv = vbatt + ibatt * rbatt;
1185 }
1186 pr_debug("ocv = %d last_ocv_uv = %d\n", ocv, last_ocv_uv);
1187}
1188
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001189static int64_t read_battery_id(struct pm8921_bms_chip *chip)
1190{
1191 int rc;
1192 struct pm8921_adc_chan_result result;
1193
1194 rc = pm8921_adc_read(chip->batt_id_channel, &result);
1195 if (rc) {
1196 pr_err("error reading batt id channel = %d, rc = %d\n",
1197 chip->vbat_channel, rc);
1198 return rc;
1199 }
1200 pr_debug("batt_id phy = %lld meas = 0x%llx\n", result.physical,
1201 result.measurement);
1202 return result.physical;
1203}
1204
1205#define PALLADIUM_ID_MIN 2500
1206#define PALLADIUM_ID_MAX 4000
1207static int set_battery_data(struct pm8921_bms_chip *chip)
1208{
1209 int64_t battery_id;
1210
1211 battery_id = read_battery_id(chip);
1212
1213 if (battery_id < 0) {
1214 pr_err("cannot read battery id err = %lld\n", battery_id);
1215 return battery_id;
1216 }
1217
1218 if (is_between(PALLADIUM_ID_MIN, PALLADIUM_ID_MAX, battery_id)) {
1219 chip->fcc = palladium_1500_data.fcc;
1220 chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut;
1221 chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut;
1222 chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut;
1223 chip->pc_sf_lut = palladium_1500_data.pc_sf_lut;
1224 return 0;
1225 } else {
1226 pr_warn("invalid battery id, palladium 1500 assumed\n");
1227 chip->fcc = palladium_1500_data.fcc;
1228 chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut;
1229 chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut;
1230 chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut;
1231 chip->pc_sf_lut = palladium_1500_data.pc_sf_lut;
1232 return 0;
1233 }
1234}
1235
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001236enum {
1237 CALC_RBATT,
1238 CALC_FCC,
1239 CALC_PC,
1240 CALC_SOC,
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001241 CALIB_HKADC,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001242};
1243
1244static int test_batt_temp = 5;
1245static int test_chargecycle = 150;
1246static int test_ocv = 3900000;
1247enum {
1248 TEST_BATT_TEMP,
1249 TEST_CHARGE_CYCLE,
1250 TEST_OCV,
1251};
1252static int get_test_param(void *data, u64 * val)
1253{
1254 switch ((int)data) {
1255 case TEST_BATT_TEMP:
1256 *val = test_batt_temp;
1257 break;
1258 case TEST_CHARGE_CYCLE:
1259 *val = test_chargecycle;
1260 break;
1261 case TEST_OCV:
1262 *val = test_ocv;
1263 break;
1264 default:
1265 return -EINVAL;
1266 }
1267 return 0;
1268}
1269static int set_test_param(void *data, u64 val)
1270{
1271 switch ((int)data) {
1272 case TEST_BATT_TEMP:
1273 test_batt_temp = (int)val;
1274 break;
1275 case TEST_CHARGE_CYCLE:
1276 test_chargecycle = (int)val;
1277 break;
1278 case TEST_OCV:
1279 test_ocv = (int)val;
1280 break;
1281 default:
1282 return -EINVAL;
1283 }
1284 return 0;
1285}
1286DEFINE_SIMPLE_ATTRIBUTE(temp_fops, get_test_param, set_test_param, "%llu\n");
1287
1288static int get_calc(void *data, u64 * val)
1289{
1290 int param = (int)data;
1291 int ret = 0;
1292
1293 *val = 0;
1294
1295 /* global irq number passed in via data */
1296 switch (param) {
1297 case CALC_RBATT:
1298 *val = calculate_rbatt(the_chip);
1299 break;
1300 case CALC_FCC:
1301 *val = calculate_fcc(the_chip, test_batt_temp,
1302 test_chargecycle);
1303 break;
1304 case CALC_PC:
1305 *val = calculate_pc(the_chip, test_ocv, test_batt_temp,
1306 test_chargecycle);
1307 break;
1308 case CALC_SOC:
1309 *val = calculate_state_of_charge(the_chip,
1310 test_batt_temp, test_chargecycle);
1311 break;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001312 case CALIB_HKADC:
1313 /* reading this will trigger calibration */
1314 *val = 0;
1315 calib_hkadc(the_chip);
1316 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001317 default:
1318 ret = -EINVAL;
1319 }
1320 return ret;
1321}
1322DEFINE_SIMPLE_ATTRIBUTE(calc_fops, get_calc, NULL, "%llu\n");
1323
1324static int get_reading(void *data, u64 * val)
1325{
1326 int param = (int)data;
1327 int ret = 0;
1328
1329 *val = 0;
1330
1331 /* global irq number passed in via data */
1332 switch (param) {
1333 case CC_MSB:
1334 case CC_LSB:
1335 read_cc(the_chip, (int *)val);
1336 break;
1337 case LAST_GOOD_OCV_VALUE:
1338 read_last_good_ocv(the_chip, (uint *)val);
1339 break;
1340 case VBATT_FOR_RBATT:
1341 read_vbatt_for_rbatt(the_chip, (uint *)val);
1342 break;
1343 case VSENSE_FOR_RBATT:
1344 read_vsense_for_rbatt(the_chip, (uint *)val);
1345 break;
1346 case OCV_FOR_RBATT:
1347 read_ocv_for_rbatt(the_chip, (uint *)val);
1348 break;
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001349 case VSENSE_AVG:
1350 read_vsense_avg(the_chip, (uint *)val);
1351 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001352 default:
1353 ret = -EINVAL;
1354 }
1355 return ret;
1356}
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001357DEFINE_SIMPLE_ATTRIBUTE(reading_fops, get_reading, NULL, "%lld\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001358
1359static int get_rt_status(void *data, u64 * val)
1360{
1361 int i = (int)data;
1362 int ret;
1363
1364 /* global irq number passed in via data */
1365 ret = pm_bms_get_rt_status(the_chip, i);
1366 *val = ret;
1367 return 0;
1368}
1369DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n");
1370
1371static int get_reg(void *data, u64 * val)
1372{
1373 int addr = (int)data;
1374 int ret;
1375 u8 temp;
1376
1377 ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp);
1378 if (ret) {
1379 pr_err("pm8xxx_readb to %x value = %d errored = %d\n",
1380 addr, temp, ret);
1381 return -EAGAIN;
1382 }
1383 *val = temp;
1384 return 0;
1385}
1386
1387static int set_reg(void *data, u64 val)
1388{
1389 int addr = (int)data;
1390 int ret;
1391 u8 temp;
1392
1393 temp = (u8) val;
1394 ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp);
1395 if (ret) {
1396 pr_err("pm8xxx_writeb to %x value = %d errored = %d\n",
1397 addr, temp, ret);
1398 return -EAGAIN;
1399 }
1400 return 0;
1401}
1402DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
1403
1404static void create_debugfs_entries(struct pm8921_bms_chip *chip)
1405{
1406 int i;
1407
1408 chip->dent = debugfs_create_dir("pm8921_bms", NULL);
1409
1410 if (IS_ERR(chip->dent)) {
1411 pr_err("pmic bms couldnt create debugfs dir\n");
1412 return;
1413 }
1414
1415 debugfs_create_file("BMS_CONTROL", 0644, chip->dent,
1416 (void *)BMS_CONTROL, &reg_fops);
1417 debugfs_create_file("BMS_OUTPUT0", 0644, chip->dent,
1418 (void *)BMS_OUTPUT0, &reg_fops);
1419 debugfs_create_file("BMS_OUTPUT1", 0644, chip->dent,
1420 (void *)BMS_OUTPUT1, &reg_fops);
1421 debugfs_create_file("BMS_TEST1", 0644, chip->dent,
1422 (void *)BMS_TEST1, &reg_fops);
1423 debugfs_create_file("CCADC_ANA_PARAM", 0644, chip->dent,
1424 (void *)CCADC_ANA_PARAM, &reg_fops);
1425 debugfs_create_file("CCADC_DIG_PARAM", 0644, chip->dent,
1426 (void *)CCADC_DIG_PARAM, &reg_fops);
1427 debugfs_create_file("CCADC_RSV", 0644, chip->dent,
1428 (void *)CCADC_RSV, &reg_fops);
1429 debugfs_create_file("CCADC_DATA0", 0644, chip->dent,
1430 (void *)CCADC_DATA0, &reg_fops);
1431 debugfs_create_file("CCADC_DATA1", 0644, chip->dent,
1432 (void *)CCADC_DATA1, &reg_fops);
1433 debugfs_create_file("CCADC_OFFSET_TRIM1", 0644, chip->dent,
1434 (void *)CCADC_OFFSET_TRIM1, &reg_fops);
1435 debugfs_create_file("CCADC_OFFSET_TRIM0", 0644, chip->dent,
1436 (void *)CCADC_OFFSET_TRIM0, &reg_fops);
1437
1438 debugfs_create_file("test_batt_temp", 0644, chip->dent,
1439 (void *)TEST_BATT_TEMP, &temp_fops);
1440 debugfs_create_file("test_chargecycle", 0644, chip->dent,
1441 (void *)TEST_CHARGE_CYCLE, &temp_fops);
1442 debugfs_create_file("test_ocv", 0644, chip->dent,
1443 (void *)TEST_OCV, &temp_fops);
1444
1445 debugfs_create_file("read_cc", 0644, chip->dent,
1446 (void *)CC_MSB, &reading_fops);
1447 debugfs_create_file("read_last_good_ocv", 0644, chip->dent,
1448 (void *)LAST_GOOD_OCV_VALUE, &reading_fops);
1449 debugfs_create_file("read_vbatt_for_rbatt", 0644, chip->dent,
1450 (void *)VBATT_FOR_RBATT, &reading_fops);
1451 debugfs_create_file("read_vsense_for_rbatt", 0644, chip->dent,
1452 (void *)VSENSE_FOR_RBATT, &reading_fops);
1453 debugfs_create_file("read_ocv_for_rbatt", 0644, chip->dent,
1454 (void *)OCV_FOR_RBATT, &reading_fops);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001455 debugfs_create_file("read_vsense_avg", 0644, chip->dent,
1456 (void *)VSENSE_AVG, &reading_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001457
1458 debugfs_create_file("show_rbatt", 0644, chip->dent,
1459 (void *)CALC_RBATT, &calc_fops);
1460 debugfs_create_file("show_fcc", 0644, chip->dent,
1461 (void *)CALC_FCC, &calc_fops);
1462 debugfs_create_file("show_pc", 0644, chip->dent,
1463 (void *)CALC_PC, &calc_fops);
1464 debugfs_create_file("show_soc", 0644, chip->dent,
1465 (void *)CALC_SOC, &calc_fops);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001466 debugfs_create_file("calib_hkadc", 0644, chip->dent,
1467 (void *)CALIB_HKADC, &calc_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001468
1469 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
1470 if (chip->pmic_bms_irq[bms_irq_data[i].irq_id])
1471 debugfs_create_file(bms_irq_data[i].name, 0444,
1472 chip->dent,
1473 (void *)bms_irq_data[i].irq_id,
1474 &rt_fops);
1475 }
1476}
1477
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001478static int __devinit pm8921_bms_probe(struct platform_device *pdev)
1479{
1480 int rc = 0;
1481 struct pm8921_bms_chip *chip;
1482 const struct pm8921_bms_platform_data *pdata
1483 = pdev->dev.platform_data;
1484 if (!pdata) {
1485 pr_err("missing platform data\n");
1486 return -EINVAL;
1487 }
1488
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001489 chip = kzalloc(sizeof(struct pm8921_bms_chip), GFP_KERNEL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001490 if (!chip) {
1491 pr_err("Cannot allocate pm_bms_chip\n");
1492 return -ENOMEM;
1493 }
1494
1495 chip->dev = &pdev->dev;
1496 chip->r_sense = pdata->r_sense;
1497 chip->i_test = pdata->i_test;
1498 chip->v_failure = pdata->v_failure;
1499 chip->calib_delay_ms = pdata->calib_delay_ms;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001500 rc = set_battery_data(chip);
1501 if (rc) {
1502 pr_err("%s bad battery data %d\n", __func__, rc);
1503 return rc;
1504 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001505
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001506 chip->batt_temp_channel = pdata->bms_cdata.batt_temp_channel;
1507 chip->vbat_channel = pdata->bms_cdata.vbat_channel;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001508 chip->ref625mv_channel = pdata->bms_cdata.ref625mv_channel;
1509 chip->ref1p25v_channel = pdata->bms_cdata.ref1p25v_channel;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001510 chip->batt_id_channel = pdata->bms_cdata.batt_id_channel;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -07001511 chip->revision = pm8xxx_get_revision(chip->dev->parent);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001512 INIT_WORK(&chip->calib_hkadc_work, calibrate_hkadc_work);
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001513
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001514 rc = pm8921_bms_hw_init(chip);
1515 if (rc) {
1516 pr_err("couldn't init hardware rc = %d\n", rc);
1517 goto free_chip;
1518 }
1519
1520 rc = request_irqs(chip, pdev);
1521 if (rc) {
1522 pr_err("couldn't register interrupts rc = %d\n", rc);
1523 goto free_chip;
1524 }
1525
1526 platform_set_drvdata(pdev, chip);
1527 the_chip = chip;
1528 create_debugfs_entries(chip);
1529
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001530 check_initial_ocv(chip);
1531
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001532 /* enable the vbatt reading interrupts for scheduling hkadc calib */
1533 pm8921_bms_enable_irq(chip, PM8921_BMS_GOOD_OCV);
1534 pm8921_bms_enable_irq(chip, PM8921_BMS_OCV_FOR_R);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001535 return 0;
1536
1537free_chip:
1538 kfree(chip);
1539 return rc;
1540}
1541
1542static int __devexit pm8921_bms_remove(struct platform_device *pdev)
1543{
1544 struct pm8921_bms_chip *chip = platform_get_drvdata(pdev);
1545
1546 free_irqs(chip);
1547 platform_set_drvdata(pdev, NULL);
1548 the_chip = NULL;
1549 kfree(chip);
1550 return 0;
1551}
1552
1553static struct platform_driver pm8921_bms_driver = {
1554 .probe = pm8921_bms_probe,
1555 .remove = __devexit_p(pm8921_bms_remove),
1556 .driver = {
1557 .name = PM8921_BMS_DEV_NAME,
1558 .owner = THIS_MODULE,
1559 },
1560};
1561
1562static int __init pm8921_bms_init(void)
1563{
1564 return platform_driver_register(&pm8921_bms_driver);
1565}
1566
1567static void __exit pm8921_bms_exit(void)
1568{
1569 platform_driver_unregister(&pm8921_bms_driver);
1570}
1571
1572late_initcall(pm8921_bms_init);
1573module_exit(pm8921_bms_exit);
1574
1575MODULE_LICENSE("GPL v2");
1576MODULE_DESCRIPTION("PMIC8921 bms driver");
1577MODULE_VERSION("1.0");
1578MODULE_ALIAS("platform:" PM8921_BMS_DEV_NAME);