Xiaozhe Shi | 28f5dd5 | 2013-01-04 12:19:58 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved. |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -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 | |
Xiaozhe Shi | 73a6569 | 2012-09-18 17:51:57 -0700 | [diff] [blame] | 13 | #define pr_fmt(fmt) "BMS: %s: " fmt, __func__ |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/of.h> |
| 21 | #include <linux/of_device.h> |
| 22 | #include <linux/power_supply.h> |
| 23 | #include <linux/spmi.h> |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 24 | #include <linux/rtc.h> |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 25 | #include <linux/delay.h> |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 26 | #include <linux/qpnp/qpnp-adc.h> |
Xiaozhe Shi | 77ec38d | 2013-04-29 16:41:58 -0700 | [diff] [blame] | 27 | #include <linux/qpnp/power-on.h> |
Xiaozhe Shi | 73a6569 | 2012-09-18 17:51:57 -0700 | [diff] [blame] | 28 | #include <linux/mfd/pm8xxx/batterydata-lib.h> |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 29 | |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 30 | /* BMS Register Offsets */ |
| 31 | #define BMS1_REVISION1 0x0 |
| 32 | #define BMS1_REVISION2 0x1 |
| 33 | #define BMS1_STATUS1 0x8 |
| 34 | #define BMS1_MODE_CTL 0X40 |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 35 | /* Coulomb counter clear registers */ |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 36 | #define BMS1_CC_DATA_CTL 0x42 |
Xiaozhe Shi | a045a56 | 2012-11-28 16:55:39 -0800 | [diff] [blame] | 37 | #define BMS1_CC_CLEAR_CTL 0x43 |
Xiaozhe Shi | 20640b5 | 2013-01-03 11:49:30 -0800 | [diff] [blame] | 38 | /* BMS Tolerances */ |
| 39 | #define BMS1_TOL_CTL 0X44 |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 40 | /* OCV limit registers */ |
| 41 | #define BMS1_OCV_USE_LOW_LIMIT_THR0 0x48 |
| 42 | #define BMS1_OCV_USE_LOW_LIMIT_THR1 0x49 |
| 43 | #define BMS1_OCV_USE_HIGH_LIMIT_THR0 0x4A |
| 44 | #define BMS1_OCV_USE_HIGH_LIMIT_THR1 0x4B |
| 45 | #define BMS1_OCV_USE_LIMIT_CTL 0x4C |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 46 | /* Delay control */ |
| 47 | #define BMS1_S1_DELAY_CTL 0x5A |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 48 | /* CC interrupt threshold */ |
| 49 | #define BMS1_CC_THR0 0x7A |
| 50 | #define BMS1_CC_THR1 0x7B |
| 51 | #define BMS1_CC_THR2 0x7C |
| 52 | #define BMS1_CC_THR3 0x7D |
| 53 | #define BMS1_CC_THR4 0x7E |
| 54 | /* OCV for r registers */ |
| 55 | #define BMS1_OCV_FOR_R_DATA0 0x80 |
| 56 | #define BMS1_OCV_FOR_R_DATA1 0x81 |
| 57 | #define BMS1_VSENSE_FOR_R_DATA0 0x82 |
| 58 | #define BMS1_VSENSE_FOR_R_DATA1 0x83 |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 59 | /* Coulomb counter data */ |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 60 | #define BMS1_CC_DATA0 0x8A |
| 61 | #define BMS1_CC_DATA1 0x8B |
| 62 | #define BMS1_CC_DATA2 0x8C |
| 63 | #define BMS1_CC_DATA3 0x8D |
| 64 | #define BMS1_CC_DATA4 0x8E |
| 65 | /* OCV for soc data */ |
| 66 | #define BMS1_OCV_FOR_SOC_DATA0 0x90 |
| 67 | #define BMS1_OCV_FOR_SOC_DATA1 0x91 |
| 68 | #define BMS1_VSENSE_PON_DATA0 0x94 |
| 69 | #define BMS1_VSENSE_PON_DATA1 0x95 |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 70 | #define BMS1_VSENSE_AVG_DATA0 0x98 |
| 71 | #define BMS1_VSENSE_AVG_DATA1 0x99 |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 72 | #define BMS1_VBAT_AVG_DATA0 0x9E |
| 73 | #define BMS1_VBAT_AVG_DATA1 0x9F |
| 74 | /* Extra bms registers */ |
Xiaozhe Shi | 5705894 | 2013-03-27 16:54:54 -0700 | [diff] [blame] | 75 | #define SOC_STORAGE_REG 0xB0 |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 76 | #define IAVG_STORAGE_REG 0xB1 |
Xiaozhe Shi | 5705894 | 2013-03-27 16:54:54 -0700 | [diff] [blame] | 77 | #define BMS1_BMS_DATA_REG_2 0xB2 |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 78 | #define BMS1_BMS_DATA_REG_3 0xB3 |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 79 | /* IADC Channel Select */ |
| 80 | #define IADC1_BMS_ADC_CH_SEL_CTL 0x48 |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 81 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 82 | /* Configuration for saving of shutdown soc/iavg */ |
| 83 | #define IGNORE_SOC_TEMP_DECIDEG 50 |
| 84 | #define IAVG_STEP_SIZE_MA 50 |
| 85 | #define IAVG_START 600 |
Xiaozhe Shi | f5f966d | 2013-02-19 14:23:11 -0800 | [diff] [blame] | 86 | #define IAVG_INVALID 0xFF |
Xiaozhe Shi | c7cbd05 | 2013-03-29 12:03:11 -0700 | [diff] [blame] | 87 | #define SOC_INVALID 0xFF |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 88 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 89 | #define IAVG_SAMPLES 16 |
| 90 | |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 91 | #define QPNP_BMS_DEV_NAME "qcom,qpnp-bms" |
| 92 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 93 | struct soc_params { |
| 94 | int fcc_uah; |
| 95 | int cc_uah; |
Xiaozhe Shi | 904f1f7 | 2012-12-04 12:47:21 -0800 | [diff] [blame] | 96 | int rbatt_mohm; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 97 | int iavg_ua; |
| 98 | int uuc_uah; |
| 99 | int ocv_charge_uah; |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 100 | int delta_time_s; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | struct raw_soc_params { |
| 104 | uint16_t last_good_ocv_raw; |
| 105 | int64_t cc; |
| 106 | int last_good_ocv_uv; |
| 107 | }; |
| 108 | |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 109 | struct qpnp_bms_chip { |
| 110 | struct device *dev; |
| 111 | struct power_supply bms_psy; |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 112 | struct power_supply *batt_psy; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 113 | struct spmi_device *spmi; |
| 114 | u16 base; |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 115 | u16 iadc_base; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 116 | |
| 117 | u8 revision1; |
| 118 | u8 revision2; |
Xiaozhe Shi | d5d2141 | 2013-02-06 17:14:41 -0800 | [diff] [blame] | 119 | int battery_present; |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 120 | int battery_status; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 121 | bool new_battery; |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 122 | bool done_charging; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 123 | bool last_soc_invalid; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 124 | /* platform data */ |
Abhijeet Dharmapurikar | eef8866 | 2012-11-08 17:26:29 -0800 | [diff] [blame] | 125 | int r_sense_uohm; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 126 | unsigned int v_cutoff_uv; |
Abhijeet Dharmapurikar | eef8866 | 2012-11-08 17:26:29 -0800 | [diff] [blame] | 127 | int max_voltage_uv; |
| 128 | int r_conn_mohm; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 129 | int shutdown_soc_valid_limit; |
| 130 | int adjust_soc_low_threshold; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 131 | int chg_term_ua; |
Xiaozhe Shi | 73a6569 | 2012-09-18 17:51:57 -0700 | [diff] [blame] | 132 | enum battery_type batt_type; |
Xiaozhe Shi | 976618f | 2013-04-30 10:49:30 -0700 | [diff] [blame] | 133 | unsigned int fcc_mah; |
Xiaozhe Shi | 73a6569 | 2012-09-18 17:51:57 -0700 | [diff] [blame] | 134 | struct single_row_lut *fcc_temp_lut; |
| 135 | struct single_row_lut *fcc_sf_lut; |
| 136 | struct pc_temp_ocv_lut *pc_temp_ocv_lut; |
| 137 | struct sf_lut *pc_sf_lut; |
| 138 | struct sf_lut *rbatt_sf_lut; |
| 139 | int default_rbatt_mohm; |
Xiaozhe Shi | 1a10aff | 2013-04-01 15:40:05 -0700 | [diff] [blame] | 140 | int rbatt_capacitive_mohm; |
Xiaozhe Shi | 6dc56f1 | 2013-05-02 15:56:55 -0700 | [diff] [blame] | 141 | int rbatt_mohm; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 142 | |
| 143 | struct delayed_work calculate_soc_delayed_work; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 144 | struct work_struct recalc_work; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 145 | |
| 146 | struct mutex bms_output_lock; |
| 147 | struct mutex last_ocv_uv_mutex; |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 148 | struct mutex vbat_monitor_mutex; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 149 | struct mutex soc_invalidation_mutex; |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 150 | struct mutex last_soc_mutex; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 151 | |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 152 | bool use_external_rsense; |
Xiaozhe Shi | bdf1474 | 2012-12-05 12:41:48 -0800 | [diff] [blame] | 153 | bool use_ocv_thresholds; |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 154 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 155 | bool ignore_shutdown_soc; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 156 | bool shutdown_soc_invalid; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 157 | int shutdown_soc; |
| 158 | int shutdown_iavg_ma; |
| 159 | |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 160 | struct wake_lock low_voltage_wake_lock; |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 161 | int low_voltage_threshold; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 162 | int low_soc_calc_threshold; |
| 163 | int low_soc_calculate_soc_ms; |
| 164 | int calculate_soc_ms; |
Abhijeet Dharmapurikar | 713b60a | 2012-12-26 21:30:05 -0800 | [diff] [blame] | 165 | struct wake_lock soc_wake_lock; |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 166 | struct wake_lock cv_wake_lock; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 167 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 168 | uint16_t ocv_reading_at_100; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 169 | uint16_t prev_last_good_ocv_raw; |
| 170 | int last_ocv_uv; |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 171 | int last_ocv_temp; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 172 | int last_cc_uah; |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 173 | unsigned long last_soc_change_sec; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 174 | unsigned long tm_sec; |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 175 | unsigned long report_tm_sec; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 176 | bool first_time_calc_soc; |
| 177 | bool first_time_calc_uuc; |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 178 | int64_t software_cc_uah; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 179 | |
| 180 | int iavg_samples_ma[IAVG_SAMPLES]; |
| 181 | int iavg_index; |
| 182 | int iavg_num_samples; |
| 183 | struct timespec t_soc_queried; |
| 184 | int last_soc; |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 185 | int last_soc_est; |
Xiaozhe Shi | cc13726 | 2013-03-10 06:21:41 -0700 | [diff] [blame] | 186 | int last_soc_unbound; |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 187 | bool was_charging_at_sleep; |
| 188 | int charge_start_tm_sec; |
| 189 | int catch_up_time_sec; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 190 | struct single_row_lut *adjusted_fcc_temp_lut; |
| 191 | |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 192 | struct qpnp_adc_tm_btm_param vbat_monitor_params; |
Xiaozhe Shi | 535494d | 2013-04-05 12:27:51 -0700 | [diff] [blame] | 193 | struct qpnp_adc_tm_btm_param die_temp_monitor_params; |
| 194 | int temperature_margin; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 195 | unsigned int vadc_v0625; |
| 196 | unsigned int vadc_v1250; |
| 197 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 198 | int prev_uuc_iavg_ma; |
| 199 | int prev_pc_unusable; |
| 200 | int ibat_at_cv_ua; |
| 201 | int soc_at_cv; |
| 202 | int prev_chg_soc; |
| 203 | int calculated_soc; |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 204 | int prev_voltage_based_soc; |
| 205 | bool use_voltage_soc; |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 206 | bool in_cv_range; |
Xiaozhe Shi | bdf1474 | 2012-12-05 12:41:48 -0800 | [diff] [blame] | 207 | |
Xiaozhe Shi | fc2f5a0 | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 208 | int prev_batt_terminal_uv; |
Xiaozhe Shi | 0ac7a00 | 2013-03-26 13:14:03 -0700 | [diff] [blame] | 209 | int high_ocv_correction_limit_uv; |
| 210 | int low_ocv_correction_limit_uv; |
| 211 | int flat_ocv_threshold_uv; |
| 212 | int hold_soc_est; |
Xiaozhe Shi | fc2f5a0 | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 213 | |
Xiaozhe Shi | bdf1474 | 2012-12-05 12:41:48 -0800 | [diff] [blame] | 214 | int ocv_high_threshold_uv; |
| 215 | int ocv_low_threshold_uv; |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 216 | unsigned long last_recalc_time; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | static struct of_device_id qpnp_bms_match_table[] = { |
| 220 | { .compatible = QPNP_BMS_DEV_NAME }, |
| 221 | {} |
| 222 | }; |
| 223 | |
| 224 | static char *qpnp_bms_supplicants[] = { |
| 225 | "battery" |
| 226 | }; |
| 227 | |
| 228 | static enum power_supply_property msm_bms_power_props[] = { |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 229 | POWER_SUPPLY_PROP_CAPACITY, |
| 230 | POWER_SUPPLY_PROP_CURRENT_NOW, |
Xiaozhe Shi | 6dc56f1 | 2013-05-02 15:56:55 -0700 | [diff] [blame] | 231 | POWER_SUPPLY_PROP_RESISTANCE, |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 232 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 233 | }; |
| 234 | |
Xiaozhe Shi | 20640b5 | 2013-01-03 11:49:30 -0800 | [diff] [blame] | 235 | static bool bms_reset; |
Xiaozhe Shi | 781b0a2 | 2012-11-05 17:18:27 -0800 | [diff] [blame] | 236 | |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 237 | static int qpnp_read_wrapper(struct qpnp_bms_chip *chip, u8 *val, |
| 238 | u16 base, int count) |
| 239 | { |
| 240 | int rc; |
| 241 | struct spmi_device *spmi = chip->spmi; |
| 242 | |
| 243 | rc = spmi_ext_register_readl(spmi->ctrl, spmi->sid, base, val, count); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 244 | if (rc) { |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 245 | pr_err("SPMI read failed rc=%d\n", rc); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 246 | return rc; |
| 247 | } |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 248 | return 0; |
| 249 | } |
| 250 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 251 | static int qpnp_write_wrapper(struct qpnp_bms_chip *chip, u8 *val, |
| 252 | u16 base, int count) |
| 253 | { |
| 254 | int rc; |
| 255 | struct spmi_device *spmi = chip->spmi; |
| 256 | |
| 257 | rc = spmi_ext_register_writel(spmi->ctrl, spmi->sid, base, val, count); |
| 258 | if (rc) { |
| 259 | pr_err("SPMI write failed rc=%d\n", rc); |
| 260 | return rc; |
| 261 | } |
| 262 | return 0; |
| 263 | } |
| 264 | |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 265 | static int qpnp_masked_write_base(struct qpnp_bms_chip *chip, u16 addr, |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 266 | u8 mask, u8 val) |
| 267 | { |
| 268 | int rc; |
| 269 | u8 reg; |
| 270 | |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 271 | rc = qpnp_read_wrapper(chip, ®, addr, 1); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 272 | if (rc) { |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 273 | pr_err("read failed addr = %03X, rc = %d\n", addr, rc); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 274 | return rc; |
| 275 | } |
| 276 | reg &= ~mask; |
| 277 | reg |= val & mask; |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 278 | rc = qpnp_write_wrapper(chip, ®, addr, 1); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 279 | if (rc) { |
| 280 | pr_err("write failed addr = %03X, val = %02x, mask = %02x, reg = %02x, rc = %d\n", |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 281 | addr, val, mask, reg, rc); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 282 | return rc; |
| 283 | } |
| 284 | return 0; |
| 285 | } |
| 286 | |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 287 | static int qpnp_masked_write_iadc(struct qpnp_bms_chip *chip, u16 addr, |
| 288 | u8 mask, u8 val) |
| 289 | { |
| 290 | return qpnp_masked_write_base(chip, chip->iadc_base + addr, mask, val); |
| 291 | } |
| 292 | |
| 293 | static int qpnp_masked_write(struct qpnp_bms_chip *chip, u16 addr, |
| 294 | u8 mask, u8 val) |
| 295 | { |
| 296 | return qpnp_masked_write_base(chip, chip->base + addr, mask, val); |
| 297 | } |
| 298 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 299 | #define HOLD_OREG_DATA BIT(0) |
| 300 | static int lock_output_data(struct qpnp_bms_chip *chip) |
| 301 | { |
| 302 | int rc; |
| 303 | |
| 304 | rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL, |
| 305 | HOLD_OREG_DATA, HOLD_OREG_DATA); |
| 306 | if (rc) { |
| 307 | pr_err("couldnt lock bms output rc = %d\n", rc); |
| 308 | return rc; |
| 309 | } |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static int unlock_output_data(struct qpnp_bms_chip *chip) |
| 314 | { |
| 315 | int rc; |
| 316 | |
| 317 | rc = qpnp_masked_write(chip, BMS1_CC_DATA_CTL, HOLD_OREG_DATA, 0); |
| 318 | if (rc) { |
| 319 | pr_err("fail to unlock BMS_CONTROL rc = %d\n", rc); |
| 320 | return rc; |
| 321 | } |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | #define V_PER_BIT_MUL_FACTOR 97656 |
| 326 | #define V_PER_BIT_DIV_FACTOR 1000 |
| 327 | #define VADC_INTRINSIC_OFFSET 0x6000 |
| 328 | |
Xiaozhe Shi | bdf1474 | 2012-12-05 12:41:48 -0800 | [diff] [blame] | 329 | static int vadc_reading_to_uv(int reading) |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 330 | { |
| 331 | if (reading <= VADC_INTRINSIC_OFFSET) |
| 332 | return 0; |
| 333 | |
| 334 | return (reading - VADC_INTRINSIC_OFFSET) |
| 335 | * V_PER_BIT_MUL_FACTOR / V_PER_BIT_DIV_FACTOR; |
| 336 | } |
| 337 | |
| 338 | #define VADC_CALIB_UV 625000 |
| 339 | #define VBATT_MUL_FACTOR 3 |
| 340 | |
Xiaozhe Shi | bdf1474 | 2012-12-05 12:41:48 -0800 | [diff] [blame] | 341 | static int adjust_vbatt_reading(struct qpnp_bms_chip *chip, int reading_uv) |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 342 | { |
| 343 | s64 numerator, denominator; |
| 344 | |
| 345 | if (reading_uv == 0) |
| 346 | return 0; |
| 347 | |
| 348 | /* don't adjust if not calibrated */ |
| 349 | if (chip->vadc_v0625 == 0 || chip->vadc_v1250 == 0) { |
| 350 | pr_debug("No cal yet return %d\n", |
| 351 | VBATT_MUL_FACTOR * reading_uv); |
| 352 | return VBATT_MUL_FACTOR * reading_uv; |
| 353 | } |
| 354 | |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 355 | numerator = ((s64)reading_uv - chip->vadc_v0625) * VADC_CALIB_UV; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 356 | denominator = (s64)chip->vadc_v1250 - chip->vadc_v0625; |
| 357 | if (denominator == 0) |
| 358 | return reading_uv * VBATT_MUL_FACTOR; |
| 359 | return (VADC_CALIB_UV + div_s64(numerator, denominator)) |
| 360 | * VBATT_MUL_FACTOR; |
| 361 | } |
| 362 | |
Xiaozhe Shi | bdf1474 | 2012-12-05 12:41:48 -0800 | [diff] [blame] | 363 | static int convert_vbatt_uv_to_raw(struct qpnp_bms_chip *chip, |
| 364 | int unadjusted_vbatt) |
| 365 | { |
| 366 | int scaled_vbatt = unadjusted_vbatt / VBATT_MUL_FACTOR; |
| 367 | |
| 368 | if (scaled_vbatt <= 0) |
| 369 | return VADC_INTRINSIC_OFFSET; |
| 370 | return ((scaled_vbatt * V_PER_BIT_DIV_FACTOR) / V_PER_BIT_MUL_FACTOR) |
| 371 | + VADC_INTRINSIC_OFFSET; |
| 372 | } |
| 373 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 374 | static inline int convert_vbatt_raw_to_uv(struct qpnp_bms_chip *chip, |
| 375 | uint16_t reading) |
| 376 | { |
Xiaozhe Shi | 4dbc01b | 2013-04-30 11:27:52 -0700 | [diff] [blame] | 377 | int64_t uv; |
| 378 | int rc; |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 379 | |
| 380 | uv = vadc_reading_to_uv(reading); |
Xiaozhe Shi | 4dbc01b | 2013-04-30 11:27:52 -0700 | [diff] [blame] | 381 | pr_debug("%u raw converted into %lld uv\n", reading, uv); |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 382 | uv = adjust_vbatt_reading(chip, uv); |
Xiaozhe Shi | 4dbc01b | 2013-04-30 11:27:52 -0700 | [diff] [blame] | 383 | pr_debug("adjusted into %lld uv\n", uv); |
| 384 | rc = qpnp_vbat_sns_comp_result(&uv); |
| 385 | if (rc) |
| 386 | pr_debug("could not compensate vbatt\n"); |
| 387 | pr_debug("compensated into %lld uv\n", uv); |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 388 | return uv; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | #define CC_READING_RESOLUTION_N 542535 |
| 392 | #define CC_READING_RESOLUTION_D 100000 |
Xiaozhe Shi | 8f5dd1b | 2013-04-30 10:27:58 -0700 | [diff] [blame] | 393 | static s64 cc_reading_to_uv(s64 reading) |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 394 | { |
| 395 | return div_s64(reading * CC_READING_RESOLUTION_N, |
| 396 | CC_READING_RESOLUTION_D); |
| 397 | } |
| 398 | |
Xiaozhe Shi | 0c48493 | 2013-02-05 16:14:10 -0800 | [diff] [blame] | 399 | #define QPNP_ADC_GAIN_IDEAL 3291LL |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 400 | static s64 cc_adjust_for_gain(s64 uv, uint16_t gain) |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 401 | { |
| 402 | s64 result_uv; |
| 403 | |
| 404 | pr_debug("adjusting_uv = %lld\n", uv); |
Xiaozhe Shi | 820a47a | 2012-11-27 13:23:27 -0800 | [diff] [blame] | 405 | if (gain == 0) { |
| 406 | pr_debug("gain is %d, not adjusting\n", gain); |
| 407 | return uv; |
| 408 | } |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 409 | pr_debug("adjusting by factor: %lld/%hu = %lld%%\n", |
Xiaozhe Shi | 0c48493 | 2013-02-05 16:14:10 -0800 | [diff] [blame] | 410 | QPNP_ADC_GAIN_IDEAL, gain, |
| 411 | div_s64(QPNP_ADC_GAIN_IDEAL * 100LL, (s64)gain)); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 412 | |
Xiaozhe Shi | 0c48493 | 2013-02-05 16:14:10 -0800 | [diff] [blame] | 413 | result_uv = div_s64(uv * QPNP_ADC_GAIN_IDEAL, (s64)gain); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 414 | pr_debug("result_uv = %lld\n", result_uv); |
| 415 | return result_uv; |
| 416 | } |
| 417 | |
| 418 | static int convert_vsense_to_uv(struct qpnp_bms_chip *chip, |
| 419 | int16_t reading) |
| 420 | { |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 421 | struct qpnp_iadc_calib calibration; |
| 422 | |
| 423 | qpnp_iadc_get_gain_and_offset(&calibration); |
| 424 | return cc_adjust_for_gain(cc_reading_to_uv(reading), |
Xiaozhe Shi | 0c48493 | 2013-02-05 16:14:10 -0800 | [diff] [blame] | 425 | calibration.gain_raw - calibration.offset_raw); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | static int read_vsense_avg(struct qpnp_bms_chip *chip, int *result_uv) |
| 429 | { |
| 430 | int rc; |
| 431 | int16_t reading; |
| 432 | |
| 433 | rc = qpnp_read_wrapper(chip, (u8 *)&reading, |
| 434 | chip->base + BMS1_VSENSE_AVG_DATA0, 2); |
| 435 | |
| 436 | if (rc) { |
| 437 | pr_err("fail to read VSENSE_AVG rc = %d\n", rc); |
| 438 | return rc; |
| 439 | } |
| 440 | |
| 441 | *result_uv = convert_vsense_to_uv(chip, reading); |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | static int get_battery_current(struct qpnp_bms_chip *chip, int *result_ua) |
| 446 | { |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 447 | int rc, vsense_uv = 0; |
| 448 | int64_t temp_current; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 449 | |
Xiaozhe Shi | d0a7954 | 2012-11-06 10:00:38 -0800 | [diff] [blame] | 450 | if (chip->r_sense_uohm == 0) { |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 451 | pr_err("r_sense is zero\n"); |
| 452 | return -EINVAL; |
| 453 | } |
| 454 | |
| 455 | mutex_lock(&chip->bms_output_lock); |
| 456 | lock_output_data(chip); |
| 457 | read_vsense_avg(chip, &vsense_uv); |
| 458 | unlock_output_data(chip); |
| 459 | mutex_unlock(&chip->bms_output_lock); |
| 460 | |
| 461 | pr_debug("vsense_uv=%duV\n", vsense_uv); |
| 462 | /* cast for signed division */ |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 463 | temp_current = div_s64((vsense_uv * 1000000LL), |
| 464 | (int)chip->r_sense_uohm); |
| 465 | |
| 466 | rc = qpnp_iadc_comp_result(&temp_current); |
| 467 | if (rc) |
| 468 | pr_debug("error compensation failed: %d\n", rc); |
| 469 | |
| 470 | *result_ua = temp_current; |
| 471 | pr_debug("err compensated ibat=%duA\n", *result_ua); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | static int get_battery_voltage(int *result_uv) |
| 476 | { |
| 477 | int rc; |
| 478 | struct qpnp_vadc_result adc_result; |
| 479 | |
| 480 | rc = qpnp_vadc_read(VBAT_SNS, &adc_result); |
| 481 | if (rc) { |
| 482 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 483 | VBAT_SNS, rc); |
| 484 | return rc; |
| 485 | } |
| 486 | pr_debug("mvolts phy = %lld meas = 0x%llx\n", adc_result.physical, |
| 487 | adc_result.measurement); |
| 488 | *result_uv = (int)adc_result.physical; |
| 489 | return 0; |
| 490 | } |
| 491 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 492 | #define CC_36_BIT_MASK 0xFFFFFFFFFLL |
| 493 | |
| 494 | static int read_cc_raw(struct qpnp_bms_chip *chip, int64_t *reading) |
| 495 | { |
| 496 | int64_t raw_reading; |
| 497 | int rc; |
| 498 | |
| 499 | rc = qpnp_read_wrapper(chip, (u8 *)&raw_reading, |
| 500 | chip->base + BMS1_CC_DATA0, 5); |
| 501 | if (rc) { |
| 502 | pr_err("Error reading cc: rc = %d\n", rc); |
| 503 | return -ENXIO; |
| 504 | } |
| 505 | |
| 506 | raw_reading = raw_reading & CC_36_BIT_MASK; |
| 507 | /* convert 36 bit signed value into 64 signed value */ |
| 508 | *reading = (raw_reading >> 35) == 0LL ? |
| 509 | raw_reading : ((-1LL ^ CC_36_BIT_MASK) | raw_reading); |
| 510 | pr_debug("before conversion: %llx, after conversion: %llx\n", |
| 511 | raw_reading, *reading); |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 516 | static int calib_vadc(struct qpnp_bms_chip *chip) |
| 517 | { |
Xiaozhe Shi | f62c015 | 2013-03-28 17:57:19 -0700 | [diff] [blame] | 518 | int rc, raw_0625, raw_1250; |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 519 | struct qpnp_vadc_result result; |
| 520 | |
| 521 | rc = qpnp_vadc_read(REF_625MV, &result); |
| 522 | if (rc) { |
| 523 | pr_debug("vadc read failed with rc = %d\n", rc); |
| 524 | return rc; |
| 525 | } |
Xiaozhe Shi | f62c015 | 2013-03-28 17:57:19 -0700 | [diff] [blame] | 526 | raw_0625 = result.adc_code; |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 527 | |
| 528 | rc = qpnp_vadc_read(REF_125V, &result); |
| 529 | if (rc) { |
| 530 | pr_debug("vadc read failed with rc = %d\n", rc); |
| 531 | return rc; |
| 532 | } |
Xiaozhe Shi | f62c015 | 2013-03-28 17:57:19 -0700 | [diff] [blame] | 533 | raw_1250 = result.adc_code; |
| 534 | chip->vadc_v0625 = vadc_reading_to_uv(raw_0625); |
| 535 | chip->vadc_v1250 = vadc_reading_to_uv(raw_1250); |
| 536 | pr_debug("vadc calib: 0625 = %d raw (%d uv), 1250 = %d raw (%d uv)\n", |
| 537 | raw_0625, chip->vadc_v0625, |
| 538 | raw_1250, chip->vadc_v1250); |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 539 | return 0; |
| 540 | } |
| 541 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 542 | static void convert_and_store_ocv(struct qpnp_bms_chip *chip, |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 543 | struct raw_soc_params *raw, |
| 544 | int batt_temp) |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 545 | { |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 546 | int rc; |
| 547 | |
| 548 | pr_debug("prev_last_good_ocv_raw = %d, last_good_ocv_raw = %d\n", |
| 549 | chip->prev_last_good_ocv_raw, |
| 550 | raw->last_good_ocv_raw); |
| 551 | rc = calib_vadc(chip); |
| 552 | if (rc) |
| 553 | pr_err("Vadc reference voltage read failed, rc = %d\n", rc); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 554 | chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw; |
| 555 | raw->last_good_ocv_uv = convert_vbatt_raw_to_uv(chip, |
| 556 | raw->last_good_ocv_raw); |
| 557 | chip->last_ocv_uv = raw->last_good_ocv_uv; |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 558 | chip->last_ocv_temp = batt_temp; |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 559 | chip->software_cc_uah = 0; |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 560 | pr_debug("last_good_ocv_uv = %d\n", raw->last_good_ocv_uv); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Xiaozhe Shi | a045a56 | 2012-11-28 16:55:39 -0800 | [diff] [blame] | 563 | #define CLEAR_CC BIT(7) |
| 564 | #define CLEAR_SW_CC BIT(6) |
| 565 | /** |
| 566 | * reset both cc and sw-cc. |
| 567 | * note: this should only be ever called from one thread |
| 568 | * or there may be a race condition where CC is never enabled |
| 569 | * again |
| 570 | */ |
| 571 | static void reset_cc(struct qpnp_bms_chip *chip) |
| 572 | { |
| 573 | int rc; |
| 574 | |
| 575 | pr_debug("resetting cc manually\n"); |
| 576 | rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL, |
| 577 | CLEAR_CC | CLEAR_SW_CC, |
| 578 | CLEAR_CC | CLEAR_SW_CC); |
| 579 | if (rc) |
| 580 | pr_err("cc reset failed: %d\n", rc); |
| 581 | |
| 582 | /* wait for 100us for cc to reset */ |
| 583 | udelay(100); |
| 584 | |
| 585 | rc = qpnp_masked_write(chip, BMS1_CC_CLEAR_CTL, |
| 586 | CLEAR_CC | CLEAR_SW_CC, 0); |
| 587 | if (rc) |
| 588 | pr_err("cc reenable failed: %d\n", rc); |
| 589 | } |
| 590 | |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 591 | static int get_battery_status(struct qpnp_bms_chip *chip) |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 592 | { |
| 593 | union power_supply_propval ret = {0,}; |
| 594 | |
| 595 | if (chip->batt_psy == NULL) |
| 596 | chip->batt_psy = power_supply_get_by_name("battery"); |
| 597 | if (chip->batt_psy) { |
| 598 | /* if battery has been registered, use the status property */ |
| 599 | chip->batt_psy->get_property(chip->batt_psy, |
| 600 | POWER_SUPPLY_PROP_STATUS, &ret); |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 601 | return ret.intval; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /* Default to false if the battery power supply is not registered. */ |
| 605 | pr_debug("battery power supply is not registered\n"); |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 606 | return POWER_SUPPLY_STATUS_UNKNOWN; |
| 607 | } |
| 608 | |
| 609 | static bool is_battery_charging(struct qpnp_bms_chip *chip) |
| 610 | { |
| 611 | return get_battery_status(chip) == POWER_SUPPLY_STATUS_CHARGING; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 612 | } |
| 613 | |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 614 | static bool is_battery_present(struct qpnp_bms_chip *chip) |
| 615 | { |
| 616 | union power_supply_propval ret = {0,}; |
| 617 | |
| 618 | if (chip->batt_psy == NULL) |
| 619 | chip->batt_psy = power_supply_get_by_name("battery"); |
| 620 | if (chip->batt_psy) { |
| 621 | /* if battery has been registered, use the status property */ |
| 622 | chip->batt_psy->get_property(chip->batt_psy, |
| 623 | POWER_SUPPLY_PROP_PRESENT, &ret); |
| 624 | return ret.intval; |
| 625 | } |
| 626 | |
| 627 | /* Default to false if the battery power supply is not registered. */ |
| 628 | pr_debug("battery power supply is not registered\n"); |
| 629 | return false; |
| 630 | } |
| 631 | |
Xiaozhe Shi | 8658c98 | 2013-04-30 11:33:07 -0700 | [diff] [blame] | 632 | static bool is_battery_full(struct qpnp_bms_chip *chip) |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 633 | { |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 634 | return get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | static int get_simultaneous_batt_v_and_i(struct qpnp_bms_chip *chip, |
| 638 | int *ibat_ua, int *vbat_uv) |
| 639 | { |
| 640 | struct qpnp_iadc_result i_result; |
| 641 | struct qpnp_vadc_result v_result; |
| 642 | enum qpnp_iadc_channels iadc_channel; |
| 643 | int rc; |
| 644 | |
| 645 | iadc_channel = chip->use_external_rsense ? |
| 646 | EXTERNAL_RSENSE : INTERNAL_RSENSE; |
Xiaozhe Shi | 8658c98 | 2013-04-30 11:33:07 -0700 | [diff] [blame] | 647 | if (is_battery_full(chip)) { |
| 648 | rc = get_battery_current(chip, ibat_ua); |
| 649 | if (rc) { |
| 650 | pr_err("bms current read failed with rc: %d\n", rc); |
| 651 | return rc; |
| 652 | } |
| 653 | rc = qpnp_vadc_read(VBAT_SNS, &v_result); |
| 654 | if (rc) { |
| 655 | pr_err("vadc read failed with rc: %d\n", rc); |
| 656 | return rc; |
| 657 | } |
| 658 | *vbat_uv = (int)v_result.physical; |
| 659 | } else { |
| 660 | rc = qpnp_iadc_vadc_sync_read(iadc_channel, &i_result, |
| 661 | VBAT_SNS, &v_result); |
| 662 | if (rc) { |
| 663 | pr_err("adc sync read failed with rc: %d\n", rc); |
| 664 | return rc; |
| 665 | } |
| 666 | /* |
| 667 | * reverse the current read by the iadc, since the bms uses |
| 668 | * flipped battery current polarity. |
| 669 | */ |
| 670 | *ibat_ua = -1 * (int)i_result.result_ua; |
| 671 | *vbat_uv = (int)v_result.physical; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 672 | } |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | static int estimate_ocv(struct qpnp_bms_chip *chip) |
| 678 | { |
| 679 | int ibat_ua, vbat_uv, ocv_est_uv; |
| 680 | int rc; |
Xiaozhe Shi | 1a10aff | 2013-04-01 15:40:05 -0700 | [diff] [blame] | 681 | int rbatt_mohm = chip->default_rbatt_mohm + chip->r_conn_mohm |
| 682 | + chip->rbatt_capacitive_mohm; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 683 | |
| 684 | rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv); |
| 685 | if (rc) { |
| 686 | pr_err("simultaneous failed rc = %d\n", rc); |
| 687 | return rc; |
| 688 | } |
| 689 | |
| 690 | ocv_est_uv = vbat_uv + (ibat_ua * rbatt_mohm) / 1000; |
| 691 | pr_debug("estimated pon ocv = %d\n", ocv_est_uv); |
| 692 | return ocv_est_uv; |
| 693 | } |
| 694 | |
| 695 | static void reset_for_new_battery(struct qpnp_bms_chip *chip, int batt_temp) |
| 696 | { |
| 697 | chip->last_ocv_uv = estimate_ocv(chip); |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 698 | mutex_lock(&chip->last_soc_mutex); |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 699 | chip->last_soc = -EINVAL; |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 700 | chip->last_soc_invalid = true; |
| 701 | mutex_unlock(&chip->last_soc_mutex); |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 702 | chip->soc_at_cv = -EINVAL; |
| 703 | chip->shutdown_soc_invalid = true; |
| 704 | chip->shutdown_soc = 0; |
| 705 | chip->shutdown_iavg_ma = 0; |
| 706 | chip->prev_pc_unusable = -EINVAL; |
| 707 | reset_cc(chip); |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 708 | chip->software_cc_uah = 0; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 709 | chip->last_cc_uah = INT_MIN; |
| 710 | chip->last_ocv_temp = batt_temp; |
Xiaozhe Shi | fc2f5a0 | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 711 | chip->prev_batt_terminal_uv = 0; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 712 | } |
| 713 | |
Abhijeet Dharmapurikar | 15f30fb | 2012-12-27 17:20:29 -0800 | [diff] [blame] | 714 | #define OCV_RAW_UNINITIALIZED 0xFFFF |
Xiaozhe Shi | 77ec38d | 2013-04-29 16:41:58 -0700 | [diff] [blame] | 715 | #define MIN_OCV_UV 2000000 |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 716 | static int read_soc_params_raw(struct qpnp_bms_chip *chip, |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 717 | struct raw_soc_params *raw, |
| 718 | int batt_temp) |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 719 | { |
Xiaozhe Shi | 77ec38d | 2013-04-29 16:41:58 -0700 | [diff] [blame] | 720 | bool warm_reset = false; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 721 | int rc; |
| 722 | |
| 723 | mutex_lock(&chip->bms_output_lock); |
Xiaozhe Shi | a045a56 | 2012-11-28 16:55:39 -0800 | [diff] [blame] | 724 | |
Abhijeet Dharmapurikar | 15f30fb | 2012-12-27 17:20:29 -0800 | [diff] [blame] | 725 | if (chip->prev_last_good_ocv_raw == OCV_RAW_UNINITIALIZED) { |
Xiaozhe Shi | a045a56 | 2012-11-28 16:55:39 -0800 | [diff] [blame] | 726 | /* software workaround for BMS 1.0 |
| 727 | * The coulomb counter does not reset upon PON, so reset it |
| 728 | * manually upon probe. */ |
| 729 | if (chip->revision1 == 0 && chip->revision2 == 0) |
| 730 | reset_cc(chip); |
| 731 | } |
| 732 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 733 | lock_output_data(chip); |
| 734 | |
| 735 | rc = qpnp_read_wrapper(chip, (u8 *)&raw->last_good_ocv_raw, |
| 736 | chip->base + BMS1_OCV_FOR_SOC_DATA0, 2); |
| 737 | if (rc) { |
| 738 | pr_err("Error reading ocv: rc = %d\n", rc); |
| 739 | return -ENXIO; |
| 740 | } |
| 741 | |
| 742 | rc = read_cc_raw(chip, &raw->cc); |
| 743 | if (rc) { |
| 744 | pr_err("Failed to read raw cc data, rc = %d\n", rc); |
| 745 | return rc; |
| 746 | } |
| 747 | |
| 748 | unlock_output_data(chip); |
| 749 | mutex_unlock(&chip->bms_output_lock); |
| 750 | |
Abhijeet Dharmapurikar | 15f30fb | 2012-12-27 17:20:29 -0800 | [diff] [blame] | 751 | if (chip->prev_last_good_ocv_raw == OCV_RAW_UNINITIALIZED) { |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 752 | convert_and_store_ocv(chip, raw, batt_temp); |
Xiaozhe Shi | 77ec38d | 2013-04-29 16:41:58 -0700 | [diff] [blame] | 753 | pr_debug("PON_OCV_UV = %d, cc = %llx\n", |
| 754 | chip->last_ocv_uv, raw->cc); |
| 755 | warm_reset = qpnp_pon_is_warm_reset(); |
| 756 | if (raw->last_good_ocv_uv < MIN_OCV_UV |
| 757 | || warm_reset > 0) { |
| 758 | pr_debug("OCV is stale or bad, estimating new OCV.\n"); |
| 759 | chip->last_ocv_uv = estimate_ocv(chip); |
| 760 | raw->last_good_ocv_uv = chip->last_ocv_uv; |
| 761 | reset_cc(chip); |
| 762 | pr_debug("New PON_OCV_UV = %d, cc = %llx\n", |
| 763 | chip->last_ocv_uv, raw->cc); |
| 764 | } |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 765 | } else if (chip->new_battery) { |
| 766 | /* if a new battery was inserted, estimate the ocv */ |
| 767 | reset_for_new_battery(chip, batt_temp); |
| 768 | raw->cc = 0; |
| 769 | raw->last_good_ocv_uv = chip->last_ocv_uv; |
| 770 | chip->new_battery = false; |
Xiaozhe Shi | 74548b9 | 2013-05-02 16:47:08 -0700 | [diff] [blame] | 771 | } else if (chip->done_charging) { |
| 772 | chip->done_charging = false; |
| 773 | /* if we just finished charging, reset CC and fake 100% */ |
| 774 | chip->ocv_reading_at_100 = raw->last_good_ocv_raw; |
| 775 | chip->last_ocv_uv = chip->max_voltage_uv; |
| 776 | raw->last_good_ocv_uv = chip->max_voltage_uv; |
| 777 | raw->cc = 0; |
| 778 | reset_cc(chip); |
| 779 | chip->last_ocv_temp = batt_temp; |
| 780 | chip->software_cc_uah = 0; |
| 781 | chip->last_cc_uah = INT_MIN; |
| 782 | pr_debug("EOC Battery full ocv_reading = 0x%x\n", |
| 783 | chip->ocv_reading_at_100); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 784 | } else if (chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) { |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 785 | convert_and_store_ocv(chip, raw, batt_temp); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 786 | /* forget the old cc value upon ocv */ |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 787 | chip->last_cc_uah = INT_MIN; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 788 | } else { |
| 789 | raw->last_good_ocv_uv = chip->last_ocv_uv; |
| 790 | } |
| 791 | |
Xiaozhe Shi | 74548b9 | 2013-05-02 16:47:08 -0700 | [diff] [blame] | 792 | /* stop faking a high OCV if we get a new OCV */ |
| 793 | if (chip->ocv_reading_at_100 != raw->last_good_ocv_raw) |
Abhijeet Dharmapurikar | 15f30fb | 2012-12-27 17:20:29 -0800 | [diff] [blame] | 794 | chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED; |
Xiaozhe Shi | 74548b9 | 2013-05-02 16:47:08 -0700 | [diff] [blame] | 795 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 796 | pr_debug("last_good_ocv_raw= 0x%x, last_good_ocv_uv= %duV\n", |
| 797 | raw->last_good_ocv_raw, raw->last_good_ocv_uv); |
| 798 | pr_debug("cc_raw= 0x%llx\n", raw->cc); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 799 | return 0; |
| 800 | } |
| 801 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 802 | static int calculate_pc(struct qpnp_bms_chip *chip, int ocv_uv, |
| 803 | int batt_temp) |
| 804 | { |
| 805 | int pc; |
| 806 | |
| 807 | pc = interpolate_pc(chip->pc_temp_ocv_lut, |
| 808 | batt_temp / 10, ocv_uv / 1000); |
| 809 | pr_debug("pc = %u %% for ocv = %d uv batt_temp = %d\n", |
| 810 | pc, ocv_uv, batt_temp); |
| 811 | /* Multiply the initial FCC value by the scale factor. */ |
| 812 | return pc; |
| 813 | } |
| 814 | |
| 815 | static int calculate_fcc(struct qpnp_bms_chip *chip, int batt_temp) |
| 816 | { |
| 817 | int fcc_uah; |
| 818 | |
| 819 | if (chip->adjusted_fcc_temp_lut == NULL) { |
| 820 | /* interpolate_fcc returns a mv value. */ |
| 821 | fcc_uah = interpolate_fcc(chip->fcc_temp_lut, |
| 822 | batt_temp) * 1000; |
| 823 | pr_debug("fcc = %d uAh\n", fcc_uah); |
| 824 | return fcc_uah; |
| 825 | } else { |
| 826 | return 1000 * interpolate_fcc(chip->adjusted_fcc_temp_lut, |
| 827 | batt_temp); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | /* calculate remaining charge at the time of ocv */ |
| 832 | static int calculate_ocv_charge(struct qpnp_bms_chip *chip, |
| 833 | struct raw_soc_params *raw, |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 834 | int fcc_uah) |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 835 | { |
| 836 | int ocv_uv, pc; |
| 837 | |
| 838 | ocv_uv = raw->last_good_ocv_uv; |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 839 | pc = calculate_pc(chip, ocv_uv, chip->last_ocv_temp); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 840 | pr_debug("ocv_uv = %d pc = %d\n", ocv_uv, pc); |
| 841 | return (fcc_uah * pc) / 100; |
| 842 | } |
| 843 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 844 | #define CC_READING_TICKS 56 |
| 845 | #define SLEEP_CLK_HZ 32764 |
| 846 | #define SECONDS_PER_HOUR 3600 |
| 847 | |
Xiaozhe Shi | a9b597d | 2013-02-12 11:00:39 -0800 | [diff] [blame] | 848 | static s64 cc_uv_to_pvh(s64 cc_uv) |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 849 | { |
Xiaozhe Shi | a9b597d | 2013-02-12 11:00:39 -0800 | [diff] [blame] | 850 | /* Note that it is necessary need to multiply by 1000000 to convert |
| 851 | * from uvh to pvh here. |
| 852 | * However, the maximum Coulomb Counter value is 2^35, which can cause |
| 853 | * an over flow. |
| 854 | * Multiply by 100000 first to perserve as much precision as possible |
| 855 | * then multiply by 10 after doing the division in order to avoid |
| 856 | * overflow on the maximum Coulomb Counter value. |
| 857 | */ |
| 858 | return div_s64(cc_uv * CC_READING_TICKS * 100000, |
| 859 | SLEEP_CLK_HZ * SECONDS_PER_HOUR) * 10; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | /** |
| 863 | * calculate_cc- |
| 864 | * @chip: the bms chip pointer |
| 865 | * @cc: the cc reading from bms h/w |
| 866 | * @val: return value |
| 867 | * @coulomb_counter: adjusted coulomb counter for 100% |
| 868 | * |
| 869 | * RETURNS: in val pointer coulomb counter based charger in uAh |
| 870 | * (micro Amp hour) |
| 871 | */ |
| 872 | static int calculate_cc(struct qpnp_bms_chip *chip, int64_t cc) |
| 873 | { |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 874 | struct qpnp_iadc_calib calibration; |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 875 | struct qpnp_vadc_result result; |
| 876 | int64_t cc_voltage_uv, cc_pvh, cc_uah; |
| 877 | int ibat_ua, rc; |
| 878 | |
| 879 | rc = qpnp_vadc_read(DIE_TEMP, &result); |
| 880 | if (rc) { |
| 881 | pr_err("could not read pmic die temperature: %d\n", rc); |
| 882 | return chip->software_cc_uah; |
| 883 | } |
| 884 | |
| 885 | rc = get_battery_current(chip, &ibat_ua); |
| 886 | if (rc) { |
| 887 | pr_err("could not read battery current: %d\n", rc); |
| 888 | return chip->software_cc_uah; |
| 889 | } |
Xiaozhe Shi | 4e37665 | 2012-10-25 12:38:50 -0700 | [diff] [blame] | 890 | |
| 891 | qpnp_iadc_get_gain_and_offset(&calibration); |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 892 | pr_debug("cc = %lld, die_temp = %lld\n", cc, result.physical); |
Xiaozhe Shi | 8f5dd1b | 2013-04-30 10:27:58 -0700 | [diff] [blame] | 893 | cc_voltage_uv = cc_reading_to_uv(cc); |
Xiaozhe Shi | 0c48493 | 2013-02-05 16:14:10 -0800 | [diff] [blame] | 894 | cc_voltage_uv = cc_adjust_for_gain(cc_voltage_uv, |
| 895 | calibration.gain_raw |
| 896 | - calibration.offset_raw); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 897 | pr_debug("cc_voltage_uv = %lld uv\n", cc_voltage_uv); |
Xiaozhe Shi | a9b597d | 2013-02-12 11:00:39 -0800 | [diff] [blame] | 898 | cc_pvh = cc_uv_to_pvh(cc_voltage_uv); |
| 899 | pr_debug("cc_pvh = %lld pvh\n", cc_pvh); |
| 900 | cc_uah = div_s64(cc_pvh, chip->r_sense_uohm); |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 901 | rc = qpnp_iadc_comp_result(&cc_uah); |
| 902 | if (rc) |
| 903 | pr_debug("error compensation failed: %d\n", rc); |
| 904 | chip->software_cc_uah += cc_uah; |
| 905 | reset_cc(chip); |
| 906 | return (int)chip->software_cc_uah; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | static int get_rbatt(struct qpnp_bms_chip *chip, |
| 910 | int soc_rbatt_mohm, int batt_temp) |
| 911 | { |
| 912 | int rbatt_mohm, scalefactor; |
| 913 | |
| 914 | rbatt_mohm = chip->default_rbatt_mohm; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 915 | if (chip->rbatt_sf_lut == NULL) { |
| 916 | pr_debug("RBATT = %d\n", rbatt_mohm); |
| 917 | return rbatt_mohm; |
| 918 | } |
| 919 | /* Convert the batt_temp to DegC from deciDegC */ |
| 920 | batt_temp = batt_temp / 10; |
| 921 | scalefactor = interpolate_scalingfactor(chip->rbatt_sf_lut, |
| 922 | batt_temp, soc_rbatt_mohm); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 923 | rbatt_mohm = (rbatt_mohm * scalefactor) / 100; |
| 924 | |
| 925 | rbatt_mohm += chip->r_conn_mohm; |
Xiaozhe Shi | 1a10aff | 2013-04-01 15:40:05 -0700 | [diff] [blame] | 926 | rbatt_mohm += chip->rbatt_capacitive_mohm; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 927 | return rbatt_mohm; |
| 928 | } |
| 929 | |
Xiaozhe Shi | 06b67cc | 2013-03-29 12:07:40 -0700 | [diff] [blame] | 930 | #define IAVG_MINIMAL_TIME 2 |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 931 | static void calculate_iavg(struct qpnp_bms_chip *chip, int cc_uah, |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 932 | int *iavg_ua, int delta_time_s) |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 933 | { |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 934 | int delta_cc_uah = 0; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 935 | |
Xiaozhe Shi | 06b67cc | 2013-03-29 12:07:40 -0700 | [diff] [blame] | 936 | /* |
| 937 | * use the battery current if called too quickly |
| 938 | */ |
| 939 | if (delta_time_s < IAVG_MINIMAL_TIME |
| 940 | || chip->last_cc_uah == INT_MIN) { |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 941 | get_battery_current(chip, iavg_ua); |
| 942 | goto out; |
| 943 | } |
| 944 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 945 | delta_cc_uah = cc_uah - chip->last_cc_uah; |
| 946 | |
| 947 | *iavg_ua = div_s64((s64)delta_cc_uah * 3600, delta_time_s); |
| 948 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 949 | out: |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 950 | pr_debug("delta_cc = %d iavg_ua = %d\n", delta_cc_uah, (int)*iavg_ua); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 951 | |
| 952 | /* remember cc_uah */ |
| 953 | chip->last_cc_uah = cc_uah; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | static int calculate_termination_uuc(struct qpnp_bms_chip *chip, |
| 957 | struct soc_params *params, |
| 958 | int batt_temp, int uuc_iavg_ma, |
| 959 | int *ret_pc_unusable) |
| 960 | { |
| 961 | int unusable_uv, pc_unusable, uuc_uah; |
| 962 | int i = 0; |
| 963 | int ocv_mv; |
| 964 | int batt_temp_degc = batt_temp / 10; |
| 965 | int rbatt_mohm; |
| 966 | int delta_uv; |
| 967 | int prev_delta_uv = 0; |
| 968 | int prev_rbatt_mohm = 0; |
| 969 | int uuc_rbatt_mohm; |
| 970 | |
| 971 | for (i = 0; i <= 100; i++) { |
| 972 | ocv_mv = interpolate_ocv(chip->pc_temp_ocv_lut, |
| 973 | batt_temp_degc, i); |
| 974 | rbatt_mohm = get_rbatt(chip, i, batt_temp); |
| 975 | unusable_uv = (rbatt_mohm * uuc_iavg_ma) |
| 976 | + (chip->v_cutoff_uv); |
| 977 | delta_uv = ocv_mv * 1000 - unusable_uv; |
| 978 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 979 | if (delta_uv > 0) |
| 980 | break; |
| 981 | |
| 982 | prev_delta_uv = delta_uv; |
| 983 | prev_rbatt_mohm = rbatt_mohm; |
| 984 | } |
| 985 | |
| 986 | uuc_rbatt_mohm = linear_interpolate(rbatt_mohm, delta_uv, |
| 987 | prev_rbatt_mohm, prev_delta_uv, |
| 988 | 0); |
| 989 | |
| 990 | unusable_uv = (uuc_rbatt_mohm * uuc_iavg_ma) + (chip->v_cutoff_uv); |
| 991 | |
| 992 | pc_unusable = calculate_pc(chip, unusable_uv, batt_temp); |
| 993 | uuc_uah = (params->fcc_uah * pc_unusable) / 100; |
Xiaozhe Shi | 794607c | 2013-02-19 10:25:59 -0800 | [diff] [blame] | 994 | pr_debug("For uuc_iavg_ma = %d, unusable_rbatt = %d unusable_uv = %d unusable_pc = %d rbatt_pc = %d uuc = %d\n", |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 995 | uuc_iavg_ma, |
| 996 | uuc_rbatt_mohm, unusable_uv, |
Xiaozhe Shi | 794607c | 2013-02-19 10:25:59 -0800 | [diff] [blame] | 997 | pc_unusable, i, uuc_uah); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 998 | *ret_pc_unusable = pc_unusable; |
| 999 | return uuc_uah; |
| 1000 | } |
| 1001 | |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1002 | #define TIME_PER_PERCENT_UUC 60 |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1003 | static int adjust_uuc(struct qpnp_bms_chip *chip, |
| 1004 | struct soc_params *params, |
| 1005 | int new_pc_unusable, |
| 1006 | int new_uuc_uah, |
| 1007 | int batt_temp) |
| 1008 | { |
| 1009 | int new_unusable_mv, new_iavg_ma; |
| 1010 | int batt_temp_degc = batt_temp / 10; |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1011 | int max_percent_change; |
| 1012 | |
| 1013 | max_percent_change = max(params->delta_time_s |
| 1014 | / TIME_PER_PERCENT_UUC, 1); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1015 | |
| 1016 | if (chip->prev_pc_unusable == -EINVAL |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1017 | || abs(chip->prev_pc_unusable - new_pc_unusable) |
| 1018 | <= max_percent_change) { |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1019 | chip->prev_pc_unusable = new_pc_unusable; |
| 1020 | return new_uuc_uah; |
| 1021 | } |
| 1022 | |
| 1023 | /* the uuc is trying to change more than 1% restrict it */ |
| 1024 | if (new_pc_unusable > chip->prev_pc_unusable) |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1025 | chip->prev_pc_unusable += max_percent_change; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1026 | else |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1027 | chip->prev_pc_unusable -= max_percent_change; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1028 | |
| 1029 | new_uuc_uah = (params->fcc_uah * chip->prev_pc_unusable) / 100; |
| 1030 | |
| 1031 | /* also find update the iavg_ma accordingly */ |
| 1032 | new_unusable_mv = interpolate_ocv(chip->pc_temp_ocv_lut, |
| 1033 | batt_temp_degc, chip->prev_pc_unusable); |
| 1034 | if (new_unusable_mv < chip->v_cutoff_uv/1000) |
| 1035 | new_unusable_mv = chip->v_cutoff_uv/1000; |
| 1036 | |
| 1037 | new_iavg_ma = (new_unusable_mv * 1000 - chip->v_cutoff_uv) |
Xiaozhe Shi | 904f1f7 | 2012-12-04 12:47:21 -0800 | [diff] [blame] | 1038 | / params->rbatt_mohm; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1039 | if (new_iavg_ma == 0) |
| 1040 | new_iavg_ma = 1; |
| 1041 | chip->prev_uuc_iavg_ma = new_iavg_ma; |
| 1042 | pr_debug("Restricting UUC to %d (%d%%) unusable_mv = %d iavg_ma = %d\n", |
| 1043 | new_uuc_uah, chip->prev_pc_unusable, |
| 1044 | new_unusable_mv, new_iavg_ma); |
| 1045 | |
| 1046 | return new_uuc_uah; |
| 1047 | } |
| 1048 | |
Abhijeet Dharmapurikar | bdf8ba8 | 2012-12-20 18:33:56 -0800 | [diff] [blame] | 1049 | #define MIN_IAVG_MA 250 |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1050 | #define MIN_SECONDS_FOR_VALID_SAMPLE 20 |
| 1051 | static int calculate_unusable_charge_uah(struct qpnp_bms_chip *chip, |
| 1052 | struct soc_params *params, |
| 1053 | int batt_temp) |
| 1054 | { |
| 1055 | int uuc_uah_iavg; |
| 1056 | int i; |
| 1057 | int uuc_iavg_ma = params->iavg_ua / 1000; |
| 1058 | int pc_unusable; |
| 1059 | |
| 1060 | /* |
| 1061 | * if called first time, fill all the samples with |
| 1062 | * the shutdown_iavg_ma |
| 1063 | */ |
| 1064 | if (chip->first_time_calc_uuc && chip->shutdown_iavg_ma != 0) { |
| 1065 | pr_debug("Using shutdown_iavg_ma = %d in all samples\n", |
| 1066 | chip->shutdown_iavg_ma); |
| 1067 | for (i = 0; i < IAVG_SAMPLES; i++) |
| 1068 | chip->iavg_samples_ma[i] = chip->shutdown_iavg_ma; |
| 1069 | |
| 1070 | chip->iavg_index = 0; |
| 1071 | chip->iavg_num_samples = IAVG_SAMPLES; |
| 1072 | } |
| 1073 | |
| 1074 | /* |
| 1075 | * if charging use a nominal avg current to keep |
| 1076 | * a reasonable UUC while charging |
| 1077 | */ |
Abhijeet Dharmapurikar | bdf8ba8 | 2012-12-20 18:33:56 -0800 | [diff] [blame] | 1078 | if (uuc_iavg_ma < MIN_IAVG_MA) |
| 1079 | uuc_iavg_ma = MIN_IAVG_MA; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1080 | chip->iavg_samples_ma[chip->iavg_index] = uuc_iavg_ma; |
| 1081 | chip->iavg_index = (chip->iavg_index + 1) % IAVG_SAMPLES; |
| 1082 | chip->iavg_num_samples++; |
| 1083 | if (chip->iavg_num_samples >= IAVG_SAMPLES) |
| 1084 | chip->iavg_num_samples = IAVG_SAMPLES; |
| 1085 | |
| 1086 | /* now that this sample is added calcualte the average */ |
| 1087 | uuc_iavg_ma = 0; |
| 1088 | if (chip->iavg_num_samples != 0) { |
| 1089 | for (i = 0; i < chip->iavg_num_samples; i++) { |
| 1090 | pr_debug("iavg_samples_ma[%d] = %d\n", i, |
| 1091 | chip->iavg_samples_ma[i]); |
| 1092 | uuc_iavg_ma += chip->iavg_samples_ma[i]; |
| 1093 | } |
| 1094 | |
| 1095 | uuc_iavg_ma = DIV_ROUND_CLOSEST(uuc_iavg_ma, |
| 1096 | chip->iavg_num_samples); |
| 1097 | } |
| 1098 | |
Xiaozhe Shi | 20640b5 | 2013-01-03 11:49:30 -0800 | [diff] [blame] | 1099 | /* |
| 1100 | * if we're in bms reset mode, force uuc to be 3% of fcc |
| 1101 | */ |
| 1102 | if (bms_reset) |
| 1103 | return (params->fcc_uah * 3) / 100; |
| 1104 | |
Xiaozhe Shi | 75e5efe | 2013-02-07 09:51:43 -0800 | [diff] [blame] | 1105 | uuc_uah_iavg = calculate_termination_uuc(chip, params, batt_temp, |
| 1106 | uuc_iavg_ma, &pc_unusable); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1107 | pr_debug("uuc_iavg_ma = %d uuc with iavg = %d\n", |
| 1108 | uuc_iavg_ma, uuc_uah_iavg); |
| 1109 | |
| 1110 | chip->prev_uuc_iavg_ma = uuc_iavg_ma; |
| 1111 | /* restrict the uuc such that it can increase only by one percent */ |
| 1112 | uuc_uah_iavg = adjust_uuc(chip, params, pc_unusable, |
| 1113 | uuc_uah_iavg, batt_temp); |
| 1114 | |
| 1115 | chip->first_time_calc_uuc = 0; |
| 1116 | return uuc_uah_iavg; |
| 1117 | } |
| 1118 | |
| 1119 | static void find_ocv_for_soc(struct qpnp_bms_chip *chip, |
| 1120 | struct soc_params *params, |
| 1121 | int batt_temp, |
| 1122 | int shutdown_soc, |
| 1123 | int *ret_ocv_uv) |
| 1124 | { |
| 1125 | s64 ocv_charge_uah; |
| 1126 | int pc, new_pc; |
| 1127 | int batt_temp_degc = batt_temp / 10; |
| 1128 | int ocv_uv; |
| 1129 | |
| 1130 | ocv_charge_uah = (s64)shutdown_soc |
| 1131 | * (params->fcc_uah - params->uuc_uah); |
| 1132 | ocv_charge_uah = div_s64(ocv_charge_uah, 100) |
| 1133 | + params->cc_uah + params->uuc_uah; |
| 1134 | pc = DIV_ROUND_CLOSEST((int)ocv_charge_uah * 100, params->fcc_uah); |
| 1135 | pc = clamp(pc, 0, 100); |
| 1136 | |
| 1137 | ocv_uv = interpolate_ocv(chip->pc_temp_ocv_lut, batt_temp_degc, pc); |
| 1138 | |
| 1139 | pr_debug("s_soc = %d, fcc = %d uuc = %d rc = %d, pc = %d, ocv mv = %d\n", |
| 1140 | shutdown_soc, params->fcc_uah, |
| 1141 | params->uuc_uah, (int)ocv_charge_uah, |
| 1142 | pc, ocv_uv); |
| 1143 | new_pc = interpolate_pc(chip->pc_temp_ocv_lut, batt_temp_degc, ocv_uv); |
| 1144 | pr_debug("test revlookup pc = %d for ocv = %d\n", new_pc, ocv_uv); |
| 1145 | |
| 1146 | while (abs(new_pc - pc) > 1) { |
| 1147 | int delta_mv = 5; |
| 1148 | |
| 1149 | if (new_pc > pc) |
| 1150 | delta_mv = -1 * delta_mv; |
| 1151 | |
| 1152 | ocv_uv = ocv_uv + delta_mv; |
| 1153 | new_pc = interpolate_pc(chip->pc_temp_ocv_lut, |
| 1154 | batt_temp_degc, ocv_uv); |
| 1155 | pr_debug("test revlookup pc = %d for ocv = %d\n", |
| 1156 | new_pc, ocv_uv); |
| 1157 | } |
| 1158 | |
| 1159 | *ret_ocv_uv = ocv_uv * 1000; |
| 1160 | params->ocv_charge_uah = (int)ocv_charge_uah; |
| 1161 | } |
| 1162 | |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1163 | static int get_current_time(unsigned long *now_tm_sec) |
| 1164 | { |
| 1165 | struct rtc_time tm; |
| 1166 | struct rtc_device *rtc; |
| 1167 | int rc; |
| 1168 | |
| 1169 | rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE); |
| 1170 | if (rtc == NULL) { |
| 1171 | pr_err("%s: unable to open rtc device (%s)\n", |
| 1172 | __FILE__, CONFIG_RTC_HCTOSYS_DEVICE); |
Xiaozhe Shi | 0e01af6 | 2013-05-06 12:56:08 -0700 | [diff] [blame] | 1173 | return -EINVAL; |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | rc = rtc_read_time(rtc, &tm); |
| 1177 | if (rc) { |
| 1178 | pr_err("Error reading rtc device (%s) : %d\n", |
| 1179 | CONFIG_RTC_HCTOSYS_DEVICE, rc); |
| 1180 | goto close_time; |
| 1181 | } |
| 1182 | |
| 1183 | rc = rtc_valid_tm(&tm); |
| 1184 | if (rc) { |
| 1185 | pr_err("Invalid RTC time (%s): %d\n", |
| 1186 | CONFIG_RTC_HCTOSYS_DEVICE, rc); |
| 1187 | goto close_time; |
| 1188 | } |
| 1189 | rtc_tm_to_time(&tm, now_tm_sec); |
| 1190 | |
| 1191 | close_time: |
| 1192 | rtc_class_close(rtc); |
| 1193 | return rc; |
| 1194 | } |
| 1195 | |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1196 | static int calculate_delta_time(unsigned long *time_stamp, int *delta_time_s) |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1197 | { |
| 1198 | unsigned long now_tm_sec = 0; |
| 1199 | |
| 1200 | /* default to delta time = 0 if anything fails */ |
| 1201 | *delta_time_s = 0; |
| 1202 | |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1203 | if (get_current_time(&now_tm_sec)) { |
| 1204 | pr_err("RTC read failed\n"); |
| 1205 | return 0; |
| 1206 | } |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1207 | |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1208 | *delta_time_s = (now_tm_sec - *time_stamp); |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1209 | |
| 1210 | /* remember this time */ |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1211 | *time_stamp = now_tm_sec; |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1212 | return 0; |
| 1213 | } |
| 1214 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1215 | static void calculate_soc_params(struct qpnp_bms_chip *chip, |
| 1216 | struct raw_soc_params *raw, |
| 1217 | struct soc_params *params, |
| 1218 | int batt_temp) |
| 1219 | { |
| 1220 | int soc_rbatt; |
| 1221 | |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1222 | calculate_delta_time(&chip->tm_sec, ¶ms->delta_time_s); |
| 1223 | pr_debug("tm_sec = %ld, delta_s = %d\n", |
| 1224 | chip->tm_sec, params->delta_time_s); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1225 | params->fcc_uah = calculate_fcc(chip, batt_temp); |
| 1226 | pr_debug("FCC = %uuAh batt_temp = %d\n", params->fcc_uah, batt_temp); |
| 1227 | |
| 1228 | /* calculate remainging charge */ |
| 1229 | params->ocv_charge_uah = calculate_ocv_charge( |
| 1230 | chip, raw, |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1231 | params->fcc_uah); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1232 | pr_debug("ocv_charge_uah = %uuAh\n", params->ocv_charge_uah); |
| 1233 | |
| 1234 | /* calculate cc micro_volt_hour */ |
| 1235 | params->cc_uah = calculate_cc(chip, raw->cc); |
Xiaozhe Shi | 1c63a89 | 2013-02-13 15:49:40 -0800 | [diff] [blame] | 1236 | pr_debug("cc_uah = %duAh raw->cc = %llx\n", params->cc_uah, raw->cc); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1237 | |
| 1238 | soc_rbatt = ((params->ocv_charge_uah - params->cc_uah) * 100) |
| 1239 | / params->fcc_uah; |
| 1240 | if (soc_rbatt < 0) |
| 1241 | soc_rbatt = 0; |
Xiaozhe Shi | 904f1f7 | 2012-12-04 12:47:21 -0800 | [diff] [blame] | 1242 | params->rbatt_mohm = get_rbatt(chip, soc_rbatt, batt_temp); |
Xiaozhe Shi | 794607c | 2013-02-19 10:25:59 -0800 | [diff] [blame] | 1243 | pr_debug("rbatt_mohm = %d\n", params->rbatt_mohm); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1244 | |
Xiaozhe Shi | 6dc56f1 | 2013-05-02 15:56:55 -0700 | [diff] [blame] | 1245 | if (params->rbatt_mohm != chip->rbatt_mohm |
| 1246 | && chip->bms_psy.name != NULL) { |
| 1247 | chip->rbatt_mohm = params->rbatt_mohm; |
| 1248 | power_supply_changed(&chip->bms_psy); |
| 1249 | } |
| 1250 | |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 1251 | calculate_iavg(chip, params->cc_uah, ¶ms->iavg_ua, |
| 1252 | params->delta_time_s); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1253 | |
| 1254 | params->uuc_uah = calculate_unusable_charge_uah(chip, params, |
| 1255 | batt_temp); |
| 1256 | pr_debug("UUC = %uuAh\n", params->uuc_uah); |
| 1257 | } |
| 1258 | |
| 1259 | static bool is_shutdown_soc_within_limits(struct qpnp_bms_chip *chip, int soc) |
| 1260 | { |
| 1261 | if (chip->shutdown_soc_invalid) { |
| 1262 | pr_debug("NOT forcing shutdown soc = %d\n", chip->shutdown_soc); |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
| 1266 | if (abs(chip->shutdown_soc - soc) > chip->shutdown_soc_valid_limit) { |
| 1267 | pr_debug("rejecting shutdown soc = %d, soc = %d limit = %d\n", |
| 1268 | chip->shutdown_soc, soc, |
| 1269 | chip->shutdown_soc_valid_limit); |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 1270 | chip->shutdown_soc_invalid = true; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1271 | return 0; |
| 1272 | } |
| 1273 | |
| 1274 | return 1; |
| 1275 | } |
| 1276 | |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1277 | static int bound_soc(int soc) |
| 1278 | { |
| 1279 | soc = max(0, soc); |
| 1280 | soc = min(100, soc); |
| 1281 | return soc; |
| 1282 | } |
| 1283 | |
Xiaozhe Shi | 20640b5 | 2013-01-03 11:49:30 -0800 | [diff] [blame] | 1284 | #define IBAT_TOL_MASK 0x0F |
| 1285 | #define OCV_TOL_MASK 0xF0 |
| 1286 | #define IBAT_TOL_DEFAULT 0x03 |
| 1287 | #define IBAT_TOL_NOCHG 0x0F |
| 1288 | #define OCV_TOL_DEFAULT 0x20 |
| 1289 | #define OCV_TOL_NO_OCV 0x00 |
| 1290 | static int stop_ocv_updates(struct qpnp_bms_chip *chip) |
| 1291 | { |
| 1292 | pr_debug("stopping ocv updates\n"); |
| 1293 | return qpnp_masked_write(chip, BMS1_TOL_CTL, |
| 1294 | OCV_TOL_MASK, OCV_TOL_NO_OCV); |
| 1295 | } |
| 1296 | |
| 1297 | static int reset_bms_for_test(struct qpnp_bms_chip *chip) |
| 1298 | { |
Xiaozhe Shi | 95da77f | 2013-02-20 13:40:06 -0800 | [diff] [blame] | 1299 | int ibat_ua = 0, vbat_uv = 0, rc; |
Xiaozhe Shi | 20640b5 | 2013-01-03 11:49:30 -0800 | [diff] [blame] | 1300 | int ocv_est_uv; |
| 1301 | |
| 1302 | if (!chip) { |
| 1303 | pr_err("BMS driver has not been initialized yet!\n"); |
| 1304 | return -EINVAL; |
| 1305 | } |
| 1306 | |
| 1307 | rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv); |
| 1308 | |
Xiaozhe Shi | 1a10aff | 2013-04-01 15:40:05 -0700 | [diff] [blame] | 1309 | /* |
| 1310 | * Don't include rbatt and rbatt_capacitative since we expect this to |
| 1311 | * be used with a fake battery which does not have internal resistances |
| 1312 | */ |
Xiaozhe Shi | 20640b5 | 2013-01-03 11:49:30 -0800 | [diff] [blame] | 1313 | ocv_est_uv = vbat_uv + (ibat_ua * chip->r_conn_mohm) / 1000; |
| 1314 | pr_debug("forcing ocv to be %d due to bms reset mode\n", ocv_est_uv); |
| 1315 | chip->last_ocv_uv = ocv_est_uv; |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1316 | mutex_lock(&chip->last_soc_mutex); |
Xiaozhe Shi | 20640b5 | 2013-01-03 11:49:30 -0800 | [diff] [blame] | 1317 | chip->last_soc = -EINVAL; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 1318 | chip->last_soc_invalid = true; |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1319 | mutex_unlock(&chip->last_soc_mutex); |
Xiaozhe Shi | 20640b5 | 2013-01-03 11:49:30 -0800 | [diff] [blame] | 1320 | reset_cc(chip); |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 1321 | chip->software_cc_uah = 0; |
Xiaozhe Shi | 20640b5 | 2013-01-03 11:49:30 -0800 | [diff] [blame] | 1322 | chip->last_cc_uah = INT_MIN; |
| 1323 | stop_ocv_updates(chip); |
| 1324 | |
| 1325 | pr_debug("bms reset to ocv = %duv vbat_ua = %d ibat_ua = %d\n", |
| 1326 | chip->last_ocv_uv, vbat_uv, ibat_ua); |
| 1327 | |
| 1328 | return rc; |
| 1329 | } |
| 1330 | |
| 1331 | static int bms_reset_set(const char *val, const struct kernel_param *kp) |
| 1332 | { |
| 1333 | int rc; |
| 1334 | |
| 1335 | rc = param_set_bool(val, kp); |
| 1336 | if (rc) { |
| 1337 | pr_err("Unable to set bms_reset: %d\n", rc); |
| 1338 | return rc; |
| 1339 | } |
| 1340 | |
| 1341 | if (*(bool *)kp->arg) { |
| 1342 | struct power_supply *bms_psy = power_supply_get_by_name("bms"); |
| 1343 | struct qpnp_bms_chip *chip = container_of(bms_psy, |
| 1344 | struct qpnp_bms_chip, bms_psy); |
| 1345 | |
| 1346 | rc = reset_bms_for_test(chip); |
| 1347 | if (rc) { |
| 1348 | pr_err("Unable to modify bms_reset: %d\n", rc); |
| 1349 | return rc; |
| 1350 | } |
| 1351 | } |
| 1352 | return 0; |
| 1353 | } |
| 1354 | |
| 1355 | static struct kernel_param_ops bms_reset_ops = { |
| 1356 | .set = bms_reset_set, |
| 1357 | .get = param_get_bool, |
| 1358 | }; |
| 1359 | |
| 1360 | module_param_cb(bms_reset, &bms_reset_ops, &bms_reset, 0644); |
| 1361 | |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 1362 | #define VBATT_ERROR_MARGIN 20000 |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1363 | static int charging_adjustments(struct qpnp_bms_chip *chip, |
| 1364 | struct soc_params *params, int soc, |
| 1365 | int vbat_uv, int ibat_ua, int batt_temp) |
| 1366 | { |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 1367 | int chg_soc, batt_terminal_uv; |
| 1368 | |
| 1369 | batt_terminal_uv = vbat_uv + VBATT_ERROR_MARGIN |
| 1370 | + (ibat_ua * chip->r_conn_mohm) / 1000; |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 1371 | |
| 1372 | if (chip->soc_at_cv == -EINVAL) { |
Abhijeet Dharmapurikar | eef8866 | 2012-11-08 17:26:29 -0800 | [diff] [blame] | 1373 | if (batt_terminal_uv >= chip->max_voltage_uv) { |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 1374 | chip->soc_at_cv = soc; |
| 1375 | chip->prev_chg_soc = soc; |
| 1376 | chip->ibat_at_cv_ua = ibat_ua; |
| 1377 | pr_debug("CC_TO_CV ibat_ua = %d CHG SOC %d\n", |
| 1378 | ibat_ua, soc); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 1379 | } else { |
| 1380 | /* In constant current charging return the calc soc */ |
| 1381 | pr_debug("CC CHG SOC %d\n", soc); |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 1382 | } |
Xiaozhe Shi | fc2f5a0 | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 1383 | |
| 1384 | chip->prev_batt_terminal_uv = batt_terminal_uv; |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 1385 | return soc; |
| 1386 | } |
| 1387 | |
| 1388 | /* |
Xiaozhe Shi | fc2f5a0 | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 1389 | * battery is in CV phase - begin linear interpolation of soc based on |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 1390 | * battery charge current |
| 1391 | */ |
| 1392 | |
| 1393 | /* |
| 1394 | * if voltage lessened (possibly because of a system load) |
| 1395 | * keep reporting the prev chg soc |
| 1396 | */ |
Xiaozhe Shi | fc2f5a0 | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 1397 | if (batt_terminal_uv <= chip->prev_batt_terminal_uv) { |
Abhijeet Dharmapurikar | eef8866 | 2012-11-08 17:26:29 -0800 | [diff] [blame] | 1398 | pr_debug("batt_terminal_uv %d < (max = %d - 10000); CC CHG SOC %d\n", |
Xiaozhe Shi | fc2f5a0 | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 1399 | batt_terminal_uv, chip->prev_batt_terminal_uv, |
| 1400 | chip->prev_chg_soc); |
| 1401 | chip->prev_batt_terminal_uv = batt_terminal_uv; |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 1402 | return chip->prev_chg_soc; |
| 1403 | } |
| 1404 | |
| 1405 | chg_soc = linear_interpolate(chip->soc_at_cv, chip->ibat_at_cv_ua, |
Abhijeet Dharmapurikar | eef8866 | 2012-11-08 17:26:29 -0800 | [diff] [blame] | 1406 | 100, -1 * chip->chg_term_ua, |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 1407 | ibat_ua); |
Xiaozhe Shi | 78d0c53 | 2012-12-10 13:02:14 -0800 | [diff] [blame] | 1408 | chg_soc = bound_soc(chg_soc); |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 1409 | |
| 1410 | /* always report a higher soc */ |
| 1411 | if (chg_soc > chip->prev_chg_soc) { |
| 1412 | int new_ocv_uv; |
| 1413 | |
| 1414 | chip->prev_chg_soc = chg_soc; |
| 1415 | |
| 1416 | find_ocv_for_soc(chip, params, batt_temp, chg_soc, &new_ocv_uv); |
| 1417 | chip->last_ocv_uv = new_ocv_uv; |
| 1418 | pr_debug("CC CHG ADJ OCV = %d CHG SOC %d\n", |
| 1419 | new_ocv_uv, |
| 1420 | chip->prev_chg_soc); |
| 1421 | } |
| 1422 | |
| 1423 | pr_debug("Reporting CHG SOC %d\n", chip->prev_chg_soc); |
Xiaozhe Shi | fc2f5a0 | 2013-01-28 15:09:04 -0800 | [diff] [blame] | 1424 | chip->prev_batt_terminal_uv = batt_terminal_uv; |
Xiaozhe Shi | 41bc1f1 | 2012-09-26 16:55:22 -0700 | [diff] [blame] | 1425 | return chip->prev_chg_soc; |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 1428 | static void very_low_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv) |
| 1429 | { |
| 1430 | /* |
| 1431 | * if battery is very low (v_cutoff voltage + 20mv) hold |
| 1432 | * a wakelock untill soc = 0% |
| 1433 | */ |
| 1434 | if (vbat_uv <= chip->low_voltage_threshold |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 1435 | && !wake_lock_active(&chip->low_voltage_wake_lock)) { |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 1436 | pr_debug("voltage = %d low holding wakelock\n", vbat_uv); |
| 1437 | wake_lock(&chip->low_voltage_wake_lock); |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 1438 | } else if (vbat_uv > chip->low_voltage_threshold |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 1439 | && wake_lock_active(&chip->low_voltage_wake_lock)) { |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 1440 | pr_debug("voltage = %d releasing wakelock\n", vbat_uv); |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 1441 | wake_unlock(&chip->low_voltage_wake_lock); |
| 1442 | } |
| 1443 | } |
| 1444 | |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 1445 | #define VBATT_ERROR_MARGIN 20000 |
| 1446 | static void cv_voltage_check(struct qpnp_bms_chip *chip, int vbat_uv) |
| 1447 | { |
| 1448 | /* |
| 1449 | * if battery is very low (v_cutoff voltage + 20mv) hold |
| 1450 | * a wakelock untill soc = 0% |
| 1451 | */ |
| 1452 | if (wake_lock_active(&chip->cv_wake_lock)) { |
| 1453 | if (chip->soc_at_cv != -EINVAL) { |
| 1454 | pr_debug("hit CV, releasing cv wakelock\n"); |
| 1455 | wake_unlock(&chip->cv_wake_lock); |
| 1456 | } else if (!is_battery_charging(chip)) { |
| 1457 | pr_debug("charging stopped, releasing cv wakelock\n"); |
| 1458 | wake_unlock(&chip->cv_wake_lock); |
| 1459 | } |
| 1460 | } else if (vbat_uv > chip->max_voltage_uv - VBATT_ERROR_MARGIN |
| 1461 | && chip->soc_at_cv == -EINVAL |
| 1462 | && is_battery_charging(chip) |
| 1463 | && !wake_lock_active(&chip->cv_wake_lock)) { |
| 1464 | pr_debug("voltage = %d holding cv wakelock\n", vbat_uv); |
| 1465 | wake_lock(&chip->cv_wake_lock); |
| 1466 | } |
| 1467 | } |
| 1468 | |
Xiaozhe Shi | 561ebf7 | 2013-03-25 13:51:27 -0700 | [diff] [blame] | 1469 | #define NO_ADJUST_HIGH_SOC_THRESHOLD 90 |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1470 | static int adjust_soc(struct qpnp_bms_chip *chip, struct soc_params *params, |
| 1471 | int soc, int batt_temp) |
| 1472 | { |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1473 | int ibat_ua = 0, vbat_uv = 0; |
| 1474 | int ocv_est_uv = 0, soc_est = 0, pc_est = 0, pc = 0; |
| 1475 | int delta_ocv_uv = 0; |
| 1476 | int n = 0; |
| 1477 | int rc_new_uah = 0; |
| 1478 | int pc_new = 0; |
| 1479 | int soc_new = 0; |
| 1480 | int slope = 0; |
| 1481 | int rc = 0; |
| 1482 | int delta_ocv_uv_limit = 0; |
Xiaozhe Shi | 0ac7a00 | 2013-03-26 13:14:03 -0700 | [diff] [blame] | 1483 | int correction_limit_uv = 0; |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1484 | |
| 1485 | rc = get_simultaneous_batt_v_and_i(chip, &ibat_ua, &vbat_uv); |
| 1486 | if (rc < 0) { |
| 1487 | pr_err("simultaneous vbat ibat failed err = %d\n", rc); |
| 1488 | goto out; |
| 1489 | } |
| 1490 | |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 1491 | very_low_voltage_check(chip, vbat_uv); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 1492 | cv_voltage_check(chip, vbat_uv); |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 1493 | |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1494 | delta_ocv_uv_limit = DIV_ROUND_CLOSEST(ibat_ua, 1000); |
| 1495 | |
Xiaozhe Shi | 904f1f7 | 2012-12-04 12:47:21 -0800 | [diff] [blame] | 1496 | ocv_est_uv = vbat_uv + (ibat_ua * params->rbatt_mohm)/1000; |
| 1497 | |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1498 | pc_est = calculate_pc(chip, ocv_est_uv, batt_temp); |
| 1499 | soc_est = div_s64((s64)params->fcc_uah * pc_est - params->uuc_uah*100, |
| 1500 | (s64)params->fcc_uah - params->uuc_uah); |
| 1501 | soc_est = bound_soc(soc_est); |
| 1502 | |
Xiaozhe Shi | 20640b5 | 2013-01-03 11:49:30 -0800 | [diff] [blame] | 1503 | /* never adjust during bms reset mode */ |
| 1504 | if (bms_reset) { |
| 1505 | pr_debug("bms reset mode, SOC adjustment skipped\n"); |
| 1506 | goto out; |
| 1507 | } |
| 1508 | |
Xiaozhe Shi | 8658c98 | 2013-04-30 11:33:07 -0700 | [diff] [blame] | 1509 | if (ibat_ua < 0 && !is_battery_full(chip)) { |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1510 | soc = charging_adjustments(chip, params, soc, vbat_uv, ibat_ua, |
| 1511 | batt_temp); |
| 1512 | goto out; |
| 1513 | } |
| 1514 | |
| 1515 | /* |
| 1516 | * do not adjust |
Xiaozhe Shi | 561ebf7 | 2013-03-25 13:51:27 -0700 | [diff] [blame] | 1517 | * if soc_est is same as what bms calculated |
| 1518 | * OR if soc_est > adjust_soc_low_threshold |
| 1519 | * OR if soc is above 90 |
| 1520 | * because we might pull it low |
| 1521 | * and cause a bad user experience |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1522 | */ |
| 1523 | if (soc_est == soc |
Xiaozhe Shi | 561ebf7 | 2013-03-25 13:51:27 -0700 | [diff] [blame] | 1524 | || soc_est > chip->adjust_soc_low_threshold |
| 1525 | || soc >= NO_ADJUST_HIGH_SOC_THRESHOLD) |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1526 | goto out; |
| 1527 | |
| 1528 | if (chip->last_soc_est == -EINVAL) |
| 1529 | chip->last_soc_est = soc; |
| 1530 | |
| 1531 | n = min(200, max(1 , soc + soc_est + chip->last_soc_est)); |
| 1532 | chip->last_soc_est = soc_est; |
| 1533 | |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1534 | pc = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp); |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1535 | if (pc > 0) { |
| 1536 | pc_new = calculate_pc(chip, |
| 1537 | chip->last_ocv_uv - (++slope * 1000), |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1538 | chip->last_ocv_temp); |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1539 | while (pc_new == pc) { |
| 1540 | /* start taking 10mV steps */ |
| 1541 | slope = slope + 10; |
| 1542 | pc_new = calculate_pc(chip, |
| 1543 | chip->last_ocv_uv - (slope * 1000), |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1544 | chip->last_ocv_temp); |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1545 | } |
| 1546 | } else { |
| 1547 | /* |
| 1548 | * pc is already at the lowest point, |
| 1549 | * assume 1 millivolt translates to 1% pc |
| 1550 | */ |
| 1551 | pc = 1; |
| 1552 | pc_new = 0; |
| 1553 | slope = 1; |
| 1554 | } |
| 1555 | |
| 1556 | delta_ocv_uv = div_s64((soc - soc_est) * (s64)slope * 1000, |
| 1557 | n * (pc - pc_new)); |
| 1558 | |
| 1559 | if (abs(delta_ocv_uv) > delta_ocv_uv_limit) { |
| 1560 | pr_debug("limiting delta ocv %d limit = %d\n", delta_ocv_uv, |
| 1561 | delta_ocv_uv_limit); |
| 1562 | |
| 1563 | if (delta_ocv_uv > 0) |
| 1564 | delta_ocv_uv = delta_ocv_uv_limit; |
| 1565 | else |
| 1566 | delta_ocv_uv = -1 * delta_ocv_uv_limit; |
| 1567 | pr_debug("new delta ocv = %d\n", delta_ocv_uv); |
| 1568 | } |
| 1569 | |
Xiaozhe Shi | 0ac7a00 | 2013-03-26 13:14:03 -0700 | [diff] [blame] | 1570 | if (chip->last_ocv_uv > chip->flat_ocv_threshold_uv) |
| 1571 | correction_limit_uv = chip->high_ocv_correction_limit_uv; |
| 1572 | else |
| 1573 | correction_limit_uv = chip->low_ocv_correction_limit_uv; |
| 1574 | |
| 1575 | if (abs(delta_ocv_uv) > correction_limit_uv) { |
| 1576 | pr_debug("limiting delta ocv %d limit = %d\n", |
| 1577 | delta_ocv_uv, correction_limit_uv); |
| 1578 | if (delta_ocv_uv > 0) |
| 1579 | delta_ocv_uv = correction_limit_uv; |
| 1580 | else |
| 1581 | delta_ocv_uv = -correction_limit_uv; |
| 1582 | pr_debug("new delta ocv = %d\n", delta_ocv_uv); |
| 1583 | } |
| 1584 | |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1585 | chip->last_ocv_uv -= delta_ocv_uv; |
| 1586 | |
| 1587 | if (chip->last_ocv_uv >= chip->max_voltage_uv) |
| 1588 | chip->last_ocv_uv = chip->max_voltage_uv; |
| 1589 | |
| 1590 | /* calculate the soc based on this new ocv */ |
Abhijeet Dharmapurikar | 4b97cdd | 2012-12-26 21:10:53 -0800 | [diff] [blame] | 1591 | pc_new = calculate_pc(chip, chip->last_ocv_uv, chip->last_ocv_temp); |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1592 | rc_new_uah = (params->fcc_uah * pc_new) / 100; |
| 1593 | soc_new = (rc_new_uah - params->cc_uah - params->uuc_uah)*100 |
| 1594 | / (params->fcc_uah - params->uuc_uah); |
| 1595 | soc_new = bound_soc(soc_new); |
| 1596 | |
| 1597 | /* |
| 1598 | * if soc_new is ZERO force it higher so that phone doesnt report soc=0 |
Xiaozhe Shi | 0ac7a00 | 2013-03-26 13:14:03 -0700 | [diff] [blame] | 1599 | * soc = 0 should happen only when soc_est is above a set value |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1600 | */ |
Xiaozhe Shi | 0ac7a00 | 2013-03-26 13:14:03 -0700 | [diff] [blame] | 1601 | if (soc_new == 0 && soc_est >= chip->hold_soc_est) |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1602 | soc_new = 1; |
| 1603 | |
| 1604 | soc = soc_new; |
| 1605 | |
| 1606 | out: |
| 1607 | pr_debug("ibat_ua = %d, vbat_uv = %d, ocv_est_uv = %d, pc_est = %d, soc_est = %d, n = %d, delta_ocv_uv = %d, last_ocv_uv = %d, pc_new = %d, soc_new = %d, rbatt = %d, slope = %d\n", |
| 1608 | ibat_ua, vbat_uv, ocv_est_uv, pc_est, |
| 1609 | soc_est, n, delta_ocv_uv, chip->last_ocv_uv, |
Xiaozhe Shi | 904f1f7 | 2012-12-04 12:47:21 -0800 | [diff] [blame] | 1610 | pc_new, soc_new, params->rbatt_mohm, slope); |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 1611 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1612 | return soc; |
| 1613 | } |
| 1614 | |
Xiaozhe Shi | 2542c60 | 2012-11-28 10:08:07 -0800 | [diff] [blame] | 1615 | static int clamp_soc_based_on_voltage(struct qpnp_bms_chip *chip, int soc) |
| 1616 | { |
| 1617 | int rc, vbat_uv; |
Xiaozhe Shi | 2542c60 | 2012-11-28 10:08:07 -0800 | [diff] [blame] | 1618 | |
Xiaozhe Shi | 3645896 | 2013-02-06 16:19:57 -0800 | [diff] [blame] | 1619 | rc = get_battery_voltage(&vbat_uv); |
| 1620 | if (rc < 0) { |
| 1621 | pr_err("adc vbat failed err = %d\n", rc); |
| 1622 | return soc; |
Xiaozhe Shi | 2542c60 | 2012-11-28 10:08:07 -0800 | [diff] [blame] | 1623 | } |
Xiaozhe Shi | 2542c60 | 2012-11-28 10:08:07 -0800 | [diff] [blame] | 1624 | if (soc == 0 && vbat_uv > chip->v_cutoff_uv) { |
| 1625 | pr_debug("clamping soc to 1, vbat (%d) > cutoff (%d)\n", |
| 1626 | vbat_uv, chip->v_cutoff_uv); |
| 1627 | return 1; |
Xiaozhe Shi | 2542c60 | 2012-11-28 10:08:07 -0800 | [diff] [blame] | 1628 | } else { |
| 1629 | pr_debug("not clamping, using soc = %d, vbat = %d and cutoff = %d\n", |
| 1630 | soc, vbat_uv, chip->v_cutoff_uv); |
| 1631 | return soc; |
| 1632 | } |
| 1633 | } |
| 1634 | |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1635 | #define SLEEP_RECALC_INTERVAL 3 |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 1636 | static int calculate_state_of_charge(struct qpnp_bms_chip *chip, |
| 1637 | struct raw_soc_params *raw, |
| 1638 | int batt_temp) |
| 1639 | { |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1640 | int soc, new_ocv_uv; |
| 1641 | int shutdown_soc, new_calculated_soc, remaining_usable_charge_uah; |
| 1642 | struct soc_params params; |
| 1643 | |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 1644 | if (!is_battery_present(chip)) { |
Xiaozhe Shi | 026fa9b | 2013-03-22 17:00:50 -0700 | [diff] [blame] | 1645 | pr_debug("battery gone, reporting 100\n"); |
| 1646 | new_calculated_soc = 100; |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 1647 | goto done_calculating; |
| 1648 | } |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1649 | calculate_soc_params(chip, raw, ¶ms, batt_temp); |
| 1650 | /* calculate remaining usable charge */ |
| 1651 | remaining_usable_charge_uah = params.ocv_charge_uah |
| 1652 | - params.cc_uah |
| 1653 | - params.uuc_uah; |
| 1654 | |
| 1655 | pr_debug("RUC = %duAh\n", remaining_usable_charge_uah); |
| 1656 | if (params.fcc_uah - params.uuc_uah <= 0) { |
Xiaozhe Shi | cb386a2 | 2012-11-29 12:11:42 -0800 | [diff] [blame] | 1657 | pr_debug("FCC = %duAh, UUC = %duAh forcing soc = 0\n", |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1658 | params.fcc_uah, |
| 1659 | params.uuc_uah); |
Xiaozhe Shi | fd8cd48 | 2013-02-12 10:00:38 -0800 | [diff] [blame] | 1660 | new_calculated_soc = 0; |
| 1661 | goto done_calculating; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
Xiaozhe Shi | fd8cd48 | 2013-02-12 10:00:38 -0800 | [diff] [blame] | 1664 | soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100), |
| 1665 | (params.fcc_uah - params.uuc_uah)); |
| 1666 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1667 | if (chip->first_time_calc_soc && soc < 0) { |
| 1668 | /* |
| 1669 | * first time calcualtion and the pon ocv is too low resulting |
| 1670 | * in a bad soc. Adjust ocv to get 0 soc |
| 1671 | */ |
| 1672 | pr_debug("soc is %d, adjusting pon ocv to make it 0\n", soc); |
| 1673 | find_ocv_for_soc(chip, ¶ms, batt_temp, 0, &new_ocv_uv); |
| 1674 | chip->last_ocv_uv = new_ocv_uv; |
| 1675 | |
| 1676 | remaining_usable_charge_uah = params.ocv_charge_uah |
| 1677 | - params.cc_uah |
| 1678 | - params.uuc_uah; |
| 1679 | |
| 1680 | soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100), |
| 1681 | (params.fcc_uah |
| 1682 | - params.uuc_uah)); |
| 1683 | pr_debug("DONE for O soc is %d, pon ocv adjusted to %duV\n", |
| 1684 | soc, chip->last_ocv_uv); |
| 1685 | } |
| 1686 | |
| 1687 | if (soc > 100) |
| 1688 | soc = 100; |
| 1689 | |
| 1690 | if (soc < 0) { |
Xiaozhe Shi | cb386a2 | 2012-11-29 12:11:42 -0800 | [diff] [blame] | 1691 | pr_debug("bad rem_usb_chg = %d rem_chg %d, cc_uah %d, unusb_chg %d\n", |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1692 | remaining_usable_charge_uah, |
| 1693 | params.ocv_charge_uah, |
| 1694 | params.cc_uah, params.uuc_uah); |
| 1695 | |
Xiaozhe Shi | cb386a2 | 2012-11-29 12:11:42 -0800 | [diff] [blame] | 1696 | pr_debug("for bad rem_usb_chg last_ocv_uv = %d batt_temp = %d fcc = %d soc =%d\n", |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1697 | chip->last_ocv_uv, batt_temp, |
| 1698 | params.fcc_uah, soc); |
| 1699 | soc = 0; |
| 1700 | } |
| 1701 | |
| 1702 | mutex_lock(&chip->soc_invalidation_mutex); |
| 1703 | shutdown_soc = chip->shutdown_soc; |
| 1704 | |
| 1705 | if (chip->first_time_calc_soc && soc != shutdown_soc |
| 1706 | && is_shutdown_soc_within_limits(chip, soc)) { |
| 1707 | /* |
| 1708 | * soc for the first time - use shutdown soc |
| 1709 | * to adjust pon ocv since it is a small percent away from |
| 1710 | * the real soc |
| 1711 | */ |
| 1712 | pr_debug("soc = %d before forcing shutdown_soc = %d\n", |
| 1713 | soc, shutdown_soc); |
| 1714 | find_ocv_for_soc(chip, ¶ms, batt_temp, |
| 1715 | shutdown_soc, &new_ocv_uv); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1716 | chip->last_ocv_uv = new_ocv_uv; |
| 1717 | |
| 1718 | remaining_usable_charge_uah = params.ocv_charge_uah |
| 1719 | - params.cc_uah |
| 1720 | - params.uuc_uah; |
| 1721 | |
| 1722 | soc = DIV_ROUND_CLOSEST((remaining_usable_charge_uah * 100), |
| 1723 | (params.fcc_uah |
| 1724 | - params.uuc_uah)); |
| 1725 | |
| 1726 | pr_debug("DONE for shutdown_soc = %d soc is %d, adjusted ocv to %duV\n", |
| 1727 | shutdown_soc, soc, chip->last_ocv_uv); |
| 1728 | } |
| 1729 | mutex_unlock(&chip->soc_invalidation_mutex); |
| 1730 | |
| 1731 | pr_debug("SOC before adjustment = %d\n", soc); |
| 1732 | new_calculated_soc = adjust_soc(chip, ¶ms, soc, batt_temp); |
| 1733 | |
Xiaozhe Shi | 445d249 | 2013-03-27 18:10:18 -0700 | [diff] [blame] | 1734 | /* always clamp soc due to BMS hw/sw immaturities */ |
| 1735 | new_calculated_soc = clamp_soc_based_on_voltage(chip, |
| 1736 | new_calculated_soc); |
Xiaozhe Shi | 2542c60 | 2012-11-28 10:08:07 -0800 | [diff] [blame] | 1737 | |
Xiaozhe Shi | fd8cd48 | 2013-02-12 10:00:38 -0800 | [diff] [blame] | 1738 | done_calculating: |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1739 | if (new_calculated_soc != chip->calculated_soc |
| 1740 | && chip->bms_psy.name != NULL) { |
| 1741 | power_supply_changed(&chip->bms_psy); |
| 1742 | pr_debug("power supply changed\n"); |
| 1743 | } |
| 1744 | |
| 1745 | chip->calculated_soc = new_calculated_soc; |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1746 | pr_debug("CC based calculated SOC = %d\n", chip->calculated_soc); |
| 1747 | mutex_lock(&chip->last_soc_mutex); |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 1748 | if (chip->last_soc_invalid) { |
| 1749 | chip->last_soc_invalid = false; |
| 1750 | chip->last_soc = -EINVAL; |
| 1751 | } |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1752 | /* |
| 1753 | * Check if more than a long time has passed since the last |
| 1754 | * calculation (more than n times compared to the soc recalculation |
| 1755 | * rate, where n is defined by SLEEP_RECALC_INTERVAL). If this is true, |
| 1756 | * then the system must have gone through a long sleep, and SoC can be |
| 1757 | * allowed to become unbounded by the last reported SoC |
| 1758 | */ |
| 1759 | if (params.delta_time_s * 1000 > |
| 1760 | chip->calculate_soc_ms * SLEEP_RECALC_INTERVAL |
| 1761 | && !chip->first_time_calc_soc) { |
| 1762 | chip->last_soc_unbound = true; |
| 1763 | chip->last_soc_change_sec = chip->last_recalc_time; |
| 1764 | pr_debug("last_soc unbound because elapsed time = %d\n", |
| 1765 | params.delta_time_s); |
| 1766 | } |
| 1767 | mutex_unlock(&chip->last_soc_mutex); |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 1768 | get_current_time(&chip->last_recalc_time); |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1769 | chip->first_time_calc_soc = 0; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 1770 | return chip->calculated_soc; |
| 1771 | } |
| 1772 | |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 1773 | static int calculate_soc_from_voltage(struct qpnp_bms_chip *chip) |
| 1774 | { |
| 1775 | int voltage_range_uv, voltage_remaining_uv, voltage_based_soc; |
Xiaozhe Shi | 3645896 | 2013-02-06 16:19:57 -0800 | [diff] [blame] | 1776 | int rc, vbat_uv; |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 1777 | |
Xiaozhe Shi | 3645896 | 2013-02-06 16:19:57 -0800 | [diff] [blame] | 1778 | rc = get_battery_voltage(&vbat_uv); |
| 1779 | if (rc < 0) { |
| 1780 | pr_err("adc vbat failed err = %d\n", rc); |
| 1781 | return rc; |
| 1782 | } |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 1783 | voltage_range_uv = chip->max_voltage_uv - chip->v_cutoff_uv; |
| 1784 | voltage_remaining_uv = vbat_uv - chip->v_cutoff_uv; |
| 1785 | voltage_based_soc = voltage_remaining_uv * 100 / voltage_range_uv; |
| 1786 | |
| 1787 | voltage_based_soc = clamp(voltage_based_soc, 0, 100); |
| 1788 | |
| 1789 | if (chip->prev_voltage_based_soc != voltage_based_soc |
| 1790 | && chip->bms_psy.name != NULL) { |
| 1791 | power_supply_changed(&chip->bms_psy); |
| 1792 | pr_debug("power supply changed\n"); |
| 1793 | } |
| 1794 | chip->prev_voltage_based_soc = voltage_based_soc; |
| 1795 | |
| 1796 | pr_debug("vbat used = %duv\n", vbat_uv); |
| 1797 | pr_debug("Calculated voltage based soc = %d\n", voltage_based_soc); |
| 1798 | return voltage_based_soc; |
Xiaozhe Shi | 781b0a2 | 2012-11-05 17:18:27 -0800 | [diff] [blame] | 1799 | } |
| 1800 | |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 1801 | static int recalculate_soc(struct qpnp_bms_chip *chip) |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 1802 | { |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 1803 | int batt_temp, rc, soc; |
| 1804 | struct qpnp_vadc_result result; |
| 1805 | struct raw_soc_params raw; |
| 1806 | |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1807 | if (!wake_lock_active(&chip->soc_wake_lock)) |
| 1808 | wake_lock(&chip->soc_wake_lock); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 1809 | mutex_lock(&chip->vbat_monitor_mutex); |
| 1810 | qpnp_adc_tm_channel_measure(&chip->vbat_monitor_params); |
| 1811 | mutex_unlock(&chip->vbat_monitor_mutex); |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 1812 | if (chip->use_voltage_soc) { |
| 1813 | soc = calculate_soc_from_voltage(chip); |
| 1814 | } else { |
Xiaozhe Shi | 535494d | 2013-04-05 12:27:51 -0700 | [diff] [blame] | 1815 | qpnp_iadc_calibrate_for_trim(); |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 1816 | rc = qpnp_vadc_read(LR_MUX1_BATT_THERM, &result); |
| 1817 | if (rc) { |
| 1818 | pr_err("error reading vadc LR_MUX1_BATT_THERM = %d, rc = %d\n", |
| 1819 | LR_MUX1_BATT_THERM, rc); |
Abhijeet Dharmapurikar | 713b60a | 2012-12-26 21:30:05 -0800 | [diff] [blame] | 1820 | soc = chip->calculated_soc; |
| 1821 | } else { |
| 1822 | pr_debug("batt_temp phy = %lld meas = 0x%llx\n", |
| 1823 | result.physical, |
| 1824 | result.measurement); |
| 1825 | batt_temp = (int)result.physical; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 1826 | |
Abhijeet Dharmapurikar | 713b60a | 2012-12-26 21:30:05 -0800 | [diff] [blame] | 1827 | mutex_lock(&chip->last_ocv_uv_mutex); |
| 1828 | read_soc_params_raw(chip, &raw, batt_temp); |
| 1829 | soc = calculate_state_of_charge(chip, &raw, batt_temp); |
| 1830 | mutex_unlock(&chip->last_ocv_uv_mutex); |
| 1831 | } |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 1832 | } |
Abhijeet Dharmapurikar | 713b60a | 2012-12-26 21:30:05 -0800 | [diff] [blame] | 1833 | wake_unlock(&chip->soc_wake_lock); |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 1834 | return soc; |
| 1835 | } |
| 1836 | |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 1837 | static void recalculate_work(struct work_struct *work) |
| 1838 | { |
| 1839 | struct qpnp_bms_chip *chip = container_of(work, |
| 1840 | struct qpnp_bms_chip, |
| 1841 | recalc_work); |
| 1842 | |
| 1843 | recalculate_soc(chip); |
| 1844 | } |
| 1845 | |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 1846 | static void calculate_soc_work(struct work_struct *work) |
| 1847 | { |
| 1848 | struct qpnp_bms_chip *chip = container_of(work, |
| 1849 | struct qpnp_bms_chip, |
| 1850 | calculate_soc_delayed_work.work); |
| 1851 | int soc = recalculate_soc(chip); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 1852 | |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 1853 | if (soc < chip->low_soc_calc_threshold |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 1854 | || wake_lock_active(&chip->low_voltage_wake_lock)) |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 1855 | schedule_delayed_work(&chip->calculate_soc_delayed_work, |
| 1856 | round_jiffies_relative(msecs_to_jiffies |
| 1857 | (chip->low_soc_calculate_soc_ms))); |
| 1858 | else |
| 1859 | schedule_delayed_work(&chip->calculate_soc_delayed_work, |
| 1860 | round_jiffies_relative(msecs_to_jiffies |
| 1861 | (chip->calculate_soc_ms))); |
| 1862 | } |
| 1863 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1864 | static void backup_soc_and_iavg(struct qpnp_bms_chip *chip, int batt_temp, |
| 1865 | int soc) |
| 1866 | { |
| 1867 | u8 temp; |
| 1868 | int rc; |
| 1869 | int iavg_ma = chip->prev_uuc_iavg_ma; |
| 1870 | |
| 1871 | if (iavg_ma > IAVG_START) |
| 1872 | temp = (iavg_ma - IAVG_START) / IAVG_STEP_SIZE_MA; |
| 1873 | else |
| 1874 | temp = 0; |
| 1875 | |
| 1876 | rc = qpnp_write_wrapper(chip, &temp, |
| 1877 | chip->base + IAVG_STORAGE_REG, 1); |
| 1878 | |
Xiaozhe Shi | c7cbd05 | 2013-03-29 12:03:11 -0700 | [diff] [blame] | 1879 | temp = soc; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1880 | |
| 1881 | /* don't store soc if temperature is below 5degC */ |
| 1882 | if (batt_temp > IGNORE_SOC_TEMP_DECIDEG) |
| 1883 | rc = qpnp_write_wrapper(chip, &temp, |
| 1884 | chip->base + SOC_STORAGE_REG, 1); |
| 1885 | } |
| 1886 | |
| 1887 | #define SOC_CATCHUP_SEC_MAX 600 |
| 1888 | #define SOC_CATCHUP_SEC_PER_PERCENT 60 |
| 1889 | #define MAX_CATCHUP_SOC (SOC_CATCHUP_SEC_MAX/SOC_CATCHUP_SEC_PER_PERCENT) |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1890 | static int scale_soc_while_chg(struct qpnp_bms_chip *chip, int chg_time_sec, |
| 1891 | int catch_up_sec, int new_soc, int prev_soc) |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1892 | { |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1893 | int scaled_soc; |
| 1894 | int numerator; |
| 1895 | |
| 1896 | /* |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1897 | * Don't report a high value immediately slowly scale the |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1898 | * value from prev_soc to the new soc based on a charge time |
| 1899 | * weighted average |
| 1900 | */ |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1901 | pr_debug("cts = %d catch_up_sec = %d\n", chg_time_sec, catch_up_sec); |
Xiaozhe Shi | 4532d2d | 2012-12-17 19:34:50 -0800 | [diff] [blame] | 1902 | if (catch_up_sec == 0) |
| 1903 | return new_soc; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1904 | /* |
| 1905 | * if charging for more than catch_up time, simply return |
| 1906 | * new soc |
| 1907 | */ |
| 1908 | if (chg_time_sec > catch_up_sec) |
| 1909 | return new_soc; |
| 1910 | |
| 1911 | numerator = (catch_up_sec - chg_time_sec) * prev_soc |
| 1912 | + chg_time_sec * new_soc; |
| 1913 | scaled_soc = numerator / catch_up_sec; |
| 1914 | |
| 1915 | pr_debug("cts = %d new_soc = %d prev_soc = %d scaled_soc = %d\n", |
| 1916 | chg_time_sec, new_soc, prev_soc, scaled_soc); |
| 1917 | |
| 1918 | return scaled_soc; |
| 1919 | } |
| 1920 | |
| 1921 | /* |
| 1922 | * bms_fake_battery is set in setups where a battery emulator is used instead |
| 1923 | * of a real battery. This makes the bms driver report a different/fake value |
| 1924 | * regardless of the calculated state of charge. |
| 1925 | */ |
| 1926 | static int bms_fake_battery = -EINVAL; |
| 1927 | module_param(bms_fake_battery, int, 0644); |
| 1928 | |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 1929 | static int report_voltage_based_soc(struct qpnp_bms_chip *chip) |
| 1930 | { |
| 1931 | pr_debug("Reported voltage based soc = %d\n", |
| 1932 | chip->prev_voltage_based_soc); |
| 1933 | return chip->prev_voltage_based_soc; |
| 1934 | } |
| 1935 | |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1936 | #define SOC_CHANGE_PER_SEC 20 |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 1937 | static int report_cc_based_soc(struct qpnp_bms_chip *chip) |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1938 | { |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1939 | int soc, soc_change; |
| 1940 | int time_since_last_change_sec, charge_time_sec = 0; |
| 1941 | unsigned long last_change_sec; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1942 | struct timespec now; |
| 1943 | struct qpnp_vadc_result result; |
| 1944 | int batt_temp; |
| 1945 | int rc; |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1946 | bool charging, charging_since_last_report; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1947 | |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1948 | soc = chip->calculated_soc; |
| 1949 | |
| 1950 | rc = qpnp_vadc_read(LR_MUX1_BATT_THERM, &result); |
| 1951 | |
| 1952 | if (rc) { |
| 1953 | pr_err("error reading adc channel = %d, rc = %d\n", |
| 1954 | LR_MUX1_BATT_THERM, rc); |
| 1955 | return rc; |
| 1956 | } |
| 1957 | pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical, |
| 1958 | result.measurement); |
| 1959 | batt_temp = (int)result.physical; |
| 1960 | |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1961 | mutex_lock(&chip->last_soc_mutex); |
| 1962 | last_change_sec = chip->last_soc_change_sec; |
| 1963 | calculate_delta_time(&last_change_sec, &time_since_last_change_sec); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1964 | |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1965 | charging = is_battery_charging(chip); |
| 1966 | charging_since_last_report = charging || (chip->last_soc_unbound |
| 1967 | && chip->was_charging_at_sleep); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1968 | /* |
| 1969 | * account for charge time - limit it to SOC_CATCHUP_SEC to |
| 1970 | * avoid overflows when charging continues for extended periods |
| 1971 | */ |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1972 | if (charging) { |
| 1973 | if (chip->charge_start_tm_sec == 0) { |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1974 | /* |
| 1975 | * calculating soc for the first time |
| 1976 | * after start of chg. Initialize catchup time |
| 1977 | */ |
| 1978 | if (abs(soc - chip->last_soc) < MAX_CATCHUP_SOC) |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1979 | chip->catch_up_time_sec = |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1980 | (soc - chip->last_soc) |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1981 | * SOC_CATCHUP_SEC_PER_PERCENT; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1982 | else |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1983 | chip->catch_up_time_sec = SOC_CATCHUP_SEC_MAX; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1984 | |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1985 | if (chip->catch_up_time_sec < 0) |
| 1986 | chip->catch_up_time_sec = 0; |
| 1987 | chip->charge_start_tm_sec = last_change_sec; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1988 | } |
| 1989 | |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1990 | charge_time_sec = min(SOC_CATCHUP_SEC_MAX, (int)last_change_sec |
| 1991 | - chip->charge_start_tm_sec); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1992 | |
| 1993 | /* end catchup if calculated soc and last soc are same */ |
| 1994 | if (chip->last_soc == soc) |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1995 | chip->catch_up_time_sec = 0; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 1996 | } |
| 1997 | |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 1998 | if (chip->last_soc != -EINVAL) { |
| 1999 | /* last_soc < soc ... if we have not been charging at all |
| 2000 | * since the last time this was called, report previous SoC. |
| 2001 | * Otherwise, scale and catch up. |
| 2002 | */ |
| 2003 | if (chip->last_soc < soc && !charging_since_last_report) |
| 2004 | soc = chip->last_soc; |
| 2005 | else if (chip->last_soc < soc && soc != 100) |
| 2006 | soc = scale_soc_while_chg(chip, charge_time_sec, |
| 2007 | chip->catch_up_time_sec, |
| 2008 | soc, chip->last_soc); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2009 | |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 2010 | soc_change = min((int)abs(chip->last_soc - soc), |
| 2011 | time_since_last_change_sec / SOC_CHANGE_PER_SEC); |
| 2012 | if (chip->last_soc_unbound) { |
| 2013 | chip->last_soc_unbound = false; |
| 2014 | } else { |
| 2015 | /* |
| 2016 | * if soc have not been unbound by resume, |
| 2017 | * only change reported SoC by 1. |
| 2018 | */ |
| 2019 | soc_change = min(1, soc_change); |
| 2020 | } |
| 2021 | |
Xiaozhe Shi | cc13726 | 2013-03-10 06:21:41 -0700 | [diff] [blame] | 2022 | if (soc < chip->last_soc && soc != 0) |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 2023 | soc = chip->last_soc - soc_change; |
Xiaozhe Shi | cc13726 | 2013-03-10 06:21:41 -0700 | [diff] [blame] | 2024 | if (soc > chip->last_soc && soc != 100) |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 2025 | soc = chip->last_soc + soc_change; |
Xiaozhe Shi | cc13726 | 2013-03-10 06:21:41 -0700 | [diff] [blame] | 2026 | } |
| 2027 | |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 2028 | if (chip->last_soc != soc) |
| 2029 | chip->last_soc_change_sec = last_change_sec; |
| 2030 | |
| 2031 | pr_debug("last_soc = %d, calculated_soc = %d, soc = %d, time since last change = %d\n", |
| 2032 | chip->last_soc, chip->calculated_soc, |
| 2033 | soc, time_since_last_change_sec); |
Xiaozhe Shi | cc13726 | 2013-03-10 06:21:41 -0700 | [diff] [blame] | 2034 | chip->last_soc = bound_soc(soc); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2035 | backup_soc_and_iavg(chip, batt_temp, chip->last_soc); |
| 2036 | pr_debug("Reported SOC = %d\n", chip->last_soc); |
| 2037 | chip->t_soc_queried = now; |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 2038 | mutex_unlock(&chip->last_soc_mutex); |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2039 | |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 2040 | return soc; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2041 | } |
| 2042 | |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 2043 | static int report_state_of_charge(struct qpnp_bms_chip *chip) |
Xiaozhe Shi | 781b0a2 | 2012-11-05 17:18:27 -0800 | [diff] [blame] | 2044 | { |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 2045 | if (bms_fake_battery != -EINVAL) { |
| 2046 | pr_debug("Returning Fake SOC = %d%%\n", bms_fake_battery); |
| 2047 | return bms_fake_battery; |
| 2048 | } else if (chip->use_voltage_soc) |
| 2049 | return report_voltage_based_soc(chip); |
| 2050 | else |
| 2051 | return report_cc_based_soc(chip); |
Xiaozhe Shi | 781b0a2 | 2012-11-05 17:18:27 -0800 | [diff] [blame] | 2052 | } |
| 2053 | |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2054 | static void configure_vbat_monitor_low(struct qpnp_bms_chip *chip) |
| 2055 | { |
| 2056 | mutex_lock(&chip->vbat_monitor_mutex); |
| 2057 | if (chip->vbat_monitor_params.state_request |
| 2058 | == ADC_TM_HIGH_LOW_THR_ENABLE) { |
| 2059 | /* |
| 2060 | * Battery is now around or below v_cutoff |
| 2061 | */ |
| 2062 | pr_debug("battery entered cutoff range\n"); |
| 2063 | if (!wake_lock_active(&chip->low_voltage_wake_lock)) { |
| 2064 | pr_debug("voltage low, holding wakelock\n"); |
| 2065 | wake_lock(&chip->low_voltage_wake_lock); |
| 2066 | cancel_delayed_work_sync( |
| 2067 | &chip->calculate_soc_delayed_work); |
| 2068 | schedule_delayed_work( |
| 2069 | &chip->calculate_soc_delayed_work, 0); |
| 2070 | } |
| 2071 | chip->vbat_monitor_params.state_request = |
| 2072 | ADC_TM_HIGH_THR_ENABLE; |
| 2073 | chip->vbat_monitor_params.high_thr = |
| 2074 | (chip->low_voltage_threshold + VBATT_ERROR_MARGIN); |
| 2075 | pr_debug("set low thr to %d and high to %d\n", |
| 2076 | chip->vbat_monitor_params.low_thr, |
| 2077 | chip->vbat_monitor_params.high_thr); |
| 2078 | chip->vbat_monitor_params.low_thr = 0; |
| 2079 | } else if (chip->vbat_monitor_params.state_request |
| 2080 | == ADC_TM_LOW_THR_ENABLE) { |
| 2081 | /* |
| 2082 | * Battery is in normal operation range. |
| 2083 | */ |
| 2084 | pr_debug("battery entered normal range\n"); |
| 2085 | if (wake_lock_active(&chip->cv_wake_lock)) { |
| 2086 | wake_unlock(&chip->cv_wake_lock); |
| 2087 | pr_debug("releasing cv wake lock\n"); |
| 2088 | } |
| 2089 | chip->in_cv_range = false; |
| 2090 | chip->vbat_monitor_params.state_request = |
| 2091 | ADC_TM_HIGH_LOW_THR_ENABLE; |
| 2092 | chip->vbat_monitor_params.high_thr = chip->max_voltage_uv |
| 2093 | - VBATT_ERROR_MARGIN; |
| 2094 | chip->vbat_monitor_params.low_thr = |
| 2095 | chip->low_voltage_threshold; |
| 2096 | pr_debug("set low thr to %d and high to %d\n", |
| 2097 | chip->vbat_monitor_params.low_thr, |
| 2098 | chip->vbat_monitor_params.high_thr); |
| 2099 | } |
| 2100 | qpnp_adc_tm_channel_measure(&chip->vbat_monitor_params); |
| 2101 | mutex_unlock(&chip->vbat_monitor_mutex); |
| 2102 | } |
| 2103 | |
| 2104 | #define CV_LOW_THRESHOLD_HYST_UV 100000 |
| 2105 | static void configure_vbat_monitor_high(struct qpnp_bms_chip *chip) |
| 2106 | { |
| 2107 | mutex_lock(&chip->vbat_monitor_mutex); |
| 2108 | if (chip->vbat_monitor_params.state_request |
| 2109 | == ADC_TM_HIGH_LOW_THR_ENABLE) { |
| 2110 | /* |
| 2111 | * Battery is around vddmax |
| 2112 | */ |
| 2113 | pr_debug("battery entered vddmax range\n"); |
| 2114 | chip->in_cv_range = true; |
| 2115 | if (!wake_lock_active(&chip->cv_wake_lock)) { |
| 2116 | wake_lock(&chip->cv_wake_lock); |
| 2117 | pr_debug("holding cv wake lock\n"); |
| 2118 | } |
| 2119 | schedule_work(&chip->recalc_work); |
| 2120 | chip->vbat_monitor_params.state_request = |
| 2121 | ADC_TM_LOW_THR_ENABLE; |
| 2122 | chip->vbat_monitor_params.low_thr = |
| 2123 | (chip->max_voltage_uv - CV_LOW_THRESHOLD_HYST_UV); |
| 2124 | chip->vbat_monitor_params.high_thr = chip->max_voltage_uv * 2; |
| 2125 | pr_debug("set low thr to %d and high to %d\n", |
| 2126 | chip->vbat_monitor_params.low_thr, |
| 2127 | chip->vbat_monitor_params.high_thr); |
| 2128 | } else if (chip->vbat_monitor_params.state_request |
| 2129 | == ADC_TM_HIGH_THR_ENABLE) { |
| 2130 | /* |
| 2131 | * Battery is in normal operation range. |
| 2132 | */ |
| 2133 | pr_debug("battery entered normal range\n"); |
| 2134 | if (wake_lock_active(&chip->low_voltage_wake_lock)) { |
| 2135 | pr_debug("voltage high, releasing wakelock\n"); |
| 2136 | wake_unlock(&chip->low_voltage_wake_lock); |
| 2137 | } |
| 2138 | chip->vbat_monitor_params.state_request = |
| 2139 | ADC_TM_HIGH_LOW_THR_ENABLE; |
| 2140 | chip->vbat_monitor_params.high_thr = |
| 2141 | chip->max_voltage_uv - VBATT_ERROR_MARGIN; |
| 2142 | chip->vbat_monitor_params.low_thr = |
| 2143 | chip->low_voltage_threshold; |
| 2144 | pr_debug("set low thr to %d and high to %d\n", |
| 2145 | chip->vbat_monitor_params.low_thr, |
| 2146 | chip->vbat_monitor_params.high_thr); |
| 2147 | } |
| 2148 | qpnp_adc_tm_channel_measure(&chip->vbat_monitor_params); |
| 2149 | mutex_unlock(&chip->vbat_monitor_mutex); |
| 2150 | } |
| 2151 | |
| 2152 | static void btm_notify_vbat(enum qpnp_tm_state state, void *ctx) |
| 2153 | { |
| 2154 | struct qpnp_bms_chip *chip = ctx; |
| 2155 | int vbat_uv; |
| 2156 | struct qpnp_vadc_result result; |
| 2157 | int rc; |
| 2158 | |
| 2159 | rc = qpnp_vadc_read(VBAT_SNS, &result); |
| 2160 | pr_debug("vbat = %lld, raw = 0x%x\n", result.physical, result.adc_code); |
| 2161 | |
| 2162 | get_battery_voltage(&vbat_uv); |
| 2163 | pr_debug("vbat is at %d, state is at %d\n", vbat_uv, state); |
| 2164 | |
| 2165 | if (state == ADC_TM_LOW_STATE) { |
| 2166 | pr_debug("low voltage btm notification triggered\n"); |
| 2167 | if (vbat_uv - VBATT_ERROR_MARGIN |
| 2168 | < chip->vbat_monitor_params.low_thr) { |
| 2169 | configure_vbat_monitor_low(chip); |
| 2170 | } else { |
| 2171 | pr_debug("faulty btm trigger, discarding\n"); |
| 2172 | qpnp_adc_tm_channel_measure( |
| 2173 | &chip->vbat_monitor_params); |
| 2174 | } |
| 2175 | } else if (state == ADC_TM_HIGH_STATE) { |
| 2176 | pr_debug("high voltage btm notification triggered\n"); |
| 2177 | if (vbat_uv + VBATT_ERROR_MARGIN |
| 2178 | > chip->vbat_monitor_params.high_thr) { |
| 2179 | configure_vbat_monitor_high(chip); |
| 2180 | } else { |
| 2181 | pr_debug("faulty btm trigger, discarding\n"); |
| 2182 | qpnp_adc_tm_channel_measure( |
| 2183 | &chip->vbat_monitor_params); |
| 2184 | } |
| 2185 | } else { |
| 2186 | pr_debug("unknown voltage notification state: %d\n", state); |
| 2187 | } |
| 2188 | power_supply_changed(&chip->bms_psy); |
| 2189 | } |
| 2190 | |
| 2191 | static int reset_vbat_monitoring(struct qpnp_bms_chip *chip) |
| 2192 | { |
| 2193 | int rc; |
| 2194 | |
| 2195 | chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_DISABLE; |
| 2196 | rc = qpnp_adc_tm_channel_measure(&chip->vbat_monitor_params); |
| 2197 | if (rc) { |
| 2198 | pr_err("tm measure failed: %d\n", rc); |
| 2199 | return rc; |
| 2200 | } |
| 2201 | mutex_lock(&chip->vbat_monitor_mutex); |
| 2202 | if (wake_lock_active(&chip->low_voltage_wake_lock)) { |
| 2203 | pr_debug("battery removed, releasing wakelock\n"); |
| 2204 | wake_unlock(&chip->low_voltage_wake_lock); |
| 2205 | } |
| 2206 | if (chip->in_cv_range) { |
| 2207 | pr_debug("battery removed, removing in_cv_range state\n"); |
| 2208 | chip->in_cv_range = false; |
| 2209 | } |
| 2210 | mutex_unlock(&chip->vbat_monitor_mutex); |
| 2211 | return 0; |
| 2212 | } |
| 2213 | |
| 2214 | static int setup_vbat_monitoring(struct qpnp_bms_chip *chip) |
| 2215 | { |
| 2216 | int rc; |
| 2217 | |
| 2218 | rc = qpnp_adc_tm_is_ready(); |
| 2219 | if (rc) { |
| 2220 | pr_info("adc tm is not ready yet: %d, defer probe\n", rc); |
| 2221 | return -EPROBE_DEFER; |
| 2222 | } |
| 2223 | |
| 2224 | if (!is_battery_present(chip)) { |
| 2225 | pr_debug("no battery inserted, do not setup vbat monitoring\n"); |
| 2226 | return 0; |
| 2227 | } |
| 2228 | |
| 2229 | chip->vbat_monitor_params.low_thr = chip->low_voltage_threshold; |
| 2230 | chip->vbat_monitor_params.high_thr = chip->max_voltage_uv |
| 2231 | - VBATT_ERROR_MARGIN; |
| 2232 | chip->vbat_monitor_params.state_request = ADC_TM_HIGH_LOW_THR_ENABLE; |
| 2233 | chip->vbat_monitor_params.channel = VBAT_SNS; |
| 2234 | chip->vbat_monitor_params.btm_ctx = (void *)chip; |
| 2235 | chip->vbat_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S; |
| 2236 | chip->vbat_monitor_params.threshold_notification = &btm_notify_vbat; |
| 2237 | pr_debug("set low thr to %d and high to %d\n", |
| 2238 | chip->vbat_monitor_params.low_thr, |
| 2239 | chip->vbat_monitor_params.high_thr); |
| 2240 | rc = qpnp_adc_tm_channel_measure(&chip->vbat_monitor_params); |
| 2241 | if (rc) { |
| 2242 | pr_err("tm setup failed: %d\n", rc); |
| 2243 | return rc; |
| 2244 | } |
| 2245 | pr_debug("setup complete\n"); |
| 2246 | return 0; |
| 2247 | } |
| 2248 | |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 2249 | static void charging_began(struct qpnp_bms_chip *chip) |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2250 | { |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 2251 | mutex_lock(&chip->last_soc_mutex); |
| 2252 | chip->charge_start_tm_sec = 0; |
| 2253 | chip->catch_up_time_sec = 0; |
| 2254 | mutex_unlock(&chip->last_soc_mutex); |
| 2255 | |
| 2256 | mutex_lock(&chip->last_ocv_uv_mutex); |
| 2257 | chip->soc_at_cv = -EINVAL; |
| 2258 | chip->prev_chg_soc = -EINVAL; |
| 2259 | mutex_unlock(&chip->last_ocv_uv_mutex); |
| 2260 | } |
| 2261 | |
| 2262 | static void charging_ended(struct qpnp_bms_chip *chip) |
| 2263 | { |
| 2264 | mutex_lock(&chip->last_soc_mutex); |
| 2265 | chip->charge_start_tm_sec = 0; |
| 2266 | chip->catch_up_time_sec = 0; |
| 2267 | mutex_unlock(&chip->last_soc_mutex); |
| 2268 | |
| 2269 | mutex_lock(&chip->last_ocv_uv_mutex); |
| 2270 | chip->soc_at_cv = -EINVAL; |
| 2271 | chip->prev_chg_soc = -EINVAL; |
| 2272 | if (get_battery_status(chip) == POWER_SUPPLY_STATUS_FULL) |
| 2273 | chip->done_charging = true; |
| 2274 | mutex_unlock(&chip->last_ocv_uv_mutex); |
| 2275 | } |
| 2276 | |
| 2277 | static void battery_status_check(struct qpnp_bms_chip *chip) |
| 2278 | { |
| 2279 | int status = get_battery_status(chip); |
| 2280 | |
| 2281 | if (chip->battery_status != status) { |
| 2282 | if (status == POWER_SUPPLY_STATUS_CHARGING) { |
| 2283 | pr_debug("charging started\n"); |
| 2284 | charging_began(chip); |
| 2285 | } else if (chip->battery_status |
| 2286 | == POWER_SUPPLY_STATUS_CHARGING) { |
| 2287 | pr_debug("charging ended\n"); |
| 2288 | charging_ended(chip); |
| 2289 | } |
| 2290 | chip->battery_status = status; |
| 2291 | /* a new battery was inserted or removed, so force a soc |
| 2292 | * recalculation to update the SoC */ |
| 2293 | schedule_work(&chip->recalc_work); |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | static void battery_insertion_check(struct qpnp_bms_chip *chip) |
| 2298 | { |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2299 | bool present = is_battery_present(chip); |
| 2300 | |
| 2301 | mutex_lock(&chip->vbat_monitor_mutex); |
| 2302 | if (chip->battery_present != present) { |
| 2303 | if (chip->battery_present != -EINVAL) { |
| 2304 | if (present) { |
| 2305 | setup_vbat_monitoring(chip); |
| 2306 | chip->new_battery = true; |
| 2307 | } else { |
| 2308 | reset_vbat_monitoring(chip); |
| 2309 | } |
| 2310 | } |
| 2311 | chip->battery_present = present; |
| 2312 | /* a new battery was inserted or removed, so force a soc |
| 2313 | * recalculation to update the SoC */ |
| 2314 | schedule_work(&chip->recalc_work); |
| 2315 | } |
| 2316 | mutex_unlock(&chip->vbat_monitor_mutex); |
| 2317 | } |
| 2318 | |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2319 | /* Returns capacity as a SoC percentage between 0 and 100 */ |
| 2320 | static int get_prop_bms_capacity(struct qpnp_bms_chip *chip) |
| 2321 | { |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 2322 | return report_state_of_charge(chip); |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2323 | } |
| 2324 | |
Xiaozhe Shi | 6dc56f1 | 2013-05-02 15:56:55 -0700 | [diff] [blame] | 2325 | /* Returns estimated battery resistance */ |
| 2326 | static int get_prop_bms_batt_resistance(struct qpnp_bms_chip *chip) |
| 2327 | { |
| 2328 | return chip->rbatt_mohm * 1000; |
| 2329 | } |
| 2330 | |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2331 | /* Returns instantaneous current in uA */ |
| 2332 | static int get_prop_bms_current_now(struct qpnp_bms_chip *chip) |
| 2333 | { |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2334 | int rc, result_ua; |
| 2335 | |
| 2336 | rc = get_battery_current(chip, &result_ua); |
| 2337 | if (rc) { |
| 2338 | pr_err("failed to get current: %d\n", rc); |
| 2339 | return rc; |
| 2340 | } |
| 2341 | return result_ua; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | /* Returns full charge design in uAh */ |
| 2345 | static int get_prop_bms_charge_full_design(struct qpnp_bms_chip *chip) |
| 2346 | { |
Xiaozhe Shi | 976618f | 2013-04-30 10:49:30 -0700 | [diff] [blame] | 2347 | return chip->fcc_mah * 1000; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2348 | } |
| 2349 | |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2350 | static void qpnp_bms_external_power_changed(struct power_supply *psy) |
| 2351 | { |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 2352 | struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip, |
| 2353 | bms_psy); |
| 2354 | |
| 2355 | battery_insertion_check(chip); |
| 2356 | battery_status_check(chip); |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | static int qpnp_bms_power_get_property(struct power_supply *psy, |
| 2360 | enum power_supply_property psp, |
| 2361 | union power_supply_propval *val) |
| 2362 | { |
| 2363 | struct qpnp_bms_chip *chip = container_of(psy, struct qpnp_bms_chip, |
| 2364 | bms_psy); |
| 2365 | |
| 2366 | switch (psp) { |
| 2367 | case POWER_SUPPLY_PROP_CAPACITY: |
| 2368 | val->intval = get_prop_bms_capacity(chip); |
| 2369 | break; |
| 2370 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 2371 | val->intval = get_prop_bms_current_now(chip); |
| 2372 | break; |
Xiaozhe Shi | 6dc56f1 | 2013-05-02 15:56:55 -0700 | [diff] [blame] | 2373 | case POWER_SUPPLY_PROP_RESISTANCE: |
| 2374 | val->intval = get_prop_bms_batt_resistance(chip); |
| 2375 | break; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2376 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
| 2377 | val->intval = get_prop_bms_charge_full_design(chip); |
| 2378 | break; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2379 | default: |
| 2380 | return -EINVAL; |
| 2381 | } |
| 2382 | return 0; |
| 2383 | } |
| 2384 | |
Xiaozhe Shi | bdf1474 | 2012-12-05 12:41:48 -0800 | [diff] [blame] | 2385 | #define OCV_USE_LIMIT_EN BIT(7) |
| 2386 | static int set_ocv_voltage_thresholds(struct qpnp_bms_chip *chip, |
| 2387 | int low_voltage_threshold, |
| 2388 | int high_voltage_threshold) |
| 2389 | { |
| 2390 | uint16_t low_voltage_raw, high_voltage_raw; |
| 2391 | int rc; |
| 2392 | |
| 2393 | low_voltage_raw = convert_vbatt_uv_to_raw(chip, |
| 2394 | low_voltage_threshold); |
| 2395 | high_voltage_raw = convert_vbatt_uv_to_raw(chip, |
| 2396 | high_voltage_threshold); |
| 2397 | rc = qpnp_write_wrapper(chip, (u8 *)&low_voltage_raw, |
| 2398 | chip->base + BMS1_OCV_USE_LOW_LIMIT_THR0, 2); |
| 2399 | if (rc) { |
| 2400 | pr_err("Failed to set ocv low voltage threshold: %d\n", rc); |
| 2401 | return rc; |
| 2402 | } |
| 2403 | rc = qpnp_write_wrapper(chip, (u8 *)&high_voltage_raw, |
| 2404 | chip->base + BMS1_OCV_USE_HIGH_LIMIT_THR0, 2); |
| 2405 | if (rc) { |
| 2406 | pr_err("Failed to set ocv high voltage threshold: %d\n", rc); |
| 2407 | return rc; |
| 2408 | } |
| 2409 | rc = qpnp_masked_write(chip, BMS1_OCV_USE_LIMIT_CTL, |
| 2410 | OCV_USE_LIMIT_EN, OCV_USE_LIMIT_EN); |
| 2411 | if (rc) { |
| 2412 | pr_err("Failed to enabled ocv voltage thresholds: %d\n", rc); |
| 2413 | return rc; |
| 2414 | } |
| 2415 | pr_debug("ocv low threshold set to %d uv or 0x%x raw\n", |
| 2416 | low_voltage_threshold, low_voltage_raw); |
| 2417 | pr_debug("ocv high threshold set to %d uv or 0x%x raw\n", |
| 2418 | high_voltage_threshold, high_voltage_raw); |
| 2419 | return 0; |
| 2420 | } |
| 2421 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2422 | static void read_shutdown_soc_and_iavg(struct qpnp_bms_chip *chip) |
| 2423 | { |
| 2424 | int rc; |
| 2425 | u8 temp; |
| 2426 | |
| 2427 | if (chip->ignore_shutdown_soc) { |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 2428 | chip->shutdown_soc_invalid = true; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2429 | chip->shutdown_soc = 0; |
| 2430 | chip->shutdown_iavg_ma = 0; |
| 2431 | } else { |
| 2432 | rc = qpnp_read_wrapper(chip, &temp, |
| 2433 | chip->base + IAVG_STORAGE_REG, 1); |
| 2434 | if (rc) { |
| 2435 | pr_err("failed to read addr = %d %d assuming %d\n", |
| 2436 | chip->base + IAVG_STORAGE_REG, rc, |
| 2437 | IAVG_START); |
| 2438 | chip->shutdown_iavg_ma = IAVG_START; |
Xiaozhe Shi | f5f966d | 2013-02-19 14:23:11 -0800 | [diff] [blame] | 2439 | } else if (temp == IAVG_INVALID) { |
| 2440 | pr_err("invalid iavg read from BMS1_DATA_REG_1, using %d\n", |
| 2441 | IAVG_START); |
| 2442 | chip->shutdown_iavg_ma = IAVG_START; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2443 | } else { |
| 2444 | if (temp == 0) { |
| 2445 | chip->shutdown_iavg_ma = IAVG_START; |
| 2446 | } else { |
| 2447 | chip->shutdown_iavg_ma = IAVG_START |
| 2448 | + IAVG_STEP_SIZE_MA * (temp + 1); |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | rc = qpnp_read_wrapper(chip, &temp, |
| 2453 | chip->base + SOC_STORAGE_REG, 1); |
| 2454 | if (rc) { |
| 2455 | pr_err("failed to read addr = %d %d\n", |
| 2456 | chip->base + SOC_STORAGE_REG, rc); |
| 2457 | } else { |
| 2458 | chip->shutdown_soc = temp; |
| 2459 | |
Xiaozhe Shi | c7cbd05 | 2013-03-29 12:03:11 -0700 | [diff] [blame] | 2460 | if (chip->shutdown_soc == SOC_INVALID) { |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2461 | pr_debug("No shutdown soc available\n"); |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 2462 | chip->shutdown_soc_invalid = true; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2463 | chip->shutdown_iavg_ma = 0; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2464 | } |
| 2465 | } |
| 2466 | } |
| 2467 | |
| 2468 | pr_debug("shutdown_soc = %d shutdown_iavg = %d shutdown_soc_invalid = %d\n", |
| 2469 | chip->shutdown_soc, |
| 2470 | chip->shutdown_iavg_ma, |
| 2471 | chip->shutdown_soc_invalid); |
| 2472 | } |
| 2473 | |
Xiaozhe Shi | 73a6569 | 2012-09-18 17:51:57 -0700 | [diff] [blame] | 2474 | #define PALLADIUM_ID_MIN 0x7F40 |
| 2475 | #define PALLADIUM_ID_MAX 0x7F5A |
| 2476 | #define DESAY_5200_ID_MIN 0x7F7F |
| 2477 | #define DESAY_5200_ID_MAX 0x802F |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2478 | static int32_t read_battery_id(struct qpnp_bms_chip *chip) |
Xiaozhe Shi | 73a6569 | 2012-09-18 17:51:57 -0700 | [diff] [blame] | 2479 | { |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2480 | int rc; |
| 2481 | struct qpnp_vadc_result result; |
| 2482 | |
| 2483 | rc = qpnp_vadc_read(LR_MUX2_BAT_ID, &result); |
| 2484 | if (rc) { |
| 2485 | pr_err("error reading batt id channel = %d, rc = %d\n", |
| 2486 | LR_MUX2_BAT_ID, rc); |
| 2487 | return rc; |
| 2488 | } |
| 2489 | pr_debug("batt_id phy = %lld meas = 0x%llx\n", result.physical, |
| 2490 | result.measurement); |
| 2491 | pr_debug("raw_code = 0x%x\n", result.adc_code); |
| 2492 | return result.adc_code; |
Xiaozhe Shi | 73a6569 | 2012-09-18 17:51:57 -0700 | [diff] [blame] | 2493 | } |
| 2494 | |
| 2495 | static int set_battery_data(struct qpnp_bms_chip *chip) |
| 2496 | { |
| 2497 | int64_t battery_id; |
Xiaozhe Shi | 77a5b05 | 2012-12-14 16:37:45 -0800 | [diff] [blame] | 2498 | struct bms_battery_data *batt_data; |
Xiaozhe Shi | 73a6569 | 2012-09-18 17:51:57 -0700 | [diff] [blame] | 2499 | |
Xiaozhe Shi | 77a5b05 | 2012-12-14 16:37:45 -0800 | [diff] [blame] | 2500 | if (chip->batt_type == BATT_DESAY) { |
| 2501 | batt_data = &desay_5200_data; |
| 2502 | } else if (chip->batt_type == BATT_PALLADIUM) { |
| 2503 | batt_data = &palladium_1500_data; |
| 2504 | } else if (chip->batt_type == BATT_OEM) { |
| 2505 | batt_data = &oem_batt_data; |
Wu Fenglin | 2ac88aa | 2013-04-25 12:43:40 +0800 | [diff] [blame] | 2506 | } else if (chip->batt_type == BATT_QRD_4V35_2000MAH) { |
| 2507 | batt_data = &QRD_4v35_2000mAh_data; |
Xiaozhe Shi | 73a6569 | 2012-09-18 17:51:57 -0700 | [diff] [blame] | 2508 | } else { |
Xiaozhe Shi | 77a5b05 | 2012-12-14 16:37:45 -0800 | [diff] [blame] | 2509 | battery_id = read_battery_id(chip); |
| 2510 | if (battery_id < 0) { |
| 2511 | pr_err("cannot read battery id err = %lld\n", |
| 2512 | battery_id); |
| 2513 | return battery_id; |
| 2514 | } |
| 2515 | |
| 2516 | if (is_between(PALLADIUM_ID_MIN, PALLADIUM_ID_MAX, |
| 2517 | battery_id)) { |
| 2518 | batt_data = &palladium_1500_data; |
| 2519 | } else if (is_between(DESAY_5200_ID_MIN, DESAY_5200_ID_MAX, |
| 2520 | battery_id)) { |
| 2521 | batt_data = &desay_5200_data; |
| 2522 | } else { |
| 2523 | pr_warn("invalid battid, palladium 1500 assumed batt_id %llx\n", |
| 2524 | battery_id); |
| 2525 | batt_data = &palladium_1500_data; |
| 2526 | } |
Xiaozhe Shi | 73a6569 | 2012-09-18 17:51:57 -0700 | [diff] [blame] | 2527 | } |
| 2528 | |
Xiaozhe Shi | 976618f | 2013-04-30 10:49:30 -0700 | [diff] [blame] | 2529 | chip->fcc_mah = batt_data->fcc; |
Xiaozhe Shi | 77a5b05 | 2012-12-14 16:37:45 -0800 | [diff] [blame] | 2530 | chip->fcc_temp_lut = batt_data->fcc_temp_lut; |
| 2531 | chip->fcc_sf_lut = batt_data->fcc_sf_lut; |
| 2532 | chip->pc_temp_ocv_lut = batt_data->pc_temp_ocv_lut; |
| 2533 | chip->pc_sf_lut = batt_data->pc_sf_lut; |
| 2534 | chip->rbatt_sf_lut = batt_data->rbatt_sf_lut; |
| 2535 | chip->default_rbatt_mohm = batt_data->default_rbatt_mohm; |
Xiaozhe Shi | 1a10aff | 2013-04-01 15:40:05 -0700 | [diff] [blame] | 2536 | chip->rbatt_capacitive_mohm = batt_data->rbatt_capacitive_mohm; |
Xiaozhe Shi | 0ac7a00 | 2013-03-26 13:14:03 -0700 | [diff] [blame] | 2537 | chip->flat_ocv_threshold_uv = batt_data->flat_ocv_threshold_uv; |
Xiaozhe Shi | 77a5b05 | 2012-12-14 16:37:45 -0800 | [diff] [blame] | 2538 | |
| 2539 | if (chip->pc_temp_ocv_lut == NULL) { |
| 2540 | pr_err("temp ocv lut table is NULL\n"); |
| 2541 | return -EINVAL; |
| 2542 | } |
| 2543 | return 0; |
Xiaozhe Shi | 73a6569 | 2012-09-18 17:51:57 -0700 | [diff] [blame] | 2544 | } |
| 2545 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2546 | #define SPMI_PROP_READ(chip_prop, qpnp_spmi_property, retval) \ |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2547 | do { \ |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2548 | retval = of_property_read_u32(chip->spmi->dev.of_node, \ |
Xiaozhe Shi | 9bd2462 | 2013-01-23 15:54:54 -0800 | [diff] [blame] | 2549 | "qcom," qpnp_spmi_property, \ |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2550 | &chip->chip_prop); \ |
| 2551 | if (retval) { \ |
| 2552 | pr_err("Error reading " #qpnp_spmi_property \ |
| 2553 | " property %d\n", rc); \ |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2554 | return -EINVAL; \ |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2555 | } \ |
| 2556 | } while (0) |
| 2557 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2558 | static inline int bms_read_properties(struct qpnp_bms_chip *chip) |
| 2559 | { |
| 2560 | int rc; |
| 2561 | |
Xiaozhe Shi | d0a7954 | 2012-11-06 10:00:38 -0800 | [diff] [blame] | 2562 | SPMI_PROP_READ(r_sense_uohm, "r-sense-uohm", rc); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2563 | SPMI_PROP_READ(v_cutoff_uv, "v-cutoff-uv", rc); |
| 2564 | SPMI_PROP_READ(max_voltage_uv, "max-voltage-uv", rc); |
| 2565 | SPMI_PROP_READ(r_conn_mohm, "r-conn-mohm", rc); |
| 2566 | SPMI_PROP_READ(chg_term_ua, "chg-term-ua", rc); |
| 2567 | SPMI_PROP_READ(shutdown_soc_valid_limit, |
| 2568 | "shutdown-soc-valid-limit", rc); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2569 | SPMI_PROP_READ(adjust_soc_low_threshold, |
| 2570 | "adjust-soc-low-threshold", rc); |
| 2571 | SPMI_PROP_READ(batt_type, "batt-type", rc); |
| 2572 | SPMI_PROP_READ(low_soc_calc_threshold, |
| 2573 | "low-soc-calculate-soc-threshold", rc); |
| 2574 | SPMI_PROP_READ(low_soc_calculate_soc_ms, |
| 2575 | "low-soc-calculate-soc-ms", rc); |
| 2576 | SPMI_PROP_READ(calculate_soc_ms, "calculate-soc-ms", rc); |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 2577 | chip->use_external_rsense = of_property_read_bool( |
| 2578 | chip->spmi->dev.of_node, |
Xiaozhe Shi | 9bd2462 | 2013-01-23 15:54:54 -0800 | [diff] [blame] | 2579 | "qcom,use-external-rsense"); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2580 | chip->ignore_shutdown_soc = of_property_read_bool( |
| 2581 | chip->spmi->dev.of_node, |
Xiaozhe Shi | 9bd2462 | 2013-01-23 15:54:54 -0800 | [diff] [blame] | 2582 | "qcom,ignore-shutdown-soc"); |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 2583 | chip->use_voltage_soc = of_property_read_bool(chip->spmi->dev.of_node, |
Xiaozhe Shi | 9bd2462 | 2013-01-23 15:54:54 -0800 | [diff] [blame] | 2584 | "qcom,use-voltage-soc"); |
Xiaozhe Shi | bdf1474 | 2012-12-05 12:41:48 -0800 | [diff] [blame] | 2585 | chip->use_ocv_thresholds = of_property_read_bool( |
| 2586 | chip->spmi->dev.of_node, |
Xiaozhe Shi | 9bd2462 | 2013-01-23 15:54:54 -0800 | [diff] [blame] | 2587 | "qcom,use-ocv-thresholds"); |
Xiaozhe Shi | 0ac7a00 | 2013-03-26 13:14:03 -0700 | [diff] [blame] | 2588 | SPMI_PROP_READ(high_ocv_correction_limit_uv, |
| 2589 | "high-ocv-correction-limit-uv", rc); |
| 2590 | SPMI_PROP_READ(low_ocv_correction_limit_uv, |
| 2591 | "low-ocv-correction-limit-uv", rc); |
| 2592 | SPMI_PROP_READ(hold_soc_est, |
| 2593 | "hold-soc-est", rc); |
| 2594 | SPMI_PROP_READ(ocv_high_threshold_uv, |
| 2595 | "ocv-voltage-high-threshold-uv", rc); |
Xiaozhe Shi | bdf1474 | 2012-12-05 12:41:48 -0800 | [diff] [blame] | 2596 | SPMI_PROP_READ(ocv_low_threshold_uv, |
| 2597 | "ocv-voltage-low-threshold-uv", rc); |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 2598 | SPMI_PROP_READ(low_voltage_threshold, "low-voltage-threshold", rc); |
Xiaozhe Shi | 535494d | 2013-04-05 12:27:51 -0700 | [diff] [blame] | 2599 | SPMI_PROP_READ(temperature_margin, "tm-temp-margin", rc); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2600 | |
| 2601 | if (chip->adjust_soc_low_threshold >= 45) |
| 2602 | chip->adjust_soc_low_threshold = 45; |
| 2603 | |
Xiaozhe Shi | d0a7954 | 2012-11-06 10:00:38 -0800 | [diff] [blame] | 2604 | pr_debug("dts data: r_sense_uohm:%d, v_cutoff_uv:%d, max_v:%d\n", |
| 2605 | chip->r_sense_uohm, chip->v_cutoff_uv, |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2606 | chip->max_voltage_uv); |
| 2607 | pr_debug("r_conn:%d, shutdown_soc: %d, adjust_soc_low:%d\n", |
| 2608 | chip->r_conn_mohm, chip->shutdown_soc_valid_limit, |
| 2609 | chip->adjust_soc_low_threshold); |
Xiaozhe Shi | 561ebf7 | 2013-03-25 13:51:27 -0700 | [diff] [blame] | 2610 | pr_debug("chg_term_ua:%d, batt_type:%d\n", |
| 2611 | chip->chg_term_ua, |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2612 | chip->batt_type); |
Xiaozhe Shi | 781b0a2 | 2012-11-05 17:18:27 -0800 | [diff] [blame] | 2613 | pr_debug("ignore_shutdown_soc:%d, use_voltage_soc:%d\n", |
Xiaozhe Shi | 79d6c1d | 2012-11-26 13:19:50 -0800 | [diff] [blame] | 2614 | chip->ignore_shutdown_soc, chip->use_voltage_soc); |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 2615 | pr_debug("use external rsense: %d\n", chip->use_external_rsense); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2616 | return 0; |
| 2617 | } |
| 2618 | |
| 2619 | static inline void bms_initialize_constants(struct qpnp_bms_chip *chip) |
| 2620 | { |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2621 | chip->prev_pc_unusable = -EINVAL; |
| 2622 | chip->soc_at_cv = -EINVAL; |
| 2623 | chip->calculated_soc = -EINVAL; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2624 | chip->last_soc = -EINVAL; |
Xiaozhe Shi | 7edde5d | 2012-09-26 11:23:09 -0700 | [diff] [blame] | 2625 | chip->last_soc_est = -EINVAL; |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2626 | chip->battery_present = -EINVAL; |
Xiaozhe Shi | 890fbf4 | 2013-05-02 16:42:53 -0700 | [diff] [blame] | 2627 | chip->battery_status = POWER_SUPPLY_STATUS_UNKNOWN; |
Xiaozhe Shi | f36d286 | 2013-01-04 10:17:35 -0800 | [diff] [blame] | 2628 | chip->last_cc_uah = INT_MIN; |
Abhijeet Dharmapurikar | 15f30fb | 2012-12-27 17:20:29 -0800 | [diff] [blame] | 2629 | chip->ocv_reading_at_100 = OCV_RAW_UNINITIALIZED; |
| 2630 | chip->prev_last_good_ocv_raw = OCV_RAW_UNINITIALIZED; |
Xiaozhe Shi | e118c69 | 2012-09-24 15:17:43 -0700 | [diff] [blame] | 2631 | chip->first_time_calc_soc = 1; |
| 2632 | chip->first_time_calc_uuc = 1; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2633 | } |
| 2634 | |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 2635 | #define REG_OFFSET_PERP_TYPE 0x04 |
| 2636 | #define REG_OFFSET_PERP_SUBTYPE 0x05 |
| 2637 | #define BMS_BMS_TYPE 0xD |
Xiaozhe Shi | ef6274c | 2013-03-06 15:23:52 -0800 | [diff] [blame] | 2638 | #define BMS_BMS1_SUBTYPE 0x1 |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 2639 | #define BMS_IADC_TYPE 0x8 |
Xiaozhe Shi | ef6274c | 2013-03-06 15:23:52 -0800 | [diff] [blame] | 2640 | #define BMS_IADC1_SUBTYPE 0x3 |
| 2641 | #define BMS_IADC2_SUBTYPE 0x5 |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 2642 | |
| 2643 | static int register_spmi(struct qpnp_bms_chip *chip, struct spmi_device *spmi) |
| 2644 | { |
| 2645 | struct spmi_resource *spmi_resource; |
| 2646 | struct resource *resource; |
| 2647 | int rc; |
| 2648 | u8 type, subtype; |
| 2649 | |
| 2650 | chip->dev = &(spmi->dev); |
| 2651 | chip->spmi = spmi; |
| 2652 | |
| 2653 | spmi_for_each_container_dev(spmi_resource, spmi) { |
| 2654 | if (!spmi_resource) { |
| 2655 | pr_err("qpnp_bms: spmi resource absent\n"); |
| 2656 | return -ENXIO; |
| 2657 | } |
| 2658 | |
| 2659 | resource = spmi_get_resource(spmi, spmi_resource, |
| 2660 | IORESOURCE_MEM, 0); |
| 2661 | if (!(resource && resource->start)) { |
| 2662 | pr_err("node %s IO resource absent!\n", |
| 2663 | spmi->dev.of_node->full_name); |
| 2664 | return -ENXIO; |
| 2665 | } |
| 2666 | |
| 2667 | rc = qpnp_read_wrapper(chip, &type, |
| 2668 | resource->start + REG_OFFSET_PERP_TYPE, 1); |
| 2669 | if (rc) { |
| 2670 | pr_err("Peripheral type read failed rc=%d\n", rc); |
| 2671 | return rc; |
| 2672 | } |
| 2673 | rc = qpnp_read_wrapper(chip, &subtype, |
| 2674 | resource->start + REG_OFFSET_PERP_SUBTYPE, 1); |
| 2675 | if (rc) { |
| 2676 | pr_err("Peripheral subtype read failed rc=%d\n", rc); |
| 2677 | return rc; |
| 2678 | } |
| 2679 | |
Xiaozhe Shi | ef6274c | 2013-03-06 15:23:52 -0800 | [diff] [blame] | 2680 | if (type == BMS_BMS_TYPE && subtype == BMS_BMS1_SUBTYPE) { |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 2681 | chip->base = resource->start; |
| 2682 | } else if (type == BMS_IADC_TYPE |
Xiaozhe Shi | ef6274c | 2013-03-06 15:23:52 -0800 | [diff] [blame] | 2683 | && (subtype == BMS_IADC1_SUBTYPE |
| 2684 | || subtype == BMS_IADC2_SUBTYPE)) { |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 2685 | chip->iadc_base = resource->start; |
| 2686 | } else { |
| 2687 | pr_err("Invalid peripheral start=0x%x type=0x%x, subtype=0x%x\n", |
| 2688 | resource->start, type, subtype); |
| 2689 | } |
| 2690 | } |
| 2691 | |
| 2692 | if (chip->base == 0) { |
| 2693 | dev_err(&spmi->dev, "BMS peripheral was not registered\n"); |
| 2694 | return -EINVAL; |
| 2695 | } |
| 2696 | if (chip->iadc_base == 0) { |
| 2697 | dev_err(&spmi->dev, "BMS_IADC peripheral was not registered\n"); |
| 2698 | return -EINVAL; |
| 2699 | } |
| 2700 | |
| 2701 | return 0; |
| 2702 | } |
| 2703 | |
| 2704 | #define ADC_CH_SEL_MASK 0x7 |
| 2705 | static int read_iadc_channel_select(struct qpnp_bms_chip *chip) |
| 2706 | { |
| 2707 | u8 iadc_channel_select; |
Xiaozhe Shi | 767fdb6 | 2013-01-10 15:09:08 -0800 | [diff] [blame] | 2708 | int32_t rds_rsense_nohm; |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 2709 | int rc; |
| 2710 | |
| 2711 | rc = qpnp_read_wrapper(chip, &iadc_channel_select, |
| 2712 | chip->iadc_base + IADC1_BMS_ADC_CH_SEL_CTL, 1); |
| 2713 | if (rc) { |
| 2714 | pr_err("Error reading bms_iadc channel register %d\n", rc); |
| 2715 | return rc; |
| 2716 | } |
| 2717 | |
| 2718 | iadc_channel_select &= ADC_CH_SEL_MASK; |
Xiaozhe Shi | 767fdb6 | 2013-01-10 15:09:08 -0800 | [diff] [blame] | 2719 | if (iadc_channel_select != EXTERNAL_RSENSE |
| 2720 | && iadc_channel_select != INTERNAL_RSENSE) { |
| 2721 | pr_err("IADC1_BMS_IADC configured incorrectly. Selected channel = %d\n", |
| 2722 | iadc_channel_select); |
| 2723 | return -EINVAL; |
| 2724 | } |
| 2725 | |
| 2726 | if (chip->use_external_rsense) { |
| 2727 | pr_debug("External rsense selected\n"); |
| 2728 | if (iadc_channel_select == INTERNAL_RSENSE) { |
| 2729 | pr_debug("Internal rsense detected; Changing rsense to external\n"); |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 2730 | rc = qpnp_masked_write_iadc(chip, |
| 2731 | IADC1_BMS_ADC_CH_SEL_CTL, |
| 2732 | ADC_CH_SEL_MASK, |
| 2733 | EXTERNAL_RSENSE); |
| 2734 | if (rc) { |
| 2735 | pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n", |
| 2736 | IADC1_BMS_ADC_CH_SEL_CTL, |
| 2737 | EXTERNAL_RSENSE, rc); |
| 2738 | return rc; |
| 2739 | } |
| 2740 | reset_cc(chip); |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 2741 | chip->software_cc_uah = 0; |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 2742 | } |
Xiaozhe Shi | 767fdb6 | 2013-01-10 15:09:08 -0800 | [diff] [blame] | 2743 | } else { |
| 2744 | pr_debug("Internal rsense selected\n"); |
| 2745 | if (iadc_channel_select == EXTERNAL_RSENSE) { |
| 2746 | pr_debug("External rsense detected; Changing rsense to internal\n"); |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 2747 | rc = qpnp_masked_write_iadc(chip, |
| 2748 | IADC1_BMS_ADC_CH_SEL_CTL, |
| 2749 | ADC_CH_SEL_MASK, |
| 2750 | INTERNAL_RSENSE); |
| 2751 | if (rc) { |
| 2752 | pr_err("Unable to set IADC1_BMS channel %x to %x: %d\n", |
| 2753 | IADC1_BMS_ADC_CH_SEL_CTL, |
| 2754 | INTERNAL_RSENSE, rc); |
| 2755 | return rc; |
| 2756 | } |
| 2757 | reset_cc(chip); |
Xiaozhe Shi | 80ba88d | 2013-04-05 10:23:34 -0700 | [diff] [blame] | 2758 | chip->software_cc_uah = 0; |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 2759 | } |
Xiaozhe Shi | 767fdb6 | 2013-01-10 15:09:08 -0800 | [diff] [blame] | 2760 | |
| 2761 | rc = qpnp_iadc_get_rsense(&rds_rsense_nohm); |
| 2762 | if (rc) { |
| 2763 | pr_err("Unable to read RDS resistance value from IADC; rc = %d\n", |
| 2764 | rc); |
| 2765 | return rc; |
| 2766 | } |
Xiaozhe Shi | d0a7954 | 2012-11-06 10:00:38 -0800 | [diff] [blame] | 2767 | chip->r_sense_uohm = rds_rsense_nohm/1000; |
| 2768 | pr_debug("rds_rsense = %d nOhm, saved as %d uOhm\n", |
| 2769 | rds_rsense_nohm, chip->r_sense_uohm); |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 2770 | } |
| 2771 | return 0; |
| 2772 | } |
| 2773 | |
Xiaozhe Shi | 535494d | 2013-04-05 12:27:51 -0700 | [diff] [blame] | 2774 | static int refresh_die_temp_monitor(struct qpnp_bms_chip *chip) |
| 2775 | { |
| 2776 | struct qpnp_vadc_result result; |
| 2777 | int rc; |
| 2778 | |
| 2779 | rc = qpnp_vadc_read(DIE_TEMP, &result); |
| 2780 | |
| 2781 | pr_debug("low = %lld, high = %lld\n", |
| 2782 | result.physical - chip->temperature_margin, |
| 2783 | result.physical + chip->temperature_margin); |
| 2784 | chip->die_temp_monitor_params.high_temp = result.physical |
| 2785 | + chip->temperature_margin; |
| 2786 | chip->die_temp_monitor_params.low_temp = result.physical |
| 2787 | - chip->temperature_margin; |
| 2788 | chip->die_temp_monitor_params.state_request = |
| 2789 | ADC_TM_HIGH_LOW_THR_ENABLE; |
| 2790 | return qpnp_adc_tm_channel_measure(&chip->die_temp_monitor_params); |
| 2791 | } |
| 2792 | |
| 2793 | static void btm_notify_die_temp(enum qpnp_tm_state state, void *ctx) |
| 2794 | { |
| 2795 | struct qpnp_bms_chip *chip = ctx; |
| 2796 | struct qpnp_vadc_result result; |
| 2797 | int rc; |
| 2798 | |
| 2799 | rc = qpnp_vadc_read(DIE_TEMP, &result); |
| 2800 | |
| 2801 | if (state == ADC_TM_LOW_STATE) |
| 2802 | pr_debug("low state triggered\n"); |
| 2803 | else if (state == ADC_TM_HIGH_STATE) |
| 2804 | pr_debug("high state triggered\n"); |
| 2805 | pr_debug("die temp = %lld, raw = 0x%x\n", |
| 2806 | result.physical, result.adc_code); |
| 2807 | schedule_work(&chip->recalc_work); |
| 2808 | refresh_die_temp_monitor(chip); |
| 2809 | } |
| 2810 | |
| 2811 | static int setup_die_temp_monitoring(struct qpnp_bms_chip *chip) |
| 2812 | { |
| 2813 | int rc = qpnp_adc_tm_is_ready(); |
| 2814 | if (rc) { |
| 2815 | pr_info("adc tm is not ready yet: %d, defer probe\n", rc); |
| 2816 | return -EPROBE_DEFER; |
| 2817 | } |
| 2818 | chip->die_temp_monitor_params.channel = DIE_TEMP; |
| 2819 | chip->die_temp_monitor_params.btm_ctx = (void *)chip; |
| 2820 | chip->die_temp_monitor_params.timer_interval = ADC_MEAS1_INTERVAL_1S; |
| 2821 | chip->die_temp_monitor_params.threshold_notification = |
| 2822 | &btm_notify_die_temp; |
| 2823 | refresh_die_temp_monitor(chip); |
| 2824 | if (rc) { |
| 2825 | pr_err("tm setup failed: %d\n", rc); |
| 2826 | return rc; |
| 2827 | } |
| 2828 | pr_debug("setup complete\n"); |
| 2829 | return 0; |
| 2830 | } |
| 2831 | |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 2832 | static int __devinit qpnp_bms_probe(struct spmi_device *spmi) |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2833 | { |
| 2834 | struct qpnp_bms_chip *chip; |
Xiaozhe Shi | 77ec38d | 2013-04-29 16:41:58 -0700 | [diff] [blame] | 2835 | bool warm_reset; |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2836 | int rc, vbatt; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2837 | |
| 2838 | chip = kzalloc(sizeof *chip, GFP_KERNEL); |
| 2839 | |
| 2840 | if (chip == NULL) { |
| 2841 | pr_err("kzalloc() failed.\n"); |
| 2842 | return -ENOMEM; |
| 2843 | } |
| 2844 | |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 2845 | rc = qpnp_vadc_is_ready(); |
| 2846 | if (rc) { |
| 2847 | pr_info("vadc not ready: %d, deferring probe\n", rc); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2848 | rc = -EPROBE_DEFER; |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 2849 | goto error_read; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2850 | } |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 2851 | |
| 2852 | rc = qpnp_iadc_is_ready(); |
| 2853 | if (rc) { |
| 2854 | pr_info("iadc not ready: %d, deferring probe\n", rc); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2855 | rc = -EPROBE_DEFER; |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 2856 | goto error_read; |
| 2857 | } |
| 2858 | |
Xiaozhe Shi | 77ec38d | 2013-04-29 16:41:58 -0700 | [diff] [blame] | 2859 | warm_reset = qpnp_pon_is_warm_reset(); |
| 2860 | rc = warm_reset; |
| 2861 | if (rc < 0) |
| 2862 | goto error_read; |
| 2863 | |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 2864 | rc = register_spmi(chip, spmi); |
| 2865 | if (rc) { |
| 2866 | pr_err("error registering spmi resource %d\n", rc); |
| 2867 | goto error_resource; |
| 2868 | } |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2869 | |
| 2870 | rc = qpnp_read_wrapper(chip, &chip->revision1, |
| 2871 | chip->base + BMS1_REVISION1, 1); |
| 2872 | if (rc) { |
| 2873 | pr_err("error reading version register %d\n", rc); |
| 2874 | goto error_read; |
| 2875 | } |
| 2876 | |
| 2877 | rc = qpnp_read_wrapper(chip, &chip->revision2, |
| 2878 | chip->base + BMS1_REVISION2, 1); |
| 2879 | if (rc) { |
| 2880 | pr_err("Error reading version register %d\n", rc); |
| 2881 | goto error_read; |
| 2882 | } |
Xiaozhe Shi | a045a56 | 2012-11-28 16:55:39 -0800 | [diff] [blame] | 2883 | pr_debug("BMS version: %hhu.%hhu\n", chip->revision2, chip->revision1); |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2884 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2885 | rc = bms_read_properties(chip); |
| 2886 | if (rc) { |
| 2887 | pr_err("Unable to read all bms properties, rc = %d\n", rc); |
| 2888 | goto error_read; |
| 2889 | } |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2890 | |
Xiaozhe Shi | dffbe69 | 2012-12-11 15:35:46 -0800 | [diff] [blame] | 2891 | rc = read_iadc_channel_select(chip); |
| 2892 | if (rc) { |
| 2893 | pr_err("Unable to get iadc selected channel = %d\n", rc); |
| 2894 | goto error_read; |
| 2895 | } |
| 2896 | |
Xiaozhe Shi | bdf1474 | 2012-12-05 12:41:48 -0800 | [diff] [blame] | 2897 | if (chip->use_ocv_thresholds) { |
| 2898 | rc = set_ocv_voltage_thresholds(chip, |
| 2899 | chip->ocv_low_threshold_uv, |
| 2900 | chip->ocv_high_threshold_uv); |
| 2901 | if (rc) { |
| 2902 | pr_err("Could not set ocv voltage thresholds: %d\n", |
| 2903 | rc); |
| 2904 | goto error_read; |
| 2905 | } |
| 2906 | } |
| 2907 | |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 2908 | rc = set_battery_data(chip); |
| 2909 | if (rc) { |
| 2910 | pr_err("Bad battery data %d\n", rc); |
| 2911 | goto error_read; |
| 2912 | } |
| 2913 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2914 | bms_initialize_constants(chip); |
| 2915 | |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 2916 | mutex_init(&chip->bms_output_lock); |
| 2917 | mutex_init(&chip->last_ocv_uv_mutex); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2918 | mutex_init(&chip->vbat_monitor_mutex); |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 2919 | mutex_init(&chip->soc_invalidation_mutex); |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 2920 | mutex_init(&chip->last_soc_mutex); |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 2921 | |
Abhijeet Dharmapurikar | 713b60a | 2012-12-26 21:30:05 -0800 | [diff] [blame] | 2922 | wake_lock_init(&chip->soc_wake_lock, WAKE_LOCK_SUSPEND, |
| 2923 | "qpnp_soc_lock"); |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 2924 | wake_lock_init(&chip->low_voltage_wake_lock, WAKE_LOCK_SUSPEND, |
| 2925 | "qpnp_low_voltage_lock"); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2926 | wake_lock_init(&chip->cv_wake_lock, WAKE_LOCK_SUSPEND, |
| 2927 | "qpnp_cv_lock"); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2928 | INIT_DELAYED_WORK(&chip->calculate_soc_delayed_work, |
| 2929 | calculate_soc_work); |
Xiaozhe Shi | 5cf7a67 | 2013-02-06 17:18:05 -0800 | [diff] [blame] | 2930 | INIT_WORK(&chip->recalc_work, recalculate_work); |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2931 | |
| 2932 | read_shutdown_soc_and_iavg(chip); |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2933 | |
| 2934 | dev_set_drvdata(&spmi->dev, chip); |
| 2935 | device_init_wakeup(&spmi->dev, 1); |
| 2936 | |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2937 | rc = setup_vbat_monitoring(chip); |
| 2938 | if (rc < 0) { |
| 2939 | pr_err("failed to set up voltage notifications: %d\n", rc); |
| 2940 | goto error_setup; |
Xiaozhe Shi | d5d2141 | 2013-02-06 17:14:41 -0800 | [diff] [blame] | 2941 | } |
| 2942 | |
Xiaozhe Shi | 535494d | 2013-04-05 12:27:51 -0700 | [diff] [blame] | 2943 | rc = setup_die_temp_monitoring(chip); |
| 2944 | if (rc < 0) { |
| 2945 | pr_err("failed to set up die temp notifications: %d\n", rc); |
| 2946 | goto error_setup; |
| 2947 | } |
| 2948 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2949 | calculate_soc_work(&(chip->calculate_soc_delayed_work.work)); |
| 2950 | |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2951 | /* setup & register the battery power supply */ |
| 2952 | chip->bms_psy.name = "bms"; |
| 2953 | chip->bms_psy.type = POWER_SUPPLY_TYPE_BMS; |
| 2954 | chip->bms_psy.properties = msm_bms_power_props; |
| 2955 | chip->bms_psy.num_properties = ARRAY_SIZE(msm_bms_power_props); |
| 2956 | chip->bms_psy.get_property = qpnp_bms_power_get_property; |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2957 | chip->bms_psy.external_power_changed = |
| 2958 | qpnp_bms_external_power_changed; |
| 2959 | chip->bms_psy.supplied_to = qpnp_bms_supplicants; |
| 2960 | chip->bms_psy.num_supplicants = ARRAY_SIZE(qpnp_bms_supplicants); |
| 2961 | |
| 2962 | rc = power_supply_register(chip->dev, &chip->bms_psy); |
| 2963 | |
| 2964 | if (rc < 0) { |
| 2965 | pr_err("power_supply_register bms failed rc = %d\n", rc); |
| 2966 | goto unregister_dc; |
| 2967 | } |
| 2968 | |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2969 | vbatt = 0; |
Xiaozhe Shi | 3645896 | 2013-02-06 16:19:57 -0800 | [diff] [blame] | 2970 | rc = get_battery_voltage(&vbatt); |
| 2971 | if (rc) { |
| 2972 | pr_err("error reading vbat_sns adc channel = %d, rc = %d\n", |
| 2973 | VBAT_SNS, rc); |
| 2974 | goto unregister_dc; |
| 2975 | } |
Xiaozhe Shi | cd7e530 | 2012-10-17 12:29:53 -0700 | [diff] [blame] | 2976 | |
Xiaozhe Shi | 77ec38d | 2013-04-29 16:41:58 -0700 | [diff] [blame] | 2977 | pr_info("probe success: soc =%d vbatt = %d ocv = %d r_sense_uohm = %u warm_reset = %d\n", |
| 2978 | get_prop_bms_capacity(chip), vbatt, chip->last_ocv_uv, |
| 2979 | chip->r_sense_uohm, warm_reset); |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2980 | return 0; |
| 2981 | |
| 2982 | unregister_dc: |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2983 | power_supply_unregister(&chip->bms_psy); |
| 2984 | error_setup: |
| 2985 | dev_set_drvdata(&spmi->dev, NULL); |
Abhijeet Dharmapurikar | 713b60a | 2012-12-26 21:30:05 -0800 | [diff] [blame] | 2986 | wake_lock_destroy(&chip->soc_wake_lock); |
Xiaozhe Shi | 4be8578 | 2013-02-22 17:33:40 -0800 | [diff] [blame] | 2987 | wake_lock_destroy(&chip->low_voltage_wake_lock); |
Xiaozhe Shi | a6618a2 | 2013-03-27 10:26:29 -0700 | [diff] [blame] | 2988 | wake_lock_destroy(&chip->cv_wake_lock); |
Xiaozhe Shi | c40b397 | 2012-11-30 14:11:16 -0800 | [diff] [blame] | 2989 | error_resource: |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 2990 | error_read: |
| 2991 | kfree(chip); |
| 2992 | return rc; |
| 2993 | } |
| 2994 | |
| 2995 | static int __devexit |
| 2996 | qpnp_bms_remove(struct spmi_device *spmi) |
| 2997 | { |
| 2998 | struct qpnp_bms_chip *chip = dev_get_drvdata(&spmi->dev); |
| 2999 | |
| 3000 | dev_set_drvdata(&spmi->dev, NULL); |
| 3001 | kfree(chip); |
| 3002 | return 0; |
| 3003 | } |
| 3004 | |
Xiaozhe Shi | d6168d8 | 2013-04-18 16:06:17 -0700 | [diff] [blame] | 3005 | static int bms_suspend(struct device *dev) |
| 3006 | { |
| 3007 | struct qpnp_bms_chip *chip = dev_get_drvdata(dev); |
| 3008 | |
| 3009 | cancel_delayed_work_sync(&chip->calculate_soc_delayed_work); |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 3010 | chip->was_charging_at_sleep = is_battery_charging(chip); |
Xiaozhe Shi | d6168d8 | 2013-04-18 16:06:17 -0700 | [diff] [blame] | 3011 | return 0; |
| 3012 | } |
| 3013 | |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 3014 | static int bms_resume(struct device *dev) |
| 3015 | { |
| 3016 | int rc; |
Xiaozhe Shi | d6168d8 | 2013-04-18 16:06:17 -0700 | [diff] [blame] | 3017 | int soc_calc_period; |
| 3018 | int time_until_next_recalc; |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 3019 | unsigned long time_since_last_recalc; |
| 3020 | unsigned long tm_now_sec; |
| 3021 | struct qpnp_bms_chip *chip = dev_get_drvdata(dev); |
| 3022 | |
| 3023 | rc = get_current_time(&tm_now_sec); |
| 3024 | if (rc) { |
| 3025 | pr_err("Could not read current time: %d\n", rc); |
| 3026 | } else if (tm_now_sec > chip->last_recalc_time) { |
| 3027 | time_since_last_recalc = tm_now_sec - chip->last_recalc_time; |
| 3028 | pr_debug("Time since last recalc: %lu\n", |
| 3029 | time_since_last_recalc); |
| 3030 | if (chip->calculated_soc < chip->low_soc_calc_threshold) |
| 3031 | soc_calc_period = chip->low_soc_calculate_soc_ms; |
| 3032 | else |
| 3033 | soc_calc_period = chip->calculate_soc_ms; |
| 3034 | |
Xiaozhe Shi | d6168d8 | 2013-04-18 16:06:17 -0700 | [diff] [blame] | 3035 | time_until_next_recalc = max(0, soc_calc_period |
| 3036 | - (int)(time_since_last_recalc * 1000)); |
| 3037 | |
Xiaozhe Shi | 04da099 | 2013-04-26 16:32:14 -0700 | [diff] [blame] | 3038 | if (!wake_lock_active(&chip->soc_wake_lock) |
| 3039 | && time_until_next_recalc == 0) |
| 3040 | wake_lock(&chip->soc_wake_lock); |
| 3041 | |
Xiaozhe Shi | d6168d8 | 2013-04-18 16:06:17 -0700 | [diff] [blame] | 3042 | schedule_delayed_work(&chip->calculate_soc_delayed_work, |
| 3043 | round_jiffies_relative(msecs_to_jiffies |
| 3044 | (time_until_next_recalc))); |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 3045 | } |
| 3046 | return 0; |
| 3047 | } |
| 3048 | |
| 3049 | static const struct dev_pm_ops qpnp_bms_pm_ops = { |
| 3050 | .resume = bms_resume, |
Xiaozhe Shi | d6168d8 | 2013-04-18 16:06:17 -0700 | [diff] [blame] | 3051 | .suspend = bms_suspend, |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 3052 | }; |
| 3053 | |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 3054 | static struct spmi_driver qpnp_bms_driver = { |
| 3055 | .probe = qpnp_bms_probe, |
| 3056 | .remove = __devexit_p(qpnp_bms_remove), |
| 3057 | .driver = { |
| 3058 | .name = QPNP_BMS_DEV_NAME, |
| 3059 | .owner = THIS_MODULE, |
| 3060 | .of_match_table = qpnp_bms_match_table, |
Xiaozhe Shi | cdeee31 | 2012-12-18 15:10:18 -0800 | [diff] [blame] | 3061 | .pm = &qpnp_bms_pm_ops, |
Xiaozhe Shi | b19f703 | 2012-08-16 12:14:16 -0700 | [diff] [blame] | 3062 | }, |
| 3063 | }; |
| 3064 | |
| 3065 | static int __init qpnp_bms_init(void) |
| 3066 | { |
| 3067 | pr_info("QPNP BMS INIT\n"); |
| 3068 | return spmi_driver_register(&qpnp_bms_driver); |
| 3069 | } |
| 3070 | |
| 3071 | static void __exit qpnp_bms_exit(void) |
| 3072 | { |
| 3073 | pr_info("QPNP BMS EXIT\n"); |
| 3074 | return spmi_driver_unregister(&qpnp_bms_driver); |
| 3075 | } |
| 3076 | |
| 3077 | module_init(qpnp_bms_init); |
| 3078 | module_exit(qpnp_bms_exit); |
| 3079 | |
| 3080 | MODULE_DESCRIPTION("QPNP BMS Driver"); |
| 3081 | MODULE_LICENSE("GPL v2"); |
| 3082 | MODULE_ALIAS("platform:" QPNP_BMS_DEV_NAME); |