blob: 676b863614b5a4a31dcd2923c02bd2fa75512da1 [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>
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -070021#include <linux/mfd/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
39#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
45
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;
78 struct delayed_work calib_work;
79 unsigned int calib_delay_ms;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -070080 unsigned int revision;
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -070081 unsigned int batt_temp_channel;
82 unsigned int vbat_channel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070083 unsigned int pmic_bms_irq[PM_BMS_MAX_INTS];
84 DECLARE_BITMAP(enabled_irqs, PM_BMS_MAX_INTS);
85};
86
87static struct pm8921_bms_chip *the_chip;
88
89#define DEFAULT_RBATT_MOHMS 128
90#define DEFAULT_UNUSABLE_CHARGE_MAH 10
91#define DEFAULT_OCV_MICROVOLTS 3900000
92#define DEFAULT_REMAINING_CHARGE_MAH 990
93#define DEFAULT_COULUMB_COUNTER 0
94#define DEFAULT_CHARGE_CYCLES 0
95
96static int last_rbatt = -EINVAL;
97static int last_fcc = -EINVAL;
98static int last_unusable_charge = -EINVAL;
99static int last_ocv_uv = -EINVAL;
100static int last_remaining_charge = -EINVAL;
101static int last_coulumb_counter = -EINVAL;
102static int last_soc = -EINVAL;
103
104static int last_chargecycles = DEFAULT_CHARGE_CYCLES;
105static int last_charge_increase;
106
107module_param(last_rbatt, int, 0644);
108module_param(last_fcc, int, 0644);
109module_param(last_unusable_charge, int, 0644);
110module_param(last_ocv_uv, int, 0644);
111module_param(last_remaining_charge, int, 0644);
112module_param(last_coulumb_counter, int, 0644);
113module_param(last_chargecycles, int, 0644);
114module_param(last_charge_increase, int, 0644);
115
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
122static void pm8921_bms_disable_irq(struct pm8921_bms_chip *chip, int interrupt)
123{
124 if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700125 pr_debug("%d\n", chip->pmic_bms_irq[interrupt]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700126 disable_irq_nosync(chip->pmic_bms_irq[interrupt]);
127 }
128}
129
130static int pm_bms_masked_write(struct pm8921_bms_chip *chip, u16 addr,
131 u8 mask, u8 val)
132{
133 int rc;
134 u8 reg;
135
136 rc = pm8xxx_readb(chip->dev->parent, addr, &reg);
137 if (rc) {
138 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
139 return rc;
140 }
141 reg &= ~mask;
142 reg |= val & mask;
143 rc = pm8xxx_writeb(chip->dev->parent, addr, reg);
144 if (rc) {
145 pr_err("write failed addr = %03X, rc = %d\n", addr, rc);
146 return rc;
147 }
148 return 0;
149}
150
151#define SELECT_OUTPUT_DATA 0x1C
152#define SELECT_OUTPUT_TYPE_SHIFT 2
153#define OCV_FOR_RBATT 0x0
154#define VSENSE_FOR_RBATT 0x1
155#define VBATT_FOR_RBATT 0x2
156#define CC_MSB 0x3
157#define CC_LSB 0x4
158#define LAST_GOOD_OCV_VALUE 0x5
159#define VSENSE_AVG 0x6
160#define VBATT_AVG 0x7
161
162static int pm_bms_read_output_data(struct pm8921_bms_chip *chip, int type,
163 int16_t *result)
164{
165 int rc;
166 u8 reg;
167
168 if (!result) {
169 pr_err("result pointer null\n");
170 return -EINVAL;
171 }
172 *result = 0;
173 if (type < OCV_FOR_RBATT || type > VBATT_AVG) {
174 pr_err("invalid type %d asked to read\n", type);
175 return -EINVAL;
176 }
177 rc = pm_bms_masked_write(chip, BMS_CONTROL, SELECT_OUTPUT_DATA,
178 type << SELECT_OUTPUT_TYPE_SHIFT);
179 if (rc) {
180 pr_err("fail to select %d type in BMS_CONTROL rc = %d\n",
181 type, rc);
182 return rc;
183 }
184
185 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT0, &reg);
186 if (rc) {
187 pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n",
188 type, rc);
189 return rc;
190 }
191 *result = reg;
192 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT1, &reg);
193 if (rc) {
194 pr_err("fail to read BMS_OUTPUT1 for type %d rc = %d\n",
195 type, rc);
196 return rc;
197 }
198 *result |= reg << 8;
199 pr_debug("type %d result %x", type, *result);
200 return 0;
201}
202
203#define V_PER_BIT_MUL_FACTOR 977
204#define V_PER_BIT_DIV_FACTOR 10
205#define CONV_READING(a) (((a) * (int)V_PER_BIT_MUL_FACTOR)\
206 /V_PER_BIT_DIV_FACTOR)
207
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700208#define CC_RESOLUTION_N_V1 1085069
209#define CC_RESOLUTION_D_V1 100000
210#define CC_RESOLUTION_N_V2 868056
211#define CC_RESOLUTION_D_V2 10000
212static s64 cc_to_microvolt_v1(s64 cc)
213{
214 return div_s64(cc * CC_RESOLUTION_N_V1, CC_RESOLUTION_D_V1);
215}
216
217static s64 cc_to_microvolt_v2(s64 cc)
218{
219 return div_s64(cc * CC_RESOLUTION_N_V2, CC_RESOLUTION_D_V2);
220}
221
222static s64 cc_to_microvolt(struct pm8921_bms_chip *chip, s64 cc)
223{
224 /*
225 * resolution (the value of a single bit) was changed after revision 2.0
226 * for more accurate readings
227 */
228 return (chip->revision < PM8XXX_REVISION_8901_2p0) ?
229 cc_to_microvolt_v1((s64)cc) :
230 cc_to_microvolt_v2((s64)cc);
231}
232
233#define CC_READING_TICKS 55
234#define SLEEP_CLK_HZ 32768
235#define SECONDS_PER_HOUR 3600
236static s64 ccmicrovolt_to_uvh(s64 cc_uv)
237{
238 return div_s64(cc_uv * CC_READING_TICKS,
239 SLEEP_CLK_HZ * SECONDS_PER_HOUR);
240}
241
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700242/* returns the signed value read from the hardware */
243static int read_cc(struct pm8921_bms_chip *chip, int *result)
244{
245 int rc;
246 uint16_t msw, lsw;
247
248 rc = pm_bms_read_output_data(chip, CC_LSB, &lsw);
249 if (rc) {
250 pr_err("fail to read CC_LSB rc = %d\n", rc);
251 return rc;
252 }
253 rc = pm_bms_read_output_data(chip, CC_MSB, &msw);
254 if (rc) {
255 pr_err("fail to read CC_MSB rc = %d\n", rc);
256 return rc;
257 }
258 *result = msw << 16 | lsw;
259 pr_debug("msw = %04x lsw = %04x cc = %d\n", msw, lsw, *result);
260 return 0;
261}
262
263static int read_last_good_ocv(struct pm8921_bms_chip *chip, uint *result)
264{
265 int rc;
266 uint16_t reading;
267
268 rc = pm_bms_read_output_data(chip, LAST_GOOD_OCV_VALUE, &reading);
269 if (rc) {
270 pr_err("fail to read LAST_GOOD_OCV_VALUE rc = %d\n", rc);
271 return rc;
272 }
273 *result = CONV_READING(reading);
274 pr_debug("raw = %04x ocv_microV = %u\n", reading, *result);
275 return 0;
276}
277
278static int read_vbatt_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
279{
280 int rc;
281 uint16_t reading;
282
283 rc = pm_bms_read_output_data(chip, VBATT_FOR_RBATT, &reading);
284 if (rc) {
285 pr_err("fail to read VBATT_FOR_RBATT rc = %d\n", rc);
286 return rc;
287 }
288 *result = CONV_READING(reading);
289 pr_debug("raw = %04x vbatt_for_r_microV = %u\n", reading, *result);
290 return 0;
291}
292
293static int read_vsense_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
294{
295 int rc;
296 uint16_t reading;
297
298 rc = pm_bms_read_output_data(chip, VSENSE_FOR_RBATT, &reading);
299 if (rc) {
300 pr_err("fail to read VSENSE_FOR_RBATT rc = %d\n", rc);
301 return rc;
302 }
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700303 *result = cc_to_microvolt(chip, reading);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700304 pr_debug("raw = %04x vsense_for_r_microV = %u\n", reading, *result);
305 return 0;
306}
307
308static int read_ocv_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
309{
310 int rc;
311 uint16_t reading;
312
313 rc = pm_bms_read_output_data(chip, OCV_FOR_RBATT, &reading);
314 if (rc) {
315 pr_err("fail to read OCV_FOR_RBATT rc = %d\n", rc);
316 return rc;
317 }
318 *result = CONV_READING(reading);
319 pr_debug("read = %04x ocv_for_r_microV = %u\n", reading, *result);
320 return 0;
321}
322
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700323static int read_vsense_avg(struct pm8921_bms_chip *chip, int *result)
324{
325 int rc;
326 int16_t reading;
327
328 rc = pm_bms_read_output_data(chip, VSENSE_AVG, &reading);
329 if (rc) {
330 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
331 return rc;
332 }
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700333 *result = cc_to_microvolt(chip, reading);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700334 pr_debug("read = %04x vsense = %d\n", reading, *result);
335 return 0;
336}
337
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338static int linear_interpolate(int y0, int x0, int y1, int x1, int x)
339{
340 if (y0 == y1 || x == x0)
341 return y0;
342 if (x1 == x0 || x == x1)
343 return y1;
344
345 return y0 + ((y1 - y0) * (x - x0) / (x1 - x0));
346}
347
348static int interpolate_single_lut(struct single_row_lut *lut, int x)
349{
350 int i, result;
351
352 if (x < lut->x[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700353 pr_debug("x %d less than known range return y = %d lut = %pS\n",
354 x, lut->y[0], lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700355 return lut->y[0];
356 }
357 if (x > lut->x[lut->cols - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700358 pr_debug("x %d more than known range return y = %d lut = %pS\n",
359 x, lut->y[lut->cols - 1], lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700360 return lut->y[lut->cols - 1];
361 }
362
363 for (i = 0; i < lut->cols; i++)
364 if (x <= lut->x[i])
365 break;
366 if (x == lut->x[i]) {
367 result = lut->y[i];
368 } else {
369 result = linear_interpolate(
370 lut->y[i - 1],
371 lut->x[i - 1],
372 lut->y[i],
373 lut->x[i],
374 x);
375 }
376 return result;
377}
378
379static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp)
380{
381 return interpolate_single_lut(chip->fcc_temp_lut, batt_temp);
382}
383
384static int interpolate_scalingfactor_fcc(struct pm8921_bms_chip *chip,
385 int cycles)
386{
387 return interpolate_single_lut(chip->fcc_sf_lut, cycles);
388}
389
390static int interpolate_scalingfactor_pc(struct pm8921_bms_chip *chip,
391 int cycles, int pc)
392{
393 int i, scalefactorrow1, scalefactorrow2, scalefactor, row1, row2;
394 int rows = chip->pc_sf_lut->rows;
395 int cols = chip->pc_sf_lut->cols;
396
397 if (pc > chip->pc_sf_lut->percent[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700398 pr_debug("pc %d greater than known pc ranges for sfd\n", pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399 row1 = 0;
400 row2 = 0;
401 }
402 if (pc < chip->pc_sf_lut->percent[rows - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700403 pr_debug("pc %d less than known pc ranges for sf", pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700404 row1 = rows - 1;
405 row2 = rows - 1;
406 }
407 for (i = 0; i < rows; i++) {
408 if (pc == chip->pc_sf_lut->percent[i]) {
409 row1 = i;
410 row2 = i;
411 break;
412 }
413 if (pc > chip->pc_sf_lut->percent[i]) {
414 row1 = i - 1;
415 row2 = i;
416 break;
417 }
418 }
419
420 if (cycles < chip->pc_sf_lut->cycles[0])
421 cycles = chip->pc_sf_lut->cycles[0];
422 if (cycles > chip->pc_sf_lut->cycles[cols - 1])
423 cycles = chip->pc_sf_lut->cycles[cols - 1];
424
425 for (i = 0; i < cols; i++)
426 if (cycles <= chip->pc_sf_lut->cycles[i])
427 break;
428 if (cycles == chip->pc_sf_lut->cycles[i]) {
429 scalefactor = linear_interpolate(
430 chip->pc_sf_lut->sf[row1][i],
431 chip->pc_sf_lut->percent[row1],
432 chip->pc_sf_lut->sf[row2][i],
433 chip->pc_sf_lut->percent[row2],
434 pc);
435 return scalefactor;
436 }
437
438 scalefactorrow1 = linear_interpolate(
439 chip->pc_sf_lut->sf[row1][i - 1],
440 chip->pc_sf_lut->cycles[i - 1],
441 chip->pc_sf_lut->sf[row1][i],
442 chip->pc_sf_lut->cycles[i],
443 cycles);
444
445 scalefactorrow2 = linear_interpolate(
446 chip->pc_sf_lut->sf[row2][i - 1],
447 chip->pc_sf_lut->cycles[i - 1],
448 chip->pc_sf_lut->sf[row2][i],
449 chip->pc_sf_lut->cycles[i],
450 cycles);
451
452 scalefactor = linear_interpolate(
453 scalefactorrow1,
454 chip->pc_sf_lut->percent[row1],
455 scalefactorrow2,
456 chip->pc_sf_lut->percent[row2],
457 pc);
458
459 return scalefactor;
460}
461
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700462static int is_between(int left, int right, int value)
463{
464 if (left >= right && left >= value && value >= right)
465 return 1;
466 if (left <= right && left <= value && value <= right)
467 return 1;
468
469 return 0;
470}
471
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700472static int interpolate_pc(struct pm8921_bms_chip *chip,
473 int batt_temp, int ocv)
474{
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700475 int i, j, pcj, pcj_minus_one, pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700476 int rows = chip->pc_temp_ocv_lut->rows;
477 int cols = chip->pc_temp_ocv_lut->cols;
478
479 if (batt_temp < chip->pc_temp_ocv_lut->temp[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700480 pr_debug("batt_temp %d < known temp range for pc\n", batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481 batt_temp = chip->pc_temp_ocv_lut->temp[0];
482 }
483 if (batt_temp > chip->pc_temp_ocv_lut->temp[cols - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700484 pr_debug("batt_temp %d > known temp range for pc\n", batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700485 batt_temp = chip->pc_temp_ocv_lut->temp[cols - 1];
486 }
487
488 for (j = 0; j < cols; j++)
489 if (batt_temp <= chip->pc_temp_ocv_lut->temp[j])
490 break;
491 if (batt_temp == chip->pc_temp_ocv_lut->temp[j]) {
492 /* found an exact match for temp in the table */
493 if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j])
494 return chip->pc_temp_ocv_lut->percent[0];
495 if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j])
496 return chip->pc_temp_ocv_lut->percent[rows - 1];
497 for (i = 0; i < rows; i++) {
498 if (ocv >= chip->pc_temp_ocv_lut->ocv[i][j]) {
499 if (ocv == chip->pc_temp_ocv_lut->ocv[i][j])
500 return
501 chip->pc_temp_ocv_lut->percent[i];
502 pc = linear_interpolate(
503 chip->pc_temp_ocv_lut->percent[i],
504 chip->pc_temp_ocv_lut->ocv[i][j],
505 chip->pc_temp_ocv_lut->percent[i - 1],
506 chip->pc_temp_ocv_lut->ocv[i - 1][j],
507 ocv);
508 return pc;
509 }
510 }
511 }
512
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700513 /*
514 * batt_temp is within temperature for
515 * column j-1 and j
516 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 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 - 1])
520 return chip->pc_temp_ocv_lut->percent[rows - 1];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700521
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700522 pcj_minus_one = 0;
523 pcj = 0;
524 for (i = 0; i < rows-1; i++) {
525 if (pcj == 0
526 && is_between(chip->pc_temp_ocv_lut->ocv[i][j],
527 chip->pc_temp_ocv_lut->ocv[i+1][j], ocv)) {
528 pcj = linear_interpolate(
529 chip->pc_temp_ocv_lut->percent[i],
530 chip->pc_temp_ocv_lut->ocv[i][j],
531 chip->pc_temp_ocv_lut->percent[i + 1],
532 chip->pc_temp_ocv_lut->ocv[i+1][j],
533 ocv);
534 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700536 if (pcj_minus_one == 0
537 && is_between(chip->pc_temp_ocv_lut->ocv[i][j-1],
538 chip->pc_temp_ocv_lut->ocv[i+1][j-1], ocv)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700540 pcj_minus_one = linear_interpolate(
541 chip->pc_temp_ocv_lut->percent[i],
542 chip->pc_temp_ocv_lut->ocv[i][j-1],
543 chip->pc_temp_ocv_lut->percent[i + 1],
544 chip->pc_temp_ocv_lut->ocv[i+1][j-1],
545 ocv);
546 }
547
548 if (pcj && pcj_minus_one) {
549 pc = linear_interpolate(
550 pcj_minus_one,
551 chip->pc_temp_ocv_lut->temp[j-1],
552 pcj,
553 chip->pc_temp_ocv_lut->temp[j],
554 batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555 return pc;
556 }
557 }
558
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700559 if (pcj)
560 return pcj;
561
562 if (pcj_minus_one)
563 return pcj_minus_one;
564
565 pr_debug("%d ocv wasn't found for temp %d in the LUT returning 100%%",
566 ocv, batt_temp);
567 return 100;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568}
569
570static int calculate_rbatt(struct pm8921_bms_chip *chip)
571{
572 int rc;
573 unsigned int ocv, vsense, vbatt, r_batt;
574
575 rc = read_ocv_for_rbatt(chip, &ocv);
576 if (rc) {
577 pr_err("fail to read ocv_for_rbatt rc = %d\n", rc);
578 ocv = 0;
579 }
580 rc = read_vbatt_for_rbatt(chip, &vbatt);
581 if (rc) {
582 pr_err("fail to read vbatt_for_rbatt rc = %d\n", rc);
583 ocv = 0;
584 }
585 rc = read_vsense_for_rbatt(chip, &vsense);
586 if (rc) {
587 pr_err("fail to read vsense_for_rbatt rc = %d\n", rc);
588 ocv = 0;
589 }
590 if (ocv == 0
591 || ocv == vbatt
592 || vsense == 0) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700593 pr_debug("rbatt readings unavailable ocv = %d, vbatt = %d,"
594 "vsen = %d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595 ocv, vbatt, vsense);
596 return -EINVAL;
597 }
598 r_batt = ((ocv - vbatt) * chip->r_sense) / vsense;
599 pr_debug("r_batt = %umilliOhms", r_batt);
600 return r_batt;
601}
602
603static int calculate_fcc(struct pm8921_bms_chip *chip, int batt_temp,
604 int chargecycles)
605{
606 int initfcc, result, scalefactor = 0;
607
608 initfcc = interpolate_fcc(chip, batt_temp);
609 pr_debug("intfcc = %umAh batt_temp = %d\n", initfcc, batt_temp);
610
611 scalefactor = interpolate_scalingfactor_fcc(chip, chargecycles);
612 pr_debug("scalefactor = %d batt_temp = %d\n", scalefactor, batt_temp);
613
614 /* Multiply the initial FCC value by the scale factor. */
615 result = (initfcc * scalefactor) / 100;
616 pr_debug("fcc mAh = %d\n", result);
617 return result;
618}
619
620static int calculate_pc(struct pm8921_bms_chip *chip, int ocv_uv, int batt_temp,
621 int chargecycles)
622{
623 int pc, scalefactor;
624
625 pc = interpolate_pc(chip, batt_temp, ocv_uv / 1000);
626 pr_debug("pc = %u for ocv = %dmicroVolts batt_temp = %d\n",
627 pc, ocv_uv, batt_temp);
628
629 scalefactor = interpolate_scalingfactor_pc(chip, chargecycles, pc);
630 pr_debug("scalefactor = %u batt_temp = %d\n", scalefactor, batt_temp);
631
632 /* Multiply the initial FCC value by the scale factor. */
633 pc = (pc * scalefactor) / 100;
634 return pc;
635}
636
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700637static void calculate_cc_mah(struct pm8921_bms_chip *chip, int64_t *val,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700638 int *coulumb_counter, int *update_userspace)
639{
640 int rc;
641 int64_t cc_voltage_uv, cc_uvh, cc_mah;
642
643 rc = read_cc(the_chip, coulumb_counter);
644 if (rc) {
645 *coulumb_counter = (last_coulumb_counter < 0) ?
646 DEFAULT_COULUMB_COUNTER : last_coulumb_counter;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700647 pr_debug("couldn't read coulumb counter err = %d assuming %d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648 rc, *coulumb_counter);
649 *update_userspace = 0;
650 }
651 cc_voltage_uv = (int64_t)*coulumb_counter;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700652 cc_voltage_uv = cc_to_microvolt(chip, cc_voltage_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700653 pr_debug("cc_voltage_uv = %lld microvolts\n", cc_voltage_uv);
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700654 cc_uvh = ccmicrovolt_to_uvh(cc_voltage_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700655 pr_debug("cc_uvh = %lld micro_volt_hour\n", cc_uvh);
656 cc_mah = div_s64(cc_uvh, chip->r_sense);
657 *val = cc_mah;
658}
659
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700660static int get_battery_uvolts(struct pm8921_bms_chip *chip, int *uvolts)
661{
662 int rc;
663 struct pm8921_adc_chan_result result;
664
665 rc = pm8921_adc_read(chip->vbat_channel, &result);
666 if (rc) {
667 pr_err("error reading adc channel = %d, rc = %d\n",
668 chip->vbat_channel, rc);
669 return rc;
670 }
671 pr_debug("mvolts phy = %lld meas = 0x%llx", result.physical,
672 result.measurement);
673 *uvolts = (int)result.physical;
674 *uvolts = *uvolts * 1000;
675 return 0;
676}
677
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700678static int calculate_state_of_charge(struct pm8921_bms_chip *chip,
679 int batt_temp, int chargecycles)
680{
681 int remaining_usable_charge, fcc, unusable_charge;
682 int remaining_charge, soc, rc, ocv, pc, coulumb_counter;
683 int rbatt, voltage_unusable_uv, pc_unusable;
684 int update_userspace = 1;
685 int64_t cc_mah;
686
687 rbatt = calculate_rbatt(chip);
688 if (rbatt < 0) {
689 rbatt = (last_rbatt < 0) ? DEFAULT_RBATT_MOHMS : last_rbatt;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700690 pr_debug("rbatt unavailable assuming %d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700691 rbatt);
692 update_userspace = 0;
693 }
694 pr_debug("rbatt = %umilliOhms", rbatt);
695
696 fcc = calculate_fcc(chip, batt_temp, chargecycles);
697 if (fcc < -EINVAL) {
698 fcc = (last_fcc < 0) ? chip->fcc : last_fcc;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700699 pr_debug("failed to read fcc assuming %d\n", fcc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700 update_userspace = 0;
701 }
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700702 pr_debug("FCC = %umAh", fcc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703
704 /* calculate unusable charge */
705 voltage_unusable_uv = (rbatt * chip->i_test)
706 + (chip->v_failure * 1000);
707 pc_unusable = calculate_pc(chip, voltage_unusable_uv,
708 batt_temp, chargecycles);
709 if (pc_unusable < 0) {
710 unusable_charge = (last_unusable_charge < 0) ?
711 DEFAULT_UNUSABLE_CHARGE_MAH : last_unusable_charge;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700712 pr_debug("unusable_charge failed assuming %d\n",
713 unusable_charge);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700714 } else {
715 unusable_charge = (fcc * pc_unusable) / 100;
716 }
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700717 pr_debug("UUC = %umAh at temp = %d, fcc = %umAh"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700718 "unusable_voltage = %umicroVolts pc_unusable = %d\n",
719 unusable_charge, batt_temp, fcc,
720 voltage_unusable_uv, pc_unusable);
721
722 /* calculate remainging charge */
723 rc = read_last_good_ocv(chip, &ocv);
724 if (rc || ocv == 0) {
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700725 rc = get_battery_uvolts(chip, &ocv);
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700726 pr_debug("ocv not available adc vbat = %d rc = %d\n", ocv, rc);
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700727 if (rc) {
728 ocv = (last_ocv_uv < 0) ?
729 DEFAULT_OCV_MICROVOLTS : last_ocv_uv;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700730 pr_debug("adc ocv failed assuming %d rc = %d\n",
731 ocv, rc);
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700732 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700733 update_userspace = 0;
734 }
735 pc = calculate_pc(chip, ocv, batt_temp, chargecycles);
736 if (pc < 0) {
737 remaining_charge = (last_remaining_charge < 0) ?
738 DEFAULT_REMAINING_CHARGE_MAH : last_remaining_charge;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700739 pr_debug("calculate remaining charge failed assuming %d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740 remaining_charge);
741 update_userspace = 0;
742 } else {
743 remaining_charge = (fcc * pc) / 100;
744 }
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700745 pr_debug("RC = %umAh ocv = %d pc = %d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700746 remaining_charge, ocv, pc);
747
748 /* calculate cc milli_volt_hour */
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700749 calculate_cc_mah(chip, &cc_mah, &coulumb_counter, &update_userspace);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700750 pr_debug("cc_mah = %lldmAh cc = %d\n", cc_mah, coulumb_counter);
751
752 /* calculate remaining usable charge */
753 remaining_usable_charge = remaining_charge - cc_mah - unusable_charge;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700754 pr_debug("RUC = %dmAh\n", remaining_usable_charge);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700755 if (remaining_usable_charge < 0) {
756 pr_err("bad rem_usb_chg cc_mah %lld, rem_chg %d unusb_chg %d\n",
757 cc_mah, remaining_charge, unusable_charge);
758 update_userspace = 0;
759 }
760
761 soc = (remaining_usable_charge * 100) / (fcc - unusable_charge);
762 if (soc > 100 || soc < 0) {
763 pr_err("bad soc rem_usb_chg %d fcc %d unusb_chg %d\n",
764 remaining_usable_charge, fcc, unusable_charge);
765 update_userspace = 0;
766 }
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700767 pr_debug("SOC = %u%%\n", soc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700768
769 if (update_userspace) {
770 last_rbatt = rbatt;
771 last_fcc = fcc;
772 last_unusable_charge = unusable_charge;
773 last_ocv_uv = ocv;
774 last_remaining_charge = remaining_charge;
775 last_coulumb_counter = coulumb_counter;
776 last_soc = soc;
777 }
778 return soc;
779}
780
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700781int pm8921_bms_get_vsense_avg(int *result)
782{
783 if (the_chip)
784 return read_vsense_avg(the_chip, result);
785
786 pr_err("called before initialization\n");
787 return -EINVAL;
788}
789EXPORT_SYMBOL(pm8921_bms_get_vsense_avg);
790
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -0700791int pm8921_bms_get_battery_current(int *result)
792{
793 if (!the_chip) {
794 pr_err("called before initialization\n");
795 return -EINVAL;
796 }
797 if (the_chip->r_sense == 0) {
798 pr_err("r_sense is zero\n");
799 return -EINVAL;
800 }
801
802 read_vsense_avg(the_chip, result);
803 pr_debug("vsense=%d\n", *result);
804 *result = *result / the_chip->r_sense;
805 return 0;
806}
807EXPORT_SYMBOL(pm8921_bms_get_battery_current);
808
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700809int pm8921_bms_get_percent_charge(void)
810{
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700811 int batt_temp, rc;
812 struct pm8921_adc_chan_result result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700814 if (!the_chip) {
815 pr_err("called before initialization\n");
816 return -EINVAL;
817 }
818
819 rc = pm8921_adc_read(the_chip->batt_temp_channel, &result);
820 if (rc) {
821 pr_err("error reading adc channel = %d, rc = %d\n",
822 the_chip->batt_temp_channel, rc);
823 return rc;
824 }
825 pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical,
826 result.measurement);
827 batt_temp = (int)result.physical;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700828 return calculate_state_of_charge(the_chip,
829 batt_temp, last_chargecycles);
830}
831EXPORT_SYMBOL_GPL(pm8921_bms_get_percent_charge);
832
833static int start_percent;
834static int end_percent;
835void pm8921_bms_charging_began(void)
836{
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700837 start_percent = pm8921_bms_get_percent_charge();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838 pr_debug("start_percent = %u%%\n", start_percent);
839}
840EXPORT_SYMBOL_GPL(pm8921_bms_charging_began);
841
842void pm8921_bms_charging_end(void)
843{
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700844 end_percent = pm8921_bms_get_percent_charge();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845 if (end_percent > start_percent) {
846 last_charge_increase = end_percent - start_percent;
847 if (last_charge_increase > 100) {
848 last_chargecycles++;
849 last_charge_increase = last_charge_increase % 100;
850 }
851 }
852 pr_debug("end_percent = %u%% last_charge_increase = %d"
853 "last_chargecycles = %d\n",
854 end_percent,
855 last_charge_increase,
856 last_chargecycles);
857}
858EXPORT_SYMBOL_GPL(pm8921_bms_charging_end);
859
860static irqreturn_t pm8921_bms_sbi_write_ok_handler(int irq, void *data)
861{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700862 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700863 return IRQ_HANDLED;
864}
865
866static irqreturn_t pm8921_bms_cc_thr_handler(int irq, void *data)
867{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700868 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700869 return IRQ_HANDLED;
870}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700871
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700872static irqreturn_t pm8921_bms_vsense_thr_handler(int irq, void *data)
873{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700874 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700875 return IRQ_HANDLED;
876}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700877
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700878static irqreturn_t pm8921_bms_vsense_for_r_handler(int irq, void *data)
879{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700880 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700881 return IRQ_HANDLED;
882}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700883
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700884static irqreturn_t pm8921_bms_ocv_for_r_handler(int irq, void *data)
885{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700886
887 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700888 return IRQ_HANDLED;
889}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700890
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700891static irqreturn_t pm8921_bms_good_ocv_handler(int irq, void *data)
892{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700893
894 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700895 return IRQ_HANDLED;
896}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700897
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700898static irqreturn_t pm8921_bms_vsense_avg_handler(int irq, void *data)
899{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700900 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700901 return IRQ_HANDLED;
902}
903
904struct pm_bms_irq_init_data {
905 unsigned int irq_id;
906 char *name;
907 unsigned long flags;
908 irqreturn_t (*handler)(int, void *);
909};
910
911#define BMS_IRQ(_id, _flags, _handler) \
912{ \
913 .irq_id = _id, \
914 .name = #_id, \
915 .flags = _flags, \
916 .handler = _handler, \
917}
918
919struct pm_bms_irq_init_data bms_irq_data[] = {
920 BMS_IRQ(PM8921_BMS_SBI_WRITE_OK, IRQF_TRIGGER_RISING,
921 pm8921_bms_sbi_write_ok_handler),
922 BMS_IRQ(PM8921_BMS_CC_THR, IRQF_TRIGGER_RISING,
923 pm8921_bms_cc_thr_handler),
924 BMS_IRQ(PM8921_BMS_VSENSE_THR, IRQF_TRIGGER_RISING,
925 pm8921_bms_vsense_thr_handler),
926 BMS_IRQ(PM8921_BMS_VSENSE_FOR_R, IRQF_TRIGGER_RISING,
927 pm8921_bms_vsense_for_r_handler),
928 BMS_IRQ(PM8921_BMS_OCV_FOR_R, IRQF_TRIGGER_RISING,
929 pm8921_bms_ocv_for_r_handler),
930 BMS_IRQ(PM8921_BMS_GOOD_OCV, IRQF_TRIGGER_RISING,
931 pm8921_bms_good_ocv_handler),
932 BMS_IRQ(PM8921_BMS_VSENSE_AVG, IRQF_TRIGGER_RISING,
933 pm8921_bms_vsense_avg_handler),
934};
935
936static void free_irqs(struct pm8921_bms_chip *chip)
937{
938 int i;
939
940 for (i = 0; i < PM_BMS_MAX_INTS; i++)
941 if (chip->pmic_bms_irq[i]) {
942 free_irq(chip->pmic_bms_irq[i], NULL);
943 chip->pmic_bms_irq[i] = 0;
944 }
945}
946
947static int __devinit request_irqs(struct pm8921_bms_chip *chip,
948 struct platform_device *pdev)
949{
950 struct resource *res;
951 int ret, i;
952
953 ret = 0;
954 bitmap_fill(chip->enabled_irqs, PM_BMS_MAX_INTS);
955
956 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
957 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
958 bms_irq_data[i].name);
959 if (res == NULL) {
960 pr_err("couldn't find %s\n", bms_irq_data[i].name);
961 goto err_out;
962 }
963 ret = request_irq(res->start, bms_irq_data[i].handler,
964 bms_irq_data[i].flags,
965 bms_irq_data[i].name, chip);
966 if (ret < 0) {
967 pr_err("couldn't request %d (%s) %d\n", res->start,
968 bms_irq_data[i].name, ret);
969 goto err_out;
970 }
971 chip->pmic_bms_irq[bms_irq_data[i].irq_id] = res->start;
972 pm8921_bms_disable_irq(chip, bms_irq_data[i].irq_id);
973 }
974 return 0;
975
976err_out:
977 free_irqs(chip);
978 return -EINVAL;
979}
980
981#define EN_BMS_BIT BIT(7)
982#define EN_PON_HS_BIT BIT(0)
983static int __devinit pm8921_bms_hw_init(struct pm8921_bms_chip *chip)
984{
985 int rc;
986
987 rc = pm_bms_masked_write(chip, BMS_CONTROL,
988 EN_BMS_BIT | EN_PON_HS_BIT, EN_BMS_BIT | EN_PON_HS_BIT);
989 if (rc) {
990 pr_err("failed to enable pon and bms addr = %d %d",
991 BMS_CONTROL, rc);
992 }
993
994 return 0;
995}
996
997enum {
998 CALC_RBATT,
999 CALC_FCC,
1000 CALC_PC,
1001 CALC_SOC,
1002};
1003
1004static int test_batt_temp = 5;
1005static int test_chargecycle = 150;
1006static int test_ocv = 3900000;
1007enum {
1008 TEST_BATT_TEMP,
1009 TEST_CHARGE_CYCLE,
1010 TEST_OCV,
1011};
1012static int get_test_param(void *data, u64 * val)
1013{
1014 switch ((int)data) {
1015 case TEST_BATT_TEMP:
1016 *val = test_batt_temp;
1017 break;
1018 case TEST_CHARGE_CYCLE:
1019 *val = test_chargecycle;
1020 break;
1021 case TEST_OCV:
1022 *val = test_ocv;
1023 break;
1024 default:
1025 return -EINVAL;
1026 }
1027 return 0;
1028}
1029static int set_test_param(void *data, u64 val)
1030{
1031 switch ((int)data) {
1032 case TEST_BATT_TEMP:
1033 test_batt_temp = (int)val;
1034 break;
1035 case TEST_CHARGE_CYCLE:
1036 test_chargecycle = (int)val;
1037 break;
1038 case TEST_OCV:
1039 test_ocv = (int)val;
1040 break;
1041 default:
1042 return -EINVAL;
1043 }
1044 return 0;
1045}
1046DEFINE_SIMPLE_ATTRIBUTE(temp_fops, get_test_param, set_test_param, "%llu\n");
1047
1048static int get_calc(void *data, u64 * val)
1049{
1050 int param = (int)data;
1051 int ret = 0;
1052
1053 *val = 0;
1054
1055 /* global irq number passed in via data */
1056 switch (param) {
1057 case CALC_RBATT:
1058 *val = calculate_rbatt(the_chip);
1059 break;
1060 case CALC_FCC:
1061 *val = calculate_fcc(the_chip, test_batt_temp,
1062 test_chargecycle);
1063 break;
1064 case CALC_PC:
1065 *val = calculate_pc(the_chip, test_ocv, test_batt_temp,
1066 test_chargecycle);
1067 break;
1068 case CALC_SOC:
1069 *val = calculate_state_of_charge(the_chip,
1070 test_batt_temp, test_chargecycle);
1071 break;
1072 default:
1073 ret = -EINVAL;
1074 }
1075 return ret;
1076}
1077DEFINE_SIMPLE_ATTRIBUTE(calc_fops, get_calc, NULL, "%llu\n");
1078
1079static int get_reading(void *data, u64 * val)
1080{
1081 int param = (int)data;
1082 int ret = 0;
1083
1084 *val = 0;
1085
1086 /* global irq number passed in via data */
1087 switch (param) {
1088 case CC_MSB:
1089 case CC_LSB:
1090 read_cc(the_chip, (int *)val);
1091 break;
1092 case LAST_GOOD_OCV_VALUE:
1093 read_last_good_ocv(the_chip, (uint *)val);
1094 break;
1095 case VBATT_FOR_RBATT:
1096 read_vbatt_for_rbatt(the_chip, (uint *)val);
1097 break;
1098 case VSENSE_FOR_RBATT:
1099 read_vsense_for_rbatt(the_chip, (uint *)val);
1100 break;
1101 case OCV_FOR_RBATT:
1102 read_ocv_for_rbatt(the_chip, (uint *)val);
1103 break;
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001104 case VSENSE_AVG:
1105 read_vsense_avg(the_chip, (uint *)val);
1106 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001107 default:
1108 ret = -EINVAL;
1109 }
1110 return ret;
1111}
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001112DEFINE_SIMPLE_ATTRIBUTE(reading_fops, get_reading, NULL, "%lld\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001113
1114static int get_rt_status(void *data, u64 * val)
1115{
1116 int i = (int)data;
1117 int ret;
1118
1119 /* global irq number passed in via data */
1120 ret = pm_bms_get_rt_status(the_chip, i);
1121 *val = ret;
1122 return 0;
1123}
1124DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n");
1125
1126static int get_reg(void *data, u64 * val)
1127{
1128 int addr = (int)data;
1129 int ret;
1130 u8 temp;
1131
1132 ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp);
1133 if (ret) {
1134 pr_err("pm8xxx_readb to %x value = %d errored = %d\n",
1135 addr, temp, ret);
1136 return -EAGAIN;
1137 }
1138 *val = temp;
1139 return 0;
1140}
1141
1142static int set_reg(void *data, u64 val)
1143{
1144 int addr = (int)data;
1145 int ret;
1146 u8 temp;
1147
1148 temp = (u8) val;
1149 ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp);
1150 if (ret) {
1151 pr_err("pm8xxx_writeb to %x value = %d errored = %d\n",
1152 addr, temp, ret);
1153 return -EAGAIN;
1154 }
1155 return 0;
1156}
1157DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
1158
1159static void create_debugfs_entries(struct pm8921_bms_chip *chip)
1160{
1161 int i;
1162
1163 chip->dent = debugfs_create_dir("pm8921_bms", NULL);
1164
1165 if (IS_ERR(chip->dent)) {
1166 pr_err("pmic bms couldnt create debugfs dir\n");
1167 return;
1168 }
1169
1170 debugfs_create_file("BMS_CONTROL", 0644, chip->dent,
1171 (void *)BMS_CONTROL, &reg_fops);
1172 debugfs_create_file("BMS_OUTPUT0", 0644, chip->dent,
1173 (void *)BMS_OUTPUT0, &reg_fops);
1174 debugfs_create_file("BMS_OUTPUT1", 0644, chip->dent,
1175 (void *)BMS_OUTPUT1, &reg_fops);
1176 debugfs_create_file("BMS_TEST1", 0644, chip->dent,
1177 (void *)BMS_TEST1, &reg_fops);
1178 debugfs_create_file("CCADC_ANA_PARAM", 0644, chip->dent,
1179 (void *)CCADC_ANA_PARAM, &reg_fops);
1180 debugfs_create_file("CCADC_DIG_PARAM", 0644, chip->dent,
1181 (void *)CCADC_DIG_PARAM, &reg_fops);
1182 debugfs_create_file("CCADC_RSV", 0644, chip->dent,
1183 (void *)CCADC_RSV, &reg_fops);
1184 debugfs_create_file("CCADC_DATA0", 0644, chip->dent,
1185 (void *)CCADC_DATA0, &reg_fops);
1186 debugfs_create_file("CCADC_DATA1", 0644, chip->dent,
1187 (void *)CCADC_DATA1, &reg_fops);
1188 debugfs_create_file("CCADC_OFFSET_TRIM1", 0644, chip->dent,
1189 (void *)CCADC_OFFSET_TRIM1, &reg_fops);
1190 debugfs_create_file("CCADC_OFFSET_TRIM0", 0644, chip->dent,
1191 (void *)CCADC_OFFSET_TRIM0, &reg_fops);
1192
1193 debugfs_create_file("test_batt_temp", 0644, chip->dent,
1194 (void *)TEST_BATT_TEMP, &temp_fops);
1195 debugfs_create_file("test_chargecycle", 0644, chip->dent,
1196 (void *)TEST_CHARGE_CYCLE, &temp_fops);
1197 debugfs_create_file("test_ocv", 0644, chip->dent,
1198 (void *)TEST_OCV, &temp_fops);
1199
1200 debugfs_create_file("read_cc", 0644, chip->dent,
1201 (void *)CC_MSB, &reading_fops);
1202 debugfs_create_file("read_last_good_ocv", 0644, chip->dent,
1203 (void *)LAST_GOOD_OCV_VALUE, &reading_fops);
1204 debugfs_create_file("read_vbatt_for_rbatt", 0644, chip->dent,
1205 (void *)VBATT_FOR_RBATT, &reading_fops);
1206 debugfs_create_file("read_vsense_for_rbatt", 0644, chip->dent,
1207 (void *)VSENSE_FOR_RBATT, &reading_fops);
1208 debugfs_create_file("read_ocv_for_rbatt", 0644, chip->dent,
1209 (void *)OCV_FOR_RBATT, &reading_fops);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001210 debugfs_create_file("read_vsense_avg", 0644, chip->dent,
1211 (void *)VSENSE_AVG, &reading_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001212
1213 debugfs_create_file("show_rbatt", 0644, chip->dent,
1214 (void *)CALC_RBATT, &calc_fops);
1215 debugfs_create_file("show_fcc", 0644, chip->dent,
1216 (void *)CALC_FCC, &calc_fops);
1217 debugfs_create_file("show_pc", 0644, chip->dent,
1218 (void *)CALC_PC, &calc_fops);
1219 debugfs_create_file("show_soc", 0644, chip->dent,
1220 (void *)CALC_SOC, &calc_fops);
1221
1222 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
1223 if (chip->pmic_bms_irq[bms_irq_data[i].irq_id])
1224 debugfs_create_file(bms_irq_data[i].name, 0444,
1225 chip->dent,
1226 (void *)bms_irq_data[i].irq_id,
1227 &rt_fops);
1228 }
1229}
1230
1231static void calibrate_work(struct work_struct *work)
1232{
1233 /* TODO */
1234}
1235
1236static int __devinit pm8921_bms_probe(struct platform_device *pdev)
1237{
1238 int rc = 0;
1239 struct pm8921_bms_chip *chip;
1240 const struct pm8921_bms_platform_data *pdata
1241 = pdev->dev.platform_data;
1242 if (!pdata) {
1243 pr_err("missing platform data\n");
1244 return -EINVAL;
1245 }
1246
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001247 chip = kzalloc(sizeof(struct pm8921_bms_chip), GFP_KERNEL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001248 if (!chip) {
1249 pr_err("Cannot allocate pm_bms_chip\n");
1250 return -ENOMEM;
1251 }
1252
1253 chip->dev = &pdev->dev;
1254 chip->r_sense = pdata->r_sense;
1255 chip->i_test = pdata->i_test;
1256 chip->v_failure = pdata->v_failure;
1257 chip->calib_delay_ms = pdata->calib_delay_ms;
1258 chip->fcc = pdata->batt_data->fcc;
1259
1260 chip->fcc_temp_lut = pdata->batt_data->fcc_temp_lut;
1261 chip->fcc_sf_lut = pdata->batt_data->fcc_sf_lut;
1262 chip->pc_temp_ocv_lut = pdata->batt_data->pc_temp_ocv_lut;
1263 chip->pc_sf_lut = pdata->batt_data->pc_sf_lut;
1264
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001265 chip->batt_temp_channel = pdata->bms_cdata.batt_temp_channel;
1266 chip->vbat_channel = pdata->bms_cdata.vbat_channel;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -07001267 chip->revision = pm8xxx_get_revision(chip->dev->parent);
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001268
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001269 rc = pm8921_bms_hw_init(chip);
1270 if (rc) {
1271 pr_err("couldn't init hardware rc = %d\n", rc);
1272 goto free_chip;
1273 }
1274
1275 rc = request_irqs(chip, pdev);
1276 if (rc) {
1277 pr_err("couldn't register interrupts rc = %d\n", rc);
1278 goto free_chip;
1279 }
1280
1281 platform_set_drvdata(pdev, chip);
1282 the_chip = chip;
1283 create_debugfs_entries(chip);
1284
1285 INIT_DELAYED_WORK(&chip->calib_work, calibrate_work);
1286 schedule_delayed_work(&chip->calib_work,
1287 round_jiffies_relative(msecs_to_jiffies
1288 (chip->calib_delay_ms)));
1289 return 0;
1290
1291free_chip:
1292 kfree(chip);
1293 return rc;
1294}
1295
1296static int __devexit pm8921_bms_remove(struct platform_device *pdev)
1297{
1298 struct pm8921_bms_chip *chip = platform_get_drvdata(pdev);
1299
1300 free_irqs(chip);
1301 platform_set_drvdata(pdev, NULL);
1302 the_chip = NULL;
1303 kfree(chip);
1304 return 0;
1305}
1306
1307static struct platform_driver pm8921_bms_driver = {
1308 .probe = pm8921_bms_probe,
1309 .remove = __devexit_p(pm8921_bms_remove),
1310 .driver = {
1311 .name = PM8921_BMS_DEV_NAME,
1312 .owner = THIS_MODULE,
1313 },
1314};
1315
1316static int __init pm8921_bms_init(void)
1317{
1318 return platform_driver_register(&pm8921_bms_driver);
1319}
1320
1321static void __exit pm8921_bms_exit(void)
1322{
1323 platform_driver_unregister(&pm8921_bms_driver);
1324}
1325
1326late_initcall(pm8921_bms_init);
1327module_exit(pm8921_bms_exit);
1328
1329MODULE_LICENSE("GPL v2");
1330MODULE_DESCRIPTION("PMIC8921 bms driver");
1331MODULE_VERSION("1.0");
1332MODULE_ALIAS("platform:" PM8921_BMS_DEV_NAME);