blob: 0bc2a71712cde37a339bd3eee217376a841a9250 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13#define pr_fmt(fmt) "%s: " fmt, __func__
14
15#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/platform_device.h>
18#include <linux/errno.h>
19#include <linux/mfd/pm8xxx/pm8921-bms.h>
20#include <linux/mfd/pm8xxx/core.h>
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -070021#include <linux/mfd/pm8xxx/pm8xxx-adc.h>
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -080022#include <linux/mfd/pm8xxx/ccadc.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070023#include <linux/interrupt.h>
24#include <linux/bitops.h>
25#include <linux/debugfs.h>
26#include <linux/slab.h>
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070027#include <linux/delay.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028
29#define BMS_CONTROL 0x224
30#define BMS_OUTPUT0 0x230
31#define BMS_OUTPUT1 0x231
32#define BMS_TEST1 0x237
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070034#define ADC_ARB_SECP_CNTRL 0x190
35#define ADC_ARB_SECP_AMUX_CNTRL 0x191
36#define ADC_ARB_SECP_ANA_PARAM 0x192
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070037#define ADC_ARB_SECP_DIG_PARAM 0x193
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070038#define ADC_ARB_SECP_RSV 0x194
39#define ADC_ARB_SECP_DATA1 0x195
40#define ADC_ARB_SECP_DATA0 0x196
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070041
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070042#define ADC_ARB_BMS_CNTRL 0x18D
43
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044enum pmic_bms_interrupts {
45 PM8921_BMS_SBI_WRITE_OK,
46 PM8921_BMS_CC_THR,
47 PM8921_BMS_VSENSE_THR,
48 PM8921_BMS_VSENSE_FOR_R,
49 PM8921_BMS_OCV_FOR_R,
50 PM8921_BMS_GOOD_OCV,
51 PM8921_BMS_VSENSE_AVG,
52 PM_BMS_MAX_INTS,
53};
54
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055struct pm8921_bms_chip {
56 struct device *dev;
57 struct dentry *dent;
58 unsigned int r_sense;
59 unsigned int i_test;
60 unsigned int v_failure;
61 unsigned int fcc;
62 struct single_row_lut *fcc_temp_lut;
63 struct single_row_lut *fcc_sf_lut;
64 struct pc_temp_ocv_lut *pc_temp_ocv_lut;
65 struct pc_sf_lut *pc_sf_lut;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070066 struct work_struct calib_hkadc_work;
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070067 struct delayed_work calib_ccadc_work;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068 unsigned int calib_delay_ms;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -070069 unsigned int revision;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070070 unsigned int xoadc_v0625;
71 unsigned int xoadc_v125;
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -070072 unsigned int batt_temp_channel;
73 unsigned int vbat_channel;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070074 unsigned int ref625mv_channel;
75 unsigned int ref1p25v_channel;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -070076 unsigned int batt_id_channel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070077 unsigned int pmic_bms_irq[PM_BMS_MAX_INTS];
78 DECLARE_BITMAP(enabled_irqs, PM_BMS_MAX_INTS);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -070079 spinlock_t bms_output_lock;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -070080 struct single_row_lut *adjusted_fcc_temp_lut;
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -070081 unsigned int charging_began;
82 unsigned int start_percent;
83 unsigned int end_percent;
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -080084
85 uint16_t ocv_reading_at_100;
86 int cc_reading_at_100;
Abhijeet Dharmapurikara93ede82011-11-17 12:20:03 -080087 int max_voltage_uv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070088};
89
90static struct pm8921_bms_chip *the_chip;
91
92#define DEFAULT_RBATT_MOHMS 128
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070093#define DEFAULT_OCV_MICROVOLTS 3900000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070094#define DEFAULT_CHARGE_CYCLES 0
95
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -070096static int last_chargecycles = DEFAULT_CHARGE_CYCLES;
97static int last_charge_increase;
98module_param(last_chargecycles, int, 0644);
99module_param(last_charge_increase, int, 0644);
100
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101static int last_rbatt = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102static int last_ocv_uv = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700103static int last_soc = -EINVAL;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700104static int last_real_fcc = -EINVAL;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700105static int last_real_fcc_batt_temp = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700107static int bms_ops_set(const char *val, const struct kernel_param *kp)
108{
109 if (*(int *)kp->arg == -EINVAL)
110 return param_set_int(val, kp);
111 else
112 return 0;
113}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700115static struct kernel_param_ops bms_param_ops = {
116 .set = bms_ops_set,
117 .get = param_get_int,
118};
119
120module_param_cb(last_rbatt, &bms_param_ops, &last_rbatt, 0644);
121module_param_cb(last_ocv_uv, &bms_param_ops, &last_ocv_uv, 0644);
122module_param_cb(last_soc, &bms_param_ops, &last_soc, 0644);
123
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -0800124/*
125 * bms_fake_battery is write only, this value is set in setups where a
126 * battery emulator is used instead of a real battery. This makes the
127 * bms driver report a higher value of charge regardless of the calculated
128 * state of charge.
129 */
130static int bms_fake_battery;
131module_param(bms_fake_battery, int, 0644);
132
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700133static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp);
134static void readjust_fcc_table(void)
135{
136 struct single_row_lut *temp, *old;
137 int i, fcc, ratio;
138
139 if (!the_chip->fcc_temp_lut) {
140 pr_err("The static fcc lut table is NULL\n");
141 return;
142 }
143
144 temp = kzalloc(sizeof(struct single_row_lut), GFP_KERNEL);
145 if (!temp) {
146 pr_err("Cannot allocate memory for adjusted fcc table\n");
147 return;
148 }
149
150 fcc = interpolate_fcc(the_chip, last_real_fcc_batt_temp);
151
152 temp->cols = the_chip->fcc_temp_lut->cols;
153 for (i = 0; i < the_chip->fcc_temp_lut->cols; i++) {
154 temp->x[i] = the_chip->fcc_temp_lut->x[i];
155 ratio = div_u64(the_chip->fcc_temp_lut->y[i] * 1000, fcc);
156 temp->y[i] = (ratio * last_real_fcc);
157 temp->y[i] /= 1000;
158 pr_debug("temp=%d, staticfcc=%d, adjfcc=%d, ratio=%d\n",
159 temp->x[i], the_chip->fcc_temp_lut->y[i],
160 temp->y[i], ratio);
161 }
162
163 old = the_chip->adjusted_fcc_temp_lut;
164 the_chip->adjusted_fcc_temp_lut = temp;
165 kfree(old);
166}
167
168static int bms_last_real_fcc_set(const char *val,
169 const struct kernel_param *kp)
170{
171 int rc = 0;
172
173 if (last_real_fcc == -EINVAL)
174 rc = param_set_int(val, kp);
175 if (rc) {
176 pr_err("Failed to set last_real_fcc rc=%d\n", rc);
177 return rc;
178 }
179 if (last_real_fcc_batt_temp != -EINVAL)
180 readjust_fcc_table();
181 return rc;
182}
183static struct kernel_param_ops bms_last_real_fcc_param_ops = {
184 .set = bms_last_real_fcc_set,
185 .get = param_get_int,
186};
187module_param_cb(last_real_fcc, &bms_last_real_fcc_param_ops,
188 &last_real_fcc, 0644);
189
190static int bms_last_real_fcc_batt_temp_set(const char *val,
191 const struct kernel_param *kp)
192{
193 int rc = 0;
194
195 if (last_real_fcc_batt_temp == -EINVAL)
196 rc = param_set_int(val, kp);
197 if (rc) {
198 pr_err("Failed to set last_real_fcc_batt_temp rc=%d\n", rc);
199 return rc;
200 }
201 if (last_real_fcc != -EINVAL)
202 readjust_fcc_table();
203 return rc;
204}
205
206static struct kernel_param_ops bms_last_real_fcc_batt_temp_param_ops = {
207 .set = bms_last_real_fcc_batt_temp_set,
208 .get = param_get_int,
209};
210module_param_cb(last_real_fcc_batt_temp, &bms_last_real_fcc_batt_temp_param_ops,
211 &last_real_fcc_batt_temp, 0644);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212
213static int pm_bms_get_rt_status(struct pm8921_bms_chip *chip, int irq_id)
214{
215 return pm8xxx_read_irq_stat(chip->dev->parent,
216 chip->pmic_bms_irq[irq_id]);
217}
218
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700219static void pm8921_bms_enable_irq(struct pm8921_bms_chip *chip, int interrupt)
220{
221 if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) {
222 dev_dbg(chip->dev, "%s %d\n", __func__,
223 chip->pmic_bms_irq[interrupt]);
224 enable_irq(chip->pmic_bms_irq[interrupt]);
225 }
226}
227
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228static void pm8921_bms_disable_irq(struct pm8921_bms_chip *chip, int interrupt)
229{
230 if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700231 pr_debug("%d\n", chip->pmic_bms_irq[interrupt]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700232 disable_irq_nosync(chip->pmic_bms_irq[interrupt]);
233 }
234}
235
236static int pm_bms_masked_write(struct pm8921_bms_chip *chip, u16 addr,
237 u8 mask, u8 val)
238{
239 int rc;
240 u8 reg;
241
242 rc = pm8xxx_readb(chip->dev->parent, addr, &reg);
243 if (rc) {
244 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
245 return rc;
246 }
247 reg &= ~mask;
248 reg |= val & mask;
249 rc = pm8xxx_writeb(chip->dev->parent, addr, reg);
250 if (rc) {
251 pr_err("write failed addr = %03X, rc = %d\n", addr, rc);
252 return rc;
253 }
254 return 0;
255}
256
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700257#define HOLD_OREG_DATA BIT(1)
258static int pm_bms_lock_output_data(struct pm8921_bms_chip *chip)
259{
260 int rc;
261
262 rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA,
263 HOLD_OREG_DATA);
264 if (rc) {
265 pr_err("couldnt lock bms output rc = %d\n", rc);
266 return rc;
267 }
268 return 0;
269}
270
271static int pm_bms_unlock_output_data(struct pm8921_bms_chip *chip)
272{
273 int rc;
274
275 rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA, 0);
276 if (rc) {
277 pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc);
278 return rc;
279 }
280 return 0;
281}
282
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700283#define SELECT_OUTPUT_DATA 0x1C
284#define SELECT_OUTPUT_TYPE_SHIFT 2
285#define OCV_FOR_RBATT 0x0
286#define VSENSE_FOR_RBATT 0x1
287#define VBATT_FOR_RBATT 0x2
288#define CC_MSB 0x3
289#define CC_LSB 0x4
290#define LAST_GOOD_OCV_VALUE 0x5
291#define VSENSE_AVG 0x6
292#define VBATT_AVG 0x7
293
294static int pm_bms_read_output_data(struct pm8921_bms_chip *chip, int type,
295 int16_t *result)
296{
297 int rc;
298 u8 reg;
299
300 if (!result) {
301 pr_err("result pointer null\n");
302 return -EINVAL;
303 }
304 *result = 0;
305 if (type < OCV_FOR_RBATT || type > VBATT_AVG) {
306 pr_err("invalid type %d asked to read\n", type);
307 return -EINVAL;
308 }
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700309
310 /* make sure the bms registers are locked */
311 rc = pm8xxx_readb(chip->dev->parent, BMS_CONTROL, &reg);
312 if (rc) {
313 pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n",
314 type, rc);
315 return rc;
316 }
317
318 /* Output register data must be held (locked) while reading output */
319 WARN_ON(!(reg && HOLD_OREG_DATA));
320
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700321 rc = pm_bms_masked_write(chip, BMS_CONTROL, SELECT_OUTPUT_DATA,
322 type << SELECT_OUTPUT_TYPE_SHIFT);
323 if (rc) {
324 pr_err("fail to select %d type in BMS_CONTROL rc = %d\n",
325 type, rc);
326 return rc;
327 }
328
329 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT0, &reg);
330 if (rc) {
331 pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n",
332 type, rc);
333 return rc;
334 }
335 *result = reg;
336 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT1, &reg);
337 if (rc) {
338 pr_err("fail to read BMS_OUTPUT1 for type %d rc = %d\n",
339 type, rc);
340 return rc;
341 }
342 *result |= reg << 8;
343 pr_debug("type %d result %x", type, *result);
344 return 0;
345}
346
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700347#define V_PER_BIT_MUL_FACTOR 97656
348#define V_PER_BIT_DIV_FACTOR 1000
349#define XOADC_INTRINSIC_OFFSET 0x6000
350static int xoadc_reading_to_microvolt(unsigned int a)
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700351{
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700352 if (a <= XOADC_INTRINSIC_OFFSET)
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700353 return 0;
354
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700355 return (a - XOADC_INTRINSIC_OFFSET)
356 * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR;
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700357}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700358
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700359#define XOADC_CALIB_UV 625000
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700360#define VBATT_MUL_FACTOR 3
361static int adjust_xo_vbatt_reading(struct pm8921_bms_chip *chip,
362 unsigned int uv)
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700363{
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700364 u64 numerator, denominator;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700365
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700366 if (uv == 0)
367 return 0;
368
369 numerator = ((u64)uv - chip->xoadc_v0625) * XOADC_CALIB_UV;
370 denominator = chip->xoadc_v125 - chip->xoadc_v0625;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700371 if (denominator == 0)
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700372 return uv * VBATT_MUL_FACTOR;
373 return (XOADC_CALIB_UV + div_u64(numerator, denominator))
374 * VBATT_MUL_FACTOR;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700375}
376
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700377#define CC_RESOLUTION_N_V1 1085069
378#define CC_RESOLUTION_D_V1 100000
379#define CC_RESOLUTION_N_V2 868056
380#define CC_RESOLUTION_D_V2 10000
381static s64 cc_to_microvolt_v1(s64 cc)
382{
383 return div_s64(cc * CC_RESOLUTION_N_V1, CC_RESOLUTION_D_V1);
384}
385
386static s64 cc_to_microvolt_v2(s64 cc)
387{
388 return div_s64(cc * CC_RESOLUTION_N_V2, CC_RESOLUTION_D_V2);
389}
390
391static s64 cc_to_microvolt(struct pm8921_bms_chip *chip, s64 cc)
392{
393 /*
394 * resolution (the value of a single bit) was changed after revision 2.0
395 * for more accurate readings
396 */
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -0700397 return (chip->revision < PM8XXX_REVISION_8921_2p0) ?
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700398 cc_to_microvolt_v1((s64)cc) :
399 cc_to_microvolt_v2((s64)cc);
400}
401
402#define CC_READING_TICKS 55
403#define SLEEP_CLK_HZ 32768
404#define SECONDS_PER_HOUR 3600
405static s64 ccmicrovolt_to_uvh(s64 cc_uv)
406{
407 return div_s64(cc_uv * CC_READING_TICKS,
408 SLEEP_CLK_HZ * SECONDS_PER_HOUR);
409}
410
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700411/* returns the signed value read from the hardware */
412static int read_cc(struct pm8921_bms_chip *chip, int *result)
413{
414 int rc;
415 uint16_t msw, lsw;
416
417 rc = pm_bms_read_output_data(chip, CC_LSB, &lsw);
418 if (rc) {
419 pr_err("fail to read CC_LSB rc = %d\n", rc);
420 return rc;
421 }
422 rc = pm_bms_read_output_data(chip, CC_MSB, &msw);
423 if (rc) {
424 pr_err("fail to read CC_MSB rc = %d\n", rc);
425 return rc;
426 }
427 *result = msw << 16 | lsw;
428 pr_debug("msw = %04x lsw = %04x cc = %d\n", msw, lsw, *result);
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -0800429 *result = *result - chip->cc_reading_at_100;
430 pr_debug("cc = %d after subtracting %d\n",
431 *result, chip->cc_reading_at_100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700432 return 0;
433}
434
435static int read_last_good_ocv(struct pm8921_bms_chip *chip, uint *result)
436{
437 int rc;
438 uint16_t reading;
439
440 rc = pm_bms_read_output_data(chip, LAST_GOOD_OCV_VALUE, &reading);
441 if (rc) {
442 pr_err("fail to read LAST_GOOD_OCV_VALUE rc = %d\n", rc);
443 return rc;
444 }
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -0800445
446 if (chip->ocv_reading_at_100 != reading) {
447 chip->ocv_reading_at_100 = 0;
448 chip->cc_reading_at_100 = 0;
449 *result = xoadc_reading_to_microvolt(reading);
450 pr_debug("raw = %04x ocv_uV = %u\n", reading, *result);
451 *result = adjust_xo_vbatt_reading(chip, *result);
452 pr_debug("after adj ocv_uV = %u\n", *result);
453 if (*result != 0)
454 last_ocv_uv = *result;
455 } else {
456 /*
Abhijeet Dharmapurikara93ede82011-11-17 12:20:03 -0800457 * force 100% ocv by selecting the highest voltage the
458 * battery could every reach
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -0800459 */
Abhijeet Dharmapurikara93ede82011-11-17 12:20:03 -0800460 *result = chip->max_voltage_uv;
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -0800461 }
462
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700463 return 0;
464}
465
466static int read_vbatt_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
467{
468 int rc;
469 uint16_t reading;
470
471 rc = pm_bms_read_output_data(chip, VBATT_FOR_RBATT, &reading);
472 if (rc) {
473 pr_err("fail to read VBATT_FOR_RBATT rc = %d\n", rc);
474 return rc;
475 }
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700476 *result = xoadc_reading_to_microvolt(reading);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477 pr_debug("raw = %04x vbatt_for_r_microV = %u\n", reading, *result);
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700478 *result = adjust_xo_vbatt_reading(chip, *result);
479 pr_debug("after adj vbatt_for_r_uV = %u\n", *result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700480 return 0;
481}
482
483static int read_vsense_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
484{
485 int rc;
486 uint16_t reading;
487
488 rc = pm_bms_read_output_data(chip, VSENSE_FOR_RBATT, &reading);
489 if (rc) {
490 pr_err("fail to read VSENSE_FOR_RBATT rc = %d\n", rc);
491 return rc;
492 }
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800493 *result = pm8xxx_ccadc_reading_to_microvolt(chip->revision, reading);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -0700494 pr_debug("raw = %04x vsense_for_r_uV = %u\n", reading, *result);
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800495 *result = pm8xxx_cc_adjust_for_gain(*result);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -0700496 pr_debug("after adj vsense_for_r_uV = %u\n", *result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497 return 0;
498}
499
500static int read_ocv_for_rbatt(struct pm8921_bms_chip *chip, uint *result)
501{
502 int rc;
503 uint16_t reading;
504
505 rc = pm_bms_read_output_data(chip, OCV_FOR_RBATT, &reading);
506 if (rc) {
507 pr_err("fail to read OCV_FOR_RBATT rc = %d\n", rc);
508 return rc;
509 }
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700510 *result = xoadc_reading_to_microvolt(reading);
511 pr_debug("raw = %04x ocv_for_r_uV = %u\n", reading, *result);
512 *result = adjust_xo_vbatt_reading(chip, *result);
513 pr_debug("after adj ocv_for_r_uV = %u\n", *result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514 return 0;
515}
516
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700517static int read_vsense_avg(struct pm8921_bms_chip *chip, int *result)
518{
519 int rc;
520 int16_t reading;
521
522 rc = pm_bms_read_output_data(chip, VSENSE_AVG, &reading);
523 if (rc) {
524 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
525 return rc;
526 }
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800527 *result = pm8xxx_ccadc_reading_to_microvolt(the_chip->revision,
528 reading);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -0700529 pr_debug("raw = %04x vsense = %d\n", reading, *result);
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800530 *result = pm8xxx_cc_adjust_for_gain((s64)*result);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -0700531 pr_debug("after adj vsense = %d\n", *result);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700532 return 0;
533}
534
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535static int linear_interpolate(int y0, int x0, int y1, int x1, int x)
536{
537 if (y0 == y1 || x == x0)
538 return y0;
539 if (x1 == x0 || x == x1)
540 return y1;
541
542 return y0 + ((y1 - y0) * (x - x0) / (x1 - x0));
543}
544
545static int interpolate_single_lut(struct single_row_lut *lut, int x)
546{
547 int i, result;
548
549 if (x < lut->x[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700550 pr_debug("x %d less than known range return y = %d lut = %pS\n",
551 x, lut->y[0], lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552 return lut->y[0];
553 }
554 if (x > lut->x[lut->cols - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700555 pr_debug("x %d more than known range return y = %d lut = %pS\n",
556 x, lut->y[lut->cols - 1], lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557 return lut->y[lut->cols - 1];
558 }
559
560 for (i = 0; i < lut->cols; i++)
561 if (x <= lut->x[i])
562 break;
563 if (x == lut->x[i]) {
564 result = lut->y[i];
565 } else {
566 result = linear_interpolate(
567 lut->y[i - 1],
568 lut->x[i - 1],
569 lut->y[i],
570 lut->x[i],
571 x);
572 }
573 return result;
574}
575
576static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp)
577{
578 return interpolate_single_lut(chip->fcc_temp_lut, batt_temp);
579}
580
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700581static int interpolate_fcc_adjusted(struct pm8921_bms_chip *chip, int batt_temp)
582{
583 return interpolate_single_lut(chip->adjusted_fcc_temp_lut, batt_temp);
584}
585
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700586static int interpolate_scalingfactor_fcc(struct pm8921_bms_chip *chip,
587 int cycles)
588{
Abhijeet Dharmapurikar205bbc02011-11-04 14:47:36 -0700589 /*
590 * sf table could be null when no battery aging data is available, in
591 * that case return 100%
592 */
593 if (chip->fcc_sf_lut)
594 return interpolate_single_lut(chip->fcc_sf_lut, cycles);
595 else
596 return 100;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597}
598
599static int interpolate_scalingfactor_pc(struct pm8921_bms_chip *chip,
600 int cycles, int pc)
601{
Abhijeet Dharmapurikar4a4456e2011-09-27 15:10:38 -0700602 int i, scalefactorrow1, scalefactorrow2, scalefactor;
Abhijeet Dharmapurikar205bbc02011-11-04 14:47:36 -0700603 int rows, cols;
Abhijeet Dharmapurikar4a4456e2011-09-27 15:10:38 -0700604 int row1 = 0;
605 int row2 = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700606
Abhijeet Dharmapurikar205bbc02011-11-04 14:47:36 -0700607 /*
608 * sf table could be null when no battery aging data is available, in
609 * that case return 100%
610 */
611 if (!chip->pc_sf_lut)
612 return 100;
613
614 rows = chip->pc_sf_lut->rows;
615 cols = chip->pc_sf_lut->cols;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616 if (pc > chip->pc_sf_lut->percent[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700617 pr_debug("pc %d greater than known pc ranges for sfd\n", pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700618 row1 = 0;
619 row2 = 0;
620 }
621 if (pc < chip->pc_sf_lut->percent[rows - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700622 pr_debug("pc %d less than known pc ranges for sf", pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623 row1 = rows - 1;
624 row2 = rows - 1;
625 }
626 for (i = 0; i < rows; i++) {
627 if (pc == chip->pc_sf_lut->percent[i]) {
628 row1 = i;
629 row2 = i;
630 break;
631 }
632 if (pc > chip->pc_sf_lut->percent[i]) {
633 row1 = i - 1;
634 row2 = i;
635 break;
636 }
637 }
638
639 if (cycles < chip->pc_sf_lut->cycles[0])
640 cycles = chip->pc_sf_lut->cycles[0];
641 if (cycles > chip->pc_sf_lut->cycles[cols - 1])
642 cycles = chip->pc_sf_lut->cycles[cols - 1];
643
644 for (i = 0; i < cols; i++)
645 if (cycles <= chip->pc_sf_lut->cycles[i])
646 break;
647 if (cycles == chip->pc_sf_lut->cycles[i]) {
648 scalefactor = linear_interpolate(
649 chip->pc_sf_lut->sf[row1][i],
650 chip->pc_sf_lut->percent[row1],
651 chip->pc_sf_lut->sf[row2][i],
652 chip->pc_sf_lut->percent[row2],
653 pc);
654 return scalefactor;
655 }
656
657 scalefactorrow1 = linear_interpolate(
658 chip->pc_sf_lut->sf[row1][i - 1],
659 chip->pc_sf_lut->cycles[i - 1],
660 chip->pc_sf_lut->sf[row1][i],
661 chip->pc_sf_lut->cycles[i],
662 cycles);
663
664 scalefactorrow2 = linear_interpolate(
665 chip->pc_sf_lut->sf[row2][i - 1],
666 chip->pc_sf_lut->cycles[i - 1],
667 chip->pc_sf_lut->sf[row2][i],
668 chip->pc_sf_lut->cycles[i],
669 cycles);
670
671 scalefactor = linear_interpolate(
672 scalefactorrow1,
673 chip->pc_sf_lut->percent[row1],
674 scalefactorrow2,
675 chip->pc_sf_lut->percent[row2],
676 pc);
677
678 return scalefactor;
679}
680
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700681static int is_between(int left, int right, int value)
682{
683 if (left >= right && left >= value && value >= right)
684 return 1;
685 if (left <= right && left <= value && value <= right)
686 return 1;
687
688 return 0;
689}
690
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700691static int interpolate_pc(struct pm8921_bms_chip *chip,
692 int batt_temp, int ocv)
693{
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700694 int i, j, pcj, pcj_minus_one, pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700695 int rows = chip->pc_temp_ocv_lut->rows;
696 int cols = chip->pc_temp_ocv_lut->cols;
697
698 if (batt_temp < chip->pc_temp_ocv_lut->temp[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700699 pr_debug("batt_temp %d < known temp range for pc\n", batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700 batt_temp = chip->pc_temp_ocv_lut->temp[0];
701 }
702 if (batt_temp > chip->pc_temp_ocv_lut->temp[cols - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700703 pr_debug("batt_temp %d > known temp range for pc\n", batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700704 batt_temp = chip->pc_temp_ocv_lut->temp[cols - 1];
705 }
706
707 for (j = 0; j < cols; j++)
708 if (batt_temp <= chip->pc_temp_ocv_lut->temp[j])
709 break;
710 if (batt_temp == chip->pc_temp_ocv_lut->temp[j]) {
711 /* found an exact match for temp in the table */
712 if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j])
713 return chip->pc_temp_ocv_lut->percent[0];
714 if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j])
715 return chip->pc_temp_ocv_lut->percent[rows - 1];
716 for (i = 0; i < rows; i++) {
717 if (ocv >= chip->pc_temp_ocv_lut->ocv[i][j]) {
718 if (ocv == chip->pc_temp_ocv_lut->ocv[i][j])
719 return
720 chip->pc_temp_ocv_lut->percent[i];
721 pc = linear_interpolate(
722 chip->pc_temp_ocv_lut->percent[i],
723 chip->pc_temp_ocv_lut->ocv[i][j],
724 chip->pc_temp_ocv_lut->percent[i - 1],
725 chip->pc_temp_ocv_lut->ocv[i - 1][j],
726 ocv);
727 return pc;
728 }
729 }
730 }
731
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700732 /*
733 * batt_temp is within temperature for
734 * column j-1 and j
735 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700736 if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j])
737 return chip->pc_temp_ocv_lut->percent[0];
738 if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j - 1])
739 return chip->pc_temp_ocv_lut->percent[rows - 1];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700741 pcj_minus_one = 0;
742 pcj = 0;
743 for (i = 0; i < rows-1; i++) {
744 if (pcj == 0
745 && is_between(chip->pc_temp_ocv_lut->ocv[i][j],
746 chip->pc_temp_ocv_lut->ocv[i+1][j], ocv)) {
747 pcj = linear_interpolate(
748 chip->pc_temp_ocv_lut->percent[i],
749 chip->pc_temp_ocv_lut->ocv[i][j],
750 chip->pc_temp_ocv_lut->percent[i + 1],
751 chip->pc_temp_ocv_lut->ocv[i+1][j],
752 ocv);
753 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700755 if (pcj_minus_one == 0
756 && is_between(chip->pc_temp_ocv_lut->ocv[i][j-1],
757 chip->pc_temp_ocv_lut->ocv[i+1][j-1], ocv)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700759 pcj_minus_one = linear_interpolate(
760 chip->pc_temp_ocv_lut->percent[i],
761 chip->pc_temp_ocv_lut->ocv[i][j-1],
762 chip->pc_temp_ocv_lut->percent[i + 1],
763 chip->pc_temp_ocv_lut->ocv[i+1][j-1],
764 ocv);
765 }
766
767 if (pcj && pcj_minus_one) {
768 pc = linear_interpolate(
769 pcj_minus_one,
770 chip->pc_temp_ocv_lut->temp[j-1],
771 pcj,
772 chip->pc_temp_ocv_lut->temp[j],
773 batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774 return pc;
775 }
776 }
777
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700778 if (pcj)
779 return pcj;
780
781 if (pcj_minus_one)
782 return pcj_minus_one;
783
784 pr_debug("%d ocv wasn't found for temp %d in the LUT returning 100%%",
785 ocv, batt_temp);
786 return 100;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700787}
788
789static int calculate_rbatt(struct pm8921_bms_chip *chip)
790{
791 int rc;
792 unsigned int ocv, vsense, vbatt, r_batt;
793
794 rc = read_ocv_for_rbatt(chip, &ocv);
795 if (rc) {
796 pr_err("fail to read ocv_for_rbatt rc = %d\n", rc);
797 ocv = 0;
798 }
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700799
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700800 rc = read_vbatt_for_rbatt(chip, &vbatt);
801 if (rc) {
802 pr_err("fail to read vbatt_for_rbatt rc = %d\n", rc);
803 ocv = 0;
804 }
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700805
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700806 rc = read_vsense_for_rbatt(chip, &vsense);
807 if (rc) {
808 pr_err("fail to read vsense_for_rbatt rc = %d\n", rc);
809 ocv = 0;
810 }
811 if (ocv == 0
812 || ocv == vbatt
813 || vsense == 0) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700814 pr_debug("rbatt readings unavailable ocv = %d, vbatt = %d,"
815 "vsen = %d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700816 ocv, vbatt, vsense);
817 return -EINVAL;
818 }
819 r_batt = ((ocv - vbatt) * chip->r_sense) / vsense;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700820 last_rbatt = r_batt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700821 pr_debug("r_batt = %umilliOhms", r_batt);
822 return r_batt;
823}
824
825static int calculate_fcc(struct pm8921_bms_chip *chip, int batt_temp,
826 int chargecycles)
827{
828 int initfcc, result, scalefactor = 0;
829
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700830 if (chip->adjusted_fcc_temp_lut == NULL) {
831 initfcc = interpolate_fcc(chip, batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700832
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700833 scalefactor = interpolate_scalingfactor_fcc(chip, chargecycles);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700834
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700835 /* Multiply the initial FCC value by the scale factor. */
836 result = (initfcc * scalefactor) / 100;
837 pr_debug("fcc mAh = %d\n", result);
838 return result;
839 } else {
840 return interpolate_fcc_adjusted(chip, batt_temp);
841 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700842}
843
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -0700844static int get_battery_uvolts(struct pm8921_bms_chip *chip, int *uvolts)
845{
846 int rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -0700847 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -0700848
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -0700849 rc = pm8xxx_adc_read(chip->vbat_channel, &result);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -0700850 if (rc) {
851 pr_err("error reading adc channel = %d, rc = %d\n",
852 chip->vbat_channel, rc);
853 return rc;
854 }
855 pr_debug("mvolts phy = %lld meas = 0x%llx", result.physical,
856 result.measurement);
857 *uvolts = (int)result.physical;
858 *uvolts = *uvolts * 1000;
859 return 0;
860}
861
862static int adc_based_ocv(struct pm8921_bms_chip *chip, int *ocv)
863{
864 int vbatt, rbatt, ibatt, rc;
865
866 rc = get_battery_uvolts(chip, &vbatt);
867 if (rc) {
868 pr_err("failed to read vbatt from adc rc = %d\n", rc);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -0700869 return rc;
870 }
871
872 rc = pm8921_bms_get_battery_current(&ibatt);
873 if (rc) {
874 pr_err("failed to read batt current rc = %d\n", rc);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -0700875 return rc;
876 }
877
878 rbatt = calculate_rbatt(the_chip);
879 if (rbatt < 0)
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700880 rbatt = (last_rbatt < 0) ? DEFAULT_RBATT_MOHMS : last_rbatt;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -0700881 *ocv = vbatt + ibatt * rbatt;
882 return 0;
883}
884
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700885static int calculate_pc(struct pm8921_bms_chip *chip, int ocv_uv, int batt_temp,
886 int chargecycles)
887{
888 int pc, scalefactor;
889
890 pc = interpolate_pc(chip, batt_temp, ocv_uv / 1000);
891 pr_debug("pc = %u for ocv = %dmicroVolts batt_temp = %d\n",
892 pc, ocv_uv, batt_temp);
893
894 scalefactor = interpolate_scalingfactor_pc(chip, chargecycles, pc);
895 pr_debug("scalefactor = %u batt_temp = %d\n", scalefactor, batt_temp);
896
897 /* Multiply the initial FCC value by the scale factor. */
898 pc = (pc * scalefactor) / 100;
899 return pc;
900}
901
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700902static void calculate_cc_mah(struct pm8921_bms_chip *chip, int64_t *val,
Abhijeet Dharmapurikar52aa9682011-07-20 14:05:47 -0700903 int *coulumb_counter)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904{
905 int rc;
906 int64_t cc_voltage_uv, cc_uvh, cc_mah;
907
908 rc = read_cc(the_chip, coulumb_counter);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909 cc_voltage_uv = (int64_t)*coulumb_counter;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700910 cc_voltage_uv = cc_to_microvolt(chip, cc_voltage_uv);
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800911 cc_voltage_uv = pm8xxx_cc_adjust_for_gain(cc_voltage_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700912 pr_debug("cc_voltage_uv = %lld microvolts\n", cc_voltage_uv);
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700913 cc_uvh = ccmicrovolt_to_uvh(cc_voltage_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914 pr_debug("cc_uvh = %lld micro_volt_hour\n", cc_uvh);
915 cc_mah = div_s64(cc_uvh, chip->r_sense);
916 *val = cc_mah;
917}
918
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700919static int calculate_unusable_charge_mah(struct pm8921_bms_chip *chip,
920 int fcc, int batt_temp, int chargecycles)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700921{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700922 int rbatt, voltage_unusable_uv, pc_unusable;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700923
924 rbatt = calculate_rbatt(chip);
925 if (rbatt < 0) {
926 rbatt = (last_rbatt < 0) ? DEFAULT_RBATT_MOHMS : last_rbatt;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700927 pr_debug("rbatt unavailable assuming %d\n", rbatt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700928 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700929
930 /* calculate unusable charge */
931 voltage_unusable_uv = (rbatt * chip->i_test)
932 + (chip->v_failure * 1000);
933 pc_unusable = calculate_pc(chip, voltage_unusable_uv,
934 batt_temp, chargecycles);
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700935 pr_debug("rbatt = %umilliOhms unusable_v =%d unusable_pc = %d\n",
936 rbatt, voltage_unusable_uv, pc_unusable);
937 return (fcc * pc_unusable) / 100;
938}
Abhijeet Dharmapurikar52aa9682011-07-20 14:05:47 -0700939
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700940/* calculate remainging charge at the time of ocv */
941static int calculate_remaining_charge_mah(struct pm8921_bms_chip *chip, int fcc,
942 int batt_temp, int chargecycles)
943{
944 int rc, ocv, pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700945
946 /* calculate remainging charge */
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -0700947 ocv = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948 rc = read_last_good_ocv(chip, &ocv);
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -0700949 if (rc)
950 pr_debug("failed to read ocv rc = %d\n", rc);
951
952 if (ocv == 0) {
953 ocv = last_ocv_uv;
954 pr_debug("ocv not available using last_ocv_uv=%d\n", ocv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700955 }
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700956
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700957 pc = calculate_pc(chip, ocv, batt_temp, chargecycles);
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -0700958 pr_debug("ocv = %d pc = %d\n", ocv, pc);
959 return (fcc * pc) / 100;
960}
961
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700962static void calculate_charging_params(struct pm8921_bms_chip *chip,
963 int batt_temp, int chargecycles,
964 int *fcc,
965 int *unusable_charge,
966 int *remaining_charge,
967 int64_t *cc_mah)
968{
969 int coulumb_counter;
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700970 unsigned long flags;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700971
972 *fcc = calculate_fcc(chip, batt_temp, chargecycles);
973 pr_debug("FCC = %umAh batt_temp = %d, cycles = %d",
974 *fcc, batt_temp, chargecycles);
975
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700976 /* fcc doesnt need to be read from hardware, lock the bms now */
977 spin_lock_irqsave(&chip->bms_output_lock, flags);
978 pm_bms_lock_output_data(chip);
979
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700980 *unusable_charge = calculate_unusable_charge_mah(chip, *fcc,
981 batt_temp, chargecycles);
982
983 pr_debug("UUC = %umAh", *unusable_charge);
984
985 /* calculate remainging charge */
986 *remaining_charge = calculate_remaining_charge_mah(chip, *fcc,
987 batt_temp, chargecycles);
988 pr_debug("RC = %umAh\n", *remaining_charge);
989
990 /* calculate cc milli_volt_hour */
991 calculate_cc_mah(chip, cc_mah, &coulumb_counter);
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -0800992 pr_debug("cc_mah = %lldmAh cc = %d\n", *cc_mah, coulumb_counter);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700993
994 pm_bms_unlock_output_data(chip);
995 spin_unlock_irqrestore(&chip->bms_output_lock, flags);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700996}
997
998static int calculate_real_fcc(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -0800999 int batt_temp, int chargecycles,
1000 int *ret_fcc)
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001001{
1002 int fcc, unusable_charge;
1003 int remaining_charge;
1004 int64_t cc_mah;
1005 int real_fcc;
1006
1007 calculate_charging_params(chip, batt_temp, chargecycles,
1008 &fcc,
1009 &unusable_charge,
1010 &remaining_charge,
1011 &cc_mah);
1012
1013 real_fcc = remaining_charge - cc_mah;
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08001014 *ret_fcc = fcc;
1015 pr_debug("real_fcc = %d, RC = %d CC = %lld fcc = %d\n",
1016 real_fcc, remaining_charge, cc_mah, fcc);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001017 return real_fcc;
1018}
1019/*
1020 * Remaining Usable Charge = remaining_charge (charge at ocv instance)
1021 * - coloumb counter charge
1022 * - unusable charge (due to battery resistance)
1023 * SOC% = (remaining usable charge/ fcc - usable_charge);
1024 */
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001025#define BATTERY_POWER_SUPPLY_SOC 53
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001026static int calculate_state_of_charge(struct pm8921_bms_chip *chip,
1027 int batt_temp, int chargecycles)
1028{
1029 int remaining_usable_charge, fcc, unusable_charge;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001030 int remaining_charge, soc;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001031 int update_userspace = 1;
1032 int64_t cc_mah;
1033
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001034 calculate_charging_params(chip, batt_temp, chargecycles,
1035 &fcc,
1036 &unusable_charge,
1037 &remaining_charge,
1038 &cc_mah);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001039
1040 /* calculate remaining usable charge */
1041 remaining_usable_charge = remaining_charge - cc_mah - unusable_charge;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001042 pr_debug("RUC = %dmAh\n", remaining_usable_charge);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001043 soc = (remaining_usable_charge * 100) / (fcc - unusable_charge);
1044 if (soc > 100)
1045 soc = 100;
1046 pr_debug("SOC = %u%%\n", soc);
1047
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -08001048 if (bms_fake_battery) {
1049 soc = BATTERY_POWER_SUPPLY_SOC;
1050 pr_debug("setting SOC = %u%% bms_fake_battery = %d\n", soc,
1051 bms_fake_battery);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001052 }
1053
1054 if (soc < 0) {
Abhijeet Dharmapurikar21ebadd2011-09-28 21:14:16 -07001055 pr_err("bad rem_usb_chg = %d rem_chg %d,"
1056 "cc_mah %lld, unusb_chg %d\n",
1057 remaining_usable_charge, remaining_charge,
1058 cc_mah, unusable_charge);
1059 pr_err("for bad rem_usb_chg last_ocv_uv = %d"
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001060 "chargecycles = %d, batt_temp = %d"
1061 "fcc = %d soc =%d\n",
1062 last_ocv_uv, chargecycles, batt_temp,
1063 fcc, soc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001064 update_userspace = 0;
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -08001065 soc = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001066 }
1067
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001068 if (last_soc == -EINVAL || soc <= last_soc) {
1069 last_soc = update_userspace ? soc : last_soc;
1070 return soc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001071 }
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001072
1073 /*
1074 * soc > last_soc
1075 * the device must be charging for reporting a higher soc, if not ignore
1076 * this soc and continue reporting the last_soc
1077 */
1078 if (the_chip->start_percent != 0) {
1079 last_soc = soc;
1080 } else {
1081 pr_debug("soc = %d reporting last_soc = %d\n", soc, last_soc);
1082 soc = last_soc;
1083 }
1084
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001085 return soc;
1086}
1087
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001088#define XOADC_MAX_1P25V 1312500
1089#define XOADC_MIN_1P25V 1187500
1090#define XOADC_MAX_0P625V 656250
1091#define XOADC_MIN_0P625V 593750
1092
1093#define HKADC_V_PER_BIT_MUL_FACTOR 977
1094#define HKADC_V_PER_BIT_DIV_FACTOR 10
1095static int calib_hkadc_convert_microvolt(unsigned int phy)
1096{
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001097 return (phy - 0x6000) *
1098 HKADC_V_PER_BIT_MUL_FACTOR / HKADC_V_PER_BIT_DIV_FACTOR;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001099}
1100
1101static void calib_hkadc(struct pm8921_bms_chip *chip)
1102{
1103 int voltage, rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001104 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001105
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001106 rc = pm8xxx_adc_read(the_chip->ref1p25v_channel, &result);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001107 if (rc) {
1108 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
1109 return;
1110 }
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001111 voltage = calib_hkadc_convert_microvolt(result.adc_code);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001112
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001113 pr_debug("result 1.25v = 0x%x, voltage = %duV adc_meas = %lld\n",
1114 result.adc_code, voltage, result.measurement);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001115
1116 /* check for valid range */
1117 if (voltage > XOADC_MAX_1P25V)
1118 voltage = XOADC_MAX_1P25V;
1119 else if (voltage < XOADC_MIN_1P25V)
1120 voltage = XOADC_MIN_1P25V;
1121 chip->xoadc_v125 = voltage;
1122
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001123 rc = pm8xxx_adc_read(the_chip->ref625mv_channel, &result);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001124 if (rc) {
1125 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
1126 return;
1127 }
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001128 voltage = calib_hkadc_convert_microvolt(result.adc_code);
1129 pr_debug("result 0.625V = 0x%x, voltage = %duV adc_mead = %lld\n",
1130 result.adc_code, voltage, result.measurement);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001131 /* check for valid range */
1132 if (voltage > XOADC_MAX_0P625V)
1133 voltage = XOADC_MAX_0P625V;
1134 else if (voltage < XOADC_MIN_0P625V)
1135 voltage = XOADC_MIN_0P625V;
1136
1137 chip->xoadc_v0625 = voltage;
1138}
1139
1140static void calibrate_hkadc_work(struct work_struct *work)
1141{
1142 struct pm8921_bms_chip *chip = container_of(work,
1143 struct pm8921_bms_chip, calib_hkadc_work);
1144
1145 calib_hkadc(chip);
1146}
1147
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001148static void calibrate_ccadc_work(struct work_struct *work)
1149{
1150 struct pm8921_bms_chip *chip = container_of(work,
1151 struct pm8921_bms_chip, calib_ccadc_work.work);
1152
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08001153 pm8xxx_calib_ccadc();
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001154 schedule_delayed_work(&chip->calib_ccadc_work,
1155 round_jiffies_relative(msecs_to_jiffies
1156 (chip->calib_delay_ms)));
1157}
1158
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001159int pm8921_bms_get_vsense_avg(int *result)
1160{
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001161 int rc = -EINVAL;
1162 unsigned long flags;
1163
1164 if (the_chip) {
1165 spin_lock_irqsave(&the_chip->bms_output_lock, flags);
1166 pm_bms_lock_output_data(the_chip);
1167 rc = read_vsense_avg(the_chip, result);
1168 pm_bms_unlock_output_data(the_chip);
1169 spin_unlock_irqrestore(&the_chip->bms_output_lock, flags);
1170 }
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001171
1172 pr_err("called before initialization\n");
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001173 return rc;
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001174}
1175EXPORT_SYMBOL(pm8921_bms_get_vsense_avg);
1176
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001177int pm8921_bms_get_battery_current(int *result)
1178{
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001179 unsigned long flags;
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001180 int vsense;
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001181
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001182 if (!the_chip) {
1183 pr_err("called before initialization\n");
1184 return -EINVAL;
1185 }
1186 if (the_chip->r_sense == 0) {
1187 pr_err("r_sense is zero\n");
1188 return -EINVAL;
1189 }
1190
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001191 spin_lock_irqsave(&the_chip->bms_output_lock, flags);
1192 pm_bms_lock_output_data(the_chip);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001193 read_vsense_avg(the_chip, &vsense);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001194 pm_bms_unlock_output_data(the_chip);
1195 spin_unlock_irqrestore(&the_chip->bms_output_lock, flags);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001196 pr_debug("vsense=%d\n", vsense);
Abhijeet Dharmapurikara7a1c6b2011-08-17 10:04:58 -07001197 /* cast for signed division */
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001198 *result = vsense / (int)the_chip->r_sense;
1199
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001200 return 0;
1201}
1202EXPORT_SYMBOL(pm8921_bms_get_battery_current);
1203
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001204int pm8921_bms_get_percent_charge(void)
1205{
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001206 int batt_temp, rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001207 struct pm8xxx_adc_chan_result result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001208
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001209 if (!the_chip) {
1210 pr_err("called before initialization\n");
1211 return -EINVAL;
1212 }
1213
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001214 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001215 if (rc) {
1216 pr_err("error reading adc channel = %d, rc = %d\n",
1217 the_chip->batt_temp_channel, rc);
1218 return rc;
1219 }
1220 pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical,
1221 result.measurement);
1222 batt_temp = (int)result.physical;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001223 return calculate_state_of_charge(the_chip,
1224 batt_temp, last_chargecycles);
1225}
1226EXPORT_SYMBOL_GPL(pm8921_bms_get_percent_charge);
1227
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07001228int pm8921_bms_get_fcc(void)
1229{
1230 int batt_temp, rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001231 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07001232
1233 if (!the_chip) {
1234 pr_err("called before initialization\n");
1235 return -EINVAL;
1236 }
1237
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001238 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07001239 if (rc) {
1240 pr_err("error reading adc channel = %d, rc = %d\n",
1241 the_chip->batt_temp_channel, rc);
1242 return rc;
1243 }
1244 pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical,
1245 result.measurement);
1246 batt_temp = (int)result.physical;
1247 return calculate_fcc(the_chip, batt_temp, last_chargecycles);
1248}
1249EXPORT_SYMBOL_GPL(pm8921_bms_get_fcc);
1250
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001251void pm8921_bms_charging_began(void)
1252{
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001253 the_chip->start_percent = pm8921_bms_get_percent_charge();
1254 pr_debug("start_percent = %u%%\n", the_chip->start_percent);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001255}
1256EXPORT_SYMBOL_GPL(pm8921_bms_charging_began);
1257
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08001258#define DELTA_FCC_PERCENT 3
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001259void pm8921_bms_charging_end(int is_battery_full)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001260{
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001261 if (is_battery_full && the_chip != NULL) {
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -08001262 unsigned long flags;
1263 int batt_temp, rc, cc_reading;
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08001264 int fcc, new_fcc, delta_fcc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001265 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001266
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001267 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001268 if (rc) {
1269 pr_err("error reading adc channel = %d, rc = %d\n",
1270 the_chip->batt_temp_channel, rc);
1271 goto charge_cycle_calculation;
1272 }
1273 pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical,
1274 result.measurement);
1275 batt_temp = (int)result.physical;
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08001276 new_fcc = calculate_real_fcc(the_chip,
1277 batt_temp, last_chargecycles,
1278 &fcc);
1279 delta_fcc = new_fcc - fcc;
1280 if (delta_fcc < 0)
1281 delta_fcc = -delta_fcc;
1282
1283 if (delta_fcc * 100 <= (DELTA_FCC_PERCENT * fcc)) {
1284 pr_debug("delta_fcc=%d < %d percent of fcc=%d\n",
1285 delta_fcc, DELTA_FCC_PERCENT, fcc);
1286 last_real_fcc = new_fcc;
1287 last_real_fcc_batt_temp = batt_temp;
1288 readjust_fcc_table();
1289 } else {
1290 pr_debug("delta_fcc=%d > %d percent of fcc=%d"
1291 "will not update real fcc\n",
1292 delta_fcc, DELTA_FCC_PERCENT, fcc);
1293 }
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -08001294
1295 spin_lock_irqsave(&the_chip->bms_output_lock, flags);
1296 pm_bms_lock_output_data(the_chip);
1297 pm_bms_read_output_data(the_chip, LAST_GOOD_OCV_VALUE,
1298 &the_chip->ocv_reading_at_100);
1299 read_cc(the_chip, &cc_reading);
1300 pm_bms_unlock_output_data(the_chip);
1301 spin_unlock_irqrestore(&the_chip->bms_output_lock, flags);
1302 the_chip->cc_reading_at_100 = cc_reading;
1303 pr_debug("EOC ocv_reading = 0x%x cc_reading = %d\n",
1304 the_chip->ocv_reading_at_100,
1305 the_chip->cc_reading_at_100);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001306 }
1307
1308charge_cycle_calculation:
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001309 the_chip->end_percent = pm8921_bms_get_percent_charge();
1310 if (the_chip->end_percent > the_chip->start_percent) {
1311 last_charge_increase =
1312 the_chip->end_percent - the_chip->start_percent;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001313 if (last_charge_increase > 100) {
1314 last_chargecycles++;
1315 last_charge_increase = last_charge_increase % 100;
1316 }
1317 }
1318 pr_debug("end_percent = %u%% last_charge_increase = %d"
1319 "last_chargecycles = %d\n",
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001320 the_chip->end_percent,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001321 last_charge_increase,
1322 last_chargecycles);
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001323 the_chip->start_percent = 0;
1324 the_chip->end_percent = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001325}
1326EXPORT_SYMBOL_GPL(pm8921_bms_charging_end);
1327
1328static irqreturn_t pm8921_bms_sbi_write_ok_handler(int irq, void *data)
1329{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001330 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001331 return IRQ_HANDLED;
1332}
1333
1334static irqreturn_t pm8921_bms_cc_thr_handler(int irq, void *data)
1335{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001336 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001337 return IRQ_HANDLED;
1338}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001339
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001340static irqreturn_t pm8921_bms_vsense_thr_handler(int irq, void *data)
1341{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001342 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001343 return IRQ_HANDLED;
1344}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001345
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001346static irqreturn_t pm8921_bms_vsense_for_r_handler(int irq, void *data)
1347{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001348 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001349 return IRQ_HANDLED;
1350}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001351
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001352static irqreturn_t pm8921_bms_ocv_for_r_handler(int irq, void *data)
1353{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001354 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001355
1356 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001357 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001358 return IRQ_HANDLED;
1359}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001360
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001361static irqreturn_t pm8921_bms_good_ocv_handler(int irq, void *data)
1362{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001363 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001364
1365 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001366 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001367 return IRQ_HANDLED;
1368}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001369
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001370static irqreturn_t pm8921_bms_vsense_avg_handler(int irq, void *data)
1371{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001372 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001373 return IRQ_HANDLED;
1374}
1375
1376struct pm_bms_irq_init_data {
1377 unsigned int irq_id;
1378 char *name;
1379 unsigned long flags;
1380 irqreturn_t (*handler)(int, void *);
1381};
1382
1383#define BMS_IRQ(_id, _flags, _handler) \
1384{ \
1385 .irq_id = _id, \
1386 .name = #_id, \
1387 .flags = _flags, \
1388 .handler = _handler, \
1389}
1390
1391struct pm_bms_irq_init_data bms_irq_data[] = {
1392 BMS_IRQ(PM8921_BMS_SBI_WRITE_OK, IRQF_TRIGGER_RISING,
1393 pm8921_bms_sbi_write_ok_handler),
1394 BMS_IRQ(PM8921_BMS_CC_THR, IRQF_TRIGGER_RISING,
1395 pm8921_bms_cc_thr_handler),
1396 BMS_IRQ(PM8921_BMS_VSENSE_THR, IRQF_TRIGGER_RISING,
1397 pm8921_bms_vsense_thr_handler),
1398 BMS_IRQ(PM8921_BMS_VSENSE_FOR_R, IRQF_TRIGGER_RISING,
1399 pm8921_bms_vsense_for_r_handler),
1400 BMS_IRQ(PM8921_BMS_OCV_FOR_R, IRQF_TRIGGER_RISING,
1401 pm8921_bms_ocv_for_r_handler),
1402 BMS_IRQ(PM8921_BMS_GOOD_OCV, IRQF_TRIGGER_RISING,
1403 pm8921_bms_good_ocv_handler),
1404 BMS_IRQ(PM8921_BMS_VSENSE_AVG, IRQF_TRIGGER_RISING,
1405 pm8921_bms_vsense_avg_handler),
1406};
1407
1408static void free_irqs(struct pm8921_bms_chip *chip)
1409{
1410 int i;
1411
1412 for (i = 0; i < PM_BMS_MAX_INTS; i++)
1413 if (chip->pmic_bms_irq[i]) {
1414 free_irq(chip->pmic_bms_irq[i], NULL);
1415 chip->pmic_bms_irq[i] = 0;
1416 }
1417}
1418
1419static int __devinit request_irqs(struct pm8921_bms_chip *chip,
1420 struct platform_device *pdev)
1421{
1422 struct resource *res;
1423 int ret, i;
1424
1425 ret = 0;
1426 bitmap_fill(chip->enabled_irqs, PM_BMS_MAX_INTS);
1427
1428 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
1429 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1430 bms_irq_data[i].name);
1431 if (res == NULL) {
1432 pr_err("couldn't find %s\n", bms_irq_data[i].name);
1433 goto err_out;
1434 }
1435 ret = request_irq(res->start, bms_irq_data[i].handler,
1436 bms_irq_data[i].flags,
1437 bms_irq_data[i].name, chip);
1438 if (ret < 0) {
1439 pr_err("couldn't request %d (%s) %d\n", res->start,
1440 bms_irq_data[i].name, ret);
1441 goto err_out;
1442 }
1443 chip->pmic_bms_irq[bms_irq_data[i].irq_id] = res->start;
1444 pm8921_bms_disable_irq(chip, bms_irq_data[i].irq_id);
1445 }
1446 return 0;
1447
1448err_out:
1449 free_irqs(chip);
1450 return -EINVAL;
1451}
1452
1453#define EN_BMS_BIT BIT(7)
1454#define EN_PON_HS_BIT BIT(0)
1455static int __devinit pm8921_bms_hw_init(struct pm8921_bms_chip *chip)
1456{
1457 int rc;
1458
1459 rc = pm_bms_masked_write(chip, BMS_CONTROL,
1460 EN_BMS_BIT | EN_PON_HS_BIT, EN_BMS_BIT | EN_PON_HS_BIT);
1461 if (rc) {
1462 pr_err("failed to enable pon and bms addr = %d %d",
1463 BMS_CONTROL, rc);
1464 }
1465
1466 return 0;
1467}
1468
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001469static void check_initial_ocv(struct pm8921_bms_chip *chip)
1470{
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001471 int ocv, rc;
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001472
1473 /*
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001474 * Check if a ocv is available in bms hw,
1475 * if not compute it here at boot time and save it
1476 * in the last_ocv_uv.
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001477 */
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001478 ocv = 0;
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001479 rc = read_last_good_ocv(chip, &ocv);
1480 if (rc || ocv == 0) {
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001481 rc = adc_based_ocv(chip, &ocv);
1482 if (rc) {
1483 pr_err("failed to read adc based ocv rc = %d\n", rc);
1484 ocv = DEFAULT_OCV_MICROVOLTS;
1485 }
1486 last_ocv_uv = ocv;
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001487 }
1488 pr_debug("ocv = %d last_ocv_uv = %d\n", ocv, last_ocv_uv);
1489}
1490
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001491static int64_t read_battery_id(struct pm8921_bms_chip *chip)
1492{
1493 int rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001494 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001495
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001496 rc = pm8xxx_adc_read(chip->batt_id_channel, &result);
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001497 if (rc) {
1498 pr_err("error reading batt id channel = %d, rc = %d\n",
1499 chip->vbat_channel, rc);
1500 return rc;
1501 }
1502 pr_debug("batt_id phy = %lld meas = 0x%llx\n", result.physical,
1503 result.measurement);
1504 return result.physical;
1505}
1506
1507#define PALLADIUM_ID_MIN 2500
1508#define PALLADIUM_ID_MAX 4000
1509static int set_battery_data(struct pm8921_bms_chip *chip)
1510{
1511 int64_t battery_id;
1512
1513 battery_id = read_battery_id(chip);
1514
1515 if (battery_id < 0) {
1516 pr_err("cannot read battery id err = %lld\n", battery_id);
1517 return battery_id;
1518 }
1519
1520 if (is_between(PALLADIUM_ID_MIN, PALLADIUM_ID_MAX, battery_id)) {
1521 chip->fcc = palladium_1500_data.fcc;
1522 chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut;
1523 chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut;
1524 chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut;
1525 chip->pc_sf_lut = palladium_1500_data.pc_sf_lut;
1526 return 0;
1527 } else {
1528 pr_warn("invalid battery id, palladium 1500 assumed\n");
1529 chip->fcc = palladium_1500_data.fcc;
1530 chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut;
1531 chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut;
1532 chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut;
1533 chip->pc_sf_lut = palladium_1500_data.pc_sf_lut;
1534 return 0;
1535 }
1536}
1537
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001538enum {
1539 CALC_RBATT,
1540 CALC_FCC,
1541 CALC_PC,
1542 CALC_SOC,
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001543 CALIB_HKADC,
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001544 CALIB_CCADC,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001545};
1546
1547static int test_batt_temp = 5;
1548static int test_chargecycle = 150;
1549static int test_ocv = 3900000;
1550enum {
1551 TEST_BATT_TEMP,
1552 TEST_CHARGE_CYCLE,
1553 TEST_OCV,
1554};
1555static int get_test_param(void *data, u64 * val)
1556{
1557 switch ((int)data) {
1558 case TEST_BATT_TEMP:
1559 *val = test_batt_temp;
1560 break;
1561 case TEST_CHARGE_CYCLE:
1562 *val = test_chargecycle;
1563 break;
1564 case TEST_OCV:
1565 *val = test_ocv;
1566 break;
1567 default:
1568 return -EINVAL;
1569 }
1570 return 0;
1571}
1572static int set_test_param(void *data, u64 val)
1573{
1574 switch ((int)data) {
1575 case TEST_BATT_TEMP:
1576 test_batt_temp = (int)val;
1577 break;
1578 case TEST_CHARGE_CYCLE:
1579 test_chargecycle = (int)val;
1580 break;
1581 case TEST_OCV:
1582 test_ocv = (int)val;
1583 break;
1584 default:
1585 return -EINVAL;
1586 }
1587 return 0;
1588}
1589DEFINE_SIMPLE_ATTRIBUTE(temp_fops, get_test_param, set_test_param, "%llu\n");
1590
1591static int get_calc(void *data, u64 * val)
1592{
1593 int param = (int)data;
1594 int ret = 0;
1595
1596 *val = 0;
1597
1598 /* global irq number passed in via data */
1599 switch (param) {
1600 case CALC_RBATT:
1601 *val = calculate_rbatt(the_chip);
1602 break;
1603 case CALC_FCC:
1604 *val = calculate_fcc(the_chip, test_batt_temp,
1605 test_chargecycle);
1606 break;
1607 case CALC_PC:
1608 *val = calculate_pc(the_chip, test_ocv, test_batt_temp,
1609 test_chargecycle);
1610 break;
1611 case CALC_SOC:
1612 *val = calculate_state_of_charge(the_chip,
1613 test_batt_temp, test_chargecycle);
1614 break;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001615 case CALIB_HKADC:
1616 /* reading this will trigger calibration */
1617 *val = 0;
1618 calib_hkadc(the_chip);
1619 break;
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001620 case CALIB_CCADC:
1621 /* reading this will trigger calibration */
1622 *val = 0;
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08001623 pm8xxx_calib_ccadc();
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001624 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001625 default:
1626 ret = -EINVAL;
1627 }
1628 return ret;
1629}
1630DEFINE_SIMPLE_ATTRIBUTE(calc_fops, get_calc, NULL, "%llu\n");
1631
1632static int get_reading(void *data, u64 * val)
1633{
1634 int param = (int)data;
1635 int ret = 0;
1636
1637 *val = 0;
1638
1639 /* global irq number passed in via data */
1640 switch (param) {
1641 case CC_MSB:
1642 case CC_LSB:
1643 read_cc(the_chip, (int *)val);
1644 break;
1645 case LAST_GOOD_OCV_VALUE:
1646 read_last_good_ocv(the_chip, (uint *)val);
1647 break;
1648 case VBATT_FOR_RBATT:
1649 read_vbatt_for_rbatt(the_chip, (uint *)val);
1650 break;
1651 case VSENSE_FOR_RBATT:
1652 read_vsense_for_rbatt(the_chip, (uint *)val);
1653 break;
1654 case OCV_FOR_RBATT:
1655 read_ocv_for_rbatt(the_chip, (uint *)val);
1656 break;
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001657 case VSENSE_AVG:
1658 read_vsense_avg(the_chip, (uint *)val);
1659 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001660 default:
1661 ret = -EINVAL;
1662 }
1663 return ret;
1664}
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001665DEFINE_SIMPLE_ATTRIBUTE(reading_fops, get_reading, NULL, "%lld\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001666
1667static int get_rt_status(void *data, u64 * val)
1668{
1669 int i = (int)data;
1670 int ret;
1671
1672 /* global irq number passed in via data */
1673 ret = pm_bms_get_rt_status(the_chip, i);
1674 *val = ret;
1675 return 0;
1676}
1677DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n");
1678
1679static int get_reg(void *data, u64 * val)
1680{
1681 int addr = (int)data;
1682 int ret;
1683 u8 temp;
1684
1685 ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp);
1686 if (ret) {
1687 pr_err("pm8xxx_readb to %x value = %d errored = %d\n",
1688 addr, temp, ret);
1689 return -EAGAIN;
1690 }
1691 *val = temp;
1692 return 0;
1693}
1694
1695static int set_reg(void *data, u64 val)
1696{
1697 int addr = (int)data;
1698 int ret;
1699 u8 temp;
1700
1701 temp = (u8) val;
1702 ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp);
1703 if (ret) {
1704 pr_err("pm8xxx_writeb to %x value = %d errored = %d\n",
1705 addr, temp, ret);
1706 return -EAGAIN;
1707 }
1708 return 0;
1709}
1710DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
1711
1712static void create_debugfs_entries(struct pm8921_bms_chip *chip)
1713{
1714 int i;
1715
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08001716 chip->dent = debugfs_create_dir("pm8921-bms", NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001717
1718 if (IS_ERR(chip->dent)) {
1719 pr_err("pmic bms couldnt create debugfs dir\n");
1720 return;
1721 }
1722
1723 debugfs_create_file("BMS_CONTROL", 0644, chip->dent,
1724 (void *)BMS_CONTROL, &reg_fops);
1725 debugfs_create_file("BMS_OUTPUT0", 0644, chip->dent,
1726 (void *)BMS_OUTPUT0, &reg_fops);
1727 debugfs_create_file("BMS_OUTPUT1", 0644, chip->dent,
1728 (void *)BMS_OUTPUT1, &reg_fops);
1729 debugfs_create_file("BMS_TEST1", 0644, chip->dent,
1730 (void *)BMS_TEST1, &reg_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001731
1732 debugfs_create_file("test_batt_temp", 0644, chip->dent,
1733 (void *)TEST_BATT_TEMP, &temp_fops);
1734 debugfs_create_file("test_chargecycle", 0644, chip->dent,
1735 (void *)TEST_CHARGE_CYCLE, &temp_fops);
1736 debugfs_create_file("test_ocv", 0644, chip->dent,
1737 (void *)TEST_OCV, &temp_fops);
1738
1739 debugfs_create_file("read_cc", 0644, chip->dent,
1740 (void *)CC_MSB, &reading_fops);
1741 debugfs_create_file("read_last_good_ocv", 0644, chip->dent,
1742 (void *)LAST_GOOD_OCV_VALUE, &reading_fops);
1743 debugfs_create_file("read_vbatt_for_rbatt", 0644, chip->dent,
1744 (void *)VBATT_FOR_RBATT, &reading_fops);
1745 debugfs_create_file("read_vsense_for_rbatt", 0644, chip->dent,
1746 (void *)VSENSE_FOR_RBATT, &reading_fops);
1747 debugfs_create_file("read_ocv_for_rbatt", 0644, chip->dent,
1748 (void *)OCV_FOR_RBATT, &reading_fops);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001749 debugfs_create_file("read_vsense_avg", 0644, chip->dent,
1750 (void *)VSENSE_AVG, &reading_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001751
1752 debugfs_create_file("show_rbatt", 0644, chip->dent,
1753 (void *)CALC_RBATT, &calc_fops);
1754 debugfs_create_file("show_fcc", 0644, chip->dent,
1755 (void *)CALC_FCC, &calc_fops);
1756 debugfs_create_file("show_pc", 0644, chip->dent,
1757 (void *)CALC_PC, &calc_fops);
1758 debugfs_create_file("show_soc", 0644, chip->dent,
1759 (void *)CALC_SOC, &calc_fops);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001760 debugfs_create_file("calib_hkadc", 0644, chip->dent,
1761 (void *)CALIB_HKADC, &calc_fops);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001762 debugfs_create_file("calib_ccadc", 0644, chip->dent,
1763 (void *)CALIB_CCADC, &calc_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001764
1765 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
1766 if (chip->pmic_bms_irq[bms_irq_data[i].irq_id])
1767 debugfs_create_file(bms_irq_data[i].name, 0444,
1768 chip->dent,
1769 (void *)bms_irq_data[i].irq_id,
1770 &rt_fops);
1771 }
1772}
1773
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001774static int __devinit pm8921_bms_probe(struct platform_device *pdev)
1775{
1776 int rc = 0;
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001777 int vbatt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001778 struct pm8921_bms_chip *chip;
1779 const struct pm8921_bms_platform_data *pdata
1780 = pdev->dev.platform_data;
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001781
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001782 if (!pdata) {
1783 pr_err("missing platform data\n");
1784 return -EINVAL;
1785 }
1786
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001787 chip = kzalloc(sizeof(struct pm8921_bms_chip), GFP_KERNEL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001788 if (!chip) {
1789 pr_err("Cannot allocate pm_bms_chip\n");
1790 return -ENOMEM;
1791 }
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001792 spin_lock_init(&chip->bms_output_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001793 chip->dev = &pdev->dev;
1794 chip->r_sense = pdata->r_sense;
1795 chip->i_test = pdata->i_test;
1796 chip->v_failure = pdata->v_failure;
1797 chip->calib_delay_ms = pdata->calib_delay_ms;
Abhijeet Dharmapurikara93ede82011-11-17 12:20:03 -08001798 chip->max_voltage_uv = pdata->max_voltage_uv;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001799 rc = set_battery_data(chip);
1800 if (rc) {
1801 pr_err("%s bad battery data %d\n", __func__, rc);
Abhijeet Dharmapurikar4a4456e2011-09-27 15:10:38 -07001802 goto free_chip;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001803 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001804
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001805 chip->batt_temp_channel = pdata->bms_cdata.batt_temp_channel;
1806 chip->vbat_channel = pdata->bms_cdata.vbat_channel;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001807 chip->ref625mv_channel = pdata->bms_cdata.ref625mv_channel;
1808 chip->ref1p25v_channel = pdata->bms_cdata.ref1p25v_channel;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001809 chip->batt_id_channel = pdata->bms_cdata.batt_id_channel;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -07001810 chip->revision = pm8xxx_get_revision(chip->dev->parent);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001811 INIT_WORK(&chip->calib_hkadc_work, calibrate_hkadc_work);
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001812
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001813 rc = request_irqs(chip, pdev);
1814 if (rc) {
1815 pr_err("couldn't register interrupts rc = %d\n", rc);
1816 goto free_chip;
1817 }
1818
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001819 rc = pm8921_bms_hw_init(chip);
1820 if (rc) {
1821 pr_err("couldn't init hardware rc = %d\n", rc);
1822 goto free_irqs;
1823 }
1824
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001825 platform_set_drvdata(pdev, chip);
1826 the_chip = chip;
1827 create_debugfs_entries(chip);
1828
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001829 check_initial_ocv(chip);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001830
1831 INIT_DELAYED_WORK(&chip->calib_ccadc_work, calibrate_ccadc_work);
1832 /* begin calibration only on chips > 2.0 */
1833 if (chip->revision >= PM8XXX_REVISION_8921_2p0)
1834 calibrate_ccadc_work(&(chip->calib_ccadc_work.work));
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001835
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001836 /* initial hkadc calibration */
1837 schedule_work(&chip->calib_hkadc_work);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001838 /* enable the vbatt reading interrupts for scheduling hkadc calib */
1839 pm8921_bms_enable_irq(chip, PM8921_BMS_GOOD_OCV);
1840 pm8921_bms_enable_irq(chip, PM8921_BMS_OCV_FOR_R);
Abhijeet Dharmapurikar21ebadd2011-09-28 21:14:16 -07001841
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001842 get_battery_uvolts(chip, &vbatt);
1843 pr_info("OK battery_capacity_at_boot=%d volt = %d ocv = %d\n",
1844 pm8921_bms_get_percent_charge(),
1845 vbatt, last_ocv_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001846 return 0;
1847
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001848free_irqs:
1849 free_irqs(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001850free_chip:
1851 kfree(chip);
1852 return rc;
1853}
1854
1855static int __devexit pm8921_bms_remove(struct platform_device *pdev)
1856{
1857 struct pm8921_bms_chip *chip = platform_get_drvdata(pdev);
1858
1859 free_irqs(chip);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001860 kfree(chip->adjusted_fcc_temp_lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001861 platform_set_drvdata(pdev, NULL);
1862 the_chip = NULL;
1863 kfree(chip);
1864 return 0;
1865}
1866
1867static struct platform_driver pm8921_bms_driver = {
1868 .probe = pm8921_bms_probe,
1869 .remove = __devexit_p(pm8921_bms_remove),
1870 .driver = {
1871 .name = PM8921_BMS_DEV_NAME,
1872 .owner = THIS_MODULE,
1873 },
1874};
1875
1876static int __init pm8921_bms_init(void)
1877{
1878 return platform_driver_register(&pm8921_bms_driver);
1879}
1880
1881static void __exit pm8921_bms_exit(void)
1882{
1883 platform_driver_unregister(&pm8921_bms_driver);
1884}
1885
1886late_initcall(pm8921_bms_init);
1887module_exit(pm8921_bms_exit);
1888
1889MODULE_LICENSE("GPL v2");
1890MODULE_DESCRIPTION("PMIC8921 bms driver");
1891MODULE_VERSION("1.0");
1892MODULE_ALIAS("platform:" PM8921_BMS_DEV_NAME);