blob: 98fa37c6d7878b6986dfbdf2e1238c1e167ab33c [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 Dharmapurikarb2680132012-05-15 16:32:51 -070023#include <linux/mfd/pm8xxx/pm8921-charger.h>
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -080024#include <linux/mfd/pm8xxx/ccadc.h>
kyungtae.oh12658992012-08-17 16:04:09 +090025#include <linux/power/bq51051b_charger.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026#include <linux/interrupt.h>
27#include <linux/bitops.h>
28#include <linux/debugfs.h>
29#include <linux/slab.h>
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070030#include <linux/delay.h>
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -080031#include <linux/mutex.h>
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -070032#include <linux/rtc.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033
34#define BMS_CONTROL 0x224
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -080035#define BMS_S1_DELAY 0x225
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036#define BMS_OUTPUT0 0x230
37#define BMS_OUTPUT1 0x231
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -080038#define BMS_TOLERANCES 0x232
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039#define BMS_TEST1 0x237
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070041#define ADC_ARB_SECP_CNTRL 0x190
42#define ADC_ARB_SECP_AMUX_CNTRL 0x191
43#define ADC_ARB_SECP_ANA_PARAM 0x192
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070044#define ADC_ARB_SECP_DIG_PARAM 0x193
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070045#define ADC_ARB_SECP_RSV 0x194
46#define ADC_ARB_SECP_DATA1 0x195
47#define ADC_ARB_SECP_DATA0 0x196
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070049#define ADC_ARB_BMS_CNTRL 0x18D
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -070050#define AMUX_TRIM_2 0x322
51#define TEST_PROGRAM_REV 0x339
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -070052
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -070053#define TEMP_SOC_STORAGE 0x107
54
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -070055#define TEMP_IAVG_STORAGE 0x105
56#define TEMP_IAVG_STORAGE_USE_MASK 0x0F
57
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070058enum pmic_bms_interrupts {
59 PM8921_BMS_SBI_WRITE_OK,
60 PM8921_BMS_CC_THR,
61 PM8921_BMS_VSENSE_THR,
62 PM8921_BMS_VSENSE_FOR_R,
63 PM8921_BMS_OCV_FOR_R,
64 PM8921_BMS_GOOD_OCV,
65 PM8921_BMS_VSENSE_AVG,
66 PM_BMS_MAX_INTS,
67};
68
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080069struct pm8921_soc_params {
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -080070 uint16_t last_good_ocv_raw;
71 int cc;
72
73 int last_good_ocv_uv;
74};
75
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080076/**
77 * struct pm8921_bms_chip -
78 * @bms_output_lock: lock to prevent concurrent bms reads
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080079 *
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -070080 * @last_ocv_uv_mutex: mutex to protect simultaneous invocations of calculate
81 * state of charge, note that last_ocv_uv could be
82 * changed as soc is adjusted. This mutex protects
83 * simultaneous updates of last_ocv_uv as well. This mutex
84 * also protects changes to *_at_100 variables used in
85 * faking 100% SOC.
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -080086 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070087struct pm8921_bms_chip {
88 struct device *dev;
89 struct dentry *dent;
90 unsigned int r_sense;
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -070091 unsigned int v_cutoff;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092 unsigned int fcc;
93 struct single_row_lut *fcc_temp_lut;
94 struct single_row_lut *fcc_sf_lut;
95 struct pc_temp_ocv_lut *pc_temp_ocv_lut;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -080096 struct sf_lut *pc_sf_lut;
97 struct sf_lut *rbatt_sf_lut;
Abhijeet Dharmapurikarded189b2012-03-30 10:12:28 -070098 int delta_rbatt_mohm;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070099 struct work_struct calib_hkadc_work;
Abhijeet Dharmapurikar9c0e6042012-07-12 14:55:13 -0700100 struct delayed_work calib_hkadc_delayed_work;
101 struct mutex calib_mutex;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -0700102 unsigned int revision;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800103 unsigned int xoadc_v0625_usb_present;
104 unsigned int xoadc_v0625_usb_absent;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700105 unsigned int xoadc_v0625;
106 unsigned int xoadc_v125;
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -0700107 unsigned int batt_temp_channel;
108 unsigned int vbat_channel;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700109 unsigned int ref625mv_channel;
110 unsigned int ref1p25v_channel;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -0700111 unsigned int batt_id_channel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112 unsigned int pmic_bms_irq[PM_BMS_MAX_INTS];
113 DECLARE_BITMAP(enabled_irqs, PM_BMS_MAX_INTS);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800114 struct mutex bms_output_lock;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700115 struct single_row_lut *adjusted_fcc_temp_lut;
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -0700116 unsigned int charging_began;
117 unsigned int start_percent;
118 unsigned int end_percent;
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -0700119 int charge_time_us;
120 int catch_up_time_us;
David Keitel35e11872012-02-17 17:40:42 -0800121 enum battery_type batt_type;
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -0800122 uint16_t ocv_reading_at_100;
123 int cc_reading_at_100;
Abhijeet Dharmapurikara93ede82011-11-17 12:20:03 -0800124 int max_voltage_uv;
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800125
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -0700126 int chg_term_ua;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800127 int default_rbatt_mohm;
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700128 int amux_2_trim_delta;
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -0700129 uint16_t prev_last_good_ocv_raw;
Abhijeet Dharmapurikarbaffba42012-03-22 14:41:10 -0700130 unsigned int rconn_mohm;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -0700131 struct mutex last_ocv_uv_mutex;
132 int last_ocv_uv;
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -0700133 int pon_ocv_uv;
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -0700134 int last_cc_uah;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -0700135 unsigned long tm_sec;
Abhijeet Dharmapurikare88f2462012-04-25 18:22:38 -0700136 int enable_fcc_learning;
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -0700137 int shutdown_soc;
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -0700138 int shutdown_iavg_ua;
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -0700139 struct delayed_work calculate_soc_delayed_work;
140 struct timespec t_soc_queried;
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -0700141 int shutdown_soc_valid_limit;
Abhijeet Dharmapurikar1f4cc512012-07-21 00:10:39 -0700142 int ignore_shutdown_soc;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -0700143 int prev_iavg_ua;
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -0700144 int prev_uuc_iavg_ma;
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -0700145 int prev_pc_unusable;
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -0700146 int adjust_soc_low_threshold;
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -0700147
148 int ibat_at_cv_ua;
149 int soc_at_cv;
150 int prev_chg_soc;
choongryeol.lee76048da2012-09-11 14:19:21 -0700151 int last_reported_soc;
152 int eoc_check_soc;
153 int soc_adjusted;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700154};
155
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -0700156/*
157 * protects against simultaneous adjustment of ocv based on shutdown soc and
158 * invalidating the shutdown soc
159 */
160static DEFINE_MUTEX(soc_invalidation_mutex);
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -0700161static int shutdown_soc_invalid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700162static struct pm8921_bms_chip *the_chip;
163
roy.park1a749b72012-06-18 22:49:20 -0700164#define DEFAULT_RBATT_MOHMS 200
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700165#define DEFAULT_OCV_MICROVOLTS 3900000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166#define DEFAULT_CHARGE_CYCLES 0
167
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800168static int last_usb_cal_delta_uv = 1800;
169module_param(last_usb_cal_delta_uv, int, 0644);
170
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700171static int last_chargecycles = DEFAULT_CHARGE_CYCLES;
172static int last_charge_increase;
173module_param(last_chargecycles, int, 0644);
174module_param(last_charge_increase, int, 0644);
175
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -0700176static int calculated_soc = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177static int last_soc = -EINVAL;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800178static int last_real_fcc_mah = -EINVAL;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700179static int last_real_fcc_batt_temp = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700181static int bms_ops_set(const char *val, const struct kernel_param *kp)
182{
183 if (*(int *)kp->arg == -EINVAL)
184 return param_set_int(val, kp);
185 else
186 return 0;
187}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700189static struct kernel_param_ops bms_param_ops = {
190 .set = bms_ops_set,
191 .get = param_get_int,
192};
193
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700194module_param_cb(last_soc, &bms_param_ops, &last_soc, 0644);
195
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -0800196/*
Abhijeet Dharmapurikar986bd8a2012-01-05 15:15:15 -0800197 * bms_fake_battery is set in setups where a battery emulator is used instead
198 * of a real battery. This makes the bms driver report a different/fake value
199 * regardless of the calculated state of charge.
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -0800200 */
Abhijeet Dharmapurikar986bd8a2012-01-05 15:15:15 -0800201static int bms_fake_battery = -EINVAL;
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -0800202module_param(bms_fake_battery, int, 0644);
203
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800204/* bms_start_XXX and bms_end_XXX are read only */
205static int bms_start_percent;
206static int bms_start_ocv_uv;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800207static int bms_start_cc_uah;
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800208static int bms_end_percent;
209static int bms_end_ocv_uv;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800210static int bms_end_cc_uah;
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800211
212static int bms_ro_ops_set(const char *val, const struct kernel_param *kp)
213{
214 return -EINVAL;
215}
216
217static struct kernel_param_ops bms_ro_param_ops = {
218 .set = bms_ro_ops_set,
219 .get = param_get_int,
220};
221module_param_cb(bms_start_percent, &bms_ro_param_ops, &bms_start_percent, 0644);
222module_param_cb(bms_start_ocv_uv, &bms_ro_param_ops, &bms_start_ocv_uv, 0644);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800223module_param_cb(bms_start_cc_uah, &bms_ro_param_ops, &bms_start_cc_uah, 0644);
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800224
225module_param_cb(bms_end_percent, &bms_ro_param_ops, &bms_end_percent, 0644);
226module_param_cb(bms_end_ocv_uv, &bms_ro_param_ops, &bms_end_ocv_uv, 0644);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800227module_param_cb(bms_end_cc_uah, &bms_ro_param_ops, &bms_end_cc_uah, 0644);
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -0800228
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700229static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp);
230static void readjust_fcc_table(void)
231{
232 struct single_row_lut *temp, *old;
233 int i, fcc, ratio;
234
235 if (!the_chip->fcc_temp_lut) {
236 pr_err("The static fcc lut table is NULL\n");
237 return;
238 }
239
240 temp = kzalloc(sizeof(struct single_row_lut), GFP_KERNEL);
241 if (!temp) {
242 pr_err("Cannot allocate memory for adjusted fcc table\n");
243 return;
244 }
245
246 fcc = interpolate_fcc(the_chip, last_real_fcc_batt_temp);
247
248 temp->cols = the_chip->fcc_temp_lut->cols;
249 for (i = 0; i < the_chip->fcc_temp_lut->cols; i++) {
250 temp->x[i] = the_chip->fcc_temp_lut->x[i];
251 ratio = div_u64(the_chip->fcc_temp_lut->y[i] * 1000, fcc);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800252 temp->y[i] = (ratio * last_real_fcc_mah);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700253 temp->y[i] /= 1000;
254 pr_debug("temp=%d, staticfcc=%d, adjfcc=%d, ratio=%d\n",
255 temp->x[i], the_chip->fcc_temp_lut->y[i],
256 temp->y[i], ratio);
257 }
258
259 old = the_chip->adjusted_fcc_temp_lut;
260 the_chip->adjusted_fcc_temp_lut = temp;
261 kfree(old);
262}
263
264static int bms_last_real_fcc_set(const char *val,
265 const struct kernel_param *kp)
266{
267 int rc = 0;
268
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800269 if (last_real_fcc_mah == -EINVAL)
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700270 rc = param_set_int(val, kp);
271 if (rc) {
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800272 pr_err("Failed to set last_real_fcc_mah rc=%d\n", rc);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700273 return rc;
274 }
275 if (last_real_fcc_batt_temp != -EINVAL)
276 readjust_fcc_table();
277 return rc;
278}
279static struct kernel_param_ops bms_last_real_fcc_param_ops = {
280 .set = bms_last_real_fcc_set,
281 .get = param_get_int,
282};
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800283module_param_cb(last_real_fcc_mah, &bms_last_real_fcc_param_ops,
284 &last_real_fcc_mah, 0644);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700285
286static int bms_last_real_fcc_batt_temp_set(const char *val,
287 const struct kernel_param *kp)
288{
289 int rc = 0;
290
291 if (last_real_fcc_batt_temp == -EINVAL)
292 rc = param_set_int(val, kp);
293 if (rc) {
294 pr_err("Failed to set last_real_fcc_batt_temp rc=%d\n", rc);
295 return rc;
296 }
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800297 if (last_real_fcc_mah != -EINVAL)
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700298 readjust_fcc_table();
299 return rc;
300}
301
302static struct kernel_param_ops bms_last_real_fcc_batt_temp_param_ops = {
303 .set = bms_last_real_fcc_batt_temp_set,
304 .get = param_get_int,
305};
306module_param_cb(last_real_fcc_batt_temp, &bms_last_real_fcc_batt_temp_param_ops,
307 &last_real_fcc_batt_temp, 0644);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700308
309static int pm_bms_get_rt_status(struct pm8921_bms_chip *chip, int irq_id)
310{
311 return pm8xxx_read_irq_stat(chip->dev->parent,
312 chip->pmic_bms_irq[irq_id]);
313}
314
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700315static void pm8921_bms_enable_irq(struct pm8921_bms_chip *chip, int interrupt)
316{
317 if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) {
318 dev_dbg(chip->dev, "%s %d\n", __func__,
319 chip->pmic_bms_irq[interrupt]);
320 enable_irq(chip->pmic_bms_irq[interrupt]);
321 }
322}
323
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700324static void pm8921_bms_disable_irq(struct pm8921_bms_chip *chip, int interrupt)
325{
326 if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700327 pr_debug("%d\n", chip->pmic_bms_irq[interrupt]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700328 disable_irq_nosync(chip->pmic_bms_irq[interrupt]);
329 }
330}
331
332static int pm_bms_masked_write(struct pm8921_bms_chip *chip, u16 addr,
333 u8 mask, u8 val)
334{
335 int rc;
336 u8 reg;
337
338 rc = pm8xxx_readb(chip->dev->parent, addr, &reg);
339 if (rc) {
340 pr_err("read failed addr = %03X, rc = %d\n", addr, rc);
341 return rc;
342 }
343 reg &= ~mask;
344 reg |= val & mask;
345 rc = pm8xxx_writeb(chip->dev->parent, addr, reg);
346 if (rc) {
347 pr_err("write failed addr = %03X, rc = %d\n", addr, rc);
348 return rc;
349 }
350 return 0;
351}
352
choongryeol.lee7346d0f2012-10-10 16:25:38 -0700353static int usb_chg_plugged_in(struct pm8921_bms_chip *chip)
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800354{
Abhijeet Dharmapurikar3da61762012-07-12 15:56:58 -0700355 int val = pm8921_is_usb_chg_plugged_in();
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800356
choongryeol.lee7346d0f2012-10-10 16:25:38 -0700357 /* if the charger driver was not initialized, use the restart reason */
358 if (val == -EINVAL) {
359 if (pm8xxx_restart_reason(chip->dev->parent)
360 == PM8XXX_RESTART_CHG)
361 val = 1;
362 else
363 val = 0;
364 }
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800365
Abhijeet Dharmapurikar3da61762012-07-12 15:56:58 -0700366 return val;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800367}
368
kyungtae.oh1ff608a2012-07-19 14:31:54 +0900369static int wireless_chg_plugged_in(void)
370{
kyungtae.oh12658992012-08-17 16:04:09 +0900371 int val = bq51051b_wireless_plugged_in();
kyungtae.oh1ff608a2012-07-19 14:31:54 +0900372
kyungtae.oh12658992012-08-17 16:04:09 +0900373 /* treat as if usb is not present in case of error */
374 if (val == -EINVAL)
375 val = 0;
kyungtae.oh1ff608a2012-07-19 14:31:54 +0900376
kyungtae.oh12658992012-08-17 16:04:09 +0900377 return val;
kyungtae.oh1ff608a2012-07-19 14:31:54 +0900378}
kyungtae.oh1ff608a2012-07-19 14:31:54 +0900379
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700380#define HOLD_OREG_DATA BIT(1)
381static int pm_bms_lock_output_data(struct pm8921_bms_chip *chip)
382{
383 int rc;
384
385 rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA,
386 HOLD_OREG_DATA);
387 if (rc) {
388 pr_err("couldnt lock bms output rc = %d\n", rc);
389 return rc;
390 }
391 return 0;
392}
393
394static int pm_bms_unlock_output_data(struct pm8921_bms_chip *chip)
395{
396 int rc;
397
398 rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA, 0);
399 if (rc) {
400 pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc);
401 return rc;
402 }
403 return 0;
404}
405
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700406#define SELECT_OUTPUT_DATA 0x1C
407#define SELECT_OUTPUT_TYPE_SHIFT 2
408#define OCV_FOR_RBATT 0x0
409#define VSENSE_FOR_RBATT 0x1
410#define VBATT_FOR_RBATT 0x2
411#define CC_MSB 0x3
412#define CC_LSB 0x4
413#define LAST_GOOD_OCV_VALUE 0x5
414#define VSENSE_AVG 0x6
415#define VBATT_AVG 0x7
416
417static int pm_bms_read_output_data(struct pm8921_bms_chip *chip, int type,
418 int16_t *result)
419{
420 int rc;
421 u8 reg;
422
423 if (!result) {
424 pr_err("result pointer null\n");
425 return -EINVAL;
426 }
427 *result = 0;
428 if (type < OCV_FOR_RBATT || type > VBATT_AVG) {
429 pr_err("invalid type %d asked to read\n", type);
430 return -EINVAL;
431 }
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -0700432
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700433 rc = pm_bms_masked_write(chip, BMS_CONTROL, SELECT_OUTPUT_DATA,
434 type << SELECT_OUTPUT_TYPE_SHIFT);
435 if (rc) {
436 pr_err("fail to select %d type in BMS_CONTROL rc = %d\n",
437 type, rc);
438 return rc;
439 }
440
441 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT0, &reg);
442 if (rc) {
443 pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n",
444 type, rc);
445 return rc;
446 }
447 *result = reg;
448 rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT1, &reg);
449 if (rc) {
450 pr_err("fail to read BMS_OUTPUT1 for type %d rc = %d\n",
451 type, rc);
452 return rc;
453 }
454 *result |= reg << 8;
455 pr_debug("type %d result %x", type, *result);
456 return 0;
457}
458
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700459#define V_PER_BIT_MUL_FACTOR 97656
460#define V_PER_BIT_DIV_FACTOR 1000
461#define XOADC_INTRINSIC_OFFSET 0x6000
462static int xoadc_reading_to_microvolt(unsigned int a)
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700463{
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700464 if (a <= XOADC_INTRINSIC_OFFSET)
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700465 return 0;
466
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700467 return (a - XOADC_INTRINSIC_OFFSET)
468 * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR;
Abhijeet Dharmapurikard76311f2011-07-19 13:35:01 -0700469}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700470
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700471#define XOADC_CALIB_UV 625000
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700472#define VBATT_MUL_FACTOR 3
473static int adjust_xo_vbatt_reading(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800474 int usb_chg, unsigned int uv)
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700475{
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800476 s64 numerator, denominator;
477 int local_delta;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700478
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700479 if (uv == 0)
480 return 0;
481
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800482 /* dont adjust if not calibrated */
483 if (chip->xoadc_v0625 == 0 || chip->xoadc_v125 == 0) {
484 pr_debug("No cal yet return %d\n", VBATT_MUL_FACTOR * uv);
485 return VBATT_MUL_FACTOR * uv;
486 }
487
488 if (usb_chg)
489 local_delta = last_usb_cal_delta_uv;
490 else
491 local_delta = 0;
492
493 pr_debug("using delta = %d\n", local_delta);
494 numerator = ((s64)uv - chip->xoadc_v0625 - local_delta)
495 * XOADC_CALIB_UV;
496 denominator = (s64)chip->xoadc_v125 - chip->xoadc_v0625 - local_delta;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700497 if (denominator == 0)
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700498 return uv * VBATT_MUL_FACTOR;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800499 return (XOADC_CALIB_UV + local_delta + div_s64(numerator, denominator))
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700500 * VBATT_MUL_FACTOR;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -0700501}
502
Abhijeet Dharmapurikar5adb2532012-06-21 16:15:43 -0700503#define CC_RESOLUTION_N 868056
504#define CC_RESOLUTION_D 10000
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700505
506static s64 cc_to_microvolt(struct pm8921_bms_chip *chip, s64 cc)
507{
Abhijeet Dharmapurikar5adb2532012-06-21 16:15:43 -0700508 return div_s64(cc * CC_RESOLUTION_N, CC_RESOLUTION_D);
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700509}
510
Abhijeet Dharmapurikar2feabe32012-07-23 21:42:17 -0700511#define CC_READING_TICKS 56
512#define SLEEP_CLK_HZ 32764
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700513#define SECONDS_PER_HOUR 3600
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800514/**
515 * ccmicrovolt_to_nvh -
516 * @cc_uv: coulumb counter converted to uV
517 *
518 * RETURNS: coulumb counter based charge in nVh
519 * (nano Volt Hour)
520 */
521static s64 ccmicrovolt_to_nvh(s64 cc_uv)
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700522{
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -0800523 return div_s64(cc_uv * CC_READING_TICKS * 1000,
Abhijeet Dharmapurikareed550b2011-07-18 22:35:24 -0700524 SLEEP_CLK_HZ * SECONDS_PER_HOUR);
525}
526
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700527/* returns the signed value read from the hardware */
528static int read_cc(struct pm8921_bms_chip *chip, int *result)
529{
530 int rc;
531 uint16_t msw, lsw;
532
533 rc = pm_bms_read_output_data(chip, CC_LSB, &lsw);
534 if (rc) {
535 pr_err("fail to read CC_LSB rc = %d\n", rc);
536 return rc;
537 }
538 rc = pm_bms_read_output_data(chip, CC_MSB, &msw);
539 if (rc) {
540 pr_err("fail to read CC_MSB rc = %d\n", rc);
541 return rc;
542 }
543 *result = msw << 16 | lsw;
544 pr_debug("msw = %04x lsw = %04x cc = %d\n", msw, lsw, *result);
545 return 0;
546}
547
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700548static int adjust_xo_vbatt_reading_for_mbg(struct pm8921_bms_chip *chip,
549 int result)
550{
551 int64_t numerator;
552 int64_t denominator;
553
554 if (chip->amux_2_trim_delta == 0)
555 return result;
556
557 numerator = (s64)result * 1000000;
558 denominator = (1000000 + (410 * (s64)chip->amux_2_trim_delta));
559 return div_s64(numerator, denominator);
560}
561
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800562static int convert_vbatt_raw_to_uv(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800563 int usb_chg,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800564 uint16_t reading, int *result)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565{
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -0700566 *result = xoadc_reading_to_microvolt(reading);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800567 pr_debug("raw = %04x vbatt = %u\n", reading, *result);
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800568 *result = adjust_xo_vbatt_reading(chip, usb_chg, *result);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800569 pr_debug("after adj vbatt = %u\n", *result);
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700570 *result = adjust_xo_vbatt_reading_for_mbg(chip, *result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571 return 0;
572}
573
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800574static int convert_vsense_to_uv(struct pm8921_bms_chip *chip,
575 int16_t reading, int *result)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576{
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800577 *result = pm8xxx_ccadc_reading_to_microvolt(chip->revision, reading);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800578 pr_debug("raw = %04x vsense = %d\n", reading, *result);
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -0800579 *result = pm8xxx_cc_adjust_for_gain(*result);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800580 pr_debug("after adj vsense = %d\n", *result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700581 return 0;
582}
583
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700584static int read_vsense_avg(struct pm8921_bms_chip *chip, int *result)
585{
586 int rc;
587 int16_t reading;
588
589 rc = pm_bms_read_output_data(chip, VSENSE_AVG, &reading);
590 if (rc) {
591 pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
592 return rc;
593 }
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -0800594
595 convert_vsense_to_uv(chip, reading, result);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700596 return 0;
597}
598
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599static int linear_interpolate(int y0, int x0, int y1, int x1, int x)
600{
601 if (y0 == y1 || x == x0)
602 return y0;
603 if (x1 == x0 || x == x1)
604 return y1;
605
606 return y0 + ((y1 - y0) * (x - x0) / (x1 - x0));
607}
608
609static int interpolate_single_lut(struct single_row_lut *lut, int x)
610{
611 int i, result;
612
613 if (x < lut->x[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700614 pr_debug("x %d less than known range return y = %d lut = %pS\n",
615 x, lut->y[0], lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616 return lut->y[0];
617 }
618 if (x > lut->x[lut->cols - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700619 pr_debug("x %d more than known range return y = %d lut = %pS\n",
620 x, lut->y[lut->cols - 1], lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700621 return lut->y[lut->cols - 1];
622 }
623
624 for (i = 0; i < lut->cols; i++)
625 if (x <= lut->x[i])
626 break;
627 if (x == lut->x[i]) {
628 result = lut->y[i];
629 } else {
630 result = linear_interpolate(
631 lut->y[i - 1],
632 lut->x[i - 1],
633 lut->y[i],
634 lut->x[i],
635 x);
636 }
637 return result;
638}
639
640static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp)
641{
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -0800642 /* batt_temp is in tenths of degC - convert it to degC for lookups */
643 batt_temp = batt_temp/10;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700644 return interpolate_single_lut(chip->fcc_temp_lut, batt_temp);
645}
646
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700647static int interpolate_fcc_adjusted(struct pm8921_bms_chip *chip, int batt_temp)
648{
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -0800649 /* batt_temp is in tenths of degC - convert it to degC for lookups */
650 batt_temp = batt_temp/10;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -0700651 return interpolate_single_lut(chip->adjusted_fcc_temp_lut, batt_temp);
652}
653
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654static int interpolate_scalingfactor_fcc(struct pm8921_bms_chip *chip,
655 int cycles)
656{
Abhijeet Dharmapurikar205bbc02011-11-04 14:47:36 -0700657 /*
658 * sf table could be null when no battery aging data is available, in
659 * that case return 100%
660 */
661 if (chip->fcc_sf_lut)
662 return interpolate_single_lut(chip->fcc_sf_lut, cycles);
663 else
664 return 100;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665}
666
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800667static int interpolate_scalingfactor(struct pm8921_bms_chip *chip,
668 struct sf_lut *sf_lut,
669 int row_entry, int pc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700670{
Abhijeet Dharmapurikar4a4456e2011-09-27 15:10:38 -0700671 int i, scalefactorrow1, scalefactorrow2, scalefactor;
Abhijeet Dharmapurikar205bbc02011-11-04 14:47:36 -0700672 int rows, cols;
Abhijeet Dharmapurikar4a4456e2011-09-27 15:10:38 -0700673 int row1 = 0;
674 int row2 = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700675
Abhijeet Dharmapurikar205bbc02011-11-04 14:47:36 -0700676 /*
677 * sf table could be null when no battery aging data is available, in
678 * that case return 100%
679 */
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800680 if (!sf_lut)
Abhijeet Dharmapurikar205bbc02011-11-04 14:47:36 -0700681 return 100;
682
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800683 rows = sf_lut->rows;
684 cols = sf_lut->cols;
685 if (pc > sf_lut->percent[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700686 pr_debug("pc %d greater than known pc ranges for sfd\n", pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700687 row1 = 0;
688 row2 = 0;
689 }
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800690 if (pc < sf_lut->percent[rows - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700691 pr_debug("pc %d less than known pc ranges for sf", pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700692 row1 = rows - 1;
693 row2 = rows - 1;
694 }
695 for (i = 0; i < rows; i++) {
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800696 if (pc == sf_lut->percent[i]) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700697 row1 = i;
698 row2 = i;
699 break;
700 }
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800701 if (pc > sf_lut->percent[i]) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700702 row1 = i - 1;
703 row2 = i;
704 break;
705 }
706 }
707
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800708 if (row_entry < sf_lut->row_entries[0])
709 row_entry = sf_lut->row_entries[0];
710 if (row_entry > sf_lut->row_entries[cols - 1])
711 row_entry = sf_lut->row_entries[cols - 1];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700712
713 for (i = 0; i < cols; i++)
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800714 if (row_entry <= sf_lut->row_entries[i])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700715 break;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800716 if (row_entry == sf_lut->row_entries[i]) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717 scalefactor = linear_interpolate(
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800718 sf_lut->sf[row1][i],
719 sf_lut->percent[row1],
720 sf_lut->sf[row2][i],
721 sf_lut->percent[row2],
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700722 pc);
723 return scalefactor;
724 }
725
726 scalefactorrow1 = linear_interpolate(
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800727 sf_lut->sf[row1][i - 1],
728 sf_lut->row_entries[i - 1],
729 sf_lut->sf[row1][i],
730 sf_lut->row_entries[i],
731 row_entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700732
733 scalefactorrow2 = linear_interpolate(
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800734 sf_lut->sf[row2][i - 1],
735 sf_lut->row_entries[i - 1],
736 sf_lut->sf[row2][i],
737 sf_lut->row_entries[i],
738 row_entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700739
740 scalefactor = linear_interpolate(
741 scalefactorrow1,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800742 sf_lut->percent[row1],
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700743 scalefactorrow2,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800744 sf_lut->percent[row2],
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700745 pc);
746
747 return scalefactor;
748}
749
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700750static int is_between(int left, int right, int value)
751{
752 if (left >= right && left >= value && value >= right)
753 return 1;
754 if (left <= right && left <= value && value <= right)
755 return 1;
756
757 return 0;
758}
759
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -0700760/* get ocv given a soc -- reverse lookup */
761static int interpolate_ocv(struct pm8921_bms_chip *chip,
762 int batt_temp_degc, int pc)
763{
764 int i, ocvrow1, ocvrow2, ocv;
765 int rows, cols;
766 int row1 = 0;
767 int row2 = 0;
768
769 rows = chip->pc_temp_ocv_lut->rows;
770 cols = chip->pc_temp_ocv_lut->cols;
771 if (pc > chip->pc_temp_ocv_lut->percent[0]) {
772 pr_debug("pc %d greater than known pc ranges for sfd\n", pc);
773 row1 = 0;
774 row2 = 0;
775 }
776 if (pc < chip->pc_temp_ocv_lut->percent[rows - 1]) {
777 pr_debug("pc %d less than known pc ranges for sf\n", pc);
778 row1 = rows - 1;
779 row2 = rows - 1;
780 }
781 for (i = 0; i < rows; i++) {
782 if (pc == chip->pc_temp_ocv_lut->percent[i]) {
783 row1 = i;
784 row2 = i;
785 break;
786 }
787 if (pc > chip->pc_temp_ocv_lut->percent[i]) {
788 row1 = i - 1;
789 row2 = i;
790 break;
791 }
792 }
793
794 if (batt_temp_degc < chip->pc_temp_ocv_lut->temp[0])
795 batt_temp_degc = chip->pc_temp_ocv_lut->temp[0];
796 if (batt_temp_degc > chip->pc_temp_ocv_lut->temp[cols - 1])
797 batt_temp_degc = chip->pc_temp_ocv_lut->temp[cols - 1];
798
799 for (i = 0; i < cols; i++)
800 if (batt_temp_degc <= chip->pc_temp_ocv_lut->temp[i])
801 break;
802 if (batt_temp_degc == chip->pc_temp_ocv_lut->temp[i]) {
803 ocv = linear_interpolate(
804 chip->pc_temp_ocv_lut->ocv[row1][i],
805 chip->pc_temp_ocv_lut->percent[row1],
806 chip->pc_temp_ocv_lut->ocv[row2][i],
807 chip->pc_temp_ocv_lut->percent[row2],
808 pc);
809 return ocv;
810 }
811
812 ocvrow1 = linear_interpolate(
813 chip->pc_temp_ocv_lut->ocv[row1][i - 1],
814 chip->pc_temp_ocv_lut->temp[i - 1],
815 chip->pc_temp_ocv_lut->ocv[row1][i],
816 chip->pc_temp_ocv_lut->temp[i],
817 batt_temp_degc);
818
819 ocvrow2 = linear_interpolate(
820 chip->pc_temp_ocv_lut->ocv[row2][i - 1],
821 chip->pc_temp_ocv_lut->temp[i - 1],
822 chip->pc_temp_ocv_lut->ocv[row2][i],
823 chip->pc_temp_ocv_lut->temp[i],
824 batt_temp_degc);
825
826 ocv = linear_interpolate(
827 ocvrow1,
828 chip->pc_temp_ocv_lut->percent[row1],
829 ocvrow2,
830 chip->pc_temp_ocv_lut->percent[row2],
831 pc);
832
833 return ocv;
834}
835
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700836static int interpolate_pc(struct pm8921_bms_chip *chip,
837 int batt_temp, int ocv)
838{
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700839 int i, j, pcj, pcj_minus_one, pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700840 int rows = chip->pc_temp_ocv_lut->rows;
841 int cols = chip->pc_temp_ocv_lut->cols;
842
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -0800843 /* batt_temp is in tenths of degC - convert it to degC for lookups */
844 batt_temp = batt_temp/10;
845
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700846 if (batt_temp < chip->pc_temp_ocv_lut->temp[0]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700847 pr_debug("batt_temp %d < known temp range for pc\n", batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700848 batt_temp = chip->pc_temp_ocv_lut->temp[0];
849 }
850 if (batt_temp > chip->pc_temp_ocv_lut->temp[cols - 1]) {
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -0700851 pr_debug("batt_temp %d > known temp range for pc\n", batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700852 batt_temp = chip->pc_temp_ocv_lut->temp[cols - 1];
853 }
854
855 for (j = 0; j < cols; j++)
856 if (batt_temp <= chip->pc_temp_ocv_lut->temp[j])
857 break;
858 if (batt_temp == chip->pc_temp_ocv_lut->temp[j]) {
859 /* found an exact match for temp in the table */
860 if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j])
861 return chip->pc_temp_ocv_lut->percent[0];
862 if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j])
863 return chip->pc_temp_ocv_lut->percent[rows - 1];
864 for (i = 0; i < rows; i++) {
865 if (ocv >= chip->pc_temp_ocv_lut->ocv[i][j]) {
866 if (ocv == chip->pc_temp_ocv_lut->ocv[i][j])
867 return
868 chip->pc_temp_ocv_lut->percent[i];
869 pc = linear_interpolate(
870 chip->pc_temp_ocv_lut->percent[i],
871 chip->pc_temp_ocv_lut->ocv[i][j],
872 chip->pc_temp_ocv_lut->percent[i - 1],
873 chip->pc_temp_ocv_lut->ocv[i - 1][j],
874 ocv);
875 return pc;
876 }
877 }
878 }
879
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700880 /*
881 * batt_temp is within temperature for
882 * column j-1 and j
883 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700884 if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j])
885 return chip->pc_temp_ocv_lut->percent[0];
886 if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j - 1])
887 return chip->pc_temp_ocv_lut->percent[rows - 1];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700888
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700889 pcj_minus_one = 0;
890 pcj = 0;
891 for (i = 0; i < rows-1; i++) {
892 if (pcj == 0
893 && is_between(chip->pc_temp_ocv_lut->ocv[i][j],
894 chip->pc_temp_ocv_lut->ocv[i+1][j], ocv)) {
895 pcj = linear_interpolate(
896 chip->pc_temp_ocv_lut->percent[i],
897 chip->pc_temp_ocv_lut->ocv[i][j],
898 chip->pc_temp_ocv_lut->percent[i + 1],
899 chip->pc_temp_ocv_lut->ocv[i+1][j],
900 ocv);
901 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700902
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700903 if (pcj_minus_one == 0
904 && is_between(chip->pc_temp_ocv_lut->ocv[i][j-1],
905 chip->pc_temp_ocv_lut->ocv[i+1][j-1], ocv)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700906
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700907 pcj_minus_one = linear_interpolate(
908 chip->pc_temp_ocv_lut->percent[i],
909 chip->pc_temp_ocv_lut->ocv[i][j-1],
910 chip->pc_temp_ocv_lut->percent[i + 1],
911 chip->pc_temp_ocv_lut->ocv[i+1][j-1],
912 ocv);
913 }
914
915 if (pcj && pcj_minus_one) {
916 pc = linear_interpolate(
917 pcj_minus_one,
918 chip->pc_temp_ocv_lut->temp[j-1],
919 pcj,
920 chip->pc_temp_ocv_lut->temp[j],
921 batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700922 return pc;
923 }
924 }
925
Abhijeet Dharmapurikar78d812f2011-07-21 19:57:21 -0700926 if (pcj)
927 return pcj;
928
929 if (pcj_minus_one)
930 return pcj_minus_one;
931
932 pr_debug("%d ocv wasn't found for temp %d in the LUT returning 100%%",
933 ocv, batt_temp);
934 return 100;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700935}
936
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800937#define BMS_MODE_BIT BIT(6)
938#define EN_VBAT_BIT BIT(5)
939#define OVERRIDE_MODE_DELAY_MS 20
Abhijeet Dharmapurikarb2680132012-05-15 16:32:51 -0700940int override_mode_simultaneous_battery_voltage_and_current(int *ibat_ua,
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800941 int *vbat_uv)
942{
943 int16_t vsense_raw;
944 int16_t vbat_raw;
945 int vsense_uv;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800946 int usb_chg;
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800947
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800948 mutex_lock(&the_chip->bms_output_lock);
949
950 pm8xxx_writeb(the_chip->dev->parent, BMS_S1_DELAY, 0x00);
951 pm_bms_masked_write(the_chip, BMS_CONTROL,
952 BMS_MODE_BIT | EN_VBAT_BIT, BMS_MODE_BIT | EN_VBAT_BIT);
953
954 msleep(OVERRIDE_MODE_DELAY_MS);
955
956 pm_bms_lock_output_data(the_chip);
957 pm_bms_read_output_data(the_chip, VSENSE_AVG, &vsense_raw);
958 pm_bms_read_output_data(the_chip, VBATT_AVG, &vbat_raw);
959 pm_bms_unlock_output_data(the_chip);
960 pm_bms_masked_write(the_chip, BMS_CONTROL,
961 BMS_MODE_BIT | EN_VBAT_BIT, 0);
962
963 pm8xxx_writeb(the_chip->dev->parent, BMS_S1_DELAY, 0x0B);
964
965 mutex_unlock(&the_chip->bms_output_lock);
966
choongryeol.lee7346d0f2012-10-10 16:25:38 -0700967 usb_chg = usb_chg_plugged_in(the_chip);
kyungtae.oh1ff608a2012-07-19 14:31:54 +0900968 usb_chg |= wireless_chg_plugged_in();
kyungtae.oh1ff608a2012-07-19 14:31:54 +0900969
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -0800970 convert_vbatt_raw_to_uv(the_chip, usb_chg, vbat_raw, vbat_uv);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800971 convert_vsense_to_uv(the_chip, vsense_raw, &vsense_uv);
972 *ibat_ua = vsense_uv * 1000 / (int)the_chip->r_sense;
973
974 pr_debug("vsense_raw = 0x%x vbat_raw = 0x%x"
975 " ibat_ua = %d vbat_uv = %d\n",
976 (uint16_t)vsense_raw, (uint16_t)vbat_raw,
977 *ibat_ua, *vbat_uv);
978 return 0;
979}
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800980
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -0700981#define MBG_TRANSIENT_ERROR_RAW 51
982static void adjust_pon_ocv_raw(struct pm8921_bms_chip *chip,
983 struct pm8921_soc_params *raw)
984{
985 /* in 8921 parts the PON ocv is taken when the MBG is not settled.
986 * decrease the pon ocv by 15mV raw value to account for it
987 * Since a 1/3rd of vbatt is supplied to the adc the raw value
988 * needs to be adjusted by 5mV worth bits
989 */
990 if (raw->last_good_ocv_raw >= MBG_TRANSIENT_ERROR_RAW)
991 raw->last_good_ocv_raw -= MBG_TRANSIENT_ERROR_RAW;
992}
993
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -0800994static int read_soc_params_raw(struct pm8921_bms_chip *chip,
995 struct pm8921_soc_params *raw)
996{
997 int usb_chg;
998
999 mutex_lock(&chip->bms_output_lock);
1000 pm_bms_lock_output_data(chip);
1001
1002 pm_bms_read_output_data(chip,
1003 LAST_GOOD_OCV_VALUE, &raw->last_good_ocv_raw);
1004 read_cc(chip, &raw->cc);
1005
1006 pm_bms_unlock_output_data(chip);
1007 mutex_unlock(&chip->bms_output_lock);
1008
choongryeol.lee7346d0f2012-10-10 16:25:38 -07001009 usb_chg = usb_chg_plugged_in(chip);
kyungtae.oh1ff608a2012-07-19 14:31:54 +09001010 usb_chg |= wireless_chg_plugged_in();
kyungtae.oh1ff608a2012-07-19 14:31:54 +09001011
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -07001012 if (chip->prev_last_good_ocv_raw == 0) {
1013 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
1014 adjust_pon_ocv_raw(chip, raw);
1015 convert_vbatt_raw_to_uv(chip, usb_chg,
1016 raw->last_good_ocv_raw, &raw->last_good_ocv_uv);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001017 chip->last_ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -07001018 pr_debug("PON_OCV_UV = %d\n", chip->last_ocv_uv);
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -07001019 } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) {
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -07001020 chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
1021 convert_vbatt_raw_to_uv(chip, usb_chg,
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001022 raw->last_good_ocv_raw, &raw->last_good_ocv_uv);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001023 chip->last_ocv_uv = raw->last_good_ocv_uv;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001024 /* forget the old cc value upon ocv */
1025 chip->last_cc_uah = 0;
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -07001026 } else {
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001027 raw->last_good_ocv_uv = chip->last_ocv_uv;
Abhijeet Dharmapurikarb7092f12012-03-14 21:19:50 -07001028 }
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001029
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001030 /* fake a high OCV if we are just done charging */
1031 if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw) {
1032 chip->ocv_reading_at_100 = 0;
1033 chip->cc_reading_at_100 = 0;
1034 } else {
1035 /*
1036 * force 100% ocv by selecting the highest voltage the
1037 * battery could ever reach
1038 */
1039 raw->last_good_ocv_uv = chip->max_voltage_uv;
1040 chip->last_ocv_uv = chip->max_voltage_uv;
1041 }
Abhijeet Dharmapurikar1a7561a2012-02-26 22:06:36 -08001042 pr_debug("0p625 = %duV\n", chip->xoadc_v0625);
1043 pr_debug("1p25 = %duV\n", chip->xoadc_v125);
Abhijeet Dharmapurikar1b8e8292012-01-17 11:01:44 -08001044 pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n",
1045 raw->last_good_ocv_raw, raw->last_good_ocv_uv);
1046 pr_debug("cc_raw= 0x%x\n", raw->cc);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001047 return 0;
1048}
1049
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001050static int get_rbatt(struct pm8921_bms_chip *chip, int soc_rbatt, int batt_temp)
1051{
1052 int rbatt, scalefactor;
1053
Abhijeet Dharmapurikar0711c522012-05-22 18:45:25 -07001054 rbatt = chip->default_rbatt_mohm;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001055 pr_debug("rbatt before scaling = %d\n", rbatt);
1056 if (chip->rbatt_sf_lut == NULL) {
1057 pr_debug("RBATT = %d\n", rbatt);
1058 return rbatt;
1059 }
Abhijeet Dharmapurikar12a891c2012-04-12 23:52:06 -07001060 /* Convert the batt_temp to DegC from deciDegC */
1061 batt_temp = batt_temp / 10;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001062 scalefactor = interpolate_scalingfactor(chip, chip->rbatt_sf_lut,
1063 batt_temp, soc_rbatt);
1064 pr_debug("rbatt sf = %d for batt_temp = %d, soc_rbatt = %d\n",
1065 scalefactor, batt_temp, soc_rbatt);
1066 rbatt = (rbatt * scalefactor) / 100;
1067
Abhijeet Dharmapurikarbaffba42012-03-22 14:41:10 -07001068 rbatt += the_chip->rconn_mohm;
1069 pr_debug("adding rconn_mohm = %d rbatt = %d\n",
1070 the_chip->rconn_mohm, rbatt);
1071
Abhijeet Dharmapurikarded189b2012-03-30 10:12:28 -07001072 if (is_between(20, 10, soc_rbatt))
1073 rbatt = rbatt
1074 + ((20 - soc_rbatt) * chip->delta_rbatt_mohm) / 10;
1075 else
1076 if (is_between(10, 0, soc_rbatt))
1077 rbatt = rbatt + chip->delta_rbatt_mohm;
1078
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001079 pr_debug("RBATT = %d\n", rbatt);
1080 return rbatt;
1081}
1082
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001083static int calculate_fcc_uah(struct pm8921_bms_chip *chip, int batt_temp,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001084 int chargecycles)
1085{
1086 int initfcc, result, scalefactor = 0;
1087
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001088 if (chip->adjusted_fcc_temp_lut == NULL) {
1089 initfcc = interpolate_fcc(chip, batt_temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001091 scalefactor = interpolate_scalingfactor_fcc(chip, chargecycles);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001093 /* Multiply the initial FCC value by the scale factor. */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001094 result = (initfcc * scalefactor * 1000) / 100;
1095 pr_debug("fcc = %d uAh\n", result);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001096 return result;
1097 } else {
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001098 return 1000 * interpolate_fcc_adjusted(chip, batt_temp);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07001099 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001100}
1101
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001102static int get_battery_uvolts(struct pm8921_bms_chip *chip, int *uvolts)
1103{
1104 int rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001105 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001106
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001107 rc = pm8xxx_adc_read(chip->vbat_channel, &result);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001108 if (rc) {
1109 pr_err("error reading adc channel = %d, rc = %d\n",
1110 chip->vbat_channel, rc);
1111 return rc;
1112 }
1113 pr_debug("mvolts phy = %lld meas = 0x%llx", result.physical,
1114 result.measurement);
1115 *uvolts = (int)result.physical;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001116 return 0;
1117}
1118
1119static int adc_based_ocv(struct pm8921_bms_chip *chip, int *ocv)
1120{
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001121 int vbatt, rbatt, ibatt_ua, rc;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001122
1123 rc = get_battery_uvolts(chip, &vbatt);
1124 if (rc) {
1125 pr_err("failed to read vbatt from adc rc = %d\n", rc);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001126 return rc;
1127 }
1128
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001129 rc = pm8921_bms_get_battery_current(&ibatt_ua);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001130 if (rc) {
1131 pr_err("failed to read batt current rc = %d\n", rc);
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001132 return rc;
1133 }
1134
Abhijeet Dharmapurikar0711c522012-05-22 18:45:25 -07001135 rbatt = chip->default_rbatt_mohm;
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001136 *ocv = vbatt + (ibatt_ua * rbatt)/1000;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07001137 return 0;
1138}
1139
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001140static int calculate_pc(struct pm8921_bms_chip *chip, int ocv_uv, int batt_temp,
1141 int chargecycles)
1142{
1143 int pc, scalefactor;
1144
1145 pc = interpolate_pc(chip, batt_temp, ocv_uv / 1000);
1146 pr_debug("pc = %u for ocv = %dmicroVolts batt_temp = %d\n",
1147 pc, ocv_uv, batt_temp);
1148
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001149 scalefactor = interpolate_scalingfactor(chip,
1150 chip->pc_sf_lut, chargecycles, pc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001151 pr_debug("scalefactor = %u batt_temp = %d\n", scalefactor, batt_temp);
1152
1153 /* Multiply the initial FCC value by the scale factor. */
1154 pc = (pc * scalefactor) / 100;
1155 return pc;
1156}
1157
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001158/**
1159 * calculate_cc_uah -
1160 * @chip: the bms chip pointer
1161 * @cc: the cc reading from bms h/w
1162 * @val: return value
1163 * @coulumb_counter: adjusted coulumb counter for 100%
1164 *
1165 * RETURNS: in val pointer coulumb counter based charger in uAh
1166 * (micro Amp hour)
1167 */
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001168static void calculate_cc_uah(struct pm8921_bms_chip *chip, int cc, int *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001169{
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001170 int64_t cc_voltage_uv, cc_nvh, cc_uah;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001171
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001172 cc_voltage_uv = cc;
1173 cc_voltage_uv -= chip->cc_reading_at_100;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001174 pr_debug("cc = %d. after subtracting 0x%x cc = %lld\n",
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001175 cc, chip->cc_reading_at_100,
1176 cc_voltage_uv);
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -07001177 cc_voltage_uv = cc_to_microvolt(chip, cc_voltage_uv);
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08001178 cc_voltage_uv = pm8xxx_cc_adjust_for_gain(cc_voltage_uv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001179 pr_debug("cc_voltage_uv = %lld microvolts\n", cc_voltage_uv);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001180 cc_nvh = ccmicrovolt_to_nvh(cc_voltage_uv);
1181 pr_debug("cc_nvh = %lld nano_volt_hour\n", cc_nvh);
1182 cc_uah = div_s64(cc_nvh, chip->r_sense);
1183 *val = cc_uah;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001184}
1185
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001186static int calculate_termination_uuc(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001187 int batt_temp, int chargecycles,
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001188 int fcc_uah, int i_ma,
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001189 int *ret_pc_unusable)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001190{
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001191 int unusable_uv, pc_unusable, uuc;
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001192 int i = 0;
1193 int ocv_mv;
1194 int batt_temp_degc = batt_temp / 10;
1195 int rbatt_mohm;
1196 int delta_uv;
1197 int prev_delta_uv = 0;
1198 int prev_rbatt_mohm = 0;
1199 int prev_ocv_mv = 0;
1200 int uuc_rbatt_uv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001201
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001202 for (i = 0; i <= 100; i++) {
1203 ocv_mv = interpolate_ocv(chip, batt_temp_degc, i);
1204 rbatt_mohm = get_rbatt(chip, i, batt_temp);
1205 unusable_uv = (rbatt_mohm * i_ma) + (chip->v_cutoff * 1000);
1206 delta_uv = ocv_mv * 1000 - unusable_uv;
1207
1208 pr_debug("soc = %d ocv = %d rbat = %d u_uv = %d delta_v = %d\n",
1209 i, ocv_mv, rbatt_mohm, unusable_uv, delta_uv);
1210
1211 if (delta_uv > 0)
1212 break;
1213
1214 prev_delta_uv = delta_uv;
1215 prev_rbatt_mohm = rbatt_mohm;
1216 prev_ocv_mv = ocv_mv;
1217 }
1218
1219 uuc_rbatt_uv = linear_interpolate(rbatt_mohm, delta_uv,
1220 prev_rbatt_mohm, prev_delta_uv,
1221 0);
1222
1223 unusable_uv = (uuc_rbatt_uv * i_ma) + (chip->v_cutoff * 1000);
1224
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001225 pc_unusable = calculate_pc(chip, unusable_uv, batt_temp, chargecycles);
1226 uuc = (fcc_uah * pc_unusable) / 100;
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001227 pr_debug("For i_ma = %d, unusable_rbatt = %d unusable_uv = %d unusable_pc = %d uuc = %d\n",
1228 i_ma, uuc_rbatt_uv, unusable_uv,
1229 pc_unusable, uuc);
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001230 *ret_pc_unusable = pc_unusable;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001231 return uuc;
1232}
1233
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001234static int adjust_uuc(struct pm8921_bms_chip *chip, int fcc_uah,
1235 int new_pc_unusable,
1236 int new_uuc,
1237 int batt_temp,
1238 int rbatt,
1239 int *iavg_ma)
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001240{
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001241 int new_unusable_mv;
1242 int batt_temp_degc = batt_temp / 10;
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001243
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001244 if (chip->prev_pc_unusable == -EINVAL
1245 || abs(chip->prev_pc_unusable - new_pc_unusable) <= 1) {
1246 chip->prev_pc_unusable = new_pc_unusable;
1247 return new_uuc;
1248 }
1249
1250 /* the uuc is trying to change more than 1% restrict it */
1251 if (new_pc_unusable > chip->prev_pc_unusable)
1252 chip->prev_pc_unusable++;
1253 else
1254 chip->prev_pc_unusable--;
1255
1256 new_uuc = (fcc_uah * chip->prev_pc_unusable) / 100;
1257
1258 /* also find update the iavg_ma accordingly */
1259 new_unusable_mv = interpolate_ocv(chip, batt_temp_degc,
1260 chip->prev_pc_unusable);
1261 if (new_unusable_mv < chip->v_cutoff)
1262 new_unusable_mv = chip->v_cutoff;
1263
1264 *iavg_ma = (new_unusable_mv - chip->v_cutoff) * 1000 / rbatt;
1265 if (*iavg_ma == 0)
1266 *iavg_ma = 1;
1267 pr_debug("Restricting UUC to %d (%d%%) unusable_mv = %d iavg_ma = %d\n",
1268 new_uuc, chip->prev_pc_unusable,
1269 new_unusable_mv, *iavg_ma);
1270
1271 return new_uuc;
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001272}
1273
1274static void calculate_iavg_ua(struct pm8921_bms_chip *chip, int cc_uah,
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001275 int *iavg_ua, int *delta_time_s)
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001276{
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001277 int delta_cc_uah;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001278 struct rtc_time tm;
1279 struct rtc_device *rtc;
1280 unsigned long now_tm_sec = 0;
1281 int rc = 0;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001282
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001283 /* if anything fails report the previous iavg_ua */
1284 *iavg_ua = chip->prev_iavg_ua;
1285
1286 rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
1287 if (rtc == NULL) {
1288 pr_err("%s: unable to open rtc device (%s)\n",
1289 __FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
1290 goto out;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001291 }
1292
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001293 rc = rtc_read_time(rtc, &tm);
1294 if (rc) {
1295 pr_err("Error reading rtc device (%s) : %d\n",
1296 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1297 goto out;
1298 }
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001299
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001300 rc = rtc_valid_tm(&tm);
1301 if (rc) {
1302 pr_err("Invalid RTC time (%s): %d\n",
1303 CONFIG_RTC_HCTOSYS_DEVICE, rc);
1304 goto out;
1305 }
1306 rtc_tm_to_time(&tm, &now_tm_sec);
1307
1308 if (chip->tm_sec == 0) {
1309 *delta_time_s = 0;
1310 pm8921_bms_get_battery_current(iavg_ua);
1311 goto out;
1312 }
1313
1314 *delta_time_s = (now_tm_sec - chip->tm_sec);
1315
1316 /* use the previous iavg if called within 15 seconds */
1317 if (*delta_time_s < 15) {
1318 *iavg_ua = chip->prev_iavg_ua;
1319 goto out;
1320 }
1321
1322 delta_cc_uah = cc_uah - chip->last_cc_uah;
1323
1324 *iavg_ua = div_s64((s64)delta_cc_uah * 3600, *delta_time_s);
1325
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001326 pr_debug("tm_sec = %ld, now_tm_sec = %ld delta_s = %d delta_cc = %d iavg_ua = %d\n",
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001327 chip->tm_sec, now_tm_sec,
1328 *delta_time_s, delta_cc_uah, (int)*iavg_ua);
1329
1330out:
1331 /* remember the iavg */
1332 chip->prev_iavg_ua = *iavg_ua;
1333
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001334 /* remember cc_uah */
1335 chip->last_cc_uah = cc_uah;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001336
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001337 /* remember this time */
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001338 chip->tm_sec = now_tm_sec;
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001339}
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001340
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001341#define IAVG_SAMPLES 16
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001342#define CHARGING_IAVG_MA 250
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001343#define MIN_SECONDS_FOR_VALID_SAMPLE 20
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001344static int calculate_unusable_charge_uah(struct pm8921_bms_chip *chip,
1345 int rbatt, int fcc_uah, int cc_uah,
1346 int soc_rbatt, int batt_temp, int chargecycles,
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001347 int iavg_ua, int delta_time_s)
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001348{
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001349 int uuc_uah_iavg;
1350 int i;
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001351 int iavg_ma = iavg_ua / 1000;
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001352 static int iavg_samples[IAVG_SAMPLES];
1353 static int iavg_index;
1354 static int iavg_num_samples;
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001355 static int firsttime = 1;
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001356 int pc_unusable;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001357
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001358 /*
1359 * if we are called first time fill all the
1360 * samples with the the shutdown_iavg_ua
1361 */
1362 if (firsttime && chip->shutdown_iavg_ua != 0) {
1363 pr_emerg("Using shutdown_iavg_ua = %d in all samples\n",
1364 chip->shutdown_iavg_ua);
1365 for (i = 0; i < IAVG_SAMPLES; i++)
1366 iavg_samples[i] = chip->shutdown_iavg_ua;
1367
1368 iavg_index = 0;
1369 iavg_num_samples = IAVG_SAMPLES;
1370 }
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001371
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001372 /*
1373 * if we are charging use a nominal avg current so that we keep
1374 * a reasonable UUC while charging
1375 */
1376 if (iavg_ma < 0)
1377 iavg_ma = CHARGING_IAVG_MA;
1378 iavg_samples[iavg_index] = iavg_ma;
1379 iavg_index = (iavg_index + 1) % IAVG_SAMPLES;
1380 iavg_num_samples++;
1381 if (iavg_num_samples >= IAVG_SAMPLES)
1382 iavg_num_samples = IAVG_SAMPLES;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001383
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001384 /* now that this sample is added calcualte the average */
1385 iavg_ma = 0;
1386 if (iavg_num_samples != 0) {
1387 for (i = 0; i < iavg_num_samples; i++) {
1388 pr_debug("iavg_samples[%d] = %d\n", i, iavg_samples[i]);
1389 iavg_ma += iavg_samples[i];
1390 }
1391
1392 iavg_ma = DIV_ROUND_CLOSEST(iavg_ma, iavg_num_samples);
1393 }
1394
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001395 uuc_uah_iavg = calculate_termination_uuc(chip,
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001396 batt_temp, chargecycles,
Abhijeet Dharmapurikar5dc6d342012-08-04 13:58:53 -07001397 fcc_uah, iavg_ma,
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001398 &pc_unusable);
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001399 pr_debug("iavg = %d uuc_iavg = %d\n", iavg_ma, uuc_uah_iavg);
1400
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001401 /* restrict the uuc such that it can increase only by one percent */
1402 uuc_uah_iavg = adjust_uuc(chip, fcc_uah, pc_unusable, uuc_uah_iavg,
1403 batt_temp, rbatt, &iavg_ma);
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001404
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07001405 /* find out what the avg current should be for this uuc */
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001406 chip->prev_uuc_iavg_ma = iavg_ma;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001407
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001408 firsttime = 0;
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001409 return uuc_uah_iavg;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001410}
Abhijeet Dharmapurikar52aa9682011-07-20 14:05:47 -07001411
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001412/* calculate remainging charge at the time of ocv */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001413static int calculate_remaining_charge_uah(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001414 struct pm8921_soc_params *raw,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001415 int fcc_uah, int batt_temp,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001416 int chargecycles)
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001417{
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001418 int ocv, pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001419
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001420 ocv = raw->last_good_ocv_uv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001421 pc = calculate_pc(chip, ocv, batt_temp, chargecycles);
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001422 pr_debug("ocv = %d pc = %d\n", ocv, pc);
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001423 return (fcc_uah * pc) / 100;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001424}
1425
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001426static void calculate_soc_params(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001427 struct pm8921_soc_params *raw,
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001428 int batt_temp, int chargecycles,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001429 int *fcc_uah,
1430 int *unusable_charge_uah,
1431 int *remaining_charge_uah,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001432 int *cc_uah,
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001433 int *rbatt,
1434 int *iavg_ua,
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001435 int *delta_time_s)
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001436{
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001437 int soc_rbatt;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001438
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001439 *fcc_uah = calculate_fcc_uah(chip, batt_temp, chargecycles);
1440 pr_debug("FCC = %uuAh batt_temp = %d, cycles = %d\n",
1441 *fcc_uah, batt_temp, chargecycles);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001442
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001443
1444 /* calculate remainging charge */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001445 *remaining_charge_uah = calculate_remaining_charge_uah(chip, raw,
1446 *fcc_uah, batt_temp, chargecycles);
1447 pr_debug("RC = %uuAh\n", *remaining_charge_uah);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001448
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001449 /* calculate cc micro_volt_hour */
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001450 calculate_cc_uah(chip, raw->cc, cc_uah);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001451 pr_debug("cc_uah = %duAh raw->cc = %x cc = %lld after subtracting %x\n",
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08001452 *cc_uah, raw->cc,
1453 (int64_t)raw->cc - chip->cc_reading_at_100,
1454 chip->cc_reading_at_100);
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001455
1456 soc_rbatt = ((*remaining_charge_uah - *cc_uah) * 100) / *fcc_uah;
1457 if (soc_rbatt < 0)
1458 soc_rbatt = 0;
1459 *rbatt = get_rbatt(chip, soc_rbatt, batt_temp);
1460
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001461 calculate_iavg_ua(chip, *cc_uah, iavg_ua, delta_time_s);
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001462
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001463 *unusable_charge_uah = calculate_unusable_charge_uah(chip, *rbatt,
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07001464 *fcc_uah, *cc_uah, soc_rbatt,
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001465 batt_temp, chargecycles, *iavg_ua,
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001466 *delta_time_s);
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001467 pr_debug("UUC = %uuAh\n", *unusable_charge_uah);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001468}
1469
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001470static int calculate_real_fcc_uah(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001471 struct pm8921_soc_params *raw,
1472 int batt_temp, int chargecycles,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001473 int *ret_fcc_uah)
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001474{
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001475 int fcc_uah, unusable_charge_uah;
1476 int remaining_charge_uah;
1477 int cc_uah;
1478 int real_fcc_uah;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001479 int rbatt;
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001480 int iavg_ua;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001481 int delta_time_s;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001482
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001483 calculate_soc_params(chip, raw, batt_temp, chargecycles,
1484 &fcc_uah,
1485 &unusable_charge_uah,
1486 &remaining_charge_uah,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001487 &cc_uah,
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001488 &rbatt,
1489 &iavg_ua,
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001490 &delta_time_s);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001491
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001492 real_fcc_uah = remaining_charge_uah - cc_uah;
1493 *ret_fcc_uah = fcc_uah;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001494 pr_debug("real_fcc = %d, RC = %d CC = %d fcc = %d\n",
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001495 real_fcc_uah, remaining_charge_uah, cc_uah, fcc_uah);
1496 return real_fcc_uah;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001497}
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001498
Abhijeet Dharmapurikarb2680132012-05-15 16:32:51 -07001499int pm8921_bms_get_simultaneous_battery_voltage_and_current(int *ibat_ua,
1500 int *vbat_uv)
1501{
1502 int rc;
1503
1504 if (the_chip == NULL) {
1505 pr_err("Called too early\n");
1506 return -EINVAL;
1507 }
1508
1509 if (pm8921_is_batfet_closed()) {
1510 return override_mode_simultaneous_battery_voltage_and_current(
1511 ibat_ua,
1512 vbat_uv);
1513 } else {
1514 pr_debug("batfet is open using separate vbat and ibat meas\n");
1515 rc = get_battery_uvolts(the_chip, vbat_uv);
1516 if (rc < 0) {
1517 pr_err("adc vbat failed err = %d\n", rc);
1518 return rc;
1519 }
1520 rc = pm8921_bms_get_battery_current(ibat_ua);
1521 if (rc < 0) {
1522 pr_err("bms ibat failed err = %d\n", rc);
1523 return rc;
1524 }
1525 }
1526
1527 return 0;
1528}
1529EXPORT_SYMBOL(pm8921_bms_get_simultaneous_battery_voltage_and_current);
1530
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001531static void find_ocv_for_soc(struct pm8921_bms_chip *chip,
1532 int batt_temp,
1533 int chargecycles,
1534 int fcc_uah,
1535 int uuc_uah,
1536 int cc_uah,
1537 int shutdown_soc,
1538 int *rc_uah,
1539 int *ocv_uv)
1540{
1541 s64 rc;
1542 int pc, new_pc;
1543 int batt_temp_degc = batt_temp / 10;
1544 int ocv;
1545
1546 rc = (s64)shutdown_soc * (fcc_uah - uuc_uah);
1547 rc = div_s64(rc, 100) + cc_uah + uuc_uah;
1548 pc = DIV_ROUND_CLOSEST((int)rc * 100, fcc_uah);
1549 pc = clamp(pc, 0, 100);
1550
1551 ocv = interpolate_ocv(chip, batt_temp_degc, pc);
1552
1553 pr_debug("s_soc = %d, fcc = %d uuc = %d rc = %d, pc = %d, ocv mv = %d\n",
1554 shutdown_soc, fcc_uah, uuc_uah, (int)rc, pc, ocv);
1555 new_pc = interpolate_pc(chip, batt_temp_degc, ocv);
1556 pr_debug("test revlookup pc = %d for ocv = %d\n", new_pc, ocv);
1557
1558 while (abs(new_pc - pc) > 1) {
1559 int delta_mv = 5;
1560
1561 if (new_pc > pc)
1562 delta_mv = -1 * delta_mv;
1563
1564 ocv = ocv + delta_mv;
1565 new_pc = interpolate_pc(chip, batt_temp_degc, ocv);
1566 pr_debug("test revlookup pc = %d for ocv = %d\n", new_pc, ocv);
1567 }
1568
1569 *ocv_uv = ocv * 1000;
1570 *rc_uah = (int)rc;
1571}
1572
1573static void adjust_rc_and_uuc_for_specific_soc(
1574 struct pm8921_bms_chip *chip,
1575 int batt_temp,
1576 int chargecycles,
1577 int soc,
1578 int fcc_uah,
1579 int uuc_uah,
1580 int cc_uah,
1581 int rc_uah,
1582 int rbatt,
1583 int *ret_ocv,
1584 int *ret_rc,
1585 int *ret_uuc,
1586 int *ret_rbatt)
1587{
1588 int ocv_uv;
1589
1590 find_ocv_for_soc(chip, batt_temp, chargecycles,
1591 fcc_uah, uuc_uah, cc_uah,
1592 soc,
1593 &rc_uah, &ocv_uv);
1594
1595 *ret_ocv = ocv_uv;
1596 *ret_rbatt = rbatt;
1597 *ret_rc = rc_uah;
1598 *ret_uuc = uuc_uah;
1599}
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001600static int bound_soc(int soc)
1601{
1602 soc = max(0, soc);
1603 soc = min(100, soc);
1604 return soc;
1605}
1606
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001607static int charging_adjustments(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001608 int soc, int vbat_uv, int ibat_ua,
1609 int batt_temp, int chargecycles,
1610 int fcc_uah, int cc_uah, int uuc_uah)
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001611{
1612 int chg_soc;
1613
1614 if (chip->soc_at_cv == -EINVAL) {
1615 /* In constant current charging return the calc soc */
1616 if (vbat_uv <= chip->max_voltage_uv)
1617 pr_debug("CC CHG SOC %d\n", soc);
1618
1619 /* Note the CC to CV point */
1620 if (vbat_uv >= chip->max_voltage_uv) {
1621 chip->soc_at_cv = soc;
1622 chip->prev_chg_soc = soc;
1623 chip->ibat_at_cv_ua = ibat_ua;
1624 pr_debug("CC_TO_CV ibat_ua = %d CHG SOC %d\n",
1625 ibat_ua, soc);
1626 }
1627 return soc;
1628 }
1629
1630 /*
1631 * battery is in CV phase - begin liner inerpolation of soc based on
1632 * battery charge current
1633 */
1634
1635 /*
1636 * if voltage lessened (possibly because of a system load)
1637 * keep reporting the prev chg soc
1638 */
1639 if (vbat_uv <= chip->max_voltage_uv) {
Abhijeet Dharmapurikard3c435e2012-08-16 19:23:18 -07001640 pr_debug("vbat %d < max = %d CC CHG SOC %d\n",
1641 vbat_uv, chip->max_voltage_uv, chip->prev_chg_soc);
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001642 return chip->prev_chg_soc;
1643 }
1644
1645 chg_soc = linear_interpolate(chip->soc_at_cv, chip->ibat_at_cv_ua,
1646 100, -100000,
1647 ibat_ua);
1648
1649 /* always report a higher soc */
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001650 if (chg_soc > chip->prev_chg_soc) {
1651 int new_ocv_uv;
1652 int new_rc;
1653
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001654 chip->prev_chg_soc = chg_soc;
1655
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001656 find_ocv_for_soc(chip, batt_temp, chargecycles,
1657 fcc_uah, uuc_uah, cc_uah,
1658 chg_soc,
1659 &new_rc, &new_ocv_uv);
1660 the_chip->last_ocv_uv = new_ocv_uv;
1661 pr_debug("CC CHG ADJ OCV = %d CHG SOC %d\n",
1662 new_ocv_uv,
1663 chip->prev_chg_soc);
1664 }
1665
Abhijeet Dharmapurikard3c435e2012-08-16 19:23:18 -07001666 pr_debug("Reporting CHG SOC %d\n", chip->prev_chg_soc);
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001667 return chip->prev_chg_soc;
1668}
1669
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001670static int last_soc_est = -EINVAL;
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001671static int adjust_soc(struct pm8921_bms_chip *chip, int soc,
1672 int batt_temp, int chargecycles,
1673 int rbatt, int fcc_uah, int uuc_uah, int cc_uah)
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001674{
1675 int ibat_ua = 0, vbat_uv = 0;
1676 int ocv_est_uv = 0, soc_est = 0, pc_est = 0, pc = 0;
1677 int delta_ocv_uv = 0;
1678 int n = 0;
1679 int rc_new_uah = 0;
1680 int pc_new = 0;
1681 int soc_new = 0;
1682 int m = 0;
Abhijeet Dharmapurikarb2680132012-05-15 16:32:51 -07001683 int rc = 0;
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -07001684 int delta_ocv_uv_limit = 0;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001685
Abhijeet Dharmapurikarb2680132012-05-15 16:32:51 -07001686 rc = pm8921_bms_get_simultaneous_battery_voltage_and_current(
1687 &ibat_ua,
1688 &vbat_uv);
1689 if (rc < 0) {
1690 pr_err("simultaneous vbat ibat failed err = %d\n", rc);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001691 goto out;
Abhijeet Dharmapurikarb2680132012-05-15 16:32:51 -07001692 }
1693
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001694
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -07001695 delta_ocv_uv_limit = DIV_ROUND_CLOSEST(ibat_ua, 1000);
1696
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001697 ocv_est_uv = vbat_uv + (ibat_ua * rbatt)/1000;
1698 pc_est = calculate_pc(chip, ocv_est_uv, batt_temp, last_chargecycles);
1699 soc_est = div_s64((s64)fcc_uah * pc_est - uuc_uah*100,
1700 (s64)fcc_uah - uuc_uah);
1701 soc_est = bound_soc(soc_est);
1702
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001703 if (ibat_ua < 0) {
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07001704 soc = charging_adjustments(chip, soc, vbat_uv, ibat_ua,
1705 batt_temp, chargecycles,
1706 fcc_uah, cc_uah, uuc_uah);
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07001707 goto out;
1708 }
1709
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001710 /*
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001711 * do not adjust
1712 * if soc is same as what bms calculated
1713 * if soc_est is between 45 and 25, this is the flat portion of the
1714 * curve where soc_est is not so accurate. We generally don't want to
1715 * adjust when soc_est is inaccurate except for the cases when soc is
1716 * way far off (higher than 50 or lesser than 20).
Abhijeet Dharmapurikar52daf652012-07-19 11:50:47 -07001717 * Also don't adjust soc if it is above 90 becuase we might pull it low
1718 * and cause a bad user experience
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001719 */
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07001720 if (soc_est == soc
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -07001721 || (is_between(45, chip->adjust_soc_low_threshold, soc_est)
1722 && is_between(50, chip->adjust_soc_low_threshold - 5, soc))
Abhijeet Dharmapurikar52daf652012-07-19 11:50:47 -07001723 || soc >= 90)
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001724 goto out;
1725
1726 if (last_soc_est == -EINVAL)
1727 last_soc_est = soc;
1728
1729 n = min(200, max(1 , soc + soc_est + last_soc_est));
1730 /* remember the last soc_est in last_soc_est */
1731 last_soc_est = soc_est;
1732
1733 pc = calculate_pc(chip, chip->last_ocv_uv,
1734 batt_temp, last_chargecycles);
1735 if (pc > 0) {
1736 pc_new = calculate_pc(chip, chip->last_ocv_uv - (++m * 1000),
1737 batt_temp, last_chargecycles);
1738 while (pc_new == pc) {
1739 /* start taking 10mV steps */
1740 m = m + 10;
1741 pc_new = calculate_pc(chip,
1742 chip->last_ocv_uv - (m * 1000),
1743 batt_temp, last_chargecycles);
1744 }
1745 } else {
1746 /*
1747 * pc is already at the lowest point,
1748 * assume 1 millivolt translates to 1% pc
1749 */
1750 pc = 1;
1751 pc_new = 0;
1752 m = 1;
1753 }
1754
1755 delta_ocv_uv = div_s64((soc - soc_est) * (s64)m * 1000,
1756 n * (pc - pc_new));
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -07001757
1758 if (abs(delta_ocv_uv) > delta_ocv_uv_limit) {
1759 pr_debug("limiting delta ocv %d limit = %d\n", delta_ocv_uv,
1760 delta_ocv_uv_limit);
1761
1762 if (delta_ocv_uv > 0)
1763 delta_ocv_uv = delta_ocv_uv_limit;
1764 else
1765 delta_ocv_uv = -1 * delta_ocv_uv_limit;
1766 pr_debug("new delta ocv = %d\n", delta_ocv_uv);
1767 }
1768
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001769 chip->last_ocv_uv -= delta_ocv_uv;
1770
1771 if (chip->last_ocv_uv >= chip->max_voltage_uv)
1772 chip->last_ocv_uv = chip->max_voltage_uv;
1773
1774 /* calculate the soc based on this new ocv */
1775 pc_new = calculate_pc(chip, chip->last_ocv_uv,
1776 batt_temp, last_chargecycles);
1777 rc_new_uah = (fcc_uah * pc_new) / 100;
1778 soc_new = (rc_new_uah - cc_uah - uuc_uah)*100 / (fcc_uah - uuc_uah);
1779 soc_new = bound_soc(soc_new);
1780
1781 /*
1782 * if soc_new is ZERO force it higher so that phone doesnt report soc=0
1783 * soc = 0 should happen only when soc_est == 0
1784 */
1785 if (soc_new == 0 && soc_est != 0)
taiyeon.kimbfafe7e2012-08-24 11:51:29 +09001786 soc_new = 2;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001787
1788 soc = soc_new;
1789
1790out:
kyungtae.ohe346f7a2012-08-08 22:07:25 +09001791 pr_info("ibat_ua = %d, vbat_uv = %d, soc = %d, batt_temp=%d\n",
1792 ibat_ua, vbat_uv, soc, batt_temp);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07001793
1794 return soc;
1795}
1796
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -07001797#define IGNORE_SOC_TEMP_DECIDEG 50
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001798#define IAVG_STEP_SIZE_MA 50
1799#define IAVG_START 600
1800#define SOC_ZERO 0xFF
1801static void backup_soc_and_iavg(struct pm8921_bms_chip *chip, int batt_temp,
1802 int soc)
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001803{
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001804 u8 temp;
1805 int iavg_ma = chip->prev_uuc_iavg_ma;
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001806
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001807 if (iavg_ma > IAVG_START)
1808 temp = (iavg_ma - IAVG_START) / IAVG_STEP_SIZE_MA;
1809 else
1810 temp = 0;
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001811
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001812 pm_bms_masked_write(chip, TEMP_IAVG_STORAGE,
1813 TEMP_IAVG_STORAGE_USE_MASK, temp);
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001814
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001815 /* since only 6 bits are available for SOC, we store half the soc */
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -07001816 if (soc == 0)
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001817 temp = SOC_ZERO;
1818 else
1819 temp = soc;
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001820
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001821 /* don't store soc if temperature is below 5degC */
1822 if (batt_temp > IGNORE_SOC_TEMP_DECIDEG)
1823 pm8xxx_writeb(the_chip->dev->parent, TEMP_SOC_STORAGE, temp);
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001824}
1825
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001826static void read_shutdown_soc_and_iavg(struct pm8921_bms_chip *chip)
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001827{
1828 int rc;
1829 u8 temp;
1830
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001831 rc = pm8xxx_readb(chip->dev->parent, TEMP_IAVG_STORAGE, &temp);
1832 if (rc) {
1833 pr_err("failed to read addr = %d %d assuming %d\n",
1834 TEMP_IAVG_STORAGE, rc, IAVG_START);
1835 chip->shutdown_iavg_ua = IAVG_START;
1836 } else {
1837 temp &= TEMP_IAVG_STORAGE_USE_MASK;
1838
1839 if (temp == 0) {
1840 chip->shutdown_iavg_ua = IAVG_START;
1841 } else {
1842 chip->shutdown_iavg_ua = IAVG_START
1843 + IAVG_STEP_SIZE_MA * (temp + 1);
1844 }
1845 }
1846
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001847 rc = pm8xxx_readb(chip->dev->parent, TEMP_SOC_STORAGE, &temp);
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001848 if (rc) {
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001849 pr_err("failed to read addr = %d %d\n", TEMP_SOC_STORAGE, rc);
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001850 } else {
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001851 chip->shutdown_soc = temp;
1852
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001853 if (chip->shutdown_soc == 0) {
1854 pr_debug("No shutdown soc available\n");
1855 shutdown_soc_invalid = 1;
1856 chip->shutdown_iavg_ua = 0;
1857 } else if (chip->shutdown_soc == SOC_ZERO) {
1858 chip->shutdown_soc = 0;
1859 }
1860 }
Abhijeet Dharmapurikar1f4cc512012-07-21 00:10:39 -07001861
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001862 if (chip->ignore_shutdown_soc) {
1863 shutdown_soc_invalid = 1;
1864 chip->shutdown_soc = 0;
1865 chip->shutdown_iavg_ua = 0;
1866 }
1867
1868 pr_debug("shutdown_soc = %d shutdown_iavg = %d shutdown_soc_invalid = %d\n",
1869 chip->shutdown_soc,
1870 chip->shutdown_iavg_ua,
1871 shutdown_soc_invalid);
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001872}
1873
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07001874#define SOC_CATCHUP_SEC_MAX 600
1875#define SOC_CATCHUP_SEC_PER_PERCENT 60
1876#define MAX_CATCHUP_SOC (SOC_CATCHUP_SEC_MAX/SOC_CATCHUP_SEC_PER_PERCENT)
1877static int scale_soc_while_chg(struct pm8921_bms_chip *chip,
1878 int delta_time_us, int new_soc, int prev_soc)
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001879{
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07001880 int chg_time_sec;
1881 int catch_up_sec;
1882 int scaled_soc;
1883 int numerator;
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07001884
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07001885 /*
1886 * The device must be charging for reporting a higher soc, if
1887 * not ignore this soc and continue reporting the prev_soc.
1888 * Also don't report a high value immediately slowly scale the
1889 * value from prev_soc to the new soc based on a charge time
1890 * weighted average
1891 */
1892
1893 /* if we are not charging return last soc */
1894 if (the_chip->start_percent == -EINVAL)
1895 return prev_soc;
1896
1897 /* if soc is called in quick succession return the last soc */
1898 if (delta_time_us < USEC_PER_SEC)
1899 return prev_soc;
1900
1901 chg_time_sec = DIV_ROUND_UP(the_chip->charge_time_us, USEC_PER_SEC);
1902 catch_up_sec = DIV_ROUND_UP(the_chip->catch_up_time_us, USEC_PER_SEC);
1903 pr_debug("cts= %d catch_up_sec = %d\n", chg_time_sec, catch_up_sec);
1904
1905 /*
1906 * if we have been charging for more than catch_up time simply return
1907 * new soc
1908 */
1909 if (chg_time_sec > catch_up_sec)
1910 return new_soc;
1911
1912 numerator = (catch_up_sec - chg_time_sec) * prev_soc
1913 + chg_time_sec * new_soc;
1914 scaled_soc = numerator / catch_up_sec;
1915
1916 pr_debug("cts = %d new_soc = %d prev_soc = %d scaled_soc = %d\n",
1917 chg_time_sec, new_soc, prev_soc, scaled_soc);
1918
1919 return scaled_soc;
1920}
1921
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07001922static bool is_shutdown_soc_within_limits(struct pm8921_bms_chip *chip, int soc)
1923{
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001924 if (shutdown_soc_invalid) {
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07001925 pr_debug("NOT forcing shutdown soc = %d\n", chip->shutdown_soc);
1926 return 0;
1927 }
1928
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07001929 if (abs(chip->shutdown_soc - soc) > chip->shutdown_soc_valid_limit) {
1930 pr_debug("rejecting shutdown soc = %d, soc = %d limit = %d\n",
1931 chip->shutdown_soc, soc,
1932 chip->shutdown_soc_valid_limit);
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07001933 shutdown_soc_invalid = 1;
1934 return 0;
1935 }
1936
1937 return 1;
1938}
choongryeol.lee76048da2012-09-11 14:19:21 -07001939
1940static int is_eoc_adjust(struct pm8921_bms_chip *chip, int soc)
1941{
1942 int batt_state = pm8921_get_batt_state();
1943 int ret = 0;
1944
1945 if (soc != 100)
1946 return 0;
1947
1948 switch (batt_state) {
1949 case POWER_SUPPLY_STATUS_CHARGING:
1950 if (chip->start_percent != -EINVAL
1951 && chip->start_percent != 100)
1952 ret = 1;
1953 break;
1954 case POWER_SUPPLY_STATUS_DISCHARGING:
1955 case POWER_SUPPLY_STATUS_NOT_CHARGING:
1956 if (chip->soc_adjusted == 1)
1957 ret = 1;
1958 break;
1959 default:
1960 break;
1961 }
1962
1963 return ret;
1964}
1965
1966static int is_recharging(struct pm8921_bms_chip *chip, int soc)
1967{
1968 if (soc == -EINVAL)
1969 return 0;
choongryeol.lee7a72ca62012-09-20 21:50:16 -07001970 if ((pm8921_get_batt_state() == POWER_SUPPLY_STATUS_FULL)
1971 && (soc < 100)
1972 && (pm8921_get_batt_health()
1973 != POWER_SUPPLY_HEALTH_OVERHEAT))
choongryeol.lee76048da2012-09-11 14:19:21 -07001974 return 1;
1975 return 0;
1976}
1977
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001978/*
1979 * Remaining Usable Charge = remaining_charge (charge at ocv instance)
1980 * - coloumb counter charge
1981 * - unusable charge (due to battery resistance)
1982 * SOC% = (remaining usable charge/ fcc - usable_charge);
1983 */
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001984static int calculate_state_of_charge(struct pm8921_bms_chip *chip,
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08001985 struct pm8921_soc_params *raw,
1986 int batt_temp, int chargecycles)
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07001987{
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001988 int remaining_usable_charge_uah, fcc_uah, unusable_charge_uah;
1989 int remaining_charge_uah, soc;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08001990 int cc_uah;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08001991 int rbatt;
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07001992 int iavg_ua;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001993 int delta_time_s;
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07001994 int new_ocv;
Abhijeet Dharmapurikar6f42a232012-07-17 22:11:13 -07001995 int new_rc_uah;
1996 int new_ucc_uah;
1997 int new_rbatt;
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07001998 int shutdown_soc;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07001999 static int firsttime = 1;
Abhijeet Dharmapurikar020b37c2011-07-20 14:14:51 -07002000
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002001 calculate_soc_params(chip, raw, batt_temp, chargecycles,
2002 &fcc_uah,
2003 &unusable_charge_uah,
2004 &remaining_charge_uah,
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002005 &cc_uah,
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07002006 &rbatt,
2007 &iavg_ua,
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07002008 &delta_time_s);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002009
2010 /* calculate remaining usable charge */
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002011 remaining_usable_charge_uah = remaining_charge_uah
2012 - cc_uah
2013 - unusable_charge_uah;
2014
2015 pr_debug("RUC = %duAh\n", remaining_usable_charge_uah);
Abhijeet Dharmapurikarbbae88312012-02-14 18:56:42 -08002016 if (fcc_uah - unusable_charge_uah <= 0) {
2017 pr_warn("FCC = %duAh, UUC = %duAh forcing soc = 0\n",
2018 fcc_uah, unusable_charge_uah);
2019 soc = 0;
2020 } else {
Abhijeet Dharmapurikar4ff213c2012-07-11 21:28:29 -07002021 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2022 (fcc_uah - unusable_charge_uah));
Abhijeet Dharmapurikarbbae88312012-02-14 18:56:42 -08002023 }
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002024
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07002025 if (firsttime && soc < 0) {
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002026 /*
2027 * first time calcualtion and the pon ocv is too low resulting
2028 * in a bad soc. Adjust ocv such that we get 0 soc
2029 */
2030 pr_debug("soc is %d, adjusting pon ocv to make it 0\n", soc);
2031 adjust_rc_and_uuc_for_specific_soc(
2032 chip,
2033 batt_temp, chargecycles,
2034 0,
2035 fcc_uah, unusable_charge_uah,
2036 cc_uah, remaining_charge_uah,
2037 rbatt,
2038 &new_ocv,
2039 &new_rc_uah, &new_ucc_uah,
2040 &new_rbatt);
2041 chip->last_ocv_uv = new_ocv;
2042 remaining_charge_uah = new_rc_uah;
2043 unusable_charge_uah = new_ucc_uah;
2044 rbatt = new_rbatt;
2045
2046 remaining_usable_charge_uah = remaining_charge_uah
2047 - cc_uah
2048 - unusable_charge_uah;
2049
2050 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2051 (fcc_uah - unusable_charge_uah));
2052 pr_debug("DONE for O soc is %d, pon ocv adjusted to %duV\n",
2053 soc, chip->last_ocv_uv);
Abhijeet Dharmapurikar986bd8a2012-01-05 15:15:15 -08002054 }
2055
2056 if (soc > 100)
2057 soc = 100;
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07002058
Abhijeet Dharmapurikar986bd8a2012-01-05 15:15:15 -08002059 if (bms_fake_battery != -EINVAL) {
2060 pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery);
2061 return bms_fake_battery;
2062 }
2063
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07002064 if (soc < 0) {
Abhijeet Dharmapurikar21ebadd2011-09-28 21:14:16 -07002065 pr_err("bad rem_usb_chg = %d rem_chg %d,"
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002066 "cc_uah %d, unusb_chg %d\n",
2067 remaining_usable_charge_uah,
2068 remaining_charge_uah,
2069 cc_uah, unusable_charge_uah);
2070
Abhijeet Dharmapurikar21ebadd2011-09-28 21:14:16 -07002071 pr_err("for bad rem_usb_chg last_ocv_uv = %d"
Abhijeet Dharmapurikaraf6f9e22011-10-03 19:19:02 -07002072 "chargecycles = %d, batt_temp = %d"
2073 "fcc = %d soc =%d\n",
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002074 chip->last_ocv_uv, chargecycles, batt_temp,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002075 fcc_uah, soc);
Abhijeet Dharmapurikar95971a82011-12-06 18:23:57 -08002076 soc = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002077 }
2078
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002079 mutex_lock(&soc_invalidation_mutex);
2080 shutdown_soc = chip->shutdown_soc;
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07002081
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07002082 if (firsttime && soc != shutdown_soc
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002083 && is_shutdown_soc_within_limits(chip, soc)) {
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002084 /*
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002085 * soc for the first time - use shutdown soc
2086 * to adjust pon ocv since it is a small percent away from
2087 * the real soc
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002088 */
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002089 pr_debug("soc = %d before forcing shutdown_soc = %d\n",
2090 soc, shutdown_soc);
2091 adjust_rc_and_uuc_for_specific_soc(
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002092 chip,
2093 batt_temp, chargecycles,
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002094 shutdown_soc,
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002095 fcc_uah, unusable_charge_uah,
2096 cc_uah, remaining_charge_uah,
2097 rbatt,
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002098 &new_ocv,
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002099 &new_rc_uah, &new_ucc_uah,
2100 &new_rbatt);
2101
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002102 chip->pon_ocv_uv = chip->last_ocv_uv;
2103 chip->last_ocv_uv = new_ocv;
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002104 remaining_charge_uah = new_rc_uah;
2105 unusable_charge_uah = new_ucc_uah;
2106 rbatt = new_rbatt;
2107
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002108 remaining_usable_charge_uah = remaining_charge_uah
2109 - cc_uah
2110 - unusable_charge_uah;
2111
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002112 soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100),
2113 (fcc_uah - unusable_charge_uah));
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002114
2115 pr_debug("DONE for shutdown_soc = %d soc is %d, adjusted ocv to %duV\n",
2116 shutdown_soc, soc, chip->last_ocv_uv);
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002117 }
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002118 mutex_unlock(&soc_invalidation_mutex);
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002119
Abhijeet Dharmapurikar0eb50ee2012-08-14 13:26:48 -07002120 pr_debug("SOC before adjustment = %d\n", soc);
2121 calculated_soc = adjust_soc(chip, soc, batt_temp, chargecycles,
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07002122 rbatt, fcc_uah, unusable_charge_uah, cc_uah);
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07002123
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002124 pr_debug("calculated SOC = %d\n", calculated_soc);
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07002125 firsttime = 0;
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002126 return calculated_soc;
2127}
2128
2129#define CALCULATE_SOC_MS 20000
2130static void calculate_soc_work(struct work_struct *work)
2131{
2132 struct pm8921_bms_chip *chip = container_of(work,
2133 struct pm8921_bms_chip,
2134 calculate_soc_delayed_work.work);
2135 int batt_temp, rc;
2136 struct pm8xxx_adc_chan_result result;
2137 struct pm8921_soc_params raw;
2138 int soc;
2139
2140 rc = pm8xxx_adc_read(chip->batt_temp_channel, &result);
2141 if (rc) {
2142 pr_err("error reading adc channel = %d, rc = %d\n",
2143 chip->batt_temp_channel, rc);
2144 return;
2145 }
2146 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
2147 result.measurement);
2148 batt_temp = (int)result.physical;
2149
2150 mutex_lock(&chip->last_ocv_uv_mutex);
2151 read_soc_params_raw(chip, &raw);
2152
2153 soc = calculate_state_of_charge(chip, &raw,
2154 batt_temp, last_chargecycles);
choongryeol.lee76048da2012-09-11 14:19:21 -07002155
2156 if (chip->eoc_check_soc
2157 && is_recharging(chip, chip->last_reported_soc)) {
2158 pm8921_force_start_charging();
2159 pr_info("Recharging is started\n");
2160 }
2161
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002162 mutex_unlock(&chip->last_ocv_uv_mutex);
2163
2164 schedule_delayed_work(&chip->calculate_soc_delayed_work,
2165 round_jiffies_relative(msecs_to_jiffies
2166 (CALCULATE_SOC_MS)));
2167}
2168
2169static int report_state_of_charge(struct pm8921_bms_chip *chip)
2170{
2171 int soc = calculated_soc;
2172 int delta_time_us;
2173 struct timespec now;
2174 struct pm8xxx_adc_chan_result result;
2175 int batt_temp;
2176 int rc;
2177
2178 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
2179 if (rc) {
2180 pr_err("error reading adc channel = %d, rc = %d\n",
2181 the_chip->batt_temp_channel, rc);
2182 return rc;
2183 }
2184 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
2185 result.measurement);
2186 batt_temp = (int)result.physical;
2187
2188 do_posix_clock_monotonic_gettime(&now);
2189 if (chip->t_soc_queried.tv_sec != 0) {
2190 delta_time_us
2191 = (now.tv_sec - chip->t_soc_queried.tv_sec) * USEC_PER_SEC
2192 + (now.tv_nsec - chip->t_soc_queried.tv_nsec) / 1000;
2193 } else {
2194 /* calculation for the first time */
2195 delta_time_us = 0;
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07002196 }
2197
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002198 /*
2199 * account for charge time - limit it to SOC_CATCHUP_SEC to
2200 * avoid overflows when charging continues for extended periods
2201 */
2202 if (the_chip->start_percent != -EINVAL) {
2203 if (the_chip->charge_time_us == 0) {
2204 /*
2205 * calculating soc for the first time
2206 * after start of chg. Initialize catchup time
2207 */
2208 if (abs(soc - last_soc) < MAX_CATCHUP_SOC)
2209 the_chip->catch_up_time_us =
2210 (soc - last_soc) * SOC_CATCHUP_SEC_PER_PERCENT
2211 * USEC_PER_SEC;
2212 else
2213 the_chip->catch_up_time_us =
2214 SOC_CATCHUP_SEC_MAX * USEC_PER_SEC;
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07002215
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002216 if (the_chip->catch_up_time_us < 0)
2217 the_chip->catch_up_time_us = 0;
2218 }
2219
2220 /* add charge time */
2221 if (the_chip->charge_time_us
2222 < SOC_CATCHUP_SEC_MAX * USEC_PER_SEC)
2223 chip->charge_time_us += delta_time_us;
2224
2225 /* end catchup if calculated soc and last soc are same */
2226 if (last_soc == soc)
2227 the_chip->catch_up_time_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002228 }
2229
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002230 /* last_soc < soc ... scale and catch up */
choongryeol.leeeb59bf62012-10-01 13:56:17 -07002231 if (last_soc != -EINVAL && soc != 100
2232 && (last_soc < soc || the_chip->start_percent != -EINVAL))
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002233 soc = scale_soc_while_chg(chip, delta_time_us, soc, last_soc);
2234
choongryeol.lee76048da2012-09-11 14:19:21 -07002235 if (chip->eoc_check_soc && is_eoc_adjust(chip, soc)) {
2236 soc = soc - 1;
2237 chip->soc_adjusted = 1;
2238 } else {
2239 chip->soc_adjusted = 0;
2240 }
2241
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002242 last_soc = soc;
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002243 backup_soc_and_iavg(chip, batt_temp, last_soc);
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -07002244 pr_debug("Reported SOC = %d\n", last_soc);
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002245 chip->t_soc_queried = now;
choongryeol.lee76048da2012-09-11 14:19:21 -07002246 chip->last_reported_soc = last_soc;
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -07002247
2248 return last_soc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002249}
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07002250
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002251void pm8921_bms_invalidate_shutdown_soc(void)
2252{
2253 int calculate_soc = 0;
2254 struct pm8921_bms_chip *chip = the_chip;
2255 int batt_temp, rc;
2256 struct pm8xxx_adc_chan_result result;
2257 struct pm8921_soc_params raw;
2258 int soc;
2259
2260 pr_debug("Invalidating shutdown soc - the battery was removed\n");
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002261 if (shutdown_soc_invalid)
2262 return;
2263
Abhijeet Dharmapurikard74be6b2012-07-21 00:02:42 -07002264 mutex_lock(&soc_invalidation_mutex);
2265 shutdown_soc_invalid = 1;
2266 last_soc = -EINVAL;
2267 if (the_chip) {
2268 /* reset to pon ocv undoing what the adjusting did */
2269 if (the_chip->pon_ocv_uv) {
2270 the_chip->last_ocv_uv = the_chip->pon_ocv_uv;
2271 calculate_soc = 1;
2272 pr_debug("resetting ocv to pon_ocv = %d\n",
2273 the_chip->pon_ocv_uv);
2274 }
2275 }
2276 mutex_unlock(&soc_invalidation_mutex);
2277 if (!calculate_soc)
2278 return;
2279
2280 rc = pm8xxx_adc_read(chip->batt_temp_channel, &result);
2281 if (rc) {
2282 pr_err("error reading adc channel = %d, rc = %d\n",
2283 chip->batt_temp_channel, rc);
2284 return;
2285 }
2286 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
2287 result.measurement);
2288 batt_temp = (int)result.physical;
2289
2290 mutex_lock(&chip->last_ocv_uv_mutex);
2291 read_soc_params_raw(chip, &raw);
2292
2293 soc = calculate_state_of_charge(chip, &raw,
2294 batt_temp, last_chargecycles);
2295 mutex_unlock(&chip->last_ocv_uv_mutex);
2296}
2297EXPORT_SYMBOL(pm8921_bms_invalidate_shutdown_soc);
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002298
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08002299#define MIN_DELTA_625_UV 1000
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002300static void calib_hkadc(struct pm8921_bms_chip *chip)
2301{
2302 int voltage, rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07002303 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08002304 int usb_chg;
2305 int this_delta;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002306
Abhijeet Dharmapurikar9c0e6042012-07-12 14:55:13 -07002307 mutex_lock(&chip->calib_mutex);
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07002308 rc = pm8xxx_adc_read(the_chip->ref1p25v_channel, &result);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002309 if (rc) {
2310 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
Abhijeet Dharmapurikar9c0e6042012-07-12 14:55:13 -07002311 goto out;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002312 }
Abhijeet Dharmapurikar619cb0c2011-12-15 15:28:14 -08002313 voltage = xoadc_reading_to_microvolt(result.adc_code);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002314
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07002315 pr_debug("result 1.25v = 0x%x, voltage = %duV adc_meas = %lld\n",
2316 result.adc_code, voltage, result.measurement);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002317
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002318 chip->xoadc_v125 = voltage;
2319
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07002320 rc = pm8xxx_adc_read(the_chip->ref625mv_channel, &result);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002321 if (rc) {
2322 pr_err("ADC failed for 1.25volts rc = %d\n", rc);
Abhijeet Dharmapurikar9c0e6042012-07-12 14:55:13 -07002323 goto out;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002324 }
Abhijeet Dharmapurikar619cb0c2011-12-15 15:28:14 -08002325 voltage = xoadc_reading_to_microvolt(result.adc_code);
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08002326
choongryeol.lee7346d0f2012-10-10 16:25:38 -07002327 usb_chg = usb_chg_plugged_in(chip);
kyungtae.oh1ff608a2012-07-19 14:31:54 +09002328 usb_chg |= wireless_chg_plugged_in();
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08002329 pr_debug("result 0.625V = 0x%x, voltage = %duV adc_meas = %lld "
2330 "usb_chg = %d\n",
2331 result.adc_code, voltage, result.measurement,
2332 usb_chg);
2333
2334 if (usb_chg)
2335 chip->xoadc_v0625_usb_present = voltage;
2336 else
2337 chip->xoadc_v0625_usb_absent = voltage;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002338
2339 chip->xoadc_v0625 = voltage;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08002340 if (chip->xoadc_v0625_usb_present && chip->xoadc_v0625_usb_absent) {
2341 this_delta = chip->xoadc_v0625_usb_present
2342 - chip->xoadc_v0625_usb_absent;
2343 pr_debug("this_delta= %duV\n", this_delta);
2344 if (this_delta > MIN_DELTA_625_UV)
2345 last_usb_cal_delta_uv = this_delta;
2346 pr_debug("625V_present= %d, 625V_absent= %d, delta = %duV\n",
2347 chip->xoadc_v0625_usb_present,
2348 chip->xoadc_v0625_usb_absent,
2349 last_usb_cal_delta_uv);
2350 }
Abhijeet Dharmapurikar9c0e6042012-07-12 14:55:13 -07002351out:
2352 mutex_unlock(&chip->calib_mutex);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002353}
2354
2355static void calibrate_hkadc_work(struct work_struct *work)
2356{
2357 struct pm8921_bms_chip *chip = container_of(work,
2358 struct pm8921_bms_chip, calib_hkadc_work);
2359
2360 calib_hkadc(chip);
2361}
2362
Abhijeet Dharmapurikar1b8e8292012-01-17 11:01:44 -08002363void pm8921_bms_calibrate_hkadc(void)
2364{
2365 schedule_work(&the_chip->calib_hkadc_work);
2366}
2367
Abhijeet Dharmapurikar9c0e6042012-07-12 14:55:13 -07002368#define HKADC_CALIB_DELAY_MS 600000
2369static void calibrate_hkadc_delayed_work(struct work_struct *work)
2370{
2371 struct pm8921_bms_chip *chip = container_of(work,
2372 struct pm8921_bms_chip,
2373 calib_hkadc_delayed_work.work);
2374
2375 calib_hkadc(chip);
2376 schedule_delayed_work(&chip->calib_hkadc_delayed_work,
2377 round_jiffies_relative(msecs_to_jiffies
2378 (HKADC_CALIB_DELAY_MS)));
2379}
2380
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07002381int pm8921_bms_get_vsense_avg(int *result)
2382{
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002383 int rc = -EINVAL;
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002384
2385 if (the_chip) {
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002386 mutex_lock(&the_chip->bms_output_lock);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002387 pm_bms_lock_output_data(the_chip);
2388 rc = read_vsense_avg(the_chip, result);
2389 pm_bms_unlock_output_data(the_chip);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002390 mutex_unlock(&the_chip->bms_output_lock);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002391 }
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07002392
2393 pr_err("called before initialization\n");
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002394 return rc;
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07002395}
2396EXPORT_SYMBOL(pm8921_bms_get_vsense_avg);
2397
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08002398int pm8921_bms_get_battery_current(int *result_ua)
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07002399{
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002400 int vsense;
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002401
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07002402 if (!the_chip) {
2403 pr_err("called before initialization\n");
2404 return -EINVAL;
2405 }
2406 if (the_chip->r_sense == 0) {
2407 pr_err("r_sense is zero\n");
2408 return -EINVAL;
2409 }
2410
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002411 mutex_lock(&the_chip->bms_output_lock);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002412 pm_bms_lock_output_data(the_chip);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002413 read_vsense_avg(the_chip, &vsense);
Abhijeet Dharmapurikar3f95fa82011-10-05 19:14:32 -07002414 pm_bms_unlock_output_data(the_chip);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002415 mutex_unlock(&the_chip->bms_output_lock);
2416 pr_debug("vsense=%duV\n", vsense);
Abhijeet Dharmapurikara7a1c6b2011-08-17 10:04:58 -07002417 /* cast for signed division */
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08002418 *result_ua = vsense * 1000 / (int)the_chip->r_sense;
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002419 pr_debug("ibat=%duA\n", *result_ua);
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07002420 return 0;
2421}
2422EXPORT_SYMBOL(pm8921_bms_get_battery_current);
2423
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002424int pm8921_bms_get_percent_charge(void)
2425{
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07002426 if (!the_chip) {
2427 pr_err("called before initialization\n");
2428 return -EINVAL;
2429 }
2430
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002431 return report_state_of_charge(the_chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002432}
2433EXPORT_SYMBOL_GPL(pm8921_bms_get_percent_charge);
2434
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002435int pm8921_bms_get_rbatt(void)
2436{
2437 int batt_temp, rc;
2438 struct pm8xxx_adc_chan_result result;
2439 struct pm8921_soc_params raw;
2440 int fcc_uah;
2441 int unusable_charge_uah;
2442 int remaining_charge_uah;
2443 int cc_uah;
2444 int rbatt;
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07002445 int iavg_ua;
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07002446 int delta_time_s;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002447
2448 if (!the_chip) {
2449 pr_err("called before initialization\n");
2450 return -EINVAL;
2451 }
2452
2453 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
2454 if (rc) {
2455 pr_err("error reading adc channel = %d, rc = %d\n",
2456 the_chip->batt_temp_channel, rc);
2457 return rc;
2458 }
2459 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
2460 result.measurement);
2461 batt_temp = (int)result.physical;
2462
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002463 mutex_lock(&the_chip->last_ocv_uv_mutex);
2464
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002465 read_soc_params_raw(the_chip, &raw);
2466
2467 calculate_soc_params(the_chip, &raw, batt_temp, last_chargecycles,
2468 &fcc_uah,
2469 &unusable_charge_uah,
2470 &remaining_charge_uah,
2471 &cc_uah,
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07002472 &rbatt,
2473 &iavg_ua,
Abhijeet Dharmapurikar16582152012-07-14 19:55:17 -07002474 &delta_time_s);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002475 mutex_unlock(&the_chip->last_ocv_uv_mutex);
2476
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002477 return rbatt;
2478}
2479EXPORT_SYMBOL_GPL(pm8921_bms_get_rbatt);
2480
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07002481int pm8921_bms_get_fcc(void)
2482{
2483 int batt_temp, rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07002484 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07002485
2486 if (!the_chip) {
2487 pr_err("called before initialization\n");
2488 return -EINVAL;
2489 }
2490
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07002491 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07002492 if (rc) {
2493 pr_err("error reading adc channel = %d, rc = %d\n",
2494 the_chip->batt_temp_channel, rc);
2495 return rc;
2496 }
2497 pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical,
2498 result.measurement);
2499 batt_temp = (int)result.physical;
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002500 return calculate_fcc_uah(the_chip, batt_temp, last_chargecycles);
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07002501}
2502EXPORT_SYMBOL_GPL(pm8921_bms_get_fcc);
2503
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -08002504#define IBAT_TOL_MASK 0x0F
2505#define OCV_TOL_MASK 0xF0
2506#define IBAT_TOL_DEFAULT 0x03
2507#define IBAT_TOL_NOCHG 0x0F
Abhijeet Dharmapurikarc99f5862012-03-14 19:33:16 -07002508#define OCV_TOL_DEFAULT 0x20
2509#define OCV_TOL_NO_OCV 0x00
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002510void pm8921_bms_charging_began(void)
2511{
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08002512 struct pm8921_soc_params raw;
2513
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002514 mutex_lock(&the_chip->last_ocv_uv_mutex);
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08002515 read_soc_params_raw(the_chip, &raw);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002516 mutex_unlock(&the_chip->last_ocv_uv_mutex);
2517
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07002518 the_chip->start_percent = report_state_of_charge(the_chip);
2519
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08002520 bms_start_percent = the_chip->start_percent;
2521 bms_start_ocv_uv = raw.last_good_ocv_uv;
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08002522 calculate_cc_uah(the_chip, raw.cc, &bms_start_cc_uah);
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -08002523 pm_bms_masked_write(the_chip, BMS_TOLERANCES,
2524 IBAT_TOL_MASK, IBAT_TOL_DEFAULT);
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002525 the_chip->charge_time_us = 0;
2526 the_chip->catch_up_time_us = 0;
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07002527
2528 the_chip->soc_at_cv = -EINVAL;
2529 the_chip->prev_chg_soc = -EINVAL;
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07002530 pr_debug("start_percent = %u%%\n", the_chip->start_percent);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002531}
2532EXPORT_SYMBOL_GPL(pm8921_bms_charging_began);
2533
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08002534#define DELTA_FCC_PERCENT 3
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002535#define MIN_START_PERCENT_FOR_LEARNING 30
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07002536void pm8921_bms_charging_end(int is_battery_full)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002537{
Abhijeet Dharmapurikarda8c3d52012-01-06 14:23:13 -08002538 int batt_temp, rc;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002539 struct pm8xxx_adc_chan_result result;
2540 struct pm8921_soc_params raw;
2541
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002542 if (the_chip == NULL)
2543 return;
2544
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002545 rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result);
2546 if (rc) {
2547 pr_err("error reading adc channel = %d, rc = %d\n",
2548 the_chip->batt_temp_channel, rc);
2549 return;
2550 }
2551 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
2552 result.measurement);
2553 batt_temp = (int)result.physical;
2554
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002555 mutex_lock(&the_chip->last_ocv_uv_mutex);
2556
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002557 read_soc_params_raw(the_chip, &raw);
2558
Abhijeet Dharmapurikar57aeb922012-01-25 13:22:26 -08002559 calculate_cc_uah(the_chip, raw.cc, &bms_end_cc_uah);
2560
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002561 bms_end_ocv_uv = raw.last_good_ocv_uv;
2562
Abhijeet Dharmapurikare88f2462012-04-25 18:22:38 -07002563 if (is_battery_full && the_chip->enable_fcc_learning
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002564 && the_chip->start_percent <= MIN_START_PERCENT_FOR_LEARNING) {
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002565 int fcc_uah, new_fcc_uah, delta_fcc_uah;
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07002566
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002567 new_fcc_uah = calculate_real_fcc_uah(the_chip, &raw,
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08002568 batt_temp, last_chargecycles,
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002569 &fcc_uah);
2570 delta_fcc_uah = new_fcc_uah - fcc_uah;
2571 if (delta_fcc_uah < 0)
2572 delta_fcc_uah = -delta_fcc_uah;
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08002573
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002574 if (delta_fcc_uah * 100 > (DELTA_FCC_PERCENT * fcc_uah)) {
2575 /* new_fcc_uah is outside the scope limit it */
2576 if (new_fcc_uah > fcc_uah)
2577 new_fcc_uah
Abhijeet Dharmapurikar873ffd12012-04-17 00:06:15 -07002578 = (fcc_uah +
2579 (DELTA_FCC_PERCENT * fcc_uah) / 100);
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002580 else
2581 new_fcc_uah
Abhijeet Dharmapurikar873ffd12012-04-17 00:06:15 -07002582 = (fcc_uah -
2583 (DELTA_FCC_PERCENT * fcc_uah) / 100);
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002584
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08002585 pr_debug("delta_fcc=%d > %d percent of fcc=%d"
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002586 "restring it to %d\n",
2587 delta_fcc_uah, DELTA_FCC_PERCENT,
2588 fcc_uah, new_fcc_uah);
Abhijeet Dharmapurikar8309df42011-11-16 21:04:09 -08002589 }
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -08002590
Abhijeet Dharmapurikarfe36d232012-03-06 20:17:54 -08002591 last_real_fcc_mah = new_fcc_uah/1000;
2592 last_real_fcc_batt_temp = batt_temp;
2593 readjust_fcc_table();
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002594 }
2595
2596 if (is_battery_full) {
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002597 the_chip->ocv_reading_at_100 = raw.last_good_ocv_raw;
2598 the_chip->cc_reading_at_100 = raw.cc;
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002599
2600 the_chip->last_ocv_uv = the_chip->max_voltage_uv;
2601 raw.last_good_ocv_uv = the_chip->max_voltage_uv;
Abhijeet Dharmapurikarb40b96f2012-04-24 13:19:25 -07002602 /*
2603 * since we are treating this as an ocv event
2604 * forget the old cc value
2605 */
2606 the_chip->last_cc_uah = 0;
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07002607 pr_debug("EOC BATT_FULL ocv_reading = 0x%x cc = 0x%x\n",
Abhijeet Dharmapurikar57b06b52011-11-07 19:14:08 -08002608 the_chip->ocv_reading_at_100,
2609 the_chip->cc_reading_at_100);
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07002610 }
2611
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002612 the_chip->end_percent = calculate_state_of_charge(the_chip, &raw,
2613 batt_temp, last_chargecycles);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002614 mutex_unlock(&the_chip->last_ocv_uv_mutex);
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08002615
2616 bms_end_percent = the_chip->end_percent;
Abhijeet Dharmapurikarab833312011-11-22 18:26:09 -08002617
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07002618 if (the_chip->end_percent > the_chip->start_percent) {
Abhijeet Dharmapurikar8a113f82012-01-19 10:58:45 -08002619 last_charge_increase +=
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07002620 the_chip->end_percent - the_chip->start_percent;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002621 if (last_charge_increase > 100) {
2622 last_chargecycles++;
2623 last_charge_increase = last_charge_increase % 100;
2624 }
2625 }
2626 pr_debug("end_percent = %u%% last_charge_increase = %d"
2627 "last_chargecycles = %d\n",
Abhijeet Dharmapurikar2ce2cc12011-11-02 15:40:29 -07002628 the_chip->end_percent,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002629 last_charge_increase,
2630 last_chargecycles);
Abhijeet Dharmapurikardba91f42011-12-21 16:56:13 -08002631 the_chip->start_percent = -EINVAL;
2632 the_chip->end_percent = -EINVAL;
Abhijeet Dharmapurikar647d0f82012-08-13 18:39:15 -07002633 the_chip->charge_time_us = 0;
2634 the_chip->catch_up_time_us = 0;
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07002635 the_chip->soc_at_cv = -EINVAL;
2636 the_chip->prev_chg_soc = -EINVAL;
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -08002637 pm_bms_masked_write(the_chip, BMS_TOLERANCES,
2638 IBAT_TOL_MASK, IBAT_TOL_NOCHG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002639}
2640EXPORT_SYMBOL_GPL(pm8921_bms_charging_end);
2641
Abhijeet Dharmapurikarc99f5862012-03-14 19:33:16 -07002642int pm8921_bms_stop_ocv_updates(struct pm8921_bms_chip *chip)
2643{
2644 pr_debug("stopping ocv updates\n");
2645 return pm_bms_masked_write(chip, BMS_TOLERANCES,
2646 OCV_TOL_MASK, OCV_TOL_NO_OCV);
2647}
2648EXPORT_SYMBOL_GPL(pm8921_bms_stop_ocv_updates);
2649
2650int pm8921_bms_start_ocv_updates(struct pm8921_bms_chip *chip)
2651{
2652 pr_debug("stopping ocv updates\n");
2653 return pm_bms_masked_write(chip, BMS_TOLERANCES,
2654 OCV_TOL_MASK, OCV_TOL_DEFAULT);
2655}
2656EXPORT_SYMBOL_GPL(pm8921_bms_start_ocv_updates);
2657
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002658static irqreturn_t pm8921_bms_sbi_write_ok_handler(int irq, void *data)
2659{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002660 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002661 return IRQ_HANDLED;
2662}
2663
2664static irqreturn_t pm8921_bms_cc_thr_handler(int irq, void *data)
2665{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002666 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002667 return IRQ_HANDLED;
2668}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002669
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002670static irqreturn_t pm8921_bms_vsense_thr_handler(int irq, void *data)
2671{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002672 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002673 return IRQ_HANDLED;
2674}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002675
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002676static irqreturn_t pm8921_bms_vsense_for_r_handler(int irq, void *data)
2677{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002678 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002679 return IRQ_HANDLED;
2680}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002681
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002682static irqreturn_t pm8921_bms_ocv_for_r_handler(int irq, void *data)
2683{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002684 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002685
2686 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002687 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002688 return IRQ_HANDLED;
2689}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002690
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002691static irqreturn_t pm8921_bms_good_ocv_handler(int irq, void *data)
2692{
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002693 struct pm8921_bms_chip *chip = data;
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002694
2695 pr_debug("irq = %d triggered", irq);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002696 schedule_work(&chip->calib_hkadc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002697 return IRQ_HANDLED;
2698}
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002699
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002700static irqreturn_t pm8921_bms_vsense_avg_handler(int irq, void *data)
2701{
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07002702 pr_debug("irq = %d triggered", irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002703 return IRQ_HANDLED;
2704}
2705
2706struct pm_bms_irq_init_data {
2707 unsigned int irq_id;
2708 char *name;
2709 unsigned long flags;
2710 irqreturn_t (*handler)(int, void *);
2711};
2712
2713#define BMS_IRQ(_id, _flags, _handler) \
2714{ \
2715 .irq_id = _id, \
2716 .name = #_id, \
2717 .flags = _flags, \
2718 .handler = _handler, \
2719}
2720
2721struct pm_bms_irq_init_data bms_irq_data[] = {
2722 BMS_IRQ(PM8921_BMS_SBI_WRITE_OK, IRQF_TRIGGER_RISING,
2723 pm8921_bms_sbi_write_ok_handler),
2724 BMS_IRQ(PM8921_BMS_CC_THR, IRQF_TRIGGER_RISING,
2725 pm8921_bms_cc_thr_handler),
2726 BMS_IRQ(PM8921_BMS_VSENSE_THR, IRQF_TRIGGER_RISING,
2727 pm8921_bms_vsense_thr_handler),
2728 BMS_IRQ(PM8921_BMS_VSENSE_FOR_R, IRQF_TRIGGER_RISING,
2729 pm8921_bms_vsense_for_r_handler),
2730 BMS_IRQ(PM8921_BMS_OCV_FOR_R, IRQF_TRIGGER_RISING,
2731 pm8921_bms_ocv_for_r_handler),
2732 BMS_IRQ(PM8921_BMS_GOOD_OCV, IRQF_TRIGGER_RISING,
2733 pm8921_bms_good_ocv_handler),
2734 BMS_IRQ(PM8921_BMS_VSENSE_AVG, IRQF_TRIGGER_RISING,
2735 pm8921_bms_vsense_avg_handler),
2736};
2737
2738static void free_irqs(struct pm8921_bms_chip *chip)
2739{
2740 int i;
2741
2742 for (i = 0; i < PM_BMS_MAX_INTS; i++)
2743 if (chip->pmic_bms_irq[i]) {
2744 free_irq(chip->pmic_bms_irq[i], NULL);
2745 chip->pmic_bms_irq[i] = 0;
2746 }
2747}
2748
2749static int __devinit request_irqs(struct pm8921_bms_chip *chip,
2750 struct platform_device *pdev)
2751{
2752 struct resource *res;
2753 int ret, i;
2754
2755 ret = 0;
2756 bitmap_fill(chip->enabled_irqs, PM_BMS_MAX_INTS);
2757
2758 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
2759 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
2760 bms_irq_data[i].name);
2761 if (res == NULL) {
2762 pr_err("couldn't find %s\n", bms_irq_data[i].name);
2763 goto err_out;
2764 }
2765 ret = request_irq(res->start, bms_irq_data[i].handler,
2766 bms_irq_data[i].flags,
2767 bms_irq_data[i].name, chip);
2768 if (ret < 0) {
2769 pr_err("couldn't request %d (%s) %d\n", res->start,
2770 bms_irq_data[i].name, ret);
2771 goto err_out;
2772 }
2773 chip->pmic_bms_irq[bms_irq_data[i].irq_id] = res->start;
2774 pm8921_bms_disable_irq(chip, bms_irq_data[i].irq_id);
2775 }
2776 return 0;
2777
2778err_out:
2779 free_irqs(chip);
2780 return -EINVAL;
2781}
2782
2783#define EN_BMS_BIT BIT(7)
2784#define EN_PON_HS_BIT BIT(0)
2785static int __devinit pm8921_bms_hw_init(struct pm8921_bms_chip *chip)
2786{
2787 int rc;
2788
2789 rc = pm_bms_masked_write(chip, BMS_CONTROL,
2790 EN_BMS_BIT | EN_PON_HS_BIT, EN_BMS_BIT | EN_PON_HS_BIT);
2791 if (rc) {
2792 pr_err("failed to enable pon and bms addr = %d %d",
2793 BMS_CONTROL, rc);
2794 }
2795
Abhijeet Dharmapurikarcad865a2012-02-09 17:14:02 -08002796 /* The charger will call start charge later if usb is present */
2797 pm_bms_masked_write(chip, BMS_TOLERANCES,
2798 IBAT_TOL_MASK, IBAT_TOL_NOCHG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002799 return 0;
2800}
2801
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002802static void check_initial_ocv(struct pm8921_bms_chip *chip)
2803{
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002804 int ocv_uv, rc;
2805 int16_t ocv_raw;
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08002806 int usb_chg;
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002807
2808 /*
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07002809 * Check if a ocv is available in bms hw,
2810 * if not compute it here at boot time and save it
2811 * in the last_ocv_uv.
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002812 */
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002813 ocv_uv = 0;
2814 pm_bms_read_output_data(chip, LAST_GOOD_OCV_VALUE, &ocv_raw);
choongryeol.lee7346d0f2012-10-10 16:25:38 -07002815 usb_chg = usb_chg_plugged_in(chip);
kyungtae.oh1ff608a2012-07-19 14:31:54 +09002816 usb_chg |= wireless_chg_plugged_in();
Abhijeet Dharmapurikaracca4182012-02-07 12:52:19 -08002817 rc = convert_vbatt_raw_to_uv(chip, usb_chg, ocv_raw, &ocv_uv);
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002818 if (rc || ocv_uv == 0) {
2819 rc = adc_based_ocv(chip, &ocv_uv);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07002820 if (rc) {
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002821 pr_err("failed to read adc based ocv_uv rc = %d\n", rc);
2822 ocv_uv = DEFAULT_OCV_MICROVOLTS;
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07002823 }
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002824 }
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07002825 chip->last_ocv_uv = ocv_uv;
2826 pr_debug("ocv_uv = %d last_ocv_uv = %d\n", ocv_uv, chip->last_ocv_uv);
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07002827}
2828
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002829static int64_t read_battery_id(struct pm8921_bms_chip *chip)
2830{
2831 int rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07002832 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002833
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07002834 rc = pm8xxx_adc_read(chip->batt_id_channel, &result);
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002835 if (rc) {
2836 pr_err("error reading batt id channel = %d, rc = %d\n",
2837 chip->vbat_channel, rc);
2838 return rc;
2839 }
2840 pr_debug("batt_id phy = %lld meas = 0x%llx\n", result.physical,
2841 result.measurement);
David Keitel8f2601b2012-02-14 22:31:07 -08002842 return result.adc_code;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002843}
2844
David Keitel8f2601b2012-02-14 22:31:07 -08002845#define PALLADIUM_ID_MIN 0x7F40
2846#define PALLADIUM_ID_MAX 0x7F5A
2847#define DESAY_5200_ID_MIN 0x7F7F
2848#define DESAY_5200_ID_MAX 0x802F
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002849static int set_battery_data(struct pm8921_bms_chip *chip)
2850{
2851 int64_t battery_id;
2852
David Keitel35e11872012-02-17 17:40:42 -08002853 if (chip->batt_type == BATT_DESAY)
2854 goto desay;
2855 else if (chip->batt_type == BATT_PALLADIUM)
2856 goto palladium;
roy.park1a749b72012-06-18 22:49:20 -07002857 else if (chip->batt_type == BATT_LGE)
2858 goto lge;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002859
David Keitel35e11872012-02-17 17:40:42 -08002860 battery_id = read_battery_id(chip);
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002861 if (battery_id < 0) {
2862 pr_err("cannot read battery id err = %lld\n", battery_id);
2863 return battery_id;
2864 }
2865
2866 if (is_between(PALLADIUM_ID_MIN, PALLADIUM_ID_MAX, battery_id)) {
David Keitel35e11872012-02-17 17:40:42 -08002867 goto palladium;
2868 } else if (is_between(DESAY_5200_ID_MIN, DESAY_5200_ID_MAX,
2869 battery_id)) {
2870 goto desay;
2871 } else {
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002872 pr_warn("invalid battid, palladium 1500 assumed batt_id %llx\n",
2873 battery_id);
2874 goto palladium;
David Keitel35e11872012-02-17 17:40:42 -08002875 }
2876
2877palladium:
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002878 chip->fcc = palladium_1500_data.fcc;
2879 chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut;
2880 chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut;
2881 chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut;
2882 chip->pc_sf_lut = palladium_1500_data.pc_sf_lut;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002883 chip->rbatt_sf_lut = palladium_1500_data.rbatt_sf_lut;
2884 chip->default_rbatt_mohm
2885 = palladium_1500_data.default_rbatt_mohm;
Abhijeet Dharmapurikarded189b2012-03-30 10:12:28 -07002886 chip->delta_rbatt_mohm = palladium_1500_data.delta_rbatt_mohm;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002887 return 0;
David Keitel35e11872012-02-17 17:40:42 -08002888desay:
David Keitel8f2601b2012-02-14 22:31:07 -08002889 chip->fcc = desay_5200_data.fcc;
2890 chip->fcc_temp_lut = desay_5200_data.fcc_temp_lut;
David Keitel8f2601b2012-02-14 22:31:07 -08002891 chip->pc_temp_ocv_lut = desay_5200_data.pc_temp_ocv_lut;
2892 chip->pc_sf_lut = desay_5200_data.pc_sf_lut;
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08002893 chip->rbatt_sf_lut = desay_5200_data.rbatt_sf_lut;
2894 chip->default_rbatt_mohm = desay_5200_data.default_rbatt_mohm;
Abhijeet Dharmapurikarded189b2012-03-30 10:12:28 -07002895 chip->delta_rbatt_mohm = desay_5200_data.delta_rbatt_mohm;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002896 return 0;
roy.park1a749b72012-06-18 22:49:20 -07002897lge:
2898 chip->fcc = lge_2100_mako_data.fcc;
2899 chip->fcc_temp_lut = lge_2100_mako_data.fcc_temp_lut;
2900 chip->fcc_sf_lut = lge_2100_mako_data.fcc_sf_lut;
2901 chip->pc_temp_ocv_lut = lge_2100_mako_data.pc_temp_ocv_lut;
2902 chip->pc_sf_lut = lge_2100_mako_data.pc_sf_lut;
2903 chip->rbatt_sf_lut = lge_2100_mako_data.rbatt_sf_lut;
2904 chip->default_rbatt_mohm
2905 = lge_2100_mako_data.default_rbatt_mohm;
2906 chip->delta_rbatt_mohm = lge_2100_mako_data.delta_rbatt_mohm;
2907 return 0;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07002908}
2909
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002910enum bms_request_operation {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002911 CALC_FCC,
2912 CALC_PC,
2913 CALC_SOC,
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002914 CALIB_HKADC,
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002915 CALIB_CCADC,
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002916 GET_VBAT_VSENSE_SIMULTANEOUS,
Abhijeet Dharmapurikarc99f5862012-03-14 19:33:16 -07002917 STOP_OCV,
2918 START_OCV,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002919};
2920
2921static int test_batt_temp = 5;
2922static int test_chargecycle = 150;
2923static int test_ocv = 3900000;
2924enum {
2925 TEST_BATT_TEMP,
2926 TEST_CHARGE_CYCLE,
2927 TEST_OCV,
2928};
2929static int get_test_param(void *data, u64 * val)
2930{
2931 switch ((int)data) {
2932 case TEST_BATT_TEMP:
2933 *val = test_batt_temp;
2934 break;
2935 case TEST_CHARGE_CYCLE:
2936 *val = test_chargecycle;
2937 break;
2938 case TEST_OCV:
2939 *val = test_ocv;
2940 break;
2941 default:
2942 return -EINVAL;
2943 }
2944 return 0;
2945}
2946static int set_test_param(void *data, u64 val)
2947{
2948 switch ((int)data) {
2949 case TEST_BATT_TEMP:
2950 test_batt_temp = (int)val;
2951 break;
2952 case TEST_CHARGE_CYCLE:
2953 test_chargecycle = (int)val;
2954 break;
2955 case TEST_OCV:
2956 test_ocv = (int)val;
2957 break;
2958 default:
2959 return -EINVAL;
2960 }
2961 return 0;
2962}
2963DEFINE_SIMPLE_ATTRIBUTE(temp_fops, get_test_param, set_test_param, "%llu\n");
2964
2965static int get_calc(void *data, u64 * val)
2966{
2967 int param = (int)data;
2968 int ret = 0;
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08002969 int ibat_ua, vbat_uv;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002970 struct pm8921_soc_params raw;
2971
2972 read_soc_params_raw(the_chip, &raw);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002973
2974 *val = 0;
2975
2976 /* global irq number passed in via data */
2977 switch (param) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002978 case CALC_FCC:
Abhijeet Dharmapurikar9c1e3992011-12-18 15:03:20 -08002979 *val = calculate_fcc_uah(the_chip, test_batt_temp,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002980 test_chargecycle);
2981 break;
2982 case CALC_PC:
2983 *val = calculate_pc(the_chip, test_ocv, test_batt_temp,
2984 test_chargecycle);
2985 break;
2986 case CALC_SOC:
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08002987 *val = calculate_state_of_charge(the_chip, &raw,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002988 test_batt_temp, test_chargecycle);
2989 break;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07002990 case CALIB_HKADC:
2991 /* reading this will trigger calibration */
2992 *val = 0;
2993 calib_hkadc(the_chip);
2994 break;
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002995 case CALIB_CCADC:
2996 /* reading this will trigger calibration */
2997 *val = 0;
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08002998 pm8xxx_calib_ccadc();
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07002999 break;
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08003000 case GET_VBAT_VSENSE_SIMULTANEOUS:
3001 /* reading this will call simultaneous vbat and vsense */
3002 *val =
3003 pm8921_bms_get_simultaneous_battery_voltage_and_current(
3004 &ibat_ua,
3005 &vbat_uv);
Abhijeet Dharmapurikarc99f5862012-03-14 19:33:16 -07003006 default:
3007 ret = -EINVAL;
3008 }
3009 return ret;
3010}
3011
3012static int set_calc(void *data, u64 val)
3013{
3014 int param = (int)data;
3015 int ret = 0;
3016
3017 switch (param) {
3018 case STOP_OCV:
3019 pm8921_bms_stop_ocv_updates(the_chip);
3020 break;
3021 case START_OCV:
3022 pm8921_bms_start_ocv_updates(the_chip);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08003023 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003024 default:
3025 ret = -EINVAL;
3026 }
3027 return ret;
3028}
Abhijeet Dharmapurikarc99f5862012-03-14 19:33:16 -07003029DEFINE_SIMPLE_ATTRIBUTE(calc_fops, get_calc, set_calc, "%llu\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003030
3031static int get_reading(void *data, u64 * val)
3032{
3033 int param = (int)data;
3034 int ret = 0;
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08003035 struct pm8921_soc_params raw;
3036
3037 read_soc_params_raw(the_chip, &raw);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003038
3039 *val = 0;
3040
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003041 switch (param) {
3042 case CC_MSB:
3043 case CC_LSB:
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08003044 *val = raw.cc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003045 break;
3046 case LAST_GOOD_OCV_VALUE:
Abhijeet Dharmapurikar228acb92011-11-21 19:21:48 -08003047 *val = raw.last_good_ocv_uv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003048 break;
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07003049 case VSENSE_AVG:
3050 read_vsense_avg(the_chip, (uint *)val);
3051 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003052 default:
3053 ret = -EINVAL;
3054 }
3055 return ret;
3056}
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07003057DEFINE_SIMPLE_ATTRIBUTE(reading_fops, get_reading, NULL, "%lld\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003058
3059static int get_rt_status(void *data, u64 * val)
3060{
3061 int i = (int)data;
3062 int ret;
3063
3064 /* global irq number passed in via data */
3065 ret = pm_bms_get_rt_status(the_chip, i);
3066 *val = ret;
3067 return 0;
3068}
3069DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n");
3070
3071static int get_reg(void *data, u64 * val)
3072{
3073 int addr = (int)data;
3074 int ret;
3075 u8 temp;
3076
3077 ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp);
3078 if (ret) {
3079 pr_err("pm8xxx_readb to %x value = %d errored = %d\n",
3080 addr, temp, ret);
3081 return -EAGAIN;
3082 }
3083 *val = temp;
3084 return 0;
3085}
3086
3087static int set_reg(void *data, u64 val)
3088{
3089 int addr = (int)data;
3090 int ret;
3091 u8 temp;
3092
3093 temp = (u8) val;
3094 ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp);
3095 if (ret) {
3096 pr_err("pm8xxx_writeb to %x value = %d errored = %d\n",
3097 addr, temp, ret);
3098 return -EAGAIN;
3099 }
3100 return 0;
3101}
3102DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
3103
3104static void create_debugfs_entries(struct pm8921_bms_chip *chip)
3105{
3106 int i;
3107
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08003108 chip->dent = debugfs_create_dir("pm8921-bms", NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003109
3110 if (IS_ERR(chip->dent)) {
3111 pr_err("pmic bms couldnt create debugfs dir\n");
3112 return;
3113 }
3114
3115 debugfs_create_file("BMS_CONTROL", 0644, chip->dent,
3116 (void *)BMS_CONTROL, &reg_fops);
3117 debugfs_create_file("BMS_OUTPUT0", 0644, chip->dent,
3118 (void *)BMS_OUTPUT0, &reg_fops);
3119 debugfs_create_file("BMS_OUTPUT1", 0644, chip->dent,
3120 (void *)BMS_OUTPUT1, &reg_fops);
3121 debugfs_create_file("BMS_TEST1", 0644, chip->dent,
3122 (void *)BMS_TEST1, &reg_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003123
3124 debugfs_create_file("test_batt_temp", 0644, chip->dent,
3125 (void *)TEST_BATT_TEMP, &temp_fops);
3126 debugfs_create_file("test_chargecycle", 0644, chip->dent,
3127 (void *)TEST_CHARGE_CYCLE, &temp_fops);
3128 debugfs_create_file("test_ocv", 0644, chip->dent,
3129 (void *)TEST_OCV, &temp_fops);
3130
3131 debugfs_create_file("read_cc", 0644, chip->dent,
3132 (void *)CC_MSB, &reading_fops);
3133 debugfs_create_file("read_last_good_ocv", 0644, chip->dent,
3134 (void *)LAST_GOOD_OCV_VALUE, &reading_fops);
3135 debugfs_create_file("read_vbatt_for_rbatt", 0644, chip->dent,
3136 (void *)VBATT_FOR_RBATT, &reading_fops);
3137 debugfs_create_file("read_vsense_for_rbatt", 0644, chip->dent,
3138 (void *)VSENSE_FOR_RBATT, &reading_fops);
3139 debugfs_create_file("read_ocv_for_rbatt", 0644, chip->dent,
3140 (void *)OCV_FOR_RBATT, &reading_fops);
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -07003141 debugfs_create_file("read_vsense_avg", 0644, chip->dent,
3142 (void *)VSENSE_AVG, &reading_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003143
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003144 debugfs_create_file("show_fcc", 0644, chip->dent,
3145 (void *)CALC_FCC, &calc_fops);
3146 debugfs_create_file("show_pc", 0644, chip->dent,
3147 (void *)CALC_PC, &calc_fops);
3148 debugfs_create_file("show_soc", 0644, chip->dent,
3149 (void *)CALC_SOC, &calc_fops);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07003150 debugfs_create_file("calib_hkadc", 0644, chip->dent,
3151 (void *)CALIB_HKADC, &calc_fops);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07003152 debugfs_create_file("calib_ccadc", 0644, chip->dent,
3153 (void *)CALIB_CCADC, &calc_fops);
Abhijeet Dharmapurikarc99f5862012-03-14 19:33:16 -07003154 debugfs_create_file("stop_ocv", 0644, chip->dent,
3155 (void *)STOP_OCV, &calc_fops);
3156 debugfs_create_file("start_ocv", 0644, chip->dent,
3157 (void *)START_OCV, &calc_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003158
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08003159 debugfs_create_file("simultaneous", 0644, chip->dent,
3160 (void *)GET_VBAT_VSENSE_SIMULTANEOUS, &calc_fops);
3161
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003162 for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) {
3163 if (chip->pmic_bms_irq[bms_irq_data[i].irq_id])
3164 debugfs_create_file(bms_irq_data[i].name, 0444,
3165 chip->dent,
3166 (void *)bms_irq_data[i].irq_id,
3167 &rt_fops);
3168 }
3169}
3170
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -07003171#define REG_SBI_CONFIG 0x04F
3172#define PAGE3_ENABLE_MASK 0x6
3173#define PROGRAM_REV_MASK 0x0F
3174#define PROGRAM_REV 0x9
3175static int read_ocv_trim(struct pm8921_bms_chip *chip)
3176{
3177 int rc;
3178 u8 reg, sbi_config;
3179
3180 rc = pm8xxx_readb(chip->dev->parent, REG_SBI_CONFIG, &sbi_config);
3181 if (rc) {
3182 pr_err("error = %d reading sbi config reg\n", rc);
3183 return rc;
3184 }
3185
3186 reg = sbi_config | PAGE3_ENABLE_MASK;
3187 rc = pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, reg);
3188 if (rc) {
3189 pr_err("error = %d writing sbi config reg\n", rc);
3190 return rc;
3191 }
3192
3193 rc = pm8xxx_readb(chip->dev->parent, TEST_PROGRAM_REV, &reg);
3194 if (rc)
3195 pr_err("Error %d reading %d addr %d\n",
3196 rc, reg, TEST_PROGRAM_REV);
3197 pr_err("program rev reg is 0x%x\n", reg);
3198 reg &= PROGRAM_REV_MASK;
3199
3200 /* If the revision is equal or higher do not adjust trim delta */
3201 if (reg >= PROGRAM_REV) {
3202 chip->amux_2_trim_delta = 0;
3203 goto restore_sbi_config;
3204 }
3205
3206 rc = pm8xxx_readb(chip->dev->parent, AMUX_TRIM_2, &reg);
3207 if (rc) {
3208 pr_err("error = %d reading trim reg\n", rc);
3209 return rc;
3210 }
3211
3212 pr_err("trim reg is 0x%x\n", reg);
3213 chip->amux_2_trim_delta = abs(0x49 - reg);
3214 pr_err("trim delta is %d\n", chip->amux_2_trim_delta);
3215
3216restore_sbi_config:
3217 rc = pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, sbi_config);
3218 if (rc) {
3219 pr_err("error = %d writing sbi config reg\n", rc);
3220 return rc;
3221 }
3222
3223 return 0;
3224}
3225
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003226static int __devinit pm8921_bms_probe(struct platform_device *pdev)
3227{
3228 int rc = 0;
Devin Kim5337a4e2012-07-12 13:49:11 -07003229 int vbatt = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003230 struct pm8921_bms_chip *chip;
3231 const struct pm8921_bms_platform_data *pdata
3232 = pdev->dev.platform_data;
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07003233
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003234 if (!pdata) {
3235 pr_err("missing platform data\n");
3236 return -EINVAL;
3237 }
3238
Abhijeet Dharmapurikar15e853d2011-06-27 20:55:47 -07003239 chip = kzalloc(sizeof(struct pm8921_bms_chip), GFP_KERNEL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003240 if (!chip) {
3241 pr_err("Cannot allocate pm_bms_chip\n");
3242 return -ENOMEM;
3243 }
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07003244
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -08003245 mutex_init(&chip->bms_output_lock);
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07003246 mutex_init(&chip->last_ocv_uv_mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003247 chip->dev = &pdev->dev;
3248 chip->r_sense = pdata->r_sense;
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -07003249 chip->v_cutoff = pdata->v_cutoff;
Abhijeet Dharmapurikara93ede82011-11-17 12:20:03 -08003250 chip->max_voltage_uv = pdata->max_voltage_uv;
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -07003251 chip->chg_term_ua = pdata->chg_term_ua;
David Keitel35e11872012-02-17 17:40:42 -08003252 chip->batt_type = pdata->battery_type;
Abhijeet Dharmapurikarbaffba42012-03-22 14:41:10 -07003253 chip->rconn_mohm = pdata->rconn_mohm;
Abhijeet Dharmapurikardba91f42011-12-21 16:56:13 -08003254 chip->start_percent = -EINVAL;
3255 chip->end_percent = -EINVAL;
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -07003256 chip->shutdown_soc_valid_limit = pdata->shutdown_soc_valid_limit;
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -07003257 chip->adjust_soc_low_threshold = pdata->adjust_soc_low_threshold;
3258 if (chip->adjust_soc_low_threshold >= 45)
3259 chip->adjust_soc_low_threshold = 45;
3260
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07003261 chip->prev_pc_unusable = -EINVAL;
Abhijeet Dharmapurikard3c435e2012-08-16 19:23:18 -07003262 chip->soc_at_cv = -EINVAL;
Abhijeet Dharmapurikar2f7f6fc2012-08-03 15:31:34 -07003263
Abhijeet Dharmapurikar1f4cc512012-07-21 00:10:39 -07003264 chip->ignore_shutdown_soc = pdata->ignore_shutdown_soc;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07003265 rc = set_battery_data(chip);
3266 if (rc) {
3267 pr_err("%s bad battery data %d\n", __func__, rc);
Abhijeet Dharmapurikar4a4456e2011-09-27 15:10:38 -07003268 goto free_chip;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07003269 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003270
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -08003271 if (chip->pc_temp_ocv_lut == NULL) {
3272 pr_err("temp ocv lut table is NULL\n");
3273 rc = -EINVAL;
3274 goto free_chip;
3275 }
3276
3277 /* set defaults in the battery data */
3278 if (chip->default_rbatt_mohm <= 0)
3279 chip->default_rbatt_mohm = DEFAULT_RBATT_MOHMS;
3280
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07003281 chip->batt_temp_channel = pdata->bms_cdata.batt_temp_channel;
3282 chip->vbat_channel = pdata->bms_cdata.vbat_channel;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07003283 chip->ref625mv_channel = pdata->bms_cdata.ref625mv_channel;
3284 chip->ref1p25v_channel = pdata->bms_cdata.ref1p25v_channel;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -07003285 chip->batt_id_channel = pdata->bms_cdata.batt_id_channel;
Abhijeet Dharmapurikarfa77f1a2011-06-27 21:11:51 -07003286 chip->revision = pm8xxx_get_revision(chip->dev->parent);
Abhijeet Dharmapurikare88f2462012-04-25 18:22:38 -07003287 chip->enable_fcc_learning = pdata->enable_fcc_learning;
choongryeol.lee76048da2012-09-11 14:19:21 -07003288 chip->last_reported_soc = -EINVAL;
3289 chip->eoc_check_soc = pdata->eoc_check_soc;
3290 chip->soc_adjusted = 0;
Abhijeet Dharmapurikar9c0e6042012-07-12 14:55:13 -07003291
3292 mutex_init(&chip->calib_mutex);
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07003293 INIT_WORK(&chip->calib_hkadc_work, calibrate_hkadc_work);
Abhijeet Dharmapurikar9c0e6042012-07-12 14:55:13 -07003294 INIT_DELAYED_WORK(&chip->calib_hkadc_delayed_work,
3295 calibrate_hkadc_delayed_work);
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -07003296
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07003297 INIT_DELAYED_WORK(&chip->calculate_soc_delayed_work,
3298 calculate_soc_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003299
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003300 rc = request_irqs(chip, pdev);
3301 if (rc) {
3302 pr_err("couldn't register interrupts rc = %d\n", rc);
3303 goto free_chip;
3304 }
3305
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07003306 rc = pm8921_bms_hw_init(chip);
3307 if (rc) {
3308 pr_err("couldn't init hardware rc = %d\n", rc);
3309 goto free_irqs;
3310 }
3311
Abhijeet Dharmapurikara7daba52012-07-27 20:40:51 -07003312 read_shutdown_soc_and_iavg(chip);
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07003313
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003314 platform_set_drvdata(pdev, chip);
3315 the_chip = chip;
3316 create_debugfs_entries(chip);
3317
Abhijeet Dharmapurikarfd8655c2012-03-18 16:41:58 -07003318 rc = read_ocv_trim(chip);
3319 if (rc) {
3320 pr_err("couldn't adjust ocv_trim rc= %d\n", rc);
3321 goto free_irqs;
3322 }
Abhijeet Dharmapurikarc3532222011-07-20 14:48:02 -07003323 check_initial_ocv(chip);
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07003324
Abhijeet Dharmapurikar9c0e6042012-07-12 14:55:13 -07003325 /* start periodic hkadc calibration */
3326 schedule_delayed_work(&chip->calib_hkadc_delayed_work, 0);
3327
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -07003328 /* enable the vbatt reading interrupts for scheduling hkadc calib */
3329 pm8921_bms_enable_irq(chip, PM8921_BMS_GOOD_OCV);
3330 pm8921_bms_enable_irq(chip, PM8921_BMS_OCV_FOR_R);
Abhijeet Dharmapurikar21ebadd2011-09-28 21:14:16 -07003331
Abhijeet Dharmapurikar41b46cd2012-07-18 19:38:43 -07003332 calculate_soc_work(&(chip->calculate_soc_delayed_work.work));
Abhijeet Dharmapurikar30f30f72012-04-26 17:34:18 -07003333
Abhijeet Dharmapurikarc23264c2011-10-28 19:34:56 -07003334 get_battery_uvolts(chip, &vbatt);
3335 pr_info("OK battery_capacity_at_boot=%d volt = %d ocv = %d\n",
3336 pm8921_bms_get_percent_charge(),
Abhijeet Dharmapurikar10bc0fd2012-04-18 21:03:50 -07003337 vbatt, chip->last_ocv_uv);
Abhijeet Dharmapurikar807249a2012-07-12 20:26:27 -07003338
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003339 return 0;
3340
Abhijeet Dharmapurikar1834c6d2011-10-28 20:00:02 -07003341free_irqs:
3342 free_irqs(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003343free_chip:
3344 kfree(chip);
3345 return rc;
3346}
3347
3348static int __devexit pm8921_bms_remove(struct platform_device *pdev)
3349{
3350 struct pm8921_bms_chip *chip = platform_get_drvdata(pdev);
3351
3352 free_irqs(chip);
Abhijeet Dharmapurikar05d42742011-10-18 19:26:03 -07003353 kfree(chip->adjusted_fcc_temp_lut);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003354 platform_set_drvdata(pdev, NULL);
3355 the_chip = NULL;
3356 kfree(chip);
3357 return 0;
3358}
3359
3360static struct platform_driver pm8921_bms_driver = {
3361 .probe = pm8921_bms_probe,
3362 .remove = __devexit_p(pm8921_bms_remove),
3363 .driver = {
3364 .name = PM8921_BMS_DEV_NAME,
3365 .owner = THIS_MODULE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003366 },
3367};
3368
3369static int __init pm8921_bms_init(void)
3370{
3371 return platform_driver_register(&pm8921_bms_driver);
3372}
3373
3374static void __exit pm8921_bms_exit(void)
3375{
3376 platform_driver_unregister(&pm8921_bms_driver);
3377}
3378
3379late_initcall(pm8921_bms_init);
3380module_exit(pm8921_bms_exit);
3381
3382MODULE_LICENSE("GPL v2");
3383MODULE_DESCRIPTION("PMIC8921 bms driver");
3384MODULE_VERSION("1.0");
3385MODULE_ALIAS("platform:" PM8921_BMS_DEV_NAME);