Flemmard | 6c7e733 | 2013-05-07 16:23:17 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | #define pr_fmt(fmt) "[BATT][BMS] " fmt |
| 14 | #define pr_fmt_debug(fmt) "[BATT][BMS]%s: " fmt, __func__ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/moduleparam.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/mfd/pm8xxx/pm8921-bms-htc.h> |
| 21 | #include <linux/mfd/pm8xxx/pm8921-charger-htc.h> |
| 22 | #include <linux/mfd/pm8xxx/core.h> |
| 23 | #include <linux/mfd/pm8xxx/pm8xxx-adc.h> |
| 24 | #include <linux/mfd/pm8xxx/ccadc.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/bitops.h> |
| 27 | #include <linux/debugfs.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/mutex.h> |
| 31 | #include <mach/board_htc.h> |
| 32 | |
| 33 | #ifdef CONFIG_HTC_BATT_8960 |
| 34 | #include "mach/htc_battery_cell.h" |
| 35 | #endif |
| 36 | |
| 37 | #if defined(pr_debug) |
| 38 | #undef pr_debug |
| 39 | #endif |
| 40 | #define pr_debug(fmt, ...) do { \ |
| 41 | if (flag_enable_bms_chg_log) \ |
| 42 | printk(KERN_INFO pr_fmt_debug(fmt), ##__VA_ARGS__); \ |
| 43 | } while (0) |
| 44 | static bool flag_enable_bms_chg_log; |
| 45 | |
| 46 | #define BMS_CONTROL 0x224 |
| 47 | #define BMS_S1_DELAY 0x225 |
| 48 | #define BMS_OUTPUT0 0x230 |
| 49 | #define BMS_OUTPUT1 0x231 |
| 50 | #define BMS_TOLERANCES 0x232 |
| 51 | #define BMS_TEST1 0x237 |
| 52 | |
| 53 | #define ADC_ARB_SECP_CNTRL 0x190 |
| 54 | #define ADC_ARB_SECP_AMUX_CNTRL 0x191 |
| 55 | #define ADC_ARB_SECP_ANA_PARAM 0x192 |
| 56 | #define ADC_ARB_SECP_DIG_PARAM 0x193 |
| 57 | #define ADC_ARB_SECP_RSV 0x194 |
| 58 | #define ADC_ARB_SECP_DATA1 0x195 |
| 59 | #define ADC_ARB_SECP_DATA0 0x196 |
| 60 | |
| 61 | #define ADC_ARB_BMS_CNTRL 0x18D |
| 62 | #define AMUX_TRIM_2 0x322 |
| 63 | #define TEST_PROGRAM_REV 0x339 |
| 64 | |
| 65 | #define OCV_UPDATE_STORAGE 0x105 |
| 66 | #define OCV_UPDATE_STORAGE_USE_MASK 0x0F |
| 67 | |
| 68 | enum pmic_bms_interrupts { |
| 69 | PM8921_BMS_SBI_WRITE_OK, |
| 70 | PM8921_BMS_CC_THR, |
| 71 | PM8921_BMS_VSENSE_THR, |
| 72 | PM8921_BMS_VSENSE_FOR_R, |
| 73 | PM8921_BMS_OCV_FOR_R, |
| 74 | PM8921_BMS_GOOD_OCV, |
| 75 | PM8921_BMS_VSENSE_AVG, |
| 76 | PM_BMS_MAX_INTS, |
| 77 | }; |
| 78 | |
| 79 | struct pm8921_soc_params { |
| 80 | uint16_t last_good_ocv_raw; |
| 81 | int cc; |
| 82 | |
| 83 | int last_good_ocv_uv; |
| 84 | }; |
| 85 | |
| 86 | struct pm8921_rbatt_params { |
| 87 | uint16_t ocv_for_rbatt_raw; |
| 88 | uint16_t vsense_for_rbatt_raw; |
| 89 | uint16_t vbatt_for_rbatt_raw; |
| 90 | |
| 91 | int ocv_for_rbatt_uv; |
| 92 | int vsense_for_rbatt_uv; |
| 93 | int vbatt_for_rbatt_uv; |
| 94 | }; |
| 95 | |
| 96 | struct pm8921_bms_chip { |
| 97 | struct device *dev; |
| 98 | struct dentry *dent; |
| 99 | unsigned int r_sense; |
| 100 | unsigned int i_test; |
| 101 | unsigned int v_failure; |
| 102 | unsigned int fcc; |
| 103 | struct single_row_lut *fcc_temp_lut; |
| 104 | struct single_row_lut *fcc_sf_lut; |
| 105 | struct pc_temp_ocv_lut *pc_temp_ocv_lut; |
| 106 | struct sf_lut *pc_sf_lut; |
| 107 | struct sf_lut *rbatt_sf_lut; |
| 108 | int delta_rbatt_mohm; |
| 109 | struct work_struct calib_hkadc_work; |
| 110 | unsigned int revision; |
| 111 | unsigned int xoadc_v0625_usb_present; |
| 112 | unsigned int xoadc_v0625_usb_absent; |
| 113 | unsigned int xoadc_v0625; |
| 114 | unsigned int xoadc_v125; |
| 115 | unsigned int batt_temp_channel; |
| 116 | unsigned int vbat_channel; |
| 117 | unsigned int ref625mv_channel; |
| 118 | unsigned int ref1p25v_channel; |
| 119 | unsigned int batt_id_channel; |
| 120 | unsigned int pmic_bms_irq[PM_BMS_MAX_INTS]; |
| 121 | DECLARE_BITMAP(enabled_irqs, PM_BMS_MAX_INTS); |
| 122 | struct mutex bms_output_lock; |
| 123 | spinlock_t bms_100_lock; |
| 124 | struct single_row_lut *adjusted_fcc_temp_lut; |
| 125 | unsigned int charging_began; |
| 126 | int start_percent; |
| 127 | int end_percent; |
| 128 | |
| 129 | uint16_t ocv_reading_at_100; |
| 130 | int cc_reading_at_100; |
| 131 | int max_voltage_uv; |
| 132 | int batt_temp_suspend; |
| 133 | int soc_rbatt_suspend; |
| 134 | int default_rbatt_mohm; |
| 135 | unsigned int rconn_mohm; |
| 136 | int amux_2_trim_delta; |
| 137 | uint16_t prev_last_good_ocv_raw; |
| 138 | int usb_chg_plugged_ready; |
| 139 | }; |
| 140 | |
| 141 | static struct pm8921_bms_chip *the_chip; |
| 142 | |
| 143 | struct pm8921_bms_debug { |
| 144 | int rbatt; |
| 145 | int rbatt_sf; |
| 146 | int voltage_unusable_uv; |
| 147 | int pc_unusable; |
| 148 | int rc_pc; |
| 149 | int scalefactor; |
| 150 | int batt_temp; |
| 151 | int soc_rbatt; |
| 152 | }; |
| 153 | static struct pm8921_bms_debug bms_dbg; |
| 154 | |
| 155 | #define DEFAULT_RBATT_MOHMS 128 |
| 156 | #define DEFAULT_OCV_MICROVOLTS 3900000 |
| 157 | #define DEFAULT_CHARGE_CYCLES 0 |
| 158 | |
| 159 | static int last_usb_cal_delta_uv = 1800; |
| 160 | module_param(last_usb_cal_delta_uv, int, 0644); |
| 161 | |
| 162 | static int last_chargecycles = DEFAULT_CHARGE_CYCLES; |
| 163 | static int last_charge_increase; |
| 164 | module_param(last_chargecycles, int, 0644); |
| 165 | module_param(last_charge_increase, int, 0644); |
| 166 | |
| 167 | static int last_rbatt = -EINVAL; |
| 168 | static int last_ocv_uv = -EINVAL; |
| 169 | static int last_soc = -EINVAL; |
| 170 | static int last_real_fcc_mah = -EINVAL; |
| 171 | static int last_real_fcc_batt_temp = -EINVAL; |
| 172 | |
| 173 | static int bms_ops_set(const char *val, const struct kernel_param *kp) |
| 174 | { |
| 175 | if (*(int *)kp->arg == -EINVAL) |
| 176 | return param_set_int(val, kp); |
| 177 | else |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static struct kernel_param_ops bms_param_ops = { |
| 182 | .set = bms_ops_set, |
| 183 | .get = param_get_int, |
| 184 | }; |
| 185 | |
| 186 | module_param_cb(last_rbatt, &bms_param_ops, &last_rbatt, 0644); |
| 187 | module_param_cb(last_ocv_uv, &bms_param_ops, &last_ocv_uv, 0644); |
| 188 | module_param_cb(last_soc, &bms_param_ops, &last_soc, 0644); |
| 189 | |
| 190 | static int bms_fake_battery = -EINVAL; |
| 191 | module_param(bms_fake_battery, int, 0644); |
| 192 | |
| 193 | static int bms_start_percent; |
| 194 | static int bms_start_ocv_uv; |
| 195 | static int bms_start_cc_uah; |
| 196 | static int bms_end_percent; |
| 197 | static int bms_end_ocv_uv; |
| 198 | static int bms_end_cc_uah; |
| 199 | static int bms_discharge_percent; |
| 200 | static int is_ocv_update_start; |
| 201 | |
| 202 | static int bms_ro_ops_set(const char *val, const struct kernel_param *kp) |
| 203 | { |
| 204 | return -EINVAL; |
| 205 | } |
| 206 | |
| 207 | static struct kernel_param_ops bms_ro_param_ops = { |
| 208 | .set = bms_ro_ops_set, |
| 209 | .get = param_get_int, |
| 210 | }; |
| 211 | module_param_cb(bms_start_percent, &bms_ro_param_ops, &bms_start_percent, 0644); |
| 212 | module_param_cb(bms_start_ocv_uv, &bms_ro_param_ops, &bms_start_ocv_uv, 0644); |
| 213 | module_param_cb(bms_start_cc_uah, &bms_ro_param_ops, &bms_start_cc_uah, 0644); |
| 214 | |
| 215 | module_param_cb(bms_end_percent, &bms_ro_param_ops, &bms_end_percent, 0644); |
| 216 | module_param_cb(bms_end_ocv_uv, &bms_ro_param_ops, &bms_end_ocv_uv, 0644); |
| 217 | module_param_cb(bms_end_cc_uah, &bms_ro_param_ops, &bms_end_cc_uah, 0644); |
| 218 | |
| 219 | static int dump_cc_uah(void); |
| 220 | |
| 221 | static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp); |
| 222 | static void readjust_fcc_table(void) |
| 223 | { |
| 224 | struct single_row_lut *temp, *old; |
| 225 | int i, fcc, ratio; |
| 226 | |
| 227 | if (!the_chip->fcc_temp_lut) { |
| 228 | pr_err("The static fcc lut table is NULL\n"); |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | temp = kzalloc(sizeof(struct single_row_lut), GFP_KERNEL); |
| 233 | if (!temp) { |
| 234 | pr_err("Cannot allocate memory for adjusted fcc table\n"); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | fcc = interpolate_fcc(the_chip, last_real_fcc_batt_temp); |
| 239 | |
| 240 | temp->cols = the_chip->fcc_temp_lut->cols; |
| 241 | for (i = 0; i < the_chip->fcc_temp_lut->cols; i++) { |
| 242 | temp->x[i] = the_chip->fcc_temp_lut->x[i]; |
| 243 | ratio = div_u64(the_chip->fcc_temp_lut->y[i] * 1000, fcc); |
| 244 | temp->y[i] = (ratio * last_real_fcc_mah); |
| 245 | temp->y[i] /= 1000; |
| 246 | pr_debug("temp=%d, staticfcc=%d, adjfcc=%d, ratio=%d\n", |
| 247 | temp->x[i], the_chip->fcc_temp_lut->y[i], |
| 248 | temp->y[i], ratio); |
| 249 | } |
| 250 | |
| 251 | old = the_chip->adjusted_fcc_temp_lut; |
| 252 | the_chip->adjusted_fcc_temp_lut = temp; |
| 253 | kfree(old); |
| 254 | } |
| 255 | |
| 256 | static int bms_last_real_fcc_set(const char *val, |
| 257 | const struct kernel_param *kp) |
| 258 | { |
| 259 | int rc = 0; |
| 260 | |
| 261 | if (last_real_fcc_mah == -EINVAL) |
| 262 | rc = param_set_int(val, kp); |
| 263 | if (rc) { |
| 264 | pr_err("Failed to set last_real_fcc_mah rc=%d\n", rc); |
| 265 | return rc; |
| 266 | } |
| 267 | if (last_real_fcc_batt_temp != -EINVAL) |
| 268 | readjust_fcc_table(); |
| 269 | return rc; |
| 270 | } |
| 271 | static struct kernel_param_ops bms_last_real_fcc_param_ops = { |
| 272 | .set = bms_last_real_fcc_set, |
| 273 | .get = param_get_int, |
| 274 | }; |
| 275 | module_param_cb(last_real_fcc_mah, &bms_last_real_fcc_param_ops, |
| 276 | &last_real_fcc_mah, 0644); |
| 277 | |
| 278 | static int bms_last_real_fcc_batt_temp_set(const char *val, |
| 279 | const struct kernel_param *kp) |
| 280 | { |
| 281 | int rc = 0; |
| 282 | |
| 283 | if (last_real_fcc_batt_temp == -EINVAL) |
| 284 | rc = param_set_int(val, kp); |
| 285 | if (rc) { |
| 286 | pr_err("Failed to set last_real_fcc_batt_temp rc=%d\n", rc); |
| 287 | return rc; |
| 288 | } |
| 289 | if (last_real_fcc_mah != -EINVAL) |
| 290 | readjust_fcc_table(); |
| 291 | return rc; |
| 292 | } |
| 293 | |
| 294 | static struct kernel_param_ops bms_last_real_fcc_batt_temp_param_ops = { |
| 295 | .set = bms_last_real_fcc_batt_temp_set, |
| 296 | .get = param_get_int, |
| 297 | }; |
| 298 | module_param_cb(last_real_fcc_batt_temp, &bms_last_real_fcc_batt_temp_param_ops, |
| 299 | &last_real_fcc_batt_temp, 0644); |
| 300 | |
| 301 | static int pm_bms_get_rt_status(struct pm8921_bms_chip *chip, int irq_id) |
| 302 | { |
| 303 | return pm8xxx_read_irq_stat(chip->dev->parent, |
| 304 | chip->pmic_bms_irq[irq_id]); |
| 305 | } |
| 306 | |
| 307 | static void pm8921_bms_enable_irq(struct pm8921_bms_chip *chip, int interrupt) |
| 308 | { |
| 309 | if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) { |
| 310 | dev_dbg(chip->dev, "%s %d\n", __func__, |
| 311 | chip->pmic_bms_irq[interrupt]); |
| 312 | enable_irq(chip->pmic_bms_irq[interrupt]); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | static void pm8921_bms_disable_irq(struct pm8921_bms_chip *chip, int interrupt) |
| 317 | { |
| 318 | if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) { |
| 319 | pr_debug("%d\n", chip->pmic_bms_irq[interrupt]); |
| 320 | disable_irq_nosync(chip->pmic_bms_irq[interrupt]); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | static int pm_bms_masked_write(struct pm8921_bms_chip *chip, u16 addr, |
| 325 | u8 mask, u8 val) |
| 326 | { |
| 327 | int rc; |
| 328 | u8 reg; |
| 329 | |
| 330 | rc = pm8xxx_readb(chip->dev->parent, addr, ®); |
| 331 | if (rc) { |
| 332 | pr_err("read failed addr = %03X, rc = %d\n", addr, rc); |
| 333 | return rc; |
| 334 | } |
| 335 | reg &= ~mask; |
| 336 | reg |= val & mask; |
| 337 | rc = pm8xxx_writeb(chip->dev->parent, addr, reg); |
| 338 | if (rc) { |
| 339 | pr_err("write failed addr = %03X, rc = %d\n", addr, rc); |
| 340 | return rc; |
| 341 | } |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static int usb_chg_plugged_in(void) |
| 346 | { |
| 347 | #if 0 |
| 348 | union power_supply_propval ret = {0,}; |
| 349 | static struct power_supply *psy; |
| 350 | if (psy == NULL) { |
| 351 | psy = power_supply_get_by_name("usb"); |
| 352 | if (psy == NULL) |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret)) |
| 357 | return 0; |
| 358 | return ret.intval; |
| 359 | #endif |
| 360 | int rc = pm8921_is_usb_chg_plugged_in(); |
| 361 | if (rc < 0) { |
| 362 | return 0; |
| 363 | } |
| 364 | the_chip->usb_chg_plugged_ready = 1; |
| 365 | return rc; |
| 366 | } |
| 367 | |
| 368 | #define HOLD_OREG_DATA BIT(1) |
| 369 | static int pm_bms_lock_output_data(struct pm8921_bms_chip *chip) |
| 370 | { |
| 371 | int rc; |
| 372 | |
| 373 | rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA, |
| 374 | HOLD_OREG_DATA); |
| 375 | if (rc) { |
| 376 | pr_err("couldnt lock bms output rc = %d\n", rc); |
| 377 | return rc; |
| 378 | } |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | static int pm_bms_unlock_output_data(struct pm8921_bms_chip *chip) |
| 383 | { |
| 384 | int rc; |
| 385 | |
| 386 | rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA, 0); |
| 387 | if (rc) { |
| 388 | pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc); |
| 389 | return rc; |
| 390 | } |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | #define SELECT_OUTPUT_DATA 0x1C |
| 395 | #define SELECT_OUTPUT_TYPE_SHIFT 2 |
| 396 | #define OCV_FOR_RBATT 0x0 |
| 397 | #define VSENSE_FOR_RBATT 0x1 |
| 398 | #define VBATT_FOR_RBATT 0x2 |
| 399 | #define CC_MSB 0x3 |
| 400 | #define CC_LSB 0x4 |
| 401 | #define LAST_GOOD_OCV_VALUE 0x5 |
| 402 | #define VSENSE_AVG 0x6 |
| 403 | #define VBATT_AVG 0x7 |
| 404 | |
| 405 | static int pm_bms_read_output_data(struct pm8921_bms_chip *chip, int type, |
| 406 | int16_t *result) |
| 407 | { |
| 408 | int rc; |
| 409 | u8 reg; |
| 410 | |
| 411 | if (!result) { |
| 412 | pr_err("result pointer null\n"); |
| 413 | return -EINVAL; |
| 414 | } |
| 415 | *result = 0; |
| 416 | if (type < OCV_FOR_RBATT || type > VBATT_AVG) { |
| 417 | pr_err("invalid type %d asked to read\n", type); |
| 418 | return -EINVAL; |
| 419 | } |
| 420 | |
| 421 | rc = pm_bms_masked_write(chip, BMS_CONTROL, SELECT_OUTPUT_DATA, |
| 422 | type << SELECT_OUTPUT_TYPE_SHIFT); |
| 423 | if (rc) { |
| 424 | pr_err("fail to select %d type in BMS_CONTROL rc = %d\n", |
| 425 | type, rc); |
| 426 | return rc; |
| 427 | } |
| 428 | |
| 429 | rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT0, ®); |
| 430 | if (rc) { |
| 431 | pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n", |
| 432 | type, rc); |
| 433 | return rc; |
| 434 | } |
| 435 | *result = reg; |
| 436 | rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT1, ®); |
| 437 | if (rc) { |
| 438 | pr_err("fail to read BMS_OUTPUT1 for type %d rc = %d\n", |
| 439 | type, rc); |
| 440 | return rc; |
| 441 | } |
| 442 | *result |= reg << 8; |
| 443 | pr_debug("type %d result %x", type, *result); |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | #define V_PER_BIT_MUL_FACTOR 97656 |
| 448 | #define V_PER_BIT_DIV_FACTOR 1000 |
| 449 | #define XOADC_INTRINSIC_OFFSET 0x6000 |
| 450 | static int xoadc_reading_to_microvolt(unsigned int a) |
| 451 | { |
| 452 | if (a <= XOADC_INTRINSIC_OFFSET) |
| 453 | return 0; |
| 454 | |
| 455 | return (a - XOADC_INTRINSIC_OFFSET) |
| 456 | * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR; |
| 457 | } |
| 458 | |
| 459 | #define XOADC_CALIB_UV 625000 |
| 460 | #define VBATT_MUL_FACTOR 3 |
| 461 | static int adjust_xo_vbatt_reading(struct pm8921_bms_chip *chip, |
| 462 | int usb_chg, unsigned int uv) |
| 463 | { |
| 464 | s64 numerator, denominator; |
| 465 | int local_delta; |
| 466 | |
| 467 | if (uv == 0) |
| 468 | return 0; |
| 469 | |
| 470 | |
| 471 | if (chip->xoadc_v0625 == 0 || chip->xoadc_v125 == 0) { |
| 472 | pr_debug("No cal yet return %d\n", VBATT_MUL_FACTOR * uv); |
| 473 | return VBATT_MUL_FACTOR * uv; |
| 474 | } |
| 475 | |
| 476 | if (usb_chg) |
| 477 | local_delta = last_usb_cal_delta_uv; |
| 478 | else |
| 479 | local_delta = 0; |
| 480 | |
| 481 | pr_debug("using delta = %d\n", local_delta); |
| 482 | numerator = ((s64)uv - chip->xoadc_v0625 - local_delta) |
| 483 | * XOADC_CALIB_UV; |
| 484 | denominator = (s64)chip->xoadc_v125 - chip->xoadc_v0625 - local_delta; |
| 485 | if (denominator == 0) |
| 486 | return uv * VBATT_MUL_FACTOR; |
| 487 | return (XOADC_CALIB_UV + local_delta + div_s64(numerator, denominator)) |
| 488 | * VBATT_MUL_FACTOR; |
| 489 | } |
| 490 | |
| 491 | #define CC_RESOLUTION_N_V1 1085069 |
| 492 | #define CC_RESOLUTION_D_V1 100000 |
| 493 | #define CC_RESOLUTION_N_V2 868056 |
| 494 | #define CC_RESOLUTION_D_V2 10000 |
| 495 | |
| 496 | static s64 cc_to_microvolt_v1(s64 cc) |
| 497 | { |
| 498 | return div_s64(cc * CC_RESOLUTION_N_V1, CC_RESOLUTION_D_V1); |
| 499 | } |
| 500 | |
| 501 | static s64 cc_to_microvolt_v2(s64 cc) |
| 502 | { |
| 503 | return div_s64(cc * CC_RESOLUTION_N_V2, CC_RESOLUTION_D_V2); |
| 504 | } |
| 505 | |
| 506 | static s64 cc_to_microvolt(struct pm8921_bms_chip *chip, s64 cc) |
| 507 | { |
| 508 | return (chip->revision < PM8XXX_REVISION_8921_2p0) ? |
| 509 | cc_to_microvolt_v1((s64)cc) : |
| 510 | cc_to_microvolt_v2((s64)cc); |
| 511 | } |
| 512 | |
| 513 | #define CC_READING_TICKS 56 |
| 514 | #define SLEEP_CLK_HZ 32764 |
| 515 | #define SECONDS_PER_HOUR 3600 |
| 516 | static s64 ccmicrovolt_to_nvh(s64 cc_uv) |
| 517 | { |
| 518 | return div_s64(cc_uv * CC_READING_TICKS * 1000, |
| 519 | SLEEP_CLK_HZ * SECONDS_PER_HOUR); |
| 520 | } |
| 521 | |
| 522 | static int read_cc(struct pm8921_bms_chip *chip, int *result) |
| 523 | { |
| 524 | int rc; |
| 525 | uint16_t msw, lsw; |
| 526 | |
| 527 | rc = pm_bms_read_output_data(chip, CC_LSB, &lsw); |
| 528 | if (rc) { |
| 529 | pr_err("fail to read CC_LSB rc = %d\n", rc); |
| 530 | return rc; |
| 531 | } |
| 532 | rc = pm_bms_read_output_data(chip, CC_MSB, &msw); |
| 533 | if (rc) { |
| 534 | pr_err("fail to read CC_MSB rc = %d\n", rc); |
| 535 | return rc; |
| 536 | } |
| 537 | *result = msw << 16 | lsw; |
| 538 | pr_debug("msw = %04x lsw = %04x cc = %d\n", msw, lsw, *result); |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | static int adjust_xo_vbatt_reading_for_mbg(struct pm8921_bms_chip *chip, |
| 543 | int result) |
| 544 | { |
| 545 | int64_t numerator; |
| 546 | int64_t denominator; |
| 547 | |
| 548 | if (chip->amux_2_trim_delta == 0) |
| 549 | return result; |
| 550 | |
| 551 | numerator = (s64)result * 1000000; |
| 552 | denominator = (1000000 + (410 * (s64)chip->amux_2_trim_delta)); |
| 553 | return div_s64(numerator, denominator); |
| 554 | } |
| 555 | |
| 556 | static int convert_vbatt_raw_to_uv(struct pm8921_bms_chip *chip, |
| 557 | int usb_chg, |
| 558 | uint16_t reading, int *result) |
| 559 | { |
| 560 | *result = xoadc_reading_to_microvolt(reading); |
| 561 | pr_debug("raw = %04x vbatt = %u\n", reading, *result); |
| 562 | *result = adjust_xo_vbatt_reading(chip, usb_chg, *result); |
| 563 | pr_debug("after adj vbatt = %u\n", *result); |
| 564 | *result = adjust_xo_vbatt_reading_for_mbg(chip, *result); |
| 565 | pr_debug("after mbg adj vbatt = %u\n", *result); |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | static int convert_vsense_to_uv(struct pm8921_bms_chip *chip, |
| 570 | int16_t reading, int *result) |
| 571 | { |
| 572 | *result = pm8xxx_ccadc_reading_to_microvolt(chip->revision, reading); |
| 573 | pr_debug("raw = %04x vsense = %d\n", reading, *result); |
| 574 | *result = pm8xxx_cc_adjust_for_gain(*result); |
| 575 | pr_debug("after adj vsense = %d\n", *result); |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | static int read_vsense_avg(struct pm8921_bms_chip *chip, int *result) |
| 580 | { |
| 581 | int rc; |
| 582 | int16_t reading; |
| 583 | |
| 584 | rc = pm_bms_read_output_data(chip, VSENSE_AVG, &reading); |
| 585 | if (rc) { |
| 586 | pr_err("fail to read VSENSE_AVG rc = %d\n", rc); |
| 587 | return rc; |
| 588 | } |
| 589 | |
| 590 | convert_vsense_to_uv(chip, reading, result); |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | static int linear_interpolate(int y0, int x0, int y1, int x1, int x) |
| 595 | { |
| 596 | if (y0 == y1 || x == x0) |
| 597 | return y0; |
| 598 | if (x1 == x0 || x == x1) |
| 599 | return y1; |
| 600 | |
| 601 | return y0 + ((y1 - y0) * (x - x0) / (x1 - x0)); |
| 602 | } |
| 603 | |
| 604 | static int interpolate_single_lut(struct single_row_lut *lut, int x) |
| 605 | { |
| 606 | int i, result; |
| 607 | |
| 608 | if (x < lut->x[0]) { |
| 609 | pr_debug("x %d less than known range return y = %d lut = %pS\n", |
| 610 | x, lut->y[0], lut); |
| 611 | return lut->y[0]; |
| 612 | } |
| 613 | if (x > lut->x[lut->cols - 1]) { |
| 614 | pr_debug("x %d more than known range return y = %d lut = %pS\n", |
| 615 | x, lut->y[lut->cols - 1], lut); |
| 616 | return lut->y[lut->cols - 1]; |
| 617 | } |
| 618 | |
| 619 | for (i = 0; i < lut->cols; i++) |
| 620 | if (x <= lut->x[i]) |
| 621 | break; |
| 622 | if (x == lut->x[i]) { |
| 623 | result = lut->y[i]; |
| 624 | } else { |
| 625 | result = linear_interpolate( |
| 626 | lut->y[i - 1], |
| 627 | lut->x[i - 1], |
| 628 | lut->y[i], |
| 629 | lut->x[i], |
| 630 | x); |
| 631 | } |
| 632 | return result; |
| 633 | } |
| 634 | |
| 635 | static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp) |
| 636 | { |
| 637 | |
| 638 | batt_temp = batt_temp/10; |
| 639 | return interpolate_single_lut(chip->fcc_temp_lut, batt_temp); |
| 640 | } |
| 641 | |
| 642 | static int interpolate_fcc_adjusted(struct pm8921_bms_chip *chip, int batt_temp) |
| 643 | { |
| 644 | |
| 645 | batt_temp = batt_temp/10; |
| 646 | return interpolate_single_lut(chip->adjusted_fcc_temp_lut, batt_temp); |
| 647 | } |
| 648 | |
| 649 | static int interpolate_scalingfactor_fcc(struct pm8921_bms_chip *chip, |
| 650 | int cycles) |
| 651 | { |
| 652 | if (chip->fcc_sf_lut) |
| 653 | return interpolate_single_lut(chip->fcc_sf_lut, cycles); |
| 654 | else |
| 655 | return 100; |
| 656 | } |
| 657 | |
| 658 | static int interpolate_scalingfactor(struct pm8921_bms_chip *chip, |
| 659 | struct sf_lut *sf_lut, |
| 660 | int row_entry, int pc) |
| 661 | { |
| 662 | int i, scalefactorrow1, scalefactorrow2, scalefactor; |
| 663 | int rows, cols; |
| 664 | int row1 = 0; |
| 665 | int row2 = 0; |
| 666 | |
| 667 | if (!sf_lut) |
| 668 | return 100; |
| 669 | |
| 670 | rows = sf_lut->rows; |
| 671 | cols = sf_lut->cols; |
| 672 | if (pc > sf_lut->percent[0]) { |
| 673 | pr_debug("pc %d greater than known pc ranges for sfd\n", pc); |
| 674 | row1 = 0; |
| 675 | row2 = 0; |
| 676 | } |
| 677 | if (pc < sf_lut->percent[rows - 1]) { |
| 678 | pr_debug("pc %d less than known pc ranges for sf", pc); |
| 679 | row1 = rows - 1; |
| 680 | row2 = rows - 1; |
| 681 | } |
| 682 | for (i = 0; i < rows; i++) { |
| 683 | if (pc == sf_lut->percent[i]) { |
| 684 | row1 = i; |
| 685 | row2 = i; |
| 686 | break; |
| 687 | } |
| 688 | if (pc > sf_lut->percent[i]) { |
| 689 | row1 = i - 1; |
| 690 | row2 = i; |
| 691 | break; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | if (row_entry < sf_lut->row_entries[0]) |
| 696 | row_entry = sf_lut->row_entries[0]; |
| 697 | if (row_entry > sf_lut->row_entries[cols - 1]) |
| 698 | row_entry = sf_lut->row_entries[cols - 1]; |
| 699 | |
| 700 | for (i = 0; i < cols; i++) |
| 701 | if (row_entry <= sf_lut->row_entries[i]) |
| 702 | break; |
| 703 | if (row_entry == sf_lut->row_entries[i]) { |
| 704 | scalefactor = linear_interpolate( |
| 705 | sf_lut->sf[row1][i], |
| 706 | sf_lut->percent[row1], |
| 707 | sf_lut->sf[row2][i], |
| 708 | sf_lut->percent[row2], |
| 709 | pc); |
| 710 | return scalefactor; |
| 711 | } |
| 712 | |
| 713 | scalefactorrow1 = linear_interpolate( |
| 714 | sf_lut->sf[row1][i - 1], |
| 715 | sf_lut->row_entries[i - 1], |
| 716 | sf_lut->sf[row1][i], |
| 717 | sf_lut->row_entries[i], |
| 718 | row_entry); |
| 719 | |
| 720 | scalefactorrow2 = linear_interpolate( |
| 721 | sf_lut->sf[row2][i - 1], |
| 722 | sf_lut->row_entries[i - 1], |
| 723 | sf_lut->sf[row2][i], |
| 724 | sf_lut->row_entries[i], |
| 725 | row_entry); |
| 726 | |
| 727 | scalefactor = linear_interpolate( |
| 728 | scalefactorrow1, |
| 729 | sf_lut->percent[row1], |
| 730 | scalefactorrow2, |
| 731 | sf_lut->percent[row2], |
| 732 | pc); |
| 733 | |
| 734 | return scalefactor; |
| 735 | } |
| 736 | |
| 737 | static int is_between(int left, int right, int value) |
| 738 | { |
| 739 | if (left >= right && left >= value && value >= right) |
| 740 | return 1; |
| 741 | if (left <= right && left <= value && value <= right) |
| 742 | return 1; |
| 743 | |
| 744 | return 0; |
| 745 | } |
| 746 | |
| 747 | static int interpolate_pc(struct pm8921_bms_chip *chip, |
| 748 | int batt_temp, int ocv) |
| 749 | { |
| 750 | int i, j, pcj, pcj_minus_one, pc; |
| 751 | int rows = chip->pc_temp_ocv_lut->rows; |
| 752 | int cols = chip->pc_temp_ocv_lut->cols; |
| 753 | |
| 754 | |
| 755 | batt_temp = batt_temp/10; |
| 756 | |
| 757 | if (batt_temp < chip->pc_temp_ocv_lut->temp[0]) { |
| 758 | pr_debug("batt_temp %d < known temp range for pc\n", batt_temp); |
| 759 | batt_temp = chip->pc_temp_ocv_lut->temp[0]; |
| 760 | } |
| 761 | if (batt_temp > chip->pc_temp_ocv_lut->temp[cols - 1]) { |
| 762 | pr_debug("batt_temp %d > known temp range for pc\n", batt_temp); |
| 763 | batt_temp = chip->pc_temp_ocv_lut->temp[cols - 1]; |
| 764 | } |
| 765 | |
| 766 | for (j = 0; j < cols; j++) |
| 767 | if (batt_temp <= chip->pc_temp_ocv_lut->temp[j]) |
| 768 | break; |
| 769 | if (batt_temp == chip->pc_temp_ocv_lut->temp[j]) { |
| 770 | |
| 771 | if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j]) |
| 772 | return chip->pc_temp_ocv_lut->percent[0]; |
| 773 | if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j]) |
| 774 | return chip->pc_temp_ocv_lut->percent[rows - 1]; |
| 775 | for (i = 0; i < rows; i++) { |
| 776 | if (ocv >= chip->pc_temp_ocv_lut->ocv[i][j]) { |
| 777 | if (ocv == chip->pc_temp_ocv_lut->ocv[i][j]) |
| 778 | return |
| 779 | chip->pc_temp_ocv_lut->percent[i]; |
| 780 | pc = linear_interpolate( |
| 781 | chip->pc_temp_ocv_lut->percent[i], |
| 782 | chip->pc_temp_ocv_lut->ocv[i][j], |
| 783 | chip->pc_temp_ocv_lut->percent[i - 1], |
| 784 | chip->pc_temp_ocv_lut->ocv[i - 1][j], |
| 785 | ocv); |
| 786 | return pc; |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j]) |
| 792 | return chip->pc_temp_ocv_lut->percent[0]; |
| 793 | if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j - 1]) |
| 794 | return chip->pc_temp_ocv_lut->percent[rows - 1]; |
| 795 | |
| 796 | pcj_minus_one = 0; |
| 797 | pcj = 0; |
| 798 | for (i = 0; i < rows-1; i++) { |
| 799 | if (pcj == 0 |
| 800 | && is_between(chip->pc_temp_ocv_lut->ocv[i][j], |
| 801 | chip->pc_temp_ocv_lut->ocv[i+1][j], ocv)) { |
| 802 | pcj = linear_interpolate( |
| 803 | chip->pc_temp_ocv_lut->percent[i], |
| 804 | chip->pc_temp_ocv_lut->ocv[i][j], |
| 805 | chip->pc_temp_ocv_lut->percent[i + 1], |
| 806 | chip->pc_temp_ocv_lut->ocv[i+1][j], |
| 807 | ocv); |
| 808 | } |
| 809 | |
| 810 | if (pcj_minus_one == 0 |
| 811 | && is_between(chip->pc_temp_ocv_lut->ocv[i][j-1], |
| 812 | chip->pc_temp_ocv_lut->ocv[i+1][j-1], ocv)) { |
| 813 | |
| 814 | pcj_minus_one = linear_interpolate( |
| 815 | chip->pc_temp_ocv_lut->percent[i], |
| 816 | chip->pc_temp_ocv_lut->ocv[i][j-1], |
| 817 | chip->pc_temp_ocv_lut->percent[i + 1], |
| 818 | chip->pc_temp_ocv_lut->ocv[i+1][j-1], |
| 819 | ocv); |
| 820 | } |
| 821 | |
| 822 | if (pcj && pcj_minus_one) { |
| 823 | pc = linear_interpolate( |
| 824 | pcj_minus_one, |
| 825 | chip->pc_temp_ocv_lut->temp[j-1], |
| 826 | pcj, |
| 827 | chip->pc_temp_ocv_lut->temp[j], |
| 828 | batt_temp); |
| 829 | return pc; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | if (pcj) |
| 834 | return pcj; |
| 835 | |
| 836 | if (pcj_minus_one) |
| 837 | return pcj_minus_one; |
| 838 | |
| 839 | pr_debug("%d ocv wasn't found for temp %d in the LUT returning 100%%", |
| 840 | ocv, batt_temp); |
| 841 | return 100; |
| 842 | } |
| 843 | |
| 844 | #define BMS_MODE_BIT BIT(6) |
| 845 | #define EN_VBAT_BIT BIT(5) |
| 846 | #define OVERRIDE_MODE_DELAY_MS 20 |
| 847 | int pm8921_bms_get_simultaneous_battery_voltage_and_current(int *ibat_ua, |
| 848 | int *vbat_uv) |
| 849 | { |
| 850 | int16_t vsense_raw; |
| 851 | int16_t vbat_raw; |
| 852 | int vsense_uv, usb_chg; |
| 853 | |
| 854 | if (the_chip == NULL) { |
| 855 | pr_err("Called to early\n"); |
| 856 | return -EINVAL; |
| 857 | } |
| 858 | |
| 859 | mutex_lock(&the_chip->bms_output_lock); |
| 860 | |
| 861 | pm8xxx_writeb(the_chip->dev->parent, BMS_S1_DELAY, 0x00); |
| 862 | pm_bms_masked_write(the_chip, BMS_CONTROL, |
| 863 | BMS_MODE_BIT | EN_VBAT_BIT, BMS_MODE_BIT | EN_VBAT_BIT); |
| 864 | |
| 865 | msleep(OVERRIDE_MODE_DELAY_MS); |
| 866 | |
| 867 | pm_bms_lock_output_data(the_chip); |
| 868 | pm_bms_read_output_data(the_chip, VSENSE_AVG, &vsense_raw); |
| 869 | pm_bms_read_output_data(the_chip, VBATT_AVG, &vbat_raw); |
| 870 | pm_bms_unlock_output_data(the_chip); |
| 871 | pm_bms_masked_write(the_chip, BMS_CONTROL, |
| 872 | BMS_MODE_BIT | EN_VBAT_BIT, 0); |
| 873 | |
| 874 | pm8xxx_writeb(the_chip->dev->parent, BMS_S1_DELAY, 0x0B); |
| 875 | |
| 876 | mutex_unlock(&the_chip->bms_output_lock); |
| 877 | |
| 878 | usb_chg = usb_chg_plugged_in(); |
| 879 | convert_vbatt_raw_to_uv(the_chip, usb_chg, vbat_raw, vbat_uv); |
| 880 | convert_vsense_to_uv(the_chip, vsense_raw, &vsense_uv); |
| 881 | *ibat_ua = vsense_uv * 1000 / (int)the_chip->r_sense; |
| 882 | |
| 883 | pr_debug("vsense_raw = 0x%x vbat_raw = 0x%x" |
| 884 | " ibat_ua = %d vbat_uv = %d\n", |
| 885 | (uint16_t)vsense_raw, (uint16_t)vbat_raw, |
| 886 | *ibat_ua, *vbat_uv); |
| 887 | return 0; |
| 888 | } |
| 889 | EXPORT_SYMBOL(pm8921_bms_get_simultaneous_battery_voltage_and_current); |
| 890 | |
| 891 | static int read_rbatt_params_raw(struct pm8921_bms_chip *chip, |
| 892 | struct pm8921_rbatt_params *raw) |
| 893 | { |
| 894 | int usb_chg; |
| 895 | |
| 896 | mutex_lock(&chip->bms_output_lock); |
| 897 | pm_bms_lock_output_data(chip); |
| 898 | |
| 899 | pm_bms_read_output_data(chip, |
| 900 | OCV_FOR_RBATT, &raw->ocv_for_rbatt_raw); |
| 901 | pm_bms_read_output_data(chip, |
| 902 | VBATT_FOR_RBATT, &raw->vbatt_for_rbatt_raw); |
| 903 | pm_bms_read_output_data(chip, |
| 904 | VSENSE_FOR_RBATT, &raw->vsense_for_rbatt_raw); |
| 905 | |
| 906 | pm_bms_unlock_output_data(chip); |
| 907 | mutex_unlock(&chip->bms_output_lock); |
| 908 | |
| 909 | usb_chg = usb_chg_plugged_in(); |
| 910 | convert_vbatt_raw_to_uv(chip, usb_chg, |
| 911 | raw->vbatt_for_rbatt_raw, &raw->vbatt_for_rbatt_uv); |
| 912 | convert_vbatt_raw_to_uv(chip, usb_chg, |
| 913 | raw->ocv_for_rbatt_raw, &raw->ocv_for_rbatt_uv); |
| 914 | convert_vsense_to_uv(chip, |
| 915 | raw->vsense_for_rbatt_raw, &raw->vsense_for_rbatt_uv); |
| 916 | |
| 917 | pr_debug("vbatt_for_rbatt_raw = 0x%x, vbatt_for_rbatt= %duV\n", |
| 918 | raw->vbatt_for_rbatt_raw, raw->vbatt_for_rbatt_uv); |
| 919 | pr_debug("ocv_for_rbatt_raw = 0x%x, ocv_for_rbatt= %duV\n", |
| 920 | raw->ocv_for_rbatt_raw, raw->ocv_for_rbatt_uv); |
| 921 | pr_debug("vsense_for_rbatt_raw = 0x%x, vsense_for_rbatt= %duV\n", |
| 922 | raw->vsense_for_rbatt_raw, raw->vsense_for_rbatt_uv); |
| 923 | return 0; |
| 924 | } |
| 925 | |
| 926 | #define MBG_TRANSIENT_ERROR_RAW 51 |
| 927 | static void adjust_pon_ocv_raw(struct pm8921_bms_chip *chip, |
| 928 | struct pm8921_soc_params *raw) |
| 929 | { |
| 930 | if (raw->last_good_ocv_raw >= MBG_TRANSIENT_ERROR_RAW) |
| 931 | raw->last_good_ocv_raw -= MBG_TRANSIENT_ERROR_RAW; |
| 932 | } |
| 933 | |
| 934 | static int read_soc_params_raw(struct pm8921_bms_chip *chip, |
| 935 | struct pm8921_soc_params *raw) |
| 936 | { |
| 937 | int usb_chg, rc; |
| 938 | uint16_t last_good_ocv_raw_ori = 0; |
| 939 | int last_good_ocv_uv_ori_uv = 0; |
| 940 | u8 ocv_updated_flag = 0; |
| 941 | |
| 942 | rc = pm8xxx_readb(chip->dev->parent, OCV_UPDATE_STORAGE, &ocv_updated_flag); |
| 943 | if (rc) { |
| 944 | pr_err("%s: failed to read addr = %d, rc=%d\n", |
| 945 | __func__, OCV_UPDATE_STORAGE, rc); |
| 946 | } else { |
| 947 | ocv_updated_flag &= OCV_UPDATE_STORAGE_USE_MASK; |
| 948 | pr_debug("%s: OCV_UPDATE_STORAGE = 0x%x\n", __func__, ocv_updated_flag); |
| 949 | } |
| 950 | |
| 951 | mutex_lock(&chip->bms_output_lock); |
| 952 | pm_bms_lock_output_data(chip); |
| 953 | |
| 954 | pm_bms_read_output_data(chip, |
| 955 | LAST_GOOD_OCV_VALUE, &raw->last_good_ocv_raw); |
| 956 | read_cc(chip, &raw->cc); |
| 957 | |
| 958 | pm_bms_unlock_output_data(chip); |
| 959 | mutex_unlock(&chip->bms_output_lock); |
| 960 | |
| 961 | usb_chg = usb_chg_plugged_in(); |
| 962 | pr_debug("%s: usb_chg=%d, usb_chg_plugged_ready=%d," |
| 963 | "prev_last_good_ocv_raw=0x%x, last_good_ocv_raw=0x%x\n", |
| 964 | __func__, usb_chg, chip->usb_chg_plugged_ready, |
| 965 | chip->prev_last_good_ocv_raw, raw->last_good_ocv_raw); |
| 966 | if (chip->prev_last_good_ocv_raw == 0) { |
| 967 | if (chip->usb_chg_plugged_ready == 1) |
| 968 | chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw; |
| 969 | last_good_ocv_raw_ori = raw->last_good_ocv_raw; |
| 970 | if (!ocv_updated_flag) |
| 971 | adjust_pon_ocv_raw(chip, raw); |
| 972 | else |
| 973 | pr_info("%s: Skip adjust_pon_ocv_raw due to ocv_updated_flag=0x%x\n", |
| 974 | __func__, ocv_updated_flag); |
| 975 | convert_vbatt_raw_to_uv(chip, usb_chg, |
| 976 | raw->last_good_ocv_raw, &raw->last_good_ocv_uv); |
| 977 | convert_vbatt_raw_to_uv(chip, usb_chg, |
| 978 | last_good_ocv_raw_ori, &last_good_ocv_uv_ori_uv); |
| 979 | last_ocv_uv = raw->last_good_ocv_uv; |
| 980 | pr_info("%s: last_good_ocv_raw/ori=0x%x/0x%x, last_good_ocv_uv/ori=%duV/%duV\n", |
| 981 | __func__, raw->last_good_ocv_raw, last_good_ocv_raw_ori, |
| 982 | raw->last_good_ocv_uv, last_good_ocv_uv_ori_uv); |
| 983 | } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) { |
| 984 | chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw; |
| 985 | convert_vbatt_raw_to_uv(chip, usb_chg, |
| 986 | raw->last_good_ocv_raw, &raw->last_good_ocv_uv); |
| 987 | last_ocv_uv = raw->last_good_ocv_uv; |
| 988 | pm_bms_masked_write(chip, OCV_UPDATE_STORAGE, |
| 989 | OCV_UPDATE_STORAGE_USE_MASK, 0x1); |
| 990 | rc = pm8xxx_readb(chip->dev->parent, OCV_UPDATE_STORAGE, &ocv_updated_flag); |
| 991 | if (rc) { |
| 992 | pr_err("%s: failed to read addr = %d, rc=%d\n", |
| 993 | __func__, OCV_UPDATE_STORAGE, rc); |
| 994 | } else { |
| 995 | ocv_updated_flag &= OCV_UPDATE_STORAGE_USE_MASK; |
| 996 | } |
| 997 | pr_info("%s: last_good_ocv_raw/uv=0x%x/%duV, ocv_updated_flag=0x%x\n", |
| 998 | __func__, raw->last_good_ocv_raw, raw->last_good_ocv_uv, |
| 999 | ocv_updated_flag); |
| 1000 | } else { |
| 1001 | raw->last_good_ocv_uv = last_ocv_uv; |
| 1002 | } |
| 1003 | |
| 1004 | pr_debug("0p625 = %duV\n", chip->xoadc_v0625); |
| 1005 | pr_debug("1p25 = %duV\n", chip->xoadc_v125); |
| 1006 | pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n", |
| 1007 | raw->last_good_ocv_raw, raw->last_good_ocv_uv); |
| 1008 | pr_debug("cc_raw= 0x%x\n", raw->cc); |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | static int get_rbatt(struct pm8921_bms_chip *chip, int soc_rbatt, int batt_temp) |
| 1013 | { |
| 1014 | int rbatt, scalefactor; |
| 1015 | |
| 1016 | rbatt = (last_rbatt < 0) ? chip->default_rbatt_mohm : last_rbatt; |
| 1017 | pr_debug("rbatt before scaling = %d\n", rbatt); |
| 1018 | if (chip->rbatt_sf_lut == NULL) { |
| 1019 | pr_debug("RBATT = %d\n", rbatt); |
| 1020 | return rbatt; |
| 1021 | } |
| 1022 | |
| 1023 | |
| 1024 | batt_temp = batt_temp / 10; |
| 1025 | scalefactor = interpolate_scalingfactor(chip, chip->rbatt_sf_lut, |
| 1026 | batt_temp, soc_rbatt); |
| 1027 | bms_dbg.rbatt_sf = scalefactor; |
| 1028 | bms_dbg.soc_rbatt = soc_rbatt; |
| 1029 | pr_debug("rbatt sf = %d for batt_temp = %d, soc_rbatt = %d\n", |
| 1030 | scalefactor, batt_temp, soc_rbatt); |
| 1031 | rbatt = (rbatt * scalefactor) / 100; |
| 1032 | |
| 1033 | rbatt += the_chip->rconn_mohm; |
| 1034 | pr_debug("adding rconn_mohm = %d rbatt = %d\n", |
| 1035 | the_chip->rconn_mohm, rbatt); |
| 1036 | |
| 1037 | if (is_between(20, 10, soc_rbatt)) |
| 1038 | rbatt = rbatt |
| 1039 | + ((20 - soc_rbatt) * chip->delta_rbatt_mohm) / 10; |
| 1040 | else |
| 1041 | if (is_between(10, 0, soc_rbatt)) |
| 1042 | rbatt = rbatt + chip->delta_rbatt_mohm; |
| 1043 | |
| 1044 | pr_debug("RBATT = %d\n", rbatt); |
| 1045 | return rbatt; |
| 1046 | } |
| 1047 | |
| 1048 | static int calculate_rbatt_resume(struct pm8921_bms_chip *chip, |
| 1049 | struct pm8921_rbatt_params *raw) |
| 1050 | { |
| 1051 | unsigned int r_batt; |
| 1052 | |
| 1053 | if (raw->ocv_for_rbatt_uv <= 0 |
| 1054 | || raw->ocv_for_rbatt_uv <= raw->vbatt_for_rbatt_uv |
| 1055 | || raw->vsense_for_rbatt_raw <= 0) { |
| 1056 | pr_debug("rbatt readings unavailable ocv = %d, vbatt = %d," |
| 1057 | "vsen = %d\n", |
| 1058 | raw->ocv_for_rbatt_uv, |
| 1059 | raw->vbatt_for_rbatt_uv, |
| 1060 | raw->vsense_for_rbatt_raw); |
| 1061 | return -EINVAL; |
| 1062 | } |
| 1063 | r_batt = ((raw->ocv_for_rbatt_uv - raw->vbatt_for_rbatt_uv) |
| 1064 | * chip->r_sense) / raw->vsense_for_rbatt_uv; |
| 1065 | pr_debug("r_batt = %umilliOhms", r_batt); |
| 1066 | return r_batt; |
| 1067 | } |
| 1068 | |
| 1069 | static int calculate_fcc_uah(struct pm8921_bms_chip *chip, int batt_temp, |
| 1070 | int chargecycles) |
| 1071 | { |
| 1072 | int initfcc, result, scalefactor = 0; |
| 1073 | |
| 1074 | if (chip->adjusted_fcc_temp_lut == NULL) { |
| 1075 | initfcc = interpolate_fcc(chip, batt_temp); |
| 1076 | |
| 1077 | scalefactor = interpolate_scalingfactor_fcc(chip, chargecycles); |
| 1078 | |
| 1079 | |
| 1080 | result = (initfcc * scalefactor * 1000) / 100; |
| 1081 | pr_debug("fcc = %d uAh\n", result); |
| 1082 | return result; |
| 1083 | } else { |
| 1084 | return 1000 * interpolate_fcc_adjusted(chip, batt_temp); |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | static int get_battery_uvolts(struct pm8921_bms_chip *chip, int *uvolts) |
| 1089 | { |
| 1090 | int rc; |
| 1091 | struct pm8xxx_adc_chan_result result; |
| 1092 | |
| 1093 | rc = pm8xxx_adc_read(chip->vbat_channel, &result); |
| 1094 | if (rc) { |
| 1095 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1096 | chip->vbat_channel, rc); |
| 1097 | return rc; |
| 1098 | } |
| 1099 | pr_debug("mvolts phy = %lld meas = 0x%llx", result.physical, |
| 1100 | result.measurement); |
| 1101 | *uvolts = (int)result.physical; |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
| 1105 | static int adc_based_ocv(struct pm8921_bms_chip *chip, int *ocv) |
| 1106 | { |
| 1107 | int vbatt, rbatt, ibatt_ua, rc; |
| 1108 | |
| 1109 | rc = get_battery_uvolts(chip, &vbatt); |
| 1110 | if (rc) { |
| 1111 | pr_err("failed to read vbatt from adc rc = %d\n", rc); |
| 1112 | return rc; |
| 1113 | } |
| 1114 | |
| 1115 | rc = pm8921_bms_get_battery_current(&ibatt_ua); |
| 1116 | if (rc) { |
| 1117 | pr_err("failed to read batt current rc = %d\n", rc); |
| 1118 | return rc; |
| 1119 | } |
| 1120 | |
| 1121 | rbatt = (last_rbatt < 0) ? chip->default_rbatt_mohm : last_rbatt; |
| 1122 | *ocv = vbatt + (ibatt_ua * rbatt)/1000; |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
| 1126 | static int calculate_pc(struct pm8921_bms_chip *chip, int ocv_uv, int batt_temp, |
| 1127 | int chargecycles) |
| 1128 | { |
| 1129 | int pc, scalefactor; |
| 1130 | |
| 1131 | pc = interpolate_pc(chip, batt_temp, ocv_uv / 1000); |
| 1132 | pr_debug("pc = %u for ocv = %dmicroVolts batt_temp = %d\n", |
| 1133 | pc, ocv_uv, batt_temp); |
| 1134 | |
| 1135 | scalefactor = interpolate_scalingfactor(chip, |
| 1136 | chip->pc_sf_lut, chargecycles, pc); |
| 1137 | pr_debug("scalefactor = %u batt_temp = %d\n", scalefactor, batt_temp); |
| 1138 | |
| 1139 | bms_dbg.scalefactor = scalefactor; |
| 1140 | |
| 1141 | pc = (pc * scalefactor) / 100; |
| 1142 | return pc; |
| 1143 | } |
| 1144 | |
| 1145 | static void calculate_cc_uah(struct pm8921_bms_chip *chip, int cc, int *val) |
| 1146 | { |
| 1147 | int64_t cc_voltage_uv, cc_nvh, cc_uah; |
| 1148 | |
| 1149 | cc_voltage_uv = cc; |
| 1150 | cc_voltage_uv -= chip->cc_reading_at_100; |
| 1151 | pr_debug("cc = %d. after subtracting %d cc = %lld\n", |
| 1152 | cc, chip->cc_reading_at_100, |
| 1153 | cc_voltage_uv); |
| 1154 | cc_voltage_uv = cc_to_microvolt(chip, cc_voltage_uv); |
| 1155 | cc_voltage_uv = pm8xxx_cc_adjust_for_gain(cc_voltage_uv); |
| 1156 | pr_debug("cc_voltage_uv = %lld microvolts\n", cc_voltage_uv); |
| 1157 | cc_nvh = ccmicrovolt_to_nvh(cc_voltage_uv); |
| 1158 | pr_debug("cc_nvh = %lld nano_volt_hour\n", cc_nvh); |
| 1159 | cc_uah = div_s64(cc_nvh, chip->r_sense); |
| 1160 | *val = cc_uah; |
| 1161 | } |
| 1162 | |
| 1163 | static int calculate_unusable_charge_uah(struct pm8921_bms_chip *chip, |
| 1164 | int rbatt, int fcc_uah, |
| 1165 | int batt_temp, int chargecycles) |
| 1166 | { |
| 1167 | int voltage_unusable_uv, pc_unusable; |
| 1168 | |
| 1169 | |
| 1170 | voltage_unusable_uv = (rbatt * chip->i_test) |
| 1171 | + (chip->v_failure * 1000); |
| 1172 | pc_unusable = calculate_pc(chip, voltage_unusable_uv, |
| 1173 | batt_temp, chargecycles); |
| 1174 | pr_debug("rbatt = %umilliOhms unusable_v =%d unusable_pc = %d\n", |
| 1175 | rbatt, voltage_unusable_uv, pc_unusable); |
| 1176 | bms_dbg.rbatt = rbatt; |
| 1177 | bms_dbg.voltage_unusable_uv = voltage_unusable_uv; |
| 1178 | bms_dbg.pc_unusable = pc_unusable; |
| 1179 | return (fcc_uah * pc_unusable) / 100; |
| 1180 | } |
| 1181 | |
| 1182 | static int calculate_remaining_charge_uah(struct pm8921_bms_chip *chip, |
| 1183 | struct pm8921_soc_params *raw, |
| 1184 | int fcc_uah, int batt_temp, |
| 1185 | int chargecycles) |
| 1186 | { |
| 1187 | int ocv, pc; |
| 1188 | |
| 1189 | |
| 1190 | ocv = 0; |
| 1191 | if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw) { |
| 1192 | chip->ocv_reading_at_100 = 0; |
| 1193 | chip->cc_reading_at_100 = 0; |
| 1194 | ocv = raw->last_good_ocv_uv; |
| 1195 | } else { |
| 1196 | ocv = chip->max_voltage_uv; |
| 1197 | } |
| 1198 | |
| 1199 | if (ocv == 0) { |
| 1200 | ocv = last_ocv_uv; |
| 1201 | pr_debug("ocv not available using last_ocv_uv=%d\n", ocv); |
| 1202 | } |
| 1203 | |
| 1204 | pc = calculate_pc(chip, ocv, batt_temp, chargecycles); |
| 1205 | bms_dbg.rc_pc = pc; |
| 1206 | pr_debug("ocv = %d pc = %d\n", ocv, pc); |
| 1207 | return (fcc_uah * pc) / 100; |
| 1208 | } |
| 1209 | |
| 1210 | static void calculate_soc_params(struct pm8921_bms_chip *chip, |
| 1211 | struct pm8921_soc_params *raw, |
| 1212 | int batt_temp, int chargecycles, |
| 1213 | int *fcc_uah, |
| 1214 | int *unusable_charge_uah, |
| 1215 | int *remaining_charge_uah, |
| 1216 | int *cc_uah, |
| 1217 | int *rbatt) |
| 1218 | { |
| 1219 | unsigned long flags; |
| 1220 | int soc_rbatt; |
| 1221 | |
| 1222 | *fcc_uah = calculate_fcc_uah(chip, batt_temp, chargecycles); |
| 1223 | pr_debug("FCC = %uuAh batt_temp = %d, cycles = %d\n", |
| 1224 | *fcc_uah, batt_temp, chargecycles); |
| 1225 | |
| 1226 | spin_lock_irqsave(&chip->bms_100_lock, flags); |
| 1227 | |
| 1228 | *remaining_charge_uah = calculate_remaining_charge_uah(chip, raw, |
| 1229 | *fcc_uah, batt_temp, chargecycles); |
| 1230 | pr_debug("RC = %uuAh\n", *remaining_charge_uah); |
| 1231 | |
| 1232 | |
| 1233 | calculate_cc_uah(chip, raw->cc, cc_uah); |
| 1234 | pr_debug("cc_uah = %duAh raw->cc = %x cc = %lld after subtracting %d\n", |
| 1235 | *cc_uah, raw->cc, |
| 1236 | (int64_t)raw->cc - chip->cc_reading_at_100, |
| 1237 | chip->cc_reading_at_100); |
| 1238 | spin_unlock_irqrestore(&chip->bms_100_lock, flags); |
| 1239 | |
| 1240 | soc_rbatt = ((*remaining_charge_uah - *cc_uah) * 100) / *fcc_uah; |
| 1241 | if (soc_rbatt < 0) |
| 1242 | soc_rbatt = 0; |
| 1243 | *rbatt = get_rbatt(chip, soc_rbatt, batt_temp); |
| 1244 | |
| 1245 | *unusable_charge_uah = calculate_unusable_charge_uah(chip, *rbatt, |
| 1246 | *fcc_uah, batt_temp, chargecycles); |
| 1247 | pr_debug("UUC = %uuAh\n", *unusable_charge_uah); |
| 1248 | } |
| 1249 | |
| 1250 | static int calculate_real_fcc_uah(struct pm8921_bms_chip *chip, |
| 1251 | struct pm8921_soc_params *raw, |
| 1252 | int batt_temp, int chargecycles, |
| 1253 | int *ret_fcc_uah) |
| 1254 | { |
| 1255 | int fcc_uah, unusable_charge_uah; |
| 1256 | int remaining_charge_uah; |
| 1257 | int cc_uah; |
| 1258 | int real_fcc_uah; |
| 1259 | int rbatt; |
| 1260 | |
| 1261 | calculate_soc_params(chip, raw, batt_temp, chargecycles, |
| 1262 | &fcc_uah, |
| 1263 | &unusable_charge_uah, |
| 1264 | &remaining_charge_uah, |
| 1265 | &cc_uah, |
| 1266 | &rbatt); |
| 1267 | |
| 1268 | real_fcc_uah = remaining_charge_uah - cc_uah; |
| 1269 | *ret_fcc_uah = fcc_uah; |
| 1270 | pr_debug("real_fcc = %d, RC = %d CC = %d fcc = %d\n", |
| 1271 | real_fcc_uah, remaining_charge_uah, cc_uah, fcc_uah); |
| 1272 | return real_fcc_uah; |
| 1273 | } |
| 1274 | static int calculate_state_of_charge(struct pm8921_bms_chip *chip, |
| 1275 | struct pm8921_soc_params *raw, |
| 1276 | int batt_temp, int chargecycles, int verbol) |
| 1277 | { |
| 1278 | int remaining_usable_charge_uah, fcc_uah, unusable_charge_uah; |
| 1279 | int remaining_charge_uah, soc, soc_remainder = 0; |
| 1280 | int update_userspace = 1; |
| 1281 | int cc_uah; |
| 1282 | int rbatt; |
| 1283 | |
| 1284 | calculate_soc_params(chip, raw, batt_temp, chargecycles, |
| 1285 | &fcc_uah, |
| 1286 | &unusable_charge_uah, |
| 1287 | &remaining_charge_uah, |
| 1288 | &cc_uah, |
| 1289 | &rbatt); |
| 1290 | bms_dbg.batt_temp = batt_temp; |
| 1291 | |
| 1292 | |
| 1293 | remaining_usable_charge_uah = remaining_charge_uah |
| 1294 | - cc_uah |
| 1295 | - unusable_charge_uah; |
| 1296 | |
| 1297 | pr_debug("RUC = %duAh\n", remaining_usable_charge_uah); |
| 1298 | if (fcc_uah - unusable_charge_uah <= 0) { |
| 1299 | pr_warn("FCC = %duAh, UUC = %duAh forcing soc = 0\n", |
| 1300 | fcc_uah, unusable_charge_uah); |
| 1301 | soc = 0; |
| 1302 | } else { |
| 1303 | soc = (remaining_usable_charge_uah * 100) |
| 1304 | / (fcc_uah - unusable_charge_uah); |
| 1305 | soc_remainder = (remaining_usable_charge_uah * 100) |
| 1306 | % (fcc_uah - unusable_charge_uah); |
| 1307 | |
| 1308 | if (soc >= 0 && soc_remainder > 0) |
| 1309 | soc += 1; |
| 1310 | } |
| 1311 | |
| 1312 | if (verbol) { |
| 1313 | pr_info("FCC=%d,UC=%d,RC=%d,CC=%d,RUC=%d,SOC=%d,SOC_R=%d," |
| 1314 | "start_percent=%d,end_percent=%d," |
| 1315 | "rbatt=%d,rbatt_sf=%d,batt_temp=%d,soc_rbatt=%d,last_rbatt=%d," |
| 1316 | "V_unusable_uv=%d,pc_unusable=%d,rc_pc=%d,scalefactor=%d\n", |
| 1317 | fcc_uah, unusable_charge_uah, remaining_charge_uah, |
| 1318 | cc_uah, remaining_usable_charge_uah, soc, soc_remainder, |
| 1319 | the_chip->start_percent, the_chip->end_percent, |
| 1320 | bms_dbg.rbatt, bms_dbg.rbatt_sf, bms_dbg.batt_temp, |
| 1321 | bms_dbg.soc_rbatt, last_rbatt, bms_dbg.voltage_unusable_uv, |
| 1322 | bms_dbg.pc_unusable, bms_dbg.rc_pc, bms_dbg.scalefactor); |
| 1323 | } |
| 1324 | |
| 1325 | if (soc > 100) |
| 1326 | soc = 100; |
| 1327 | pr_debug("SOC = %u%%\n", soc); |
| 1328 | |
| 1329 | if (bms_fake_battery != -EINVAL) { |
| 1330 | pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery); |
| 1331 | return bms_fake_battery; |
| 1332 | } |
| 1333 | |
| 1334 | if (soc < 0) { |
| 1335 | pr_err("bad rem_usb_chg = %d rem_chg %d," |
| 1336 | "cc_uah %d, unusb_chg %d\n", |
| 1337 | remaining_usable_charge_uah, |
| 1338 | remaining_charge_uah, |
| 1339 | cc_uah, unusable_charge_uah); |
| 1340 | |
| 1341 | pr_err("for bad rem_usb_chg last_ocv_uv = %d" |
| 1342 | "chargecycles = %d, batt_temp = %d" |
| 1343 | "fcc = %d soc =%d\n", |
| 1344 | last_ocv_uv, chargecycles, batt_temp, |
| 1345 | fcc_uah, soc); |
| 1346 | update_userspace = 0; |
| 1347 | soc = 0; |
| 1348 | } |
| 1349 | |
| 1350 | if (last_soc == -EINVAL || soc <= last_soc) { |
| 1351 | last_soc = update_userspace ? soc : last_soc; |
| 1352 | return soc; |
| 1353 | } |
| 1354 | |
| 1355 | if (the_chip->start_percent != -EINVAL) { |
| 1356 | last_soc = soc; |
| 1357 | } else { |
| 1358 | pr_info("soc = %d reporting last_soc = %d\n", soc, last_soc); |
| 1359 | soc = last_soc; |
| 1360 | } |
| 1361 | |
| 1362 | |
| 1363 | return soc; |
| 1364 | } |
| 1365 | |
| 1366 | #define MIN_DELTA_625_UV 1000 |
| 1367 | static void calib_hkadc(struct pm8921_bms_chip *chip) |
| 1368 | { |
| 1369 | int voltage, rc; |
| 1370 | struct pm8xxx_adc_chan_result result; |
| 1371 | int usb_chg; |
| 1372 | int this_delta; |
| 1373 | |
| 1374 | rc = pm8xxx_adc_read(the_chip->ref1p25v_channel, &result); |
| 1375 | if (rc) { |
| 1376 | pr_err("ADC failed for 1.25volts rc = %d\n", rc); |
| 1377 | return; |
| 1378 | } |
| 1379 | voltage = xoadc_reading_to_microvolt(result.adc_code); |
| 1380 | |
| 1381 | pr_debug("result 1.25v = 0x%x, voltage = %duV adc_meas = %lld\n", |
| 1382 | result.adc_code, voltage, result.measurement); |
| 1383 | |
| 1384 | chip->xoadc_v125 = voltage; |
| 1385 | |
| 1386 | rc = pm8xxx_adc_read(the_chip->ref625mv_channel, &result); |
| 1387 | if (rc) { |
| 1388 | pr_err("ADC failed for 1.25volts rc = %d\n", rc); |
| 1389 | return; |
| 1390 | } |
| 1391 | voltage = xoadc_reading_to_microvolt(result.adc_code); |
| 1392 | |
| 1393 | usb_chg = usb_chg_plugged_in(); |
| 1394 | pr_debug("result 0.625V = 0x%x, voltage = %duV adc_meas = %lld " |
| 1395 | "usb_chg = %d\n", |
| 1396 | result.adc_code, voltage, result.measurement, |
| 1397 | usb_chg); |
| 1398 | |
| 1399 | if (usb_chg) |
| 1400 | chip->xoadc_v0625_usb_present = voltage; |
| 1401 | else |
| 1402 | chip->xoadc_v0625_usb_absent = voltage; |
| 1403 | |
| 1404 | chip->xoadc_v0625 = voltage; |
| 1405 | if (chip->xoadc_v0625_usb_present && chip->xoadc_v0625_usb_absent) { |
| 1406 | this_delta = chip->xoadc_v0625_usb_present |
| 1407 | - chip->xoadc_v0625_usb_absent; |
| 1408 | pr_debug("this_delta= %duV\n", this_delta); |
| 1409 | if (this_delta > MIN_DELTA_625_UV) |
| 1410 | last_usb_cal_delta_uv = this_delta; |
| 1411 | pr_debug("625V_present= %d, 625V_absent= %d, delta = %duV\n", |
| 1412 | chip->xoadc_v0625_usb_present, |
| 1413 | chip->xoadc_v0625_usb_absent, |
| 1414 | last_usb_cal_delta_uv); |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | static void calibrate_hkadc_work(struct work_struct *work) |
| 1419 | { |
| 1420 | struct pm8921_bms_chip *chip = container_of(work, |
| 1421 | struct pm8921_bms_chip, calib_hkadc_work); |
| 1422 | |
| 1423 | calib_hkadc(chip); |
| 1424 | } |
| 1425 | |
| 1426 | void pm8921_bms_calibrate_hkadc(void) |
| 1427 | { |
| 1428 | schedule_work(&the_chip->calib_hkadc_work); |
| 1429 | } |
| 1430 | |
| 1431 | int pm8921_bms_get_vsense_avg(int *result) |
| 1432 | { |
| 1433 | int rc = -EINVAL; |
| 1434 | |
| 1435 | if (the_chip) { |
| 1436 | mutex_lock(&the_chip->bms_output_lock); |
| 1437 | pm_bms_lock_output_data(the_chip); |
| 1438 | rc = read_vsense_avg(the_chip, result); |
| 1439 | pm_bms_unlock_output_data(the_chip); |
| 1440 | mutex_unlock(&the_chip->bms_output_lock); |
| 1441 | } else |
| 1442 | pr_err("called before initialization\n"); |
| 1443 | return rc; |
| 1444 | } |
| 1445 | EXPORT_SYMBOL(pm8921_bms_get_vsense_avg); |
| 1446 | |
| 1447 | int pm8921_bms_get_battery_current(int *result_ua) |
| 1448 | { |
| 1449 | int vsense; |
| 1450 | |
| 1451 | if (!the_chip) { |
| 1452 | pr_err("called before initialization\n"); |
| 1453 | return -EINVAL; |
| 1454 | } |
| 1455 | if (the_chip->r_sense == 0) { |
| 1456 | pr_err("r_sense is zero\n"); |
| 1457 | return -EINVAL; |
| 1458 | } |
| 1459 | |
| 1460 | mutex_lock(&the_chip->bms_output_lock); |
| 1461 | pm_bms_lock_output_data(the_chip); |
| 1462 | read_vsense_avg(the_chip, &vsense); |
| 1463 | pm_bms_unlock_output_data(the_chip); |
| 1464 | mutex_unlock(&the_chip->bms_output_lock); |
| 1465 | pr_debug("vsense=%duV\n", vsense); |
| 1466 | |
| 1467 | *result_ua = vsense * 1000 / (int)the_chip->r_sense; |
| 1468 | pr_debug("ibat=%duA\n", *result_ua); |
| 1469 | return 0; |
| 1470 | } |
| 1471 | EXPORT_SYMBOL(pm8921_bms_get_battery_current); |
| 1472 | |
| 1473 | int pm8921_bms_get_percent_charge(void) |
| 1474 | { |
| 1475 | int batt_temp, rc; |
| 1476 | struct pm8xxx_adc_chan_result result; |
| 1477 | struct pm8921_soc_params raw; |
| 1478 | |
| 1479 | if (!the_chip) { |
| 1480 | pr_err("called before initialization\n"); |
| 1481 | return -EINVAL; |
| 1482 | } |
| 1483 | |
| 1484 | rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result); |
| 1485 | if (rc) { |
| 1486 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1487 | the_chip->batt_temp_channel, rc); |
| 1488 | return rc; |
| 1489 | } |
| 1490 | pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical, |
| 1491 | result.measurement); |
| 1492 | batt_temp = (int)result.physical; |
| 1493 | |
| 1494 | read_soc_params_raw(the_chip, &raw); |
| 1495 | |
| 1496 | return calculate_state_of_charge(the_chip, &raw, |
| 1497 | batt_temp, last_chargecycles, 0); |
| 1498 | } |
| 1499 | EXPORT_SYMBOL_GPL(pm8921_bms_get_percent_charge); |
| 1500 | |
| 1501 | int pm8921_bms_get_rbatt(void) |
| 1502 | { |
| 1503 | int batt_temp, rc; |
| 1504 | struct pm8xxx_adc_chan_result result; |
| 1505 | struct pm8921_soc_params raw; |
| 1506 | int fcc_uah; |
| 1507 | int unusable_charge_uah; |
| 1508 | int remaining_charge_uah; |
| 1509 | int cc_uah; |
| 1510 | int rbatt; |
| 1511 | |
| 1512 | if (!the_chip) { |
| 1513 | pr_err("called before initialization\n"); |
| 1514 | return -EINVAL; |
| 1515 | } |
| 1516 | |
| 1517 | rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result); |
| 1518 | if (rc) { |
| 1519 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1520 | the_chip->batt_temp_channel, rc); |
| 1521 | return rc; |
| 1522 | } |
| 1523 | pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical, |
| 1524 | result.measurement); |
| 1525 | batt_temp = (int)result.physical; |
| 1526 | |
| 1527 | read_soc_params_raw(the_chip, &raw); |
| 1528 | |
| 1529 | calculate_soc_params(the_chip, &raw, batt_temp, last_chargecycles, |
| 1530 | &fcc_uah, |
| 1531 | &unusable_charge_uah, |
| 1532 | &remaining_charge_uah, |
| 1533 | &cc_uah, |
| 1534 | &rbatt); |
| 1535 | return rbatt; |
| 1536 | } |
| 1537 | EXPORT_SYMBOL_GPL(pm8921_bms_get_rbatt); |
| 1538 | |
| 1539 | int pm8921_bms_get_fcc(void) |
| 1540 | { |
| 1541 | int batt_temp, rc; |
| 1542 | struct pm8xxx_adc_chan_result result; |
| 1543 | |
| 1544 | if (!the_chip) { |
| 1545 | pr_err("called before initialization\n"); |
| 1546 | return -EINVAL; |
| 1547 | } |
| 1548 | |
| 1549 | rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result); |
| 1550 | if (rc) { |
| 1551 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1552 | the_chip->batt_temp_channel, rc); |
| 1553 | return rc; |
| 1554 | } |
| 1555 | pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical, |
| 1556 | result.measurement); |
| 1557 | batt_temp = (int)result.physical; |
| 1558 | return calculate_fcc_uah(the_chip, batt_temp, last_chargecycles); |
| 1559 | } |
| 1560 | EXPORT_SYMBOL_GPL(pm8921_bms_get_fcc); |
| 1561 | |
| 1562 | #ifdef CONFIG_HTC_BATT_8960 |
| 1563 | int pm8921_bms_get_batt_current(int *result) |
| 1564 | { |
| 1565 | return pm8921_bms_get_battery_current(result); |
| 1566 | } |
| 1567 | |
| 1568 | int pm8921_bms_get_batt_soc(int *result) |
| 1569 | { |
| 1570 | int batt_temp, rc; |
| 1571 | struct pm8xxx_adc_chan_result temp_result; |
| 1572 | struct pm8921_soc_params raw; |
| 1573 | |
| 1574 | if (!the_chip) { |
| 1575 | pr_err("called before initialization\n"); |
| 1576 | return -EINVAL; |
| 1577 | } |
| 1578 | |
| 1579 | rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &temp_result); |
| 1580 | if (rc) { |
| 1581 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1582 | the_chip->batt_temp_channel, rc); |
| 1583 | return rc; |
| 1584 | } |
| 1585 | pr_debug("batt_temp phy = %lld meas = 0x%llx", temp_result.physical, |
| 1586 | temp_result.measurement); |
| 1587 | batt_temp = (int)temp_result.physical; |
| 1588 | |
| 1589 | read_soc_params_raw(the_chip, &raw); |
| 1590 | |
| 1591 | *result = calculate_state_of_charge(the_chip, &raw, |
| 1592 | batt_temp, last_chargecycles, 1); |
| 1593 | if (bms_discharge_percent && |
| 1594 | ((bms_discharge_percent - *result) >= 5)) { |
| 1595 | pr_info("OCV can be update due to %d - %d >= 5\n", |
| 1596 | bms_discharge_percent, *result); |
| 1597 | bms_discharge_percent = 0; |
| 1598 | pm8921_bms_start_ocv_updates(); |
| 1599 | } |
| 1600 | |
| 1601 | return 0; |
| 1602 | } |
| 1603 | |
| 1604 | int pm8921_bms_get_batt_cc(int *result) |
| 1605 | { |
| 1606 | *result = dump_cc_uah(); |
| 1607 | |
| 1608 | return 0; |
| 1609 | } |
| 1610 | #endif |
| 1611 | |
| 1612 | #define IBAT_TOL_MASK 0x0F |
| 1613 | #define OCV_TOL 0xF0 |
| 1614 | #define OCV_TOL_MASK 0xF0 |
| 1615 | #define IBAT_TOL_DEFAULT 0x03 |
| 1616 | #define IBAT_TOL_NOCHG 0x0F |
| 1617 | #define OCV_TOL_DEFAULT 0x20 |
| 1618 | #define OCV_TOL_NO_OCV 0x00 |
| 1619 | int pm8921_bms_charging_began(void) |
| 1620 | { |
| 1621 | int batt_temp, rc = 0; |
| 1622 | struct pm8xxx_adc_chan_result result; |
| 1623 | struct pm8921_soc_params raw; |
| 1624 | |
| 1625 | if (!the_chip) { |
| 1626 | pr_err("called before initialization\n"); |
| 1627 | return -EINVAL; |
| 1628 | } |
| 1629 | |
| 1630 | rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result); |
| 1631 | if (rc) { |
| 1632 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1633 | the_chip->batt_temp_channel, rc); |
| 1634 | return rc; |
| 1635 | } |
| 1636 | pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical, |
| 1637 | result.measurement); |
| 1638 | batt_temp = (int)result.physical; |
| 1639 | |
| 1640 | read_soc_params_raw(the_chip, &raw); |
| 1641 | |
| 1642 | the_chip->start_percent = calculate_state_of_charge(the_chip, &raw, |
| 1643 | batt_temp, last_chargecycles, 0); |
| 1644 | bms_start_percent = the_chip->start_percent; |
| 1645 | bms_start_ocv_uv = raw.last_good_ocv_uv; |
| 1646 | calculate_cc_uah(the_chip, raw.cc, &bms_start_cc_uah); |
| 1647 | pm_bms_masked_write(the_chip, BMS_TOLERANCES, |
| 1648 | IBAT_TOL_MASK, IBAT_TOL_DEFAULT); |
| 1649 | pr_info("start_percent = %d%%\n", the_chip->start_percent); |
| 1650 | bms_discharge_percent = 0; |
| 1651 | pm8921_bms_stop_ocv_updates(); |
| 1652 | |
| 1653 | return rc; |
| 1654 | } |
| 1655 | EXPORT_SYMBOL_GPL(pm8921_bms_charging_began); |
| 1656 | |
| 1657 | #define DELTA_FCC_PERCENT 3 |
| 1658 | #define MIN_START_PERCENT_FOR_LEARNING (-30) |
| 1659 | void pm8921_bms_charging_end(int is_battery_full) |
| 1660 | { |
| 1661 | int batt_temp, rc; |
| 1662 | struct pm8xxx_adc_chan_result result; |
| 1663 | struct pm8921_soc_params raw; |
| 1664 | |
| 1665 | if (the_chip == NULL) |
| 1666 | return; |
| 1667 | |
| 1668 | rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result); |
| 1669 | if (rc) { |
| 1670 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1671 | the_chip->batt_temp_channel, rc); |
| 1672 | return; |
| 1673 | } |
| 1674 | pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical, |
| 1675 | result.measurement); |
| 1676 | batt_temp = (int)result.physical; |
| 1677 | |
| 1678 | read_soc_params_raw(the_chip, &raw); |
| 1679 | |
| 1680 | calculate_cc_uah(the_chip, raw.cc, &bms_end_cc_uah); |
| 1681 | |
| 1682 | if (is_battery_full |
| 1683 | && the_chip->start_percent <= MIN_START_PERCENT_FOR_LEARNING) { |
| 1684 | int fcc_uah, new_fcc_uah, delta_fcc_uah; |
| 1685 | |
| 1686 | new_fcc_uah = calculate_real_fcc_uah(the_chip, &raw, |
| 1687 | batt_temp, last_chargecycles, |
| 1688 | &fcc_uah); |
| 1689 | delta_fcc_uah = new_fcc_uah - fcc_uah; |
| 1690 | if (delta_fcc_uah < 0) |
| 1691 | delta_fcc_uah = -delta_fcc_uah; |
| 1692 | |
| 1693 | if (delta_fcc_uah * 100 > (DELTA_FCC_PERCENT * fcc_uah)) { |
| 1694 | |
| 1695 | if (new_fcc_uah > fcc_uah) |
| 1696 | new_fcc_uah |
| 1697 | = (fcc_uah + |
| 1698 | (DELTA_FCC_PERCENT * fcc_uah) / 100); |
| 1699 | else |
| 1700 | new_fcc_uah |
| 1701 | = (fcc_uah - |
| 1702 | (DELTA_FCC_PERCENT * fcc_uah) / 100); |
| 1703 | pr_info("delta_fcc=%d > %d percent of fcc=%d" |
| 1704 | "restring it to %d\n", |
| 1705 | delta_fcc_uah, DELTA_FCC_PERCENT, |
| 1706 | fcc_uah, new_fcc_uah); |
| 1707 | } |
| 1708 | last_real_fcc_mah = new_fcc_uah/1000; |
| 1709 | last_real_fcc_batt_temp = batt_temp; |
| 1710 | readjust_fcc_table(); |
| 1711 | pr_info("learnt fcc = %d batt_temp = %d\n", |
| 1712 | last_real_fcc_mah, last_real_fcc_batt_temp); |
| 1713 | } |
| 1714 | |
| 1715 | |
| 1716 | if (is_battery_full) { |
| 1717 | unsigned long flags; |
| 1718 | spin_lock_irqsave(&the_chip->bms_100_lock, flags); |
| 1719 | the_chip->ocv_reading_at_100 = raw.last_good_ocv_raw; |
| 1720 | the_chip->cc_reading_at_100 = raw.cc; |
| 1721 | spin_unlock_irqrestore(&the_chip->bms_100_lock, flags); |
| 1722 | pr_debug("EOC ocv_reading = 0x%x cc = %d\n", |
| 1723 | the_chip->ocv_reading_at_100, |
| 1724 | the_chip->cc_reading_at_100); |
| 1725 | pm8921_bms_start_ocv_updates(); |
| 1726 | } |
| 1727 | |
| 1728 | the_chip->end_percent = calculate_state_of_charge(the_chip, &raw, |
| 1729 | batt_temp, last_chargecycles, 0); |
| 1730 | |
| 1731 | bms_end_percent = the_chip->end_percent; |
| 1732 | if (!is_battery_full) |
| 1733 | bms_discharge_percent = the_chip->end_percent; |
| 1734 | else |
| 1735 | bms_discharge_percent = 0; |
| 1736 | bms_end_ocv_uv = raw.last_good_ocv_uv; |
| 1737 | |
| 1738 | if (the_chip->end_percent > the_chip->start_percent) { |
| 1739 | last_charge_increase += |
| 1740 | the_chip->end_percent - the_chip->start_percent; |
| 1741 | if (last_charge_increase > 100) { |
| 1742 | last_chargecycles++; |
| 1743 | last_charge_increase = last_charge_increase % 100; |
| 1744 | } |
| 1745 | } |
| 1746 | pr_info("end_percent = %d%% last_charge_increase = %d" |
| 1747 | "last_chargecycles = %d\n", |
| 1748 | the_chip->end_percent, |
| 1749 | last_charge_increase, |
| 1750 | last_chargecycles); |
| 1751 | the_chip->start_percent = -EINVAL; |
| 1752 | the_chip->end_percent = -EINVAL; |
| 1753 | pm_bms_masked_write(the_chip, BMS_TOLERANCES, |
| 1754 | IBAT_TOL_MASK, IBAT_TOL_NOCHG); |
| 1755 | } |
| 1756 | EXPORT_SYMBOL_GPL(pm8921_bms_charging_end); |
| 1757 | |
| 1758 | int pm8921_bms_stop_ocv_updates(void) |
| 1759 | { |
| 1760 | if (!the_chip) { |
| 1761 | pr_err("called before init\n"); |
| 1762 | return -EINVAL; |
| 1763 | } |
| 1764 | if (!is_ocv_update_start) { |
| 1765 | pr_info("ocv updates is already stopped"); |
| 1766 | return -EINVAL; |
| 1767 | } |
| 1768 | is_ocv_update_start = 0; |
| 1769 | pr_info("stopping ocv updates, is_ocv_update_start=%d", is_ocv_update_start); |
| 1770 | return pm_bms_masked_write(the_chip, BMS_TOLERANCES, |
| 1771 | OCV_TOL_MASK, OCV_TOL_NO_OCV); |
| 1772 | } |
| 1773 | EXPORT_SYMBOL_GPL(pm8921_bms_stop_ocv_updates); |
| 1774 | |
| 1775 | int pm8921_bms_start_ocv_updates(void) |
| 1776 | { |
| 1777 | if (!the_chip) { |
| 1778 | pr_err("called before init\n"); |
| 1779 | return -EINVAL; |
| 1780 | } |
| 1781 | if (is_ocv_update_start) { |
| 1782 | pr_info("ocv updates is already started"); |
| 1783 | return -EINVAL; |
| 1784 | } |
| 1785 | is_ocv_update_start = 1; |
| 1786 | pr_info("starting ocv updates, is_ocv_update_start=%d", is_ocv_update_start); |
| 1787 | return pm_bms_masked_write(the_chip, BMS_TOLERANCES, |
| 1788 | OCV_TOL_MASK, OCV_TOL_DEFAULT); |
| 1789 | } |
| 1790 | EXPORT_SYMBOL_GPL(pm8921_bms_start_ocv_updates); |
| 1791 | |
| 1792 | static irqreturn_t pm8921_bms_sbi_write_ok_handler(int irq, void *data) |
| 1793 | { |
| 1794 | pr_debug("irq = %d triggered", irq); |
| 1795 | return IRQ_HANDLED; |
| 1796 | } |
| 1797 | |
| 1798 | static irqreturn_t pm8921_bms_cc_thr_handler(int irq, void *data) |
| 1799 | { |
| 1800 | pr_debug("irq = %d triggered", irq); |
| 1801 | return IRQ_HANDLED; |
| 1802 | } |
| 1803 | |
| 1804 | static irqreturn_t pm8921_bms_vsense_thr_handler(int irq, void *data) |
| 1805 | { |
| 1806 | pr_debug("irq = %d triggered", irq); |
| 1807 | return IRQ_HANDLED; |
| 1808 | } |
| 1809 | |
| 1810 | static irqreturn_t pm8921_bms_vsense_for_r_handler(int irq, void *data) |
| 1811 | { |
| 1812 | pr_debug("irq = %d triggered", irq); |
| 1813 | return IRQ_HANDLED; |
| 1814 | } |
| 1815 | |
| 1816 | static irqreturn_t pm8921_bms_ocv_for_r_handler(int irq, void *data) |
| 1817 | { |
| 1818 | struct pm8921_bms_chip *chip = data; |
| 1819 | |
| 1820 | pr_debug("irq = %d triggered", irq); |
| 1821 | schedule_work(&chip->calib_hkadc_work); |
| 1822 | return IRQ_HANDLED; |
| 1823 | } |
| 1824 | |
| 1825 | static irqreturn_t pm8921_bms_good_ocv_handler(int irq, void *data) |
| 1826 | { |
| 1827 | struct pm8921_bms_chip *chip = data; |
| 1828 | |
| 1829 | pr_debug("irq = %d triggered", irq); |
| 1830 | schedule_work(&chip->calib_hkadc_work); |
| 1831 | return IRQ_HANDLED; |
| 1832 | } |
| 1833 | |
| 1834 | static irqreturn_t pm8921_bms_vsense_avg_handler(int irq, void *data) |
| 1835 | { |
| 1836 | pr_debug("irq = %d triggered", irq); |
| 1837 | return IRQ_HANDLED; |
| 1838 | } |
| 1839 | |
| 1840 | struct pm_bms_irq_init_data { |
| 1841 | unsigned int irq_id; |
| 1842 | char *name; |
| 1843 | unsigned long flags; |
| 1844 | irqreturn_t (*handler)(int, void *); |
| 1845 | }; |
| 1846 | |
| 1847 | #define BMS_IRQ(_id, _flags, _handler) \ |
| 1848 | { \ |
| 1849 | .irq_id = _id, \ |
| 1850 | .name = #_id, \ |
| 1851 | .flags = _flags, \ |
| 1852 | .handler = _handler, \ |
| 1853 | } |
| 1854 | |
| 1855 | struct pm_bms_irq_init_data bms_irq_data[] = { |
| 1856 | BMS_IRQ(PM8921_BMS_SBI_WRITE_OK, IRQF_TRIGGER_RISING, |
| 1857 | pm8921_bms_sbi_write_ok_handler), |
| 1858 | BMS_IRQ(PM8921_BMS_CC_THR, IRQF_TRIGGER_RISING, |
| 1859 | pm8921_bms_cc_thr_handler), |
| 1860 | BMS_IRQ(PM8921_BMS_VSENSE_THR, IRQF_TRIGGER_RISING, |
| 1861 | pm8921_bms_vsense_thr_handler), |
| 1862 | BMS_IRQ(PM8921_BMS_VSENSE_FOR_R, IRQF_TRIGGER_RISING, |
| 1863 | pm8921_bms_vsense_for_r_handler), |
| 1864 | BMS_IRQ(PM8921_BMS_OCV_FOR_R, IRQF_TRIGGER_RISING, |
| 1865 | pm8921_bms_ocv_for_r_handler), |
| 1866 | BMS_IRQ(PM8921_BMS_GOOD_OCV, IRQF_TRIGGER_RISING, |
| 1867 | pm8921_bms_good_ocv_handler), |
| 1868 | BMS_IRQ(PM8921_BMS_VSENSE_AVG, IRQF_TRIGGER_RISING, |
| 1869 | pm8921_bms_vsense_avg_handler), |
| 1870 | }; |
| 1871 | |
| 1872 | static void free_irqs(struct pm8921_bms_chip *chip) |
| 1873 | { |
| 1874 | int i; |
| 1875 | |
| 1876 | for (i = 0; i < PM_BMS_MAX_INTS; i++) |
| 1877 | if (chip->pmic_bms_irq[i]) { |
| 1878 | free_irq(chip->pmic_bms_irq[i], NULL); |
| 1879 | chip->pmic_bms_irq[i] = 0; |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | static int __devinit request_irqs(struct pm8921_bms_chip *chip, |
| 1884 | struct platform_device *pdev) |
| 1885 | { |
| 1886 | struct resource *res; |
| 1887 | int ret, i; |
| 1888 | |
| 1889 | ret = 0; |
| 1890 | bitmap_fill(chip->enabled_irqs, PM_BMS_MAX_INTS); |
| 1891 | |
| 1892 | for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) { |
| 1893 | res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, |
| 1894 | bms_irq_data[i].name); |
| 1895 | if (res == NULL) { |
| 1896 | pr_err("couldn't find %s\n", bms_irq_data[i].name); |
| 1897 | goto err_out; |
| 1898 | } |
| 1899 | ret = request_irq(res->start, bms_irq_data[i].handler, |
| 1900 | bms_irq_data[i].flags, |
| 1901 | bms_irq_data[i].name, chip); |
| 1902 | if (ret < 0) { |
| 1903 | pr_err("couldn't request %d (%s) %d\n", res->start, |
| 1904 | bms_irq_data[i].name, ret); |
| 1905 | goto err_out; |
| 1906 | } |
| 1907 | chip->pmic_bms_irq[bms_irq_data[i].irq_id] = res->start; |
| 1908 | pm8921_bms_disable_irq(chip, bms_irq_data[i].irq_id); |
| 1909 | } |
| 1910 | return 0; |
| 1911 | |
| 1912 | err_out: |
| 1913 | free_irqs(chip); |
| 1914 | return -EINVAL; |
| 1915 | } |
| 1916 | |
| 1917 | #define EN_BMS_BIT BIT(7) |
| 1918 | #define EN_PON_HS_BIT BIT(0) |
| 1919 | static int __devinit pm8921_bms_hw_init(struct pm8921_bms_chip *chip) |
| 1920 | { |
| 1921 | int rc; |
| 1922 | |
| 1923 | rc = pm_bms_masked_write(chip, BMS_CONTROL, |
| 1924 | EN_BMS_BIT | EN_PON_HS_BIT, EN_BMS_BIT | EN_PON_HS_BIT); |
| 1925 | if (rc) { |
| 1926 | pr_err("failed to enable pon and bms addr = %d %d", |
| 1927 | BMS_CONTROL, rc); |
| 1928 | } |
| 1929 | |
| 1930 | |
| 1931 | pm_bms_masked_write(chip, BMS_TOLERANCES, |
| 1932 | IBAT_TOL_MASK, IBAT_TOL_NOCHG); |
| 1933 | |
| 1934 | is_ocv_update_start = 1; |
| 1935 | pm_bms_masked_write(chip, BMS_TOLERANCES, |
| 1936 | OCV_TOL_MASK, OCV_TOL_DEFAULT); |
| 1937 | return 0; |
| 1938 | } |
| 1939 | |
| 1940 | static int bms_disabled; |
| 1941 | static int set_disable_bms_param(const char *val, struct kernel_param *kp) |
| 1942 | { |
| 1943 | u8 data; |
| 1944 | int rc; |
| 1945 | struct pm8921_bms_chip *chip = the_chip; |
| 1946 | |
| 1947 | rc = param_set_int(val, kp); |
| 1948 | |
| 1949 | if (rc) { |
| 1950 | pr_err("error setting value %d\n", rc); |
| 1951 | return rc; |
| 1952 | } |
| 1953 | |
| 1954 | if (bms_disabled) |
| 1955 | data = 0; |
| 1956 | else |
| 1957 | data = EN_BMS_BIT | EN_PON_HS_BIT; |
| 1958 | |
| 1959 | pr_info("set bms_disabled =%d\n", bms_disabled); |
| 1960 | rc = pm_bms_masked_write(chip, BMS_CONTROL, |
| 1961 | EN_BMS_BIT | EN_PON_HS_BIT, data); |
| 1962 | if (rc) { |
| 1963 | pr_err("failed to enable pon and bms addr = %d %d", |
| 1964 | BMS_CONTROL, rc); |
| 1965 | } |
| 1966 | |
| 1967 | return 0; |
| 1968 | } |
| 1969 | module_param_call(disabled, set_disable_bms_param, param_get_uint, |
| 1970 | &bms_disabled, 0644); |
| 1971 | |
| 1972 | static void check_initial_ocv(struct pm8921_bms_chip *chip) |
| 1973 | { |
| 1974 | int ocv_uv, rc; |
| 1975 | int16_t ocv_raw; |
| 1976 | int usb_chg; |
| 1977 | |
| 1978 | ocv_uv = 0; |
| 1979 | pm_bms_read_output_data(chip, LAST_GOOD_OCV_VALUE, &ocv_raw); |
| 1980 | usb_chg = usb_chg_plugged_in(); |
| 1981 | rc = convert_vbatt_raw_to_uv(chip, usb_chg, ocv_raw, &ocv_uv); |
| 1982 | if (rc || ocv_uv == 0) { |
| 1983 | rc = adc_based_ocv(chip, &ocv_uv); |
| 1984 | if (rc) { |
| 1985 | pr_err("failed to read adc based ocv_uv rc = %d\n", rc); |
| 1986 | ocv_uv = DEFAULT_OCV_MICROVOLTS; |
| 1987 | } |
| 1988 | last_ocv_uv = ocv_uv; |
| 1989 | } |
| 1990 | pr_debug("ocv_uv = %d last_ocv_uv = %d\n", ocv_uv, last_ocv_uv); |
| 1991 | } |
| 1992 | |
| 1993 | static int64_t read_battery_id(struct pm8921_bms_chip *chip) |
| 1994 | { |
| 1995 | int rc; |
| 1996 | struct pm8xxx_adc_chan_result result; |
| 1997 | |
| 1998 | rc = pm8xxx_adc_read(chip->batt_id_channel, &result); |
| 1999 | if (rc) { |
| 2000 | pr_err("error reading batt id channel = %d, rc = %d\n", |
| 2001 | chip->vbat_channel, rc); |
| 2002 | return rc; |
| 2003 | } |
| 2004 | pr_debug("batt_id phy = %lld meas = 0x%llx\n", result.physical, |
| 2005 | result.measurement); |
| 2006 | return result.physical; |
| 2007 | } |
| 2008 | |
| 2009 | #ifdef CONFIG_HTC_BATT_8960 |
| 2010 | #define PM8921_BMS_HTC_FAKE_BATT_ID (1) |
| 2011 | static int set_battery_data(struct pm8921_bms_chip *chip) |
| 2012 | { |
| 2013 | int battery_id_mv, batt_id; |
| 2014 | struct pm8921_bms_battery_data* bms_battery_data; |
| 2015 | |
| 2016 | |
| 2017 | |
| 2018 | if (pm8xxx_get_revision(chip->dev->parent) < PM8XXX_REVISION_8921_2p0) { |
| 2019 | batt_id = PM8921_BMS_HTC_FAKE_BATT_ID; |
| 2020 | htc_battery_cell_set_cur_cell_by_id(batt_id); |
| 2021 | } else { |
| 2022 | battery_id_mv = (int)read_battery_id(chip) / 1000; |
| 2023 | |
| 2024 | batt_id = htc_battery_cell_find_and_set_id_auto(battery_id_mv); |
| 2025 | } |
| 2026 | |
| 2027 | |
| 2028 | bms_battery_data = htc_battery_cell_get_cur_cell_gauge_cdata(); |
| 2029 | |
| 2030 | if (bms_battery_data) { |
| 2031 | pr_info("set bms_battery_data (cell_id=%d).\n", |
| 2032 | batt_id); |
| 2033 | chip->fcc = bms_battery_data->fcc; |
| 2034 | chip->fcc_temp_lut = bms_battery_data->fcc_temp_lut; |
| 2035 | chip->fcc_sf_lut = bms_battery_data->fcc_sf_lut; |
| 2036 | chip->pc_temp_ocv_lut = bms_battery_data->pc_temp_ocv_lut; |
| 2037 | chip->pc_sf_lut = bms_battery_data->pc_sf_lut; |
| 2038 | chip->rbatt_sf_lut = bms_battery_data->rbatt_sf_lut; |
| 2039 | chip->default_rbatt_mohm |
| 2040 | = bms_battery_data->default_rbatt_mohm; |
| 2041 | chip->delta_rbatt_mohm |
| 2042 | = bms_battery_data->delta_rbatt_mohm; |
| 2043 | } else { |
| 2044 | pr_err("bms_battery_data doesn't exist (id=%d)\n", |
| 2045 | batt_id); |
| 2046 | chip->fcc = palladium_1500_data.fcc; |
| 2047 | chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut; |
| 2048 | chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut; |
| 2049 | chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut; |
| 2050 | chip->pc_sf_lut = palladium_1500_data.pc_sf_lut; |
| 2051 | chip->rbatt_sf_lut = palladium_1500_data.rbatt_sf_lut; |
| 2052 | chip->default_rbatt_mohm |
| 2053 | = palladium_1500_data.default_rbatt_mohm; |
| 2054 | chip->delta_rbatt_mohm |
| 2055 | = palladium_1500_data.delta_rbatt_mohm; |
| 2056 | } |
| 2057 | return 0; |
| 2058 | } |
| 2059 | #else |
| 2060 | #define PALLADIUM_ID_MIN 0x7F40 |
| 2061 | #define PALLADIUM_ID_MAX 0x7F5A |
| 2062 | #define DESAY_5200_ID_MIN 0x7F7F |
| 2063 | #define DESAY_5200_ID_MAX 0x802F |
| 2064 | static int set_battery_data(struct pm8921_bms_chip *chip) |
| 2065 | { |
| 2066 | int64_t battery_id; |
| 2067 | |
| 2068 | battery_id = read_battery_id(chip); |
| 2069 | |
| 2070 | if (battery_id < 0) { |
| 2071 | pr_err("cannot read battery id err = %lld\n", battery_id); |
| 2072 | return battery_id; |
| 2073 | } |
| 2074 | |
| 2075 | if (is_between(PALLADIUM_ID_MIN, PALLADIUM_ID_MAX, battery_id)) { |
| 2076 | chip->fcc = palladium_1500_data.fcc; |
| 2077 | chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut; |
| 2078 | chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut; |
| 2079 | chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut; |
| 2080 | chip->pc_sf_lut = palladium_1500_data.pc_sf_lut; |
| 2081 | chip->rbatt_sf_lut = palladium_1500_data.rbatt_sf_lut; |
| 2082 | chip->default_rbatt_mohm |
| 2083 | = palladium_1500_data.default_rbatt_mohm; |
| 2084 | chip->delta_rbatt_mohm |
| 2085 | = palladium_1500_data.delta_rbatt_mohm; |
| 2086 | return 0; |
| 2087 | } else if (is_between(DESAY_5200_ID_MIN, DESAY_5200_ID_MAX, |
| 2088 | battery_id)) { |
| 2089 | chip->fcc = desay_5200_data.fcc; |
| 2090 | chip->fcc_temp_lut = desay_5200_data.fcc_temp_lut; |
| 2091 | chip->fcc_sf_lut = desay_5200_data.fcc_sf_lut; |
| 2092 | chip->pc_temp_ocv_lut = desay_5200_data.pc_temp_ocv_lut; |
| 2093 | chip->pc_sf_lut = desay_5200_data.pc_sf_lut; |
| 2094 | chip->rbatt_sf_lut = desay_5200_data.rbatt_sf_lut; |
| 2095 | chip->default_rbatt_mohm = desay_5200_data.default_rbatt_mohm; |
| 2096 | chip->delta_rbatt_mohm = desay_5200_data.delta_rbatt_mohm; |
| 2097 | return 0; |
| 2098 | } else { |
| 2099 | pr_warn("invalid battery id, palladium 1500 assumed batt_id %llx\n", |
| 2100 | battery_id); |
| 2101 | chip->fcc = palladium_1500_data.fcc; |
| 2102 | chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut; |
| 2103 | chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut; |
| 2104 | chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut; |
| 2105 | chip->pc_sf_lut = palladium_1500_data.pc_sf_lut; |
| 2106 | chip->rbatt_sf_lut = palladium_1500_data.rbatt_sf_lut; |
| 2107 | chip->default_rbatt_mohm |
| 2108 | = palladium_1500_data.default_rbatt_mohm; |
| 2109 | chip->delta_rbatt_mohm |
| 2110 | = palladium_1500_data.delta_rbatt_mohm; |
| 2111 | return 0; |
| 2112 | } |
| 2113 | } |
| 2114 | #endif |
| 2115 | |
| 2116 | enum bms_request_operation { |
| 2117 | CALC_RBATT, |
| 2118 | CALC_FCC, |
| 2119 | CALC_PC, |
| 2120 | CALC_SOC, |
| 2121 | CALIB_HKADC, |
| 2122 | CALIB_CCADC, |
| 2123 | STOP_OCV, |
| 2124 | START_OCV, |
| 2125 | GET_VBAT_VSENSE_SIMULTANEOUS, |
| 2126 | }; |
| 2127 | |
| 2128 | static int test_batt_temp = 5; |
| 2129 | static int test_chargecycle = 150; |
| 2130 | static int test_ocv = 3900000; |
| 2131 | enum { |
| 2132 | TEST_BATT_TEMP, |
| 2133 | TEST_CHARGE_CYCLE, |
| 2134 | TEST_OCV, |
| 2135 | }; |
| 2136 | static int get_test_param(void *data, u64 * val) |
| 2137 | { |
| 2138 | switch ((int)data) { |
| 2139 | case TEST_BATT_TEMP: |
| 2140 | *val = test_batt_temp; |
| 2141 | break; |
| 2142 | case TEST_CHARGE_CYCLE: |
| 2143 | *val = test_chargecycle; |
| 2144 | break; |
| 2145 | case TEST_OCV: |
| 2146 | *val = test_ocv; |
| 2147 | break; |
| 2148 | default: |
| 2149 | return -EINVAL; |
| 2150 | } |
| 2151 | return 0; |
| 2152 | } |
| 2153 | static int set_test_param(void *data, u64 val) |
| 2154 | { |
| 2155 | switch ((int)data) { |
| 2156 | case TEST_BATT_TEMP: |
| 2157 | test_batt_temp = (int)val; |
| 2158 | break; |
| 2159 | case TEST_CHARGE_CYCLE: |
| 2160 | test_chargecycle = (int)val; |
| 2161 | break; |
| 2162 | case TEST_OCV: |
| 2163 | test_ocv = (int)val; |
| 2164 | break; |
| 2165 | default: |
| 2166 | return -EINVAL; |
| 2167 | } |
| 2168 | return 0; |
| 2169 | } |
| 2170 | DEFINE_SIMPLE_ATTRIBUTE(temp_fops, get_test_param, set_test_param, "%llu\n"); |
| 2171 | |
| 2172 | static int get_calc(void *data, u64 * val) |
| 2173 | { |
| 2174 | int param = (int)data; |
| 2175 | int ret = 0; |
| 2176 | int ibat_ua, vbat_uv; |
| 2177 | struct pm8921_soc_params raw; |
| 2178 | struct pm8921_rbatt_params rraw; |
| 2179 | |
| 2180 | read_soc_params_raw(the_chip, &raw); |
| 2181 | read_rbatt_params_raw(the_chip, &rraw); |
| 2182 | |
| 2183 | *val = 0; |
| 2184 | |
| 2185 | |
| 2186 | switch (param) { |
| 2187 | case CALC_RBATT: |
| 2188 | *val = calculate_rbatt_resume(the_chip, &rraw); |
| 2189 | break; |
| 2190 | case CALC_FCC: |
| 2191 | *val = calculate_fcc_uah(the_chip, test_batt_temp, |
| 2192 | test_chargecycle); |
| 2193 | break; |
| 2194 | case CALC_PC: |
| 2195 | *val = calculate_pc(the_chip, test_ocv, test_batt_temp, |
| 2196 | test_chargecycle); |
| 2197 | break; |
| 2198 | case CALC_SOC: |
| 2199 | *val = calculate_state_of_charge(the_chip, &raw, |
| 2200 | test_batt_temp, test_chargecycle, 0); |
| 2201 | break; |
| 2202 | case CALIB_HKADC: |
| 2203 | |
| 2204 | *val = 0; |
| 2205 | calib_hkadc(the_chip); |
| 2206 | break; |
| 2207 | case CALIB_CCADC: |
| 2208 | |
| 2209 | *val = 0; |
| 2210 | pm8xxx_calib_ccadc(); |
| 2211 | break; |
| 2212 | case GET_VBAT_VSENSE_SIMULTANEOUS: |
| 2213 | |
| 2214 | *val = |
| 2215 | pm8921_bms_get_simultaneous_battery_voltage_and_current( |
| 2216 | &ibat_ua, |
| 2217 | &vbat_uv); |
| 2218 | break; |
| 2219 | default: |
| 2220 | ret = -EINVAL; |
| 2221 | } |
| 2222 | return ret; |
| 2223 | } |
| 2224 | |
| 2225 | static int set_calc(void *data, u64 val) |
| 2226 | { |
| 2227 | int param = (int)data; |
| 2228 | int ret = 0; |
| 2229 | |
| 2230 | switch (param) { |
| 2231 | case STOP_OCV: |
| 2232 | pm8921_bms_stop_ocv_updates(); |
| 2233 | break; |
| 2234 | case START_OCV: |
| 2235 | pm8921_bms_start_ocv_updates(); |
| 2236 | break; |
| 2237 | default: |
| 2238 | ret = -EINVAL; |
| 2239 | } |
| 2240 | return ret; |
| 2241 | } |
| 2242 | DEFINE_SIMPLE_ATTRIBUTE(calc_fops, get_calc, set_calc, "%llu\n"); |
| 2243 | |
| 2244 | static int get_reading(void *data, u64 * val) |
| 2245 | { |
| 2246 | int param = (int)data; |
| 2247 | int ret = 0; |
| 2248 | struct pm8921_soc_params raw; |
| 2249 | struct pm8921_rbatt_params rraw; |
| 2250 | |
| 2251 | read_soc_params_raw(the_chip, &raw); |
| 2252 | read_rbatt_params_raw(the_chip, &rraw); |
| 2253 | |
| 2254 | *val = 0; |
| 2255 | |
| 2256 | switch (param) { |
| 2257 | case CC_MSB: |
| 2258 | case CC_LSB: |
| 2259 | *val = raw.cc; |
| 2260 | break; |
| 2261 | case LAST_GOOD_OCV_VALUE: |
| 2262 | *val = raw.last_good_ocv_uv; |
| 2263 | break; |
| 2264 | case VBATT_FOR_RBATT: |
| 2265 | *val = rraw.vbatt_for_rbatt_uv; |
| 2266 | break; |
| 2267 | case VSENSE_FOR_RBATT: |
| 2268 | *val = rraw.vsense_for_rbatt_uv; |
| 2269 | break; |
| 2270 | case OCV_FOR_RBATT: |
| 2271 | *val = rraw.ocv_for_rbatt_uv; |
| 2272 | break; |
| 2273 | case VSENSE_AVG: |
| 2274 | read_vsense_avg(the_chip, (uint *)val); |
| 2275 | break; |
| 2276 | default: |
| 2277 | ret = -EINVAL; |
| 2278 | } |
| 2279 | return ret; |
| 2280 | } |
| 2281 | DEFINE_SIMPLE_ATTRIBUTE(reading_fops, get_reading, NULL, "%lld\n"); |
| 2282 | |
| 2283 | static int get_rt_status(void *data, u64 * val) |
| 2284 | { |
| 2285 | int i = (int)data; |
| 2286 | int ret; |
| 2287 | |
| 2288 | |
| 2289 | ret = pm_bms_get_rt_status(the_chip, i); |
| 2290 | *val = ret; |
| 2291 | return 0; |
| 2292 | } |
| 2293 | DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n"); |
| 2294 | |
| 2295 | static int get_reg(void *data, u64 * val) |
| 2296 | { |
| 2297 | int addr = (int)data; |
| 2298 | int ret; |
| 2299 | u8 temp; |
| 2300 | |
| 2301 | ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp); |
| 2302 | if (ret) { |
| 2303 | pr_err("pm8xxx_readb to %x value = %d errored = %d\n", |
| 2304 | addr, temp, ret); |
| 2305 | return -EAGAIN; |
| 2306 | } |
| 2307 | *val = temp; |
| 2308 | return 0; |
| 2309 | } |
| 2310 | |
| 2311 | static int set_reg(void *data, u64 val) |
| 2312 | { |
| 2313 | int addr = (int)data; |
| 2314 | int ret; |
| 2315 | u8 temp; |
| 2316 | |
| 2317 | temp = (u8) val; |
| 2318 | ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp); |
| 2319 | if (ret) { |
| 2320 | pr_err("pm8xxx_writeb to %x value = %d errored = %d\n", |
| 2321 | addr, temp, ret); |
| 2322 | return -EAGAIN; |
| 2323 | } |
| 2324 | return 0; |
| 2325 | } |
| 2326 | DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n"); |
| 2327 | |
| 2328 | static void dump_all(void) |
| 2329 | { |
| 2330 | u64 val; |
| 2331 | |
| 2332 | get_reg((void *)BMS_CONTROL, &val); |
| 2333 | pr_info("BMS_CONTROL = 0x%02llx\n", val); |
| 2334 | get_reg((void *)BMS_OUTPUT0, &val); |
| 2335 | pr_info("BMS_OUTPUT0 = 0x%02llx\n", val); |
| 2336 | get_reg((void *)BMS_OUTPUT1, &val); |
| 2337 | pr_info("BMS_OUTPUT1 = 0x%02llx\n", val); |
| 2338 | get_reg((void *)BMS_TOLERANCES, &val); |
| 2339 | pr_info("BMS_TOLERANCES = 0x%02llx\n", val); |
| 2340 | get_reg((void *)BMS_TEST1, &val); |
| 2341 | pr_info("BMS_TEST1 = 0x%02llx\n", val); |
| 2342 | get_reg((void *)OCV_UPDATE_STORAGE, &val); |
| 2343 | pr_info("OCV_UPDATE_STORAGE = 0x%02llx\n", val); |
| 2344 | |
| 2345 | |
| 2346 | get_reading((void *)CC_MSB, &val); |
| 2347 | pr_info("read_cc = 0x%lld\n", val); |
| 2348 | get_reading((void *)LAST_GOOD_OCV_VALUE, &val); |
| 2349 | pr_info("last_good_ocv = 0x%lld\n", val); |
| 2350 | get_reading((void *)VBATT_FOR_RBATT, &val); |
| 2351 | pr_info("vbatt_for_rbatt = 0x%lld\n", val); |
| 2352 | get_reading((void *)VSENSE_FOR_RBATT, &val); |
| 2353 | pr_info("vsense_for_rbatt = 0x%lld\n", val); |
| 2354 | get_reading((void *)OCV_FOR_RBATT, &val); |
| 2355 | pr_info("ocv_for_rbatt = 0x%lld\n", val); |
| 2356 | get_reading((void *)VSENSE_AVG, &val); |
| 2357 | pr_info("vsense_avg = 0x%lld\n", val); |
| 2358 | |
| 2359 | |
| 2360 | pr_info("BMS irq: %d%d%d%d%d%d%d\n", |
| 2361 | pm_bms_get_rt_status(the_chip, PM8921_BMS_SBI_WRITE_OK), |
| 2362 | pm_bms_get_rt_status(the_chip, PM8921_BMS_CC_THR), |
| 2363 | pm_bms_get_rt_status(the_chip, PM8921_BMS_VSENSE_THR), |
| 2364 | pm_bms_get_rt_status(the_chip, PM8921_BMS_VSENSE_FOR_R), |
| 2365 | pm_bms_get_rt_status(the_chip, PM8921_BMS_OCV_FOR_R), |
| 2366 | pm_bms_get_rt_status(the_chip, PM8921_BMS_GOOD_OCV), |
| 2367 | pm_bms_get_rt_status(the_chip, PM8921_BMS_VSENSE_AVG)); |
| 2368 | |
| 2369 | pm8xxx_ccadc_dump_all(); |
| 2370 | } |
| 2371 | |
| 2372 | inline int pm8921_bms_dump_all(void) |
| 2373 | { |
| 2374 | if (!the_chip) { |
| 2375 | pr_err("called before init\n"); |
| 2376 | return -EINVAL; |
| 2377 | } |
| 2378 | dump_all(); |
| 2379 | return 0; |
| 2380 | } |
| 2381 | EXPORT_SYMBOL(pm8921_bms_dump_all); |
| 2382 | |
| 2383 | int pm8921_bms_get_attr_text(char *buf, int size) |
| 2384 | { |
| 2385 | struct pm8921_soc_params raw; |
| 2386 | struct pm8921_rbatt_params rraw; |
| 2387 | unsigned long flags; |
| 2388 | int len = 0; |
| 2389 | u64 val = 0; |
| 2390 | int cc_uah, fcc_uah, unusable_charge_uah, remaining_charge_uah; |
| 2391 | int chargecycles; |
| 2392 | int soc_rbatt, rbatt; |
| 2393 | int batt_temp, rc; |
| 2394 | struct pm8xxx_adc_chan_result result; |
| 2395 | |
| 2396 | if (!the_chip) { |
| 2397 | pr_err("driver not initialized\n"); |
| 2398 | return 0; |
| 2399 | } |
| 2400 | |
| 2401 | get_reg((void *)BMS_CONTROL, &val); |
| 2402 | len += scnprintf(buf + len, size - len, |
| 2403 | "BMS_CONTROL: 0x%02llx;\n", val); |
| 2404 | get_reg((void *)BMS_OUTPUT0, &val); |
| 2405 | len += scnprintf(buf + len, size - len, |
| 2406 | "BMS_OUTPUT0: 0x%02llx;\n", val); |
| 2407 | get_reg((void *)BMS_OUTPUT1, &val); |
| 2408 | len += scnprintf(buf + len, size - len, |
| 2409 | "BMS_OUTPUT1: 0x%02llx;\n", val); |
| 2410 | get_reg((void *)BMS_TOLERANCES, &val); |
| 2411 | len += scnprintf(buf + len, size - len, |
| 2412 | "BMS_TOLERANCES: 0x%02llx;\n", val); |
| 2413 | get_reg((void *)BMS_TEST1, &val); |
| 2414 | len += scnprintf(buf + len, size - len, |
| 2415 | "BMS_TEST1: 0x%02llx;\n", val); |
| 2416 | get_reg((void *)OCV_UPDATE_STORAGE, &val); |
| 2417 | len += scnprintf(buf + len, size - len, |
| 2418 | "OCV_UPDATE_STORAGE: 0x%02llx;\n", val); |
| 2419 | |
| 2420 | len += scnprintf(buf + len, size - len, |
| 2421 | "bms_discharge_soc: %d;\n", bms_discharge_percent); |
| 2422 | len += scnprintf(buf + len, size - len, |
| 2423 | "is_ocv_update_start: %d;\n", is_ocv_update_start); |
| 2424 | |
| 2425 | read_soc_params_raw(the_chip, &raw); |
| 2426 | read_rbatt_params_raw(the_chip, &rraw); |
| 2427 | |
| 2428 | len += scnprintf(buf + len, size - len, |
| 2429 | "OCV_FOR_RBATT_RAW: 0x%x;\n", rraw.ocv_for_rbatt_raw); |
| 2430 | len += scnprintf(buf + len, size - len, |
| 2431 | "VBATT_FOR_RBATT_RAW: 0x%x;\n", rraw.vbatt_for_rbatt_raw); |
| 2432 | len += scnprintf(buf + len, size - len, |
| 2433 | "VSENSE_FOR_RBATT_RAW: 0x%x;\n", rraw.vsense_for_rbatt_raw); |
| 2434 | len += scnprintf(buf + len, size - len, |
| 2435 | "LAST_GOOD_OCV_RAW: 0x%x;\n", raw.last_good_ocv_raw); |
| 2436 | len += scnprintf(buf + len, size - len, |
| 2437 | "CC_RAW: 0x%x;\n", raw.cc); |
| 2438 | |
| 2439 | len += scnprintf(buf + len, size - len, |
| 2440 | "ocv_for_rbatt_uv: %d;\n", rraw.ocv_for_rbatt_uv); |
| 2441 | len += scnprintf(buf + len, size - len, |
| 2442 | "vbatt_for_rbatt_uv: %d;\n", rraw.vbatt_for_rbatt_uv); |
| 2443 | len += scnprintf(buf + len, size - len, |
| 2444 | "vsense_for_rbatt_uv: %d;\n", rraw.vsense_for_rbatt_uv); |
| 2445 | len += scnprintf(buf + len, size - len, |
| 2446 | "last_good_ocv_uv: %d;\n", raw.last_good_ocv_uv); |
| 2447 | |
| 2448 | rc = pm8xxx_adc_read(the_chip->batt_temp_channel, &result); |
| 2449 | if (rc) { |
| 2450 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 2451 | the_chip->batt_temp_channel, rc); |
| 2452 | return len; |
| 2453 | } |
| 2454 | batt_temp = (int)result.physical; |
| 2455 | chargecycles = last_chargecycles; |
| 2456 | fcc_uah = calculate_fcc_uah(the_chip, batt_temp, chargecycles); |
| 2457 | remaining_charge_uah = calculate_remaining_charge_uah(the_chip, &raw, |
| 2458 | fcc_uah, batt_temp, chargecycles); |
| 2459 | spin_lock_irqsave(&the_chip->bms_100_lock, flags); |
| 2460 | calculate_cc_uah(the_chip, raw.cc, &cc_uah); |
| 2461 | spin_unlock_irqrestore(&the_chip->bms_100_lock, flags); |
| 2462 | soc_rbatt = ((remaining_charge_uah - cc_uah) * 100) / fcc_uah; |
| 2463 | if (soc_rbatt < 0) |
| 2464 | soc_rbatt = 0; |
| 2465 | rbatt = get_rbatt(the_chip, soc_rbatt, batt_temp); |
| 2466 | unusable_charge_uah = calculate_unusable_charge_uah(the_chip, rbatt, |
| 2467 | fcc_uah, batt_temp, chargecycles); |
| 2468 | len += scnprintf(buf + len, size - len, |
| 2469 | "rbatt(milliOhms): %d;\n", bms_dbg.rbatt); |
| 2470 | len += scnprintf(buf + len, size - len, |
| 2471 | "rbatt_scalefactor: %d;\n", bms_dbg.rbatt_sf); |
| 2472 | len += scnprintf(buf + len, size - len, |
| 2473 | "soc_rbatt(%%): %d;\n", bms_dbg.soc_rbatt); |
| 2474 | len += scnprintf(buf + len, size - len, |
| 2475 | "last_rbatt(%%): %d;\n", last_rbatt); |
| 2476 | len += scnprintf(buf + len, size - len, |
| 2477 | "voltage_unusable_uv(uV): %d;\n", bms_dbg.voltage_unusable_uv); |
| 2478 | len += scnprintf(buf + len, size - len, |
| 2479 | "pc_unusable(%%): %d;\n", bms_dbg.pc_unusable); |
| 2480 | len += scnprintf(buf + len, size - len, |
| 2481 | "rc_pc(%%): %d;\n", bms_dbg.rc_pc); |
| 2482 | len += scnprintf(buf + len, size - len, |
| 2483 | "scalefactor(): %d;\n", bms_dbg.scalefactor); |
| 2484 | len += scnprintf(buf + len, size - len, |
| 2485 | "fcc(uAh): %d;\n", fcc_uah); |
| 2486 | len += scnprintf(buf + len, size - len, |
| 2487 | "unusable_charge(uAh): %d;\n", unusable_charge_uah); |
| 2488 | len += scnprintf(buf + len, size - len, |
| 2489 | "remaining_charge(uAh): %d;\n", remaining_charge_uah); |
| 2490 | len += scnprintf(buf + len, size - len, |
| 2491 | "cc(uAh): %d;\n", cc_uah); |
| 2492 | len += scnprintf(buf + len, size - len, |
| 2493 | "chargecycles: %d;\n", chargecycles); |
| 2494 | len += scnprintf(buf + len, size - len, |
| 2495 | "start_percent: %d;\n", the_chip->start_percent); |
| 2496 | len += scnprintf(buf + len, size - len, |
| 2497 | "end_percent: %d;\n", the_chip->end_percent); |
| 2498 | |
| 2499 | |
| 2500 | len += pm8xxx_ccadc_get_attr_text(buf + len, size - len); |
| 2501 | |
| 2502 | return len; |
| 2503 | } |
| 2504 | EXPORT_SYMBOL(pm8921_bms_get_attr_text); |
| 2505 | |
| 2506 | static void create_debugfs_entries(struct pm8921_bms_chip *chip) |
| 2507 | { |
| 2508 | int i; |
| 2509 | |
| 2510 | chip->dent = debugfs_create_dir("pm8921-bms", NULL); |
| 2511 | |
| 2512 | if (IS_ERR(chip->dent)) { |
| 2513 | pr_err("pmic bms couldnt create debugfs dir\n"); |
| 2514 | return; |
| 2515 | } |
| 2516 | |
| 2517 | debugfs_create_file("BMS_CONTROL", 0644, chip->dent, |
| 2518 | (void *)BMS_CONTROL, ®_fops); |
| 2519 | debugfs_create_file("BMS_OUTPUT0", 0644, chip->dent, |
| 2520 | (void *)BMS_OUTPUT0, ®_fops); |
| 2521 | debugfs_create_file("BMS_OUTPUT1", 0644, chip->dent, |
| 2522 | (void *)BMS_OUTPUT1, ®_fops); |
| 2523 | debugfs_create_file("BMS_TEST1", 0644, chip->dent, |
| 2524 | (void *)BMS_TEST1, ®_fops); |
| 2525 | |
| 2526 | debugfs_create_file("test_batt_temp", 0644, chip->dent, |
| 2527 | (void *)TEST_BATT_TEMP, &temp_fops); |
| 2528 | debugfs_create_file("test_chargecycle", 0644, chip->dent, |
| 2529 | (void *)TEST_CHARGE_CYCLE, &temp_fops); |
| 2530 | debugfs_create_file("test_ocv", 0644, chip->dent, |
| 2531 | (void *)TEST_OCV, &temp_fops); |
| 2532 | |
| 2533 | debugfs_create_file("read_cc", 0644, chip->dent, |
| 2534 | (void *)CC_MSB, &reading_fops); |
| 2535 | debugfs_create_file("read_last_good_ocv", 0644, chip->dent, |
| 2536 | (void *)LAST_GOOD_OCV_VALUE, &reading_fops); |
| 2537 | debugfs_create_file("read_vbatt_for_rbatt", 0644, chip->dent, |
| 2538 | (void *)VBATT_FOR_RBATT, &reading_fops); |
| 2539 | debugfs_create_file("read_vsense_for_rbatt", 0644, chip->dent, |
| 2540 | (void *)VSENSE_FOR_RBATT, &reading_fops); |
| 2541 | debugfs_create_file("read_ocv_for_rbatt", 0644, chip->dent, |
| 2542 | (void *)OCV_FOR_RBATT, &reading_fops); |
| 2543 | debugfs_create_file("read_vsense_avg", 0644, chip->dent, |
| 2544 | (void *)VSENSE_AVG, &reading_fops); |
| 2545 | |
| 2546 | debugfs_create_file("show_rbatt", 0644, chip->dent, |
| 2547 | (void *)CALC_RBATT, &calc_fops); |
| 2548 | debugfs_create_file("show_fcc", 0644, chip->dent, |
| 2549 | (void *)CALC_FCC, &calc_fops); |
| 2550 | debugfs_create_file("show_pc", 0644, chip->dent, |
| 2551 | (void *)CALC_PC, &calc_fops); |
| 2552 | debugfs_create_file("show_soc", 0644, chip->dent, |
| 2553 | (void *)CALC_SOC, &calc_fops); |
| 2554 | debugfs_create_file("calib_hkadc", 0644, chip->dent, |
| 2555 | (void *)CALIB_HKADC, &calc_fops); |
| 2556 | debugfs_create_file("calib_ccadc", 0644, chip->dent, |
| 2557 | (void *)CALIB_CCADC, &calc_fops); |
| 2558 | debugfs_create_file("stop_ocv", 0644, chip->dent, |
| 2559 | (void *)STOP_OCV, &calc_fops); |
| 2560 | debugfs_create_file("start_ocv", 0644, chip->dent, |
| 2561 | (void *)START_OCV, &calc_fops); |
| 2562 | |
| 2563 | debugfs_create_file("simultaneous", 0644, chip->dent, |
| 2564 | (void *)GET_VBAT_VSENSE_SIMULTANEOUS, &calc_fops); |
| 2565 | |
| 2566 | for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) { |
| 2567 | if (chip->pmic_bms_irq[bms_irq_data[i].irq_id]) |
| 2568 | debugfs_create_file(bms_irq_data[i].name, 0444, |
| 2569 | chip->dent, |
| 2570 | (void *)bms_irq_data[i].irq_id, |
| 2571 | &rt_fops); |
| 2572 | } |
| 2573 | } |
| 2574 | |
| 2575 | static int dump_cc_uah(void) |
| 2576 | { |
| 2577 | unsigned long flags; |
| 2578 | struct pm8921_soc_params raw; |
| 2579 | int cc_uah; |
| 2580 | |
| 2581 | if (!the_chip) { |
| 2582 | pr_err("driver not initialized\n"); |
| 2583 | return 0; |
| 2584 | } |
| 2585 | read_soc_params_raw(the_chip, &raw); |
| 2586 | |
| 2587 | spin_lock_irqsave(&the_chip->bms_100_lock, flags); |
| 2588 | |
| 2589 | calculate_cc_uah(the_chip, raw.cc, &cc_uah); |
| 2590 | pr_info("cc_uah = %duAh, raw->cc = %x," |
| 2591 | " cc = %lld after subtracting %d\n", |
| 2592 | cc_uah, raw.cc, |
| 2593 | (int64_t)raw.cc - the_chip->cc_reading_at_100, |
| 2594 | the_chip->cc_reading_at_100); |
| 2595 | spin_unlock_irqrestore(&the_chip->bms_100_lock, flags); |
| 2596 | return cc_uah; |
| 2597 | } |
| 2598 | |
| 2599 | int prev_cc_uah = 0; |
| 2600 | static int pm8921_bms_suspend(struct device *dev) |
| 2601 | { |
| 2602 | u64 val; |
| 2603 | int rc; |
| 2604 | struct pm8xxx_adc_chan_result result; |
| 2605 | struct pm8921_bms_chip *chip = dev_get_drvdata(dev); |
| 2606 | struct pm8921_soc_params raw; |
| 2607 | int fcc_uah; |
| 2608 | int remaining_charge_uah; |
| 2609 | int cc_uah; |
| 2610 | |
| 2611 | chip->batt_temp_suspend = 0; |
| 2612 | rc = pm8xxx_adc_read(chip->batt_temp_channel, &result); |
| 2613 | if (rc) { |
| 2614 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 2615 | chip->batt_temp_channel, rc); |
| 2616 | } |
| 2617 | chip->batt_temp_suspend = (int)result.physical; |
| 2618 | read_soc_params_raw(chip, &raw); |
| 2619 | |
| 2620 | fcc_uah = calculate_fcc_uah(chip, |
| 2621 | chip->batt_temp_suspend, last_chargecycles); |
| 2622 | pr_debug("FCC = %uuAh batt_temp = %d, cycles = %d\n", |
| 2623 | fcc_uah, chip->batt_temp_suspend, last_chargecycles); |
| 2624 | |
| 2625 | remaining_charge_uah = calculate_remaining_charge_uah(chip, &raw, |
| 2626 | fcc_uah, chip->batt_temp_suspend, |
| 2627 | last_chargecycles); |
| 2628 | pr_debug("RC = %uuAh\n", remaining_charge_uah); |
| 2629 | |
| 2630 | |
| 2631 | calculate_cc_uah(chip, raw.cc, &cc_uah); |
| 2632 | pr_debug("cc_uah = %duAh raw->cc = %x cc = %lld after subtracting %d\n", |
| 2633 | cc_uah, raw.cc, |
| 2634 | (int64_t)raw.cc - chip->cc_reading_at_100, |
| 2635 | chip->cc_reading_at_100); |
| 2636 | chip->soc_rbatt_suspend = ((remaining_charge_uah - cc_uah) * 100) |
| 2637 | / fcc_uah; |
| 2638 | |
| 2639 | dump_cc_uah(); |
| 2640 | get_reg((void *)BMS_TOLERANCES, &val); |
| 2641 | pr_info("BMS_TOLERANCES = 0x%02llx\n", val); |
| 2642 | return 0; |
| 2643 | } |
| 2644 | |
| 2645 | #define DELTA_RBATT_PERCENT 10 |
| 2646 | static int pm8921_bms_resume(struct device *dev) |
| 2647 | { |
| 2648 | u64 val; |
| 2649 | struct pm8921_rbatt_params raw; |
| 2650 | struct pm8921_bms_chip *chip = dev_get_drvdata(dev); |
| 2651 | int rbatt; |
| 2652 | int expected_rbatt; |
| 2653 | int scalefactor; |
| 2654 | int delta_rbatt; |
| 2655 | |
| 2656 | read_rbatt_params_raw(chip, &raw); |
| 2657 | rbatt = calculate_rbatt_resume(chip, &raw); |
| 2658 | |
| 2659 | if (rbatt < 0) |
| 2660 | return 0; |
| 2661 | |
| 2662 | expected_rbatt |
| 2663 | = (last_rbatt < 0) ? chip->default_rbatt_mohm : last_rbatt; |
| 2664 | |
| 2665 | if (chip->rbatt_sf_lut) { |
| 2666 | scalefactor = interpolate_scalingfactor(chip, |
| 2667 | chip->rbatt_sf_lut, |
| 2668 | chip->batt_temp_suspend / 10, |
| 2669 | chip->soc_rbatt_suspend); |
| 2670 | rbatt = rbatt * 100 / scalefactor; |
| 2671 | } |
| 2672 | |
| 2673 | delta_rbatt = expected_rbatt - rbatt; |
| 2674 | if (delta_rbatt) |
| 2675 | delta_rbatt = -delta_rbatt; |
| 2676 | if (delta_rbatt * 100 <= DELTA_RBATT_PERCENT * expected_rbatt) |
| 2677 | last_rbatt = rbatt; |
| 2678 | |
| 2679 | dump_cc_uah(); |
| 2680 | get_reg((void *)BMS_TOLERANCES, &val); |
| 2681 | pr_info("last_rbatt:%d , BMS_TOLERANCES = 0x%02llx\n", last_rbatt, val); |
| 2682 | return 0; |
| 2683 | } |
| 2684 | |
| 2685 | static const struct dev_pm_ops pm8921_bms_pm_ops = { |
| 2686 | .suspend = pm8921_bms_suspend, |
| 2687 | .resume = pm8921_bms_resume, |
| 2688 | }; |
| 2689 | |
| 2690 | #define REG_SBI_CONFIG 0x04F |
| 2691 | #define PAGE3_ENABLE_MASK 0x6 |
| 2692 | #define PROGRAM_REV_MASK 0x0F |
| 2693 | #define PROGRAM_REV 0x9 |
| 2694 | static int read_ocv_trim(struct pm8921_bms_chip *chip) |
| 2695 | { |
| 2696 | int rc; |
| 2697 | u8 reg, sbi_config; |
| 2698 | |
| 2699 | rc = pm8xxx_readb(chip->dev->parent, REG_SBI_CONFIG, &sbi_config); |
| 2700 | if (rc) { |
| 2701 | pr_err("error = %d reading sbi config reg\n", rc); |
| 2702 | return rc; |
| 2703 | } |
| 2704 | |
| 2705 | reg = sbi_config | PAGE3_ENABLE_MASK; |
| 2706 | rc = pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, reg); |
| 2707 | if (rc) { |
| 2708 | pr_err("error = %d writing sbi config reg\n", rc); |
| 2709 | return rc; |
| 2710 | } |
| 2711 | |
| 2712 | rc = pm8xxx_readb(chip->dev->parent, TEST_PROGRAM_REV, ®); |
| 2713 | if (rc) |
| 2714 | pr_err("Error %d reading %d addr %d\n", |
| 2715 | rc, reg, TEST_PROGRAM_REV); |
| 2716 | pr_info("program rev reg is 0x%x\n", reg); |
| 2717 | reg &= PROGRAM_REV_MASK; |
| 2718 | |
| 2719 | |
| 2720 | if (reg >= PROGRAM_REV) { |
| 2721 | chip->amux_2_trim_delta = 0; |
| 2722 | goto restore_sbi_config; |
| 2723 | } |
| 2724 | |
| 2725 | rc = pm8xxx_readb(chip->dev->parent, AMUX_TRIM_2, ®); |
| 2726 | if (rc) { |
| 2727 | pr_err("error = %d reading trim reg\n", rc); |
| 2728 | return rc; |
| 2729 | } |
| 2730 | |
| 2731 | chip->amux_2_trim_delta = abs(0x49 - reg); |
| 2732 | pr_info("trim reg=0x%x, trim delta=%d\n", reg, chip->amux_2_trim_delta); |
| 2733 | |
| 2734 | restore_sbi_config: |
| 2735 | rc = pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, sbi_config); |
| 2736 | if (rc) { |
| 2737 | pr_err("error = %d writing sbi config reg\n", rc); |
| 2738 | return rc; |
| 2739 | } |
| 2740 | |
| 2741 | return 0; |
| 2742 | } |
| 2743 | |
| 2744 | static int __devinit pm8921_bms_probe(struct platform_device *pdev) |
| 2745 | { |
| 2746 | int rc = 0; |
| 2747 | int vbatt; |
| 2748 | struct pm8921_bms_chip *chip; |
| 2749 | const struct pm8921_bms_platform_data *pdata |
| 2750 | = pdev->dev.platform_data; |
| 2751 | #ifdef CONFIG_HTC_BATT_8960 |
| 2752 | const struct pm8921_charger_batt_param *chg_batt_param; |
| 2753 | #endif |
| 2754 | |
| 2755 | pr_info("%s\n", __func__); |
| 2756 | |
| 2757 | if (!pdata) { |
| 2758 | pr_err("missing platform data\n"); |
| 2759 | return -EINVAL; |
| 2760 | } |
| 2761 | |
| 2762 | chip = kzalloc(sizeof(struct pm8921_bms_chip), GFP_KERNEL); |
| 2763 | if (!chip) { |
| 2764 | pr_err("Cannot allocate pm_bms_chip\n"); |
| 2765 | return -ENOMEM; |
| 2766 | } |
| 2767 | mutex_init(&chip->bms_output_lock); |
| 2768 | spin_lock_init(&chip->bms_100_lock); |
| 2769 | chip->dev = &pdev->dev; |
| 2770 | chip->r_sense = pdata->r_sense; |
| 2771 | chip->i_test = pdata->i_test; |
| 2772 | chip->v_failure = pdata->v_failure; |
| 2773 | chip->rconn_mohm = pdata->rconn_mohm; |
| 2774 | chip->start_percent = -EINVAL; |
| 2775 | chip->end_percent = -EINVAL; |
| 2776 | |
| 2777 | chip->batt_temp_channel = pdata->bms_cdata.batt_temp_channel; |
| 2778 | chip->vbat_channel = pdata->bms_cdata.vbat_channel; |
| 2779 | chip->ref625mv_channel = pdata->bms_cdata.ref625mv_channel; |
| 2780 | chip->ref1p25v_channel = pdata->bms_cdata.ref1p25v_channel; |
| 2781 | chip->batt_id_channel = pdata->bms_cdata.batt_id_channel; |
| 2782 | chip->revision = pm8xxx_get_revision(chip->dev->parent); |
| 2783 | INIT_WORK(&chip->calib_hkadc_work, calibrate_hkadc_work); |
| 2784 | |
| 2785 | rc = set_battery_data(chip); |
| 2786 | if (rc) { |
| 2787 | pr_err("%s bad battery data %d\n", __func__, rc); |
| 2788 | goto free_chip; |
| 2789 | } |
| 2790 | #ifdef CONFIG_HTC_BATT_8960 |
| 2791 | |
| 2792 | chg_batt_param = htc_battery_cell_get_cur_cell_charger_cdata(); |
| 2793 | if (!chg_batt_param) { |
| 2794 | chip->max_voltage_uv = pdata->max_voltage_uv; |
| 2795 | } else { |
| 2796 | chip->max_voltage_uv = chg_batt_param->max_voltage * 1000; |
| 2797 | } |
| 2798 | #else |
| 2799 | chip->max_voltage_uv = pdata->max_voltage_uv; |
| 2800 | #endif |
| 2801 | |
| 2802 | if (chip->pc_temp_ocv_lut == NULL) { |
| 2803 | pr_err("temp ocv lut table is NULL\n"); |
| 2804 | rc = -EINVAL; |
| 2805 | goto free_chip; |
| 2806 | } |
| 2807 | |
| 2808 | |
| 2809 | if (chip->default_rbatt_mohm <= 0) |
| 2810 | chip->default_rbatt_mohm = DEFAULT_RBATT_MOHMS; |
| 2811 | |
| 2812 | |
| 2813 | rc = request_irqs(chip, pdev); |
| 2814 | if (rc) { |
| 2815 | pr_err("couldn't register interrupts rc = %d\n", rc); |
| 2816 | goto free_chip; |
| 2817 | } |
| 2818 | |
| 2819 | rc = pm8921_bms_hw_init(chip); |
| 2820 | if (rc) { |
| 2821 | pr_err("couldn't init hardware rc = %d\n", rc); |
| 2822 | goto free_irqs; |
| 2823 | } |
| 2824 | |
| 2825 | platform_set_drvdata(pdev, chip); |
| 2826 | the_chip = chip; |
| 2827 | create_debugfs_entries(chip); |
| 2828 | |
| 2829 | rc = read_ocv_trim(chip); |
| 2830 | if (rc) { |
| 2831 | pr_err("couldn't adjust ocv_trim rc= %d\n", rc); |
| 2832 | goto free_irqs; |
| 2833 | } |
| 2834 | check_initial_ocv(chip); |
| 2835 | |
| 2836 | |
| 2837 | schedule_work(&chip->calib_hkadc_work); |
| 2838 | |
| 2839 | pm8921_bms_enable_irq(chip, PM8921_BMS_GOOD_OCV); |
| 2840 | pm8921_bms_enable_irq(chip, PM8921_BMS_OCV_FOR_R); |
| 2841 | |
| 2842 | get_battery_uvolts(chip, &vbatt); |
| 2843 | pr_info("OK battery_capacity_at_boot=%d volt = %d ocv = %d\n", |
| 2844 | pm8921_bms_get_percent_charge(), |
| 2845 | vbatt, last_ocv_uv); |
| 2846 | pr_info("r_sense=%u,i_test=%u,v_failure=%u\n", chip->r_sense, chip->i_test, chip->v_failure); |
| 2847 | return 0; |
| 2848 | |
| 2849 | free_irqs: |
| 2850 | free_irqs(chip); |
| 2851 | free_chip: |
| 2852 | kfree(chip); |
| 2853 | return rc; |
| 2854 | } |
| 2855 | |
| 2856 | static int __devexit pm8921_bms_remove(struct platform_device *pdev) |
| 2857 | { |
| 2858 | struct pm8921_bms_chip *chip = platform_get_drvdata(pdev); |
| 2859 | |
| 2860 | free_irqs(chip); |
| 2861 | kfree(chip->adjusted_fcc_temp_lut); |
| 2862 | platform_set_drvdata(pdev, NULL); |
| 2863 | the_chip = NULL; |
| 2864 | kfree(chip); |
| 2865 | return 0; |
| 2866 | } |
| 2867 | |
| 2868 | static struct platform_driver pm8921_bms_driver = { |
| 2869 | .probe = pm8921_bms_probe, |
| 2870 | .remove = __devexit_p(pm8921_bms_remove), |
| 2871 | .driver = { |
| 2872 | .name = PM8921_BMS_DEV_NAME, |
| 2873 | .owner = THIS_MODULE, |
| 2874 | .pm = &pm8921_bms_pm_ops, |
| 2875 | }, |
| 2876 | }; |
| 2877 | |
| 2878 | static int __init pm8921_bms_init(void) |
| 2879 | { |
| 2880 | flag_enable_bms_chg_log = |
| 2881 | (get_kernel_flag() & KERNEL_FLAG_ENABLE_BMS_CHARGER_LOG) ? 1 : 0; |
| 2882 | return platform_driver_register(&pm8921_bms_driver); |
| 2883 | } |
| 2884 | |
| 2885 | static void __exit pm8921_bms_exit(void) |
| 2886 | { |
| 2887 | platform_driver_unregister(&pm8921_bms_driver); |
| 2888 | } |
| 2889 | |
| 2890 | late_initcall(pm8921_bms_init); |
| 2891 | module_exit(pm8921_bms_exit); |
| 2892 | |
| 2893 | MODULE_LICENSE("GPL v2"); |
| 2894 | MODULE_DESCRIPTION("PMIC8921 bms driver"); |
| 2895 | MODULE_VERSION("1.0"); |
| 2896 | MODULE_ALIAS("platform:" PM8921_BMS_DEV_NAME); |