Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/moduleparam.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/mfd/pm8xxx/pm8921-bms.h> |
| 20 | #include <linux/mfd/pm8xxx/core.h> |
Siddartha Mohanadoss | 77d106e | 2011-09-20 16:25:59 -0700 | [diff] [blame] | 21 | #include <linux/mfd/pm8xxx/pm8921-adc.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/bitops.h> |
| 24 | #include <linux/debugfs.h> |
| 25 | #include <linux/slab.h> |
| 26 | |
| 27 | #define BMS_CONTROL 0x224 |
| 28 | #define BMS_OUTPUT0 0x230 |
| 29 | #define BMS_OUTPUT1 0x231 |
| 30 | #define BMS_TEST1 0x237 |
| 31 | #define CCADC_ANA_PARAM 0x240 |
| 32 | #define CCADC_DIG_PARAM 0x241 |
| 33 | #define CCADC_RSV 0x242 |
| 34 | #define CCADC_DATA0 0x244 |
| 35 | #define CCADC_DATA1 0x245 |
| 36 | #define CCADC_OFFSET_TRIM1 0x34A |
| 37 | #define CCADC_OFFSET_TRIM0 0x34B |
| 38 | |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 39 | #define ADC_ARB_SECP_CNTRL 0x190 |
| 40 | #define ADC_ARB_SECP_AMUX_CNTRL 0x191 |
| 41 | #define ADC_ARB_SECP_ANA_PARAM 0x192 |
| 42 | #define ADC_ARB_SECP_RSV 0x194 |
| 43 | #define ADC_ARB_SECP_DATA1 0x195 |
| 44 | #define ADC_ARB_SECP_DATA0 0x196 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 45 | |
| 46 | enum pmic_bms_interrupts { |
| 47 | PM8921_BMS_SBI_WRITE_OK, |
| 48 | PM8921_BMS_CC_THR, |
| 49 | PM8921_BMS_VSENSE_THR, |
| 50 | PM8921_BMS_VSENSE_FOR_R, |
| 51 | PM8921_BMS_OCV_FOR_R, |
| 52 | PM8921_BMS_GOOD_OCV, |
| 53 | PM8921_BMS_VSENSE_AVG, |
| 54 | PM_BMS_MAX_INTS, |
| 55 | }; |
| 56 | |
| 57 | /** |
| 58 | * struct pm8921_bms_chip -device information |
| 59 | * @dev: device pointer to access the parent |
| 60 | * @dent: debugfs directory |
| 61 | * @r_sense: batt sense resistance value |
| 62 | * @i_test: peak current |
| 63 | * @v_failure: battery dead voltage |
| 64 | * @fcc: battery capacity |
| 65 | * |
| 66 | */ |
| 67 | struct pm8921_bms_chip { |
| 68 | struct device *dev; |
| 69 | struct dentry *dent; |
| 70 | unsigned int r_sense; |
| 71 | unsigned int i_test; |
| 72 | unsigned int v_failure; |
| 73 | unsigned int fcc; |
| 74 | struct single_row_lut *fcc_temp_lut; |
| 75 | struct single_row_lut *fcc_sf_lut; |
| 76 | struct pc_temp_ocv_lut *pc_temp_ocv_lut; |
| 77 | struct pc_sf_lut *pc_sf_lut; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 78 | struct work_struct calib_hkadc_work; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 79 | unsigned int calib_delay_ms; |
Abhijeet Dharmapurikar | fa77f1a | 2011-06-27 21:11:51 -0700 | [diff] [blame] | 80 | unsigned int revision; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 81 | unsigned int xoadc_v0625; |
| 82 | unsigned int xoadc_v125; |
Abhijeet Dharmapurikar | c448d98 | 2011-06-28 20:48:36 -0700 | [diff] [blame] | 83 | unsigned int batt_temp_channel; |
| 84 | unsigned int vbat_channel; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 85 | unsigned int ref625mv_channel; |
| 86 | unsigned int ref1p25v_channel; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 87 | unsigned int batt_id_channel; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 88 | unsigned int pmic_bms_irq[PM_BMS_MAX_INTS]; |
| 89 | DECLARE_BITMAP(enabled_irqs, PM_BMS_MAX_INTS); |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame^] | 90 | spinlock_t bms_output_lock; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | static struct pm8921_bms_chip *the_chip; |
| 94 | |
| 95 | #define DEFAULT_RBATT_MOHMS 128 |
| 96 | #define DEFAULT_UNUSABLE_CHARGE_MAH 10 |
| 97 | #define DEFAULT_OCV_MICROVOLTS 3900000 |
| 98 | #define DEFAULT_REMAINING_CHARGE_MAH 990 |
| 99 | #define DEFAULT_COULUMB_COUNTER 0 |
| 100 | #define DEFAULT_CHARGE_CYCLES 0 |
| 101 | |
| 102 | static int last_rbatt = -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 103 | static int last_ocv_uv = -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 104 | static int last_soc = -EINVAL; |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 105 | static int last_real_fcc = -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 106 | |
| 107 | static int last_chargecycles = DEFAULT_CHARGE_CYCLES; |
| 108 | static int last_charge_increase; |
| 109 | |
| 110 | module_param(last_rbatt, int, 0644); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 111 | module_param(last_ocv_uv, int, 0644); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 112 | module_param(last_chargecycles, int, 0644); |
| 113 | module_param(last_charge_increase, int, 0644); |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 114 | module_param(last_real_fcc, int, 0644); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 115 | |
| 116 | static int pm_bms_get_rt_status(struct pm8921_bms_chip *chip, int irq_id) |
| 117 | { |
| 118 | return pm8xxx_read_irq_stat(chip->dev->parent, |
| 119 | chip->pmic_bms_irq[irq_id]); |
| 120 | } |
| 121 | |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 122 | static void pm8921_bms_enable_irq(struct pm8921_bms_chip *chip, int interrupt) |
| 123 | { |
| 124 | if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) { |
| 125 | dev_dbg(chip->dev, "%s %d\n", __func__, |
| 126 | chip->pmic_bms_irq[interrupt]); |
| 127 | enable_irq(chip->pmic_bms_irq[interrupt]); |
| 128 | } |
| 129 | } |
| 130 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 131 | static void pm8921_bms_disable_irq(struct pm8921_bms_chip *chip, int interrupt) |
| 132 | { |
| 133 | if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 134 | pr_debug("%d\n", chip->pmic_bms_irq[interrupt]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 135 | disable_irq_nosync(chip->pmic_bms_irq[interrupt]); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | static int pm_bms_masked_write(struct pm8921_bms_chip *chip, u16 addr, |
| 140 | u8 mask, u8 val) |
| 141 | { |
| 142 | int rc; |
| 143 | u8 reg; |
| 144 | |
| 145 | rc = pm8xxx_readb(chip->dev->parent, addr, ®); |
| 146 | if (rc) { |
| 147 | pr_err("read failed addr = %03X, rc = %d\n", addr, rc); |
| 148 | return rc; |
| 149 | } |
| 150 | reg &= ~mask; |
| 151 | reg |= val & mask; |
| 152 | rc = pm8xxx_writeb(chip->dev->parent, addr, reg); |
| 153 | if (rc) { |
| 154 | pr_err("write failed addr = %03X, rc = %d\n", addr, rc); |
| 155 | return rc; |
| 156 | } |
| 157 | return 0; |
| 158 | } |
| 159 | |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame^] | 160 | #define HOLD_OREG_DATA BIT(1) |
| 161 | static int pm_bms_lock_output_data(struct pm8921_bms_chip *chip) |
| 162 | { |
| 163 | int rc; |
| 164 | |
| 165 | rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA, |
| 166 | HOLD_OREG_DATA); |
| 167 | if (rc) { |
| 168 | pr_err("couldnt lock bms output rc = %d\n", rc); |
| 169 | return rc; |
| 170 | } |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static int pm_bms_unlock_output_data(struct pm8921_bms_chip *chip) |
| 175 | { |
| 176 | int rc; |
| 177 | |
| 178 | rc = pm_bms_masked_write(chip, BMS_CONTROL, HOLD_OREG_DATA, 0); |
| 179 | if (rc) { |
| 180 | pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc); |
| 181 | return rc; |
| 182 | } |
| 183 | return 0; |
| 184 | } |
| 185 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 186 | #define SELECT_OUTPUT_DATA 0x1C |
| 187 | #define SELECT_OUTPUT_TYPE_SHIFT 2 |
| 188 | #define OCV_FOR_RBATT 0x0 |
| 189 | #define VSENSE_FOR_RBATT 0x1 |
| 190 | #define VBATT_FOR_RBATT 0x2 |
| 191 | #define CC_MSB 0x3 |
| 192 | #define CC_LSB 0x4 |
| 193 | #define LAST_GOOD_OCV_VALUE 0x5 |
| 194 | #define VSENSE_AVG 0x6 |
| 195 | #define VBATT_AVG 0x7 |
| 196 | |
| 197 | static int pm_bms_read_output_data(struct pm8921_bms_chip *chip, int type, |
| 198 | int16_t *result) |
| 199 | { |
| 200 | int rc; |
| 201 | u8 reg; |
| 202 | |
| 203 | if (!result) { |
| 204 | pr_err("result pointer null\n"); |
| 205 | return -EINVAL; |
| 206 | } |
| 207 | *result = 0; |
| 208 | if (type < OCV_FOR_RBATT || type > VBATT_AVG) { |
| 209 | pr_err("invalid type %d asked to read\n", type); |
| 210 | return -EINVAL; |
| 211 | } |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame^] | 212 | |
| 213 | /* make sure the bms registers are locked */ |
| 214 | rc = pm8xxx_readb(chip->dev->parent, BMS_CONTROL, ®); |
| 215 | if (rc) { |
| 216 | pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n", |
| 217 | type, rc); |
| 218 | return rc; |
| 219 | } |
| 220 | |
| 221 | /* Output register data must be held (locked) while reading output */ |
| 222 | WARN_ON(!(reg && HOLD_OREG_DATA)); |
| 223 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 224 | rc = pm_bms_masked_write(chip, BMS_CONTROL, SELECT_OUTPUT_DATA, |
| 225 | type << SELECT_OUTPUT_TYPE_SHIFT); |
| 226 | if (rc) { |
| 227 | pr_err("fail to select %d type in BMS_CONTROL rc = %d\n", |
| 228 | type, rc); |
| 229 | return rc; |
| 230 | } |
| 231 | |
| 232 | rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT0, ®); |
| 233 | if (rc) { |
| 234 | pr_err("fail to read BMS_OUTPUT0 for type %d rc = %d\n", |
| 235 | type, rc); |
| 236 | return rc; |
| 237 | } |
| 238 | *result = reg; |
| 239 | rc = pm8xxx_readb(chip->dev->parent, BMS_OUTPUT1, ®); |
| 240 | if (rc) { |
| 241 | pr_err("fail to read BMS_OUTPUT1 for type %d rc = %d\n", |
| 242 | type, rc); |
| 243 | return rc; |
| 244 | } |
| 245 | *result |= reg << 8; |
| 246 | pr_debug("type %d result %x", type, *result); |
| 247 | return 0; |
| 248 | } |
| 249 | |
Abhijeet Dharmapurikar | d76311f | 2011-07-19 13:35:01 -0700 | [diff] [blame] | 250 | #define V_PER_BIT_MUL_FACTOR 293 |
| 251 | #define INTRINSIC_OFFSET 0x6000 |
| 252 | static int vbatt_to_microvolt(unsigned int a) |
| 253 | { |
| 254 | if (a <= INTRINSIC_OFFSET) |
| 255 | return 0; |
| 256 | |
| 257 | return (a - INTRINSIC_OFFSET) * V_PER_BIT_MUL_FACTOR; |
| 258 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 259 | |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 260 | #define XOADC_CALIB_UV 625000 |
| 261 | static int adjust_xo_reading(struct pm8921_bms_chip *chip, unsigned int uv) |
| 262 | { |
| 263 | u64 numerator = ((u64)uv - chip->xoadc_v0625) * XOADC_CALIB_UV; |
| 264 | u64 denominator = chip->xoadc_v125 - chip->xoadc_v0625; |
| 265 | |
| 266 | if (denominator == 0) |
| 267 | return uv; |
| 268 | return XOADC_CALIB_UV + div_u64(numerator, denominator); |
| 269 | } |
| 270 | |
Abhijeet Dharmapurikar | eed550b | 2011-07-18 22:35:24 -0700 | [diff] [blame] | 271 | #define CC_RESOLUTION_N_V1 1085069 |
| 272 | #define CC_RESOLUTION_D_V1 100000 |
| 273 | #define CC_RESOLUTION_N_V2 868056 |
| 274 | #define CC_RESOLUTION_D_V2 10000 |
| 275 | static s64 cc_to_microvolt_v1(s64 cc) |
| 276 | { |
| 277 | return div_s64(cc * CC_RESOLUTION_N_V1, CC_RESOLUTION_D_V1); |
| 278 | } |
| 279 | |
| 280 | static s64 cc_to_microvolt_v2(s64 cc) |
| 281 | { |
| 282 | return div_s64(cc * CC_RESOLUTION_N_V2, CC_RESOLUTION_D_V2); |
| 283 | } |
| 284 | |
| 285 | static s64 cc_to_microvolt(struct pm8921_bms_chip *chip, s64 cc) |
| 286 | { |
| 287 | /* |
| 288 | * resolution (the value of a single bit) was changed after revision 2.0 |
| 289 | * for more accurate readings |
| 290 | */ |
| 291 | return (chip->revision < PM8XXX_REVISION_8901_2p0) ? |
| 292 | cc_to_microvolt_v1((s64)cc) : |
| 293 | cc_to_microvolt_v2((s64)cc); |
| 294 | } |
| 295 | |
| 296 | #define CC_READING_TICKS 55 |
| 297 | #define SLEEP_CLK_HZ 32768 |
| 298 | #define SECONDS_PER_HOUR 3600 |
| 299 | static s64 ccmicrovolt_to_uvh(s64 cc_uv) |
| 300 | { |
| 301 | return div_s64(cc_uv * CC_READING_TICKS, |
| 302 | SLEEP_CLK_HZ * SECONDS_PER_HOUR); |
| 303 | } |
| 304 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 305 | /* returns the signed value read from the hardware */ |
| 306 | static int read_cc(struct pm8921_bms_chip *chip, int *result) |
| 307 | { |
| 308 | int rc; |
| 309 | uint16_t msw, lsw; |
| 310 | |
| 311 | rc = pm_bms_read_output_data(chip, CC_LSB, &lsw); |
| 312 | if (rc) { |
| 313 | pr_err("fail to read CC_LSB rc = %d\n", rc); |
| 314 | return rc; |
| 315 | } |
| 316 | rc = pm_bms_read_output_data(chip, CC_MSB, &msw); |
| 317 | if (rc) { |
| 318 | pr_err("fail to read CC_MSB rc = %d\n", rc); |
| 319 | return rc; |
| 320 | } |
| 321 | *result = msw << 16 | lsw; |
| 322 | pr_debug("msw = %04x lsw = %04x cc = %d\n", msw, lsw, *result); |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | static int read_last_good_ocv(struct pm8921_bms_chip *chip, uint *result) |
| 327 | { |
| 328 | int rc; |
| 329 | uint16_t reading; |
| 330 | |
| 331 | rc = pm_bms_read_output_data(chip, LAST_GOOD_OCV_VALUE, &reading); |
| 332 | if (rc) { |
| 333 | pr_err("fail to read LAST_GOOD_OCV_VALUE rc = %d\n", rc); |
| 334 | return rc; |
| 335 | } |
Abhijeet Dharmapurikar | d76311f | 2011-07-19 13:35:01 -0700 | [diff] [blame] | 336 | *result = vbatt_to_microvolt(reading); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 337 | pr_debug("raw = %04x ocv_microV = %u\n", reading, *result); |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static int read_vbatt_for_rbatt(struct pm8921_bms_chip *chip, uint *result) |
| 342 | { |
| 343 | int rc; |
| 344 | uint16_t reading; |
| 345 | |
| 346 | rc = pm_bms_read_output_data(chip, VBATT_FOR_RBATT, &reading); |
| 347 | if (rc) { |
| 348 | pr_err("fail to read VBATT_FOR_RBATT rc = %d\n", rc); |
| 349 | return rc; |
| 350 | } |
Abhijeet Dharmapurikar | d76311f | 2011-07-19 13:35:01 -0700 | [diff] [blame] | 351 | *result = vbatt_to_microvolt(reading); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 352 | pr_debug("raw = %04x vbatt_for_r_microV = %u\n", reading, *result); |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | static int read_vsense_for_rbatt(struct pm8921_bms_chip *chip, uint *result) |
| 357 | { |
| 358 | int rc; |
| 359 | uint16_t reading; |
| 360 | |
| 361 | rc = pm_bms_read_output_data(chip, VSENSE_FOR_RBATT, &reading); |
| 362 | if (rc) { |
| 363 | pr_err("fail to read VSENSE_FOR_RBATT rc = %d\n", rc); |
| 364 | return rc; |
| 365 | } |
Abhijeet Dharmapurikar | eed550b | 2011-07-18 22:35:24 -0700 | [diff] [blame] | 366 | *result = cc_to_microvolt(chip, reading); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 367 | pr_debug("raw = %04x vsense_for_r_microV = %u\n", reading, *result); |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | static int read_ocv_for_rbatt(struct pm8921_bms_chip *chip, uint *result) |
| 372 | { |
| 373 | int rc; |
| 374 | uint16_t reading; |
| 375 | |
| 376 | rc = pm_bms_read_output_data(chip, OCV_FOR_RBATT, &reading); |
| 377 | if (rc) { |
| 378 | pr_err("fail to read OCV_FOR_RBATT rc = %d\n", rc); |
| 379 | return rc; |
| 380 | } |
Abhijeet Dharmapurikar | d76311f | 2011-07-19 13:35:01 -0700 | [diff] [blame] | 381 | *result = vbatt_to_microvolt(reading); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 382 | pr_debug("read = %04x ocv_for_r_microV = %u\n", reading, *result); |
| 383 | return 0; |
| 384 | } |
| 385 | |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 386 | static int read_vsense_avg(struct pm8921_bms_chip *chip, int *result) |
| 387 | { |
| 388 | int rc; |
| 389 | int16_t reading; |
| 390 | |
| 391 | rc = pm_bms_read_output_data(chip, VSENSE_AVG, &reading); |
| 392 | if (rc) { |
| 393 | pr_err("fail to read VSENSE_AVG rc = %d\n", rc); |
| 394 | return rc; |
| 395 | } |
Abhijeet Dharmapurikar | eed550b | 2011-07-18 22:35:24 -0700 | [diff] [blame] | 396 | *result = cc_to_microvolt(chip, reading); |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 397 | pr_debug("read = %04x vsense = %d\n", reading, *result); |
| 398 | return 0; |
| 399 | } |
| 400 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 401 | static int linear_interpolate(int y0, int x0, int y1, int x1, int x) |
| 402 | { |
| 403 | if (y0 == y1 || x == x0) |
| 404 | return y0; |
| 405 | if (x1 == x0 || x == x1) |
| 406 | return y1; |
| 407 | |
| 408 | return y0 + ((y1 - y0) * (x - x0) / (x1 - x0)); |
| 409 | } |
| 410 | |
| 411 | static int interpolate_single_lut(struct single_row_lut *lut, int x) |
| 412 | { |
| 413 | int i, result; |
| 414 | |
| 415 | if (x < lut->x[0]) { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 416 | pr_debug("x %d less than known range return y = %d lut = %pS\n", |
| 417 | x, lut->y[0], lut); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 418 | return lut->y[0]; |
| 419 | } |
| 420 | if (x > lut->x[lut->cols - 1]) { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 421 | pr_debug("x %d more than known range return y = %d lut = %pS\n", |
| 422 | x, lut->y[lut->cols - 1], lut); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 423 | return lut->y[lut->cols - 1]; |
| 424 | } |
| 425 | |
| 426 | for (i = 0; i < lut->cols; i++) |
| 427 | if (x <= lut->x[i]) |
| 428 | break; |
| 429 | if (x == lut->x[i]) { |
| 430 | result = lut->y[i]; |
| 431 | } else { |
| 432 | result = linear_interpolate( |
| 433 | lut->y[i - 1], |
| 434 | lut->x[i - 1], |
| 435 | lut->y[i], |
| 436 | lut->x[i], |
| 437 | x); |
| 438 | } |
| 439 | return result; |
| 440 | } |
| 441 | |
| 442 | static int interpolate_fcc(struct pm8921_bms_chip *chip, int batt_temp) |
| 443 | { |
| 444 | return interpolate_single_lut(chip->fcc_temp_lut, batt_temp); |
| 445 | } |
| 446 | |
| 447 | static int interpolate_scalingfactor_fcc(struct pm8921_bms_chip *chip, |
| 448 | int cycles) |
| 449 | { |
| 450 | return interpolate_single_lut(chip->fcc_sf_lut, cycles); |
| 451 | } |
| 452 | |
| 453 | static int interpolate_scalingfactor_pc(struct pm8921_bms_chip *chip, |
| 454 | int cycles, int pc) |
| 455 | { |
Abhijeet Dharmapurikar | 4a4456e | 2011-09-27 15:10:38 -0700 | [diff] [blame] | 456 | int i, scalefactorrow1, scalefactorrow2, scalefactor; |
| 457 | int row1 = 0; |
| 458 | int row2 = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 459 | int rows = chip->pc_sf_lut->rows; |
| 460 | int cols = chip->pc_sf_lut->cols; |
| 461 | |
| 462 | if (pc > chip->pc_sf_lut->percent[0]) { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 463 | pr_debug("pc %d greater than known pc ranges for sfd\n", pc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 464 | row1 = 0; |
| 465 | row2 = 0; |
| 466 | } |
| 467 | if (pc < chip->pc_sf_lut->percent[rows - 1]) { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 468 | pr_debug("pc %d less than known pc ranges for sf", pc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 469 | row1 = rows - 1; |
| 470 | row2 = rows - 1; |
| 471 | } |
| 472 | for (i = 0; i < rows; i++) { |
| 473 | if (pc == chip->pc_sf_lut->percent[i]) { |
| 474 | row1 = i; |
| 475 | row2 = i; |
| 476 | break; |
| 477 | } |
| 478 | if (pc > chip->pc_sf_lut->percent[i]) { |
| 479 | row1 = i - 1; |
| 480 | row2 = i; |
| 481 | break; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | if (cycles < chip->pc_sf_lut->cycles[0]) |
| 486 | cycles = chip->pc_sf_lut->cycles[0]; |
| 487 | if (cycles > chip->pc_sf_lut->cycles[cols - 1]) |
| 488 | cycles = chip->pc_sf_lut->cycles[cols - 1]; |
| 489 | |
| 490 | for (i = 0; i < cols; i++) |
| 491 | if (cycles <= chip->pc_sf_lut->cycles[i]) |
| 492 | break; |
| 493 | if (cycles == chip->pc_sf_lut->cycles[i]) { |
| 494 | scalefactor = linear_interpolate( |
| 495 | chip->pc_sf_lut->sf[row1][i], |
| 496 | chip->pc_sf_lut->percent[row1], |
| 497 | chip->pc_sf_lut->sf[row2][i], |
| 498 | chip->pc_sf_lut->percent[row2], |
| 499 | pc); |
| 500 | return scalefactor; |
| 501 | } |
| 502 | |
| 503 | scalefactorrow1 = linear_interpolate( |
| 504 | chip->pc_sf_lut->sf[row1][i - 1], |
| 505 | chip->pc_sf_lut->cycles[i - 1], |
| 506 | chip->pc_sf_lut->sf[row1][i], |
| 507 | chip->pc_sf_lut->cycles[i], |
| 508 | cycles); |
| 509 | |
| 510 | scalefactorrow2 = linear_interpolate( |
| 511 | chip->pc_sf_lut->sf[row2][i - 1], |
| 512 | chip->pc_sf_lut->cycles[i - 1], |
| 513 | chip->pc_sf_lut->sf[row2][i], |
| 514 | chip->pc_sf_lut->cycles[i], |
| 515 | cycles); |
| 516 | |
| 517 | scalefactor = linear_interpolate( |
| 518 | scalefactorrow1, |
| 519 | chip->pc_sf_lut->percent[row1], |
| 520 | scalefactorrow2, |
| 521 | chip->pc_sf_lut->percent[row2], |
| 522 | pc); |
| 523 | |
| 524 | return scalefactor; |
| 525 | } |
| 526 | |
Abhijeet Dharmapurikar | 78d812f | 2011-07-21 19:57:21 -0700 | [diff] [blame] | 527 | static int is_between(int left, int right, int value) |
| 528 | { |
| 529 | if (left >= right && left >= value && value >= right) |
| 530 | return 1; |
| 531 | if (left <= right && left <= value && value <= right) |
| 532 | return 1; |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 537 | static int interpolate_pc(struct pm8921_bms_chip *chip, |
| 538 | int batt_temp, int ocv) |
| 539 | { |
Abhijeet Dharmapurikar | 78d812f | 2011-07-21 19:57:21 -0700 | [diff] [blame] | 540 | int i, j, pcj, pcj_minus_one, pc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 541 | int rows = chip->pc_temp_ocv_lut->rows; |
| 542 | int cols = chip->pc_temp_ocv_lut->cols; |
| 543 | |
| 544 | if (batt_temp < chip->pc_temp_ocv_lut->temp[0]) { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 545 | pr_debug("batt_temp %d < known temp range for pc\n", batt_temp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 546 | batt_temp = chip->pc_temp_ocv_lut->temp[0]; |
| 547 | } |
| 548 | if (batt_temp > chip->pc_temp_ocv_lut->temp[cols - 1]) { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 549 | pr_debug("batt_temp %d > known temp range for pc\n", batt_temp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 550 | batt_temp = chip->pc_temp_ocv_lut->temp[cols - 1]; |
| 551 | } |
| 552 | |
| 553 | for (j = 0; j < cols; j++) |
| 554 | if (batt_temp <= chip->pc_temp_ocv_lut->temp[j]) |
| 555 | break; |
| 556 | if (batt_temp == chip->pc_temp_ocv_lut->temp[j]) { |
| 557 | /* found an exact match for temp in the table */ |
| 558 | if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j]) |
| 559 | return chip->pc_temp_ocv_lut->percent[0]; |
| 560 | if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j]) |
| 561 | return chip->pc_temp_ocv_lut->percent[rows - 1]; |
| 562 | for (i = 0; i < rows; i++) { |
| 563 | if (ocv >= chip->pc_temp_ocv_lut->ocv[i][j]) { |
| 564 | if (ocv == chip->pc_temp_ocv_lut->ocv[i][j]) |
| 565 | return |
| 566 | chip->pc_temp_ocv_lut->percent[i]; |
| 567 | pc = linear_interpolate( |
| 568 | chip->pc_temp_ocv_lut->percent[i], |
| 569 | chip->pc_temp_ocv_lut->ocv[i][j], |
| 570 | chip->pc_temp_ocv_lut->percent[i - 1], |
| 571 | chip->pc_temp_ocv_lut->ocv[i - 1][j], |
| 572 | ocv); |
| 573 | return pc; |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
Abhijeet Dharmapurikar | 78d812f | 2011-07-21 19:57:21 -0700 | [diff] [blame] | 578 | /* |
| 579 | * batt_temp is within temperature for |
| 580 | * column j-1 and j |
| 581 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 582 | if (ocv >= chip->pc_temp_ocv_lut->ocv[0][j]) |
| 583 | return chip->pc_temp_ocv_lut->percent[0]; |
| 584 | if (ocv <= chip->pc_temp_ocv_lut->ocv[rows - 1][j - 1]) |
| 585 | return chip->pc_temp_ocv_lut->percent[rows - 1]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 586 | |
Abhijeet Dharmapurikar | 78d812f | 2011-07-21 19:57:21 -0700 | [diff] [blame] | 587 | pcj_minus_one = 0; |
| 588 | pcj = 0; |
| 589 | for (i = 0; i < rows-1; i++) { |
| 590 | if (pcj == 0 |
| 591 | && is_between(chip->pc_temp_ocv_lut->ocv[i][j], |
| 592 | chip->pc_temp_ocv_lut->ocv[i+1][j], ocv)) { |
| 593 | pcj = linear_interpolate( |
| 594 | chip->pc_temp_ocv_lut->percent[i], |
| 595 | chip->pc_temp_ocv_lut->ocv[i][j], |
| 596 | chip->pc_temp_ocv_lut->percent[i + 1], |
| 597 | chip->pc_temp_ocv_lut->ocv[i+1][j], |
| 598 | ocv); |
| 599 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 600 | |
Abhijeet Dharmapurikar | 78d812f | 2011-07-21 19:57:21 -0700 | [diff] [blame] | 601 | if (pcj_minus_one == 0 |
| 602 | && is_between(chip->pc_temp_ocv_lut->ocv[i][j-1], |
| 603 | chip->pc_temp_ocv_lut->ocv[i+1][j-1], ocv)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 604 | |
Abhijeet Dharmapurikar | 78d812f | 2011-07-21 19:57:21 -0700 | [diff] [blame] | 605 | pcj_minus_one = linear_interpolate( |
| 606 | chip->pc_temp_ocv_lut->percent[i], |
| 607 | chip->pc_temp_ocv_lut->ocv[i][j-1], |
| 608 | chip->pc_temp_ocv_lut->percent[i + 1], |
| 609 | chip->pc_temp_ocv_lut->ocv[i+1][j-1], |
| 610 | ocv); |
| 611 | } |
| 612 | |
| 613 | if (pcj && pcj_minus_one) { |
| 614 | pc = linear_interpolate( |
| 615 | pcj_minus_one, |
| 616 | chip->pc_temp_ocv_lut->temp[j-1], |
| 617 | pcj, |
| 618 | chip->pc_temp_ocv_lut->temp[j], |
| 619 | batt_temp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 620 | return pc; |
| 621 | } |
| 622 | } |
| 623 | |
Abhijeet Dharmapurikar | 78d812f | 2011-07-21 19:57:21 -0700 | [diff] [blame] | 624 | if (pcj) |
| 625 | return pcj; |
| 626 | |
| 627 | if (pcj_minus_one) |
| 628 | return pcj_minus_one; |
| 629 | |
| 630 | pr_debug("%d ocv wasn't found for temp %d in the LUT returning 100%%", |
| 631 | ocv, batt_temp); |
| 632 | return 100; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | static int calculate_rbatt(struct pm8921_bms_chip *chip) |
| 636 | { |
| 637 | int rc; |
| 638 | unsigned int ocv, vsense, vbatt, r_batt; |
| 639 | |
| 640 | rc = read_ocv_for_rbatt(chip, &ocv); |
| 641 | if (rc) { |
| 642 | pr_err("fail to read ocv_for_rbatt rc = %d\n", rc); |
| 643 | ocv = 0; |
| 644 | } |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 645 | ocv = adjust_xo_reading(chip, ocv); |
| 646 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 647 | rc = read_vbatt_for_rbatt(chip, &vbatt); |
| 648 | if (rc) { |
| 649 | pr_err("fail to read vbatt_for_rbatt rc = %d\n", rc); |
| 650 | ocv = 0; |
| 651 | } |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 652 | vbatt = adjust_xo_reading(chip, vbatt); |
| 653 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 654 | rc = read_vsense_for_rbatt(chip, &vsense); |
| 655 | if (rc) { |
| 656 | pr_err("fail to read vsense_for_rbatt rc = %d\n", rc); |
| 657 | ocv = 0; |
| 658 | } |
| 659 | if (ocv == 0 |
| 660 | || ocv == vbatt |
| 661 | || vsense == 0) { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 662 | pr_debug("rbatt readings unavailable ocv = %d, vbatt = %d," |
| 663 | "vsen = %d\n", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 664 | ocv, vbatt, vsense); |
| 665 | return -EINVAL; |
| 666 | } |
| 667 | r_batt = ((ocv - vbatt) * chip->r_sense) / vsense; |
| 668 | pr_debug("r_batt = %umilliOhms", r_batt); |
| 669 | return r_batt; |
| 670 | } |
| 671 | |
| 672 | static int calculate_fcc(struct pm8921_bms_chip *chip, int batt_temp, |
| 673 | int chargecycles) |
| 674 | { |
| 675 | int initfcc, result, scalefactor = 0; |
| 676 | |
| 677 | initfcc = interpolate_fcc(chip, batt_temp); |
| 678 | pr_debug("intfcc = %umAh batt_temp = %d\n", initfcc, batt_temp); |
| 679 | |
| 680 | scalefactor = interpolate_scalingfactor_fcc(chip, chargecycles); |
| 681 | pr_debug("scalefactor = %d batt_temp = %d\n", scalefactor, batt_temp); |
| 682 | |
| 683 | /* Multiply the initial FCC value by the scale factor. */ |
| 684 | result = (initfcc * scalefactor) / 100; |
| 685 | pr_debug("fcc mAh = %d\n", result); |
| 686 | return result; |
| 687 | } |
| 688 | |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 689 | static int get_battery_uvolts(struct pm8921_bms_chip *chip, int *uvolts) |
| 690 | { |
| 691 | int rc; |
| 692 | struct pm8921_adc_chan_result result; |
| 693 | |
| 694 | rc = pm8921_adc_read(chip->vbat_channel, &result); |
| 695 | if (rc) { |
| 696 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 697 | chip->vbat_channel, rc); |
| 698 | return rc; |
| 699 | } |
| 700 | pr_debug("mvolts phy = %lld meas = 0x%llx", result.physical, |
| 701 | result.measurement); |
| 702 | *uvolts = (int)result.physical; |
| 703 | *uvolts = *uvolts * 1000; |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | static int adc_based_ocv(struct pm8921_bms_chip *chip, int *ocv) |
| 708 | { |
| 709 | int vbatt, rbatt, ibatt, rc; |
| 710 | |
| 711 | rc = get_battery_uvolts(chip, &vbatt); |
| 712 | if (rc) { |
| 713 | pr_err("failed to read vbatt from adc rc = %d\n", rc); |
| 714 | last_ocv_uv = DEFAULT_OCV_MICROVOLTS; |
| 715 | return rc; |
| 716 | } |
| 717 | |
| 718 | rc = pm8921_bms_get_battery_current(&ibatt); |
| 719 | if (rc) { |
| 720 | pr_err("failed to read batt current rc = %d\n", rc); |
| 721 | last_ocv_uv = DEFAULT_OCV_MICROVOLTS; |
| 722 | return rc; |
| 723 | } |
| 724 | |
| 725 | rbatt = calculate_rbatt(the_chip); |
| 726 | if (rbatt < 0) |
| 727 | rbatt = DEFAULT_RBATT_MOHMS; |
| 728 | *ocv = vbatt + ibatt * rbatt; |
| 729 | return 0; |
| 730 | } |
| 731 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 732 | static int calculate_pc(struct pm8921_bms_chip *chip, int ocv_uv, int batt_temp, |
| 733 | int chargecycles) |
| 734 | { |
| 735 | int pc, scalefactor; |
| 736 | |
| 737 | pc = interpolate_pc(chip, batt_temp, ocv_uv / 1000); |
| 738 | pr_debug("pc = %u for ocv = %dmicroVolts batt_temp = %d\n", |
| 739 | pc, ocv_uv, batt_temp); |
| 740 | |
| 741 | scalefactor = interpolate_scalingfactor_pc(chip, chargecycles, pc); |
| 742 | pr_debug("scalefactor = %u batt_temp = %d\n", scalefactor, batt_temp); |
| 743 | |
| 744 | /* Multiply the initial FCC value by the scale factor. */ |
| 745 | pc = (pc * scalefactor) / 100; |
| 746 | return pc; |
| 747 | } |
| 748 | |
Abhijeet Dharmapurikar | fa77f1a | 2011-06-27 21:11:51 -0700 | [diff] [blame] | 749 | static void calculate_cc_mah(struct pm8921_bms_chip *chip, int64_t *val, |
Abhijeet Dharmapurikar | 52aa968 | 2011-07-20 14:05:47 -0700 | [diff] [blame] | 750 | int *coulumb_counter) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 751 | { |
| 752 | int rc; |
| 753 | int64_t cc_voltage_uv, cc_uvh, cc_mah; |
| 754 | |
| 755 | rc = read_cc(the_chip, coulumb_counter); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 756 | cc_voltage_uv = (int64_t)*coulumb_counter; |
Abhijeet Dharmapurikar | fa77f1a | 2011-06-27 21:11:51 -0700 | [diff] [blame] | 757 | cc_voltage_uv = cc_to_microvolt(chip, cc_voltage_uv); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 758 | pr_debug("cc_voltage_uv = %lld microvolts\n", cc_voltage_uv); |
Abhijeet Dharmapurikar | fa77f1a | 2011-06-27 21:11:51 -0700 | [diff] [blame] | 759 | cc_uvh = ccmicrovolt_to_uvh(cc_voltage_uv); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 760 | pr_debug("cc_uvh = %lld micro_volt_hour\n", cc_uvh); |
| 761 | cc_mah = div_s64(cc_uvh, chip->r_sense); |
| 762 | *val = cc_mah; |
| 763 | } |
| 764 | |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 765 | static int calculate_unusable_charge_mah(struct pm8921_bms_chip *chip, |
| 766 | int fcc, int batt_temp, int chargecycles) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 767 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 768 | int rbatt, voltage_unusable_uv, pc_unusable; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 769 | |
| 770 | rbatt = calculate_rbatt(chip); |
| 771 | if (rbatt < 0) { |
| 772 | rbatt = (last_rbatt < 0) ? DEFAULT_RBATT_MOHMS : last_rbatt; |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 773 | pr_debug("rbatt unavailable assuming %d\n", rbatt); |
| 774 | } else { |
| 775 | last_rbatt = rbatt; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 776 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 777 | |
| 778 | /* calculate unusable charge */ |
| 779 | voltage_unusable_uv = (rbatt * chip->i_test) |
| 780 | + (chip->v_failure * 1000); |
| 781 | pc_unusable = calculate_pc(chip, voltage_unusable_uv, |
| 782 | batt_temp, chargecycles); |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 783 | pr_debug("rbatt = %umilliOhms unusable_v =%d unusable_pc = %d\n", |
| 784 | rbatt, voltage_unusable_uv, pc_unusable); |
| 785 | return (fcc * pc_unusable) / 100; |
| 786 | } |
Abhijeet Dharmapurikar | 52aa968 | 2011-07-20 14:05:47 -0700 | [diff] [blame] | 787 | |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 788 | /* calculate remainging charge at the time of ocv */ |
| 789 | static int calculate_remaining_charge_mah(struct pm8921_bms_chip *chip, int fcc, |
| 790 | int batt_temp, int chargecycles) |
| 791 | { |
| 792 | int rc, ocv, pc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 793 | |
| 794 | /* calculate remainging charge */ |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 795 | ocv = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 796 | rc = read_last_good_ocv(chip, &ocv); |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 797 | if (rc) |
| 798 | pr_debug("failed to read ocv rc = %d\n", rc); |
| 799 | |
| 800 | if (ocv == 0) { |
| 801 | ocv = last_ocv_uv; |
| 802 | pr_debug("ocv not available using last_ocv_uv=%d\n", ocv); |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 803 | } else { |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 804 | /* update the usespace param since a good ocv is available */ |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 805 | last_ocv_uv = ocv; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 806 | } |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 807 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 808 | pc = calculate_pc(chip, ocv, batt_temp, chargecycles); |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 809 | pr_debug("ocv = %d pc = %d\n", ocv, pc); |
| 810 | return (fcc * pc) / 100; |
| 811 | } |
| 812 | |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 813 | static void calculate_charging_params(struct pm8921_bms_chip *chip, |
| 814 | int batt_temp, int chargecycles, |
| 815 | int *fcc, |
| 816 | int *unusable_charge, |
| 817 | int *remaining_charge, |
| 818 | int64_t *cc_mah) |
| 819 | { |
| 820 | int coulumb_counter; |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame^] | 821 | unsigned long flags; |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 822 | |
| 823 | *fcc = calculate_fcc(chip, batt_temp, chargecycles); |
| 824 | pr_debug("FCC = %umAh batt_temp = %d, cycles = %d", |
| 825 | *fcc, batt_temp, chargecycles); |
| 826 | |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame^] | 827 | /* fcc doesnt need to be read from hardware, lock the bms now */ |
| 828 | spin_lock_irqsave(&chip->bms_output_lock, flags); |
| 829 | pm_bms_lock_output_data(chip); |
| 830 | |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 831 | *unusable_charge = calculate_unusable_charge_mah(chip, *fcc, |
| 832 | batt_temp, chargecycles); |
| 833 | |
| 834 | pr_debug("UUC = %umAh", *unusable_charge); |
| 835 | |
| 836 | /* calculate remainging charge */ |
| 837 | *remaining_charge = calculate_remaining_charge_mah(chip, *fcc, |
| 838 | batt_temp, chargecycles); |
| 839 | pr_debug("RC = %umAh\n", *remaining_charge); |
| 840 | |
| 841 | /* calculate cc milli_volt_hour */ |
| 842 | calculate_cc_mah(chip, cc_mah, &coulumb_counter); |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame^] | 843 | |
| 844 | pm_bms_unlock_output_data(chip); |
| 845 | spin_unlock_irqrestore(&chip->bms_output_lock, flags); |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 846 | pr_debug("cc_mah = %lldmAh cc = %d\n", *cc_mah, coulumb_counter); |
| 847 | } |
| 848 | |
| 849 | static int calculate_real_fcc(struct pm8921_bms_chip *chip, |
| 850 | int batt_temp, int chargecycles) |
| 851 | { |
| 852 | int fcc, unusable_charge; |
| 853 | int remaining_charge; |
| 854 | int64_t cc_mah; |
| 855 | int real_fcc; |
| 856 | |
| 857 | calculate_charging_params(chip, batt_temp, chargecycles, |
| 858 | &fcc, |
| 859 | &unusable_charge, |
| 860 | &remaining_charge, |
| 861 | &cc_mah); |
| 862 | |
| 863 | real_fcc = remaining_charge - cc_mah; |
| 864 | |
| 865 | return real_fcc; |
| 866 | } |
| 867 | /* |
| 868 | * Remaining Usable Charge = remaining_charge (charge at ocv instance) |
| 869 | * - coloumb counter charge |
| 870 | * - unusable charge (due to battery resistance) |
| 871 | * SOC% = (remaining usable charge/ fcc - usable_charge); |
| 872 | */ |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 873 | #define BMS_BATT_NOMINAL 3700000 |
| 874 | #define MIN_OPERABLE_SOC 10 |
| 875 | #define BATTERY_POWER_SUPPLY_SOC 53 |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 876 | static int calculate_state_of_charge(struct pm8921_bms_chip *chip, |
| 877 | int batt_temp, int chargecycles) |
| 878 | { |
| 879 | int remaining_usable_charge, fcc, unusable_charge; |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 880 | int remaining_charge, soc; |
Abhijeet Dharmapurikar | 020b37c | 2011-07-20 14:14:51 -0700 | [diff] [blame] | 881 | int update_userspace = 1; |
| 882 | int64_t cc_mah; |
| 883 | |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 884 | calculate_charging_params(chip, batt_temp, chargecycles, |
| 885 | &fcc, |
| 886 | &unusable_charge, |
| 887 | &remaining_charge, |
| 888 | &cc_mah); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 889 | |
| 890 | /* calculate remaining usable charge */ |
| 891 | remaining_usable_charge = remaining_charge - cc_mah - unusable_charge; |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 892 | pr_debug("RUC = %dmAh\n", remaining_usable_charge); |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 893 | soc = (remaining_usable_charge * 100) / (fcc - unusable_charge); |
| 894 | if (soc > 100) |
| 895 | soc = 100; |
| 896 | pr_debug("SOC = %u%%\n", soc); |
| 897 | |
| 898 | if (soc < MIN_OPERABLE_SOC) { |
| 899 | int ocv = 0, rc; |
| 900 | |
| 901 | rc = adc_based_ocv(chip, &ocv); |
| 902 | if (rc == 0 && ocv >= BMS_BATT_NOMINAL) { |
| 903 | /* |
| 904 | * The ocv doesnt seem to have dropped for |
| 905 | * soc to go negative. |
| 906 | * The setup must be using a power supply |
| 907 | * instead of real batteries. |
| 908 | * Fake high enough soc to prevent userspace |
| 909 | * shutdown for low battery |
| 910 | */ |
| 911 | soc = BATTERY_POWER_SUPPLY_SOC; |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame^] | 912 | pr_debug("Adjusting SOC to %d\n", soc); |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
| 916 | if (soc < 0) { |
Abhijeet Dharmapurikar | 21ebadd | 2011-09-28 21:14:16 -0700 | [diff] [blame] | 917 | pr_err("bad rem_usb_chg = %d rem_chg %d," |
| 918 | "cc_mah %lld, unusb_chg %d\n", |
| 919 | remaining_usable_charge, remaining_charge, |
| 920 | cc_mah, unusable_charge); |
| 921 | pr_err("for bad rem_usb_chg last_ocv_uv = %d" |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 922 | "chargecycles = %d, batt_temp = %d" |
| 923 | "fcc = %d soc =%d\n", |
| 924 | last_ocv_uv, chargecycles, batt_temp, |
| 925 | fcc, soc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 926 | update_userspace = 0; |
| 927 | } |
| 928 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 929 | if (update_userspace) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 930 | last_soc = soc; |
| 931 | } |
| 932 | return soc; |
| 933 | } |
| 934 | |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 935 | #define XOADC_MAX_1P25V 1312500 |
| 936 | #define XOADC_MIN_1P25V 1187500 |
| 937 | #define XOADC_MAX_0P625V 656250 |
| 938 | #define XOADC_MIN_0P625V 593750 |
| 939 | |
| 940 | #define HKADC_V_PER_BIT_MUL_FACTOR 977 |
| 941 | #define HKADC_V_PER_BIT_DIV_FACTOR 10 |
| 942 | static int calib_hkadc_convert_microvolt(unsigned int phy) |
| 943 | { |
| 944 | return phy * HKADC_V_PER_BIT_MUL_FACTOR / HKADC_V_PER_BIT_DIV_FACTOR; |
| 945 | } |
| 946 | |
| 947 | static void calib_hkadc(struct pm8921_bms_chip *chip) |
| 948 | { |
| 949 | int voltage, rc; |
| 950 | struct pm8921_adc_chan_result result; |
| 951 | |
| 952 | rc = pm8921_adc_read(the_chip->ref1p25v_channel, &result); |
| 953 | if (rc) { |
| 954 | pr_err("ADC failed for 1.25volts rc = %d\n", rc); |
| 955 | return; |
| 956 | } |
| 957 | voltage = calib_hkadc_convert_microvolt(result.physical); |
| 958 | |
| 959 | pr_debug("result 1.25v = 0x%llx, voltage = %dmV adc_meas = %lld\n", |
| 960 | result.physical, voltage, result.measurement); |
| 961 | |
| 962 | /* check for valid range */ |
| 963 | if (voltage > XOADC_MAX_1P25V) |
| 964 | voltage = XOADC_MAX_1P25V; |
| 965 | else if (voltage < XOADC_MIN_1P25V) |
| 966 | voltage = XOADC_MIN_1P25V; |
| 967 | chip->xoadc_v125 = voltage; |
| 968 | |
| 969 | rc = pm8921_adc_read(the_chip->ref625mv_channel, &result); |
| 970 | if (rc) { |
| 971 | pr_err("ADC failed for 1.25volts rc = %d\n", rc); |
| 972 | return; |
| 973 | } |
| 974 | voltage = calib_hkadc_convert_microvolt(result.physical); |
| 975 | pr_debug("result 0.625V = 0x%llx, voltage = %dmV adc_mead = %lld\n", |
| 976 | result.physical, voltage, result.measurement); |
| 977 | /* check for valid range */ |
| 978 | if (voltage > XOADC_MAX_0P625V) |
| 979 | voltage = XOADC_MAX_0P625V; |
| 980 | else if (voltage < XOADC_MIN_0P625V) |
| 981 | voltage = XOADC_MIN_0P625V; |
| 982 | |
| 983 | chip->xoadc_v0625 = voltage; |
| 984 | } |
| 985 | |
| 986 | static void calibrate_hkadc_work(struct work_struct *work) |
| 987 | { |
| 988 | struct pm8921_bms_chip *chip = container_of(work, |
| 989 | struct pm8921_bms_chip, calib_hkadc_work); |
| 990 | |
| 991 | calib_hkadc(chip); |
| 992 | } |
| 993 | |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 994 | int pm8921_bms_get_vsense_avg(int *result) |
| 995 | { |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame^] | 996 | int rc = -EINVAL; |
| 997 | unsigned long flags; |
| 998 | |
| 999 | if (the_chip) { |
| 1000 | spin_lock_irqsave(&the_chip->bms_output_lock, flags); |
| 1001 | pm_bms_lock_output_data(the_chip); |
| 1002 | rc = read_vsense_avg(the_chip, result); |
| 1003 | pm_bms_unlock_output_data(the_chip); |
| 1004 | spin_unlock_irqrestore(&the_chip->bms_output_lock, flags); |
| 1005 | } |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 1006 | |
| 1007 | pr_err("called before initialization\n"); |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame^] | 1008 | return rc; |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 1009 | } |
| 1010 | EXPORT_SYMBOL(pm8921_bms_get_vsense_avg); |
| 1011 | |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 1012 | int pm8921_bms_get_battery_current(int *result) |
| 1013 | { |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame^] | 1014 | unsigned long flags; |
| 1015 | |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 1016 | if (!the_chip) { |
| 1017 | pr_err("called before initialization\n"); |
| 1018 | return -EINVAL; |
| 1019 | } |
| 1020 | if (the_chip->r_sense == 0) { |
| 1021 | pr_err("r_sense is zero\n"); |
| 1022 | return -EINVAL; |
| 1023 | } |
| 1024 | |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame^] | 1025 | spin_lock_irqsave(&the_chip->bms_output_lock, flags); |
| 1026 | pm_bms_lock_output_data(the_chip); |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 1027 | read_vsense_avg(the_chip, result); |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame^] | 1028 | pm_bms_unlock_output_data(the_chip); |
| 1029 | spin_unlock_irqrestore(&the_chip->bms_output_lock, flags); |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 1030 | pr_debug("vsense=%d\n", *result); |
Abhijeet Dharmapurikar | a7a1c6b | 2011-08-17 10:04:58 -0700 | [diff] [blame] | 1031 | /* cast for signed division */ |
| 1032 | *result = *result / (int)the_chip->r_sense; |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 1033 | return 0; |
| 1034 | } |
| 1035 | EXPORT_SYMBOL(pm8921_bms_get_battery_current); |
| 1036 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1037 | int pm8921_bms_get_percent_charge(void) |
| 1038 | { |
Abhijeet Dharmapurikar | c448d98 | 2011-06-28 20:48:36 -0700 | [diff] [blame] | 1039 | int batt_temp, rc; |
| 1040 | struct pm8921_adc_chan_result result; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1041 | |
Abhijeet Dharmapurikar | c448d98 | 2011-06-28 20:48:36 -0700 | [diff] [blame] | 1042 | if (!the_chip) { |
| 1043 | pr_err("called before initialization\n"); |
| 1044 | return -EINVAL; |
| 1045 | } |
| 1046 | |
| 1047 | rc = pm8921_adc_read(the_chip->batt_temp_channel, &result); |
| 1048 | if (rc) { |
| 1049 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1050 | the_chip->batt_temp_channel, rc); |
| 1051 | return rc; |
| 1052 | } |
| 1053 | pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical, |
| 1054 | result.measurement); |
| 1055 | batt_temp = (int)result.physical; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1056 | return calculate_state_of_charge(the_chip, |
| 1057 | batt_temp, last_chargecycles); |
| 1058 | } |
| 1059 | EXPORT_SYMBOL_GPL(pm8921_bms_get_percent_charge); |
| 1060 | |
Abhijeet Dharmapurikar | 325bdc9 | 2011-09-07 20:45:49 -0700 | [diff] [blame] | 1061 | int pm8921_bms_get_fcc(void) |
| 1062 | { |
| 1063 | int batt_temp, rc; |
| 1064 | struct pm8921_adc_chan_result result; |
| 1065 | |
| 1066 | if (!the_chip) { |
| 1067 | pr_err("called before initialization\n"); |
| 1068 | return -EINVAL; |
| 1069 | } |
| 1070 | |
| 1071 | rc = pm8921_adc_read(the_chip->batt_temp_channel, &result); |
| 1072 | if (rc) { |
| 1073 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1074 | the_chip->batt_temp_channel, rc); |
| 1075 | return rc; |
| 1076 | } |
| 1077 | pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical, |
| 1078 | result.measurement); |
| 1079 | batt_temp = (int)result.physical; |
| 1080 | return calculate_fcc(the_chip, batt_temp, last_chargecycles); |
| 1081 | } |
| 1082 | EXPORT_SYMBOL_GPL(pm8921_bms_get_fcc); |
| 1083 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1084 | static int start_percent; |
| 1085 | static int end_percent; |
| 1086 | void pm8921_bms_charging_began(void) |
| 1087 | { |
Abhijeet Dharmapurikar | c448d98 | 2011-06-28 20:48:36 -0700 | [diff] [blame] | 1088 | start_percent = pm8921_bms_get_percent_charge(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1089 | pr_debug("start_percent = %u%%\n", start_percent); |
| 1090 | } |
| 1091 | EXPORT_SYMBOL_GPL(pm8921_bms_charging_began); |
| 1092 | |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1093 | void pm8921_bms_charging_end(int is_battery_full) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1094 | { |
Abhijeet Dharmapurikar | fad7235 | 2011-09-25 23:06:05 -0700 | [diff] [blame] | 1095 | if (is_battery_full && the_chip != NULL) { |
| 1096 | int batt_temp, rc; |
| 1097 | struct pm8921_adc_chan_result result; |
| 1098 | |
| 1099 | rc = pm8921_adc_read(the_chip->batt_temp_channel, &result); |
| 1100 | if (rc) { |
| 1101 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1102 | the_chip->batt_temp_channel, rc); |
| 1103 | goto charge_cycle_calculation; |
| 1104 | } |
| 1105 | pr_debug("batt_temp phy = %lld meas = 0x%llx", result.physical, |
| 1106 | result.measurement); |
| 1107 | batt_temp = (int)result.physical; |
| 1108 | last_real_fcc = calculate_real_fcc(the_chip, |
| 1109 | batt_temp, last_chargecycles); |
| 1110 | } |
| 1111 | |
| 1112 | charge_cycle_calculation: |
Abhijeet Dharmapurikar | c448d98 | 2011-06-28 20:48:36 -0700 | [diff] [blame] | 1113 | end_percent = pm8921_bms_get_percent_charge(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1114 | if (end_percent > start_percent) { |
| 1115 | last_charge_increase = end_percent - start_percent; |
| 1116 | if (last_charge_increase > 100) { |
| 1117 | last_chargecycles++; |
| 1118 | last_charge_increase = last_charge_increase % 100; |
| 1119 | } |
| 1120 | } |
| 1121 | pr_debug("end_percent = %u%% last_charge_increase = %d" |
| 1122 | "last_chargecycles = %d\n", |
| 1123 | end_percent, |
| 1124 | last_charge_increase, |
| 1125 | last_chargecycles); |
| 1126 | } |
| 1127 | EXPORT_SYMBOL_GPL(pm8921_bms_charging_end); |
| 1128 | |
| 1129 | static irqreturn_t pm8921_bms_sbi_write_ok_handler(int irq, void *data) |
| 1130 | { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1131 | pr_debug("irq = %d triggered", irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1132 | return IRQ_HANDLED; |
| 1133 | } |
| 1134 | |
| 1135 | static irqreturn_t pm8921_bms_cc_thr_handler(int irq, void *data) |
| 1136 | { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1137 | pr_debug("irq = %d triggered", irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1138 | return IRQ_HANDLED; |
| 1139 | } |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1140 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1141 | static irqreturn_t pm8921_bms_vsense_thr_handler(int irq, void *data) |
| 1142 | { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1143 | pr_debug("irq = %d triggered", irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1144 | return IRQ_HANDLED; |
| 1145 | } |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1146 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1147 | static irqreturn_t pm8921_bms_vsense_for_r_handler(int irq, void *data) |
| 1148 | { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1149 | pr_debug("irq = %d triggered", irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1150 | return IRQ_HANDLED; |
| 1151 | } |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1152 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1153 | static irqreturn_t pm8921_bms_ocv_for_r_handler(int irq, void *data) |
| 1154 | { |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1155 | struct pm8921_bms_chip *chip = data; |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1156 | |
| 1157 | pr_debug("irq = %d triggered", irq); |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1158 | schedule_work(&chip->calib_hkadc_work); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1159 | return IRQ_HANDLED; |
| 1160 | } |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1161 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1162 | static irqreturn_t pm8921_bms_good_ocv_handler(int irq, void *data) |
| 1163 | { |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1164 | struct pm8921_bms_chip *chip = data; |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1165 | |
| 1166 | pr_debug("irq = %d triggered", irq); |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1167 | schedule_work(&chip->calib_hkadc_work); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1168 | return IRQ_HANDLED; |
| 1169 | } |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1170 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1171 | static irqreturn_t pm8921_bms_vsense_avg_handler(int irq, void *data) |
| 1172 | { |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1173 | pr_debug("irq = %d triggered", irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1174 | return IRQ_HANDLED; |
| 1175 | } |
| 1176 | |
| 1177 | struct pm_bms_irq_init_data { |
| 1178 | unsigned int irq_id; |
| 1179 | char *name; |
| 1180 | unsigned long flags; |
| 1181 | irqreturn_t (*handler)(int, void *); |
| 1182 | }; |
| 1183 | |
| 1184 | #define BMS_IRQ(_id, _flags, _handler) \ |
| 1185 | { \ |
| 1186 | .irq_id = _id, \ |
| 1187 | .name = #_id, \ |
| 1188 | .flags = _flags, \ |
| 1189 | .handler = _handler, \ |
| 1190 | } |
| 1191 | |
| 1192 | struct pm_bms_irq_init_data bms_irq_data[] = { |
| 1193 | BMS_IRQ(PM8921_BMS_SBI_WRITE_OK, IRQF_TRIGGER_RISING, |
| 1194 | pm8921_bms_sbi_write_ok_handler), |
| 1195 | BMS_IRQ(PM8921_BMS_CC_THR, IRQF_TRIGGER_RISING, |
| 1196 | pm8921_bms_cc_thr_handler), |
| 1197 | BMS_IRQ(PM8921_BMS_VSENSE_THR, IRQF_TRIGGER_RISING, |
| 1198 | pm8921_bms_vsense_thr_handler), |
| 1199 | BMS_IRQ(PM8921_BMS_VSENSE_FOR_R, IRQF_TRIGGER_RISING, |
| 1200 | pm8921_bms_vsense_for_r_handler), |
| 1201 | BMS_IRQ(PM8921_BMS_OCV_FOR_R, IRQF_TRIGGER_RISING, |
| 1202 | pm8921_bms_ocv_for_r_handler), |
| 1203 | BMS_IRQ(PM8921_BMS_GOOD_OCV, IRQF_TRIGGER_RISING, |
| 1204 | pm8921_bms_good_ocv_handler), |
| 1205 | BMS_IRQ(PM8921_BMS_VSENSE_AVG, IRQF_TRIGGER_RISING, |
| 1206 | pm8921_bms_vsense_avg_handler), |
| 1207 | }; |
| 1208 | |
| 1209 | static void free_irqs(struct pm8921_bms_chip *chip) |
| 1210 | { |
| 1211 | int i; |
| 1212 | |
| 1213 | for (i = 0; i < PM_BMS_MAX_INTS; i++) |
| 1214 | if (chip->pmic_bms_irq[i]) { |
| 1215 | free_irq(chip->pmic_bms_irq[i], NULL); |
| 1216 | chip->pmic_bms_irq[i] = 0; |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | static int __devinit request_irqs(struct pm8921_bms_chip *chip, |
| 1221 | struct platform_device *pdev) |
| 1222 | { |
| 1223 | struct resource *res; |
| 1224 | int ret, i; |
| 1225 | |
| 1226 | ret = 0; |
| 1227 | bitmap_fill(chip->enabled_irqs, PM_BMS_MAX_INTS); |
| 1228 | |
| 1229 | for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) { |
| 1230 | res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, |
| 1231 | bms_irq_data[i].name); |
| 1232 | if (res == NULL) { |
| 1233 | pr_err("couldn't find %s\n", bms_irq_data[i].name); |
| 1234 | goto err_out; |
| 1235 | } |
| 1236 | ret = request_irq(res->start, bms_irq_data[i].handler, |
| 1237 | bms_irq_data[i].flags, |
| 1238 | bms_irq_data[i].name, chip); |
| 1239 | if (ret < 0) { |
| 1240 | pr_err("couldn't request %d (%s) %d\n", res->start, |
| 1241 | bms_irq_data[i].name, ret); |
| 1242 | goto err_out; |
| 1243 | } |
| 1244 | chip->pmic_bms_irq[bms_irq_data[i].irq_id] = res->start; |
| 1245 | pm8921_bms_disable_irq(chip, bms_irq_data[i].irq_id); |
| 1246 | } |
| 1247 | return 0; |
| 1248 | |
| 1249 | err_out: |
| 1250 | free_irqs(chip); |
| 1251 | return -EINVAL; |
| 1252 | } |
| 1253 | |
| 1254 | #define EN_BMS_BIT BIT(7) |
| 1255 | #define EN_PON_HS_BIT BIT(0) |
| 1256 | static int __devinit pm8921_bms_hw_init(struct pm8921_bms_chip *chip) |
| 1257 | { |
| 1258 | int rc; |
| 1259 | |
| 1260 | rc = pm_bms_masked_write(chip, BMS_CONTROL, |
| 1261 | EN_BMS_BIT | EN_PON_HS_BIT, EN_BMS_BIT | EN_PON_HS_BIT); |
| 1262 | if (rc) { |
| 1263 | pr_err("failed to enable pon and bms addr = %d %d", |
| 1264 | BMS_CONTROL, rc); |
| 1265 | } |
| 1266 | |
| 1267 | return 0; |
| 1268 | } |
| 1269 | |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 1270 | static void check_initial_ocv(struct pm8921_bms_chip *chip) |
| 1271 | { |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1272 | int ocv, rc; |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 1273 | |
| 1274 | /* |
| 1275 | * Check if a last_good_ocv is available, |
| 1276 | * if not compute it here at boot time. |
| 1277 | */ |
| 1278 | rc = read_last_good_ocv(chip, &ocv); |
| 1279 | if (rc || ocv == 0) { |
Abhijeet Dharmapurikar | af6f9e2 | 2011-10-03 19:19:02 -0700 | [diff] [blame] | 1280 | rc = adc_based_ocv(chip, &last_ocv_uv); |
| 1281 | pr_err("failed to read ocv from adc and bms rc = %d\n", rc); |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 1282 | } |
| 1283 | pr_debug("ocv = %d last_ocv_uv = %d\n", ocv, last_ocv_uv); |
| 1284 | } |
| 1285 | |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 1286 | static int64_t read_battery_id(struct pm8921_bms_chip *chip) |
| 1287 | { |
| 1288 | int rc; |
| 1289 | struct pm8921_adc_chan_result result; |
| 1290 | |
| 1291 | rc = pm8921_adc_read(chip->batt_id_channel, &result); |
| 1292 | if (rc) { |
| 1293 | pr_err("error reading batt id channel = %d, rc = %d\n", |
| 1294 | chip->vbat_channel, rc); |
| 1295 | return rc; |
| 1296 | } |
| 1297 | pr_debug("batt_id phy = %lld meas = 0x%llx\n", result.physical, |
| 1298 | result.measurement); |
| 1299 | return result.physical; |
| 1300 | } |
| 1301 | |
| 1302 | #define PALLADIUM_ID_MIN 2500 |
| 1303 | #define PALLADIUM_ID_MAX 4000 |
| 1304 | static int set_battery_data(struct pm8921_bms_chip *chip) |
| 1305 | { |
| 1306 | int64_t battery_id; |
| 1307 | |
| 1308 | battery_id = read_battery_id(chip); |
| 1309 | |
| 1310 | if (battery_id < 0) { |
| 1311 | pr_err("cannot read battery id err = %lld\n", battery_id); |
| 1312 | return battery_id; |
| 1313 | } |
| 1314 | |
| 1315 | if (is_between(PALLADIUM_ID_MIN, PALLADIUM_ID_MAX, battery_id)) { |
| 1316 | chip->fcc = palladium_1500_data.fcc; |
| 1317 | chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut; |
| 1318 | chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut; |
| 1319 | chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut; |
| 1320 | chip->pc_sf_lut = palladium_1500_data.pc_sf_lut; |
| 1321 | return 0; |
| 1322 | } else { |
| 1323 | pr_warn("invalid battery id, palladium 1500 assumed\n"); |
| 1324 | chip->fcc = palladium_1500_data.fcc; |
| 1325 | chip->fcc_temp_lut = palladium_1500_data.fcc_temp_lut; |
| 1326 | chip->fcc_sf_lut = palladium_1500_data.fcc_sf_lut; |
| 1327 | chip->pc_temp_ocv_lut = palladium_1500_data.pc_temp_ocv_lut; |
| 1328 | chip->pc_sf_lut = palladium_1500_data.pc_sf_lut; |
| 1329 | return 0; |
| 1330 | } |
| 1331 | } |
| 1332 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1333 | enum { |
| 1334 | CALC_RBATT, |
| 1335 | CALC_FCC, |
| 1336 | CALC_PC, |
| 1337 | CALC_SOC, |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1338 | CALIB_HKADC, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1339 | }; |
| 1340 | |
| 1341 | static int test_batt_temp = 5; |
| 1342 | static int test_chargecycle = 150; |
| 1343 | static int test_ocv = 3900000; |
| 1344 | enum { |
| 1345 | TEST_BATT_TEMP, |
| 1346 | TEST_CHARGE_CYCLE, |
| 1347 | TEST_OCV, |
| 1348 | }; |
| 1349 | static int get_test_param(void *data, u64 * val) |
| 1350 | { |
| 1351 | switch ((int)data) { |
| 1352 | case TEST_BATT_TEMP: |
| 1353 | *val = test_batt_temp; |
| 1354 | break; |
| 1355 | case TEST_CHARGE_CYCLE: |
| 1356 | *val = test_chargecycle; |
| 1357 | break; |
| 1358 | case TEST_OCV: |
| 1359 | *val = test_ocv; |
| 1360 | break; |
| 1361 | default: |
| 1362 | return -EINVAL; |
| 1363 | } |
| 1364 | return 0; |
| 1365 | } |
| 1366 | static int set_test_param(void *data, u64 val) |
| 1367 | { |
| 1368 | switch ((int)data) { |
| 1369 | case TEST_BATT_TEMP: |
| 1370 | test_batt_temp = (int)val; |
| 1371 | break; |
| 1372 | case TEST_CHARGE_CYCLE: |
| 1373 | test_chargecycle = (int)val; |
| 1374 | break; |
| 1375 | case TEST_OCV: |
| 1376 | test_ocv = (int)val; |
| 1377 | break; |
| 1378 | default: |
| 1379 | return -EINVAL; |
| 1380 | } |
| 1381 | return 0; |
| 1382 | } |
| 1383 | DEFINE_SIMPLE_ATTRIBUTE(temp_fops, get_test_param, set_test_param, "%llu\n"); |
| 1384 | |
| 1385 | static int get_calc(void *data, u64 * val) |
| 1386 | { |
| 1387 | int param = (int)data; |
| 1388 | int ret = 0; |
| 1389 | |
| 1390 | *val = 0; |
| 1391 | |
| 1392 | /* global irq number passed in via data */ |
| 1393 | switch (param) { |
| 1394 | case CALC_RBATT: |
| 1395 | *val = calculate_rbatt(the_chip); |
| 1396 | break; |
| 1397 | case CALC_FCC: |
| 1398 | *val = calculate_fcc(the_chip, test_batt_temp, |
| 1399 | test_chargecycle); |
| 1400 | break; |
| 1401 | case CALC_PC: |
| 1402 | *val = calculate_pc(the_chip, test_ocv, test_batt_temp, |
| 1403 | test_chargecycle); |
| 1404 | break; |
| 1405 | case CALC_SOC: |
| 1406 | *val = calculate_state_of_charge(the_chip, |
| 1407 | test_batt_temp, test_chargecycle); |
| 1408 | break; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1409 | case CALIB_HKADC: |
| 1410 | /* reading this will trigger calibration */ |
| 1411 | *val = 0; |
| 1412 | calib_hkadc(the_chip); |
| 1413 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1414 | default: |
| 1415 | ret = -EINVAL; |
| 1416 | } |
| 1417 | return ret; |
| 1418 | } |
| 1419 | DEFINE_SIMPLE_ATTRIBUTE(calc_fops, get_calc, NULL, "%llu\n"); |
| 1420 | |
| 1421 | static int get_reading(void *data, u64 * val) |
| 1422 | { |
| 1423 | int param = (int)data; |
| 1424 | int ret = 0; |
| 1425 | |
| 1426 | *val = 0; |
| 1427 | |
| 1428 | /* global irq number passed in via data */ |
| 1429 | switch (param) { |
| 1430 | case CC_MSB: |
| 1431 | case CC_LSB: |
| 1432 | read_cc(the_chip, (int *)val); |
| 1433 | break; |
| 1434 | case LAST_GOOD_OCV_VALUE: |
| 1435 | read_last_good_ocv(the_chip, (uint *)val); |
| 1436 | break; |
| 1437 | case VBATT_FOR_RBATT: |
| 1438 | read_vbatt_for_rbatt(the_chip, (uint *)val); |
| 1439 | break; |
| 1440 | case VSENSE_FOR_RBATT: |
| 1441 | read_vsense_for_rbatt(the_chip, (uint *)val); |
| 1442 | break; |
| 1443 | case OCV_FOR_RBATT: |
| 1444 | read_ocv_for_rbatt(the_chip, (uint *)val); |
| 1445 | break; |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 1446 | case VSENSE_AVG: |
| 1447 | read_vsense_avg(the_chip, (uint *)val); |
| 1448 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1449 | default: |
| 1450 | ret = -EINVAL; |
| 1451 | } |
| 1452 | return ret; |
| 1453 | } |
Abhijeet Dharmapurikar | e0c3e5f | 2011-07-11 21:17:23 -0700 | [diff] [blame] | 1454 | DEFINE_SIMPLE_ATTRIBUTE(reading_fops, get_reading, NULL, "%lld\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1455 | |
| 1456 | static int get_rt_status(void *data, u64 * val) |
| 1457 | { |
| 1458 | int i = (int)data; |
| 1459 | int ret; |
| 1460 | |
| 1461 | /* global irq number passed in via data */ |
| 1462 | ret = pm_bms_get_rt_status(the_chip, i); |
| 1463 | *val = ret; |
| 1464 | return 0; |
| 1465 | } |
| 1466 | DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n"); |
| 1467 | |
| 1468 | static int get_reg(void *data, u64 * val) |
| 1469 | { |
| 1470 | int addr = (int)data; |
| 1471 | int ret; |
| 1472 | u8 temp; |
| 1473 | |
| 1474 | ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp); |
| 1475 | if (ret) { |
| 1476 | pr_err("pm8xxx_readb to %x value = %d errored = %d\n", |
| 1477 | addr, temp, ret); |
| 1478 | return -EAGAIN; |
| 1479 | } |
| 1480 | *val = temp; |
| 1481 | return 0; |
| 1482 | } |
| 1483 | |
| 1484 | static int set_reg(void *data, u64 val) |
| 1485 | { |
| 1486 | int addr = (int)data; |
| 1487 | int ret; |
| 1488 | u8 temp; |
| 1489 | |
| 1490 | temp = (u8) val; |
| 1491 | ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp); |
| 1492 | if (ret) { |
| 1493 | pr_err("pm8xxx_writeb to %x value = %d errored = %d\n", |
| 1494 | addr, temp, ret); |
| 1495 | return -EAGAIN; |
| 1496 | } |
| 1497 | return 0; |
| 1498 | } |
| 1499 | DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n"); |
| 1500 | |
| 1501 | static void create_debugfs_entries(struct pm8921_bms_chip *chip) |
| 1502 | { |
| 1503 | int i; |
| 1504 | |
| 1505 | chip->dent = debugfs_create_dir("pm8921_bms", NULL); |
| 1506 | |
| 1507 | if (IS_ERR(chip->dent)) { |
| 1508 | pr_err("pmic bms couldnt create debugfs dir\n"); |
| 1509 | return; |
| 1510 | } |
| 1511 | |
| 1512 | debugfs_create_file("BMS_CONTROL", 0644, chip->dent, |
| 1513 | (void *)BMS_CONTROL, ®_fops); |
| 1514 | debugfs_create_file("BMS_OUTPUT0", 0644, chip->dent, |
| 1515 | (void *)BMS_OUTPUT0, ®_fops); |
| 1516 | debugfs_create_file("BMS_OUTPUT1", 0644, chip->dent, |
| 1517 | (void *)BMS_OUTPUT1, ®_fops); |
| 1518 | debugfs_create_file("BMS_TEST1", 0644, chip->dent, |
| 1519 | (void *)BMS_TEST1, ®_fops); |
| 1520 | debugfs_create_file("CCADC_ANA_PARAM", 0644, chip->dent, |
| 1521 | (void *)CCADC_ANA_PARAM, ®_fops); |
| 1522 | debugfs_create_file("CCADC_DIG_PARAM", 0644, chip->dent, |
| 1523 | (void *)CCADC_DIG_PARAM, ®_fops); |
| 1524 | debugfs_create_file("CCADC_RSV", 0644, chip->dent, |
| 1525 | (void *)CCADC_RSV, ®_fops); |
| 1526 | debugfs_create_file("CCADC_DATA0", 0644, chip->dent, |
| 1527 | (void *)CCADC_DATA0, ®_fops); |
| 1528 | debugfs_create_file("CCADC_DATA1", 0644, chip->dent, |
| 1529 | (void *)CCADC_DATA1, ®_fops); |
| 1530 | debugfs_create_file("CCADC_OFFSET_TRIM1", 0644, chip->dent, |
| 1531 | (void *)CCADC_OFFSET_TRIM1, ®_fops); |
| 1532 | debugfs_create_file("CCADC_OFFSET_TRIM0", 0644, chip->dent, |
| 1533 | (void *)CCADC_OFFSET_TRIM0, ®_fops); |
| 1534 | |
| 1535 | debugfs_create_file("test_batt_temp", 0644, chip->dent, |
| 1536 | (void *)TEST_BATT_TEMP, &temp_fops); |
| 1537 | debugfs_create_file("test_chargecycle", 0644, chip->dent, |
| 1538 | (void *)TEST_CHARGE_CYCLE, &temp_fops); |
| 1539 | debugfs_create_file("test_ocv", 0644, chip->dent, |
| 1540 | (void *)TEST_OCV, &temp_fops); |
| 1541 | |
| 1542 | debugfs_create_file("read_cc", 0644, chip->dent, |
| 1543 | (void *)CC_MSB, &reading_fops); |
| 1544 | debugfs_create_file("read_last_good_ocv", 0644, chip->dent, |
| 1545 | (void *)LAST_GOOD_OCV_VALUE, &reading_fops); |
| 1546 | debugfs_create_file("read_vbatt_for_rbatt", 0644, chip->dent, |
| 1547 | (void *)VBATT_FOR_RBATT, &reading_fops); |
| 1548 | debugfs_create_file("read_vsense_for_rbatt", 0644, chip->dent, |
| 1549 | (void *)VSENSE_FOR_RBATT, &reading_fops); |
| 1550 | debugfs_create_file("read_ocv_for_rbatt", 0644, chip->dent, |
| 1551 | (void *)OCV_FOR_RBATT, &reading_fops); |
Abhijeet Dharmapurikar | 2fb6f08 | 2011-06-28 17:10:27 -0700 | [diff] [blame] | 1552 | debugfs_create_file("read_vsense_avg", 0644, chip->dent, |
| 1553 | (void *)VSENSE_AVG, &reading_fops); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1554 | |
| 1555 | debugfs_create_file("show_rbatt", 0644, chip->dent, |
| 1556 | (void *)CALC_RBATT, &calc_fops); |
| 1557 | debugfs_create_file("show_fcc", 0644, chip->dent, |
| 1558 | (void *)CALC_FCC, &calc_fops); |
| 1559 | debugfs_create_file("show_pc", 0644, chip->dent, |
| 1560 | (void *)CALC_PC, &calc_fops); |
| 1561 | debugfs_create_file("show_soc", 0644, chip->dent, |
| 1562 | (void *)CALC_SOC, &calc_fops); |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1563 | debugfs_create_file("calib_hkadc", 0644, chip->dent, |
| 1564 | (void *)CALIB_HKADC, &calc_fops); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1565 | |
| 1566 | for (i = 0; i < ARRAY_SIZE(bms_irq_data); i++) { |
| 1567 | if (chip->pmic_bms_irq[bms_irq_data[i].irq_id]) |
| 1568 | debugfs_create_file(bms_irq_data[i].name, 0444, |
| 1569 | chip->dent, |
| 1570 | (void *)bms_irq_data[i].irq_id, |
| 1571 | &rt_fops); |
| 1572 | } |
| 1573 | } |
| 1574 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1575 | static int __devinit pm8921_bms_probe(struct platform_device *pdev) |
| 1576 | { |
| 1577 | int rc = 0; |
| 1578 | struct pm8921_bms_chip *chip; |
| 1579 | const struct pm8921_bms_platform_data *pdata |
| 1580 | = pdev->dev.platform_data; |
| 1581 | if (!pdata) { |
| 1582 | pr_err("missing platform data\n"); |
| 1583 | return -EINVAL; |
| 1584 | } |
| 1585 | |
Abhijeet Dharmapurikar | 15e853d | 2011-06-27 20:55:47 -0700 | [diff] [blame] | 1586 | chip = kzalloc(sizeof(struct pm8921_bms_chip), GFP_KERNEL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1587 | if (!chip) { |
| 1588 | pr_err("Cannot allocate pm_bms_chip\n"); |
| 1589 | return -ENOMEM; |
| 1590 | } |
Abhijeet Dharmapurikar | 3f95fa8 | 2011-10-05 19:14:32 -0700 | [diff] [blame^] | 1591 | spin_lock_init(&chip->bms_output_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1592 | chip->dev = &pdev->dev; |
| 1593 | chip->r_sense = pdata->r_sense; |
| 1594 | chip->i_test = pdata->i_test; |
| 1595 | chip->v_failure = pdata->v_failure; |
| 1596 | chip->calib_delay_ms = pdata->calib_delay_ms; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 1597 | rc = set_battery_data(chip); |
| 1598 | if (rc) { |
| 1599 | pr_err("%s bad battery data %d\n", __func__, rc); |
Abhijeet Dharmapurikar | 4a4456e | 2011-09-27 15:10:38 -0700 | [diff] [blame] | 1600 | goto free_chip; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 1601 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1602 | |
Abhijeet Dharmapurikar | c448d98 | 2011-06-28 20:48:36 -0700 | [diff] [blame] | 1603 | chip->batt_temp_channel = pdata->bms_cdata.batt_temp_channel; |
| 1604 | chip->vbat_channel = pdata->bms_cdata.vbat_channel; |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1605 | chip->ref625mv_channel = pdata->bms_cdata.ref625mv_channel; |
| 1606 | chip->ref1p25v_channel = pdata->bms_cdata.ref1p25v_channel; |
Abhijeet Dharmapurikar | e922bbb | 2011-09-01 13:05:06 -0700 | [diff] [blame] | 1607 | chip->batt_id_channel = pdata->bms_cdata.batt_id_channel; |
Abhijeet Dharmapurikar | fa77f1a | 2011-06-27 21:11:51 -0700 | [diff] [blame] | 1608 | chip->revision = pm8xxx_get_revision(chip->dev->parent); |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1609 | INIT_WORK(&chip->calib_hkadc_work, calibrate_hkadc_work); |
Abhijeet Dharmapurikar | c448d98 | 2011-06-28 20:48:36 -0700 | [diff] [blame] | 1610 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1611 | rc = pm8921_bms_hw_init(chip); |
| 1612 | if (rc) { |
| 1613 | pr_err("couldn't init hardware rc = %d\n", rc); |
| 1614 | goto free_chip; |
| 1615 | } |
| 1616 | |
| 1617 | rc = request_irqs(chip, pdev); |
| 1618 | if (rc) { |
| 1619 | pr_err("couldn't register interrupts rc = %d\n", rc); |
| 1620 | goto free_chip; |
| 1621 | } |
| 1622 | |
| 1623 | platform_set_drvdata(pdev, chip); |
| 1624 | the_chip = chip; |
| 1625 | create_debugfs_entries(chip); |
| 1626 | |
Abhijeet Dharmapurikar | c353222 | 2011-07-20 14:48:02 -0700 | [diff] [blame] | 1627 | check_initial_ocv(chip); |
| 1628 | |
Abhijeet Dharmapurikar | 76d4db9 | 2011-07-19 11:54:52 -0700 | [diff] [blame] | 1629 | /* enable the vbatt reading interrupts for scheduling hkadc calib */ |
| 1630 | pm8921_bms_enable_irq(chip, PM8921_BMS_GOOD_OCV); |
| 1631 | pm8921_bms_enable_irq(chip, PM8921_BMS_OCV_FOR_R); |
Abhijeet Dharmapurikar | 21ebadd | 2011-09-28 21:14:16 -0700 | [diff] [blame] | 1632 | |
| 1633 | pr_info("OK battery_capacity_at_boot=%d\n", |
| 1634 | pm8921_bms_get_percent_charge()); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1635 | return 0; |
| 1636 | |
| 1637 | free_chip: |
| 1638 | kfree(chip); |
| 1639 | return rc; |
| 1640 | } |
| 1641 | |
| 1642 | static int __devexit pm8921_bms_remove(struct platform_device *pdev) |
| 1643 | { |
| 1644 | struct pm8921_bms_chip *chip = platform_get_drvdata(pdev); |
| 1645 | |
| 1646 | free_irqs(chip); |
| 1647 | platform_set_drvdata(pdev, NULL); |
| 1648 | the_chip = NULL; |
| 1649 | kfree(chip); |
| 1650 | return 0; |
| 1651 | } |
| 1652 | |
| 1653 | static struct platform_driver pm8921_bms_driver = { |
| 1654 | .probe = pm8921_bms_probe, |
| 1655 | .remove = __devexit_p(pm8921_bms_remove), |
| 1656 | .driver = { |
| 1657 | .name = PM8921_BMS_DEV_NAME, |
| 1658 | .owner = THIS_MODULE, |
| 1659 | }, |
| 1660 | }; |
| 1661 | |
| 1662 | static int __init pm8921_bms_init(void) |
| 1663 | { |
| 1664 | return platform_driver_register(&pm8921_bms_driver); |
| 1665 | } |
| 1666 | |
| 1667 | static void __exit pm8921_bms_exit(void) |
| 1668 | { |
| 1669 | platform_driver_unregister(&pm8921_bms_driver); |
| 1670 | } |
| 1671 | |
| 1672 | late_initcall(pm8921_bms_init); |
| 1673 | module_exit(pm8921_bms_exit); |
| 1674 | |
| 1675 | MODULE_LICENSE("GPL v2"); |
| 1676 | MODULE_DESCRIPTION("PMIC8921 bms driver"); |
| 1677 | MODULE_VERSION("1.0"); |
| 1678 | MODULE_ALIAS("platform:" PM8921_BMS_DEV_NAME); |