blob: 7c73c2366409ff11abb1229560cac1732f02e1cf [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
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;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070087 unsigned int pmic_bms_irq[PM_BMS_MAX_INTS];
88 DECLARE_BITMAP(enabled_irqs, PM_BMS_MAX_INTS);
89};
90
91static struct pm8921_bms_chip *the_chip;
92
93#define DEFAULT_RBATT_MOHMS 128
94#define DEFAULT_UNUSABLE_CHARGE_MAH 10
95#define DEFAULT_OCV_MICROVOLTS 3900000
96#define DEFAULT_REMAINING_CHARGE_MAH 990
97#define DEFAULT_COULUMB_COUNTER 0
98#define DEFAULT_CHARGE_CYCLES 0
99
100static int last_rbatt = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101static int last_ocv_uv = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102static int last_soc = -EINVAL;
103
104static int last_chargecycles = DEFAULT_CHARGE_CYCLES;
105static int last_charge_increase;
106
107module_param(last_rbatt, int, 0644);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700108module_param(last_ocv_uv, int, 0644);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109module_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
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700118static void pm8921_bms_enable_irq(struct pm8921_bms_chip *chip, int interrupt)
119{
120 if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) {
121 dev_dbg(chip->dev, "%s %d\n", __func__,
122 chip->pmic_bms_irq[interrupt]);
123 enable_irq(chip->pmic_bms_irq[interrupt]);
124 }
125}
126
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700127static void pm8921_bms_disable_irq(struct pm8921_bms_chip *chip, int interrupt)
128{
129 if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700130 pr_debug("%d\n", chip->pmic_bms_irq[interrupt]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700131 disable_irq_nosync(chip->pmic_bms_irq[interrupt]);
132 }
133}
134
135static int pm_bms_masked_write(struct pm8921_bms_chip *chip, u16 addr,
136 u8 mask, u8 val)
137{
138 int rc;
139 u8 reg;
140
141 rc = pm8xxx_readb(chip->dev->parent, addr, &reg);
142 if (rc) {
143 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
144 return rc;
145 }
146 reg &= ~mask;
147 reg |= val & mask;
148 rc = pm8xxx_writeb(chip->dev->parent, addr, reg);
149 if (rc) {
150 pr_err("write failed addr = %03X, rc = %d\n", addr, rc);
151 return rc;
152 }
153 return 0;
154}
155
156#define SELECT_OUTPUT_DATA 0x1C
157#define SELECT_OUTPUT_TYPE_SHIFT 2
158#define OCV_FOR_RBATT 0x0
159#define VSENSE_FOR_RBATT 0x1
160#define VBATT_FOR_RBATT 0x2
161#define CC_MSB 0x3
162#define CC_LSB 0x4
163#define LAST_GOOD_OCV_VALUE 0x5
164#define VSENSE_AVG 0x6
165#define VBATT_AVG 0x7
166
167static int pm_bms_read_output_data(struct pm8921_bms_chip *chip, int type,
168 int16_t *result)
169{
170 int rc;
171 u8 reg;
172
173 if (!result) {
174 pr_err("result pointer null\n");
175 return -EINVAL;
176 }
177 *result = 0;
178 if (type < OCV_FOR_RBATT || type > VBATT_AVG) {
179 pr_err("invalid type %d asked to read\n", type);
180 return -EINVAL;
181 }
182 rc = pm_bms_masked_write(chip, BMS_CONTROL, SELECT_OUTPUT_DATA,
183 type << SELECT_OUTPUT_TYPE_SHIFT);
184 if (rc) {
185 pr_err("fail to select %d type in BMS_CONTROL rc = %d\n",
186 type, rc);
187 return rc;
188 }
189
190 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT0, &reg);
191 if (rc) {
192 pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n",
193 type, rc);
194 return rc;
195 }
196 *result = reg;
197 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT1, &reg);
198 if (rc) {
199 pr_err("fail to read BMS_OUTPUT1 for type %d rc = %d\n",
200 type, rc);
201 return rc;
202 }
203 *result |= reg << 8;
204 pr_debug("type %d result %x", type, *result);
205 return 0;
206}
207
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700208#define V_PER_BIT_MUL_FACTOR 293
209#define INTRINSIC_OFFSET 0x6000
210static int vbatt_to_microvolt(unsigned int a)
211{
212 if (a <= INTRINSIC_OFFSET)
213 return 0;
214
215 return (a - INTRINSIC_OFFSET) * V_PER_BIT_MUL_FACTOR;
216}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700217
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700218#define XOADC_CALIB_UV 625000
219static int adjust_xo_reading(struct pm8921_bms_chip *chip, unsigned int uv)
220{
221 u64 numerator = ((u64)uv - chip->xoadc_v0625) * XOADC_CALIB_UV;
222 u64 denominator = chip->xoadc_v125 - chip->xoadc_v0625;
223
224 if (denominator == 0)
225 return uv;
226 return XOADC_CALIB_UV + div_u64(numerator, denominator);
227}
228
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700229#define CC_RESOLUTION_N_V1 1085069
230#define CC_RESOLUTION_D_V1 100000
231#define CC_RESOLUTION_N_V2 868056
232#define CC_RESOLUTION_D_V2 10000
233static s64 cc_to_microvolt_v1(s64 cc)
234{
235 return div_s64(cc * CC_RESOLUTION_N_V1, CC_RESOLUTION_D_V1);
236}
237
238static s64 cc_to_microvolt_v2(s64 cc)
239{
240 return div_s64(cc * CC_RESOLUTION_N_V2, CC_RESOLUTION_D_V2);
241}
242
243static s64 cc_to_microvolt(struct pm8921_bms_chip *chip, s64 cc)
244{
245 /*
246 * resolution (the value of a single bit) was changed after revision 2.0
247 * for more accurate readings
248 */
249 return (chip->revision < PM8XXX_REVISION_8901_2p0) ?
250 cc_to_microvolt_v1((s64)cc) :
251 cc_to_microvolt_v2((s64)cc);
252}
253
254#define CC_READING_TICKS 55
255#define SLEEP_CLK_HZ 32768
256#define SECONDS_PER_HOUR 3600
257static s64 ccmicrovolt_to_uvh(s64 cc_uv)
258{
259 return div_s64(cc_uv * CC_READING_TICKS,
260 SLEEP_CLK_HZ * SECONDS_PER_HOUR);
261}
262
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263/* returns the signed value read from the hardware */
264static int read_cc(struct pm8921_bms_chip *chip, int *result)
265{
266 int rc;
267 uint16_t msw, lsw;
268
269 rc = pm_bms_read_output_data(chip, CC_LSB, &lsw);
270 if (rc) {
271 pr_err("fail to read CC_LSB rc = %d\n", rc);
272 return rc;
273 }
274 rc = pm_bms_read_output_data(chip, CC_MSB, &msw);
275 if (rc) {
276 pr_err("fail to read CC_MSB rc = %d\n", rc);
277 return rc;
278 }
279 *result = msw << 16 | lsw;
280 pr_debug("msw = %04x lsw = %04x cc = %d\n", msw, lsw, *result);
281 return 0;
282}
283
284static int read_last_good_ocv(struct pm8921_bms_chip *chip, uint *result)
285{
286 int rc;
287 uint16_t reading;
288
289 rc = pm_bms_read_output_data(chip, LAST_GOOD_OCV_VALUE, &reading);
290 if (rc) {
291 pr_err("fail to read LAST_GOOD_OCV_VALUE rc = %d\n", rc);
292 return rc;
293 }
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700294 *result = vbatt_to_microvolt(reading);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700295 pr_debug("raw = %04x ocv_microV = %u\n", reading, *result);
296 return 0;
297}
298
299static int read_vbatt_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
300{
301 int rc;
302 uint16_t reading;
303
304 rc = pm_bms_read_output_data(chip, VBATT_FOR_RBATT, &reading);
305 if (rc) {
306 pr_err("fail to read VBATT_FOR_RBATT rc = %d\n", rc);
307 return rc;
308 }
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700309 *result = vbatt_to_microvolt(reading);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310 pr_debug("raw = %04x vbatt_for_r_microV = %u\n", reading, *result);
311 return 0;
312}
313
314static int read_vsense_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
315{
316 int rc;
317 uint16_t reading;
318
319 rc = pm_bms_read_output_data(chip, VSENSE_FOR_RBATT, &reading);
320 if (rc) {
321 pr_err("fail to read VSENSE_FOR_RBATT rc = %d\n", rc);
322 return rc;
323 }
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700324 *result = cc_to_microvolt(chip, reading);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700325 pr_debug("raw = %04x vsense_for_r_microV = %u\n", reading, *result);
326 return 0;
327}
328
329static int read_ocv_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
330{
331 int rc;
332 uint16_t reading;
333
334 rc = pm_bms_read_output_data(chip, OCV_FOR_RBATT, &reading);
335 if (rc) {
336 pr_err("fail to read OCV_FOR_RBATT rc = %d\n", rc);
337 return rc;
338 }
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700339 *result = vbatt_to_microvolt(reading);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700340 pr_debug("read = %04x ocv_for_r_microV = %u\n", reading, *result);
341 return 0;
342}
343
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700344static int read_vsense_avg(struct pm8921_bms_chip *chip, int *result)
345{
346 int rc;
347 int16_t reading;
348
349 rc = pm_bms_read_output_data(chip, VSENSE_AVG, &reading);
350 if (rc) {
351 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
352 return rc;
353 }
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700354 *result = cc_to_microvolt(chip, reading);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700355 pr_debug("read = %04x vsense = %d\n", reading, *result);
356 return 0;
357}
358
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700359static int linear_interpolate(int y0, int x0, int y1, int x1, int x)
360{
361 if (y0 == y1 || x == x0)
362 return y0;
363 if (x1 == x0 || x == x1)
364 return y1;
365
366 return y0 + ((y1 - y0) * (x - x0) / (x1 - x0));
367}
368
369static int interpolate_single_lut(struct single_row_lut *lut, int x)
370{
371 int i, result;
372
373 if (x < lut->x[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700374 pr_debug("x %d less than known range return y = %d lut = %pS\n",
375 x, lut->y[0], lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700376 return lut->y[0];
377 }
378 if (x > lut->x[lut->cols - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700379 pr_debug("x %d more than known range return y = %d lut = %pS\n",
380 x, lut->y[lut->cols - 1], lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381 return lut->y[lut->cols - 1];
382 }
383
384 for (i = 0; i < lut->cols; i++)
385 if (x <= lut->x[i])
386 break;
387 if (x == lut->x[i]) {
388 result = lut->y[i];
389 } else {
390 result = linear_interpolate(
391 lut->y[i - 1],
392 lut->x[i - 1],
393 lut->y[i],
394 lut->x[i],
395 x);
396 }
397 return result;
398}
399
400static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp)
401{
402 return interpolate_single_lut(chip->fcc_temp_lut, batt_temp);
403}
404
405static int interpolate_scalingfactor_fcc(struct pm8921_bms_chip *chip,
406 int cycles)
407{
408 return interpolate_single_lut(chip->fcc_sf_lut, cycles);
409}
410
411static int interpolate_scalingfactor_pc(struct pm8921_bms_chip *chip,
412 int cycles, int pc)
413{
414 int i, scalefactorrow1, scalefactorrow2, scalefactor, row1, row2;
415 int rows = chip->pc_sf_lut->rows;
416 int cols = chip->pc_sf_lut->cols;
417
418 if (pc > chip->pc_sf_lut->percent[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700419 pr_debug("pc %d greater than known pc ranges for sfd\n", pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700420 row1 = 0;
421 row2 = 0;
422 }
423 if (pc < chip->pc_sf_lut->percent[rows - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700424 pr_debug("pc %d less than known pc ranges for sf", pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425 row1 = rows - 1;
426 row2 = rows - 1;
427 }
428 for (i = 0; i < rows; i++) {
429 if (pc == chip->pc_sf_lut->percent[i]) {
430 row1 = i;
431 row2 = i;
432 break;
433 }
434 if (pc > chip->pc_sf_lut->percent[i]) {
435 row1 = i - 1;
436 row2 = i;
437 break;
438 }
439 }
440
441 if (cycles < chip->pc_sf_lut->cycles[0])
442 cycles = chip->pc_sf_lut->cycles[0];
443 if (cycles > chip->pc_sf_lut->cycles[cols - 1])
444 cycles = chip->pc_sf_lut->cycles[cols - 1];
445
446 for (i = 0; i < cols; i++)
447 if (cycles <= chip->pc_sf_lut->cycles[i])
448 break;
449 if (cycles == chip->pc_sf_lut->cycles[i]) {
450 scalefactor = linear_interpolate(
451 chip->pc_sf_lut->sf[row1][i],
452 chip->pc_sf_lut->percent[row1],
453 chip->pc_sf_lut->sf[row2][i],
454 chip->pc_sf_lut->percent[row2],
455 pc);
456 return scalefactor;
457 }
458
459 scalefactorrow1 = linear_interpolate(
460 chip->pc_sf_lut->sf[row1][i - 1],
461 chip->pc_sf_lut->cycles[i - 1],
462 chip->pc_sf_lut->sf[row1][i],
463 chip->pc_sf_lut->cycles[i],
464 cycles);
465
466 scalefactorrow2 = linear_interpolate(
467 chip->pc_sf_lut->sf[row2][i - 1],
468 chip->pc_sf_lut->cycles[i - 1],
469 chip->pc_sf_lut->sf[row2][i],
470 chip->pc_sf_lut->cycles[i],
471 cycles);
472
473 scalefactor = linear_interpolate(
474 scalefactorrow1,
475 chip->pc_sf_lut->percent[row1],
476 scalefactorrow2,
477 chip->pc_sf_lut->percent[row2],
478 pc);
479
480 return scalefactor;
481}
482
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700483static int is_between(int left, int right, int value)
484{
485 if (left >= right && left >= value && value >= right)
486 return 1;
487 if (left <= right && left <= value && value <= right)
488 return 1;
489
490 return 0;
491}
492
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493static int interpolate_pc(struct pm8921_bms_chip *chip,
494 int batt_temp, int ocv)
495{
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700496 int i, j, pcj, pcj_minus_one, pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497 int rows = chip->pc_temp_ocv_lut->rows;
498 int cols = chip->pc_temp_ocv_lut->cols;
499
500 if (batt_temp < chip->pc_temp_ocv_lut->temp[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700501 pr_debug("batt_temp %d < known temp range for pc\n", batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502 batt_temp = chip->pc_temp_ocv_lut->temp[0];
503 }
504 if (batt_temp > chip->pc_temp_ocv_lut->temp[cols - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700505 pr_debug("batt_temp %d > known temp range for pc\n", batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506 batt_temp = chip->pc_temp_ocv_lut->temp[cols - 1];
507 }
508
509 for (j = 0; j < cols; j++)
510 if (batt_temp <= chip->pc_temp_ocv_lut->temp[j])
511 break;
512 if (batt_temp == chip->pc_temp_ocv_lut->temp[j]) {
513 /* found an exact match for temp in the table */
514 if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j])
515 return chip->pc_temp_ocv_lut->percent[0];
516 if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j])
517 return chip->pc_temp_ocv_lut->percent[rows - 1];
518 for (i = 0; i < rows; i++) {
519 if (ocv >= chip->pc_temp_ocv_lut->ocv[i][j]) {
520 if (ocv == chip->pc_temp_ocv_lut->ocv[i][j])
521 return
522 chip->pc_temp_ocv_lut->percent[i];
523 pc = linear_interpolate(
524 chip->pc_temp_ocv_lut->percent[i],
525 chip->pc_temp_ocv_lut->ocv[i][j],
526 chip->pc_temp_ocv_lut->percent[i - 1],
527 chip->pc_temp_ocv_lut->ocv[i - 1][j],
528 ocv);
529 return pc;
530 }
531 }
532 }
533
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700534 /*
535 * batt_temp is within temperature for
536 * column j-1 and j
537 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538 if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j])
539 return chip->pc_temp_ocv_lut->percent[0];
540 if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j - 1])
541 return chip->pc_temp_ocv_lut->percent[rows - 1];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700543 pcj_minus_one = 0;
544 pcj = 0;
545 for (i = 0; i < rows-1; i++) {
546 if (pcj == 0
547 && is_between(chip->pc_temp_ocv_lut->ocv[i][j],
548 chip->pc_temp_ocv_lut->ocv[i+1][j], ocv)) {
549 pcj = linear_interpolate(
550 chip->pc_temp_ocv_lut->percent[i],
551 chip->pc_temp_ocv_lut->ocv[i][j],
552 chip->pc_temp_ocv_lut->percent[i + 1],
553 chip->pc_temp_ocv_lut->ocv[i+1][j],
554 ocv);
555 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700557 if (pcj_minus_one == 0
558 && is_between(chip->pc_temp_ocv_lut->ocv[i][j-1],
559 chip->pc_temp_ocv_lut->ocv[i+1][j-1], ocv)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700561 pcj_minus_one = linear_interpolate(
562 chip->pc_temp_ocv_lut->percent[i],
563 chip->pc_temp_ocv_lut->ocv[i][j-1],
564 chip->pc_temp_ocv_lut->percent[i + 1],
565 chip->pc_temp_ocv_lut->ocv[i+1][j-1],
566 ocv);
567 }
568
569 if (pcj && pcj_minus_one) {
570 pc = linear_interpolate(
571 pcj_minus_one,
572 chip->pc_temp_ocv_lut->temp[j-1],
573 pcj,
574 chip->pc_temp_ocv_lut->temp[j],
575 batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576 return pc;
577 }
578 }
579
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700580 if (pcj)
581 return pcj;
582
583 if (pcj_minus_one)
584 return pcj_minus_one;
585
586 pr_debug("%d ocv wasn't found for temp %d in the LUT returning 100%%",
587 ocv, batt_temp);
588 return 100;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589}
590
591static int calculate_rbatt(struct pm8921_bms_chip *chip)
592{
593 int rc;
594 unsigned int ocv, vsense, vbatt, r_batt;
595
596 rc = read_ocv_for_rbatt(chip, &ocv);
597 if (rc) {
598 pr_err("fail to read ocv_for_rbatt rc = %d\n", rc);
599 ocv = 0;
600 }
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700601 ocv = adjust_xo_reading(chip, ocv);
602
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603 rc = read_vbatt_for_rbatt(chip, &vbatt);
604 if (rc) {
605 pr_err("fail to read vbatt_for_rbatt rc = %d\n", rc);
606 ocv = 0;
607 }
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700608 vbatt = adjust_xo_reading(chip, vbatt);
609
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700610 rc = read_vsense_for_rbatt(chip, &vsense);
611 if (rc) {
612 pr_err("fail to read vsense_for_rbatt rc = %d\n", rc);
613 ocv = 0;
614 }
615 if (ocv == 0
616 || ocv == vbatt
617 || vsense == 0) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700618 pr_debug("rbatt readings unavailable ocv = %d, vbatt = %d,"
619 "vsen = %d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700620 ocv, vbatt, vsense);
621 return -EINVAL;
622 }
623 r_batt = ((ocv - vbatt) * chip->r_sense) / vsense;
624 pr_debug("r_batt = %umilliOhms", r_batt);
625 return r_batt;
626}
627
628static int calculate_fcc(struct pm8921_bms_chip *chip, int batt_temp,
629 int chargecycles)
630{
631 int initfcc, result, scalefactor = 0;
632
633 initfcc = interpolate_fcc(chip, batt_temp);
634 pr_debug("intfcc = %umAh batt_temp = %d\n", initfcc, batt_temp);
635
636 scalefactor = interpolate_scalingfactor_fcc(chip, chargecycles);
637 pr_debug("scalefactor = %d batt_temp = %d\n", scalefactor, batt_temp);
638
639 /* Multiply the initial FCC value by the scale factor. */
640 result = (initfcc * scalefactor) / 100;
641 pr_debug("fcc mAh = %d\n", result);
642 return result;
643}
644
645static int calculate_pc(struct pm8921_bms_chip *chip, int ocv_uv, int batt_temp,
646 int chargecycles)
647{
648 int pc, scalefactor;
649
650 pc = interpolate_pc(chip, batt_temp, ocv_uv / 1000);
651 pr_debug("pc = %u for ocv = %dmicroVolts batt_temp = %d\n",
652 pc, ocv_uv, batt_temp);
653
654 scalefactor = interpolate_scalingfactor_pc(chip, chargecycles, pc);
655 pr_debug("scalefactor = %u batt_temp = %d\n", scalefactor, batt_temp);
656
657 /* Multiply the initial FCC value by the scale factor. */
658 pc = (pc * scalefactor) / 100;
659 return pc;
660}
661
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700662static void calculate_cc_mah(struct pm8921_bms_chip *chip, int64_t *val,
Abhijeet Dharmapurikar52aa9682011-07-20 14:05:47 -0700663 int *coulumb_counter)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664{
665 int rc;
666 int64_t cc_voltage_uv, cc_uvh, cc_mah;
667
668 rc = read_cc(the_chip, coulumb_counter);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700669 cc_voltage_uv = (int64_t)*coulumb_counter;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700670 cc_voltage_uv = cc_to_microvolt(chip, cc_voltage_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700671 pr_debug("cc_voltage_uv = %lld microvolts\n", cc_voltage_uv);
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700672 cc_uvh = ccmicrovolt_to_uvh(cc_voltage_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700673 pr_debug("cc_uvh = %lld micro_volt_hour\n", cc_uvh);
674 cc_mah = div_s64(cc_uvh, chip->r_sense);
675 *val = cc_mah;
676}
677
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700678static int get_battery_uvolts(struct pm8921_bms_chip *chip, int *uvolts)
679{
680 int rc;
681 struct pm8921_adc_chan_result result;
682
683 rc = pm8921_adc_read(chip->vbat_channel, &result);
684 if (rc) {
685 pr_err("error reading adc channel = %d, rc = %d\n",
686 chip->vbat_channel, rc);
687 return rc;
688 }
689 pr_debug("mvolts phy = %lld meas = 0x%llx", result.physical,
690 result.measurement);
691 *uvolts = (int)result.physical;
692 *uvolts = *uvolts * 1000;
693 return 0;
694}
695
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700696static int calculate_unusable_charge_mah(struct pm8921_bms_chip *chip,
697 int fcc, int batt_temp, int chargecycles)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700698{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700699 int rbatt, voltage_unusable_uv, pc_unusable;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700
701 rbatt = calculate_rbatt(chip);
702 if (rbatt < 0) {
703 rbatt = (last_rbatt < 0) ? DEFAULT_RBATT_MOHMS : last_rbatt;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700704 pr_debug("rbatt unavailable assuming %d\n", rbatt);
705 } else {
706 last_rbatt = rbatt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700707 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708
709 /* calculate unusable charge */
710 voltage_unusable_uv = (rbatt * chip->i_test)
711 + (chip->v_failure * 1000);
712 pc_unusable = calculate_pc(chip, voltage_unusable_uv,
713 batt_temp, chargecycles);
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700714 pr_debug("rbatt = %umilliOhms unusable_v =%d unusable_pc = %d\n",
715 rbatt, voltage_unusable_uv, pc_unusable);
716 return (fcc * pc_unusable) / 100;
717}
Abhijeet Dharmapurikar52aa9682011-07-20 14:05:47 -0700718
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700719/* calculate remainging charge at the time of ocv */
720static int calculate_remaining_charge_mah(struct pm8921_bms_chip *chip, int fcc,
721 int batt_temp, int chargecycles)
722{
723 int rc, ocv, pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700724
725 /* calculate remainging charge */
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -0700726 ocv = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700727 rc = read_last_good_ocv(chip, &ocv);
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -0700728 if (rc)
729 pr_debug("failed to read ocv rc = %d\n", rc);
730
731 if (ocv == 0) {
732 ocv = last_ocv_uv;
733 pr_debug("ocv not available using last_ocv_uv=%d\n", ocv);
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700734 } else {
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -0700735 /* update the usespace param since a good ocv is available */
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700736 last_ocv_uv = ocv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700737 }
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700738
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700739 pc = calculate_pc(chip, ocv, batt_temp, chargecycles);
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700740 pr_debug("ocv = %d pc = %d\n", ocv, pc);
741 return (fcc * pc) / 100;
742}
743
744static int calculate_state_of_charge(struct pm8921_bms_chip *chip,
745 int batt_temp, int chargecycles)
746{
747 int remaining_usable_charge, fcc, unusable_charge;
748 int remaining_charge, soc, coulumb_counter;
749 int update_userspace = 1;
750 int64_t cc_mah;
751
752 fcc = calculate_fcc(chip, batt_temp, chargecycles);
753 pr_debug("FCC = %umAh batt_temp = %d, cycles = %d",
754 fcc, batt_temp, chargecycles);
755
756 unusable_charge = calculate_unusable_charge_mah(chip, fcc,
757 batt_temp, chargecycles);
758
759 pr_debug("UUC = %umAh", unusable_charge);
760
761 /* calculate remainging charge */
762 remaining_charge = calculate_remaining_charge_mah(chip, fcc, batt_temp,
763 chargecycles);
764 pr_debug("RC = %umAh\n", remaining_charge);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765
766 /* calculate cc milli_volt_hour */
Abhijeet Dharmapurikar52aa9682011-07-20 14:05:47 -0700767 calculate_cc_mah(chip, &cc_mah, &coulumb_counter);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700768 pr_debug("cc_mah = %lldmAh cc = %d\n", cc_mah, coulumb_counter);
769
770 /* calculate remaining usable charge */
771 remaining_usable_charge = remaining_charge - cc_mah - unusable_charge;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700772 pr_debug("RUC = %dmAh\n", remaining_usable_charge);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700773 if (remaining_usable_charge < 0) {
774 pr_err("bad rem_usb_chg cc_mah %lld, rem_chg %d unusb_chg %d\n",
775 cc_mah, remaining_charge, unusable_charge);
776 update_userspace = 0;
777 }
778
779 soc = (remaining_usable_charge * 100) / (fcc - unusable_charge);
780 if (soc > 100 || soc < 0) {
781 pr_err("bad soc rem_usb_chg %d fcc %d unusb_chg %d\n",
782 remaining_usable_charge, fcc, unusable_charge);
783 update_userspace = 0;
784 }
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700785 pr_debug("SOC = %u%%\n", soc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700786
787 if (update_userspace) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788 last_soc = soc;
789 }
790 return soc;
791}
792
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700793#define XOADC_MAX_1P25V 1312500
794#define XOADC_MIN_1P25V 1187500
795#define XOADC_MAX_0P625V 656250
796#define XOADC_MIN_0P625V 593750
797
798#define HKADC_V_PER_BIT_MUL_FACTOR 977
799#define HKADC_V_PER_BIT_DIV_FACTOR 10
800static int calib_hkadc_convert_microvolt(unsigned int phy)
801{
802 return phy * HKADC_V_PER_BIT_MUL_FACTOR / HKADC_V_PER_BIT_DIV_FACTOR;
803}
804
805static void calib_hkadc(struct pm8921_bms_chip *chip)
806{
807 int voltage, rc;
808 struct pm8921_adc_chan_result result;
809
810 rc = pm8921_adc_read(the_chip->ref1p25v_channel, &result);
811 if (rc) {
812 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
813 return;
814 }
815 voltage = calib_hkadc_convert_microvolt(result.physical);
816
817 pr_debug("result 1.25v = 0x%llx, voltage = %dmV adc_meas = %lld\n",
818 result.physical, voltage, result.measurement);
819
820 /* check for valid range */
821 if (voltage > XOADC_MAX_1P25V)
822 voltage = XOADC_MAX_1P25V;
823 else if (voltage < XOADC_MIN_1P25V)
824 voltage = XOADC_MIN_1P25V;
825 chip->xoadc_v125 = voltage;
826
827 rc = pm8921_adc_read(the_chip->ref625mv_channel, &result);
828 if (rc) {
829 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
830 return;
831 }
832 voltage = calib_hkadc_convert_microvolt(result.physical);
833 pr_debug("result 0.625V = 0x%llx, voltage = %dmV adc_mead = %lld\n",
834 result.physical, voltage, result.measurement);
835 /* check for valid range */
836 if (voltage > XOADC_MAX_0P625V)
837 voltage = XOADC_MAX_0P625V;
838 else if (voltage < XOADC_MIN_0P625V)
839 voltage = XOADC_MIN_0P625V;
840
841 chip->xoadc_v0625 = voltage;
842}
843
844static void calibrate_hkadc_work(struct work_struct *work)
845{
846 struct pm8921_bms_chip *chip = container_of(work,
847 struct pm8921_bms_chip, calib_hkadc_work);
848
849 calib_hkadc(chip);
850}
851
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700852int pm8921_bms_get_vsense_avg(int *result)
853{
854 if (the_chip)
855 return read_vsense_avg(the_chip, result);
856
857 pr_err("called before initialization\n");
858 return -EINVAL;
859}
860EXPORT_SYMBOL(pm8921_bms_get_vsense_avg);
861
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -0700862int pm8921_bms_get_battery_current(int *result)
863{
864 if (!the_chip) {
865 pr_err("called before initialization\n");
866 return -EINVAL;
867 }
868 if (the_chip->r_sense == 0) {
869 pr_err("r_sense is zero\n");
870 return -EINVAL;
871 }
872
873 read_vsense_avg(the_chip, result);
874 pr_debug("vsense=%d\n", *result);
875 *result = *result / the_chip->r_sense;
876 return 0;
877}
878EXPORT_SYMBOL(pm8921_bms_get_battery_current);
879
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880int pm8921_bms_get_percent_charge(void)
881{
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700882 int batt_temp, rc;
883 struct pm8921_adc_chan_result result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700884
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700885 if (!the_chip) {
886 pr_err("called before initialization\n");
887 return -EINVAL;
888 }
889
890 rc = pm8921_adc_read(the_chip->batt_temp_channel, &result);
891 if (rc) {
892 pr_err("error reading adc channel = %d, rc = %d\n",
893 the_chip->batt_temp_channel, rc);
894 return rc;
895 }
896 pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical,
897 result.measurement);
898 batt_temp = (int)result.physical;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700899 return calculate_state_of_charge(the_chip,
900 batt_temp, last_chargecycles);
901}
902EXPORT_SYMBOL_GPL(pm8921_bms_get_percent_charge);
903
904static int start_percent;
905static int end_percent;
906void pm8921_bms_charging_began(void)
907{
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700908 start_percent = pm8921_bms_get_percent_charge();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909 pr_debug("start_percent = %u%%\n", start_percent);
910}
911EXPORT_SYMBOL_GPL(pm8921_bms_charging_began);
912
913void pm8921_bms_charging_end(void)
914{
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700915 end_percent = pm8921_bms_get_percent_charge();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700916 if (end_percent > start_percent) {
917 last_charge_increase = end_percent - start_percent;
918 if (last_charge_increase > 100) {
919 last_chargecycles++;
920 last_charge_increase = last_charge_increase % 100;
921 }
922 }
923 pr_debug("end_percent = %u%% last_charge_increase = %d"
924 "last_chargecycles = %d\n",
925 end_percent,
926 last_charge_increase,
927 last_chargecycles);
928}
929EXPORT_SYMBOL_GPL(pm8921_bms_charging_end);
930
931static irqreturn_t pm8921_bms_sbi_write_ok_handler(int irq, void *data)
932{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700933 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700934 return IRQ_HANDLED;
935}
936
937static irqreturn_t pm8921_bms_cc_thr_handler(int irq, void *data)
938{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700939 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700940 return IRQ_HANDLED;
941}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700942
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700943static irqreturn_t pm8921_bms_vsense_thr_handler(int irq, void *data)
944{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700945 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946 return IRQ_HANDLED;
947}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700948
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700949static irqreturn_t pm8921_bms_vsense_for_r_handler(int irq, void *data)
950{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700951 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952 return IRQ_HANDLED;
953}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700954
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700955static irqreturn_t pm8921_bms_ocv_for_r_handler(int irq, void *data)
956{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700957 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700958
959 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700960 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700961 return IRQ_HANDLED;
962}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700963
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700964static irqreturn_t pm8921_bms_good_ocv_handler(int irq, void *data)
965{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700966 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700967
968 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700969 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700970 return IRQ_HANDLED;
971}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700972
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700973static irqreturn_t pm8921_bms_vsense_avg_handler(int irq, void *data)
974{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700975 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700976 return IRQ_HANDLED;
977}
978
979struct pm_bms_irq_init_data {
980 unsigned int irq_id;
981 char *name;
982 unsigned long flags;
983 irqreturn_t (*handler)(int, void *);
984};
985
986#define BMS_IRQ(_id, _flags, _handler) \
987{ \
988 .irq_id = _id, \
989 .name = #_id, \
990 .flags = _flags, \
991 .handler = _handler, \
992}
993
994struct pm_bms_irq_init_data bms_irq_data[] = {
995 BMS_IRQ(PM8921_BMS_SBI_WRITE_OK, IRQF_TRIGGER_RISING,
996 pm8921_bms_sbi_write_ok_handler),
997 BMS_IRQ(PM8921_BMS_CC_THR, IRQF_TRIGGER_RISING,
998 pm8921_bms_cc_thr_handler),
999 BMS_IRQ(PM8921_BMS_VSENSE_THR, IRQF_TRIGGER_RISING,
1000 pm8921_bms_vsense_thr_handler),
1001 BMS_IRQ(PM8921_BMS_VSENSE_FOR_R, IRQF_TRIGGER_RISING,
1002 pm8921_bms_vsense_for_r_handler),
1003 BMS_IRQ(PM8921_BMS_OCV_FOR_R, IRQF_TRIGGER_RISING,
1004 pm8921_bms_ocv_for_r_handler),
1005 BMS_IRQ(PM8921_BMS_GOOD_OCV, IRQF_TRIGGER_RISING,
1006 pm8921_bms_good_ocv_handler),
1007 BMS_IRQ(PM8921_BMS_VSENSE_AVG, IRQF_TRIGGER_RISING,
1008 pm8921_bms_vsense_avg_handler),
1009};
1010
1011static void free_irqs(struct pm8921_bms_chip *chip)
1012{
1013 int i;
1014
1015 for (i = 0; i < PM_BMS_MAX_INTS; i++)
1016 if (chip->pmic_bms_irq[i]) {
1017 free_irq(chip->pmic_bms_irq[i], NULL);
1018 chip->pmic_bms_irq[i] = 0;
1019 }
1020}
1021
1022static int __devinit request_irqs(struct pm8921_bms_chip *chip,
1023 struct platform_device *pdev)
1024{
1025 struct resource *res;
1026 int ret, i;
1027
1028 ret = 0;
1029 bitmap_fill(chip->enabled_irqs, PM_BMS_MAX_INTS);
1030
1031 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
1032 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1033 bms_irq_data[i].name);
1034 if (res == NULL) {
1035 pr_err("couldn't find %s\n", bms_irq_data[i].name);
1036 goto err_out;
1037 }
1038 ret = request_irq(res->start, bms_irq_data[i].handler,
1039 bms_irq_data[i].flags,
1040 bms_irq_data[i].name, chip);
1041 if (ret < 0) {
1042 pr_err("couldn't request %d (%s) %d\n", res->start,
1043 bms_irq_data[i].name, ret);
1044 goto err_out;
1045 }
1046 chip->pmic_bms_irq[bms_irq_data[i].irq_id] = res->start;
1047 pm8921_bms_disable_irq(chip, bms_irq_data[i].irq_id);
1048 }
1049 return 0;
1050
1051err_out:
1052 free_irqs(chip);
1053 return -EINVAL;
1054}
1055
1056#define EN_BMS_BIT BIT(7)
1057#define EN_PON_HS_BIT BIT(0)
1058static int __devinit pm8921_bms_hw_init(struct pm8921_bms_chip *chip)
1059{
1060 int rc;
1061
1062 rc = pm_bms_masked_write(chip, BMS_CONTROL,
1063 EN_BMS_BIT | EN_PON_HS_BIT, EN_BMS_BIT | EN_PON_HS_BIT);
1064 if (rc) {
1065 pr_err("failed to enable pon and bms addr = %d %d",
1066 BMS_CONTROL, rc);
1067 }
1068
1069 return 0;
1070}
1071
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001072static void check_initial_ocv(struct pm8921_bms_chip *chip)
1073{
1074 int ocv, vbatt, rbatt, ibatt, rc;
1075
1076 /*
1077 * Check if a last_good_ocv is available,
1078 * if not compute it here at boot time.
1079 */
1080 rc = read_last_good_ocv(chip, &ocv);
1081 if (rc || ocv == 0) {
1082 rc = get_battery_uvolts(chip, &vbatt);
1083 if (rc) {
1084 pr_err("failed to read vbatt from adc rc = %d\n", rc);
1085 last_ocv_uv = DEFAULT_OCV_MICROVOLTS;
1086 return;
1087 }
1088
1089 rc = pm8921_bms_get_battery_current(&ibatt);
1090 if (rc) {
1091 pr_err("failed to read batt current rc = %d\n", rc);
1092 last_ocv_uv = DEFAULT_OCV_MICROVOLTS;
1093 return;
1094 }
1095
1096 rbatt = calculate_rbatt(the_chip);
1097 if (rbatt < 0)
1098 rbatt = DEFAULT_RBATT_MOHMS;
1099 last_ocv_uv = vbatt + ibatt * rbatt;
1100 }
1101 pr_debug("ocv = %d last_ocv_uv = %d\n", ocv, last_ocv_uv);
1102}
1103
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001104enum {
1105 CALC_RBATT,
1106 CALC_FCC,
1107 CALC_PC,
1108 CALC_SOC,
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001109 CALIB_HKADC,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001110};
1111
1112static int test_batt_temp = 5;
1113static int test_chargecycle = 150;
1114static int test_ocv = 3900000;
1115enum {
1116 TEST_BATT_TEMP,
1117 TEST_CHARGE_CYCLE,
1118 TEST_OCV,
1119};
1120static int get_test_param(void *data, u64 * val)
1121{
1122 switch ((int)data) {
1123 case TEST_BATT_TEMP:
1124 *val = test_batt_temp;
1125 break;
1126 case TEST_CHARGE_CYCLE:
1127 *val = test_chargecycle;
1128 break;
1129 case TEST_OCV:
1130 *val = test_ocv;
1131 break;
1132 default:
1133 return -EINVAL;
1134 }
1135 return 0;
1136}
1137static int set_test_param(void *data, u64 val)
1138{
1139 switch ((int)data) {
1140 case TEST_BATT_TEMP:
1141 test_batt_temp = (int)val;
1142 break;
1143 case TEST_CHARGE_CYCLE:
1144 test_chargecycle = (int)val;
1145 break;
1146 case TEST_OCV:
1147 test_ocv = (int)val;
1148 break;
1149 default:
1150 return -EINVAL;
1151 }
1152 return 0;
1153}
1154DEFINE_SIMPLE_ATTRIBUTE(temp_fops, get_test_param, set_test_param, "%llu\n");
1155
1156static int get_calc(void *data, u64 * val)
1157{
1158 int param = (int)data;
1159 int ret = 0;
1160
1161 *val = 0;
1162
1163 /* global irq number passed in via data */
1164 switch (param) {
1165 case CALC_RBATT:
1166 *val = calculate_rbatt(the_chip);
1167 break;
1168 case CALC_FCC:
1169 *val = calculate_fcc(the_chip, test_batt_temp,
1170 test_chargecycle);
1171 break;
1172 case CALC_PC:
1173 *val = calculate_pc(the_chip, test_ocv, test_batt_temp,
1174 test_chargecycle);
1175 break;
1176 case CALC_SOC:
1177 *val = calculate_state_of_charge(the_chip,
1178 test_batt_temp, test_chargecycle);
1179 break;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001180 case CALIB_HKADC:
1181 /* reading this will trigger calibration */
1182 *val = 0;
1183 calib_hkadc(the_chip);
1184 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001185 default:
1186 ret = -EINVAL;
1187 }
1188 return ret;
1189}
1190DEFINE_SIMPLE_ATTRIBUTE(calc_fops, get_calc, NULL, "%llu\n");
1191
1192static int get_reading(void *data, u64 * val)
1193{
1194 int param = (int)data;
1195 int ret = 0;
1196
1197 *val = 0;
1198
1199 /* global irq number passed in via data */
1200 switch (param) {
1201 case CC_MSB:
1202 case CC_LSB:
1203 read_cc(the_chip, (int *)val);
1204 break;
1205 case LAST_GOOD_OCV_VALUE:
1206 read_last_good_ocv(the_chip, (uint *)val);
1207 break;
1208 case VBATT_FOR_RBATT:
1209 read_vbatt_for_rbatt(the_chip, (uint *)val);
1210 break;
1211 case VSENSE_FOR_RBATT:
1212 read_vsense_for_rbatt(the_chip, (uint *)val);
1213 break;
1214 case OCV_FOR_RBATT:
1215 read_ocv_for_rbatt(the_chip, (uint *)val);
1216 break;
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001217 case VSENSE_AVG:
1218 read_vsense_avg(the_chip, (uint *)val);
1219 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001220 default:
1221 ret = -EINVAL;
1222 }
1223 return ret;
1224}
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001225DEFINE_SIMPLE_ATTRIBUTE(reading_fops, get_reading, NULL, "%lld\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001226
1227static int get_rt_status(void *data, u64 * val)
1228{
1229 int i = (int)data;
1230 int ret;
1231
1232 /* global irq number passed in via data */
1233 ret = pm_bms_get_rt_status(the_chip, i);
1234 *val = ret;
1235 return 0;
1236}
1237DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n");
1238
1239static int get_reg(void *data, u64 * val)
1240{
1241 int addr = (int)data;
1242 int ret;
1243 u8 temp;
1244
1245 ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp);
1246 if (ret) {
1247 pr_err("pm8xxx_readb to %x value = %d errored = %d\n",
1248 addr, temp, ret);
1249 return -EAGAIN;
1250 }
1251 *val = temp;
1252 return 0;
1253}
1254
1255static int set_reg(void *data, u64 val)
1256{
1257 int addr = (int)data;
1258 int ret;
1259 u8 temp;
1260
1261 temp = (u8) val;
1262 ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp);
1263 if (ret) {
1264 pr_err("pm8xxx_writeb to %x value = %d errored = %d\n",
1265 addr, temp, ret);
1266 return -EAGAIN;
1267 }
1268 return 0;
1269}
1270DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
1271
1272static void create_debugfs_entries(struct pm8921_bms_chip *chip)
1273{
1274 int i;
1275
1276 chip->dent = debugfs_create_dir("pm8921_bms", NULL);
1277
1278 if (IS_ERR(chip->dent)) {
1279 pr_err("pmic bms couldnt create debugfs dir\n");
1280 return;
1281 }
1282
1283 debugfs_create_file("BMS_CONTROL", 0644, chip->dent,
1284 (void *)BMS_CONTROL, &reg_fops);
1285 debugfs_create_file("BMS_OUTPUT0", 0644, chip->dent,
1286 (void *)BMS_OUTPUT0, &reg_fops);
1287 debugfs_create_file("BMS_OUTPUT1", 0644, chip->dent,
1288 (void *)BMS_OUTPUT1, &reg_fops);
1289 debugfs_create_file("BMS_TEST1", 0644, chip->dent,
1290 (void *)BMS_TEST1, &reg_fops);
1291 debugfs_create_file("CCADC_ANA_PARAM", 0644, chip->dent,
1292 (void *)CCADC_ANA_PARAM, &reg_fops);
1293 debugfs_create_file("CCADC_DIG_PARAM", 0644, chip->dent,
1294 (void *)CCADC_DIG_PARAM, &reg_fops);
1295 debugfs_create_file("CCADC_RSV", 0644, chip->dent,
1296 (void *)CCADC_RSV, &reg_fops);
1297 debugfs_create_file("CCADC_DATA0", 0644, chip->dent,
1298 (void *)CCADC_DATA0, &reg_fops);
1299 debugfs_create_file("CCADC_DATA1", 0644, chip->dent,
1300 (void *)CCADC_DATA1, &reg_fops);
1301 debugfs_create_file("CCADC_OFFSET_TRIM1", 0644, chip->dent,
1302 (void *)CCADC_OFFSET_TRIM1, &reg_fops);
1303 debugfs_create_file("CCADC_OFFSET_TRIM0", 0644, chip->dent,
1304 (void *)CCADC_OFFSET_TRIM0, &reg_fops);
1305
1306 debugfs_create_file("test_batt_temp", 0644, chip->dent,
1307 (void *)TEST_BATT_TEMP, &temp_fops);
1308 debugfs_create_file("test_chargecycle", 0644, chip->dent,
1309 (void *)TEST_CHARGE_CYCLE, &temp_fops);
1310 debugfs_create_file("test_ocv", 0644, chip->dent,
1311 (void *)TEST_OCV, &temp_fops);
1312
1313 debugfs_create_file("read_cc", 0644, chip->dent,
1314 (void *)CC_MSB, &reading_fops);
1315 debugfs_create_file("read_last_good_ocv", 0644, chip->dent,
1316 (void *)LAST_GOOD_OCV_VALUE, &reading_fops);
1317 debugfs_create_file("read_vbatt_for_rbatt", 0644, chip->dent,
1318 (void *)VBATT_FOR_RBATT, &reading_fops);
1319 debugfs_create_file("read_vsense_for_rbatt", 0644, chip->dent,
1320 (void *)VSENSE_FOR_RBATT, &reading_fops);
1321 debugfs_create_file("read_ocv_for_rbatt", 0644, chip->dent,
1322 (void *)OCV_FOR_RBATT, &reading_fops);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001323 debugfs_create_file("read_vsense_avg", 0644, chip->dent,
1324 (void *)VSENSE_AVG, &reading_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001325
1326 debugfs_create_file("show_rbatt", 0644, chip->dent,
1327 (void *)CALC_RBATT, &calc_fops);
1328 debugfs_create_file("show_fcc", 0644, chip->dent,
1329 (void *)CALC_FCC, &calc_fops);
1330 debugfs_create_file("show_pc", 0644, chip->dent,
1331 (void *)CALC_PC, &calc_fops);
1332 debugfs_create_file("show_soc", 0644, chip->dent,
1333 (void *)CALC_SOC, &calc_fops);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001334 debugfs_create_file("calib_hkadc", 0644, chip->dent,
1335 (void *)CALIB_HKADC, &calc_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001336
1337 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
1338 if (chip->pmic_bms_irq[bms_irq_data[i].irq_id])
1339 debugfs_create_file(bms_irq_data[i].name, 0444,
1340 chip->dent,
1341 (void *)bms_irq_data[i].irq_id,
1342 &rt_fops);
1343 }
1344}
1345
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001346static int __devinit pm8921_bms_probe(struct platform_device *pdev)
1347{
1348 int rc = 0;
1349 struct pm8921_bms_chip *chip;
1350 const struct pm8921_bms_platform_data *pdata
1351 = pdev->dev.platform_data;
1352 if (!pdata) {
1353 pr_err("missing platform data\n");
1354 return -EINVAL;
1355 }
1356
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001357 chip = kzalloc(sizeof(struct pm8921_bms_chip), GFP_KERNEL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001358 if (!chip) {
1359 pr_err("Cannot allocate pm_bms_chip\n");
1360 return -ENOMEM;
1361 }
1362
1363 chip->dev = &pdev->dev;
1364 chip->r_sense = pdata->r_sense;
1365 chip->i_test = pdata->i_test;
1366 chip->v_failure = pdata->v_failure;
1367 chip->calib_delay_ms = pdata->calib_delay_ms;
1368 chip->fcc = pdata->batt_data->fcc;
1369
1370 chip->fcc_temp_lut = pdata->batt_data->fcc_temp_lut;
1371 chip->fcc_sf_lut = pdata->batt_data->fcc_sf_lut;
1372 chip->pc_temp_ocv_lut = pdata->batt_data->pc_temp_ocv_lut;
1373 chip->pc_sf_lut = pdata->batt_data->pc_sf_lut;
1374
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001375 chip->batt_temp_channel = pdata->bms_cdata.batt_temp_channel;
1376 chip->vbat_channel = pdata->bms_cdata.vbat_channel;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001377 chip->ref625mv_channel = pdata->bms_cdata.ref625mv_channel;
1378 chip->ref1p25v_channel = pdata->bms_cdata.ref1p25v_channel;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -07001379 chip->revision = pm8xxx_get_revision(chip->dev->parent);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001380 INIT_WORK(&chip->calib_hkadc_work, calibrate_hkadc_work);
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001381
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001382 rc = pm8921_bms_hw_init(chip);
1383 if (rc) {
1384 pr_err("couldn't init hardware rc = %d\n", rc);
1385 goto free_chip;
1386 }
1387
1388 rc = request_irqs(chip, pdev);
1389 if (rc) {
1390 pr_err("couldn't register interrupts rc = %d\n", rc);
1391 goto free_chip;
1392 }
1393
1394 platform_set_drvdata(pdev, chip);
1395 the_chip = chip;
1396 create_debugfs_entries(chip);
1397
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001398 check_initial_ocv(chip);
1399
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001400 /* enable the vbatt reading interrupts for scheduling hkadc calib */
1401 pm8921_bms_enable_irq(chip, PM8921_BMS_GOOD_OCV);
1402 pm8921_bms_enable_irq(chip, PM8921_BMS_OCV_FOR_R);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001403 return 0;
1404
1405free_chip:
1406 kfree(chip);
1407 return rc;
1408}
1409
1410static int __devexit pm8921_bms_remove(struct platform_device *pdev)
1411{
1412 struct pm8921_bms_chip *chip = platform_get_drvdata(pdev);
1413
1414 free_irqs(chip);
1415 platform_set_drvdata(pdev, NULL);
1416 the_chip = NULL;
1417 kfree(chip);
1418 return 0;
1419}
1420
1421static struct platform_driver pm8921_bms_driver = {
1422 .probe = pm8921_bms_probe,
1423 .remove = __devexit_p(pm8921_bms_remove),
1424 .driver = {
1425 .name = PM8921_BMS_DEV_NAME,
1426 .owner = THIS_MODULE,
1427 },
1428};
1429
1430static int __init pm8921_bms_init(void)
1431{
1432 return platform_driver_register(&pm8921_bms_driver);
1433}
1434
1435static void __exit pm8921_bms_exit(void)
1436{
1437 platform_driver_unregister(&pm8921_bms_driver);
1438}
1439
1440late_initcall(pm8921_bms_init);
1441module_exit(pm8921_bms_exit);
1442
1443MODULE_LICENSE("GPL v2");
1444MODULE_DESCRIPTION("PMIC8921 bms driver");
1445MODULE_VERSION("1.0");
1446MODULE_ALIAS("platform:" PM8921_BMS_DEV_NAME);