blob: 6ad6a18f98c163791bd8db66f30d3a7e397099c9 [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>
21#include <linux/interrupt.h>
22#include <linux/bitops.h>
23#include <linux/debugfs.h>
24#include <linux/slab.h>
25
26#define BMS_CONTROL 0x224
27#define BMS_OUTPUT0 0x230
28#define BMS_OUTPUT1 0x231
29#define BMS_TEST1 0x237
30#define CCADC_ANA_PARAM 0x240
31#define CCADC_DIG_PARAM 0x241
32#define CCADC_RSV 0x242
33#define CCADC_DATA0 0x244
34#define CCADC_DATA1 0x245
35#define CCADC_OFFSET_TRIM1 0x34A
36#define CCADC_OFFSET_TRIM0 0x34B
37
38#define ADC_ARB_SECP_CNTRL 0x190
39#define ADC_ARB_SECP_AMUX_CNTRL 0x191
40#define ADC_ARB_SECP_ANA_PARAM 0x192
41#define ADC_ARB_SECP_RSV 0x194
42#define ADC_ARB_SECP_DATA1 0x195
43#define ADC_ARB_SECP_DATA0 0x196
44
45enum pmic_bms_interrupts {
46 PM8921_BMS_SBI_WRITE_OK,
47 PM8921_BMS_CC_THR,
48 PM8921_BMS_VSENSE_THR,
49 PM8921_BMS_VSENSE_FOR_R,
50 PM8921_BMS_OCV_FOR_R,
51 PM8921_BMS_GOOD_OCV,
52 PM8921_BMS_VSENSE_AVG,
53 PM_BMS_MAX_INTS,
54};
55
56/**
57 * struct pm8921_bms_chip -device information
58 * @dev: device pointer to access the parent
59 * @dent: debugfs directory
60 * @r_sense: batt sense resistance value
61 * @i_test: peak current
62 * @v_failure: battery dead voltage
63 * @fcc: battery capacity
64 *
65 */
66struct pm8921_bms_chip {
67 struct device *dev;
68 struct dentry *dent;
69 unsigned int r_sense;
70 unsigned int i_test;
71 unsigned int v_failure;
72 unsigned int fcc;
73 struct single_row_lut *fcc_temp_lut;
74 struct single_row_lut *fcc_sf_lut;
75 struct pc_temp_ocv_lut *pc_temp_ocv_lut;
76 struct pc_sf_lut *pc_sf_lut;
77 struct delayed_work calib_work;
78 unsigned int calib_delay_ms;
79 unsigned int pmic_bms_irq[PM_BMS_MAX_INTS];
80 DECLARE_BITMAP(enabled_irqs, PM_BMS_MAX_INTS);
81};
82
83static struct pm8921_bms_chip *the_chip;
84
85#define DEFAULT_RBATT_MOHMS 128
86#define DEFAULT_UNUSABLE_CHARGE_MAH 10
87#define DEFAULT_OCV_MICROVOLTS 3900000
88#define DEFAULT_REMAINING_CHARGE_MAH 990
89#define DEFAULT_COULUMB_COUNTER 0
90#define DEFAULT_CHARGE_CYCLES 0
91
92static int last_rbatt = -EINVAL;
93static int last_fcc = -EINVAL;
94static int last_unusable_charge = -EINVAL;
95static int last_ocv_uv = -EINVAL;
96static int last_remaining_charge = -EINVAL;
97static int last_coulumb_counter = -EINVAL;
98static int last_soc = -EINVAL;
99
100static int last_chargecycles = DEFAULT_CHARGE_CYCLES;
101static int last_charge_increase;
102
103module_param(last_rbatt, int, 0644);
104module_param(last_fcc, int, 0644);
105module_param(last_unusable_charge, int, 0644);
106module_param(last_ocv_uv, int, 0644);
107module_param(last_remaining_charge, int, 0644);
108module_param(last_coulumb_counter, int, 0644);
109module_param(last_chargecycles, int, 0644);
110module_param(last_charge_increase, int, 0644);
111
112static int pm_bms_get_rt_status(struct pm8921_bms_chip *chip, int irq_id)
113{
114 return pm8xxx_read_irq_stat(chip->dev->parent,
115 chip->pmic_bms_irq[irq_id]);
116}
117
118static void pm8921_bms_disable_irq(struct pm8921_bms_chip *chip, int interrupt)
119{
120 if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) {
121 dev_dbg(chip->dev, "%d\n", chip->pmic_bms_irq[interrupt]);
122 disable_irq_nosync(chip->pmic_bms_irq[interrupt]);
123 }
124}
125
126static int pm_bms_masked_write(struct pm8921_bms_chip *chip, u16 addr,
127 u8 mask, u8 val)
128{
129 int rc;
130 u8 reg;
131
132 rc = pm8xxx_readb(chip->dev->parent, addr, &reg);
133 if (rc) {
134 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
135 return rc;
136 }
137 reg &= ~mask;
138 reg |= val & mask;
139 rc = pm8xxx_writeb(chip->dev->parent, addr, reg);
140 if (rc) {
141 pr_err("write failed addr = %03X, rc = %d\n", addr, rc);
142 return rc;
143 }
144 return 0;
145}
146
147#define SELECT_OUTPUT_DATA 0x1C
148#define SELECT_OUTPUT_TYPE_SHIFT 2
149#define OCV_FOR_RBATT 0x0
150#define VSENSE_FOR_RBATT 0x1
151#define VBATT_FOR_RBATT 0x2
152#define CC_MSB 0x3
153#define CC_LSB 0x4
154#define LAST_GOOD_OCV_VALUE 0x5
155#define VSENSE_AVG 0x6
156#define VBATT_AVG 0x7
157
158static int pm_bms_read_output_data(struct pm8921_bms_chip *chip, int type,
159 int16_t *result)
160{
161 int rc;
162 u8 reg;
163
164 if (!result) {
165 pr_err("result pointer null\n");
166 return -EINVAL;
167 }
168 *result = 0;
169 if (type < OCV_FOR_RBATT || type > VBATT_AVG) {
170 pr_err("invalid type %d asked to read\n", type);
171 return -EINVAL;
172 }
173 rc = pm_bms_masked_write(chip, BMS_CONTROL, SELECT_OUTPUT_DATA,
174 type << SELECT_OUTPUT_TYPE_SHIFT);
175 if (rc) {
176 pr_err("fail to select %d type in BMS_CONTROL rc = %d\n",
177 type, rc);
178 return rc;
179 }
180
181 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT0, &reg);
182 if (rc) {
183 pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n",
184 type, rc);
185 return rc;
186 }
187 *result = reg;
188 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT1, &reg);
189 if (rc) {
190 pr_err("fail to read BMS_OUTPUT1 for type %d rc = %d\n",
191 type, rc);
192 return rc;
193 }
194 *result |= reg << 8;
195 pr_debug("type %d result %x", type, *result);
196 return 0;
197}
198
199#define V_PER_BIT_MUL_FACTOR 977
200#define V_PER_BIT_DIV_FACTOR 10
201#define CONV_READING(a) (((a) * (int)V_PER_BIT_MUL_FACTOR)\
202 /V_PER_BIT_DIV_FACTOR)
203
204/* returns the signed value read from the hardware */
205static int read_cc(struct pm8921_bms_chip *chip, int *result)
206{
207 int rc;
208 uint16_t msw, lsw;
209
210 rc = pm_bms_read_output_data(chip, CC_LSB, &lsw);
211 if (rc) {
212 pr_err("fail to read CC_LSB rc = %d\n", rc);
213 return rc;
214 }
215 rc = pm_bms_read_output_data(chip, CC_MSB, &msw);
216 if (rc) {
217 pr_err("fail to read CC_MSB rc = %d\n", rc);
218 return rc;
219 }
220 *result = msw << 16 | lsw;
221 pr_debug("msw = %04x lsw = %04x cc = %d\n", msw, lsw, *result);
222 return 0;
223}
224
225static int read_last_good_ocv(struct pm8921_bms_chip *chip, uint *result)
226{
227 int rc;
228 uint16_t reading;
229
230 rc = pm_bms_read_output_data(chip, LAST_GOOD_OCV_VALUE, &reading);
231 if (rc) {
232 pr_err("fail to read LAST_GOOD_OCV_VALUE rc = %d\n", rc);
233 return rc;
234 }
235 *result = CONV_READING(reading);
236 pr_debug("raw = %04x ocv_microV = %u\n", reading, *result);
237 return 0;
238}
239
240static int read_vbatt_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
241{
242 int rc;
243 uint16_t reading;
244
245 rc = pm_bms_read_output_data(chip, VBATT_FOR_RBATT, &reading);
246 if (rc) {
247 pr_err("fail to read VBATT_FOR_RBATT rc = %d\n", rc);
248 return rc;
249 }
250 *result = CONV_READING(reading);
251 pr_debug("raw = %04x vbatt_for_r_microV = %u\n", reading, *result);
252 return 0;
253}
254
255static int read_vsense_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
256{
257 int rc;
258 uint16_t reading;
259
260 rc = pm_bms_read_output_data(chip, VSENSE_FOR_RBATT, &reading);
261 if (rc) {
262 pr_err("fail to read VSENSE_FOR_RBATT rc = %d\n", rc);
263 return rc;
264 }
265 *result = CONV_READING(reading);
266 pr_debug("raw = %04x vsense_for_r_microV = %u\n", reading, *result);
267 return 0;
268}
269
270static int read_ocv_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
271{
272 int rc;
273 uint16_t reading;
274
275 rc = pm_bms_read_output_data(chip, OCV_FOR_RBATT, &reading);
276 if (rc) {
277 pr_err("fail to read OCV_FOR_RBATT rc = %d\n", rc);
278 return rc;
279 }
280 *result = CONV_READING(reading);
281 pr_debug("read = %04x ocv_for_r_microV = %u\n", reading, *result);
282 return 0;
283}
284
285static int linear_interpolate(int y0, int x0, int y1, int x1, int x)
286{
287 if (y0 == y1 || x == x0)
288 return y0;
289 if (x1 == x0 || x == x1)
290 return y1;
291
292 return y0 + ((y1 - y0) * (x - x0) / (x1 - x0));
293}
294
295static int interpolate_single_lut(struct single_row_lut *lut, int x)
296{
297 int i, result;
298
299 if (x < lut->x[0]) {
300 pr_err("x %d less than known range returning y = %d\n",
301 x, lut->y[0]);
302 return lut->y[0];
303 }
304 if (x > lut->x[lut->cols - 1]) {
305 pr_err("x %d more than known range returning y = %d\n",
306 x, lut->y[lut->cols - 1]);
307 return lut->y[lut->cols - 1];
308 }
309
310 for (i = 0; i < lut->cols; i++)
311 if (x <= lut->x[i])
312 break;
313 if (x == lut->x[i]) {
314 result = lut->y[i];
315 } else {
316 result = linear_interpolate(
317 lut->y[i - 1],
318 lut->x[i - 1],
319 lut->y[i],
320 lut->x[i],
321 x);
322 }
323 return result;
324}
325
326static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp)
327{
328 return interpolate_single_lut(chip->fcc_temp_lut, batt_temp);
329}
330
331static int interpolate_scalingfactor_fcc(struct pm8921_bms_chip *chip,
332 int cycles)
333{
334 return interpolate_single_lut(chip->fcc_sf_lut, cycles);
335}
336
337static int interpolate_scalingfactor_pc(struct pm8921_bms_chip *chip,
338 int cycles, int pc)
339{
340 int i, scalefactorrow1, scalefactorrow2, scalefactor, row1, row2;
341 int rows = chip->pc_sf_lut->rows;
342 int cols = chip->pc_sf_lut->cols;
343
344 if (pc > chip->pc_sf_lut->percent[0]) {
345 pr_err("pc %d greater than known pc ranges for sfd\n", pc);
346 row1 = 0;
347 row2 = 0;
348 }
349 if (pc < chip->pc_sf_lut->percent[rows - 1]) {
350 pr_err("pc %d less than known pc ranges for sf", pc);
351 row1 = rows - 1;
352 row2 = rows - 1;
353 }
354 for (i = 0; i < rows; i++) {
355 if (pc == chip->pc_sf_lut->percent[i]) {
356 row1 = i;
357 row2 = i;
358 break;
359 }
360 if (pc > chip->pc_sf_lut->percent[i]) {
361 row1 = i - 1;
362 row2 = i;
363 break;
364 }
365 }
366
367 if (cycles < chip->pc_sf_lut->cycles[0])
368 cycles = chip->pc_sf_lut->cycles[0];
369 if (cycles > chip->pc_sf_lut->cycles[cols - 1])
370 cycles = chip->pc_sf_lut->cycles[cols - 1];
371
372 for (i = 0; i < cols; i++)
373 if (cycles <= chip->pc_sf_lut->cycles[i])
374 break;
375 if (cycles == chip->pc_sf_lut->cycles[i]) {
376 scalefactor = linear_interpolate(
377 chip->pc_sf_lut->sf[row1][i],
378 chip->pc_sf_lut->percent[row1],
379 chip->pc_sf_lut->sf[row2][i],
380 chip->pc_sf_lut->percent[row2],
381 pc);
382 return scalefactor;
383 }
384
385 scalefactorrow1 = linear_interpolate(
386 chip->pc_sf_lut->sf[row1][i - 1],
387 chip->pc_sf_lut->cycles[i - 1],
388 chip->pc_sf_lut->sf[row1][i],
389 chip->pc_sf_lut->cycles[i],
390 cycles);
391
392 scalefactorrow2 = linear_interpolate(
393 chip->pc_sf_lut->sf[row2][i - 1],
394 chip->pc_sf_lut->cycles[i - 1],
395 chip->pc_sf_lut->sf[row2][i],
396 chip->pc_sf_lut->cycles[i],
397 cycles);
398
399 scalefactor = linear_interpolate(
400 scalefactorrow1,
401 chip->pc_sf_lut->percent[row1],
402 scalefactorrow2,
403 chip->pc_sf_lut->percent[row2],
404 pc);
405
406 return scalefactor;
407}
408
409static int interpolate_pc(struct pm8921_bms_chip *chip,
410 int batt_temp, int ocv)
411{
412 int i, j, ocvi, ocviplusone, pc = 0;
413 int rows = chip->pc_temp_ocv_lut->rows;
414 int cols = chip->pc_temp_ocv_lut->cols;
415
416 if (batt_temp < chip->pc_temp_ocv_lut->temp[0]) {
417 pr_err("batt_temp %d < known temp range for pc\n", batt_temp);
418 batt_temp = chip->pc_temp_ocv_lut->temp[0];
419 }
420 if (batt_temp > chip->pc_temp_ocv_lut->temp[cols - 1]) {
421 pr_err("batt_temp %d > known temp range for pc\n", batt_temp);
422 batt_temp = chip->pc_temp_ocv_lut->temp[cols - 1];
423 }
424
425 for (j = 0; j < cols; j++)
426 if (batt_temp <= chip->pc_temp_ocv_lut->temp[j])
427 break;
428 if (batt_temp == chip->pc_temp_ocv_lut->temp[j]) {
429 /* found an exact match for temp in the table */
430 if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j])
431 return chip->pc_temp_ocv_lut->percent[0];
432 if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j])
433 return chip->pc_temp_ocv_lut->percent[rows - 1];
434 for (i = 0; i < rows; i++) {
435 if (ocv >= chip->pc_temp_ocv_lut->ocv[i][j]) {
436 if (ocv == chip->pc_temp_ocv_lut->ocv[i][j])
437 return
438 chip->pc_temp_ocv_lut->percent[i];
439 pc = linear_interpolate(
440 chip->pc_temp_ocv_lut->percent[i],
441 chip->pc_temp_ocv_lut->ocv[i][j],
442 chip->pc_temp_ocv_lut->percent[i - 1],
443 chip->pc_temp_ocv_lut->ocv[i - 1][j],
444 ocv);
445 return pc;
446 }
447 }
448 }
449
450 if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j])
451 return chip->pc_temp_ocv_lut->percent[0];
452 if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j - 1])
453 return chip->pc_temp_ocv_lut->percent[rows - 1];
454 for (i = 0; i < rows; i++) {
455 if (ocv >= chip->pc_temp_ocv_lut->ocv[i][j - 1]
456 && ocv <= chip->pc_temp_ocv_lut->ocv[i][j]) {
457 pc = chip->pc_temp_ocv_lut->percent[i];
458
459 if (i < rows - 1
460 && ocv >=
461 chip->pc_temp_ocv_lut->ocv[i + 1][j - 1]
462 && ocv <=
463 chip->pc_temp_ocv_lut->ocv[i + 1][j]) {
464 ocvi = linear_interpolate(
465 chip->pc_temp_ocv_lut->ocv[i][j - 1],
466 chip->pc_temp_ocv_lut->temp[j - 1],
467 chip->pc_temp_ocv_lut->ocv[i][j],
468 chip->pc_temp_ocv_lut->temp[j],
469 batt_temp);
470
471 ocviplusone = linear_interpolate(
472 chip->pc_temp_ocv_lut
473 ->ocv[i + 1][j - 1],
474 chip->pc_temp_ocv_lut->temp[j - 1],
475 chip->pc_temp_ocv_lut->ocv[i + 1][j],
476 chip->pc_temp_ocv_lut->temp[j],
477 batt_temp);
478
479 pc = linear_interpolate(
480 chip->pc_temp_ocv_lut->percent[i],
481 ocvi,
482 chip->pc_temp_ocv_lut->percent[i + 1],
483 ocviplusone,
484 ocv);
485 }
486 return pc;
487 }
488 }
489
490 pr_err("%d ocv wasn't found for temp %d in the LUT returning pc = %d",
491 ocv, batt_temp, pc);
492 return pc;
493}
494
495static int calculate_rbatt(struct pm8921_bms_chip *chip)
496{
497 int rc;
498 unsigned int ocv, vsense, vbatt, r_batt;
499
500 rc = read_ocv_for_rbatt(chip, &ocv);
501 if (rc) {
502 pr_err("fail to read ocv_for_rbatt rc = %d\n", rc);
503 ocv = 0;
504 }
505 rc = read_vbatt_for_rbatt(chip, &vbatt);
506 if (rc) {
507 pr_err("fail to read vbatt_for_rbatt rc = %d\n", rc);
508 ocv = 0;
509 }
510 rc = read_vsense_for_rbatt(chip, &vsense);
511 if (rc) {
512 pr_err("fail to read vsense_for_rbatt rc = %d\n", rc);
513 ocv = 0;
514 }
515 if (ocv == 0
516 || ocv == vbatt
517 || vsense == 0) {
518 pr_warning("incorret reading ocv = %d, vbatt = %d, vsen = %d\n",
519 ocv, vbatt, vsense);
520 return -EINVAL;
521 }
522 r_batt = ((ocv - vbatt) * chip->r_sense) / vsense;
523 pr_debug("r_batt = %umilliOhms", r_batt);
524 return r_batt;
525}
526
527static int calculate_fcc(struct pm8921_bms_chip *chip, int batt_temp,
528 int chargecycles)
529{
530 int initfcc, result, scalefactor = 0;
531
532 initfcc = interpolate_fcc(chip, batt_temp);
533 pr_debug("intfcc = %umAh batt_temp = %d\n", initfcc, batt_temp);
534
535 scalefactor = interpolate_scalingfactor_fcc(chip, chargecycles);
536 pr_debug("scalefactor = %d batt_temp = %d\n", scalefactor, batt_temp);
537
538 /* Multiply the initial FCC value by the scale factor. */
539 result = (initfcc * scalefactor) / 100;
540 pr_debug("fcc mAh = %d\n", result);
541 return result;
542}
543
544static int calculate_pc(struct pm8921_bms_chip *chip, int ocv_uv, int batt_temp,
545 int chargecycles)
546{
547 int pc, scalefactor;
548
549 pc = interpolate_pc(chip, batt_temp, ocv_uv / 1000);
550 pr_debug("pc = %u for ocv = %dmicroVolts batt_temp = %d\n",
551 pc, ocv_uv, batt_temp);
552
553 scalefactor = interpolate_scalingfactor_pc(chip, chargecycles, pc);
554 pr_debug("scalefactor = %u batt_temp = %d\n", scalefactor, batt_temp);
555
556 /* Multiply the initial FCC value by the scale factor. */
557 pc = (pc * scalefactor) / 100;
558 return pc;
559}
560
561#define CC_TO_MICROVOLT(cc) div_s64(cc * 1085069, 100000);
562#define CCMICROVOLT_TO_UVH(cc_uv) div_s64(cc_uv * 55, 32768 * 3600)
563
564static void calculate_cc_mvh(struct pm8921_bms_chip *chip, int64_t *val,
565 int *coulumb_counter, int *update_userspace)
566{
567 int rc;
568 int64_t cc_voltage_uv, cc_uvh, cc_mah;
569
570 rc = read_cc(the_chip, coulumb_counter);
571 if (rc) {
572 *coulumb_counter = (last_coulumb_counter < 0) ?
573 DEFAULT_COULUMB_COUNTER : last_coulumb_counter;
574 pr_err("couldn't read coulumb counter err = %d assuming %d\n",
575 rc, *coulumb_counter);
576 *update_userspace = 0;
577 }
578 cc_voltage_uv = (int64_t)*coulumb_counter;
579 cc_voltage_uv = CC_TO_MICROVOLT(cc_voltage_uv);
580 pr_debug("cc_voltage_uv = %lld microvolts\n", cc_voltage_uv);
581 cc_uvh = CCMICROVOLT_TO_UVH(cc_voltage_uv);
582 pr_debug("cc_uvh = %lld micro_volt_hour\n", cc_uvh);
583 cc_mah = div_s64(cc_uvh, chip->r_sense);
584 *val = cc_mah;
585}
586
587static int calculate_state_of_charge(struct pm8921_bms_chip *chip,
588 int batt_temp, int chargecycles)
589{
590 int remaining_usable_charge, fcc, unusable_charge;
591 int remaining_charge, soc, rc, ocv, pc, coulumb_counter;
592 int rbatt, voltage_unusable_uv, pc_unusable;
593 int update_userspace = 1;
594 int64_t cc_mah;
595
596 rbatt = calculate_rbatt(chip);
597 if (rbatt < 0) {
598 rbatt = (last_rbatt < 0) ? DEFAULT_RBATT_MOHMS : last_rbatt;
599 pr_err("failed to read rbatt assuming %d\n",
600 rbatt);
601 update_userspace = 0;
602 }
603 pr_debug("rbatt = %umilliOhms", rbatt);
604
605 fcc = calculate_fcc(chip, batt_temp, chargecycles);
606 if (fcc < -EINVAL) {
607 fcc = (last_fcc < 0) ? chip->fcc : last_fcc;
608 pr_err("failed to read fcc assuming %d\n", fcc);
609 update_userspace = 0;
610 }
611 pr_debug("fcc = %umAh", fcc);
612
613 /* calculate unusable charge */
614 voltage_unusable_uv = (rbatt * chip->i_test)
615 + (chip->v_failure * 1000);
616 pc_unusable = calculate_pc(chip, voltage_unusable_uv,
617 batt_temp, chargecycles);
618 if (pc_unusable < 0) {
619 unusable_charge = (last_unusable_charge < 0) ?
620 DEFAULT_UNUSABLE_CHARGE_MAH : last_unusable_charge;
621 pr_err("unusable_charge failed assuming %d\n", unusable_charge);
622 } else {
623 unusable_charge = (fcc * pc_unusable) / 100;
624 }
625 pr_debug("unusable_charge = %umAh at temp = %d, fcc = %umAh"
626 "unusable_voltage = %umicroVolts pc_unusable = %d\n",
627 unusable_charge, batt_temp, fcc,
628 voltage_unusable_uv, pc_unusable);
629
630 /* calculate remainging charge */
631 rc = read_last_good_ocv(chip, &ocv);
632 if (rc || ocv == 0) {
633 ocv = (last_ocv_uv < 0) ? DEFAULT_OCV_MICROVOLTS : last_ocv_uv;
634 pr_err("ocv failed assuming %d rc = %d\n", ocv, rc);
635 update_userspace = 0;
636 }
637 pc = calculate_pc(chip, ocv, batt_temp, chargecycles);
638 if (pc < 0) {
639 remaining_charge = (last_remaining_charge < 0) ?
640 DEFAULT_REMAINING_CHARGE_MAH : last_remaining_charge;
641 pr_err("calculate remaining charge failed assuming %d\n",
642 remaining_charge);
643 update_userspace = 0;
644 } else {
645 remaining_charge = (fcc * pc) / 100;
646 }
647 pr_debug("remaining_charge = %umAh ocv = %d pc = %d\n",
648 remaining_charge, ocv, pc);
649
650 /* calculate cc milli_volt_hour */
651 calculate_cc_mvh(chip, &cc_mah, &coulumb_counter, &update_userspace);
652 pr_debug("cc_mah = %lldmAh cc = %d\n", cc_mah, coulumb_counter);
653
654 /* calculate remaining usable charge */
655 remaining_usable_charge = remaining_charge - cc_mah - unusable_charge;
656 pr_debug("remaining_usable_charge = %dmAh\n", remaining_usable_charge);
657 if (remaining_usable_charge < 0) {
658 pr_err("bad rem_usb_chg cc_mah %lld, rem_chg %d unusb_chg %d\n",
659 cc_mah, remaining_charge, unusable_charge);
660 update_userspace = 0;
661 }
662
663 soc = (remaining_usable_charge * 100) / (fcc - unusable_charge);
664 if (soc > 100 || soc < 0) {
665 pr_err("bad soc rem_usb_chg %d fcc %d unusb_chg %d\n",
666 remaining_usable_charge, fcc, unusable_charge);
667 update_userspace = 0;
668 }
669 pr_debug("soc = %u%%\n", soc);
670
671 if (update_userspace) {
672 last_rbatt = rbatt;
673 last_fcc = fcc;
674 last_unusable_charge = unusable_charge;
675 last_ocv_uv = ocv;
676 last_remaining_charge = remaining_charge;
677 last_coulumb_counter = coulumb_counter;
678 last_soc = soc;
679 }
680 return soc;
681}
682
683int pm8921_bms_get_percent_charge(void)
684{
685 /* TODO get batt_temp from ADC */
686 int batt_temp = 73;
687
688 return calculate_state_of_charge(the_chip,
689 batt_temp, last_chargecycles);
690}
691EXPORT_SYMBOL_GPL(pm8921_bms_get_percent_charge);
692
693static int start_percent;
694static int end_percent;
695void pm8921_bms_charging_began(void)
696{
697 /* TODO get batt_temp from ADC */
698 int batt_temp = 73;
699
700 start_percent = calculate_state_of_charge(the_chip,
701 batt_temp, last_chargecycles);
702 pr_debug("start_percent = %u%%\n", start_percent);
703}
704EXPORT_SYMBOL_GPL(pm8921_bms_charging_began);
705
706void pm8921_bms_charging_end(void)
707{
708 /* TODO get batt_temp from ADC */
709 int batt_temp = 73;
710
711 end_percent = calculate_state_of_charge(the_chip,
712 batt_temp, last_chargecycles);
713 if (end_percent > start_percent) {
714 last_charge_increase = end_percent - start_percent;
715 if (last_charge_increase > 100) {
716 last_chargecycles++;
717 last_charge_increase = last_charge_increase % 100;
718 }
719 }
720 pr_debug("end_percent = %u%% last_charge_increase = %d"
721 "last_chargecycles = %d\n",
722 end_percent,
723 last_charge_increase,
724 last_chargecycles);
725}
726EXPORT_SYMBOL_GPL(pm8921_bms_charging_end);
727
728static irqreturn_t pm8921_bms_sbi_write_ok_handler(int irq, void *data)
729{
730 return IRQ_HANDLED;
731}
732
733static irqreturn_t pm8921_bms_cc_thr_handler(int irq, void *data)
734{
735 return IRQ_HANDLED;
736}
737static irqreturn_t pm8921_bms_vsense_thr_handler(int irq, void *data)
738{
739 return IRQ_HANDLED;
740}
741static irqreturn_t pm8921_bms_vsense_for_r_handler(int irq, void *data)
742{
743 return IRQ_HANDLED;
744}
745static irqreturn_t pm8921_bms_ocv_for_r_handler(int irq, void *data)
746{
747 return IRQ_HANDLED;
748}
749static irqreturn_t pm8921_bms_good_ocv_handler(int irq, void *data)
750{
751 return IRQ_HANDLED;
752}
753static irqreturn_t pm8921_bms_vsense_avg_handler(int irq, void *data)
754{
755 return IRQ_HANDLED;
756}
757
758struct pm_bms_irq_init_data {
759 unsigned int irq_id;
760 char *name;
761 unsigned long flags;
762 irqreturn_t (*handler)(int, void *);
763};
764
765#define BMS_IRQ(_id, _flags, _handler) \
766{ \
767 .irq_id = _id, \
768 .name = #_id, \
769 .flags = _flags, \
770 .handler = _handler, \
771}
772
773struct pm_bms_irq_init_data bms_irq_data[] = {
774 BMS_IRQ(PM8921_BMS_SBI_WRITE_OK, IRQF_TRIGGER_RISING,
775 pm8921_bms_sbi_write_ok_handler),
776 BMS_IRQ(PM8921_BMS_CC_THR, IRQF_TRIGGER_RISING,
777 pm8921_bms_cc_thr_handler),
778 BMS_IRQ(PM8921_BMS_VSENSE_THR, IRQF_TRIGGER_RISING,
779 pm8921_bms_vsense_thr_handler),
780 BMS_IRQ(PM8921_BMS_VSENSE_FOR_R, IRQF_TRIGGER_RISING,
781 pm8921_bms_vsense_for_r_handler),
782 BMS_IRQ(PM8921_BMS_OCV_FOR_R, IRQF_TRIGGER_RISING,
783 pm8921_bms_ocv_for_r_handler),
784 BMS_IRQ(PM8921_BMS_GOOD_OCV, IRQF_TRIGGER_RISING,
785 pm8921_bms_good_ocv_handler),
786 BMS_IRQ(PM8921_BMS_VSENSE_AVG, IRQF_TRIGGER_RISING,
787 pm8921_bms_vsense_avg_handler),
788};
789
790static void free_irqs(struct pm8921_bms_chip *chip)
791{
792 int i;
793
794 for (i = 0; i < PM_BMS_MAX_INTS; i++)
795 if (chip->pmic_bms_irq[i]) {
796 free_irq(chip->pmic_bms_irq[i], NULL);
797 chip->pmic_bms_irq[i] = 0;
798 }
799}
800
801static int __devinit request_irqs(struct pm8921_bms_chip *chip,
802 struct platform_device *pdev)
803{
804 struct resource *res;
805 int ret, i;
806
807 ret = 0;
808 bitmap_fill(chip->enabled_irqs, PM_BMS_MAX_INTS);
809
810 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
811 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
812 bms_irq_data[i].name);
813 if (res == NULL) {
814 pr_err("couldn't find %s\n", bms_irq_data[i].name);
815 goto err_out;
816 }
817 ret = request_irq(res->start, bms_irq_data[i].handler,
818 bms_irq_data[i].flags,
819 bms_irq_data[i].name, chip);
820 if (ret < 0) {
821 pr_err("couldn't request %d (%s) %d\n", res->start,
822 bms_irq_data[i].name, ret);
823 goto err_out;
824 }
825 chip->pmic_bms_irq[bms_irq_data[i].irq_id] = res->start;
826 pm8921_bms_disable_irq(chip, bms_irq_data[i].irq_id);
827 }
828 return 0;
829
830err_out:
831 free_irqs(chip);
832 return -EINVAL;
833}
834
835#define EN_BMS_BIT BIT(7)
836#define EN_PON_HS_BIT BIT(0)
837static int __devinit pm8921_bms_hw_init(struct pm8921_bms_chip *chip)
838{
839 int rc;
840
841 rc = pm_bms_masked_write(chip, BMS_CONTROL,
842 EN_BMS_BIT | EN_PON_HS_BIT, EN_BMS_BIT | EN_PON_HS_BIT);
843 if (rc) {
844 pr_err("failed to enable pon and bms addr = %d %d",
845 BMS_CONTROL, rc);
846 }
847
848 return 0;
849}
850
851enum {
852 CALC_RBATT,
853 CALC_FCC,
854 CALC_PC,
855 CALC_SOC,
856};
857
858static int test_batt_temp = 5;
859static int test_chargecycle = 150;
860static int test_ocv = 3900000;
861enum {
862 TEST_BATT_TEMP,
863 TEST_CHARGE_CYCLE,
864 TEST_OCV,
865};
866static int get_test_param(void *data, u64 * val)
867{
868 switch ((int)data) {
869 case TEST_BATT_TEMP:
870 *val = test_batt_temp;
871 break;
872 case TEST_CHARGE_CYCLE:
873 *val = test_chargecycle;
874 break;
875 case TEST_OCV:
876 *val = test_ocv;
877 break;
878 default:
879 return -EINVAL;
880 }
881 return 0;
882}
883static int set_test_param(void *data, u64 val)
884{
885 switch ((int)data) {
886 case TEST_BATT_TEMP:
887 test_batt_temp = (int)val;
888 break;
889 case TEST_CHARGE_CYCLE:
890 test_chargecycle = (int)val;
891 break;
892 case TEST_OCV:
893 test_ocv = (int)val;
894 break;
895 default:
896 return -EINVAL;
897 }
898 return 0;
899}
900DEFINE_SIMPLE_ATTRIBUTE(temp_fops, get_test_param, set_test_param, "%llu\n");
901
902static int get_calc(void *data, u64 * val)
903{
904 int param = (int)data;
905 int ret = 0;
906
907 *val = 0;
908
909 /* global irq number passed in via data */
910 switch (param) {
911 case CALC_RBATT:
912 *val = calculate_rbatt(the_chip);
913 break;
914 case CALC_FCC:
915 *val = calculate_fcc(the_chip, test_batt_temp,
916 test_chargecycle);
917 break;
918 case CALC_PC:
919 *val = calculate_pc(the_chip, test_ocv, test_batt_temp,
920 test_chargecycle);
921 break;
922 case CALC_SOC:
923 *val = calculate_state_of_charge(the_chip,
924 test_batt_temp, test_chargecycle);
925 break;
926 default:
927 ret = -EINVAL;
928 }
929 return ret;
930}
931DEFINE_SIMPLE_ATTRIBUTE(calc_fops, get_calc, NULL, "%llu\n");
932
933static int get_reading(void *data, u64 * val)
934{
935 int param = (int)data;
936 int ret = 0;
937
938 *val = 0;
939
940 /* global irq number passed in via data */
941 switch (param) {
942 case CC_MSB:
943 case CC_LSB:
944 read_cc(the_chip, (int *)val);
945 break;
946 case LAST_GOOD_OCV_VALUE:
947 read_last_good_ocv(the_chip, (uint *)val);
948 break;
949 case VBATT_FOR_RBATT:
950 read_vbatt_for_rbatt(the_chip, (uint *)val);
951 break;
952 case VSENSE_FOR_RBATT:
953 read_vsense_for_rbatt(the_chip, (uint *)val);
954 break;
955 case OCV_FOR_RBATT:
956 read_ocv_for_rbatt(the_chip, (uint *)val);
957 break;
958 default:
959 ret = -EINVAL;
960 }
961 return ret;
962}
963DEFINE_SIMPLE_ATTRIBUTE(reading_fops, get_reading, NULL, "%llu\n");
964
965static int get_rt_status(void *data, u64 * val)
966{
967 int i = (int)data;
968 int ret;
969
970 /* global irq number passed in via data */
971 ret = pm_bms_get_rt_status(the_chip, i);
972 *val = ret;
973 return 0;
974}
975DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n");
976
977static int get_reg(void *data, u64 * val)
978{
979 int addr = (int)data;
980 int ret;
981 u8 temp;
982
983 ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp);
984 if (ret) {
985 pr_err("pm8xxx_readb to %x value = %d errored = %d\n",
986 addr, temp, ret);
987 return -EAGAIN;
988 }
989 *val = temp;
990 return 0;
991}
992
993static int set_reg(void *data, u64 val)
994{
995 int addr = (int)data;
996 int ret;
997 u8 temp;
998
999 temp = (u8) val;
1000 ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp);
1001 if (ret) {
1002 pr_err("pm8xxx_writeb to %x value = %d errored = %d\n",
1003 addr, temp, ret);
1004 return -EAGAIN;
1005 }
1006 return 0;
1007}
1008DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
1009
1010static void create_debugfs_entries(struct pm8921_bms_chip *chip)
1011{
1012 int i;
1013
1014 chip->dent = debugfs_create_dir("pm8921_bms", NULL);
1015
1016 if (IS_ERR(chip->dent)) {
1017 pr_err("pmic bms couldnt create debugfs dir\n");
1018 return;
1019 }
1020
1021 debugfs_create_file("BMS_CONTROL", 0644, chip->dent,
1022 (void *)BMS_CONTROL, &reg_fops);
1023 debugfs_create_file("BMS_OUTPUT0", 0644, chip->dent,
1024 (void *)BMS_OUTPUT0, &reg_fops);
1025 debugfs_create_file("BMS_OUTPUT1", 0644, chip->dent,
1026 (void *)BMS_OUTPUT1, &reg_fops);
1027 debugfs_create_file("BMS_TEST1", 0644, chip->dent,
1028 (void *)BMS_TEST1, &reg_fops);
1029 debugfs_create_file("CCADC_ANA_PARAM", 0644, chip->dent,
1030 (void *)CCADC_ANA_PARAM, &reg_fops);
1031 debugfs_create_file("CCADC_DIG_PARAM", 0644, chip->dent,
1032 (void *)CCADC_DIG_PARAM, &reg_fops);
1033 debugfs_create_file("CCADC_RSV", 0644, chip->dent,
1034 (void *)CCADC_RSV, &reg_fops);
1035 debugfs_create_file("CCADC_DATA0", 0644, chip->dent,
1036 (void *)CCADC_DATA0, &reg_fops);
1037 debugfs_create_file("CCADC_DATA1", 0644, chip->dent,
1038 (void *)CCADC_DATA1, &reg_fops);
1039 debugfs_create_file("CCADC_OFFSET_TRIM1", 0644, chip->dent,
1040 (void *)CCADC_OFFSET_TRIM1, &reg_fops);
1041 debugfs_create_file("CCADC_OFFSET_TRIM0", 0644, chip->dent,
1042 (void *)CCADC_OFFSET_TRIM0, &reg_fops);
1043
1044 debugfs_create_file("test_batt_temp", 0644, chip->dent,
1045 (void *)TEST_BATT_TEMP, &temp_fops);
1046 debugfs_create_file("test_chargecycle", 0644, chip->dent,
1047 (void *)TEST_CHARGE_CYCLE, &temp_fops);
1048 debugfs_create_file("test_ocv", 0644, chip->dent,
1049 (void *)TEST_OCV, &temp_fops);
1050
1051 debugfs_create_file("read_cc", 0644, chip->dent,
1052 (void *)CC_MSB, &reading_fops);
1053 debugfs_create_file("read_last_good_ocv", 0644, chip->dent,
1054 (void *)LAST_GOOD_OCV_VALUE, &reading_fops);
1055 debugfs_create_file("read_vbatt_for_rbatt", 0644, chip->dent,
1056 (void *)VBATT_FOR_RBATT, &reading_fops);
1057 debugfs_create_file("read_vsense_for_rbatt", 0644, chip->dent,
1058 (void *)VSENSE_FOR_RBATT, &reading_fops);
1059 debugfs_create_file("read_ocv_for_rbatt", 0644, chip->dent,
1060 (void *)OCV_FOR_RBATT, &reading_fops);
1061
1062 debugfs_create_file("show_rbatt", 0644, chip->dent,
1063 (void *)CALC_RBATT, &calc_fops);
1064 debugfs_create_file("show_fcc", 0644, chip->dent,
1065 (void *)CALC_FCC, &calc_fops);
1066 debugfs_create_file("show_pc", 0644, chip->dent,
1067 (void *)CALC_PC, &calc_fops);
1068 debugfs_create_file("show_soc", 0644, chip->dent,
1069 (void *)CALC_SOC, &calc_fops);
1070
1071 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
1072 if (chip->pmic_bms_irq[bms_irq_data[i].irq_id])
1073 debugfs_create_file(bms_irq_data[i].name, 0444,
1074 chip->dent,
1075 (void *)bms_irq_data[i].irq_id,
1076 &rt_fops);
1077 }
1078}
1079
1080static void calibrate_work(struct work_struct *work)
1081{
1082 /* TODO */
1083}
1084
1085static int __devinit pm8921_bms_probe(struct platform_device *pdev)
1086{
1087 int rc = 0;
1088 struct pm8921_bms_chip *chip;
1089 const struct pm8921_bms_platform_data *pdata
1090 = pdev->dev.platform_data;
1091 if (!pdata) {
1092 pr_err("missing platform data\n");
1093 return -EINVAL;
1094 }
1095
1096 chip = kzalloc(sizeof(struct pm8921_bms_chip),
1097 GFP_KERNEL);
1098 if (!chip) {
1099 pr_err("Cannot allocate pm_bms_chip\n");
1100 return -ENOMEM;
1101 }
1102
1103 chip->dev = &pdev->dev;
1104 chip->r_sense = pdata->r_sense;
1105 chip->i_test = pdata->i_test;
1106 chip->v_failure = pdata->v_failure;
1107 chip->calib_delay_ms = pdata->calib_delay_ms;
1108 chip->fcc = pdata->batt_data->fcc;
1109
1110 chip->fcc_temp_lut = pdata->batt_data->fcc_temp_lut;
1111 chip->fcc_sf_lut = pdata->batt_data->fcc_sf_lut;
1112 chip->pc_temp_ocv_lut = pdata->batt_data->pc_temp_ocv_lut;
1113 chip->pc_sf_lut = pdata->batt_data->pc_sf_lut;
1114
1115 rc = pm8921_bms_hw_init(chip);
1116 if (rc) {
1117 pr_err("couldn't init hardware rc = %d\n", rc);
1118 goto free_chip;
1119 }
1120
1121 rc = request_irqs(chip, pdev);
1122 if (rc) {
1123 pr_err("couldn't register interrupts rc = %d\n", rc);
1124 goto free_chip;
1125 }
1126
1127 platform_set_drvdata(pdev, chip);
1128 the_chip = chip;
1129 create_debugfs_entries(chip);
1130
1131 INIT_DELAYED_WORK(&chip->calib_work, calibrate_work);
1132 schedule_delayed_work(&chip->calib_work,
1133 round_jiffies_relative(msecs_to_jiffies
1134 (chip->calib_delay_ms)));
1135 return 0;
1136
1137free_chip:
1138 kfree(chip);
1139 return rc;
1140}
1141
1142static int __devexit pm8921_bms_remove(struct platform_device *pdev)
1143{
1144 struct pm8921_bms_chip *chip = platform_get_drvdata(pdev);
1145
1146 free_irqs(chip);
1147 platform_set_drvdata(pdev, NULL);
1148 the_chip = NULL;
1149 kfree(chip);
1150 return 0;
1151}
1152
1153static struct platform_driver pm8921_bms_driver = {
1154 .probe = pm8921_bms_probe,
1155 .remove = __devexit_p(pm8921_bms_remove),
1156 .driver = {
1157 .name = PM8921_BMS_DEV_NAME,
1158 .owner = THIS_MODULE,
1159 },
1160};
1161
1162static int __init pm8921_bms_init(void)
1163{
1164 return platform_driver_register(&pm8921_bms_driver);
1165}
1166
1167static void __exit pm8921_bms_exit(void)
1168{
1169 platform_driver_unregister(&pm8921_bms_driver);
1170}
1171
1172late_initcall(pm8921_bms_init);
1173module_exit(pm8921_bms_exit);
1174
1175MODULE_LICENSE("GPL v2");
1176MODULE_DESCRIPTION("PMIC8921 bms driver");
1177MODULE_VERSION("1.0");
1178MODULE_ALIAS("platform:" PM8921_BMS_DEV_NAME);