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