blob: e21862a260f09d2dfb818e3df7c5da931dde6d0d [file] [log] [blame]
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001/* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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>
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -080019#include <linux/power_supply.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070020#include <linux/mfd/pm8xxx/pm8921-bms.h>
21#include <linux/mfd/pm8xxx/core.h>
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -070022#include <linux/mfd/pm8xxx/pm8xxx-adc.h>
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -080023#include <linux/mfd/pm8xxx/ccadc.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070024#include <linux/interrupt.h>
25#include <linux/bitops.h>
26#include <linux/debugfs.h>
27#include <linux/slab.h>
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070028#include <linux/delay.h>
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -080029#include <linux/mutex.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030
31#define BMS_CONTROL 0x224
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -080032#define BMS_S1_DELAY 0x225
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033#define BMS_OUTPUT0 0x230
34#define BMS_OUTPUT1 0x231
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -080035#define BMS_TOLERANCES 0x232
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036#define BMS_TEST1 0x237
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070038#define ADC_ARB_SECP_CNTRL 0x190
39#define ADC_ARB_SECP_AMUX_CNTRL 0x191
40#define ADC_ARB_SECP_ANA_PARAM 0x192
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070041#define ADC_ARB_SECP_DIG_PARAM 0x193
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070042#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
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070046#define ADC_ARB_BMS_CNTRL 0x18D
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -070047#define AMUX_TRIM_2 0x322
48#define TEST_PROGRAM_REV 0x339
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070049
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050enum pmic_bms_interrupts {
51 PM8921_BMS_SBI_WRITE_OK,
52 PM8921_BMS_CC_THR,
53 PM8921_BMS_VSENSE_THR,
54 PM8921_BMS_VSENSE_FOR_R,
55 PM8921_BMS_OCV_FOR_R,
56 PM8921_BMS_GOOD_OCV,
57 PM8921_BMS_VSENSE_AVG,
58 PM_BMS_MAX_INTS,
59};
60
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080061struct pm8921_soc_params {
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -080062 uint16_t last_good_ocv_raw;
63 int cc;
64
65 int last_good_ocv_uv;
66};
67
68struct pm8921_rbatt_params {
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080069 uint16_t ocv_for_rbatt_raw;
70 uint16_t vsense_for_rbatt_raw;
71 uint16_t vbatt_for_rbatt_raw;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080072
73 int ocv_for_rbatt_uv;
74 int vsense_for_rbatt_uv;
75 int vbatt_for_rbatt_uv;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080076};
77
78/**
79 * struct pm8921_bms_chip -
80 * @bms_output_lock: lock to prevent concurrent bms reads
81 * @bms_100_lock: lock to prevent concurrent updates to values that force
82 * 100% charge
83 *
84 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070085struct pm8921_bms_chip {
86 struct device *dev;
87 struct dentry *dent;
88 unsigned int r_sense;
89 unsigned int i_test;
90 unsigned int v_failure;
91 unsigned int fcc;
92 struct single_row_lut *fcc_temp_lut;
93 struct single_row_lut *fcc_sf_lut;
94 struct pc_temp_ocv_lut *pc_temp_ocv_lut;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -080095 struct sf_lut *pc_sf_lut;
96 struct sf_lut *rbatt_sf_lut;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070097 struct work_struct calib_hkadc_work;
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070098 struct delayed_work calib_ccadc_work;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099 unsigned int calib_delay_ms;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700100 unsigned int revision;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800101 unsigned int xoadc_v0625_usb_present;
102 unsigned int xoadc_v0625_usb_absent;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700103 unsigned int xoadc_v0625;
104 unsigned int xoadc_v125;
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700105 unsigned int batt_temp_channel;
106 unsigned int vbat_channel;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700107 unsigned int ref625mv_channel;
108 unsigned int ref1p25v_channel;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -0700109 unsigned int batt_id_channel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110 unsigned int pmic_bms_irq[PM_BMS_MAX_INTS];
111 DECLARE_BITMAP(enabled_irqs, PM_BMS_MAX_INTS);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800112 struct mutex bms_output_lock;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800113 spinlock_t bms_100_lock;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700114 struct single_row_lut *adjusted_fcc_temp_lut;
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -0700115 unsigned int charging_began;
116 unsigned int start_percent;
117 unsigned int end_percent;
David Keitel35e11872012-02-17 17:40:42 -0800118 enum battery_type batt_type;
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -0800119 uint16_t ocv_reading_at_100;
120 int cc_reading_at_100;
Abhijeet Dharmapurikara93ede82011-11-17 12:20:03 -0800121 int max_voltage_uv;
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800122
123 int batt_temp_suspend;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800124 int soc_rbatt_suspend;
125 int default_rbatt_mohm;
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700126 int amux_2_trim_delta;
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -0700127 uint16_t prev_last_good_ocv_raw;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700128};
129
130static struct pm8921_bms_chip *the_chip;
131
132#define DEFAULT_RBATT_MOHMS 128
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133#define DEFAULT_OCV_MICROVOLTS 3900000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134#define DEFAULT_CHARGE_CYCLES 0
135
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800136static int last_usb_cal_delta_uv = 1800;
137module_param(last_usb_cal_delta_uv, int, 0644);
138
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700139static int last_chargecycles = DEFAULT_CHARGE_CYCLES;
140static int last_charge_increase;
141module_param(last_chargecycles, int, 0644);
142module_param(last_charge_increase, int, 0644);
143
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700144static int last_rbatt = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145static int last_ocv_uv = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146static int last_soc = -EINVAL;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800147static int last_real_fcc_mah = -EINVAL;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700148static int last_real_fcc_batt_temp = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700150static int bms_ops_set(const char *val, const struct kernel_param *kp)
151{
152 if (*(int *)kp->arg == -EINVAL)
153 return param_set_int(val, kp);
154 else
155 return 0;
156}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700157
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700158static struct kernel_param_ops bms_param_ops = {
159 .set = bms_ops_set,
160 .get = param_get_int,
161};
162
163module_param_cb(last_rbatt, &bms_param_ops, &last_rbatt, 0644);
164module_param_cb(last_ocv_uv, &bms_param_ops, &last_ocv_uv, 0644);
165module_param_cb(last_soc, &bms_param_ops, &last_soc, 0644);
166
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -0800167/*
Abhijeet Dharmapurikar986bd8a2012-01-05 15:15:15 -0800168 * bms_fake_battery is set in setups where a battery emulator is used instead
169 * of a real battery. This makes the bms driver report a different/fake value
170 * regardless of the calculated state of charge.
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -0800171 */
Abhijeet Dharmapurikar986bd8a2012-01-05 15:15:15 -0800172static int bms_fake_battery = -EINVAL;
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -0800173module_param(bms_fake_battery, int, 0644);
174
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800175/* bms_start_XXX and bms_end_XXX are read only */
176static int bms_start_percent;
177static int bms_start_ocv_uv;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800178static int bms_start_cc_uah;
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800179static int bms_end_percent;
180static int bms_end_ocv_uv;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800181static int bms_end_cc_uah;
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800182
183static int bms_ro_ops_set(const char *val, const struct kernel_param *kp)
184{
185 return -EINVAL;
186}
187
188static struct kernel_param_ops bms_ro_param_ops = {
189 .set = bms_ro_ops_set,
190 .get = param_get_int,
191};
192module_param_cb(bms_start_percent, &bms_ro_param_ops, &bms_start_percent, 0644);
193module_param_cb(bms_start_ocv_uv, &bms_ro_param_ops, &bms_start_ocv_uv, 0644);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800194module_param_cb(bms_start_cc_uah, &bms_ro_param_ops, &bms_start_cc_uah, 0644);
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800195
196module_param_cb(bms_end_percent, &bms_ro_param_ops, &bms_end_percent, 0644);
197module_param_cb(bms_end_ocv_uv, &bms_ro_param_ops, &bms_end_ocv_uv, 0644);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800198module_param_cb(bms_end_cc_uah, &bms_ro_param_ops, &bms_end_cc_uah, 0644);
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800199
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700200static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp);
201static void readjust_fcc_table(void)
202{
203 struct single_row_lut *temp, *old;
204 int i, fcc, ratio;
205
206 if (!the_chip->fcc_temp_lut) {
207 pr_err("The static fcc lut table is NULL\n");
208 return;
209 }
210
211 temp = kzalloc(sizeof(struct single_row_lut), GFP_KERNEL);
212 if (!temp) {
213 pr_err("Cannot allocate memory for adjusted fcc table\n");
214 return;
215 }
216
217 fcc = interpolate_fcc(the_chip, last_real_fcc_batt_temp);
218
219 temp->cols = the_chip->fcc_temp_lut->cols;
220 for (i = 0; i < the_chip->fcc_temp_lut->cols; i++) {
221 temp->x[i] = the_chip->fcc_temp_lut->x[i];
222 ratio = div_u64(the_chip->fcc_temp_lut->y[i] * 1000, fcc);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800223 temp->y[i] = (ratio * last_real_fcc_mah);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700224 temp->y[i] /= 1000;
225 pr_debug("temp=%d, staticfcc=%d, adjfcc=%d, ratio=%d\n",
226 temp->x[i], the_chip->fcc_temp_lut->y[i],
227 temp->y[i], ratio);
228 }
229
230 old = the_chip->adjusted_fcc_temp_lut;
231 the_chip->adjusted_fcc_temp_lut = temp;
232 kfree(old);
233}
234
235static int bms_last_real_fcc_set(const char *val,
236 const struct kernel_param *kp)
237{
238 int rc = 0;
239
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800240 if (last_real_fcc_mah == -EINVAL)
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700241 rc = param_set_int(val, kp);
242 if (rc) {
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800243 pr_err("Failed to set last_real_fcc_mah rc=%d\n", rc);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700244 return rc;
245 }
246 if (last_real_fcc_batt_temp != -EINVAL)
247 readjust_fcc_table();
248 return rc;
249}
250static struct kernel_param_ops bms_last_real_fcc_param_ops = {
251 .set = bms_last_real_fcc_set,
252 .get = param_get_int,
253};
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800254module_param_cb(last_real_fcc_mah, &bms_last_real_fcc_param_ops,
255 &last_real_fcc_mah, 0644);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700256
257static int bms_last_real_fcc_batt_temp_set(const char *val,
258 const struct kernel_param *kp)
259{
260 int rc = 0;
261
262 if (last_real_fcc_batt_temp == -EINVAL)
263 rc = param_set_int(val, kp);
264 if (rc) {
265 pr_err("Failed to set last_real_fcc_batt_temp rc=%d\n", rc);
266 return rc;
267 }
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800268 if (last_real_fcc_mah != -EINVAL)
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700269 readjust_fcc_table();
270 return rc;
271}
272
273static struct kernel_param_ops bms_last_real_fcc_batt_temp_param_ops = {
274 .set = bms_last_real_fcc_batt_temp_set,
275 .get = param_get_int,
276};
277module_param_cb(last_real_fcc_batt_temp, &bms_last_real_fcc_batt_temp_param_ops,
278 &last_real_fcc_batt_temp, 0644);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700279
280static int pm_bms_get_rt_status(struct pm8921_bms_chip *chip, int irq_id)
281{
282 return pm8xxx_read_irq_stat(chip->dev->parent,
283 chip->pmic_bms_irq[irq_id]);
284}
285
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700286static void pm8921_bms_enable_irq(struct pm8921_bms_chip *chip, int interrupt)
287{
288 if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) {
289 dev_dbg(chip->dev, "%s %d\n", __func__,
290 chip->pmic_bms_irq[interrupt]);
291 enable_irq(chip->pmic_bms_irq[interrupt]);
292 }
293}
294
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700295static void pm8921_bms_disable_irq(struct pm8921_bms_chip *chip, int interrupt)
296{
297 if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700298 pr_debug("%d\n", chip->pmic_bms_irq[interrupt]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700299 disable_irq_nosync(chip->pmic_bms_irq[interrupt]);
300 }
301}
302
303static int pm_bms_masked_write(struct pm8921_bms_chip *chip, u16 addr,
304 u8 mask, u8 val)
305{
306 int rc;
307 u8 reg;
308
309 rc = pm8xxx_readb(chip->dev->parent, addr, &reg);
310 if (rc) {
311 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
312 return rc;
313 }
314 reg &= ~mask;
315 reg |= val & mask;
316 rc = pm8xxx_writeb(chip->dev->parent, addr, reg);
317 if (rc) {
318 pr_err("write failed addr = %03X, rc = %d\n", addr, rc);
319 return rc;
320 }
321 return 0;
322}
323
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800324static int usb_chg_plugged_in(void)
325{
326 union power_supply_propval ret = {0,};
327 static struct power_supply *psy;
328
329 if (psy == NULL) {
330 psy = power_supply_get_by_name("usb");
331 if (psy == NULL)
332 return 0;
333 }
334
335 if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
336 return 0;
337
338 return ret.intval;
339}
340
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700341#define HOLD_OREG_DATA BIT(1)
342static int pm_bms_lock_output_data(struct pm8921_bms_chip *chip)
343{
344 int rc;
345
346 rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA,
347 HOLD_OREG_DATA);
348 if (rc) {
349 pr_err("couldnt lock bms output rc = %d\n", rc);
350 return rc;
351 }
352 return 0;
353}
354
355static int pm_bms_unlock_output_data(struct pm8921_bms_chip *chip)
356{
357 int rc;
358
359 rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA, 0);
360 if (rc) {
361 pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc);
362 return rc;
363 }
364 return 0;
365}
366
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700367#define SELECT_OUTPUT_DATA 0x1C
368#define SELECT_OUTPUT_TYPE_SHIFT 2
369#define OCV_FOR_RBATT 0x0
370#define VSENSE_FOR_RBATT 0x1
371#define VBATT_FOR_RBATT 0x2
372#define CC_MSB 0x3
373#define CC_LSB 0x4
374#define LAST_GOOD_OCV_VALUE 0x5
375#define VSENSE_AVG 0x6
376#define VBATT_AVG 0x7
377
378static int pm_bms_read_output_data(struct pm8921_bms_chip *chip, int type,
379 int16_t *result)
380{
381 int rc;
382 u8 reg;
383
384 if (!result) {
385 pr_err("result pointer null\n");
386 return -EINVAL;
387 }
388 *result = 0;
389 if (type < OCV_FOR_RBATT || type > VBATT_AVG) {
390 pr_err("invalid type %d asked to read\n", type);
391 return -EINVAL;
392 }
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700393
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700394 rc = pm_bms_masked_write(chip, BMS_CONTROL, SELECT_OUTPUT_DATA,
395 type << SELECT_OUTPUT_TYPE_SHIFT);
396 if (rc) {
397 pr_err("fail to select %d type in BMS_CONTROL rc = %d\n",
398 type, rc);
399 return rc;
400 }
401
402 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT0, &reg);
403 if (rc) {
404 pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n",
405 type, rc);
406 return rc;
407 }
408 *result = reg;
409 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT1, &reg);
410 if (rc) {
411 pr_err("fail to read BMS_OUTPUT1 for type %d rc = %d\n",
412 type, rc);
413 return rc;
414 }
415 *result |= reg << 8;
416 pr_debug("type %d result %x", type, *result);
417 return 0;
418}
419
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700420#define V_PER_BIT_MUL_FACTOR 97656
421#define V_PER_BIT_DIV_FACTOR 1000
422#define XOADC_INTRINSIC_OFFSET 0x6000
423static int xoadc_reading_to_microvolt(unsigned int a)
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700424{
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700425 if (a <= XOADC_INTRINSIC_OFFSET)
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700426 return 0;
427
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700428 return (a - XOADC_INTRINSIC_OFFSET)
429 * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR;
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700430}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700432#define XOADC_CALIB_UV 625000
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700433#define VBATT_MUL_FACTOR 3
434static int adjust_xo_vbatt_reading(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800435 int usb_chg, unsigned int uv)
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700436{
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800437 s64 numerator, denominator;
438 int local_delta;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700439
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700440 if (uv == 0)
441 return 0;
442
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800443 /* dont adjust if not calibrated */
444 if (chip->xoadc_v0625 == 0 || chip->xoadc_v125 == 0) {
445 pr_debug("No cal yet return %d\n", VBATT_MUL_FACTOR * uv);
446 return VBATT_MUL_FACTOR * uv;
447 }
448
449 if (usb_chg)
450 local_delta = last_usb_cal_delta_uv;
451 else
452 local_delta = 0;
453
454 pr_debug("using delta = %d\n", local_delta);
455 numerator = ((s64)uv - chip->xoadc_v0625 - local_delta)
456 * XOADC_CALIB_UV;
457 denominator = (s64)chip->xoadc_v125 - chip->xoadc_v0625 - local_delta;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700458 if (denominator == 0)
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700459 return uv * VBATT_MUL_FACTOR;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800460 return (XOADC_CALIB_UV + local_delta + div_s64(numerator, denominator))
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700461 * VBATT_MUL_FACTOR;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700462}
463
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700464#define CC_RESOLUTION_N_V1 1085069
465#define CC_RESOLUTION_D_V1 100000
466#define CC_RESOLUTION_N_V2 868056
467#define CC_RESOLUTION_D_V2 10000
468static s64 cc_to_microvolt_v1(s64 cc)
469{
470 return div_s64(cc * CC_RESOLUTION_N_V1, CC_RESOLUTION_D_V1);
471}
472
473static s64 cc_to_microvolt_v2(s64 cc)
474{
475 return div_s64(cc * CC_RESOLUTION_N_V2, CC_RESOLUTION_D_V2);
476}
477
478static s64 cc_to_microvolt(struct pm8921_bms_chip *chip, s64 cc)
479{
480 /*
481 * resolution (the value of a single bit) was changed after revision 2.0
482 * for more accurate readings
483 */
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -0700484 return (chip->revision < PM8XXX_REVISION_8921_2p0) ?
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700485 cc_to_microvolt_v1((s64)cc) :
486 cc_to_microvolt_v2((s64)cc);
487}
488
489#define CC_READING_TICKS 55
490#define SLEEP_CLK_HZ 32768
491#define SECONDS_PER_HOUR 3600
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800492/**
493 * ccmicrovolt_to_nvh -
494 * @cc_uv: coulumb counter converted to uV
495 *
496 * RETURNS: coulumb counter based charge in nVh
497 * (nano Volt Hour)
498 */
499static s64 ccmicrovolt_to_nvh(s64 cc_uv)
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700500{
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800501 return div_s64(cc_uv * CC_READING_TICKS * 1000,
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700502 SLEEP_CLK_HZ * SECONDS_PER_HOUR);
503}
504
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505/* returns the signed value read from the hardware */
506static int read_cc(struct pm8921_bms_chip *chip, int *result)
507{
508 int rc;
509 uint16_t msw, lsw;
510
511 rc = pm_bms_read_output_data(chip, CC_LSB, &lsw);
512 if (rc) {
513 pr_err("fail to read CC_LSB rc = %d\n", rc);
514 return rc;
515 }
516 rc = pm_bms_read_output_data(chip, CC_MSB, &msw);
517 if (rc) {
518 pr_err("fail to read CC_MSB rc = %d\n", rc);
519 return rc;
520 }
521 *result = msw << 16 | lsw;
522 pr_debug("msw = %04x lsw = %04x cc = %d\n", msw, lsw, *result);
523 return 0;
524}
525
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700526static int adjust_xo_vbatt_reading_for_mbg(struct pm8921_bms_chip *chip,
527 int result)
528{
529 int64_t numerator;
530 int64_t denominator;
531
532 if (chip->amux_2_trim_delta == 0)
533 return result;
534
535 numerator = (s64)result * 1000000;
536 denominator = (1000000 + (410 * (s64)chip->amux_2_trim_delta));
537 return div_s64(numerator, denominator);
538}
539
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800540static int convert_vbatt_raw_to_uv(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800541 int usb_chg,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800542 uint16_t reading, int *result)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543{
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700544 *result = xoadc_reading_to_microvolt(reading);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800545 pr_debug("raw = %04x vbatt = %u\n", reading, *result);
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800546 *result = adjust_xo_vbatt_reading(chip, usb_chg, *result);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800547 pr_debug("after adj vbatt = %u\n", *result);
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700548 *result = adjust_xo_vbatt_reading_for_mbg(chip, *result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700549 return 0;
550}
551
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800552static int convert_vsense_to_uv(struct pm8921_bms_chip *chip,
553 int16_t reading, int *result)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554{
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800555 *result = pm8xxx_ccadc_reading_to_microvolt(chip->revision, reading);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800556 pr_debug("raw = %04x vsense = %d\n", reading, *result);
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800557 *result = pm8xxx_cc_adjust_for_gain(*result);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800558 pr_debug("after adj vsense = %d\n", *result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559 return 0;
560}
561
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700562static int read_vsense_avg(struct pm8921_bms_chip *chip, int *result)
563{
564 int rc;
565 int16_t reading;
566
567 rc = pm_bms_read_output_data(chip, VSENSE_AVG, &reading);
568 if (rc) {
569 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
570 return rc;
571 }
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800572
573 convert_vsense_to_uv(chip, reading, result);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700574 return 0;
575}
576
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577static int linear_interpolate(int y0, int x0, int y1, int x1, int x)
578{
579 if (y0 == y1 || x == x0)
580 return y0;
581 if (x1 == x0 || x == x1)
582 return y1;
583
584 return y0 + ((y1 - y0) * (x - x0) / (x1 - x0));
585}
586
587static int interpolate_single_lut(struct single_row_lut *lut, int x)
588{
589 int i, result;
590
591 if (x < lut->x[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700592 pr_debug("x %d less than known range return y = %d lut = %pS\n",
593 x, lut->y[0], lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594 return lut->y[0];
595 }
596 if (x > lut->x[lut->cols - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700597 pr_debug("x %d more than known range return y = %d lut = %pS\n",
598 x, lut->y[lut->cols - 1], lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599 return lut->y[lut->cols - 1];
600 }
601
602 for (i = 0; i < lut->cols; i++)
603 if (x <= lut->x[i])
604 break;
605 if (x == lut->x[i]) {
606 result = lut->y[i];
607 } else {
608 result = linear_interpolate(
609 lut->y[i - 1],
610 lut->x[i - 1],
611 lut->y[i],
612 lut->x[i],
613 x);
614 }
615 return result;
616}
617
618static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp)
619{
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -0800620 /* batt_temp is in tenths of degC - convert it to degC for lookups */
621 batt_temp = batt_temp/10;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700622 return interpolate_single_lut(chip->fcc_temp_lut, batt_temp);
623}
624
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700625static int interpolate_fcc_adjusted(struct pm8921_bms_chip *chip, int batt_temp)
626{
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -0800627 /* batt_temp is in tenths of degC - convert it to degC for lookups */
628 batt_temp = batt_temp/10;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700629 return interpolate_single_lut(chip->adjusted_fcc_temp_lut, batt_temp);
630}
631
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632static int interpolate_scalingfactor_fcc(struct pm8921_bms_chip *chip,
633 int cycles)
634{
Abhijeet Dharmapurikar205bbc02011-11-04 14:47:36 -0700635 /*
636 * sf table could be null when no battery aging data is available, in
637 * that case return 100%
638 */
639 if (chip->fcc_sf_lut)
640 return interpolate_single_lut(chip->fcc_sf_lut, cycles);
641 else
642 return 100;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700643}
644
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800645static int interpolate_scalingfactor(struct pm8921_bms_chip *chip,
646 struct sf_lut *sf_lut,
647 int row_entry, int pc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648{
Abhijeet Dharmapurikar4a4456e2011-09-27 15:10:38 -0700649 int i, scalefactorrow1, scalefactorrow2, scalefactor;
Abhijeet Dharmapurikar205bbc02011-11-04 14:47:36 -0700650 int rows, cols;
Abhijeet Dharmapurikar4a4456e2011-09-27 15:10:38 -0700651 int row1 = 0;
652 int row2 = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700653
Abhijeet Dharmapurikar205bbc02011-11-04 14:47:36 -0700654 /*
655 * sf table could be null when no battery aging data is available, in
656 * that case return 100%
657 */
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800658 if (!sf_lut)
Abhijeet Dharmapurikar205bbc02011-11-04 14:47:36 -0700659 return 100;
660
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800661 rows = sf_lut->rows;
662 cols = sf_lut->cols;
663 if (pc > sf_lut->percent[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700664 pr_debug("pc %d greater than known pc ranges for sfd\n", pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665 row1 = 0;
666 row2 = 0;
667 }
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800668 if (pc < sf_lut->percent[rows - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700669 pr_debug("pc %d less than known pc ranges for sf", pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700670 row1 = rows - 1;
671 row2 = rows - 1;
672 }
673 for (i = 0; i < rows; i++) {
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800674 if (pc == sf_lut->percent[i]) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700675 row1 = i;
676 row2 = i;
677 break;
678 }
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800679 if (pc > sf_lut->percent[i]) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700680 row1 = i - 1;
681 row2 = i;
682 break;
683 }
684 }
685
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800686 if (row_entry < sf_lut->row_entries[0])
687 row_entry = sf_lut->row_entries[0];
688 if (row_entry > sf_lut->row_entries[cols - 1])
689 row_entry = sf_lut->row_entries[cols - 1];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700690
691 for (i = 0; i < cols; i++)
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800692 if (row_entry <= sf_lut->row_entries[i])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700693 break;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800694 if (row_entry == sf_lut->row_entries[i]) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700695 scalefactor = linear_interpolate(
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800696 sf_lut->sf[row1][i],
697 sf_lut->percent[row1],
698 sf_lut->sf[row2][i],
699 sf_lut->percent[row2],
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700 pc);
701 return scalefactor;
702 }
703
704 scalefactorrow1 = linear_interpolate(
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800705 sf_lut->sf[row1][i - 1],
706 sf_lut->row_entries[i - 1],
707 sf_lut->sf[row1][i],
708 sf_lut->row_entries[i],
709 row_entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710
711 scalefactorrow2 = linear_interpolate(
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800712 sf_lut->sf[row2][i - 1],
713 sf_lut->row_entries[i - 1],
714 sf_lut->sf[row2][i],
715 sf_lut->row_entries[i],
716 row_entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717
718 scalefactor = linear_interpolate(
719 scalefactorrow1,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800720 sf_lut->percent[row1],
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700721 scalefactorrow2,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800722 sf_lut->percent[row2],
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723 pc);
724
725 return scalefactor;
726}
727
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700728static int is_between(int left, int right, int value)
729{
730 if (left >= right && left >= value && value >= right)
731 return 1;
732 if (left <= right && left <= value && value <= right)
733 return 1;
734
735 return 0;
736}
737
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700738static int interpolate_pc(struct pm8921_bms_chip *chip,
739 int batt_temp, int ocv)
740{
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700741 int i, j, pcj, pcj_minus_one, pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700742 int rows = chip->pc_temp_ocv_lut->rows;
743 int cols = chip->pc_temp_ocv_lut->cols;
744
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -0800745 /* batt_temp is in tenths of degC - convert it to degC for lookups */
746 batt_temp = batt_temp/10;
747
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748 if (batt_temp < chip->pc_temp_ocv_lut->temp[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700749 pr_debug("batt_temp %d < known temp range for pc\n", batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700750 batt_temp = chip->pc_temp_ocv_lut->temp[0];
751 }
752 if (batt_temp > chip->pc_temp_ocv_lut->temp[cols - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700753 pr_debug("batt_temp %d > known temp range for pc\n", batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754 batt_temp = chip->pc_temp_ocv_lut->temp[cols - 1];
755 }
756
757 for (j = 0; j < cols; j++)
758 if (batt_temp <= chip->pc_temp_ocv_lut->temp[j])
759 break;
760 if (batt_temp == chip->pc_temp_ocv_lut->temp[j]) {
761 /* found an exact match for temp in the table */
762 if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j])
763 return chip->pc_temp_ocv_lut->percent[0];
764 if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j])
765 return chip->pc_temp_ocv_lut->percent[rows - 1];
766 for (i = 0; i < rows; i++) {
767 if (ocv >= chip->pc_temp_ocv_lut->ocv[i][j]) {
768 if (ocv == chip->pc_temp_ocv_lut->ocv[i][j])
769 return
770 chip->pc_temp_ocv_lut->percent[i];
771 pc = linear_interpolate(
772 chip->pc_temp_ocv_lut->percent[i],
773 chip->pc_temp_ocv_lut->ocv[i][j],
774 chip->pc_temp_ocv_lut->percent[i - 1],
775 chip->pc_temp_ocv_lut->ocv[i - 1][j],
776 ocv);
777 return pc;
778 }
779 }
780 }
781
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700782 /*
783 * batt_temp is within temperature for
784 * column j-1 and j
785 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700786 if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j])
787 return chip->pc_temp_ocv_lut->percent[0];
788 if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j - 1])
789 return chip->pc_temp_ocv_lut->percent[rows - 1];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700790
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700791 pcj_minus_one = 0;
792 pcj = 0;
793 for (i = 0; i < rows-1; i++) {
794 if (pcj == 0
795 && is_between(chip->pc_temp_ocv_lut->ocv[i][j],
796 chip->pc_temp_ocv_lut->ocv[i+1][j], ocv)) {
797 pcj = linear_interpolate(
798 chip->pc_temp_ocv_lut->percent[i],
799 chip->pc_temp_ocv_lut->ocv[i][j],
800 chip->pc_temp_ocv_lut->percent[i + 1],
801 chip->pc_temp_ocv_lut->ocv[i+1][j],
802 ocv);
803 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700804
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700805 if (pcj_minus_one == 0
806 && is_between(chip->pc_temp_ocv_lut->ocv[i][j-1],
807 chip->pc_temp_ocv_lut->ocv[i+1][j-1], ocv)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700808
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700809 pcj_minus_one = linear_interpolate(
810 chip->pc_temp_ocv_lut->percent[i],
811 chip->pc_temp_ocv_lut->ocv[i][j-1],
812 chip->pc_temp_ocv_lut->percent[i + 1],
813 chip->pc_temp_ocv_lut->ocv[i+1][j-1],
814 ocv);
815 }
816
817 if (pcj && pcj_minus_one) {
818 pc = linear_interpolate(
819 pcj_minus_one,
820 chip->pc_temp_ocv_lut->temp[j-1],
821 pcj,
822 chip->pc_temp_ocv_lut->temp[j],
823 batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700824 return pc;
825 }
826 }
827
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700828 if (pcj)
829 return pcj;
830
831 if (pcj_minus_one)
832 return pcj_minus_one;
833
834 pr_debug("%d ocv wasn't found for temp %d in the LUT returning 100%%",
835 ocv, batt_temp);
836 return 100;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700837}
838
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800839#define BMS_MODE_BIT BIT(6)
840#define EN_VBAT_BIT BIT(5)
841#define OVERRIDE_MODE_DELAY_MS 20
842int pm8921_bms_get_simultaneous_battery_voltage_and_current(int *ibat_ua,
843 int *vbat_uv)
844{
845 int16_t vsense_raw;
846 int16_t vbat_raw;
847 int vsense_uv;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800848 int usb_chg;
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800849
850 if (the_chip == NULL) {
851 pr_err("Called to early\n");
852 return -EINVAL;
853 }
854
855 mutex_lock(&the_chip->bms_output_lock);
856
857 pm8xxx_writeb(the_chip->dev->parent, BMS_S1_DELAY, 0x00);
858 pm_bms_masked_write(the_chip, BMS_CONTROL,
859 BMS_MODE_BIT | EN_VBAT_BIT, BMS_MODE_BIT | EN_VBAT_BIT);
860
861 msleep(OVERRIDE_MODE_DELAY_MS);
862
863 pm_bms_lock_output_data(the_chip);
864 pm_bms_read_output_data(the_chip, VSENSE_AVG, &vsense_raw);
865 pm_bms_read_output_data(the_chip, VBATT_AVG, &vbat_raw);
866 pm_bms_unlock_output_data(the_chip);
867 pm_bms_masked_write(the_chip, BMS_CONTROL,
868 BMS_MODE_BIT | EN_VBAT_BIT, 0);
869
870 pm8xxx_writeb(the_chip->dev->parent, BMS_S1_DELAY, 0x0B);
871
872 mutex_unlock(&the_chip->bms_output_lock);
873
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800874 usb_chg = usb_chg_plugged_in();
875
876 convert_vbatt_raw_to_uv(the_chip, usb_chg, vbat_raw, vbat_uv);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800877 convert_vsense_to_uv(the_chip, vsense_raw, &vsense_uv);
878 *ibat_ua = vsense_uv * 1000 / (int)the_chip->r_sense;
879
880 pr_debug("vsense_raw = 0x%x vbat_raw = 0x%x"
881 " ibat_ua = %d vbat_uv = %d\n",
882 (uint16_t)vsense_raw, (uint16_t)vbat_raw,
883 *ibat_ua, *vbat_uv);
884 return 0;
885}
886EXPORT_SYMBOL(pm8921_bms_get_simultaneous_battery_voltage_and_current);
887
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800888static int read_rbatt_params_raw(struct pm8921_bms_chip *chip,
889 struct pm8921_rbatt_params *raw)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890{
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800891 int usb_chg;
892
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800893 mutex_lock(&chip->bms_output_lock);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800894 pm_bms_lock_output_data(chip);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700895
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800896 pm_bms_read_output_data(chip,
897 OCV_FOR_RBATT, &raw->ocv_for_rbatt_raw);
898 pm_bms_read_output_data(chip,
899 VBATT_FOR_RBATT, &raw->vbatt_for_rbatt_raw);
900 pm_bms_read_output_data(chip,
901 VSENSE_FOR_RBATT, &raw->vsense_for_rbatt_raw);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700902
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800903 pm_bms_unlock_output_data(chip);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800904 mutex_unlock(&chip->bms_output_lock);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800905
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800906 usb_chg = usb_chg_plugged_in();
907 convert_vbatt_raw_to_uv(chip, usb_chg,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800908 raw->vbatt_for_rbatt_raw, &raw->vbatt_for_rbatt_uv);
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800909 convert_vbatt_raw_to_uv(chip, usb_chg,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800910 raw->ocv_for_rbatt_raw, &raw->ocv_for_rbatt_uv);
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800911 convert_vsense_to_uv(chip, raw->vsense_for_rbatt_raw,
912 &raw->vsense_for_rbatt_uv);
Abhijeet Dharmapurikar1b8e8292012-01-17 11:01:44 -0800913
914 pr_debug("vbatt_for_rbatt_raw = 0x%x, vbatt_for_rbatt= %duV\n",
915 raw->vbatt_for_rbatt_raw, raw->vbatt_for_rbatt_uv);
916 pr_debug("ocv_for_rbatt_raw = 0x%x, ocv_for_rbatt= %duV\n",
917 raw->ocv_for_rbatt_raw, raw->ocv_for_rbatt_uv);
918 pr_debug("vsense_for_rbatt_raw = 0x%x, vsense_for_rbatt= %duV\n",
919 raw->vsense_for_rbatt_raw, raw->vsense_for_rbatt_uv);
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800920 return 0;
921}
922
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700923#define MBG_TRANSIENT_ERROR_RAW 51
924static void adjust_pon_ocv_raw(struct pm8921_bms_chip *chip,
925 struct pm8921_soc_params *raw)
926{
927 /* in 8921 parts the PON ocv is taken when the MBG is not settled.
928 * decrease the pon ocv by 15mV raw value to account for it
929 * Since a 1/3rd of vbatt is supplied to the adc the raw value
930 * needs to be adjusted by 5mV worth bits
931 */
932 if (raw->last_good_ocv_raw >= MBG_TRANSIENT_ERROR_RAW)
933 raw->last_good_ocv_raw -= MBG_TRANSIENT_ERROR_RAW;
934}
935
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800936static int read_soc_params_raw(struct pm8921_bms_chip *chip,
937 struct pm8921_soc_params *raw)
938{
939 int usb_chg;
940
941 mutex_lock(&chip->bms_output_lock);
942 pm_bms_lock_output_data(chip);
943
944 pm_bms_read_output_data(chip,
945 LAST_GOOD_OCV_VALUE, &raw->last_good_ocv_raw);
946 read_cc(chip, &raw->cc);
947
948 pm_bms_unlock_output_data(chip);
949 mutex_unlock(&chip->bms_output_lock);
950
951 usb_chg = usb_chg_plugged_in();
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -0700952
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700953 if (chip->prev_last_good_ocv_raw == 0) {
954 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
955 adjust_pon_ocv_raw(chip, raw);
956 convert_vbatt_raw_to_uv(chip, usb_chg,
957 raw->last_good_ocv_raw, &raw->last_good_ocv_uv);
958 last_ocv_uv = raw->last_good_ocv_uv;
959 } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) {
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -0700960 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
961 convert_vbatt_raw_to_uv(chip, usb_chg,
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800962 raw->last_good_ocv_raw, &raw->last_good_ocv_uv);
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -0700963 last_ocv_uv = raw->last_good_ocv_uv;
964 } else {
965 raw->last_good_ocv_uv = last_ocv_uv;
966 }
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800967
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800968 pr_debug("0p625 = %duV\n", chip->xoadc_v0625);
969 pr_debug("1p25 = %duV\n", chip->xoadc_v125);
Abhijeet Dharmapurikar1b8e8292012-01-17 11:01:44 -0800970 pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n",
971 raw->last_good_ocv_raw, raw->last_good_ocv_uv);
972 pr_debug("cc_raw= 0x%x\n", raw->cc);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800973 return 0;
974}
975
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800976static int get_rbatt(struct pm8921_bms_chip *chip, int soc_rbatt, int batt_temp)
977{
978 int rbatt, scalefactor;
979
980 rbatt = (last_rbatt < 0) ? chip->default_rbatt_mohm : last_rbatt;
981 pr_debug("rbatt before scaling = %d\n", rbatt);
982 if (chip->rbatt_sf_lut == NULL) {
983 pr_debug("RBATT = %d\n", rbatt);
984 return rbatt;
985 }
986
987 scalefactor = interpolate_scalingfactor(chip, chip->rbatt_sf_lut,
988 batt_temp, soc_rbatt);
989 pr_debug("rbatt sf = %d for batt_temp = %d, soc_rbatt = %d\n",
990 scalefactor, batt_temp, soc_rbatt);
991 rbatt = (rbatt * scalefactor) / 100;
992
993 pr_debug("RBATT = %d\n", rbatt);
994 return rbatt;
995}
996
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800997static int calculate_rbatt_resume(struct pm8921_bms_chip *chip,
998 struct pm8921_rbatt_params *raw)
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800999{
1000 unsigned int r_batt;
1001
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08001002 if (raw->ocv_for_rbatt_uv <= 0
1003 || raw->ocv_for_rbatt_uv <= raw->vbatt_for_rbatt_uv
1004 || raw->vsense_for_rbatt_raw <= 0) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001005 pr_debug("rbatt readings unavailable ocv = %d, vbatt = %d,"
1006 "vsen = %d\n",
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001007 raw->ocv_for_rbatt_uv,
1008 raw->vbatt_for_rbatt_uv,
1009 raw->vsense_for_rbatt_raw);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001010 return -EINVAL;
1011 }
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001012 r_batt = ((raw->ocv_for_rbatt_uv - raw->vbatt_for_rbatt_uv)
1013 * chip->r_sense) / raw->vsense_for_rbatt_uv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014 pr_debug("r_batt = %umilliOhms", r_batt);
1015 return r_batt;
1016}
1017
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001018static int calculate_fcc_uah(struct pm8921_bms_chip *chip, int batt_temp,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001019 int chargecycles)
1020{
1021 int initfcc, result, scalefactor = 0;
1022
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001023 if (chip->adjusted_fcc_temp_lut == NULL) {
1024 initfcc = interpolate_fcc(chip, batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001025
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001026 scalefactor = interpolate_scalingfactor_fcc(chip, chargecycles);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001027
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001028 /* Multiply the initial FCC value by the scale factor. */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001029 result = (initfcc * scalefactor * 1000) / 100;
1030 pr_debug("fcc = %d uAh\n", result);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001031 return result;
1032 } else {
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001033 return 1000 * interpolate_fcc_adjusted(chip, batt_temp);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001034 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001035}
1036
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001037static int get_battery_uvolts(struct pm8921_bms_chip *chip, int *uvolts)
1038{
1039 int rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001040 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001041
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001042 rc = pm8xxx_adc_read(chip->vbat_channel, &result);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001043 if (rc) {
1044 pr_err("error reading adc channel = %d, rc = %d\n",
1045 chip->vbat_channel, rc);
1046 return rc;
1047 }
1048 pr_debug("mvolts phy = %lld meas = 0x%llx", result.physical,
1049 result.measurement);
1050 *uvolts = (int)result.physical;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001051 return 0;
1052}
1053
1054static int adc_based_ocv(struct pm8921_bms_chip *chip, int *ocv)
1055{
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001056 int vbatt, rbatt, ibatt_ua, rc;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001057
1058 rc = get_battery_uvolts(chip, &vbatt);
1059 if (rc) {
1060 pr_err("failed to read vbatt from adc rc = %d\n", rc);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001061 return rc;
1062 }
1063
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001064 rc = pm8921_bms_get_battery_current(&ibatt_ua);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001065 if (rc) {
1066 pr_err("failed to read batt current rc = %d\n", rc);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001067 return rc;
1068 }
1069
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001070 rbatt = (last_rbatt < 0) ? chip->default_rbatt_mohm : last_rbatt;
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001071 *ocv = vbatt + (ibatt_ua * rbatt)/1000;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001072 return 0;
1073}
1074
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001075static int calculate_pc(struct pm8921_bms_chip *chip, int ocv_uv, int batt_temp,
1076 int chargecycles)
1077{
1078 int pc, scalefactor;
1079
1080 pc = interpolate_pc(chip, batt_temp, ocv_uv / 1000);
1081 pr_debug("pc = %u for ocv = %dmicroVolts batt_temp = %d\n",
1082 pc, ocv_uv, batt_temp);
1083
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001084 scalefactor = interpolate_scalingfactor(chip,
1085 chip->pc_sf_lut, chargecycles, pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001086 pr_debug("scalefactor = %u batt_temp = %d\n", scalefactor, batt_temp);
1087
1088 /* Multiply the initial FCC value by the scale factor. */
1089 pc = (pc * scalefactor) / 100;
1090 return pc;
1091}
1092
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001093/**
1094 * calculate_cc_uah -
1095 * @chip: the bms chip pointer
1096 * @cc: the cc reading from bms h/w
1097 * @val: return value
1098 * @coulumb_counter: adjusted coulumb counter for 100%
1099 *
1100 * RETURNS: in val pointer coulumb counter based charger in uAh
1101 * (micro Amp hour)
1102 */
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001103static void calculate_cc_uah(struct pm8921_bms_chip *chip, int cc, int *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001104{
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001105 int64_t cc_voltage_uv, cc_nvh, cc_uah;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001106
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001107 cc_voltage_uv = cc;
1108 cc_voltage_uv -= chip->cc_reading_at_100;
1109 pr_debug("cc = %d. after subtracting %d cc = %lld\n",
1110 cc, chip->cc_reading_at_100,
1111 cc_voltage_uv);
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -07001112 cc_voltage_uv = cc_to_microvolt(chip, cc_voltage_uv);
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08001113 cc_voltage_uv = pm8xxx_cc_adjust_for_gain(cc_voltage_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001114 pr_debug("cc_voltage_uv = %lld microvolts\n", cc_voltage_uv);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001115 cc_nvh = ccmicrovolt_to_nvh(cc_voltage_uv);
1116 pr_debug("cc_nvh = %lld nano_volt_hour\n", cc_nvh);
1117 cc_uah = div_s64(cc_nvh, chip->r_sense);
1118 *val = cc_uah;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001119}
1120
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001121static int calculate_unusable_charge_uah(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001122 int rbatt, int fcc_uah,
1123 int batt_temp, int chargecycles)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001124{
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001125 int voltage_unusable_uv, pc_unusable;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001126
1127 /* calculate unusable charge */
1128 voltage_unusable_uv = (rbatt * chip->i_test)
1129 + (chip->v_failure * 1000);
1130 pc_unusable = calculate_pc(chip, voltage_unusable_uv,
1131 batt_temp, chargecycles);
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001132 pr_debug("rbatt = %umilliOhms unusable_v =%d unusable_pc = %d\n",
1133 rbatt, voltage_unusable_uv, pc_unusable);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001134 return (fcc_uah * pc_unusable) / 100;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001135}
Abhijeet Dharmapurikar52aa9682011-07-20 14:05:47 -07001136
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001137/* calculate remainging charge at the time of ocv */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001138static int calculate_remaining_charge_uah(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001139 struct pm8921_soc_params *raw,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001140 int fcc_uah, int batt_temp,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001141 int chargecycles)
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001142{
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001143 int ocv, pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001144
1145 /* calculate remainging charge */
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001146 ocv = 0;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001147 if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw) {
1148 chip->ocv_reading_at_100 = 0;
1149 chip->cc_reading_at_100 = 0;
1150 ocv = raw->last_good_ocv_uv;
1151 } else {
1152 /*
1153 * force 100% ocv by selecting the highest voltage the
1154 * battery could every reach
1155 */
1156 ocv = chip->max_voltage_uv;
1157 }
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001158
1159 if (ocv == 0) {
1160 ocv = last_ocv_uv;
1161 pr_debug("ocv not available using last_ocv_uv=%d\n", ocv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001162 }
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001163
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001164 pc = calculate_pc(chip, ocv, batt_temp, chargecycles);
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001165 pr_debug("ocv = %d pc = %d\n", ocv, pc);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001166 return (fcc_uah * pc) / 100;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001167}
1168
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001169static void calculate_soc_params(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001170 struct pm8921_soc_params *raw,
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001171 int batt_temp, int chargecycles,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001172 int *fcc_uah,
1173 int *unusable_charge_uah,
1174 int *remaining_charge_uah,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001175 int *cc_uah,
1176 int *rbatt)
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001177{
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001178 unsigned long flags;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001179 int soc_rbatt;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001180
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001181 *fcc_uah = calculate_fcc_uah(chip, batt_temp, chargecycles);
1182 pr_debug("FCC = %uuAh batt_temp = %d, cycles = %d\n",
1183 *fcc_uah, batt_temp, chargecycles);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001184
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001185
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001186 spin_lock_irqsave(&chip->bms_100_lock, flags);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001187 /* calculate remainging charge */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001188 *remaining_charge_uah = calculate_remaining_charge_uah(chip, raw,
1189 *fcc_uah, batt_temp, chargecycles);
1190 pr_debug("RC = %uuAh\n", *remaining_charge_uah);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001191
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001192 /* calculate cc micro_volt_hour */
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001193 calculate_cc_uah(chip, raw->cc, cc_uah);
1194 pr_debug("cc_uah = %duAh raw->cc = %x cc = %lld after subtracting %d\n",
1195 *cc_uah, raw->cc,
1196 (int64_t)raw->cc - chip->cc_reading_at_100,
1197 chip->cc_reading_at_100);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001198 spin_unlock_irqrestore(&chip->bms_100_lock, flags);
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001199
1200 soc_rbatt = ((*remaining_charge_uah - *cc_uah) * 100) / *fcc_uah;
1201 if (soc_rbatt < 0)
1202 soc_rbatt = 0;
1203 *rbatt = get_rbatt(chip, soc_rbatt, batt_temp);
1204
1205 *unusable_charge_uah = calculate_unusable_charge_uah(chip, *rbatt,
1206 *fcc_uah, batt_temp, chargecycles);
1207 pr_debug("UUC = %uuAh\n", *unusable_charge_uah);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001208}
1209
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001210static int calculate_real_fcc_uah(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001211 struct pm8921_soc_params *raw,
1212 int batt_temp, int chargecycles,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001213 int *ret_fcc_uah)
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001214{
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001215 int fcc_uah, unusable_charge_uah;
1216 int remaining_charge_uah;
1217 int cc_uah;
1218 int real_fcc_uah;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001219 int rbatt;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001220
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001221 calculate_soc_params(chip, raw, batt_temp, chargecycles,
1222 &fcc_uah,
1223 &unusable_charge_uah,
1224 &remaining_charge_uah,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001225 &cc_uah,
1226 &rbatt);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001227
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001228 real_fcc_uah = remaining_charge_uah - cc_uah;
1229 *ret_fcc_uah = fcc_uah;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001230 pr_debug("real_fcc = %d, RC = %d CC = %d fcc = %d\n",
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001231 real_fcc_uah, remaining_charge_uah, cc_uah, fcc_uah);
1232 return real_fcc_uah;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001233}
1234/*
1235 * Remaining Usable Charge = remaining_charge (charge at ocv instance)
1236 * - coloumb counter charge
1237 * - unusable charge (due to battery resistance)
1238 * SOC% = (remaining usable charge/ fcc - usable_charge);
1239 */
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001240static int calculate_state_of_charge(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001241 struct pm8921_soc_params *raw,
1242 int batt_temp, int chargecycles)
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001243{
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001244 int remaining_usable_charge_uah, fcc_uah, unusable_charge_uah;
1245 int remaining_charge_uah, soc;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001246 int update_userspace = 1;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001247 int cc_uah;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001248 int rbatt;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001249
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001250 calculate_soc_params(chip, raw, batt_temp, chargecycles,
1251 &fcc_uah,
1252 &unusable_charge_uah,
1253 &remaining_charge_uah,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001254 &cc_uah,
1255 &rbatt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001256
1257 /* calculate remaining usable charge */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001258 remaining_usable_charge_uah = remaining_charge_uah
1259 - cc_uah
1260 - unusable_charge_uah;
1261
1262 pr_debug("RUC = %duAh\n", remaining_usable_charge_uah);
Abhijeet Dharmapurikarbbae88312012-02-14 18:56:42 -08001263 if (fcc_uah - unusable_charge_uah <= 0) {
1264 pr_warn("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
1265 fcc_uah, unusable_charge_uah);
1266 soc = 0;
1267 } else {
1268 soc = (remaining_usable_charge_uah * 100)
1269 / (fcc_uah - unusable_charge_uah);
1270 }
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001271
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001272 if (soc > 100)
1273 soc = 100;
1274 pr_debug("SOC = %u%%\n", soc);
1275
Abhijeet Dharmapurikar986bd8a2012-01-05 15:15:15 -08001276 if (bms_fake_battery != -EINVAL) {
1277 pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery);
1278 return bms_fake_battery;
1279 }
1280
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001281 if (soc < 0) {
Abhijeet Dharmapurikar21ebadd2011-09-28 21:14:16 -07001282 pr_err("bad rem_usb_chg = %d rem_chg %d,"
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001283 "cc_uah %d, unusb_chg %d\n",
1284 remaining_usable_charge_uah,
1285 remaining_charge_uah,
1286 cc_uah, unusable_charge_uah);
1287
Abhijeet Dharmapurikar21ebadd2011-09-28 21:14:16 -07001288 pr_err("for bad rem_usb_chg last_ocv_uv = %d"
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001289 "chargecycles = %d, batt_temp = %d"
1290 "fcc = %d soc =%d\n",
1291 last_ocv_uv, chargecycles, batt_temp,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001292 fcc_uah, soc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001293 update_userspace = 0;
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -08001294 soc = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001295 }
1296
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001297 if (last_soc == -EINVAL || soc <= last_soc) {
1298 last_soc = update_userspace ? soc : last_soc;
Abhijeet Dharmapurikar986bd8a2012-01-05 15:15:15 -08001299 return soc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001300 }
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001301
1302 /*
1303 * soc > last_soc
1304 * the device must be charging for reporting a higher soc, if not ignore
1305 * this soc and continue reporting the last_soc
1306 */
Abhijeet Dharmapurikardba91f42011-12-21 16:56:13 -08001307 if (the_chip->start_percent != -EINVAL) {
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001308 last_soc = soc;
1309 } else {
1310 pr_debug("soc = %d reporting last_soc = %d\n", soc, last_soc);
1311 soc = last_soc;
1312 }
1313
Abhijeet Dharmapurikar986bd8a2012-01-05 15:15:15 -08001314 return soc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001315}
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08001316#define MIN_DELTA_625_UV 1000
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001317static void calib_hkadc(struct pm8921_bms_chip *chip)
1318{
1319 int voltage, rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001320 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08001321 int usb_chg;
1322 int this_delta;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001323
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001324 rc = pm8xxx_adc_read(the_chip->ref1p25v_channel, &result);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001325 if (rc) {
1326 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
1327 return;
1328 }
Abhijeet Dharmapurikar619cb0c2011-12-15 15:28:14 -08001329 voltage = xoadc_reading_to_microvolt(result.adc_code);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001330
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07001331 pr_debug("result 1.25v = 0x%x, voltage = %duV adc_meas = %lld\n",
1332 result.adc_code, voltage, result.measurement);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001333
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001334 chip->xoadc_v125 = voltage;
1335
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001336 rc = pm8xxx_adc_read(the_chip->ref625mv_channel, &result);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001337 if (rc) {
1338 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
1339 return;
1340 }
Abhijeet Dharmapurikar619cb0c2011-12-15 15:28:14 -08001341 voltage = xoadc_reading_to_microvolt(result.adc_code);
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08001342
1343 usb_chg = usb_chg_plugged_in();
1344 pr_debug("result 0.625V = 0x%x, voltage = %duV adc_meas = %lld "
1345 "usb_chg = %d\n",
1346 result.adc_code, voltage, result.measurement,
1347 usb_chg);
1348
1349 if (usb_chg)
1350 chip->xoadc_v0625_usb_present = voltage;
1351 else
1352 chip->xoadc_v0625_usb_absent = voltage;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001353
1354 chip->xoadc_v0625 = voltage;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08001355 if (chip->xoadc_v0625_usb_present && chip->xoadc_v0625_usb_absent) {
1356 this_delta = chip->xoadc_v0625_usb_present
1357 - chip->xoadc_v0625_usb_absent;
1358 pr_debug("this_delta= %duV\n", this_delta);
1359 if (this_delta > MIN_DELTA_625_UV)
1360 last_usb_cal_delta_uv = this_delta;
1361 pr_debug("625V_present= %d, 625V_absent= %d, delta = %duV\n",
1362 chip->xoadc_v0625_usb_present,
1363 chip->xoadc_v0625_usb_absent,
1364 last_usb_cal_delta_uv);
1365 }
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001366}
1367
1368static void calibrate_hkadc_work(struct work_struct *work)
1369{
1370 struct pm8921_bms_chip *chip = container_of(work,
1371 struct pm8921_bms_chip, calib_hkadc_work);
1372
1373 calib_hkadc(chip);
1374}
1375
Abhijeet Dharmapurikar1b8e8292012-01-17 11:01:44 -08001376void pm8921_bms_calibrate_hkadc(void)
1377{
1378 schedule_work(&the_chip->calib_hkadc_work);
1379}
1380
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001381static void calibrate_ccadc_work(struct work_struct *work)
1382{
1383 struct pm8921_bms_chip *chip = container_of(work,
1384 struct pm8921_bms_chip, calib_ccadc_work.work);
1385
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08001386 pm8xxx_calib_ccadc();
Abhijeet Dharmapurikar1b8e8292012-01-17 11:01:44 -08001387 calib_hkadc(chip);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001388 schedule_delayed_work(&chip->calib_ccadc_work,
1389 round_jiffies_relative(msecs_to_jiffies
1390 (chip->calib_delay_ms)));
1391}
1392
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001393int pm8921_bms_get_vsense_avg(int *result)
1394{
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001395 int rc = -EINVAL;
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001396
1397 if (the_chip) {
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08001398 mutex_lock(&the_chip->bms_output_lock);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001399 pm_bms_lock_output_data(the_chip);
1400 rc = read_vsense_avg(the_chip, result);
1401 pm_bms_unlock_output_data(the_chip);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08001402 mutex_unlock(&the_chip->bms_output_lock);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001403 }
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001404
1405 pr_err("called before initialization\n");
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001406 return rc;
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07001407}
1408EXPORT_SYMBOL(pm8921_bms_get_vsense_avg);
1409
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001410int pm8921_bms_get_battery_current(int *result_ua)
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001411{
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001412 int vsense;
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001413
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001414 if (!the_chip) {
1415 pr_err("called before initialization\n");
1416 return -EINVAL;
1417 }
1418 if (the_chip->r_sense == 0) {
1419 pr_err("r_sense is zero\n");
1420 return -EINVAL;
1421 }
1422
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08001423 mutex_lock(&the_chip->bms_output_lock);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001424 pm_bms_lock_output_data(the_chip);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001425 read_vsense_avg(the_chip, &vsense);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07001426 pm_bms_unlock_output_data(the_chip);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08001427 mutex_unlock(&the_chip->bms_output_lock);
1428 pr_debug("vsense=%duV\n", vsense);
Abhijeet Dharmapurikara7a1c6b2011-08-17 10:04:58 -07001429 /* cast for signed division */
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001430 *result_ua = vsense * 1000 / (int)the_chip->r_sense;
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08001431 pr_debug("ibat=%duA\n", *result_ua);
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001432 return 0;
1433}
1434EXPORT_SYMBOL(pm8921_bms_get_battery_current);
1435
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001436int pm8921_bms_get_percent_charge(void)
1437{
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001438 int batt_temp, rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001439 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001440 struct pm8921_soc_params raw;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001441
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001442 if (!the_chip) {
1443 pr_err("called before initialization\n");
1444 return -EINVAL;
1445 }
1446
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001447 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07001448 if (rc) {
1449 pr_err("error reading adc channel = %d, rc = %d\n",
1450 the_chip->batt_temp_channel, rc);
1451 return rc;
1452 }
1453 pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical,
1454 result.measurement);
1455 batt_temp = (int)result.physical;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001456
1457 read_soc_params_raw(the_chip, &raw);
1458
1459 return calculate_state_of_charge(the_chip, &raw,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001460 batt_temp, last_chargecycles);
1461}
1462EXPORT_SYMBOL_GPL(pm8921_bms_get_percent_charge);
1463
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001464int pm8921_bms_get_rbatt(void)
1465{
1466 int batt_temp, rc;
1467 struct pm8xxx_adc_chan_result result;
1468 struct pm8921_soc_params raw;
1469 int fcc_uah;
1470 int unusable_charge_uah;
1471 int remaining_charge_uah;
1472 int cc_uah;
1473 int rbatt;
1474
1475 if (!the_chip) {
1476 pr_err("called before initialization\n");
1477 return -EINVAL;
1478 }
1479
1480 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
1481 if (rc) {
1482 pr_err("error reading adc channel = %d, rc = %d\n",
1483 the_chip->batt_temp_channel, rc);
1484 return rc;
1485 }
1486 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
1487 result.measurement);
1488 batt_temp = (int)result.physical;
1489
1490 read_soc_params_raw(the_chip, &raw);
1491
1492 calculate_soc_params(the_chip, &raw, batt_temp, last_chargecycles,
1493 &fcc_uah,
1494 &unusable_charge_uah,
1495 &remaining_charge_uah,
1496 &cc_uah,
1497 &rbatt);
1498 return rbatt;
1499}
1500EXPORT_SYMBOL_GPL(pm8921_bms_get_rbatt);
1501
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07001502int pm8921_bms_get_fcc(void)
1503{
1504 int batt_temp, rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001505 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07001506
1507 if (!the_chip) {
1508 pr_err("called before initialization\n");
1509 return -EINVAL;
1510 }
1511
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001512 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07001513 if (rc) {
1514 pr_err("error reading adc channel = %d, rc = %d\n",
1515 the_chip->batt_temp_channel, rc);
1516 return rc;
1517 }
1518 pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical,
1519 result.measurement);
1520 batt_temp = (int)result.physical;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001521 return calculate_fcc_uah(the_chip, batt_temp, last_chargecycles);
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07001522}
1523EXPORT_SYMBOL_GPL(pm8921_bms_get_fcc);
1524
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -08001525#define IBAT_TOL_MASK 0x0F
1526#define OCV_TOL_MASK 0xF0
1527#define IBAT_TOL_DEFAULT 0x03
1528#define IBAT_TOL_NOCHG 0x0F
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001529void pm8921_bms_charging_began(void)
1530{
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001531 int batt_temp, rc;
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08001532 struct pm8xxx_adc_chan_result result;
1533 struct pm8921_soc_params raw;
1534
1535 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
1536 if (rc) {
1537 pr_err("error reading adc channel = %d, rc = %d\n",
1538 the_chip->batt_temp_channel, rc);
1539 return;
1540 }
1541 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
1542 result.measurement);
1543 batt_temp = (int)result.physical;
1544
1545 read_soc_params_raw(the_chip, &raw);
1546
1547 the_chip->start_percent = calculate_state_of_charge(the_chip, &raw,
1548 batt_temp, last_chargecycles);
1549 bms_start_percent = the_chip->start_percent;
1550 bms_start_ocv_uv = raw.last_good_ocv_uv;
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001551 calculate_cc_uah(the_chip, raw.cc, &bms_start_cc_uah);
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -08001552 pm_bms_masked_write(the_chip, BMS_TOLERANCES,
1553 IBAT_TOL_MASK, IBAT_TOL_DEFAULT);
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001554 pr_debug("start_percent = %u%%\n", the_chip->start_percent);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001555}
1556EXPORT_SYMBOL_GPL(pm8921_bms_charging_began);
1557
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08001558#define DELTA_FCC_PERCENT 3
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08001559#define MIN_START_PERCENT_FOR_LEARNING 30
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001560void pm8921_bms_charging_end(int is_battery_full)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001561{
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001562 int batt_temp, rc;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001563 struct pm8xxx_adc_chan_result result;
1564 struct pm8921_soc_params raw;
1565
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001566 if (the_chip == NULL)
1567 return;
1568
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001569 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
1570 if (rc) {
1571 pr_err("error reading adc channel = %d, rc = %d\n",
1572 the_chip->batt_temp_channel, rc);
1573 return;
1574 }
1575 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
1576 result.measurement);
1577 batt_temp = (int)result.physical;
1578
1579 read_soc_params_raw(the_chip, &raw);
1580
Abhijeet Dharmapurikar57aeb922012-01-25 13:22:26 -08001581 calculate_cc_uah(the_chip, raw.cc, &bms_end_cc_uah);
1582
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08001583 if (is_battery_full
1584 && the_chip->start_percent <= MIN_START_PERCENT_FOR_LEARNING) {
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -08001585 unsigned long flags;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001586 int fcc_uah, new_fcc_uah, delta_fcc_uah;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001587
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001588 new_fcc_uah = calculate_real_fcc_uah(the_chip, &raw,
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08001589 batt_temp, last_chargecycles,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001590 &fcc_uah);
1591 delta_fcc_uah = new_fcc_uah - fcc_uah;
1592 if (delta_fcc_uah < 0)
1593 delta_fcc_uah = -delta_fcc_uah;
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08001594
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08001595 if (delta_fcc_uah * 100 > (DELTA_FCC_PERCENT * fcc_uah)) {
1596 /* new_fcc_uah is outside the scope limit it */
1597 if (new_fcc_uah > fcc_uah)
1598 new_fcc_uah
1599 = (fcc_uah + DELTA_FCC_PERCENT * fcc_uah);
1600 else
1601 new_fcc_uah
1602 = (fcc_uah - DELTA_FCC_PERCENT * fcc_uah);
1603
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08001604 pr_debug("delta_fcc=%d > %d percent of fcc=%d"
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08001605 "restring it to %d\n",
1606 delta_fcc_uah, DELTA_FCC_PERCENT,
1607 fcc_uah, new_fcc_uah);
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08001608 }
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -08001609
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08001610 last_real_fcc_mah = new_fcc_uah/1000;
1611 last_real_fcc_batt_temp = batt_temp;
1612 readjust_fcc_table();
1613
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001614 spin_lock_irqsave(&the_chip->bms_100_lock, flags);
1615 the_chip->ocv_reading_at_100 = raw.last_good_ocv_raw;
1616 the_chip->cc_reading_at_100 = raw.cc;
1617 spin_unlock_irqrestore(&the_chip->bms_100_lock, flags);
1618 pr_debug("EOC ocv_reading = 0x%x cc = %d\n",
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -08001619 the_chip->ocv_reading_at_100,
1620 the_chip->cc_reading_at_100);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001621 }
1622
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001623 the_chip->end_percent = calculate_state_of_charge(the_chip, &raw,
1624 batt_temp, last_chargecycles);
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08001625
1626 bms_end_percent = the_chip->end_percent;
1627 bms_end_ocv_uv = raw.last_good_ocv_uv;
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08001628
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001629 if (the_chip->end_percent > the_chip->start_percent) {
Abhijeet Dharmapurikar8a113f82012-01-19 10:58:45 -08001630 last_charge_increase +=
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001631 the_chip->end_percent - the_chip->start_percent;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001632 if (last_charge_increase > 100) {
1633 last_chargecycles++;
1634 last_charge_increase = last_charge_increase % 100;
1635 }
1636 }
1637 pr_debug("end_percent = %u%% last_charge_increase = %d"
1638 "last_chargecycles = %d\n",
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07001639 the_chip->end_percent,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001640 last_charge_increase,
1641 last_chargecycles);
Abhijeet Dharmapurikardba91f42011-12-21 16:56:13 -08001642 the_chip->start_percent = -EINVAL;
1643 the_chip->end_percent = -EINVAL;
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -08001644 pm_bms_masked_write(the_chip, BMS_TOLERANCES,
1645 IBAT_TOL_MASK, IBAT_TOL_NOCHG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001646}
1647EXPORT_SYMBOL_GPL(pm8921_bms_charging_end);
1648
1649static irqreturn_t pm8921_bms_sbi_write_ok_handler(int irq, void *data)
1650{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001651 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001652 return IRQ_HANDLED;
1653}
1654
1655static irqreturn_t pm8921_bms_cc_thr_handler(int irq, void *data)
1656{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001657 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001658 return IRQ_HANDLED;
1659}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001660
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001661static irqreturn_t pm8921_bms_vsense_thr_handler(int irq, void *data)
1662{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001663 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001664 return IRQ_HANDLED;
1665}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001666
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001667static irqreturn_t pm8921_bms_vsense_for_r_handler(int irq, void *data)
1668{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001669 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001670 return IRQ_HANDLED;
1671}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001672
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001673static irqreturn_t pm8921_bms_ocv_for_r_handler(int irq, void *data)
1674{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001675 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001676
1677 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001678 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001679 return IRQ_HANDLED;
1680}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001681
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001682static irqreturn_t pm8921_bms_good_ocv_handler(int irq, void *data)
1683{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001684 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001685
1686 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001687 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001688 return IRQ_HANDLED;
1689}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001690
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001691static irqreturn_t pm8921_bms_vsense_avg_handler(int irq, void *data)
1692{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07001693 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001694 return IRQ_HANDLED;
1695}
1696
1697struct pm_bms_irq_init_data {
1698 unsigned int irq_id;
1699 char *name;
1700 unsigned long flags;
1701 irqreturn_t (*handler)(int, void *);
1702};
1703
1704#define BMS_IRQ(_id, _flags, _handler) \
1705{ \
1706 .irq_id = _id, \
1707 .name = #_id, \
1708 .flags = _flags, \
1709 .handler = _handler, \
1710}
1711
1712struct pm_bms_irq_init_data bms_irq_data[] = {
1713 BMS_IRQ(PM8921_BMS_SBI_WRITE_OK, IRQF_TRIGGER_RISING,
1714 pm8921_bms_sbi_write_ok_handler),
1715 BMS_IRQ(PM8921_BMS_CC_THR, IRQF_TRIGGER_RISING,
1716 pm8921_bms_cc_thr_handler),
1717 BMS_IRQ(PM8921_BMS_VSENSE_THR, IRQF_TRIGGER_RISING,
1718 pm8921_bms_vsense_thr_handler),
1719 BMS_IRQ(PM8921_BMS_VSENSE_FOR_R, IRQF_TRIGGER_RISING,
1720 pm8921_bms_vsense_for_r_handler),
1721 BMS_IRQ(PM8921_BMS_OCV_FOR_R, IRQF_TRIGGER_RISING,
1722 pm8921_bms_ocv_for_r_handler),
1723 BMS_IRQ(PM8921_BMS_GOOD_OCV, IRQF_TRIGGER_RISING,
1724 pm8921_bms_good_ocv_handler),
1725 BMS_IRQ(PM8921_BMS_VSENSE_AVG, IRQF_TRIGGER_RISING,
1726 pm8921_bms_vsense_avg_handler),
1727};
1728
1729static void free_irqs(struct pm8921_bms_chip *chip)
1730{
1731 int i;
1732
1733 for (i = 0; i < PM_BMS_MAX_INTS; i++)
1734 if (chip->pmic_bms_irq[i]) {
1735 free_irq(chip->pmic_bms_irq[i], NULL);
1736 chip->pmic_bms_irq[i] = 0;
1737 }
1738}
1739
1740static int __devinit request_irqs(struct pm8921_bms_chip *chip,
1741 struct platform_device *pdev)
1742{
1743 struct resource *res;
1744 int ret, i;
1745
1746 ret = 0;
1747 bitmap_fill(chip->enabled_irqs, PM_BMS_MAX_INTS);
1748
1749 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
1750 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1751 bms_irq_data[i].name);
1752 if (res == NULL) {
1753 pr_err("couldn't find %s\n", bms_irq_data[i].name);
1754 goto err_out;
1755 }
1756 ret = request_irq(res->start, bms_irq_data[i].handler,
1757 bms_irq_data[i].flags,
1758 bms_irq_data[i].name, chip);
1759 if (ret < 0) {
1760 pr_err("couldn't request %d (%s) %d\n", res->start,
1761 bms_irq_data[i].name, ret);
1762 goto err_out;
1763 }
1764 chip->pmic_bms_irq[bms_irq_data[i].irq_id] = res->start;
1765 pm8921_bms_disable_irq(chip, bms_irq_data[i].irq_id);
1766 }
1767 return 0;
1768
1769err_out:
1770 free_irqs(chip);
1771 return -EINVAL;
1772}
1773
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001774static int pm8921_bms_suspend(struct device *dev)
1775{
1776 int rc;
1777 struct pm8xxx_adc_chan_result result;
1778 struct pm8921_bms_chip *chip = dev_get_drvdata(dev);
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001779 struct pm8921_soc_params raw;
1780 int fcc_uah;
1781 int remaining_charge_uah;
1782 int cc_uah;
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001783
1784 chip->batt_temp_suspend = 0;
1785 rc = pm8xxx_adc_read(chip->batt_temp_channel, &result);
1786 if (rc) {
1787 pr_err("error reading adc channel = %d, rc = %d\n",
1788 chip->batt_temp_channel, rc);
1789 }
1790 chip->batt_temp_suspend = (int)result.physical;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001791 read_soc_params_raw(chip, &raw);
1792
1793 fcc_uah = calculate_fcc_uah(chip,
1794 chip->batt_temp_suspend, last_chargecycles);
1795 pr_debug("FCC = %uuAh batt_temp = %d, cycles = %d\n",
1796 fcc_uah, chip->batt_temp_suspend, last_chargecycles);
1797 /* calculate remainging charge */
1798 remaining_charge_uah = calculate_remaining_charge_uah(chip, &raw,
1799 fcc_uah, chip->batt_temp_suspend,
1800 last_chargecycles);
1801 pr_debug("RC = %uuAh\n", remaining_charge_uah);
1802
1803 /* calculate cc micro_volt_hour */
1804 calculate_cc_uah(chip, raw.cc, &cc_uah);
1805 pr_debug("cc_uah = %duAh raw->cc = %x cc = %lld after subtracting %d\n",
1806 cc_uah, raw.cc,
1807 (int64_t)raw.cc - chip->cc_reading_at_100,
1808 chip->cc_reading_at_100);
1809 chip->soc_rbatt_suspend = ((remaining_charge_uah - cc_uah) * 100)
1810 / fcc_uah;
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001811 return 0;
1812}
1813
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001814#define DELTA_RBATT_PERCENT 10
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001815static int pm8921_bms_resume(struct device *dev)
1816{
1817 struct pm8921_rbatt_params raw;
1818 struct pm8921_bms_chip *chip = dev_get_drvdata(dev);
1819 int rbatt;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001820 int expected_rbatt;
1821 int scalefactor;
1822 int delta_rbatt;
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001823
1824 read_rbatt_params_raw(chip, &raw);
1825 rbatt = calculate_rbatt_resume(chip, &raw);
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001826
1827 if (rbatt < 0)
1828 return 0;
1829
1830 expected_rbatt
1831 = (last_rbatt < 0) ? chip->default_rbatt_mohm : last_rbatt;
1832
1833 if (chip->rbatt_sf_lut) {
1834 scalefactor = interpolate_scalingfactor(chip,
1835 chip->rbatt_sf_lut,
1836 chip->batt_temp_suspend,
1837 chip->soc_rbatt_suspend);
1838 rbatt = rbatt * 100 / scalefactor;
1839 }
1840
1841 delta_rbatt = expected_rbatt - rbatt;
1842 if (delta_rbatt)
1843 delta_rbatt = -delta_rbatt;
1844 /*
1845 * only update last_rbatt if rbatt is within some
1846 * percent of expected_rbatt
1847 */
1848 if (delta_rbatt * 100 <= DELTA_RBATT_PERCENT * expected_rbatt)
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001849 last_rbatt = rbatt;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001850
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001851 return 0;
1852}
1853
1854static const struct dev_pm_ops pm8921_pm_ops = {
1855 .suspend = pm8921_bms_suspend,
1856 .resume = pm8921_bms_resume,
1857};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001858#define EN_BMS_BIT BIT(7)
1859#define EN_PON_HS_BIT BIT(0)
1860static int __devinit pm8921_bms_hw_init(struct pm8921_bms_chip *chip)
1861{
1862 int rc;
1863
1864 rc = pm_bms_masked_write(chip, BMS_CONTROL,
1865 EN_BMS_BIT | EN_PON_HS_BIT, EN_BMS_BIT | EN_PON_HS_BIT);
1866 if (rc) {
1867 pr_err("failed to enable pon and bms addr = %d %d",
1868 BMS_CONTROL, rc);
1869 }
1870
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -08001871 /* The charger will call start charge later if usb is present */
1872 pm_bms_masked_write(chip, BMS_TOLERANCES,
1873 IBAT_TOL_MASK, IBAT_TOL_NOCHG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001874 return 0;
1875}
1876
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001877static void check_initial_ocv(struct pm8921_bms_chip *chip)
1878{
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001879 int ocv_uv, rc;
1880 int16_t ocv_raw;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08001881 int usb_chg;
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001882
1883 /*
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001884 * Check if a ocv is available in bms hw,
1885 * if not compute it here at boot time and save it
1886 * in the last_ocv_uv.
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001887 */
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001888 ocv_uv = 0;
1889 pm_bms_read_output_data(chip, LAST_GOOD_OCV_VALUE, &ocv_raw);
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08001890 usb_chg = usb_chg_plugged_in();
1891 rc = convert_vbatt_raw_to_uv(chip, usb_chg, ocv_raw, &ocv_uv);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001892 if (rc || ocv_uv == 0) {
1893 rc = adc_based_ocv(chip, &ocv_uv);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001894 if (rc) {
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001895 pr_err("failed to read adc based ocv_uv rc = %d\n", rc);
1896 ocv_uv = DEFAULT_OCV_MICROVOLTS;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001897 }
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001898 last_ocv_uv = ocv_uv;
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001899 }
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001900 pr_debug("ocv_uv = %d last_ocv_uv = %d\n", ocv_uv, last_ocv_uv);
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07001901}
1902
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001903static int64_t read_battery_id(struct pm8921_bms_chip *chip)
1904{
1905 int rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001906 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001907
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001908 rc = pm8xxx_adc_read(chip->batt_id_channel, &result);
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001909 if (rc) {
1910 pr_err("error reading batt id channel = %d, rc = %d\n",
1911 chip->vbat_channel, rc);
1912 return rc;
1913 }
1914 pr_debug("batt_id phy = %lld meas = 0x%llx\n", result.physical,
1915 result.measurement);
David Keitel8f2601b2012-02-14 22:31:07 -08001916 return result.adc_code;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001917}
1918
David Keitel8f2601b2012-02-14 22:31:07 -08001919#define PALLADIUM_ID_MIN 0x7F40
1920#define PALLADIUM_ID_MAX 0x7F5A
1921#define DESAY_5200_ID_MIN 0x7F7F
1922#define DESAY_5200_ID_MAX 0x802F
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001923static int set_battery_data(struct pm8921_bms_chip *chip)
1924{
1925 int64_t battery_id;
1926
David Keitel35e11872012-02-17 17:40:42 -08001927 if (chip->batt_type == BATT_DESAY)
1928 goto desay;
1929 else if (chip->batt_type == BATT_PALLADIUM)
1930 goto palladium;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001931
David Keitel35e11872012-02-17 17:40:42 -08001932 battery_id = read_battery_id(chip);
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001933 if (battery_id < 0) {
1934 pr_err("cannot read battery id err = %lld\n", battery_id);
1935 return battery_id;
1936 }
1937
1938 if (is_between(PALLADIUM_ID_MIN, PALLADIUM_ID_MAX, battery_id)) {
David Keitel35e11872012-02-17 17:40:42 -08001939 goto palladium;
1940 } else if (is_between(DESAY_5200_ID_MIN, DESAY_5200_ID_MAX,
1941 battery_id)) {
1942 goto desay;
1943 } else {
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001944 pr_warn("invalid battid, palladium 1500 assumed batt_id %llx\n",
1945 battery_id);
1946 goto palladium;
David Keitel35e11872012-02-17 17:40:42 -08001947 }
1948
1949palladium:
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001950 chip->fcc = palladium_1500_data.fcc;
1951 chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut;
1952 chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut;
1953 chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut;
1954 chip->pc_sf_lut = palladium_1500_data.pc_sf_lut;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001955 chip->rbatt_sf_lut = palladium_1500_data.rbatt_sf_lut;
1956 chip->default_rbatt_mohm
1957 = palladium_1500_data.default_rbatt_mohm;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001958 return 0;
David Keitel35e11872012-02-17 17:40:42 -08001959desay:
David Keitel8f2601b2012-02-14 22:31:07 -08001960 chip->fcc = desay_5200_data.fcc;
1961 chip->fcc_temp_lut = desay_5200_data.fcc_temp_lut;
David Keitel8f2601b2012-02-14 22:31:07 -08001962 chip->pc_temp_ocv_lut = desay_5200_data.pc_temp_ocv_lut;
1963 chip->pc_sf_lut = desay_5200_data.pc_sf_lut;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001964 chip->rbatt_sf_lut = desay_5200_data.rbatt_sf_lut;
1965 chip->default_rbatt_mohm = desay_5200_data.default_rbatt_mohm;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001966 return 0;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07001967}
1968
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08001969enum bms_request_operation {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001970 CALC_RBATT,
1971 CALC_FCC,
1972 CALC_PC,
1973 CALC_SOC,
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07001974 CALIB_HKADC,
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07001975 CALIB_CCADC,
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08001976 GET_VBAT_VSENSE_SIMULTANEOUS,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001977};
1978
1979static int test_batt_temp = 5;
1980static int test_chargecycle = 150;
1981static int test_ocv = 3900000;
1982enum {
1983 TEST_BATT_TEMP,
1984 TEST_CHARGE_CYCLE,
1985 TEST_OCV,
1986};
1987static int get_test_param(void *data, u64 * val)
1988{
1989 switch ((int)data) {
1990 case TEST_BATT_TEMP:
1991 *val = test_batt_temp;
1992 break;
1993 case TEST_CHARGE_CYCLE:
1994 *val = test_chargecycle;
1995 break;
1996 case TEST_OCV:
1997 *val = test_ocv;
1998 break;
1999 default:
2000 return -EINVAL;
2001 }
2002 return 0;
2003}
2004static int set_test_param(void *data, u64 val)
2005{
2006 switch ((int)data) {
2007 case TEST_BATT_TEMP:
2008 test_batt_temp = (int)val;
2009 break;
2010 case TEST_CHARGE_CYCLE:
2011 test_chargecycle = (int)val;
2012 break;
2013 case TEST_OCV:
2014 test_ocv = (int)val;
2015 break;
2016 default:
2017 return -EINVAL;
2018 }
2019 return 0;
2020}
2021DEFINE_SIMPLE_ATTRIBUTE(temp_fops, get_test_param, set_test_param, "%llu\n");
2022
2023static int get_calc(void *data, u64 * val)
2024{
2025 int param = (int)data;
2026 int ret = 0;
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002027 int ibat_ua, vbat_uv;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002028 struct pm8921_soc_params raw;
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002029 struct pm8921_rbatt_params rraw;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002030
2031 read_soc_params_raw(the_chip, &raw);
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002032 read_rbatt_params_raw(the_chip, &rraw);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002033
2034 *val = 0;
2035
2036 /* global irq number passed in via data */
2037 switch (param) {
2038 case CALC_RBATT:
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002039 *val = calculate_rbatt_resume(the_chip, &rraw);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002040 break;
2041 case CALC_FCC:
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002042 *val = calculate_fcc_uah(the_chip, test_batt_temp,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002043 test_chargecycle);
2044 break;
2045 case CALC_PC:
2046 *val = calculate_pc(the_chip, test_ocv, test_batt_temp,
2047 test_chargecycle);
2048 break;
2049 case CALC_SOC:
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002050 *val = calculate_state_of_charge(the_chip, &raw,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002051 test_batt_temp, test_chargecycle);
2052 break;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002053 case CALIB_HKADC:
2054 /* reading this will trigger calibration */
2055 *val = 0;
2056 calib_hkadc(the_chip);
2057 break;
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002058 case CALIB_CCADC:
2059 /* reading this will trigger calibration */
2060 *val = 0;
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08002061 pm8xxx_calib_ccadc();
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002062 break;
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002063 case GET_VBAT_VSENSE_SIMULTANEOUS:
2064 /* reading this will call simultaneous vbat and vsense */
2065 *val =
2066 pm8921_bms_get_simultaneous_battery_voltage_and_current(
2067 &ibat_ua,
2068 &vbat_uv);
2069 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002070 default:
2071 ret = -EINVAL;
2072 }
2073 return ret;
2074}
2075DEFINE_SIMPLE_ATTRIBUTE(calc_fops, get_calc, NULL, "%llu\n");
2076
2077static int get_reading(void *data, u64 * val)
2078{
2079 int param = (int)data;
2080 int ret = 0;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002081 struct pm8921_soc_params raw;
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002082 struct pm8921_rbatt_params rraw;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002083
2084 read_soc_params_raw(the_chip, &raw);
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002085 read_rbatt_params_raw(the_chip, &rraw);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002086
2087 *val = 0;
2088
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002089 switch (param) {
2090 case CC_MSB:
2091 case CC_LSB:
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002092 *val = raw.cc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002093 break;
2094 case LAST_GOOD_OCV_VALUE:
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002095 *val = raw.last_good_ocv_uv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002096 break;
2097 case VBATT_FOR_RBATT:
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002098 *val = rraw.vbatt_for_rbatt_uv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002099 break;
2100 case VSENSE_FOR_RBATT:
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002101 *val = rraw.vsense_for_rbatt_uv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002102 break;
2103 case OCV_FOR_RBATT:
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002104 *val = rraw.ocv_for_rbatt_uv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002105 break;
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07002106 case VSENSE_AVG:
2107 read_vsense_avg(the_chip, (uint *)val);
2108 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002109 default:
2110 ret = -EINVAL;
2111 }
2112 return ret;
2113}
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07002114DEFINE_SIMPLE_ATTRIBUTE(reading_fops, get_reading, NULL, "%lld\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002115
2116static int get_rt_status(void *data, u64 * val)
2117{
2118 int i = (int)data;
2119 int ret;
2120
2121 /* global irq number passed in via data */
2122 ret = pm_bms_get_rt_status(the_chip, i);
2123 *val = ret;
2124 return 0;
2125}
2126DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n");
2127
2128static int get_reg(void *data, u64 * val)
2129{
2130 int addr = (int)data;
2131 int ret;
2132 u8 temp;
2133
2134 ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp);
2135 if (ret) {
2136 pr_err("pm8xxx_readb to %x value = %d errored = %d\n",
2137 addr, temp, ret);
2138 return -EAGAIN;
2139 }
2140 *val = temp;
2141 return 0;
2142}
2143
2144static int set_reg(void *data, u64 val)
2145{
2146 int addr = (int)data;
2147 int ret;
2148 u8 temp;
2149
2150 temp = (u8) val;
2151 ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp);
2152 if (ret) {
2153 pr_err("pm8xxx_writeb to %x value = %d errored = %d\n",
2154 addr, temp, ret);
2155 return -EAGAIN;
2156 }
2157 return 0;
2158}
2159DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
2160
2161static void create_debugfs_entries(struct pm8921_bms_chip *chip)
2162{
2163 int i;
2164
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08002165 chip->dent = debugfs_create_dir("pm8921-bms", NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002166
2167 if (IS_ERR(chip->dent)) {
2168 pr_err("pmic bms couldnt create debugfs dir\n");
2169 return;
2170 }
2171
2172 debugfs_create_file("BMS_CONTROL", 0644, chip->dent,
2173 (void *)BMS_CONTROL, &reg_fops);
2174 debugfs_create_file("BMS_OUTPUT0", 0644, chip->dent,
2175 (void *)BMS_OUTPUT0, &reg_fops);
2176 debugfs_create_file("BMS_OUTPUT1", 0644, chip->dent,
2177 (void *)BMS_OUTPUT1, &reg_fops);
2178 debugfs_create_file("BMS_TEST1", 0644, chip->dent,
2179 (void *)BMS_TEST1, &reg_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002180
2181 debugfs_create_file("test_batt_temp", 0644, chip->dent,
2182 (void *)TEST_BATT_TEMP, &temp_fops);
2183 debugfs_create_file("test_chargecycle", 0644, chip->dent,
2184 (void *)TEST_CHARGE_CYCLE, &temp_fops);
2185 debugfs_create_file("test_ocv", 0644, chip->dent,
2186 (void *)TEST_OCV, &temp_fops);
2187
2188 debugfs_create_file("read_cc", 0644, chip->dent,
2189 (void *)CC_MSB, &reading_fops);
2190 debugfs_create_file("read_last_good_ocv", 0644, chip->dent,
2191 (void *)LAST_GOOD_OCV_VALUE, &reading_fops);
2192 debugfs_create_file("read_vbatt_for_rbatt", 0644, chip->dent,
2193 (void *)VBATT_FOR_RBATT, &reading_fops);
2194 debugfs_create_file("read_vsense_for_rbatt", 0644, chip->dent,
2195 (void *)VSENSE_FOR_RBATT, &reading_fops);
2196 debugfs_create_file("read_ocv_for_rbatt", 0644, chip->dent,
2197 (void *)OCV_FOR_RBATT, &reading_fops);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07002198 debugfs_create_file("read_vsense_avg", 0644, chip->dent,
2199 (void *)VSENSE_AVG, &reading_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002200
2201 debugfs_create_file("show_rbatt", 0644, chip->dent,
2202 (void *)CALC_RBATT, &calc_fops);
2203 debugfs_create_file("show_fcc", 0644, chip->dent,
2204 (void *)CALC_FCC, &calc_fops);
2205 debugfs_create_file("show_pc", 0644, chip->dent,
2206 (void *)CALC_PC, &calc_fops);
2207 debugfs_create_file("show_soc", 0644, chip->dent,
2208 (void *)CALC_SOC, &calc_fops);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002209 debugfs_create_file("calib_hkadc", 0644, chip->dent,
2210 (void *)CALIB_HKADC, &calc_fops);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002211 debugfs_create_file("calib_ccadc", 0644, chip->dent,
2212 (void *)CALIB_CCADC, &calc_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002213
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002214 debugfs_create_file("simultaneous", 0644, chip->dent,
2215 (void *)GET_VBAT_VSENSE_SIMULTANEOUS, &calc_fops);
2216
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002217 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
2218 if (chip->pmic_bms_irq[bms_irq_data[i].irq_id])
2219 debugfs_create_file(bms_irq_data[i].name, 0444,
2220 chip->dent,
2221 (void *)bms_irq_data[i].irq_id,
2222 &rt_fops);
2223 }
2224}
2225
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -07002226#define REG_SBI_CONFIG 0x04F
2227#define PAGE3_ENABLE_MASK 0x6
2228#define PROGRAM_REV_MASK 0x0F
2229#define PROGRAM_REV 0x9
2230static int read_ocv_trim(struct pm8921_bms_chip *chip)
2231{
2232 int rc;
2233 u8 reg, sbi_config;
2234
2235 rc = pm8xxx_readb(chip->dev->parent, REG_SBI_CONFIG, &sbi_config);
2236 if (rc) {
2237 pr_err("error = %d reading sbi config reg\n", rc);
2238 return rc;
2239 }
2240
2241 reg = sbi_config | PAGE3_ENABLE_MASK;
2242 rc = pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, reg);
2243 if (rc) {
2244 pr_err("error = %d writing sbi config reg\n", rc);
2245 return rc;
2246 }
2247
2248 rc = pm8xxx_readb(chip->dev->parent, TEST_PROGRAM_REV, &reg);
2249 if (rc)
2250 pr_err("Error %d reading %d addr %d\n",
2251 rc, reg, TEST_PROGRAM_REV);
2252 pr_err("program rev reg is 0x%x\n", reg);
2253 reg &= PROGRAM_REV_MASK;
2254
2255 /* If the revision is equal or higher do not adjust trim delta */
2256 if (reg >= PROGRAM_REV) {
2257 chip->amux_2_trim_delta = 0;
2258 goto restore_sbi_config;
2259 }
2260
2261 rc = pm8xxx_readb(chip->dev->parent, AMUX_TRIM_2, &reg);
2262 if (rc) {
2263 pr_err("error = %d reading trim reg\n", rc);
2264 return rc;
2265 }
2266
2267 pr_err("trim reg is 0x%x\n", reg);
2268 chip->amux_2_trim_delta = abs(0x49 - reg);
2269 pr_err("trim delta is %d\n", chip->amux_2_trim_delta);
2270
2271restore_sbi_config:
2272 rc = pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, sbi_config);
2273 if (rc) {
2274 pr_err("error = %d writing sbi config reg\n", rc);
2275 return rc;
2276 }
2277
2278 return 0;
2279}
2280
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002281static int __devinit pm8921_bms_probe(struct platform_device *pdev)
2282{
2283 int rc = 0;
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07002284 int vbatt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002285 struct pm8921_bms_chip *chip;
2286 const struct pm8921_bms_platform_data *pdata
2287 = pdev->dev.platform_data;
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07002288
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002289 if (!pdata) {
2290 pr_err("missing platform data\n");
2291 return -EINVAL;
2292 }
2293
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002294 chip = kzalloc(sizeof(struct pm8921_bms_chip), GFP_KERNEL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002295 if (!chip) {
2296 pr_err("Cannot allocate pm_bms_chip\n");
2297 return -ENOMEM;
2298 }
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002299 mutex_init(&chip->bms_output_lock);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002300 spin_lock_init(&chip->bms_100_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002301 chip->dev = &pdev->dev;
2302 chip->r_sense = pdata->r_sense;
2303 chip->i_test = pdata->i_test;
2304 chip->v_failure = pdata->v_failure;
2305 chip->calib_delay_ms = pdata->calib_delay_ms;
Abhijeet Dharmapurikara93ede82011-11-17 12:20:03 -08002306 chip->max_voltage_uv = pdata->max_voltage_uv;
David Keitel35e11872012-02-17 17:40:42 -08002307 chip->batt_type = pdata->battery_type;
Abhijeet Dharmapurikardba91f42011-12-21 16:56:13 -08002308 chip->start_percent = -EINVAL;
2309 chip->end_percent = -EINVAL;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002310 rc = set_battery_data(chip);
2311 if (rc) {
2312 pr_err("%s bad battery data %d\n", __func__, rc);
Abhijeet Dharmapurikar4a4456e2011-09-27 15:10:38 -07002313 goto free_chip;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002314 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002315
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002316 if (chip->pc_temp_ocv_lut == NULL) {
2317 pr_err("temp ocv lut table is NULL\n");
2318 rc = -EINVAL;
2319 goto free_chip;
2320 }
2321
2322 /* set defaults in the battery data */
2323 if (chip->default_rbatt_mohm <= 0)
2324 chip->default_rbatt_mohm = DEFAULT_RBATT_MOHMS;
2325
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07002326 chip->batt_temp_channel = pdata->bms_cdata.batt_temp_channel;
2327 chip->vbat_channel = pdata->bms_cdata.vbat_channel;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002328 chip->ref625mv_channel = pdata->bms_cdata.ref625mv_channel;
2329 chip->ref1p25v_channel = pdata->bms_cdata.ref1p25v_channel;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002330 chip->batt_id_channel = pdata->bms_cdata.batt_id_channel;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -07002331 chip->revision = pm8xxx_get_revision(chip->dev->parent);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002332 INIT_WORK(&chip->calib_hkadc_work, calibrate_hkadc_work);
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07002333
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002334 rc = request_irqs(chip, pdev);
2335 if (rc) {
2336 pr_err("couldn't register interrupts rc = %d\n", rc);
2337 goto free_chip;
2338 }
2339
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002340 rc = pm8921_bms_hw_init(chip);
2341 if (rc) {
2342 pr_err("couldn't init hardware rc = %d\n", rc);
2343 goto free_irqs;
2344 }
2345
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002346 platform_set_drvdata(pdev, chip);
2347 the_chip = chip;
2348 create_debugfs_entries(chip);
2349
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -07002350 rc = read_ocv_trim(chip);
2351 if (rc) {
2352 pr_err("couldn't adjust ocv_trim rc= %d\n", rc);
2353 goto free_irqs;
2354 }
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002355 check_initial_ocv(chip);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002356
2357 INIT_DELAYED_WORK(&chip->calib_ccadc_work, calibrate_ccadc_work);
2358 /* begin calibration only on chips > 2.0 */
2359 if (chip->revision >= PM8XXX_REVISION_8921_2p0)
Abhijeet Dharmapurikarccfc4f32012-01-16 17:35:18 -08002360 schedule_delayed_work(&chip->calib_ccadc_work, 0);
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002361
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07002362 /* initial hkadc calibration */
2363 schedule_work(&chip->calib_hkadc_work);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002364 /* enable the vbatt reading interrupts for scheduling hkadc calib */
2365 pm8921_bms_enable_irq(chip, PM8921_BMS_GOOD_OCV);
2366 pm8921_bms_enable_irq(chip, PM8921_BMS_OCV_FOR_R);
Abhijeet Dharmapurikar21ebadd2011-09-28 21:14:16 -07002367
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07002368 get_battery_uvolts(chip, &vbatt);
2369 pr_info("OK battery_capacity_at_boot=%d volt = %d ocv = %d\n",
2370 pm8921_bms_get_percent_charge(),
2371 vbatt, last_ocv_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002372 return 0;
2373
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002374free_irqs:
2375 free_irqs(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002376free_chip:
2377 kfree(chip);
2378 return rc;
2379}
2380
2381static int __devexit pm8921_bms_remove(struct platform_device *pdev)
2382{
2383 struct pm8921_bms_chip *chip = platform_get_drvdata(pdev);
2384
2385 free_irqs(chip);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07002386 kfree(chip->adjusted_fcc_temp_lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002387 platform_set_drvdata(pdev, NULL);
2388 the_chip = NULL;
2389 kfree(chip);
2390 return 0;
2391}
2392
2393static struct platform_driver pm8921_bms_driver = {
2394 .probe = pm8921_bms_probe,
2395 .remove = __devexit_p(pm8921_bms_remove),
2396 .driver = {
2397 .name = PM8921_BMS_DEV_NAME,
2398 .owner = THIS_MODULE,
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08002399 .pm = &pm8921_pm_ops
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002400 },
2401};
2402
2403static int __init pm8921_bms_init(void)
2404{
2405 return platform_driver_register(&pm8921_bms_driver);
2406}
2407
2408static void __exit pm8921_bms_exit(void)
2409{
2410 platform_driver_unregister(&pm8921_bms_driver);
2411}
2412
2413late_initcall(pm8921_bms_init);
2414module_exit(pm8921_bms_exit);
2415
2416MODULE_LICENSE("GPL v2");
2417MODULE_DESCRIPTION("PMIC8921 bms driver");
2418MODULE_VERSION("1.0");
2419MODULE_ALIAS("platform:" PM8921_BMS_DEV_NAME);